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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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