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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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