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

Subversion リポジトリの参照

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

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

revision 2894 by kumaneko, Tue Aug 11 08:15:11 2009 UTC revision 3697 by kumaneko, Tue May 25 08:24:59 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/policy_io.c   * security/ccsecurity/policy_io.c
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.0-pre   2009/08/08   * Version: 1.7.2   2010/04/01
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 12  Line 12 
12    
13  #include "internal.h"  #include "internal.h"
14    
15  /* Lock for protecting ccs_profile->comment  */  static struct ccs_profile ccs_default_profile = {
16  static DEFINE_SPINLOCK(ccs_profile_comment_lock);          .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_AUTOLEARN_EXEC_REALPATH] = { "AUTOLEARN_EXEC_REALPATH", 0, 1 },          = "file::open",
55          [CCS_AUTOLEARN_EXEC_ARGV0] = { "AUTOLEARN_EXEC_ARGV0", 0, 1 },          [CCS_MAC_FILE_CREATE]
56          [CCS_MAC_FOR_IOCTL]       = { "MAC_FOR_IOCTL",       0, 3 },          = "file::create",
57          [CCS_MAC_FOR_FILEATTR]    = { "MAC_FOR_FILEATTR",    0, 3 },          [CCS_MAC_FILE_UNLINK]
58          [CCS_MAC_FOR_ENV]         = { "MAC_FOR_ENV",         0, 3 },          = "file::unlink",
59          [CCS_MAC_FOR_NETWORK]     = { "MAC_FOR_NETWORK",     0, 3 },          [CCS_MAC_FILE_MKDIR]
60          [CCS_MAC_FOR_SIGNAL]      = { "MAC_FOR_SIGNAL",      0, 3 },          = "file::mkdir",
61          [CCS_MAC_FOR_NAMESPACE]   = { "MAC_FOR_NAMESPACE",   0, 3 },          [CCS_MAC_FILE_RMDIR]
62          [CCS_RESTRICT_AUTOBIND]   = { "RESTRICT_AUTOBIND",   0, 1 },          = "file::rmdir",
63          [CCS_MAX_ACCEPT_ENTRY]          [CCS_MAC_FILE_MKFIFO]
64          = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },          = "file::mkfifo",
65  #ifdef CONFIG_CCSECURITY_AUDIT          [CCS_MAC_FILE_MKSOCK]
66          [CCS_MAX_GRANT_LOG]          = "file::mksock",
67          = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },          [CCS_MAC_FILE_TRUNCATE]
68          [CCS_MAX_REJECT_LOG]          = "file::truncate",
69          = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },          [CCS_MAC_FILE_SYMLINK]
70  #endif          = "file::symlink",
71          [CCS_VERBOSE]             = { "TOMOYO_VERBOSE",      1, 1 },          [CCS_MAC_FILE_REWRITE]
72          [CCS_SLEEP_PERIOD]          = "file::rewrite",
73          = { "SLEEP_PERIOD",        0, 3000 }, /* in 0.1 second */          [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_FILE_TRANSIT]
98            = "file::transit",
99            [CCS_MAC_ENVIRON]
100            = "misc::env",
101            [CCS_MAC_NETWORK_UDP_BIND]
102            = "network::inet_udp_bind",
103            [CCS_MAC_NETWORK_UDP_CONNECT]
104            = "network::inet_udp_connect",
105            [CCS_MAC_NETWORK_TCP_BIND]
106            = "network::inet_tcp_bind",
107            [CCS_MAC_NETWORK_TCP_LISTEN]
108            = "network::inet_tcp_listen",
109            [CCS_MAC_NETWORK_TCP_CONNECT]
110            = "network::inet_tcp_connect",
111            [CCS_MAC_NETWORK_TCP_ACCEPT]
112            = "network::inet_tcp_accept",
113            [CCS_MAC_NETWORK_RAW_BIND]
114            = "network::inet_raw_bind",
115            [CCS_MAC_NETWORK_RAW_CONNECT]
116            = "network::inet_raw_connect",
117            [CCS_MAC_SIGNAL]
118            = "ipc::signal",
119            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]
120            = "capability::inet_tcp_create",
121            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]
122            = "capability::inet_tcp_listen",
123            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]
124            = "capability::inet_tcp_connect",
125            [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]
126            = "capability::use_inet_udp",
127            [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]
128            = "capability::use_inet_ip",
129            [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
130            = "capability::use_route",
131            [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
132            = "capability::use_packet",
133            [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]
134            = "capability::SYS_MOUNT",
135            [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]
136            = "capability::SYS_UMOUNT",
137            [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
138            = "capability::SYS_REBOOT",
139            [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]
140            = "capability::SYS_CHROOT",
141            [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]
142            = "capability::SYS_KILL",
143            [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
144            = "capability::SYS_VHANGUP",
145            [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
146            = "capability::SYS_TIME",
147            [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]
148            = "capability::SYS_NICE",
149            [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]
150            = "capability::SYS_SETHOSTNAME",
151            [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
152            = "capability::use_kernel_module",
153            [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]
154            = "capability::create_fifo",
155            [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]
156            = "capability::create_block_dev",
157            [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]
158            = "capability::create_char_dev",
159            [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]
160            = "capability::create_unix_socket",
161            [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]
162            = "capability::SYS_LINK",
163            [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]
164            = "capability::SYS_SYMLINK",
165            [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]
166            = "capability::SYS_RENAME",
167            [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]
168            = "capability::SYS_UNLINK",
169            [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]
170            = "capability::SYS_CHMOD",
171            [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]
172            = "capability::SYS_CHOWN",
173            [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]
174            = "capability::SYS_IOCTL",
175            [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
176            = "capability::SYS_KEXEC_LOAD",
177            [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]
178            = "capability::SYS_PIVOT_ROOT",
179            [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
180            = "capability::SYS_PTRACE",
181            [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]
182            = "capability::conceal_mount",
183            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
184             + CCS_MAC_CATEGORY_FILE] = "file",
185            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
186             + CCS_MAC_CATEGORY_NETWORK] = "network",
187            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
188             + CCS_MAC_CATEGORY_MISC] = "misc",
189            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
190             + CCS_MAC_CATEGORY_IPC] = "ipc",
191            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
192             + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
193  };  };
194    
195  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
196  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
197    
198  /**  /**
199   * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.   * ccs_cap2keyword - Convert capability operation to capability name.
200   *   *
201   * @str: Unused.   * @operation: The capability index.
202   *   *
203   * Returns 0.   * Returns the name of the specified capability's name.
204   */   */
205  static int __init ccs_quiet_setup(char *str)  const char *ccs_cap2keyword(const u8 operation)
206  {  {
207          ccs_control_array[CCS_VERBOSE].current_value = 0;          return operation < CCS_MAX_CAPABILITY_INDEX
208          return 0;                  ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;
209  }  }
210    
211  __setup("CCS_QUIET", ccs_quiet_setup);  /**
212     * ccs_yesno - Return "yes" or "no".
213     *
214     * @value: Bool value.
215     */
216    static const char *ccs_yesno(const unsigned int value)
217    {
218            return value ? "yes" : "no";
219    }
220    
221  /**  /**
222   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
# Line 102  bool ccs_io_printf(struct ccs_io_buffer Line 246  bool ccs_io_printf(struct ccs_io_buffer
246  }  }
247    
248  /**  /**
249   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
250   *   *
251   * @profile: Profile number to create.   * @profile: Profile number to create.
252   *   *
253   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
254   */   */
255  static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_assign_profile(const unsigned int profile)
                                                           profile)  
256  {  {
257          struct ccs_profile *ptr;          struct ccs_profile *ptr;
258          struct ccs_profile *entry;          struct ccs_profile *entry;
         int i;  
259          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
260                  return NULL;                  return NULL;
261          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
262          if (ptr)          if (ptr)
263                  return ptr;                  return ptr;
264          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
265          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
266                    goto out;
267          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
268          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
269                  ptr = entry;                  ptr = entry;
270                  for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)                  ptr->audit = &ccs_default_profile.preference;
271                          ptr->value[i] = ccs_control_array[i].current_value;                  ptr->learning = &ccs_default_profile.preference;
272                  /*                  ptr->permissive = &ccs_default_profile.preference;
273                   * Needn't to initialize "ptr->capability_value"                  ptr->enforcing = &ccs_default_profile.preference;
274                   * because they are always 0.                  ptr->default_config = CCS_CONFIG_DISABLED |
275                   */                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
276                    memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
277                           sizeof(ptr->config));
278                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
279                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
280                  entry = NULL;                  entry = NULL;
281          }          }
282          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
283     out:
284          kfree(entry);          kfree(entry);
285          return ptr;          return ptr;
286  }  }
287    
288  /**  /**
289     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
290     */
291    static void ccs_check_profile(void)
292    {
293            struct ccs_domain_info *domain;
294            const int idx = ccs_read_lock();
295            ccs_policy_loaded = true;
296            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
297                    const u8 profile = domain->profile;
298                    if (ccs_profile_ptr[profile])
299                            continue;
300                    panic("Profile %u (used by '%s') not defined.\n",
301                          profile, domain->domainname->name);
302            }
303            ccs_read_unlock(idx);
304            if (ccs_profile_version != 20090903)
305                    panic("Profile version %u is not supported.\n",
306                          ccs_profile_version);
307            printk(KERN_INFO "CCSecurity: 1.7.2   2010/04/01\n");
308            printk(KERN_INFO "Mandatory Access Control activated.\n");
309    }
310    
311    /**
312     * ccs_profile - Find a profile.
313     *
314     * @profile: Profile number to find.
315     *
316     * Returns pointer to "struct ccs_profile".
317     */
318    struct ccs_profile *ccs_profile(const u8 profile)
319    {
320            struct ccs_profile *ptr = ccs_profile_ptr[profile];
321            if (!ccs_policy_loaded)
322                    return &ccs_default_profile;
323            BUG_ON(!ptr);
324            return ptr;
325    }
326    
327    /**
328   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
329   *   *
330   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 150  static int ccs_write_profile(struct ccs_ Line 335  static int ccs_write_profile(struct ccs_
335  {  {
336          char *data = head->write_buf;          char *data = head->write_buf;
337          unsigned int i;          unsigned int i;
338          unsigned int value;          int value;
339            int mode;
340            u8 config;
341            bool use_default = false;
342          char *cp;          char *cp;
343          struct ccs_profile *ccs_profile;          struct ccs_profile *profile;
344            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
345                    return 0;
346          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
347          if (data != cp) {          if (data == cp) {
348                    profile = &ccs_default_profile;
349            } else {
350                  if (*cp != '-')                  if (*cp != '-')
351                          return -EINVAL;                          return -EINVAL;
352                  data = cp + 1;                  data = cp + 1;
353                    profile = ccs_assign_profile(i);
354                    if (!profile)
355                            return -EINVAL;
356          }          }
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
                 return -EINVAL;  
357          cp = strchr(data, '=');          cp = strchr(data, '=');
358          if (!cp)          if (!cp)
359                  return -EINVAL;                  return -EINVAL;
360          *cp = '\0';          *cp++ = '\0';
361            if (profile != &ccs_default_profile)
362                    use_default = strstr(cp, "use_default") != NULL;
363            if (strstr(cp, "verbose=yes"))
364                    value = 1;
365            else if (strstr(cp, "verbose=no"))
366                    value = 0;
367            else
368                    value = -1;
369            if (!strcmp(data, CCS_KEYWORD_PREFERENCE_AUDIT)) {
370    #ifdef CONFIG_CCSECURITY_AUDIT
371                    char *cp2;
372    #endif
373                    if (use_default) {
374                            profile->audit = &ccs_default_profile.preference;
375                            return 0;
376                    }
377                    profile->audit = &profile->preference;
378    #ifdef CONFIG_CCSECURITY_AUDIT
379                    cp2 = strstr(cp, "max_grant_log=");
380                    if (cp2)
381                            sscanf(cp2 + 14, "%u",
382                                   &profile->preference.audit_max_grant_log);
383                    cp2 = strstr(cp, "max_reject_log=");
384                    if (cp2)
385                            sscanf(cp2 + 15, "%u",
386                                   &profile->preference.audit_max_reject_log);
387    #endif
388                    if (strstr(cp, "task_info=yes"))
389                            profile->preference.audit_task_info = true;
390                    else if (strstr(cp, "task_info=no"))
391                            profile->preference.audit_task_info = false;
392                    if (strstr(cp, "path_info=yes"))
393                            profile->preference.audit_path_info = true;
394                    else if (strstr(cp, "path_info=no"))
395                            profile->preference.audit_path_info = false;
396                    return 0;
397            }
398            if (!strcmp(data, CCS_KEYWORD_PREFERENCE_ENFORCING)) {
399                    char *cp2;
400                    if (use_default) {
401                            profile->enforcing = &ccs_default_profile.preference;
402                            return 0;
403                    }
404                    profile->enforcing = &profile->preference;
405                    if (value >= 0)
406                            profile->preference.enforcing_verbose = value;
407                    cp2 = strstr(cp, "penalty=");
408                    if (cp2)
409                            sscanf(cp2 + 8, "%u",
410                                   &profile->preference.enforcing_penalty);
411                    return 0;
412            }
413            if (!strcmp(data, CCS_KEYWORD_PREFERENCE_PERMISSIVE)) {
414                    if (use_default) {
415                            profile->permissive = &ccs_default_profile.preference;
416                            return 0;
417                    }
418                    profile->permissive = &profile->preference;
419                    if (value >= 0)
420                            profile->preference.permissive_verbose = value;
421                    return 0;
422            }
423            if (!strcmp(data, CCS_KEYWORD_PREFERENCE_LEARNING)) {
424                    char *cp2;
425                    if (use_default) {
426                            profile->learning = &ccs_default_profile.preference;
427                            return 0;
428                    }
429                    profile->learning = &profile->preference;
430                    if (value >= 0)
431                            profile->preference.learning_verbose = value;
432                    cp2 = strstr(cp, "max_entry=");
433                    if (cp2)
434                            sscanf(cp2 + 10, "%u",
435                                   &profile->preference.learning_max_entry);
436                    if (strstr(cp, "exec.realpath=yes"))
437                            profile->preference.learning_exec_realpath = true;
438                    else if (strstr(cp, "exec.realpath=no"))
439                            profile->preference.learning_exec_realpath = false;
440                    if (strstr(cp, "exec.argv0=yes"))
441                            profile->preference.learning_exec_argv0 = true;
442                    else if (strstr(cp, "exec.argv0=no"))
443                            profile->preference.learning_exec_argv0 = false;
444                    if (strstr(cp, "symlink.target=yes"))
445                            profile->preference.learning_symlink_target = true;
446                    else if (strstr(cp, "symlink.target=no"))
447                            profile->preference.learning_symlink_target = false;
448                    return 0;
449            }
450            if (profile == &ccs_default_profile)
451                    return -EINVAL;
452          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
453                  const struct ccs_path_info *new_comment                  const struct ccs_path_info *old_comment = profile->comment;
454                          = 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 *****/  
455                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
                 ccs_profile_entry_used[0] = true;  
