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

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 2932 by kumaneko, Fri Aug 21 08:26:08 2009 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 3953 by kumaneko, Mon Sep 6 01:24:45 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-pre   2010/09/01
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 12  Line 12 
12    
13  #include "internal.h"  #include "internal.h"
14    
15  /* Lock for protecting ccs_profile->comment  */  static struct ccs_profile ccs_default_profile = {
16  static DEFINE_SPINLOCK(ccs_profile_comment_lock);  #ifdef CONFIG_CCSECURITY_AUDIT
17            .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
18  /* String table for functionality that takes 2 modes. */          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
19  static const char *ccs_mode_2[2] = {  #endif
20          "disabled", "enabled"          .preference.enforcing_penalty = 0,
21            .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
22            .preference.learning_exec_realpath = true,
23            .preference.learning_exec_argv0 = true,
24            .preference.learning_symlink_target = true,
25  };  };
26    
27  static const char *ccs_mode_4[4] = {  /* Profile version. Currently only 20100903 is defined. */
28          "disabled", "learning", "permissive", "enforcing"  static unsigned int ccs_profile_version;
 };  
29    
30  static bool ccs_mac_keywords_used[CCS_MAX_MAC_INDEX +  /* Profile table. Memory is allocated as needed. */
31                                    CCS_MAX_CAPABILITY_INDEX];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
32    
33  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +  /* String table for functionality that takes 4 modes. */
34                                      CCS_MAX_CAPABILITY_INDEX] = {  const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {
35          [CCS_MAC_EXECUTE]    = "execute",          [CCS_CONFIG_DISABLED] = "disabled",
36          [CCS_MAC_OPEN]       = "open",          [CCS_CONFIG_LEARNING] = "learning",
37          [CCS_MAC_CREATE]     = "create",          [CCS_CONFIG_PERMISSIVE] = "permissive",
38          [CCS_MAC_UNLINK]     = "unlink",          [CCS_CONFIG_ENFORCING] = "enforcing"
         [CCS_MAC_MKDIR]      = "mkdir",  
         [CCS_MAC_RMDIR]      = "rmdir",  
         [CCS_MAC_MKFIFO]     = "mkfifo",  
         [CCS_MAC_MKSOCK]     = "mksock",  
         [CCS_MAC_TRUNCATE]   = "truncate",  
         [CCS_MAC_SYMLINK]    = "symlink",  
         [CCS_MAC_REWRITE]    = "rewrite",  
         [CCS_MAC_MKBLOCK]    = "mkblock",  
         [CCS_MAC_MKCHAR]     = "mkchar",  
         [CCS_MAC_LINK]       = "link",  
         [CCS_MAC_RENAME]     = "rename",  
         [CCS_MAC_CHMOD]      = "chmod",  
         [CCS_MAC_CHOWN]      = "chown",  
         [CCS_MAC_CHGRP]      = "chgrp",  
         [CCS_MAC_IOCTL]      = "ioctl",  
         [CCS_MAC_CHROOT]     = "chroot",  
         [CCS_MAC_MOUNT]      = "mount",  
         [CCS_MAC_UMOUNT]     = "umount",  
         [CCS_MAC_PIVOT_ROOT] = "pivot_root",  
         [CCS_MAC_ENVIRON]    = "env",  
         [CCS_MAC_NETWORK]    = "network",  
         [CCS_MAC_SIGNAL]     = "signal",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  
         = "inet_tcp_create",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]  
         = "inet_tcp_listen",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]  
         = "inet_tcp_connect",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET] = "use_inet_udp",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]   = "use_inet_ip",  
         [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]      = "use_route",  
         [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]     = "use_packet",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]             = "SYS_MOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]            = "SYS_UMOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]            = "SYS_REBOOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]            = "SYS_CHROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]              = "SYS_KILL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]           = "SYS_VHANGUP",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]           = "SYS_TIME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]              = "SYS_NICE",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]       = "SYS_SETHOSTNAME",  
         [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]     = "use_kernel_module",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]           = "create_fifo",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]      = "create_block_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]       = "create_char_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]    = "create_unix_socket",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]              = "SYS_LINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]           = "SYS_SYMLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]            = "SYS_RENAME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]            = "SYS_UNLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]             = "SYS_CHMOD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]             = "SYS_CHOWN",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]             = "SYS_IOCTL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]        = "SYS_KEXEC_LOAD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]        = "SYS_PIVOT_ROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]            = "SYS_PTRACE",  
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]         = "conceal_mount"  
39  };  };
40    
41  /* Table for profile. */  /* String table for /proc/ccs/profile */
42  static struct {  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +
43          const char *keyword;                                      CCS_MAX_CAPABILITY_INDEX +
44          unsigned int current_value;                                      CCS_MAX_MAC_CATEGORY_INDEX] = {
45          const unsigned int max_value;          [CCS_MAC_FILE_EXECUTE]
46  } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {          = "file::execute",
47          [CCS_AUTOLEARN_EXEC_REALPATH] = { "AUTOLEARN_EXEC_REALPATH", 0, 1 },          [CCS_MAC_FILE_OPEN]
48          [CCS_AUTOLEARN_EXEC_ARGV0] = { "AUTOLEARN_EXEC_ARGV0", 0, 1 },          = "file::open",
49          [CCS_MAX_ACCEPT_ENTRY]          [CCS_MAC_FILE_CREATE]
50          = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },          = "file::create",
51  #ifdef CONFIG_CCSECURITY_AUDIT          [CCS_MAC_FILE_UNLINK]
52          [CCS_MAX_GRANT_LOG]          = "file::unlink",
53          = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },          [CCS_MAC_FILE_MKDIR]
54          [CCS_MAX_REJECT_LOG]          = "file::mkdir",
55          = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },          [CCS_MAC_FILE_RMDIR]
56  #endif          = "file::rmdir",
57          [CCS_VERBOSE] = { "PRINT_VIOLATION",      1, 1 },          [CCS_MAC_FILE_MKFIFO]
58          [CCS_SLEEP_PERIOD] = { "SLEEP_PERIOD", 0, 3000 }, /* in 0.1 second */          = "file::mkfifo",
59            [CCS_MAC_FILE_MKSOCK]
60            = "file::mksock",
61            [CCS_MAC_FILE_TRUNCATE]
62            = "file::truncate",
63            [CCS_MAC_FILE_SYMLINK]
64            = "file::symlink",
65            [CCS_MAC_FILE_MKBLOCK]
66            = "file::mkblock",
67            [CCS_MAC_FILE_MKCHAR]
68            = "file::mkchar",
69            [CCS_MAC_FILE_LINK]
70            = "file::link",
71            [CCS_MAC_FILE_RENAME]
72            = "file::rename",
73            [CCS_MAC_FILE_CHMOD]
74            = "file::chmod",
75            [CCS_MAC_FILE_CHOWN]
76            = "file::chown",
77            [CCS_MAC_FILE_CHGRP]
78            = "file::chgrp",
79            [CCS_MAC_FILE_IOCTL]
80            = "file::ioctl",
81            [CCS_MAC_FILE_CHROOT]
82            = "file::chroot",
83            [CCS_MAC_FILE_MOUNT]
84            = "file::mount",
85            [CCS_MAC_FILE_UMOUNT]
86            = "file::umount",
87            [CCS_MAC_FILE_PIVOT_ROOT]
88            = "file::pivot_root",
89            [CCS_MAC_ENVIRON]
90            = "misc::env",
91            [CCS_MAC_NETWORK_INET_STREAM_BIND]
92            = "network::inet_stream_bind",
93            [CCS_MAC_NETWORK_INET_STREAM_LISTEN]
94            = "network::inet_stream_listen",
95            [CCS_MAC_NETWORK_INET_STREAM_CONNECT]
96            = "network::inet_stream_connect",
97            [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]
98            = "network::inet_stream_accept",
99            [CCS_MAC_NETWORK_INET_DGRAM_BIND]
100            = "network::inet_dgram_bind",
101            [CCS_MAC_NETWORK_INET_DGRAM_SEND]
102            = "network::inet_dgram_send",
103            [CCS_MAC_NETWORK_INET_DGRAM_RECV]
104            = "network::inet_dgram_recv",
105            [CCS_MAC_NETWORK_INET_RAW_BIND]
106            = "network::inet_raw_bind",
107            [CCS_MAC_NETWORK_INET_RAW_SEND]
108            = "network::inet_raw_send",
109            [CCS_MAC_NETWORK_INET_RAW_RECV]
110            = "network::inet_raw_recv",
111            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]
112            = "network::unix_stream_bind",
113            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]
114            = "network::unix_stream_listen",
115            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]
116            = "network::unix_stream_connect",
117            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]
118            = "network::unix_stream_accept",
119            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]
120            = "network::unix_dgram_bind",
121            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]
122            = "network::unix_dgram_send",
123            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]
124            = "network::unix_dgram_recv",
125            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]
126            = "network::unix_seqpacket_bind",
127            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]
128            = "network::unix_seqpacket_listen",
129            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT]
130            = "network::unix_seqpacket_connect",
131            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]
132            = "network::unix_seqpacket_accept",
133            [CCS_MAC_SIGNAL]
134            = "ipc::signal",
135            [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
136            = "capability::use_route",
137            [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
138            = "capability::use_packet",
139            [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
140            = "capability::SYS_REBOOT",
141            [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
142            = "capability::SYS_VHANGUP",
143            [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
144            = "capability::SYS_TIME",
145            [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]
146            = "capability::SYS_NICE",
147            [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]
148            = "capability::SYS_SETHOSTNAME",
149            [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
150            = "capability::use_kernel_module",
151            [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
152            = "capability::SYS_KEXEC_LOAD",
153            [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
154            = "capability::SYS_PTRACE",
155            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
156             + CCS_MAC_CATEGORY_FILE] = "file",
157            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
158             + CCS_MAC_CATEGORY_NETWORK] = "network",
159            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
160             + CCS_MAC_CATEGORY_MISC] = "misc",
161            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
162             + CCS_MAC_CATEGORY_IPC] = "ipc",
163            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
164             + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
165  };  };
166    
167  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
# Line 125  static bool ccs_manage_by_non_root; Line 177  static bool ccs_manage_by_non_root;
177  const char *ccs_cap2keyword(const u8 operation)  const char *ccs_cap2keyword(const u8 operation)
178  {  {
179          return operation < CCS_MAX_CAPABILITY_INDEX          return operation < CCS_MAX_CAPABILITY_INDEX
180                  ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] : NULL;                  ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;
181  }  }
182    
183  /**  /**
184   * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.   * ccs_yesno - Return "yes" or "no".
185   *   *
186   * @str: Unused.   * @value: Bool value.
187     */
188    static const char *ccs_yesno(const unsigned int value)
189    {
190            return value ? "yes" : "no";
191    }
192    
193    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
194    {
195            va_list args;
196            const int pos = strlen(buffer);
197            va_start(args, fmt);
198            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
199            va_end(args);
200    }
201    
202    /**
203     * ccs_flush - Flush queued string to userspace's buffer.
204   *   *
205   * Returns 0.   * @head:   Pointer to "struct ccs_io_buffer".
206     *
207     * Returns true if all data was flushed, false otherwise.
208   */   */
209  static int __init ccs_quiet_setup(char *str)  static bool ccs_flush(struct ccs_io_buffer *head)
210  {  {
211          ccs_control_array[CCS_VERBOSE].current_value = 0;          while (head->r.w_pos) {
212          return 0;                  const char *w = head->r.w[0];
213                    int len = strlen(w);
214                    if (len) {
215                            if (len > head->read_user_buf_avail)
216                                    len = head->read_user_buf_avail;
217                            if (!len)
218                                    return false;
219                            if (copy_to_user(head->read_user_buf, w, len))
220                                    return false;
221                            head->read_user_buf_avail -= len;
222                            head->read_user_buf += len;
223                            w += len;
224                    }
225                    if (*w) {
226                            head->r.w[0] = w;
227                            return false;
228                    }
229                    /* Add '\0' for audit logs and query. */
230                    if (head->poll) {
231                            if (!head->read_user_buf_avail ||
232                                copy_to_user(head->read_user_buf, "", 1))
233                                    return false;
234                            head->read_user_buf_avail--;
235                            head->read_user_buf++;
236                    }
237                    head->r.w_pos--;
238                    for (len = 0; len < head->r.w_pos; len++)
239                            head->r.w[len] = head->r.w[len + 1];
240            }
241            head->r.avail = 0;
242            return true;
243  }  }
244    
245  __setup("CCS_QUIET", ccs_quiet_setup);  /**
246     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
247     *
248     * @head:   Pointer to "struct ccs_io_buffer".
249     * @string: String to print.
250     *
251     * Note that @string has to be kept valid until @head is kfree()d.
252     * This means that char[] allocated on stack memory cannot be passed to
253     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
254     */
255    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
256    {
257            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
258                    head->r.w[head->r.w_pos++] = string;
259                    ccs_flush(head);
260            } else
261                    printk(KERN_WARNING "Too many words in a line.\n");
262    }
263    
264  /**  /**
265   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
266   *   *
267   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
268   * @fmt:  The printf()'s format string, followed by parameters.   * @fmt:  The printf()'s format string, followed by parameters.
  *  
  * Returns true on success, false otherwise.  
  *  
  * The snprintf() will truncate, but ccs_io_printf() won't.  
269   */   */
270  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
271  {  {
272          va_list args;          va_list args;
273          int len;          int len;
274          int pos = head->read_avail;          int pos = head->r.avail;
275          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
276          if (size <= 0)          if (size <= 0)
277                  return false;                  return;
278          va_start(args, fmt);          va_start(args, fmt);
279          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
280          va_end(args);          va_end(args);
281          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
282                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
283          head->read_avail += len;                  return;
284          return true;          }
285            head->r.avail += len;
286            ccs_set_string(head, head->read_buf + pos);
287    }
288    
289    static void ccs_set_space(struct ccs_io_buffer *head)
290    {
291            ccs_set_string(head, " ");
292    }
293    
294    static bool ccs_set_lf(struct ccs_io_buffer *head)
295    {
296            ccs_set_string(head, "\n");
297            return !head->r.w_pos;
298  }  }
299    
300  /**  /**
301   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
302   *   *
303   * @profile: Profile number to create.   * @profile: Profile number to create.
304   *   *
305   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
306   */   */
307  static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_assign_profile(const unsigned int profile)
                                                           profile)  
