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

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

trunk/1.7.x/ccs-patch/security/ccsecurity/policy_io.c revision 3109 by kumaneko, Fri Oct 16 05:02:23 2009 UTC branches/ccs-patch/security/ccsecurity/policy_io.c revision 3871 by kumaneko, Sun Aug 1 01:42:05 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.1-pre   2009/10/16   * Version: 1.8.0-pre   2010/08/01
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 13  Line 13 
13  #include "internal.h"  #include "internal.h"
14    
15  static struct ccs_profile ccs_default_profile = {  static struct ccs_profile ccs_default_profile = {
         .learning = &ccs_default_profile.preference,  
         .permissive = &ccs_default_profile.preference,  
         .enforcing = &ccs_default_profile.preference,  
         .audit = &ccs_default_profile.preference,  
16  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
17          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
18          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
# Line 24  static struct ccs_profile ccs_default_pr Line 20  static struct ccs_profile ccs_default_pr
20          .preference.audit_task_info = true,          .preference.audit_task_info = true,
21          .preference.audit_path_info = true,          .preference.audit_path_info = true,
22          .preference.enforcing_penalty = 0,          .preference.enforcing_penalty = 0,
         .preference.enforcing_verbose = true,  
23          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
         .preference.learning_verbose = false,  
24          .preference.learning_exec_realpath = true,          .preference.learning_exec_realpath = true,
25          .preference.learning_exec_argv0 = true,          .preference.learning_exec_argv0 = true,
26          .preference.learning_symlink_target = true,          .preference.learning_symlink_target = true,
         .preference.permissive_verbose = true  
27  };  };
28    
29    /* Profile version. Currently only 20090903 is defined. */
30    static unsigned int ccs_profile_version;
31    
32  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
33  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
34    
 /* Lock for protecting "struct ccs_profile"->comment  */  
 static DEFINE_SPINLOCK(ccs_profile_comment_lock);  
   
35  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
36  static const char *ccs_mode_4[4] = {  const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {
37          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED] = "disabled",
38            [CCS_CONFIG_LEARNING] = "learning",
39            [CCS_CONFIG_PERMISSIVE] = "permissive",
40            [CCS_CONFIG_ENFORCING] = "enforcing"
41  };  };
42    
43  /* String table for /proc/ccs/profile */  /* String table for /proc/ccs/profile */
# Line 68  static const char *ccs_mac_keywords[CCS_ Line 64  static const char *ccs_mac_keywords[CCS_
64          = "file::truncate",          = "file::truncate",
65          [CCS_MAC_FILE_SYMLINK]          [CCS_MAC_FILE_SYMLINK]
66          = "file::symlink",          = "file::symlink",
         [CCS_MAC_FILE_REWRITE]  
         = "file::rewrite",  
67          [CCS_MAC_FILE_MKBLOCK]          [CCS_MAC_FILE_MKBLOCK]
68          = "file::mkblock",          = "file::mkblock",
69          [CCS_MAC_FILE_MKCHAR]          [CCS_MAC_FILE_MKCHAR]
# Line 94  static const char *ccs_mac_keywords[CCS_ Line 88  static const char *ccs_mac_keywords[CCS_
88          = "file::umount",          = "file::umount",
89          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_FILE_PIVOT_ROOT]
90          = "file::pivot_root",          = "file::pivot_root",
91            [CCS_MAC_FILE_TRANSIT]
92            = "file::transit",
93          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
94          = "misc::env",          = "misc::env",
         [CCS_MAC_NETWORK_UDP_BIND]  
         = "network::inet_udp_bind",  
         [CCS_MAC_NETWORK_UDP_CONNECT]  
         = "network::inet_udp_connect",  
95          [CCS_MAC_NETWORK_TCP_BIND]          [CCS_MAC_NETWORK_TCP_BIND]
96          = "network::inet_tcp_bind",          = "network::inet_tcp_bind",
97          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_MAC_NETWORK_TCP_LISTEN]
# Line 108  static const char *ccs_mac_keywords[CCS_ Line 100  static const char *ccs_mac_keywords[CCS_
100          = "network::inet_tcp_connect",          = "network::inet_tcp_connect",
101          [CCS_MAC_NETWORK_TCP_ACCEPT]          [CCS_MAC_NETWORK_TCP_ACCEPT]
102          = "network::inet_tcp_accept",          = "network::inet_tcp_accept",
103            [CCS_MAC_NETWORK_UDP_BIND]
104            = "network::inet_udp_bind",
105            [CCS_MAC_NETWORK_UDP_SEND]
106            = "network::inet_udp_send",
107            [CCS_MAC_NETWORK_UDP_RECV]
108            = "network::inet_udp_recv",
109          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_MAC_NETWORK_RAW_BIND]
110          = "network::inet_raw_bind",          = "network::inet_raw_bind",
111          [CCS_MAC_NETWORK_RAW_CONNECT]          [CCS_MAC_NETWORK_RAW_SEND]
112          = "network::inet_raw_connect",          = "network::inet_raw_send",
113            [CCS_MAC_NETWORK_RAW_RECV]
114            = "network::inet_raw_recv",
115          [CCS_MAC_SIGNAL]          [CCS_MAC_SIGNAL]
116          = "ipc::signal",          = "ipc::signal",
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  
         = "capability::inet_tcp_create",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]  
         = "capability::inet_tcp_listen",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]  
         = "capability::inet_tcp_connect",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]  
         = "capability::use_inet_udp",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]  
         = "capability::use_inet_ip",  
117          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
118          = "capability::use_route",          = "capability::use_route",
119          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
120          = "capability::use_packet",          = "capability::use_packet",
         [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]  
         = "capability::SYS_MOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]  
         = "capability::SYS_UMOUNT",  
121          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
122          = "capability::SYS_REBOOT",          = "capability::SYS_REBOOT",
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]  
         = "capability::SYS_CHROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]  
         = "capability::SYS_KILL",  
123          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
124          = "capability::SYS_VHANGUP",          = "capability::SYS_VHANGUP",
125          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
# Line 148  static const char *ccs_mac_keywords[CCS_ Line 130  static const char *ccs_mac_keywords[CCS_
130          = "capability::SYS_SETHOSTNAME",          = "capability::SYS_SETHOSTNAME",
131          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
132          = "capability::use_kernel_module",          = "capability::use_kernel_module",
         [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]  
         = "capability::create_fifo",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]  
         = "capability::create_block_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]  
         = "capability::create_char_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]  
         = "capability::create_unix_socket",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]  
         = "capability::SYS_LINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]  
         = "capability::SYS_SYMLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]  
         = "capability::SYS_RENAME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]  
         = "capability::SYS_UNLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]  
         = "capability::SYS_CHMOD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]  
         = "capability::SYS_CHOWN",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]  
         = "capability::SYS_IOCTL",  