456                  return 0;                  return 0;
457          }          }
458          if (ccs_str_starts(&data, CCS_KEYWORD_MAC_FOR_CAPABILITY)) {          if (!strcmp(data, "CONFIG")) {
459                  if (sscanf(cp + 1, "%u", &value) != 1) {                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
460                          for (i = 0; i < 4; i++) {                          + CCS_MAX_MAC_CATEGORY_INDEX;
461                                  if (strcmp(cp + 1, ccs_mode_4[i]))                  config = profile->default_config;
462                                          continue;          } else if (ccs_str_starts(&data, "CONFIG::")) {
463                                  value = i;                  config = 0;
464                                  break;                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
465                          }                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
466                          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]))  
467                                  continue;                                  continue;
468                          ccs_profile->capability_value[i] = value;                          config = profile->config[i];
469                          ccs_profile_entry_used[i + 1 + CCS_MAX_CONTROL_INDEX]                          break;
                                 = true;  
                         return 0;  
470                  }                  }
471                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
472                        + CCS_MAX_MAC_CATEGORY_INDEX)
473                            return -EINVAL;
474            } else {
475                  return -EINVAL;                  return -EINVAL;
476          }          }
477          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {          if (use_default) {
478                  if (strcmp(data, ccs_control_array[i].keyword))                  config = CCS_CONFIG_USE_DEFAULT;
479                          continue;          } else {
480                  if (sscanf(cp + 1, "%u", &value) != 1) {                  for (mode = 3; mode >= 0; mode--)
481                          int j;                          if (strstr(cp, ccs_mode_4[mode]))
482                          const char **modes;                                  /*
483                          switch (i) {                                   * Update lower 3 bits in order to distinguish
484                          case CCS_AUTOLEARN_EXEC_REALPATH:                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
485                          case CCS_AUTOLEARN_EXEC_ARGV0:                                   */
486                          case CCS_RESTRICT_AUTOBIND:                                  config = (config & ~7) | mode;
487                          case CCS_VERBOSE:  #ifdef CONFIG_CCSECURITY_AUDIT
488                                  modes = ccs_mode_2;                  if (config != CCS_CONFIG_USE_DEFAULT) {
489                                  break;                          if (strstr(cp, "grant_log=yes"))
490                          default:                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
491                                  modes = ccs_mode_4;                          else if (strstr(cp, "grant_log=no"))
492                                  break;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
493                          }                          if (strstr(cp, "reject_log=yes"))
494                          for (j = 0; j < 4; j++) {                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
495                                  if (strcmp(cp + 1, modes[j]))                          else if (strstr(cp, "reject_log=no"))
496                                          continue;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
                                 value = j;  
                                 break;  
                         }  
                         if (j == 4)  
                                 return -EINVAL;  
                 } else if (value > ccs_control_array[i].max_value) {  
                         value = ccs_control_array[i].max_value;  
497                  }                  }
498                  ccs_profile->value[i] = value;  #endif
                 ccs_profile_entry_used[i + 1] = true;  
                 return 0;  
499          }          }
500          return -EINVAL;          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
501                + CCS_MAX_MAC_CATEGORY_INDEX)
502                    profile->config[i] = config;
503            else if (config != CCS_CONFIG_USE_DEFAULT)
504                    profile->default_config = config;
505            return 0;
506    }
507    
508    static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)
509    {
510            struct ccs_preference *pref = &ccs_default_profile.preference;
511            const struct ccs_profile *profile = idx >= 0 ?
512                    ccs_profile_ptr[idx] : NULL;
513            char buffer[16] = "";
514            if (profile) {
515                    buffer[sizeof(buffer) - 1] = '\0';
516                    snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
517            }
518            if (profile) {
519                    pref = profile->audit;
520                    if (pref == &ccs_default_profile.preference)
521                            pref = NULL;
522            }
523            if (pref && !ccs_io_printf(head, "%s%s={ "
524    #ifdef CONFIG_CCSECURITY_AUDIT
525                                       "max_grant_log=%u max_reject_log=%u "
526    #endif
527                                       "task_info=%s path_info=%s }\n", buffer,
528                                       CCS_KEYWORD_PREFERENCE_AUDIT,
529    #ifdef CONFIG_CCSECURITY_AUDIT
530                                       pref->audit_max_grant_log,
531                                       pref->audit_max_reject_log,
532    #endif
533                                       ccs_yesno(pref->audit_task_info),
534                                       ccs_yesno(pref->audit_path_info)))
535                    return false;
536            if (profile) {
537                    pref = profile->learning;
538                    if (pref == &ccs_default_profile.preference)
539                            pref = NULL;
540            }
541            if (pref && !ccs_io_printf(head, "%s%s={ "
542                                       "verbose=%s max_entry=%u exec.realpath=%s "
543                                       "exec.argv0=%s symlink.target=%s }\n",
544                                       buffer, CCS_KEYWORD_PREFERENCE_LEARNING,
545                                       ccs_yesno(pref->learning_verbose),
546                                       pref->learning_max_entry,
547                                       ccs_yesno(pref->learning_exec_realpath),
548                                       ccs_yesno(pref->learning_exec_argv0),
549                                       ccs_yesno(pref->learning_symlink_target)))
550                    return false;
551            if (profile) {
552                    pref = profile->permissive;
553                    if (pref == &ccs_default_profile.preference)
554                            pref = NULL;
555            }
556            if (pref && !ccs_io_printf(head, "%s%s={ verbose=%s }\n", buffer,
557                                       CCS_KEYWORD_PREFERENCE_PERMISSIVE,
558                                       ccs_yesno(pref->permissive_verbose)))
559                    return false;
560            if (profile) {
561                    pref = profile->enforcing;
562                    if (pref == &ccs_default_profile.preference)
563                            pref = NULL;
564            }
565            return !pref || ccs_io_printf(head, "%s%s={ verbose=%s penalty=%u }\n",
566                                          buffer, CCS_KEYWORD_PREFERENCE_ENFORCING,
567                                          ccs_yesno(pref->enforcing_verbose),
568                                          pref->enforcing_penalty);
569  }  }
570    
571  /**  /**
572   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
573   *   *
574   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
575   */   */
576  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
577  {  {
578          static const int ccs_total          int index;
                 = CCS_MAX_CONTROL_INDEX + CCS_MAX_CAPABILITY_INDEX + 1;  
         int step;  
579          if (head->read_eof)          if (head->read_eof)
580                  return 0;                  return;
581          for (step = head->read_step; step < CCS_MAX_PROFILES * ccs_total;          if (head->read_bit)
582               step++) {                  goto body;
583                  const u8 index = step / ccs_total;          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
584                  u8 type = step % ccs_total;          ccs_print_preference(head, -1);
585                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];          head->read_bit = 1;
586                  head->read_step = step;   body:
587                  if (!ccs_profile)          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {
588                          continue;                  bool done;
589                  if (!ccs_profile_entry_used[type])                  u8 config;
590                          continue;                  int i;
591                  if (!type) { /* Print profile' comment tag. */                  int pos;
592                          bool done;                  const struct ccs_profile *profile = ccs_profile_ptr[index];
593                          /***** CRITICAL SECTION START *****/                  const struct ccs_path_info *comment;
594                          spin_lock(&ccs_profile_comment_lock);                  head->read_step = index;
595                          done = ccs_io_printf(head, "%u-COMMENT=%s\n",                  if (!profile)
                                              index, ccs_profile->comment ?  
                                              ccs_profile->comment->name : "");  
                         spin_unlock(&ccs_profile_comment_lock);  
                         /***** CRITICAL SECTION END *****/  
                         if (!done)  
                                 break;  
596                          continue;                          continue;
597                    pos = head->read_avail;
598                    comment = profile->comment;
599                    done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,
600                                         comment ? comment->name : "");
601                    if (!done)
602                            goto out;
603                    config = profile->default_config;
604    #ifdef CONFIG_CCSECURITY_AUDIT
605                    if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
606                                       "grant_log=%s reject_log=%s }\n", index,
607                                       "CONFIG", "", ccs_mode_4[config & 3],
608                                       ccs_yesno(config &
609                                                 CCS_CONFIG_WANT_GRANT_LOG),
610                                       ccs_yesno(config &
611                                                 CCS_CONFIG_WANT_REJECT_LOG)))
612                            goto out;
613    #else
614                    if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,
615                                       "CONFIG", "", ccs_mode_4[config & 3]))
616                            goto out;
617    #endif
618                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
619                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
620    #ifdef CONFIG_CCSECURITY_AUDIT
621                            const char *g;
622                            const char *r;
623    #endif
624                            config = profile->config[i];
625                            if (config == CCS_CONFIG_USE_DEFAULT)
626                                    continue;
627    #ifdef CONFIG_CCSECURITY_AUDIT
628                            g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);
629                            r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);
630                            if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
631                                               "grant_log=%s reject_log=%s }\n",
632                                               index, "CONFIG::",
633                                               ccs_mac_keywords[i],
634                                               ccs_mode_4[config & 3], g, r))
635                                    goto out;
636    #else
637                            if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",
638                                               index, "CONFIG::",
639                                               ccs_mac_keywords[i],
640                                               ccs_mode_4[config & 3]))
641                                    goto out;
642    #endif
643                  }                  }
644                  type--;                  if (!ccs_print_preference(head, index))
645                  if (type >= CCS_MAX_CONTROL_INDEX) {                          goto out;
646                          const int i = type - CCS_MAX_CONTROL_INDEX;                  continue;
647                          const u8 value = ccs_profile->capability_value[i];   out:
648                          if (!ccs_io_printf(head,                  head->read_avail = pos;
649                                             "%u-" CCS_KEYWORD_MAC_FOR_CAPABILITY                  break;
                                            "%s=%s\n", index,  
                                            ccs_capability_control_keyword[i],  
                                            ccs_mode_4[value]))  
                                 break;  
                 } else {  
                         const unsigned int value = ccs_profile->value[type];  
                         const char **modes = NULL;  
                         const char *keyword = ccs_control_array[type].keyword;  
                         switch (ccs_control_array[type].max_value) {  
                         case 3:  
                                 modes = ccs_mode_4;  
                                 break;  
                         case 1:  
                                 modes = ccs_mode_2;  
                                 break;  
                         }  
                         if (modes) {  
                                 if (!ccs_io_printf(head, "%u-%s=%s\n", index,  
                                                    keyword, modes[value]))  
                                         break;  
                         } else {  
                                 if (!ccs_io_printf(head, "%u-%s=%u\n", index,  
                                                    keyword, value))  
                                         break;  
                         }  
                 }  
