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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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