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

Subversion リポジトリの参照

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

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

branches/ccs-patch/security/ccsecurity/policy_io.c revision 2864 by kumaneko, Fri Aug 7 06:40:37 2009 UTC trunk/1.7.x/ccs-patch/security/ccsecurity/policy_io.c revision 3531 by kumaneko, Thu Mar 25 05:14:06 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/07/03   * Version: 1.7.2-pre   2010/03/21
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 12  Line 12 
12    
13  #include "internal.h"  #include "internal.h"
14    
15  /* Lock for protecting ccs_profile->comment  */  static struct ccs_profile ccs_default_profile = {
16  static DEFINE_SPINLOCK(ccs_profile_comment_lock);          .learning = &ccs_default_profile.preference,
17            .permissive = &ccs_default_profile.preference,
18            .enforcing = &ccs_default_profile.preference,
19            .audit = &ccs_default_profile.preference,
20    #ifdef CONFIG_CCSECURITY_AUDIT
21            .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
22            .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
23    #endif
24            .preference.audit_task_info = true,
25            .preference.audit_path_info = true,
26            .preference.enforcing_penalty = 0,
27            .preference.enforcing_verbose = true,
28            .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
29            .preference.learning_verbose = false,
30            .preference.learning_exec_realpath = true,
31            .preference.learning_exec_argv0 = true,
32            .preference.learning_symlink_target = true,
33            .preference.permissive_verbose = true
34    };
35    
36  static bool ccs_profile_entry_used[CCS_MAX_CONTROL_INDEX +  /* Profile version. Currently only 20090903 is defined. */
37                                     CCS_MAX_CAPABILITY_INDEX + 1];  static unsigned int ccs_profile_version;
38    
39    /* Profile table. Memory is allocated as needed. */
40    static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
42  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
43  static const char *ccs_mode_4[4] = {  static const char *ccs_mode_4[4] = {
44          "disabled", "learning", "permissive", "enforcing"          "disabled", "learning", "permissive", "enforcing"
45  };  };
 /* String table for functionality that takes 2 modes. */  
 static const char *ccs_mode_2[4] = {  
         "disabled", "enabled", "enabled", "enabled"  
 };  
46    
47  /* Table for profile. */  /* String table for /proc/ccs/profile */
48  static struct {  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +
49          const char *keyword;                                      CCS_MAX_CAPABILITY_INDEX +
50          unsigned int current_value;                                      CCS_MAX_MAC_CATEGORY_INDEX] = {
51          const unsigned int max_value;          [CCS_MAC_FILE_EXECUTE]
52  } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {          = "file::execute",
53          [CCS_MAC_FOR_FILE]        = { "MAC_FOR_FILE",        0, 3 },          [CCS_MAC_FILE_OPEN]
54          [CCS_MAC_FOR_IOCTL]       = { "MAC_FOR_IOCTL",       0, 3 },          = "file::open",
55          [CCS_MAC_FOR_ARGV0]       = { "MAC_FOR_ARGV0",       0, 3 },          [CCS_MAC_FILE_CREATE]
56          [CCS_MAC_FOR_ENV]         = { "MAC_FOR_ENV",         0, 3 },          = "file::create",
57          [CCS_MAC_FOR_NETWORK]     = { "MAC_FOR_NETWORK",     0, 3 },          [CCS_MAC_FILE_UNLINK]
58          [CCS_MAC_FOR_SIGNAL]      = { "MAC_FOR_SIGNAL",      0, 3 },          = "file::unlink",
59          [CCS_MAC_FOR_NAMESPACE]   = { "MAC_FOR_NAMESPACE",   0, 3 },          [CCS_MAC_FILE_MKDIR]
60          [CCS_RESTRICT_AUTOBIND]   = { "RESTRICT_AUTOBIND",   0, 1 },          = "file::mkdir",
61          [CCS_MAX_ACCEPT_ENTRY]          [CCS_MAC_FILE_RMDIR]
62          = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },          = "file::rmdir",
63  #ifdef CONFIG_CCSECURITY_AUDIT          [CCS_MAC_FILE_MKFIFO]
64          [CCS_MAX_GRANT_LOG]          = "file::mkfifo",
65          = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },          [CCS_MAC_FILE_MKSOCK]
66          [CCS_MAX_REJECT_LOG]          = "file::mksock",
67          = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },          [CCS_MAC_FILE_TRUNCATE]
68  #endif          = "file::truncate",
69          [CCS_VERBOSE]             = { "TOMOYO_VERBOSE",      1, 1 },          [CCS_MAC_FILE_SYMLINK]
70          [CCS_SLEEP_PERIOD]          = "file::symlink",
71          = { "SLEEP_PERIOD",        0, 3000 }, /* in 0.1 second */          [CCS_MAC_FILE_REWRITE]
72            = "file::rewrite",
73            [CCS_MAC_FILE_MKBLOCK]
74            = "file::mkblock",
75            [CCS_MAC_FILE_MKCHAR]
76            = "file::mkchar",
77            [CCS_MAC_FILE_LINK]
78            = "file::link",
79            [CCS_MAC_FILE_RENAME]
80            = "file::rename",
81            [CCS_MAC_FILE_CHMOD]
82            = "file::chmod",
83            [CCS_MAC_FILE_CHOWN]
84            = "file::chown",
85            [CCS_MAC_FILE_CHGRP]
86            = "file::chgrp",
87            [CCS_MAC_FILE_IOCTL]
88            = "file::ioctl",
89            [CCS_MAC_FILE_CHROOT]
90            = "file::chroot",
91            [CCS_MAC_FILE_MOUNT]
92            = "file::mount",
93            [CCS_MAC_FILE_UMOUNT]
94            = "file::umount",
95            [CCS_MAC_FILE_PIVOT_ROOT]
96            = "file::pivot_root",
97            [CCS_MAC_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 106  bool ccs_io_printf(struct ccs_io_buffer Line 252  bool ccs_io_printf(struct ccs_io_buffer
252   *   *
253   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
254   */   */
255  struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int
256                                                     profile)                                                            profile)
257  {  {
258          struct ccs_profile *ptr;          struct ccs_profile *ptr;
259          struct ccs_profile *entry;          struct ccs_profile *entry;
260          int i;          if (profile >= CCS_MAX_PROFILES)
         if (profile >= MAX_PROFILES)  
261                  return NULL;                  return NULL;
262          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
263          if (ptr)          if (ptr)
264                  return ptr;                  return ptr;
265          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
266          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
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;
# Line 138  struct ccs_profile *ccs_find_or_assign_n Line 285  struct ccs_profile *ccs_find_or_assign_n
285  }  }
286    
287  /**  /**
288     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
289     */
290    static void ccs_check_profile(void)
291    {
292            struct ccs_domain_info *domain;
293            ccs_policy_loaded = true;
294            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
295                    const u8 profile = domain->profile;
296                    if (ccs_profile_ptr[profile])
297                            continue;
298                    panic("Profile %u (used by '%s') not defined.\n",
299                          profile, domain->domainname->name);
300            }
301            if (ccs_profile_version != 20090903)
302                    panic("Profile version %u is not supported.\n",
303                          ccs_profile_version);
304            printk(KERN_INFO "CCSecurity: 1.7.2-pre   2010/03/21\n");
305            printk(KERN_INFO "Mandatory Access Control activated.\n");
306    }
307    
308    /**
309     * ccs_profile - Find a profile.
310     *
311     * @profile: Profile number to find.
312     *
313     * Returns pointer to "struct ccs_profile".
314     */
315    struct ccs_profile *ccs_profile(const u8 profile)
316    {
317            struct ccs_profile *ptr = ccs_profile_ptr[profile];
318            if (!ccs_policy_loaded)
319                    return &ccs_default_profile;
320            BUG_ON(!ptr);
321            return ptr;
322    }
323    
324    /**
325   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
326   *   *
327   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 148  static int ccs_write_profile(struct ccs_ Line 332  static int ccs_write_profile(struct ccs_
332  {  {
333          char *data = head->write_buf;          char *data = head->write_buf;
334          unsigned int i;          unsigned int i;
335          unsigned int value;          int value;
336            int mode;
337            u8 config;
338            bool use_default = false;
339          char *cp;          char *cp;
340          struct ccs_profile *ccs_profile;          struct ccs_profile *profile;
341            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
342                    return 0;
343          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
344          if (data != cp) {          if (data == cp) {
345                    profile = &ccs_default_profile;
346            } else {
347                  if (*cp != '-')                  if (*cp != '-')
348                          return -EINVAL;                          return -EINVAL;
349                  data = cp + 1;                  data = cp + 1;
350                    profile = ccs_find_or_assign_new_profile(i);
351                    if (!profile)
352                            return -EINVAL;
353          }          }
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
                 return -EINVAL;  
354          cp = strchr(data, '=');          cp = strchr(data, '=');
355          if (!cp)          if (!cp)
356                  return -EINVAL;                  return -EINVAL;
357          *cp = '\0';          *cp++ = '\0';
358            if (profile != &ccs_default_profile)
359                    use_default = strstr(cp, "use_default") != NULL;
360            if (strstr(cp, "verbose=yes"))
361                    value = 1;
362            else if (strstr(cp, "verbose=no"))
363                    value = 0;
364            else
365                    value = -1;
366            if (!strcmp(data, "PREFERENCE::audit")) {
367    #ifdef CONFIG_CCSECURITY_AUDIT
368                    char *cp2;
369    #endif
370                    if (use_default) {
371                            profile->audit = &ccs_default_profile.preference;
372                            return 0;
373                    }
374                    profile->audit = &profile->preference;
375    #ifdef CONFIG_CCSECURITY_AUDIT
376                    cp2 = strstr(cp, "max_grant_log=");
377                    if (cp2)
378                            sscanf(cp2 + 14, "%u",
379                                   &profile->preference.audit_max_grant_log);
380                    cp2 = strstr(cp, "max_reject_log=");
381                    if (cp2)
382                            sscanf(cp2 + 15, "%u",
383                                   &profile->preference.audit_max_reject_log);
384    #endif
385                    if (strstr(cp, "task_info=yes"))
386                            profile->preference.audit_task_info = true;
387                    else if (strstr(cp, "task_info=no"))
388                            profile->preference.audit_task_info = false;
389                    if (strstr(cp, "path_info=yes"))
390                            profile->preference.audit_path_info = true;
391                    else if (strstr(cp, "path_info=no"))
392                            profile->preference.audit_path_info = false;
393                    return 0;
394            }
395            if (!strcmp(data, "PREFERENCE::enforcing")) {
396                    char *cp2;
397                    if (use_default) {
398                            profile->enforcing = &ccs_default_profile.preference;
399                            return 0;
400                    }
401                    profile->enforcing = &profile->preference;
402                    if (value >= 0)
403                            profile->preference.enforcing_verbose = value;
404                    cp2 = strstr(cp, "penalty=");
405                    if (cp2)
406                            sscanf(cp2 + 8, "%u",
407                                   &profile->preference.enforcing_penalty);
408                    return 0;
409            }
410            if (!strcmp(data, "PREFERENCE::permissive")) {
411                    if (use_default) {
412                            profile->permissive = &ccs_default_profile.preference;
413                            return 0;
414                    }
415                    profile->permissive = &profile->preference;
416                    if (value >= 0)
417                            profile->preference.permissive_verbose = value;
418                    return 0;
419            }
420            if (!strcmp(data, "PREFERENCE::learning")) {
421                    char *cp2;
422                    if (use_default) {
423                            profile->learning = &ccs_default_profile.preference;
424                            return 0;
425                    }
426                    profile->learning = &profile->preference;
427                    if (value >= 0)
428                            profile->preference.learning_verbose = value;
429                    cp2 = strstr(cp, "max_entry=");
430                    if (cp2)
431                            sscanf(cp2 + 10, "%u",
432                                   &profile->preference.learning_max_entry);
433                    if (strstr(cp, "exec.realpath=yes"))
434                            profile->preference.learning_exec_realpath = true;
435                    else if (strstr(cp, "exec.realpath=no"))
436                            profile->preference.learning_exec_realpath = false;
437                    if (strstr(cp, "exec.argv0=yes"))
438                            profile->preference.learning_exec_argv0 = true;
439                    else if (strstr(cp, "exec.argv0=no"))
440                            profile->preference.learning_exec_argv0 = false;
441                    if (strstr(cp, "symlink.target=yes"))
442                            profile->preference.learning_symlink_target = true;
443                    else if (strstr(cp, "symlink.target=no"))
444                            profile->preference.learning_symlink_target = false;
445                    return 0;
446            }
447            if (profile == &ccs_default_profile)
448                    return -EINVAL;
449          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
450                  const struct ccs_path_info *new_comment                  const struct ccs_path_info *old_comment = profile->comment;
451                          = 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 *****/  
452                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
                 ccs_profile_entry_used[0] = true;  
