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

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