650          }          }
651          if (step == CCS_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". */  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,
656  LIST_HEAD(ccs_policy_manager_list);                                     const struct ccs_acl_head *b)
657    {
658            return container_of(a, struct ccs_manager, head)->manager
659                    == container_of(b, struct ccs_manager, head)->manager;
660    }
661    
662  /**  /**
663   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 327  LIST_HEAD(ccs_policy_manager_list); Line 669  LIST_HEAD(ccs_policy_manager_list);
669   */   */
670  static int ccs_update_manager_entry(const char *manager, const bool is_delete)  static int ccs_update_manager_entry(const char *manager, const bool is_delete)
671  {  {
672          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         const struct ccs_path_info *saved_manager;  
673          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
674          bool is_domain = false;          if (ccs_domain_def(manager)) {
675          if (ccs_is_domain_def(manager)) {                  if (!ccs_correct_domain(manager))
                 if (!ccs_is_correct_domain(manager))  
676                          return -EINVAL;                          return -EINVAL;
677                  is_domain = true;                  e.is_domain = true;
678          } else {          } else {
679                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager, 1, -1, -1))
680                          return -EINVAL;                          return -EINVAL;
681          }          }
682          saved_manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
683          if (!saved_manager)          if (!e.manager)
684                  return -ENOMEM;                  return error;
685          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
686                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                                    CCS_ID_MANAGER, ccs_same_manager_entry);
687          mutex_lock(&ccs_policy_lock);          ccs_put_name(e.manager);
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (ptr->manager != saved_manager)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {  
                 entry->manager = saved_manager;  
                 saved_manager = NULL;  
                 entry->is_domain = is_domain;  
                 list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
         ccs_put_name(saved_manager);  
         kfree(entry);  
688          return error;          return error;
689  }  }
690    
691  /**  /**
692   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
693   *   *
694   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
695   *   *
696   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
697   */   */
698  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
699  {  {
700          char *data = head->write_buf;          char *data = head->write_buf;
701          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
# Line 386  static int ccs_write_manager_policy(stru Line 707  static int ccs_write_manager_policy(stru
707  }  }
708    
709  /**  /**
710   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
711   *   *
712   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
713   *   *
  * Returns 0.  
  *  
714   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
715   */   */
716  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
717  {  {
718          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
719          if (head->read_eof)          if (head->read_eof)
720                  return 0;                  return;
721          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(pos, head->read_var2,
722                  struct ccs_policy_manager_entry *ptr;                               &ccs_policy_list[CCS_ID_MANAGER]) {
723                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                  struct ccs_manager *ptr
724                  if (ptr->is_deleted)                          = list_entry(pos, typeof(*ptr), head.list);
725                    if (ptr->head.is_deleted)
726                          continue;                          continue;
727                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
728                          return 0;                          return;
729          }          }
730          head->read_eof = true;          head->read_eof = true;
         return 0;  
731  }  }
732    
733  /**  /**
734   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
735   *   *
736   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
737   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
738   *   *
739   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
740   */   */
741  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
742  {  {
743          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
744          const char *exe;          const char *exe;
745          struct task_struct *task = current;          struct task_struct *task = current;
746          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
747                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
748          bool found = false;          bool found = false;
         ccs_check_read_lock();  
749          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
750                  return true;                  return true;
751          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
752                  return true;                  return true;
753          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
754                  return false;                  return false;
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (!ptr->is_deleted && ptr->is_domain  
                     && !ccs_pathcmp(domainname, ptr->manager)) {  
                         /* Set manager flag. */  
                         task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;  
                         return true;  
                 }  
         }  
755          exe = ccs_get_exe();          exe = ccs_get_exe();
756          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
757                  return false;                                  head.list) {
758          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
759                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
760                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
761                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
762                          /* Set manager flag. */                                  continue;
763                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
764                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
765                                    continue;
766                  }                  }
767                    /* Set manager flag. */
768                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
769                    found = true;
770                    break;
771          }          }
772          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
773                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 479  static bool ccs_is_policy_manager(void) Line 793  static bool ccs_is_policy_manager(void)
793  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
794  {  {
795          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
796          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
797                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
798                  if (cp)          if (cp)
799                          *cp++ = '\0';                  *cp++ = '\0';
         }  
800          return cp;          return cp;
801  }  }
802    
803  /**  /**
804   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
805   *   *
806   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
807   * @data: String to parse.   * @data: String to parse.
# Line 505  static char *ccs_find_condition_part(cha Line 810  static char *ccs_find_condition_part(cha
810   *   *
811   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
812   */   */
813  static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)  static bool ccs_select_one(struct ccs_io_buffer *head, const char *data)
814  {  {
815          unsigned int pid;          unsigned int pid;
816          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
817          ccs_check_read_lock();          bool global_pid = false;
818          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "allow_execute")) {
819                  head->read_execute_only = true;                  head->read_execute_only = true;
820                  return true;                  return true;
821          }          }
822          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
823                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
824                  struct task_struct *p;                  struct task_struct *p;
825                  /***** CRITICAL SECTION START *****/                  ccs_tasklist_lock();
826                  read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
827                    if (global_pid)
828                            p = ccsecurity_exports.find_task_by_pid_ns(pid,
829                                                                   &init_pid_ns);
830                    else
831                            p = ccsecurity_exports.find_task_by_vpid(pid);
832    #else
833                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
834    #endif
835                  if (p)                  if (p)
836                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
837                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
                 /***** CRITICAL SECTION END *****/  
838          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
839                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
840                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
841          } else          } else
842                  return false;                  return false;
# Line 553  static bool ccs_is_select_one(struct ccs Line 865  static bool ccs_is_select_one(struct ccs
865          return true;          return true;
866  }  }
867    
868    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
869                                 struct ccs_condition *cond, const bool is_delete)
870    {
871            u8 i;
872            static const struct {
873                    const char *keyword;
874                    int (*write) (char *, struct ccs_domain_info *,
875                                  struct ccs_condition *, const bool);
876            } ccs_callback[5] = {
877                    { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
878                    { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
879                    { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
880                    { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
881                    { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
882            };
883            int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
884                          const bool) = ccs_write_file;
885            for (i = 0; i < 5; i++) {
886                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
887                            continue;
888                    write = ccs_callback[i].write;
889                    break;
890            }
891            return write(data, domain, cond, is_delete);
892    }
893    
894  /**  /**
895   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
896   *   *
897   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
898   *   *
899   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
900   */   */
901  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
902  {  {
903          char *data = head->write_buf;          char *data = head->write_buf;
904          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->write_var1;
# Line 574  static int ccs_write_domain_policy(struc Line 912  static int ccs_write_domain_policy(struc
912                  is_delete = true;                  is_delete = true;
913          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
914                  is_select = true;                  is_select = true;
915          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
916                  return 0;                  return 0;
917          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
918          if (!ccs_is_policy_manager())          if (!ccs_manager())
919                  return -EPERM;                  return -EPERM;
920          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
921                  domain = NULL;                  domain = NULL;
922                  if (is_delete)                  if (is_delete)
923                          ccs_delete_domain(data);                          ccs_delete_domain(data);
924                  else if (is_select)                  else if (is_select)
925                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
926                  else                  else
927                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
928                  head->write_var1 = domain;                  head->write_var1 = domain;
929                  return 0;                  return 0;
930          }          }
# Line 595  static int ccs_write_domain_policy(struc Line 933  static int ccs_write_domain_policy(struc
933    
934          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
935              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
936                  if (ccs_profile_ptr[profile] || !ccs_policy_loaded)                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
937                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
938                  return 0;                  return 0;
939          }          }
# Line 607  static int ccs_write_domain_policy(struc Line 945  static int ccs_write_domain_policy(struc
945                  domain->ignore_global_allow_env = !is_delete;                  domain->ignore_global_allow_env = !is_delete;
946                  return 0;                  return 0;
947          }          }
948            if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {
949                    domain->quota_warned = !is_delete;
950                    return 0;
951            }
952            if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {
953                    domain->domain_transition_failed = !is_delete;
954                    return 0;
955            }
956          cp = ccs_find_condition_part(data);          cp = ccs_find_condition_part(data);
957          if (cp) {          if (cp) {
958                  cond = ccs_get_condition(cp);                  cond = ccs_get_condition(cp);
959                  if (!cond)                  if (!cond)
960                          return -EINVAL;                          return -EINVAL;
961          }          }
962          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))          error = ccs_write_domain2(data, domain, cond, is_delete);
                 error = ccs_write_capability_policy(data, domain, cond,  
                                                     is_delete);  
         else if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))  
                 error = ccs_write_network_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))  
                 error = ccs_write_signal_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))  
                 error = ccs_write_env_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))  
                 error = ccs_write_mount_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_UNMOUNT))  
                 error = ccs_write_umount_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CHROOT))  
                 error = ccs_write_chroot_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, CCS_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);  