133          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
134          = "capability::SYS_KEXEC_LOAD",          = "capability::SYS_KEXEC_LOAD",
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
135          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
136          = "capability::SYS_PTRACE",          = "capability::SYS_PTRACE",
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
137          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
138           + CCS_MAC_CATEGORY_FILE] = "file",           + CCS_MAC_CATEGORY_FILE] = "file",
139          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
# Line 216  static const char *ccs_yesno(const unsig Line 172  static const char *ccs_yesno(const unsig
172          return value ? "yes" : "no";          return value ? "yes" : "no";
173  }  }
174    
175    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
176    {
177            va_list args;
178            const int pos = strlen(buffer);
179            va_start(args, fmt);
180            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
181            va_end(args);
182    }
183    
184  /**  /**
185   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
186   *   *
187   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
188   *   *
189   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
190     */
191    static bool ccs_flush(struct ccs_io_buffer *head)
192    {
193            while (head->r.w_pos) {
194                    const char *w = head->r.w[0];
195                    int len = strlen(w);
196                    if (len) {
197                            if (len > head->read_user_buf_avail)
198                                    len = head->read_user_buf_avail;
199                            if (!len)
200                                    return false;
201                            if (copy_to_user(head->read_user_buf, w, len))
202                                    return false;
203                            head->read_user_buf_avail -= len;
204                            head->read_user_buf += len;
205                            w += len;
206                    }
207                    if (*w) {
208                            head->r.w[0] = w;
209                            return false;
210                    }
211                    /* Add '\0' for audit logs and query. */
212                    if (head->poll) {
213                            if (!head->read_user_buf_avail ||
214                                copy_to_user(head->read_user_buf, "", 1))
215                                    return false;
216                            head->read_user_buf_avail--;
217                            head->read_user_buf++;
218                    }
219                    head->r.w_pos--;
220                    for (len = 0; len < head->r.w_pos; len++)
221                            head->r.w[len] = head->r.w[len + 1];
222            }
223            head->r.avail = 0;
224            return true;
225    }
226    
227    /**
228     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
229   *   *
230   * The snprintf() will truncate, but ccs_io_printf() won't.   * @head:   Pointer to "struct ccs_io_buffer".
231     * @string: String to print.
232     *
233     * Note that @string has to be kept valid until @head is kfree()d.
234     * This means that char[] allocated on stack memory cannot be passed to
235     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
236   */   */
237  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
238    {
239            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
240                    head->r.w[head->r.w_pos++] = string;
241                    ccs_flush(head);
242            } else
243                    printk(KERN_WARNING "Too many words in a line.\n");
244    }
245    
246    /**
247     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
248     *
249     * @head: Pointer to "struct ccs_io_buffer".
250     * @fmt:  The printf()'s format string, followed by parameters.
251     */
252    void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
253  {  {
254          va_list args;          va_list args;
255          int len;          int len;
256          int pos = head->read_avail;          int pos = head->r.avail;
257          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
258          if (size <= 0)          if (size <= 0)
259                  return false;                  return;
260          va_start(args, fmt);          va_start(args, fmt);
261          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
262          va_end(args);          va_end(args);
263          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
264                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
265          head->read_avail += len;                  return;
266          return true;          }
267            head->r.avail += len;
268            ccs_set_string(head, head->read_buf + pos);
269    }
270    
271    static void ccs_set_space(struct ccs_io_buffer *head)
272    {
273            ccs_set_string(head, " ");
274    }
275    
276    static bool ccs_set_lf(struct ccs_io_buffer *head)
277    {
278            ccs_set_string(head, "\n");
279            return !head->r.w_pos;
280  }  }
281    
282  /**  /**
283   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
284   *   *
285   * @profile: Profile number to create.   * @profile: Profile number to create.
286   *   *
287   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
288   */   */
289  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)  
290  {  {
291          struct ccs_profile *ptr;          struct ccs_profile *ptr;
292          struct ccs_profile *entry;          struct ccs_profile *entry;
# Line 260  static struct ccs_profile *ccs_find_or_a Line 295  static struct ccs_profile *ccs_find_or_a
295          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
296          if (ptr)          if (ptr)
297                  return ptr;                  return ptr;
298          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
299          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
300                    goto out;
301          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
302          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
303                  ptr = entry;                  ptr = entry;
                 ptr->audit = &ccs_default_profile.preference;  
                 ptr->learning = &ccs_default_profile.preference;  
                 ptr->permissive = &ccs_default_profile.preference;  
                 ptr->enforcing = &ccs_default_profile.preference;  
304                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
305                            CCS_CONFIG_VERBOSE |
306                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
307                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
308                         sizeof(ptr->config));                         sizeof(ptr->config));
# Line 278  static struct ccs_profile *ccs_find_or_a Line 311  static struct ccs_profile *ccs_find_or_a
311                  entry = NULL;                  entry = NULL;
312          }          }
313          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
314     out:
315          kfree(entry);          kfree(entry);
316          return ptr;          return ptr;
317  }  }
# Line 285  static struct ccs_profile *ccs_find_or_a Line 319  static struct ccs_profile *ccs_find_or_a
319  /**  /**
320   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
321   */   */
322  void ccs_check_profile(void)  static void ccs_check_profile(void)
323  {  {
324          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
325            const int idx = ccs_read_lock();
326          ccs_policy_loaded = true;          ccs_policy_loaded = true;
327          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
328                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
# Line 296  void ccs_check_profile(void) Line 331  void ccs_check_profile(void)
331                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
332                        profile, domain->domainname->name);                        profile, domain->domainname->name);
333          }          }
334            ccs_read_unlock(idx);
335            if (ccs_profile_version != 20090903)
336                    panic("Profile version %u is not supported.\n",
337                          ccs_profile_version);
338            printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/08/01\n");
339            printk(KERN_INFO "Mandatory Access Control activated.\n");
340  }  }
341    
342  /**  /**
# Line 314  struct ccs_profile *ccs_profile(const u8 Line 355  struct ccs_profile *ccs_profile(const u8
355          return ptr;          return ptr;
356  }  }
357    
358  /**  static s8 ccs_find_yesno(const char *string, const char *find)
  * ccs_write_profile - Write profile table.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_write_profile(struct ccs_io_buffer *head)  
359  {  {
360          char *data = head->write_buf;          const char *cp = strstr(string, find);
361          unsigned int i;          if (cp) {
362          int value;                  cp += strlen(find);
363          int mode;                  if (!strncmp(cp, "=yes", 4))
364          u8 config;                          return 1;
365          bool use_default = false;                  else if (!strncmp(cp, "=no", 3))
         char *cp;  
         struct ccs_profile *profile;  
         i = simple_strtoul(data, &cp, 10);  
         if (data == cp) {  
                 profile = &ccs_default_profile;  
         } else {  
                 if (*cp != '-')  
                         return -EINVAL;  
                 data = cp + 1;  
                 profile = ccs_find_or_assign_new_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
         }  
         cp = strchr(data, '=');  
         if (!cp)  
                 return -EINVAL;  
         *cp++ = '\0';  
         if (profile != &ccs_default_profile)  
                 use_default = strstr(cp, "use_default") != NULL;  
         if (strstr(cp, "verbose=yes"))  
                 value = 1;  
         else if (strstr(cp, "verbose=no"))  
                 value = 0;  
         else  
                 value = -1;  
         if (!strcmp(data, "PREFERENCE::audit")) {  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 char *cp2;  
 #endif  
                 if (use_default) {  
                         profile->audit = &ccs_default_profile.preference;  
366                          return 0;                          return 0;
                 }  
                 profile->audit = &profile->preference;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 cp2 = strstr(cp, "max_grant_log=");  
                 if (cp2)  
                         sscanf(cp2 + 14, "%u",  
                                &profile->preference.audit_max_grant_log);  
                 cp2 = strstr(cp, "max_reject_log=");  
                 if (cp2)  
                         sscanf(cp2 + 15, "%u",  
                                &profile->preference.audit_max_reject_log);  
 #endif  
                 if (strstr(cp, "task_info=yes"))  
                         profile->preference.audit_task_info = true;  
                 else if (strstr(cp, "task_info=no"))  
                         profile->preference.audit_task_info = false;  
                 if (strstr(cp, "path_info=yes"))  
                         profile->preference.audit_path_info = true;  
                 else if (strstr(cp, "path_info=no"))  
                         profile->preference.audit_path_info = false;  
                 return 0;  
367          }          }
368          if (!strcmp(data, "PREFERENCE::enforcing")) {          return -1;
369                  char *cp2;  }
370                  if (use_default) {  
371                          profile->enforcing = &ccs_default_profile.preference;  static void ccs_set_bool(bool *b, const char *string, const char *find)
372                          return 0;  {
373                  }          switch (ccs_find_yesno(string, find)) {
374                  profile->enforcing = &profile->preference;          case 1:
375                  if (value >= 0)                  *b = true;
376                          profile->preference.enforcing_verbose = value;                  break;
377                  cp2 = strstr(cp, "penalty=");          case 0:
378                  if (cp2)                  *b = false;
379                          sscanf(cp2 + 8, "%u",                  break;
                                &profile->preference.enforcing_penalty);  
                 return 0;  
380          }          }
381          if (!strcmp(data, "PREFERENCE::permissive")) {  }
382                  if (use_default) {  
383                          profile->permissive = &ccs_default_profile.preference;  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
384                          return 0;  {
385                  }          const char *cp = strstr(string, find);
386                  profile->permissive = &profile->preference;          if (cp)
387                  if (value >= 0)                  sscanf(cp + strlen(find), "=%u", i);
388                          profile->preference.permissive_verbose = value;  }
389                  return 0;  
390    static void ccs_set_pref(const char *name, const char *value,
391                             struct ccs_profile *profile)
392    {
393            if (!strcmp(name, "audit")) {
394    #ifdef CONFIG_CCSECURITY_AUDIT
395                    ccs_set_uint(&profile->preference.audit_max_grant_log, value,
396                                 "max_grant_log");
397                    ccs_set_uint(&profile->preference.audit_max_reject_log, value,
398                                 "max_reject_log");
399    #endif
400                    ccs_set_bool(&profile->preference.audit_task_info, value,
401                                 "task_info");
402                    ccs_set_bool(&profile->preference.audit_path_info, value,
403                                 "path_info");
404                    return;
405          }          }
406          if (!strcmp(data, "PREFERENCE::learning")) {          if (!strcmp(name, "enforcing")) {
407                  char *cp2;                  ccs_set_uint(&profile->preference.enforcing_penalty, value,
408                  if (use_default) {                               "penalty");
409                          profile->learning = &ccs_default_profile.preference;                  return;
                         return 0;  
                 }  
                 profile->learning = &profile->preference;  
                 if (value >= 0)  
                         profile->preference.learning_verbose = value;  
                 cp2 = strstr(cp, "max_entry=");  
                 if (cp2)  
                         sscanf(cp2 + 10, "%u",  
                                &profile->preference.learning_max_entry);  
                 if (strstr(cp, "exec.realpath=yes"))  
                         profile->preference.learning_exec_realpath = true;  
                 else if (strstr(cp, "exec.realpath=no"))  
                         profile->preference.learning_exec_realpath = false;  
                 if (strstr(cp, "exec.argv0=yes"))  
                         profile->preference.learning_exec_argv0 = true;  
                 else if (strstr(cp, "exec.argv0=no"))  
                         profile->preference.learning_exec_argv0 = false;  
                 if (strstr(cp, "symlink.target=yes"))  
                         profile->preference.learning_symlink_target = true;  
                 else if (strstr(cp, "symlink.target=no"))  
                         profile->preference.learning_symlink_target = false;  
                 return 0;  
410          }          }
411          if (profile == &ccs_default_profile)          if (!strcmp(name, "learning")) {
412                  return -EINVAL;                  ccs_set_uint(&profile->preference.learning_max_entry, value,
413          if (!strcmp(data, "COMMENT")) {                               "max_entry");
414                  const struct ccs_path_info *new_comment = ccs_get_name(cp);                  ccs_set_bool(&profile->preference.learning_exec_realpath,
415                  const struct ccs_path_info *old_comment;                               value, "exec.realpath");
416                  /* Protect reader from ccs_put_name(). */                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
417                  spin_lock(&ccs_profile_comment_lock);                               "exec.argv0");
418                  old_comment = profile->comment;                  ccs_set_bool(&profile->preference.learning_symlink_target,
419                  profile->comment = new_comment;                               value, "symlink.target");
420                  spin_unlock(&ccs_profile_comment_lock);                  return;
                 ccs_put_name(old_comment);  
                 return 0;  
