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

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