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

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

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