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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/ccs-patch/security/ccsecurity/policy_io.c revision 2888 by kumaneko, Mon Aug 10 07:31:29 2009 UTC trunk/1.7.x/ccs-patch/security/ccsecurity/policy_io.c revision 3422 by kumaneko, Thu Feb 4 07:11:17 2010 UTC
# Line 3  Line 3 
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2009  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.0-pre   2009/08/08   * Version: 1.7.1+   2009/12/20
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 12  Line 12 
12    
13  #include "internal.h"  #include "internal.h"
14    
15  /* Lock for protecting ccs_profile->comment  */  static struct ccs_profile ccs_default_profile = {
16  static DEFINE_SPINLOCK(ccs_profile_comment_lock);          .learning = &ccs_default_profile.preference,
17            .permissive = &ccs_default_profile.preference,
18            .enforcing = &ccs_default_profile.preference,
19            .audit = &ccs_default_profile.preference,
20    #ifdef CONFIG_CCSECURITY_AUDIT
21            .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
22            .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
23    #endif
24            .preference.audit_task_info = true,
25            .preference.audit_path_info = true,
26            .preference.enforcing_penalty = 0,
27            .preference.enforcing_verbose = true,
28            .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
29            .preference.learning_verbose = false,
30            .preference.learning_exec_realpath = true,
31            .preference.learning_exec_argv0 = true,
32            .preference.learning_symlink_target = true,
33            .preference.permissive_verbose = true
34    };
35    
36  static bool ccs_profile_entry_used[CCS_MAX_CONTROL_INDEX +  /* Profile version. Currently only 20090903 is defined. */
37                                     CCS_MAX_CAPABILITY_INDEX + 1];  static unsigned int ccs_profile_version;
38    
39    /* Profile table. Memory is allocated as needed. */
40    static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
42  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
43  static const char *ccs_mode_4[4] = {  static const char *ccs_mode_4[4] = {
44          "disabled", "learning", "permissive", "enforcing"          "disabled", "learning", "permissive", "enforcing"
45  };  };
 /* String table for functionality that takes 2 modes. */  
 static const char *ccs_mode_2[4] = {  
         "disabled", "enabled", "enabled", "enabled"  
 };  
46    
47  /* Table for profile. */  /* String table for /proc/ccs/profile */
48  static struct {  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +
49          const char *keyword;                                      CCS_MAX_CAPABILITY_INDEX +
50          unsigned int current_value;                                      CCS_MAX_MAC_CATEGORY_INDEX] = {
51          const unsigned int max_value;          [CCS_MAC_FILE_EXECUTE]
52  } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {          = "file::execute",
53          [CCS_MAC_FOR_FILE]        = { "MAC_FOR_FILE",        0, 3 },          [CCS_MAC_FILE_OPEN]
54          [CCS_MAC_FOR_IOCTL]       = { "MAC_FOR_IOCTL",       0, 3 },          = "file::open",
55          [CCS_MAC_FOR_FILEATTR]    = { "MAC_FOR_FILEATTR",    0, 3 },          [CCS_MAC_FILE_CREATE]
56          [CCS_MAC_FOR_ARGV0]       = { "MAC_FOR_ARGV0",       0, 3 },          = "file::create",
57          [CCS_MAC_FOR_ENV]         = { "MAC_FOR_ENV",         0, 3 },          [CCS_MAC_FILE_UNLINK]
58          [CCS_MAC_FOR_NETWORK]     = { "MAC_FOR_NETWORK",     0, 3 },          = "file::unlink",
59          [CCS_MAC_FOR_SIGNAL]      = { "MAC_FOR_SIGNAL",      0, 3 },          [CCS_MAC_FILE_MKDIR]
60          [CCS_MAC_FOR_NAMESPACE]   = { "MAC_FOR_NAMESPACE",   0, 3 },          = "file::mkdir",
61          [CCS_RESTRICT_AUTOBIND]   = { "RESTRICT_AUTOBIND",   0, 1 },          [CCS_MAC_FILE_RMDIR]
62          [CCS_MAX_ACCEPT_ENTRY]          = "file::rmdir",
63          = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },          [CCS_MAC_FILE_MKFIFO]
64  #ifdef CONFIG_CCSECURITY_AUDIT          = "file::mkfifo",
65          [CCS_MAX_GRANT_LOG]          [CCS_MAC_FILE_MKSOCK]
66          = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },          = "file::mksock",
67          [CCS_MAX_REJECT_LOG]          [CCS_MAC_FILE_TRUNCATE]
68          = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },          = "file::truncate",
69  #endif          [CCS_MAC_FILE_SYMLINK]
70          [CCS_VERBOSE]             = { "TOMOYO_VERBOSE",      1, 1 },          = "file::symlink",
71          [CCS_SLEEP_PERIOD]          [CCS_MAC_FILE_REWRITE]
72          = { "SLEEP_PERIOD",        0, 3000 }, /* in 0.1 second */          = "file::rewrite",
73            [CCS_MAC_FILE_MKBLOCK]
74            = "file::mkblock",
75            [CCS_MAC_FILE_MKCHAR]
76            = "file::mkchar",
77            [CCS_MAC_FILE_LINK]
78            = "file::link",
79            [CCS_MAC_FILE_RENAME]
80            = "file::rename",
81            [CCS_MAC_FILE_CHMOD]
82            = "file::chmod",
83            [CCS_MAC_FILE_CHOWN]
84            = "file::chown",
85            [CCS_MAC_FILE_CHGRP]
86            = "file::chgrp",
87            [CCS_MAC_FILE_IOCTL]
88            = "file::ioctl",
89            [CCS_MAC_FILE_CHROOT]
90            = "file::chroot",
91            [CCS_MAC_FILE_MOUNT]
92            = "file::mount",
93            [CCS_MAC_FILE_UMOUNT]
94            = "file::umount",
95            [CCS_MAC_FILE_PIVOT_ROOT]
96            = "file::pivot_root",
97            [CCS_MAC_ENVIRON]
98            = "misc::env",
99            [CCS_MAC_NETWORK_UDP_BIND]
100            = "network::inet_udp_bind",
101            [CCS_MAC_NETWORK_UDP_CONNECT]
102            = "network::inet_udp_connect",
103            [CCS_MAC_NETWORK_TCP_BIND]
104            = "network::inet_tcp_bind",
105            [CCS_MAC_NETWORK_TCP_LISTEN]
106            = "network::inet_tcp_listen",
107            [CCS_MAC_NETWORK_TCP_CONNECT]
108            = "network::inet_tcp_connect",
109            [CCS_MAC_NETWORK_TCP_ACCEPT]
110            = "network::inet_tcp_accept",
111            [CCS_MAC_NETWORK_RAW_BIND]
112            = "network::inet_raw_bind",
113            [CCS_MAC_NETWORK_RAW_CONNECT]
114            = "network::inet_raw_connect",
115            [CCS_MAC_SIGNAL]
116            = "ipc::signal",
117            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]
118            = "capability::inet_tcp_create",
119            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]
120            = "capability::inet_tcp_listen",
121            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]
122            = "capability::inet_tcp_connect",
123            [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]
124            = "capability::use_inet_udp",
125            [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]
126            = "capability::use_inet_ip",
127            [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
128            = "capability::use_route",
129            [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
130            = "capability::use_packet",
131            [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]
132            = "capability::SYS_MOUNT",
133            [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]
134            = "capability::SYS_UMOUNT",
135            [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
136            = "capability::SYS_REBOOT",
137            [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]
138            = "capability::SYS_CHROOT",
139            [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]
140            = "capability::SYS_KILL",
141            [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
142            = "capability::SYS_VHANGUP",
143            [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
144            = "capability::SYS_TIME",
145            [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]
146            = "capability::SYS_NICE",
147            [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]
148            = "capability::SYS_SETHOSTNAME",
149            [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
150            = "capability::use_kernel_module",
151            [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]
152            = "capability::create_fifo",
153            [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]
154            = "capability::create_block_dev",
155            [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]
156            = "capability::create_char_dev",
157            [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]
158            = "capability::create_unix_socket",
159            [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]
160            = "capability::SYS_LINK",
161            [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]
162            = "capability::SYS_SYMLINK",
163            [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]
164            = "capability::SYS_RENAME",
165            [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]
166            = "capability::SYS_UNLINK",
167            [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]
168            = "capability::SYS_CHMOD",
169            [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]
170            = "capability::SYS_CHOWN",
171            [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]
172            = "capability::SYS_IOCTL",
173            [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
174            = "capability::SYS_KEXEC_LOAD",
175            [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]
176            = "capability::SYS_PIVOT_ROOT",
177            [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
178            = "capability::SYS_PTRACE",
179            [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]
180            = "capability::conceal_mount",
181            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
182             + CCS_MAC_CATEGORY_FILE] = "file",
183            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
184             + CCS_MAC_CATEGORY_NETWORK] = "network",
185            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
186             + CCS_MAC_CATEGORY_MISC] = "misc",
187            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
188             + CCS_MAC_CATEGORY_IPC] = "ipc",
189            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
190             + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
191  };  };
192    
193  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
194  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
195    
196  /**  /**
197   * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.   * ccs_cap2keyword - Convert capability operation to capability name.
198   *   *
199   * @str: Unused.   * @operation: The capability index.
200   *   *
201   * Returns 0.   * Returns the name of the specified capability's name.
202   */   */
203  static int __init ccs_quiet_setup(char *str)  const char *ccs_cap2keyword(const u8 operation)
204  {  {
205          ccs_control_array[CCS_VERBOSE].current_value = 0;          return operation < CCS_MAX_CAPABILITY_INDEX
206          return 0;                  ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;
207  }  }
208    
209  __setup("CCS_QUIET", ccs_quiet_setup);  /**
210     * ccs_yesno - Return "yes" or "no".
211     *
212     * @value: Bool value.
213     */
214    static const char *ccs_yesno(const unsigned int value)
215    {
216            return value ? "yes" : "no";
217    }
218    
219  /**  /**
220   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
# Line 107  bool ccs_io_printf(struct ccs_io_buffer Line 250  bool ccs_io_printf(struct ccs_io_buffer
250   *   *
251   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
252   */   */
253  struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int
254                                                     profile)                                                            profile)
255  {  {
256          struct ccs_profile *ptr;          struct ccs_profile *ptr;
257          struct ccs_profile *entry;          struct ccs_profile *entry;
258          int i;          if (profile >= CCS_MAX_PROFILES)
         if (profile >= MAX_PROFILES)  
259                  return NULL;                  return NULL;
260          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
261          if (ptr)          if (ptr)
# Line 123  struct ccs_profile *ccs_find_or_assign_n Line 265  struct ccs_profile *ccs_find_or_assign_n
265          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
266          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
267                  ptr = entry;                  ptr = entry;
268                  for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)                  ptr->audit = &ccs_default_profile.preference;
269                          ptr->value[i] = ccs_control_array[i].current_value;                  ptr->learning = &ccs_default_profile.preference;
270                  /*                  ptr->permissive = &ccs_default_profile.preference;
271                   * Needn't to initialize "ptr->capability_value"                  ptr->enforcing = &ccs_default_profile.preference;
272                   * because they are always 0.                  ptr->default_config = CCS_CONFIG_DISABLED |
273                   */                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
274                    memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
275                           sizeof(ptr->config));
276                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
277                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
278                  entry = NULL;                  entry = NULL;
# Line 139  struct ccs_profile *ccs_find_or_assign_n Line 283  struct ccs_profile *ccs_find_or_assign_n
283  }  }
284    
285  /**  /**
286     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
287     */
288    void ccs_check_profile(void)
289    {
290            struct ccs_domain_info *domain;
291            ccs_policy_loaded = true;
292            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
293                    const u8 profile = domain->profile;
294                    if (ccs_profile_ptr[profile])
295                            continue;
296                    panic("Profile %u (used by '%s') not defined.\n",
297                          profile, domain->domainname->name);
298            }
299            if (ccs_profile_version != 20090903)
300                    panic("Profile version %u is not supported.\n",
301                          ccs_profile_version);
302    }
303    
304    /**
305     * ccs_profile - Find a profile.
306     *
307     * @profile: Profile number to find.
308     *
309     * Returns pointer to "struct ccs_profile".
310     */
311    struct ccs_profile *ccs_profile(const u8 profile)
312    {
313            struct ccs_profile *ptr = ccs_profile_ptr[profile];
314            if (!ccs_policy_loaded)
315                    return &ccs_default_profile;
316            BUG_ON(!ptr);
317            return ptr;
318    }
319    
320    /**
321   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
322   *   *
323   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 149  static int ccs_write_profile(struct ccs_ Line 328  static int ccs_write_profile(struct ccs_
328  {  {
329          char *data = head->write_buf;          char *data = head->write_buf;
330          unsigned int i;          unsigned int i;
331          unsigned int value;          int value;
332            int mode;
333            u8 config;
334            bool use_default = false;
335          char *cp;          char *cp;
336          struct ccs_profile *ccs_profile;          struct ccs_profile *profile;
337            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
338                    return 0;
339          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
340          if (data != cp) {          if (data == cp) {
341                    profile = &ccs_default_profile;
342            } else {
343                  if (*cp != '-')                  if (*cp != '-')
344                          return -EINVAL;                          return -EINVAL;
345                  data = cp + 1;                  data = cp + 1;
346                    profile = ccs_find_or_assign_new_profile(i);
347                    if (!profile)
348                            return -EINVAL;
349          }          }
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
                 return -EINVAL;  
350          cp = strchr(data, '=');          cp = strchr(data, '=');
351          if (!cp)          if (!cp)
352                  return -EINVAL;                  return -EINVAL;
353          *cp = '\0';          *cp++ = '\0';
354            if (profile != &ccs_default_profile)
355                    use_default = strstr(cp, "use_default") != NULL;
356            if (strstr(cp, "verbose=yes"))
357                    value = 1;
358            else if (strstr(cp, "verbose=no"))
359                    value = 0;
360            else
361                    value = -1;
362            if (!strcmp(data, "PREFERENCE::audit")) {
363    #ifdef CONFIG_CCSECURITY_AUDIT
364                    char *cp2;
365    #endif
366                    if (use_default) {
367                            profile->audit = &ccs_default_profile.preference;
368                            return 0;
369                    }
370                    profile->audit = &profile->preference;
371    #ifdef CONFIG_CCSECURITY_AUDIT
372                    cp2 = strstr(cp, "max_grant_log=");
373                    if (cp2)
374                            sscanf(cp2 + 14, "%u",
375                                   &profile->preference.audit_max_grant_log);
376                    cp2 = strstr(cp, "max_reject_log=");
377                    if (cp2)
378                            sscanf(cp2 + 15, "%u",
379                                   &profile->preference.audit_max_reject_log);
380    #endif
381                    if (strstr(cp, "task_info=yes"))
382                            profile->preference.audit_task_info = true;
383                    else if (strstr(cp, "task_info=no"))
384                            profile->preference.audit_task_info = false;
385                    if (strstr(cp, "path_info=yes"))
386                            profile->preference.audit_path_info = true;
387                    else if (strstr(cp, "path_info=no"))
388                            profile->preference.audit_path_info = false;
389                    return 0;
390            }
391            if (!strcmp(data, "PREFERENCE::enforcing")) {
392                    char *cp2;
393                    if (use_default) {
394                            profile->enforcing = &ccs_default_profile.preference;
395                            return 0;
396                    }
397                    profile->enforcing = &profile->preference;
398                    if (value >= 0)
399                            profile->preference.enforcing_verbose = value;
400                    cp2 = strstr(cp, "penalty=");
401                    if (cp2)
402                            sscanf(cp2 + 8, "%u",
403                                   &profile->preference.enforcing_penalty);
404                    return 0;
405            }
406            if (!strcmp(data, "PREFERENCE::permissive")) {
407                    if (use_default) {
408                            profile->permissive = &ccs_default_profile.preference;
409                            return 0;
410                    }
411                    profile->permissive = &profile->preference;
412                    if (value >= 0)
413                            profile->preference.permissive_verbose = value;
414                    return 0;
415            }
416            if (!strcmp(data, "PREFERENCE::learning")) {
417                    char *cp2;
418                    if (use_default) {
419                            profile->learning = &ccs_default_profile.preference;
420                            return 0;
421                    }
422                    profile->learning = &profile->preference;
423                    if (value >= 0)
424                            profile->preference.learning_verbose = value;
425                    cp2 = strstr(cp, "max_entry=");
426                    if (cp2)
427                            sscanf(cp2 + 10, "%u",
428                                   &profile->preference.learning_max_entry);
429                    if (strstr(cp, "exec.realpath=yes"))
430                            profile->preference.learning_exec_realpath = true;
431                    else if (strstr(cp, "exec.realpath=no"))
432                            profile->preference.learning_exec_realpath = false;
433                    if (strstr(cp, "exec.argv0=yes"))
434                            profile->preference.learning_exec_argv0 = true;
435                    else if (strstr(cp, "exec.argv0=no"))
436                            profile->preference.learning_exec_argv0 = false;
437                    if (strstr(cp, "symlink.target=yes"))
438                            profile->preference.learning_symlink_target = true;
439                    else if (strstr(cp, "symlink.target=no"))
440                            profile->preference.learning_symlink_target = false;
441                    return 0;
442            }
443            if (profile == &ccs_default_profile)
444                    return -EINVAL;
445          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
446                  const struct ccs_path_info *new_comment                  const struct ccs_path_info *old_comment = profile->comment;
447                          = ccs_get_name(cp + 1);                  profile->comment = ccs_get_name(cp);
                 const struct ccs_path_info *old_comment;  
                 /* Protect reader from ccs_put_name(). */  
                 /***** CRITICAL SECTION START *****/  
                 spin_lock(&ccs_profile_comment_lock);  
                 old_comment = ccs_profile->comment;  
                 ccs_profile->comment = new_comment;  
                 spin_unlock(&ccs_profile_comment_lock);  
                 /***** CRITICAL SECTION END *****/  