453                  return 0;                  return 0;
454          }          }
455          if (ccs_str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {          if (!strcmp(data, "CONFIG")) {
456                  if (sscanf(cp + 1, "%u", &value) != 1) {                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
457                          for (i = 0; i < 4; i++) {                          + CCS_MAX_MAC_CATEGORY_INDEX;
458                                  if (strcmp(cp + 1, ccs_mode_4[i]))                  config = profile->default_config;
459                                          continue;          } else if (ccs_str_starts(&data, "CONFIG::")) {
460                                  value = i;                  config = 0;
461                                  break;                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
462                          }                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
463                          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]))  
464                                  continue;                                  continue;
465                          ccs_profile->capability_value[i] = value;                          config = profile->config[i];
466                          ccs_profile_entry_used[i + 1 + CCS_MAX_CONTROL_INDEX]                          break;
                                 = true;  
                         return 0;  
467                  }                  }
468                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
469                        + CCS_MAX_MAC_CATEGORY_INDEX)
470                            return -EINVAL;
471            } else {
472                  return -EINVAL;                  return -EINVAL;
473          }          }
474          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {          if (use_default) {
475                  if (strcmp(data, ccs_control_array[i].keyword))                  config = CCS_CONFIG_USE_DEFAULT;
476                          continue;          } else {
477                  if (sscanf(cp + 1, "%u", &value) != 1) {                  for (mode = 3; mode >= 0; mode--)
478                          int j;                          if (strstr(cp, ccs_mode_4[mode]))
479                          const char **modes;                                  /*
480                          switch (i) {                                   * Update lower 3 bits in order to distinguish
481                          case CCS_RESTRICT_AUTOBIND:                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
482                          case CCS_VERBOSE:                                   */
483                                  modes = ccs_mode_2;                                  config = (config & ~7) | mode;
484                                  break;  #ifdef CONFIG_CCSECURITY_AUDIT
485                          default:                  if (config != CCS_CONFIG_USE_DEFAULT) {
486                                  modes = ccs_mode_4;                          if (strstr(cp, "grant_log=yes"))
487                                  break;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
488                          }                          else if (strstr(cp, "grant_log=no"))
489                          for (j = 0; j < 4; j++) {                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
490                                  if (strcmp(cp + 1, modes[j]))                          if (strstr(cp, "reject_log=yes"))
491                                          continue;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
492                                  value = j;                          else if (strstr(cp, "reject_log=no"))
493                                  break;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
                         }  
                         if (j == 4)  
                                 return -EINVAL;  
                 } else if (value > ccs_control_array[i].max_value) {  
                         value = ccs_control_array[i].max_value;  
494                  }                  }
495                  ccs_profile->value[i] = value;  #endif
                 ccs_profile_entry_used[i + 1] = true;  
                 return 0;  
496          }          }
497          return -EINVAL;          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
498                + CCS_MAX_MAC_CATEGORY_INDEX)
499                    profile->config[i] = config;
500            else if (config != CCS_CONFIG_USE_DEFAULT)
501                    profile->default_config = config;
502            return 0;
503  }  }
504    
505  /**  /**
506   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
507   *   *
508   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
509   */   */
510  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
511  {  {
512          static const int ccs_total          int index;
                 = CCS_MAX_CONTROL_INDEX + CCS_MAX_CAPABILITY_INDEX + 1;  
         int step;  
513          if (head->read_eof)          if (head->read_eof)
514                  return 0;                  return;
515          for (step = head->read_step; step < MAX_PROFILES * ccs_total; step++) {          if (head->read_bit)
516                  const u8 index = step / ccs_total;                  goto body;
517                  u8 type = step % ccs_total;          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
518                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];          ccs_io_printf(head, "PREFERENCE::audit={ "
519                  head->read_step = step;  #ifdef CONFIG_CCSECURITY_AUDIT
520                  if (!ccs_profile)                        "max_grant_log=%u max_reject_log=%u "
521                          continue;  #endif
522                  if (!ccs_profile_entry_used[type])                        "task_info=%s path_info=%s }\n",
523                          continue;  #ifdef CONFIG_CCSECURITY_AUDIT
524                  if (!type) { /* Print profile' comment tag. */                        ccs_default_profile.preference.audit_max_grant_log,
525                          bool done;                        ccs_default_profile.preference.audit_max_reject_log,
526                          /***** CRITICAL SECTION START *****/  #endif
527                          spin_lock(&ccs_profile_comment_lock);                        ccs_yesno(ccs_default_profile.preference.
528                          done = ccs_io_printf(head, "%u-COMMENT=%s\n",                                  audit_task_info),
529                                               index, ccs_profile->comment ?                        ccs_yesno(ccs_default_profile.preference.
530                                               ccs_profile->comment->name : "");                                  audit_path_info));
531                          spin_unlock(&ccs_profile_comment_lock);          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "
532                          /***** CRITICAL SECTION END *****/                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",
533                          if (!done)                        ccs_yesno(ccs_default_profile.preference.
534                                  break;                                  learning_verbose),
535                          ccs_default_profile.preference.learning_max_entry,
536                          ccs_yesno(ccs_default_profile.preference.
537                                    learning_exec_realpath),
538                          ccs_yesno(ccs_default_profile.preference.
539                                    learning_exec_argv0),
540                          ccs_yesno(ccs_default_profile.preference.
541                                    learning_symlink_target));
542            ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",
543                          ccs_yesno(ccs_default_profile.preference.
544                                    permissive_verbose));
545            ccs_io_printf(head, "PREFERENCE::enforcing={ verbose=%s penalty=%u "
546                          "}\n",
547                          ccs_yesno(ccs_default_profile.preference.
548                                    enforcing_verbose),
549                          ccs_default_profile.preference.enforcing_penalty);
550            head->read_bit = 1;
551     body:
552            for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {
553                    bool done;
554                    u8 config;
555                    int i;
556                    int pos;
557                    const struct ccs_profile *profile = ccs_profile_ptr[index];
558                    const struct ccs_path_info *comment;
559                    head->read_step = index;
560                    if (!profile)
561                          continue;                          continue;
562                    pos = head->read_avail;
563                    comment = profile->comment;
564                    done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,
565                                         comment ? comment->name : "");
566                    if (!done)
567                            goto out;
568                    config = profile->default_config;
569    #ifdef CONFIG_CCSECURITY_AUDIT
570                    if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "
571                                       "reject_log=%s }\n", index,
572                                       ccs_mode_4[config & 3],
573                                       ccs_yesno(config &
574                                                 CCS_CONFIG_WANT_GRANT_LOG),
575                                       ccs_yesno(config &
576                                                 CCS_CONFIG_WANT_REJECT_LOG)))
577                            goto out;
578    #else
579                    if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,
580                                       ccs_mode_4[config & 3]))
581                            goto out;
582    #endif
583                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
584                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
585    #ifdef CONFIG_CCSECURITY_AUDIT
586                            const char *g;
587                            const char *r;
588    #endif
589                            config = profile->config[i];
590                            if (config == CCS_CONFIG_USE_DEFAULT)
591                                    continue;
592    #ifdef CONFIG_CCSECURITY_AUDIT
593                            g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);
594                            r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);
595                            if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "
596                                               "grant_log=%s reject_log=%s }\n",
597                                               index, ccs_mac_keywords[i],
598                                               ccs_mode_4[config & 3], g, r))
599                                    goto out;
600    #else
601                            if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",
602                                               index, ccs_mac_keywords[i],
603                                               ccs_mode_4[config & 3]))
604                                    goto out;
605    #endif
606                  }                  }
607                  type--;                  if (profile->audit != &ccs_default_profile.preference &&
608                  if (type >= CCS_MAX_CONTROL_INDEX) {                      !ccs_io_printf(head, "%u-PREFERENCE::audit={ "
609                          const int i = type - CCS_MAX_CONTROL_INDEX;  #ifdef CONFIG_CCSECURITY_AUDIT
610                          const u8 value = ccs_profile->capability_value[i];                                     "max_grant_log=%u max_reject_log=%u "
611                          if (!ccs_io_printf(head,  #endif
612                                             "%u-" KEYWORD_MAC_FOR_CAPABILITY                                     "task_info=%s path_info=%s }\n", index,
613                                             "%s=%s\n", index,  #ifdef CONFIG_CCSECURITY_AUDIT
614                                             ccs_capability_control_keyword[i],                                     profile->preference.audit_max_grant_log,
615                                             ccs_mode_4[value]))                                     profile->preference.audit_max_reject_log,
616                                  break;  #endif
617                  } else {                                     ccs_yesno(profile->preference.
618                          const unsigned int value = ccs_profile->value[type];                                               audit_task_info),
619                          const char **modes = NULL;                                     ccs_yesno(profile->preference.
620                          const char *keyword = ccs_control_array[type].keyword;                                               audit_path_info)))
621                          switch (ccs_control_array[type].max_value) {                          goto out;
622                          case 3:                  if (profile->learning != &ccs_default_profile.preference &&
623                                  modes = ccs_mode_4;                      !ccs_io_printf(head, "%u-PREFERENCE::learning={ "
624                                  break;                                     "verbose=%s max_entry=%u exec.realpath=%s "
625                          case 1:                                     "exec.argv0=%s symlink.target=%s }\n",
626                                  modes = ccs_mode_2;                                     index,
627                                  break;                                     ccs_yesno(profile->preference.
628                          }                                               learning_verbose),
629                          if (modes) {                                     profile->preference.learning_max_entry,
630                                  if (!ccs_io_printf(head, "%u-%s=%s\n", index,                                     ccs_yesno(profile->preference.
631                                                     keyword, modes[value]))                                               learning_exec_realpath),
632                                          break;                                     ccs_yesno(profile->preference.
633                          } else {                                               learning_exec_argv0),
634                                  if (!ccs_io_printf(head, "%u-%s=%u\n", index,                                     ccs_yesno(profile->preference.
635                                                     keyword, value))                                               learning_symlink_target)))
636                                          break;                          goto out;
637                          }                  if (profile->permissive != &ccs_default_profile.preference &&
638                  }                      !ccs_io_printf(head, "%u-PREFERENCE::permissive={ "
639                                       "verbose=%s }\n", index,
640                                       ccs_yesno(profile->preference.
641                                                 permissive_verbose)))
642                            goto out;
643                    if (profile->enforcing != &ccs_default_profile.preference &&
644                        !ccs_io_printf(head, "%u-PREFERENCE::enforcing={ "
645                                       "verbose=%s penalty=%u }\n", index,
646                                       ccs_yesno(profile->preference.
647                                                 enforcing_verbose),
648                                       profile->preference.enforcing_penalty))
649                            goto out;
650                    continue;
651     out:
652                    head->read_avail = pos;
653                    break;
654          }          }
655          if (step == MAX_PROFILES * ccs_total)          if (index == CCS_MAX_PROFILES)
656                  head->read_eof = true;                  head->read_eof = true;
         return 0;  
657  }  }
658    
659  /* The list for "struct ccs_policy_manager_entry". */  /* The list for "struct ccs_policy_manager_entry". */
# Line 322  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  {  {
         struct ccs_policy_manager_entry *entry = NULL;  
672          struct ccs_policy_manager_entry *ptr;          struct ccs_policy_manager_entry *ptr;
673          const struct ccs_path_info *saved_manager;          struct ccs_policy_manager_entry e = { };
674          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
         bool is_domain = false;  
675          if (ccs_is_domain_def(manager)) {          if (ccs_is_domain_def(manager)) {
676                  if (!ccs_is_correct_domain(manager))                  if (!ccs_is_correct_domain(manager))
677                          return -EINVAL;                          return -EINVAL;
678                  is_domain = true;                  e.is_domain = true;
679          } else {          } else {
680                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_is_correct_path(manager, 1, -1, -1))
681                          return -EINVAL;                          return -EINVAL;
682          }          }
683          saved_manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
684          if (!saved_manager)          if (!e.manager)
685                  return -ENOMEM;                  return -ENOMEM;
         if (!is_delete)  
                 entry = kzalloc(sizeof(*entry), GFP_KERNEL);  
686          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
687          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
688                  if (ptr->manager != saved_manager)                  if (ptr->manager != e.manager)
689                          continue;                          continue;
690                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
691                  error = 0;                  error = 0;
692                  break;                  break;
693          }          }
694          if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {          if (!is_delete && error) {
695                  entry->manager = saved_manager;                  struct ccs_policy_manager_entry *entry =
696                  saved_manager = NULL;                          ccs_commit_ok(&e, sizeof(e));
697                  entry->is_domain = is_domain;                  if (entry) {
698                  list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);                          list_add_tail_rcu(&entry->list,
699                  entry = NULL;                                            &ccs_policy_manager_list);
700                  error = 0;                          error = 0;
701                    }
702          }          }
703          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
704          ccs_put_name(saved_manager);          ccs_put_name(e.manager);
         kfree(entry);  