308  {  {
309          struct ccs_profile *ptr;          struct ccs_profile *ptr;
310          struct ccs_profile *entry;          struct ccs_profile *entry;
         int i;  
311          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
312                  return NULL;                  return NULL;
313          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
314          if (ptr)          if (ptr)
315                  return ptr;                  return ptr;
316          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
317          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
318                    goto out;
319          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
320          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
321                  ptr = entry;                  ptr = entry;
322                  for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)                  ptr->default_config = CCS_CONFIG_DISABLED |
323                          ptr->value[i] = ccs_control_array[i].current_value;                          CCS_CONFIG_VERBOSE |
324                            CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
325                    memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
326                           sizeof(ptr->config));
327                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
328                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
329                  entry = NULL;                  entry = NULL;
330          }          }
331          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
332     out:
333          kfree(entry);          kfree(entry);
334          return ptr;          return ptr;
335  }  }
336    
337  /**  /**
338     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
339     */
340    static void ccs_check_profile(void)
341    {
342            struct ccs_domain_info *domain;
343            const int idx = ccs_read_lock();
344            ccs_policy_loaded = true;
345            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
346                    const u8 profile = domain->profile;
347                    if (ccs_profile_ptr[profile])
348                            continue;
349                    panic("Profile %u (used by '%s') not defined.\n",
350                          profile, domain->domainname->name);
351            }
352            ccs_read_unlock(idx);
353            if (ccs_profile_version != 20100903)
354                    panic("Profile version %u is not supported.\n",
355                          ccs_profile_version);
356            printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/09/01\n");
357            printk(KERN_INFO "Mandatory Access Control activated.\n");
358    }
359    
360    /**
361     * ccs_profile - Find a profile.
362     *
363     * @profile: Profile number to find.
364     *
365     * Returns pointer to "struct ccs_profile".
366     */
367    struct ccs_profile *ccs_profile(const u8 profile)
368    {
369            struct ccs_profile *ptr = ccs_profile_ptr[profile];
370            if (!ccs_policy_loaded)
371                    return &ccs_default_profile;
372            BUG_ON(!ptr);
373            return ptr;
374    }
375    
376    static s8 ccs_find_yesno(const char *string, const char *find)
377    {
378            const char *cp = strstr(string, find);
379            if (cp) {
380                    cp += strlen(find);
381                    if (!strncmp(cp, "=yes", 4))
382                            return 1;
383                    else if (!strncmp(cp, "=no", 3))
384                            return 0;
385            }
386            return -1;
387    }
388    
389    static void ccs_set_bool(bool *b, const char *string, const char *find)
390    {
391            switch (ccs_find_yesno(string, find)) {
392            case 1:
393                    *b = true;
394                    break;
395            case 0:
396                    *b = false;
397                    break;
398            }
399    }
400    
401    static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
402    {
403            const char *cp = strstr(string, find);
404            if (cp)
405                    sscanf(cp + strlen(find), "=%u", i);
406    }
407    
408    static void ccs_set_pref(const char *name, const char *value,
409                             struct ccs_profile *profile)
410    {
411    #ifdef CONFIG_CCSECURITY_AUDIT
412            if (!strcmp(name, "audit")) {
413                    ccs_set_uint(&profile->preference.audit_max_grant_log, value,
414                                 "max_grant_log");
415                    ccs_set_uint(&profile->preference.audit_max_reject_log, value,
416                                 "max_reject_log");
417                    return;
418            }
419    #endif
420            if (!strcmp(name, "enforcing")) {
421                    ccs_set_uint(&profile->preference.enforcing_penalty, value,
422                                 "penalty");
423                    return;
424            }
425            if (!strcmp(name, "learning")) {
426                    ccs_set_uint(&profile->preference.learning_max_entry, value,
427                                 "max_entry");
428                    ccs_set_bool(&profile->preference.learning_exec_realpath,
429                                 value, "exec.realpath");
430                    ccs_set_bool(&profile->preference.learning_exec_argv0, value,
431                                 "exec.argv0");
432                    ccs_set_bool(&profile->preference.learning_symlink_target,
433                                 value, "symlink.target");
434                    return;
435            }
436    }
437    
438    static int ccs_set_mode(char *name, const char *value,
439                            struct ccs_profile *profile)
440    {
441            u8 i;
442            u8 config;
443            if (!strcmp(name, "CONFIG")) {
444                    i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
445                            + CCS_MAX_MAC_CATEGORY_INDEX;
446                    config = profile->default_config;
447            } else if (ccs_str_starts(&name, "CONFIG::")) {
448                    config = 0;
449                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
450                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
451                            if (strcmp(name, ccs_mac_keywords[i]))
452                                    continue;
453                            config = profile->config[i];
454                            break;
455                    }
456                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
457                        + CCS_MAX_MAC_CATEGORY_INDEX)
458                            return -EINVAL;
459            } else {
460                    return -EINVAL;
461            }
462            if (strstr(value, "use_default")) {
463                    config = CCS_CONFIG_USE_DEFAULT;
464            } else {
465                    u8 mode;
466                    for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
467                            if (strstr(value, ccs_mode[mode]))
468                                    /*
469                                     * Update lower 3 bits in order to distinguish
470                                     * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
471                                     */
472                                    config = (config & ~7) | mode;
473                    if (config != CCS_CONFIG_USE_DEFAULT) {
474                            switch (ccs_find_yesno(value, "verbose")) {
475                            case 1:
476                                    config |= CCS_CONFIG_VERBOSE;
477                                    break;
478                            case 0:
479                                    config &= ~CCS_CONFIG_VERBOSE;
480                                    break;
481                            }
482    #ifdef CONFIG_CCSECURITY_AUDIT
483                            switch (ccs_find_yesno(value, "grant_log")) {
484                            case 1:
485                                    config |= CCS_CONFIG_WANT_GRANT_LOG;
486                                    break;
487                            case 0:
488                                    config &= ~CCS_CONFIG_WANT_GRANT_LOG;
489                                    break;
490                            }
491                            switch (ccs_find_yesno(value, "reject_log")) {
492                            case 1:
493                                    config |= CCS_CONFIG_WANT_REJECT_LOG;
494                                    break;
495                            case 0:
496                                    config &= ~CCS_CONFIG_WANT_REJECT_LOG;
497                                    break;
498                            }
499    #endif
500                    }
501            }
502            if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
503                + CCS_MAX_MAC_CATEGORY_INDEX)
504                    profile->config[i] = config;
505            else if (config != CCS_CONFIG_USE_DEFAULT)
506                    profile->default_config = config;
507            return 0;
508    }
509    
510    /**
511   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
512   *   *
513   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 215  static int ccs_write_profile(struct ccs_ Line 518  static int ccs_write_profile(struct ccs_
518  {  {
519          char *data = head->write_buf;          char *data = head->write_buf;
520          unsigned int i;          unsigned int i;
         unsigned int value;  
         int index = -1;  
         int mode;  
521          char *cp;          char *cp;
522          struct ccs_profile *ccs_profile;          struct ccs_profile *profile;
523            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
524                    return 0;
525          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
526          if (data != cp) {          if (*cp != '-')
527                  if (*cp != '-')                  return -EINVAL;
528                          return -EINVAL;          data = cp + 1;
529                  data = cp + 1;          profile = ccs_assign_profile(i);
530          }          if (!profile)
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
531                  return -EINVAL;                  return -EINVAL;
532          cp = strchr(data, '=');          cp = strchr(data, '=');
533          if (!cp)          if (!cp)
534                  return -EINVAL;                  return -EINVAL;
535          *cp++ = '\0';          *cp++ = '\0';
536          if (!strcmp(data, "COMMENT")) {          if (ccs_str_starts(&data, "PREFERENCE::")) {
537                  const struct ccs_path_info *new_comment = ccs_get_name(cp);                  ccs_set_pref(data, cp, profile);
                 const struct ccs_path_info *old_comment;  
                 /* Protect reader from ccs_put_name(). */  
                 spin_lock(&ccs_profile_comment_lock);  
                 old_comment = ccs_profile->comment;  
                 ccs_profile->comment = new_comment;  
                 spin_unlock(&ccs_profile_comment_lock);  
                 ccs_put_name(old_comment);  