448                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
                 ccs_profile_entry_used[0] = true;  
449                  return 0;                  return 0;
450          }          }
451          if (ccs_str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {          if (!strcmp(data, "CONFIG")) {
452                  if (sscanf(cp + 1, "%u", &value) != 1) {                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
453                          for (i = 0; i < 4; i++) {                          + CCS_MAX_MAC_CATEGORY_INDEX;
454                                  if (strcmp(cp + 1, ccs_mode_4[i]))                  config = profile->default_config;
455                                          continue;          } else if (ccs_str_starts(&data, "CONFIG::")) {
456                                  value = i;                  config = 0;
457                                  break;                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
458                          }                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
459                          if (i == 4)                          if (strcmp(data, ccs_mac_keywords[i]))
                                 return -EINVAL;  
                 }  
                 if (value > 3)  
                         value = 3;  
                 for (i = 0; i < CCS_MAX_CAPABILITY_INDEX; i++) {  
                         if (strcmp(data, ccs_capability_control_keyword[i]))  
460                                  continue;                                  continue;
461                          ccs_profile->capability_value[i] = value;                          config = profile->config[i];
462                          ccs_profile_entry_used[i + 1 + CCS_MAX_CONTROL_INDEX]                          break;
                                 = true;  
                         return 0;  
463                  }                  }
464                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
465                        + CCS_MAX_MAC_CATEGORY_INDEX)
466                            return -EINVAL;
467            } else {
468                  return -EINVAL;                  return -EINVAL;
469          }          }
470          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {          if (use_default) {
471                  if (strcmp(data, ccs_control_array[i].keyword))                  config = CCS_CONFIG_USE_DEFAULT;
472                          continue;          } else {
473                  if (sscanf(cp + 1, "%u", &value) != 1) {                  for (mode = 3; mode >= 0; mode--)
474                          int j;                          if (strstr(cp, ccs_mode_4[mode]))
475                          const char **modes;                                  /*
476                          switch (i) {                                   * Update lower 3 bits in order to distinguish
477                          case CCS_RESTRICT_AUTOBIND:                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
478                          case CCS_VERBOSE:                                   */
479                                  modes = ccs_mode_2;                                  config = (config & ~7) | mode;
480                                  break;  #ifdef CONFIG_CCSECURITY_AUDIT
481                          default:                  if (config != CCS_CONFIG_USE_DEFAULT) {
482                                  modes = ccs_mode_4;                          if (strstr(cp, "grant_log=yes"))
483                                  break;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
484                          }                          else if (strstr(cp, "grant_log=no"))
485                          for (j = 0; j < 4; j++) {                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
486                                  if (strcmp(cp + 1, modes[j]))                          if (strstr(cp, "reject_log=yes"))
487                                          continue;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
488                                  value = j;                          else if (strstr(cp, "reject_log=no"))
489                                  break;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
                         }  
                         if (j == 4)  
                                 return -EINVAL;  
                 } else if (value > ccs_control_array[i].max_value) {  
                         value = ccs_control_array[i].max_value;  
490                  }                  }
491                  ccs_profile->value[i] = value;  #endif
                 ccs_profile_entry_used[i + 1] = true;  
                 return 0;  
492          }          }
493          return -EINVAL;          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
494                + CCS_MAX_MAC_CATEGORY_INDEX)
495                    profile->config[i] = config;
496            else if (config != CCS_CONFIG_USE_DEFAULT)
497                    profile->default_config = config;
498            return 0;
499  }  }
500    
501  /**  /**
502   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
503   *   *
504   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
505   */   */
506  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
507  {  {
508          static const int ccs_total          int index;
                 = CCS_MAX_CONTROL_INDEX + CCS_MAX_CAPABILITY_INDEX + 1;  
         int step;  
509          if (head->read_eof)          if (head->read_eof)
510                  return 0;                  return;
511          for (step = head->read_step; step < MAX_PROFILES * ccs_total; step++) {          if (head->read_bit)
512                  const u8 index = step / ccs_total;                  goto body;
513                  u8 type = step % ccs_total;          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
514                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];          ccs_io_printf(head, "PREFERENCE::audit={ "
515                  head->read_step = step;  #ifdef CONFIG_CCSECURITY_AUDIT
516                  if (!ccs_profile)                        "max_grant_log=%u max_reject_log=%u "
517                          continue;  #endif
518                  if (!ccs_profile_entry_used[type])                        "task_info=%s path_info=%s }\n",
519                          continue;  #ifdef CONFIG_CCSECURITY_AUDIT
520                  if (!type) { /* Print profile' comment tag. */                        ccs_default_profile.preference.audit_max_grant_log,
521                          bool done;                        ccs_default_profile.preference.audit_max_reject_log,
522                          /***** CRITICAL SECTION START *****/  #endif
523                          spin_lock(&ccs_profile_comment_lock);                        ccs_yesno(ccs_default_profile.preference.
524                          done = ccs_io_printf(head, "%u-COMMENT=%s\n",                                  audit_task_info),
525                                               index, ccs_profile->comment ?                        ccs_yesno(ccs_default_profile.preference.
526                                               ccs_profile->comment->name : "");                                  audit_path_info));
527                          spin_unlock(&ccs_profile_comment_lock);          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "
528                          /***** CRITICAL SECTION END *****/                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",
529                          if (!done)                        ccs_yesno(ccs_default_profile.preference.
530                                  break;                                  learning_verbose),
531                          ccs_default_profile.preference.learning_max_entry,
532                          ccs_yesno(ccs_default_profile.preference.
533                                    learning_exec_realpath),
534                          ccs_yesno(ccs_default_profile.preference.
535                                    learning_exec_argv0),
536                          ccs_yesno(ccs_default_profile.preference.
537                                    learning_symlink_target));
538            ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",
539                          ccs_yesno(ccs_default_profile.preference.
540                                    permissive_verbose));
541            ccs_io_printf(head, "PREFERENCE::enforcing={ verbose=%s penalty=%u "
542                          "}\n",
543                          ccs_yesno(ccs_default_profile.preference.
544                                    enforcing_verbose),
545                          ccs_default_profile.preference.enforcing_penalty);
546            head->read_bit = 1;
547     body:
548            for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {
549                    bool done;
550                    u8 config;
551                    int i;
552                    int pos;
553                    const struct ccs_profile *profile = ccs_profile_ptr[index];
554                    const struct ccs_path_info *comment;
555                    head->read_step = index;
556                    if (!profile)
557                          continue;                          continue;
558                    pos = head->read_avail;
559                    comment = profile->comment;
560                    done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,
561                                         comment ? comment->name : "");
562                    if (!done)
563                            goto out;
564                    config = profile->default_config;
565    #ifdef CONFIG_CCSECURITY_AUDIT
566                    if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "
567                                       "reject_log=%s }\n", index,
568                                       ccs_mode_4[config & 3],
569                                       ccs_yesno(config &
570                                                 CCS_CONFIG_WANT_GRANT_LOG),
571                                       ccs_yesno(config &
572                                                 CCS_CONFIG_WANT_REJECT_LOG)))
573                            goto out;
574    #else
575                    if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,
576                                       ccs_mode_4[config & 3]))
577                            goto out;
578    #endif
579                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
580                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
581    #ifdef CONFIG_CCSECURITY_AUDIT
582                            const char *g;
583                            const char *r;
584    #endif
585                            config = profile->config[i];
586                            if (config == CCS_CONFIG_USE_DEFAULT)
587                                    continue;
588    #ifdef CONFIG_CCSECURITY_AUDIT
589                            g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);
590                            r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);
591                            if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "
592                                               "grant_log=%s reject_log=%s }\n",
593                                               index, ccs_mac_keywords[i],
594                                               ccs_mode_4[config & 3], g, r))
595                                    goto out;
596    #else
597                            if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",
598                                               index, ccs_mac_keywords[i],
599                                               ccs_mode_4[config & 3]))
600                                    goto out;
601    #endif
602                  }                  }
603                  type--;                  if (profile->audit != &ccs_default_profile.preference &&
604                  if (type >= CCS_MAX_CONTROL_INDEX) {                      !ccs_io_printf(head, "%u-PREFERENCE::audit={ "
605                          const int i = type - CCS_MAX_CONTROL_INDEX;  #ifdef CONFIG_CCSECURITY_AUDIT
606                          const u8 value = ccs_profile->capability_value[i];                                     "max_grant_log=%u max_reject_log=%u "
607                          if (!ccs_io_printf(head,  #endif
608                                             "%u-" KEYWORD_MAC_FOR_CAPABILITY                                     "task_info=%s path_info=%s }\n", index,
609                                             "%s=%s\n", index,  #ifdef CONFIG_CCSECURITY_AUDIT
610                                             ccs_capability_control_keyword[i],                                     profile->preference.audit_max_grant_log,
611                                             ccs_mode_4[value]))                                     profile->preference.audit_max_reject_log,
612                                  break;  #endif
613                  } else {                                     ccs_yesno(profile->preference.
614                          const unsigned int value = ccs_profile->value[type];                                               audit_task_info),
615                          const char **modes = NULL;                                     ccs_yesno(profile->preference.
616                          const char *keyword = ccs_control_array[type].keyword;                                               audit_path_info)))
617                          switch (ccs_control_array[type].max_value) {                          goto out;
618                          case 3:                  if (profile->learning != &ccs_default_profile.preference &&
619                                  modes = ccs_mode_4;                      !ccs_io_printf(head, "%u-PREFERENCE::learning={ "
620                                  break;                                     "verbose=%s max_entry=%u exec.realpath=%s "
621                          case 1:                                     "exec.argv0=%s symlink.target=%s }\n",
622                                  modes = ccs_mode_2;                                     index,
623                                  break;                                     ccs_yesno(profile->preference.
624                          }                                               learning_verbose),
625                          if (modes) {                                     profile->preference.learning_max_entry,
626                                  if (!ccs_io_printf(head, "%u-%s=%s\n", index,                                     ccs_yesno(profile->preference.
627                                                     keyword, modes[value]))                                               learning_exec_realpath),
628                                          break;                                     ccs_yesno(profile->preference.
629                          } else {                                               learning_exec_argv0),
630                                  if (!ccs_io_printf(head, "%u-%s=%u\n", index,                                     ccs_yesno(profile->preference.
631                                                     keyword, value))                                               learning_symlink_target)))
632                                          break;                          goto out;
633                          }                  if (profile->permissive != &ccs_default_profile.preference &&
634                  }                      !ccs_io_printf(head, "%u-PREFERENCE::permissive={ "
635                                       "verbose=%s }\n", index,
636                                       ccs_yesno(profile->preference.
637                                                 permissive_verbose)))
638                            goto out;
639                    if (profile->enforcing != &ccs_default_profile.preference &&
640                        !ccs_io_printf(head, "%u-PREFERENCE::enforcing={ "
641                                       "verbose=%s penalty=%u }\n", index,
642                                       ccs_yesno(profile->preference.
643                                                 enforcing_verbose),
644                                       profile->preference.enforcing_penalty))
645                            goto out;
646                    continue;
647     out:
648                    head->read_avail = pos;
649                    break;
650          }          }
651          if (step == MAX_PROFILES * ccs_total)          if (index == CCS_MAX_PROFILES)
652                  head->read_eof = true;                  head->read_eof = true;
         return 0;  