963          if (cond)          if (cond)
964                  ccs_put_condition(cond);                  ccs_put_condition(cond);
965          return error;          return error;
966  }  }
967    
968    /**
969     * ccs_print_name_union - Print a ccs_name_union.
970     *
971     * @head: Pointer to "struct ccs_io_buffer".
972     * @ptr:  Pointer to "struct ccs_name_union".
973     *
974     * Returns true on success, false otherwise.
975     */
976  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static bool ccs_print_name_union(struct ccs_io_buffer *head,
977                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
978  {  {
# Line 650  static bool ccs_print_name_union(struct Line 985  static bool ccs_print_name_union(struct
985          return ccs_io_printf(head, " %s", ptr->filename->name);          return ccs_io_printf(head, " %s", ptr->filename->name);
986  }  }
987    
988    /**
989     * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.
990     *
991     * @head: Pointer to "struct ccs_io_buffer".
992     * @ptr:  Pointer to "struct ccs_name_union".
993     *
994     * Returns true on success, false otherwise.
995     */
996  static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,  static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
997                                          const struct ccs_name_union *ptr)                                          const struct ccs_name_union *ptr)
998  {  {
# Line 659  static bool ccs_print_name_union_quoted( Line 1002  static bool ccs_print_name_union_quoted(
1002          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1003  }  }
1004    
1005    static void ccs_print_number(char *buffer, int buffer_len,
1006                                 const struct ccs_number_union *ptr)
1007    {
1008            int i;
1009            unsigned long min = ptr->values[0];
1010            const unsigned long max = ptr->values[1];
1011            u8 min_type = ptr->value_type[0];
1012            const u8 max_type = ptr->value_type[1];
1013            memset(buffer, 0, buffer_len);
1014            buffer_len -= 2;
1015            for (i = 0; i < 2; i++) {
1016                    int len;
1017                    switch (min_type) {
1018                    case CCS_VALUE_TYPE_HEXADECIMAL:
1019                            snprintf(buffer, buffer_len, "0x%lX", min);
1020                            break;
1021                    case CCS_VALUE_TYPE_OCTAL:
1022                            snprintf(buffer, buffer_len, "0%lo", min);
1023                            break;
1024                    default:
1025                            snprintf(buffer, buffer_len, "%lu", min);
1026                            break;
1027                    }
1028                    if (min == max && min_type == max_type)
1029                            break;
1030                    len = strlen(buffer);
1031                    buffer[len++] = '-';
1032                    buffer += len;
1033                    buffer_len -= len;
1034                    min_type = max_type;
1035                    min = max;
1036            }
1037    }
1038    
1039    /**
1040     * ccs_print_number_union_common - Print a ccs_number_union.
1041     *
1042     * @head:       Pointer to "struct ccs_io_buffer".
1043     * @ptr:        Pointer to "struct ccs_number_union".
1044     * @need_space: True if a space character is needed.
1045     *
1046     * Returns true on success, false otherwise.
1047     */
1048  static bool ccs_print_number_union_common(struct ccs_io_buffer *head,  static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
1049                                            const struct ccs_number_union *ptr,                                            const struct ccs_number_union *ptr,
1050                                            const bool need_space)                                            const bool need_space)
1051  {  {
1052          unsigned long min;          char buffer[128];
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
1053          if (need_space && !ccs_io_printf(head, " "))          if (need_space && !ccs_io_printf(head, " "))
1054                  return false;                  return false;
1055          if (ptr->is_group)          if (ptr->is_group)
1056                  return ccs_io_printf(head, "@%s",                  return ccs_io_printf(head, "@%s",
1057                                       ptr->group->group_name->name);                                       ptr->group->group_name->name);
1058          min_type = ptr->min_type;          ccs_print_number(buffer, sizeof(buffer), ptr);
1059          max_type = ptr->max_type;          return ccs_io_printf(head, "%s", buffer);
         min = ptr->values[0];  
         max = ptr->values[1];  
         switch (min_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 if (!ccs_io_printf(head, "0x%lX", min))  
                         return false;  
                 break;  
         case CCS_VALUE_TYPE_OCTAL:  
                 if (!ccs_io_printf(head, "0%lo", min))  
                         return false;  
                 break;  
         default:  
                 if (!ccs_io_printf(head, "%lu", min))  
                         return false;  
                 break;  
         }  
         if (min == max && min_type == max_type)  
                 return true;  
         switch (max_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 return ccs_io_printf(head, "-0x%lX", max);  
         case CCS_VALUE_TYPE_OCTAL:  
                 return ccs_io_printf(head, "-0%lo", max);  
         default:  
                 return ccs_io_printf(head, "-%lu", max);  
         }  
1060  }  }
1061    
1062    /**
1063     * ccs_print_number_union - Print a ccs_number_union.
1064     *
1065     * @head:       Pointer to "struct ccs_io_buffer".
1066     * @ptr:        Pointer to "struct ccs_number_union".
1067     *
1068     * Returns true on success, false otherwise.
1069     */
1070  static bool ccs_print_number_union(struct ccs_io_buffer *head,  static bool ccs_print_number_union(struct ccs_io_buffer *head,
1071                                     const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1072  {  {
1073          return ccs_print_number_union_common(head, ptr, true);          return ccs_print_number_union_common(head, ptr, true);
1074  }  }
1075    
1076    /**
1077     * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.
1078     *
1079     * @head:       Pointer to "struct ccs_io_buffer".
1080     * @ptr:        Pointer to "struct ccs_number_union".
1081     *
1082     * Returns true on success, false otherwise.
1083     */
1084  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1085                                             const struct ccs_number_union *ptr)                                             const struct ccs_number_union *ptr)
1086  {  {
# Line 728  static bool ccs_print_condition(struct c Line 1101  static bool ccs_print_condition(struct c
1101          const struct ccs_condition_element *condp;          const struct ccs_condition_element *condp;
1102          const struct ccs_number_union *numbers_p;          const struct ccs_number_union *numbers_p;
1103          const struct ccs_name_union *names_p;          const struct ccs_name_union *names_p;
1104          const struct ccs_argv_entry *argv;          const struct ccs_argv *argv;
1105          const struct ccs_envp_entry *envp;          const struct ccs_envp *envp;
1106          u16 condc;          u16 condc;
1107          u16 i;          u16 i;
1108          u16 j;          u16 j;
# Line 741  static bool ccs_print_condition(struct c Line 1114  static bool ccs_print_condition(struct c
1114          numbers_p = (const struct ccs_number_union *) (condp + condc);          numbers_p = (const struct ccs_number_union *) (condp + condc);
1115          names_p = (const struct ccs_name_union *)          names_p = (const struct ccs_name_union *)
1116                  (numbers_p + cond->numbers_count);                  (numbers_p + cond->numbers_count);
1117          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);          argv = (const struct ccs_argv *) (names_p + cond->names_count);
1118          envp = (const struct ccs_envp_entry *) (argv + cond->argc);          envp = (const struct ccs_envp *) (argv + cond->argc);
1119          memset(buffer, 0, sizeof(buffer));          memset(buffer, 0, sizeof(buffer));
1120          if (condc && !ccs_io_printf(head, "%s", " if"))          if (condc && !ccs_io_printf(head, "%s", " if"))
1121                  goto out;                  goto out;
# Line 818  static bool ccs_print_condition(struct c Line 1191  static bool ccs_print_condition(struct c
1191                                     cond->post_state[j]))                                     cond->post_state[j]))
1192                          goto out;                          goto out;
1193          }          }
1194            if (i & (1 << 4)) {
1195                    if (!ccs_io_printf(head, " audit=%s",
1196                                       ccs_yesno(cond->post_state[4])))
1197                            goto out;
1198            }
1199   no_condition:   no_condition:
1200          if (ccs_io_printf(head, "\n"))          if (ccs_io_printf(head, "\n"))
1201                  return true;                  return true;
# Line 826  static bool ccs_print_condition(struct c Line 1204  static bool ccs_print_condition(struct c
1204  }  }
1205    
1206  /**  /**
1207   * ccs_print_single_path_acl - Print a single path ACL entry.   * ccs_print_path_acl - Print a path ACL entry.
1208   *   *
1209   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1210   * @ptr:  Pointer to "struct ccs_single_path_acl_record".   * @ptr:  Pointer to "struct ccs_path_acl".
1211   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1212   *   *
1213   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1214   */   */
1215  static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1216                                        struct ccs_single_path_acl_record *ptr,                                 struct ccs_path_acl *ptr,
1217                                        const struct ccs_condition *cond)                                 const struct ccs_condition *cond)
1218  {  {
1219          int pos;          int pos;
1220          u8 bit;          u8 bit;
1221          const u16 perm = ptr->perm;          const u16 perm = ptr->perm;
1222          for (bit = head->read_bit; bit < CCS_MAX_SINGLE_PATH_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
                 const char *msg;  
1223                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1224                          continue;                          continue;
1225                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE_ACL)                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1226                        && bit != CCS_TYPE_TRANSIT)
1227                          continue;                          continue;
1228                  /* Print "read/write" instead of "read" and "write". */                  /* Print "read/write" instead of "read" and "write". */
1229                  if ((bit == CCS_TYPE_READ_ACL || bit == CCS_TYPE_WRITE_ACL)                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1230                      && (perm & (1 << CCS_TYPE_READ_WRITE_ACL)))                      && (perm & (1 << CCS_TYPE_READ_WRITE)))
1231                          continue;                          continue;
                 msg = ccs_sp2keyword(bit);  
1232                  pos = head->read_avail;                  pos = head->read_avail;
1233                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||
1234                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1235                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1236                          goto out;                          head->read_bit = bit;
1237                            head->read_avail = pos;
1238                            return false;
1239                    }
1240          }          }
1241          head->read_bit = 0;          head->read_bit = 0;
1242          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1243  }  }
1244    
1245  /**  /**
1246   * ccs_print_mkdev_acl - Print a mkdev ACL entry.   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.
1247   *   *
1248   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1249   * @ptr:  Pointer to "struct ccs_mkdev_acl_record".   * @ptr:  Pointer to "struct ccs_path_number3_acl".
1250   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1251   *   *
1252   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1253   */   */
1254  static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,
1255                                  struct ccs_mkdev_acl_record *ptr,                                         struct ccs_path_number3_acl *ptr,
1256                                  const struct ccs_condition *cond)                                         const struct ccs_condition *cond)
1257  {  {
1258          int pos;          int pos;
1259          u8 bit;          u8 bit;
1260          const u16 perm = ptr->perm;          const u16 perm = ptr->perm;
1261          for (bit = head->read_bit; bit < CCS_MAX_MKDEV_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;
1262                  const char *msg;               bit++) {
1263                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1264                          continue;                          continue;
                 msg = ccs_mkdev2keyword(bit);  
1265                  pos = head->read_avail;                  pos = head->read_avail;
1266                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s",
1267                                       ccs_path_number32keyword(bit)) ||
1268                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1269                        !ccs_print_number_union(head, &ptr->mode) ||
1270                      !ccs_print_number_union(head, &ptr->major) ||                      !ccs_print_number_union(head, &ptr->major) ||
1271                      !ccs_print_number_union(head, &ptr->minor) ||                      !ccs_print_number_union(head, &ptr->minor) ||
1272                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1273                          goto out;                          head->read_bit = bit;
1274                            head->read_avail = pos;
1275                            return false;
1276                    }
1277          }          }
1278          head->read_bit = 0;          head->read_bit = 0;
1279          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1280  }  }
1281    
1282  /**  /**
1283   * ccs_print_double_path_acl - Print a double path ACL entry.   * ccs_print_path2_acl - Print a path2 ACL entry.
1284   *   *
1285   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1286   * @ptr:  Pointer to "struct ccs_double_path_acl_record".   * @ptr:  Pointer to "struct ccs_path2_acl".
1287   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1288   *   *
1289   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1290   */   */
1291  static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1292                                        struct ccs_double_path_acl_record *ptr,                                  struct ccs_path2_acl *ptr,
1293                                        const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1294  {  {
1295          int pos;          int pos;
1296          u8 bit;          u8 bit;
1297          const u8 perm = ptr->perm;          const u8 perm = ptr->perm;
1298          for (bit = head->read_bit; bit < CCS_MAX_DOUBLE_PATH_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {
                 const char *msg;  
1299                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1300                          continue;                          continue;
                 msg = ccs_dp2keyword(bit);  
1301                  pos = head->read_avail;                  pos = head->read_avail;
1302                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s",
1303                                       ccs_path22keyword(bit)) ||
1304                      !ccs_print_name_union(head, &ptr->name1) ||                      !ccs_print_name_union(head, &ptr->name1) ||
1305                      !ccs_print_name_union(head, &ptr->name2) ||                      !ccs_print_name_union(head, &ptr->name2) ||
1306                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1307                          goto out;                          head->read_bit = bit;
1308                            head->read_avail = pos;
1309                            return false;
1310                    }
1311          }          }
1312          head->read_bit = 0;          head->read_bit = 0;
1313          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1314  }  }
1315    
1316  /**  /**
1317   * ccs_print_path_number_acl - Print an ioctl/chmod/chown/chgrp ACL entry.   * ccs_print_path_number_acl - Print a path_number ACL entry.
1318   *   *
1319   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1320   * @ptr:  Pointer to "struct ccs_path_number_acl_record".   * @ptr:  Pointer to "struct ccs_path_number_acl".
1321   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1322   *   *
1323   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1324   */   */
1325  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1326                                        struct ccs_path_number_acl_record *ptr,                                        struct ccs_path_number_acl *ptr,
1327                                        const struct ccs_condition *cond)                                        const struct ccs_condition *cond)
1328  {  {
1329          int pos;          int pos;
1330          u8 bit;          u8 bit;
1331          const u8 perm = ptr->perm;          const u8 perm = ptr->perm;
1332          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1333                  const char *msg;               bit++) {
1334                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1335                          continue;                          continue;
                 msg = ccs_path_number2keyword(bit);  
1336                  pos = head->read_avail;                  pos = head->read_avail;
1337                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s",
1338                                       ccs_path_number2keyword(bit)) ||
1339                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1340                      !ccs_print_number_union(head, &ptr->number) ||                      !ccs_print_number_union(head, &ptr->number) ||
1341                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond)) {
1342                          goto out;                          head->read_bit = bit;
1343                            head->read_avail = pos;
1344                            return false;
1345                    }
1346          }          }
1347          head->read_bit = 0;          head->read_bit = 0;
1348          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1349  }  }
1350    
1351  /**  /**
1352   * ccs_print_env_acl - Print an evironment variable name's ACL entry.   * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1353   *   *
1354   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1355   * @ptr:  Pointer to "struct ccs_env_acl_record".   * @ptr:  Pointer to "struct ccs_env_acl".
1356   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1357   *   *
1358   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1359   */   */
1360  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1361                                struct ccs_env_acl_record *ptr,                                struct ccs_env_acl *ptr,
1362                                const struct ccs_condition *cond)                                const struct ccs_condition *cond)
1363  {  {
1364          int pos = head->read_avail;          const int pos = head->read_avail;
1365          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name))          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1366                  goto out;              !ccs_print_condition(head, cond)) {
1367          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1368                  goto out;                  return false;
1369            }
1370          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1371  }  }
1372    
1373  /**  /**
1374   * ccs_print_capability_acl - Print a capability ACL entry.   * ccs_print_capability_acl - Print a capability ACL entry.
1375   *   *
1376   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1377   * @ptr:  Pointer to "struct ccs_capability_acl_record".   * @ptr:  Pointer to "struct ccs_capability_acl".
1378   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1379   *   *
1380   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1381   */   */
1382  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1383                                       struct ccs_capability_acl_record *ptr,                                       struct ccs_capability_acl *ptr,
1384                                       const struct ccs_condition *cond)                                       const struct ccs_condition *cond)
1385  {  {
1386          int pos = head->read_avail;          const int pos = head->read_avail;
1387          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1388                             ccs_cap2keyword(ptr->operation)))                             ccs_cap2keyword(ptr->operation)) ||
1389                  goto out;              !ccs_print_condition(head, cond)) {
1390          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl_record".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl_record *ptr)  
 {  
         const u32 min_address = ptr->address.ipv4.min;  
         const u32 max_address = ptr->address.ipv4.max;  
         if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))  
                 return false;  
         if (min_address != max_address  
             && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))  
                 return false;  
         return true;  
 }  
   
 /**  
  * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl_record".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl_record *ptr)  
 {  
         char buf[64];  
         const struct in6_addr *min_address = ptr->address.ipv6.min;  
         const struct in6_addr *max_address = ptr->address.ipv6.max;  
         ccs_print_ipv6(buf, sizeof(buf), min_address);  
         if (!ccs_io_printf(head, "%s", buf))  
1391                  return false;                  return false;
         if (min_address != max_address) {  
                 ccs_print_ipv6(buf, sizeof(buf), max_address);  
                 if (!ccs_io_printf(head, "-%s", buf))  
                         return false;  
1392          }          }
1393          return true;          return true;
1394  }  }
# Line 1074  static bool ccs_print_ipv6_entry(struct Line 1397  static bool ccs_print_ipv6_entry(struct
1397   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_network_acl - Print a network ACL entry.
1398   *   *
1399   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1400   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
1401   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1402   *   *
1403   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1404   */   */
1405  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1406                                    struct ccs_ip_network_acl_record *ptr,                                    struct ccs_ip_network_acl *ptr,
1407                                    const struct ccs_condition *cond)                                    const struct ccs_condition *cond)
1408  {  {
1409          int pos = head->read_avail;          int pos;
1410          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",          u8 bit;
1411                             ccs_net2keyword(ptr->operation_type)))          const u8 perm = ptr->perm;
1412                  goto out;          char buf[128];
1413          switch (ptr->record_type) {          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1414          case CCS_IP_RECORD_TYPE_ADDRESS_GROUP:                  const char *w[2] = { "", "" };
1415                  if (!ccs_io_printf(head, "@%s",                  if (!(perm & (1 << bit)))
1416                                     ptr->address.group->group_name->name))                          continue;
1417                          goto out;                  pos = head->read_avail;
1418                  break;                  switch (ptr->address_type) {
1419          case CCS_IP_RECORD_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1420                  if (!ccs_print_ipv4_entry(head, ptr))                          w[0] = "@";
1421                          goto out;                          w[1] = ptr->address.group->group_name->name;
1422                  break;                          break;
1423          case CCS_IP_RECORD_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1424                  if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1425                                           ptr->address.ipv4.max);
1426                            w[0] = buf;
1427                            break;
1428                    case CCS_IP_ADDRESS_TYPE_IPv6:
1429                            ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1430                                           ptr->address.ipv6.max);
1431                            w[0] = buf;
1432                            break;
1433                    }
1434                    if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1435                                       ccs_net2keyword(bit), w[0], w[1]) ||
1436                        !ccs_print_number_union(head, &ptr->port) ||
1437                        !ccs_print_condition(head, cond))
1438                          goto out;                          goto out;
                 break;  