705          return error;          return error;
706  }  }
707    
# Line 372  static int ccs_update_manager_entry(cons Line 715  static int ccs_update_manager_entry(cons
715  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager_policy(struct ccs_io_buffer *head)
716  {  {
717          char *data = head->write_buf;          char *data = head->write_buf;
718          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
719          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
720                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
721                  return 0;                  return 0;
# Line 385  static int ccs_write_manager_policy(stru Line 728  static int ccs_write_manager_policy(stru
728   *   *
729   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
730   *   *
  * Returns 0.  
  *  
731   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
732   */   */
733  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager_policy(struct ccs_io_buffer *head)
734  {  {
735          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
736          if (head->read_eof)          if (head->read_eof)
737                  return 0;                  return;
738          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {
739                  struct ccs_policy_manager_entry *ptr;                  struct ccs_policy_manager_entry *ptr;
740                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);
741                  if (ptr->is_deleted)                  if (ptr->is_deleted)
742                          continue;                          continue;
743                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
744                          return 0;                          return;
745          }          }
746          head->read_eof = true;          head->read_eof = true;
         return 0;  
747  }  }
748    
749  /**  /**
# Line 423  static bool ccs_is_policy_manager(void) Line 762  static bool ccs_is_policy_manager(void)
762          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
763                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
764          bool found = false;          bool found = false;
         ccs_check_read_lock();  
765          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
766                  return true;                  return true;
767          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)
# Line 504  static bool ccs_is_select_one(struct ccs Line 842  static bool ccs_is_select_one(struct ccs
842  {  {
843          unsigned int pid;          unsigned int pid;
844          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
845          ccs_check_read_lock();          bool global_pid = false;
846          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "allow_execute")) {
847                  head->read_execute_only = true;                  head->read_execute_only = true;
848                  return true;                  return true;
849          }          }
850          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
851                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
852                  struct task_struct *p;                  struct task_struct *p;
853                  /***** CRITICAL SECTION START *****/                  ccs_tasklist_lock();
854                  read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
855                    if (global_pid)
856                            p = ccsecurity_exports.find_task_by_pid_ns(pid,
857                                                                   &init_pid_ns);
858                    else
859                            p = ccsecurity_exports.find_task_by_vpid(pid);
860    #else
861                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
862    #endif
863                  if (p)                  if (p)
864                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
865                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
                 /***** CRITICAL SECTION END *****/  
866          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
867                  if (ccs_is_domain_def(data + 7))                  if (ccs_is_domain_def(data + 7))
868                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
# Line 548  static bool ccs_is_select_one(struct ccs Line 893  static bool ccs_is_select_one(struct ccs
893          return true;          return true;
894  }  }
895    
896    static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,
897                                        struct ccs_condition *cond,
898                                        const bool is_delete)
899    {
900            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))
901                    return ccs_write_capability_policy(data, domain, cond,
902                                                       is_delete);
903            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))
904                    return ccs_write_network_policy(data, domain, cond, is_delete);
905            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))
906                    return ccs_write_signal_policy(data, domain, cond, is_delete);
907            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))
908                    return ccs_write_env_policy(data, domain, cond, is_delete);
909            if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))
910                    return ccs_write_mount_policy(data, domain, cond, is_delete);
911            return ccs_write_file_policy(data, domain, cond, is_delete);
912    }
913    
914  /**  /**
915   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain_policy - Write domain policy.
916   *   *
# Line 565  static int ccs_write_domain_policy(struc Line 928  static int ccs_write_domain_policy(struc
928          struct ccs_condition *cond = NULL;          struct ccs_condition *cond = NULL;
929          char *cp;          char *cp;
930          int error;          int error;
931          if (ccs_str_starts(&data, KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
932                  is_delete = true;                  is_delete = true;
933          else if (ccs_str_starts(&data, KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
934                  is_select = true;                  is_select = true;
935          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_is_select_one(head, data))
936                  return 0;                  return 0;
# Line 588  static int ccs_write_domain_policy(struc Line 951  static int ccs_write_domain_policy(struc
951          if (!domain)          if (!domain)
952                  return -EINVAL;                  return -EINVAL;
953    
954          if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
955              && profile < MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
956                  if (ccs_profile_ptr[profile] || !ccs_policy_loaded)                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
957                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
958                  return 0;                  return 0;
959          }          }
960          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
961                  domain->ignore_global_allow_read = !is_delete;                  domain->ignore_global_allow_read = !is_delete;
962                  return 0;                  return 0;
963          }          }
964          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
965                  domain->ignore_global_allow_env = !is_delete;                  domain->ignore_global_allow_env = !is_delete;
966                  return 0;                  return 0;
967          }          }
968            if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {
969                    domain->quota_warned = !is_delete;
970                    return 0;
971            }
972            if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {
973                    domain->domain_transition_failed = !is_delete;
974                    return 0;
975            }
976          cp = ccs_find_condition_part(data);          cp = ccs_find_condition_part(data);
977          if (cp) {          if (cp) {
978                  cond = ccs_get_condition(cp);                  cond = ccs_get_condition(cp);
979                  if (!cond)                  if (!cond)
980                          return -EINVAL;                          return -EINVAL;
981          }          }
982          if (ccs_str_starts(&data, KEYWORD_ALLOW_CAPABILITY))          error = ccs_write_domain_policy2(data, domain, cond, is_delete);
                 error = ccs_write_capability_policy(data, domain, cond,  
                                                     is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_NETWORK))  
                 error = ccs_write_network_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_SIGNAL))  
                 error = ccs_write_signal_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_ARGV0))  
                 error = ccs_write_argv0_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))  
                 error = ccs_write_env_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_IOCTL))  
                 error = ccs_write_ioctl_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_MOUNT))  
                 error = ccs_write_mount_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_UNMOUNT))  
                 error = ccs_write_umount_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_CHROOT))  
                 error = ccs_write_chroot_policy(data, domain, cond, is_delete);  
         else if (ccs_str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT))  
                 error = ccs_write_pivot_root_policy(data, domain, cond,  
                                                     is_delete);  
         else  
                 error = ccs_write_file_policy(data, domain, cond, is_delete);  
983          if (cond)          if (cond)
984                  ccs_put_condition(cond);                  ccs_put_condition(cond);
985          return error;          return error;
986  }  }
987    
988  static bool ccs_print_name_union(struct ccs_io_buffer *head, bool is_group,  /**
989                                   union ccs_name_union *group)   * ccs_print_name_union - Print a ccs_name_union.
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(struct ccs_io_buffer *head,
997                                     const struct ccs_name_union *ptr)
998  {  {
999          const int pos = head->read_avail;          int pos = head->read_avail;
1000          if (pos && head->read_buf[pos - 1] == ' ')          if (pos && head->read_buf[pos - 1] == ' ')
1001                  head->read_avail--;                  head->read_avail--;
1002          if (is_group)          if (ptr->is_group)
1003                  return ccs_io_printf(head, " @%s", group->group->group_name->name);                  return ccs_io_printf(head, " @%s",
1004          return ccs_io_printf(head, " %s", group->filename->name);                                       ptr->group->group_name->name);
1005            return ccs_io_printf(head, " %s", ptr->filename->name);
1006  }  }
1007    
1008  static bool ccs_print_number_union(struct ccs_io_buffer *head, bool is_group,  /**
1009                                     union ccs_number_union *group)   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.
1010     *
1011     * @head: Pointer to "struct ccs_io_buffer".
1012     * @ptr:  Pointer to "struct ccs_name_union".
1013     *
1014     * Returns true on success, false otherwise.
1015     */
1016    static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1017                                            const struct ccs_name_union *ptr)
1018  {  {
1019          unsigned int min;          if (ptr->is_group)
1020          unsigned int max;                  return ccs_io_printf(head, "@%s",
1021          if (is_group)                                       ptr->group->group_name->name);
1022                  return ccs_io_printf(head, " @%s", group->group->group_name->name);          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1023          min = group->value.min;  }
1024          max = group->value.max;  
1025          if (min == max)  /**
1026                  return ccs_io_printf(head, " %u", min);   * ccs_print_number_union_common - Print a ccs_number_union.
1027          return ccs_io_printf(head, " %u-%u", min, max);   *
1028     * @head:       Pointer to "struct ccs_io_buffer".
1029     * @ptr:        Pointer to "struct ccs_number_union".
1030     * @need_space: True if a space character is needed.
1031     *
1032     * Returns true on success, false otherwise.
1033     */
1034    static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
1035                                              const struct ccs_number_union *ptr,
1036                                              const bool need_space)
1037    {
1038            unsigned long min;
1039            unsigned long max;
1040            u8 min_type;
1041            u8 max_type;
1042            if (need_space && !ccs_io_printf(head, " "))
1043                    return false;
1044            if (ptr->is_group)
1045                    return ccs_io_printf(head, "@%s",
1046                                         ptr->group->group_name->name);
1047            min_type = ptr->min_type;
1048            max_type = ptr->max_type;
1049            min = ptr->values[0];
1050            max = ptr->values[1];
1051            switch (min_type) {
1052            case CCS_VALUE_TYPE_HEXADECIMAL:
1053                    if (!ccs_io_printf(head, "0x%lX", min))
1054                            return false;
1055                    break;
1056            case CCS_VALUE_TYPE_OCTAL:
1057                    if (!ccs_io_printf(head, "0%lo", min))
1058                            return false;
1059                    break;
1060            default:
1061                    if (!ccs_io_printf(head, "%lu", min))
1062                            return false;
1063                    break;
1064            }
1065            if (min == max && min_type == max_type)
1066                    return true;
1067            switch (max_type) {
1068            case CCS_VALUE_TYPE_HEXADECIMAL:
1069                    return ccs_io_printf(head, "-0x%lX", max);
1070            case CCS_VALUE_TYPE_OCTAL:
1071                    return ccs_io_printf(head, "-0%lo", max);
1072            default:
1073                    return ccs_io_printf(head, "-%lu", max);
1074            }
1075  }  }
1076    
1077  /**  /**
1078   * ccs_print_single_path_acl - Print a single path ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1079     *
1080     * @head:       Pointer to "struct ccs_io_buffer".
1081     * @ptr:        Pointer to "struct ccs_number_union".
1082     *
1083     * Returns true on success, false otherwise.
1084     */
1085    bool ccs_print_number_union(struct ccs_io_buffer *head,
1086                                const struct ccs_number_union *ptr)
1087    {
1088            return ccs_print_number_union_common(head, ptr, true);
1089    }
1090    
1091    /**
1092     * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.
1093     *
1094     * @head:       Pointer to "struct ccs_io_buffer".
1095     * @ptr:        Pointer to "struct ccs_number_union".
1096     *
1097     * Returns true on success, false otherwise.
1098     */
1099    static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1100                                               const struct ccs_number_union *ptr)
1101    {
1102            return ccs_print_number_union_common(head, ptr, false);
1103    }
1104    
1105    /**
1106     * ccs_print_condition - Print condition part.
1107   *   *
1108   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to "struct ccs_single_path_acl_record".  
1109   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1110   *   *
1111   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1112   */   */
1113  static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1114                                        struct ccs_single_path_acl_record *ptr,                                  const struct ccs_condition *cond)
                                       const struct ccs_condition *cond)  