653  }  }
654    
655  /* The list for "struct ccs_policy_manager_entry". */  /* The list for "struct ccs_policy_manager_entry". */
# Line 325  static int ccs_update_manager_entry(cons Line 667  static int ccs_update_manager_entry(cons
667  {  {
668          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_policy_manager_entry *entry = NULL;
669          struct ccs_policy_manager_entry *ptr;          struct ccs_policy_manager_entry *ptr;
670          const struct ccs_path_info *saved_manager;          struct ccs_policy_manager_entry e = { };
671          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
         bool is_domain = false;  
672          if (ccs_is_domain_def(manager)) {          if (ccs_is_domain_def(manager)) {
673                  if (!ccs_is_correct_domain(manager))                  if (!ccs_is_correct_domain(manager))
674                          return -EINVAL;                          return -EINVAL;
675                  is_domain = true;                  e.is_domain = true;
676          } else {          } else {
677                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_is_correct_path(manager, 1, -1, -1))
678                          return -EINVAL;                          return -EINVAL;
679          }          }
680          saved_manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
681          if (!saved_manager)          if (!e.manager)
682                  return -ENOMEM;                  return -ENOMEM;
683          if (!is_delete)          if (!is_delete)
684                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                  entry = kmalloc(sizeof(e), GFP_KERNEL);
685          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
686          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
687                  if (ptr->manager != saved_manager)                  if (ptr->manager != e.manager)
688                          continue;                          continue;
689                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
690                  error = 0;                  error = 0;
691                  break;                  break;
692          }          }
693          if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
                 entry->manager = saved_manager;  
                 saved_manager = NULL;  
                 entry->is_domain = is_domain;  
694                  list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);                  list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);
695                  entry = NULL;                  entry = NULL;
696                  error = 0;                  error = 0;
697          }          }
698          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
699          ccs_put_name(saved_manager);          ccs_put_name(e.manager);
700          kfree(entry);          kfree(entry);
701          return error;          return error;
702  }  }
# Line 373  static int ccs_update_manager_entry(cons Line 711  static int ccs_update_manager_entry(cons
711  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager_policy(struct ccs_io_buffer *head)
712  {  {
713          char *data = head->write_buf;          char *data = head->write_buf;
714          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
715          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
716                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
717                  return 0;                  return 0;
# Line 386  static int ccs_write_manager_policy(stru Line 724  static int ccs_write_manager_policy(stru
724   *   *
725   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
726   *   *
  * Returns 0.  
  *  
727   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
728   */   */
729  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager_policy(struct ccs_io_buffer *head)
730  {  {
731          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
732          if (head->read_eof)          if (head->read_eof)
733                  return 0;                  return;
734          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {
735                  struct ccs_policy_manager_entry *ptr;                  struct ccs_policy_manager_entry *ptr;
736                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);
737                  if (ptr->is_deleted)                  if (ptr->is_deleted)
738                          continue;                          continue;
739                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
740                          return 0;                          return;
741          }          }
742          head->read_eof = true;          head->read_eof = true;
         return 0;  
743  }  }
744    
745  /**  /**
# Line 424  static bool ccs_is_policy_manager(void) Line 758  static bool ccs_is_policy_manager(void)
758          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
759                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
760          bool found = false;          bool found = false;
         ccs_check_read_lock();  
761          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
762                  return true;                  return true;
763          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)
# Line 505  static bool ccs_is_select_one(struct ccs Line 838  static bool ccs_is_select_one(struct ccs
838  {  {
839          unsigned int pid;          unsigned int pid;
840          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
841          ccs_check_read_lock();          bool global_pid = false;
842          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "allow_execute")) {
843                  head->read_execute_only = true;                  head->read_execute_only = true;
844                  return true;                  return true;
845          }          }
846          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
847                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
848                  struct task_struct *p;                  struct task_struct *p;
849                  /***** CRITICAL SECTION START *****/                  ccs_tasklist_lock();
850                  read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
851                    if (global_pid)
852                            p = find_task_by_pid_ns(pid, &init_pid_ns);
853                    else
854                            p = find_task_by_vpid(pid);
855    #else
856                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
857    #endif
858                  if (p)                  if (p)
859                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
860                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
                 /***** CRITICAL SECTION END *****/  