421          }          }
422          if (!strcmp(data, "CONFIG")) {  }
423    
424    static int ccs_set_mode(char *name, const char *value,
425                            struct ccs_profile *profile)
426    {
427            u8 i;
428            u8 config;
429            if (!strcmp(name, "CONFIG")) {
430                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
431                          + CCS_MAX_MAC_CATEGORY_INDEX;                          + CCS_MAX_MAC_CATEGORY_INDEX;
432                  config = profile->default_config;                  config = profile->default_config;
433          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
434                  config = 0;                  config = 0;
435                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
436                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
437                          if (strcmp(data, ccs_mac_keywords[i]))                          if (strcmp(name, ccs_mac_keywords[i]))
438                                  continue;                                  continue;
439                          config = profile->config[i];                          config = profile->config[i];
440                          break;                          break;
# Line 467  static int ccs_write_profile(struct ccs_ Line 445  static int ccs_write_profile(struct ccs_
445          } else {          } else {
446                  return -EINVAL;                  return -EINVAL;
447          }          }
448          if (use_default) {          if (strstr(value, "use_default")) {
449                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
450          } else {          } else {
451                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
452                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
453                            if (strstr(value, ccs_mode[mode]))
454                                  /*                                  /*
455                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
456                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
457                                   */                                   */
458                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
459                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
460                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "verbose")) {
461                            case 1:
462                                    config |= CCS_CONFIG_VERBOSE;
463                                    break;
464                            case 0:
465                                    config &= ~CCS_CONFIG_VERBOSE;
466                                    break;
467                            }
468    #ifdef CONFIG_CCSECURITY_AUDIT
469                            switch (ccs_find_yesno(value, "grant_log")) {
470                            case 1:
471                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
472                          else if (strstr(cp, "grant_log=no"))                                  break;
473                            case 0:
474                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
475                          if (strstr(cp, "reject_log=yes"))                                  break;
476                            }
477                            switch (ccs_find_yesno(value, "reject_log")) {
478                            case 1:
479                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
480                          else if (strstr(cp, "reject_log=no"))                                  break;
481                            case 0:
482                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
483                  }                                  break;
484                            }
485  #endif  #endif
486                    }
487          }          }
488          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
489              + CCS_MAX_MAC_CATEGORY_INDEX)              + CCS_MAX_MAC_CATEGORY_INDEX)
# Line 499  static int ccs_write_profile(struct ccs_ Line 494  static int ccs_write_profile(struct ccs_
494  }  }
495    
496  /**  /**
497   * ccs_read_profile - Read profile table.   * ccs_write_profile - Write profile table.
498   *   *
499   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
500     *
501     * Returns 0 on success, negative value otherwise.
502   */   */
503  static void ccs_read_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
504  {  {
505          int index;          char *data = head->write_buf;
506          if (head->read_eof)          unsigned int i;
507                  return;          char *cp;
508          if (head->read_bit)          struct ccs_profile *profile;
509                  goto body;          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
510          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");                  return 0;
511          ccs_io_printf(head, "PREFERENCE::audit={ "          i = simple_strtoul(data, &cp, 10);
512            if (*cp != '-')
513                    return -EINVAL;
514            data = cp + 1;
515            profile = ccs_assign_profile(i);
516            if (!profile)
517                    return -EINVAL;
518            cp = strchr(data, '=');
519            if (!cp)
520                    return -EINVAL;
521            *cp++ = '\0';
522            if (ccs_str_starts(&data, "PREFERENCE::")) {
523                    ccs_set_pref(data, cp, profile);
524                    return 0;
525            }
526            if (!strcmp(data, "COMMENT")) {
527                    const struct ccs_path_info *old_comment = profile->comment;
528                    profile->comment = ccs_get_name(cp);
529                    ccs_put_name(old_comment);
530                    return 0;
531            }
532            return ccs_set_mode(data, cp, profile);
533    }
534    
535    static void ccs_print_preference(struct ccs_io_buffer *head, const int index)
536    {
537            struct ccs_profile *profile = ccs_profile_ptr[index];
538            struct ccs_preference *pref = &profile->preference;
539            ccs_io_printf(head, "%u-PREFERENCE::%s={ "
540  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
541                        "max_grant_log=%u max_reject_log=%u "                        "max_grant_log=%u max_reject_log=%u "
542  #endif  #endif
543                        "task_info=%s path_info=%s }\n",                        "task_info=%s path_info=%s }\n", index,
544                          "audit",
545  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
546                        ccs_default_profile.preference.audit_max_grant_log,                        pref->audit_max_grant_log,
547                        ccs_default_profile.preference.audit_max_reject_log,                        pref->audit_max_reject_log,
548  #endif  #endif
549                        ccs_yesno(ccs_default_profile.preference.                        ccs_yesno(pref->audit_task_info),
550                                  audit_task_info),                        ccs_yesno(pref->audit_path_info));
551                        ccs_yesno(ccs_default_profile.preference.          ccs_io_printf(head, "%u-PREFERENCE::%s={ "
552                                  audit_path_info));                        "max_entry=%u exec.realpath=%s "
553          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "                        "exec.argv0=%s symlink.target=%s }\n",
554                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",                        index, "learning",
555                        ccs_yesno(ccs_default_profile.preference.                        pref->learning_max_entry,
556                                  learning_verbose),                        ccs_yesno(pref->learning_exec_realpath),
557                        ccs_default_profile.preference.learning_max_entry,                        ccs_yesno(pref->learning_exec_argv0),
558                        ccs_yesno(ccs_default_profile.preference.                        ccs_yesno(pref->learning_symlink_target));
559                                  learning_exec_realpath),          ccs_io_printf(head, "%u-PREFERENCE::%s={ penalty=%u }\n", index,
560                        ccs_yesno(ccs_default_profile.preference.                        "enforcing", pref->enforcing_penalty);
561                                  learning_exec_argv0),  }
562                        ccs_yesno(ccs_default_profile.preference.  
563                                  learning_symlink_target));  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
564          ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",  {
565                        ccs_yesno(ccs_default_profile.preference.          ccs_io_printf(head, "={ mode=%s verbose=%s", ccs_mode[config & 3],
566                                  permissive_verbose));                        ccs_yesno(config & CCS_CONFIG_VERBOSE));
         ccs_io_printf(head, "PREFERENCE::enforcing={ verbose=%s penalty=%u "  
                       "}\n",  
                       ccs_yesno(ccs_default_profile.preference.  
                                 enforcing_verbose),  
                       ccs_default_profile.preference.enforcing_penalty);  
         head->read_bit = 1;  
  body:  
         for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {  
                 bool done;  
                 u8 config;  
                 int i;  
                 int pos;  
                 const struct ccs_profile *profile = ccs_profile_ptr[index];  
                 head->read_step = index;  
                 if (!profile)  
                         continue;  
                 pos = head->read_avail;  
                 spin_lock(&ccs_profile_comment_lock);  
                 done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,  
                                      profile->comment ? profile->comment->name  
                                      : "");  
                 spin_unlock(&ccs_profile_comment_lock);  
                 if (!done)  
                         goto out;  
                 config = profile->default_config;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "  
                                    "reject_log=%s }\n", index,  
                                    ccs_mode_4[config & 3],  
                                    ccs_yesno(config &  
                                              CCS_CONFIG_WANT_GRANT_LOG),  
                                    ccs_yesno(config &  
                                              CCS_CONFIG_WANT_REJECT_LOG)))  
                         goto out;  
 #else  
                 if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,  
                                    ccs_mode_4[config & 3]))  
                         goto out;  
 #endif  
                 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
                              + CCS_MAX_MAC_CATEGORY_INDEX; i++) {  
567  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
568                          const char *g;          ccs_io_printf(head, " grant_log=%s reject_log=%s",
569                          const char *r;                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
570                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
571  #endif  #endif
572                          config = profile->config[i];          ccs_set_string(head, " }\n");
573    }
574    
575    /**
576     * ccs_read_profile - Read profile table.
577     *
578     * @head: Pointer to "struct ccs_io_buffer".
579     */
580    static void ccs_read_profile(struct ccs_io_buffer *head)
581    {
582            u8 index;
583            const struct ccs_profile *profile;
584     next:
585            index = head->r.index;
586            profile = ccs_profile_ptr[index];
587            switch (head->r.step) {
588            case 0:
589                    ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
590                    head->r.step++;
591                    break;
592            case 1:
593                    for ( ; head->r.index < CCS_MAX_PROFILES;
594                          head->r.index++)
595                            if (ccs_profile_ptr[head->r.index])
596                                    break;
597                    if (head->r.index == CCS_MAX_PROFILES)
598                            return;
599                    head->r.step++;
600                    break;
601            case 2:
602                    {
603                            const struct ccs_path_info *comment = profile->comment;
604                            ccs_io_printf(head, "%u-COMMENT=", index);
605                            ccs_set_string(head, comment ? comment->name : "");
606                            ccs_set_lf(head);
607                            head->r.step++;
608                    }
609                    break;
610            case 3:
611                    {
612                            ccs_io_printf(head, "%u-%s", index, "CONFIG");
613                            ccs_print_config(head, profile->default_config);
614                            head->r.bit = 0;
615                            head->r.step++;
616                    }
617                    break;
618            case 4:
619                    for ( ; head->r.bit < CCS_MAX_MAC_INDEX
620                                  + CCS_MAX_CAPABILITY_INDEX
621                                  + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
622                            const u8 i = head->r.bit;
623                            const u8 config = profile->config[i];
624                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
625                                  continue;                                  continue;
626  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
627                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                        ccs_mac_keywords[i]);
628                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          ccs_print_config(head, config);
629                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                          head->r.bit++;
630                                             "grant_log=%s reject_log=%s }\n",                          break;
631                                             index, ccs_mac_keywords[i],                  }
632                                             ccs_mode_4[config & 3], g, r))                  if (head->r.bit == CCS_MAX_MAC_INDEX
633                                  goto out;                      + CCS_MAX_CAPABILITY_INDEX
634  #else                      + CCS_MAX_MAC_CATEGORY_INDEX) {
635                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          ccs_print_preference(head, index);
636                                             index, ccs_mac_keywords[i],                          head->r.index++;
637                                             ccs_mode_4[config & 3]))                          head->r.step = 1;
                                 goto out;  
 #endif  
638                  }                  }
                 if (profile->audit != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::audit={ "  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    "max_grant_log=%u max_reject_log=%u "  
 #endif  
                                    "task_info=%s path_info=%s }\n", index,  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    profile->preference.audit_max_grant_log,  
                                    profile->preference.audit_max_reject_log,  
 #endif  
                                    ccs_yesno(profile->preference.  
                                              audit_task_info),  
                                    ccs_yesno(profile->preference.  
                                              audit_path_info)))  
                         goto out;  
                 if (profile->learning != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::learning={ "  
                                    "verbose=%s max_entry=%u exec.realpath=%s "  
                                    "exec.argv0=%s symlink.target=%s }\n",  
                                    index,  
                                    ccs_yesno(profile->preference.  
                                              learning_verbose),  
                                    profile->preference.learning_max_entry,  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_realpath),  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_argv0),  
                                    ccs_yesno(profile->preference.  
                                              learning_symlink_target)))  
                         goto out;  
                 if (profile->permissive != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::permissive={ "  
                                    "verbose=%s }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              permissive_verbose)))  
                         goto out;  
                 if (profile->enforcing != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::enforcing={ "  
                                    "verbose=%s penalty=%u }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              enforcing_verbose),  
                                    profile->preference.enforcing_penalty))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
639                  break;                  break;
640          }          }
641          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
642                  head->read_eof = true;                  goto next;
643  }  }
644    
645  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager(const struct ccs_acl_head *a,
646  LIST_HEAD(ccs_policy_manager_list);                               const struct ccs_acl_head *b)
647    {
648            return container_of(a, struct ccs_manager, head)->manager
649                    == container_of(b, struct ccs_manager, head)->manager;
650    }
651    
652  /**  /**
653   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 666  LIST_HEAD(ccs_policy_manager_list); Line 659  LIST_HEAD(ccs_policy_manager_list);
659   */   */
660  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)
661  {  {
662          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
663          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
664          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
665                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
666                          return -EINVAL;                          return -EINVAL;
667                  e.is_domain = true;                  e.is_domain = true;
668          } else {          } else {
669                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
670                          return -EINVAL;                          return -EINVAL;
671          }          }
672          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
673          if (!e.manager)          if (!e.manager)
674                  return -ENOMEM;                  return error;
675          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
676                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
677          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);  
678          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
679          return error;          return error;
680  }  }
681    
682  /**  /**
683   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
684   *   *
685   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
686   *   *
687   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
688   */   */
689  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
690  {  {
691          char *data = head->write_buf;          char *data = head->write_buf;
692          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
# Line 721  static int ccs_write_manager_policy(stru Line 698  static int ccs_write_manager_policy(stru
698  }  }
699    
700  /**  /**
701   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
702   *   *
703   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
704   *   *
705   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
706   */   */
707  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
708  {  {
709          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
710                  return;                  return;
711          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
712                  struct ccs_policy_manager_entry *ptr;                  struct ccs_manager *ptr =
713                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                          list_entry(head->r.acl, typeof(*ptr), head.list);
714                  if (ptr->is_deleted)                  if (ptr->head.is_deleted)
715                          continue;                          continue;
716                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
717                          return;                          return;
718                    ccs_set_string(head, ptr->manager->name);
719                    ccs_set_lf(head);
720          }          }
721          head->read_eof = true;          head->r.eof = true;
722  }  }
723    
724  /**  /**
725   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
726   *   *
727   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
728   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
729   *   *
730   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
731   */   */
732  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
733  {  {
734          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
735          const char *exe;          const char *exe;
736          struct task_struct *task = current;          struct task_struct *task = current;
737          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
# Line 761  static bool ccs_is_policy_manager(void) Line 739  static bool ccs_is_policy_manager(void)
739          bool found = false;          bool found = false;
740          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
741                  return true;                  return true;
742          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
743                  return true;                  return true;
744          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
745                  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;  
                 }  
         }  
