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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3691 - (hide annotations) (download) (as text)
Sun May 23 03:22:24 2010 UTC (14 years ago) by kumaneko
File MIME type: text/x-csrc
File size: 78056 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     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))
890     return ccs_write_capability_policy(data, domain, cond,
891     is_delete);
892     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))
893     return ccs_write_network_policy(data, domain, cond, is_delete);
894     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))
895     return ccs_write_signal_policy(data, domain, cond, is_delete);
896     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))
897     return ccs_write_env_policy(data, domain, cond, is_delete);
898     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))
899     return ccs_write_mount_policy(data, domain, cond, is_delete);
900     return ccs_write_file_policy(data, domain, cond, is_delete);
901     }
902    
903 kumaneko 2863 /**
904     * ccs_write_domain_policy - Write domain policy.
905     *
906     * @head: Pointer to "struct ccs_io_buffer".
907     *
908     * Returns 0 on success, negative value otherwise.
909     */
910     static int ccs_write_domain_policy(struct ccs_io_buffer *head)
911     {
912     char *data = head->write_buf;
913     struct ccs_domain_info *domain = head->write_var1;
914     bool is_delete = false;
915     bool is_select = false;
916     unsigned int profile;
917     struct ccs_condition *cond = NULL;
918     char *cp;
919     int error;
920 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
921 kumaneko 2863 is_delete = true;
922 kumaneko 2892 else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
923 kumaneko 2863 is_select = true;
924     if (is_select && ccs_is_select_one(head, data))
925     return 0;
926     /* Don't allow updating policies by non manager programs. */
927     if (!ccs_is_policy_manager())
928     return -EPERM;
929     if (ccs_is_domain_def(data)) {
930     domain = NULL;
931     if (is_delete)
932     ccs_delete_domain(data);
933     else if (is_select)
934     domain = ccs_find_domain(data);
935     else
936     domain = ccs_find_or_assign_new_domain(data, 0);
937     head->write_var1 = domain;
938     return 0;
939     }
940     if (!domain)
941     return -EINVAL;
942    
943 kumaneko 2892 if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
944     && profile < CCS_MAX_PROFILES) {
945 kumaneko 2991 if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
946 kumaneko 2863 domain->profile = (u8) profile;
947     return 0;
948     }
949 kumaneko 2892 if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
950 kumaneko 2863 domain->ignore_global_allow_read = !is_delete;
951     return 0;
952     }
953 kumaneko 2892 if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
954 kumaneko 2863 domain->ignore_global_allow_env = !is_delete;
955     return 0;
956     }
957 kumaneko 3258 if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {
958     domain->quota_warned = !is_delete;
959     return 0;
960     }
961     if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {
962     domain->domain_transition_failed = !is_delete;
963     return 0;
964     }
965 kumaneko 2863 cp = ccs_find_condition_part(data);
966     if (cp) {
967     cond = ccs_get_condition(cp);
968     if (!cond)
969     return -EINVAL;
970     }
971 kumaneko 2897 error = ccs_write_domain_policy2(data, domain, cond, is_delete);
972 kumaneko 2863 if (cond)
973     ccs_put_condition(cond);
974     return error;
975     }
976    
977 kumaneko 2948 /**
978     * ccs_print_name_union - Print a ccs_name_union.
979     *
980     * @head: Pointer to "struct ccs_io_buffer".
981     * @ptr: Pointer to "struct ccs_name_union".
982     *
983     * Returns true on success, false otherwise.
984     */
985 kumaneko 2888 static bool ccs_print_name_union(struct ccs_io_buffer *head,
986 kumaneko 2894 const struct ccs_name_union *ptr)
987 kumaneko 2863 {
988 kumaneko 2894 int pos = head->read_avail;
989 kumaneko 2863 if (pos && head->read_buf[pos - 1] == ' ')
990     head->read_avail--;
991 kumaneko 2888 if (ptr->is_group)
992 kumaneko 2870 return ccs_io_printf(head, " @%s",
993 kumaneko 2888 ptr->group->group_name->name);
994     return ccs_io_printf(head, " %s", ptr->filename->name);
995 kumaneko 2863 }
996    
997 kumaneko 2948 /**
998     * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.
999     *
1000     * @head: Pointer to "struct ccs_io_buffer".
1001     * @ptr: Pointer to "struct ccs_name_union".
1002     *
1003     * Returns true on success, false otherwise.
1004     */
1005 kumaneko 2894 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1006     const struct ccs_name_union *ptr)
1007 kumaneko 2863 {
1008 kumaneko 2894 if (ptr->is_group)
1009     return ccs_io_printf(head, "@%s",
1010     ptr->group->group_name->name);
1011     return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1012     }
1013    
1014 kumaneko 3690 static void ccs_print_number(char *buffer, int buffer_len,
1015     const struct ccs_number_union *ptr)
1016     {
1017     int i;
1018     unsigned long min = ptr->values[0];
1019     const unsigned long max = ptr->values[1];
1020     u8 min_type = ptr->value_type[0];
1021     const u8 max_type = ptr->value_type[1];
1022     memset(buffer, 0, buffer_len);
1023     buffer_len -= 2;
1024     for (i = 0; i < 2; i++) {
1025     int len;
1026     switch (min_type) {
1027     case CCS_VALUE_TYPE_HEXADECIMAL:
1028     snprintf(buffer, buffer_len, "0x%lX", min);
1029     break;
1030     case CCS_VALUE_TYPE_OCTAL:
1031     snprintf(buffer, buffer_len, "0%lo", min);
1032     break;
1033     default:
1034     snprintf(buffer, buffer_len, "%lu", min);
1035     break;
1036     }
1037     if (min == max && min_type == max_type)
1038     break;
1039     len = strlen(buffer);
1040     buffer[len++] = '-';
1041     buffer += len;
1042     buffer_len -= len;
1043     min_type = max_type;
1044     min = max;
1045     }
1046     }
1047    
1048 kumaneko 2948 /**
1049     * ccs_print_number_union_common - Print a ccs_number_union.
1050     *
1051     * @head: Pointer to "struct ccs_io_buffer".
1052     * @ptr: Pointer to "struct ccs_number_union".
1053     * @need_space: True if a space character is needed.
1054     *
1055     * Returns true on success, false otherwise.
1056     */
1057 kumaneko 2894 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
1058     const struct ccs_number_union *ptr,
1059     const bool need_space)
1060     {
1061 kumaneko 3690 char buffer[128];
1062 kumaneko 2894 if (need_space && !ccs_io_printf(head, " "))
1063     return false;
1064 kumaneko 2888 if (ptr->is_group)
1065 kumaneko 2894 return ccs_io_printf(head, "@%s",
1066 kumaneko 2888 ptr->group->group_name->name);
1067 kumaneko 3690 ccs_print_number(buffer, sizeof(buffer), ptr);
1068     return ccs_io_printf(head, "%s", buffer);
1069 kumaneko 2863 }
1070    
1071 kumaneko 2948 /**
1072     * ccs_print_number_union - Print a ccs_number_union.
1073     *
1074     * @head: Pointer to "struct ccs_io_buffer".
1075     * @ptr: Pointer to "struct ccs_number_union".
1076     *
1077     * Returns true on success, false otherwise.
1078     */
1079 kumaneko 2900 bool ccs_print_number_union(struct ccs_io_buffer *head,
1080     const struct ccs_number_union *ptr)
1081 kumaneko 2894 {
1082     return ccs_print_number_union_common(head, ptr, true);
1083     }
1084    
1085 kumaneko 2948 /**
1086     * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.
1087     *
1088     * @head: Pointer to "struct ccs_io_buffer".
1089     * @ptr: Pointer to "struct ccs_number_union".
1090     *
1091     * Returns true on success, false otherwise.
1092     */
1093 kumaneko 2894 static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1094     const struct ccs_number_union *ptr)
1095     {
1096     return ccs_print_number_union_common(head, ptr, false);
1097     }
1098    
1099 kumaneko 2863 /**
1100 kumaneko 2894 * ccs_print_condition - Print condition part.
1101     *
1102     * @head: Pointer to "struct ccs_io_buffer".
1103     * @cond: Pointer to "struct ccs_condition". May be NULL.
1104     *
1105     * Returns true on success, false otherwise.
1106     */
1107     static bool ccs_print_condition(struct ccs_io_buffer *head,
1108     const struct ccs_condition *cond)
1109     {
1110     const struct ccs_condition_element *condp;
1111     const struct ccs_number_union *numbers_p;
1112     const struct ccs_name_union *names_p;
1113     const struct ccs_argv_entry *argv;
1114     const struct ccs_envp_entry *envp;
1115     u16 condc;
1116     u16 i;
1117     u16 j;
1118     char buffer[32];
1119     if (!cond)
1120     goto no_condition;
1121     condc = cond->condc;
1122     condp = (const struct ccs_condition_element *) (cond + 1);
1123     numbers_p = (const struct ccs_number_union *) (condp + condc);
1124     names_p = (const struct ccs_name_union *)
1125     (numbers_p + cond->numbers_count);
1126     argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);
1127     envp = (const struct ccs_envp_entry *) (argv + cond->argc);
1128     memset(buffer, 0, sizeof(buffer));
1129     if (condc && !ccs_io_printf(head, "%s", " if"))
1130     goto out;
1131     for (i = 0; i < condc; i++) {
1132     const u8 match = condp->equals;
1133     const u8 left = condp->left;
1134     const u8 right = condp->right;
1135     condp++;
1136     switch (left) {
1137     case CCS_ARGV_ENTRY:
1138     if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",
1139     argv->index, argv->is_not ?
1140     "!=" : "=", argv->value->name))
1141     goto out;
1142     argv++;
1143     continue;
1144     case CCS_ENVP_ENTRY:
1145     if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",
1146     envp->name->name, envp->is_not ?
1147     "!=" : "="))
1148     goto out;
1149     if (envp->value) {
1150     if (!ccs_io_printf(head, "\"%s\"",
1151     envp->value->name))
1152     goto out;
1153     } else {
1154     if (!ccs_io_printf(head, "NULL"))
1155     goto out;
1156     }
1157     envp++;
1158     continue;
1159     case CCS_NUMBER_UNION:
1160     if (!ccs_print_number_union(head, numbers_p++))
1161     goto out;
1162     break;
1163     default:
1164     if (left >= CCS_MAX_CONDITION_KEYWORD)
1165     goto out;
1166     if (!ccs_io_printf(head, " %s",
1167     ccs_condition_keyword[left]))
1168     goto out;
1169     break;
1170     }
1171     if (!ccs_io_printf(head, "%s", match ? "=" : "!="))
1172     goto out;
1173     switch (right) {
1174     case CCS_NAME_UNION:
1175     if (!ccs_print_name_union_quoted(head, names_p++))
1176     goto out;
1177     break;
1178     case CCS_NUMBER_UNION:
1179     if (!ccs_print_number_union_nospace(head, numbers_p++))
1180     goto out;
1181     break;
1182     default:
1183     if (right >= CCS_MAX_CONDITION_KEYWORD)
1184     goto out;
1185     if (!ccs_io_printf(head, "%s",
1186     ccs_condition_keyword[right]))
1187     goto out;
1188     break;
1189     }
1190     }
1191     i = cond->post_state[3];
1192     if (!i)
1193     goto no_condition;
1194     if (!ccs_io_printf(head, " ; set"))
1195     goto out;
1196     for (j = 0; j < 3; j++) {
1197     if (!(i & (1 << j)))
1198     continue;
1199     if (!ccs_io_printf(head, " task.state[%u]=%u", j,
1200     cond->post_state[j]))
1201     goto out;
1202     }
1203     no_condition:
1204     if (ccs_io_printf(head, "\n"))
1205     return true;
1206     out:
1207     return false;
1208     }
1209    
1210     /**
1211 kumaneko 3106 * ccs_print_path_acl - Print a path ACL entry.
1212 kumaneko 2863 *
1213     * @head: Pointer to "struct ccs_io_buffer".
1214 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_acl".
1215 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1216     *
1217     * Returns true on success, false otherwise.
1218     */
1219 kumaneko 2915 static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1220 kumaneko 2943 struct ccs_path_acl *ptr,
1221     const struct ccs_condition *cond)
1222 kumaneko 2863 {
1223     int pos;
1224     u8 bit;
1225     const u16 perm = ptr->perm;
1226 kumaneko 2915 for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
1227 kumaneko 2863 if (!(perm & (1 << bit)))
1228     continue;
1229 kumaneko 3484 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1230     && bit != CCS_TYPE_TRANSIT)
1231 kumaneko 2863 continue;
1232     /* Print "read/write" instead of "read" and "write". */
1233 kumaneko 2915 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1234     && (perm & (1 << CCS_TYPE_READ_WRITE)))
1235 kumaneko 2863 continue;
1236     pos = head->read_avail;
1237 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||
1238 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
1239 kumaneko 2897 !ccs_print_condition(head, cond)) {
1240     head->read_bit = bit;
1241     head->read_avail = pos;
1242     return false;
1243     }
1244 kumaneko 2863 }
1245     head->read_bit = 0;
1246     return true;
1247     }
1248    
1249     /**
1250 kumaneko 2922 * ccs_print_path_number3_acl - Print a path_number3 ACL entry.
1251 kumaneko 2863 *
1252     * @head: Pointer to "struct ccs_io_buffer".
1253 kumaneko 2922 * @ptr: Pointer to "struct ccs_path_number3_acl".
1254 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1255     *
1256     * Returns true on success, false otherwise.
1257     */
1258 kumaneko 2922 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,
1259 kumaneko 2943 struct ccs_path_number3_acl *ptr,
1260     const struct ccs_condition *cond)
1261 kumaneko 2863 {
1262     int pos;
1263     u8 bit;
1264     const u16 perm = ptr->perm;
1265 kumaneko 2922 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;
1266 kumaneko 2916 bit++) {
1267 kumaneko 2863 if (!(perm & (1 << bit)))
1268     continue;
1269     pos = head->read_avail;
1270 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
1271 kumaneko 2922 ccs_path_number32keyword(bit)) ||
1272 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
1273 kumaneko 2922 !ccs_print_number_union(head, &ptr->mode) ||
1274 kumaneko 2888 !ccs_print_number_union(head, &ptr->major) ||
1275     !ccs_print_number_union(head, &ptr->minor) ||
1276 kumaneko 2897 !ccs_print_condition(head, cond)) {
1277     head->read_bit = bit;
1278     head->read_avail = pos;
1279     return false;
1280     }
1281 kumaneko 2863 }
1282     head->read_bit = 0;
1283     return true;
1284     }
1285    
1286     /**
1287 kumaneko 2948 * ccs_print_path2_acl - Print a path2 ACL entry.
1288 kumaneko 2863 *
1289     * @head: Pointer to "struct ccs_io_buffer".
1290 kumaneko 2922 * @ptr: Pointer to "struct ccs_path2_acl".
1291 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1292     *
1293     * Returns true on success, false otherwise.
1294     */
1295 kumaneko 2922 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1296 kumaneko 2943 struct ccs_path2_acl *ptr,
1297     const struct ccs_condition *cond)
1298 kumaneko 2863 {
1299     int pos;
1300     u8 bit;
1301     const u8 perm = ptr->perm;
1302 kumaneko 2922 for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1303 kumaneko 2863 if (!(perm & (1 << bit)))
1304     continue;
1305     pos = head->read_avail;
1306 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
1307 kumaneko 2922 ccs_path22keyword(bit)) ||
1308 kumaneko 2888 !ccs_print_name_union(head, &ptr->name1) ||
1309     !ccs_print_name_union(head, &ptr->name2) ||
1310 kumaneko 2897 !ccs_print_condition(head, cond)) {
1311     head->read_bit = bit;
1312     head->read_avail = pos;
1313     return false;
1314     }
1315 kumaneko 2863 }
1316     head->read_bit = 0;
1317     return true;
1318     }
1319    
1320     /**
1321 kumaneko 2948 * ccs_print_path_number_acl - Print a path_number ACL entry.
1322 kumaneko 2863 *
1323     * @head: Pointer to "struct ccs_io_buffer".
1324 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_number_acl".
1325 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1326     *
1327     * Returns true on success, false otherwise.
1328     */
1329 kumaneko 2871 static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1330 kumaneko 2915 struct ccs_path_number_acl *ptr,
1331 kumaneko 2871 const struct ccs_condition *cond)
1332 kumaneko 2863 {
1333 kumaneko 2871 int pos;
1334     u8 bit;
1335     const u8 perm = ptr->perm;
1336 kumaneko 2916 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1337     bit++) {
1338 kumaneko 2871 if (!(perm & (1 << bit)))
1339     continue;
1340     pos = head->read_avail;
1341 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
1342     ccs_path_number2keyword(bit)) ||
1343 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
1344     !ccs_print_number_union(head, &ptr->number) ||
1345 kumaneko 2897 !ccs_print_condition(head, cond)) {
1346     head->read_bit = bit;
1347     head->read_avail = pos;
1348     return false;
1349     }
1350 kumaneko 2871 }
1351     head->read_bit = 0;
1352 kumaneko 2863 return true;
1353     }
1354    
1355     /**
1356     * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1357     *
1358     * @head: Pointer to "struct ccs_io_buffer".
1359 kumaneko 2915 * @ptr: Pointer to "struct ccs_env_acl".
1360 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1361     *
1362     * Returns true on success, false otherwise.
1363     */
1364     static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1365 kumaneko 2915 struct ccs_env_acl *ptr,
1366 kumaneko 2863 const struct ccs_condition *cond)
1367     {
1368 kumaneko 2897 const int pos = head->read_avail;
1369     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1370     !ccs_print_condition(head, cond)) {
1371     head->read_avail = pos;
1372     return false;
1373     }
1374 kumaneko 2863 return true;
1375     }
1376    
1377     /**
1378     * ccs_print_capability_acl - Print a capability ACL entry.
1379     *
1380     * @head: Pointer to "struct ccs_io_buffer".
1381 kumaneko 2915 * @ptr: Pointer to "struct ccs_capability_acl".
1382 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1383     *
1384     * Returns true on success, false otherwise.
1385     */
1386     static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1387 kumaneko 2915 struct ccs_capability_acl *ptr,
1388 kumaneko 2863 const struct ccs_condition *cond)
1389     {
1390 kumaneko 2897 const int pos = head->read_avail;
1391 kumaneko 2892 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1392 kumaneko 2897 ccs_cap2keyword(ptr->operation)) ||
1393     !ccs_print_condition(head, cond)) {
1394     head->read_avail = pos;
1395     return false;
1396     }
1397 kumaneko 2863 return true;
1398     }
1399    
1400     /**
1401     * ccs_print_network_acl - Print a network ACL entry.
1402     *
1403     * @head: Pointer to "struct ccs_io_buffer".
1404 kumaneko 2915 * @ptr: Pointer to "struct ccs_ip_network_acl".
1405 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1406     *
1407     * Returns true on success, false otherwise.
1408     */
1409     static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1410 kumaneko 2915 struct ccs_ip_network_acl *ptr,
1411 kumaneko 2863 const struct ccs_condition *cond)
1412     {
1413 kumaneko 2916 int pos;
1414     u8 bit;
1415     const u16 perm = ptr->perm;
1416 kumaneko 3690 char buf[128];
1417 kumaneko 2916 for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1418 kumaneko 3690 const char *w[2] = { "", "" };
1419 kumaneko 2916 if (!(perm & (1 << bit)))
1420     continue;
1421     pos = head->read_avail;
1422     switch (ptr->address_type) {
1423     case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1424 kumaneko 3690 w[0] = "@";
1425     w[1] = ptr->address.group->group_name->name;
1426 kumaneko 2916 break;
1427     case CCS_IP_ADDRESS_TYPE_IPv4:
1428 kumaneko 3690 ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1429     ptr->address.ipv4.max);
1430     w[0] = buf;
1431 kumaneko 2916 break;
1432     case CCS_IP_ADDRESS_TYPE_IPv6:
1433 kumaneko 3690 ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1434     ptr->address.ipv6.max);
1435     w[0] = buf;
1436 kumaneko 2916 break;
1437     }
1438 kumaneko 3690 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1439     ccs_net2keyword(bit), w[0], w[1]) ||
1440     !ccs_print_number_union(head, &ptr->port) ||
1441 kumaneko 2916 !ccs_print_condition(head, cond))
1442 kumaneko 2863 goto out;
1443     }
1444 kumaneko 2916 head->read_bit = 0;
1445 kumaneko 2863 return true;
1446     out:
1447 kumaneko 2916 head->read_bit = bit;
1448 kumaneko 2863 head->read_avail = pos;
1449     return false;
1450     }
1451    
1452     /**
1453     * ccs_print_signal_acl - Print a signal ACL entry.
1454     *
1455     * @head: Pointer to "struct ccs_io_buffer".
1456 kumaneko 2915 * @ptr: Pointer to "struct signale_acl".
1457 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1458     *
1459     * Returns true on success, false otherwise.
1460     */
1461     static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1462 kumaneko 2915 struct ccs_signal_acl *ptr,
1463 kumaneko 2863 const struct ccs_condition *cond)
1464     {
1465 kumaneko 2897 const int pos = head->read_avail;
1466 kumaneko 2892 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1467 kumaneko 2897 ptr->sig, ptr->domainname->name) ||
1468     !ccs_print_condition(head, cond)) {
1469     head->read_avail = pos;
1470     return false;
1471     }
1472 kumaneko 2863 return true;
1473     }
1474    
1475     /**
1476     * ccs_print_execute_handler_record - Print an execute handler ACL entry.
1477     *
1478     * @head: Pointer to "struct ccs_io_buffer".
1479     * @keyword: Name of the keyword.
1480     * @ptr: Pointer to "struct ccs_execute_handler_record".
1481     *
1482     * Returns true on success, false otherwise.
1483     */
1484     static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,
1485     const char *keyword,
1486 kumaneko 3625 struct ccs_execute_handler_record
1487     *ptr)
1488 kumaneko 2863 {
1489     return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1490     }
1491    
1492     /**
1493     * ccs_print_mount_acl - Print a mount ACL entry.
1494     *
1495     * @head: Pointer to "struct ccs_io_buffer".
1496 kumaneko 2915 * @ptr: Pointer to "struct ccs_mount_acl".
1497 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1498     *
1499     * Returns true on success, false otherwise.
1500     */
1501     static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1502 kumaneko 2915 struct ccs_mount_acl *ptr,
1503 kumaneko 2863 const struct ccs_condition *cond)
1504     {
1505 kumaneko 2897 const int pos = head->read_avail;
1506     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1507     !ccs_print_name_union(head, &ptr->dev_name) ||
1508     !ccs_print_name_union(head, &ptr->dir_name) ||
1509     !ccs_print_name_union(head, &ptr->fs_type) ||
1510     !ccs_print_number_union(head, &ptr->flags) ||
1511     !ccs_print_condition(head, cond)) {
1512     head->read_avail = pos;
1513     return false;
1514     }
1515 kumaneko 2863 return true;
1516     }
1517    
1518     /**
1519     * ccs_print_entry - Print an ACL entry.
1520     *
1521     * @head: Pointer to "struct ccs_io_buffer".
1522     * @ptr: Pointer to an ACL entry.
1523     *
1524     * Returns true on success, false otherwise.
1525     */
1526     static bool ccs_print_entry(struct ccs_io_buffer *head,
1527     struct ccs_acl_info *ptr)
1528     {
1529     const struct ccs_condition *cond = ptr->cond;
1530 kumaneko 2900 const u8 acl_type = ptr->type;
1531     if (ptr->is_deleted)
1532 kumaneko 2863 return true;
1533 kumaneko 2915 if (acl_type == CCS_TYPE_PATH_ACL) {
1534     struct ccs_path_acl *acl
1535 kumaneko 3046 = container_of(ptr, struct ccs_path_acl, head);
1536 kumaneko 2915 return ccs_print_path_acl(head, acl, cond);
1537 kumaneko 2863 }
1538 kumaneko 2892 if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {
1539 kumaneko 2863 struct ccs_execute_handler_record *acl
1540     = container_of(ptr, struct ccs_execute_handler_record,
1541     head);
1542 kumaneko 2892 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;
1543 kumaneko 2863 return ccs_print_execute_handler_record(head, keyword, acl);
1544     }
1545 kumaneko 2892 if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1546 kumaneko 2863 struct ccs_execute_handler_record *acl
1547     = container_of(ptr, struct ccs_execute_handler_record,
1548     head);
1549 kumaneko 2892 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1550 kumaneko 2863 return ccs_print_execute_handler_record(head, keyword, acl);
1551     }
1552     if (head->read_execute_only)
1553     return true;
1554 kumaneko 2922 if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {
1555     struct ccs_path_number3_acl *acl
1556     = container_of(ptr, struct ccs_path_number3_acl, head);
1557     return ccs_print_path_number3_acl(head, acl, cond);
1558 kumaneko 2863 }
1559 kumaneko 2922 if (acl_type == CCS_TYPE_PATH2_ACL) {
1560     struct ccs_path2_acl *acl
1561 kumaneko 3106 = container_of(ptr, struct ccs_path2_acl, head);
1562 kumaneko 2922 return ccs_print_path2_acl(head, acl, cond);
1563 kumaneko 2863 }
1564 kumaneko 2892 if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1565 kumaneko 2915 struct ccs_path_number_acl *acl
1566 kumaneko 3106 = container_of(ptr, struct ccs_path_number_acl, head);
1567 kumaneko 2871 return ccs_print_path_number_acl(head, acl, cond);
1568 kumaneko 2863 }
1569 kumaneko 2892 if (acl_type == CCS_TYPE_ENV_ACL) {
1570 kumaneko 2915 struct ccs_env_acl *acl
1571     = container_of(ptr, struct ccs_env_acl, head);
1572 kumaneko 2863 return ccs_print_env_acl(head, acl, cond);
1573     }
1574 kumaneko 2892 if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1575 kumaneko 2915 struct ccs_capability_acl *acl
1576 kumaneko 3106 = container_of(ptr, struct ccs_capability_acl, head);
1577 kumaneko 2863 return ccs_print_capability_acl(head, acl, cond);
1578     }
1579 kumaneko 2892 if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1580 kumaneko 2915 struct ccs_ip_network_acl *acl
1581 kumaneko 3106 = container_of(ptr, struct ccs_ip_network_acl, head);
1582 kumaneko 2863 return ccs_print_network_acl(head, acl, cond);
1583     }
1584 kumaneko 2892 if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1585 kumaneko 2915 struct ccs_signal_acl *acl
1586     = container_of(ptr, struct ccs_signal_acl, head);
1587 kumaneko 2863 return ccs_print_signal_acl(head, acl, cond);
1588     }
1589 kumaneko 2892 if (acl_type == CCS_TYPE_MOUNT_ACL) {
1590 kumaneko 2915 struct ccs_mount_acl *acl
1591     = container_of(ptr, struct ccs_mount_acl, head);
1592 kumaneko 2863 return ccs_print_mount_acl(head, acl, cond);
1593     }
1594     BUG(); /* This must not happen. */
1595     return false;
1596     }
1597    
1598     /**
1599     * ccs_read_domain_policy - Read domain policy.
1600     *
1601     * @head: Pointer to "struct ccs_io_buffer".
1602     *
1603     * Caller holds ccs_read_lock().
1604     */
1605 kumaneko 2943 static void ccs_read_domain_policy(struct ccs_io_buffer *head)
1606 kumaneko 2863 {
1607     struct list_head *dpos;
1608     struct list_head *apos;
1609     if (head->read_eof)
1610 kumaneko 2943 return;
1611 kumaneko 2863 if (head->read_step == 0)
1612     head->read_step = 1;
1613     list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
1614     struct ccs_domain_info *domain;
1615     const char *quota_exceeded = "";
1616     const char *transition_failed = "";
1617     const char *ignore_global_allow_read = "";
1618     const char *ignore_global_allow_env = "";
1619     domain = list_entry(dpos, struct ccs_domain_info, list);
1620     if (head->read_step != 1)
1621     goto acl_loop;
1622     if (domain->is_deleted && !head->read_single_domain)
1623     continue;
1624     /* Print domainname and flags. */
1625     if (domain->quota_warned)
1626 kumaneko 3258 quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";
1627 kumaneko 2863 if (domain->domain_transition_failed)
1628 kumaneko 3258 transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";
1629 kumaneko 2863 if (domain->ignore_global_allow_read)
1630     ignore_global_allow_read
1631 kumaneko 2892 = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
1632 kumaneko 2863 if (domain->ignore_global_allow_env)
1633     ignore_global_allow_env
1634 kumaneko 2892 = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
1635     if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"
1636 kumaneko 2863 "%s%s%s%s\n", domain->domainname->name,
1637     domain->profile, quota_exceeded,
1638     transition_failed,
1639     ignore_global_allow_read,
1640     ignore_global_allow_env))
1641 kumaneko 2943 return;
1642 kumaneko 2863 head->read_step = 2;
1643     acl_loop:
1644     if (head->read_step == 3)
1645     goto tail_mark;
1646     /* Print ACL entries in the domain. */
1647     list_for_each_cookie(apos, head->read_var2,
1648     &domain->acl_info_list) {
1649     struct ccs_acl_info *ptr
1650     = list_entry(apos, struct ccs_acl_info, list);
1651     if (!ccs_print_entry(head, ptr))
1652 kumaneko 2943 return;
1653 kumaneko 2863 }
1654     head->read_step = 3;
1655     tail_mark:
1656     if (!ccs_io_printf(head, "\n"))
1657 kumaneko 2943 return;
1658 kumaneko 2863 head->read_step = 1;
1659     if (head->read_single_domain)
1660     break;
1661     }
1662     head->read_eof = true;
1663     }
1664    
1665     /**
1666     * ccs_write_domain_profile - Assign profile for specified domain.
1667     *
1668     * @head: Pointer to "struct ccs_io_buffer".
1669     *
1670     * Returns 0 on success, -EINVAL otherwise.
1671     *
1672     * This is equivalent to doing
1673     *
1674     * ( echo "select " $domainname; echo "use_profile " $profile ) |
1675 kumaneko 2948 * /usr/sbin/ccs-loadpolicy -d
1676 kumaneko 2863 *
1677     * Caller holds ccs_read_lock().
1678     */
1679     static int ccs_write_domain_profile(struct ccs_io_buffer *head)
1680     {
1681     char *data = head->write_buf;
1682     char *cp = strchr(data, ' ');
1683     struct ccs_domain_info *domain;
1684     unsigned int profile;
1685     if (!cp)
1686     return -EINVAL;
1687     *cp = '\0';
1688     profile = simple_strtoul(data, NULL, 10);
1689 kumaneko 2892 if (profile >= CCS_MAX_PROFILES)
1690 kumaneko 2863 return -EINVAL;
1691     domain = ccs_find_domain(cp + 1);
1692 kumaneko 2991 if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1693 kumaneko 2863 domain->profile = (u8) profile;
1694     return 0;
1695     }
1696    
1697     /**
1698     * ccs_read_domain_profile - Read only domainname and profile.
1699     *
1700     * @head: Pointer to "struct ccs_io_buffer".
1701     *
1702     * This is equivalent to doing
1703     *
1704     * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1705     * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1706     * domainname = $0; } else if ( $1 == "use_profile" ) {
1707     * print $2 " " domainname; domainname = ""; } } ; '
1708     *
1709     * Caller holds ccs_read_lock().
1710     */
1711 kumaneko 2943 static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1712 kumaneko 2863 {
1713     struct list_head *pos;
1714     if (head->read_eof)
1715 kumaneko 2943 return;
1716 kumaneko 2863 list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1717     struct ccs_domain_info *domain;
1718     domain = list_entry(pos, struct ccs_domain_info, list);
1719     if (domain->is_deleted)
1720     continue;
1721     if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1722     domain->domainname->name))
1723 kumaneko 2943 return;
1724 kumaneko 2863 }
1725     head->read_eof = true;
1726     }
1727    
1728     /**
1729     * ccs_write_pid: Specify PID to obtain domainname.
1730     *
1731     * @head: Pointer to "struct ccs_io_buffer".
1732     *
1733     * Returns 0.
1734     */
1735     static int ccs_write_pid(struct ccs_io_buffer *head)
1736     {
1737     head->read_eof = false;
1738     return 0;
1739     }
1740    
1741     /**
1742     * ccs_read_pid - Read information of a process.
1743     *
1744     * @head: Pointer to "struct ccs_io_buffer".
1745     *
1746     * Returns the domainname which the specified PID is in or
1747     * process information of the specified PID on success,
1748     * empty string otherwise.
1749     *
1750     * Caller holds ccs_read_lock().
1751     */
1752 kumaneko 2943 static void ccs_read_pid(struct ccs_io_buffer *head)
1753 kumaneko 2863 {
1754     char *buf = head->write_buf;
1755     bool task_info = false;
1756 kumaneko 2970 bool global_pid = false;
1757 kumaneko 2863 unsigned int pid;
1758     struct task_struct *p;
1759     struct ccs_domain_info *domain = NULL;
1760     u32 ccs_flags = 0;
1761     /* Accessing write_buf is safe because head->io_sem is held. */
1762 kumaneko 3136 if (!buf) {
1763     head->read_eof = true;
1764 kumaneko 2943 return; /* Do nothing if open(O_RDONLY). */
1765 kumaneko 3136 }
1766 kumaneko 2863 if (head->read_avail || head->read_eof)
1767 kumaneko 2943 return;
1768 kumaneko 2863 head->read_eof = true;
1769     if (ccs_str_starts(&buf, "info "))
1770     task_info = true;
1771 kumaneko 2970 if (ccs_str_starts(&buf, "global-pid "))
1772     global_pid = true;
1773 kumaneko 2863 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1774 kumaneko 3248 ccs_tasklist_lock();
1775 kumaneko 2970 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1776     if (global_pid)
1777 kumaneko 3502 p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1778 kumaneko 2970 else
1779 kumaneko 3502 p = ccsecurity_exports.find_task_by_vpid(pid);
1780 kumaneko 2970 #else
1781 kumaneko 2863 p = find_task_by_pid(pid);
1782 kumaneko 2970 #endif
1783 kumaneko 2863 if (p) {
1784     domain = ccs_task_domain(p);
1785     ccs_flags = p->ccs_flags;
1786     }
1787 kumaneko 3248 ccs_tasklist_unlock();
1788 kumaneko 2863 if (!domain)
1789 kumaneko 2943 return;
1790 kumaneko 2863 if (!task_info)
1791     ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1792     domain->domainname->name);
1793     else
1794     ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1795     "state[0]=%u state[1]=%u state[2]=%u", pid,
1796 kumaneko 2958 ccs_yesno(ccs_flags &
1797     CCS_TASK_IS_POLICY_MANAGER),
1798     ccs_yesno(ccs_flags &
1799     CCS_TASK_IS_EXECUTE_HANDLER),
1800 kumaneko 2863 (u8) (ccs_flags >> 24),
1801     (u8) (ccs_flags >> 16),
1802     (u8) (ccs_flags >> 8));
1803     }
1804    
1805     /**
1806     * ccs_write_exception_policy - Write exception policy.
1807     *
1808     * @head: Pointer to "struct ccs_io_buffer".
1809     *
1810     * Returns 0 on success, negative value otherwise.
1811     */
1812     static int ccs_write_exception_policy(struct ccs_io_buffer *head)
1813     {
1814     char *data = head->write_buf;
1815 kumaneko 3691 const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1816     u8 i;
1817     static const struct {
1818     const char *keyword;
1819     int (*write) (char *, const bool, const u8);
1820     } ccs_callback[13] = {
1821     { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper_policy },
1822     { CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1823     ccs_write_domain_initializer_policy },
1824     { CCS_KEYWORD_KEEP_DOMAIN, ccs_write_domain_keeper_policy },
1825     { CCS_KEYWORD_INITIALIZE_DOMAIN,
1826     ccs_write_domain_initializer_policy },
1827     { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator_policy },
1828     { CCS_KEYWORD_ALLOW_READ, ccs_write_globally_readable_policy },
1829     { CCS_KEYWORD_ALLOW_ENV,
1830     ccs_write_globally_usable_env_policy },
1831     { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern_policy },
1832     { CCS_KEYWORD_PATH_GROUP, ccs_write_path_group_policy },
1833     { CCS_KEYWORD_NUMBER_GROUP, ccs_write_number_group_policy },
1834     { CCS_KEYWORD_ADDRESS_GROUP, ccs_write_address_group_policy },
1835     { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite_policy },
1836     { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port_policy }
1837     };
1838     for (i = 0; i < 13; i++) {
1839     if (ccs_str_starts(&data, ccs_callback[i].keyword))
1840     return ccs_callback[i].write(data, is_delete, i < 2);
1841     }
1842 kumaneko 2863 return -EINVAL;
1843     }
1844    
1845     /**
1846 kumaneko 3689 * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1847 kumaneko 2863 *
1848     * @head: Pointer to "struct ccs_io_buffer".
1849 kumaneko 3689 * @idx: Index number.
1850 kumaneko 2863 *
1851 kumaneko 3689 * Returns true on success, false otherwise.
1852     *
1853 kumaneko 2863 * Caller holds ccs_read_lock().
1854     */
1855 kumaneko 3689 static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1856 kumaneko 2863 {
1857 kumaneko 3689 struct list_head *gpos;
1858     struct list_head *mpos;
1859 kumaneko 3690 const char *w[3] = { "", "", "" };
1860     if (idx == CCS_PATH_GROUP)
1861     w[0] = CCS_KEYWORD_PATH_GROUP;
1862     else if (idx == CCS_NUMBER_GROUP)
1863     w[0] = CCS_KEYWORD_NUMBER_GROUP;
1864     else if (idx == CCS_ADDRESS_GROUP)
1865     w[0] = CCS_KEYWORD_ADDRESS_GROUP;
1866 kumaneko 3689 list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1867     struct ccs_group *group =
1868     list_entry(gpos, struct ccs_group, head.list);
1869 kumaneko 3690 w[1] = group->group_name->name;
1870 kumaneko 3689 list_for_each_cookie(mpos, head->read_var2,
1871     &group->member_list) {
1872 kumaneko 3690 char buffer[128];
1873 kumaneko 3689 struct ccs_acl_head *ptr =
1874     list_entry(mpos, struct ccs_acl_head, list);
1875     if (ptr->is_deleted)
1876     continue;
1877     if (idx == CCS_PATH_GROUP) {
1878 kumaneko 3690 w[2] = container_of(ptr, struct ccs_path_group,
1879     head)->member_name->name;
1880 kumaneko 3689 } else if (idx == CCS_NUMBER_GROUP) {
1881 kumaneko 3690 w[2] = buffer;
1882     ccs_print_number(buffer, sizeof(buffer),
1883     &container_of
1884     (ptr, struct ccs_number_group,
1885     head)->number);
1886 kumaneko 3689 } else if (idx == CCS_ADDRESS_GROUP) {
1887     struct ccs_address_group *member =
1888     container_of(ptr, typeof(*member),
1889     head);
1890 kumaneko 3690 w[2] = buffer;
1891     if (member->is_ipv6)
1892     ccs_print_ipv6(buffer, sizeof(buffer),
1893     member->min.ipv6,
1894     member->max.ipv6);
1895     else
1896     ccs_print_ipv4(buffer, sizeof(buffer),
1897     member->min.ipv4,
1898     member->max.ipv4);
1899 kumaneko 3689 }
1900 kumaneko 3690 if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1901     w[2]))
1902     return false;
1903 kumaneko 3689 }
1904     }
1905     return true;
1906     }
1907    
1908     /**
1909     * ccs_read_policy - Read "struct ccs_..._entry" list.
1910     *
1911     * @head: Pointer to "struct ccs_io_buffer".
1912     * @idx: Index number.
1913     *
1914     * Returns true on success, false otherwise.
1915     *
1916     * Caller holds ccs_read_lock().
1917     */
1918     static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1919     {
1920     struct list_head *pos;
1921     list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1922     const char *w[4] = { "", "", "", "" };
1923     char buffer[16];
1924     struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1925     list);
1926     if (acl->is_deleted)
1927     continue;
1928     switch (idx) {
1929     case CCS_ID_DOMAIN_KEEPER:
1930     {
1931     struct ccs_domain_keeper_entry *ptr =
1932     container_of(acl, typeof(*ptr), head);
1933     w[0] = ptr->is_not ?
1934 kumaneko 3691 CCS_KEYWORD_NO_KEEP_DOMAIN :
1935 kumaneko 3689 CCS_KEYWORD_KEEP_DOMAIN;
1936     if (ptr->program) {
1937     w[1] = ptr->program->name;
1938     w[2] = " from ";
1939     }
1940     w[3] = ptr->domainname->name;
1941     }
1942 kumaneko 2863 break;
1943 kumaneko 3689 case CCS_ID_DOMAIN_INITIALIZER:
1944     {
1945     struct ccs_domain_initializer_entry *ptr =
1946     container_of(acl, typeof(*ptr), head);
1947     w[0] = ptr->is_not ?
1948 kumaneko 3691 CCS_KEYWORD_NO_INITIALIZE_DOMAIN :
1949 kumaneko 3689 CCS_KEYWORD_INITIALIZE_DOMAIN;
1950     w[1] = ptr->program->name;
1951     if (ptr->domainname) {
1952     w[2] = " from ";
1953     w[3] = ptr->domainname->name;
1954     }
1955     }
1956 kumaneko 2943 break;
1957 kumaneko 3689 case CCS_ID_AGGREGATOR:
1958     {
1959     struct ccs_aggregator_entry *ptr =
1960     container_of(acl, typeof(*ptr), head);
1961     w[0] = CCS_KEYWORD_AGGREGATOR;
1962     w[1] = ptr->original_name->name;
1963     w[2] = " ";
1964     w[3] = ptr->aggregated_name->name;
1965     }
1966 kumaneko 2943 break;
1967 kumaneko 3689 case CCS_ID_GLOBALLY_READABLE:
1968     {
1969     struct ccs_globally_readable_file_entry *ptr =
1970     container_of(acl, typeof(*ptr), head);
1971     w[0] = CCS_KEYWORD_ALLOW_READ;
1972     w[1] = ptr->filename->name;
1973     }
1974 kumaneko 2943 break;
1975 kumaneko 3689 case CCS_ID_PATTERN:
1976     {
1977     struct ccs_pattern_entry *ptr =
1978     container_of(acl, typeof(*ptr), head);
1979     w[0] = CCS_KEYWORD_FILE_PATTERN;
1980     w[1] = ptr->pattern->name;
1981     }
1982 kumaneko 2943 break;
1983 kumaneko 3689 case CCS_ID_NO_REWRITE:
1984     {
1985     struct ccs_no_rewrite_entry *ptr =
1986     container_of(acl, typeof(*ptr), head);
1987     w[0] = CCS_KEYWORD_DENY_REWRITE;
1988     w[1] = ptr->pattern->name;
1989     }
1990 kumaneko 2943 break;
1991 kumaneko 3689 case CCS_ID_GLOBAL_ENV:
1992     {
1993     struct ccs_globally_usable_env_entry *ptr =
1994     container_of(acl, typeof(*ptr), head);
1995     w[0] = CCS_KEYWORD_ALLOW_ENV;
1996     w[1] = ptr->env->name;
1997     }
1998 kumaneko 2943 break;
1999 kumaneko 3689 case CCS_ID_RESERVEDPORT:
2000     {
2001     struct ccs_reserved_entry *ptr =
2002     container_of(acl, typeof(*ptr), head);
2003     const u16 min_port = ptr->min_port;
2004     const u16 max_port = ptr->max_port;
2005     w[0] = CCS_KEYWORD_DENY_AUTOBIND;
2006     snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
2007     min_port, min_port != max_port ?
2008     '-' : '\0', max_port);
2009     buffer[sizeof(buffer) - 1] = '\0';
2010     w[1] = buffer;
2011     }
2012 kumaneko 2943 break;
2013 kumaneko 3689 }
2014     if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))
2015     return false;
2016 kumaneko 2863 }
2017 kumaneko 3689 return true;
2018 kumaneko 2863 }
2019    
2020 kumaneko 2897 /**
2021 kumaneko 3689 * ccs_read_exception_policy - Read exception policy.
2022     *
2023     * @head: Pointer to "struct ccs_io_buffer".
2024     *
2025     * Caller holds ccs_read_lock().
2026     */
2027     static void ccs_read_exception_policy(struct ccs_io_buffer *head)
2028     {
2029     if (head->read_eof)
2030     return;
2031     while (head->read_step < CCS_MAX_POLICY &&
2032     ccs_read_policy(head, head->read_step))
2033     head->read_step++;
2034     if (head->read_step < CCS_MAX_POLICY)
2035     return;
2036     while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2037     ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
2038     head->read_step++;
2039     head->read_eof = head->read_step == CCS_MAX_POLICY + CCS_MAX_GROUP;
2040     }
2041    
2042     /**
2043 kumaneko 2897 * ccs_get_argv0 - Get argv[0].
2044     *
2045     * @ee: Pointer to "struct ccs_execve_entry".
2046     *
2047     * Returns true on success, false otherwise.
2048     */
2049     static bool ccs_get_argv0(struct ccs_execve_entry *ee)
2050     {
2051     struct linux_binprm *bprm = ee->bprm;
2052     char *arg_ptr = ee->tmp;
2053     int arg_len = 0;
2054     unsigned long pos = bprm->p;
2055     int offset = pos % PAGE_SIZE;
2056     bool done = false;
2057     if (!bprm->argc)
2058     goto out;
2059     while (1) {
2060     if (!ccs_dump_page(bprm, pos, &ee->dump))
2061     goto out;
2062     pos += PAGE_SIZE - offset;
2063     /* Read. */
2064     while (offset < PAGE_SIZE) {
2065     const char *kaddr = ee->dump.data;
2066     const unsigned char c = kaddr[offset++];
2067 kumaneko 2932 if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
2068 kumaneko 2897 if (c == '\\') {
2069     arg_ptr[arg_len++] = '\\';
2070     arg_ptr[arg_len++] = '\\';
2071     } else if (c > ' ' && c < 127) {
2072     arg_ptr[arg_len++] = c;
2073     } else {
2074     arg_ptr[arg_len++] = '\\';
2075     arg_ptr[arg_len++] = (c >> 6) + '0';
2076     arg_ptr[arg_len++]
2077     = ((c >> 3) & 7) + '0';
2078     arg_ptr[arg_len++] = (c & 7) + '0';
2079     }
2080     } else {
2081     arg_ptr[arg_len] = '\0';
2082     done = true;
2083     break;
2084     }
2085     }
2086     offset = 0;
2087     if (done)
2088     break;
2089     }
2090     return true;
2091     out:
2092     return false;
2093     }
2094    
2095 kumaneko 2948 /**
2096     * ccs_get_execute_condition - Get condition part for execute requests.
2097     *
2098     * @ee: Pointer to "struct ccs_execve_entry".
2099     *
2100     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2101     */
2102 kumaneko 2897 static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry
2103     *ee)
2104     {
2105     struct ccs_condition *cond;
2106     char *buf;
2107     int len = 256;
2108     char *realpath = NULL;
2109     char *argv0 = NULL;
2110 kumaneko 3627 const struct ccs_profile *profile = ccs_profile(ccs_current_domain()->
2111     profile);
2112 kumaneko 2962 if (profile->learning->learning_exec_realpath) {
2113 kumaneko 2897 struct file *file = ee->bprm->file;
2114 kumaneko 2911 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2115     struct path path = { file->f_vfsmnt, file->f_dentry };
2116     realpath = ccs_realpath_from_path(&path);
2117     #else
2118     realpath = ccs_realpath_from_path(&file->f_path);
2119     #endif
2120 kumaneko 2897 if (realpath)
2121     len += strlen(realpath) + 17;
2122     }
2123 kumaneko 2962 if (profile->learning->learning_exec_argv0) {
2124 kumaneko 2897 if (ccs_get_argv0(ee)) {
2125     argv0 = ee->tmp;
2126     len += strlen(argv0) + 16;
2127     }
2128     }
2129 kumaneko 3512 buf = kmalloc(len, CCS_GFP_FLAGS);
2130 kumaneko 2958 if (!buf) {
2131     kfree(realpath);
2132 kumaneko 2897 return NULL;
2133 kumaneko 2958 }
2134 kumaneko 2897 snprintf(buf, len - 1, "if");
2135     if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2136     const int pos = strlen(buf);
2137     snprintf(buf + pos, len - pos - 1,
2138     " task.type=execute_handler");
2139     }
2140     if (realpath) {
2141     const int pos = strlen(buf);
2142     snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",
2143     realpath);
2144     kfree(realpath);
2145     }
2146     if (argv0) {
2147     const int pos = strlen(buf);
2148     snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",
2149     argv0);
2150     }
2151     cond = ccs_get_condition(buf);
2152     kfree(buf);
2153     return cond;
2154     }
2155    
2156 kumaneko 2961 /**
2157     * ccs_get_symlink_condition - Get condition part for symlink requests.
2158     *
2159     * @r: Pointer to "struct ccs_request_info".
2160     *
2161     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2162     */
2163     static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info
2164     *r)
2165     {
2166     struct ccs_condition *cond;
2167     char *buf;
2168     int len = 256;
2169     const char *symlink = NULL;
2170     const struct ccs_profile *profile = ccs_profile(r->profile);
2171 kumaneko 2962 if (profile->learning->learning_symlink_target) {
2172 kumaneko 2961 symlink = r->obj->symlink_target->name;
2173     len += strlen(symlink) + 18;
2174     }
2175 kumaneko 3512 buf = kmalloc(len, CCS_GFP_FLAGS);
2176 kumaneko 2961 if (!buf)
2177     return NULL;
2178     snprintf(buf, len - 1, "if");
2179     if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2180     const int pos = strlen(buf);
2181     snprintf(buf + pos, len - pos - 1,
2182     " task.type=execute_handler");
2183     }
2184     if (symlink) {
2185     const int pos = strlen(buf);
2186     snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",
2187     symlink);
2188     }
2189     cond = ccs_get_condition(buf);
2190     kfree(buf);
2191     return cond;
2192     }
2193    
2194 kumaneko 2863 /* Wait queue for ccs_query_list. */
2195     static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2196    
2197     /* Lock for manipulating ccs_query_list. */
2198     static DEFINE_SPINLOCK(ccs_query_list_lock);
2199    
2200     /* Structure for query. */
2201     struct ccs_query_entry {
2202     struct list_head list;
2203     char *query;
2204     int query_len;
2205     unsigned int serial;
2206     int timer;
2207     int answer;
2208     };
2209    
2210     /* The list for "struct ccs_query_entry". */
2211     static LIST_HEAD(ccs_query_list);
2212    
2213     /* Number of "struct file" referring /proc/ccs/query interface. */
2214     static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2215    
2216     /**
2217 kumaneko 2922 * ccs_supervisor - Ask for the supervisor's decision.
2218 kumaneko 2863 *
2219     * @r: Pointer to "struct ccs_request_info".
2220     * @fmt: The printf()'s format string, followed by parameters.
2221     *
2222     * Returns 0 if the supervisor decided to permit the access request which
2223 kumaneko 3494 * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2224     * decided to retry the access request which violated the policy in enforcing
2225     * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2226 kumaneko 2863 */
2227 kumaneko 2922 int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2228 kumaneko 2863 {
2229     va_list args;
2230     int error = -EPERM;
2231     int pos;
2232     int len;
2233     static unsigned int ccs_serial;
2234     struct ccs_query_entry *ccs_query_entry = NULL;
2235     bool quota_exceeded = false;
2236     char *header;
2237 kumaneko 3627 struct ccs_domain_info * const domain = ccs_current_domain();
2238 kumaneko 2897 switch (r->mode) {
2239     char *buffer;
2240     struct ccs_condition *cond;
2241 kumaneko 2958 case CCS_CONFIG_LEARNING:
2242 kumaneko 2897 if (!ccs_domain_quota_ok(r))
2243     return 0;
2244     va_start(args, fmt);
2245     len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
2246     va_end(args);
2247 kumaneko 3512 buffer = kmalloc(len, CCS_GFP_FLAGS);
2248 kumaneko 2897 if (!buffer)
2249     return 0;
2250     va_start(args, fmt);
2251     vsnprintf(buffer, len - 1, fmt, args);
2252     va_end(args);
2253     ccs_normalize_line(buffer);
2254     if (r->ee && !strncmp(buffer, "allow_execute ", 14))
2255     cond = ccs_get_execute_condition(r->ee);
2256 kumaneko 2961 else if (r->obj && r->obj->symlink_target)
2257     cond = ccs_get_symlink_condition(r);
2258 kumaneko 2897 else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
2259     char str[] = "if task.type=execute_handler";
2260     cond = ccs_get_condition(str);
2261     } else
2262     cond = NULL;
2263 kumaneko 3627 ccs_write_domain_policy2(buffer, domain, cond, false);
2264 kumaneko 2897 ccs_put_condition(cond);
2265     kfree(buffer);
2266     /* fall through */
2267 kumaneko 2958 case CCS_CONFIG_PERMISSIVE:
2268 kumaneko 2897 return 0;
2269     }
2270 kumaneko 2863 if (!atomic_read(&ccs_query_observers)) {
2271     int i;
2272     if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2273     return -EPERM;
2274 kumaneko 3627 for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2275 kumaneko 2958 enforcing_penalty; i++) {
2276 kumaneko 2863 set_current_state(TASK_INTERRUPTIBLE);
2277     schedule_timeout(HZ / 10);
2278     }
2279     return -EPERM;
2280     }
2281     va_start(args, fmt);
2282     len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2283     va_end(args);
2284     header = ccs_init_audit_log(&len, r);
2285     if (!header)
2286     goto out;
2287 kumaneko 3512 ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2288 kumaneko 2863 if (!ccs_query_entry)
2289     goto out;
2290 kumaneko 2900 len = ccs_round2(len);
2291 kumaneko 3512 ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2292 kumaneko 2863 if (!ccs_query_entry->query)
2293     goto out;
2294     INIT_LIST_HEAD(&ccs_query_entry->list);
2295     spin_lock(&ccs_query_list_lock);
2296     if (ccs_quota_for_query && ccs_query_memory_size + len +
2297     sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
2298     quota_exceeded = true;
2299     } else {
2300     ccs_query_memory_size += len + sizeof(*ccs_query_entry);
2301     ccs_query_entry->serial = ccs_serial++;
2302     }
2303     spin_unlock(&ccs_query_list_lock);
2304     if (quota_exceeded)
2305     goto out;
2306     pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
2307     ccs_query_entry->serial, r->retry, header);
2308     kfree(header);
2309     header = NULL;
2310     va_start(args, fmt);
2311     vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);
2312     ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
2313     va_end(args);
2314     spin_lock(&ccs_query_list_lock);
2315<