861          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
862                  if (ccs_is_domain_def(data + 7))                  if (ccs_is_domain_def(data + 7))
863                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
# Line 549  static bool ccs_is_select_one(struct ccs Line 888  static bool ccs_is_select_one(struct ccs
888          return true;          return true;
889  }  }
890    
891    static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,
892                                        struct ccs_condition *cond,
893                                        const bool is_delete)
894    {
895            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))
896                    return ccs_write_capability_policy(data, domain, cond,
897                                                       is_delete);
898            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))
899                    return ccs_write_network_policy(data, domain, cond, is_delete);
900            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))
901                    return ccs_write_signal_policy(data, domain, cond, is_delete);
902            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))
903                    return ccs_write_env_policy(data, domain, cond, is_delete);
904            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))
905                    return ccs_write_mount_policy(data, domain, cond, is_delete);
906            return ccs_write_file_policy(data, domain, cond, is_delete);
907    }
908    
909  /**  /**
910   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain_policy - Write domain policy.
911   *   *
# Line 566  static int ccs_write_domain_policy(struc Line 923  static int ccs_write_domain_policy(struc
923          struct ccs_condition *cond = NULL;          struct ccs_condition *cond = NULL;
924          char *cp;          char *cp;
925          int error;          int error;
926          if (ccs_str_starts(&data, KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
927                  is_delete = true;                  is_delete = true;
928          else if (ccs_str_starts(&data, KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
929                  is_select = true;                  is_select = true;
930          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_is_select_one(head, data))
931                  return 0;                  return 0;
# Line 589  static int ccs_write_domain_policy(struc Line 946  static int ccs_write_domain_policy(struc
946          if (!domain)          if (!domain)
947                  return -EINVAL;                  return -EINVAL;
948    
949          if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
950              && profile < MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
951                  if (ccs_profile_ptr[profile] || !ccs_policy_loaded)                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
952                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
953                  return 0;                  return 0;
954          }          }
955          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
956                  domain->ignore_global_allow_read = !is_delete;                  domain->ignore_global_allow_read = !is_delete;
957                  return 0;                  return 0;
958          }          }
959          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
960                  domain->ignore_global_allow_env = !is_delete;                  domain->ignore_global_allow_env = !is_delete;
961                  return 0;                  return 0;
962          }          }
963            if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {
964                    domain->quota_warned = !is_delete;
965                    return 0;
966            }
967            if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {
968                    domain->domain_transition_failed = !is_delete;
969                    return 0;
970            }
971          cp = ccs_find_condition_part(data);          cp = ccs_find_condition_part(data);
972          if (cp) {          if (cp) {
973                  cond = ccs_get_condition(cp);                  cond = ccs_get_condition(cp);
974                  if (!cond)                  if (!cond)
975                          return -EINVAL;                          return -EINVAL;
976          }          }
977          if (ccs_str_starts(&data, KEYWORD_ALLOW_CAPABILITY))          error = ccs_write_domain_policy2(data, domain, cond, is_delete);
                 error = ccs_write_capability_policy(data, domain, cond,  
                                                     is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_NETWORK))  
                 error = ccs_write_network_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_SIGNAL))  
                 error = ccs_write_signal_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_ARGV0))  
                 error = ccs_write_argv0_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))  
                 error = ccs_write_env_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_MOUNT))  
                 error = ccs_write_mount_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_UNMOUNT))  
                 error = ccs_write_umount_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_CHROOT))  
                 error = ccs_write_chroot_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT))  
                 error = ccs_write_pivot_root_policy(data, domain, cond,  
                                                     is_delete);  
         else  
                 error = ccs_write_file_policy(data, domain, cond, is_delete);  
978          if (cond)          if (cond)
979                  ccs_put_condition(cond);                  ccs_put_condition(cond);
980          return error;          return error;
981  }  }
982    
983    /**
984     * ccs_print_name_union - Print a ccs_name_union.
985     *
986     * @head: Pointer to "struct ccs_io_buffer".
987     * @ptr:  Pointer to "struct ccs_name_union".
988     *
989     * Returns true on success, false otherwise.
990     */
991  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static bool ccs_print_name_union(struct ccs_io_buffer *head,
992                                   struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
993  {  {
994          const int pos = head->read_avail;          int pos = head->read_avail;
995          if (pos && head->read_buf[pos - 1] == ' ')          if (pos && head->read_buf[pos - 1] == ' ')
996                  head->read_avail--;                  head->read_avail--;
997          if (ptr->is_group)          if (ptr->is_group)
# Line 648  static bool ccs_print_name_union(struct Line 1000  static bool ccs_print_name_union(struct
1000          return ccs_io_printf(head, " %s", ptr->filename->name);          return ccs_io_printf(head, " %s", ptr->filename->name);
1001  }  }
1002    
1003  static bool ccs_print_number_union(struct ccs_io_buffer *head,  /**
1004                                     struct ccs_number_union *ptr)   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.
1005     *
1006     * @head: Pointer to "struct ccs_io_buffer".
1007     * @ptr:  Pointer to "struct ccs_name_union".
1008     *
1009     * Returns true on success, false otherwise.
1010     */
1011    static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1012                                            const struct ccs_name_union *ptr)
1013    {
1014            if (ptr->is_group)
1015                    return ccs_io_printf(head, "@%s",
1016                                         ptr->group->group_name->name);
1017            return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1018    }
1019    
1020    /**
1021     * ccs_print_number_union_common - Print a ccs_number_union.
1022     *
1023     * @head:       Pointer to "struct ccs_io_buffer".
1024     * @ptr:        Pointer to "struct ccs_number_union".
1025     * @need_space: True if a space character is needed.
1026     *
1027     * Returns true on success, false otherwise.
1028     */
1029    static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
1030                                              const struct ccs_number_union *ptr,
1031                                              const bool need_space)
1032  {  {
1033          unsigned long min;          unsigned long min;
1034          unsigned long max;          unsigned long max;
1035            u8 min_type;
1036            u8 max_type;
1037            if (need_space && !ccs_io_printf(head, " "))
1038                    return false;
1039          if (ptr->is_group)          if (ptr->is_group)
1040                  return ccs_io_printf(head, " @%s",                  return ccs_io_printf(head, "@%s",
1041                                       ptr->group->group_name->name);                                       ptr->group->group_name->name);
1042            min_type = ptr->min_type;
1043            max_type = ptr->max_type;
1044          min = ptr->values[0];          min = ptr->values[0];
1045          max = ptr->values[1];          max = ptr->values[1];
1046          if (min == max)          switch (min_type) {
1047                  return ccs_io_printf(head, " %lu", min);          case CCS_VALUE_TYPE_HEXADECIMAL:
1048          return ccs_io_printf(head, " %lu-%lu", min, max);                  if (!ccs_io_printf(head, "0x%lX", min))
1049                            return false;
1050                    break;
1051            case CCS_VALUE_TYPE_OCTAL:
1052                    if (!ccs_io_printf(head, "0%lo", min))
1053                            return false;
1054                    break;
1055            default:
1056                    if (!ccs_io_printf(head, "%lu", min))
1057                            return false;
1058                    break;
1059            }
1060            if (min == max && min_type == max_type)
1061                    return true;
1062            switch (max_type) {
1063            case CCS_VALUE_TYPE_HEXADECIMAL:
1064                    return ccs_io_printf(head, "-0x%lX", max);
1065            case CCS_VALUE_TYPE_OCTAL:
1066                    return ccs_io_printf(head, "-0%lo", max);
1067            default:
1068                    return ccs_io_printf(head, "-%lu", max);
1069            }
1070  }  }
1071    
1072  /**  /**
1073   * ccs_print_single_path_acl - Print a single path ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1074     *
1075     * @head:       Pointer to "struct ccs_io_buffer".
1076     * @ptr:        Pointer to "struct ccs_number_union".
1077     *
1078     * Returns true on success, false otherwise.
1079     */
1080    bool ccs_print_number_union(struct ccs_io_buffer *head,
1081                                const struct ccs_number_union *ptr)
1082    {
1083            return ccs_print_number_union_common(head, ptr, true);
1084    }
1085    
1086    /**
1087     * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.
1088     *
1089     * @head:       Pointer to "struct ccs_io_buffer".
1090     * @ptr:        Pointer to "struct ccs_number_union".
1091     *
1092     * Returns true on success, false otherwise.
1093     */
1094    static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1095                                               const struct ccs_number_union *ptr)
1096    {
1097            return ccs_print_number_union_common(head, ptr, false);
1098    }
1099    
1100    /**
1101     * ccs_print_condition - Print condition part.
1102   *   *
1103   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to "struct ccs_single_path_acl_record".  
1104   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1105   *   *
1106   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1107   */   */
1108  static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1109                                        struct ccs_single_path_acl_record *ptr,                                  const struct ccs_condition *cond)
1110                                        const struct ccs_condition *cond)  {
1111            const struct ccs_condition_element *condp;
1112            const struct ccs_number_union *numbers_p;
1113            const struct ccs_name_union *names_p;
1114            const struct ccs_argv_entry *argv;
1115            const struct ccs_envp_entry *envp;
1116            u16 condc;
1117            u16 i;
1118            u16 j;
1119            char buffer[32];
1120            if (!cond)
1121                    goto no_condition;
1122            condc = cond->condc;
1123            condp = (const struct ccs_condition_element *) (cond + 1);
1124            numbers_p = (const struct ccs_number_union *) (condp + condc);
1125            names_p = (const struct ccs_name_union *)
1126                    (numbers_p + cond->numbers_count);
1127            argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);
1128            envp = (const struct ccs_envp_entry *) (argv + cond->argc);
1129            memset(buffer, 0, sizeof(buffer));
1130            if (condc && !ccs_io_printf(head, "%s", " if"))
1131                    goto out;
1132            for (i = 0; i < condc; i++) {
1133                    const u8 match = condp->equals;
1134                    const u8 left = condp->left;
1135                    const u8 right = condp->right;
1136                    condp++;
1137                    switch (left) {
1138                    case CCS_ARGV_ENTRY:
1139                            if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",
1140                                               argv->index, argv->is_not ?
1141                                               "!=" : "=", argv->value->name))
1142                                    goto out;
1143                            argv++;
1144                            continue;
1145                    case CCS_ENVP_ENTRY:
1146                            if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",
1147                                               envp->name->name, envp->is_not ?
1148                                               "!=" : "="))
1149                                    goto out;
1150                            if (envp->value) {
1151                                    if (!ccs_io_printf(head, "\"%s\"",
1152                                                       envp->value->name))
1153                                            goto out;
1154                            } else {
1155                                    if (!ccs_io_printf(head, "NULL"))
1156                                            goto out;
1157                            }
1158                            envp++;
1159                            continue;
1160                    case CCS_NUMBER_UNION:
1161                            if (!ccs_print_number_union(head, numbers_p++))
1162                                    goto out;
1163                            break;
1164                    default:
1165                            if (left >= CCS_MAX_CONDITION_KEYWORD)
1166                                    goto out;
1167                            if (!ccs_io_printf(head, " %s",
1168                                               ccs_condition_keyword[left]))
1169                                    goto out;
1170                            break;
1171                    }
1172                    if (!ccs_io_printf(head, "%s", match ? "=" : "!="))
1173                            goto out;
1174                    switch (right) {
1175                    case CCS_NAME_UNION:
1176                            if (!ccs_print_name_union_quoted(head, names_p++))
1177                                    goto out;
1178                            break;
1179                    case CCS_NUMBER_UNION:
1180                            if (!ccs_print_number_union_nospace(head, numbers_p++))
1181                                    goto out;
1182                            break;
1183                    default:
1184                            if (right >= CCS_MAX_CONDITION_KEYWORD)
1185                                    goto out;
1186                            if (!ccs_io_printf(head, "%s",
1187                                               ccs_condition_keyword[right]))
1188                                    goto out;
1189                            break;
1190                    }
1191            }
1192            i = cond->post_state[3];
1193            if (!i)
1194                    goto no_condition;
1195            if (!ccs_io_printf(head, " ; set"))
1196                    goto out;
1197            for (j = 0; j < 3; j++) {
1198                    if (!(i & (1 << j)))
1199                            continue;
1200                    if (!ccs_io_printf(head, " task.state[%u]=%u", j,
1201                                       cond->post_state[j]))
1202                            goto out;
1203            }
1204     no_condition:
1205            if (ccs_io_printf(head, "\n"))
1206                    return true;
1207     out:
1208            return false;
1209    }
1210    
1211    /**
1212     * ccs_print_path_acl - Print a path ACL entry.
1213     *
1214     * @head: Pointer to "struct ccs_io_buffer".
1215     * @ptr:  Pointer to "struct ccs_path_acl".
1216     * @cond: Pointer to "struct ccs_condition". May be NULL.
1217     *
1218     * Returns true on success, false otherwise.
1219     */
1220    static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1221                                   struct ccs_path_acl *ptr,
1222                                   const struct ccs_condition *cond)
1223  {  {
1224          int pos;          int pos;
1225          u8 bit;          u8 bit;
1226          const u16 perm = ptr->perm;          const u16 perm = ptr->perm;
1227          for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
                 const char *msg;  
1228                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1229                          continue;                          continue;
1230                  if (head->read_execute_only && bit != TYPE_EXECUTE_ACL)                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)
1231                          continue;                          continue;
1232                  /* Print "read/write" instead of "read" and "write". */                  /* Print "read/write" instead of "read" and "write". */
1233                  if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1234                      && (perm & (1 << TYPE_READ_WRITE_ACL)))                      && (perm & (1 << CCS_TYPE_READ_WRITE)))
1235                          continue;                          continue;
                 msg = ccs_sp2keyword(bit);  
1236                  pos = head->read_avail;                  pos = head->read_avail;
1237                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||
1238                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1239                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1240                          goto out;                          head->read_bit = bit;
1241                            head->read_avail = pos;
1242                            return false;
1243                    }
1244          }          }
1245          head->read_bit = 0;          head->read_bit = 0;
1246          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1247  }  }
1248    
1249  /**  /**
1250   * ccs_print_mkdev_acl - Print a mkdev ACL entry.   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.
1251   *   *
1252   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1253   * @ptr:  Pointer to "struct ccs_mkdev_acl_record".   * @ptr:  Pointer to "struct ccs_path_number3_acl".
1254   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1255   *   *
1256   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1257   */   */
1258  static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,
1259                                  struct ccs_mkdev_acl_record *ptr,                                         struct ccs_path_number3_acl *ptr,
1260                                  const struct ccs_condition *cond)                                         const struct ccs_condition *cond)
1261  {  {
1262          int pos;          int pos;
1263          u8 bit;          u8 bit;
1264          const u16 perm = ptr->perm;          const u16 perm = ptr->perm;
1265          for (bit = head->read_bit; bit < MAX_MKDEV_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;
1266                  const char *msg;               bit++) {
1267                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1268                          continue;                          continue;
                 msg = ccs_mkdev2keyword(bit);  
1269                  pos = head->read_avail;                  pos = head->read_avail;
1270                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s",
1271                                       ccs_path_number32keyword(bit)) ||
1272                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1273                        !ccs_print_number_union(head, &ptr->mode) ||
1274                      !ccs_print_number_union(head, &ptr->major) ||                      !ccs_print_number_union(head, &ptr->major) ||
1275                      !ccs_print_number_union(head, &ptr->minor) ||                      !ccs_print_number_union(head, &ptr->minor) ||
1276                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1277                          goto out;                          head->read_bit = bit;
1278                            head->read_avail = pos;
1279                            return false;
1280                    }
1281          }          }
1282          head->read_bit = 0;          head->read_bit = 0;
1283          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1284  }  }
1285    
1286  /**  /**
1287   * ccs_print_double_path_acl - Print a double path ACL entry.   * ccs_print_path2_acl - Print a path2 ACL entry.
1288   *   *
1289   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1290   * @ptr:  Pointer to "struct ccs_double_path_acl_record".   * @ptr:  Pointer to "struct ccs_path2_acl".
1291   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1292   *   *
1293   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1294   */   */
1295  static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1296                                        struct ccs_double_path_acl_record *ptr,                                  struct ccs_path2_acl *ptr,
1297                                        const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1298  {  {
1299          int pos;          int pos;
1300          u8 bit;          u8 bit;
1301          const u8 perm = ptr->perm;          const u8 perm = ptr->perm;
1302          for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {
                 const char *msg;  
1303                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1304                          continue;                          continue;
                 msg = ccs_dp2keyword(bit);  
1305                  pos = head->read_avail;                  pos = head->read_avail;
1306                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s",
1307                                       ccs_path22keyword(bit)) ||
1308                      !ccs_print_name_union(head, &ptr->name1) ||                      !ccs_print_name_union(head, &ptr->name1) ||
1309                      !ccs_print_name_union(head, &ptr->name2) ||                      !ccs_print_name_union(head, &ptr->name2) ||
1310                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1311                          goto out;                          head->read_bit = bit;
1312                            head->read_avail = pos;
1313                            return false;
1314                    }
1315          }          }
1316          head->read_bit = 0;          head->read_bit = 0;
1317          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1318  }  }
1319    
1320  /**  /**
1321   * ccs_print_path_number_acl - Print an ioctl/chmod/chown/chgrp ACL entry.   * ccs_print_path_number_acl - Print a path_number ACL entry.
1322   *   *
1323   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1324   * @ptr:  Pointer to "struct ccs_path_number_acl_record".   * @ptr:  Pointer to "struct ccs_path_number_acl".
1325   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1326   *   *
1327   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1328   */   */
1329  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1330                                        struct ccs_path_number_acl_record *ptr,                                        struct ccs_path_number_acl *ptr,
1331                                        const struct ccs_condition *cond)                                        const struct ccs_condition *cond)
1332  {  {
1333          int pos;          int pos;
1334          u8 bit;          u8 bit;
1335          const u8 perm = ptr->perm;          const u8 perm = ptr->perm;
1336          for (bit = head->read_bit; bit < MAX_PATH_NUMBER_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1337                  const char *msg;               bit++) {
1338                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1339                          continue;                          continue;
                 msg = ccs_path_number2keyword(bit);  
1340                  pos = head->read_avail;                  pos = head->read_avail;
1341                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s",
1342                                       ccs_path_number2keyword(bit)) ||
1343                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1344                      !ccs_print_number_union(head, &ptr->number) ||                      !ccs_print_number_union(head, &ptr->number) ||
1345                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1346                          goto out;                          head->read_bit = bit;
1347                            head->read_avail = pos;
1348                            return false;
1349                    }
1350          }          }
1351          head->read_bit = 0;          head->read_bit = 0;
1352          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_argv0_acl - Print an argv[0] ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_argv0_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,  
                                 struct ccs_argv0_acl_record *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",  
                            ptr->filename->name, ptr->argv0->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
