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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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