538                  return 0;                  return 0;
539          }          }
540          if (!ccs_str_starts(&data, "MAC::"))          if (!strcmp(data, "COMMENT")) {
541                  goto not_mac;                  const struct ccs_path_info *old_comment = profile->comment;
542          if (ccs_str_starts(&data, CCS_KEYWORD_CAPABILITY))                  profile->comment = ccs_get_name(cp);
543                  for (i = 0; i < CCS_MAX_CAPABILITY_INDEX; i++) {                  ccs_put_name(old_comment);
                         if (strcmp(data,  
                                    ccs_mac_keywords[CCS_MAX_MAC_INDEX + i]))  
                                 continue;  
                         index = CCS_MAX_MAC_INDEX + i;  
                         break;  
                 }  
         else  
                 for (i = 0; i < CCS_MAX_MAC_INDEX; i++) {  
                         if (strcmp(data, ccs_mac_keywords[i]))  
                                 continue;  
                         index = i;  
                         break;  
                 }  
         if (index < 0)  
                 return -EINVAL;  
         ccs_mac_keywords_used[index] = 1;  
         ccs_profile->no_grant_log[index] = !!strstr(cp, "no_grant_log");  
         ccs_profile->no_reject_log[index] = !!strstr(cp, "no_reject_log");  
         for (mode = 0; mode < 4; mode++)  
                 if (strstr(cp, ccs_mode_4[mode]))  
                         ccs_profile->mac_mode[index] = mode;  
         return 0;  
  not_mac:  
         for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {  
                 if (strcmp(data, ccs_control_array[i].keyword))  
                         continue;  
                 if (sscanf(cp, "%u", &value) != 1) {  
                         int j;  
                         for (j = 0; j < 2; j++) {  
                                 if (strcmp(cp, ccs_mode_2[j]))  
                                         continue;  
                                 value = j;  
                                 break;  
                         }  
                         if (j == 2)  
                                 return -EINVAL;  
                 } else if (value > ccs_control_array[i].max_value) {  
                         value = ccs_control_array[i].max_value;  
                 }  
                 ccs_profile->value[i] = value;  
544                  return 0;                  return 0;
545          }          }
546          return -EINVAL;          return ccs_set_mode(data, cp, profile);
547  }  }
548    
549  static bool ccs_print_mac_mode(struct ccs_io_buffer *head, u8 index)  static void ccs_print_preference(struct ccs_io_buffer *head, const int index)
550  {  {
551          const int pos = head->read_avail;          struct ccs_profile *profile = ccs_profile_ptr[index];
552          int i;          struct ccs_preference *pref = &profile->preference;
553          const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];  #ifdef CONFIG_CCSECURITY_AUDIT
554          for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX; i++) {          ccs_io_printf(head, "%u-PREFERENCE::%s={ "
555                  if (!ccs_mac_keywords_used[i])                        "max_grant_log=%u max_reject_log=%u  }\n", index,
556                          continue;                        "audit", pref->audit_max_grant_log,
557                  if (!ccs_io_printf(head, "%u-MAC::%s%s=%s %s %s\n", index,                        pref->audit_max_reject_log);
558                                     i >= CCS_MAX_MAC_INDEX ?  #endif
559                                     CCS_KEYWORD_CAPABILITY : "",          ccs_io_printf(head, "%u-PREFERENCE::%s={ "
560                                     ccs_mac_keywords[i],                        "max_entry=%u exec.realpath=%s "
561                                     ccs_mode_4[ccs_profile->mac_mode[i]],                        "exec.argv0=%s symlink.target=%s }\n",
562                                     ccs_profile->no_grant_log[i] ?                        index, "learning",
563                                     "no_grant_log" : "",                        pref->learning_max_entry,
564                                     ccs_profile->no_reject_log[i] ?                        ccs_yesno(pref->learning_exec_realpath),
565                                     "no_reject_log" : ""))                        ccs_yesno(pref->learning_exec_argv0),
566                          goto out;                        ccs_yesno(pref->learning_symlink_target));
567          }          ccs_io_printf(head, "%u-PREFERENCE::%s={ penalty=%u }\n", index,
568          return true;                        "enforcing", pref->enforcing_penalty);
569   out:  }
570          head->read_avail = pos;  
571          return false;  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
572    {
573            ccs_io_printf(head, "={ mode=%s verbose=%s", ccs_mode[config & 3],
574                          ccs_yesno(config & CCS_CONFIG_VERBOSE));
575    #ifdef CONFIG_CCSECURITY_AUDIT
576            ccs_io_printf(head, " grant_log=%s reject_log=%s",
577                          ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
578                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
579    #endif
580            ccs_set_string(head, " }\n");
581  }  }
582    
583  /**  /**
584   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
585   *   *
586   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
587   */   */
588  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
589  {  {
590          static const int ccs_total = CCS_MAX_CONTROL_INDEX + 2;          u8 index;
591          int step;          const struct ccs_profile *profile;
592          if (head->read_eof)   next:
593                  return 0;          index = head->r.index;
594          for (step = head->read_step; step < CCS_MAX_PROFILES * ccs_total;          profile = ccs_profile_ptr[index];
595               step++) {          switch (head->r.step) {
596                  const u8 index = step / ccs_total;          case 0:
597                  u8 type = step % ccs_total;                  ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20100903");
598                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];                  head->r.step++;
599                  head->read_step = step;                  break;
600                  if (!ccs_profile)          case 1:
601                          continue;                  for ( ; head->r.index < CCS_MAX_PROFILES;
602                  if (!type) { /* Print profile' comment tag. */                        head->r.index++)
603                          bool done;                          if (ccs_profile_ptr[head->r.index])
                         spin_lock(&ccs_profile_comment_lock);  
                         done = ccs_io_printf(head, "%u-COMMENT=%s\n",  
                                              index, ccs_profile->comment ?  
                                              ccs_profile->comment->name : "");  
                         spin_unlock(&ccs_profile_comment_lock);  
                         if (!done)  
                                 break;  
                         continue;  
                 } else if (type == 1) {  
                         if (!ccs_print_mac_mode(head, index))  
604                                  break;                                  break;
605                          continue;                  if (head->r.index == CCS_MAX_PROFILES)
606                            return;
607                    head->r.step++;
608                    break;
609            case 2:
610                    {
611                            const struct ccs_path_info *comment = profile->comment;
612                            ccs_io_printf(head, "%u-COMMENT=", index);
613                            ccs_set_string(head, comment ? comment->name : "");
614                            ccs_set_lf(head);
615                            head->r.step++;
616                  }                  }
617                  type -= 2;                  break;
618            case 3:
619                  {                  {
620                          const unsigned int value = ccs_profile->value[type];                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
621                          const char *keyword = ccs_control_array[type].keyword;                          ccs_print_config(head, profile->default_config);
622                          if (ccs_control_array[type].max_value == 1) {                          head->r.bit = 0;
623                                  if (!ccs_io_printf(head, "%u-%s=%s\n", index,                          head->r.step++;
624                                                     keyword, ccs_mode_2[value]))                  }
625                                          break;                  break;
626                          } else {          case 4:
627                                  if (!ccs_io_printf(head, "%u-%s=%u\n", index,                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
628                                                     keyword, value))                                + CCS_MAX_CAPABILITY_INDEX
629                                          break;                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
630                          }                          const u8 i = head->r.bit;
631                            const u8 config = profile->config[i];
632                            if (config == CCS_CONFIG_USE_DEFAULT)
633                                    continue;
634                            ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
635                                          ccs_mac_keywords[i]);
636                            ccs_print_config(head, config);
637                            head->r.bit++;
638                            break;
639                  }                  }
640                    if (head->r.bit == CCS_MAX_MAC_INDEX
641                        + CCS_MAX_CAPABILITY_INDEX
642                        + CCS_MAX_MAC_CATEGORY_INDEX) {
643                            ccs_print_preference(head, index);
644                            head->r.index++;
645                            head->r.step = 1;
646                    }
647                    break;
648          }          }
649          if (step == CCS_MAX_PROFILES * ccs_total)          if (ccs_flush(head))
650                  head->read_eof = true;                  goto next;
         return 0;  
651  }  }
652    
653  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager(const struct ccs_acl_head *a,
654  LIST_HEAD(ccs_policy_manager_list);                               const struct ccs_acl_head *b)
655    {
656            return container_of(a, struct ccs_manager, head)->manager
657                    == container_of(b, struct ccs_manager, head)->manager;
658    }
659    
660  /**  /**
661   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 387  LIST_HEAD(ccs_policy_manager_list); Line 667  LIST_HEAD(ccs_policy_manager_list);
667   */   */
668  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)
669  {  {
670          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
671          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
672          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
673                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
674                          return -EINVAL;                          return -EINVAL;
675                  e.is_domain = true;                  e.is_domain = true;
676          } else {          } else {
677                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
678                          return -EINVAL;                          return -EINVAL;
679          }          }
680          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
681          if (!e.manager)          if (!e.manager)
682                  return -ENOMEM;                  return error;
683          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
684                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
685          mutex_lock(&ccs_policy_lock);                                    ccs_same_manager);
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (ptr->manager != e.manager)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {  
                 list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
686          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
687          return error;          return error;
688  }  }
689    
690  /**  /**
691   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
692   *   *
693   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
694   *   *
695   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
696   */   */
697  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
698  {  {
699          char *data = head->write_buf;          char *data = head->write_buf;
700          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
# Line 442  static int ccs_write_manager_policy(stru Line 706  static int ccs_write_manager_policy(stru
706  }  }
707    
708  /**  /**
709   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
710   *   *
711   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
712   *   *
  * Returns 0.  
  *  
713   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
714   */   */
715  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
716  {  {
717          struct list_head *pos;          if (head->r.eof)
718          ccs_assert_read_lock();                  return;
719          if (head->read_eof)          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
720                  return 0;                  struct ccs_manager *ptr =
721          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {                          list_entry(head->r.acl, typeof(*ptr), head.list);
722                  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)  
723                          continue;                          continue;
724                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
725                          return 0;                          return;
726                    ccs_set_string(head, ptr->manager->name);
727                    ccs_set_lf(head);
728          }          }
729          head->read_eof = true;          head->r.eof = true;
         return 0;  
730  }  }
731    
732  /**  /**
733   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
734   *   *
735   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
736   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
737   *   *
738   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
739   */   */
740  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
741  {  {
742          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
743          const char *exe;          const char *exe;
744          struct task_struct *task = current;          struct task_struct *task = current;
745          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
746                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
747          bool found = false;          bool found = false;
         ccs_assert_read_lock();  
748          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
749                  return true;                  return true;
750          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
751                  return true;                  return true;
752          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
753                  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;  
                 }  
         }  
754          exe = ccs_get_exe();          exe = ccs_get_exe();
755          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
756                  return false;                                  head.list) {
757          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
758                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
759                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
760                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
761                          /* Set manager flag. */                                  continue;
762                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
763                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
764                                    continue;
765                  }                  }
766                    /* Set manager flag. */
767                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
768                    found = true;
769                    break;
770          }          }
771          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
772                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 525  static bool ccs_is_policy_manager(void) Line 782  static bool ccs_is_policy_manager(void)
782  }  }
783    
784  /**  /**
785   * 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.  
786   *   *
787   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
788   * @data: String to parse.   * @data: String to parse.
# Line 561  static char *ccs_find_condition_part(cha Line 791  static char *ccs_find_condition_part(cha
791   *   *
792   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
793   */   */
794  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)
795  {  {
796          unsigned int pid;          unsigned int pid;
797          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
798          ccs_assert_read_lock();          bool global_pid = false;
799          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
800                  head->read_execute_only = true;                  head->r.print_execute_only = true;
801                  return true;                  return true;
802          }          }
803          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
804                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
805                  struct task_struct *p;                  struct task_struct *p;
806                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
807    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
808                    if (global_pid)
809                            p = ccsecurity_exports.find_task_by_pid_ns(pid,
810                                                                   &init_pid_ns);
811                    else
812                            p = ccsecurity_exports.find_task_by_vpid(pid);
813    #else
814                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
815    #endif
816                  if (p)                  if (p)
817                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
818                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
819          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
820                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
821                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
822          } else          } else
823                  return false;                  return false;
824          head->write_var1 = domain;          head->w.domain = domain;
825          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
826          if (!head->read_buf)          if (!head->read_buf)
827                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
828          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
829            head->r.print_this_domain_only = true;
830            if (domain)
831                    head->r.domain = &domain->list;
832            else
833                    head->r.eof = true;
834          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
835          head->read_single_domain = true;          if (domain && domain->is_deleted)
836          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");  
         }  