1353  }  }
1354    
1355  /**  /**
1356   * ccs_print_env_acl - Print an evironment variable name's ACL entry.   * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1357   *   *
1358   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1359   * @ptr:  Pointer to "struct ccs_env_acl_record".   * @ptr:  Pointer to "struct ccs_env_acl".
1360   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1361   *   *
1362   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1363   */   */
1364  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1365                                struct ccs_env_acl_record *ptr,                                struct ccs_env_acl *ptr,
1366                                const struct ccs_condition *cond)                                const struct ccs_condition *cond)
1367  {  {
1368          int pos = head->read_avail;          const int pos = head->read_avail;
1369          if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1370                  goto out;              !ccs_print_condition(head, cond)) {
1371          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1372                  goto out;                  return false;
1373            }
1374          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1375  }  }
1376    
1377  /**  /**
1378   * ccs_print_capability_acl - Print a capability ACL entry.   * ccs_print_capability_acl - Print a capability ACL entry.
1379   *   *
1380   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1381   * @ptr:  Pointer to "struct ccs_capability_acl_record".   * @ptr:  Pointer to "struct ccs_capability_acl".
1382   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1383   *   *
1384   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1385   */   */
1386  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1387                                       struct ccs_capability_acl_record *ptr,                                       struct ccs_capability_acl *ptr,
1388                                       const struct ccs_condition *cond)                                       const struct ccs_condition *cond)
1389  {  {
1390          int pos = head->read_avail;          const int pos = head->read_avail;
1391          if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1392                             ccs_cap2keyword(ptr->operation)))                             ccs_cap2keyword(ptr->operation)) ||
1393                  goto out;              !ccs_print_condition(head, cond)) {
1394          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1395                  goto out;                  return false;
1396            }
1397          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1398  }  }
1399    
1400  /**  /**
1401   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.
1402   *   *
1403   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1404   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
1405   *   *
1406   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1407   */   */
1408  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,
1409                                   struct ccs_ip_network_acl_record *ptr)                                   struct ccs_ip_network_acl *ptr)
1410  {  {
1411          const u32 min_address = ptr->address.ipv4.min;          const u32 min_address = ptr->address.ipv4.min;
1412          const u32 max_address = ptr->address.ipv4.max;          const u32 max_address = ptr->address.ipv4.max;
# Line 912  static bool ccs_print_ipv4_entry(struct Line 1422  static bool ccs_print_ipv4_entry(struct
1422   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.
1423   *   *
1424   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1425   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
1426   *   *
1427   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1428   */   */
1429  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,
1430                                   struct ccs_ip_network_acl_record *ptr)                                   struct ccs_ip_network_acl *ptr)
1431  {  {
1432          char buf[64];          char buf[64];
1433          const struct in6_addr *min_address = ptr->address.ipv6.min;          const struct in6_addr *min_address = ptr->address.ipv6.min;
# Line 937  static bool ccs_print_ipv6_entry(struct Line 1447  static bool ccs_print_ipv6_entry(struct
1447   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_network_acl - Print a network ACL entry.
1448   *   *
1449   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1450   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
1451   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1452   *   *
1453   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1454   */   */
1455  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1456                                    struct ccs_ip_network_acl_record *ptr,                                    struct ccs_ip_network_acl *ptr,
1457                                    const struct ccs_condition *cond)                                    const struct ccs_condition *cond)
1458  {  {
1459          int pos = head->read_avail;          int pos;
1460          if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",          u8 bit;
1461                             ccs_net2keyword(ptr->operation_type)))          const u16 perm = ptr->perm;
1462                  goto out;          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1463          switch (ptr->record_type) {                  if (!(perm & (1 << bit)))
1464          case IP_RECORD_TYPE_ADDRESS_GROUP:                          continue;
1465                  if (!ccs_io_printf(head, "@%s",                  pos = head->read_avail;
1466                                     ptr->address.group->group_name->name))                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",
1467                          goto out;                                     ccs_net2keyword(bit)))
                 break;  
         case IP_RECORD_TYPE_IPv4:  
                 if (!ccs_print_ipv4_entry(head, ptr))  
1468                          goto out;                          goto out;
1469                  break;                  switch (ptr->address_type) {
1470          case IP_RECORD_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1471                  if (!ccs_print_ipv6_entry(head, ptr))                          if (!ccs_io_printf(head, "@%s", ptr->address.group->
1472                                               group_name->name))
1473                                    goto out;
1474                            break;
1475                    case CCS_IP_ADDRESS_TYPE_IPv4:
1476                            if (!ccs_print_ipv4_entry(head, ptr))
1477                                    goto out;
1478                            break;
1479                    case CCS_IP_ADDRESS_TYPE_IPv6:
1480                            if (!ccs_print_ipv6_entry(head, ptr))
1481                                    goto out;
1482                            break;
1483                    }
1484                    if (!ccs_print_number_union(head, &ptr->port) ||
1485                        !ccs_print_condition(head, cond))
1486                          goto out;                          goto out;
                 break;  
1487          }          }
1488          if (!ccs_print_number_union(head, &ptr->port) ||          head->read_bit = 0;
             !ccs_print_condition(head, cond))  
                 goto out;  
1489          return true;          return true;
1490   out:   out:
1491            head->read_bit = bit;
1492          head->read_avail = pos;          head->read_avail = pos;
1493          return false;          return false;
1494  }  }
# Line 978  static bool ccs_print_network_acl(struct Line 1497  static bool ccs_print_network_acl(struct
1497   * ccs_print_signal_acl - Print a signal ACL entry.   * ccs_print_signal_acl - Print a signal ACL entry.
1498   *   *
1499   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1500   * @ptr:  Pointer to "struct signale_acl_record".   * @ptr:  Pointer to "struct signale_acl".
1501   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1502   *   *
1503   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1504   */   */
1505  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1506                                   struct ccs_signal_acl_record *ptr,                                   struct ccs_signal_acl *ptr,
1507                                   const struct ccs_condition *cond)                                   const struct ccs_condition *cond)
1508  {  {
1509          int pos = head->read_avail;          const int pos = head->read_avail;
1510          if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1511                             ptr->sig, ptr->domainname->name))                             ptr->sig, ptr->domainname->name) ||
1512                  goto out;              !ccs_print_condition(head, cond)) {
1513          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1514                  goto out;                  return false;
1515            }
1516          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1517  }  }
1518    
1519  /**  /**
# Line 1020  static bool ccs_print_execute_handler_re Line 1537  static bool ccs_print_execute_handler_re
1537   * ccs_print_mount_acl - Print a mount ACL entry.   * ccs_print_mount_acl - Print a mount ACL entry.
1538   *   *
1539   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1540   * @ptr:  Pointer to "struct ccs_mount_acl_record".   * @ptr:  Pointer to "struct ccs_mount_acl".
1541   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1542   *   *
1543   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1544   */   */
1545  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1546                                  struct ccs_mount_acl_record *ptr,                                  struct ccs_mount_acl *ptr,
1547                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1548  {  {
1549          int pos = head->read_avail;          const int pos = head->read_avail;
1550          if (!ccs_io_printf(head, KEYWORD_ALLOW_MOUNT "%s %s %s 0x%lX\n",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1551                             ptr->dev_name->name, ptr->dir_name->name,              !ccs_print_name_union(head, &ptr->dev_name) ||
1552                             ptr->fs_type->name, ptr->flags))              !ccs_print_name_union(head, &ptr->dir_name) ||
1553                  goto out;              !ccs_print_name_union(head, &ptr->fs_type) ||
1554          if (!ccs_print_condition(head, cond))              !ccs_print_number_union(head, &ptr->flags) ||
1555                  goto out;              !ccs_print_condition(head, cond)) {
1556          return true;                  head->read_avail = pos;
1557   out:                  return false;
1558          head->read_avail = pos;          }
         return false;  
 }  
   
 /**  
  * ccs_print_umount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_umount_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_umount_acl(struct ccs_io_buffer *head,  
                                  struct ccs_umount_acl_record *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_UNMOUNT "%s\n",  
                            ptr->dir->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_chroot_acl - Print a chroot ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_chroot_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,  
                                  struct ccs_chroot_acl_record *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_CHROOT "%s\n",  
                            ptr->dir->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_pivot_root_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  
                                      struct ccs_pivot_root_acl_record *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_PIVOT_ROOT "%s %s\n",  
                            ptr->new_root->name, ptr->old_root->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
1559          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1560  }  }
1561    
1562  /**  /**
# Line 1129  static bool ccs_print_entry(struct ccs_i Line 1571  static bool ccs_print_entry(struct ccs_i
1571                              struct ccs_acl_info *ptr)                              struct ccs_acl_info *ptr)
1572  {  {
1573          const struct ccs_condition *cond = ptr->cond;          const struct ccs_condition *cond = ptr->cond;
1574          const u8 acl_type = ccs_acl_type2(ptr);          const u8 acl_type = ptr->type;
1575          if (acl_type & ACL_DELETED)          if (ptr->is_deleted)
1576                  return true;                  return true;
1577          if (acl_type == TYPE_SINGLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH_ACL) {
1578                  struct ccs_single_path_acl_record *acl                  struct ccs_path_acl *acl
1579                          = container_of(ptr, struct ccs_single_path_acl_record,                          = container_of(ptr, struct ccs_path_acl, head);
1580                                         head);                  return ccs_print_path_acl(head, acl, cond);
                 return ccs_print_single_path_acl(head, acl, cond);  
1581          }          }
1582          if (acl_type == TYPE_EXECUTE_HANDLER) {          if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {
1583                  struct ccs_execute_handler_record *acl                  struct ccs_execute_handler_record *acl
1584                          = container_of(ptr, struct ccs_execute_handler_record,                          = container_of(ptr, struct ccs_execute_handler_record,
1585                                         head);                                         head);
1586                  const char *keyword = KEYWORD_EXECUTE_HANDLER;                  const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;
1587                  return ccs_print_execute_handler_record(head, keyword, acl);                  return ccs_print_execute_handler_record(head, keyword, acl);
1588          }          }
1589          if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {          if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1590                  struct ccs_execute_handler_record *acl                  struct ccs_execute_handler_record *acl
1591                          = container_of(ptr, struct ccs_execute_handler_record,                          = container_of(ptr, struct ccs_execute_handler_record,
1592                                         head);                                         head);
1593                  const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;                  const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1594                  return ccs_print_execute_handler_record(head, keyword, acl);                  return ccs_print_execute_handler_record(head, keyword, acl);
1595          }          }
1596          if (head->read_execute_only)          if (head->read_execute_only)
1597                  return true;                  return true;
1598          if (acl_type == TYPE_MKDEV_ACL) {          if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {
1599                  struct ccs_mkdev_acl_record *acl                  struct ccs_path_number3_acl *acl
1600                          = container_of(ptr, struct ccs_mkdev_acl_record, head);                          = container_of(ptr, struct ccs_path_number3_acl, head);
1601                  return ccs_print_mkdev_acl(head, acl, cond);                  return ccs_print_path_number3_acl(head, acl, cond);
1602          }          }
1603          if (acl_type == TYPE_DOUBLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH2_ACL) {
1604                  struct ccs_double_path_acl_record *acl                  struct ccs_path2_acl *acl
1605                          = container_of(ptr, struct ccs_double_path_acl_record,                          = container_of(ptr, struct ccs_path2_acl, head);
1606                                         head);                  return ccs_print_path2_acl(head, acl, cond);
1607                  return ccs_print_double_path_acl(head, acl, cond);          }
1608          }          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1609          if (acl_type == TYPE_PATH_NUMBER_ACL) {                  struct ccs_path_number_acl *acl
1610                  struct ccs_path_number_acl_record *acl                          = container_of(ptr, struct ccs_path_number_acl, head);
                         = container_of(ptr, struct ccs_path_number_acl_record,  
                                        head);  
1611                  return ccs_print_path_number_acl(head, acl, cond);                  return ccs_print_path_number_acl(head, acl, cond);
1612          }          }
1613          if (acl_type == TYPE_ARGV0_ACL) {          if (acl_type == CCS_TYPE_ENV_ACL) {
1614                  struct ccs_argv0_acl_record *acl                  struct ccs_env_acl *acl
1615                          = container_of(ptr, struct ccs_argv0_acl_record, head);                          = container_of(ptr, struct ccs_env_acl, head);
                 return ccs_print_argv0_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_ENV_ACL) {  
                 struct ccs_env_acl_record *acl  
                         = container_of(ptr, struct ccs_env_acl_record, head);  
1616                  return ccs_print_env_acl(head, acl, cond);                  return ccs_print_env_acl(head, acl, cond);
1617          }          }
1618          if (acl_type == TYPE_CAPABILITY_ACL) {          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1619                  struct ccs_capability_acl_record *acl                  struct ccs_capability_acl *acl
1620                          = container_of(ptr, struct ccs_capability_acl_record,                          = container_of(ptr, struct ccs_capability_acl, head);
                                        head);  
1621                  return ccs_print_capability_acl(head, acl, cond);                  return ccs_print_capability_acl(head, acl, cond);
1622          }          }
1623          if (acl_type == TYPE_IP_NETWORK_ACL) {          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1624                  struct ccs_ip_network_acl_record *acl                  struct ccs_ip_network_acl *acl
1625                          = container_of(ptr, struct ccs_ip_network_acl_record,                          = container_of(ptr, struct ccs_ip_network_acl, head);
                                        head);  
1626                  return ccs_print_network_acl(head, acl, cond);                  return ccs_print_network_acl(head, acl, cond);
1627          }          }
1628          if (acl_type == TYPE_SIGNAL_ACL) {          if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1629                  struct ccs_signal_acl_record *acl                  struct ccs_signal_acl *acl
1630                          = container_of(ptr, struct ccs_signal_acl_record, head);                          = container_of(ptr, struct ccs_signal_acl, head);
1631                  return ccs_print_signal_acl(head, acl, cond);                  return ccs_print_signal_acl(head, acl, cond);
1632          }          }
1633          if (acl_type == TYPE_MOUNT_ACL) {          if (acl_type == CCS_TYPE_MOUNT_ACL) {
1634                  struct ccs_mount_acl_record *acl                  struct ccs_mount_acl *acl
1635                          = container_of(ptr, struct ccs_mount_acl_record, head);                          = container_of(ptr, struct ccs_mount_acl, head);
1636                  return ccs_print_mount_acl(head, acl, cond);                  return ccs_print_mount_acl(head, acl, cond);
1637          }          }
         if (acl_type == TYPE_UMOUNT_ACL) {  
                 struct ccs_umount_acl_record *acl  
                         = container_of(ptr, struct ccs_umount_acl_record, head);  
                 return ccs_print_umount_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_CHROOT_ACL) {  
                 struct ccs_chroot_acl_record *acl  
                         = container_of(ptr, struct ccs_chroot_acl_record, head);  
                 return ccs_print_chroot_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_PIVOT_ROOT_ACL) {  
                 struct ccs_pivot_root_acl_record *acl  
                         = container_of(ptr, struct ccs_pivot_root_acl_record,  
                                        head);  
                 return ccs_print_pivot_root_acl(head, acl, cond);  
         }  
         /* Workaround for gcc 3.2.2's inline bug. */  
         if (acl_type & ACL_DELETED)  
                 return true;  