1439          }          }
1440          if (!ccs_print_number_union(head, &ptr->port) ||          head->read_bit = 0;
             !ccs_print_condition(head, cond))  
                 goto out;  
1441          return true;          return true;
1442   out:   out:
1443            head->read_bit = bit;
1444          head->read_avail = pos;          head->read_avail = pos;
1445          return false;          return false;
1446  }  }
# Line 1115  static bool ccs_print_network_acl(struct Line 1449  static bool ccs_print_network_acl(struct
1449   * ccs_print_signal_acl - Print a signal ACL entry.   * ccs_print_signal_acl - Print a signal ACL entry.
1450   *   *
1451   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1452   * @ptr:  Pointer to "struct signale_acl_record".   * @ptr:  Pointer to "struct signale_acl".
1453   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1454   *   *
1455   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1456   */   */
1457  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1458                                   struct ccs_signal_acl_record *ptr,                                   struct ccs_signal_acl *ptr,
1459                                   const struct ccs_condition *cond)                                   const struct ccs_condition *cond)
1460  {  {
1461          int pos = head->read_avail;          const int pos = head->read_avail;
1462          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1463                             ptr->sig, ptr->domainname->name))                             ptr->sig, ptr->domainname->name) ||
1464                  goto out;              !ccs_print_condition(head, cond)) {
1465          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1466                  goto out;                  return false;
1467            }
1468          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1469  }  }
1470    
1471  /**  /**
1472   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_print_execute_handler - Print an execute handler ACL entry.
1473   *   *
1474   * @head:    Pointer to "struct ccs_io_buffer".   * @head:    Pointer to "struct ccs_io_buffer".
1475   * @keyword: Name of the keyword.   * @keyword: Name of the keyword.
1476   * @ptr:     Pointer to "struct ccs_execute_handler_record".   * @ptr:     Pointer to "struct ccs_execute_handler".
1477   *   *
1478   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1479   */   */
1480  static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  static bool ccs_print_execute_handler(struct ccs_io_buffer *head,
1481                                               const char *keyword,                                        const char *keyword,
1482                                               struct ccs_execute_handler_record *                                        struct ccs_execute_handler *ptr)
                                              ptr)  
1483  {  {
1484          return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);          return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1485  }  }
# Line 1157  static bool ccs_print_execute_handler_re Line 1488  static bool ccs_print_execute_handler_re
1488   * ccs_print_mount_acl - Print a mount ACL entry.   * ccs_print_mount_acl - Print a mount ACL entry.
1489   *   *
1490   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1491   * @ptr:  Pointer to "struct ccs_mount_acl_record".   * @ptr:  Pointer to "struct ccs_mount_acl".
1492   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1493   *   *
1494   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1495   */   */
1496  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1497                                  struct ccs_mount_acl_record *ptr,                                  struct ccs_mount_acl *ptr,
1498                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1499  {  {
1500          int pos = head->read_avail;          const int pos = head->read_avail;
1501          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT "%s %s %s 0x%lX\n",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1502                             ptr->dev_name->name, ptr->dir_name->name,              !ccs_print_name_union(head, &ptr->dev_name) ||
1503                             ptr->fs_type->name, ptr->flags))              !ccs_print_name_union(head, &ptr->dir_name) ||
1504                  goto out;              !ccs_print_name_union(head, &ptr->fs_type) ||
1505          if (!ccs_print_condition(head, cond))              !ccs_print_number_union(head, &ptr->flags) ||
1506                  goto out;              !ccs_print_condition(head, cond)) {
1507          return true;                  head->read_avail = pos;
1508   out:                  return false;
1509          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, CCS_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, CCS_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, CCS_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;  
1510          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1511  }  }
1512    
1513  /**  /**
# Line 1266  static bool ccs_print_entry(struct ccs_i Line 1522  static bool ccs_print_entry(struct ccs_i
1522                              struct ccs_acl_info *ptr)                              struct ccs_acl_info *ptr)
1523  {  {
1524          const struct ccs_condition *cond = ptr->cond;          const struct ccs_condition *cond = ptr->cond;
1525          const u8 acl_type = ccs_acl_type2(ptr);          const u8 acl_type = ptr->type;
1526          if (acl_type & CCS_ACL_DELETED)          if (ptr->is_deleted)
1527                  return true;                  return true;
1528          if (acl_type == CCS_TYPE_SINGLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH_ACL) {
1529                  struct ccs_single_path_acl_record *acl                  struct ccs_path_acl *acl
1530                          = container_of(ptr, struct ccs_single_path_acl_record,                          = container_of(ptr, struct ccs_path_acl, head);
1531                                         head);                  return ccs_print_path_acl(head, acl, cond);
1532                  return ccs_print_single_path_acl(head, acl, cond);          }
1533          }          if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1534          if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {              acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1535                  struct ccs_execute_handler_record *acl                  struct ccs_execute_handler *acl
1536                          = container_of(ptr, struct ccs_execute_handler_record,                          = container_of(ptr, struct ccs_execute_handler, head);
1537                                         head);                  const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1538                  const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;                          CCS_KEYWORD_EXECUTE_HANDLER :
1539                  return ccs_print_execute_handler_record(head, keyword, acl);                          CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1540          }                  return ccs_print_execute_handler(head, keyword, acl);
         if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
1541          }          }
1542          if (head->read_execute_only)          if (head->read_execute_only)
1543                  return true;                  return true;
1544          if (acl_type == CCS_TYPE_MKDEV_ACL) {          if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {
1545                  struct ccs_mkdev_acl_record *acl                  struct ccs_path_number3_acl *acl
1546                          = container_of(ptr, struct ccs_mkdev_acl_record, head);                          = container_of(ptr, struct ccs_path_number3_acl, head);
1547                  return ccs_print_mkdev_acl(head, acl, cond);                  return ccs_print_path_number3_acl(head, acl, cond);
1548          }          }
1549          if (acl_type == CCS_TYPE_DOUBLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH2_ACL) {
1550                  struct ccs_double_path_acl_record *acl                  struct ccs_path2_acl *acl
1551                          = container_of(ptr, struct ccs_double_path_acl_record,                          = container_of(ptr, struct ccs_path2_acl, head);
1552                                         head);                  return ccs_print_path2_acl(head, acl, cond);
                 return ccs_print_double_path_acl(head, acl, cond);  
1553          }          }
1554          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1555                  struct ccs_path_number_acl_record *acl                  struct ccs_path_number_acl *acl
1556                          = container_of(ptr, struct ccs_path_number_acl_record,                          = container_of(ptr, struct ccs_path_number_acl, head);
                                        head);  
1557                  return ccs_print_path_number_acl(head, acl, cond);                  return ccs_print_path_number_acl(head, acl, cond);
1558          }          }
1559          if (acl_type == CCS_TYPE_ENV_ACL) {          if (acl_type == CCS_TYPE_ENV_ACL) {
1560                  struct ccs_env_acl_record *acl                  struct ccs_env_acl *acl
1561                          = container_of(ptr, struct ccs_env_acl_record, head);                          = container_of(ptr, struct ccs_env_acl, head);
1562                  return ccs_print_env_acl(head, acl, cond);                  return ccs_print_env_acl(head, acl, cond);
1563          }          }
1564          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1565                  struct ccs_capability_acl_record *acl                  struct ccs_capability_acl *acl
1566                          = container_of(ptr, struct ccs_capability_acl_record,                          = container_of(ptr, struct ccs_capability_acl, head);
                                        head);  
1567                  return ccs_print_capability_acl(head, acl, cond);                  return ccs_print_capability_acl(head, acl, cond);
1568          }          }
1569          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1570                  struct ccs_ip_network_acl_record *acl                  struct ccs_ip_network_acl *acl
1571                          = container_of(ptr, struct ccs_ip_network_acl_record,                          = container_of(ptr, struct ccs_ip_network_acl, head);
                                        head);  
1572                  return ccs_print_network_acl(head, acl, cond);                  return ccs_print_network_acl(head, acl, cond);
1573          }          }
1574          if (acl_type == CCS_TYPE_SIGNAL_ACL) {          if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1575                  struct ccs_signal_acl_record *acl                  struct ccs_signal_acl *acl
1576                          = container_of(ptr, struct ccs_signal_acl_record, head);                          = container_of(ptr, struct ccs_signal_acl, head);
1577                  return ccs_print_signal_acl(head, acl, cond);                  return ccs_print_signal_acl(head, acl, cond);
1578          }          }
1579          if (acl_type == CCS_TYPE_MOUNT_ACL) {          if (acl_type == CCS_TYPE_MOUNT_ACL) {
1580                  struct ccs_mount_acl_record *acl                  struct ccs_mount_acl *acl
1581                          = container_of(ptr, struct ccs_mount_acl_record, head);                          = container_of(ptr, struct ccs_mount_acl, head);
1582                  return ccs_print_mount_acl(head, acl, cond);                  return ccs_print_mount_acl(head, acl, cond);
1583          }          }
         if (acl_type == CCS_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 == CCS_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 == CCS_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 & CCS_ACL_DELETED)  
                 return true;  
1584          BUG(); /* This must not happen. */          BUG(); /* This must not happen. */
1585          return false;          return false;
1586  }  }
1587    
1588  /**  /**
1589   * ccs_read_domain_policy - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1590   *   *
1591   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1592     * @domain: Pointer to "struct ccs_domain_info".
1593   *   *
1594   * Returns 0.   * Caller holds ccs_read_lock().
1595     *
1596     * Returns true on success, false otherwise.
1597     */
1598    static bool ccs_read_domain2(struct ccs_io_buffer *head,
1599                                 struct ccs_domain_info *domain)
1600    {
1601            struct list_head *pos;
1602            /* Print ACL entries in the domain. */
1603            list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {
1604                    struct ccs_acl_info *ptr
1605                            = list_entry(pos, struct ccs_acl_info, list);
1606                    if (!ccs_print_entry(head, ptr))
1607                            return false;
1608            }
1609            return true;
1610    }
1611    
1612    /**
1613     * ccs_read_domain - Read domain policy.
1614     *
1615     * @head: Pointer to "struct ccs_io_buffer".
1616   *   *
1617   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1618   */   */
1619  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1620  {  {
1621          struct list_head *dpos;          struct list_head *pos;
         struct list_head *apos;  
         ccs_check_read_lock();  
1622          if (head->read_eof)          if (head->read_eof)
1623                  return 0;                  return;
1624          if (head->read_step == 0)          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1625                  head->read_step = 1;                  struct ccs_domain_info *domain =
1626          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(pos, struct ccs_domain_info, list);
1627                  struct ccs_domain_info *domain;                  const char *w[4] = { "", "", "", "" };
1628                  const char *quota_exceeded = "";                  switch (head->read_step) {
1629                  const char *transition_failed = "";                  case 0:
1630                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted && !head->read_single_domain)
1631                  const char *ignore_global_allow_env = "";                                  continue;
1632                  domain = list_entry(dpos, struct ccs_domain_info, list);                          /* Print domainname and flags. */
1633                  if (head->read_step != 1)                          if (domain->quota_warned)
1634                          goto acl_loop;                                  w[0] = CCS_KEYWORD_QUOTA_EXCEEDED "\n";
1635                  if (domain->is_deleted && !head->read_single_domain)                          if (domain->domain_transition_failed)
1636                          continue;                                  w[1] = CCS_KEYWORD_TRANSITION_FAILED "\n";
1637                  /* Print domainname and flags. */                          if (domain->ignore_global_allow_read)
1638                  if (domain->quota_warned)                                  w[2] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
1639                          quota_exceeded = "quota_exceeded\n";                                          "\n";
1640                  if (domain->domain_transition_failed)                          if (domain->ignore_global_allow_env)
1641                          transition_failed = "transition_failed\n";                                  w[3] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV
1642                  if (domain->ignore_global_allow_read)                                          "\n";
1643                          ignore_global_allow_read                          if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE
1644                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                             "%u\n%s%s%s%s\n",
1645                  if (domain->ignore_global_allow_env)                                             domain->domainname->name,
1646                          ignore_global_allow_env                                             domain->profile, w[0], w[1], w[2],
1647                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                                             w[3]))
1648                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                                  return;
1649                                     "%s%s%s%s\n", domain->domainname->name,                          head->read_step++;
1650                                     domain->profile, quota_exceeded,                          /* fall through */
1651                                     transition_failed,                  case 1:
1652                                     ignore_global_allow_read,                          if (!ccs_read_domain2(head, domain))
1653                                     ignore_global_allow_env))                                  return;
1654                          return 0;                          head->read_step++;
1655                  head->read_step = 2;                          /* fall through */
1656   acl_loop:                  case 2:
1657                  if (head->read_step == 3)                          if (!ccs_io_printf(head, "\n"))
1658                          goto tail_mark;                                  return;
1659                  /* Print ACL entries in the domain. */                          head->read_step = 0;
1660                  list_for_each_cookie(apos, head->read_var2,                          if (head->read_single_domain)
1661                                       &domain->acl_info_list) {                                  goto out;
1662                          struct ccs_acl_info *ptr                  }
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
                                 return 0;  
                 }  
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return 0;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1663          }          }
1664     out:
1665          head->read_eof = true;          head->read_eof = true;
         return 0;  