837          return true;          return true;
838  }  }
839    
840  static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,  static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
841                                      struct ccs_condition *cond,                                   const struct ccs_acl_info *b)
842                                      const bool is_delete)  {
843  {          const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
844          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))          const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
845                  return ccs_write_capability_policy(data, domain, cond,          return p1->handler == p2->handler;
846                                                     is_delete);  }
847          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))  
848                  return ccs_write_network_policy(data, domain, cond, is_delete);  static bool ccs_same_task_acl(const struct ccs_acl_info *a,
849          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                                const struct ccs_acl_info *b)
850                  return ccs_write_signal_policy(data, domain, cond, is_delete);  {
851          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))          const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
852                  return ccs_write_env_policy(data, domain, cond, is_delete);          const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
853          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          return p1->domainname == p2->domainname;
854                  return ccs_write_mount_policy(data, domain, cond, is_delete);  }
855          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_UNMOUNT))  
856                  return ccs_write_umount_policy(data, domain, cond, is_delete);  /**
857          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CHROOT))   * ccs_write_task - Update task related list.
858                  return ccs_write_chroot_policy(data, domain, cond, is_delete);   *
859          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_PIVOT_ROOT))   * @param: Pointer to "struct ccs_acl_param".
860                  return ccs_write_pivot_root_policy(data, domain, cond,   *
861                                                     is_delete);   * Returns 0 on success, negative value otherwise.
862          return ccs_write_file_policy(data, domain, cond, is_delete);   */
863    static int ccs_write_task(struct ccs_acl_param *param)
864    {
865            int error;
866            const bool is_auto = ccs_str_starts(&param->data,
867                                                "auto_domain_transition ");
868            if (!is_auto && !ccs_str_starts(&param->data,
869                                            "manual_domain_transition ")) {
870                    struct ccs_handler_acl e = { };
871                    char *handler;
872                    if (ccs_str_starts(&param->data, "auto_execute_handler "))
873                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
874                    else if (ccs_str_starts(&param->data,
875                                            "denied_execute_handler "))
876                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
877                    else
878                            return -EINVAL;
879                    handler = ccs_read_token(param);
880                    if (!ccs_correct_path(handler))
881                            return -EINVAL;
882                    e.handler = ccs_get_name(handler);
883                    if (!e.handler)
884                            return -ENOMEM;
885                    if (e.handler->is_patterned)
886                            error = -EINVAL; /* No patterns allowed. */
887                    else
888                            error = ccs_update_domain(&e.head, sizeof(e), param,
889                                                      ccs_same_handler_acl, NULL);
890                    ccs_put_name(e.handler);
891            } else {
892                    struct ccs_task_acl e = {
893                            .head.type = is_auto ?
894                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
895                            .domainname = ccs_get_domainname(param),
896                    };
897                    if (!e.domainname)
898                            error = -EINVAL;
899                    else
900                            error = ccs_update_domain(&e.head, sizeof(e), param,
901                                                      ccs_same_task_acl, NULL);
902                    ccs_put_name(e.domainname);
903            }
904            return error;
905    }
906    
907    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
908                                 const bool is_delete)
909    {
910            struct ccs_acl_param param = {
911                    .data = data,
912                    .domain = domain,
913                    .is_delete = is_delete,
914            };
915            static const struct {
916                    const char *keyword;
917                    int (*write) (struct ccs_acl_param *);
918            } ccs_callback[7] = {
919                    { "file ", ccs_write_file },
920                    { "network inet ", ccs_write_inet_network },
921                    { "network unix ", ccs_write_unix_network },
922                    { "misc ", ccs_write_misc },
923                    { "capability ", ccs_write_capability },
924                    { "ipc ", ccs_write_ipc },
925                    { "task ", ccs_write_task },
926            };
927            u8 i;
928            for (i = 0; i < 7; i++) {
929                    if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
930                            continue;
931                    return ccs_callback[i].write(&param);
932            }
933            return -EINVAL;
934  }  }
935    
936    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
937            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
938            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
939    };
940            
941  /**  /**
942   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
943   *   *
944   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
945   *   *
946   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
947   */   */
948  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
949  {  {
950          char *data = head->write_buf;          char *data = head->write_buf;
951          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
952          bool is_delete = false;          bool is_delete = false;
953          bool is_select = false;          bool is_select = false;
954          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
955          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
956                  is_delete = true;                  is_delete = true;
957          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
958                  is_select = true;                  is_select = true;
959          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
960                  return 0;                  return 0;
961          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
962          if (!ccs_is_policy_manager())          if (!ccs_manager())
963                  return -EPERM;                  return -EPERM;
964          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
965                  domain = NULL;                  domain = NULL;
966                  if (is_delete)                  if (is_delete)
967                          ccs_delete_domain(data);                          ccs_delete_domain(data);
968                  else if (is_select)                  else if (is_select)
969                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
970                  else                  else
971                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
972                  head->write_var1 = domain;                  head->w.domain = domain;
973                  return 0;                  return 0;
974          }          }
975          if (!domain)          if (!domain)
# Line 674  static int ccs_write_domain_policy(struc Line 977  static int ccs_write_domain_policy(struc
977    
978          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
979              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
980                  if (ccs_profile_ptr[profile] || !ccs_policy_loaded)                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
981                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
982                  return 0;                  return 0;
983          }          }
984          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
985                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
986                    domain->group = (u8) profile;
987                  return 0;                  return 0;
988          }          }
989          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
990                  domain->ignore_global_allow_env = !is_delete;                  const char *cp = ccs_dif[profile];
991                    if (strncmp(data, cp, strlen(cp) - 1))
992                            continue;
993                    domain->flags[profile] = !is_delete;
994                  return 0;                  return 0;
995          }          }
996          cp = ccs_find_condition_part(data);          return ccs_write_domain2(data, domain, is_delete);
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         error = ccs_write_domain_policy2(data, domain, cond, is_delete);  
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
 }  
   
 static bool ccs_print_name_union(struct ccs_io_buffer *head,  
                                  const struct ccs_name_union *ptr)  
 {  
         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_name_union_quoted(struct ccs_io_buffer *head,  
                                         const struct ccs_name_union *ptr)  
 {  
         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_common(struct ccs_io_buffer *head,  
                                           const struct ccs_number_union *ptr,  
                                           const bool need_space)  
 {  
         unsigned long min;  
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
         if (need_space && !ccs_io_printf(head, " "))  
                 return false;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         min_type = ptr->min_type;  
         max_type = ptr->max_type;  
         min = ptr->values[0];  
         max = ptr->values[1];  
         switch (min_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 if (!ccs_io_printf(head, "0x%lX", min))  
                         return false;  
                 break;  
         case CCS_VALUE_TYPE_OCTAL:  
                 if (!ccs_io_printf(head, "0%lo", min))  
                         return false;  
                 break;  
         default:  
                 if (!ccs_io_printf(head, "%lu", min))  
                         return false;  
                 break;  
         }  
         if (min == max && min_type == max_type)  
                 return true;  
         switch (max_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 return ccs_io_printf(head, "-0x%lX", max);  
         case CCS_VALUE_TYPE_OCTAL:  
                 return ccs_io_printf(head, "-0%lo", max);  
         default:  
                 return ccs_io_printf(head, "-%lu", max);  
         }  
 }  
   
 bool ccs_print_number_union(struct ccs_io_buffer *head,  
                             const struct ccs_number_union *ptr)  
 {  
         return ccs_print_number_union_common(head, ptr, true);  
 }  
   
 static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  
                                            const struct ccs_number_union *ptr)  
 {  
         return ccs_print_number_union_common(head, ptr, false);  
997  }  }
998    
999  /**  /**
1000   * ccs_print_condition - Print condition part.   * ccs_print_name_union - Print a ccs_name_union.
1001   *   *
1002   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1003   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
1004   */   */
1005  static bool ccs_print_condition(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1006                                  const struct ccs_condition *cond)                                   const struct ccs_name_union *ptr)
1007  {  {
1008          const struct ccs_condition_element *condp;          const bool cond = head->r.print_cond_part;
         const struct ccs_number_union *numbers_p;  
         const struct ccs_name_union *names_p;  
         const struct ccs_argv_entry *argv;  
         const struct ccs_envp_entry *envp;  
         u16 condc;  
         u16 i;  
         u16 j;  
         char buffer[32];  
1009          if (!cond)          if (!cond)
1010                  goto no_condition;                  ccs_set_space(head);
1011          condc = cond->condc;          if (ptr->is_group) {
1012          condp = (const struct ccs_condition_element *) (cond + 1);                  ccs_set_string(head, "@");
1013          numbers_p = (const struct ccs_number_union *) (condp + condc);                  ccs_set_string(head, ptr->group->group_name->name);
1014          names_p = (const struct ccs_name_union *)          } else {
1015                  (numbers_p + cond->numbers_count);                  if (cond)
1016          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                          ccs_set_string(head, "\"");
1017          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                  ccs_set_string(head, ptr->filename->name);
1018          memset(buffer, 0, sizeof(buffer));                  if (cond)
1019          if (condc && !ccs_io_printf(head, "%s", " if"))                          ccs_set_string(head, "\"");
                 goto out;  
         for (i = 0; i < condc; i++) {  
                 const u8 match = condp->equals;  
                 const u8 left = condp->left;  
                 const u8 right = condp->right;  
                 condp++;  
                 switch (left) {  
                 case CCS_ARGV_ENTRY:  
                         if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",  
                                            argv->index, argv->is_not ?  
                                            "!=" : "=", argv->value->name))  
                                 goto out;  
                         argv++;  
                         continue;  
                 case CCS_ENVP_ENTRY:  
                         if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",  
                                            envp->name->name, envp->is_not ?  
                                            "!=" : "="))  
                                 goto out;  
                         if (envp->value) {  
                                 if (!ccs_io_printf(head, "\"%s\"",  
                                                    envp->value->name))  
                                         goto out;  
                         } else {  
                                 if (!ccs_io_printf(head, "NULL"))  
                                         goto out;  
                         }  
                         envp++;  
                         continue;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (left >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, " %s",  
                                            ccs_condition_keyword[left]))  
                                 goto out;  
                         break;  
                 }  
                 if (!ccs_io_printf(head, "%s", match ? "=" : "!="))  
                         goto out;  
                 switch (right) {  
                 case CCS_NAME_UNION:  
                         if (!ccs_print_name_union_quoted(head, names_p++))  
                                 goto out;  
                         break;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union_nospace(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (right >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, "%s",  
                                            ccs_condition_keyword[right]))  
                                 goto out;  
                         break;  
                 }  
         }  
         i = cond->post_state[3];  
         if (!i)  
                 goto no_condition;  
         if (!ccs_io_printf(head, " ; set"))  
                 goto out;  
         for (j = 0; j < 3; j++) {  
                 if (!(i & (1 << j)))  
                         continue;  
                 if (!ccs_io_printf(head, " task.state[%u]=%u", j,  
                                    cond->post_state[j]))  
                         goto out;  
1020          }          }
  no_condition:  
         if (ccs_io_printf(head, "\n"))  
                 return true;  
  out:  
         return false;  
1021  }  }
1022    
1023  /**  /**
1024   * ccs_print_path_acl - Print a single path ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1025   *   *
1026   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1027   * @ptr:  Pointer to "struct ccs_path_acl".   * @ptr:  Pointer to "struct ccs_number_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
1028   */   */
1029  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1030                                        struct ccs_path_acl *ptr,                                     const struct ccs_number_union *ptr)
                                       const struct ccs_condition *cond)  
1031  {  {
1032          int pos;          if (!head->r.print_cond_part)
1033          u8 bit;                  ccs_set_space(head);
1034          const u16 perm = ptr->perm;          if (ptr->is_group) {
1035          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {                  ccs_set_string(head, "@");
1036                  if (!(perm & (1 << bit)))                  ccs_set_string(head, ptr->group->group_name->name);
1037                          continue;          } else {
1038                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)                  int i;
1039                          continue;                  unsigned long min = ptr->values[0];
1040                  /* Print "read/write" instead of "read" and "write". */                  const unsigned long max = ptr->values[1];
1041                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)                  u8 min_type = ptr->value_type[0];
1042                      && (perm & (1 << CCS_TYPE_READ_WRITE)))                  const u8 max_type = ptr->value_type[1];
1043                          continue;                  char buffer[128];
1044                  pos = head->read_avail;                  buffer[0] = '\0';
1045                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||                  for (i = 0; i < 2; i++) {
1046                      !ccs_print_name_union(head, &ptr->name) ||                          switch (min_type) {
1047                      !ccs_print_condition(head, cond)) {                          case CCS_VALUE_TYPE_HEXADECIMAL:
1048                          head->read_bit = bit;                                  ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1049                          head->read_avail = pos;                                                min);
1050                          return false;                                  break;
1051                            case CCS_VALUE_TYPE_OCTAL:
1052                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1053                                                  min);
1054                                    break;
1055                            default:
1056                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1057                                                  min);
1058                                    break;
1059                            }
1060                            if (min == max && min_type == max_type)
1061                                    break;
1062                            ccs_addprintf(buffer, sizeof(buffer), "-");
1063                            min_type = max_type;
1064                            min = max;
1065                  }                  }
1066                    ccs_io_printf(head, "%s", buffer);
1067          }          }
         head->read_bit = 0;  
         return true;  
1068  }  }
1069    
1070  /**  /**
1071   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_print_condition - Print condition part.
1072   *   *
1073   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1074   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @cond: Pointer to "struct ccs_condition".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1075   *   *
1076   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1077   */   */
1078  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
                                 struct ccs_path_number3_acl *ptr,  