1638          BUG(); /* This must not happen. */          BUG(); /* This must not happen. */
1639          return false;          return false;
1640  }  }
# Line 1231  static bool ccs_print_entry(struct ccs_i Line 1644  static bool ccs_print_entry(struct ccs_i
1644   *   *
1645   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1646   *   *
  * Returns 0.  
  *  
1647   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1648   */   */
1649  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain_policy(struct ccs_io_buffer *head)
1650  {  {
1651          struct list_head *dpos;          struct list_head *dpos;
1652          struct list_head *apos;          struct list_head *apos;
         ccs_check_read_lock();  
1653          if (head->read_eof)          if (head->read_eof)
1654                  return 0;                  return;
1655          if (head->read_step == 0)          if (head->read_step == 0)
1656                  head->read_step = 1;                  head->read_step = 1;
1657          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
# Line 1257  static int ccs_read_domain_policy(struct Line 1667  static int ccs_read_domain_policy(struct
1667                          continue;                          continue;
1668                  /* Print domainname and flags. */                  /* Print domainname and flags. */
1669                  if (domain->quota_warned)                  if (domain->quota_warned)
1670                          quota_exceeded = "quota_exceeded\n";                          quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";
1671                  if (domain->domain_transition_failed)                  if (domain->domain_transition_failed)
1672                          transition_failed = "transition_failed\n";                          transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";
1673                  if (domain->ignore_global_allow_read)                  if (domain->ignore_global_allow_read)
1674                          ignore_global_allow_read                          ignore_global_allow_read
1675                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
1676                  if (domain->ignore_global_allow_env)                  if (domain->ignore_global_allow_env)
1677                          ignore_global_allow_env                          ignore_global_allow_env
1678                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
1679                  if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"
1680                                     "%s%s%s%s\n", domain->domainname->name,                                     "%s%s%s%s\n", domain->domainname->name,
1681                                     domain->profile, quota_exceeded,                                     domain->profile, quota_exceeded,
1682                                     transition_failed,                                     transition_failed,
1683                                     ignore_global_allow_read,                                     ignore_global_allow_read,
1684                                     ignore_global_allow_env))                                     ignore_global_allow_env))
1685                          return 0;                          return;
1686                  head->read_step = 2;                  head->read_step = 2;
1687   acl_loop:   acl_loop:
1688                  if (head->read_step == 3)                  if (head->read_step == 3)
# Line 1283  static int ccs_read_domain_policy(struct Line 1693  static int ccs_read_domain_policy(struct
1693                          struct ccs_acl_info *ptr                          struct ccs_acl_info *ptr
1694                                  = list_entry(apos, struct ccs_acl_info, list);                                  = list_entry(apos, struct ccs_acl_info, list);
1695                          if (!ccs_print_entry(head, ptr))                          if (!ccs_print_entry(head, ptr))
1696                                  return 0;                                  return;
1697                  }                  }
1698                  head->read_step = 3;                  head->read_step = 3;
1699   tail_mark:   tail_mark:
1700                  if (!ccs_io_printf(head, "\n"))                  if (!ccs_io_printf(head, "\n"))
1701                          return 0;                          return;
1702                  head->read_step = 1;                  head->read_step = 1;
1703                  if (head->read_single_domain)                  if (head->read_single_domain)
1704                          break;                          break;
1705          }          }
1706          head->read_eof = true;          head->read_eof = true;
         return 0;  
1707  }  }
1708    
1709  /**  /**
# Line 1307  static int ccs_read_domain_policy(struct Line 1716  static int ccs_read_domain_policy(struct
1716   * This is equivalent to doing   * This is equivalent to doing
1717   *   *
1718   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1719   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1720   *   *
1721   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1722   */   */
# Line 1317  static int ccs_write_domain_profile(stru Line 1726  static int ccs_write_domain_profile(stru
1726          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1727          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1728          unsigned int profile;          unsigned int profile;
         ccs_check_read_lock();  
1729          if (!cp)          if (!cp)
1730                  return -EINVAL;                  return -EINVAL;
1731          *cp = '\0';          *cp = '\0';
1732          profile = simple_strtoul(data, NULL, 10);          profile = simple_strtoul(data, NULL, 10);
1733          if (profile >= MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1734                  return -EINVAL;                  return -EINVAL;
1735          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1736          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1737                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1738          return 0;          return 0;
1739  }  }
# Line 1335  static int ccs_write_domain_profile(stru Line 1743  static int ccs_write_domain_profile(stru
1743   *   *
1744   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1745   *   *
  * Returns list of profile number and domainname pairs.  
  *  
1746   * This is equivalent to doing   * This is equivalent to doing
1747   *   *
1748   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1346  static int ccs_write_domain_profile(stru Line 1752  static int ccs_write_domain_profile(stru
1752   *   *
1753   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1754   */   */
1755  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1756  {  {
1757          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
1758          if (head->read_eof)          if (head->read_eof)
1759                  return 0;                  return;
1760          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1761                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain;
1762                  domain = list_entry(pos, struct ccs_domain_info, list);                  domain = list_entry(pos, struct ccs_domain_info, list);
# Line 1359  static int ccs_read_domain_profile(struc Line 1764  static int ccs_read_domain_profile(struc
1764                          continue;                          continue;
1765                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1766                                     domain->domainname->name))                                     domain->domainname->name))
1767                          return 0;                          return;
1768          }          }
1769          head->read_eof = true;          head->read_eof = true;
         return 0;  
1770  }  }
1771    
1772  /**  /**
# Line 1389  static int ccs_write_pid(struct ccs_io_b Line 1793  static int ccs_write_pid(struct ccs_io_b
1793   *   *
1794   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1795   */   */
1796  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1797  {  {
1798          char *buf = head->write_buf;          char *buf = head->write_buf;
1799          bool task_info = false;          bool task_info = false;
1800            bool global_pid = false;
1801          unsigned int pid;          unsigned int pid;
1802          struct task_struct *p;          struct task_struct *p;
1803          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1804          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_check_read_lock();  
1805          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1806          if (!buf)          if (!buf) {
1807                  goto done; /* Do nothing if open(O_RDONLY). */                  head->read_eof = true;
1808                    return; /* Do nothing if open(O_RDONLY). */
1809            }
1810          if (head->read_avail || head->read_eof)          if (head->read_avail || head->read_eof)
1811                  goto done;                  return;
1812          head->read_eof = true;          head->read_eof = true;
1813          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1814                  task_info = true;                  task_info = true;
1815            if (ccs_str_starts(&buf, "global-pid "))
1816                    global_pid = true;
1817          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1818          /***** CRITICAL SECTION START *****/          ccs_tasklist_lock();
1819          read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1820            if (global_pid)
1821                    p = find_task_by_pid_ns(pid, &init_pid_ns);
1822            else
1823                    p = find_task_by_vpid(pid);
1824    #else
1825          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1826    #endif
1827          if (p) {          if (p) {
1828                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1829                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1830          }          }
1831          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
         /***** CRITICAL SECTION END *****/  
1832          if (!domain)          if (!domain)
1833                  goto done;                  return;
1834          if (!task_info)          if (!task_info)
1835                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1836                                domain->domainname->name);                                domain->domainname->name);
1837          else          else
1838                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1839                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1840                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                ccs_yesno(ccs_flags &
1841                                "yes" : "no",                                          CCS_TASK_IS_POLICY_MANAGER),
1842                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                ccs_yesno(ccs_flags &
1843                                "yes" : "no",                                          CCS_TASK_IS_EXECUTE_HANDLER),
1844                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1845                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1846                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
  done:  
         return 0;  