1666  }  }
1667    
1668  /**  /**
# Line 1439  static int ccs_read_domain_policy(struct Line 1675  static int ccs_read_domain_policy(struct
1675   * This is equivalent to doing   * This is equivalent to doing
1676   *   *
1677   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1678   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1679   *   *
1680   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1681   */   */
# Line 1449  static int ccs_write_domain_profile(stru Line 1685  static int ccs_write_domain_profile(stru
1685          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1686          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1687          unsigned int profile;          unsigned int profile;
         ccs_check_read_lock();  
1688          if (!cp)          if (!cp)
1689                  return -EINVAL;                  return -EINVAL;
1690          *cp = '\0';          *cp = '\0';
# Line 1457  static int ccs_write_domain_profile(stru Line 1692  static int ccs_write_domain_profile(stru
1692          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1693                  return -EINVAL;                  return -EINVAL;
1694          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1695          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1696                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1697          return 0;          return 0;
1698  }  }
# Line 1467  static int ccs_write_domain_profile(stru Line 1702  static int ccs_write_domain_profile(stru
1702   *   *
1703   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1704   *   *
  * Returns list of profile number and domainname pairs.  
  *  
1705   * This is equivalent to doing   * This is equivalent to doing
1706   *   *
1707   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1478  static int ccs_write_domain_profile(stru Line 1711  static int ccs_write_domain_profile(stru
1711   *   *
1712   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1713   */   */
1714  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1715  {  {
1716          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
1717          if (head->read_eof)          if (head->read_eof)
1718                  return 0;                  return;
1719          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1720                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain;
1721                  domain = list_entry(pos, struct ccs_domain_info, list);                  domain = list_entry(pos, struct ccs_domain_info, list);
# Line 1491  static int ccs_read_domain_profile(struc Line 1723  static int ccs_read_domain_profile(struc
1723                          continue;                          continue;
1724                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1725                                     domain->domainname->name))                                     domain->domainname->name))
1726                          return 0;                          return;
1727          }          }
1728          head->read_eof = true;          head->read_eof = true;
         return 0;  
1729  }  }
1730    
1731  /**  /**
# Line 1521  static int ccs_write_pid(struct ccs_io_b Line 1752  static int ccs_write_pid(struct ccs_io_b
1752   *   *
1753   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1754   */   */
1755  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1756  {  {
1757          char *buf = head->write_buf;          char *buf = head->write_buf;
1758          bool task_info = false;          bool task_info = false;
1759            bool global_pid = false;
1760          unsigned int pid;          unsigned int pid;
1761          struct task_struct *p;          struct task_struct *p;
1762          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1763          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_check_read_lock();  
1764          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1765          if (!buf)          if (!buf) {
1766                  goto done; /* Do nothing if open(O_RDONLY). */                  head->read_eof = true;
1767                    return; /* Do nothing if open(O_RDONLY). */
1768            }
1769          if (head->read_avail || head->read_eof)          if (head->read_avail || head->read_eof)
1770                  goto done;                  return;
1771          head->read_eof = true;          head->read_eof = true;
1772          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1773                  task_info = true;                  task_info = true;
1774            if (ccs_str_starts(&buf, "global-pid "))
1775                    global_pid = true;
1776          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1777          /***** CRITICAL SECTION START *****/          ccs_tasklist_lock();
1778          read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1779            if (global_pid)
1780                    p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1781            else
1782                    p = ccsecurity_exports.find_task_by_vpid(pid);
1783    #else
1784          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1785    #endif
1786          if (p) {          if (p) {
1787                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1788                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1789          }          }
1790          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
         /***** CRITICAL SECTION END *****/  
1791          if (!domain)          if (!domain)
1792                  goto done;                  return;
1793          if (!task_info)          if (!task_info)
1794                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1795                                domain->domainname->name);                                domain->domainname->name);
1796          else          else
1797                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1798                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1799                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                ccs_yesno(ccs_flags &
1800                                "yes" : "no",                                          CCS_TASK_IS_MANAGER),
1801                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                ccs_yesno(ccs_flags &
1802                                "yes" : "no",                                          CCS_TASK_IS_EXECUTE_HANDLER),
1803                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1804                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1805                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
  done:  
         return 0;  
