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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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