1847  }  }
1848    
1849  /**  /**
# Line 1445  static int ccs_read_pid(struct ccs_io_bu Line 1856  static int ccs_read_pid(struct ccs_io_bu
1856  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception_policy(struct ccs_io_buffer *head)
1857  {  {
1858          char *data = head->write_buf;          char *data = head->write_buf;
1859          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1860          if (ccs_str_starts(&data, KEYWORD_KEEP_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))
1861                  return ccs_write_domain_keeper_policy(data, false, is_delete);                  return ccs_write_domain_keeper_policy(data, false, is_delete);
1862          if (ccs_str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))
1863                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  return ccs_write_domain_keeper_policy(data, true, is_delete);
1864          if (ccs_str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))
1865                  return ccs_write_domain_initializer_policy(data, false,                  return ccs_write_domain_initializer_policy(data, false,
1866                                                             is_delete);                                                             is_delete);
1867          if (ccs_str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))
1868                  return ccs_write_domain_initializer_policy(data, true,                  return ccs_write_domain_initializer_policy(data, true,
1869                                                             is_delete);                                                             is_delete);
1870          if (ccs_str_starts(&data, KEYWORD_AGGREGATOR))          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))
1871                  return ccs_write_aggregator_policy(data, is_delete);                  return ccs_write_aggregator_policy(data, is_delete);
1872          if (ccs_str_starts(&data, KEYWORD_ALLOW_READ))          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))
1873                  return ccs_write_globally_readable_policy(data, is_delete);                  return ccs_write_globally_readable_policy(data, is_delete);
1874          if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))
1875                  return ccs_write_globally_usable_env_policy(data, is_delete);                  return ccs_write_globally_usable_env_policy(data, is_delete);
1876          if (ccs_str_starts(&data, KEYWORD_FILE_PATTERN))          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))
1877                  return ccs_write_pattern_policy(data, is_delete);                  return ccs_write_pattern_policy(data, is_delete);
1878          if (ccs_str_starts(&data, KEYWORD_PATH_GROUP))          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))
1879                  return ccs_write_path_group_policy(data, is_delete);                  return ccs_write_path_group_policy(data, is_delete);
1880          if (ccs_str_starts(&data, KEYWORD_NUMBER_GROUP))          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))
1881                  return ccs_write_number_group_policy(data, is_delete);                  return ccs_write_number_group_policy(data, is_delete);
1882          if (ccs_str_starts(&data, KEYWORD_DENY_REWRITE))          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))
1883                  return ccs_write_no_rewrite_policy(data, is_delete);                  return ccs_write_no_rewrite_policy(data, is_delete);
1884          if (ccs_str_starts(&data, KEYWORD_ADDRESS_GROUP))          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))
1885                  return ccs_write_address_group_policy(data, is_delete);                  return ccs_write_address_group_policy(data, is_delete);
1886          if (ccs_str_starts(&data, KEYWORD_DENY_AUTOBIND))          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))
1887                  return ccs_write_reserved_port_policy(data, is_delete);                  return ccs_write_reserved_port_policy(data, is_delete);
1888          return -EINVAL;          return -EINVAL;
1889  }  }
# Line 1482  static int ccs_write_exception_policy(st Line 1893  static int ccs_write_exception_policy(st
1893   *   *
1894   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1895   *   *
  * Returns 0 on success, -EINVAL otherwise.  
  *  
1896   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1897   */   */
1898  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static void ccs_read_exception_policy(struct ccs_io_buffer *head)
1899  {  {
1900          ccs_check_read_lock();          if (head->read_eof)
1901          if (!head->read_eof) {                  return;
1902                  switch (head->read_step) {          switch (head->read_step) {
1903                  case 0:          case 0:
1904                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1905                          head->read_step = 1;                  head->read_step = 1;
1906                  case 1:          case 1:
1907                          if (!ccs_read_domain_keeper_policy(head))                  if (!ccs_read_domain_keeper_policy(head))
1908                                  break;                          break;
1909                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1910                          head->read_step = 2;                  head->read_step = 2;
1911                  case 2:          case 2:
1912                          if (!ccs_read_globally_readable_policy(head))                  if (!ccs_read_globally_readable_policy(head))
1913                                  break;                          break;
1914                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1915                          head->read_step = 3;                  head->read_step = 3;
1916                  case 3:          case 3:
1917                          if (!ccs_read_globally_usable_env_policy(head))                  if (!ccs_read_globally_usable_env_policy(head))
1918                                  break;                          break;
1919                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1920                          head->read_step = 4;                  head->read_step = 4;
1921                  case 4:          case 4:
1922                          if (!ccs_read_domain_initializer_policy(head))                  if (!ccs_read_domain_initializer_policy(head))
1923                                  break;                          break;
1924                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1925                          head->read_step = 6;                  head->read_step = 6;
1926                  case 6:          case 6:
1927                          if (!ccs_read_aggregator_policy(head))                  if (!ccs_read_aggregator_policy(head))
1928                                  break;                          break;
1929                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1930                          head->read_step = 7;                  head->read_step = 7;
1931                  case 7:          case 7:
1932                          if (!ccs_read_file_pattern(head))                  if (!ccs_read_file_pattern(head))
1933                                  break;                          break;
1934                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1935                          head->read_step = 8;                  head->read_step = 8;
1936                  case 8:          case 8:
1937                          if (!ccs_read_no_rewrite_policy(head))                  if (!ccs_read_no_rewrite_policy(head))
1938                                  break;                          break;
1939                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1940                          head->read_step = 9;                  head->read_step = 9;
1941                  case 9:          case 9:
1942                          if (!ccs_read_path_group_policy(head))                  if (!ccs_read_path_group_policy(head))
1943                                  break;                          break;
1944                          head->read_var1 = NULL;                  head->read_var1 = NULL;
1945                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1946                          head->read_step = 10;                  head->read_step = 10;
1947                  case 10:          case 10:
1948                          if (!ccs_read_number_group_policy(head))                  if (!ccs_read_number_group_policy(head))
1949                                  break;                          break;
1950                          head->read_var1 = NULL;                  head->read_var1 = NULL;
1951                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1952                          head->read_step = 11;                  head->read_step = 11;
1953                  case 11:          case 11:
1954                          if (!ccs_read_address_group_policy(head))                  if (!ccs_read_address_group_policy(head))
1955                                  break;                          break;
1956                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1957                          head->read_step = 12;                  head->read_step = 12;
1958                  case 12:          case 12:
1959                          if (!ccs_read_reserved_port_policy(head))                  if (!ccs_read_reserved_port_policy(head))
1960                            break;
1961                    head->read_eof = true;
1962            }
1963    }
1964    
1965    /**
1966     * ccs_get_argv0 - Get argv[0].
1967     *
1968     * @ee: Pointer to "struct ccs_execve_entry".
1969     *
1970     * Returns true on success, false otherwise.
1971     */
1972    static bool ccs_get_argv0(struct ccs_execve_entry *ee)
1973    {
1974            struct linux_binprm *bprm = ee->bprm;
1975            char *arg_ptr = ee->tmp;
1976            int arg_len = 0;
1977            unsigned long pos = bprm->p;
1978            int offset = pos % PAGE_SIZE;
1979            bool done = false;
1980            if (!bprm->argc)
1981                    goto out;
1982            while (1) {
1983                    if (!ccs_dump_page(bprm, pos, &ee->dump))
1984                            goto out;
1985                    pos += PAGE_SIZE - offset;
1986                    /* Read. */
1987                    while (offset < PAGE_SIZE) {
1988                            const char *kaddr = ee->dump.data;
1989                            const unsigned char c = kaddr[offset++];
1990                            if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
1991                                    if (c == '\\') {
1992                                            arg_ptr[arg_len++] = '\\';
1993                                            arg_ptr[arg_len++] = '\\';
1994                                    } else if (c > ' ' && c < 127) {
1995                                            arg_ptr[arg_len++] = c;
1996                                    } else {
1997                                            arg_ptr[arg_len++] = '\\';
1998                                            arg_ptr[arg_len++] = (c >> 6) + '0';
1999                                            arg_ptr[arg_len++]
2000                                                    = ((c >> 3) & 7) + '0';
2001                                            arg_ptr[arg_len++] = (c & 7) + '0';
2002                                    }
2003                            } else {
2004                                    arg_ptr[arg_len] = '\0';
2005                                    done = true;
2006                                  break;                                  break;
2007                          head->read_eof = true;                          }
2008                    }
2009                    offset = 0;
2010                    if (done)
2011                          break;                          break;
2012                  default:          }
2013                          return -EINVAL;          return true;
2014     out:
2015            return false;
2016    }
2017    
2018    /**
2019     * ccs_get_execute_condition - Get condition part for execute requests.
2020     *
2021     * @ee: Pointer to "struct ccs_execve_entry".
2022     *
2023     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2024     */
2025    static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry
2026                                                           *ee)
2027    {
2028            struct ccs_condition *cond;
2029            char *buf;
2030            int len = 256;
2031            char *realpath = NULL;
2032            char *argv0 = NULL;
2033            const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);
2034            if (profile->learning->learning_exec_realpath) {
2035                    struct file *file = ee->bprm->file;
2036    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2037                    struct path path = { file->f_vfsmnt, file->f_dentry };
2038                    realpath = ccs_realpath_from_path(&path);
2039    #else
2040                    realpath = ccs_realpath_from_path(&file->f_path);
2041    #endif
2042                    if (realpath)
2043                            len += strlen(realpath) + 17;
2044            }
2045            if (profile->learning->learning_exec_argv0) {
2046                    if (ccs_get_argv0(ee)) {
2047                            argv0 = ee->tmp;
2048                            len += strlen(argv0) + 16;
2049                  }                  }
2050          }          }
2051          return 0;          buf = kmalloc(len, GFP_KERNEL);
2052            if (!buf) {
2053                    kfree(realpath);
2054                    return NULL;
2055            }
2056            snprintf(buf, len - 1, "if");
2057            if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2058                    const int pos = strlen(buf);
2059                    snprintf(buf + pos, len - pos - 1,
2060                             " task.type=execute_handler");
2061            }
2062            if (realpath) {
2063                    const int pos = strlen(buf);
2064                    snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",
2065                             realpath);
2066                    kfree(realpath);
2067            }
2068            if (argv0) {
2069                    const int pos = strlen(buf);
2070                    snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",
2071                             argv0);
2072            }
2073            cond = ccs_get_condition(buf);
2074            kfree(buf);
2075            return cond;
2076    }
2077    
2078    /**
2079     * ccs_get_symlink_condition - Get condition part for symlink requests.
2080     *
2081     * @r: Pointer to "struct ccs_request_info".
2082     *
2083     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2084     */
2085    static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info
2086                                                           *r)
2087    {
2088            struct ccs_condition *cond;
2089            char *buf;
2090            int len = 256;
2091            const char *symlink = NULL;
2092            const struct ccs_profile *profile = ccs_profile(r->profile);
2093            if (profile->learning->learning_symlink_target) {
2094                    symlink = r->obj->symlink_target->name;
2095                    len += strlen(symlink) + 18;
2096            }
2097            buf = kmalloc(len, GFP_KERNEL);
2098            if (!buf)
2099                    return NULL;
2100            snprintf(buf, len - 1, "if");
2101            if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2102                    const int pos = strlen(buf);
2103                    snprintf(buf + pos, len - pos - 1,
2104                             " task.type=execute_handler");
2105            }
2106            if (symlink) {
2107                    const int pos = strlen(buf);
2108                    snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",
2109                             symlink);
2110            }
2111            cond = ccs_get_condition(buf);
2112            kfree(buf);
2113            return cond;
2114  }  }
2115    
2116  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 1581  static LIST_HEAD(ccs_query_list); Line 2136  static LIST_HEAD(ccs_query_list);
2136  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2137    
2138  /**  /**
2139   * ccs_check_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
2140   *   *
2141   * @r:       Pointer to "struct ccs_request_info".   * @r:       Pointer to "struct ccs_request_info".
2142   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt:     The printf()'s format string, followed by parameters.
# Line 1589  static atomic_t ccs_query_observers = AT Line 2144  static atomic_t ccs_query_observers = AT
2144   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
2145   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, 1 if the supervisor decided to
2146   * retry the access request which violated the policy in enforcing mode,   * retry the access request which violated the policy in enforcing mode,
2147   * -EPERM otherwise.   * 0 if it is not in enforcing mode, -EPERM otherwise.
2148   */   */
2149  int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2150  {  {
2151          va_list args;          va_list args;
2152          int error = -EPERM;          int error = -EPERM;
# Line 1603  int ccs_check_supervisor(struct ccs_requ Line 2158  int ccs_check_supervisor(struct ccs_requ
2158          char *header;          char *header;
2159          if (!r->domain)          if (!r->domain)
2160                  r->domain = ccs_current_domain();                  r->domain = ccs_current_domain();
2161            switch (r->mode) {
2162                    char *buffer;
2163                    struct ccs_condition *cond;
2164            case CCS_CONFIG_LEARNING:
2165                    if (!ccs_domain_quota_ok(r))
2166                            return 0;
2167                    va_start(args, fmt);
2168                    len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
2169                    va_end(args);
2170                    buffer = kmalloc(len, GFP_KERNEL);
2171                    if (!buffer)
2172                            return 0;
2173                    va_start(args, fmt);
2174                    vsnprintf(buffer, len - 1, fmt, args);
2175                    va_end(args);
2176                    ccs_normalize_line(buffer);
2177                    if (r->ee && !strncmp(buffer, "allow_execute ", 14))
2178                            cond = ccs_get_execute_condition(r->ee);
2179                    else if (r->obj && r->obj->symlink_target)
2180                            cond = ccs_get_symlink_condition(r);
2181                    else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
2182                            char str[] = "if task.type=execute_handler";
2183                            cond = ccs_get_condition(str);
2184                    } else
2185                            cond = NULL;
2186                    ccs_write_domain_policy2(buffer, r->domain, cond, false);
2187                    ccs_put_condition(cond);
2188                    kfree(buffer);
2189                    /* fall through */
2190            case CCS_CONFIG_PERMISSIVE:
2191                    return 0;
2192            }
2193          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2194                  int i;                  int i;
2195                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2196                          return -EPERM;                          return -EPERM;
2197                  for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->
2198                       i++) {                               enforcing_penalty; i++) {
2199                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2200                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2201                  }                  }
# Line 1623  int ccs_check_supervisor(struct ccs_requ Line 2210  int ccs_check_supervisor(struct ccs_requ
2210          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);
2211          if (!ccs_query_entry)          if (!ccs_query_entry)
2212                  goto out;                  goto out;
2213            len = ccs_round2(len);
2214          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);
2215          if (!ccs_query_entry->query)          if (!ccs_query_entry->query)
2216                  goto out;                  goto out;
2217          INIT_LIST_HEAD(&ccs_query_entry->list);          INIT_LIST_HEAD(&ccs_query_entry->list);
         /***** CRITICAL SECTION START *****/  
