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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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