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

Subversion リポジトリの参照

Annotation of /branches/ccs-patch/security/ccsecurity/policy_io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3692 - (hide annotations) (download) (as text)
Sun May 23 07:27:24 2010 UTC (14 years ago) by kumaneko
File MIME type: text/x-csrc
File size: 78302 byte(s)


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