2218          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2219          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2220              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
# Line 1637  int ccs_check_supervisor(struct ccs_requ Line 2224  int ccs_check_supervisor(struct ccs_requ
2224                  ccs_query_entry->serial = ccs_serial++;                  ccs_query_entry->serial = ccs_serial++;
2225          }          }
2226          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2227          if (quota_exceeded)          if (quota_exceeded)
2228                  goto out;                  goto out;
2229          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
# Line 1648  int ccs_check_supervisor(struct ccs_requ Line 2234  int ccs_check_supervisor(struct ccs_requ
2234          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);
2235          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
2236          va_end(args);          va_end(args);
         /***** CRITICAL SECTION START *****/  
2237          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2238          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2239          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2240          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2241          for (ccs_query_entry->timer = 0;          for (ccs_query_entry->timer = 0;
2242               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;
# Line 1663  int ccs_check_supervisor(struct ccs_requ Line 2247  int ccs_check_supervisor(struct ccs_requ
2247                  if (ccs_query_entry->answer)                  if (ccs_query_entry->answer)
2248                          break;                          break;
2249          }          }
         /***** CRITICAL SECTION START *****/  
2250          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2251          list_del(&ccs_query_entry->list);          list_del(&ccs_query_entry->list);
2252          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
2253          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2254          switch (ccs_query_entry->answer) {          switch (ccs_query_entry->answer) {
2255          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2256                  error = 1;                  error = 1;
# Line 1709  static int ccs_poll_query(struct file *f Line 2291  static int ccs_poll_query(struct file *f
2291          bool found = false;          bool found = false;
2292          u8 i;          u8 i;
2293          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
                 /***** CRITICAL SECTION START *****/  
2294                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2295                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2296                          struct ccs_query_entry *ptr                          struct ccs_query_entry *ptr
# Line 1720  static int ccs_poll_query(struct file *f Line 2301  static int ccs_poll_query(struct file *f
2301                          break;                          break;
2302                  }                  }
2303                  spin_unlock(&ccs_query_list_lock);                  spin_unlock(&ccs_query_list_lock);
                 /***** CRITICAL SECTION END *****/  
2304                  if (found)                  if (found)
2305                          return POLLIN | POLLRDNORM;                          return POLLIN | POLLRDNORM;
2306                  if (i)                  if (i)
# Line 1734  static int ccs_poll_query(struct file *f Line 2314  static int ccs_poll_query(struct file *f
2314   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2315   *   *
2316   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
2317   */   */
2318  static int ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2319  {  {
2320          struct list_head *tmp;          struct list_head *tmp;
2321          int pos = 0;          int pos = 0;
2322          int len = 0;          int len = 0;
2323          char *buf;          char *buf;
2324          if (head->read_avail)          if (head->read_avail)
2325                  return 0;                  return;
2326          if (head->read_buf) {          if (head->read_buf) {
2327                  kfree(head->read_buf);                  kfree(head->read_buf);
2328                  head->read_buf = NULL;                  head->read_buf = NULL;
2329                  head->readbuf_size = 0;                  head->readbuf_size = 0;
2330          }          }
         /***** CRITICAL SECTION START *****/  
2331          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2332          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2333                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1763  static int ccs_read_query(struct ccs_io_ Line 2340  static int ccs_read_query(struct ccs_io_
2340                  break;                  break;
2341          }          }
2342          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2343          if (!len) {          if (!len) {
2344                  head->read_step = 0;                  head->read_step = 0;
2345                  return 0;                  return;
2346          }          }
2347          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, GFP_KERNEL);
2348          if (!buf)          if (!buf)
2349                  return 0;                  return;
2350          pos = 0;          pos = 0;
         /***** CRITICAL SECTION START *****/  
2351          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2352          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2353                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1790  static int ccs_read_query(struct ccs_io_ Line 2365  static int ccs_read_query(struct ccs_io_
2365                  break;                  break;
2366          }          }
2367          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2368          if (buf[0]) {          if (buf[0]) {
2369                  head->read_avail = len;                  head->read_avail = len;
2370                  head->readbuf_size = head->read_avail;                  head->readbuf_size = head->read_avail;
# Line 1799  static int ccs_read_query(struct ccs_io_ Line 2373  static int ccs_read_query(struct ccs_io_
2373          } else {          } else {
2374                  kfree(buf);                  kfree(buf);
2375          }          }
         return 0;  
2376  }  }
2377    
2378  /**  /**
# Line 1815  static int ccs_write_answer(struct ccs_i Line 2388  static int ccs_write_answer(struct ccs_i
2388          struct list_head *tmp;          struct list_head *tmp;
2389          unsigned int serial;          unsigned int serial;
2390          unsigned int answer;          unsigned int answer;
         /***** CRITICAL SECTION START *****/  
2391          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2392          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2393                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1823  static int ccs_write_answer(struct ccs_i Line 2395  static int ccs_write_answer(struct ccs_i
2395                  ptr->timer = 0;                  ptr->timer = 0;
2396          }          }
2397          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2398          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2399                  return -EINVAL;                  return -EINVAL;
         /***** CRITICAL SECTION START *****/  
2400          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2401          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2402                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1838  static int ccs_write_answer(struct ccs_i Line 2408  static int ccs_write_answer(struct ccs_i
2408                  break;                  break;
2409          }          }
2410          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2411          return 0;          return 0;
2412  }  }
2413    
# Line 1846  static int ccs_write_answer(struct ccs_i Line 2415  static int ccs_write_answer(struct ccs_i
2415   * ccs_read_version: Get version.   * ccs_read_version: Get version.
2416   *   *
2417   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns version information.  
2418   */   */
2419  static int ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2420  {  {
2421          if (!head->read_eof) {          if (head->read_eof)
2422                  ccs_io_printf(head, "1.7.0-pre");                  return;
2423                  head->read_eof = true;          ccs_io_printf(head, "1.7.1");
2424          }          head->read_eof = true;
         return 0;  
2425  }  }
2426    
2427  /**  /**
2428   * ccs_read_self_domain - Get the current process's domainname.   * ccs_read_self_domain - Get the current process's domainname.
2429   *   *
2430   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns the current process's domainname.  
2431   */   */
2432  static int ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2433  {  {
2434          if (!head->read_eof) {          if (head->read_eof)
2435                  /*                  return;
2436                   * ccs_current_domain()->domainname != NULL          /*
2437                   * because every process belongs to a domain and           * ccs_current_domain()->domainname != NULL because every process
2438                   * the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2439                   */           */
2440                  ccs_io_printf(head, "%s",          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2441                                ccs_current_domain()->domainname->name);          head->read_eof = true;
                 head->read_eof = true;  
         }  
         return 0;  
2442  }  }
2443    
2444  /**  /**
# Line 1906  int ccs_open_control(const u8 type, stru Line 2467  int ccs_open_control(const u8 type, stru
2467                  break;                  break;
2468  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2469          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;  
2470          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2471                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_audit_log;
2472                  head->read = ccs_read_reject_log;                  head->read = ccs_read_audit_log;
2473                  break;                  break;
2474  #endif  #endif
2475          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 1962  int ccs_open_control(const u8 type, stru Line 2520  int ccs_open_control(const u8 type, stru
2520                   */                   */
2521                  head->read = NULL;                  head->read = NULL;
2522                  head->poll = NULL;                  head->poll = NULL;
2523          } else if (type != CCS_QUERY &&          } else if (!head->poll) {
2524                     type != CCS_GRANTLOG && type != CCS_REJECTLOG) {                  /* Don't allocate read_buf for poll() access. */
                 /*  
                  * Don't allocate buffer for reading if the file is one of  
                  * /proc/ccs/grant_log , /proc/ccs/reject_log , /proc/ccs/query.  
                  */  
2525                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2526                          head->readbuf_size = 4096 * 2;                          head->readbuf_size = 4096;
2527                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);
2528                  if (!head->read_buf) {                  if (!head->read_buf) {
2529                          kfree(head);                          kfree(head);
# Line 1983  int ccs_open_control(const u8 type, stru Line 2537  int ccs_open_control(const u8 type, stru
2537                   */                   */
2538                  head->write = NULL;                  head->write = NULL;
2539          } else if (head->write) {          } else if (head->write) {
2540                  head->writebuf_size = 4096 * 2;                  head->writebuf_size = 4096;
2541                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);
2542                  if (!head->write_buf) {                  if (!head->write_buf) {
2543                          kfree(head->read_buf);                          kfree(head->read_buf);
# Line 2004  int ccs_open_control(const u8 type, stru Line 2558  int ccs_open_control(const u8 type, stru
2558                  ccs_read_control(file, NULL, 0);                  ccs_read_control(file, NULL, 0);
2559          /*          /*
2560           * If the file is /proc/ccs/query , increment the observer counter.           * If the file is /proc/ccs/query , increment the observer counter.
2561           * The obserber counter is used by ccs_check_supervisor() to see if           * The obserber counter is used by ccs_supervisor() to see if
2562           * there is some process monitoring /proc/ccs/query.           * there is some process monitoring /proc/ccs/query.
2563           */           */
2564          else if (type == CCS_QUERY)          else if (type == CCS_QUERY)
# Line 2019  int ccs_open_control(const u8 type, stru Line 2573  int ccs_open_control(const u8 type, stru
2573   * @wait: Pointer to "poll_table".   * @wait: Pointer to "poll_table".
2574   *   *
2575   * Waits for read readiness.   * Waits for read readiness.
2576   * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2577   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2578   * /usr/lib/ccs/ccs-auditd.   * /usr/sbin/ccs-auditd .
2579   */   */
2580  int ccs_poll_control(struct file *file, poll_table *wait)  int ccs_poll_control(struct file *file, poll_table *wait)
2581  {  {
# Line 2052  int ccs_read_control(struct file *file, Line 2606  int ccs_read_control(struct file *file,
2606                  return -EFAULT;                  return -EFAULT;
2607          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2608                  return -EINTR;                  return -EINTR;
2609          /* Call the policy handler. */          while (1) {
2610          len = head->read(head);                  /* Call the policy handler. */
2611          if (len < 0)                  head->read(head);
2612                  goto out;                  /* Write to buffer. */
2613          /* Write to buffer. */                  len = head->read_avail;
2614          len = head->read_avail;                  if (len || head->poll || head->read_eof)
2615                            break;
2616                    len = head->readbuf_size * 2;
2617                    cp = kzalloc(len, GFP_KERNEL);
2618                    if (!cp) {
2619                            len = -ENOMEM;
2620                            goto out;
2621                    }
2622                    kfree(head->read_buf);
2623                    head->read_buf = cp;
2624                    head->readbuf_size = len;
2625            }
2626          if (len > buffer_len)          if (len > buffer_len)
2627                  len = buffer_len;                  len = buffer_len;
2628          if (!len)          if (!len)
# Line 2106  int ccs_write_control(struct file *file, Line 2671  int ccs_write_control(struct file *file,
2671          while (avail_len > 0) {          while (avail_len > 0) {
2672                  char c;                  char c;
2673                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->write_avail >= head->writebuf_size - 1) {
2674                          error = -ENOMEM;                          const int len = head->writebuf_size * 2;
2675                          break;                          char *cp = kzalloc(len, GFP_KERNEL);
2676                  } else if (get_user(c, buffer)) {                          if (!cp) {
2677                                    error = -ENOMEM;
2678                                    break;
2679                            }
2680                            memmove(cp, cp0, head->write_avail);
2681                            kfree(cp0);
2682                            head->write_buf = cp;
2683                            cp0 = cp;
2684                            head->writebuf_size = len;
2685                    }
2686                    if (get_user(c, buffer)) {
2687                          error = -EFAULT;                          error = -EFAULT;
2688                          break;                          break;
2689                  }                  }

Legend:
Removed from v.2888  
changed lines
  Added in v.3422

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