1806  }  }
1807    
1808  /**  /**
1809   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1810   *   *
1811   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1812   *   *
1813   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1814   */   */
1815  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1816  {  {
1817          char *data = head->write_buf;          char *data = head->write_buf;
1818          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1819          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1820                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1821          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1822                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool, const u8);
1823          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[8] = {
1824                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper },
1825                                                             is_delete);                  { CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1826          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                    ccs_write_domain_initializer },
1827                  return ccs_write_domain_initializer_policy(data, true,                  { CCS_KEYWORD_KEEP_DOMAIN, ccs_write_domain_keeper },
1828                                                             is_delete);                  { CCS_KEYWORD_INITIALIZE_DOMAIN,
1829          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                    ccs_write_domain_initializer },
1830                  return ccs_write_aggregator_policy(data, is_delete);                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1831          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1832                  return ccs_write_globally_readable_policy(data, is_delete);                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1833          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1834                  return ccs_write_globally_usable_env_policy(data, is_delete);          };
1835          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          static const char *ccs_name[CCS_MAX_GROUP] = {
1836                  return ccs_write_pattern_policy(data, is_delete);                  [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1837          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                  [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1838                  return ccs_write_path_group_policy(data, is_delete);                  [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1839          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))          };
1840                  return ccs_write_number_group_policy(data, is_delete);          for (i = 0; i < 10; i++) {
1841          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1842                  return ccs_write_no_rewrite_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete, i < 2);
1843          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))          }
1844                  return ccs_write_address_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++) {
1845          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                  if (ccs_str_starts(&data, ccs_name[i]))
1846                  return ccs_write_reserved_port_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1847          return -EINVAL;          }
1848            {
1849                    int error;
1850                    struct ccs_condition *cond = NULL;
1851                    char *cp = ccs_find_condition_part(data);
1852                    if (cp) {
1853                            cond = ccs_get_condition(cp);
1854                            if (!cond)
1855                                    return -EINVAL;
1856                    }
1857                    error = ccs_write_domain2(data, &ccs_global_domain, cond,
1858                                              is_delete);
1859                    if (cond)
1860                            ccs_put_condition(cond);
1861                    return error;
1862            }
1863  }  }
1864    
1865  /**  /**
1866   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1867   *   *
1868   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1869     * @idx:  Index number.
1870   *   *
1871   * Returns 0 on success, -EINVAL otherwise.   * Returns true on success, false otherwise.
1872   *   *
1873   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1874   */   */
1875  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1876  {  {
1877          ccs_check_read_lock();          struct list_head *gpos;
1878          if (!head->read_eof) {          struct list_head *mpos;
1879                  switch (head->read_step) {          const char *w[3] = { "", "", "" };
1880                  case 0:          if (idx == CCS_PATH_GROUP)
1881                          head->read_var2 = NULL;                  w[0] = CCS_KEYWORD_PATH_GROUP;
1882                          head->read_step = 1;          else if (idx == CCS_NUMBER_GROUP)
1883                  case 1:                  w[0] = CCS_KEYWORD_NUMBER_GROUP;
1884                          if (!ccs_read_domain_keeper_policy(head))          else if (idx == CCS_ADDRESS_GROUP)
1885                                  break;                  w[0] = CCS_KEYWORD_ADDRESS_GROUP;
1886                          head->read_var2 = NULL;          list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1887                          head->read_step = 2;                  struct ccs_group *group =
1888                  case 2:                          list_entry(gpos, struct ccs_group, head.list);
1889                          if (!ccs_read_globally_readable_policy(head))                  w[1] = group->group_name->name;
1890                                  break;                  list_for_each_cookie(mpos, head->read_var2,
1891                          head->read_var2 = NULL;                                       &group->member_list) {
1892                          head->read_step = 3;                          char buffer[128];
1893                  case 3:                          struct ccs_acl_head *ptr =
1894                          if (!ccs_read_globally_usable_env_policy(head))                                  list_entry(mpos, struct ccs_acl_head, list);
1895                                  break;                          if (ptr->is_deleted)
1896                          head->read_var2 = NULL;                                  continue;
1897                          head->read_step = 4;                          if (idx == CCS_PATH_GROUP) {
1898                  case 4:                                  w[2] = container_of(ptr, struct ccs_path_group,
1899                          if (!ccs_read_domain_initializer_policy(head))                                                      head)->member_name->name;
1900                                  break;                          } else if (idx == CCS_NUMBER_GROUP) {
1901                          head->read_var2 = NULL;                                  w[2] = buffer;
1902                          head->read_step = 6;                                  ccs_print_number(buffer, sizeof(buffer),
1903                  case 6:                                                   &container_of
1904                          if (!ccs_read_aggregator_policy(head))                                                   (ptr, struct ccs_number_group,
1905                                  break;                                                    head)->number);
1906                          head->read_var2 = NULL;                          } else if (idx == CCS_ADDRESS_GROUP) {
1907                          head->read_step = 7;                                  struct ccs_address_group *member =
1908                  case 7:                                          container_of(ptr, typeof(*member),
1909                          if (!ccs_read_file_pattern(head))                                                       head);
1910                                  break;                                  w[2] = buffer;
1911                          head->read_var2 = NULL;                                  if (member->is_ipv6)
1912                          head->read_step = 8;                                          ccs_print_ipv6(buffer, sizeof(buffer),
1913                  case 8:                                                         member->min.ipv6,
1914                          if (!ccs_read_no_rewrite_policy(head))                                                         member->max.ipv6);
1915                                  break;                                  else
1916                          head->read_var2 = NULL;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1917                          head->read_step = 9;                                                         member->min.ipv4,
1918                  case 9:                                                         member->max.ipv4);
1919                          if (!ccs_read_path_group_policy(head))                          }
1920                                  break;                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1921                          head->read_var1 = NULL;                                             w[2]))
1922                          head->read_var2 = NULL;                                  return false;
1923                          head->read_step = 10;                  }
1924                  case 10:          }
1925                          if (!ccs_read_number_group_policy(head))          return true;
1926                                  break;  }
1927                          head->read_var1 = NULL;  
1928                          head->read_var2 = NULL;  /**
1929                          head->read_step = 11;   * ccs_read_policy - Read "struct ccs_..._entry" list.
1930                  case 11:   *
1931                          if (!ccs_read_address_group_policy(head))   * @head: Pointer to "struct ccs_io_buffer".
1932                                  break;   * @idx:  Index number.
1933                          head->read_var2 = NULL;   *
1934                          head->read_step = 12;   * Returns true on success, false otherwise.
1935                  case 12:   *
1936                          if (!ccs_read_reserved_port_policy(head))   * Caller holds ccs_read_lock().
1937                                  break;   */
1938                          head->read_eof = true;  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1939    {
1940            struct list_head *pos;
1941            list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1942                    const char *w[4] = { "", "", "", "" };
1943                    char buffer[16];
1944                    struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1945                                                            list);
1946                    if (acl->is_deleted)
1947                            continue;
1948                    switch (idx) {
1949                    case CCS_ID_DOMAIN_KEEPER:
1950                            {
1951                                    struct ccs_domain_keeper *ptr =
1952                                            container_of(acl, typeof(*ptr), head);
1953                                    w[0] = ptr->is_not ?
1954                                            CCS_KEYWORD_NO_KEEP_DOMAIN :
1955                                            CCS_KEYWORD_KEEP_DOMAIN;
1956                                    if (ptr->program) {
1957                                            w[1] = ptr->program->name;
1958                                            w[2] = " from ";
1959                                    }
1960                                    w[3] = ptr->domainname->name;
1961                            }
1962                            break;
1963                    case CCS_ID_DOMAIN_INITIALIZER:
1964                            {
1965                                    struct ccs_domain_initializer *ptr =
1966                                            container_of(acl, typeof(*ptr), head);
1967                                    w[0] = ptr->is_not ?
1968                                            CCS_KEYWORD_NO_INITIALIZE_DOMAIN :
1969                                            CCS_KEYWORD_INITIALIZE_DOMAIN;
1970                                    w[1] = ptr->program->name;
1971                                    if (ptr->domainname) {
1972                                            w[2] = " from ";
1973                                            w[3] = ptr->domainname->name;
1974                                    }
1975                            }
1976                            break;
1977                    case CCS_ID_AGGREGATOR:
1978                            {
1979                                    struct ccs_aggregator *ptr =
1980                                            container_of(acl, typeof(*ptr), head);
1981                                    w[0] = CCS_KEYWORD_AGGREGATOR;
1982                                    w[1] = ptr->original_name->name;
1983                                    w[2] = " ";
1984                                    w[3] = ptr->aggregated_name->name;
1985                            }
1986                            break;
1987                    case CCS_ID_PATTERN:
1988                            {
1989                                    struct ccs_pattern *ptr =
1990                                            container_of(acl, typeof(*ptr), head);
1991                                    w[0] = CCS_KEYWORD_FILE_PATTERN;
1992                                    w[1] = ptr->pattern->name;
1993                            }
1994                            break;
1995                    case CCS_ID_NO_REWRITE:
1996                            {
1997                                    struct ccs_no_rewrite *ptr =
1998                                            container_of(acl, typeof(*ptr), head);
1999                                    w[0] = CCS_KEYWORD_DENY_REWRITE;
2000                                    w[1] = ptr->pattern->name;
2001                            }
2002                            break;
2003                    case CCS_ID_RESERVEDPORT:
2004                            {
2005                                    struct ccs_reserved *ptr =
2006                                            container_of(acl, typeof(*ptr), head);
2007                                    const u16 min_port = ptr->min_port;
2008                                    const u16 max_port = ptr->max_port;
2009                                    w[0] = CCS_KEYWORD_DENY_AUTOBIND;
2010                                    snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
2011                                             min_port, min_port != max_port ?
2012                                             '-' : '\0', max_port);
2013                                    buffer[sizeof(buffer) - 1] = '\0';
2014                                    w[1] = buffer;
2015                            }
2016                          break;                          break;
2017                  default:                  default:
2018                          return -EINVAL;                          continue;
2019                  }                  }
2020                    if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))
2021                            return false;
2022          }          }
2023          return 0;          return true;
2024    }
2025    
2026    static void ccs_read_global_domain(struct ccs_io_buffer *head)
2027    {
2028            if (!head->read_eof)
2029                    head->read_eof = ccs_read_domain2(head, &ccs_global_domain);
2030    }
2031    
2032    /**
2033     * ccs_read_exception - Read exception policy.
2034     *
2035     * @head: Pointer to "struct ccs_io_buffer".
2036     *
2037     * Caller holds ccs_read_lock().
2038     */
2039    static void ccs_read_exception(struct ccs_io_buffer *head)
2040    {
2041            if (head->read_eof)
2042                    return;
2043            while (head->read_step < CCS_MAX_POLICY &&
2044                   ccs_read_policy(head, head->read_step))
2045                    head->read_step++;
2046            if (head->read_step < CCS_MAX_POLICY)
2047                    return;
2048            while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2049                   ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
2050                    head->read_step++;
2051            if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2052                    return;
2053            head->read = ccs_read_global_domain;
2054    }
2055    
2056    /**
2057     * ccs_get_argv0 - Get argv[0].
2058     *
2059     * @ee: Pointer to "struct ccs_execve".
2060     *
2061     * Returns true on success, false otherwise.
2062     */
2063    static bool ccs_get_argv0(struct ccs_execve *ee)
2064    {
2065            struct linux_binprm *bprm = ee->bprm;
2066            char *arg_ptr = ee->tmp;
2067            int arg_len = 0;
2068            unsigned long pos = bprm->p;
2069            int offset = pos % PAGE_SIZE;
2070            bool done = false;
2071            if (!bprm->argc)
2072                    goto out;
2073            while (1) {
2074                    if (!ccs_dump_page(bprm, pos, &ee->dump))
2075                            goto out;
2076                    pos += PAGE_SIZE - offset;
2077                    /* Read. */
2078                    while (offset < PAGE_SIZE) {
2079                            const char *kaddr = ee->dump.data;
2080                            const unsigned char c = kaddr[offset++];
2081                            if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
2082                                    if (c == '\\') {
2083                                            arg_ptr[arg_len++] = '\\';
2084                                            arg_ptr[arg_len++] = '\\';
2085                                    } else if (c > ' ' && c < 127) {
2086                                            arg_ptr[arg_len++] = c;
2087                                    } else {
2088                                            arg_ptr[arg_len++] = '\\';
2089                                            arg_ptr[arg_len++] = (c >> 6) + '0';
2090                                            arg_ptr[arg_len++]
2091                                                    = ((c >> 3) & 7) + '0';
2092                                            arg_ptr[arg_len++] = (c & 7) + '0';
2093                                    }
2094                            } else {
2095                                    arg_ptr[arg_len] = '\0';
2096                                    done = true;
2097                                    break;
2098                            }
2099                    }
2100                    offset = 0;
2101                    if (done)
2102                            break;
2103            }
2104            return true;
2105     out:
2106            return false;
2107    }
2108    
2109    /**
2110     * ccs_get_execute_condition - Get condition part for execute requests.
2111     *
2112     * @ee: Pointer to "struct ccs_execve".
2113     *
2114     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2115     */
2116    static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve
2117                                                           *ee)
2118    {
2119            struct ccs_condition *cond;
2120            char *buf;
2121            int len = 256;
2122            char *realpath = NULL;
2123            char *argv0 = NULL;
2124            const struct ccs_profile *profile = ccs_profile(ccs_current_domain()->
2125                                                            profile);
2126            if (profile->learning->learning_exec_realpath) {
2127                    struct file *file = ee->bprm->file;
2128    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2129                    struct path path = { file->f_vfsmnt, file->f_dentry };
2130                    realpath = ccs_realpath_from_path(&path);
2131    #else
2132                    realpath = ccs_realpath_from_path(&file->f_path);
2133    #endif
2134                    if (realpath)
2135                            len += strlen(realpath) + 17;
2136            }
2137            if (profile->learning->learning_exec_argv0) {
2138                    if (ccs_get_argv0(ee)) {
2139                            argv0 = ee->tmp;
2140                            len += strlen(argv0) + 16;
2141                    }
2142            }
2143            buf = kmalloc(len, CCS_GFP_FLAGS);
2144            if (!buf) {
2145                    kfree(realpath);
2146                    return NULL;
2147            }
2148            snprintf(buf, len - 1, "if");
2149            if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2150                    const int pos = strlen(buf);
2151                    snprintf(buf + pos, len - pos - 1,
2152                             " task.type=execute_handler");
2153            }
2154            if (realpath) {
2155                    const int pos = strlen(buf);
2156                    snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",
2157                             realpath);
2158                    kfree(realpath);
2159            }
2160            if (argv0) {
2161                    const int pos = strlen(buf);
2162                    snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",
2163                             argv0);
2164            }
2165            cond = ccs_get_condition(buf);
2166            kfree(buf);
2167            return cond;
2168    }
2169    
2170    /**
2171     * ccs_get_symlink_condition - Get condition part for symlink requests.
2172     *
2173     * @r: Pointer to "struct ccs_request_info".
2174     *
2175     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2176     */
2177    static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info
2178                                                           *r)
2179    {
2180            struct ccs_condition *cond;
2181            char *buf;
2182            int len = 256;
2183            const char *symlink = NULL;
2184            const struct ccs_profile *profile = ccs_profile(r->profile);
2185            if (profile->learning->learning_symlink_target) {
2186                    symlink = r->obj->symlink_target->name;
2187                    len += strlen(symlink) + 18;
2188            }
2189            buf = kmalloc(len, CCS_GFP_FLAGS);
2190            if (!buf)
2191                    return NULL;
2192            snprintf(buf, len - 1, "if");
2193            if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2194                    const int pos = strlen(buf);
2195                    snprintf(buf + pos, len - pos - 1,
2196                             " task.type=execute_handler");
2197            }
2198            if (symlink) {
2199                    const int pos = strlen(buf);
2200                    snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",
2201                             symlink);
2202            }
2203            cond = ccs_get_condition(buf);
2204            kfree(buf);
2205            return cond;
2206  }  }
2207    
2208  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 1713  static LIST_HEAD(ccs_query_list); Line 2228  static LIST_HEAD(ccs_query_list);
2228  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2229    
2230  /**  /**
2231   * ccs_check_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
2232   *   *
2233   * @r:       Pointer to "struct ccs_request_info".   * @r:       Pointer to "struct ccs_request_info".
2234   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt:     The printf()'s format string, followed by parameters.
2235   *   *
2236   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
2237   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2238   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
2239   * -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2240   */   */
2241  int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2242  {  {
2243          va_list args;          va_list args;
2244          int error = -EPERM;          int error = -EPERM;
# Line 1733  int ccs_check_supervisor(struct ccs_requ Line 2248  int ccs_check_supervisor(struct ccs_requ
2248          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query_entry *ccs_query_entry = NULL;
2249          bool quota_exceeded = false;          bool quota_exceeded = false;
2250          char *header;          char *header;
2251          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
2252                  r->domain = ccs_current_domain();          switch (r->mode) {
2253                    char *buffer;
2254                    struct ccs_condition *cond;
2255            case CCS_CONFIG_LEARNING:
2256                    if (!ccs_domain_quota_ok(r))
2257                            return 0;
2258                    va_start(args, fmt);
2259                    len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
2260                    va_end(args);
2261                    buffer = kmalloc(len, CCS_GFP_FLAGS);
2262                    if (!buffer)
2263                            return 0;
2264                    va_start(args, fmt);
2265                    vsnprintf(buffer, len - 1, fmt, args);
2266                    va_end(args);
2267                    ccs_normalize_line(buffer);
2268                    if (r->ee && !strncmp(buffer, "allow_execute ", 14))
2269                            cond = ccs_get_execute_condition(r->ee);
2270                    else if (r->obj && r->obj->symlink_target)
2271                            cond = ccs_get_symlink_condition(r);
2272                    else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
2273                            char str[] = "if task.type=execute_handler";
2274                            cond = ccs_get_condition(str);
2275                    } else
2276                            cond = NULL;
2277                    ccs_write_domain2(buffer, domain, cond, false);
2278                    ccs_put_condition(cond);
2279                    kfree(buffer);
2280                    /* fall through */
2281            case CCS_CONFIG_PERMISSIVE:
2282                    return 0;
2283            }
2284          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2285                  int i;                  int i;
2286                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2287                          return -EPERM;                          return -EPERM;
2288                  for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2289                       i++) {                               enforcing_penalty; i++) {
2290                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2291                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2292                  }                  }
# Line 1749  int ccs_check_supervisor(struct ccs_requ Line 2295  int ccs_check_supervisor(struct ccs_requ
2295          va_start(args, fmt);          va_start(args, fmt);
2296          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2297          va_end(args);          va_end(args);
2298          header = ccs_init_audit_log(&len, r);          header = ccs_init_log(&len, r);
2299          if (!header)          if (!header)
2300                  goto out;                  goto out;
2301          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2302          if (!ccs_query_entry)          if (!ccs_query_entry)
2303                  goto out;                  goto out;
2304          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          len = ccs_round2(len);
2305            ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2306          if (!ccs_query_entry->query)          if (!ccs_query_entry->query)
2307                  goto out;                  goto out;
2308          INIT_LIST_HEAD(&ccs_query_entry->list);          INIT_LIST_HEAD(&ccs_query_entry->list);
         /***** CRITICAL SECTION START *****/  