1115  {  {
1116          int pos;          const struct ccs_condition_element *condp;
1117          u8 bit;          const struct ccs_number_union *numbers_p;
1118          const u16 perm = ptr->perm;          const struct ccs_name_union *names_p;
1119          for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {          const struct ccs_argv_entry *argv;
1120                  const char *msg;          const struct ccs_envp_entry *envp;
1121                  if (!(perm & (1 << bit)))          u16 condc;
1122            u16 i;
1123            u16 j;
1124            char buffer[32];
1125            if (!cond)
1126                    goto no_condition;
1127            condc = cond->condc;
1128            condp = (const struct ccs_condition_element *) (cond + 1);
1129            numbers_p = (const struct ccs_number_union *) (condp + condc);
1130            names_p = (const struct ccs_name_union *)
1131                    (numbers_p + cond->numbers_count);
1132            argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);
1133            envp = (const struct ccs_envp_entry *) (argv + cond->argc);
1134            memset(buffer, 0, sizeof(buffer));
1135            if (condc && !ccs_io_printf(head, "%s", " if"))
1136                    goto out;
1137            for (i = 0; i < condc; i++) {
1138                    const u8 match = condp->equals;
1139                    const u8 left = condp->left;
1140                    const u8 right = condp->right;
1141                    condp++;
1142                    switch (left) {
1143                    case CCS_ARGV_ENTRY:
1144                            if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",
1145                                               argv->index, argv->is_not ?
1146                                               "!=" : "=", argv->value->name))
1147                                    goto out;
1148                            argv++;
1149                          continue;                          continue;
1150                  if (head->read_execute_only && bit != TYPE_EXECUTE_ACL)                  case CCS_ENVP_ENTRY:
1151                            if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",
1152                                               envp->name->name, envp->is_not ?
1153                                               "!=" : "="))
1154                                    goto out;
1155                            if (envp->value) {
1156                                    if (!ccs_io_printf(head, "\"%s\"",
1157                                                       envp->value->name))
1158                                            goto out;
1159                            } else {
1160                                    if (!ccs_io_printf(head, "NULL"))
1161                                            goto out;
1162                            }
1163                            envp++;
1164                          continue;                          continue;
1165                  /* Print "read/write" instead of "read" and "write". */                  case CCS_NUMBER_UNION:
1166                  if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)                          if (!ccs_print_number_union(head, numbers_p++))
1167                      && (perm & (1 << TYPE_READ_WRITE_ACL)))                                  goto out;
1168                            break;
1169                    default:
1170                            if (left >= CCS_MAX_CONDITION_KEYWORD)
1171                                    goto out;
1172                            if (!ccs_io_printf(head, " %s",
1173                                               ccs_condition_keyword[left]))
1174                                    goto out;
1175                            break;
1176                    }
1177                    if (!ccs_io_printf(head, "%s", match ? "=" : "!="))
1178                            goto out;
1179                    switch (right) {
1180                    case CCS_NAME_UNION:
1181                            if (!ccs_print_name_union_quoted(head, names_p++))
1182                                    goto out;
1183                            break;
1184                    case CCS_NUMBER_UNION:
1185                            if (!ccs_print_number_union_nospace(head, numbers_p++))
1186                                    goto out;
1187                            break;
1188                    default:
1189                            if (right >= CCS_MAX_CONDITION_KEYWORD)
1190                                    goto out;
1191                            if (!ccs_io_printf(head, "%s",
1192                                               ccs_condition_keyword[right]))
1193                                    goto out;
1194                            break;
1195                    }
1196            }
1197            i = cond->post_state[3];
1198            if (!i)
1199                    goto no_condition;
1200            if (!ccs_io_printf(head, " ; set"))
1201                    goto out;
1202            for (j = 0; j < 3; j++) {
1203                    if (!(i & (1 << j)))
1204                          continue;                          continue;
1205                  msg = ccs_sp2keyword(bit);                  if (!ccs_io_printf(head, " task.state[%u]=%u", j,
1206                  pos = head->read_avail;                                     cond->post_state[j]))
                 if (!ccs_io_printf(head, "allow_%s", msg) ||  
                     !ccs_print_name_union(head, ptr->name_is_group,  
                                           &ptr->name) ||  
                     !ccs_print_condition(head, cond))  
1207                          goto out;                          goto out;
1208          }          }
1209          head->read_bit = 0;   no_condition:
1210          return true;          if (ccs_io_printf(head, "\n"))
1211                    return true;
1212   out:   out:
         head->read_bit = bit;  
         head->read_avail = pos;  
1213          return false;          return false;
1214  }  }
1215    
1216  /**  /**
1217   * ccs_print_mkdev_acl - Print a mkdev ACL entry.   * ccs_print_path_acl - Print a path ACL entry.
1218   *   *
1219   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1220   * @ptr:  Pointer to "struct ccs_mkdev_acl_record".   * @ptr:  Pointer to "struct ccs_path_acl".
1221   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1222   *   *
1223   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1224   */   */
1225  static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1226                                  struct ccs_mkdev_acl_record *ptr,                                 struct ccs_path_acl *ptr,
1227                                  const struct ccs_condition *cond)                                 const struct ccs_condition *cond)
1228  {  {
1229          int pos;          int pos;
1230          u8 bit;          u8 bit;
1231          const u16 perm = ptr->perm;          const u16 perm = ptr->perm;
1232          for (bit = head->read_bit; bit < MAX_MKDEV_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
                 const char *msg;  
1233                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1234                          continue;                          continue;
1235                  msg = ccs_mkdev2keyword(bit);                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1236                        && bit != CCS_TYPE_TRANSIT)
1237                            continue;
1238                    /* Print "read/write" instead of "read" and "write". */
1239                    if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1240                        && (perm & (1 << CCS_TYPE_READ_WRITE)))
1241                            continue;
1242                  pos = head->read_avail;                  pos = head->read_avail;
1243                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||
1244                      !ccs_print_name_union(head, ptr->name_is_group,                      !ccs_print_name_union(head, &ptr->name) ||
1245                                            &ptr->name) ||                      !ccs_print_condition(head, cond)) {
1246                      !ccs_print_number_union(head, ptr->major_is_group,                          head->read_bit = bit;
1247                                              &ptr->major) ||                          head->read_avail = pos;
1248                      !ccs_print_number_union(head, ptr->minor_is_group,                          return false;
1249                                              &ptr->minor) ||                  }
                     !ccs_print_condition(head, cond))  
                         goto out;  
1250          }          }
1251          head->read_bit = 0;          head->read_bit = 0;
1252          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1253  }  }
1254    
1255  /**  /**
1256   * ccs_print_double_path_acl - Print a double path ACL entry.   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.
1257   *   *
1258   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1259   * @ptr:  Pointer to "struct ccs_double_path_acl_record".   * @ptr:  Pointer to "struct ccs_path_number3_acl".
1260   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1261   *   *
1262   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1263   */   */
1264  static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,
1265                                        struct ccs_double_path_acl_record *ptr,                                         struct ccs_path_number3_acl *ptr,
1266                                        const struct ccs_condition *cond)                                         const struct ccs_condition *cond)
1267  {  {
1268          int pos;          int pos;
1269          u8 bit;          u8 bit;
1270          const u8 perm = ptr->perm;          const u16 perm = ptr->perm;
1271          for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;
1272                  const char *msg;               bit++) {
1273                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1274                          continue;                          continue;
                 msg = ccs_dp2keyword(bit);  
1275                  pos = head->read_avail;                  pos = head->read_avail;
1276                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  if (!ccs_io_printf(head, "allow_%s",
1277                      !ccs_print_name_union(head, ptr->name1_is_group,                                     ccs_path_number32keyword(bit)) ||
1278                                            &ptr->name1) ||                      !ccs_print_name_union(head, &ptr->name) ||
1279                      !ccs_print_name_union(head, ptr->name2_is_group,                      !ccs_print_number_union(head, &ptr->mode) ||
1280                                            &ptr->name2) ||                      !ccs_print_number_union(head, &ptr->major) ||
1281                      !ccs_print_condition(head, cond))                      !ccs_print_number_union(head, &ptr->minor) ||
1282                          goto out;                      !ccs_print_condition(head, cond)) {
1283                            head->read_bit = bit;
1284                            head->read_avail = pos;
1285                            return false;
1286                    }
1287          }          }
1288          head->read_bit = 0;          head->read_bit = 0;
1289          return true;          return true;
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1290  }  }
1291    
1292  /**  /**
1293   * ccs_print_ioctl_acl - Print an ioctl ACL entry.   * ccs_print_path2_acl - Print a path2 ACL entry.
1294   *   *
1295   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1296   * @ptr:  Pointer to "struct ccs_ioctl_acl_record".   * @ptr:  Pointer to "struct ccs_path2_acl".
1297   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1298   *   *
1299   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1300   */   */
1301  static bool ccs_print_ioctl_acl(struct ccs_io_buffer *head,  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1302                                  struct ccs_ioctl_acl_record *ptr,                                  struct ccs_path2_acl *ptr,
1303                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1304  {  {
1305          int pos = head->read_avail;          int pos;
1306          if (!ccs_io_printf(head, KEYWORD_ALLOW_IOCTL) ||          u8 bit;
1307              !ccs_print_name_union(head, ptr->name_is_group, &ptr->name) ||          const u8 perm = ptr->perm;
1308              !ccs_print_number_union(head, ptr->cmd_is_group, &ptr->cmd) ||          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1309              !ccs_print_condition(head, cond))                  if (!(perm & (1 << bit)))
1310                  goto out;                          continue;
1311                    pos = head->read_avail;
1312                    if (!ccs_io_printf(head, "allow_%s",
1313                                       ccs_path22keyword(bit)) ||
1314                        !ccs_print_name_union(head, &ptr->name1) ||
1315                        !ccs_print_name_union(head, &ptr->name2) ||
1316                        !ccs_print_condition(head, cond)) {
1317                            head->read_bit = bit;
1318                            head->read_avail = pos;
1319                            return false;
1320                    }
1321            }
1322            head->read_bit = 0;
1323          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1324  }  }
1325    
1326  /**  /**
1327   * ccs_print_argv0_acl - Print an argv[0] ACL entry.   * ccs_print_path_number_acl - Print a path_number ACL entry.
1328   *   *
1329   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1330   * @ptr:  Pointer to "struct ccs_argv0_acl_record".   * @ptr:  Pointer to "struct ccs_path_number_acl".
1331   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1332   *   *
1333   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1334   */   */
1335  static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1336                                  struct ccs_argv0_acl_record *ptr,                                        struct ccs_path_number_acl *ptr,
1337                                  const struct ccs_condition *cond)                                        const struct ccs_condition *cond)
1338  {  {
1339          int pos = head->read_avail;          int pos;
1340          if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",          u8 bit;
1341                             ptr->filename->name, ptr->argv0->name))          const u8 perm = ptr->perm;
1342                  goto out;          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1343          if (!ccs_print_condition(head, cond))               bit++) {
1344                  goto out;                  if (!(perm & (1 << bit)))
1345                            continue;
1346                    pos = head->read_avail;
1347                    if (!ccs_io_printf(head, "allow_%s",
1348                                       ccs_path_number2keyword(bit)) ||
1349                        !ccs_print_name_union(head, &ptr->name) ||
1350                        !ccs_print_number_union(head, &ptr->number) ||
1351                        !ccs_print_condition(head, cond)) {
1352                            head->read_bit = bit;
1353                            head->read_avail = pos;
1354                            return false;
1355                    }
1356            }
1357            head->read_bit = 0;
1358          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1359  }  }
1360    
1361  /**  /**
1362   * ccs_print_env_acl - Print an evironment variable name's ACL entry.   * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1363   *   *
1364   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1365   * @ptr:  Pointer to "struct ccs_env_acl_record".   * @ptr:  Pointer to "struct ccs_env_acl".
1366   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1367   *   *
1368   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1369   */   */
1370  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1371                                struct ccs_env_acl_record *ptr,                                struct ccs_env_acl *ptr,
1372                                const struct ccs_condition *cond)                                const struct ccs_condition *cond)
1373  {  {
1374          int pos = head->read_avail;          const int pos = head->read_avail;
1375          if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1376                  goto out;              !ccs_print_condition(head, cond)) {
1377          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1378                  goto out;                  return false;
1379            }
1380          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1381  }  }
1382    
1383  /**  /**
1384   * ccs_print_capability_acl - Print a capability ACL entry.   * ccs_print_capability_acl - Print a capability ACL entry.
1385   *   *
1386   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1387   * @ptr:  Pointer to "struct ccs_capability_acl_record".   * @ptr:  Pointer to "struct ccs_capability_acl".
1388   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1389   *   *
1390   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1391   */   */
1392  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1393                                       struct ccs_capability_acl_record *ptr,                                       struct ccs_capability_acl *ptr,
1394                                       const struct ccs_condition *cond)                                       const struct ccs_condition *cond)
1395  {  {
1396          int pos = head->read_avail;          const int pos = head->read_avail;
1397          if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1398                             ccs_cap2keyword(ptr->operation)))                             ccs_cap2keyword(ptr->operation)) ||
1399                  goto out;              !ccs_print_condition(head, cond)) {
1400          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1401                  goto out;                  return false;
1402            }
1403          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1404  }  }
1405    
1406  /**  /**
1407   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.
1408   *   *
1409   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1410   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
1411   *   *
1412   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1413   */   */
1414  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,
1415                                   struct ccs_ip_network_acl_record *ptr)                                   struct ccs_ip_network_acl *ptr)
1416  {  {
1417          const u32 min_address = ptr->address.ipv4.min;          const u32 min_address = ptr->address.ipv4.min;
1418          const u32 max_address = ptr->address.ipv4.max;          const u32 max_address = ptr->address.ipv4.max;
# Line 906  static bool ccs_print_ipv4_entry(struct Line 1428  static bool ccs_print_ipv4_entry(struct
1428   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.
1429   *   *
1430   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1431   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
1432   *   *
1433   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1434   */   */
1435  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,
1436                                   struct ccs_ip_network_acl_record *ptr)                                   struct ccs_ip_network_acl *ptr)
1437  {  {
1438          char buf[64];          char buf[64];
1439          const struct in6_addr *min_address = ptr->address.ipv6.min;          const struct in6_addr *min_address = ptr->address.ipv6.min;
# Line 931  static bool ccs_print_ipv6_entry(struct Line 1453  static bool ccs_print_ipv6_entry(struct
1453   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_network_acl - Print a network ACL entry.
1454   *   *
1455   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1456   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
1457   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1458   *   *
1459   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1460   */   */
1461  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1462                                    struct ccs_ip_network_acl_record *ptr,                                    struct ccs_ip_network_acl *ptr,
1463                                    const struct ccs_condition *cond)                                    const struct ccs_condition *cond)
1464  {  {
1465          int pos = head->read_avail;          int pos;
1466          if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",          u8 bit;
1467                             ccs_net2keyword(ptr->operation_type)))          const u16 perm = ptr->perm;
1468                  goto out;          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1469          switch (ptr->record_type) {                  if (!(perm & (1 << bit)))
1470          case IP_RECORD_TYPE_ADDRESS_GROUP:                          continue;
1471                  if (!ccs_io_printf(head, "@%s",                  pos = head->read_avail;
1472                                     ptr->address.group->group_name->name))                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",
1473                          goto out;                                     ccs_net2keyword(bit)))
                 break;  
         case IP_RECORD_TYPE_IPv4:  
                 if (!ccs_print_ipv4_entry(head, ptr))  