746          exe = ccs_get_exe();          exe = ccs_get_exe();
747          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
748                  return false;                                  head.list) {
749          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
750                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
751                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
752                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
753                          /* Set manager flag. */                                  continue;
754                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
755                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
756                                    continue;
757                  }                  }
758                    /* Set manager flag. */
759                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
760                    found = true;
761                    break;
762          }          }
763          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
764                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 809  static bool ccs_is_policy_manager(void) Line 784  static bool ccs_is_policy_manager(void)
784  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
785  {  {
786          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
787          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
788                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
789                  if (cp)          if (cp)
790                          *cp++ = '\0';                  *cp++ = '\0';
         }  
791          return cp;          return cp;
792  }  }
793    
794  /**  /**
795   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
796   *   *
797   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
798   * @data: String to parse.   * @data: String to parse.
# Line 835  static char *ccs_find_condition_part(cha Line 801  static char *ccs_find_condition_part(cha
801   *   *
802   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
803   */   */
804  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)
805  {  {
806          unsigned int pid;          unsigned int pid;
807          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
808          bool global_pid = false;          bool global_pid = false;
809          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
810                  head->read_execute_only = true;                  head->r.print_execute_only = true;
811                  return true;                  return true;
812          }          }
813          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
814              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
815                  struct task_struct *p;                  struct task_struct *p;
816                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
817  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
818                  if (global_pid)                  if (global_pid)
819                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
820                                                                   &init_pid_ns);
821                  else                  else
822                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
823  #else  #else
824                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
825  #endif  #endif
826                  if (p)                  if (p)
827                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
828                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
829          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
830                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
831                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
832          } else          } else
833                  return false;                  return false;
834          head->write_var1 = domain;          head->w.domain = domain;
835          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
836          if (!head->read_buf)          if (!head->read_buf)
837                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
838          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
839            head->r.print_this_domain_only = true;
840            head->r.eof = !domain;
841            head->r.domain = &domain->list;
842          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
843          head->read_single_domain = true;          if (domain && domain->is_deleted)
844          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");  
         }  
845          return true;          return true;
846  }  }
847    
848  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,
849                                      struct ccs_condition *cond,                               const bool is_delete)
850                                      const bool is_delete)  {
851  {          static const struct {
852          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))                  const char *keyword;
853                  return ccs_write_capability_policy(data, domain, cond,                  int (*write) (char *, struct ccs_domain_info *,
854                                                     is_delete);                                struct ccs_condition *, const bool);
855          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))          } ccs_callback[4] = {
856                  return ccs_write_network_policy(data, domain, cond, is_delete);                  { "network ", ccs_write_network },
857          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                  { "misc ", ccs_write_misc },
858                  return ccs_write_signal_policy(data, domain, cond, is_delete);                  { "capability ", ccs_write_capability },
859          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { "ipc ", ccs_write_ipc },
860                  return ccs_write_env_policy(data, domain, cond, is_delete);          };
861          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
862                  return ccs_write_mount_policy(data, domain, cond, is_delete);                        const bool) = ccs_write_file;
863          return ccs_write_file_policy(data, domain, cond, is_delete);          int error;
864            u8 i;
865            struct ccs_condition *cond = NULL;
866            char *cp = ccs_find_condition_part(data);
867            if (cp) {
868                    cond = ccs_get_condition(cp);
869                    if (!cond)
870                            return -EINVAL;
871            }
872            for (i = 0; i < 4; i++) {
873                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
874                            continue;
875                    write = ccs_callback[i].write;
876                    break;
877            }
878            error = write(data, domain, cond, is_delete);
879            if (cond)
880                    ccs_put_condition(cond);
881            return error;
882  }  }
883    
884    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
885            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
886            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
887    };
888            
889  /**  /**
890   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
891   *   *
892   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
893   *   *
894   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
895   */   */
896  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
897  {  {
898          char *data = head->write_buf;          char *data = head->write_buf;
899          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
900          bool is_delete = false;          bool is_delete = false;
901          bool is_select = false;          bool is_select = false;
902          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
903          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
904                  is_delete = true;                  is_delete = true;
905          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
906                  is_select = true;                  is_select = true;
907          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
908                  return 0;                  return 0;
909          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
910          if (!ccs_is_policy_manager())          if (!ccs_manager())
911                  return -EPERM;                  return -EPERM;
912          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
913                  domain = NULL;                  domain = NULL;
914                  if (is_delete)                  if (is_delete)
915                          ccs_delete_domain(data);                          ccs_delete_domain(data);
916                  else if (is_select)                  else if (is_select)
917                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
918                  else                  else
919                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0);
920                  head->write_var1 = domain;                  head->w.domain = domain;
921                  return 0;                  return 0;
922          }          }
923          if (!domain)          if (!domain)
# Line 953  static int ccs_write_domain_policy(struc Line 929  static int ccs_write_domain_policy(struc
929                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
930                  return 0;                  return 0;
931          }          }
932          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
933                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
934                    domain->group = (u8) profile;
935                  return 0;                  return 0;
936          }          }
937          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
938                  domain->ignore_global_allow_env = !is_delete;                  const char *cp = ccs_dif[profile];
939                    if (strncmp(data, cp, strlen(cp) - 1))
940                            continue;
941                    domain->flags[profile] = !is_delete;
942                  return 0;                  return 0;
943          }          }
944          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;  
945  }  }
946    
947  /**  /**
# Line 978  static int ccs_write_domain_policy(struc Line 949  static int ccs_write_domain_policy(struc
949   *   *
950   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
951   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
952   */   */
953  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
954                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
955  {  {
956          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
957          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
958                  head->read_avail--;                  ccs_set_space(head);
959          if (ptr->is_group)          if (ptr->is_group) {
960                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
961                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
962          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
963  }                  if (cond)
964                            ccs_set_string(head, "\"");
965  /**                  ccs_set_string(head, ptr->filename->name);
966   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
967   *                          ccs_set_string(head, "\"");
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_name_union".  
  *  
  * Returns true on success, false otherwise.  
  */  
 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);  
 }  
   
 /**  
  * ccs_print_number_union_common - Print a ccs_number_union.  
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
  * @need_space: True if a space character is needed.  
  *  
  * Returns true on success, false otherwise.  
  */  
 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);  
968          }          }
969  }  }
970    
971  /**  /**
972   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
973   *   *
974   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
975   * @ptr:        Pointer to "struct ccs_number_union".   * @ptr:  Pointer to "struct ccs_number_union".
  *  
  * Returns true on success, false otherwise.  
  */  
 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);  
 }  
   
 /**  
  * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.  
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
  *  
  * Returns true on success, false otherwise.  
976   */   */
977  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
978                                             const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
979  {  {
980          return ccs_print_number_union_common(head, ptr, false);          if (!head->r.print_cond_part)
981                    ccs_set_space(head);
982            if (ptr->is_group) {
983                    ccs_set_string(head, "@");
984                    ccs_set_string(head, ptr->group->group_name->name);
985            } else {
986                    int i;
987                    unsigned long min = ptr->values[0];
988                    const unsigned long max = ptr->values[1];
989                    u8 min_type = ptr->value_type[0];
990                    const u8 max_type = ptr->value_type[1];
991                    char buffer[128];
992                    buffer[0] = '\0';
993                    for (i = 0; i < 2; i++) {
994                            switch (min_type) {
995                            case CCS_VALUE_TYPE_HEXADECIMAL:
996                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
997                                                  min);
998                                    break;
999                            case CCS_VALUE_TYPE_OCTAL:
1000                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1001                                                  min);
1002                                    break;
1003                            default:
1004                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1005                                                  min);
1006                                    break;
1007                            }
1008                            if (min == max && min_type == max_type)
1009                                    break;
1010                            ccs_addprintf(buffer, sizeof(buffer), "-");
1011                            min_type = max_type;
1012                            min = max;
1013                    }
1014                    ccs_io_printf(head, "%s", buffer);
1015            }
1016  }  }
1017    
1018  /**  /**
1019   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1020   *   *
1021   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1022   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1023   *   *
1024   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1025   */   */
1026  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1027                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1028  {  {
1029          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1030          const struct ccs_number_union *numbers_p;          case 0:
1031          const struct ccs_name_union *names_p;                  {
1032          const struct ccs_argv_entry *argv;                          if (cond->condc)
1033          const struct ccs_envp_entry *envp;                                  ccs_set_string(head, " if");
1034          u16 condc;                          head->r.cond_index = 0;
1035          u16 i;                          head->r.cond_step++;
1036          u16 j;                  }
1037          char buffer[32];                  /* fall through */
1038          if (!cond)          case 1:
1039                  goto no_condition;                  {
1040          condc = cond->condc;                          const u16 condc = cond->condc;
1041          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_condition_element *condp =
1042          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(condp)) (cond + 1);
1043          names_p = (const struct ccs_name_union *)                          const struct ccs_number_union *numbers_p =
1044                  (numbers_p + cond->numbers_count);                                  (typeof(numbers_p)) (condp + condc);
1045          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                          const struct ccs_name_union *names_p =
1046          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                                  (typeof(names_p))
1047          memset(buffer, 0, sizeof(buffer));                                  (numbers_p + cond->numbers_count);
1048          if (condc && !ccs_io_printf(head, "%s", " if"))                          const struct ccs_argv *argv =
1049                  goto out;                                  (typeof(argv)) (names_p + cond->names_count);
1050          for (i = 0; i < condc; i++) {                          const struct ccs_envp *envp =
1051                  const u8 match = condp->equals;                                  (typeof(envp)) (argv + cond->argc);
1052                  const u8 left = condp->left;                          u16 skip;
1053                  const u8 right = condp->right;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1054                  condp++;                                  const u8 left = condp->left;
1055                  switch (left) {                                  const u8 right = condp->right;
1056                  case CCS_ARGV_ENTRY:                                  condp++;
1057                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  switch (left) {
1058                                             argv->index, argv->is_not ?                                  case CCS_ARGV_ENTRY:
1059                                             "!=" : "=", argv->value->name))                                          argv++;
1060                                  goto out;                                          continue;
1061                          argv++;                                  case CCS_ENVP_ENTRY:
1062                          continue;                                          envp++;
1063                  case CCS_ENVP_ENTRY:                                          continue;
1064                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  case CCS_NUMBER_UNION:
1065                                             envp->name->name, envp->is_not ?                                          numbers_p++;
1066                                             "!=" : "="))                                          break;
1067                                  goto out;                                  }
1068                          if (envp->value) {                                  switch (right) {
1069                                  if (!ccs_io_printf(head, "\"%s\"",                                  case CCS_NAME_UNION:
1070                                                     envp->value->name))                                          names_p++;
1071                                          goto out;                                          break;
1072                          } else {                                  case CCS_NUMBER_UNION:
1073                                  if (!ccs_io_printf(head, "NULL"))                                          numbers_p++;
1074                                          goto out;                                          break;
1075                                    }
1076                            }
1077                            while (head->r.cond_index < condc) {
1078                                    const u8 match = condp->equals;
1079                                    const u8 left = condp->left;
1080                                    const u8 right = condp->right;
1081                                    if (!ccs_flush(head))
1082                                            return false;
1083                                    condp++;
1084                                    head->r.cond_index++;
1085                                    ccs_set_space(head);
1086                                    switch (left) {
1087                                    case CCS_ARGV_ENTRY:
1088                                            ccs_io_printf(head,
1089                                                          "exec.argv[%u]%s\"%s\"",
1090                                                          argv->index,
1091                                                          argv->is_not ?
1092                                                          "!=" : "=",
1093                                                          argv->value->name);
1094                                            argv++;
1095                                            continue;
1096                                    case CCS_ENVP_ENTRY:
1097                                            ccs_io_printf(head,
1098                                                          "exec.envp[\"%s\"]%s",
1099                                                          envp->name->name,
1100                                                          envp->is_not ?
1101                                                          "!=" : "=");
1102                                            if (envp->value) {
1103                                                    ccs_set_string(head, "\"");
1104                                                    ccs_set_string(head, envp->
1105                                                                   value->name);
1106                                                    ccs_set_string(head, "\"");
1107                                            } else {
1108                                                    ccs_set_string(head, "NULL");
1109                                            }
1110                                            envp++;
1111                                            continue;
1112                                    case CCS_NUMBER_UNION:
1113                                            ccs_print_number_union(head,
1114                                                                   numbers_p++);
1115                                            break;
1116                                    default:
1117                                            ccs_set_string(head,
1118                                                   ccs_condition_keyword[left]);
1119                                            break;
1120                                    }
1121                                    ccs_set_string(head, match ? "=" : "!=");
1122                                    switch (right) {
1123                                    case CCS_NAME_UNION:
1124                                            ccs_print_name_union(head, names_p++);
1125                                            break;
1126                                    case CCS_NUMBER_UNION:
1127                                            ccs_print_number_union(head,
1128                                                                   numbers_p++);
1129                                            break;
1130                                    default:
1131                                            ccs_set_string(head,
1132                                                   ccs_condition_keyword[right]);
1133                                            break;
1134                                    }
1135                          }                          }
                         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;  