2309          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2310          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2311              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
# Line 1769  int ccs_check_supervisor(struct ccs_requ Line 2315  int ccs_check_supervisor(struct ccs_requ
2315                  ccs_query_entry->serial = ccs_serial++;                  ccs_query_entry->serial = ccs_serial++;
2316          }          }
2317          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2318          if (quota_exceeded)          if (quota_exceeded)
2319                  goto out;                  goto out;
2320          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 1780  int ccs_check_supervisor(struct ccs_requ Line 2325  int ccs_check_supervisor(struct ccs_requ
2325          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);
2326          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
2327          va_end(args);          va_end(args);
         /***** CRITICAL SECTION START *****/  
2328          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2329          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2330          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2331          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2332          for (ccs_query_entry->timer = 0;          for (ccs_query_entry->timer = 0;
2333               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;
# Line 1795  int ccs_check_supervisor(struct ccs_requ Line 2338  int ccs_check_supervisor(struct ccs_requ
2338                  if (ccs_query_entry->answer)                  if (ccs_query_entry->answer)
2339                          break;                          break;
2340          }          }
         /***** CRITICAL SECTION START *****/  
2341          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2342          list_del(&ccs_query_entry->list);          list_del(&ccs_query_entry->list);
2343          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
2344          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2345          switch (ccs_query_entry->answer) {          switch (ccs_query_entry->answer) {
2346          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2347                  error = 1;                  error = CCS_RETRY_REQUEST;
2348                  r->retry++;                  r->retry++;
2349                  break;                  break;
2350          case 1:          case 1:
# Line 1841  static int ccs_poll_query(struct file *f Line 2382  static int ccs_poll_query(struct file *f
2382          bool found = false;          bool found = false;
2383          u8 i;          u8 i;
2384          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
                 /***** CRITICAL SECTION START *****/  
2385                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2386                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2387                          struct ccs_query_entry *ptr                          struct ccs_query_entry *ptr =
2388                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, struct ccs_query_entry, list);
2389                          if (ptr->answer)                          if (ptr->answer)
2390                                  continue;                                  continue;
2391                          found = true;                          found = true;
2392                          break;                          break;
2393                  }                  }
2394                  spin_unlock(&ccs_query_list_lock);                  spin_unlock(&ccs_query_list_lock);
                 /***** CRITICAL SECTION END *****/  
2395                  if (found)                  if (found)
2396                          return POLLIN | POLLRDNORM;                          return POLLIN | POLLRDNORM;
2397                  if (i)                  if (i)
# Line 1866  static int ccs_poll_query(struct file *f Line 2405  static int ccs_poll_query(struct file *f
2405   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2406   *   *
2407   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
2408   */   */
2409  static int ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2410  {  {
2411          struct list_head *tmp;          struct list_head *tmp;
2412          int pos = 0;          int pos = 0;
2413          int len = 0;          int len = 0;
2414          char *buf;          char *buf;
2415          if (head->read_avail)          if (head->read_avail)
2416                  return 0;                  return;
2417          if (head->read_buf) {          if (head->read_buf) {
2418                  kfree(head->read_buf);                  kfree(head->read_buf);
2419                  head->read_buf = NULL;                  head->read_buf = NULL;
2420                  head->readbuf_size = 0;                  head->readbuf_size = 0;
2421          }          }
         /***** CRITICAL SECTION START *****/  
2422          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2423          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2424                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1895  static int ccs_read_query(struct ccs_io_ Line 2431  static int ccs_read_query(struct ccs_io_
2431                  break;                  break;
2432          }          }
2433          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2434          if (!len) {          if (!len) {
2435                  head->read_step = 0;                  head->read_step = 0;
2436                  return 0;                  return;
2437          }          }
2438          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2439          if (!buf)          if (!buf)
2440                  return 0;                  return;
2441          pos = 0;          pos = 0;
         /***** CRITICAL SECTION START *****/  
2442          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2443          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2444                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1922  static int ccs_read_query(struct ccs_io_ Line 2456  static int ccs_read_query(struct ccs_io_
2456                  break;                  break;
2457          }          }
2458          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2459          if (buf[0]) {          if (buf[0]) {
2460                  head->read_avail = len;                  head->read_avail = len;
2461                  head->readbuf_size = head->read_avail;                  head->readbuf_size = head->read_avail;
# Line 1931  static int ccs_read_query(struct ccs_io_ Line 2464  static int ccs_read_query(struct ccs_io_
2464          } else {          } else {
2465                  kfree(buf);                  kfree(buf);
2466          }          }
         return 0;  
2467  }  }
2468    
2469  /**  /**
# Line 1947  static int ccs_write_answer(struct ccs_i Line 2479  static int ccs_write_answer(struct ccs_i
2479          struct list_head *tmp;          struct list_head *tmp;
2480          unsigned int serial;          unsigned int serial;
2481          unsigned int answer;          unsigned int answer;
         /***** CRITICAL SECTION START *****/  
2482          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2483          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2484                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1955  static int ccs_write_answer(struct ccs_i Line 2486  static int ccs_write_answer(struct ccs_i
2486                  ptr->timer = 0;                  ptr->timer = 0;
2487          }          }
2488          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2489          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2490                  return -EINVAL;                  return -EINVAL;
         /***** CRITICAL SECTION START *****/  
2491          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2492          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2493                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1970  static int ccs_write_answer(struct ccs_i Line 2499  static int ccs_write_answer(struct ccs_i
2499                  break;                  break;
2500          }          }
2501          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2502          return 0;          return 0;
2503  }  }
2504    
# Line 1978  static int ccs_write_answer(struct ccs_i Line 2506  static int ccs_write_answer(struct ccs_i
2506   * ccs_read_version: Get version.   * ccs_read_version: Get version.
2507   *   *
2508   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns version information.  
2509   */   */
2510  static int ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2511  {  {
2512          if (!head->read_eof) {          if (head->read_eof)
2513                  ccs_io_printf(head, "1.7.0-pre");                  return;
2514                  head->read_eof = true;          ccs_io_printf(head, "1.7.2");
2515          }          head->read_eof = true;
         return 0;  
2516  }  }
2517    
2518  /**  /**
2519   * ccs_read_self_domain - Get the current process's domainname.   * ccs_read_self_domain - Get the current process's domainname.
2520   *   *
2521   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns the current process's domainname.  
2522   */   */
2523  static int ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2524  {  {
2525          if (!head->read_eof) {          if (head->read_eof)
2526                  /*                  return;
2527                   * ccs_current_domain()->domainname != NULL          /*
2528                   * because every process belongs to a domain and           * ccs_current_domain()->domainname != NULL because every process
2529                   * the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2530                   */           */
2531                  ccs_io_printf(head, "%s",          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2532                                ccs_current_domain()->domainname->name);          head->read_eof = true;
                 head->read_eof = true;  
         }  
         return 0;  
2533  }  }
2534    
2535  /**  /**
# Line 2022  static int ccs_read_self_domain(struct c Line 2542  static int ccs_read_self_domain(struct c
2542   */   */
2543  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2544  {  {
2545          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2546          if (!head)          if (!head)
2547                  return -ENOMEM;                  return -ENOMEM;
2548          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2549          head->type = type;          head->type = type;
2550          switch (type) {          switch (type) {
2551          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2552                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2553                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2554                  break;                  break;
2555          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2556                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2557                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2558                  break;                  break;
2559  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2560          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;  
2561          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2562                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2563                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2564                  break;                  break;
2565  #endif  #endif
2566          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2083  int ccs_open_control(const u8 type, stru Line 2600  int ccs_open_control(const u8 type, stru
2600                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2601                  break;                  break;
2602          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2603                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2604                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2605                  break;                  break;
2606          }          }
2607          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2094  int ccs_open_control(const u8 type, stru Line 2611  int ccs_open_control(const u8 type, stru
2611                   */                   */
2612                  head->read = NULL;                  head->read = NULL;
2613                  head->poll = NULL;                  head->poll = NULL;
2614          } else if (type != CCS_QUERY &&          } else if (!head->poll) {
2615                     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.  
                  */  
2616                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2617                          head->readbuf_size = 4096 * 2;                          head->readbuf_size = 4096;
2618                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2619                  if (!head->read_buf) {                  if (!head->read_buf) {
2620                          kfree(head);                          kfree(head);
2621                          return -ENOMEM;                          return -ENOMEM;
# Line 2115  int ccs_open_control(const u8 type, stru Line 2628  int ccs_open_control(const u8 type, stru
2628                   */                   */
2629                  head->write = NULL;                  head->write = NULL;
2630          } else if (head->write) {          } else if (head->write) {
2631                  head->writebuf_size = 4096 * 2;                  head->writebuf_size = 4096;
2632                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2633                  if (!head->write_buf) {                  if (!head->write_buf) {
2634                          kfree(head->read_buf);                          kfree(head->read_buf);
2635                          kfree(head);                          kfree(head);
# Line 2125  int ccs_open_control(const u8 type, stru Line 2638  int ccs_open_control(const u8 type, stru
2638          }          }
2639          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2640              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2641                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2642          file->private_data = head;          file->private_data = head;
2643          /*          /*
2644           * Call the handler now if the file is /proc/ccs/self_domain           * Call the handler now if the file is /proc/ccs/self_domain
# Line 2136  int ccs_open_control(const u8 type, stru Line 2649  int ccs_open_control(const u8 type, stru
2649                  ccs_read_control(file, NULL, 0);                  ccs_read_control(file, NULL, 0);
2650          /*          /*
2651           * If the file is /proc/ccs/query , increment the observer counter.           * If the file is /proc/ccs/query , increment the observer counter.
2652           * The obserber counter is used by ccs_check_supervisor() to see if           * The obserber counter is used by ccs_supervisor() to see if
2653           * there is some process monitoring /proc/ccs/query.           * there is some process monitoring /proc/ccs/query.
2654           */           */
2655          else if (type == CCS_QUERY)          else if (type == CCS_QUERY)
# Line 2151  int ccs_open_control(const u8 type, stru Line 2664  int ccs_open_control(const u8 type, stru
2664   * @wait: Pointer to "poll_table".   * @wait: Pointer to "poll_table".
2665   *   *
2666   * Waits for read readiness.   * Waits for read readiness.
2667   * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2668   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2669   * /usr/lib/ccs/ccs-auditd.   * /usr/sbin/ccs-auditd .
2670   */   */
2671  int ccs_poll_control(struct file *file, poll_table *wait)  int ccs_poll_control(struct file *file, poll_table *wait)
2672  {  {
# Line 2178  int ccs_read_control(struct file *file, Line 2691  int ccs_read_control(struct file *file,
2691          int len = 0;          int len = 0;
2692          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2693          char *cp;          char *cp;
2694            int idx;
2695          if (!head->read)          if (!head->read)
2696                  return -ENOSYS;                  return -ENOSYS;
2697          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2698                  return -EFAULT;                  return -EFAULT;
2699          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2700                  return -EINTR;                  return -EINTR;
2701          /* Call the policy handler. */          idx = ccs_read_lock();
2702          len = head->read(head);          while (1) {
2703          if (len < 0)                  /* Call the policy handler. */
2704                  goto out;                  head->read(head);
2705          /* Write to buffer. */                  /* Write to buffer. */
2706          len = head->read_avail;                  len = head->read_avail;
2707                    if (len || head->poll || head->read_eof)
2708                            break;
2709                    len = head->readbuf_size * 2;
2710                    cp = kzalloc(len, CCS_GFP_FLAGS);
2711                    if (!cp) {
2712                            len = -ENOMEM;
2713                            goto out;
2714                    }
2715                    kfree(head->read_buf);
2716                    head->read_buf = cp;
2717                    head->readbuf_size = len;
2718            }
2719          if (len > buffer_len)          if (len > buffer_len)
2720                  len = buffer_len;                  len = buffer_len;
2721          if (!len)          if (!len)
# Line 2203  int ccs_read_control(struct file *file, Line 2729  int ccs_read_control(struct file *file,
2729          head->read_avail -= len;          head->read_avail -= len;
2730          memmove(cp, cp + len, head->read_avail);          memmove(cp, cp + len, head->read_avail);
2731   out:   out:
2732            ccs_read_unlock(idx);
2733          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2734          return len;          return len;
2735  }  }
# Line 2223  int ccs_write_control(struct file *file, Line 2750  int ccs_write_control(struct file *file,
2750          int error = buffer_len;          int error = buffer_len;
2751          int avail_len = buffer_len;          int avail_len = buffer_len;
2752          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2753            int idx;
2754          if (!head->write)          if (!head->write)
2755                  return -ENOSYS;                  return -ENOSYS;
2756          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2757                  return -EFAULT;                  return -EFAULT;
         /* Don't allow updating policies by non manager programs. */  
         if (head->write != ccs_write_pid &&  
             head->write != ccs_write_domain_policy &&  
             !ccs_is_policy_manager())  
                 return -EPERM;  
2758          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2759                  return -EINTR;                  return -EINTR;
2760            idx = ccs_read_lock();
2761            /* Don't allow updating policies by non manager programs. */
2762            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2763                !ccs_manager()) {
2764                    ccs_read_unlock(idx);
2765                    mutex_unlock(&head->io_sem);
2766                    return -EPERM;
2767            }
2768          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2769          while (avail_len > 0) {          while (avail_len > 0) {
2770                  char c;                  char c;
2771                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->write_avail >= head->writebuf_size - 1) {