1474                          goto out;                          goto out;
1475                  break;                  switch (ptr->address_type) {
1476          case IP_RECORD_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1477                  if (!ccs_print_ipv6_entry(head, ptr))                          if (!ccs_io_printf(head, "@%s", ptr->address.group->
1478                                               group_name->name))
1479                                    goto out;
1480                            break;
1481                    case CCS_IP_ADDRESS_TYPE_IPv4:
1482                            if (!ccs_print_ipv4_entry(head, ptr))
1483                                    goto out;
1484                            break;
1485                    case CCS_IP_ADDRESS_TYPE_IPv6:
1486                            if (!ccs_print_ipv6_entry(head, ptr))
1487                                    goto out;
1488                            break;
1489                    }
1490                    if (!ccs_print_number_union(head, &ptr->port) ||
1491                        !ccs_print_condition(head, cond))
1492                          goto out;                          goto out;
                 break;  
1493          }          }
1494          if (!ccs_print_number_union(head, ptr->port_is_group, &ptr->port) ||          head->read_bit = 0;
             !ccs_print_condition(head, cond))  
                 goto out;  
1495          return true;          return true;
1496   out:   out:
1497            head->read_bit = bit;
1498          head->read_avail = pos;          head->read_avail = pos;
1499          return false;          return false;
1500  }  }
# Line 972  static bool ccs_print_network_acl(struct Line 1503  static bool ccs_print_network_acl(struct
1503   * ccs_print_signal_acl - Print a signal ACL entry.   * ccs_print_signal_acl - Print a signal ACL entry.
1504   *   *
1505   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1506   * @ptr:  Pointer to "struct signale_acl_record".   * @ptr:  Pointer to "struct signale_acl".
1507   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1508   *   *
1509   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1510   */   */
1511  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1512                                   struct ccs_signal_acl_record *ptr,                                   struct ccs_signal_acl *ptr,
1513                                   const struct ccs_condition *cond)                                   const struct ccs_condition *cond)
1514  {  {
1515          int pos = head->read_avail;          const int pos = head->read_avail;
1516          if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1517                             ptr->sig, ptr->domainname->name))                             ptr->sig, ptr->domainname->name) ||
1518                  goto out;              !ccs_print_condition(head, cond)) {
1519          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1520                  goto out;                  return false;
1521            }
1522          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1523  }  }
1524    
1525  /**  /**
# Line 1014  static bool ccs_print_execute_handler_re Line 1543  static bool ccs_print_execute_handler_re
1543   * ccs_print_mount_acl - Print a mount ACL entry.   * ccs_print_mount_acl - Print a mount ACL entry.
1544   *   *
1545   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1546   * @ptr:  Pointer to "struct ccs_mount_acl_record".   * @ptr:  Pointer to "struct ccs_mount_acl".
1547   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". May be NULL.
1548   *   *
1549   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1550   */   */
1551  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1552                                  struct ccs_mount_acl_record *ptr,                                  struct ccs_mount_acl *ptr,
1553                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1554  {  {
1555          int pos = head->read_avail;          const int pos = head->read_avail;
1556          if (!ccs_io_printf(head, KEYWORD_ALLOW_MOUNT "%s %s %s 0x%lX\n",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1557                             ptr->dev_name->name, ptr->dir_name->name,              !ccs_print_name_union(head, &ptr->dev_name) ||
1558                             ptr->fs_type->name, ptr->flags))              !ccs_print_name_union(head, &ptr->dir_name) ||
1559                  goto out;              !ccs_print_name_union(head, &ptr->fs_type) ||
1560          if (!ccs_print_condition(head, cond))              !ccs_print_number_union(head, &ptr->flags) ||
1561                  goto out;              !ccs_print_condition(head, cond)) {
1562          return true;                  head->read_avail = pos;
1563   out:                  return false;
1564          head->read_avail = pos;          }
         return false;  
 }  
   
 /**  
  * ccs_print_umount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_umount_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_umount_acl(struct ccs_io_buffer *head,  
                                  struct ccs_umount_acl_record *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_UNMOUNT "%s\n",  
                            ptr->dir->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_chroot_acl - Print a chroot ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_chroot_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,  
                                  struct ccs_chroot_acl_record *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_CHROOT "%s\n",  
                            ptr->dir->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_pivot_root_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  
                                      struct ccs_pivot_root_acl_record *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_PIVOT_ROOT "%s %s\n",  
                            ptr->new_root->name, ptr->old_root->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
1565          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1566  }  }
1567    
1568  /**  /**
# Line 1123  static bool ccs_print_entry(struct ccs_i Line 1577  static bool ccs_print_entry(struct ccs_i
1577                              struct ccs_acl_info *ptr)                              struct ccs_acl_info *ptr)
1578  {  {
1579          const struct ccs_condition *cond = ptr->cond;          const struct ccs_condition *cond = ptr->cond;
1580          const u8 acl_type = ccs_acl_type2(ptr);          const u8 acl_type = ptr->type;
1581          if (acl_type & ACL_DELETED)          if (ptr->is_deleted)
1582                  return true;                  return true;
1583          if (acl_type == TYPE_SINGLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH_ACL) {
1584                  struct ccs_single_path_acl_record *acl                  struct ccs_path_acl *acl
1585                          = container_of(ptr, struct ccs_single_path_acl_record,                          = container_of(ptr, struct ccs_path_acl, head);
1586                                         head);                  return ccs_print_path_acl(head, acl, cond);
                 return ccs_print_single_path_acl(head, acl, cond);  
1587          }          }
1588          if (acl_type == TYPE_EXECUTE_HANDLER) {          if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {
1589                  struct ccs_execute_handler_record *acl                  struct ccs_execute_handler_record *acl
1590                          = container_of(ptr, struct ccs_execute_handler_record,                          = container_of(ptr, struct ccs_execute_handler_record,
1591                                         head);                                         head);
1592                  const char *keyword = KEYWORD_EXECUTE_HANDLER;                  const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;
1593                  return ccs_print_execute_handler_record(head, keyword, acl);                  return ccs_print_execute_handler_record(head, keyword, acl);
1594          }          }
1595          if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {          if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1596                  struct ccs_execute_handler_record *acl                  struct ccs_execute_handler_record *acl
1597                          = container_of(ptr, struct ccs_execute_handler_record,                          = container_of(ptr, struct ccs_execute_handler_record,
1598                                         head);                                         head);
1599                  const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;                  const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1600                  return ccs_print_execute_handler_record(head, keyword, acl);                  return ccs_print_execute_handler_record(head, keyword, acl);
1601          }          }
1602          if (head->read_execute_only)          if (head->read_execute_only)
1603                  return true;                  return true;
1604          if (acl_type == TYPE_MKDEV_ACL) {          if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {
1605                  struct ccs_mkdev_acl_record *acl                  struct ccs_path_number3_acl *acl
1606                          = container_of(ptr, struct ccs_mkdev_acl_record, head);                          = container_of(ptr, struct ccs_path_number3_acl, head);
1607                  return ccs_print_mkdev_acl(head, acl, cond);                  return ccs_print_path_number3_acl(head, acl, cond);
1608          }          }
1609          if (acl_type == TYPE_DOUBLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH2_ACL) {
1610                  struct ccs_double_path_acl_record *acl                  struct ccs_path2_acl *acl
1611                          = container_of(ptr, struct ccs_double_path_acl_record,                          = container_of(ptr, struct ccs_path2_acl, head);
1612                                         head);                  return ccs_print_path2_acl(head, acl, cond);
1613                  return ccs_print_double_path_acl(head, acl, cond);          }
1614          }          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1615          if (acl_type == TYPE_IOCTL_ACL) {                  struct ccs_path_number_acl *acl
1616                  struct ccs_ioctl_acl_record *acl                          = container_of(ptr, struct ccs_path_number_acl, head);
1617                          = container_of(ptr, struct ccs_ioctl_acl_record, head);                  return ccs_print_path_number_acl(head, acl, cond);
1618                  return ccs_print_ioctl_acl(head, acl, cond);          }
1619          }          if (acl_type == CCS_TYPE_ENV_ACL) {
1620          if (acl_type == TYPE_ARGV0_ACL) {                  struct ccs_env_acl *acl
1621                  struct ccs_argv0_acl_record *acl                          = container_of(ptr, struct ccs_env_acl, head);
                         = container_of(ptr, struct ccs_argv0_acl_record, head);  
                 return ccs_print_argv0_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_ENV_ACL) {  
                 struct ccs_env_acl_record *acl  
                         = container_of(ptr, struct ccs_env_acl_record, head);  
1622                  return ccs_print_env_acl(head, acl, cond);                  return ccs_print_env_acl(head, acl, cond);
1623          }          }
1624          if (acl_type == TYPE_CAPABILITY_ACL) {          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1625                  struct ccs_capability_acl_record *acl                  struct ccs_capability_acl *acl
1626                          = container_of(ptr, struct ccs_capability_acl_record,                          = container_of(ptr, struct ccs_capability_acl, head);
                                        head);  
1627                  return ccs_print_capability_acl(head, acl, cond);                  return ccs_print_capability_acl(head, acl, cond);
1628          }          }
1629          if (acl_type == TYPE_IP_NETWORK_ACL) {          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1630                  struct ccs_ip_network_acl_record *acl                  struct ccs_ip_network_acl *acl
1631                          = container_of(ptr, struct ccs_ip_network_acl_record,                          = container_of(ptr, struct ccs_ip_network_acl, head);
                                        head);  
1632                  return ccs_print_network_acl(head, acl, cond);                  return ccs_print_network_acl(head, acl, cond);
1633          }          }
1634          if (acl_type == TYPE_SIGNAL_ACL) {          if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1635                  struct ccs_signal_acl_record *acl                  struct ccs_signal_acl *acl
1636                          = container_of(ptr, struct ccs_signal_acl_record, head);                          = container_of(ptr, struct ccs_signal_acl, head);
1637                  return ccs_print_signal_acl(head, acl, cond);                  return ccs_print_signal_acl(head, acl, cond);
1638          }          }
1639          if (acl_type == TYPE_MOUNT_ACL) {          if (acl_type == CCS_TYPE_MOUNT_ACL) {
1640                  struct ccs_mount_acl_record *acl                  struct ccs_mount_acl *acl
1641                          = container_of(ptr, struct ccs_mount_acl_record, head);                          = container_of(ptr, struct ccs_mount_acl, head);
1642                  return ccs_print_mount_acl(head, acl, cond);                  return ccs_print_mount_acl(head, acl, cond);
1643          }          }
         if (acl_type == TYPE_UMOUNT_ACL) {  
                 struct ccs_umount_acl_record *acl  
                         = container_of(ptr, struct ccs_umount_acl_record, head);  
                 return ccs_print_umount_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_CHROOT_ACL) {  
                 struct ccs_chroot_acl_record *acl  
                         = container_of(ptr, struct ccs_chroot_acl_record, head);  
                 return ccs_print_chroot_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_PIVOT_ROOT_ACL) {  
                 struct ccs_pivot_root_acl_record *acl  
                         = container_of(ptr, struct ccs_pivot_root_acl_record,  
                                        head);  
                 return ccs_print_pivot_root_acl(head, acl, cond);  
         }  
         /* Workaround for gcc 3.2.2's inline bug. */  
         if (acl_type & ACL_DELETED)  
                 return true;  
1644          BUG(); /* This must not happen. */          BUG(); /* This must not happen. */
1645          return false;          return false;
1646  }  }
# Line 1224  static bool ccs_print_entry(struct ccs_i Line 1650  static bool ccs_print_entry(struct ccs_i
1650   *   *
1651   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1652   *   *
  * Returns 0.  
  *  
1653   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1654   */   */
1655  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain_policy(struct ccs_io_buffer *head)
1656  {  {
1657          struct list_head *dpos;          struct list_head *dpos;
1658          struct list_head *apos;          struct list_head *apos;
         ccs_check_read_lock();  
1659          if (head->read_eof)          if (head->read_eof)
1660                  return 0;                  return;
1661          if (head->read_step == 0)          if (head->read_step == 0)
1662                  head->read_step = 1;                  head->read_step = 1;
1663          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
# Line 1250  static int ccs_read_domain_policy(struct Line 1673  static int ccs_read_domain_policy(struct
1673                          continue;                          continue;
1674                  /* Print domainname and flags. */                  /* Print domainname and flags. */
1675                  if (domain->quota_warned)                  if (domain->quota_warned)
1676                          quota_exceeded = "quota_exceeded\n";                          quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";
1677                  if (domain->domain_transition_failed)                  if (domain->domain_transition_failed)
1678                          transition_failed = "transition_failed\n";                          transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";
1679                  if (domain->ignore_global_allow_read)                  if (domain->ignore_global_allow_read)
1680                          ignore_global_allow_read                          ignore_global_allow_read
1681                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
1682                  if (domain->ignore_global_allow_env)                  if (domain->ignore_global_allow_env)
1683                          ignore_global_allow_env                          ignore_global_allow_env
1684                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
1685                  if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"
1686                                     "%s%s%s%s\n", domain->domainname->name,                                     "%s%s%s%s\n", domain->domainname->name,
1687                                     domain->profile, quota_exceeded,                                     domain->profile, quota_exceeded,
1688                                     transition_failed,                                     transition_failed,
1689                                     ignore_global_allow_read,                                     ignore_global_allow_read,
1690                                     ignore_global_allow_env))                                     ignore_global_allow_env))
1691                          return 0;                          return;
1692                  head->read_step = 2;                  head->read_step = 2;
1693   acl_loop:   acl_loop:
1694                  if (head->read_step == 3)                  if (head->read_step == 3)
# Line 1276  static int ccs_read_domain_policy(struct Line 1699  static int ccs_read_domain_policy(struct
1699                          struct ccs_acl_info *ptr                          struct ccs_acl_info *ptr
1700                                  = list_entry(apos, struct ccs_acl_info, list);                                  = list_entry(apos, struct ccs_acl_info, list);
1701                          if (!ccs_print_entry(head, ptr))                          if (!ccs_print_entry(head, ptr))
1702                                  return 0;                                  return;
1703                  }                  }
1704                  head->read_step = 3;                  head->read_step = 3;
1705   tail_mark:   tail_mark:
1706                  if (!ccs_io_printf(head, "\n"))                  if (!ccs_io_printf(head, "\n"))
1707                          return 0;                          return;
1708                  head->read_step = 1;                  head->read_step = 1;
1709                  if (head->read_single_domain)                  if (head->read_single_domain)
1710                          break;                          break;
1711          }          }
1712          head->read_eof = true;          head->read_eof = true;
         return 0;  
1713  }  }
1714    
1715  /**  /**
# Line 1300  static int ccs_read_domain_policy(struct Line 1722  static int ccs_read_domain_policy(struct
1722   * This is equivalent to doing   * This is equivalent to doing
1723   *   *
1724   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1725   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1726   *   *
1727   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1728   */   */
# Line 1310  static int ccs_write_domain_profile(stru Line 1732  static int ccs_write_domain_profile(stru
1732          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1733          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1734          unsigned int profile;          unsigned int profile;
         ccs_check_read_lock();  
1735          if (!cp)          if (!cp)
1736                  return -EINVAL;                  return -EINVAL;
1737          *cp = '\0';          *cp = '\0';
1738          profile = simple_strtoul(data, NULL, 10);          profile = simple_strtoul(data, NULL, 10);
1739          if (profile >= MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1740                  return -EINVAL;                  return -EINVAL;
1741          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1742          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1743                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1744          return 0;          return 0;
1745  }  }
# Line 1328  static int ccs_write_domain_profile(stru Line 1749  static int ccs_write_domain_profile(stru
1749   *   *
1750   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1751   *   *
  * Returns list of profile number and domainname pairs.  
  *  
1752   * This is equivalent to doing   * This is equivalent to doing
1753   *   *
1754   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1339  static int ccs_write_domain_profile(stru Line 1758  static int ccs_write_domain_profile(stru
1758   *   *
1759   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1760   */   */
1761  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1762  {  {
1763          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
1764          if (head->read_eof)          if (head->read_eof)
1765                  return 0;                  return;
1766          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1767                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain;
1768                  domain = list_entry(pos, struct ccs_domain_info, list);                  domain = list_entry(pos, struct ccs_domain_info, list);
# Line 1352  static int ccs_read_domain_profile(struc Line 1770  static int ccs_read_domain_profile(struc
1770                          continue;                          continue;
1771                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1772                                     domain->domainname->name))                                     domain->domainname->name))
1773                          return 0;                          return;
1774          }          }
1775          head->read_eof = true;          head->read_eof = true;
         return 0;  
1776  }  }
1777    
1778  /**  /**
# Line 1382  static int ccs_write_pid(struct ccs_io_b Line 1799  static int ccs_write_pid(struct ccs_io_b
1799   *   *
1800   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1801   */   */
1802  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1803  {  {
1804          char *buf = head->write_buf;          char *buf = head->write_buf;
1805          bool task_info = false;          bool task_info = false;
1806            bool global_pid = false;
1807          unsigned int pid;          unsigned int pid;
1808          struct task_struct *p;          struct task_struct *p;
1809          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1810          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_check_read_lock();  
1811          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1812          if (!buf)          if (!buf) {
1813                  goto done; /* Do nothing if open(O_RDONLY). */                  head->read_eof = true;
1814                    return; /* Do nothing if open(O_RDONLY). */
1815            }
1816          if (head->read_avail || head->read_eof)          if (head->read_avail || head->read_eof)
1817                  goto done;                  return;
1818          head->read_eof = true;          head->read_eof = true;
1819          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1820                  task_info = true;                  task_info = true;
1821            if (ccs_str_starts(&buf, "global-pid "))
1822                    global_pid = true;
1823          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1824          /***** CRITICAL SECTION START *****/          ccs_tasklist_lock();
1825          read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1826            if (global_pid)
1827                    p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1828            else
1829                    p = ccsecurity_exports.find_task_by_vpid(pid);
1830    #else
1831          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1832    #endif
1833          if (p) {          if (p) {
1834                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1835                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1836          }          }
1837          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
         /***** CRITICAL SECTION END *****/  
1838          if (!domain)          if (!domain)
1839                  goto done;                  return;
1840          if (!task_info)          if (!task_info)
1841                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1842                                domain->domainname->name);                                domain->domainname->name);
1843          else          else
1844                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1845                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1846                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                ccs_yesno(ccs_flags &
1847                                "yes" : "no",                                          CCS_TASK_IS_POLICY_MANAGER),
1848                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                ccs_yesno(ccs_flags &
1849                                "yes" : "no",                                          CCS_TASK_IS_EXECUTE_HANDLER),
1850                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1851                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1852                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
  done:  
         return 0;  
1853  }  }
1854    
1855  /**  /**
# Line 1438  static int ccs_read_pid(struct ccs_io_bu Line 1862  static int ccs_read_pid(struct ccs_io_bu
1862  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception_policy(struct ccs_io_buffer *head)
1863  {  {
1864          char *data = head->write_buf;          char *data = head->write_buf;
1865          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1866          if (ccs_str_starts(&data, KEYWORD_KEEP_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))
1867                  return ccs_write_domain_keeper_policy(data, false, is_delete);                  return ccs_write_domain_keeper_policy(data, false, is_delete);
1868          if (ccs_str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))
1869                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  return ccs_write_domain_keeper_policy(data, true, is_delete);
1870          if (ccs_str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))
1871                  return ccs_write_domain_initializer_policy(data, false,                  return ccs_write_domain_initializer_policy(data, false,
1872                                                             is_delete);                                                             is_delete);
1873          if (ccs_str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))
1874                  return ccs_write_domain_initializer_policy(data, true,                  return ccs_write_domain_initializer_policy(data, true,
1875                                                             is_delete);                                                             is_delete);
1876          if (ccs_str_starts(&data, KEYWORD_AGGREGATOR))          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))
1877                  return ccs_write_aggregator_policy(data, is_delete);                  return ccs_write_aggregator_policy(data, is_delete);
1878          if (ccs_str_starts(&data, KEYWORD_ALLOW_READ))          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))
1879                  return ccs_write_globally_readable_policy(data, is_delete);                  return ccs_write_globally_readable_policy(data, is_delete);
1880          if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))
1881                  return ccs_write_globally_usable_env_policy(data, is_delete);                  return ccs_write_globally_usable_env_policy(data, is_delete);
1882          if (ccs_str_starts(&data, KEYWORD_FILE_PATTERN))          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))
1883                  return ccs_write_pattern_policy(data, is_delete);                  return ccs_write_pattern_policy(data, is_delete);
1884          if (ccs_str_starts(&data, KEYWORD_PATH_GROUP))          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))
1885                  return ccs_write_path_group_policy(data, is_delete);                  return ccs_write_path_group_policy(data, is_delete);
1886          if (ccs_str_starts(&data, KEYWORD_NUMBER_GROUP))          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))
1887                  return ccs_write_number_group_policy(data, is_delete);                  return ccs_write_number_group_policy(data, is_delete);
1888          if (ccs_str_starts(&data, KEYWORD_DENY_REWRITE))          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))
1889                  return ccs_write_no_rewrite_policy(data, is_delete);                  return ccs_write_no_rewrite_policy(data, is_delete);
1890          if (ccs_str_starts(&data, KEYWORD_ADDRESS_GROUP))          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))
1891                  return ccs_write_address_group_policy(data, is_delete);                  return ccs_write_address_group_policy(data, is_delete);
1892          if (ccs_str_starts(&data, KEYWORD_DENY_AUTOBIND))          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))
1893                  return ccs_write_reserved_port_policy(data, is_delete);                  return ccs_write_reserved_port_policy(data, is_delete);
1894          return -EINVAL;          return -EINVAL;
1895  }  }
# Line 1475  static int ccs_write_exception_policy(st Line 1899  static int ccs_write_exception_policy(st
1899   *   *
1900   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1901   *   *
  * Returns 0 on success, -EINVAL otherwise.  
  *  
1902   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1903   */   */
1904  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static void ccs_read_exception_policy(struct ccs_io_buffer *head)
1905  {  {
1906          ccs_check_read_lock();          if (head->read_eof)
1907          if (!head->read_eof) {                  return;
1908                  switch (head->read_step) {          switch (head->read_step) {
1909                  case 0:          case 0:
1910                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1911                          head->read_step = 1;                  head->read_step = 1;
1912                  case 1:          case 1:
1913                          if (!ccs_read_domain_keeper_policy(head))                  if (!ccs_read_domain_keeper_policy(head))
1914                                  break;                          break;
1915                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1916                          head->read_step = 2;                  head->read_step = 2;
1917                  case 2:          case 2:
1918                          if (!ccs_read_globally_readable_policy(head))                  if (!ccs_read_globally_readable_policy(head))
1919                                  break;                          break;
1920                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1921                          head->read_step = 3;                  head->read_step = 3;
1922                  case 3:          case 3:
1923                          if (!ccs_read_globally_usable_env_policy(head))                  if (!ccs_read_globally_usable_env_policy(head))
1924                                  break;                          break;
1925                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1926                          head->read_step = 4;                  head->read_step = 4;
1927                  case 4:          case 4:
1928                          if (!ccs_read_domain_initializer_policy(head))                  if (!ccs_read_domain_initializer_policy(head))
1929                                  break;                          break;
1930                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1931                          head->read_step = 6;                  head->read_step = 6;
1932                  case 6:          case 6:
1933                          if (!ccs_read_aggregator_policy(head))                  if (!ccs_read_aggregator_policy(head))
1934                                  break;                          break;
1935                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1936                          head->read_step = 7;                  head->read_step = 7;
1937                  case 7:          case 7:
1938                          if (!ccs_read_file_pattern(head))                  if (!ccs_read_file_pattern(head))
1939                                  break;                          break;
1940                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1941                          head->read_step = 8;                  head->read_step = 8;
1942                  case 8:          case 8:
1943                          if (!ccs_read_no_rewrite_policy(head))                  if (!ccs_read_no_rewrite_policy(head))
1944                                  break;                          break;
1945                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1946                          head->read_step = 9;                  head->read_step = 9;
1947                  case 9:          case 9:
1948                          if (!ccs_read_path_group_policy(head))                  if (!ccs_read_path_group_policy(head))
1949                                  break;                          break;
1950                          head->read_var1 = NULL;                  head->read_var1 = NULL;
1951                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1952                          head->read_step = 10;                  head->read_step = 10;
1953                  case 10:          case 10:
1954                          if (!ccs_read_number_group_policy(head))                  if (!ccs_read_number_group_policy(head))
1955                                  break;                          break;
1956                          head->read_var1 = NULL;                  head->read_var1 = NULL;
1957                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1958                          head->read_step = 11;                  head->read_step = 11;
1959                  case 11:          case 11:
1960                          if (!ccs_read_address_group_policy(head))                  if (!ccs_read_address_group_policy(head))
1961                                  break;                          break;
1962                          head->read_var2 = NULL;                  head->read_var2 = NULL;
1963                          head->read_step = 12;                  head->read_step = 12;
1964                  case 12:          case 12:
1965                          if (!ccs_read_reserved_port_policy(head))                  if (!ccs_read_reserved_port_policy(head))
1966                            break;
1967                    head->read_eof = true;
1968            }
1969    }
1970    
1971    /**
1972     * ccs_get_argv0 - Get argv[0].
1973     *
1974     * @ee: Pointer to "struct ccs_execve_entry".
1975     *
1976     * Returns true on success, false otherwise.
1977     */
1978    static bool ccs_get_argv0(struct ccs_execve_entry *ee)
1979    {
1980            struct linux_binprm *bprm = ee->bprm;
1981            char *arg_ptr = ee->tmp;
1982            int arg_len = 0;
1983            unsigned long pos = bprm->p;
1984            int offset = pos % PAGE_SIZE;
1985            bool done = false;
1986            if (!bprm->argc)
1987                    goto out;
1988            while (1) {
1989                    if (!ccs_dump_page(bprm, pos, &ee->dump))
1990                            goto out;
1991                    pos += PAGE_SIZE - offset;
1992                    /* Read. */
1993                    while (offset < PAGE_SIZE) {
1994                            const char *kaddr = ee->dump.data;
1995                            const unsigned char c = kaddr[offset++];
1996                            if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
1997                                    if (c == '\\') {
1998                                            arg_ptr[arg_len++] = '\\';
1999                                            arg_ptr[arg_len++] = '\\';
2000                                    } else if (c > ' ' && c < 127) {
2001                                            arg_ptr[arg_len++] = c;
2002                                    } else {
2003                                            arg_ptr[arg_len++] = '\\';
2004                                            arg_ptr[arg_len++] = (c >> 6) + '0';
2005                                            arg_ptr[arg_len++]
2006                                                    = ((c >> 3) & 7) + '0';
2007                                            arg_ptr[arg_len++] = (c & 7) + '0';
2008                                    }
2009                            } else {
2010                                    arg_ptr[arg_len] = '\0';
2011                                    done = true;
2012                                  break;                                  break;
2013                          head->read_eof = true;                          }
2014                    }
2015                    offset = 0;
2016                    if (done)
2017                          break;                          break;
2018                  default:          }
2019                          return -EINVAL;          return true;
2020     out:
2021            return false;
2022    }
2023    
2024    /**
2025     * ccs_get_execute_condition - Get condition part for execute requests.
2026     *
2027     * @ee: Pointer to "struct ccs_execve_entry".
2028     *
2029     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2030     */
2031    static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry
2032                                                           *ee)
2033    {
2034            struct ccs_condition *cond;
2035            char *buf;
2036            int len = 256;
2037            char *realpath = NULL;
2038            char *argv0 = NULL;
2039            const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);
2040            if (profile->learning->learning_exec_realpath) {
2041                    struct file *file = ee->bprm->file;
2042    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2043                    struct path path = { file->f_vfsmnt, file->f_dentry };
2044                    realpath = ccs_realpath_from_path(&path);
2045    #else
2046                    realpath = ccs_realpath_from_path(&file->f_path);
2047    #endif
2048                    if (realpath)
2049                            len += strlen(realpath) + 17;
2050            }
2051            if (profile->learning->learning_exec_argv0) {
2052                    if (ccs_get_argv0(ee)) {
2053                            argv0 = ee->tmp;
2054                            len += strlen(argv0) + 16;
2055                  }                  }
2056          }          }
2057          return 0;          buf = kmalloc(len, CCS_GFP_FLAGS);
2058            if (!buf) {
2059                    kfree(realpath);
2060                    return NULL;
2061            }
2062            snprintf(buf, len - 1, "if");
2063            if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2064                    const int pos = strlen(buf);
2065                    snprintf(buf + pos, len - pos - 1,
2066                             " task.type=execute_handler");
2067            }
2068            if (realpath) {
2069                    const int pos = strlen(buf);
2070                    snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",
2071                             realpath);
2072                    kfree(realpath);
2073            }
2074            if (argv0) {
2075                    const int pos = strlen(buf);
2076                    snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",
2077                             argv0);
2078            }
2079            cond = ccs_get_condition(buf);
2080            kfree(buf);
2081            return cond;
2082    }
2083    
2084    /**
2085     * ccs_get_symlink_condition - Get condition part for symlink requests.
2086     *
2087     * @r: Pointer to "struct ccs_request_info".
2088     *
2089     * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2090     */
2091    static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info
2092                                                           *r)
2093    {
2094            struct ccs_condition *cond;
2095            char *buf;
2096            int len = 256;
2097            const char *symlink = NULL;
2098            const struct ccs_profile *profile = ccs_profile(r->profile);
2099            if (profile->learning->learning_symlink_target) {
2100                    symlink = r->obj->symlink_target->name;
2101                    len += strlen(symlink) + 18;
2102            }
2103            buf = kmalloc(len, CCS_GFP_FLAGS);
2104            if (!buf)
2105                    return NULL;
2106            snprintf(buf, len - 1, "if");
2107            if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2108                    const int pos = strlen(buf);
2109                    snprintf(buf + pos, len - pos - 1,
2110                             " task.type=execute_handler");
2111            }
2112            if (symlink) {
2113                    const int pos = strlen(buf);
2114                    snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",
2115                             symlink);
2116            }
2117            cond = ccs_get_condition(buf);
2118            kfree(buf);
2119            return cond;
2120  }  }
2121    
2122  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 1574  static LIST_HEAD(ccs_query_list); Line 2142  static LIST_HEAD(ccs_query_list);
2142  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2143    
2144  /**  /**
2145   * ccs_check_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
2146   *   *
2147   * @r:       Pointer to "struct ccs_request_info".   * @r:       Pointer to "struct ccs_request_info".
2148   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt:     The printf()'s format string, followed by parameters.
2149   *   *
2150   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
2151   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2152   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
2153   * -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2154   */   */
2155  int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2156  {  {
2157          va_list args;          va_list args;
2158          int error = -EPERM;          int error = -EPERM;
# Line 1596  int ccs_check_supervisor(struct ccs_requ Line 2164  int ccs_check_supervisor(struct ccs_requ
2164          char *header;          char *header;
2165          if (!r->domain)          if (!r->domain)
2166                  r->domain = ccs_current_domain();                  r->domain = ccs_current_domain();
2167            switch (r->mode) {
2168                    char *buffer;
2169                    struct ccs_condition *cond;
2170            case CCS_CONFIG_LEARNING:
2171                    if (!ccs_domain_quota_ok(r))
2172                            return 0;
2173                    va_start(args, fmt);
2174                    len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
2175                    va_end(args);
2176                    buffer = kmalloc(len, CCS_GFP_FLAGS);
2177                    if (!buffer)
2178                            return 0;
2179                    va_start(args, fmt);
2180                    vsnprintf(buffer, len - 1, fmt, args);
2181                    va_end(args);
2182                    ccs_normalize_line(buffer);
2183                    if (r->ee && !strncmp(buffer, "allow_execute ", 14))
2184                            cond = ccs_get_execute_condition(r->ee);
2185                    else if (r->obj && r->obj->symlink_target)
2186                            cond = ccs_get_symlink_condition(r);
2187                    else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
2188                            char str[] = "if task.type=execute_handler";
2189                            cond = ccs_get_condition(str);
2190                    } else
2191                            cond = NULL;
2192                    ccs_write_domain_policy2(buffer, r->domain, cond, false);
2193                    ccs_put_condition(cond);
2194                    kfree(buffer);
2195                    /* fall through */
2196            case CCS_CONFIG_PERMISSIVE:
2197                    return 0;
2198            }
2199          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2200                  int i;                  int i;
2201                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2202                          return -EPERM;                          return -EPERM;
2203                  for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->
2204                       i++) {                               enforcing_penalty; i++) {
2205                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2206                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2207                  }                  }
# Line 1613  int ccs_check_supervisor(struct ccs_requ Line 2213  int ccs_check_supervisor(struct ccs_requ
2213          header = ccs_init_audit_log(&len, r);          header = ccs_init_audit_log(&len, r);
2214          if (!header)          if (!header)
2215                  goto out;                  goto out;
2216          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2217          if (!ccs_query_entry)          if (!ccs_query_entry)
2218                  goto out;                  goto out;
2219          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          len = ccs_round2(len);
2220            ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2221          if (!ccs_query_entry->query)          if (!ccs_query_entry->query)
2222                  goto out;                  goto out;
2223          INIT_LIST_HEAD(&ccs_query_entry->list);          INIT_LIST_HEAD(&ccs_query_entry->list);
         /***** CRITICAL SECTION START *****/  
2224          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2225          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2226              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
# Line 1630  int ccs_check_supervisor(struct ccs_requ Line 2230  int ccs_check_supervisor(struct ccs_requ
2230                  ccs_query_entry->serial = ccs_serial++;                  ccs_query_entry->serial = ccs_serial++;
2231          }          }
2232          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2233          if (quota_exceeded)          if (quota_exceeded)
2234                  goto out;                  goto out;
2235          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 1641  int ccs_check_supervisor(struct ccs_requ Line 2240  int ccs_check_supervisor(struct ccs_requ
2240          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);
2241          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
2242          va_end(args);          va_end(args);
         /***** CRITICAL SECTION START *****/  
2243          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2244          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2245          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2246          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2247          for (ccs_query_entry->timer = 0;          for (ccs_query_entry->timer = 0;
2248               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;
# Line 1656  int ccs_check_supervisor(struct ccs_requ Line 2253  int ccs_check_supervisor(struct ccs_requ
2253                  if (ccs_query_entry->answer)                  if (ccs_query_entry->answer)
2254                          break;                          break;
2255          }          }
         /***** CRITICAL SECTION START *****/  
2256          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2257          list_del(&ccs_query_entry->list);          list_del(&ccs_query_entry->list);
2258          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
2259          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2260          switch (ccs_query_entry->answer) {          switch (ccs_query_entry->answer) {
2261          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2262                  error = 1;                  error = CCS_RETRY_REQUEST;
2263                  r->retry++;                  r->retry++;
2264                  break;                  break;
2265          case 1:          case 1:
# Line 1702  static int ccs_poll_query(struct file *f Line 2297  static int ccs_poll_query(struct file *f
2297          bool found = false;          bool found = false;
2298          u8 i;          u8 i;
2299          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
                 /***** CRITICAL SECTION START *****/  
2300                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2301                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2302                          struct ccs_query_entry *ptr                          struct ccs_query_entry *ptr
# Line 1713  static int ccs_poll_query(struct file *f Line 2307  static int ccs_poll_query(struct file *f
2307                          break;                          break;
2308                  }                  }
2309                  spin_unlock(&ccs_query_list_lock);                  spin_unlock(&ccs_query_list_lock);
                 /***** CRITICAL SECTION END *****/  
2310                  if (found)                  if (found)
2311                          return POLLIN | POLLRDNORM;                          return POLLIN | POLLRDNORM;
2312                  if (i)                  if (i)
# Line 1727  static int ccs_poll_query(struct file *f Line 2320  static int ccs_poll_query(struct file *f
2320   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2321   *   *
2322   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
2323   */   */
2324  static int ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2325  {  {
2326          struct list_head *tmp;          struct list_head *tmp;
2327          int pos = 0;          int pos = 0;
2328          int len = 0;          int len = 0;
2329          char *buf;          char *buf;
2330          if (head->read_avail)          if (head->read_avail)
2331                  return 0;                  return;
2332          if (head->read_buf) {          if (head->read_buf) {
2333                  kfree(head->read_buf);                  kfree(head->read_buf);
2334                  head->read_buf = NULL;                  head->read_buf = NULL;
2335                  head->readbuf_size = 0;                  head->readbuf_size = 0;
2336          }          }
         /***** CRITICAL SECTION START *****/  
2337          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2338          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2339                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1756  static int ccs_read_query(struct ccs_io_ Line 2346  static int ccs_read_query(struct ccs_io_
2346                  break;                  break;
2347          }          }
2348          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2349          if (!len) {          if (!len) {
2350                  head->read_step = 0;                  head->read_step = 0;
2351                  return 0;                  return;
2352          }          }
2353          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2354          if (!buf)          if (!buf)
2355                  return 0;                  return;
2356          pos = 0;          pos = 0;
         /***** CRITICAL SECTION START *****/  
2357          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2358          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2359                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1783  static int ccs_read_query(struct ccs_io_ Line 2371  static int ccs_read_query(struct ccs_io_
2371                  break;                  break;
2372          }          }
2373          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2374          if (buf[0]) {          if (buf[0]) {
2375                  head->read_avail = len;                  head->read_avail = len;
2376                  head->readbuf_size = head->read_avail;                  head->readbuf_size = head->read_avail;
# Line 1792  static int ccs_read_query(struct ccs_io_ Line 2379  static int ccs_read_query(struct ccs_io_
2379          } else {          } else {
2380                  kfree(buf);                  kfree(buf);
2381          }          }
         return 0;  
2382  }  }
2383    
2384  /**  /**
# Line 1808  static int ccs_write_answer(struct ccs_i Line 2394  static int ccs_write_answer(struct ccs_i
2394          struct list_head *tmp;          struct list_head *tmp;
2395          unsigned int serial;          unsigned int serial;
2396          unsigned int answer;          unsigned int answer;
         /***** CRITICAL SECTION START *****/  
2397          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2398          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2399                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1816  static int ccs_write_answer(struct ccs_i Line 2401  static int ccs_write_answer(struct ccs_i
2401                  ptr->timer = 0;                  ptr->timer = 0;
2402          }          }
2403          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2404          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2405                  return -EINVAL;                  return -EINVAL;
         /***** CRITICAL SECTION START *****/  
2406          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2407          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2408                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1831  static int ccs_write_answer(struct ccs_i Line 2414  static int ccs_write_answer(struct ccs_i
2414                  break;                  break;
2415          }          }
2416          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2417          return 0;          return 0;
2418  }  }
2419    
# Line 1839  static int ccs_write_answer(struct ccs_i Line 2421  static int ccs_write_answer(struct ccs_i
2421   * ccs_read_version: Get version.   * ccs_read_version: Get version.
2422   *   *
2423   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns version information.  
2424   */   */
2425  static int ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2426  {  {
2427          if (!head->read_eof) {          if (head->read_eof)
2428                  ccs_io_printf(head, "1.7.0-pre");                  return;
2429                  head->read_eof = true;          ccs_io_printf(head, "1.7.1");
2430          }          head->read_eof = true;
         return 0;  
2431  }  }
2432    
2433  /**  /**
2434   * ccs_read_self_domain - Get the current process's domainname.   * ccs_read_self_domain - Get the current process's domainname.
2435   *   *
2436   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns the current process's domainname.  
2437   */   */
2438  static int ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2439  {  {
2440          if (!head->read_eof) {          if (head->read_eof)
2441                  /*                  return;
2442                   * ccs_current_domain()->domainname != NULL          /*
2443                   * because every process belongs to a domain and           * ccs_current_domain()->domainname != NULL because every process
2444                   * the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2445                   */           */
2446                  ccs_io_printf(head, "%s",          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2447                                ccs_current_domain()->domainname->name);          head->read_eof = true;
                 head->read_eof = true;  
         }  
         return 0;  
2448  }  }
2449    
2450  /**  /**
# Line 1883  static int ccs_read_self_domain(struct c Line 2457  static int ccs_read_self_domain(struct c
2457   */   */
2458  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2459  {  {
2460          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2461          if (!head)          if (!head)
2462                  return -ENOMEM;                  return -ENOMEM;
2463          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
# Line 1899  int ccs_open_control(const u8 type, stru Line 2473  int ccs_open_control(const u8 type, stru
2473                  break;                  break;
2474  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2475          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;  
2476          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2477                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_audit_log;
2478                  head->read = ccs_read_reject_log;                  head->read = ccs_read_audit_log;
2479                  break;                  break;
2480  #endif  #endif
2481          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 1955  int ccs_open_control(const u8 type, stru Line 2526  int ccs_open_control(const u8 type, stru
2526                   */                   */
2527                  head->read = NULL;                  head->read = NULL;
2528                  head->poll = NULL;                  head->poll = NULL;
2529          } else if (type != CCS_QUERY &&          } else if (!head->poll) {
2530                     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.  
                  */  
2531                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2532                          head->readbuf_size = 4096 * 2;                          head->readbuf_size = 4096;
2533                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2534                  if (!head->read_buf) {                  if (!head->read_buf) {
2535                          kfree(head);                          kfree(head);
2536                          return -ENOMEM;                          return -ENOMEM;
# Line 1976  int ccs_open_control(const u8 type, stru Line 2543  int ccs_open_control(const u8 type, stru
2543                   */                   */
2544                  head->write = NULL;                  head->write = NULL;
2545          } else if (head->write) {          } else if (head->write) {
2546                  head->writebuf_size = 4096 * 2;                  head->writebuf_size = 4096;
2547                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2548                  if (!head->write_buf) {                  if (!head->write_buf) {
2549                          kfree(head->read_buf);                          kfree(head->read_buf);
2550                          kfree(head);                          kfree(head);
# Line 1985  int ccs_open_control(const u8 type, stru Line 2552  int ccs_open_control(const u8 type, stru
2552                  }                  }
2553          }          }
2554          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2555              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG) {
2556                  head->reader_idx = ccs_read_lock();                  spin_lock(&ccs_io_buffer_list_lock);
2557                    list_add(&head->list, &ccs_io_buffer_list);
2558                    spin_unlock(&ccs_io_buffer_list_lock);
2559            }
2560          file->private_data = head;          file->private_data = head;
2561          /*          /*
2562           * 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 1997  int ccs_open_control(const u8 type, stru Line 2567  int ccs_open_control(const u8 type, stru
2567                  ccs_read_control(file, NULL, 0);                  ccs_read_control(file, NULL, 0);
2568          /*          /*
2569           * If the file is /proc/ccs/query , increment the observer counter.           * If the file is /proc/ccs/query , increment the observer counter.
2570           * The obserber counter is used by ccs_check_supervisor() to see if           * The obserber counter is used by ccs_supervisor() to see if
2571           * there is some process monitoring /proc/ccs/query.           * there is some process monitoring /proc/ccs/query.
2572           */           */
2573          else if (type == CCS_QUERY)          else if (type == CCS_QUERY)
# Line 2012  int ccs_open_control(const u8 type, stru Line 2582  int ccs_open_control(const u8 type, stru
2582   * @wait: Pointer to "poll_table".   * @wait: Pointer to "poll_table".
2583   *   *
2584   * Waits for read readiness.   * Waits for read readiness.
2585   * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2586   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2587   * /usr/lib/ccs/ccs-auditd.   * /usr/sbin/ccs-auditd .
2588   */   */
2589  int ccs_poll_control(struct file *file, poll_table *wait)  int ccs_poll_control(struct file *file, poll_table *wait)
2590  {  {
# Line 2039  int ccs_read_control(struct file *file, Line 2609  int ccs_read_control(struct file *file,
2609          int len = 0;          int len = 0;
2610          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2611          char *cp;          char *cp;
2612            int idx;
2613          if (!head->read)          if (!head->read)
2614                  return -ENOSYS;                  return -ENOSYS;
2615          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2616                  return -EFAULT;                  return -EFAULT;
2617          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2618                  return -EINTR;                  return -EINTR;
2619          /* Call the policy handler. */          idx = ccs_read_lock();
2620          len = head->read(head);          while (1) {
2621          if (len < 0)                  /* Call the policy handler. */
2622                  goto out;                  head->read(head);
2623          /* Write to buffer. */                  /* Write to buffer. */
2624          len = head->read_avail;                  len = head->read_avail;
2625                    if (len || head->poll || head->read_eof)
2626                            break;
2627                    len = head->readbuf_size * 2;
2628                    cp = kzalloc(len, CCS_GFP_FLAGS);
2629                    if (!cp) {
2630                            len = -ENOMEM;
2631                            goto out;
2632                    }
2633                    kfree(head->read_buf);
2634                    head->read_buf = cp;
2635                    head->readbuf_size = len;
2636            }
2637          if (len > buffer_len)          if (len > buffer_len)
2638                  len = buffer_len;                  len = buffer_len;
2639          if (!len)          if (!len)
# Line 2064  int ccs_read_control(struct file *file, Line 2647  int ccs_read_control(struct file *file,
2647          head->read_avail -= len;          head->read_avail -= len;
2648          memmove(cp, cp + len, head->read_avail);          memmove(cp, cp + len, head->read_avail);
2649   out:   out:
2650            ccs_read_unlock(idx);
2651          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2652          return len;          return len;
2653  }  }
# Line 2084  int ccs_write_control(struct file *file, Line 2668  int ccs_write_control(struct file *file,
2668          int error = buffer_len;          int error = buffer_len;
2669          int avail_len = buffer_len;          int avail_len = buffer_len;
2670          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2671            int idx;
2672          if (!head->write)          if (!head->write)
2673                  return -ENOSYS;                  return -ENOSYS;
2674          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
# Line 2095  int ccs_write_control(struct file *file, Line 2680  int ccs_write_control(struct file *file,
2680                  return -EPERM;                  return -EPERM;
2681          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2682                  return -EINTR;                  return -EINTR;
2683            idx = ccs_read_lock();
2684          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2685          while (avail_len > 0) {          while (avail_len > 0) {
2686                  char c;                  char c;
2687                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->write_avail >= head->writebuf_size - 1) {
2688                          error = -ENOMEM;                          const int len = head->writebuf_size * 2;
2689                          break;                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2690                  } else if (get_user(c, buffer)) {                          if (!cp) {
2691                                    error = -ENOMEM;
2692                                    break;
2693                            }
2694                            memmove(cp, cp0, head->write_avail);
2695                            kfree(cp0);
2696                            head->write_buf = cp;
2697                            cp0 = cp;
2698                            head->writebuf_size = len;
2699                    }
2700                    if (get_user(c, buffer)) {
2701                          error = -EFAULT;                          error = -EFAULT;
2702                          break;                          break;
2703                  }                  }
# Line 2115  int ccs_write_control(struct file *file, Line 2711  int ccs_write_control(struct file *file,
2711                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2712                  head->write(head);                  head->write(head);
2713          }          }
2714            ccs_read_unlock(idx);
2715          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2716          return error;          return error;
2717  }  }
# Line 2137  int ccs_close_control(struct file *file) Line 2734  int ccs_close_control(struct file *file)
2734          if (type == CCS_QUERY)          if (type == CCS_QUERY)
2735                  atomic_dec(&ccs_query_observers);                  atomic_dec(&ccs_query_observers);
2736          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2737              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG) {
2738                  ccs_read_unlock(head->reader_idx);                  spin_lock(&ccs_io_buffer_list_lock);
2739                    list_del(&head->list);
2740                    spin_unlock(&ccs_io_buffer_list_lock);
2741            }
2742          /* Release memory used for policy I/O. */          /* Release memory used for policy I/O. */
2743          kfree(head->read_buf);          kfree(head->read_buf);
2744          head->read_buf = NULL;          head->read_buf = NULL;
# Line 2151  int ccs_close_control(struct file *file) Line 2751  int ccs_close_control(struct file *file)
2751                  ccs_run_gc();                  ccs_run_gc();
2752          return 0;          return 0;
2753  }  }
2754    
2755    void __init ccs_policy_io_init(void)
2756    {
2757            ccsecurity_ops.check_profile = ccs_check_profile;
2758    }

Legend:
Removed from v.2864  
changed lines
  Added in v.3531

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