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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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