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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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