1079                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1080  {  {
1081          int pos;          switch (head->r.cond_step) {
1082          u8 bit;          case 0:
1083          const u16 perm = ptr->perm;                  head->r.cond_index = 0;
1084          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;                  head->r.cond_step++;
1085               bit++) {                  /* fall through */
1086                  if (!(perm & (1 << bit)))          case 1:
1087                          continue;                  {
1088                  pos = head->read_avail;                          const u16 condc = cond->condc;
1089                  if (!ccs_io_printf(head, "allow_%s",                          const struct ccs_condition_element *condp =
1090                                     ccs_path_number32keyword(bit)) ||                                  (typeof(condp)) (cond + 1);
1091                      !ccs_print_name_union(head, &ptr->name) ||                          const struct ccs_number_union *numbers_p =
1092                      !ccs_print_number_union(head, &ptr->mode) ||                                  (typeof(numbers_p)) (condp + condc);
1093                      !ccs_print_number_union(head, &ptr->major) ||                          const struct ccs_name_union *names_p =
1094                      !ccs_print_number_union(head, &ptr->minor) ||                                  (typeof(names_p))
1095                      !ccs_print_condition(head, cond)) {                                  (numbers_p + cond->numbers_count);
1096                          head->read_bit = bit;                          const struct ccs_argv *argv =
1097                          head->read_avail = pos;                                  (typeof(argv)) (names_p + cond->names_count);
1098                          return false;                          const struct ccs_envp *envp =
1099                  }                                  (typeof(envp)) (argv + cond->argc);
1100          }                          u16 skip;
1101          head->read_bit = 0;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1102          return true;                                  const u8 left = condp->left;
1103  }                                  const u8 right = condp->right;
1104                                    condp++;
1105  /**                                  switch (left) {
1106   * ccs_print_path2_acl - Print a double path ACL entry.                                  case CCS_ARGV_ENTRY:
1107   *                                          argv++;
1108   * @head: Pointer to "struct ccs_io_buffer".                                          continue;
1109   * @ptr:  Pointer to "struct ccs_path2_acl".                                  case CCS_ENVP_ENTRY:
1110   * @cond: Pointer to "struct ccs_condition". May be NULL.                                          envp++;
1111   *                                          continue;
1112   * Returns true on success, false otherwise.                                  case CCS_NUMBER_UNION:
1113   */                                          numbers_p++;
1114  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,                                          break;
1115                                        struct ccs_path2_acl *ptr,                                  }
1116                                        const struct ccs_condition *cond)                                  switch (right) {
1117  {                                  case CCS_NAME_UNION:
1118          int pos;                                          names_p++;
1119          u8 bit;                                          break;
1120          const u8 perm = ptr->perm;                                  case CCS_NUMBER_UNION:
1121          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {                                          numbers_p++;
1122                  if (!(perm & (1 << bit)))                                          break;
1123                          continue;                                  }
1124                  pos = head->read_avail;                          }
1125                  if (!ccs_io_printf(head, "allow_%s",                          while (head->r.cond_index < condc) {
1126                                     ccs_path22keyword(bit)) ||                                  const u8 match = condp->equals;
1127                      !ccs_print_name_union(head, &ptr->name1) ||                                  const u8 left = condp->left;
1128                      !ccs_print_name_union(head, &ptr->name2) ||                                  const u8 right = condp->right;
1129                      !ccs_print_condition(head, cond)) {                                  if (!ccs_flush(head))
1130                          head->read_bit = bit;                                          return false;
1131                          head->read_avail = pos;                                  condp++;
1132                          return false;                                  head->r.cond_index++;
1133                                    ccs_set_space(head);
1134                                    switch (left) {
1135                                    case CCS_ARGV_ENTRY:
1136                                            ccs_io_printf(head,
1137                                                          "exec.argv[%u]%s\"%s\"",
1138                                                          argv->index,
1139                                                          argv->is_not ?
1140                                                          "!=" : "=",
1141                                                          argv->value->name);
1142                                            argv++;
1143                                            continue;
1144                                    case CCS_ENVP_ENTRY:
1145                                            ccs_io_printf(head,
1146                                                          "exec.envp[\"%s\"]%s",
1147                                                          envp->name->name,
1148                                                          envp->is_not ?
1149                                                          "!=" : "=");
1150                                            if (envp->value) {
1151                                                    ccs_set_string(head, "\"");
1152                                                    ccs_set_string(head, envp->
1153                                                                   value->name);
1154                                                    ccs_set_string(head, "\"");
1155                                            } else {
1156                                                    ccs_set_string(head, "NULL");
1157                                            }
1158                                            envp++;
1159                                            continue;
1160                                    case CCS_NUMBER_UNION:
1161                                            ccs_print_number_union(head,
1162                                                                   numbers_p++);
1163                                            break;
1164                                    default:
1165                                            ccs_set_string(head,
1166                                                   ccs_condition_keyword[left]);
1167                                            break;
1168                                    }
1169                                    ccs_set_string(head, match ? "=" : "!=");
1170                                    switch (right) {
1171                                    case CCS_NAME_UNION:
1172                                            ccs_print_name_union(head, names_p++);
1173                                            break;
1174                                    case CCS_NUMBER_UNION:
1175                                            ccs_print_number_union(head,
1176                                                                   numbers_p++);
1177                                            break;
1178                                    default:
1179                                            ccs_set_string(head,
1180                                                   ccs_condition_keyword[right]);
1181                                            break;
1182                                    }
1183                            }
1184                  }                  }
1185          }                  head->r.cond_step++;
1186          head->read_bit = 0;                  /* fall through */
1187          return true;          case 2:
1188  }                  if (!ccs_flush(head))
1189                            break;
1190  /**                  head->r.cond_step++;
1191   * ccs_print_path_number_acl - Print an ioctl/chmod/chown/chgrp ACL entry.                  /* fall through */
1192   *          case 3:
1193   * @head: Pointer to "struct ccs_io_buffer".                  if (cond->audit)
1194   * @ptr:  Pointer to "struct ccs_path_number_acl".                          ccs_io_printf(head, " audit=%s",
1195   * @cond: Pointer to "struct ccs_condition". May be NULL.                                        ccs_yesno(cond->audit == 2));
1196   *                  if (cond->transit) {
1197   * Returns true on success, false otherwise.                          ccs_set_string(head, " auto_domain_transitition=\"");
1198   */                          ccs_set_string(head, cond->transit->name);
1199  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,                          ccs_set_string(head, "\"");
                                       struct ccs_path_number_acl *ptr,  
                                       const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u8 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path_number2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->number) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
1200                  }                  }
1201                    ccs_set_lf(head);
1202                    return true;
1203          }          }
1204          head->read_bit = 0;          return false;
         return true;  
 }  
   
 /**  
  * ccs_print_env_acl - Print an evironment variable name's ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_env_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_env_acl(struct ccs_io_buffer *head,  
                               struct ccs_env_acl *ptr,  
                               const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_capability_acl - Print a capability ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_capability_acl".  
  * @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 *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",  
                            ccs_cap2keyword(ptr->operation)) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
1205  }  }
1206    
1207  /**  /**
1208   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.   * ccs_fns - Find next set bit.
1209   *   *
1210   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1211   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @bit:  First bit to find.
1212   *   *
1213   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1214   */   */
1215  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                                  struct ccs_ip_network_acl *ptr)  
1216  {  {
1217          const u32 min_address = ptr->address.ipv4.min;          for ( ; bit < 8; bit++)
1218          const u32 max_address = ptr->address.ipv4.max;                  if (perm & (1 << bit))
1219          if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))                          break;
1220                  return false;          return bit;
         if (min_address != max_address  
             && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))  
                 return false;  
         return true;  
1221  }  }
1222    
1223  /**  static void ccs_set_group(struct ccs_io_buffer *head)
  * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl *ptr)  
1224  {  {
1225          char buf[64];          if (head->type == CCS_EXCEPTIONPOLICY)
1226          const struct in6_addr *min_address = ptr->address.ipv6.min;                  ccs_io_printf(head, "acl_group %u ", head->r.group_index);
         const struct in6_addr *max_address = ptr->address.ipv6.max;  
         ccs_print_ipv6(buf, sizeof(buf), min_address);  
         if (!ccs_io_printf(head, "%s", buf))  
                 return false;  
         if (min_address != max_address) {  
                 ccs_print_ipv6(buf, sizeof(buf), max_address);  
                 if (!ccs_io_printf(head, "-%s", buf))  
                         return false;  
         }  
         return true;  
1227  }  }
1228    
1229  /**  /**
1230   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_entry - Print an ACL entry.
1231   *   *
1232   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1233   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1234   *   *
1235   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1236   */   */
1237  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1238                                    struct ccs_ip_network_acl *ptr,                              const struct ccs_acl_info *acl)
                                   const struct ccs_condition *cond)  
