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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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