1136                  }                  }
1137                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1138                          goto out;                  /* fall through */
1139                  switch (right) {          case 2:
1140                  case CCS_NAME_UNION:                  if (!ccs_flush(head))
                         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;  
1141                          break;                          break;
1142                    head->r.cond_step++;
1143                    /* fall through */
1144            case 3:
1145                    {
1146                            u8 j;
1147                            const u8 i = cond->post_state[3];
1148                            if (i)
1149                                    ccs_set_string(head, " ; set");
1150                            for (j = 0; j < 3; j++)
1151                                    if ((i & (1 << j)))
1152                                            ccs_io_printf(head,
1153                                                          " task.state[%u]=%u", j,
1154                                                          cond->post_state[j]);
1155                            if (i & (1 << 4))
1156                                    ccs_io_printf(head, " audit=%s",
1157                                                  ccs_yesno(cond->post_state[4]));
1158                  }                  }
1159          }                  ccs_set_lf(head);
         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;  
         }  
  no_condition:  
         if (ccs_io_printf(head, "\n"))  
1160                  return true;                  return true;
  out:  
         return false;  
 }  
   
 /**  
  * ccs_print_path_acl - Print a path ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_acl(struct ccs_io_buffer *head,  
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)  
                         continue;  
                 /* Print "read/write" instead of "read" and "write". */  
                 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)  
                     && (perm & (1 << CCS_TYPE_READ_WRITE)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1161          }          }
1162          head->read_bit = 0;          return false;
         return true;  
 }  
   
 /**  
  * ccs_print_path_number3_acl - Print a path_number3 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_number3_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  
                                        struct ccs_path_number3_acl *ptr,  
                                        const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 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;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1163  }  }
1164    
1165  /**  static void ccs_set_group(struct ccs_io_buffer *head)
  * ccs_print_path2_acl - Print a path2 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path2_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  
                                 struct ccs_path2_acl *ptr,  
                                 const struct ccs_condition *cond)  
1166  {  {
1167          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1168          u8 bit;                  ccs_io_printf(head, "acl_group %u ", head->r.group_index);
         const u8 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path22keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name1) ||  
                     !ccs_print_name_union(head, &ptr->name2) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1169  }  }
1170    
1171  /**  static void ccs_print_bits(struct ccs_io_buffer *head, const u16 perm,
1172   * ccs_print_path_number_acl - Print a path_number ACL entry.                             const char *keywords[]) {
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_number_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  
                                       struct ccs_path_number_acl *ptr,  
                                       const struct ccs_condition *cond)  
 {  
         int pos;  
1173          u8 bit;          u8 bit;
1174          const u8 perm = ptr->perm;          bool first = true;
1175          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;          for (bit = 0; bit < 16; bit++) {
              bit++) {  
1176                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1177                          continue;                          continue;
1178                  pos = head->read_avail;                  if (!first)
1179                  if (!ccs_io_printf(head, "allow_%s",                          ccs_set_string(head, "/");
1180                                     ccs_path_number2keyword(bit)) ||                  first = false;
1181                      !ccs_print_name_union(head, &ptr->name) ||                  ccs_set_string(head, keywords[bit]);
                     !ccs_print_number_union(head, &ptr->number) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1182          }          }
         head->read_bit = 0;  
         return true;  
1183  }  }
1184    
1185  /**  static bool ccs_print_file_bits(struct ccs_io_buffer *head, const u16 perm,
1186   * ccs_print_env_acl - Print an evironment variable name's ACL entry.                                  const char *keywords[]) {
1187   *          if (!perm)
  * @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;  
1188                  return false;                  return false;
1189          }          ccs_set_group(head);
1190            ccs_set_string(head, "file ");
1191            ccs_print_bits(head, perm, keywords);
1192          return true;          return true;
1193  }  }
1194    
1195  /**  static bool ccs_print_network_bits(struct ccs_io_buffer *head,
1196   * ccs_print_capability_acl - Print a capability ACL entry.                                     const char *protocol, const u8 perm) {
1197   *          if (!perm)
  * @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;  
 }  
   
 /**  
  * ccs_print_ipv4_entry - Print IPv4 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_ipv4_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl *ptr)  
 {  
         const u32 min_address = ptr->address.ipv4.min;  
         const u32 max_address = ptr->address.ipv4.max;  
         if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))  
                 return false;  
         if (min_address != max_address  
             && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))  
1198                  return false;                  return false;
1199            ccs_set_group(head);
1200            ccs_set_string(head, "network ");
1201            ccs_set_string(head, protocol);
1202            ccs_set_space(head);
1203            ccs_print_bits(head, perm, ccs_net_keyword);
1204          return true;          return true;
1205  }  }
1206    
1207  /**  /**
1208   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_print_entry - Print an ACL entry.
1209   *   *
1210   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1211   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @acl:  Pointer to an ACL entry.
1212   *   *
1213   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1214   */   */
1215  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1216                                   struct ccs_ip_network_acl *ptr)                              const struct ccs_acl_info *acl)
1217  {  {
1218          char buf[64];          const u8 acl_type = acl->type;
1219          const struct in6_addr *min_address = ptr->address.ipv6.min;          if (head->r.print_cond_part)
1220          const struct in6_addr *max_address = ptr->address.ipv6.max;                  goto print_cond_part;
1221          ccs_print_ipv6(buf, sizeof(buf), min_address);          if (acl->is_deleted)
1222          if (!ccs_io_printf(head, "%s", buf))                  return true;
1223            if (!ccs_flush(head))
1224                  return false;                  return false;
1225          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1226                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1227                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1228                          return false;                  u16 perm = ptr->perm;
1229          }                  if (head->r.print_execute_only)
1230          return true;                          perm &= (1 << CCS_TYPE_EXECUTE) |
1231  }                                  (1 << CCS_TYPE_TRANSIT);
1232                    if (!ccs_print_file_bits(head, perm, ccs_path_keyword))
1233  /**                          goto done;
1234   * ccs_print_network_acl - Print a network ACL entry.                  ccs_print_name_union(head, &ptr->name);
1235   *          } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1236   * @head: Pointer to "struct ccs_io_buffer".                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1237   * @ptr:  Pointer to "struct ccs_ip_network_acl".                  struct ccs_execute_handler *ptr
1238   * @cond: Pointer to "struct ccs_condition". May be NULL.                          = container_of(acl, typeof(*ptr), head);
1239   *                  ccs_set_group(head);
1240   * Returns true on success, false otherwise.                  ccs_io_printf(head, "%s ",
1241   */                                acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1242  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                                CCS_KEYWORD_EXECUTE_HANDLER :
1243                                    struct ccs_ip_network_acl *ptr,                                CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1244                                    const struct ccs_condition *cond)                  ccs_set_string(head, ptr->handler->name);
1245  {          } else if (head->r.print_execute_only) {
1246          int pos;                  return true;
1247          u8 bit;          } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1248          const u16 perm = ptr->perm;                  struct ccs_mkdev_acl *ptr =
1249          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                          container_of(acl, typeof(*ptr), head);
1250                  if (!(perm & (1 << bit)))                  if (!ccs_print_file_bits(head, ptr->perm, ccs_mkdev_keyword))
1251                          continue;                          goto done;
1252                  pos = head->read_avail;                  ccs_print_name_union(head, &ptr->name);
1253                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",                  ccs_print_number_union(head, &ptr->mode);
1254                                     ccs_net2keyword(bit)))                  ccs_print_number_union(head, &ptr->major);
1255                          goto out;                  ccs_print_number_union(head, &ptr->minor);
1256            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1257                    struct ccs_path2_acl *ptr =
1258                            container_of(acl, typeof(*ptr), head);
1259                    if (!ccs_print_file_bits(head, ptr->perm, ccs_path2_keyword))
1260                            goto done;
1261                    ccs_print_name_union(head, &ptr->name1);
1262                    ccs_print_name_union(head, &ptr->name2);
1263            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1264                    struct ccs_path_number_acl *ptr =
1265                            container_of(acl, typeof(*ptr), head);
1266                    if (!ccs_print_file_bits(head, ptr->perm,
1267                                             ccs_path_number_keyword))
1268                            goto done;
1269                    ccs_print_name_union(head, &ptr->name);
1270                    ccs_print_number_union(head, &ptr->number);
1271            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1272                    struct ccs_env_acl *ptr =
1273                            container_of(acl, typeof(*ptr), head);
1274                    ccs_set_group(head);
1275                    ccs_set_string(head, "misc env ");
1276                    ccs_set_string(head, ptr->env->name);
1277            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1278                    struct ccs_capability_acl *ptr =
1279                            container_of(acl, typeof(*ptr), head);
1280                    ccs_set_group(head);
1281                    ccs_set_string(head, "capability ");
1282                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1283            } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1284                    struct ccs_ip_network_acl *ptr =
1285                            container_of(acl, typeof(*ptr), head);
1286                    if (!ccs_print_network_bits(head,
1287                                                ccs_net_protocol_keyword
1288                                                [ptr->protocol], ptr->perm))
1289                            goto done;
1290                    ccs_set_space(head);
1291                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1292                            char buf[128];
1293                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1294                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1295                                             group_name->name))                          ccs_set_string(head,
1296                                  goto out;                                         ptr->address.group->group_name->name);
1297                          break;                          break;
1298                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1299                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1300                                  goto out;                                         ptr->address.ipv4.max);
1301                            ccs_io_printf(head, "%s", buf);
1302                          break;                          break;
1303                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1304                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1305                                  goto out;                                         ptr->address.ipv6.max);
1306                          break;                          ccs_io_printf(head, "%s", buf);
1307                  }                          break;
1308                  if (!ccs_print_number_union(head, &ptr->port) ||                  }
1309                      !ccs_print_condition(head, cond))                  ccs_print_number_union(head, &ptr->port);
1310                          goto out;          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1311          }                  struct ccs_signal_acl *ptr =
1312          head->read_bit = 0;                          container_of(acl, typeof(*ptr), head);
1313          return true;                  ccs_set_group(head);
1314   out:                  ccs_set_string(head, "ipc signal ");
1315          head->read_bit = bit;                  ccs_io_printf(head, "%u ", ptr->sig);
1316          head->read_avail = pos;                  ccs_set_string(head, ptr->domainname->name);
1317          return false;          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1318  }                  struct ccs_mount_acl *ptr =
1319                            container_of(acl, typeof(*ptr), head);
1320  /**                  ccs_set_group(head);
1321   * ccs_print_signal_acl - Print a signal ACL entry.                  ccs_io_printf(head, "file mount");
1322   *                  ccs_print_name_union(head, &ptr->dev_name);
1323   * @head: Pointer to "struct ccs_io_buffer".                  ccs_print_name_union(head, &ptr->dir_name);
1324   * @ptr:  Pointer to "struct signale_acl".                  ccs_print_name_union(head, &ptr->fs_type);
1325   * @cond: Pointer to "struct ccs_condition". May be NULL.                  ccs_print_number_union(head, &ptr->flags);
1326   *          }
1327   * Returns true on success, false otherwise.          if (acl->cond) {
1328   */                  head->r.print_cond_part = true;
1329  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,                  head->r.cond_step = 0;
1330                                   struct ccs_signal_acl *ptr,                  if (!ccs_flush(head))
1331                                   const struct ccs_condition *cond)                          return false;
1332  {   print_cond_part:
1333          const int pos = head->read_avail;                  if (!ccs_print_condition(head, acl->cond))
1334          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",                          return false;
1335                             ptr->sig, ptr->domainname->name) ||                  head->r.print_cond_part = false;
1336              !ccs_print_condition(head, cond)) {          } else {
1337                  head->read_avail = pos;                  ccs_set_lf(head);
                 return false;  
1338          }          }
1339     done:
1340          return true;          return true;
1341  }  }
1342    
1343  /**  /**
1344   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_read_domain2 - Read domain policy.
1345   *   *
1346   * @head:    Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1347   * @keyword: Name of the keyword.   * @domain: Pointer to "struct ccs_domain_info".
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
1348   *   *
1349   * 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.  
1350   *   *
1351   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1352   */   */
1353  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1354                                  struct ccs_mount_acl *ptr,                               struct ccs_domain_info *domain)
                                 const struct ccs_condition *cond)  