1239  {  {
1240          int pos;          const u8 acl_type = acl->type;
1241          u8 bit;          u8 bit;
1242          const u16 perm = ptr->perm;          if (head->r.print_cond_part)
1243          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                  goto print_cond_part;
1244                  if (!(perm & (1 << bit)))          if (acl->is_deleted)
1245                          continue;                  return true;
1246                  pos = head->read_avail;   next:
1247                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",          bit = head->r.bit;
1248                                     ccs_net2keyword(bit)))          if (!ccs_flush(head))
1249                          goto out;                  return false;
1250            else if (acl_type == CCS_TYPE_PATH_ACL) {
1251                    struct ccs_path_acl *ptr
1252                            = container_of(acl, typeof(*ptr), head);
1253                    const u16 perm = ptr->perm;
1254                    for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1255                            if (!(perm & (1 << bit)))
1256                                    continue;
1257                            if (head->r.print_execute_only &&
1258                                bit != CCS_TYPE_EXECUTE
1259                                /* && bit != CCS_TYPE_TRANSIT */)
1260                                    continue;
1261                            break;
1262                    }
1263                    if (bit >= CCS_MAX_PATH_OPERATION)
1264                            goto done;
1265                    ccs_set_group(head);
1266                    ccs_set_string(head, "file ");
1267                    ccs_set_string(head, ccs_path_keyword[bit]);
1268                    ccs_print_name_union(head, &ptr->name);
1269            } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1270                       acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1271                    struct ccs_handler_acl *ptr
1272                            = container_of(acl, typeof(*ptr), head);
1273                    ccs_set_group(head);
1274                    ccs_set_string(head, "task ");
1275                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1276                                   ? "auto_execute_handler " :
1277                                   "denied_execute_handler ");
1278                    ccs_set_string(head, ptr->handler->name);
1279            } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1280                       acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1281                    struct ccs_task_acl *ptr =
1282                            container_of(acl, typeof(*ptr), head);
1283                    ccs_set_group(head);
1284                    ccs_set_string(head, "task ");
1285                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1286                                   "auto_domain_transition " :
1287                                   "manual_domain_transition ");
1288                    ccs_set_string(head, ptr->domainname->name);
1289            } else if (head->r.print_execute_only) {
1290                    return true;
1291            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1292                    struct ccs_mkdev_acl *ptr =
1293                            container_of(acl, typeof(*ptr), head);
1294                    bit = ccs_fns(ptr->perm, bit);
1295                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1296                            goto done;
1297                    ccs_set_group(head);
1298                    ccs_set_string(head, "file ");
1299                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1300                    ccs_print_name_union(head, &ptr->name);
1301                    ccs_print_number_union(head, &ptr->mode);
1302                    ccs_print_number_union(head, &ptr->major);
1303                    ccs_print_number_union(head, &ptr->minor);
1304            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1305                    struct ccs_path2_acl *ptr =
1306                            container_of(acl, typeof(*ptr), head);
1307                    bit = ccs_fns(ptr->perm, bit);
1308                    if (bit >= CCS_MAX_PATH2_OPERATION)
1309                            goto done;
1310                    ccs_set_group(head);
1311                    ccs_set_string(head, "file ");
1312                    ccs_set_string(head, ccs_path2_keyword[bit]);
1313                    ccs_print_name_union(head, &ptr->name1);
1314                    ccs_print_name_union(head, &ptr->name2);
1315            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1316                    struct ccs_path_number_acl *ptr =
1317                            container_of(acl, typeof(*ptr), head);
1318                    bit = ccs_fns(ptr->perm, bit);
1319                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1320                            goto done;
1321                    ccs_set_group(head);
1322                    ccs_set_string(head, "file ");
1323                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1324                    ccs_print_name_union(head, &ptr->name);
1325                    ccs_print_number_union(head, &ptr->number);
1326            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1327                    struct ccs_env_acl *ptr =
1328                            container_of(acl, typeof(*ptr), head);
1329                    ccs_set_group(head);
1330                    ccs_set_string(head, "misc env ");
1331                    ccs_set_string(head, ptr->env->name);
1332            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1333                    struct ccs_capability_acl *ptr =
1334                            container_of(acl, typeof(*ptr), head);
1335                    ccs_set_group(head);
1336                    ccs_set_string(head, "capability ");
1337                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1338            } else if (acl_type == CCS_TYPE_INET_ACL) {
1339                    struct ccs_inet_acl *ptr =
1340                            container_of(acl, typeof(*ptr), head);
1341                    bit = ccs_fns(ptr->perm, bit);
1342                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1343                            goto done;
1344                    ccs_set_group(head);
1345                    ccs_set_string(head, "network inet ");
1346                    ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1347                    ccs_set_space(head);
1348                    ccs_set_string(head, ccs_socket_keyword[bit]);
1349                    ccs_set_space(head);
1350                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1351                            char buf[128];
1352                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1353                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1354                                             group_name->name))                          ccs_set_string(head,
1355                                  goto out;                                         ptr->address.group->group_name->name);
1356                          break;                          break;
1357                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1358                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1359                                  goto out;                                         ptr->address.ipv4.max);
1360                            ccs_io_printf(head, "%s", buf);
1361                          break;                          break;
1362                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1363                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1364                                  goto out;                                         ptr->address.ipv6.max);
1365                            ccs_io_printf(head, "%s", buf);
1366                          break;                          break;
1367                  }                  }
1368                  if (!ccs_print_number_union(head, &ptr->port) ||                  ccs_print_number_union(head, &ptr->port);
1369                      !ccs_print_condition(head, cond))          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1370                          goto out;                  struct ccs_unix_acl *ptr =
1371          }                          container_of(acl, typeof(*ptr), head);
1372          head->read_bit = 0;                  bit = ccs_fns(ptr->perm, bit);
1373          return true;                  if (bit >= CCS_MAX_NETWORK_OPERATION)
1374   out:                          goto done;
1375          head->read_bit = bit;                  ccs_set_group(head);
1376          head->read_avail = pos;                  ccs_set_string(head, "network unix ");
1377          return false;                  ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1378  }                  ccs_set_space(head);
1379                    ccs_set_string(head, ccs_socket_keyword[bit]);
1380  /**                  ccs_print_name_union(head, &ptr->name);
1381   * ccs_print_signal_acl - Print a signal ACL entry.          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1382   *                  struct ccs_signal_acl *ptr =
1383   * @head: Pointer to "struct ccs_io_buffer".                          container_of(acl, typeof(*ptr), head);
1384   * @ptr:  Pointer to "struct signale_acl".                  ccs_set_group(head);
1385   * @cond: Pointer to "struct ccs_condition". May be NULL.                  ccs_set_string(head, "ipc signal ");
1386   *                  ccs_io_printf(head, "%u ", ptr->sig);
1387   * Returns true on success, false otherwise.                  ccs_set_string(head, ptr->domainname->name);
1388   */          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1389  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,                  struct ccs_mount_acl *ptr =
1390                                   struct ccs_signal_acl *ptr,                          container_of(acl, typeof(*ptr), head);
1391                                   const struct ccs_condition *cond)                  ccs_set_group(head);
1392  {                  ccs_io_printf(head, "file mount");
1393          const int pos = head->read_avail;                  ccs_print_name_union(head, &ptr->dev_name);
1394          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",                  ccs_print_name_union(head, &ptr->dir_name);
1395                             ptr->sig, ptr->domainname->name) ||                  ccs_print_name_union(head, &ptr->fs_type);
1396              !ccs_print_condition(head, cond)) {                  ccs_print_number_union(head, &ptr->flags);
1397                  head->read_avail = pos;          }
1398                  return false;          head->r.bit = bit + 1;
1399          }          if (acl->cond) {
1400          return true;                  head->r.print_cond_part = true;
1401  }                  head->r.cond_step = 0;
1402                    if (!ccs_flush(head))
1403  /**                          return false;
1404   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   print_cond_part:
1405   *                  if (!ccs_print_condition(head, acl->cond))
1406   * @head:    Pointer to "struct ccs_io_buffer".                          return false;
1407   * @keyword: Name of the keyword.                  head->r.print_cond_part = false;
1408   * @ptr:     Pointer to "struct ccs_execute_handler_record".          } else {
1409   *                  ccs_set_lf(head);
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  
                                              const char *keyword,  
                                              struct ccs_execute_handler_record *  
                                              ptr)  
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @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 *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||  
             !ccs_print_name_union(head, &ptr->dev_name) ||  
             !ccs_print_name_union(head, &ptr->dir_name) ||  
             !ccs_print_name_union(head, &ptr->fs_type) ||  
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1410          }          }
1411          return true;          switch (acl_type) {
1412  }          case CCS_TYPE_PATH_ACL:
1413            case CCS_TYPE_MKDEV_ACL:
1414  /**          case CCS_TYPE_PATH2_ACL:
1415   * ccs_print_umount_acl - Print a mount ACL entry.          case CCS_TYPE_PATH_NUMBER_ACL:
1416   *          case CCS_TYPE_INET_ACL:
1417   * @head: Pointer to "struct ccs_io_buffer".          case CCS_TYPE_UNIX_ACL:
1418   * @ptr:  Pointer to "struct ccs_umount_acl".                  goto next;
  * @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 *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_UNMOUNT) ||  
             !ccs_print_name_union(head, &ptr->dir) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1419          }          }
1420     done:
1421            head->r.bit = 0;
1422          return true;          return true;
1423  }  }
1424    
1425  /**  /**
1426   * ccs_print_chroot_acl - Print a chroot ACL entry.   * ccs_read_domain2 - Read domain policy.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_chroot_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1427   *   *
1428   * Returns true on success, false otherwise.   * @head:   Pointer to "struct ccs_io_buffer".
1429   */   * @domain: Pointer to "struct ccs_domain_info".
1430  static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,   * @index:  Index number.
                                  struct ccs_chroot_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CHROOT) ||  
             !ccs_print_name_union(head, &ptr->dir) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.  
1431   *   *
1432   * @head: Pointer to "struct ccs_io_buffer".   * Caller holds ccs_read_lock().
  * @ptr:  Pointer to "struct ccs_pivot_root_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1433   *   *
1434   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1435   */   */
1436  static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1437                                       struct ccs_pivot_root_acl *ptr,                               struct ccs_domain_info *domain,
1438                                       const struct ccs_condition *cond)                               const u8 index)
1439  {  {
1440          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1441          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_PIVOT_ROOT) ||                  struct ccs_acl_info *ptr =
1442              !ccs_print_name_union(head, &ptr->new_root) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1443              !ccs_print_name_union(head, &ptr->old_root) ||                  if (!ccs_print_entry(head, ptr))
1444              !ccs_print_condition(head, cond)) {                          return false;
                 head->read_avail = pos;  
                 return false;  
1445          }          }
1446            head->r.acl = NULL;
1447          return true;          return true;
1448  }  }
1449    
1450  /**  /**
1451   * ccs_print_entry - Print an ACL entry.   * ccs_read_domain - Read domain policy.
1452   *   *
1453   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to an ACL entry.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_entry(struct ccs_io_buffer *head,  
                             struct ccs_acl_info *ptr)  
 {  
         const struct ccs_condition *cond = ptr->cond;  
         const u8 acl_type = ptr->type;  
         if (ptr->is_deleted)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_ACL) {  
                 struct ccs_path_acl *acl  
                         = container_of(ptr, struct ccs_path_acl,  
                                        head);  
                 return ccs_print_path_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, struct ccs_path_number3_acl, head);  
                 return ccs_print_path_number3_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, struct ccs_path2_acl,  
                                        head);  
                 return ccs_print_path2_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, struct ccs_path_number_acl,  
                                        head);  
                 return ccs_print_path_number_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, struct ccs_env_acl, head);  
                 return ccs_print_env_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, struct ccs_capability_acl,  
                                        head);  
                 return ccs_print_capability_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, struct ccs_ip_network_acl,  
                                        head);  
                 return ccs_print_network_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, struct ccs_signal_acl, head);  
                 return ccs_print_signal_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, struct ccs_mount_acl, head);  
                 return ccs_print_mount_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_UMOUNT_ACL) {  
                 struct ccs_umount_acl *acl  
                         = container_of(ptr, struct ccs_umount_acl, head);  
                 return ccs_print_umount_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CHROOT_ACL) {  
                 struct ccs_chroot_acl *acl  
                         = container_of(ptr, struct ccs_chroot_acl, head);  
                 return ccs_print_chroot_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PIVOT_ROOT_ACL) {  
                 struct ccs_pivot_root_acl *acl  
                         = container_of(ptr, struct ccs_pivot_root_acl,  
                                        head);  
                 return ccs_print_pivot_root_acl(head, acl, cond);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
  * ccs_read_domain_policy - Read domain policy.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns 0.  
1454   *   *
1455   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1456   */   */
1457  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1458  {  {
1459          struct list_head *dpos;          if (head->r.eof)
1460          struct list_head *apos;                  return;
1461          ccs_assert_read_lock();          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1462          if (head->read_eof)                  struct ccs_domain_info *domain =
1463                  return 0;                          list_entry(head->r.domain, typeof(*domain), list);
1464          if (head->read_step == 0)                  switch (head->r.step) {
1465                  head->read_step = 1;                          u8 i;
1466          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                  case 0:
1467                  struct ccs_domain_info *domain;                          if (domain->is_deleted &&
1468                  const char *quota_exceeded = "";                              !head->r.print_this_domain_only)
1469                  const char *transition_failed = "";                                  continue;
1470                  const char *ignore_global_allow_read = "";                          /* Print domainname and flags. */
1471                  const char *ignore_global_allow_env = "";                          ccs_set_string(head, domain->domainname->name);
1472                  domain = list_entry(dpos, struct ccs_domain_info, list);                          ccs_set_lf(head);
1473                  if (head->read_step != 1)                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1474                          goto acl_loop;                                        domain->profile);
1475                  if (domain->is_deleted && !head->read_single_domain)                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1476                          continue;                                        domain->group);
1477                  /* Print domainname and flags. */                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1478                  if (domain->quota_warned)                                  if (domain->flags[i])
1479                          quota_exceeded = "quota_exceeded\n";                                          ccs_set_string(head, ccs_dif[i]);
1480                  if (domain->domain_transition_failed)                          head->r.step++;
1481                          transition_failed = "transition_failed\n";                          ccs_set_lf(head);
1482                  if (domain->ignore_global_allow_read)                          /* fall through */
1483                          ignore_global_allow_read                  case 1:
1484                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          if (!ccs_read_domain2(head, domain, 0))
1485                  if (domain->ignore_global_allow_env)                                  return;
1486                          ignore_global_allow_env                          head->r.step++;
1487                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          /* fall through */
1488                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                  case 2:
1489                                     "%s%s%s%s\n", domain->domainname->name,                          if (!ccs_read_domain2(head, domain, 1))
1490                                     domain->profile, quota_exceeded,                                  return;
1491                                     transition_failed,                          head->r.step++;
1492                                     ignore_global_allow_read,                          if (!ccs_set_lf(head))
1493                                     ignore_global_allow_env))                                  return;
1494                          return 0;                          /* fall through */
1495                  head->read_step = 2;                  case 3:
1496   acl_loop:                          head->r.step = 0;
1497                  if (head->read_step == 3)                          if (head->r.print_this_domain_only)
1498                          goto tail_mark;                                  goto done;
1499                  /* 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;  
1500          }          }
1501          head->read_eof = true;   done:
1502          return 0;          head->r.eof = true;
1503  }  }
1504    
1505  /**  /**
# Line 1488  static int ccs_read_domain_policy(struct Line 1512  static int ccs_read_domain_policy(struct
1512   * This is equivalent to doing   * This is equivalent to doing
1513   *   *
1514   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1515   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1516   *   *
1517   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1518   */   */
# Line 1498  static int ccs_write_domain_profile(stru Line 1522  static int ccs_write_domain_profile(stru
1522          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1523          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1524          unsigned int profile;          unsigned int profile;
         ccs_assert_read_lock();  
