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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3821 - (hide annotations) (download) (as text)
Thu Jul 15 13:19:14 2010 UTC (13 years, 10 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 68213 byte(s)


1 kumaneko 2863 /*
2 kumaneko 2864 * security/ccsecurity/policy_io.c
3 kumaneko 2863 *
4 kumaneko 3441 * Copyright (C) 2005-2010 NTT DATA CORPORATION
5 kumaneko 2863 *
6 kumaneko 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_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
1007     };
1008    
1009 kumaneko 2863 /**
1010 kumaneko 3693 * ccs_write_domain - Write domain policy.
1011 kumaneko 2863 *
1012     * @head: Pointer to "struct ccs_io_buffer".
1013     *
1014     * Returns 0 on success, negative value otherwise.
1015     */
1016 kumaneko 3693 static int ccs_write_domain(struct ccs_io_buffer *head)
1017 kumaneko 2863 {
1018     char *data = head->write_buf;
1019 kumaneko 3780 struct ccs_domain_info *domain = head->w.domain;
1020 kumaneko 2863 bool is_delete = false;
1021     bool is_select = false;
1022     unsigned int profile;
1023 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
1024 kumaneko 2863 is_delete = true;
1025 kumaneko 2892 else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
1026 kumaneko 2863 is_select = true;
1027 kumaneko 3693 if (is_select && ccs_select_one(head, data))
1028 kumaneko 2863 return 0;
1029     /* Don't allow updating policies by non manager programs. */
1030 kumaneko 3693 if (!ccs_manager())
1031 kumaneko 2863 return -EPERM;
1032 kumaneko 3693 if (ccs_domain_def(data)) {
1033 kumaneko 2863 domain = NULL;
1034     if (is_delete)
1035     ccs_delete_domain(data);
1036     else if (is_select)
1037     domain = ccs_find_domain(data);
1038     else
1039 kumaneko 3821 domain = ccs_assign_domain(data, 0, 0);
1040 kumaneko 3780 head->w.domain = domain;
1041 kumaneko 2863 return 0;
1042     }
1043     if (!domain)
1044     return -EINVAL;
1045    
1046 kumaneko 2892 if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
1047     && profile < CCS_MAX_PROFILES) {
1048 kumaneko 2991 if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1049 kumaneko 2863 domain->profile = (u8) profile;
1050     return 0;
1051     }
1052 kumaneko 3821 if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
1053     && profile < CCS_MAX_ACL_GROUPS) {
1054     domain->group = (u8) profile;
1055     return 0;
1056     }
1057 kumaneko 3747 for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1058     const char *cp = ccs_dif[profile];
1059     if (strncmp(data, cp, strlen(cp) - 1))
1060     continue;
1061     domain->flags[profile] = !is_delete;
1062 kumaneko 3702 return 0;
1063     }
1064 kumaneko 3746 return ccs_write_domain2(data, domain, is_delete);
1065 kumaneko 2863 }
1066    
1067 kumaneko 2948 /**
1068     * ccs_print_name_union - Print a ccs_name_union.
1069     *
1070     * @head: Pointer to "struct ccs_io_buffer".
1071     * @ptr: Pointer to "struct ccs_name_union".
1072     */
1073 kumaneko 3780 static void ccs_print_name_union(struct ccs_io_buffer *head,
1074 kumaneko 2894 const struct ccs_name_union *ptr)
1075 kumaneko 2863 {
1076 kumaneko 3780 const bool cond = head->r.print_cond_part;
1077     if (!cond)
1078     ccs_set_space(head);
1079     if (ptr->is_group) {
1080     ccs_set_string(head, "@");
1081     ccs_set_string(head, ptr->group->group_name->name);
1082     } else {
1083     if (cond)
1084     ccs_set_string(head, "\"");
1085     ccs_set_string(head, ptr->filename->name);
1086     if (cond)
1087     ccs_set_string(head, "\"");
1088     }
1089 kumaneko 2863 }
1090    
1091 kumaneko 2948 /**
1092 kumaneko 3780 * ccs_print_number_union - Print a ccs_number_union.
1093 kumaneko 2948 *
1094     * @head: Pointer to "struct ccs_io_buffer".
1095 kumaneko 3780 * @ptr: Pointer to "struct ccs_number_union".
1096 kumaneko 2948 */
1097 kumaneko 3780 static void ccs_print_number_union(struct ccs_io_buffer *head,
1098     const struct ccs_number_union *ptr)
1099 kumaneko 2863 {
1100 kumaneko 3780 if (!head->r.print_cond_part)
1101     ccs_set_space(head);
1102     if (ptr->is_group) {
1103     ccs_set_string(head, "@");
1104     ccs_set_string(head, ptr->group->group_name->name);
1105     } else {
1106     int i;
1107     unsigned long min = ptr->values[0];
1108     const unsigned long max = ptr->values[1];
1109     u8 min_type = ptr->value_type[0];
1110     const u8 max_type = ptr->value_type[1];
1111     char buffer[128];
1112     buffer[0] = '\0';
1113     for (i = 0; i < 2; i++) {
1114     switch (min_type) {
1115     case CCS_VALUE_TYPE_HEXADECIMAL:
1116     ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1117     min);
1118     break;
1119     case CCS_VALUE_TYPE_OCTAL:
1120     ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1121     min);
1122     break;
1123     default:
1124     ccs_addprintf(buffer, sizeof(buffer), "%lu",
1125     min);
1126     break;
1127     }
1128     if (min == max && min_type == max_type)
1129     break;
1130     ccs_addprintf(buffer, sizeof(buffer), "-");
1131     min_type = max_type;
1132     min = max;
1133 kumaneko 3690 }
1134 kumaneko 3780 ccs_io_printf(head, "%s", buffer);
1135 kumaneko 3690 }
1136     }
1137    
1138 kumaneko 2948 /**
1139 kumaneko 2894 * ccs_print_condition - Print condition part.
1140     *
1141     * @head: Pointer to "struct ccs_io_buffer".
1142 kumaneko 3780 * @cond: Pointer to "struct ccs_condition".
1143 kumaneko 2894 *
1144     * Returns true on success, false otherwise.
1145     */
1146     static bool ccs_print_condition(struct ccs_io_buffer *head,
1147     const struct ccs_condition *cond)
1148     {
1149 kumaneko 3780 switch (head->r.cond_step) {
1150     case 0:
1151     {
1152     if (cond->condc)
1153     ccs_set_string(head, " if");
1154     head->r.cond_index = 0;
1155     head->r.cond_step++;
1156     }
1157     /* fall through */
1158     case 1:
1159     {
1160     const u16 condc = cond->condc;
1161     const struct ccs_condition_element *condp =
1162     (typeof(condp)) (cond + 1);
1163     const struct ccs_number_union *numbers_p =
1164     (typeof(numbers_p)) (condp + condc);
1165     const struct ccs_name_union *names_p =
1166     (typeof(names_p))
1167     (numbers_p + cond->numbers_count);
1168     const struct ccs_argv *argv =
1169     (typeof(argv)) (names_p + cond->names_count);
1170     const struct ccs_envp *envp =
1171     (typeof(envp)) (argv + cond->argc);
1172     u16 skip;
1173     for (skip = 0; skip < head->r.cond_index; skip++) {
1174     const u8 left = condp->left;
1175     const u8 right = condp->right;
1176     condp++;
1177     switch (left) {
1178     case CCS_ARGV_ENTRY:
1179     argv++;
1180     continue;
1181     case CCS_ENVP_ENTRY:
1182     envp++;
1183     continue;
1184     case CCS_NUMBER_UNION:
1185     numbers_p++;
1186     break;
1187     }
1188     switch (right) {
1189     case CCS_NAME_UNION:
1190     names_p++;
1191     break;
1192     case CCS_NUMBER_UNION:
1193     numbers_p++;
1194     break;
1195     }
1196 kumaneko 2894 }
1197 kumaneko 3780 while (head->r.cond_index < condc) {
1198     const u8 match = condp->equals;
1199     const u8 left = condp->left;
1200     const u8 right = condp->right;
1201     if (!ccs_flush(head))
1202     return false;
1203     condp++;
1204     head->r.cond_index++;
1205     ccs_set_space(head);
1206     switch (left) {
1207     case CCS_ARGV_ENTRY:
1208     ccs_io_printf(head,
1209     "exec.argv[%u]%s\"%s\"",
1210     argv->index,
1211     argv->is_not ?
1212     "!=" : "=",
1213     argv->value->name);
1214     argv++;
1215     continue;
1216     case CCS_ENVP_ENTRY:
1217     ccs_io_printf(head,
1218     "exec.envp[\"%s\"]%s",
1219     envp->name->name,
1220     envp->is_not ?
1221     "!=" : "=");
1222     if (envp->value) {
1223     ccs_set_string(head, "\"");
1224     ccs_set_string(head, envp->
1225     value->name);
1226     ccs_set_string(head, "\"");
1227     } else {
1228     ccs_set_string(head, "NULL");
1229     }
1230     envp++;
1231     continue;
1232     case CCS_NUMBER_UNION:
1233     ccs_print_number_union(head,
1234     numbers_p++);
1235     break;
1236     default:
1237     ccs_set_string(head,
1238     ccs_condition_keyword[left]);
1239     break;
1240     }
1241     ccs_set_string(head, match ? "=" : "!=");
1242     switch (right) {
1243     case CCS_NAME_UNION:
1244     ccs_print_name_union(head, names_p++);
1245     break;
1246     case CCS_NUMBER_UNION:
1247     ccs_print_number_union(head,
1248     numbers_p++);
1249     break;
1250     default:
1251     ccs_set_string(head,
1252     ccs_condition_keyword[right]);
1253     break;
1254     }
1255     }
1256 kumaneko 2894 }
1257 kumaneko 3780 head->r.cond_step++;
1258     /* fall through */
1259     case 2:
1260     if (!ccs_flush(head))
1261 kumaneko 2894 break;
1262 kumaneko 3780 head->r.cond_step++;
1263     /* fall through */
1264     case 3:
1265     {
1266     u8 j;
1267     const u8 i = cond->post_state[3];
1268     if (i)
1269     ccs_set_string(head, " ; set");
1270     for (j = 0; j < 3; j++)
1271     if ((i & (1 << j)))
1272     ccs_io_printf(head,
1273     " task.state[%u]=%u", j,
1274     cond->post_state[j]);
1275     if (i & (1 << 4))
1276     ccs_io_printf(head, " audit=%s",
1277     ccs_yesno(cond->post_state[4]));
1278 kumaneko 2894 }
1279 kumaneko 3780 ccs_set_lf(head);
1280     return true;
1281 kumaneko 2894 }
1282     return false;
1283     }
1284    
1285     /**
1286 kumaneko 3780 * ccs_fns - Find next set bit.
1287     *
1288     * @perm: 8 bits value.
1289     * @bit: First bit to find.
1290     *
1291     * Returns next set bit on success, 8 otherwise.
1292     */
1293     static u8 ccs_fns(const u8 perm, u8 bit)
1294     {
1295     for ( ; bit < 8; bit++)
1296     if (perm & (1 << bit))
1297     break;
1298     return bit;
1299     }
1300    
1301 kumaneko 3821 static void ccs_set_group(struct ccs_io_buffer *head)
1302     {
1303     if (head->type == CCS_EXCEPTIONPOLICY)
1304     ccs_io_printf(head, "acl_group %u ", head->r.group_index);
1305     }
1306    
1307 kumaneko 3780 /**
1308 kumaneko 3772 * ccs_print_entry - Print an ACL entry.
1309 kumaneko 2863 *
1310     * @head: Pointer to "struct ccs_io_buffer".
1311 kumaneko 3780 * @acl: Pointer to an ACL entry.
1312 kumaneko 2863 *
1313     * Returns true on success, false otherwise.
1314     */
1315 kumaneko 3772 static bool ccs_print_entry(struct ccs_io_buffer *head,
1316     const struct ccs_acl_info *acl)
1317 kumaneko 2863 {
1318 kumaneko 3772 const u8 acl_type = acl->type;
1319 kumaneko 3780 u8 bit;
1320     if (head->r.print_cond_part)
1321     goto print_cond_part;
1322 kumaneko 3772 if (acl->is_deleted)
1323     return true;
1324     next:
1325 kumaneko 3780 bit = head->r.bit;
1326     if (!ccs_flush(head))
1327     return false;
1328     else if (acl_type == CCS_TYPE_PATH_ACL) {
1329 kumaneko 3772 struct ccs_path_acl *ptr
1330     = container_of(acl, typeof(*ptr), head);
1331     const u16 perm = ptr->perm;
1332     for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1333     if (!(perm & (1 << bit)))
1334     continue;
1335 kumaneko 3780 if (head->r.print_execute_only &&
1336     bit != CCS_TYPE_EXECUTE && bit != CCS_TYPE_TRANSIT)
1337 kumaneko 3772 continue;
1338     break;
1339     }
1340 kumaneko 3780 if (bit >= CCS_MAX_PATH_OPERATION)
1341 kumaneko 3772 goto done;
1342 kumaneko 3821 ccs_set_group(head);
1343 kumaneko 3808 ccs_set_string(head, "file ");
1344     ccs_set_string(head, ccs_path_keyword[bit]);
1345 kumaneko 3780 ccs_print_name_union(head, &ptr->name);
1346 kumaneko 3772 } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1347     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1348     struct ccs_execute_handler *ptr
1349     = container_of(acl, typeof(*ptr), head);
1350 kumaneko 3821 ccs_set_group(head);
1351 kumaneko 3780 ccs_io_printf(head, "%s ",
1352     acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1353     CCS_KEYWORD_EXECUTE_HANDLER :
1354     CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1355     ccs_set_string(head, ptr->handler->name);
1356     } else if (head->r.print_execute_only) {
1357 kumaneko 3772 return true;
1358     } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1359 kumaneko 3780 struct ccs_mkdev_acl *ptr =
1360     container_of(acl, typeof(*ptr), head);
1361     bit = ccs_fns(ptr->perm, bit);
1362     if (bit >= CCS_MAX_MKDEV_OPERATION)
1363 kumaneko 3772 goto done;
1364 kumaneko 3821 ccs_set_group(head);
1365 kumaneko 3808 ccs_set_string(head, "file ");
1366     ccs_set_string(head, ccs_mkdev_keyword[bit]);
1367 kumaneko 3780 ccs_print_name_union(head, &ptr->name);
1368     ccs_print_number_union(head, &ptr->mode);
1369     ccs_print_number_union(head, &ptr->major);
1370     ccs_print_number_union(head, &ptr->minor);
1371 kumaneko 3772 } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1372 kumaneko 3780 struct ccs_path2_acl *ptr =
1373     container_of(acl, typeof(*ptr), head);
1374     bit = ccs_fns(ptr->perm, bit);
1375     if (bit >= CCS_MAX_PATH2_OPERATION)
1376 kumaneko 3772 goto done;
1377 kumaneko 3821 ccs_set_group(head);
1378 kumaneko 3808 ccs_set_string(head, "file ");
1379     ccs_set_string(head, ccs_path2_keyword[bit]);
1380 kumaneko 3780 ccs_print_name_union(head, &ptr->name1);
1381     ccs_print_name_union(head, &ptr->name2);
1382 kumaneko 3772 } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1383 kumaneko 3780 struct ccs_path_number_acl *ptr =
1384     container_of(acl, typeof(*ptr), head);
1385     bit = ccs_fns(ptr->perm, bit);
1386     if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1387 kumaneko 3772 goto done;
1388 kumaneko 3821 ccs_set_group(head);
1389 kumaneko 3808 ccs_set_string(head, "file ");
1390     ccs_set_string(head, ccs_path_number_keyword[bit]);
1391 kumaneko 3780 ccs_print_name_union(head, &ptr->name);
1392     ccs_print_number_union(head, &ptr->number);
1393 kumaneko 3772 } else if (acl_type == CCS_TYPE_ENV_ACL) {
1394 kumaneko 3780 struct ccs_env_acl *ptr =
1395     container_of(acl, typeof(*ptr), head);
1396 kumaneko 3821 ccs_set_group(head);
1397 kumaneko 3808 ccs_set_string(head, "misc env ");
1398 kumaneko 3780 ccs_set_string(head, ptr->env->name);
1399 kumaneko 3772 } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1400 kumaneko 3780 struct ccs_capability_acl *ptr =
1401     container_of(acl, typeof(*ptr), head);
1402 kumaneko 3821 ccs_set_group(head);
1403 kumaneko 3808 ccs_set_string(head, "capability ");
1404 kumaneko 3780 ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1405 kumaneko 3772 } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1406 kumaneko 3780 struct ccs_ip_network_acl *ptr =
1407     container_of(acl, typeof(*ptr), head);
1408     bit = ccs_fns(ptr->perm, bit);
1409     if (bit >= CCS_MAX_NETWORK_OPERATION)
1410 kumaneko 3772 goto done;
1411 kumaneko 3821 ccs_set_group(head);
1412 kumaneko 3808 ccs_set_string(head, "network ");
1413     ccs_set_string(head, ccs_net_keyword[bit]);
1414     ccs_set_space(head);
1415 kumaneko 2916 switch (ptr->address_type) {
1416 kumaneko 3780 char buf[128];
1417 kumaneko 2916 case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1418 kumaneko 3780 ccs_set_string(head, "@");
1419     ccs_set_string(head,
1420     ptr->address.group->group_name->name);
1421 kumaneko 2916 break;
1422     case CCS_IP_ADDRESS_TYPE_IPv4:
1423 kumaneko 3690 ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1424     ptr->address.ipv4.max);
1425 kumaneko 3780 ccs_io_printf(head, "%s", buf);
1426 kumaneko 2916 break;
1427     case CCS_IP_ADDRESS_TYPE_IPv6:
1428 kumaneko 3690 ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1429     ptr->address.ipv6.max);
1430 kumaneko 3780 ccs_io_printf(head, "%s", buf);
1431 kumaneko 2916 break;
1432     }
1433 kumaneko 3780 ccs_print_number_union(head, &ptr->port);
1434 kumaneko 3772 } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1435 kumaneko 3780 struct ccs_signal_acl *ptr =
1436     container_of(acl, typeof(*ptr), head);
1437 kumaneko 3821 ccs_set_group(head);
1438 kumaneko 3808 ccs_set_string(head, "ipc signal ");
1439     ccs_io_printf(head, "%u ", ptr->sig);
1440 kumaneko 3780 ccs_set_string(head, ptr->domainname->name);
1441 kumaneko 3772 } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1442 kumaneko 3780 struct ccs_mount_acl *ptr =
1443     container_of(acl, typeof(*ptr), head);
1444 kumaneko 3821 ccs_set_group(head);
1445 kumaneko 3808 ccs_io_printf(head, "file mount");
1446 kumaneko 3780 ccs_print_name_union(head, &ptr->dev_name);
1447     ccs_print_name_union(head, &ptr->dir_name);
1448     ccs_print_name_union(head, &ptr->fs_type);
1449     ccs_print_number_union(head, &ptr->flags);
1450 kumaneko 2863 }
1451 kumaneko 3780 head->r.bit = bit + 1;
1452     if (acl->cond) {
1453     head->r.print_cond_part = true;
1454     head->r.cond_step = 0;
1455     if (!ccs_flush(head))
1456     return false;
1457     print_cond_part:
1458     if (!ccs_print_condition(head, acl->cond))
1459     return false;
1460     head->r.print_cond_part = false;
1461     } else {
1462     ccs_set_lf(head);
1463 kumaneko 2897 }
1464 kumaneko 3772 switch (acl_type) {
1465     case CCS_TYPE_PATH_ACL:
1466     case CCS_TYPE_MKDEV_ACL:
1467     case CCS_TYPE_PATH2_ACL:
1468     case CCS_TYPE_PATH_NUMBER_ACL:
1469     case CCS_TYPE_IP_NETWORK_ACL:
1470     goto next;
1471 kumaneko 3747 }
1472 kumaneko 3772 done:
1473 kumaneko 3780 head->r.bit = 0;
1474 kumaneko 3747 return true;
1475 kumaneko 2863 }
1476    
1477     /**
1478 kumaneko 3697 * ccs_read_domain2 - Read domain policy.
1479     *
1480     * @head: Pointer to "struct ccs_io_buffer".
1481     * @domain: Pointer to "struct ccs_domain_info".
1482     *
1483     * Caller holds ccs_read_lock().
1484     *
1485     * Returns true on success, false otherwise.
1486     */
1487     static bool ccs_read_domain2(struct ccs_io_buffer *head,
1488     struct ccs_domain_info *domain)
1489     {
1490 kumaneko 3780 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1491     struct ccs_acl_info *ptr =
1492     list_entry(head->r.acl, typeof(*ptr), list);
1493 kumaneko 3697 if (!ccs_print_entry(head, ptr))
1494     return false;
1495     }
1496 kumaneko 3780 head->r.acl = NULL;
1497 kumaneko 3697 return true;
1498     }
1499    
1500     /**
1501 kumaneko 3693 * ccs_read_domain - Read domain policy.
1502 kumaneko 2863 *
1503     * @head: Pointer to "struct ccs_io_buffer".
1504     *
1505     * Caller holds ccs_read_lock().
1506     */
1507 kumaneko 3693 static void ccs_read_domain(struct ccs_io_buffer *head)
1508 kumaneko 2863 {
1509 kumaneko 3780 if (head->r.eof)
1510 kumaneko 2943 return;
1511 kumaneko 3780 list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1512 kumaneko 3697 struct ccs_domain_info *domain =
1513 kumaneko 3780 list_entry(head->r.domain, typeof(*domain), list);
1514     switch (head->r.step) {
1515 kumaneko 3747 u8 i;
1516 kumaneko 3697 case 0:
1517 kumaneko 3780 if (domain->is_deleted &&
1518     !head->r.print_this_domain_only)
1519 kumaneko 3697 continue;
1520     /* Print domainname and flags. */
1521 kumaneko 3780 ccs_set_string(head, domain->domainname->name);
1522     ccs_set_lf(head);
1523     ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1524     domain->profile);
1525 kumaneko 3821 ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1526     domain->group);
1527 kumaneko 3747 for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1528 kumaneko 3780 if (domain->flags[i])
1529     ccs_set_string(head, ccs_dif[i]);
1530     head->r.step++;
1531     ccs_set_lf(head);
1532 kumaneko 3697 /* fall through */
1533     case 1:
1534     if (!ccs_read_domain2(head, domain))
1535     return;
1536 kumaneko 3780 head->r.step++;
1537     if (!ccs_set_lf(head))
1538     return;
1539 kumaneko 3697 /* fall through */
1540     case 2:
1541 kumaneko 3780 head->r.step = 0;
1542     if (head->r.print_this_domain_only)
1543     goto done;
1544 kumaneko 2863 }
1545     }
1546 kumaneko 3780 done:
1547     head->r.eof = true;
1548 kumaneko 2863 }
1549    
1550     /**
1551     * ccs_write_domain_profile - Assign profile for specified domain.
1552     *
1553     * @head: Pointer to "struct ccs_io_buffer".
1554     *
1555     * Returns 0 on success, -EINVAL otherwise.
1556     *
1557     * This is equivalent to doing
1558     *
1559     * ( echo "select " $domainname; echo "use_profile " $profile ) |
1560 kumaneko 2948 * /usr/sbin/ccs-loadpolicy -d
1561 kumaneko 2863 *
1562     * Caller holds ccs_read_lock().
1563     */
1564     static int ccs_write_domain_profile(struct ccs_io_buffer *head)
1565     {
1566     char *data = head->write_buf;
1567     char *cp = strchr(data, ' ');
1568     struct ccs_domain_info *domain;
1569     unsigned int profile;
1570     if (!cp)
1571     return -EINVAL;
1572     *cp = '\0';
1573     profile = simple_strtoul(data, NULL, 10);
1574 kumaneko 2892 if (profile >= CCS_MAX_PROFILES)
1575 kumaneko 2863 return -EINVAL;
1576     domain = ccs_find_domain(cp + 1);
1577 kumaneko 2991 if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1578 kumaneko 2863 domain->profile = (u8) profile;
1579     return 0;
1580     }
1581    
1582     /**
1583     * ccs_read_domain_profile - Read only domainname and profile.
1584     *
1585     * @head: Pointer to "struct ccs_io_buffer".
1586     *
1587     * This is equivalent to doing
1588     *
1589     * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1590     * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1591     * domainname = $0; } else if ( $1 == "use_profile" ) {
1592     * print $2 " " domainname; domainname = ""; } } ; '
1593     *
1594     * Caller holds ccs_read_lock().
1595     */
1596 kumaneko 2943 static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1597 kumaneko 2863 {
1598 kumaneko 3780 if (head->r.eof)
1599 kumaneko 2943 return;
1600 kumaneko 3780 list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1601     struct ccs_domain_info *domain =
1602     list_entry(head->r.domain, typeof(*domain), list);
1603 kumaneko 2863 if (domain->is_deleted)
1604     continue;
1605 kumaneko 3780 if (!ccs_flush(head))
1606 kumaneko 2943 return;
1607 kumaneko 3780 ccs_io_printf(head, "%u ", domain->profile);
1608     ccs_set_string(head, domain->domainname->name);
1609     ccs_set_lf(head);
1610 kumaneko 2863 }
1611 kumaneko 3780 head->r.eof = true;
1612 kumaneko 2863 }
1613    
1614     /**
1615     * ccs_write_pid: Specify PID to obtain domainname.
1616     *
1617     * @head: Pointer to "struct ccs_io_buffer".
1618     *
1619     * Returns 0.
1620     */
1621     static int ccs_write_pid(struct ccs_io_buffer *head)
1622     {
1623 kumaneko 3780 head->r.eof = false;
1624 kumaneko 2863 return 0;
1625     }
1626    
1627     /**
1628     * ccs_read_pid - Read information of a process.
1629     *
1630     * @head: Pointer to "struct ccs_io_buffer".
1631     *
1632     * Returns the domainname which the specified PID is in or
1633     * process information of the specified PID on success,
1634     * empty string otherwise.
1635     *
1636     * Caller holds ccs_read_lock().
1637     */
1638 kumaneko 2943 static void ccs_read_pid(struct ccs_io_buffer *head)
1639 kumaneko 2863 {
1640     char *buf = head->write_buf;
1641     bool task_info = false;
1642 kumaneko 2970 bool global_pid = false;
1643 kumaneko 2863 unsigned int pid;
1644     struct task_struct *p;
1645     struct ccs_domain_info *domain = NULL;
1646     u32 ccs_flags = 0;
1647     /* Accessing write_buf is safe because head->io_sem is held. */
1648 kumaneko 3136 if (!buf) {
1649 kumaneko 3780 head->r.eof = true;
1650 kumaneko 2943 return; /* Do nothing if open(O_RDONLY). */
1651 kumaneko 3136 }
1652 kumaneko 3780 if (head->r.w_pos || head->r.eof)
1653 kumaneko 2943 return;
1654 kumaneko 3780 head->r.eof = true;
1655 kumaneko 2863 if (ccs_str_starts(&buf, "info "))
1656     task_info = true;
1657 kumaneko 2970 if (ccs_str_starts(&buf, "global-pid "))
1658     global_pid = true;
1659 kumaneko 2863 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1660 kumaneko 3248 ccs_tasklist_lock();
1661 kumaneko 2970 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1662     if (global_pid)
1663 kumaneko 3502 p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1664 kumaneko 2970 else
1665 kumaneko 3502 p = ccsecurity_exports.find_task_by_vpid(pid);
1666 kumaneko 2970 #else
1667 kumaneko 2863 p = find_task_by_pid(pid);
1668 kumaneko 2970 #endif
1669 kumaneko 2863 if (p) {
1670     domain = ccs_task_domain(p);
1671     ccs_flags = p->ccs_flags;
1672     }
1673 kumaneko 3248 ccs_tasklist_unlock();
1674 kumaneko 2863 if (!domain)
1675 kumaneko 2943 return;
1676 kumaneko 3780 if (!task_info) {
1677     ccs_io_printf(head, "%u %u ", pid, domain->profile);
1678     ccs_set_string(head, domain->domainname->name);
1679     } else {
1680 kumaneko 2863 ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1681     "state[0]=%u state[1]=%u state[2]=%u", pid,
1682 kumaneko 2958 ccs_yesno(ccs_flags &
1683 kumaneko 3693 CCS_TASK_IS_MANAGER),
1684 kumaneko 2958 ccs_yesno(ccs_flags &
1685     CCS_TASK_IS_EXECUTE_HANDLER),
1686 kumaneko 2863 (u8) (ccs_flags >> 24),
1687     (u8) (ccs_flags >> 16),
1688     (u8) (ccs_flags >> 8));
1689 kumaneko 3780 }
1690 kumaneko 2863 }
1691    
1692 kumaneko 3752 static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1693     [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1694     = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1695 kumaneko 3754 [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1696     [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1697     [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1698 kumaneko 3752 };
1699    
1700     static const char *ccs_group_name[CCS_MAX_GROUP] = {
1701     [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1702     [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1703     [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1704     };
1705    
1706 kumaneko 2863 /**
1707 kumaneko 3693 * ccs_write_exception - Write exception policy.
1708 kumaneko 2863 *
1709     * @head: Pointer to "struct ccs_io_buffer".
1710     *
1711     * Returns 0 on success, negative value otherwise.
1712     */
1713 kumaneko 3693 static int ccs_write_exception(struct ccs_io_buffer *head)
1714 kumaneko 2863 {
1715     char *data = head->write_buf;
1716 kumaneko 3691 const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1717     u8 i;
1718     static const struct {
1719     const char *keyword;
1720 kumaneko 3752 int (*write) (char *, const bool);
1721 kumaneko 3808 } ccs_callback[3] = {
1722 kumaneko 3693 { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1723     { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1724     { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1725 kumaneko 3691 };
1726 kumaneko 3808 for (i = 0; i < 3; i++)
1727 kumaneko 3691 if (ccs_str_starts(&data, ccs_callback[i].keyword))
1728 kumaneko 3752 return ccs_callback[i].write(data, is_delete);
1729 kumaneko 3781 for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1730 kumaneko 3752 if (ccs_str_starts(&data, ccs_transition_type[i]))
1731     return ccs_write_transition_control(data, is_delete,
1732     i);
1733 kumaneko 3781 for (i = 0; i < CCS_MAX_GROUP; i++)
1734 kumaneko 3752 if (ccs_str_starts(&data, ccs_group_name[i]))
1735 kumaneko 3693 return ccs_write_group(data, is_delete, i);
1736 kumaneko 3821 if (ccs_str_starts(&data, "acl_group ")) {
1737     unsigned int group;
1738     if (sscanf(data, "%u", &group) == 1 &&
1739     group < CCS_MAX_ACL_GROUPS) {
1740     data = strchr(data, ' ');
1741     if (data)
1742     return ccs_write_domain2(data + 1,
1743     &ccs_acl_group[group],
1744     is_delete);
1745     }
1746     }
1747     return -EINVAL;
1748 kumaneko 2863 }
1749    
1750     /**
1751 kumaneko 3689 * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1752 kumaneko 2863 *
1753     * @head: Pointer to "struct ccs_io_buffer".
1754 kumaneko 3689 * @idx: Index number.
1755 kumaneko 2863 *
1756 kumaneko 3689 * Returns true on success, false otherwise.
1757     *
1758 kumaneko 2863 * Caller holds ccs_read_lock().
1759     */
1760 kumaneko 3689 static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1761 kumaneko 2863 {
1762 kumaneko 3780 list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1763 kumaneko 3689 struct ccs_group *group =
1764 kumaneko 3780 list_entry(head->r.group, typeof(*group), head.list);
1765     list_for_each_cookie(head->r.acl, &group->member_list) {
1766 kumaneko 3689 struct ccs_acl_head *ptr =
1767 kumaneko 3780 list_entry(head->r.acl, typeof(*ptr), list);
1768 kumaneko 3689 if (ptr->is_deleted)
1769     continue;
1770 kumaneko 3780 if (!ccs_flush(head))
1771     return false;
1772     ccs_set_string(head, ccs_group_name[idx]);
1773     ccs_set_string(head, group->group_name->name);
1774 kumaneko 3689 if (idx == CCS_PATH_GROUP) {
1775 kumaneko 3780 ccs_set_space(head);
1776     ccs_set_string(head, container_of
1777     (ptr, struct ccs_path_group,
1778     head)->member_name->name);
1779 kumaneko 3689 } else if (idx == CCS_NUMBER_GROUP) {
1780 kumaneko 3780 ccs_print_number_union(head, &container_of
1781     (ptr, struct ccs_number_group,
1782     head)->number);
1783 kumaneko 3689 } else if (idx == CCS_ADDRESS_GROUP) {
1784 kumaneko 3780 char buffer[128];
1785 kumaneko 3689 struct ccs_address_group *member =
1786     container_of(ptr, typeof(*member),
1787     head);
1788 kumaneko 3690 if (member->is_ipv6)
1789     ccs_print_ipv6(buffer, sizeof(buffer),
1790     member->min.ipv6,
1791     member->max.ipv6);
1792     else
1793     ccs_print_ipv4(buffer, sizeof(buffer),
1794     member->min.ipv4,
1795     member->max.ipv4);
1796 kumaneko 3780 ccs_io_printf(head, " %s", buffer);
1797 kumaneko 3689 }
1798 kumaneko 3780 ccs_set_lf(head);
1799 kumaneko 3689 }
1800 kumaneko 3780 head->r.acl = NULL;
1801 kumaneko 3689 }
1802 kumaneko 3780 head->r.group = NULL;
1803 kumaneko 3689 return true;
1804     }
1805    
1806     /**
1807     * ccs_read_policy - Read "struct ccs_..._entry" list.
1808     *
1809     * @head: Pointer to "struct ccs_io_buffer".
1810     * @idx: Index number.
1811     *
1812     * Returns true on success, false otherwise.
1813     *
1814     * Caller holds ccs_read_lock().
1815     */
1816     static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1817     {
1818 kumaneko 3780 list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1819     struct ccs_acl_head *acl =
1820     container_of(head->r.acl, typeof(*acl), list);
1821 kumaneko 3689 if (acl->is_deleted)
1822     continue;
1823 kumaneko 3780 if (!ccs_flush(head))
1824     return false;
1825 kumaneko 3689 switch (idx) {
1826 kumaneko 3752 case CCS_ID_TRANSITION_CONTROL:
1827 kumaneko 3689 {
1828 kumaneko 3752 struct ccs_transition_control *ptr =
1829 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1830 kumaneko 3780 ccs_set_string(head,
1831     ccs_transition_type[ptr->type]);
1832     ccs_set_string(head, ptr->program ?
1833     ptr->program->name : "any");
1834     ccs_set_string(head, " from ");
1835     ccs_set_string(head, ptr->domainname ?
1836     ptr->domainname->name : "any");
1837 kumaneko 3689 }
1838 kumaneko 2863 break;
1839 kumaneko 3689 case CCS_ID_AGGREGATOR:
1840     {
1841 kumaneko 3693 struct ccs_aggregator *ptr =
1842 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1843 kumaneko 3780 ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1844     ccs_set_string(head, ptr->original_name->name);
1845     ccs_set_space(head);
1846     ccs_set_string(head,
1847     ptr->aggregated_name->name);
1848 kumaneko 3689 }
1849 kumaneko 2943 break;
1850 kumaneko 3689 case CCS_ID_PATTERN:
1851     {
1852 kumaneko 3693 struct ccs_pattern *ptr =
1853 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1854 kumaneko 3780 ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1855     ccs_set_string(head, ptr->pattern->name);
1856 kumaneko 3689 }
1857 kumaneko 2943 break;
1858 kumaneko 3689 case CCS_ID_RESERVEDPORT:
1859     {
1860 kumaneko 3693 struct ccs_reserved *ptr =
1861 kumaneko 3689 container_of(acl, typeof(*ptr), head);
1862     const u16 min_port = ptr->min_port;
1863     const u16 max_port = ptr->max_port;
1864 kumaneko 3780 ccs_set_string(head,
1865     CCS_KEYWORD_DENY_AUTOBIND);
1866     ccs_io_printf(head, "%u", min_port);
1867     if (min_port != max_port)
1868     ccs_io_printf(head, "-%u", max_port);
1869 kumaneko 3689 }
1870 kumaneko 2943 break;
1871 kumaneko 3696 default:
1872     continue;
1873 kumaneko 3689 }
1874 kumaneko 3780 ccs_set_lf(head);
1875 kumaneko 2863 }
1876 kumaneko 3780 head->r.acl = NULL;
1877 kumaneko 3689 return true;
1878 kumaneko 2863 }
1879    
1880 kumaneko 2897 /**
1881 kumaneko 3693 * ccs_read_exception - Read exception policy.
1882 kumaneko 3689 *
1883     * @head: Pointer to "struct ccs_io_buffer".
1884     *
1885     * Caller holds ccs_read_lock().
1886     */
1887 kumaneko 3693 static void ccs_read_exception(struct ccs_io_buffer *head)
1888 kumaneko 3689 {
1889 kumaneko 3780 if (head->r.eof)
1890 kumaneko 3689 return;
1891 kumaneko 3780 while (head->r.step < CCS_MAX_POLICY &&
1892     ccs_read_policy(head, head->r.step))
1893     head->r.step++;
1894     if (head->r.step < CCS_MAX_POLICY)
1895 kumaneko 3689 return;
1896 kumaneko 3780 while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1897     ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1898     head->r.step++;
1899     if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1900 kumaneko 3697 return;
1901 kumaneko 3821 while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1902     + CCS_MAX_ACL_GROUPS) {
1903     head->r.group_index = head->r.step - CCS_MAX_POLICY
1904     - CCS_MAX_GROUP;
1905     if (!ccs_read_domain2(head,
1906     &ccs_acl_group[head->r.group_index]))
1907     return;
1908     head->r.step++;
1909     }
1910     head->r.eof = true;
1911 kumaneko 3689 }
1912    
1913 kumaneko 2863 /* Wait queue for ccs_query_list. */
1914     static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
1915    
1916     /* Lock for manipulating ccs_query_list. */
1917     static DEFINE_SPINLOCK(ccs_query_list_lock);
1918    
1919     /* Structure for query. */
1920 kumaneko 3781 struct ccs_query {
1921 kumaneko 2863 struct list_head list;
1922     char *query;
1923     int query_len;
1924     unsigned int serial;
1925     int timer;
1926     int answer;
1927     };
1928    
1929 kumaneko 3781 /* The list for "struct ccs_query". */
1930 kumaneko 2863 static LIST_HEAD(ccs_query_list);
1931    
1932     /* Number of "struct file" referring /proc/ccs/query interface. */
1933     static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1934    
1935 kumaneko 3761 static void ccs_truncate(char *str)
1936     {
1937     while (* (unsigned char *) str > (unsigned char) ' ')
1938     str++;
1939     *str = '\0';
1940     }
1941    
1942 kumaneko 2863 /**
1943 kumaneko 2922 * ccs_supervisor - Ask for the supervisor's decision.
1944 kumaneko 2863 *
1945 kumaneko 3701 * @r: Pointer to "struct ccs_request_info".
1946     * @fmt: The printf()'s format string, followed by parameters.
1947 kumaneko 2863 *
1948     * Returns 0 if the supervisor decided to permit the access request which
1949 kumaneko 3494 * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1950     * decided to retry the access request which violated the policy in enforcing
1951     * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1952 kumaneko 2863 */
1953 kumaneko 2922 int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1954 kumaneko 2863 {
1955     va_list args;
1956     int error = -EPERM;
1957     int pos;
1958     int len;
1959     static unsigned int ccs_serial;
1960 kumaneko 3781 struct ccs_query *entry = NULL;
1961 kumaneko 2863 bool quota_exceeded = false;
1962     char *header;
1963 kumaneko 3627 struct ccs_domain_info * const domain = ccs_current_domain();
1964 kumaneko 3746 va_start(args, fmt);
1965     len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1966     va_end(args);
1967     if (r->mode == CCS_CONFIG_LEARNING) {
1968 kumaneko 2897 char *buffer;
1969 kumaneko 3746 char *realpath = NULL;
1970     char *argv0 = NULL;
1971 kumaneko 3761 char *symlink = NULL;
1972     char *handler = NULL;
1973 kumaneko 3746 const struct ccs_preference *pref;
1974 kumaneko 2897 if (!ccs_domain_quota_ok(r))
1975     return 0;
1976 kumaneko 3761 header = ccs_init_log(&len, r);
1977     if (!header)
1978     return 0;
1979 kumaneko 3746 pref = ccs_profile(r->profile)->learning;
1980 kumaneko 3761 /* strstr() will return NULL if ordering is wrong. */
1981 kumaneko 3746 if (r->param_type == CCS_TYPE_PATH_ACL &&
1982     r->param.path.operation == CCS_TYPE_EXECUTE) {
1983     if (pref->learning_exec_argv0) {
1984 kumaneko 3761 argv0 = strstr(header, " argv[]={ \"");
1985     if (argv0) {
1986     argv0 += 10;
1987     ccs_truncate(argv0);
1988 kumaneko 3746 }
1989     }
1990 kumaneko 3761 if (pref->learning_exec_realpath) {
1991     realpath = strstr(header,
1992     " exec={ realpath=\"");
1993     if (realpath) {
1994     realpath += 8;
1995     ccs_truncate(realpath);
1996     }
1997     }
1998     } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1999     r->param.path.operation == CCS_TYPE_SYMLINK &&
2000     pref->learning_symlink_target) {
2001     symlink = strstr(header, " symlink.target=\"");
2002     if (symlink)
2003     ccs_truncate(symlink + 1);
2004 kumaneko 3746 }
2005 kumaneko 3761 handler = strstr(header, "type=execute_handler");
2006     if (handler)
2007     ccs_truncate(handler);
2008 kumaneko 3512 buffer = kmalloc(len, CCS_GFP_FLAGS);
2009 kumaneko 3746 if (buffer) {
2010     va_start(args, fmt);
2011     vsnprintf(buffer, len - 1, fmt, args);
2012     va_end(args);
2013     if (handler || realpath || argv0 || symlink) {
2014     ccs_addprintf(buffer, len, " if");
2015     if (handler)
2016 kumaneko 3761 ccs_addprintf(buffer, len, " task.%s",
2017     handler);
2018 kumaneko 3746 if (realpath)
2019 kumaneko 3761 ccs_addprintf(buffer, len, " exec.%s",
2020 kumaneko 3746 realpath);
2021     if (argv0)
2022     ccs_addprintf(buffer, len,
2023 kumaneko 3761 " exec.argv[0]=%s",
2024 kumaneko 3746 argv0);
2025     if (symlink)
2026 kumaneko 3761 ccs_addprintf(buffer, len, "%s",
2027 kumaneko 3746 symlink);
2028     }
2029     ccs_normalize_line(buffer);
2030     ccs_write_domain2(buffer, domain, false);
2031     kfree(buffer);
2032     }
2033 kumaneko 3761 kfree(header);
2034 kumaneko 2897 return 0;
2035     }
2036 kumaneko 3746 if (r->mode != CCS_CONFIG_ENFORCING)
2037     return 0;
2038 kumaneko 2863 if (!atomic_read(&ccs_query_observers)) {
2039     int i;
2040     if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2041     return -EPERM;
2042 kumaneko 3627 for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2043 kumaneko 2958 enforcing_penalty; i++) {
2044 kumaneko 2863 set_current_state(TASK_INTERRUPTIBLE);
2045     schedule_timeout(HZ / 10);
2046     }
2047     return -EPERM;
2048     }
2049 kumaneko 3694 header = ccs_init_log(&len, r);
2050 kumaneko 2863 if (!header)
2051     goto out;
2052 kumaneko 3781 entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2053     if (!entry)
2054 kumaneko 2863 goto out;
2055 kumaneko 2900 len = ccs_round2(len);
2056 kumaneko 3781 entry->query = kzalloc(len, CCS_GFP_FLAGS);
2057     if (!entry->query)
2058 kumaneko 2863 goto out;
2059     spin_lock(&ccs_query_list_lock);
2060     if (ccs_quota_for_query && ccs_query_memory_size + len +
2061 kumaneko 3781 sizeof(*entry) >= ccs_quota_for_query) {
2062 kumaneko 2863 quota_exceeded = true;
2063     } else {
2064 kumaneko 3781 ccs_query_memory_size += len + sizeof(*entry);
2065     entry->serial = ccs_serial++;
2066 kumaneko 2863 }
2067     spin_unlock(&ccs_query_list_lock);
2068     if (quota_exceeded)
2069     goto out;
2070 kumaneko 3781 pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2071     entry->serial, r->retry, header);
2072 kumaneko 2863 kfree(header);
2073     header = NULL;
2074 kumaneko 3780 va_start(args, fmt);
2075 kumaneko 3781 vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2076     entry->query_len = strlen(entry->query) + 1;
2077 kumaneko 3780 va_end(args);
2078 kumaneko 2863 spin_lock(&ccs_query_list_lock);
2079 kumaneko 3781 list_add_tail(&entry->list, &ccs_query_list);
2080 kumaneko 2863 spin_unlock(&ccs_query_list_lock);
2081     /* Give 10 seconds for supervisor's opinion. */
2082 kumaneko 3781 for (entry->timer = 0;
2083     atomic_read(&ccs_query_observers) && entry->timer < 100;
2084     entry->timer++) {
2085 kumaneko 2863 wake_up(&ccs_query_wait);
2086     set_current_state(TASK_INTERRUPTIBLE);
2087     schedule_timeout(HZ / 10);
2088 kumaneko 3781 if (entry->answer)
2089 kumaneko 2863 break;
2090     }
2091     spin_lock(&ccs_query_list_lock);
2092 kumaneko 3781 list_del(&entry->list);
2093     ccs_query_memory_size -= len + sizeof(*entry);
2094 kumaneko 2863 spin_unlock(&ccs_query_list_lock);
2095 kumaneko 3781 switch (entry->answer) {
2096 kumaneko 2863 case 3: /* Asked to retry by administrator. */
2097 kumaneko 3494 error = CCS_RETRY_REQUEST;
2098 kumaneko 2863 r->retry++;
2099     break;
2100     case 1:
2101     /* Granted by administrator. */
2102     error = 0;
2103     break;
2104     case 0:
2105     /* Timed out. */
2106     break;
2107     default:
2108     /* Rejected by administrator. */
2109     break;
2110     }
2111     out:
2112 kumaneko 3781 if (entry)
2113     kfree(entry->query);
2114     kfree(entry);
2115 kumaneko 2863 kfree(header);
2116     return error;
2117     }
2118    
2119     /**
2120     * ccs_poll_query - poll() for /proc/ccs/query.
2121     *
2122     * @file: Pointer to "struct file".
2123     * @wait: Pointer to "poll_table".
2124     *
2125     * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2126     *
2127     * Waits for access requests which violated policy in enforcing mode.
2128     */
2129     static int ccs_poll_query(struct file *file, poll_table *wait)
2130     {
2131     struct list_head *tmp;
2132     bool found = false;
2133     u8 i;
2134     for (i = 0; i < 2; i++) {
2135     spin_lock(&ccs_query_list_lock);
2136     list_for_each(tmp, &ccs_query_list) {
2137 kumaneko 3781 struct ccs_query *ptr =
2138     list_entry(tmp, typeof(*ptr), list);
2139 kumaneko 2863 if (ptr->answer)
2140     continue;
2141     found = true;
2142     break;
2143     }
2144     spin_unlock(&ccs_query_list_lock);
2145     if (found)
2146     return POLLIN | POLLRDNORM;
2147     if (i)
2148     break;
2149     poll_wait(file, &ccs_query_wait, wait);
2150     }
2151     return 0;
2152     }
2153    
2154     /**
2155     * ccs_read_query - Read access requests which violated policy in enforcing mode.
2156     *
2157     * @head: Pointer to "struct ccs_io_buffer".
2158     */
2159 kumaneko 2943 static void ccs_read_query(struct ccs_io_buffer *head)
2160 kumaneko 2863 {
2161     struct list_head *tmp;
2162     int pos = 0;
2163     int len = 0;
2164     char *buf;
2165 kumaneko 3780 if (head->r.w_pos)
2166 kumaneko 2943 return;
2167 kumaneko 2863 if (head->read_buf) {
2168     kfree(head->read_buf);
2169     head->read_buf = NULL;
2170     }
2171     spin_lock(&ccs_query_list_lock);
2172     list_for_each(tmp, &ccs_query_list) {
2173 kumaneko 3781 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2174 kumaneko 2863 if (ptr->answer)
2175     continue;
2176 kumaneko 3780 if (pos++ != head->r.query_index)
2177 kumaneko 2863 continue;
2178     len = ptr->query_len;
2179     break;
2180     }
2181     spin_unlock(&ccs_query_list_lock);
2182     if (!len) {
2183 kumaneko 3780 head->r.query_index = 0;
2184 kumaneko 2943 return;
2185 kumaneko 2863 }
2186 kumaneko 3512 buf = kzalloc(len, CCS_GFP_FLAGS);
2187 kumaneko 2863 if (!buf)
2188 kumaneko 2943 return;
2189 kumaneko 2863 pos = 0;
2190     spin_lock(&ccs_query_list_lock);
2191     list_for_each(tmp, &ccs_query_list) {
2192 kumaneko 3781 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2193 kumaneko 2863 if (ptr->answer)
2194     continue;
2195 kumaneko 3780 if (pos++ != head->r.query_index)
2196 kumaneko 2863 continue;
2197     /*
2198     * Some query can be skipped because ccs_query_list
2199     * can change, but I don't care.
2200     */
2201     if (len == ptr->query_len)
2202     memmove(buf, ptr->query, len);
2203     break;
2204     }
2205     spin_unlock(&ccs_query_list_lock);
2206     if (buf[0]) {
2207     head->read_buf = buf;
2208 kumaneko 3780 head->r.w[head->r.w_pos++] = buf;
2209     head->r.query_index++;
2210 kumaneko 2863 } else {
2211     kfree(buf);
2212     }
2213     }
2214    
2215     /**
2216     * ccs_write_answer - Write the supervisor's decision.
2217     *
2218     * @head: Pointer to "struct ccs_io_buffer".
2219     *
2220     * Returns 0 on success, -EINVAL otherwise.
2221     */
2222     static int ccs_write_answer(struct ccs_io_buffer *head)
2223     {
2224     char *data = head->write_buf;
2225     struct list_head *tmp;
2226     unsigned int serial;
2227     unsigned int answer;
2228     spin_lock(&ccs_query_list_lock);
2229     list_for_each(tmp, &ccs_query_list) {
2230 kumaneko 3781 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2231 kumaneko 2863 ptr->timer = 0;
2232     }
2233     spin_unlock(&ccs_query_list_lock);
2234     if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2235     return -EINVAL;
2236     spin_lock(&ccs_query_list_lock);
2237     list_for_each(tmp, &ccs_query_list) {
2238 kumaneko 3781 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2239 kumaneko 2863 if (ptr->serial != serial)
2240     continue;
2241     if (!ptr->answer)
2242     ptr->answer = answer;
2243     break;
2244     }
2245     spin_unlock(&ccs_query_list_lock);
2246     return 0;
2247     }
2248    
2249     /**
2250     * ccs_read_version: Get version.
2251     *
2252     * @head: Pointer to "struct ccs_io_buffer".
2253     */
2254 kumaneko 2943 static void ccs_read_version(struct ccs_io_buffer *head)
2255 kumaneko 2863 {
2256 kumaneko 3780 if (head->r.eof)
2257 kumaneko 2943 return;
2258 kumaneko 3780 ccs_set_string(head, "1.7.2");
2259     head->r.eof = true;
2260 kumaneko 2863 }
2261    
2262     /**
2263     * ccs_read_self_domain - Get the current process's domainname.
2264     *
2265     * @head: Pointer to "struct ccs_io_buffer".
2266     */
2267 kumaneko 2943 static void ccs_read_self_domain(struct ccs_io_buffer *head)
2268 kumaneko 2863 {
2269 kumaneko 3780 if (head->r.eof)
2270 kumaneko 2943 return;
2271     /*
2272