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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4280 - (hide annotations) (download) (as text)
Fri Dec 31 02:05:48 2010 UTC (13 years, 4 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 77170 byte(s)


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