1525          if (!cp)          if (!cp)
1526                  return -EINVAL;                  return -EINVAL;
1527          *cp = '\0';          *cp = '\0';
# Line 1506  static int ccs_write_domain_profile(stru Line 1529  static int ccs_write_domain_profile(stru
1529          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1530                  return -EINVAL;                  return -EINVAL;
1531          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1532          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1533                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1534          return 0;          return 0;
1535  }  }
# Line 1516  static int ccs_write_domain_profile(stru Line 1539  static int ccs_write_domain_profile(stru
1539   *   *
1540   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1541   *   *
  * Returns list of profile number and domainname pairs.  
  *  
1542   * This is equivalent to doing   * This is equivalent to doing
1543   *   *
1544   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1527  static int ccs_write_domain_profile(stru Line 1548  static int ccs_write_domain_profile(stru
1548   *   *
1549   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1550   */   */
1551  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1552  {  {
1553          struct list_head *pos;          if (head->r.eof)
1554          ccs_assert_read_lock();                  return;
1555          if (head->read_eof)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1556                  return 0;                  struct ccs_domain_info *domain =
1557          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);  
1558                  if (domain->is_deleted)                  if (domain->is_deleted)
1559                          continue;                          continue;
1560                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
1561                                     domain->domainname->name))                          return;
1562                          return 0;                  ccs_io_printf(head, "%u ", domain->profile);
1563                    ccs_set_string(head, domain->domainname->name);
1564                    ccs_set_lf(head);
1565          }          }
1566          head->read_eof = true;          head->r.eof = true;
         return 0;  
1567  }  }
1568    
1569  /**  /**
# Line 1555  static int ccs_read_domain_profile(struc Line 1575  static int ccs_read_domain_profile(struc
1575   */   */
1576  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1577  {  {
1578          head->read_eof = false;          head->r.eof = false;
1579          return 0;          return 0;
1580  }  }
1581    
# Line 1570  static int ccs_write_pid(struct ccs_io_b Line 1590  static int ccs_write_pid(struct ccs_io_b
1590   *   *
1591   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1592   */   */
1593  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1594  {  {
1595          char *buf = head->write_buf;          char *buf = head->write_buf;
1596          bool task_info = false;          bool task_info = false;
1597            bool global_pid = false;
1598          unsigned int pid;          unsigned int pid;
1599          struct task_struct *p;          struct task_struct *p;
1600          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1601          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_assert_read_lock();  
1602          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1603          if (!buf)          if (!buf) {
1604                  goto done; /* Do nothing if open(O_RDONLY). */                  head->r.eof = true;
1605          if (head->read_avail || head->read_eof)                  return; /* Do nothing if open(O_RDONLY). */
1606                  goto done;          }
1607          head->read_eof = true;          if (head->r.w_pos || head->r.eof)
1608                    return;
1609            head->r.eof = true;
1610          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1611                  task_info = true;                  task_info = true;
1612            if (ccs_str_starts(&buf, "global-pid "))
1613                    global_pid = true;
1614          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1615          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1616    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1617            if (global_pid)
1618                    p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1619            else
1620                    p = ccsecurity_exports.find_task_by_vpid(pid);
1621    #else
1622          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1623    #endif
1624          if (p) {          if (p) {
1625                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1626                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1627          }          }
1628          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1629          if (!domain)          if (!domain)
1630                  goto done;                  return;
1631          if (!task_info)          if (!task_info) {
1632                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1633                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1634          else          } else {
1635                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
1636                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                ccs_yesno(ccs_flags &
1637                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                          CCS_TASK_IS_MANAGER),
1638                                "yes" : "no",                                ccs_yesno(ccs_flags &
1639                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                          CCS_TASK_IS_EXECUTE_HANDLER));
1640                                "yes" : "no",          }
                               (u8) (ccs_flags >> 24),  
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
  done:  
         return 0;  
1641  }  }
1642    
1643    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1644            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1645            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1646            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1647            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1648            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1649    };
1650    
1651    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1652            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1653            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1654            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1655    };
1656    
1657  /**  /**
1658   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1659   *   *
1660   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1661   *   *
1662   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1663   */   */
1664  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1665  {  {
1666          char *data = head->write_buf;          char *data = head->write_buf;
1667          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1668          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1669                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1670          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1671                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1672          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1673                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1674                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1675          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1676                  return ccs_write_domain_initializer_policy(data, true,          };
1677                                                             is_delete);          for (i = 0; i < 3; i++)
1678          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1679                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1680          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1681                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1682          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1683                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1684          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1685                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1686          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1687                  return ccs_write_path_group_policy(data, is_delete);          if (ccs_str_starts(&data, "acl_group ")) {
1688          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  unsigned int group;
1689                  return ccs_write_number_group_policy(data, is_delete);                  if (sscanf(data, "%u", &group) == 1 &&
1690          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                      group < CCS_MAX_ACL_GROUPS) {
1691                  return ccs_write_no_rewrite_policy(data, is_delete);                          data = strchr(data, ' ');
1692          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                          if (data)
1693                  return ccs_write_address_group_policy(data, is_delete);                                  return ccs_write_domain2(data + 1,
1694          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                                                           &ccs_acl_group[group],
1695                  return ccs_write_reserved_port_policy(data, is_delete);                                                           is_delete);
1696                    }
1697            }
1698          return -EINVAL;          return -EINVAL;
1699  }  }
1700    
1701  /**  /**
1702   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1703   *   *
1704   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1705     * @idx:  Index number.
1706   *   *
1707   * Returns 0 on success, -EINVAL otherwise.   * Returns true on success, false otherwise.
1708   *   *
1709   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1710   */   */
1711  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1712  {  {
1713          ccs_assert_read_lock();          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1714          if (!head->read_eof) {                  struct ccs_group *group =
1715                  switch (head->read_step) {                          list_entry(head->r.group, typeof(*group), head.list);
1716                  case 0:                  list_for_each_cookie(head->r.acl, &group->member_list) {
1717                          head->read_var2 = NULL;                          struct ccs_acl_head *ptr =
1718                          head->read_step = 1;                                  list_entry(head->r.acl, typeof(*ptr), list);
1719                  case 1:                          if (ptr->is_deleted)
1720                          if (!ccs_read_domain_keeper_policy(head))                                  continue;
1721                                  break;                          if (!ccs_flush(head))
1722                          head->read_var2 = NULL;                                  return false;
1723                          head->read_step = 2;                          ccs_set_string(head, ccs_group_name[idx]);
1724                  case 2:                          ccs_set_string(head, group->group_name->name);
1725                          if (!ccs_read_globally_readable_policy(head))                          if (idx == CCS_PATH_GROUP) {
1726                                  break;                                  ccs_set_space(head);
1727                          head->read_var2 = NULL;                                  ccs_set_string(head, container_of
1728                          head->read_step = 3;                                                 (ptr, struct ccs_path_group,
1729                  case 3:                                                  head)->member_name->name);
1730                          if (!ccs_read_globally_usable_env_policy(head))                          } else if (idx == CCS_NUMBER_GROUP) {
1731                                  break;                                  ccs_print_number_union(head, &container_of
1732                          head->read_var2 = NULL;                                                         (ptr, struct ccs_number_group,
1733                          head->read_step = 4;                                                          head)->number);
1734                  case 4:                          } else if (idx == CCS_ADDRESS_GROUP) {
1735                          if (!ccs_read_domain_initializer_policy(head))                                  char buffer[128];
1736                                  break;                                  struct ccs_address_group *member =
1737                          head->read_var2 = NULL;                                          container_of(ptr, typeof(*member),
1738                          head->read_step = 6;                                                       head);
1739                  case 6:                                  if (member->is_ipv6)
1740                          if (!ccs_read_aggregator_policy(head))                                          ccs_print_ipv6(buffer, sizeof(buffer),
1741                                  break;                                                         member->min.ipv6,
1742                          head->read_var2 = NULL;                                                         member->max.ipv6);
1743                          head->read_step = 7;                                  else
1744                  case 7:                                          ccs_print_ipv4(buffer, sizeof(buffer),
1745                          if (!ccs_read_file_pattern(head))                                                         member->min.ipv4,
1746                                  break;                                                         member->max.ipv4);
1747                          head->read_var2 = NULL;                                  ccs_io_printf(head, " %s", buffer);
1748                          head->read_step = 8;                          }
1749                  case 8:                          ccs_set_lf(head);
                         if (!ccs_read_no_rewrite_policy(head))  
                                 break;  
                         head->read_var2 = NULL;  
                         head->read_step = 9;  
                 case 9:  
                         if (!ccs_read_path_group_policy(head))  
                                 break;  
                         head->read_var1 = NULL;  
                         head->read_var2 = NULL;  
                         head->read_step = 10;  
                 case 10:  
                         if (!ccs_read_number_group_policy(head))  
                                 break;  
                         head->read_var1 = NULL;  
                         head->read_var2 = NULL;  
                         head->read_step = 11;  
                 case 11:  
                         if (!ccs_read_address_group_policy(head))  
                                 break;  
                         head->read_var2 = NULL;  
                         head->read_step = 12;  
                 case 12:  
                         if (!ccs_read_reserved_port_policy(head))  
                                 break;  
                         head->read_eof = true;  
                         break;  
                 default:  
                         return -EINVAL;  
1750                  }                  }
1751                    head->r.acl = NULL;
1752          }          }
1753          return 0;          head->r.group = NULL;
1754            return true;
1755  }  }
1756    
1757  /**  /**
1758   * ccs_get_argv0 - Get argv[0].   * ccs_read_policy - Read "struct ccs_..._entry" list.
1759   *   *
1760   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1761     * @idx:  Index number.
1762   *   *
1763   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1764     *
1765     * Caller holds ccs_read_lock().
1766   */   */
1767  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1768  {  {
1769          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1770          char *arg_ptr = ee->tmp;                  struct ccs_acl_head *acl =
1771          int arg_len = 0;                          container_of(head->r.acl, typeof(*acl), list);
1772          unsigned long pos = bprm->p;                  if (acl->is_deleted)
1773          int offset = pos % PAGE_SIZE;                          continue;
1774          bool done = false;                  if (!ccs_flush(head))
1775          if (!bprm->argc)                          return false;
1776                  goto out;                  switch (idx) {
1777          while (1) {                  case CCS_ID_TRANSITION_CONTROL:
1778                  if (!ccs_dump_page(bprm, pos, &ee->dump))                          {
1779                          goto out;                                  struct ccs_transition_control *ptr =
1780                  pos += PAGE_SIZE - offset;                                          container_of(acl, typeof(*ptr), head);
1781                  /* Read. */                                  ccs_set_string(head,
1782                  while (offset < PAGE_SIZE) {                                                 ccs_transition_type[ptr->type]);
1783                          const char *kaddr = ee->dump.data;                                  ccs_set_string(head, ptr->program ?
1784                          const unsigned char c = kaddr[offset++];                                                 ptr->program->name : "any");
1785                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                  ccs_set_string(head, " from ");
1786                                  if (c == '\\') {                                  ccs_set_string(head, ptr->domainname ?
1787                                          arg_ptr[arg_len++] = '\\';                                                 ptr->domainname->name : "any");
1788                                          arg_ptr[arg_len++] = '\\';                          }
1789                                  } else if (c == '/') {                          break;
1790                                          arg_len = 0;                  case CCS_ID_AGGREGATOR:
1791                                  } else if (c > ' ' && c < 127) {                          {
1792                                          arg_ptr[arg_len++] = c;                                  struct ccs_aggregator *ptr =
1793                                  } else {                                          container_of(acl, typeof(*ptr), head);
1794                                          arg_ptr[arg_len++] = '\\';                                  ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1795                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                  ccs_set_string(head, ptr->original_name->name);
1796                                          arg_ptr[arg_len++]                                  ccs_set_space(head);
1797                                                  = ((c >> 3) & 7) + '0';                                  ccs_set_string(head,
1798                                          arg_ptr[arg_len++] = (c & 7) + '0';                                                 ptr->aggregated_name->name);
                                 }  
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
1799                          }                          }
                 }  
                 offset = 0;  
                 if (done)  
1800                          break;                          break;
1801                    case CCS_ID_PATTERN:
1802                            {
1803                                    struct ccs_pattern *ptr =
1804                                            container_of(acl, typeof(*ptr), head);
1805                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1806                                    ccs_set_string(head, ptr->pattern->name);
1807                            }
1808                            break;
1809                    case CCS_ID_RESERVEDPORT:
1810                            {
1811                                    struct ccs_reserved *ptr =
1812                                            container_of(acl, typeof(*ptr), head);
1813                                    const u16 min_port = ptr->min_port;
1814                                    const u16 max_port = ptr->max_port;
1815                                    ccs_set_string(head,
1816                                                   CCS_KEYWORD_DENY_AUTOBIND);
1817                                    ccs_io_printf(head, "%u", min_port);
1818                                    if (min_port != max_port)
1819                                            ccs_io_printf(head, "-%u", max_port);
1820                            }
1821                            break;
1822                    default:
1823                            continue;
1824                    }
1825                    ccs_set_lf(head);
1826          }          }
1827            head->r.acl = NULL;
1828          return true;          return true;
  out:  
         return false;  