1355  {  {
1356          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1357          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||                  struct ccs_acl_info *ptr =
1358              !ccs_print_name_union(head, &ptr->dev_name) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1359              !ccs_print_name_union(head, &ptr->dir_name) ||                  if (!ccs_print_entry(head, ptr))
1360              !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;  
1361          }          }
1362            head->r.acl = NULL;
1363          return true;          return true;
1364  }  }
1365    
1366  /**  /**
1367   * ccs_print_entry - Print an ACL entry.   * ccs_read_domain - Read domain policy.
  *  
  * @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);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
  * ccs_read_domain_policy - Read domain policy.  
1368   *   *
1369   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1370   *   *
1371   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1372   */   */
1373  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1374  {  {
1375          struct list_head *dpos;          if (head->r.eof)
         struct list_head *apos;  
         if (head->read_eof)  
1376                  return;                  return;
1377          if (head->read_step == 0)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1378                  head->read_step = 1;                  struct ccs_domain_info *domain =
1379          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
1380                  struct ccs_domain_info *domain;                  switch (head->r.step) {
1381                  const char *quota_exceeded = "";                          u8 i;
1382                  const char *transition_failed = "";                  case 0:
1383                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted &&
1384                  const char *ignore_global_allow_env = "";                              !head->r.print_this_domain_only)
1385                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1386                  if (head->read_step != 1)                          /* Print domainname and flags. */
1387                          goto acl_loop;                          ccs_set_string(head, domain->domainname->name);
1388                  if (domain->is_deleted && !head->read_single_domain)                          ccs_set_lf(head);
1389                          continue;                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1390                  /* Print domainname and flags. */                                        domain->profile);
1391                  if (domain->quota_warned)                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1392                          quota_exceeded = "quota_exceeded\n";                                        domain->group);
1393                  if (domain->domain_transition_failed)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1394                          transition_failed = "transition_failed\n";                                  if (domain->flags[i])
1395                  if (domain->ignore_global_allow_read)                                          ccs_set_string(head, ccs_dif[i]);
1396                          ignore_global_allow_read                          head->r.step++;
1397                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          ccs_set_lf(head);
1398                  if (domain->ignore_global_allow_env)                          /* fall through */
1399                          ignore_global_allow_env                  case 1:
1400                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          if (!ccs_read_domain2(head, domain))
1401                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                                  return;
1402                                     "%s%s%s%s\n", domain->domainname->name,                          head->r.step++;
1403                                     domain->profile, quota_exceeded,                          if (!ccs_set_lf(head))
                                    transition_failed,  
                                    ignore_global_allow_read,  
                                    ignore_global_allow_env))  
                         return;  
                 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))  
