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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4784 - (hide annotations) (download) (as text)
Thu Mar 31 03:06:59 2011 UTC (13 years, 2 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 78132 byte(s)


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