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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2916 - (hide annotations) (download) (as text)
Mon Aug 17 12:23:47 2009 UTC (14 years, 9 months ago) by kumaneko
Original Path: branches/ccs-patch/security/ccsecurity/policy_io.c
File MIME type: text/x-csrc
File size: 67154 byte(s)


1 kumaneko 2863 /*
2 kumaneko 2864 * security/ccsecurity/policy_io.c
3 kumaneko 2863 *
4     * Copyright (C) 2005-2009 NTT DATA CORPORATION
5     *
6 kumaneko 2869 * Version: 1.7.0-pre 2009/08/08
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     /* Lock for protecting ccs_profile->comment */
16     static DEFINE_SPINLOCK(ccs_profile_comment_lock);
17    
18 kumaneko 2915 /* String table for functionality that takes 2 modes. */
19     static const char *ccs_mode_2[2] = {
20     "disabled", "enabled"
21     };
22 kumaneko 2863
23 kumaneko 2915 static const char *ccs_keyword_mode[4] = {
24     "MAC_MODE_DISABLED", "MAC_MODE_LEARNING",
25     "MAC_MODE_PERMISSIVE", "MAC_MODE_ENFORCING"
26 kumaneko 2863 };
27 kumaneko 2915
28     static const char *ccs_keyword_capability_mode[4] = {
29     "MAC_MODE_CAPABILITY_DISABLED", "MAC_MODE_CAPABILITY_LEARNING",
30     "MAC_MODE_CAPABILITY_PERMISSIVE", "MAC_MODE_CAPABILITY_ENFORCING"
31 kumaneko 2863 };
32    
33 kumaneko 2915 static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX] = {
34     [CCS_MAC_EXECUTE] = "execute",
35     [CCS_MAC_OPEN] = "open",
36     [CCS_MAC_CREATE] = "create",
37     [CCS_MAC_UNLINK] = "unlink",
38     [CCS_MAC_MKDIR] = "mkdir",
39     [CCS_MAC_RMDIR] = "rmdir",
40     [CCS_MAC_MKFIFO] = "mkfifo",
41     [CCS_MAC_MKSOCK] = "mksock",
42     [CCS_MAC_TRUNCATE] = "truncate",
43     [CCS_MAC_SYMLINK] = "symlink",
44     [CCS_MAC_REWRITE] = "rewrite",
45     [CCS_MAC_MKBLOCK] = "mkblock",
46     [CCS_MAC_MKCHAR] = "mkchar",
47     [CCS_MAC_LINK] = "link",
48     [CCS_MAC_RENAME] = "rename",
49     [CCS_MAC_CHMOD] = "chmod",
50     [CCS_MAC_CHOWN] = "chown",
51     [CCS_MAC_CHGRP] = "chgrp",
52     [CCS_MAC_IOCTL] = "ioctl",
53     [CCS_MAC_CHROOT] = "chroot",
54     [CCS_MAC_MOUNT] = "mount",
55     [CCS_MAC_UMOUNT] = "umount",
56     [CCS_MAC_PIVOT_ROOT] = "pivot_root",
57     [CCS_MAC_ENVIRON] = "env",
58     [CCS_MAC_NETWORK] = "network",
59     [CCS_MAC_SIGNAL] = "signal"
60     };
61    
62 kumaneko 2863 /* Table for profile. */
63     static struct {
64     const char *keyword;
65     unsigned int current_value;
66     const unsigned int max_value;
67     } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {
68 kumaneko 2893 [CCS_AUTOLEARN_EXEC_REALPATH] = { "AUTOLEARN_EXEC_REALPATH", 0, 1 },
69     [CCS_AUTOLEARN_EXEC_ARGV0] = { "AUTOLEARN_EXEC_ARGV0", 0, 1 },
70 kumaneko 2863 [CCS_RESTRICT_AUTOBIND] = { "RESTRICT_AUTOBIND", 0, 1 },
71     [CCS_MAX_ACCEPT_ENTRY]
72     = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },
73     #ifdef CONFIG_CCSECURITY_AUDIT
74     [CCS_MAX_GRANT_LOG]
75     = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },
76     [CCS_MAX_REJECT_LOG]
77     = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },
78     #endif
79     [CCS_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
80     [CCS_SLEEP_PERIOD]
81     = { "SLEEP_PERIOD", 0, 3000 }, /* in 0.1 second */
82     };
83    
84     /* Permit policy management by non-root user? */
85     static bool ccs_manage_by_non_root;
86    
87     /**
88     * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.
89     *
90     * @str: Unused.
91     *
92     * Returns 0.
93     */
94     static int __init ccs_quiet_setup(char *str)
95     {
96     ccs_control_array[CCS_VERBOSE].current_value = 0;
97     return 0;
98     }
99    
100     __setup("CCS_QUIET", ccs_quiet_setup);
101    
102     /**
103     * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
104     *
105     * @head: Pointer to "struct ccs_io_buffer".
106     * @fmt: The printf()'s format string, followed by parameters.
107     *
108     * Returns true on success, false otherwise.
109     *
110     * The snprintf() will truncate, but ccs_io_printf() won't.
111     */
112     bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
113     {
114     va_list args;
115     int len;
116     int pos = head->read_avail;
117     int size = head->readbuf_size - pos;
118     if (size <= 0)
119     return false;
120     va_start(args, fmt);
121     len = vsnprintf(head->read_buf + pos, size, fmt, args);
122     va_end(args);
123     if (pos + len >= head->readbuf_size)
124     return false;
125     head->read_avail += len;
126     return true;
127     }
128    
129     /**
130     * ccs_find_or_assign_new_profile - Create a new profile.
131     *
132     * @profile: Profile number to create.
133     *
134     * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
135     */
136 kumaneko 2892 static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int
137     profile)
138 kumaneko 2863 {
139     struct ccs_profile *ptr;
140     struct ccs_profile *entry;
141     int i;
142 kumaneko 2892 if (profile >= CCS_MAX_PROFILES)
143 kumaneko 2863 return NULL;
144     ptr = ccs_profile_ptr[profile];
145     if (ptr)
146     return ptr;
147     entry = kzalloc(sizeof(*entry), GFP_KERNEL);
148     mutex_lock(&ccs_policy_lock);
149     ptr = ccs_profile_ptr[profile];
150     if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
151     ptr = entry;
152     for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)
153     ptr->value[i] = ccs_control_array[i].current_value;
154     mb(); /* Avoid out-of-order execution. */
155     ccs_profile_ptr[profile] = ptr;
156     entry = NULL;
157     }
158     mutex_unlock(&ccs_policy_lock);
159     kfree(entry);
160     return ptr;
161     }
162    
163     /**
164     * ccs_write_profile - Write profile table.
165     *
166     * @head: Pointer to "struct ccs_io_buffer".
167     *
168     * Returns 0 on success, negative value otherwise.
169     */
170     static int ccs_write_profile(struct ccs_io_buffer *head)
171     {
172     char *data = head->write_buf;
173     unsigned int i;
174     unsigned int value;
175 kumaneko 2915 int mode;
176 kumaneko 2863 char *cp;
177     struct ccs_profile *ccs_profile;
178     i = simple_strtoul(data, &cp, 10);
179     if (data != cp) {
180     if (*cp != '-')
181     return -EINVAL;
182     data = cp + 1;
183     }
184     ccs_profile = ccs_find_or_assign_new_profile(i);
185     if (!ccs_profile)
186     return -EINVAL;
187     cp = strchr(data, '=');
188     if (!cp)
189     return -EINVAL;
190     *cp = '\0';
191     if (!strcmp(data, "COMMENT")) {
192     const struct ccs_path_info *new_comment
193     = ccs_get_name(cp + 1);
194     const struct ccs_path_info *old_comment;
195     /* Protect reader from ccs_put_name(). */
196     /***** CRITICAL SECTION START *****/
197     spin_lock(&ccs_profile_comment_lock);
198     old_comment = ccs_profile->comment;
199     ccs_profile->comment = new_comment;
200     spin_unlock(&ccs_profile_comment_lock);
201     /***** CRITICAL SECTION END *****/
202     ccs_put_name(old_comment);
203     return 0;
204     }
205 kumaneko 2915 for (mode = 0; mode < 4; mode++) {
206     if (strcmp(data, ccs_keyword_mode[mode]))
207     continue;
208 kumaneko 2908 cp++;
209     while (1) {
210     char *cp2 = strchr(cp, ' ');
211     if (cp2)
212     *cp2 = '\0';
213 kumaneko 2915 for (i = 0; i < CCS_MAX_MAC_INDEX; i++) {
214     if (strcmp(cp, ccs_mac_keywords[i]))
215     continue;
216     ccs_profile->mac_mode[i] = mode;
217     }
218     if (!cp2)
219     break;
220     cp = cp2 + 1;
221     }
222     return 0;
223     }
224     for (mode = 0; mode < 4; mode++) {
225     if (strcmp(data, ccs_keyword_capability_mode[mode]))
226     continue;
227     cp++;
228     while (1) {
229     char *cp2 = strchr(cp, ' ');
230     if (cp2)
231     *cp2 = '\0';
232 kumaneko 2908 for (i = 0; i < CCS_MAX_CAPABILITY_INDEX; i++) {
233     if (strcmp(cp, ccs_capability_list[i]))
234 kumaneko 2863 continue;
235 kumaneko 2915 ccs_profile->mac_capability_mode[i] = mode;
236 kumaneko 2908 }
237     if (!cp2)
238 kumaneko 2863 break;
239 kumaneko 2908 cp = cp2 + 1;
240 kumaneko 2863 }
241 kumaneko 2908 return 0;
242 kumaneko 2863 }
243     for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {
244     if (strcmp(data, ccs_control_array[i].keyword))
245     continue;
246     if (sscanf(cp + 1, "%u", &value) != 1) {
247     int j;
248 kumaneko 2915 for (j = 0; j < 2; j++) {
249     if (strcmp(cp + 1, ccs_mode_2[j]))
250 kumaneko 2863 continue;
251     value = j;
252     break;
253     }
254     if (j == 4)
255     return -EINVAL;
256     } else if (value > ccs_control_array[i].max_value) {
257     value = ccs_control_array[i].max_value;
258     }
259     ccs_profile->value[i] = value;
260     return 0;
261     }
262     return -EINVAL;
263     }
264    
265 kumaneko 2915 static bool ccs_print_mac_mode(struct ccs_io_buffer *head, u8 index)
266 kumaneko 2908 {
267     const int pos = head->read_avail;
268     int i;
269 kumaneko 2915 int mode;
270 kumaneko 2908 const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];
271 kumaneko 2915 for (mode = 0; mode < 4; mode++) {
272     if (!ccs_io_printf(head, "%u-%s={", index, ccs_keyword_mode[mode]))
273 kumaneko 2908 goto out;
274 kumaneko 2915 for (i = 0; i < CCS_MAX_MAC_INDEX; i++) {
275     if (ccs_profile->mac_mode[i] != mode)
276     continue;
277     if (!ccs_io_printf(head, " %s", ccs_mac_keywords[i]))
278     goto out;
279     }
280     if (!ccs_io_printf(head, " }\n"))
281     goto out;
282 kumaneko 2908 }
283     return true;
284     out:
285     head->read_avail = pos;
286     return false;
287     }
288    
289 kumaneko 2915 static bool ccs_print_capability_mode(struct ccs_io_buffer *head, u8 index)
290     {
291     const int pos = head->read_avail;
292     int i;
293     int mode;
294     const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];
295     for (mode = 0; mode < 4; mode++) {
296     if (!ccs_io_printf(head, "%u-%s={", index, ccs_keyword_capability_mode[mode]))
297     goto out;
298     for (i = 0; i < CCS_MAX_CAPABILITY_INDEX; i++) {
299     if (ccs_profile->mac_capability_mode[i] != mode)
300     continue;
301     if (!ccs_io_printf(head, " %s", ccs_capability_list[i]))
302     goto out;
303     }
304     if (!ccs_io_printf(head, " }\n"))
305     goto out;
306     }
307     return true;
308     out:
309     head->read_avail = pos;
310     return false;
311     }
312    
313 kumaneko 2863 /**
314     * ccs_read_profile - Read profile table.
315     *
316     * @head: Pointer to "struct ccs_io_buffer".
317     *
318     * Returns 0.
319     */
320     static int ccs_read_profile(struct ccs_io_buffer *head)
321     {
322 kumaneko 2908 static const int ccs_total = CCS_MAX_CONTROL_INDEX + 2;
323 kumaneko 2863 int step;
324     if (head->read_eof)
325     return 0;
326 kumaneko 2892 for (step = head->read_step; step < CCS_MAX_PROFILES * ccs_total;
327     step++) {
328 kumaneko 2863 const u8 index = step / ccs_total;
329     u8 type = step % ccs_total;
330     const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];
331     head->read_step = step;
332     if (!ccs_profile)
333     continue;
334     if (!type) { /* Print profile' comment tag. */
335     bool done;
336     /***** CRITICAL SECTION START *****/
337     spin_lock(&ccs_profile_comment_lock);
338     done = ccs_io_printf(head, "%u-COMMENT=%s\n",
339     index, ccs_profile->comment ?
340     ccs_profile->comment->name : "");
341     spin_unlock(&ccs_profile_comment_lock);
342     /***** CRITICAL SECTION END *****/
343     if (!done)
344     break;
345     continue;
346 kumaneko 2915 } else if (type == 1) {
347     if (!ccs_print_mac_mode(head, index))
348 kumaneko 2863 break;
349 kumaneko 2908 continue;
350 kumaneko 2915 } else if (type == 2) {
351     if (!ccs_print_capability_mode(head, index))
352     break;
353     continue;
354 kumaneko 2908 }
355 kumaneko 2915 type -= 3;
356 kumaneko 2908 {
357 kumaneko 2863 const unsigned int value = ccs_profile->value[type];
358     const char *keyword = ccs_control_array[type].keyword;
359 kumaneko 2915 if (ccs_control_array[type].max_value == 1) {
360 kumaneko 2863 if (!ccs_io_printf(head, "%u-%s=%s\n", index,
361 kumaneko 2915 keyword, ccs_mode_2[value]))
362 kumaneko 2863 break;
363     } else {
364     if (!ccs_io_printf(head, "%u-%s=%u\n", index,
365     keyword, value))
366     break;
367     }
368     }
369     }
370 kumaneko 2892 if (step == CCS_MAX_PROFILES * ccs_total)
371 kumaneko 2863 head->read_eof = true;
372     return 0;
373     }
374    
375     /* The list for "struct ccs_policy_manager_entry". */
376     LIST_HEAD(ccs_policy_manager_list);
377    
378     /**
379     * ccs_update_manager_entry - Add a manager entry.
380     *
381     * @manager: The path to manager or the domainnamme.
382     * @is_delete: True if it is a delete request.
383     *
384     * Returns 0 on success, negative value otherwise.
385     */
386     static int ccs_update_manager_entry(const char *manager, const bool is_delete)
387     {
388     struct ccs_policy_manager_entry *entry = NULL;
389     struct ccs_policy_manager_entry *ptr;
390 kumaneko 2900 struct ccs_policy_manager_entry e = { };
391 kumaneko 2863 int error = is_delete ? -ENOENT : -ENOMEM;
392     if (ccs_is_domain_def(manager)) {
393     if (!ccs_is_correct_domain(manager))
394     return -EINVAL;
395 kumaneko 2900 e.is_domain = true;
396 kumaneko 2863 } else {
397     if (!ccs_is_correct_path(manager, 1, -1, -1))
398     return -EINVAL;
399     }
400 kumaneko 2900 e.manager = ccs_get_name(manager);
401     if (!e.manager)
402 kumaneko 2863 return -ENOMEM;
403     if (!is_delete)
404 kumaneko 2900 entry = kmalloc(sizeof(e), GFP_KERNEL);
405 kumaneko 2863 mutex_lock(&ccs_policy_lock);
406     list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
407 kumaneko 2900 if (ptr->manager != e.manager)
408 kumaneko 2863 continue;
409     ptr->is_deleted = is_delete;
410     error = 0;
411     break;
412     }
413 kumaneko 2900 if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
414 kumaneko 2863 list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);
415     entry = NULL;
416     error = 0;
417     }
418     mutex_unlock(&ccs_policy_lock);
419 kumaneko 2900 ccs_put_name(e.manager);
420 kumaneko 2863 kfree(entry);
421     return error;
422     }
423    
424     /**
425     * ccs_write_manager_policy - Write manager policy.
426     *
427     * @head: Pointer to "struct ccs_io_buffer".
428     *
429     * Returns 0 on success, negative value otherwise.
430     */
431     static int ccs_write_manager_policy(struct ccs_io_buffer *head)
432     {
433     char *data = head->write_buf;
434 kumaneko 2892 bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
435 kumaneko 2863 if (!strcmp(data, "manage_by_non_root")) {
436     ccs_manage_by_non_root = !is_delete;
437     return 0;
438     }
439     return ccs_update_manager_entry(data, is_delete);
440     }
441    
442     /**
443     * ccs_read_manager_policy - Read manager policy.
444     *
445     * @head: Pointer to "struct ccs_io_buffer".
446     *
447     * Returns 0.
448     *
449     * Caller holds ccs_read_lock().
450     */
451     static int ccs_read_manager_policy(struct ccs_io_buffer *head)
452     {
453     struct list_head *pos;
454     ccs_check_read_lock();
455     if (head->read_eof)
456     return 0;
457     list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {
458     struct ccs_policy_manager_entry *ptr;
459     ptr = list_entry(pos, struct ccs_policy_manager_entry, list);
460     if (ptr->is_deleted)
461     continue;
462     if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
463     return 0;
464     }
465     head->read_eof = true;
466     return 0;
467     }
468    
469     /**
470     * ccs_is_policy_manager - Check whether the current process is a policy manager.
471     *
472     * Returns true if the current process is permitted to modify policy
473     * via /proc/ccs/ interface.
474     *
475     * Caller holds ccs_read_lock().
476     */
477     static bool ccs_is_policy_manager(void)
478     {
479     struct ccs_policy_manager_entry *ptr;
480     const char *exe;
481     struct task_struct *task = current;
482     const struct ccs_path_info *domainname
483     = ccs_current_domain()->domainname;
484     bool found = false;
485     ccs_check_read_lock();
486     if (!ccs_policy_loaded)
487     return true;
488     if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)
489     return true;
490     if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
491     return false;
492     list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
493     if (!ptr->is_deleted && ptr->is_domain
494     && !ccs_pathcmp(domainname, ptr->manager)) {
495     /* Set manager flag. */
496     task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;
497     return true;
498     }
499     }
500     exe = ccs_get_exe();
501     if (!exe)
502     return false;
503     list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
504     if (!ptr->is_deleted && !ptr->is_domain
505     && !strcmp(exe, ptr->manager->name)) {
506     found = true;
507     /* Set manager flag. */
508     task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;
509     break;
510     }
511     }
512     if (!found) { /* Reduce error messages. */
513     static pid_t ccs_last_pid;
514     const pid_t pid = current->pid;
515     if (ccs_last_pid != pid) {
516     printk(KERN_WARNING "%s ( %s ) is not permitted to "
517     "update policies.\n", domainname->name, exe);
518     ccs_last_pid = pid;
519     }
520     }
521     kfree(exe);
522     return found;
523     }
524    
525     /**
526     * ccs_find_condition_part - Find condition part from the statement.
527     *
528     * @data: String to parse.
529     *
530     * Returns pointer to the condition part if it was found in the statement,
531     * NULL otherwise.
532     */
533     static char *ccs_find_condition_part(char *data)
534     {
535     char *cp = strstr(data, " if ");
536     if (cp) {
537     while (1) {
538     char *cp2 = strstr(cp + 3, " if ");
539     if (!cp2)
540     break;
541     cp = cp2;
542     }
543     *cp++ = '\0';
544     } else {
545     cp = strstr(data, " ; set ");
546     if (cp)
547     *cp++ = '\0';
548     }
549     return cp;
550     }
551    
552     /**
553     * ccs_is_select_one - Parse select command.
554     *
555     * @head: Pointer to "struct ccs_io_buffer".
556     * @data: String to parse.
557     *
558     * Returns true on success, false otherwise.
559     *
560     * Caller holds ccs_read_lock().
561     */
562     static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)
563     {
564     unsigned int pid;
565     struct ccs_domain_info *domain = NULL;
566     ccs_check_read_lock();
567     if (!strcmp(data, "allow_execute")) {
568     head->read_execute_only = true;
569     return true;
570     }
571     if (sscanf(data, "pid=%u", &pid) == 1) {
572     struct task_struct *p;
573     /***** CRITICAL SECTION START *****/
574     read_lock(&tasklist_lock);
575     p = find_task_by_pid(pid);
576     if (p)
577     domain = ccs_task_domain(p);
578     read_unlock(&tasklist_lock);
579     /***** CRITICAL SECTION END *****/
580     } else if (!strncmp(data, "domain=", 7)) {
581     if (ccs_is_domain_def(data + 7))
582     domain = ccs_find_domain(data + 7);
583     } else
584     return false;
585     head->write_var1 = domain;
586     /* Accessing read_buf is safe because head->io_sem is held. */
587     if (!head->read_buf)
588     return true; /* Do nothing if open(O_WRONLY). */
589     head->read_avail = 0;
590     ccs_io_printf(head, "# select %s\n", data);
591     head->read_single_domain = true;
592     head->read_eof = !domain;
593     if (domain) {
594     struct ccs_domain_info *d;
595     head->read_var1 = NULL;
596     list_for_each_entry_rcu(d, &ccs_domain_list, list) {
597     if (d == domain)
598     break;
599     head->read_var1 = &d->list;
600     }
601     head->read_var2 = NULL;
602     head->read_bit = 0;
603     head->read_step = 0;
604     if (domain->is_deleted)
605     ccs_io_printf(head, "# This is a deleted domain.\n");
606     }
607     return true;
608     }
609    
610 kumaneko 2897 static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,
611     struct ccs_condition *cond,
612     const bool is_delete)
613     {
614     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))
615     return ccs_write_capability_policy(data, domain, cond,
616     is_delete);
617     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))
618     return ccs_write_network_policy(data, domain, cond, is_delete);
619     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))
620     return ccs_write_signal_policy(data, domain, cond, is_delete);
621     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))
622     return ccs_write_env_policy(data, domain, cond, is_delete);
623     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))
624     return ccs_write_mount_policy(data, domain, cond, is_delete);
625     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_UNMOUNT))
626     return ccs_write_umount_policy(data, domain, cond, is_delete);
627     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CHROOT))
628     return ccs_write_chroot_policy(data, domain, cond, is_delete);
629     if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_PIVOT_ROOT))
630     return ccs_write_pivot_root_policy(data, domain, cond,
631     is_delete);
632     return ccs_write_file_policy(data, domain, cond, is_delete);
633     }
634    
635 kumaneko 2863 /**
636     * ccs_write_domain_policy - Write domain policy.
637     *
638     * @head: Pointer to "struct ccs_io_buffer".
639     *
640     * Returns 0 on success, negative value otherwise.
641     */
642     static int ccs_write_domain_policy(struct ccs_io_buffer *head)
643     {
644     char *data = head->write_buf;
645     struct ccs_domain_info *domain = head->write_var1;
646     bool is_delete = false;
647     bool is_select = false;
648     unsigned int profile;
649     struct ccs_condition *cond = NULL;
650     char *cp;
651     int error;
652 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
653 kumaneko 2863 is_delete = true;
654 kumaneko 2892 else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
655 kumaneko 2863 is_select = true;
656     if (is_select && ccs_is_select_one(head, data))
657     return 0;
658     /* Don't allow updating policies by non manager programs. */
659     if (!ccs_is_policy_manager())
660     return -EPERM;
661     if (ccs_is_domain_def(data)) {
662     domain = NULL;
663     if (is_delete)
664     ccs_delete_domain(data);
665     else if (is_select)
666     domain = ccs_find_domain(data);
667     else
668     domain = ccs_find_or_assign_new_domain(data, 0);
669     head->write_var1 = domain;
670     return 0;
671     }
672     if (!domain)
673     return -EINVAL;
674    
675 kumaneko 2892 if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
676     && profile < CCS_MAX_PROFILES) {
677 kumaneko 2863 if (ccs_profile_ptr[profile] || !ccs_policy_loaded)
678     domain->profile = (u8) profile;
679     return 0;
680     }
681 kumaneko 2892 if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
682 kumaneko 2863 domain->ignore_global_allow_read = !is_delete;
683     return 0;
684     }
685 kumaneko 2892 if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
686 kumaneko 2863 domain->ignore_global_allow_env = !is_delete;
687     return 0;
688     }
689     cp = ccs_find_condition_part(data);
690     if (cp) {
691     cond = ccs_get_condition(cp);
692     if (!cond)
693     return -EINVAL;
694     }
695 kumaneko 2897 error = ccs_write_domain_policy2(data, domain, cond, is_delete);
696 kumaneko 2863 if (cond)
697     ccs_put_condition(cond);
698     return error;
699     }
700    
701 kumaneko 2888 static bool ccs_print_name_union(struct ccs_io_buffer *head,
702 kumaneko 2894 const struct ccs_name_union *ptr)
703 kumaneko 2863 {
704 kumaneko 2894 int pos = head->read_avail;
705 kumaneko 2863 if (pos && head->read_buf[pos - 1] == ' ')
706     head->read_avail--;
707 kumaneko 2888 if (ptr->is_group)
708 kumaneko 2870 return ccs_io_printf(head, " @%s",
709 kumaneko 2888 ptr->group->group_name->name);
710     return ccs_io_printf(head, " %s", ptr->filename->name);
711 kumaneko 2863 }
712    
713 kumaneko 2894 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
714     const struct ccs_name_union *ptr)
715 kumaneko 2863 {
716 kumaneko 2894 if (ptr->is_group)
717     return ccs_io_printf(head, "@%s",
718     ptr->group->group_name->name);
719     return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
720     }
721    
722     static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
723     const struct ccs_number_union *ptr,
724     const bool need_space)
725     {
726 kumaneko 2888 unsigned long min;
727     unsigned long max;
728 kumaneko 2890 u8 min_type;
729     u8 max_type;
730 kumaneko 2894 if (need_space && !ccs_io_printf(head, " "))
731     return false;
732 kumaneko 2888 if (ptr->is_group)
733 kumaneko 2894 return ccs_io_printf(head, "@%s",
734 kumaneko 2888 ptr->group->group_name->name);
735 kumaneko 2890 min_type = ptr->min_type;
736     max_type = ptr->max_type;
737 kumaneko 2888 min = ptr->values[0];
738     max = ptr->values[1];
739 kumaneko 2890 switch (min_type) {
740 kumaneko 2892 case CCS_VALUE_TYPE_HEXADECIMAL:
741 kumaneko 2894 if (!ccs_io_printf(head, "0x%lX", min))
742 kumaneko 2890 return false;
743     break;
744 kumaneko 2892 case CCS_VALUE_TYPE_OCTAL:
745 kumaneko 2894 if (!ccs_io_printf(head, "0%lo", min))
746 kumaneko 2890 return false;
747     break;
748     default:
749 kumaneko 2894 if (!ccs_io_printf(head, "%lu", min))
750 kumaneko 2890 return false;
751     break;
752     }
753     if (min == max && min_type == max_type)
754     return true;
755     switch (max_type) {
756 kumaneko 2892 case CCS_VALUE_TYPE_HEXADECIMAL:
757 kumaneko 2890 return ccs_io_printf(head, "-0x%lX", max);
758 kumaneko 2892 case CCS_VALUE_TYPE_OCTAL:
759 kumaneko 2890 return ccs_io_printf(head, "-0%lo", max);
760     default:
761     return ccs_io_printf(head, "-%lu", max);
762     }
763 kumaneko 2863 }
764    
765 kumaneko 2900 bool ccs_print_number_union(struct ccs_io_buffer *head,
766     const struct ccs_number_union *ptr)
767 kumaneko 2894 {
768     return ccs_print_number_union_common(head, ptr, true);
769     }
770    
771     static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
772     const struct ccs_number_union *ptr)
773     {
774     return ccs_print_number_union_common(head, ptr, false);
775     }
776    
777 kumaneko 2863 /**
778 kumaneko 2894 * ccs_print_condition - Print condition part.
779     *
780     * @head: Pointer to "struct ccs_io_buffer".
781     * @cond: Pointer to "struct ccs_condition". May be NULL.
782     *
783     * Returns true on success, false otherwise.
784     */
785     static bool ccs_print_condition(struct ccs_io_buffer *head,
786     const struct ccs_condition *cond)
787     {
788     const struct ccs_condition_element *condp;
789     const struct ccs_number_union *numbers_p;
790     const struct ccs_name_union *names_p;
791     const struct ccs_argv_entry *argv;
792     const struct ccs_envp_entry *envp;
793     u16 condc;
794     u16 i;
795     u16 j;
796     char buffer[32];
797     if (!cond)
798     goto no_condition;
799     condc = cond->condc;
800     condp = (const struct ccs_condition_element *) (cond + 1);
801     numbers_p = (const struct ccs_number_union *) (condp + condc);
802     names_p = (const struct ccs_name_union *)
803     (numbers_p + cond->numbers_count);
804     argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);
805     envp = (const struct ccs_envp_entry *) (argv + cond->argc);
806     memset(buffer, 0, sizeof(buffer));
807     if (condc && !ccs_io_printf(head, "%s", " if"))
808     goto out;
809     for (i = 0; i < condc; i++) {
810     const u8 match = condp->equals;
811     const u8 left = condp->left;
812     const u8 right = condp->right;
813     condp++;
814     switch (left) {
815     case CCS_ARGV_ENTRY:
816     if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",
817     argv->index, argv->is_not ?
818     "!=" : "=", argv->value->name))
819     goto out;
820     argv++;
821     continue;
822     case CCS_ENVP_ENTRY:
823     if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",
824     envp->name->name, envp->is_not ?
825     "!=" : "="))
826     goto out;
827     if (envp->value) {
828     if (!ccs_io_printf(head, "\"%s\"",
829     envp->value->name))
830     goto out;
831     } else {
832     if (!ccs_io_printf(head, "NULL"))
833     goto out;
834     }
835     envp++;
836     continue;
837     case CCS_NUMBER_UNION:
838     if (!ccs_print_number_union(head, numbers_p++))
839     goto out;
840     break;
841     default:
842     if (left >= CCS_MAX_CONDITION_KEYWORD)
843     goto out;
844     if (!ccs_io_printf(head, " %s",
845     ccs_condition_keyword[left]))
846     goto out;
847     break;
848     }
849     if (!ccs_io_printf(head, "%s", match ? "=" : "!="))
850     goto out;
851     switch (right) {
852     case CCS_NAME_UNION:
853     if (!ccs_print_name_union_quoted(head, names_p++))
854     goto out;
855     break;
856     case CCS_NUMBER_UNION:
857     if (!ccs_print_number_union_nospace(head, numbers_p++))
858     goto out;
859     break;
860     default:
861     if (right >= CCS_MAX_CONDITION_KEYWORD)
862     goto out;
863     if (!ccs_io_printf(head, "%s",
864     ccs_condition_keyword[right]))
865     goto out;
866     break;
867     }
868     }
869     i = cond->post_state[3];
870     if (!i)
871     goto no_condition;
872     if (!ccs_io_printf(head, " ; set"))
873     goto out;
874     for (j = 0; j < 3; j++) {
875     if (!(i & (1 << j)))
876     continue;
877     if (!ccs_io_printf(head, " task.state[%u]=%u", j,
878     cond->post_state[j]))
879     goto out;
880     }
881     no_condition:
882     if (ccs_io_printf(head, "\n"))
883     return true;
884     out:
885     return false;
886     }
887    
888     /**
889 kumaneko 2915 * ccs_print_path_acl - Print a single path ACL entry.
890 kumaneko 2863 *
891     * @head: Pointer to "struct ccs_io_buffer".
892 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_acl".
893 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
894     *
895     * Returns true on success, false otherwise.
896     */
897 kumaneko 2915 static bool ccs_print_path_acl(struct ccs_io_buffer *head,
898     struct ccs_path_acl *ptr,
899 kumaneko 2863 const struct ccs_condition *cond)
900     {
901     int pos;
902     u8 bit;
903     const u16 perm = ptr->perm;
904 kumaneko 2915 for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
905 kumaneko 2863 if (!(perm & (1 << bit)))
906     continue;
907 kumaneko 2915 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)
908 kumaneko 2863 continue;
909     /* Print "read/write" instead of "read" and "write". */
910 kumaneko 2915 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
911     && (perm & (1 << CCS_TYPE_READ_WRITE)))
912 kumaneko 2863 continue;
913     pos = head->read_avail;
914 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||
915 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
916 kumaneko 2897 !ccs_print_condition(head, cond)) {
917     head->read_bit = bit;
918     head->read_avail = pos;
919     return false;
920     }
921 kumaneko 2863 }
922     head->read_bit = 0;
923     return true;
924     }
925    
926     /**
927 kumaneko 2915 * ccs_print_path_number_number_acl - Print a path_number_number ACL entry.
928 kumaneko 2863 *
929     * @head: Pointer to "struct ccs_io_buffer".
930 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_number_number_acl".
931 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
932     *
933     * Returns true on success, false otherwise.
934     */
935 kumaneko 2915 static bool ccs_print_path_number_number_acl(struct ccs_io_buffer *head,
936     struct ccs_path_number_number_acl *ptr,
937 kumaneko 2863 const struct ccs_condition *cond)
938     {
939     int pos;
940     u8 bit;
941     const u16 perm = ptr->perm;
942 kumaneko 2916 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_NUMBER_OPERATION;
943     bit++) {
944 kumaneko 2863 if (!(perm & (1 << bit)))
945     continue;
946     pos = head->read_avail;
947 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
948     ccs_path_number_number2keyword(bit)) ||
949 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
950     !ccs_print_number_union(head, &ptr->major) ||
951     !ccs_print_number_union(head, &ptr->minor) ||
952 kumaneko 2897 !ccs_print_condition(head, cond)) {
953     head->read_bit = bit;
954     head->read_avail = pos;
955     return false;
956     }
957 kumaneko 2863 }
958     head->read_bit = 0;
959     return true;
960     }
961    
962     /**
963 kumaneko 2915 * ccs_print_path_path_acl - Print a double path ACL entry.
964 kumaneko 2863 *
965     * @head: Pointer to "struct ccs_io_buffer".
966 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_path_acl".
967 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
968     *
969     * Returns true on success, false otherwise.
970     */
971 kumaneko 2915 static bool ccs_print_path_path_acl(struct ccs_io_buffer *head,
972     struct ccs_path_path_acl *ptr,
973 kumaneko 2863 const struct ccs_condition *cond)
974     {
975     int pos;
976     u8 bit;
977     const u8 perm = ptr->perm;
978 kumaneko 2915 for (bit = head->read_bit; bit < CCS_MAX_PATH_PATH_OPERATION; bit++) {
979 kumaneko 2863 if (!(perm & (1 << bit)))
980     continue;
981     pos = head->read_avail;
982 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
983     ccs_path_path2keyword(bit)) ||
984 kumaneko 2888 !ccs_print_name_union(head, &ptr->name1) ||
985     !ccs_print_name_union(head, &ptr->name2) ||
986 kumaneko 2897 !ccs_print_condition(head, cond)) {
987     head->read_bit = bit;
988     head->read_avail = pos;
989     return false;
990     }
991 kumaneko 2863 }
992     head->read_bit = 0;
993     return true;
994     }
995    
996     /**
997 kumaneko 2871 * ccs_print_path_number_acl - Print an ioctl/chmod/chown/chgrp ACL entry.
998 kumaneko 2863 *
999     * @head: Pointer to "struct ccs_io_buffer".
1000 kumaneko 2915 * @ptr: Pointer to "struct ccs_path_number_acl".
1001 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1002     *
1003     * Returns true on success, false otherwise.
1004     */
1005 kumaneko 2871 static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1006 kumaneko 2915 struct ccs_path_number_acl *ptr,
1007 kumaneko 2871 const struct ccs_condition *cond)
1008 kumaneko 2863 {
1009 kumaneko 2871 int pos;
1010     u8 bit;
1011     const u8 perm = ptr->perm;
1012 kumaneko 2916 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1013     bit++) {
1014 kumaneko 2871 if (!(perm & (1 << bit)))
1015     continue;
1016     pos = head->read_avail;
1017 kumaneko 2916 if (!ccs_io_printf(head, "allow_%s",
1018     ccs_path_number2keyword(bit)) ||
1019 kumaneko 2888 !ccs_print_name_union(head, &ptr->name) ||
1020     !ccs_print_number_union(head, &ptr->number) ||
1021 kumaneko 2897 !ccs_print_condition(head, cond)) {
1022     head->read_bit = bit;
1023     head->read_avail = pos;
1024     return false;
1025     }
1026 kumaneko 2871 }
1027     head->read_bit = 0;
1028 kumaneko 2863 return true;
1029     }
1030    
1031     /**
1032     * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1033     *
1034     * @head: Pointer to "struct ccs_io_buffer".
1035 kumaneko 2915 * @ptr: Pointer to "struct ccs_env_acl".
1036 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1037     *
1038     * Returns true on success, false otherwise.
1039     */
1040     static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1041 kumaneko 2915 struct ccs_env_acl *ptr,
1042 kumaneko 2863 const struct ccs_condition *cond)
1043     {
1044 kumaneko 2897 const int pos = head->read_avail;
1045     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1046     !ccs_print_condition(head, cond)) {
1047     head->read_avail = pos;
1048     return false;
1049     }
1050 kumaneko 2863 return true;
1051     }
1052    
1053     /**
1054     * ccs_print_capability_acl - Print a capability ACL entry.
1055     *
1056     * @head: Pointer to "struct ccs_io_buffer".
1057 kumaneko 2915 * @ptr: Pointer to "struct ccs_capability_acl".
1058 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1059     *
1060     * Returns true on success, false otherwise.
1061     */
1062     static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1063 kumaneko 2915 struct ccs_capability_acl *ptr,
1064 kumaneko 2863 const struct ccs_condition *cond)
1065     {
1066 kumaneko 2897 const int pos = head->read_avail;
1067 kumaneko 2892 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1068 kumaneko 2897 ccs_cap2keyword(ptr->operation)) ||
1069     !ccs_print_condition(head, cond)) {
1070     head->read_avail = pos;
1071     return false;
1072     }
1073 kumaneko 2863 return true;
1074     }
1075    
1076     /**
1077     * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.
1078     *
1079     * @head: Pointer to "struct ccs_io_buffer".
1080 kumaneko 2915 * @ptr: Pointer to "struct ccs_ip_network_acl".
1081 kumaneko 2863 *
1082     * Returns true on success, false otherwise.
1083     */
1084     static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,
1085 kumaneko 2915 struct ccs_ip_network_acl *ptr)
1086 kumaneko 2863 {
1087     const u32 min_address = ptr->address.ipv4.min;
1088     const u32 max_address = ptr->address.ipv4.max;
1089     if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1090     return false;
1091     if (min_address != max_address
1092     && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1093     return false;
1094     return true;
1095     }
1096    
1097     /**
1098     * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.
1099     *
1100     * @head: Pointer to "struct ccs_io_buffer".
1101 kumaneko 2915 * @ptr: Pointer to "struct ccs_ip_network_acl".
1102 kumaneko 2863 *
1103     * Returns true on success, false otherwise.
1104     */
1105     static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,
1106 kumaneko 2915 struct ccs_ip_network_acl *ptr)
1107 kumaneko 2863 {
1108     char buf[64];
1109     const struct in6_addr *min_address = ptr->address.ipv6.min;
1110     const struct in6_addr *max_address = ptr->address.ipv6.max;
1111     ccs_print_ipv6(buf, sizeof(buf), min_address);
1112     if (!ccs_io_printf(head, "%s", buf))
1113     return false;
1114     if (min_address != max_address) {
1115     ccs_print_ipv6(buf, sizeof(buf), max_address);
1116     if (!ccs_io_printf(head, "-%s", buf))
1117     return false;
1118     }
1119     return true;
1120     }
1121    
1122     /**
1123     * ccs_print_network_acl - Print a network ACL entry.
1124     *
1125     * @head: Pointer to "struct ccs_io_buffer".
1126 kumaneko 2915 * @ptr: Pointer to "struct ccs_ip_network_acl".
1127 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1128     *
1129     * Returns true on success, false otherwise.
1130     */
1131     static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1132 kumaneko 2915 struct ccs_ip_network_acl *ptr,
1133 kumaneko 2863 const struct ccs_condition *cond)
1134     {
1135 kumaneko 2916 int pos;
1136     u8 bit;
1137     const u16 perm = ptr->perm;
1138     for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1139     if (!(perm & (1 << bit)))
1140     continue;
1141     pos = head->read_avail;
1142     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",
1143     ccs_net2keyword(bit)))
1144 kumaneko 2863 goto out;
1145 kumaneko 2916 switch (ptr->address_type) {
1146     case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1147     if (!ccs_io_printf(head, "@%s", ptr->address.group->
1148     group_name->name))
1149     goto out;
1150     break;
1151     case CCS_IP_ADDRESS_TYPE_IPv4:
1152     if (!ccs_print_ipv4_entry(head, ptr))
1153     goto out;
1154     break;
1155     case CCS_IP_ADDRESS_TYPE_IPv6:
1156     if (!ccs_print_ipv6_entry(head, ptr))
1157     goto out;
1158     break;
1159     }
1160     if (!ccs_print_number_union(head, &ptr->port) ||
1161     !ccs_print_condition(head, cond))
1162 kumaneko 2863 goto out;
1163     }
1164 kumaneko 2916 head->read_bit = 0;
1165 kumaneko 2863 return true;
1166     out:
1167 kumaneko 2916 head->read_bit = bit;
1168 kumaneko 2863 head->read_avail = pos;
1169     return false;
1170     }
1171    
1172     /**
1173     * ccs_print_signal_acl - Print a signal ACL entry.
1174     *
1175     * @head: Pointer to "struct ccs_io_buffer".
1176 kumaneko 2915 * @ptr: Pointer to "struct signale_acl".
1177 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1178     *
1179     * Returns true on success, false otherwise.
1180     */
1181     static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1182 kumaneko 2915 struct ccs_signal_acl *ptr,
1183 kumaneko 2863 const struct ccs_condition *cond)
1184     {
1185 kumaneko 2897 const int pos = head->read_avail;
1186 kumaneko 2892 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1187 kumaneko 2897 ptr->sig, ptr->domainname->name) ||
1188     !ccs_print_condition(head, cond)) {
1189     head->read_avail = pos;
1190     return false;
1191     }
1192 kumaneko 2863 return true;
1193     }
1194    
1195     /**
1196     * ccs_print_execute_handler_record - Print an execute handler ACL entry.
1197     *
1198     * @head: Pointer to "struct ccs_io_buffer".
1199     * @keyword: Name of the keyword.
1200     * @ptr: Pointer to "struct ccs_execute_handler_record".
1201     *
1202     * Returns true on success, false otherwise.
1203     */
1204     static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,
1205     const char *keyword,
1206     struct ccs_execute_handler_record *
1207     ptr)
1208     {
1209     return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1210     }
1211    
1212     /**
1213     * ccs_print_mount_acl - Print a mount ACL entry.
1214     *
1215     * @head: Pointer to "struct ccs_io_buffer".
1216 kumaneko 2915 * @ptr: Pointer to "struct ccs_mount_acl".
1217 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1218     *
1219     * Returns true on success, false otherwise.
1220     */
1221     static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1222 kumaneko 2915 struct ccs_mount_acl *ptr,
1223 kumaneko 2863 const struct ccs_condition *cond)
1224     {
1225 kumaneko 2897 const int pos = head->read_avail;
1226     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1227     !ccs_print_name_union(head, &ptr->dev_name) ||
1228     !ccs_print_name_union(head, &ptr->dir_name) ||
1229     !ccs_print_name_union(head, &ptr->fs_type) ||
1230     !ccs_print_number_union(head, &ptr->flags) ||
1231     !ccs_print_condition(head, cond)) {
1232     head->read_avail = pos;
1233     return false;
1234     }
1235 kumaneko 2863 return true;
1236     }
1237    
1238     /**
1239     * ccs_print_umount_acl - Print a mount ACL entry.
1240     *
1241     * @head: Pointer to "struct ccs_io_buffer".
1242 kumaneko 2915 * @ptr: Pointer to "struct ccs_umount_acl".
1243 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1244     *
1245     * Returns true on success, false otherwise.
1246     */
1247     static bool ccs_print_umount_acl(struct ccs_io_buffer *head,
1248 kumaneko 2915 struct ccs_umount_acl *ptr,
1249 kumaneko 2863 const struct ccs_condition *cond)
1250     {
1251 kumaneko 2897 const int pos = head->read_avail;
1252     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_UNMOUNT) ||
1253     !ccs_print_name_union(head, &ptr->dir) ||
1254     !ccs_print_condition(head, cond)) {
1255     head->read_avail = pos;
1256     return false;
1257     }
1258 kumaneko 2863 return true;
1259     }
1260    
1261     /**
1262     * ccs_print_chroot_acl - Print a chroot ACL entry.
1263     *
1264     * @head: Pointer to "struct ccs_io_buffer".
1265 kumaneko 2915 * @ptr: Pointer to "struct ccs_chroot_acl".
1266 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1267     *
1268     * Returns true on success, false otherwise.
1269     */
1270     static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,
1271 kumaneko 2915 struct ccs_chroot_acl *ptr,
1272 kumaneko 2863 const struct ccs_condition *cond)
1273     {
1274 kumaneko 2897 const int pos = head->read_avail;
1275     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CHROOT) ||
1276     !ccs_print_name_union(head, &ptr->dir) ||
1277     !ccs_print_condition(head, cond)) {
1278     head->read_avail = pos;
1279     return false;
1280     }
1281 kumaneko 2863 return true;
1282     }
1283    
1284     /**
1285     * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.
1286     *
1287     * @head: Pointer to "struct ccs_io_buffer".
1288 kumaneko 2915 * @ptr: Pointer to "struct ccs_pivot_root_acl".
1289 kumaneko 2863 * @cond: Pointer to "struct ccs_condition". May be NULL.
1290     *
1291     * Returns true on success, false otherwise.
1292     */
1293     static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,
1294 kumaneko 2915 struct ccs_pivot_root_acl *ptr,
1295 kumaneko 2863 const struct ccs_condition *cond)
1296     {
1297 kumaneko 2897 const int pos = head->read_avail;
1298     if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_PIVOT_ROOT) ||
1299     !ccs_print_name_union(head, &ptr->new_root) ||
1300     !ccs_print_name_union(head, &ptr->old_root) ||
1301     !ccs_print_condition(head, cond)) {
1302     head->read_avail = pos;
1303     return false;
1304     }
1305 kumaneko 2863 return true;
1306     }
1307    
1308     /**
1309     * ccs_print_entry - Print an ACL entry.
1310     *
1311     * @head: Pointer to "struct ccs_io_buffer".
1312     * @ptr: Pointer to an ACL entry.
1313     *
1314     * Returns true on success, false otherwise.
1315     */
1316     static bool ccs_print_entry(struct ccs_io_buffer *head,
1317     struct ccs_acl_info *ptr)
1318     {
1319     const struct ccs_condition *cond = ptr->cond;
1320 kumaneko 2900 const u8 acl_type = ptr->type;
1321     if (ptr->is_deleted)
1322 kumaneko 2863 return true;
1323 kumaneko 2915 if (acl_type == CCS_TYPE_PATH_ACL) {
1324     struct ccs_path_acl *acl
1325     = container_of(ptr, struct ccs_path_acl,
1326 kumaneko 2863 head);
1327 kumaneko 2915 return ccs_print_path_acl(head, acl, cond);
1328 kumaneko 2863 }
1329 kumaneko 2892 if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {
1330 kumaneko 2863 struct ccs_execute_handler_record *acl
1331     = container_of(ptr, struct ccs_execute_handler_record,
1332     head);
1333 kumaneko 2892 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;
1334 kumaneko 2863 return ccs_print_execute_handler_record(head, keyword, acl);
1335     }
1336 kumaneko 2892 if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1337 kumaneko 2863 struct ccs_execute_handler_record *acl
1338     = container_of(ptr, struct ccs_execute_handler_record,
1339     head);
1340 kumaneko 2892 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1341 kumaneko 2863 return ccs_print_execute_handler_record(head, keyword, acl);
1342     }
1343     if (head->read_execute_only)
1344     return true;
1345 kumaneko 2915 if (acl_type == CCS_TYPE_PATH_NUMBER_NUMBER_ACL) {
1346     struct ccs_path_number_number_acl *acl
1347     = container_of(ptr, struct ccs_path_number_number_acl, head);
1348     return ccs_print_path_number_number_acl(head, acl, cond);
1349 kumaneko 2863 }
1350 kumaneko 2915 if (acl_type == CCS_TYPE_PATH_PATH_ACL) {
1351     struct ccs_path_path_acl *acl
1352     = container_of(ptr, struct ccs_path_path_acl,
1353 kumaneko 2863 head);
1354 kumaneko 2915 return ccs_print_path_path_acl(head, acl, cond);
1355 kumaneko 2863 }
1356 kumaneko 2892 if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1357 kumaneko 2915 struct ccs_path_number_acl *acl
1358     = container_of(ptr, struct ccs_path_number_acl,
1359 kumaneko 2871 head);
1360     return ccs_print_path_number_acl(head, acl, cond);
1361 kumaneko 2863 }
1362 kumaneko 2892 if (acl_type == CCS_TYPE_ENV_ACL) {
1363 kumaneko 2915 struct ccs_env_acl *acl
1364     = container_of(ptr, struct ccs_env_acl, head);
1365 kumaneko 2863 return ccs_print_env_acl(head, acl, cond);
1366     }
1367 kumaneko 2892 if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1368 kumaneko 2915 struct ccs_capability_acl *acl
1369     = container_of(ptr, struct ccs_capability_acl,
1370 kumaneko 2863 head);
1371     return ccs_print_capability_acl(head, acl, cond);
1372     }
1373 kumaneko 2892 if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1374 kumaneko 2915 struct ccs_ip_network_acl *acl
1375     = container_of(ptr, struct ccs_ip_network_acl,
1376 kumaneko 2863 head);
1377     return ccs_print_network_acl(head, acl, cond);
1378     }
1379 kumaneko 2892 if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1380 kumaneko 2915 struct ccs_signal_acl *acl
1381     = container_of(ptr, struct ccs_signal_acl, head);
1382 kumaneko 2863 return ccs_print_signal_acl(head, acl, cond);
1383     }
1384 kumaneko 2892 if (acl_type == CCS_TYPE_MOUNT_ACL) {
1385 kumaneko 2915 struct ccs_mount_acl *acl
1386     = container_of(ptr, struct ccs_mount_acl, head);
1387 kumaneko 2863 return ccs_print_mount_acl(head, acl, cond);
1388     }
1389 kumaneko 2892 if (acl_type == CCS_TYPE_UMOUNT_ACL) {
1390 kumaneko 2915 struct ccs_umount_acl *acl
1391     = container_of(ptr, struct ccs_umount_acl, head);
1392 kumaneko 2863 return ccs_print_umount_acl(head, acl, cond);
1393     }
1394 kumaneko 2892 if (acl_type == CCS_TYPE_CHROOT_ACL) {
1395 kumaneko 2915 struct ccs_chroot_acl *acl
1396     = container_of(ptr, struct ccs_chroot_acl, head);
1397 kumaneko 2863 return ccs_print_chroot_acl(head, acl, cond);
1398     }
1399 kumaneko 2892 if (acl_type == CCS_TYPE_PIVOT_ROOT_ACL) {
1400 kumaneko 2915 struct ccs_pivot_root_acl *acl
1401     = container_of(ptr, struct ccs_pivot_root_acl,
1402 kumaneko 2863 head);
1403     return ccs_print_pivot_root_acl(head, acl, cond);
1404     }
1405     BUG(); /* This must not happen. */
1406     return false;
1407     }
1408    
1409     /**
1410     * ccs_read_domain_policy - Read domain policy.
1411     *
1412     * @head: Pointer to "struct ccs_io_buffer".
1413     *
1414     * Returns 0.
1415     *
1416     * Caller holds ccs_read_lock().
1417     */
1418     static int ccs_read_domain_policy(struct ccs_io_buffer *head)
1419     {
1420     struct list_head *dpos;
1421     struct list_head *apos;
1422     ccs_check_read_lock();
1423     if (head->read_eof)
1424     return 0;
1425     if (head->read_step == 0)
1426     head->read_step = 1;
1427     list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
1428     struct ccs_domain_info *domain;
1429     const char *quota_exceeded = "";
1430     const char *transition_failed = "";
1431     const char *ignore_global_allow_read = "";
1432     const char *ignore_global_allow_env = "";
1433     domain = list_entry(dpos, struct ccs_domain_info, list);
1434     if (head->read_step != 1)
1435     goto acl_loop;
1436     if (domain->is_deleted && !head->read_single_domain)
1437     continue;
1438     /* Print domainname and flags. */
1439     if (domain->quota_warned)
1440     quota_exceeded = "quota_exceeded\n";
1441     if (domain->domain_transition_failed)
1442     transition_failed = "transition_failed\n";
1443     if (domain->ignore_global_allow_read)
1444     ignore_global_allow_read
1445 kumaneko 2892 = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
1446 kumaneko 2863 if (domain->ignore_global_allow_env)
1447     ignore_global_allow_env
1448 kumaneko 2892 = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
1449     if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"
1450 kumaneko 2863 "%s%s%s%s\n", domain->domainname->name,
1451     domain->profile, quota_exceeded,
1452     transition_failed,
1453     ignore_global_allow_read,
1454     ignore_global_allow_env))
1455     return 0;
1456     head->read_step = 2;
1457     acl_loop:
1458     if (head->read_step == 3)
1459     goto tail_mark;
1460     /* Print ACL entries in the domain. */
1461     list_for_each_cookie(apos, head->read_var2,
1462     &domain->acl_info_list) {
1463     struct ccs_acl_info *ptr
1464     = list_entry(apos, struct ccs_acl_info, list);
1465     if (!ccs_print_entry(head, ptr))
1466     return 0;
1467     }
1468     head->read_step = 3;
1469     tail_mark:
1470     if (!ccs_io_printf(head, "\n"))
1471     return 0;
1472     head->read_step = 1;
1473     if (head->read_single_domain)
1474     break;
1475     }
1476     head->read_eof = true;
1477     return 0;
1478     }
1479    
1480     /**
1481     * ccs_write_domain_profile - Assign profile for specified domain.
1482     *
1483     * @head: Pointer to "struct ccs_io_buffer".
1484     *
1485     * Returns 0 on success, -EINVAL otherwise.
1486     *
1487     * This is equivalent to doing
1488     *
1489     * ( echo "select " $domainname; echo "use_profile " $profile ) |
1490     * /usr/lib/ccs/loadpolicy -d
1491     *
1492     * Caller holds ccs_read_lock().
1493     */
1494     static int ccs_write_domain_profile(struct ccs_io_buffer *head)
1495     {
1496     char *data = head->write_buf;
1497     char *cp = strchr(data, ' ');
1498     struct ccs_domain_info *domain;
1499     unsigned int profile;
1500     ccs_check_read_lock();
1501     if (!cp)
1502     return -EINVAL;
1503     *cp = '\0';
1504     profile = simple_strtoul(data, NULL, 10);
1505 kumaneko 2892 if (profile >= CCS_MAX_PROFILES)
1506 kumaneko 2863 return -EINVAL;
1507     domain = ccs_find_domain(cp + 1);
1508     if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))
1509     domain->profile = (u8) profile;
1510     return 0;
1511     }
1512    
1513     /**
1514     * ccs_read_domain_profile - Read only domainname and profile.
1515     *
1516     * @head: Pointer to "struct ccs_io_buffer".
1517     *
1518     * Returns list of profile number and domainname pairs.
1519     *
1520     * This is equivalent to doing
1521     *
1522     * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1523     * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1524     * domainname = $0; } else if ( $1 == "use_profile" ) {
1525     * print $2 " " domainname; domainname = ""; } } ; '
1526     *
1527     * Caller holds ccs_read_lock().
1528     */
1529     static int ccs_read_domain_profile(struct ccs_io_buffer *head)
1530     {
1531     struct list_head *pos;
1532     ccs_check_read_lock();
1533     if (head->read_eof)
1534     return 0;
1535     list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1536     struct ccs_domain_info *domain;
1537     domain = list_entry(pos, struct ccs_domain_info, list);
1538     if (domain->is_deleted)
1539     continue;
1540     if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1541     domain->domainname->name))
1542     return 0;
1543     }
1544     head->read_eof = true;
1545     return 0;
1546     }
1547    
1548     /**
1549     * ccs_write_pid: Specify PID to obtain domainname.
1550     *
1551     * @head: Pointer to "struct ccs_io_buffer".
1552     *
1553     * Returns 0.
1554     */
1555     static int ccs_write_pid(struct ccs_io_buffer *head)
1556     {
1557     head->read_eof = false;
1558     return 0;
1559     }
1560    
1561     /**
1562     * ccs_read_pid - Read information of a process.
1563     *
1564     * @head: Pointer to "struct ccs_io_buffer".
1565     *
1566     * Returns the domainname which the specified PID is in or
1567     * process information of the specified PID on success,
1568     * empty string otherwise.
1569     *
1570     * Caller holds ccs_read_lock().
1571     */
1572     static int ccs_read_pid(struct ccs_io_buffer *head)
1573     {
1574     char *buf = head->write_buf;
1575     bool task_info = false;
1576     unsigned int pid;
1577     struct task_struct *p;
1578     struct ccs_domain_info *domain = NULL;
1579     u32 ccs_flags = 0;
1580     ccs_check_read_lock();
1581     /* Accessing write_buf is safe because head->io_sem is held. */
1582     if (!buf)
1583     goto done; /* Do nothing if open(O_RDONLY). */
1584     if (head->read_avail || head->read_eof)
1585     goto done;
1586     head->read_eof = true;
1587     if (ccs_str_starts(&buf, "info "))
1588     task_info = true;
1589     pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1590     /***** CRITICAL SECTION START *****/
1591     read_lock(&tasklist_lock);
1592     p = find_task_by_pid(pid);
1593     if (p) {
1594     domain = ccs_task_domain(p);
1595     ccs_flags = p->ccs_flags;
1596     }
1597     read_unlock(&tasklist_lock);
1598     /***** CRITICAL SECTION END *****/
1599     if (!domain)
1600     goto done;
1601     if (!task_info)
1602     ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1603     domain->domainname->name);
1604     else
1605     ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1606     "state[0]=%u state[1]=%u state[2]=%u", pid,
1607     ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?
1608     "yes" : "no",
1609     ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?
1610     "yes" : "no",
1611     (u8) (ccs_flags >> 24),
1612     (u8) (ccs_flags >> 16),
1613     (u8) (ccs_flags >> 8));
1614     done:
1615     return 0;
1616     }
1617    
1618     /**
1619     * ccs_write_exception_policy - Write exception policy.
1620     *
1621     * @head: Pointer to "struct ccs_io_buffer".
1622     *
1623     * Returns 0 on success, negative value otherwise.
1624     */
1625     static int ccs_write_exception_policy(struct ccs_io_buffer *head)
1626     {
1627     char *data = head->write_buf;
1628 kumaneko 2892 bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1629     if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))
1630 kumaneko 2863 return ccs_write_domain_keeper_policy(data, false, is_delete);
1631 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))
1632 kumaneko 2863 return ccs_write_domain_keeper_policy(data, true, is_delete);
1633 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))
1634 kumaneko 2863 return ccs_write_domain_initializer_policy(data, false,
1635     is_delete);
1636 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))
1637 kumaneko 2863 return ccs_write_domain_initializer_policy(data, true,
1638     is_delete);
1639 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))
1640 kumaneko 2863 return ccs_write_aggregator_policy(data, is_delete);
1641 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))
1642 kumaneko 2863 return ccs_write_globally_readable_policy(data, is_delete);
1643 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))
1644 kumaneko 2863 return ccs_write_globally_usable_env_policy(data, is_delete);
1645 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))
1646 kumaneko 2863 return ccs_write_pattern_policy(data, is_delete);
1647 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))
1648 kumaneko 2863 return ccs_write_path_group_policy(data, is_delete);
1649 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))
1650 kumaneko 2863 return ccs_write_number_group_policy(data, is_delete);
1651 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))
1652 kumaneko 2863 return ccs_write_no_rewrite_policy(data, is_delete);
1653 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))
1654 kumaneko 2863 return ccs_write_address_group_policy(data, is_delete);
1655 kumaneko 2892 if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))
1656 kumaneko 2863 return ccs_write_reserved_port_policy(data, is_delete);
1657     return -EINVAL;
1658     }
1659    
1660     /**
1661     * ccs_read_exception_policy - Read exception policy.
1662     *
1663     * @head: Pointer to "struct ccs_io_buffer".
1664     *
1665     * Returns 0 on success, -EINVAL otherwise.
1666     *
1667     * Caller holds ccs_read_lock().
1668     */
1669     static int ccs_read_exception_policy(struct ccs_io_buffer *head)
1670     {
1671     ccs_check_read_lock();
1672     if (!head->read_eof) {
1673     switch (head->read_step) {
1674     case 0:
1675     head->read_var2 = NULL;
1676     head->read_step = 1;
1677     case 1:
1678     if (!ccs_read_domain_keeper_policy(head))
1679     break;
1680     head->read_var2 = NULL;
1681     head->read_step = 2;
1682     case 2:
1683     if (!ccs_read_globally_readable_policy(head))
1684     break;
1685     head->read_var2 = NULL;
1686     head->read_step = 3;
1687     case 3:
1688     if (!ccs_read_globally_usable_env_policy(head))
1689     break;
1690     head->read_var2 = NULL;
1691     head->read_step = 4;
1692     case 4:
1693     if (!ccs_read_domain_initializer_policy(head))
1694     break;
1695     head->read_var2 = NULL;
1696     head->read_step = 6;
1697     case 6:
1698     if (!ccs_read_aggregator_policy(head))
1699     break;
1700     head->read_var2 = NULL;
1701     head->read_step = 7;
1702     case 7:
1703     if (!ccs_read_file_pattern(head))
1704     break;
1705     head->read_var2 = NULL;
1706     head->read_step = 8;
1707     case 8:
1708     if (!ccs_read_no_rewrite_policy(head))
1709     break;
1710     head->read_var2 = NULL;
1711     head->read_step = 9;
1712     case 9:
1713     if (!ccs_read_path_group_policy(head))
1714     break;
1715     head->read_var1 = NULL;
1716     head->read_var2 = NULL;
1717     head->read_step = 10;
1718     case 10:
1719     if (!ccs_read_number_group_policy(head))
1720     break;
1721     head->read_var1 = NULL;
1722     head->read_var2 = NULL;
1723     head->read_step = 11;
1724     case 11:
1725     if (!ccs_read_address_group_policy(head))
1726     break;
1727     head->read_var2 = NULL;
1728     head->read_step = 12;
1729     case 12:
1730     if (!ccs_read_reserved_port_policy(head))
1731     break;
1732     head->read_eof = true;
1733     break;
1734     default:
1735     return -EINVAL;
1736     }
1737     }
1738     return 0;
1739     }
1740    
1741 kumaneko 2897 /**
1742     * ccs_get_argv0 - Get argv[0].
1743     *
1744     * @ee: Pointer to "struct ccs_execve_entry".
1745     *
1746     * Returns true on success, false otherwise.
1747     */
1748     static bool ccs_get_argv0(struct ccs_execve_entry *ee)
1749     {
1750     struct linux_binprm *bprm = ee->bprm;
1751     char *arg_ptr = ee->tmp;
1752     int arg_len = 0;
1753     unsigned long pos = bprm->p;
1754     int offset = pos % PAGE_SIZE;
1755     bool done = false;
1756     if (!bprm->argc)
1757     goto out;
1758     while (1) {
1759     if (!ccs_dump_page(bprm, pos, &ee->dump))
1760     goto out;
1761     pos += PAGE_SIZE - offset;
1762     /* Read. */
1763     while (offset < PAGE_SIZE) {
1764     const char *kaddr = ee->dump.data;
1765     const unsigned char c = kaddr[offset++];
1766     if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {
1767     if (c == '\\') {
1768     arg_ptr[arg_len++] = '\\';
1769     arg_ptr[arg_len++] = '\\';
1770     } else if (c == '/') {
1771     arg_len = 0;
1772     } else if (c > ' ' && c < 127) {
1773     arg_ptr[arg_len++] = c;
1774     } else {
1775     arg_ptr[arg_len++] = '\\';
1776     arg_ptr[arg_len++] = (c >> 6) + '0';
1777     arg_ptr[arg_len++]
1778     = ((c >> 3) & 7) + '0';
1779     arg_ptr[arg_len++] = (c & 7) + '0';
1780     }
1781     } else {
1782     arg_ptr[arg_len] = '\0';
1783     done = true;
1784     break;
1785     }
1786     }
1787     offset = 0;
1788     if (done)
1789     break;
1790     }
1791     return true;
1792     out:
1793     return false;
1794     }
1795    
1796     static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry
1797     *ee)
1798     {
1799     struct ccs_condition *cond;
1800     char *buf;
1801     int len = 256;
1802     char *realpath = NULL;
1803     char *argv0 = NULL;
1804     if (ccs_check_flags(NULL, CCS_AUTOLEARN_EXEC_REALPATH)) {
1805     struct file *file = ee->bprm->file;
1806 kumaneko 2911 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1807     struct path path = { file->f_vfsmnt, file->f_dentry };
1808     realpath = ccs_realpath_from_path(&path);
1809     #else
1810     realpath = ccs_realpath_from_path(&file->f_path);
1811     #endif
1812 kumaneko 2897 if (realpath)
1813     len += strlen(realpath) + 17;
1814     }
1815     if (ccs_check_flags(NULL, CCS_AUTOLEARN_EXEC_REALPATH)) {
1816     if (ccs_get_argv0(ee)) {
1817     argv0 = ee->tmp;
1818     len += strlen(argv0) + 16;
1819     }
1820     }
1821     buf = kmalloc(len, GFP_KERNEL);
1822     if (!buf)
1823     return NULL;
1824     snprintf(buf, len - 1, "if");
1825     if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
1826     const int pos = strlen(buf);
1827     snprintf(buf + pos, len - pos - 1,
1828     " task.type=execute_handler");
1829     }
1830     if (realpath) {
1831     const int pos = strlen(buf);
1832     snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",
1833     realpath);
1834     kfree(realpath);
1835     }
1836     if (argv0) {
1837     const int pos = strlen(buf);
1838     snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",
1839     argv0);
1840     }
1841     cond = ccs_get_condition(buf);
1842     kfree(buf);
1843     return cond;
1844     }
1845    
1846 kumaneko 2863 /* Wait queue for ccs_query_list. */
1847     static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
1848    
1849     /* Lock for manipulating ccs_query_list. */
1850     static DEFINE_SPINLOCK(ccs_query_list_lock);
1851    
1852     /* Structure for query. */
1853     struct ccs_query_entry {
1854     struct list_head list;
1855     char *query;
1856     int query_len;
1857     unsigned int serial;
1858     int timer;
1859     int answer;
1860     };
1861    
1862     /* The list for "struct ccs_query_entry". */
1863     static LIST_HEAD(ccs_query_list);
1864    
1865     /* Number of "struct file" referring /proc/ccs/query interface. */
1866     static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1867    
1868     /**
1869     * ccs_check_supervisor - Ask for the supervisor's decision.
1870     *
1871     * @r: Pointer to "struct ccs_request_info".
1872     * @fmt: The printf()'s format string, followed by parameters.
1873     *
1874     * Returns 0 if the supervisor decided to permit the access request which
1875     * violated the policy in enforcing mode, 1 if the supervisor decided to
1876     * retry the access request which violated the policy in enforcing mode,
1877 kumaneko 2897 * 0 if it is not in enforcing mode, -EPERM otherwise.
1878 kumaneko 2863 */
1879     int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1880     {
1881     va_list args;
1882     int error = -EPERM;
1883     int pos;
1884     int len;
1885     static unsigned int ccs_serial;
1886     struct ccs_query_entry *ccs_query_entry = NULL;
1887     bool quota_exceeded = false;
1888     char *header;
1889     if (!r->domain)
1890     r->domain = ccs_current_domain();
1891 kumaneko 2897 switch (r->mode) {
1892     char *buffer;
1893     struct ccs_condition *cond;
1894     case 1:
1895     if (!ccs_domain_quota_ok(r))
1896     return 0;
1897     va_start(args, fmt);
1898     len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1899     va_end(args);
1900     buffer = kmalloc(len, GFP_KERNEL);
1901     if (!buffer)
1902     return 0;
1903     va_start(args, fmt);
1904     vsnprintf(buffer, len - 1, fmt, args);
1905     va_end(args);
1906     ccs_normalize_line(buffer);
1907     if (r->ee && !strncmp(buffer, "allow_execute ", 14))
1908     cond = ccs_get_execute_condition(r->ee);
1909     else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
1910     char str[] = "if task.type=execute_handler";
1911     cond = ccs_get_condition(str);
1912     } else
1913     cond = NULL;
1914     ccs_write_domain_policy2(buffer, r->domain, cond, false);
1915     ccs_put_condition(cond);
1916     kfree(buffer);
1917     /* fall through */
1918     case 2:
1919     return 0;
1920     }
1921 kumaneko 2863 if (!atomic_read(&ccs_query_observers)) {
1922     int i;
1923     if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
1924     return -EPERM;
1925     for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);
1926     i++) {
1927     set_current_state(TASK_INTERRUPTIBLE);
1928     schedule_timeout(HZ / 10);
1929     }
1930     return -EPERM;
1931     }
1932     va_start(args, fmt);
1933     len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1934     va_end(args);
1935     header = ccs_init_audit_log(&len, r);
1936     if (!header)
1937     goto out;
1938     ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);
1939     if (!ccs_query_entry)
1940     goto out;
1941 kumaneko 2900 len = ccs_round2(len);
1942 kumaneko 2863 ccs_query_entry->query = kzalloc(len, GFP_KERNEL);
1943     if (!ccs_query_entry->query)
1944     goto out;
1945     INIT_LIST_HEAD(&ccs_query_entry->list);
1946     /***** CRITICAL SECTION START *****/
1947     spin_lock(&ccs_query_list_lock);
1948     if (ccs_quota_for_query && ccs_query_memory_size + len +
1949     sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
1950     quota_exceeded = true;
1951     } else {
1952     ccs_query_memory_size += len + sizeof(*ccs_query_entry);
1953     ccs_query_entry->serial = ccs_serial++;
1954     }
1955     spin_unlock(&ccs_query_list_lock);
1956     /***** CRITICAL SECTION END *****/
1957     if (quota_exceeded)
1958     goto out;
1959     pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
1960     ccs_query_entry->serial, r->retry, header);
1961     kfree(header);
1962     header = NULL;
1963     va_start(args, fmt);
1964     vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);
1965     ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
1966     va_end(args);
1967     /***** CRITICAL SECTION START *****/
1968     spin_lock(&ccs_query_list_lock);
1969     list_add_tail(&ccs_query_entry->list, &ccs_query_list);
1970     spin_unlock(&ccs_query_list_lock);
1971     /***** CRITICAL SECTION END *****/
1972     /* Give 10 seconds for supervisor's opinion. */
1973     for (ccs_query_entry->timer = 0;
1974     atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;
1975     ccs_query_entry->timer++) {
1976     wake_up(&ccs_query_wait);
1977     set_current_state(TASK_INTERRUPTIBLE);
1978     schedule_timeout(HZ / 10);
1979     if (ccs_query_entry->answer)
1980     break;
1981     }
1982     /***** CRITICAL SECTION START *****/
1983     spin_lock(&ccs_query_list_lock);
1984     list_del(&ccs_query_entry->list);
1985     ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
1986     spin_unlock(&ccs_query_list_lock);
1987     /***** CRITICAL SECTION END *****/
1988     switch (ccs_query_entry->answer) {
1989     case 3: /* Asked to retry by administrator. */
1990     error = 1;
1991     r->retry++;
1992     break;
1993     case 1:
1994     /* Granted by administrator. */
1995     error = 0;
1996     break;
1997     case 0:
1998     /* Timed out. */
1999     break;
2000     default:
2001     /* Rejected by administrator. */
2002     break;
2003     }
2004     out:
2005     if (ccs_query_entry)
2006     kfree(ccs_query_entry->query);
2007     kfree(ccs_query_entry);
2008     kfree(header);
2009     return error;
2010     }
2011    
2012     /**
2013     * ccs_poll_query - poll() for /proc/ccs/query.
2014     *
2015     * @file: Pointer to "struct file".
2016     * @wait: Pointer to "poll_table".
2017     *
2018     * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2019     *
2020     * Waits for access requests which violated policy in enforcing mode.
2021     */
2022     static int ccs_poll_query(struct file *file, poll_table *wait)
2023     {
2024     struct list_head *tmp;
2025     bool found = false;
2026     u8 i;
2027     for (i = 0; i < 2; i++) {
2028     /***** CRITICAL SECTION START *****/
2029     spin_lock(&ccs_query_list_lock);
2030     list_for_each(tmp, &ccs_query_list) {
2031     struct ccs_query_entry *ptr
2032     = list_entry(tmp, struct ccs_query_entry, list);
2033     if (ptr->answer)
2034     continue;
2035     found = true;
2036     break;
2037     }
2038     spin_unlock(&ccs_query_list_lock);
2039     /***** CRITICAL SECTION END *****/
2040     if (found)
2041     return POLLIN | POLLRDNORM;
2042     if (i)
2043     break;
2044     poll_wait(file, &ccs_query_wait, wait);
2045     }
2046     return 0;
2047     }
2048    
2049     /**
2050     * ccs_read_query - Read access requests which violated policy in enforcing mode.
2051     *
2052     * @head: Pointer to "struct ccs_io_buffer".
2053     *
2054     * Returns 0.
2055     */
2056     static int ccs_read_query(struct ccs_io_buffer *head)
2057     {
2058     struct list_head *tmp;
2059     int pos = 0;
2060     int len = 0;
2061     char *buf;
2062     if (head->read_avail)
2063     return 0;
2064     if (head->read_buf) {
2065     kfree(head->read_buf);
2066     head->read_buf = NULL;
2067     head->readbuf_size = 0;
2068     }
2069     /***** CRITICAL SECTION START *****/
2070     spin_lock(&ccs_query_list_lock);
2071     list_for_each(tmp, &ccs_query_list) {
2072     struct ccs_query_entry *ptr
2073     = list_entry(tmp, struct ccs_query_entry, list);
2074     if (ptr->answer)
2075     continue;
2076     if (pos++ != head->read_step)
2077     continue;
2078     len = ptr->query_len;
2079     break;
2080     }
2081     spin_unlock(&ccs_query_list_lock);
2082     /***** CRITICAL SECTION END *****/
2083     if (!len) {
2084     head->read_step = 0;
2085     return 0;
2086     }
2087     buf = kzalloc(len, GFP_KERNEL);
2088     if (!buf)
2089     return 0;
2090     pos = 0;
2091     /***** CRITICAL SECTION START *****/
2092     spin_lock(&ccs_query_list_lock);
2093     list_for_each(tmp, &ccs_query_list) {
2094     struct ccs_query_entry *ptr
2095     = list_entry(tmp, struct ccs_query_entry, list);
2096     if (ptr->answer)
2097     continue;
2098     if (pos++ != head->read_step)
2099     continue;
2100     /*
2101     * Some query can be skipped because ccs_query_list
2102     * can change, but I don't care.
2103     */
2104     if (len == ptr->query_len)
2105     memmove(buf, ptr->query, len);
2106     break;
2107     }
2108     spin_unlock(&ccs_query_list_lock);
2109     /***** CRITICAL SECTION END *****/
2110     if (buf[0]) {
2111     head->read_avail = len;
2112     head->readbuf_size = head->read_avail;
2113     head->read_buf = buf;
2114     head->read_step++;
2115     } else {
2116     kfree(buf);
2117     }
2118     return 0;
2119     }
2120    
2121     /**
2122     * ccs_write_answer - Write the supervisor's decision.
2123     *
2124     * @head: Pointer to "struct ccs_io_buffer".
2125     *
2126     * Returns 0 on success, -EINVAL otherwise.
2127     */
2128     static int ccs_write_answer(struct ccs_io_buffer *head)
2129     {
2130     char *data = head->write_buf;
2131     struct list_head *tmp;
2132     unsigned int serial;
2133     unsigned int answer;
2134     /***** CRITICAL SECTION START *****/
2135     spin_lock(&ccs_query_list_lock);
2136     list_for_each(tmp, &ccs_query_list) {
2137     struct ccs_query_entry *ptr
2138     = list_entry(tmp, struct ccs_query_entry, list);
2139     ptr->timer = 0;
2140     }
2141     spin_unlock(&ccs_query_list_lock);
2142     /***** CRITICAL SECTION END *****/
2143     if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2144     return -EINVAL;
2145     /***** CRITICAL SECTION START *****/
2146     spin_lock(&ccs_query_list_lock);
2147     list_for_each(tmp, &ccs_query_list) {
2148     struct ccs_query_entry *ptr
2149     = list_entry(tmp, struct ccs_query_entry, list);
2150     if (ptr->serial != serial)
2151     continue;
2152     if (!ptr->answer)
2153     ptr->answer = answer;
2154     break;
2155     }
2156     spin_unlock(&ccs_query_list_lock);
2157     /***** CRITICAL SECTION END *****/
2158     return 0;
2159     }
2160    
2161     /**
2162     * ccs_read_version: Get version.
2163     *
2164     * @head: Pointer to "struct ccs_io_buffer".
2165     *
2166     * Returns version information.
2167     */
2168     static int ccs_read_version(struct ccs_io_buffer *head)
2169     {
2170     if (!head->read_eof) {
2171     ccs_io_printf(head, "1.7.0-pre");
2172     head->read_eof = true;
2173     }
2174     return 0;
2175     }
2176    
2177     /**
2178     * ccs_read_self_domain - Get the current process's domainname.
2179     *
2180     * @head: Pointer to "struct ccs_io_buffer".
2181     *
2182     * Returns the current process's domainname.
2183     */
2184     static int ccs_read_self_domain(struct ccs_io_buffer *head)
2185     {
2186     if (!head->read_eof) {
2187     /*
2188     * ccs_current_domain()->domainname != NULL
2189     * because every process belongs to a domain and
2190     * the domain's name cannot be NULL.
2191     */
2192     ccs_io_printf(head, "%s",
2193     ccs_current_domain()->domainname->name);
2194     head->read_eof = true;
2195     }
2196     return 0;
2197     }
2198    
2199     /**
2200     * ccs_open_control - open() for /proc/ccs/ interface.
2201     *
2202     * @type: Type of interface.
2203     * @file: Pointer to "struct file".
2204     *
2205     * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
2206     */
2207     int ccs_open_control(const u8 type, struct file *file)
2208     {
2209     struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);
2210     if (!head)
2211     return -ENOMEM;
2212     mutex_init(&head->io_sem);
2213     head->type = type;
2214     switch (type) {
2215     case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2216     head->write = ccs_write_domain_policy;
2217     head->read = ccs_read_domain_policy;
2218     break;
2219     case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2220     head->write = ccs_write_exception_policy;
2221     head->read = ccs_read_exception_policy;
2222     break;
2223     #ifdef CONFIG_CCSECURITY_AUDIT
2224     case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2225     head->poll = ccs_poll_grant_log;
2226     head->read = ccs_read_grant_log;
2227     break;
2228     case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2229     head->poll = ccs_poll_reject_log;
2230     head->read = ccs_read_reject_log;
2231     break;
2232     #endif
2233     case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
2234     head->read = ccs_read_self_domain;
2235     break;
2236     case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2237     head->write = ccs_write_domain_profile;
2238     head->read = ccs_read_domain_profile;
2239     break;
2240     case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */
2241     /* Allow execute_handler to read process's status. */
2242     if (!(current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
2243     kfree(head);
2244     return -EPERM;
2245     }
2246     /* fall through */
2247     case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */
2248     head->write = ccs_write_pid;
2249     head->read = ccs_read_pid;
2250     break;
2251     case CCS_VERSION: /* /proc/ccs/version */
2252     head->read = ccs_read_version;
2253     head->readbuf_size = 128;
2254     break;
2255     case CCS_MEMINFO: /* /proc/ccs/meminfo */
2256     head->write = ccs_write_memory_quota;
2257     head->read = ccs_read_memory_counter;
2258     head->readbuf_size = 512;
2259     break;
2260     case CCS_PROFILE: /* /proc/ccs/profile */
2261     head->write = ccs_write_profile;
2262     head->read = ccs_read_profile;
2263     break;
2264     case CCS_QUERY: /* /proc/ccs/query */
2265     head->poll = ccs_poll_query;
2266     head->write = ccs_write_answer;
2267     head->read = ccs_read_query;
2268     break;
2269     case CCS_MANAGER: /* /proc/ccs/manager */
2270     head->write = ccs_write_manager_policy;
2271     head->read = ccs_read_manager_policy;
2272     break;
2273     }
2274     if (!(file->f_mode & FMODE_READ)) {
2275     /*
2276     * No need to allocate read_buf since it is not opened
2277     * for reading.
2278     */
2279     head->read = NULL;
2280     head->poll = NULL;
2281     } else if (type != CCS_QUERY &&
2282     type != CCS_GRANTLOG && type != CCS_REJECTLOG) {
2283     /*
2284     * Don't allocate buffer for reading if the file is one of
2285     * /proc/ccs/grant_log , /proc/ccs/reject_log , /proc/ccs/query.
2286     */
2287     if (!head->readbuf_size)
2288     head->readbuf_size = 4096 * 2;
2289     head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);
2290     if (!head->read_buf) {
2291     kfree(head);
2292     return -ENOMEM;
2293     }
2294     }
2295     if (!(file->f_mode & FMODE_WRITE)) {
2296     /*
2297     * No need to allocate write_buf since it is not opened
2298     * for writing.
2299     */
2300     head->write = NULL;
2301     } else if (head->write) {
2302     head->writebuf_size = 4096 * 2;
2303     head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);
2304     if (!head->write_buf) {
2305     kfree(head->read_buf);
2306     kfree(head);
2307     return -ENOMEM;
2308     }
2309     }
2310     if (type != CCS_QUERY &&
2311     type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2312     head->reader_idx = ccs_read_lock();
2313     file->private_data = head;
2314     /*
2315     * Call the handler now if the file is /proc/ccs/self_domain
2316     * so that the user can use "cat < /proc/ccs/self_domain" to
2317     * know the current process's domainname.
2318     */
2319     if (type == CCS_SELFDOMAIN)
2320     ccs_read_control(file, NULL, 0);
2321     /*
2322     * If the file is /proc/ccs/query , increment the observer counter.
2323     * The obserber counter is used by ccs_check_supervisor() to see if
2324     * there is some process monitoring /proc/ccs/query.
2325     */
2326     else if (type == CCS_QUERY)
2327     atomic_inc(&ccs_query_observers);
2328     return 0;
2329     }
2330    
2331     /**
2332     * ccs_poll_control - poll() for /proc/ccs/ interface.
2333     *
2334     * @file: Pointer to "struct file".
2335     * @wait: Pointer to "poll_table".
2336     *
2337     * Waits for read readiness.
2338     * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and
2339     * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2340     * /usr/lib/ccs/ccs-auditd.
2341     */
2342     int ccs_poll_control(struct file *file, poll_table *wait)
2343     {
2344     struct ccs_io_buffer *head = file->private_data;
2345     if (!head->poll)
2346     return -ENOSYS;
2347     return head->poll(file, wait);
2348     }
2349    
2350     /**
2351     * ccs_read_control - read() for /proc/ccs/ interface.
2352     *
2353     * @file: Pointer to "struct file".
2354     * @buffer: Poiner to buffer to write to.
2355     * @buffer_len: Size of @buffer.
2356     *
2357     * Returns bytes read on success, negative value otherwise.
2358     */
2359     int ccs_read_control(struct file *file, char __user *buffer,
2360     const int buffer_len)
2361     {
2362     int len = 0;
2363     struct ccs_io_buffer *head = file->private_data;
2364     char *cp;
2365     if (!head->read)
2366     return -ENOSYS;
2367     if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2368     return -EFAULT;
2369     if (mutex_lock_interruptible(&head->io_sem))
2370     return -EINTR;
2371     /* Call the policy handler. */
2372     len = head->read(head);
2373     if (len < 0)
2374     goto out;
2375     /* Write to buffer. */
2376     len = head->read_avail;
2377     if (len > buffer_len)
2378     len = buffer_len;
2379     if (!len)
2380     goto out;
2381     /* head->read_buf changes by some functions. */
2382     cp = head->read_buf;
2383     if (copy_to_user(buffer, cp, len)) {
2384     len = -EFAULT;
2385     goto out;
2386     }
2387     head->read_avail -= len;
2388     memmove(cp, cp + len, head->read_avail);
2389     out:
2390     mutex_unlock(&head->io_sem);
2391     return len;
2392     }
2393    
2394     /**
2395     * ccs_write_control - write() for /proc/ccs/ interface.
2396     *
2397     * @file: Pointer to "struct file".
2398     * @buffer: Pointer to buffer to read from.
2399     * @buffer_len: Size of @buffer.
2400     *
2401     * Returns @buffer_len on success, negative value otherwise.
2402     */
2403     int ccs_write_control(struct file *file, const char __user *buffer,
2404     const int buffer_len)
2405     {
2406     struct ccs_io_buffer *head = file->private_data;
2407     int error = buffer_len;
2408     int avail_len = buffer_len;
2409     char *cp0 = head->write_buf;
2410     if (!head->write)
2411     return -ENOSYS;
2412     if (!access_ok(VERIFY_READ, buffer, buffer_len))
2413     return -EFAULT;
2414     /* Don't allow updating policies by non manager programs. */
2415     if (head->write != ccs_write_pid &&
2416     head->write != ccs_write_domain_policy &&
2417     !ccs_is_policy_manager())
2418     return -EPERM;
2419     if (mutex_lock_interruptible(&head->io_sem))
2420     return -EINTR;
2421     /* Read a line and dispatch it to the policy handler. */
2422     while (avail_len > 0) {
2423     char c;
2424     if (head->write_avail >= head->writebuf_size - 1) {
2425     error = -ENOMEM;
2426     break;
2427     } else if (get_user(c, buffer)) {
2428     error = -EFAULT;
2429     break;
2430     }
2431     buffer++;
2432     avail_len--;
2433     cp0[head->write_avail++] = c;
2434     if (c != '\n')
2435     continue;
2436     cp0[head->write_avail - 1] = '\0';
2437     head->write_avail = 0;
2438     ccs_normalize_line(cp0);
2439     head->write(head);
2440     }
2441     mutex_unlock(&head->io_sem);
2442     return error;
2443     }
2444    
2445     /**
2446     * ccs_close_control - close() for /proc/ccs/ interface.
2447     *
2448     * @file: Pointer to "struct file".
2449     *
2450     * Releases memory and returns 0.
2451     */
2452     int ccs_close_control(struct file *file)
2453     {
2454     struct ccs_io_buffer *head = file->private_data;
2455     const bool is_write = head->write_buf != NULL;
2456     const u8 type = head->type;
2457     /*
2458     * If the file is /proc/ccs/query , decrement the observer counter.
2459     */
2460     if (type ==