1404                                  return;                                  return;
1405                            /* fall through */
1406                    case 2:
1407                            head->r.step = 0;
1408                            if (head->r.print_this_domain_only)
1409                                    goto done;
1410                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1411          }          }
1412          head->read_eof = true;   done:
1413            head->r.eof = true;
1414  }  }
1415    
1416  /**  /**
# Line 1747  static int ccs_write_domain_profile(stru Line 1461  static int ccs_write_domain_profile(stru
1461   */   */
1462  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1463  {  {
1464          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1465                  return;                  return;
1466          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1467                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1468                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1469                  if (domain->is_deleted)                  if (domain->is_deleted)
1470                          continue;                          continue;
1471                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1472                          return;                          return;
1473                    ccs_io_printf(head, "%u ", domain->profile);
1474                    ccs_set_string(head, domain->domainname->name);
1475                    ccs_set_lf(head);
1476          }          }
1477          head->read_eof = true;          head->r.eof = true;
1478  }  }
1479    
1480  /**  /**
# Line 1771  static void ccs_read_domain_profile(stru Line 1486  static void ccs_read_domain_profile(stru
1486   */   */
1487  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1488  {  {
1489          head->read_eof = false;          head->r.eof = false;
1490          return 0;          return 0;
1491  }  }
1492    
# Line 1796  static void ccs_read_pid(struct ccs_io_b Line 1511  static void ccs_read_pid(struct ccs_io_b
1511          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1512          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1513          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1514          if (!buf)          if (!buf) {
1515                    head->r.eof = true;
1516                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1517          if (head->read_avail || head->read_eof)          }
1518            if (head->r.w_pos || head->r.eof)
1519                  return;                  return;
1520          head->read_eof = true;          head->r.eof = true;
1521          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1522                  task_info = true;                  task_info = true;
1523          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
1524                  global_pid = true;                  global_pid = true;
1525          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1526          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1527  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1528          if (global_pid)          if (global_pid)
1529                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1530          else          else
1531                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1532  #else  #else
1533          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1534  #endif  #endif
# Line 1819  static void ccs_read_pid(struct ccs_io_b Line 1536  static void ccs_read_pid(struct ccs_io_b
1536                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1537                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1538          }          }
1539          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1540          if (!domain)          if (!domain)
1541                  return;                  return;
1542          if (!task_info)          if (!task_info) {
1543                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1544                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1545          else          } else {
1546                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1547                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1548                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1549                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1550                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1551                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER),
1552                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1553                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1554                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1555            }
1556  }  }
1557    
1558    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1559            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1560            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1561            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1562            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1563            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1564    };
1565    
1566    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1567            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1568            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1569            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1570    };
1571    
1572  /**  /**
1573   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1574   *   *
1575   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1576   *   *
1577   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1578   */   */
1579  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1580  {  {
1581          char *data = head->write_buf;          char *data = head->write_buf;
1582          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1583          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1584                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1585          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1586                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1587          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1588                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1589                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1590          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1591                  return ccs_write_domain_initializer_policy(data, true,          };
1592                                                             is_delete);          for (i = 0; i < 3; i++)
1593          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1594                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1595          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1596                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1597          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1598                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1599          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1600                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1601          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1602                  return ccs_write_path_group_policy(data, is_delete);          if (ccs_str_starts(&data, "acl_group ")) {
1603          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  unsigned int group;
1604                  return ccs_write_number_group_policy(data, is_delete);                  if (sscanf(data, "%u", &group) == 1 &&
1605          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                      group < CCS_MAX_ACL_GROUPS) {
1606                  return ccs_write_no_rewrite_policy(data, is_delete);                          data = strchr(data, ' ');
1607          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                          if (data)
1608                  return ccs_write_address_group_policy(data, is_delete);                                  return ccs_write_domain2(data + 1,
1609          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                                                           &ccs_acl_group[group],
1610                  return ccs_write_reserved_port_policy(data, is_delete);                                                           is_delete);
1611                    }
1612            }
1613          return -EINVAL;          return -EINVAL;
1614  }  }
1615    
1616  /**  /**
1617   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1618   *   *
1619   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1620   *   * @idx:  Index number.
  * Caller holds ccs_read_lock().  
  */  
 static void ccs_read_exception_policy(struct ccs_io_buffer *head)  
 {  
         if (head->read_eof)  
                 return;  
         switch (head->read_step) {  
         case 0:  
                 head->read_var2 = NULL;  
                 head->read_step = 1;  
         case 1:  
                 if (!ccs_read_domain_keeper_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 2;  
         case 2:  
                 if (!ccs_read_globally_readable_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 3;  
         case 3:  
                 if (!ccs_read_globally_usable_env_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 4;  
         case 4:  
                 if (!ccs_read_domain_initializer_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 6;  
         case 6:  
                 if (!ccs_read_aggregator_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 7;  
         case 7:  
                 if (!ccs_read_file_pattern(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 8;  
         case 8:  
                 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;  
         }  
 }  
   
 /**  
  * ccs_get_argv0 - Get argv[0].  
  *  
  * @ee: Pointer to "struct ccs_execve_entry".  
1621   *   *
1622   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1623     *
1624     * Caller holds ccs_read_lock().
1625   */   */
1626  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1627  {  {
1628          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1629          char *arg_ptr = ee->tmp;                  struct ccs_group *group =
1630          int arg_len = 0;                          list_entry(head->r.group, typeof(*group), head.list);
1631          unsigned long pos = bprm->p;                  list_for_each_cookie(head->r.acl, &group->member_list) {
1632          int offset = pos % PAGE_SIZE;                          struct ccs_acl_head *ptr =
1633          bool done = false;                                  list_entry(head->r.acl, typeof(*ptr), list);
1634          if (!bprm->argc)                          if (ptr->is_deleted)
1635                  goto out;                                  continue;
1636          while (1) {                          if (!ccs_flush(head))
1637                  if (!ccs_dump_page(bprm, pos, &ee->dump))                                  return false;
1638                          goto out;                          ccs_set_string(head, ccs_group_name[idx]);
1639                  pos += PAGE_SIZE - offset;                          ccs_set_string(head, group->group_name->name);
1640                  /* Read. */                          if (idx == CCS_PATH_GROUP) {
1641                  while (offset < PAGE_SIZE) {                                  ccs_set_space(head);
1642                          const char *kaddr = ee->dump.data;                                  ccs_set_string(head, container_of
1643                          const unsigned char c = kaddr[offset++];                                                 (ptr, struct ccs_path_group,
1644                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                  head)->member_name->name);
1645                                  if (c == '\\') {                          } else if (idx == CCS_NUMBER_GROUP) {
1646                                          arg_ptr[arg_len++] = '\\';                                  ccs_print_number_union(head, &container_of
1647                                          arg_ptr[arg_len++] = '\\';                                                         (ptr, struct ccs_number_group,
1648                                  } else if (c > ' ' && c < 127) {                                                          head)->number);
1649                                          arg_ptr[arg_len++] = c;                          } else if (idx == CCS_ADDRESS_GROUP) {
1650                                  } else {                                  char buffer[128];
1651                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_address_group *member =
1652                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(ptr, typeof(*member),
1653                                          arg_ptr[arg_len++]                                                       head);
1654                                                  = ((c >> 3) & 7) + '0';                                  if (member->is_ipv6)
1655                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          ccs_print_ipv6(buffer, sizeof(buffer),
1656                                  }                                                         member->min.ipv6,
1657                          } else {                                                         member->max.ipv6);
1658                                  arg_ptr[arg_len] = '\0';                                  else
1659                                  done = true;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1660                                  break;                                                         member->min.ipv4,
1661                                                           member->max.ipv4);
1662                                    ccs_io_printf(head, " %s", buffer);
1663                          }                          }
1664                            ccs_set_lf(head);
1665                  }                  }
1666                  offset = 0;                  head->r.acl = NULL;
                 if (done)  
                         break;  
1667          }          }
1668            head->r.group = NULL;
1669          return true;          return true;
  out:  
         return false;  
1670  }  }
1671    
1672  /**  /**
1673   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_read_policy - Read "struct ccs_..._entry" list.
1674   *   *
1675   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1676     * @idx:  Index number.
1677     *
1678     * Returns true on success, false otherwise.
1679   *   *
1680   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1681   */   */
1682  static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
                                                        *ee)  
1683  {  {
1684          struct ccs_condition *cond;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1685          char *buf;                  struct ccs_acl_head *acl =
1686          int len = 256;                          container_of(head->r.acl, typeof(*acl), list);
1687          char *realpath = NULL;                  if (acl->is_deleted)
1688          char *argv0 = NULL;                          continue;
1689          const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);                  if (!ccs_flush(head))
1690          if (profile->learning->learning_exec_realpath) {                          return false;
1691                  struct file *file = ee->bprm->file;                  switch (idx) {
1692  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)                  case CCS_ID_TRANSITION_CONTROL:
1693                  struct path path = { file->f_vfsmnt, file->f_dentry };                          {
1694                  realpath = ccs_realpath_from_path(&path);                                  struct ccs_transition_control *ptr =
1695  #else                                          container_of(acl, typeof(*ptr), head);
1696                  realpath = ccs_realpath_from_path(&file->f_path);                                  ccs_set_string(head,
1697  #endif                                                 ccs_transition_type[ptr->type]);
1698                  if (realpath)                                  ccs_set_string(head, ptr->program ?
1699                          len += strlen(realpath) + 17;                                                 ptr->program->name : "any");
1700          }                                  ccs_set_string(head, " from ");
1701          if (profile->learning->learning_exec_argv0) {                                  ccs_set_string(head, ptr->domainname ?
1702                  if (ccs_get_argv0(ee)) {                                                 ptr->domainname->name : "any");
1703                          argv0 = ee->tmp;                          }
1704                          len += strlen(argv0) + 16;                          break;
1705                    case CCS_ID_AGGREGATOR:
1706                            {
1707                                    struct ccs_aggregator *ptr =
1708                                            container_of(acl, typeof(*ptr), head);
1709                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1710                                    ccs_set_string(head, ptr->original_name->name);
1711                                    ccs_set_space(head);
1712                                    ccs_set_string(head,
1713                                                   ptr->aggregated_name->name);
1714                            }
1715                            break;
1716                    case CCS_ID_PATTERN:
1717                            {
1718                                    struct ccs_pattern *ptr =
1719                                            container_of(acl, typeof(*ptr), head);
1720                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1721                                    ccs_set_string(head, ptr->pattern->name);
1722                            }
1723                            break;
1724                    case CCS_ID_RESERVEDPORT:
1725                            {
1726                                    struct ccs_reserved *ptr =
1727                                            container_of(acl, typeof(*ptr), head);
1728                                    const u16 min_port = ptr->min_port;
1729                                    const u16 max_port = ptr->max_port;
1730                                    ccs_set_string(head,
1731                                                   CCS_KEYWORD_DENY_AUTOBIND);
1732                                    ccs_io_printf(head, "%u", min_port);
1733                                    if (min_port != max_port)
1734                                            ccs_io_printf(head, "-%u", max_port);
1735                            }
1736                            break;
1737                    default:
1738                            continue;
1739                  }                  }
1740                    ccs_set_lf(head);
1741          }          }
1742          buf = kmalloc(len, GFP_KERNEL);          head->r.acl = NULL;
1743          if (!buf) {          return true;
                 kfree(realpath);  
                 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;  
1744  }  }
1745    
1746  /**  /**
1747   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
1748   *   *
1749   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
1750   *   *
1751   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1752   */   */
1753  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
1754  {  {
1755          struct ccs_condition *cond;          if (head->r.eof)
1756          char *buf;                  return;
1757          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
1758          const char *symlink = NULL;                 ccs_read_policy(head, head->r.step))
1759          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->r.step++;
1760          if (profile->learning->learning_symlink_target) {          if (head->r.step < CCS_MAX_POLICY)
1761                  symlink = r->obj->symlink_target->name;                  return;
1762                  len += strlen(symlink) + 18;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1763                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1764                    head->r.step++;
1765            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1766                    return;
1767            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1768                   + CCS_MAX_ACL_GROUPS) {
1769                    head->r.group_index = head->r.step - CCS_MAX_POLICY
1770                            - CCS_MAX_GROUP;
1771                    if (!ccs_read_domain2(head,
1772                                          &ccs_acl_group[head->r.group_index]))
1773                            return;
1774                    head->r.step++;
1775          }          }
1776          buf = kmalloc(len, GFP_KERNEL);          head->r.eof = true;
         if (!buf)  
                 return NULL;  
         snprintf(buf, len - 1, "if");  
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (symlink) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",  
                          symlink);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
1777  }  }
1778    
1779  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2111  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1783  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1783  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1784    
1785  /* Structure for query. */  /* Structure for query. */
1786  struct ccs_query_entry {  struct ccs_query {
1787          struct list_head list;          struct list_head list;
1788          char *query;          char *query;
1789          int query_len;          int query_len;
# Line 2120  struct ccs_query_entry { Line 1792  struct ccs_query_entry {
1792          int answer;          int answer;
1793  };  };
1794    
1795  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1796  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1797    
1798  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1799  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1800    
1801    static void ccs_truncate(char *str)
1802    {
1803            while (* (unsigned char *) str > (unsigned char) ' ')
1804                    str++;
1805            *str = '\0';
1806    }
1807    
1808  /**  /**
1809   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1810   *   *
1811   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1812   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1813   *   *
1814   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1815   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1816   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1817   * 0 if it is not in enforcing mode, -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1818   */   */
1819  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1820  {  {
# Line 2144  int ccs_supervisor(struct ccs_request_in Line 1823  int ccs_supervisor(struct ccs_request_in
1823          int pos;          int pos;
1824          int len;          int len;
1825          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1826          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1827          bool quota_exceeded = false;          bool quota_exceeded = false;
1828          char *header;          char *header;
1829          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1830                  r->domain = ccs_current_domain();          va_start(args, fmt);
1831          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1832            va_end(args);
1833            if (r->mode == CCS_CONFIG_LEARNING) {
1834                  char *buffer;                  char *buffer;
1835                  struct ccs_condition *cond;                  char *realpath = NULL;
1836          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1837                    char *symlink = NULL;
1838                    char *handler = NULL;
1839                    const struct ccs_preference *pref;
1840                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1841                          return 0;                          return 0;
1842                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1843                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, GFP_KERNEL);  
                 if (!buffer)  
1844                          return 0;                          return 0;
1845                  va_start(args, fmt);                  pref = &ccs_profile(r->profile)->preference;
1846                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1847                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1848                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1849                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1850                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1851                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
1852                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1853                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1854                          char str[] = "if task.type=execute_handler";                                  }
1855                          cond = ccs_get_condition(str);                          }
1856                  } else                          if (pref->learning_exec_realpath) {
1857                          cond = NULL;                                  realpath = strstr(header,
1858                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
1859                  ccs_put_condition(cond);                                  if (realpath) {
1860                  kfree(buffer);                                          realpath += 8;
1861                  /* fall through */                                          ccs_truncate(realpath);
1862          case CCS_CONFIG_PERMISSIVE:                                  }
1863                            }
1864                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1865                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1866                               pref->learning_symlink_target) {
1867                            symlink = strstr(header, " symlink.target=\"");
1868                            if (symlink)
1869                                    ccs_truncate(symlink + 1);
1870                    }
1871                    handler = strstr(header, "type=execute_handler");
1872                    if (handler)
1873                            ccs_truncate(handler);
1874                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1875                    if (buffer) {
1876                            va_start(args, fmt);
1877                            vsnprintf(buffer, len - 1, fmt, args);
1878                            va_end(args);
1879                            if (handler || realpath || argv0 || symlink) {
1880                                    ccs_addprintf(buffer, len, " if");
1881                                    if (handler)
1882                                            ccs_addprintf(buffer, len, " task.%s",
1883                                                          handler);
1884                                    if (realpath)
1885                                            ccs_addprintf(buffer, len, " exec.%s",
1886                                                          realpath);
1887                                    if (argv0)
1888                                            ccs_addprintf(buffer, len,
1889                                                          " exec.argv[0]=%s",
1890                                                          argv0);
1891                                    if (symlink)
1892                                            ccs_addprintf(buffer, len, "%s",
1893                                                          symlink);
1894                            }
1895                            ccs_normalize_line(buffer);
1896                            ccs_write_domain2(buffer, domain, false);
1897                            kfree(buffer);
1898                    }
1899                    kfree(header);
1900                  return 0;                  return 0;
1901          }          }
1902            if (r->mode != CCS_CONFIG_ENFORCING)
1903                    return 0;
1904          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
1905                  int i;                  int i;
1906                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
1907                          return -EPERM;                          return -EPERM;
1908                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->preference.
1909                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
1910                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
1911                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
1912                  }                  }
1913                  return -EPERM;                  return -EPERM;
1914          }          }
1915          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);  
1916          if (!header)          if (!header)
1917                  goto out;                  goto out;
1918          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
1919          if (!ccs_query_entry)          if (!entry)
1920                  goto out;                  goto out;
1921          len = ccs_round2(len);          len = ccs_round2(len);
1922          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
1923          if (!ccs_query_entry->query)          if (!entry->query)
1924                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
1925          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
1926          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
1927              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
1928                  quota_exceeded = true;                  quota_exceeded = true;
1929          } else {          } else {
1930                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
1931                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
1932          }          }
1933          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
1934          if (quota_exceeded)          if (quota_exceeded)
1935                  goto out;                  goto out;
1936          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
1937                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
1938          kfree(header);          kfree(header);
1939          header = NULL;          header = NULL;
1940          va_start(args, fmt);          va_start(args, fmt);
1941          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
1942          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
1943          va_end(args);          va_end(args);
1944          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
1945          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
1946          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
1947          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
1948          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
1949               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
1950               ccs_query_entry->timer++) {               entry->timer++) {
1951                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
1952                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
1953                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
1954                  if (ccs_query_entry->answer)                  if (entry->answer)
1955                          break;                          break;
1956          }          }
1957          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
1958          list_del(&ccs_query_entry->list);          list_del(&entry->list);
1959          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
1960          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
1961          switch (ccs_query_entry->answer) {          switch (entry->answer) {
1962          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
1963                  error = 1;                  error = CCS_RETRY_REQUEST;
1964                  r->retry++;                  r->retry++;
1965                  break;                  break;
1966          case 1:          case 1:
# Line 2259  int ccs_supervisor(struct ccs_request_in Line 1975  int ccs_supervisor(struct ccs_request_in
1975                  break;                  break;
1976          }          }
1977   out:   out:
1978          if (ccs_query_entry)          if (entry)
1979                  kfree(ccs_query_entry->query);                  kfree(entry->query);
1980          kfree(ccs_query_entry);          kfree(entry);
1981          kfree(header);          kfree(header);
1982          return error;          return error;
1983  }  }
# Line 2284  static int ccs_poll_query(struct file *f Line 2000  static int ccs_poll_query(struct file *f
2000          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2001                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2002                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2003                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2004                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2005                          if (ptr->answer)                          if (ptr->answer)
2006                                  continue;                                  continue;
2007                          found = true;                          found = true;
# Line 2312  static void ccs_read_query(struct ccs_io Line 2028  static void ccs_read_query(struct ccs_io
2028          int pos = 0;          int pos = 0;
2029          int len = 0;          int len = 0;
2030          char *buf;          char *buf;
2031          if (head->read_avail)          if (head->r.w_pos)
2032                  return;                  return;
2033          if (head->read_buf) {          if (head->read_buf) {
2034                  kfree(head->read_buf);                  kfree(head->read_buf);
2035                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2036          }          }
2037          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2038          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2039                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2040                  if (ptr->answer)                  if (ptr->answer)
2041                          continue;                          continue;
2042                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2043                          continue;                          continue;
2044                  len = ptr->query_len;                  len = ptr->query_len;
2045                  break;                  break;
2046          }          }
2047          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2048          if (!len) {          if (!len) {
2049                  head->read_step = 0;                  head->r.query_index = 0;
2050                  return;                  return;
2051          }          }
2052          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2053          if (!buf)          if (!buf)
2054                  return;                  return;
2055          pos = 0;          pos = 0;
2056          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2057          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2058                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2059                  if (ptr->answer)                  if (ptr->answer)
2060                          continue;                          continue;
2061                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2062                          continue;                          continue;
2063                  /*                  /*
2064                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2357  static void ccs_read_query(struct ccs_io Line 2070  static void ccs_read_query(struct ccs_io
2070          }          }
2071          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2072          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2073                  head->read_buf = buf;                  head->read_buf = buf;
2074                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2075                    head->r.query_index++;
2076          } else {          } else {
2077                  kfree(buf);                  kfree(buf);
2078          }          }
# Line 2381  static int ccs_write_answer(struct ccs_i Line 2093  static int ccs_write_answer(struct ccs_i
2093          unsigned int answer;          unsigned int answer;
2094          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2095          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2096                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2097                  ptr->timer = 0;                  ptr->timer = 0;
2098          }          }
2099          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2390  static int ccs_write_answer(struct ccs_i Line 2101  static int ccs_write_answer(struct ccs_i
2101                  return -EINVAL;                  return -EINVAL;
2102          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2103          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2104                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2105                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2106                          continue;                          continue;
2107                  if (!ptr->answer)                  if (!ptr->answer)
# Line 2409  static int ccs_write_answer(struct ccs_i Line 2119  static int ccs_write_answer(struct ccs_i
2119   */   */
2120  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2121  {  {
2122          if (head->read_eof)          if (head->r.eof)
2123                  return;                  return;
2124          ccs_io_printf(head, "1.7.1-pre");          ccs_set_string(head, "1.8.0-pre");
2125          head->read_eof = true;          head->r.eof = true;
2126  }  }
2127    
2128  /**  /**
# Line 2422  static void ccs_read_version(struct ccs_ Line 2132  static void ccs_read_version(struct ccs_
2132   */   */
2133  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2134  {  {
2135          if (head->read_eof)          if (head->r.eof)
2136                  return;                  return;
2137          /*          /*
2138           * ccs_current_domain()->domainname != NULL because every process           * ccs_current_domain()->domainname != NULL because every process
2139           * belongs to a domain and the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2140           */           */
2141          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2142          head->read_eof = true;          head->r.eof = true;
2143  }  }
2144    
2145  /**  /**
# Line 2442  static void ccs_read_self_domain(struct Line 2152  static void ccs_read_self_domain(struct
2152   */   */
2153  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2154  {  {
2155          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2156          if (!head)          if (!head)
2157                  return -ENOMEM;                  return -ENOMEM;
2158          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2159          head->type = type;          head->type = type;
2160          switch (type) {          switch (type) {
2161          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2162                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2163                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2164                  break;                  break;
2165          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2166                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2167                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2168                  break;                  break;
2169  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2170          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;  
2171          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2172                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2173                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2174                  break;                  break;
2175  #endif  #endif
2176          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2503  int ccs_open_control(const u8 type, stru Line 2210  int ccs_open_control(const u8 type, stru
2210                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2211                  break;                  break;
2212          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2213                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2214                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2215                  break;                  break;
2216          }          }
2217          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2518  int ccs_open_control(const u8 type, stru Line 2225  int ccs_open_control(const u8 type, stru
2225                  /* Don't allocate read_buf for poll() access. */                  /* Don't allocate read_buf for poll() access. */
2226                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2227                          head->readbuf_size = 4096;                          head->readbuf_size = 4096;
2228                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2229                  if (!head->read_buf) {                  if (!head->read_buf) {
2230                          kfree(head);                          kfree(head);
2231                          return -ENOMEM;                          return -ENOMEM;
# Line 2532  int ccs_open_control(const u8 type, stru Line 2239  int ccs_open_control(const u8 type, stru
2239                  head->write = NULL;                  head->write = NULL;
2240          } else if (head->write) {          } else if (head->write) {
2241                  head->writebuf_size = 4096;                  head->writebuf_size = 4096;
2242                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2243                  if (!head->write_buf) {                  if (!head->write_buf) {
2244                          kfree(head->read_buf);                          kfree(head->read_buf);
2245                          kfree(head);                          kfree(head);
# Line 2541  int ccs_open_control(const u8 type, stru Line 2248  int ccs_open_control(const u8 type, stru
2248          }          }
2249          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2250              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2251                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2252          file->private_data = head;          file->private_data = head;
2253          /*          /*
2254           * Call the handler now if the file is /proc/ccs/self_domain           * Call the handler now if the file is /proc/ccs/self_domain
# Line 2591  int ccs_poll_control(struct file *file, Line 2298  int ccs_poll_control(struct file *file,
2298  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2299                       const int buffer_len)                       const int buffer_len)
2300  {  {
2301          int len = 0;          int len;
2302          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2303          char *cp;          int idx;
2304          if (!head->read)          if (!head->read)
2305                  return -ENOSYS;                  return -ENOSYS;
2306          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2307                  return -EFAULT;                  return -EFAULT;
2308          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2309                  return -EINTR;                  return -EINTR;
2310          while (1) {          head->read_user_buf = buffer;
2311            head->read_user_buf_avail = buffer_len;
2312            idx = ccs_read_lock();
2313            if (ccs_flush(head))
2314                  /* Call the policy handler. */                  /* Call the policy handler. */
2315                  head->read(head);                  head->read(head);
2316                  /* Write to buffer. */          ccs_flush(head);
2317                  len = head->read_avail;          ccs_read_unlock(idx);
2318                  if (len || head->poll || head->read_eof)          len = head->read_user_buf - buffer;
                         break;  
                 len = head->readbuf_size * 2;  
                 cp = kzalloc(len, GFP_KERNEL);  
                 if (!cp) {  
                         len = -ENOMEM;  
                         goto out;  
                 }  
                 kfree(head->read_buf);  
                 head->read_buf = cp;  
                 head->readbuf_size = len;  
         }  
         if (len > buffer_len)  
                 len = buffer_len;  
         if (!len)  
                 goto out;  
         /* head->read_buf changes by some functions. */  
         cp = head->read_buf;  
         if (copy_to_user(buffer, cp, len)) {  
                 len = -EFAULT;  
                 goto out;  
         }  
         head->read_avail -= len;  
         memmove(cp, cp + len, head->read_avail);  
  out:  
2319          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2320          return len;          return len;
2321  }  }
# Line 2650  int ccs_write_control(struct file *file, Line 2336  int ccs_write_control(struct file *file,
2336          int error = buffer_len;          int error = buffer_len;
2337          int avail_len = buffer_len;          int avail_len = buffer_len;
2338          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2339            int idx;
2340          if (!head->write)          if (!head->write)
2341                  return -ENOSYS;                  return -ENOSYS;
2342          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2343                  return -EFAULT;                  return -EFAULT;
         /* Don't allow updating policies by non manager programs. */  
         if (head->write != ccs_write_pid &&  
             head->write != ccs_write_domain_policy &&  
             !ccs_is_policy_manager())  
                 return -EPERM;  
2344          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2345                  return -EINTR;                  return -EINTR;
2346            idx = ccs_read_lock();
2347            /* Don't allow updating policies by non manager programs. */
2348            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2349                !ccs_manager()) {
2350                    ccs_read_unlock(idx);
2351                    mutex_unlock(&head->io_sem);
2352                    return -EPERM;
2353            }
2354          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2355          while (avail_len > 0) {          while (avail_len > 0) {
2356                  char c;                  char c;
2357                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->w.avail >= head->writebuf_size - 1) {
2358                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2359                          char *cp = kzalloc(len, GFP_KERNEL);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2360                          if (!cp) {                          if (!cp) {
2361                                  error = -ENOMEM;                                  error = -ENOMEM;
2362                                  break;                                  break;
2363                          }                          }
2364                          memmove(cp, cp0, head->write_avail);                          memmove(cp, cp0, head->w.avail);
2365                          kfree(cp0);                          kfree(cp0);
2366                          head->write_buf = cp;                          head->write_buf = cp;
2367                          cp0 = cp;                          cp0 = cp;
# Line 2683  int ccs_write_control(struct file *file, Line 2373  int ccs_write_control(struct file *file,
2373                  }                  }
2374                  buffer++;                  buffer++;
2375                  avail_len--;                  avail_len--;
2376                  cp0[head->write_avail++] = c;                  cp0[head->w.avail++] = c;
2377                  if (c != '\n')                  if (c != '\n')
2378                          continue;                          continue;
2379                  cp0[head->write_avail - 1] = '\0';                  cp0[head->w.avail - 1] = '\0';
2380                  head->write_avail = 0;                  head->w.avail = 0;
2381                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2382                  head->write(head);                  head->write(head);
2383          }          }
2384            ccs_read_unlock(idx);
2385          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2386          return error;          return error;
2387  }  }
# Line 2714  int ccs_close_control(struct file *file) Line 2405  int ccs_close_control(struct file *file)
2405                  atomic_dec(&ccs_query_observers);                  atomic_dec(&ccs_query_observers);
2406          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2407              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2408                  ccs_read_unlock(head->reader_idx);                  ccs_unlock(head->reader_idx);
2409          /* Release memory used for policy I/O. */          /* Release memory used for policy I/O. */
2410          kfree(head->read_buf);          kfree(head->read_buf);
2411          head->read_buf = NULL;          head->read_buf = NULL;
# Line 2727  int ccs_close_control(struct file *file) Line 2418  int ccs_close_control(struct file *file)
2418                  ccs_run_gc();                  ccs_run_gc();
2419          return 0;          return 0;
2420  }  }
2421    
2422    void __init ccs_policy_io_init(void)
2423    {
2424            ccsecurity_ops.check_profile = ccs_check_profile;
2425    }

Legend:
Removed from v.3109  
changed lines
  Added in v.3871

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26