オープンソース・ソフトウェアの開発とダウンロード

Subversion リポジトリの参照

Annotation of /trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3752 - (hide annotations) (download) (as text)
Fri Jun 11 05:46:17 2010 UTC (13 years, 11 months ago) by kumaneko
Original Path: branches/ccs-patch/security/ccsecurity/policy_io.c
File MIME type: text/x-csrc
File size: 74791 byte(s)
Add a new keyword "any" for domain transition control.
1 kumaneko 2863 /*
2 kumaneko 2864 * security/ccsecurity/policy_io.c
3 kumaneko 2863 *
4 kumaneko 3441 * Copyright (C) 2005-2010 NTT DATA CORPORATION
5 kumaneko 2863 *
6 kumaneko 3731 * Version: 1.7.2+ 2010/06/04
7 kumaneko 2863 *
8     * This file is applicable to both 2.4.30 and 2.6.11 and later.
9     * See README.ccs for ChangeLog.
10     *
11     */
12    
13     #include "internal.h"
14    
15 kumaneko 2962 static struct ccs_profile ccs_default_profile = {
16     .learning = &ccs_default_profile.preference,
17     .permissive = &ccs_default_profile.preference,
18     .enforcing = &ccs_default_profile.preference,
19 kumaneko 3069 .audit = &ccs_default_profile.preference,
20 kumaneko 2962 #ifdef CONFIG_CCSECURITY_AUDIT
21     .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
22     .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
23     #endif
24 kumaneko 3064 .preference.audit_task_info = true,
25     .preference.audit_path_info = true,
26 kumaneko 2962 .preference.enforcing_penalty = 0,
27     .preference.enforcing_verbose = true,
28     .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
29     .preference.learning_verbose = false,
30     .preference.learning_exec_realpath = true,
31     .preference.learning_exec_argv0 = true,
32     .preference.learning_symlink_target = true,
33     .preference.permissive_verbose = true
34     };
35    
36 kumaneko 3158 /* Profile version. Currently only 20090903 is defined. */
37     static unsigned int ccs_profile_version;
38    
39 kumaneko 2958 /* Profile table. Memory is allocated as needed. */
40     static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
42 kumaneko 2948 /* String table for functionality that takes 4 modes. */
43 kumaneko 3748 static const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {
44     [CCS_CONFIG_DISABLED] = "disabled",
45     [CCS_CONFIG_LEARNING] = "learning",
46     [CCS_CONFIG_PERMISSIVE] = "permissive",
47     [CCS_CONFIG_ENFORCING] = "enforcing"
48 kumaneko 2863 };
49 kumaneko 2915
50 kumaneko 2948 /* String table for /proc/ccs/profile */
51 kumaneko 2918 static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +
52 kumaneko 2943 CCS_MAX_CAPABILITY_INDEX +
53     CCS_MAX_MAC_CATEGORY_INDEX] = {
54     [CCS_MAC_FILE_EXECUTE]
55     = "file::execute",
56     [CCS_MAC_FILE_OPEN]
57     = "file::open",
58     [CCS_MAC_FILE_CREATE]
59     = "file::create",
60     [CCS_MAC_FILE_UNLINK]
61     = "file::unlink",
62     [CCS_MAC_FILE_MKDIR]
63     = "file::mkdir",
64     [CCS_MAC_FILE_RMDIR]
65     = "file::rmdir",
66     [CCS_MAC_FILE_MKFIFO]
67     = "file::mkfifo",
68     [CCS_MAC_FILE_MKSOCK]
69     = "file::mksock",
70     [CCS_MAC_FILE_TRUNCATE]
71     = "file::truncate",
72     [CCS_MAC_FILE_SYMLINK]
73     = "file::symlink",
74     [CCS_MAC_FILE_REWRITE]
75     = "file::rewrite",
76     [CCS_MAC_FILE_MKBLOCK]
77     = "file::mkblock",
78     [CCS_MAC_FILE_MKCHAR]
79     = "file::mkchar",
80     [CCS_MAC_FILE_LINK]
81     = "file::link",
82     [CCS_MAC_FILE_RENAME]
83     = "file::rename",
84     [CCS_MAC_FILE_CHMOD]
85     = "file::chmod",
86     [CCS_MAC_FILE_CHOWN]
87     = "file::chown",
88     [CCS_MAC_FILE_CHGRP]
89     = "file::chgrp",
90     [CCS_MAC_FILE_IOCTL]
91     = "file::ioctl",
92     [CCS_MAC_FILE_CHROOT]
93     = "file::chroot",
94     [CCS_MAC_FILE_MOUNT]
95     = "file::mount",
96     [CCS_MAC_FILE_UMOUNT]
97     = "file::umount",
98     [CCS_MAC_FILE_PIVOT_ROOT]
99     = "file::pivot_root",
100 kumaneko 3484 [CCS_MAC_FILE_TRANSIT]
101     = "file::transit",
102 kumaneko 2943 [CCS_MAC_ENVIRON]
103     = "misc::env",
104     [CCS_MAC_NETWORK_UDP_BIND]
105     = "network::inet_udp_bind",
106     [CCS_MAC_NETWORK_UDP_CONNECT]
107     = "network::inet_udp_connect",
108     [CCS_MAC_NETWORK_TCP_BIND]
109     = "network::inet_tcp_bind",
110     [CCS_MAC_NETWORK_TCP_LISTEN]
111     = "network::inet_tcp_listen",
112     [CCS_MAC_NETWORK_TCP_CONNECT]
113     = "network::inet_tcp_connect",
114     [CCS_MAC_NETWORK_TCP_ACCEPT]
115     = "network::inet_tcp_accept",
116     [CCS_MAC_NETWORK_RAW_BIND]
117     = "network::inet_raw_bind",
118     [CCS_MAC_NETWORK_RAW_CONNECT]
119     = "network::inet_raw_connect",
120     [CCS_MAC_SIGNAL]
121     = "ipc::signal",
122 kumaneko 2918 [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]
123 kumaneko 2943 = "capability::inet_tcp_create",
124 kumaneko 2918 [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]
125 kumaneko 2943 = "capability::inet_tcp_listen",
126 kumaneko 2918 [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]
127 kumaneko 2943 = "capability::inet_tcp_connect",
128     [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]
129     = "capability::use_inet_udp",
130     [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]
131     = "capability::use_inet_ip",
132     [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
133     = "capability::use_route",
134     [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
135     = "capability::use_packet",
136     [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]
137     = "capability::SYS_MOUNT",
138     [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]
139     = "capability::SYS_UMOUNT",
140     [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
141     = "capability::SYS_REBOOT",
142     [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]
143     = "capability::SYS_CHROOT",
144     [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]
145     = "capability::SYS_KILL",
146     [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
147     = "capability::SYS_VHANGUP",
148     [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
149     = "capability::SYS_TIME",
150     [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]
151     = "capability::SYS_NICE",
152     [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]
153     = "capability::SYS_SETHOSTNAME",
154     [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
155     = "capability::use_kernel_module",
156     [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]
157     = "capability::create_fifo",
158     [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]
159     = "capability::create_block_dev",
160     [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]
161     = "capability::create_char_dev",
162     [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]
163     = "capability::create_unix_socket",
164     [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]
165     = "capability::SYS_LINK",
166     [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]
167     = "capability::SYS_SYMLINK",
168     [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]
169     = "capability::SYS_RENAME",
170     [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]
171     = "capability::SYS_UNLINK",
172     [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]
173     = "capability::SYS_CHMOD",
174     [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]
175     = "capability::SYS_CHOWN",
176     [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]
177     = "capability::SYS_IOCTL",
178     [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
179     = "capability::SYS_KEXEC_LOAD",
180     [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]
181     = "capability::SYS_PIVOT_ROOT",
182     [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
183     = "capability::SYS_PTRACE",
184     [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]
185     = "capability::conceal_mount",
186     [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
187     + CCS_MAC_CATEGORY_FILE] = "file",
188     [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
189     + CCS_MAC_CATEGORY_NETWORK] = "network",
190     [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
191     + CCS_MAC_CATEGORY_MISC] = "misc",
192     [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
193     + CCS_MAC_CATEGORY_IPC] = "ipc",
194     [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
195     + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
196 kumaneko 2915 };
197    
198 kumaneko 2863 /* Permit policy management by non-root user? */
199     static bool ccs_manage_by_non_root;
200    
201     /**
202 kumaneko 2918 * ccs_cap2keyword - Convert capability operation to capability name.
203     *
204     * @operation: The capability index.
205     *
206     * Returns the name of the specified capability's name.
207     */
208     const char *ccs_cap2keyword(const u8 operation)
209     {
210     return operation < CCS_MAX_CAPABILITY_INDEX
211 kumaneko 2943 ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;
212 kumaneko 2918 }
213    
214     /**
215 kumaneko 2958 * ccs_yesno - Return "yes" or "no".
216 kumaneko 2863 *
217 kumaneko 2958 * @value: Bool value.
218 kumaneko 2863 */
219 kumaneko 2958 static const char *ccs_yesno(const unsigned int value)
220 kumaneko 2863 {
221 kumaneko 2958 return value ? "yes" : "no";
222 kumaneko 2863 }
223    
224     /**
225     * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
226     *
227     * @head: Pointer to "struct ccs_io_buffer".
228     * @fmt: The printf()'s format string, followed by parameters.
229     *
230     * Returns true on success, false otherwise.
231     *
232     * The snprintf() will truncate, but ccs_io_printf() won't.
233     */
234     bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
235     {
236     va_list args;
237     int len;
238     int pos = head->read_avail;
239     int size = head->readbuf_size - pos;
240     if (size <= 0)
241     return false;
242     va_start(args, fmt);
243     len = vsnprintf(head->read_buf + pos, size, fmt, args);
244     va_end(args);
245     if (pos + len >= head->readbuf_size)
246     return false;
247     head->read_avail += len;
248     return true;
249     }
250    
251     /**
252 kumaneko 3694 * ccs_assign_profile - Create a new profile.
253 kumaneko 2863 *
254     * @profile: Profile number to create.
255     *
256     * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
257     */
258 kumaneko 3694 static struct ccs_profile *ccs_assign_profile(const unsigned int profile)
259 kumaneko 2863 {
260     struct ccs_profile *ptr;
261     struct ccs_profile *entry;
262 kumaneko 2892 if (profile >= CCS_MAX_PROFILES)
263 kumaneko 2863 return NULL;
264     ptr = ccs_profile_ptr[profile];
265     if (ptr)
266     return ptr;
267 kumaneko 3512 entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
268 kumaneko 3534 if (mutex_lock_interruptible(&ccs_policy_lock))
269     goto out;
270 kumaneko 2863 ptr = ccs_profile_ptr[profile];
271     if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
272     ptr = entry;
273 kumaneko 2962 ptr->audit = &ccs_default_profile.preference;
274     ptr->learning = &ccs_default_profile.preference;
275     ptr->permissive = &ccs_default_profile.preference;
276     ptr->enforcing = &ccs_default_profile.preference;
277 kumaneko 2958 ptr->default_config = CCS_CONFIG_DISABLED |
278     CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
279     memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
280 kumaneko 2943 sizeof(ptr->config));
281 kumaneko 2863 mb(); /* Avoid out-of-order execution. */
282     ccs_profile_ptr[profile] = ptr;
283     entry = NULL;
284     }
285     mutex_unlock(&ccs_policy_lock);
286 kumaneko 3534 out:
287 kumaneko 2863 kfree(entry);
288     return ptr;
289     }
290    
291     /**
292 kumaneko 2991 * ccs_check_profile - Check all profiles currently assigned to domains are defined.
293     */
294 kumaneko 3502 static void ccs_check_profile(void)
295 kumaneko 2991 {
296     struct ccs_domain_info *domain;
297 kumaneko 3535 const int idx = ccs_read_lock();
298 kumaneko 2991 ccs_policy_loaded = true;
299     list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
300     const u8 profile = domain->profile;
301     if (ccs_profile_ptr[profile])
302     continue;
303     panic("Profile %u (used by '%s') not defined.\n",
304     profile, domain->domainname->name);
305     }
306 kumaneko 3535 ccs_read_unlock(idx);
307 kumaneko 3158 if (ccs_profile_version != 20090903)
308     panic("Profile version %u is not supported.\n",
309     ccs_profile_version);
310 kumaneko 3731 printk(KERN_INFO "CCSecurity: 1.7.2+ 2010/06/04\n");
311 kumaneko 3502 printk(KERN_INFO "Mandatory Access Control activated.\n");
312 kumaneko 2991 }
313    
314     /**
315 kumaneko 2958 * ccs_profile - Find a profile.
316     *
317     * @profile: Profile number to find.
318     *
319 kumaneko 2991 * Returns pointer to "struct ccs_profile".
320 kumaneko 2958 */
321     struct ccs_profile *ccs_profile(const u8 profile)
322     {
323 kumaneko 2974 struct ccs_profile *ptr = ccs_profile_ptr[profile];
324 kumaneko 2962 if (!ccs_policy_loaded)
325     return &ccs_default_profile;
326 kumaneko 2974 BUG_ON(!ptr);
327     return ptr;
328 kumaneko 2958 }
329    
330 kumaneko 3747 static s8 ccs_find_yesno(const char *string, const char *find)
331 kumaneko 2863 {
332 kumaneko 3747 const char *cp = strstr(string, find);
333     if (cp) {
334     cp += strlen(find);
335     if (strncmp(cp, "=yes", 4))
336     return 1;
337     else if (strncmp(cp, "=no", 3))
338     return 0;
339 kumaneko 2863 }
340 kumaneko 3747 return -1;
341     }
342    
343     static void ccs_set_bool(bool *b, const char *string, const char *find)
344     {
345     switch (ccs_find_yesno(string, find)) {
346     case 1:
347     *b = true;
348     break;
349     case 0:
350     *b = false;
351     break;
352     }
353     }
354    
355     static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
356     {
357     const char *cp = strstr(string, find);
358     if (cp)
359     sscanf(cp + strlen(find), "=%u", i);
360     }
361    
362     static void ccs_set_pref(const char *cp, const char *data,
363     struct ccs_profile *profile, const bool use_default)
364     {
365     struct ccs_preference **pref;
366     bool *verbose;
367     if (!strcmp(data, "audit")) {
368 kumaneko 2962 if (use_default) {
369 kumaneko 3747 pref = &profile->audit;
370     goto set_default;
371 kumaneko 2962 }
372     profile->audit = &profile->preference;
373 kumaneko 3069 #ifdef CONFIG_CCSECURITY_AUDIT
374 kumaneko 3747 ccs_set_uint(&profile->preference.audit_max_grant_log, cp,
375     "max_grant_log");
376     ccs_set_uint(&profile->preference.audit_max_reject_log, cp,
377     "max_reject_log");
378 kumaneko 3069 #endif
379 kumaneko 3747 ccs_set_bool(&profile->preference.audit_task_info, cp,
380     "task_info");
381     ccs_set_bool(&profile->preference.audit_path_info, cp,
382     "path_info");
383     return;
384 kumaneko 2863 }
385 kumaneko 3747 if (!strcmp(data, "enforcing")) {
386 kumaneko 2962 if (use_default) {
387 kumaneko 3747 pref = &profile->enforcing;
388     goto set_default;
389 kumaneko 2962 }
390     profile->enforcing = &profile->preference;
391 kumaneko 3747 ccs_set_uint(&profile->preference.enforcing_penalty, cp,
392     "penalty");
393     verbose = &profile->preference.enforcing_verbose;
394     goto set_verbose;
395 kumaneko 2958 }
396 kumaneko 3747 if (!strcmp(data, "permissive")) {
397 kumaneko 2962 if (use_default) {
398 kumaneko 3747 pref = &profile->permissive;
399     goto set_default;
400 kumaneko 2962 }
401     profile->permissive = &profile->preference;
402 kumaneko 3747 verbose = &profile->preference.permissive_verbose;
403     goto set_verbose;
404 kumaneko 2958 }
405 kumaneko 3747 if (!strcmp(data, "learning")) {
406 kumaneko 2962 if (use_default) {
407 kumaneko 3747 pref = &profile->learning;
408     goto set_default;
409 kumaneko 2962 }
410     profile->learning = &profile->preference;
411 kumaneko 3747 ccs_set_uint(&profile->preference.learning_max_entry, cp,
412     "max_entry");
413     ccs_set_bool(&profile->preference.learning_exec_realpath, cp,
414     "exec.realpath");
415     ccs_set_bool(&profile->preference.learning_exec_argv0, cp,
416     "exec.argv0");
417     ccs_set_bool(&profile->preference.learning_symlink_target, cp,
418     "symlink.target");
419     verbose = &profile->preference.learning_verbose;
420     goto set_verbose;
421 kumaneko 2958 }
422 kumaneko 3747 return;
423     set_default:
424     *pref = &ccs_default_profile.preference;
425     return;
426     set_verbose:
427     ccs_set_bool(verbose, cp, "verbose");
428     }
429    
430     static int ccs_set_mode(const char *cp, char *data,
431     struct ccs_profile *profile, const bool use_default)
432     {
433     u8 i;
434     u8 config;
435 kumaneko 2958 if (!strcmp(data, "CONFIG")) {
436     i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
437     + CCS_MAX_MAC_CATEGORY_INDEX;
438     config = profile->default_config;
439     } else if (ccs_str_starts(&data, "CONFIG::")) {
440     config = 0;
441 kumaneko 2943 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
442     + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
443     if (strcmp(data, ccs_mac_keywords[i]))
444     continue;
445 kumaneko 2958 config = profile->config[i];
446 kumaneko 2943 break;
447     }
448 kumaneko 2958 if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
449     + CCS_MAX_MAC_CATEGORY_INDEX)
450     return -EINVAL;
451     } else {
452     return -EINVAL;
453     }
454 kumaneko 2962 if (use_default) {
455 kumaneko 2958 config = CCS_CONFIG_USE_DEFAULT;
456     } else {
457 kumaneko 3747 u8 mode;
458 kumaneko 3748 for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode--)
459     if (strstr(cp, ccs_mode[mode]))
460 kumaneko 2958 /*
461     * Update lower 3 bits in order to distinguish
462     * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
463     */
464     config = (config & ~7) | mode;
465 kumaneko 2959 #ifdef CONFIG_CCSECURITY_AUDIT
466 kumaneko 2958 if (config != CCS_CONFIG_USE_DEFAULT) {
467 kumaneko 3747 switch (ccs_find_yesno(cp, "grant_log")) {
468     case 1:
469 kumaneko 2958 config |= CCS_CONFIG_WANT_GRANT_LOG;
470 kumaneko 3747 break;
471     case 0:
472 kumaneko 2958 config &= ~CCS_CONFIG_WANT_GRANT_LOG;
473 kumaneko 3747 break;
474     }
475     switch (ccs_find_yesno(cp, "reject_log")) {
476     case 1:
477 kumaneko 2958 config |= CCS_CONFIG_WANT_REJECT_LOG;
478 kumaneko 3747 break;
479     case 0:
480 kumaneko 2958 config &= ~CCS_CONFIG_WANT_REJECT_LOG;
481 kumaneko 3747 break;
482     }
483 kumaneko 2958 }
484 kumaneko 2959 #endif
485 kumaneko 2958 }
486     if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
487     + CCS_MAX_MAC_CATEGORY_INDEX)
488     profile->config[i] = config;
489     else if (config != CCS_CONFIG_USE_DEFAULT)
490     profile->default_config = config;
491 kumaneko 2943 return 0;
492 kumaneko 2863 }
493    
494 kumaneko 3747 /**
495     * ccs_write_profile - Write profile table.
496     *
497     * @head: Pointer to "struct ccs_io_buffer".
498     *
499     * Returns 0 on success, negative value otherwise.
500     */
501     static int ccs_write_profile(struct ccs_io_buffer *head)
502     {
503     char *data = head->write_buf;
504     bool use_default = false;
505     char *cp;
506     int i;
507     struct ccs_profile *profile;
508     if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
509     return 0;
510     i = simple_strtoul(data, &cp, 10);
511     if (data == cp) {
512     profile = &ccs_default_profile;
513     } else {
514     if (*cp != '-')
515     return -EINVAL;
516     data = cp + 1;
517     profile = ccs_assign_profile(i);
518     if (!profile)
519     return -EINVAL;
520     }
521     cp = strchr(data, '=');
522     if (!cp)
523     return -EINVAL;
524     *cp++ = '\0';
525     if (profile != &ccs_default_profile)
526     use_default = strstr(cp, "use_default") != NULL;
527     if (ccs_str_starts(&data, "PREFERENCE::")) {
528     ccs_set_pref(cp, data, profile, use_default);
529     return 0;
530     }
531     if (profile == &ccs_default_profile)
532     return -EINVAL;
533     if (!strcmp(data, "COMMENT")) {
534     const struct ccs_path_info *old_comment = profile->comment;
535     profile->comment = ccs_get_name(cp);
536     ccs_put_name(old_comment);
537     return 0;
538     }
539     return ccs_set_mode(cp, data, profile, use_default);
540     }
541    
542 kumaneko 3691 static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)
543     {
544     struct ccs_preference *pref = &ccs_default_profile.preference;
545     const struct ccs_profile *profile = idx >= 0 ?
546     ccs_profile_ptr[idx] : NULL;
547     char buffer[16] = "";
548     if (profile) {
549     buffer[sizeof(buffer) - 1] = '\0';
550     snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
551     }
552     if (profile) {
553     pref = profile->audit;
554     if (pref == &ccs_default_profile.preference)
555     pref = NULL;
556     }
557 kumaneko 3747 if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "
558 kumaneko 3691 #ifdef CONFIG_CCSECURITY_AUDIT
559     "max_grant_log=%u max_reject_log=%u "
560     #endif
561     "task_info=%s path_info=%s }\n", buffer,
562 kumaneko 3747 "audit",
563 kumaneko 3691 #ifdef CONFIG_CCSECURITY_AUDIT
564     pref->audit_max_grant_log,
565     pref->audit_max_reject_log,
566     #endif
567     ccs_yesno(pref->audit_task_info),
568     ccs_yesno(pref->audit_path_info)))
569     return false;
570     if (profile) {
571     pref = profile->learning;
572     if (pref == &ccs_default_profile.preference)
573     pref = NULL;
574     }
575 kumaneko 3747 if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "
576 kumaneko 3691 "verbose=%s max_entry=%u exec.realpath=%s "
577     "exec.argv0=%s symlink.target=%s }\n",
578 kumaneko 3747 buffer, "learning",
579 kumaneko 3691 ccs_yesno(pref->learning_verbose),
580     pref->learning_max_entry,
581     ccs_yesno(pref->learning_exec_realpath),
582     ccs_yesno(pref->learning_exec_argv0),
583     ccs_yesno(pref->learning_symlink_target)))
584     return false;
585     if (profile) {
586     pref = profile->permissive;
587     if (pref == &ccs_default_profile.preference)
588     pref = NULL;
589     }
590 kumaneko 3747 if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
591     buffer, "permissive",
592 kumaneko 3691 ccs_yesno(pref->permissive_verbose)))
593     return false;
594     if (profile) {
595     pref = profile->enforcing;
596     if (pref == &ccs_default_profile.preference)
597     pref = NULL;
598     }
599 kumaneko 3747 return !pref || ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
600     "penalty=%u }\n", buffer, "enforcing",
601 kumaneko 3691 ccs_yesno(pref->enforcing_verbose),
602     pref->enforcing_penalty);
603     }
604    
605 kumaneko 2863 /**
606     * ccs_read_profile - Read profile table.
607     *
608     * @head: Pointer to "struct ccs_io_buffer".
609     */
610 kumaneko 2943 static void ccs_read_profile(struct ccs_io_buffer *head)
611 kumaneko 2863 {
612 kumaneko 2958 int index;
613 kumaneko 2863 if (head->read_eof)
614 kumaneko 2943 return;
615 kumaneko 2965 if (head->read_bit)
616     goto body;
617 kumaneko 2976 ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
618 kumaneko 3691 ccs_print_preference(head, -1);
619 kumaneko 2965 head->read_bit = 1;
620     body:
621 kumaneko 2958 for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {
622     bool done;
623     u8 config;
624     int i;
625 kumaneko 2965 int pos;
626 kumaneko 2958 const struct ccs_profile *profile = ccs_profile_ptr[index];
627 kumaneko 3131 const struct ccs_path_info *comment;
628 kumaneko 2958 head->read_step = index;
629     if (!profile)
630 kumaneko 2863 continue;
631 kumaneko 2965 pos = head->read_avail;
632 kumaneko 3131 comment = profile->comment;
633 kumaneko 2958 done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,
634 kumaneko 3131 comment ? comment->name : "");
635 kumaneko 2958 if (!done)
636     goto out;
637     config = profile->default_config;
638 kumaneko 2959 #ifdef CONFIG_CCSECURITY_AUDIT
639 kumaneko 3691 if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
640     "grant_log=%s reject_log=%s }\n", index,
641 kumaneko 3748 "CONFIG", "", ccs_mode[config & 3],
642 kumaneko 2958 ccs_yesno(config &
643     CCS_CONFIG_WANT_GRANT_LOG),
644     ccs_yesno(config &
645     CCS_CONFIG_WANT_REJECT_LOG)))
646     goto out;
647 kumaneko 2959 #else
648 kumaneko 3691 if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,
649 kumaneko 3748 "CONFIG", "", ccs_mode[config & 3]))
650 kumaneko 2959 goto out;
651     #endif
652 kumaneko 2958 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
653     + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
654 kumaneko 2959 #ifdef CONFIG_CCSECURITY_AUDIT
655 kumaneko 2958 const char *g;
656     const char *r;
657 kumaneko 2959 #endif
658 kumaneko 2958 config = profile->config[i];
659     if (config == CCS_CONFIG_USE_DEFAULT)
660     continue;
661 kumaneko 2959 #ifdef CONFIG_CCSECURITY_AUDIT
662 kumaneko 2958 g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);
663     r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);
664 kumaneko 3691 if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
665 kumaneko 2958 "grant_log=%s reject_log=%s }\n",
666 kumaneko 3691 index, "CONFIG::",
667     ccs_mac_keywords[i],
668 kumaneko 3748 ccs_mode[config & 3], g, r))
669 kumaneko 2958 goto out;
670 kumaneko 2959 #else
671 kumaneko 3691 if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",
672     index, "CONFIG::",
673     ccs_mac_keywords[i],
674 kumaneko 3748 ccs_mode[config & 3]))
675 kumaneko 2959 goto out;
676     #endif
677 kumaneko 2908 }
678 kumaneko 3691 if (!ccs_print_preference(head, index))
679 kumaneko 2959 goto out;
680 kumaneko 2958 continue;
681     out:
682     head->read_avail = pos;
683     break;
684 kumaneko 2863 }
685 kumaneko 2958 if (index == CCS_MAX_PROFILES)
686 kumaneko 2863 head->read_eof = true;
687     }
688    
689 kumaneko 3693 static bool ccs_same_manager_entry(const struct ccs_acl_head *a,
690 kumaneko 3695 const struct ccs_acl_head *b)
691 kumaneko 3689 {
692 kumaneko 3693 return container_of(a, struct ccs_manager, head)->manager
693     == container_of(b, struct ccs_manager, head)->manager;
694 kumaneko 3689 }
695 kumaneko 2863
696     /**
697     * ccs_update_manager_entry - Add a manager entry.
698     *
699     * @manager: The path to manager or the domainnamme.
700     * @is_delete: True if it is a delete request.
701     *
702     * Returns 0 on success, negative value otherwise.
703     */
704     static int ccs_update_manager_entry(const char *manager, const bool is_delete)
705     {
706 kumaneko 3693 struct ccs_manager e = { };
707 kumaneko 2863 int error = is_delete ? -ENOENT : -ENOMEM;
708 kumaneko 3693 if (ccs_domain_def(manager)) {
709     if (!ccs_correct_domain(manager))
710 kumaneko 2863 return -EINVAL;
711 kumaneko 2900 e.is_domain = true;
712 kumaneko 2863 } else {
713 kumaneko 3707 if (!ccs_correct_path(manager))
714 kumaneko 2863 return -EINVAL;
715     }
716 kumaneko 2900 e.manager = ccs_get_name(manager);
717     if (!e.manager)
718 kumaneko 3689 return error;
719     error = ccs_update_policy(&e.head, sizeof(e), is_delete,
720 kumaneko 3693 CCS_ID_MANAGER, ccs_same_manager_entry);
721 kumaneko 2900 ccs_put_name(e.manager);
722 kumaneko 2863 return error;
723     }
724    
725     /**
726 kumaneko 3693 * ccs_write_manager - Write manager policy.
727 kumaneko 2863 *
728     * @head: Pointer to "struct ccs_io_buffer".
729     *
730     * Returns 0 on success, negative value otherwise.
731     */
732 kumaneko 3693 static int ccs_write_manager(struct ccs_io_buffer *head)
733 kumaneko 2863 {
734     char *data = head->write_buf;
735 kumaneko 2892 bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
736 kumaneko 2863 if (!strcmp(data, "manage_by_non_root")) {
737     ccs_manage_by_non_root = !is_delete;
738     return 0;
739     }
740     return ccs_update_manager_entry(data, is_delete);
741     }
742    
743     /**
744 kumaneko 3693 * ccs_read_manager - Read manager policy.
745 kumaneko 2863 *
746     * @head: Pointer to "struct ccs_io_buffer".
747     *
748     * Caller holds ccs_read_lock().
749     */
750 kumaneko 3693 static void ccs_read_manager(struct ccs_io_buffer *head)
751 kumaneko 2863 {
752     struct list_head *pos;
753     if (head->read_eof)
754 kumaneko 2943 return;
755 kumaneko 3689 list_for_each_cookie(pos, head->read_var2,
756     &ccs_policy_list[CCS_ID_MANAGER]) {
757 kumaneko 3693 struct ccs_manager *ptr
758     = list_entry(pos, typeof(*ptr), head.list);
759 kumaneko 3689 if (ptr->head.is_deleted)
760 kumaneko 2863 continue;
761     if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
762 kumaneko 2943 return;
763 kumaneko 2863 }
764     head->read_eof = true;
765     }
766    
767     /**
768 kumaneko 3693 * ccs_manager - Check whether the current process is a policy manager.
769 kumaneko 2863 *
770     * Returns true if the current process is permitted to modify policy
771     * via /proc/ccs/ interface.
772     *
773     * Caller holds ccs_read_lock().
774     */
775 kumaneko 3693 static bool ccs_manager(void)
776 kumaneko 2863 {
777 kumaneko 3693 struct ccs_manager *ptr;
778 kumaneko 2863 const char *exe;
779     struct task_struct *task = current;
780     const struct ccs_path_info *domainname
781     = ccs_current_domain()->domainname;
782     bool found = false;
783     if (!ccs_policy_loaded)
784     return true;
785 kumaneko 3693 if (task->ccs_flags & CCS_TASK_IS_MANAGER)
786 kumaneko 2863 return true;
787     if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
788     return false;
789     exe = ccs_get_exe();
790 kumaneko 3689 list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
791     head.list) {
792 kumaneko 3697 if (ptr->head.is_deleted)
793     continue;
794     if (ptr->is_domain) {
795     if (ccs_pathcmp(domainname, ptr->manager))
796     continue;
797     } else {
798     if (!exe || strcmp(exe, ptr->manager->name))
799     continue;
800 kumaneko 2863 }
801 kumaneko 3697 /* Set manager flag. */
802     task->ccs_flags |= CCS_TASK_IS_MANAGER;
803     found = true;
804     break;
805 kumaneko 2863 }
806     if (!found) { /* Reduce error messages. */
807     static pid_t ccs_last_pid;
808     const pid_t pid = current->pid;
809     if (ccs_last_pid != pid) {
810     printk(KERN_WARNING "%s ( %s ) is not permitted to "
811     "update policies.\n", domainname->name, exe);
812     ccs_last_pid = pid;
813     }
814     }
815     kfree(exe);
816     return found;
817     }
818    
819     /**
820     * ccs_find_condition_part - Find condition part from the statement.
821     *
822     * @data: String to parse.
823     *
824     * Returns pointer to the condition part if it was found in the statement,
825     * NULL otherwise.
826     */
827 kumaneko 3697 static char *ccs_find_condition_part(char *data)
828 kumaneko 2863 {
829     char *cp = strstr(data, " if ");
830 kumaneko 3696 if (!cp)
831     cp = strstr(data, " ; set ");
832     if (cp)
833 kumaneko 2863 *cp++ = '\0';
834     return cp;
835     }
836    
837     /**
838 kumaneko 3693 * ccs_select_one - Parse select command.
839 kumaneko 2863 *
840     * @head: Pointer to "struct ccs_io_buffer".
841     * @data: String to parse.
842     *
843     * Returns true on success, false otherwise.
844     *
845     * Caller holds ccs_read_lock().
846     */
847 kumaneko 3693 static bool ccs_select_one(struct ccs_io_buffer *head, const char *data)
848 kumaneko 2863 {
849     unsigned int pid;
850     struct ccs_domain_info *domain = NULL;
851 kumaneko 2970 bool global_pid = false;
852 kumaneko 2863 if (!strcmp(data, "allow_execute")) {
853     head->read_execute_only = true;
854     return true;
855     }
856 kumaneko 2970 if (sscanf(data, "pid=%u", &pid) == 1 ||
857     (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
858 kumaneko 2863 struct task_struct *p;
859 kumaneko 3248 ccs_tasklist_lock();
860 kumaneko 2970 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
861     if (global_pid)
862 kumaneko 3502 p = ccsecurity_exports.find_task_by_pid_ns(pid,
863     &init_pid_ns);
864 kumaneko 2970 else
865 kumaneko 3502 p = ccsecurity_exports.find_task_by_vpid(pid);
866 kumaneko 2970 #else
867 kumaneko 2863 p = find_task_by_pid(pid);
868 kumaneko 2970 #endif
869 kumaneko 2863 if (p)
870     domain = ccs_task_domain(p);
871 kumaneko 3248 ccs_tasklist_unlock();
872 kumaneko 2863 } else if (!strncmp(data, "domain=", 7)) {
873 kumaneko 3693 if (ccs_domain_def(data + 7))
874 kumaneko 2863 domain = ccs_find_domain(data + 7);
875     } else
876     return false;
877     head->write_var1 = domain;
878     /* Accessing read_buf is safe because head->io_sem is held. */
879     if (!head->read_buf)
880     return true; /* Do nothing if open(O_WRONLY). */
881     head->read_avail = 0;
882     ccs_io_printf(head, "# select %s\n", data);
883     head->read_single_domain = true;
884     head->read_eof = !domain;
885     if (domain) {
886     struct ccs_domain_info *d;
887     head->read_var1 = NULL;
888     list_for_each_entry_rcu(d, &ccs_domain_list, list) {
889     if (d == domain)
890     break;
891     head->read_var1 = &d->list;
892     }
893     head->read_var2 = NULL;
894     head->read_bit = 0;
895     head->read_step = 0;
896     if (domain->is_deleted)
897     ccs_io_printf(head, "# This is a deleted domain.\n");
898     }
899     return true;
900     }
901    
902 kumaneko 3693 static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
903 kumaneko 3746 const bool is_delete)
904 kumaneko 2897 {
905 kumaneko 3692 static const struct {
906     const char *keyword;
907     int (*write) (char *, struct ccs_domain_info *,
908     struct ccs_condition *, const bool);
909     } ccs_callback[5] = {
910 kumaneko 3693 { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
911     { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
912     { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
913     { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
914     { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
915 kumaneko 3692 };
916 kumaneko 3693 int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
917     const bool) = ccs_write_file;
918 kumaneko 3746 int error;
919     u8 i;
920     struct ccs_condition *cond = NULL;
921     char *cp = ccs_find_condition_part(data);
922     if (cp) {
923     cond = ccs_get_condition(cp);
924     if (!cond)
925     return -EINVAL;
926     }
927 kumaneko 3692 for (i = 0; i < 5; i++) {
928     if (!ccs_str_starts(&data, ccs_callback[i].keyword))
929     continue;
930     write = ccs_callback[i].write;
931     break;
932     }
933 kumaneko 3746 error = write(data, domain, cond, is_delete);
934     if (cond)
935     ccs_put_condition(cond);
936     return error;
937 kumaneko 2897 }
938    
939 kumaneko 3747 static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
940     [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
941     [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",
942     [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]
943     = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",
944     [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]
945     = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",
946     [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
947     };
948    
949 kumaneko 2863 /**
950 kumaneko 3693 * ccs_write_domain - Write domain policy.
951 kumaneko 2863 *
952     * @head: Pointer to "struct ccs_io_buffer".
953     *
954     * Returns 0 on success, negative value otherwise.
955     */
956 kumaneko 3693 static int ccs_write_domain(struct ccs_io_buffer *head)
957 kumaneko 2863 {
958     char *data = head->write_buf;
959     struct ccs_domain_info *domain = head->write_var1;
960     bool is_delete = false;
961     bool is_select = false;
962     unsigned int profile;
963 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
964 kumaneko 2863 is_delete = true;
965 kumaneko 2892 else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
966 kumaneko 2863 is_select = true;
967 kumaneko 3693 if (is_select && ccs_select_one(head, data))
968 kumaneko 2863 return 0;
969     /* Don't allow updating policies by non manager programs. */
970 kumaneko 3693 if (!ccs_manager())
971 kumaneko 2863 return -EPERM;
972 kumaneko 3693 if (ccs_domain_def(data)) {
973 kumaneko 2863 domain = NULL;
974     if (is_delete)
975     ccs_delete_domain(data);
976     else if (is_select)
977     domain = ccs_find_domain(data);
978     else
979 kumaneko 3694 domain = ccs_assign_domain(data, 0);
980 kumaneko 2863 head->write_var1 = domain;
981     return 0;
982     }
983     if (!domain)
984     return -EINVAL;
985    
986 kumaneko 2892 if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
987     && profile < CCS_MAX_PROFILES) {
988 kumaneko 2991 if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
989 kumaneko 2863 domain->profile = (u8) profile;
990     return 0;
991     }
992 kumaneko 3747 for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
993     const char *cp = ccs_dif[profile];
994     if (strncmp(data, cp, strlen(cp) - 1))
995     continue;
996     domain->flags[profile] = !is_delete;
997 kumaneko 3702 return 0;
998     }
999 kumaneko 3746 return ccs_write_domain2(data, domain, is_delete);
1000 kumaneko 2863 }
1001    
1002 kumaneko 2948 /**
1003     * ccs_print_name_union - Print a ccs_name_union.
1004     *
1005     * @head: Pointer to "struct ccs_io_buffer".
1006     * @ptr: Pointer to "struct ccs_name_union".
1007     *
1008     * Returns true on success, false otherwise.
1009     */
1010 kumaneko 2888 static bool ccs_print_name_union(struct ccs_io_buffer *head,
1011 kumaneko 2894 const struct ccs_name_union *ptr)
1012 kumaneko 2863 {
1013 kumaneko 2894 int pos = head->read_avail;
1014 kumaneko 2863 if (pos && head->read_buf[pos - 1] == ' ')
1015     head->read_avail--;
1016 kumaneko 2888 if (ptr->is_group)
1017 kumaneko 2870 return ccs_io_printf(head, " @%s",
1018 kumaneko 2888 ptr->group->group_name->name);
1019     return ccs_io_printf(head, " %s", ptr->filename->name);
1020 kumaneko 2863 }
1021    
1022 kumaneko 2948 /**
1023     * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.
1024     *
1025     * @head: Pointer to "struct ccs_io_buffer".
1026     * @ptr: Pointer to "struct ccs_name_union".
1027     *
1028     * Returns true on success, false otherwise.
1029     */
1030 kumaneko 2894 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1031     const struct ccs_name_union *ptr)
1032 kumaneko 2863 {
1033 kumaneko 2894 if (ptr->is_group)
1034     return ccs_io_printf(head, "@%s",
1035     ptr->group->group_name->name);
1036     return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1037     }
1038    
1039 kumaneko 3690 static void ccs_print_number(char *buffer, int buffer_len,
1040     const struct ccs_number_union *ptr)
1041     {
1042     int i;
1043     unsigned long min = ptr->values[0];
1044     const unsigned long max = ptr->values[1];
1045     u8 min_type = ptr->value_type[0];
1046     const u8 max_type = ptr->value_type[1];
1047     memset(buffer, 0, buffer_len);
1048     buffer_len -= 2;
1049     for (i = 0; i < 2; i++) {
1050     int len;
1051     switch (min_type) {
1052     case CCS_VALUE_TYPE_HEXADECIMAL:
1053     snprintf(buffer, buffer_len, "0x%lX", min);
1054     break;
1055     case CCS_VALUE_TYPE_OCTAL:
1056     snprintf(buffer, buffer_len, "0%lo", min);
1057     break;
1058     default:
1059     snprintf(buffer, buffer_len, "%lu", min);
1060     break;
1061     }
1062     if (min == max && min_type == max_type)
1063     break;
1064     len = strlen(buffer);
1065     buffer[len++] = '-';
1066     buffer += len;
1067     buffer_len -= len;
1068     min_type = max_type;
1069     min = max;
1070     }
1071     }
1072    
1073 kumaneko 2948 /**
1074     * ccs_print_number_union_common - Print a ccs_number_union.
1075     *
1076     * @head: Pointer to "struct ccs_io_buffer".
1077     * @ptr: Pointer to "struct ccs_number_union".
1078     * @need_space: True if a space character is needed.
1079     *
1080     * Returns true on success, false otherwise.
1081     */
1082 kumaneko 2894 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
1083     const struct ccs_number_union *ptr,
1084     const bool need_space)
1085     {
1086 kumaneko 3690 char buffer[128];
1087 kumaneko 2894 if (need_space && !ccs_io_printf(head, " "))
1088     return false;
1089 kumaneko 2888 if (ptr->is_group)
1090 kumaneko 2894 return ccs_io_printf(head, "@%s",
1091 kumaneko 2888 ptr->group->group_name->name);
1092 kumaneko 3690 ccs_print_number(buffer, sizeof(buffer), ptr);
1093     return ccs_io_printf(head, "%s", buffer);
1094 kumaneko 2863 }
1095    
1096 kumaneko 2948 /**
1097     * ccs_print_number_union - Print a ccs_number_union.
1098     *
1099     * @head: Pointer to "struct ccs_io_buffer".
1100     * @ptr: Pointer to "struct ccs_number_union".
1101     *
1102     * Returns true on success, false otherwise.
1103     */
1104 kumaneko 3695 static bool ccs_print_number_union(struct ccs_io_buffer *head,
1105     const struct ccs_number_union *ptr)
1106 kumaneko 2894 {
1107     return ccs_print_number_union_common(head, ptr, true);
1108     }
1109    
1110 kumaneko 2948 /**
1111     * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.
1112     *
1113     * @head: Pointer to "struct ccs_io_buffer".
1114     * @ptr: Pointer to "struct ccs_number_union".
1115     *
1116     * Returns true on success, false otherwise.
1117     */
1118 kumaneko 2894 static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1119     const struct ccs_number_union *ptr)
1120     {
1121     return ccs_print_number_union_common(head, ptr, false);
1122     }
1123    
1124 kumaneko 2863 /**
1125 kumaneko 2894 * ccs_print_condition - Print condition part.
1126     *
1127     * @head: Pointer to "struct ccs_io_buffer".
1128 kumaneko 3747 * @cond: Pointer to "struct ccs_condition". Maybe NULL.
1129 kumaneko 2894 *
1130     * Returns true on success, false otherwise.
1131     */
1132     static bool ccs_print_condition(struct ccs_io_buffer *head,
1133     const struct ccs_condition *cond)
1134     {
1135     const struct ccs_condition_element *condp;
1136     const struct ccs_number_union *numbers_p;
1137     const struct ccs_name_union *names_p;
1138 kumaneko 3693 const struct ccs_argv *argv;
1139     const struct ccs_envp *envp;
1140 kumaneko 2894 u16 condc;
1141     u16 i;
1142     u16 j;
1143     char buffer[32];
1144     if (!cond)
1145     goto no_condition;
1146     condc = cond->condc;
1147     condp = (const struct ccs_condition_element *) (cond + 1);
1148     numbers_p = (const struct ccs_number_union *) (condp + condc);
1149     names_p = (const struct ccs_name_union *)
1150     (numbers_p + cond->numbers_count);
1151 kumaneko 3693 argv = (const struct ccs_argv *) (names_p + cond->names_count);
1152     envp = (const struct ccs_envp *) (argv + cond->argc);
1153 kumaneko 2894 memset(buffer, 0, sizeof(buffer));
1154     if (condc && !ccs_io_printf(head, "%s", " if"))
1155     goto out;
1156     for (i = 0; i < condc; i++) {
1157     const u8 match = condp->equals;
1158     const u8 left = condp->left;
1159     const u8 right = condp->right;
1160     condp++;
1161     switch (left) {
1162     case CCS_ARGV_ENTRY:
1163     if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",
1164     argv->index, argv->is_not ?
1165     "!=" : "=", argv->value->name))
1166     goto out;
1167     argv++;
1168     continue;
1169     case CCS_ENVP_ENTRY:
1170     if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",
1171     envp->name->name, envp->is_not ?
1172     "!=" : "="))
1173     goto out;
1174     if (envp->value) {
1175     if (!ccs_io_printf(head, "\"%s\"",
1176     envp->value->name))
1177     goto out;
1178     } else {
1179     if (!ccs_io_printf(head, "NULL"))
1180     goto out;
1181     }
1182     envp++;
1183     continue;
1184     case CCS_NUMBER_UNION:
1185     if (!ccs_print_number_union(head, numbers_p++))
1186     goto out;
1187     break;
1188     default:
1189     if (left >= CCS_MAX_CONDITION_KEYWORD)
1190     goto out;
1191     if (!ccs_io_printf(head, " %s",
1192     ccs_condition_keyword[left]))
1193     goto out;
1194     break;
1195     }
1196     if (!ccs_io_printf(head, "%s", match ? "=" : "!="))
1197     goto out;
1198     switch (right) {
1199     case CCS_NAME_UNION:
1200     if (!ccs_print_name_union_quoted(head, names_p++))
1201     goto out;
1202     break;
1203     case CCS_NUMBER_UNION:
1204     if (!ccs_print_number_union_nospace(head, numbers_p++))
1205     goto out;
1206     break;
1207     default:
1208     if (right >= CCS_MAX_CONDITION_KEYWORD)
1209     goto out;
1210     if (!ccs_io_printf(head, "%s",
1211     ccs_condition_keyword[right]))
1212     goto out;
1213     break;
1214     }
1215     }
1216     i = cond->post_state[3];
1217     if (!i)
1218     goto no_condition;
1219     if (!ccs_io_printf(head, " ; set"))
1220     goto out;
1221     for (j = 0; j < 3; j++) {
1222     if (!(i & (1 << j)))
1223     continue;
1224     if (!ccs_io_printf(head, " task.state[%u]=%u", j,
1225     cond->post_state[j]))
1226     goto out;
1227     }
1228 kumaneko 3696 if (i & (1 << 4)) {
1229     if (!ccs_io_printf(head, " audit=%s",
1230     ccs_yesno(cond->post_state[4])))
1231     goto out;
1232     }
1233 kumaneko 2894 no_condition:
1234     if (ccs_io_printf(head, "\n"))
1235     return true;
1236     out:
1237     return false;
1238     }
1239    
1240     /**
1241 kumaneko 3106 * ccs_print_path_acl - Print a path ACL entry.
1242 kumaneko 2863 *
1243     * @head: Pointer to "struct ccs_io_buffer".
1244 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_acl".
1245 kumaneko 2863 *
1246     * Returns true on success, false otherwise.
1247     */
1248 kumaneko 2915 static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1249 kumaneko 3747 struct ccs_path_acl *ptr)
1250 kumaneko 2863 {
1251     int pos;
1252     u8 bit;
1253     const u16 perm = ptr->perm;
1254 kumaneko 2915 for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
1255 kumaneko 2863 if (!(perm & (1 << bit)))
1256     continue;
1257 kumaneko 3484 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1258     && bit != CCS_TYPE_TRANSIT)
1259 kumaneko 2863 continue;
1260     /* Print "read/write" instead of "read" and "write". */
1261 kumaneko 2915 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1262     && (perm & (1 << CCS_TYPE_READ_WRITE)))
1263 kumaneko 2863 continue;
1264     pos = head->read_avail;
1265 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||
1266 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
1267 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1268 kumaneko 2897 head->read_bit = bit;
1269     head->read_avail = pos;
1270     return false;
1271     }
1272 kumaneko 2863 }
1273     head->read_bit = 0;
1274     return true;
1275     }
1276    
1277     /**
1278 kumaneko 2922 * ccs_print_path_number3_acl - Print a path_number3 ACL entry.
1279 kumaneko 2863 *
1280     * @head: Pointer to "struct ccs_io_buffer".
1281 kumaneko 2922 * @ptr: Pointer to "struct ccs_path_number3_acl".
1282 kumaneko 2863 *
1283     * Returns true on success, false otherwise.
1284     */
1285 kumaneko 2922 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,
1286 kumaneko 3747 struct ccs_path_number3_acl *ptr)
1287 kumaneko 2863 {
1288     int pos;
1289     u8 bit;
1290     const u16 perm = ptr->perm;
1291 kumaneko 2922 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;
1292 kumaneko 2916 bit++) {
1293 kumaneko 2863 if (!(perm & (1 << bit)))
1294     continue;
1295     pos = head->read_avail;
1296 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
1297 kumaneko 2922 ccs_path_number32keyword(bit)) ||
1298 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
1299 kumaneko 2922 !ccs_print_number_union(head, &ptr->mode) ||
1300 kumaneko 2888 !ccs_print_number_union(head, &ptr->major) ||
1301     !ccs_print_number_union(head, &ptr->minor) ||
1302 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1303 kumaneko 2897 head->read_bit = bit;
1304     head->read_avail = pos;
1305     return false;
1306     }
1307 kumaneko 2863 }
1308     head->read_bit = 0;
1309     return true;
1310     }
1311    
1312     /**
1313 kumaneko 2948 * ccs_print_path2_acl - Print a path2 ACL entry.
1314 kumaneko 2863 *
1315     * @head: Pointer to "struct ccs_io_buffer".
1316 kumaneko 2922 * @ptr: Pointer to "struct ccs_path2_acl".
1317 kumaneko 2863 *
1318     * Returns true on success, false otherwise.
1319     */
1320 kumaneko 2922 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1321 kumaneko 3747 struct ccs_path2_acl *ptr)
1322 kumaneko 2863 {
1323     int pos;
1324     u8 bit;
1325     const u8 perm = ptr->perm;
1326 kumaneko 2922 for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1327 kumaneko 2863 if (!(perm & (1 << bit)))
1328     continue;
1329     pos = head->read_avail;
1330 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
1331 kumaneko 2922 ccs_path22keyword(bit)) ||
1332 kumaneko 2888 !ccs_print_name_union(head, &ptr->name1) ||
1333     !ccs_print_name_union(head, &ptr->name2) ||
1334 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1335 kumaneko 2897 head->read_bit = bit;
1336     head->read_avail = pos;
1337     return false;
1338     }
1339 kumaneko 2863 }
1340     head->read_bit = 0;
1341     return true;
1342     }
1343    
1344     /**
1345 kumaneko 2948 * ccs_print_path_number_acl - Print a path_number ACL entry.
1346 kumaneko 2863 *
1347     * @head: Pointer to "struct ccs_io_buffer".
1348 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_number_acl".
1349 kumaneko 2863 *
1350     * Returns true on success, false otherwise.
1351     */
1352 kumaneko 2871 static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1353 kumaneko 3747 struct ccs_path_number_acl *ptr)
1354 kumaneko 2863 {
1355 kumaneko 2871 int pos;
1356     u8 bit;
1357     const u8 perm = ptr->perm;
1358 kumaneko 2916 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1359     bit++) {
1360 kumaneko 2871 if (!(perm & (1 << bit)))
1361     continue;
1362     pos = head->read_avail;
1363 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
1364     ccs_path_number2keyword(bit)) ||
1365 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
1366     !ccs_print_number_union(head, &ptr->number) ||
1367 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1368 kumaneko 2897 head->read_bit = bit;
1369     head->read_avail = pos;
1370     return false;
1371     }
1372 kumaneko 2871 }
1373     head->read_bit = 0;
1374 kumaneko 2863 return true;
1375     }
1376    
1377     /**
1378     * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1379     *
1380     * @head: Pointer to "struct ccs_io_buffer".
1381 kumaneko 2915 * @ptr: Pointer to "struct ccs_env_acl".
1382 kumaneko 2863 *
1383     * Returns true on success, false otherwise.
1384     */
1385     static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1386 kumaneko 3747 struct ccs_env_acl *ptr)
1387 kumaneko 2863 {
1388 kumaneko 2897 const int pos = head->read_avail;
1389     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1390 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1391 kumaneko 2897 head->read_avail = pos;
1392     return false;
1393     }
1394 kumaneko 2863 return true;
1395     }
1396    
1397     /**
1398     * ccs_print_capability_acl - Print a capability ACL entry.
1399     *
1400     * @head: Pointer to "struct ccs_io_buffer".
1401 kumaneko 2915 * @ptr: Pointer to "struct ccs_capability_acl".
1402 kumaneko 2863 *
1403     * Returns true on success, false otherwise.
1404     */
1405     static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1406 kumaneko 3747 struct ccs_capability_acl *ptr)
1407 kumaneko 2863 {
1408 kumaneko 2897 const int pos = head->read_avail;
1409 kumaneko 2892 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1410 kumaneko 2897 ccs_cap2keyword(ptr->operation)) ||
1411 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1412 kumaneko 2897 head->read_avail = pos;
1413     return false;
1414     }
1415 kumaneko 2863 return true;
1416     }
1417    
1418     /**
1419     * ccs_print_network_acl - Print a network ACL entry.
1420     *
1421     * @head: Pointer to "struct ccs_io_buffer".
1422 kumaneko 2915 * @ptr: Pointer to "struct ccs_ip_network_acl".
1423 kumaneko 2863 *
1424     * Returns true on success, false otherwise.
1425     */
1426     static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1427 kumaneko 3747 struct ccs_ip_network_acl *ptr)
1428 kumaneko 2863 {
1429 kumaneko 2916 int pos;
1430     u8 bit;
1431 kumaneko 3695 const u8 perm = ptr->perm;
1432 kumaneko 3690 char buf[128];
1433 kumaneko 2916 for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1434 kumaneko 3690 const char *w[2] = { "", "" };
1435 kumaneko 2916 if (!(perm & (1 << bit)))
1436     continue;
1437     pos = head->read_avail;
1438     switch (ptr->address_type) {
1439     case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1440 kumaneko 3690 w[0] = "@";
1441     w[1] = ptr->address.group->group_name->name;
1442 kumaneko 2916 break;
1443     case CCS_IP_ADDRESS_TYPE_IPv4:
1444 kumaneko 3690 ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1445     ptr->address.ipv4.max);
1446     w[0] = buf;
1447 kumaneko 2916 break;
1448     case CCS_IP_ADDRESS_TYPE_IPv6:
1449 kumaneko 3690 ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1450     ptr->address.ipv6.max);
1451     w[0] = buf;
1452 kumaneko 2916 break;
1453     }
1454 kumaneko 3690 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1455     ccs_net2keyword(bit), w[0], w[1]) ||
1456     !ccs_print_number_union(head, &ptr->port) ||
1457 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond))
1458 kumaneko 2863 goto out;
1459     }
1460 kumaneko 2916 head->read_bit = 0;
1461 kumaneko 2863 return true;
1462     out:
1463 kumaneko 2916 head->read_bit = bit;
1464 kumaneko 2863 head->read_avail = pos;
1465     return false;
1466     }
1467    
1468     /**
1469     * ccs_print_signal_acl - Print a signal ACL entry.
1470     *
1471     * @head: Pointer to "struct ccs_io_buffer".
1472 kumaneko 2915 * @ptr: Pointer to "struct signale_acl".
1473 kumaneko 2863 *
1474     * Returns true on success, false otherwise.
1475     */
1476     static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1477 kumaneko 3747 struct ccs_signal_acl *ptr)
1478 kumaneko 2863 {
1479 kumaneko 2897 const int pos = head->read_avail;
1480 kumaneko 2892 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1481 kumaneko 2897 ptr->sig, ptr->domainname->name) ||
1482 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1483 kumaneko 2897 head->read_avail = pos;
1484     return false;
1485     }
1486 kumaneko 2863 return true;
1487     }
1488    
1489     /**
1490 kumaneko 3697 * ccs_print_execute_handler - Print an execute handler ACL entry.
1491 kumaneko 2863 *
1492     * @head: Pointer to "struct ccs_io_buffer".
1493     * @keyword: Name of the keyword.
1494 kumaneko 3697 * @ptr: Pointer to "struct ccs_execute_handler".
1495 kumaneko 2863 *
1496     * Returns true on success, false otherwise.
1497     */
1498 kumaneko 3697 static bool ccs_print_execute_handler(struct ccs_io_buffer *head,
1499     const char *keyword,
1500     struct ccs_execute_handler *ptr)
1501 kumaneko 2863 {
1502 kumaneko 3747 const int pos = head->read_avail;
1503     if (!ccs_io_printf(head, "%s %s", keyword, ptr->handler->name) ||
1504     !ccs_print_condition(head, ptr->head.cond)) {
1505     head->read_avail = pos;
1506     return false;
1507     }
1508     return true;
1509 kumaneko 2863 }
1510    
1511     /**
1512     * ccs_print_mount_acl - Print a mount ACL entry.
1513     *
1514     * @head: Pointer to "struct ccs_io_buffer".
1515 kumaneko 2915 * @ptr: Pointer to "struct ccs_mount_acl".
1516 kumaneko 2863 *
1517     * Returns true on success, false otherwise.
1518     */
1519     static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1520 kumaneko 3747 struct ccs_mount_acl *ptr)
1521 kumaneko 2863 {
1522 kumaneko 2897 const int pos = head->read_avail;
1523     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1524     !ccs_print_name_union(head, &ptr->dev_name) ||
1525     !ccs_print_name_union(head, &ptr->dir_name) ||
1526     !ccs_print_name_union(head, &ptr->fs_type) ||
1527     !ccs_print_number_union(head, &ptr->flags) ||
1528 kumaneko 3747 !ccs_print_condition(head, ptr->head.cond)) {
1529 kumaneko 2897 head->read_avail = pos;
1530     return false;
1531     }
1532 kumaneko 2863 return true;
1533     }
1534    
1535     /**
1536     * ccs_print_entry - Print an ACL entry.
1537     *
1538     * @head: Pointer to "struct ccs_io_buffer".
1539     * @ptr: Pointer to an ACL entry.
1540     *
1541     * Returns true on success, false otherwise.
1542     */
1543     static bool ccs_print_entry(struct ccs_io_buffer *head,
1544     struct ccs_acl_info *ptr)
1545     {
1546 kumaneko 2900 const u8 acl_type = ptr->type;
1547     if (ptr->is_deleted)
1548 kumaneko 2863 return true;
1549 kumaneko 2915 if (acl_type == CCS_TYPE_PATH_ACL) {
1550     struct ccs_path_acl *acl
1551 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1552     return ccs_print_path_acl(head, acl);
1553 kumaneko 2863 }
1554 kumaneko 3697 if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1555     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1556     struct ccs_execute_handler *acl
1557 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1558 kumaneko 3697 const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1559     CCS_KEYWORD_EXECUTE_HANDLER :
1560     CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1561     return ccs_print_execute_handler(head, keyword, acl);
1562 kumaneko 2863 }
1563     if (head->read_execute_only)
1564     return true;
1565 kumaneko 2922 if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {
1566     struct ccs_path_number3_acl *acl
1567 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1568     return ccs_print_path_number3_acl(head, acl);
1569 kumaneko 2863 }
1570 kumaneko 2922 if (acl_type == CCS_TYPE_PATH2_ACL) {
1571     struct ccs_path2_acl *acl
1572 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1573     return ccs_print_path2_acl(head, acl);
1574 kumaneko 2863 }
1575 kumaneko 2892 if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1576 kumaneko 2915 struct ccs_path_number_acl *acl
1577 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1578     return ccs_print_path_number_acl(head, acl);
1579 kumaneko 2863 }
1580 kumaneko 2892 if (acl_type == CCS_TYPE_ENV_ACL) {
1581 kumaneko 2915 struct ccs_env_acl *acl
1582 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1583     return ccs_print_env_acl(head, acl);
1584 kumaneko 2863 }
1585 kumaneko 2892 if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1586 kumaneko 2915 struct ccs_capability_acl *acl
1587 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1588     return ccs_print_capability_acl(head, acl);
1589 kumaneko 2863 }
1590 kumaneko 2892 if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1591 kumaneko 2915 struct ccs_ip_network_acl *acl
1592 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1593     return ccs_print_network_acl(head, acl);
1594 kumaneko 2863 }
1595 kumaneko 2892 if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1596 kumaneko 2915 struct ccs_signal_acl *acl
1597 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1598     return ccs_print_signal_acl(head, acl);
1599 kumaneko 2863 }
1600 kumaneko 2892 if (acl_type == CCS_TYPE_MOUNT_ACL) {
1601 kumaneko 2915 struct ccs_mount_acl *acl
1602 kumaneko 3747 = container_of(ptr, typeof(*acl), head);
1603     return ccs_print_mount_acl(head, acl);
1604 kumaneko 2863 }
1605     BUG(); /* This must not happen. */
1606     return false;
1607     }
1608    
1609     /**
1610 kumaneko 3697 * ccs_read_domain2 - Read domain policy.
1611     *
1612     * @head: Pointer to "struct ccs_io_buffer".
1613     * @domain: Pointer to "struct ccs_domain_info".
1614     *
1615     * Caller holds ccs_read_lock().
1616     *
1617     * Returns true on success, false otherwise.
1618     */
1619     static bool ccs_read_domain2(struct ccs_io_buffer *head,
1620     struct ccs_domain_info *domain)
1621     {
1622     struct list_head *pos;
1623     /* Print ACL entries in the domain. */
1624     list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {
1625     struct ccs_acl_info *ptr
1626     = list_entry(pos, struct ccs_acl_info, list);
1627     if (!ccs_print_entry(head, ptr))
1628     return false;
1629     }
1630     return true;
1631     }
1632    
1633     /**
1634 kumaneko 3693 * ccs_read_domain - Read domain policy.
1635 kumaneko 2863 *
1636     * @head: Pointer to "struct ccs_io_buffer".
1637     *
1638     * Caller holds ccs_read_lock().
1639     */
1640 kumaneko 3693 static void ccs_read_domain(struct ccs_io_buffer *head)
1641 kumaneko 2863 {
1642 kumaneko 3697 struct list_head *pos;
1643 kumaneko 2863 if (head->read_eof)
1644 kumaneko 2943 return;
1645 kumaneko 3697 list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1646     struct ccs_domain_info *domain =
1647     list_entry(pos, struct ccs_domain_info, list);
1648     switch (head->read_step) {
1649 kumaneko 3747 u8 i;
1650     const char *w[CCS_MAX_DOMAIN_INFO_FLAGS];
1651 kumaneko 3697 case 0:
1652     if (domain->is_deleted && !head->read_single_domain)
1653     continue;
1654     /* Print domainname and flags. */
1655 kumaneko 3747 for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1656     w[i] = domain->flags[i] ? ccs_dif[i] : "";
1657 kumaneko 3697 if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE
1658 kumaneko 3702 "%u\n%s%s%s%s%s\n",
1659 kumaneko 3697 domain->domainname->name,
1660 kumaneko 3747 domain->profile,
1661     w[CCS_DIF_QUOTA_WARNED],
1662     w[CCS_DIF_IGNORE_GLOBAL],
1663     w[CCS_DIF_IGNORE_GLOBAL_ALLOW_READ],
1664     w[CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV],
1665     w[CCS_DIF_TRANSITION_FAILED]))
1666 kumaneko 2943 return;
1667 kumaneko 3697 head->read_step++;
1668     /* fall through */
1669     case 1:
1670     if (!ccs_read_domain2(head, domain))
1671     return;
1672     head->read_step++;
1673     /* fall through */
1674     case 2:
1675     if (!ccs_io_printf(head, "\n"))
1676     return;
1677     head->read_step = 0;
1678     if (head->read_single_domain)
1679     goto out;
1680 kumaneko 2863 }
1681     }
1682 kumaneko 3697 out:
1683 kumaneko 2863 head->read_eof = true;
1684     }
1685    
1686     /**
1687     * ccs_write_domain_profile - Assign profile for specified domain.
1688     *
1689     * @head: Pointer to "struct ccs_io_buffer".
1690     *
1691     * Returns 0 on success, -EINVAL otherwise.
1692     *
1693     * This is equivalent to doing
1694     *
1695     * ( echo "select " $domainname; echo "use_profile " $profile ) |
1696 kumaneko 2948 * /usr/sbin/ccs-loadpolicy -d
1697 kumaneko 2863 *
1698     * Caller holds ccs_read_lock().
1699     */
1700     static int ccs_write_domain_profile(struct ccs_io_buffer *head)
1701     {
1702     char *data = head->write_buf;
1703     char *cp = strchr(data, ' ');
1704     struct ccs_domain_info *domain;
1705     unsigned int profile;
1706     if (!cp)
1707     return -EINVAL;
1708     *cp = '\0';
1709     profile = simple_strtoul(data, NULL, 10);
1710 kumaneko 2892 if (profile >= CCS_MAX_PROFILES)
1711 kumaneko 2863 return -EINVAL;
1712     domain = ccs_find_domain(cp + 1);
1713 kumaneko 2991 if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1714 kumaneko 2863 domain->profile = (u8) profile;
1715     return 0;
1716     }
1717    
1718     /**
1719     * ccs_read_domain_profile - Read only domainname and profile.
1720     *
1721     * @head: Pointer to "struct ccs_io_buffer".
1722     *
1723     * This is equivalent to doing
1724     *
1725     * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1726     * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1727     * domainname = $0; } else if ( $1 == "use_profile" ) {
1728     * print $2 " " domainname; domainname = ""; } } ; '
1729     *
1730     * Caller holds ccs_read_lock().
1731     */
1732 kumaneko 2943 static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1733 kumaneko 2863 {
1734     struct list_head *pos;
1735     if (head->read_eof)
1736 kumaneko 2943 return;
1737 kumaneko 2863 list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1738     struct ccs_domain_info *domain;
1739     domain = list_entry(pos, struct ccs_domain_info, list);
1740     if (domain->is_deleted)
1741     continue;
1742     if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1743     domain->domainname->name))
1744 kumaneko 2943 return;
1745 kumaneko 2863 }
1746     head->read_eof = true;
1747     }
1748    
1749     /**
1750     * ccs_write_pid: Specify PID to obtain domainname.
1751     *
1752     * @head: Pointer to "struct ccs_io_buffer".
1753     *
1754     * Returns 0.
1755     */
1756     static int ccs_write_pid(struct ccs_io_buffer *head)
1757     {
1758     head->read_eof = false;
1759     return 0;
1760     }
1761    
1762     /**
1763     * ccs_read_pid - Read information of a process.
1764     *
1765     * @head: Pointer to "struct ccs_io_buffer".
1766     *
1767     * Returns the domainname which the specified PID is in or
1768     * process information of the specified PID on success,
1769     * empty string otherwise.
1770     *
1771     * Caller holds ccs_read_lock().
1772     */
1773 kumaneko 2943 static void ccs_read_pid(struct ccs_io_buffer *head)
1774 kumaneko 2863 {
1775     char *buf = head->write_buf;
1776     bool task_info = false;
1777 kumaneko 2970 bool global_pid = false;
1778 kumaneko 2863 unsigned int pid;
1779     struct task_struct *p;
1780     struct ccs_domain_info *domain = NULL;
1781     u32 ccs_flags = 0;
1782     /* Accessing write_buf is safe because head->io_sem is held. */
1783 kumaneko 3136 if (!buf) {
1784     head->read_eof = true;
1785 kumaneko 2943 return; /* Do nothing if open(O_RDONLY). */
1786 kumaneko 3136 }
1787 kumaneko 2863 if (head->read_avail || head->read_eof)
1788 kumaneko 2943 return;
1789 kumaneko 2863 head->read_eof = true;
1790     if (ccs_str_starts(&buf, "info "))
1791     task_info = true;
1792 kumaneko 2970 if (ccs_str_starts(&buf, "global-pid "))
1793     global_pid = true;
1794 kumaneko 2863 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1795 kumaneko 3248 ccs_tasklist_lock();
1796 kumaneko 2970 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1797     if (global_pid)
1798 kumaneko 3502 p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1799 kumaneko 2970 else
1800 kumaneko 3502 p = ccsecurity_exports.find_task_by_vpid(pid);
1801 kumaneko 2970 #else
1802 kumaneko 2863 p = find_task_by_pid(pid);
1803 kumaneko 2970 #endif
1804 kumaneko 2863 if (p) {
1805     domain = ccs_task_domain(p);
1806     ccs_flags = p->ccs_flags;
1807     }
1808 kumaneko 3248 ccs_tasklist_unlock();
1809 kumaneko 2863 if (!domain)
1810 kumaneko 2943 return;
1811 kumaneko 2863 if (!task_info)
1812     ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1813     domain->domainname->name);
1814     else
1815     ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1816     "state[0]=%u state[1]=%u state[2]=%u", pid,
1817 kumaneko 2958 ccs_yesno(ccs_flags &
1818 kumaneko 3693 CCS_TASK_IS_MANAGER),
1819 kumaneko 2958 ccs_yesno(ccs_flags &
1820     CCS_TASK_IS_EXECUTE_HANDLER),
1821 kumaneko 2863 (u8) (ccs_flags >> 24),
1822     (u8) (ccs_flags >> 16),
1823     (u8) (ccs_flags >> 8));
1824     }
1825    
1826 kumaneko 3752 static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1827     [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1828     [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1829     = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1830     [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN,
1831     [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN
1832     };
1833    
1834     static const char *ccs_group_name[CCS_MAX_GROUP] = {
1835     [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1836     [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1837     [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1838     };
1839    
1840 kumaneko 2863 /**
1841 kumaneko 3693 * ccs_write_exception - Write exception policy.
1842 kumaneko 2863 *
1843     * @head: Pointer to "struct ccs_io_buffer".
1844     *
1845     * Returns 0 on success, negative value otherwise.
1846     */
1847 kumaneko 3693 static int ccs_write_exception(struct ccs_io_buffer *head)
1848 kumaneko 2863 {
1849     char *data = head->write_buf;
1850 kumaneko 3691 const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1851     u8 i;
1852     static const struct {
1853     const char *keyword;
1854 kumaneko 3752 int (*write) (char *, const bool);
1855     } ccs_callback[4] = {
1856 kumaneko 3693 { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1857     { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1858     { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1859     { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1860 kumaneko 3691 };
1861 kumaneko 3752 for (i = 0; i < 4; i++) {
1862 kumaneko 3691 if (ccs_str_starts(&data, ccs_callback[i].keyword))
1863 kumaneko 3752 return ccs_callback[i].write(data, is_delete);
1864 kumaneko 3691 }
1865 kumaneko 3752 for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) {
1866     if (ccs_str_starts(&data, ccs_transition_type[i]))
1867     return ccs_write_transition_control(data, is_delete,
1868     i);
1869     }
1870 kumaneko 3692 for (i = 0; i < CCS_MAX_GROUP; i++) {
1871 kumaneko 3752 if (ccs_str_starts(&data, ccs_group_name[i]))
1872 kumaneko 3693 return ccs_write_group(data, is_delete, i);
1873 kumaneko 3692 }
1874 kumaneko 3746 return ccs_write_domain2(data, &ccs_global_domain, is_delete);
1875 kumaneko 2863 }
1876    
1877     /**
1878 kumaneko 3689 * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1879 kumaneko 2863 *
1880     * @head: Pointer to "struct ccs_io_buffer".
1881 kumaneko 3689 * @idx: Index number.
1882 kumaneko 2863 *
1883 kumaneko 3689 * Returns true on success, false otherwise.
1884     *
1885 kumaneko 2863 * Caller holds ccs_read_lock().
1886     */
1887 kumaneko 3689 static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1888 kumaneko 2863 {
1889 kumaneko 3689 struct list_head *gpos;
1890     struct list_head *mpos;
1891 kumaneko 3690 const char *w[3] = { "", "", "" };
1892 kumaneko 3752 w[0] = ccs_group_name[idx];
1893 kumaneko 3689 list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1894     struct ccs_group *group =
1895     list_entry(gpos, struct ccs_group, head.list);
1896 kumaneko 3690 w[1] = group->group_name->name;
1897 kumaneko 3689 list_for_each_cookie(mpos, head->read_var2,
1898     &group->member_list) {
1899 kumaneko 3690 char buffer[128];
1900 kumaneko 3689 struct ccs_acl_head *ptr =
1901     list_entry(mpos, struct ccs_acl_head, list);
1902     if (ptr->is_deleted)
1903     continue;
1904     if (idx == CCS_PATH_GROUP) {
1905 kumaneko 3690 w[2] = container_of(ptr, struct ccs_path_group,
1906     head)->member_name->name;
1907 kumaneko 3689 } else if (idx == CCS_NUMBER_GROUP) {
1908 kumaneko 3690 w[2] = buffer;
1909     ccs_print_number(buffer, sizeof(buffer),
1910     &container_of
1911     (ptr, struct ccs_number_group,
1912     head)->number);
1913 kumaneko 3689 } else if (idx == CCS_ADDRESS_GROUP) {
1914     struct ccs_address_group *member =
1915     container_of(ptr, typeof(*member),
1916     head);
1917 kumaneko 3690 w[2] = buffer;
1918     if (member->is_ipv6)
1919     ccs_print_ipv6(buffer, sizeof(buffer),
1920     member->min.ipv6,
1921     member->max.ipv6);
1922     else
1923     ccs_print_ipv4(buffer, sizeof(buffer),
1924     member->min.ipv4,
1925     member->max.ipv4);
1926 kumaneko 3689 }
1927 kumaneko 3690 if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1928     w[2]))
1929     return false;
1930 kumaneko 3689 }
1931     }
1932     return true;
1933     }
1934    
1935     /**
1936     * ccs_read_policy - Read "struct ccs_..._entry" list.
1937     *
1938     * @head: Pointer to "struct ccs_io_buffer".
1939     * @idx: Index number.
1940     *
1941     * Returns true on success, false otherwise.
1942     *
1943     * Caller holds ccs_read_lock().
1944     */
1945     static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1946     {
1947     struct list_head *pos;
1948     list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1949     const char *w[4] = { "", "", "", "" };
1950     char buffer[16];
1951     struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1952     list);
1953     if (acl->is_deleted)
1954     continue;
1955     switch (idx) {
1956 kumaneko 3752 case CCS_ID_TRANSITION_CONTROL:
1957 kumaneko 3689 {
1958 kumaneko 3752 struct ccs_transition_control *ptr =
1959 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1960 kumaneko 3752 w[0] = ccs_transition_type[ptr->type];
1961     w[1] = ptr->program ? ptr->program->name
1962     : "any";
1963     w[2] = " from ";
1964     w[3] = ptr->domainname ? ptr->domainname->name
1965     : "any";
1966 kumaneko 3689 }
1967 kumaneko 2863 break;
1968 kumaneko 3689 case CCS_ID_AGGREGATOR:
1969     {
1970 kumaneko 3693 struct ccs_aggregator *ptr =
1971 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1972     w[0] = CCS_KEYWORD_AGGREGATOR;
1973     w[1] = ptr->original_name->name;
1974     w[2] = " ";
1975     w[3] = ptr->aggregated_name->name;
1976     }
1977 kumaneko 2943 break;
1978 kumaneko 3689 case CCS_ID_PATTERN:
1979     {
1980 kumaneko 3693 struct ccs_pattern *ptr =
1981 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1982     w[0] = CCS_KEYWORD_FILE_PATTERN;
1983     w[1] = ptr->pattern->name;
1984     }
1985 kumaneko 2943 break;
1986 kumaneko 3689 case CCS_ID_NO_REWRITE:
1987     {
1988 kumaneko 3693 struct ccs_no_rewrite *ptr =
1989 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1990     w[0] = CCS_KEYWORD_DENY_REWRITE;
1991     w[1] = ptr->pattern->name;
1992     }
1993 kumaneko 2943 break;
1994 kumaneko 3689 case CCS_ID_RESERVEDPORT:
1995     {
1996 kumaneko 3693 struct ccs_reserved *ptr =
1997 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1998     const u16 min_port = ptr->min_port;
1999     const u16 max_port = ptr->max_port;
2000     w[0] = CCS_KEYWORD_DENY_AUTOBIND;
2001     snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
2002     min_port, min_port != max_port ?
2003     '-' : '\0', max_port);
2004     buffer[sizeof(buffer) - 1] = '\0';
2005     w[1] = buffer;
2006     }
2007 kumaneko 2943 break;
2008 kumaneko 3696 default:
2009     continue;
2010 kumaneko 3689 }
2011 kumaneko 3697 if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))
2012     return false;
2013 kumaneko 2863 }
2014 kumaneko 3689 return true;
2015 kumaneko 2863 }
2016    
2017 kumaneko 3697 static void ccs_read_global_domain(struct ccs_io_buffer *head)
2018     {
2019     if (!head->read_eof)
2020     head->read_eof = ccs_read_domain2(head, &ccs_global_domain);
2021     }
2022    
2023 kumaneko 2897 /**
2024 kumaneko 3693 * ccs_read_exception - Read exception policy.
2025 kumaneko 3689 *
2026     * @head: Pointer to "struct ccs_io_buffer".
2027     *
2028     * Caller holds ccs_read_lock().
2029     */
2030 kumaneko 3693 static void ccs_read_exception(struct ccs_io_buffer *head)
2031 kumaneko 3689 {
2032     if (head->read_eof)
2033     return;
2034     while (head->read_step < CCS_MAX_POLICY &&
2035     ccs_read_policy(head, head->read_step))
2036     head->read_step++;
2037     if (head->read_step < CCS_MAX_POLICY)
2038     return;
2039     while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2040     ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
2041     head->read_step++;
2042 kumaneko 3697 if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2043     return;
2044     head->read = ccs_read_global_domain;
2045 kumaneko 3689 }
2046    
2047     /**
2048 kumaneko 2897 * ccs_get_argv0 - Get argv[0].
2049     *
2050 kumaneko 3693 * @ee: Pointer to "struct ccs_execve".
2051 kumaneko 2897 *
2052     * Returns true on success, false otherwise.
2053     */
2054 kumaneko 3693 static bool ccs_get_argv0(struct ccs_execve *ee)
2055 kumaneko 2897 {
2056     struct linux_binprm *bprm = ee->bprm;
2057     char *arg_ptr = ee->tmp;
2058     int arg_len = 0;
2059     unsigned long pos = bprm->p;
2060     int offset = pos % PAGE_SIZE;
2061     bool done = false;
2062     if (!bprm->argc)
2063     goto out;
2064     while (1) {
2065     if (!ccs_dump_page(bprm, pos, &ee->dump))
2066     goto out;
2067     pos += PAGE_SIZE - offset;
2068     /* Read. */
2069     while (offset < PAGE_SIZE) {
2070     const char *kaddr = ee->dump.data;
2071     const unsigned char c = kaddr[offset++];
2072 kumaneko 2932 if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
2073 kumaneko 2897 if (c == '\\') {
2074     arg_ptr[arg_len++] = '\\';
2075     arg_ptr[arg_len++] = '\\';
2076     } else if (c > ' ' && c < 127) {
2077     arg_ptr[arg_len++] = c;
2078     } else {
2079     arg_ptr[arg_len++] = '\\';
2080     arg_ptr[arg_len++] = (c >> 6) + '0';
2081     arg_ptr[arg_len++]
2082     = ((c >> 3) & 7) + '0';
2083     arg_ptr[arg_len++] = (c & 7) + '0';
2084     }
2085     } else {
2086     arg_ptr[arg_len] = '\0';
2087     done = true;
2088     break;
2089     }
2090     }
2091     offset = 0;
2092     if (done)
2093     break;
2094     }
2095     return true;
2096     out:
2097     return false;
2098     }
2099    
2100 kumaneko 2863 /* Wait queue for ccs_query_list. */
2101     static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2102    
2103     /* Lock for manipulating ccs_query_list. */
2104     static DEFINE_SPINLOCK(ccs_query_list_lock);
2105    
2106     /* Structure for query. */
2107     struct ccs_query_entry {
2108     struct list_head list;
2109     char *query;
2110     int query_len;
2111     unsigned int serial;
2112     int timer;
2113     int answer;
2114     };
2115    
2116     /* The list for "struct ccs_query_entry". */
2117     static LIST_HEAD(ccs_query_list);
2118    
2119     /* Number of "struct file" referring /proc/ccs/query interface. */
2120     static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2121    
2122 kumaneko 3746 static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
2123     {
2124     va_list args;
2125     const int pos = strlen(buffer);
2126     va_start(args, fmt);
2127     vsnprintf(buffer + pos, len - pos - 1, fmt, args);
2128     va_end(args);
2129     }
2130    
2131 kumaneko 2863 /**
2132 kumaneko 2922 * ccs_supervisor - Ask for the supervisor's decision.
2133 kumaneko 2863 *
2134 kumaneko 3701 * @r: Pointer to "struct ccs_request_info".
2135     * @fmt: The printf()'s format string, followed by parameters.
2136 kumaneko 2863 *
2137     * Returns 0 if the supervisor decided to permit the access request which
2138 kumaneko 3494 * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2139     * decided to retry the access request which violated the policy in enforcing
2140     * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2141 kumaneko 2863 */
2142 kumaneko 2922 int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2143 kumaneko 2863 {
2144     va_list args;
2145     int error = -EPERM;
2146     int pos;
2147     int len;
2148     static unsigned int ccs_serial;
2149     struct ccs_query_entry *ccs_query_entry = NULL;
2150     bool quota_exceeded = false;
2151     char *header;
2152 kumaneko 3627 struct ccs_domain_info * const domain = ccs_current_domain();
2153 kumaneko 3746 va_start(args, fmt);
2154     len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
2155     va_end(args);
2156     if (r->mode == CCS_CONFIG_LEARNING) {
2157 kumaneko 2897 char *buffer;
2158 kumaneko 3746 char *realpath = NULL;
2159     char *argv0 = NULL;
2160     const char *symlink = NULL;
2161     const struct ccs_preference *pref;
2162 kumaneko 2897 if (!ccs_domain_quota_ok(r))
2163     return 0;
2164 kumaneko 3746 pref = ccs_profile(r->profile)->learning;
2165     if (r->param_type == CCS_TYPE_PATH_ACL &&
2166     r->param.path.operation == CCS_TYPE_EXECUTE) {
2167     if (pref->learning_exec_realpath) {
2168     struct file *file = r->ee->bprm->file;
2169     #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2170     struct path path = { file->f_vfsmnt,
2171     file->f_dentry };
2172     realpath = ccs_realpath_from_path(&path);
2173     #else
2174     realpath = ccs_realpath_from_path(&file->
2175     f_path);
2176     #endif
2177     if (realpath)
2178     len += strlen(realpath);
2179     }
2180     if (pref->learning_exec_argv0) {
2181     if (ccs_get_argv0(r->ee)) {
2182     argv0 = r->ee->tmp;
2183     len += strlen(argv0);
2184     }
2185     }
2186     }
2187     if (r->param_type == CCS_TYPE_PATH_ACL &&
2188     r->param.path.operation == CCS_TYPE_SYMLINK &&
2189     pref->learning_symlink_target) {
2190     symlink = r->obj->symlink_target->name;
2191     len += strlen(symlink);
2192     }
2193 kumaneko 3512 buffer = kmalloc(len, CCS_GFP_FLAGS);
2194 kumaneko 3746 if (buffer) {
2195     const bool handler = (current->ccs_flags &
2196     CCS_TASK_IS_EXECUTE_HANDLER)
2197     != 0;
2198     va_start(args, fmt);
2199     vsnprintf(buffer, len - 1, fmt, args);
2200     va_end(args);
2201     if (handler || realpath || argv0 || symlink) {
2202     ccs_addprintf(buffer, len, " if");
2203     if (handler)
2204     ccs_addprintf(buffer, len, " task.type"
2205     "=execute_handler");
2206     if (realpath)
2207     ccs_addprintf(buffer, len,
2208     " exec.realpath=\"%s\"",
2209     realpath);
2210     if (argv0)
2211     ccs_addprintf(buffer, len,
2212     " exec.argv[0]=\"%s\"",
2213     argv0);
2214     if (symlink)
2215     ccs_addprintf(buffer, len,
2216     " symlink.target=\"%s\"",
2217     symlink);
2218     }
2219     ccs_normalize_line(buffer);
2220     ccs_write_domain2(buffer, domain, false);
2221     kfree(buffer);
2222     }
2223     kfree(realpath);
2224 kumaneko 2897 return 0;
2225     }
2226 kumaneko 3746 if (r->mode != CCS_CONFIG_ENFORCING)
2227     return 0;
2228 kumaneko 2863 if (!atomic_read(&ccs_query_observers)) {
2229     int i;
2230     if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2231     return -EPERM;
2232 kumaneko 3627 for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2233 kumaneko 2958 enforcing_penalty; i++) {
2234 kumaneko 2863 set_current_state(TASK_INTERRUPTIBLE);
2235     schedule_timeout(HZ / 10);
2236     }
2237     return -EPERM;
2238     }
2239 kumaneko 3694 header = ccs_init_log(&len, r);
2240 kumaneko 2863 if (!header)
2241     goto out;
2242 kumaneko 3512 ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2243 kumaneko 2863 if (!ccs_query_entry)
2244     goto out;
2245 kumaneko 2900 len = ccs_round2(len);
2246 kumaneko 3512 ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2247 kumaneko 2863 if (!ccs_query_entry->query)
2248     goto out;
2249     INIT_LIST_HEAD(&ccs_query_entry->list);
2250     spin_lock(&ccs_query_list_lock);
2251     if (ccs_quota_for_query && ccs_query_memory_size + len +
2252     sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
2253     quota_exceeded = true;
2254     } else {
2255     ccs_query_memory_size += len + sizeof(*ccs_query_entry);
2256     ccs_query_entry->serial = ccs_serial++;
2257     }
2258     spin_unlock(&ccs_query_list_lock);
2259     if (quota_exceeded)
2260     goto out;
2261 kumaneko 3746 snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
2262     ccs_query_entry->serial, r->retry, header);
2263 kumaneko 2863 kfree(header);
2264     header = NULL;
2265 kumaneko 3746 ccs_addprintf(ccs_query_entry->query, len, fmt, args);
2266 kumaneko 2863 ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
2267     spin_lock(&ccs_query_list_lock);
2268     list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2269     spin_unlock(&ccs_query_list_lock);
2270     /* Give 10 seconds for supervisor's opinion. */
2271     for (ccs_query_entry->timer = 0;
2272     atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;
2273     ccs_query_entry->timer++) {
2274     wake_up(&ccs_query_wait);
2275     set_current_state(TASK_INTERRUPTIBLE);
2276     schedule_timeout(HZ / 10);
2277     if (ccs_query_entry->answer)
2278     break;
2279     }
2280     spin_lock(&ccs_query_list_lock);
2281     list_del(&ccs_query_entry->list);
2282     ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
2283     spin_unlock(&ccs_query_list_lock);
2284     switch (ccs_query_entry->answer) {
2285     case 3: /* Asked to retry by administrator. */
2286 kumaneko 3494 error = CCS_RETRY_REQUEST;
2287 kumaneko 2863 r->retry++;
2288     break;
2289     case 1:
2290     /* Granted by administrator. */
2291     error = 0;
2292     break;
2293     case 0:
2294     /* Timed out. */
2295     break;
2296     default:
2297     /* Rejected by administrator. */
2298     break;
2299     }
2300     out:
2301     if (ccs_query_entry)
2302     kfree(ccs_query_entry->query);
2303     kfree(ccs_query_entry);
2304     kfree(header);
2305     return error;
2306     }
2307    
2308     /**
2309     * ccs_poll_query - poll() for /proc/ccs/query.
2310