1829  }  }
1830    
1831  static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry  /**
1832                                                         *ee)   * ccs_read_exception - Read exception policy.
1833     *
1834     * @head: Pointer to "struct ccs_io_buffer".
1835     *
1836     * Caller holds ccs_read_lock().
1837     */
1838    static void ccs_read_exception(struct ccs_io_buffer *head)
1839  {  {
1840          struct ccs_condition *cond;          if (head->r.eof)
1841          char *buf;                  return;
1842          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
1843          char *realpath = NULL;                 ccs_read_policy(head, head->r.step))
1844          char *argv0 = NULL;                  head->r.step++;
1845          if (ccs_flags(NULL, CCS_AUTOLEARN_EXEC_REALPATH)) {          if (head->r.step < CCS_MAX_POLICY)
1846                  struct file *file = ee->bprm->file;                  return;
1847  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1848                  struct path path = { file->f_vfsmnt, file->f_dentry };                 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1849                  realpath = ccs_realpath_from_path(&path);                  head->r.step++;
1850  #else          if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1851                  realpath = ccs_realpath_from_path(&file->f_path);                  return;
1852  #endif          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1853                  if (realpath)                 + CCS_MAX_ACL_GROUPS * 2) {
1854                          len += strlen(realpath) + 17;                  head->r.group_index = (head->r.step - CCS_MAX_POLICY
1855          }                                         - CCS_MAX_GROUP) / 2;
1856          if (ccs_flags(NULL, CCS_AUTOLEARN_EXEC_REALPATH)) {                  if (!ccs_read_domain2(head,
1857                  if (ccs_get_argv0(ee)) {                                        &ccs_acl_group[head->r.group_index],
1858                          argv0 = ee->tmp;                                        head->r.step & 1))
1859                          len += strlen(argv0) + 16;                          return;
1860                  }                  head->r.step++;
1861          }          }
1862          buf = kmalloc(len, GFP_KERNEL);          head->r.eof = true;
         if (!buf)  
                 return NULL;  
         snprintf(buf, len - 1, "if");  
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (realpath) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",  
                          realpath);  
                 kfree(realpath);  
         }  
         if (argv0) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",  
                          argv0);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
1863  }  }
1864    
1865  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 1849  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1869  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1869  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1870    
1871  /* Structure for query. */  /* Structure for query. */
1872  struct ccs_query_entry {  struct ccs_query {
1873          struct list_head list;          struct list_head list;
1874          char *query;          char *query;
1875          int query_len;          int query_len;
# Line 1858  struct ccs_query_entry { Line 1878  struct ccs_query_entry {
1878          int answer;          int answer;
1879  };  };
1880    
1881  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1882  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1883    
1884  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1885  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1886    
1887    static void ccs_truncate(char *str)
1888    {
1889            while (* (unsigned char *) str > (unsigned char) ' ')
1890                    str++;
1891            *str = '\0';
1892    }
1893    
1894  /**  /**
1895   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1896   *   *
1897   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1898   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1899   *   *
1900   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1901   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1902   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1903   * 0 if it is not in enforcing mode, -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1904   */   */
1905  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1906  {  {
# Line 1882  int ccs_supervisor(struct ccs_request_in Line 1909  int ccs_supervisor(struct ccs_request_in
1909          int pos;          int pos;
1910          int len;          int len;
1911          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1912          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1913          bool quota_exceeded = false;          bool quota_exceeded = false;
1914          char *header;          char *header;
1915          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1916                  r->domain = ccs_current_domain();          va_start(args, fmt);
1917          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1918            va_end(args);
1919            if (r->mode == CCS_CONFIG_LEARNING) {
1920                  char *buffer;                  char *buffer;
1921                  struct ccs_condition *cond;                  char *realpath = NULL;
1922          case 1:                  char *argv0 = NULL;
1923                    char *symlink = NULL;
1924                    char *handler = NULL;
1925                    const struct ccs_preference *pref;
1926                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1927                          return 0;                          return 0;
1928                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1929                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, GFP_KERNEL);  
                 if (!buffer)  
1930                          return 0;                          return 0;
1931                  va_start(args, fmt);                  pref = &ccs_profile(r->profile)->preference;
1932                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1933                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1934                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1935                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1936                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1937                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                  if (argv0) {
1938                          char str[] = "if task.type=execute_handler";                                          argv0 += 10;
1939                          cond = ccs_get_condition(str);                                          ccs_truncate(argv0);
1940                  } else                                  }
1941                          cond = NULL;                          }
1942                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                          if (pref->learning_exec_realpath) {
1943                  ccs_put_condition(cond);                                  realpath = strstr(header,
1944                  kfree(buffer);                                                    " exec={ realpath=\"");
1945                  /* fall through */                                  if (realpath) {
1946          case 2:                                          realpath += 8;
1947                                            ccs_truncate(realpath);
1948                                    }
1949                            }
1950                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1951                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1952                               pref->learning_symlink_target) {
1953                            symlink = strstr(header, " symlink.target=\"");
1954                            if (symlink)
1955                                    ccs_truncate(symlink + 1);
1956                    }
1957                    handler = strstr(header, "type=execute_handler");
1958                    if (handler)
1959                            ccs_truncate(handler);
1960                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1961                    if (buffer) {
1962                            va_start(args, fmt);
1963                            vsnprintf(buffer, len - 1, fmt, args);
1964                            va_end(args);
1965                            if (handler || realpath || argv0 || symlink) {
1966                                    if (handler)
1967                                            ccs_addprintf(buffer, len, " task.%s",
1968                                                          handler);
1969                                    if (realpath)
1970                                            ccs_addprintf(buffer, len, " exec.%s",
1971                                                          realpath);
1972                                    if (argv0)
1973                                            ccs_addprintf(buffer, len,
1974                                                          " exec.argv[0]=%s",
1975                                                          argv0);
1976                                    if (symlink)
1977                                            ccs_addprintf(buffer, len, "%s",
1978                                                          symlink);
1979                            }
1980                            ccs_normalize_line(buffer);
1981                            ccs_write_domain2(buffer, domain, false);
1982                            kfree(buffer);
1983                    }
1984                    kfree(header);
1985                  return 0;                  return 0;
1986          }          }
1987            if (r->mode != CCS_CONFIG_ENFORCING)
1988                    return 0;
1989          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
1990                  int i;                  int i;
1991                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
1992                          return -EPERM;                          return -EPERM;
1993                  for (i = 0; i < ccs_flags(r->domain, CCS_SLEEP_PERIOD);                  for (i = 0; i < ccs_profile(domain->profile)->preference.
1994                       i++) {                               enforcing_penalty; i++) {
1995                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
1996                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
1997                  }                  }
1998                  return -EPERM;                  return -EPERM;
1999          }          }
2000          va_start(args, fmt);          header = ccs_init_log(&len, r);
         len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;  
         va_end(args);  
         header = ccs_init_audit_log(&len, r);  
2001          if (!header)          if (!header)
2002                  goto out;                  goto out;
2003          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2004          if (!ccs_query_entry)          if (!entry)
2005                  goto out;                  goto out;
2006          len = ccs_round2(len);          len = ccs_round2(len);
2007          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
2008          if (!ccs_query_entry->query)          if (!entry->query)
2009                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
2010          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2011          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2012              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
2013                  quota_exceeded = true;                  quota_exceeded = true;
2014          } else {          } else {
2015                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
2016                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
2017          }          }
2018          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2019          if (quota_exceeded)          if (quota_exceeded)
2020                  goto out;                  goto out;
2021          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2022                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
2023          kfree(header);          kfree(header);
2024          header = NULL;          header = NULL;
2025          va_start(args, fmt);          va_start(args, fmt);
2026          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2027          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
2028          va_end(args);          va_end(args);
2029          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2030          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2031          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2032          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2033          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2034               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2035               ccs_query_entry->timer++) {               entry->timer++) {
2036                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2037                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2038                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2039                  if (ccs_query_entry->answer)                  if (entry->answer)
2040                          break;                          break;
2041          }          }
2042          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2043          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2044          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2045          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2046          switch (ccs_query_entry->answer) {          switch (entry->answer) {
2047          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2048                  error = 1;                  error = CCS_RETRY_REQUEST;
2049                  r->retry++;                  r->retry++;
2050                  break;                  break;
2051          case 1:          case 1:
# Line 1995  int ccs_supervisor(struct ccs_request_in Line 2060  int ccs_supervisor(struct ccs_request_in
2060                  break;                  break;
2061          }          }
2062   out:   out:
2063          if (ccs_query_entry)          if (entry)
2064                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2065          kfree(ccs_query_entry);          kfree(entry);
2066          kfree(header);          kfree(header);
2067          return error;          return error;
2068  }  }
# Line 2020  static int ccs_poll_query(struct file *f Line 2085  static int ccs_poll_query(struct file *f
2085          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2086                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2087                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2088                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2089                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2090                          if (ptr->answer)                          if (ptr->answer)
2091                                  continue;                                  continue;
2092                          found = true;                          found = true;
# Line 2041  static int ccs_poll_query(struct file *f Line 2106  static int ccs_poll_query(struct file *f
2106   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2107   *   *
2108   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
2109   */   */
2110  static int ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2111  {  {
2112          struct list_head *tmp;          struct list_head *tmp;
2113          int pos = 0;          int pos = 0;
2114          int len = 0;          int len = 0;
2115          char *buf;          char *buf;
2116          if (head->read_avail)          if (head->r.w_pos)
2117                  return 0;                  return;
2118          if (head->read_buf) {          if (head->read_buf) {
2119                  kfree(head->read_buf);                  kfree(head->read_buf);
2120                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2121          }          }
2122          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2123          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2124                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2125                  if (ptr->answer)                  if (ptr->answer)
2126                          continue;                          continue;
2127                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2128                          continue;                          continue;
2129                  len = ptr->query_len;                  len = ptr->query_len;
2130                  break;                  break;
2131          }          }
2132          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2133          if (!len) {          if (!len) {
2134                  head->read_step = 0;                  head->r.query_index = 0;
2135                  return 0;                  return;
2136          }          }
2137          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2138          if (!buf)          if (!buf)
2139                  return 0;                  return;
2140          pos = 0;          pos = 0;
2141          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2142          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2143                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2144                  if (ptr->answer)                  if (ptr->answer)
2145                          continue;                          continue;
2146                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2147                          continue;                          continue;
2148                  /*                  /*
2149                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2095  static int ccs_read_query(struct ccs_io_ Line 2155  static int ccs_read_query(struct ccs_io_
2155          }          }
2156          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2157          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2158                  head->read_buf = buf;                  head->read_buf = buf;
2159                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2160                    head->r.query_index++;
2161          } else {          } else {
2162                  kfree(buf);                  kfree(buf);
2163          }          }
         return 0;  
2164  }  }
2165    
2166  /**  /**
# Line 2120  static int ccs_write_answer(struct ccs_i Line 2178  static int ccs_write_answer(struct ccs_i
2178          unsigned int answer;          unsigned int answer;
2179          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2180          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2181                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2182                  ptr->timer = 0;                  ptr->timer = 0;
2183          }          }
2184          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2129  static int ccs_write_answer(struct ccs_i Line 2186  static int ccs_write_answer(struct ccs_i
2186                  return -EINVAL;                  return -EINVAL;
2187          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2188          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {