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

Subversion リポジトリの参照

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

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

revision 2869 by kumaneko, Sat Aug 8 02:47:05 2009 UTC revision 3761 by kumaneko, Sun Jun 13 14:58:41 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/policy_io.c   * security/ccsecurity/policy_io.c
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.0-pre   2009/08/08   * Version: 1.7.2+   2010/06/04
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 12  Line 12 
12    
13  #include "internal.h"  #include "internal.h"
14    
15  /* Lock for protecting ccs_profile->comment  */  static struct ccs_profile ccs_default_profile = {
16  static DEFINE_SPINLOCK(ccs_profile_comment_lock);          .learning = &ccs_default_profile.preference,
17            .permissive = &ccs_default_profile.preference,
18            .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[CCS_CONFIG_MAX_MODE] = {
44          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED] = "disabled",
45  };          [CCS_CONFIG_LEARNING] = "learning",
46  /* String table for functionality that takes 2 modes. */          [CCS_CONFIG_PERMISSIVE] = "permissive",
47  static const char *ccs_mode_2[4] = {          [CCS_CONFIG_ENFORCING] = "enforcing"
         "disabled", "enabled", "enabled", "enabled"  
48  };  };
49    
50  /* Table for profile. */  /* String table for /proc/ccs/profile */
51  static struct {  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +
52          const char *keyword;                                      CCS_MAX_CAPABILITY_INDEX +
53          unsigned int current_value;                                      CCS_MAX_MAC_CATEGORY_INDEX] = {
54          const unsigned int max_value;          [CCS_MAC_FILE_EXECUTE]
55  } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {          = "file::execute",
56          [CCS_MAC_FOR_FILE]        = { "MAC_FOR_FILE",        0, 3 },          [CCS_MAC_FILE_OPEN]
57          [CCS_MAC_FOR_IOCTL]       = { "MAC_FOR_IOCTL",       0, 3 },          = "file::open",
58          [CCS_MAC_FOR_ARGV0]       = { "MAC_FOR_ARGV0",       0, 3 },          [CCS_MAC_FILE_CREATE]
59          [CCS_MAC_FOR_ENV]         = { "MAC_FOR_ENV",         0, 3 },          = "file::create",
60          [CCS_MAC_FOR_NETWORK]     = { "MAC_FOR_NETWORK",     0, 3 },          [CCS_MAC_FILE_UNLINK]
61          [CCS_MAC_FOR_SIGNAL]      = { "MAC_FOR_SIGNAL",      0, 3 },          = "file::unlink",
62          [CCS_MAC_FOR_NAMESPACE]   = { "MAC_FOR_NAMESPACE",   0, 3 },          [CCS_MAC_FILE_MKDIR]
63          [CCS_RESTRICT_AUTOBIND]   = { "RESTRICT_AUTOBIND",   0, 1 },          = "file::mkdir",
64          [CCS_MAX_ACCEPT_ENTRY]          [CCS_MAC_FILE_RMDIR]
65          = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },          = "file::rmdir",
66  #ifdef CONFIG_CCSECURITY_AUDIT          [CCS_MAC_FILE_MKFIFO]
67          [CCS_MAX_GRANT_LOG]          = "file::mkfifo",
68          = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },          [CCS_MAC_FILE_MKSOCK]
69          [CCS_MAX_REJECT_LOG]          = "file::mksock",
70          = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },          [CCS_MAC_FILE_TRUNCATE]
71  #endif          = "file::truncate",
72          [CCS_VERBOSE]             = { "TOMOYO_VERBOSE",      1, 1 },          [CCS_MAC_FILE_SYMLINK]
73          [CCS_SLEEP_PERIOD]          = "file::symlink",
74          = { "SLEEP_PERIOD",        0, 3000 }, /* in 0.1 second */          [CCS_MAC_FILE_REWRITE]
75            = "file::rewrite",
76            [CCS_MAC_FILE_MKBLOCK]
77            = "file::mkblock",
78            [CCS_MAC_FILE_MKCHAR]
79            = "file::mkchar",
80            [CCS_MAC_FILE_LINK]
81            = "file::link",
82            [CCS_MAC_FILE_RENAME]
83            = "file::rename",
84            [CCS_MAC_FILE_CHMOD]
85            = "file::chmod",
86            [CCS_MAC_FILE_CHOWN]
87            = "file::chown",
88            [CCS_MAC_FILE_CHGRP]
89            = "file::chgrp",
90            [CCS_MAC_FILE_IOCTL]
91            = "file::ioctl",
92            [CCS_MAC_FILE_CHROOT]
93            = "file::chroot",
94            [CCS_MAC_FILE_MOUNT]
95            = "file::mount",
96            [CCS_MAC_FILE_UMOUNT]
97            = "file::umount",
98            [CCS_MAC_FILE_PIVOT_ROOT]
99            = "file::pivot_root",
100            [CCS_MAC_FILE_TRANSIT]
101            = "file::transit",
102            [CCS_MAC_ENVIRON]
103            = "misc::env",
104            [CCS_MAC_NETWORK_UDP_BIND]
105            = "network::inet_udp_bind",
106            [CCS_MAC_NETWORK_UDP_CONNECT]
107            = "network::inet_udp_connect",
108            [CCS_MAC_NETWORK_TCP_BIND]
109            = "network::inet_tcp_bind",
110            [CCS_MAC_NETWORK_TCP_LISTEN]
111            = "network::inet_tcp_listen",
112            [CCS_MAC_NETWORK_TCP_CONNECT]
113            = "network::inet_tcp_connect",
114            [CCS_MAC_NETWORK_TCP_ACCEPT]
115            = "network::inet_tcp_accept",
116            [CCS_MAC_NETWORK_RAW_BIND]
117            = "network::inet_raw_bind",
118            [CCS_MAC_NETWORK_RAW_CONNECT]
119            = "network::inet_raw_connect",
120            [CCS_MAC_SIGNAL]
121            = "ipc::signal",
122            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]
123            = "capability::inet_tcp_create",
124            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]
125            = "capability::inet_tcp_listen",
126            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]
127            = "capability::inet_tcp_connect",
128            [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]
129            = "capability::use_inet_udp",
130            [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]
131            = "capability::use_inet_ip",
132            [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
133            = "capability::use_route",
134            [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
135            = "capability::use_packet",
136            [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]
137            = "capability::SYS_MOUNT",
138            [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]
139            = "capability::SYS_UMOUNT",
140            [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
141            = "capability::SYS_REBOOT",
142            [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]
143            = "capability::SYS_CHROOT",
144            [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]
145            = "capability::SYS_KILL",
146            [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
147            = "capability::SYS_VHANGUP",
148            [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
149            = "capability::SYS_TIME",
150            [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]
151            = "capability::SYS_NICE",
152            [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]
153            = "capability::SYS_SETHOSTNAME",
154            [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
155            = "capability::use_kernel_module",
156            [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]
157            = "capability::create_fifo",
158            [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]
159            = "capability::create_block_dev",
160            [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]
161            = "capability::create_char_dev",
162            [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]
163            = "capability::create_unix_socket",
164            [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]
165            = "capability::SYS_LINK",
166            [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]
167            = "capability::SYS_SYMLINK",
168            [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]
169            = "capability::SYS_RENAME",
170            [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]
171            = "capability::SYS_UNLINK",
172            [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]
173            = "capability::SYS_CHMOD",
174            [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]
175            = "capability::SYS_CHOWN",
176            [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]
177            = "capability::SYS_IOCTL",
178            [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
179            = "capability::SYS_KEXEC_LOAD",
180            [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]
181            = "capability::SYS_PIVOT_ROOT",
182            [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
183            = "capability::SYS_PTRACE",
184            [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]
185            = "capability::conceal_mount",
186            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
187             + CCS_MAC_CATEGORY_FILE] = "file",
188            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
189             + CCS_MAC_CATEGORY_NETWORK] = "network",
190            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
191             + CCS_MAC_CATEGORY_MISC] = "misc",
192            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
193             + CCS_MAC_CATEGORY_IPC] = "ipc",
194            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
195             + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
196  };  };
197    
198  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
199  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
200    
201  /**  /**
202   * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.   * ccs_cap2keyword - Convert capability operation to capability name.
203   *   *
204   * @str: Unused.   * @operation: The capability index.
205   *   *
206   * Returns 0.   * Returns the name of the specified capability's name.
207   */   */
208  static int __init ccs_quiet_setup(char *str)  const char *ccs_cap2keyword(const u8 operation)
209  {  {
210          ccs_control_array[CCS_VERBOSE].current_value = 0;          return operation < CCS_MAX_CAPABILITY_INDEX
211          return 0;                  ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;
212  }  }
213    
214  __setup("CCS_QUIET", ccs_quiet_setup);  /**
215     * ccs_yesno - Return "yes" or "no".
216     *
217     * @value: Bool value.
218     */
219    static const char *ccs_yesno(const unsigned int value)
220    {
221            return value ? "yes" : "no";
222    }
223    
224  /**  /**
225   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
# Line 100  bool ccs_io_printf(struct ccs_io_buffer Line 249  bool ccs_io_printf(struct ccs_io_buffer
249  }  }
250    
251  /**  /**
252   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
253   *   *
254   * @profile: Profile number to create.   * @profile: Profile number to create.
255   *   *
256   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
257   */   */
258  struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_assign_profile(const unsigned int profile)
                                                    profile)  
259  {  {
260          struct ccs_profile *ptr;          struct ccs_profile *ptr;
261          struct ccs_profile *entry;          struct ccs_profile *entry;
262          int i;          if (profile >= CCS_MAX_PROFILES)
         if (profile >= MAX_PROFILES)  
263                  return NULL;                  return NULL;
264          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
265          if (ptr)          if (ptr)
266                  return ptr;                  return ptr;
267          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
268          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
269                    goto out;
270          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
271          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
272                  ptr = entry;                  ptr = entry;
273                  for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)                  ptr->audit = &ccs_default_profile.preference;
274                          ptr->value[i] = ccs_control_array[i].current_value;                  ptr->learning = &ccs_default_profile.preference;
275                  /*                  ptr->permissive = &ccs_default_profile.preference;
276                   * Needn't to initialize "ptr->capability_value"                  ptr->enforcing = &ccs_default_profile.preference;
277                   * because they are always 0.                  ptr->default_config = CCS_CONFIG_DISABLED |
278                   */                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
279                    memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
280                           sizeof(ptr->config));
281                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
282                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
283                  entry = NULL;                  entry = NULL;
284          }          }
285          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
286     out:
287          kfree(entry);          kfree(entry);
288          return ptr;          return ptr;
289  }  }
290    
291  /**  /**
292     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
293     */
294    static void ccs_check_profile(void)
295    {
296            struct ccs_domain_info *domain;
297            const int idx = ccs_read_lock();
298            ccs_policy_loaded = true;
299            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
300                    const u8 profile = domain->profile;
301                    if (ccs_profile_ptr[profile])
302                            continue;
303                    panic("Profile %u (used by '%s') not defined.\n",
304                          profile, domain->domainname->name);
305            }
306            ccs_read_unlock(idx);
307            if (ccs_profile_version != 20090903)
308                    panic("Profile version %u is not supported.\n",
309                          ccs_profile_version);
310            printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");
311            printk(KERN_INFO "Mandatory Access Control activated.\n");
312    }
313    
314    /**
315     * ccs_profile - Find a profile.
316     *
317     * @profile: Profile number to find.
318     *
319     * Returns pointer to "struct ccs_profile".
320     */
321    struct ccs_profile *ccs_profile(const u8 profile)
322    {
323            struct ccs_profile *ptr = ccs_profile_ptr[profile];
324            if (!ccs_policy_loaded)
325                    return &ccs_default_profile;
326            BUG_ON(!ptr);
327            return ptr;
328    }
329    
330    static s8 ccs_find_yesno(const char *string, const char *find)
331    {
332            const char *cp = strstr(string, find);
333            if (cp) {
334                    cp += strlen(find);
335                    if (!strncmp(cp, "=yes", 4))
336                            return 1;
337                    else if (!strncmp(cp, "=no", 3))
338                            return 0;
339            }
340            return -1;
341    }
342    
343    static void ccs_set_bool(bool *b, const char *string, const char *find)
344    {
345            switch (ccs_find_yesno(string, find)) {
346            case 1:
347                    *b = true;
348                    break;
349            case 0:
350                    *b = false;
351                    break;
352            }
353    }
354    
355    static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
356    {
357            const char *cp = strstr(string, find);
358            if (cp)
359                    sscanf(cp + strlen(find), "=%u", i);
360    }
361    
362    static void ccs_set_pref(const char *name, const char *value,
363                             const bool use_default, struct ccs_profile *profile)
364    {
365            struct ccs_preference **pref;
366            bool *verbose;
367            if (!strcmp(name, "audit")) {
368                    if (use_default) {
369                            pref = &profile->audit;
370                            goto set_default;
371                    }
372                    profile->audit = &profile->preference;
373    #ifdef CONFIG_CCSECURITY_AUDIT
374                    ccs_set_uint(&profile->preference.audit_max_grant_log, value,
375                                 "max_grant_log");
376                    ccs_set_uint(&profile->preference.audit_max_reject_log, value,
377                                 "max_reject_log");
378    #endif
379                    ccs_set_bool(&profile->preference.audit_task_info, value,
380                                 "task_info");
381                    ccs_set_bool(&profile->preference.audit_path_info, value,
382                                 "path_info");
383                    return;
384            }
385            if (!strcmp(name, "enforcing")) {
386                    if (use_default) {
387                            pref = &profile->enforcing;
388                            goto set_default;
389                    }
390                    profile->enforcing = &profile->preference;
391                    ccs_set_uint(&profile->preference.enforcing_penalty, value,
392                                 "penalty");
393                    verbose = &profile->preference.enforcing_verbose;
394                    goto set_verbose;
395            }
396            if (!strcmp(name, "permissive")) {
397                    if (use_default) {
398                            pref = &profile->permissive;
399                            goto set_default;
400                    }
401                    profile->permissive = &profile->preference;
402                    verbose = &profile->preference.permissive_verbose;
403                    goto set_verbose;
404            }
405            if (!strcmp(name, "learning")) {
406                    if (use_default) {
407                            pref = &profile->learning;
408                            goto set_default;
409                    }
410                    profile->learning = &profile->preference;
411                    ccs_set_uint(&profile->preference.learning_max_entry, value,
412                                 "max_entry");
413                    ccs_set_bool(&profile->preference.learning_exec_realpath,
414                                 value, "exec.realpath");
415                    ccs_set_bool(&profile->preference.learning_exec_argv0, value,
416                                 "exec.argv0");
417                    ccs_set_bool(&profile->preference.learning_symlink_target,
418                                 value, "symlink.target");
419                    verbose = &profile->preference.learning_verbose;
420                    goto set_verbose;
421            }
422            return;
423     set_default:
424            *pref = &ccs_default_profile.preference;
425            return;
426     set_verbose:
427            ccs_set_bool(verbose, value, "verbose");
428    }
429    
430    static int ccs_set_mode(char *name, const char *value, const bool use_default,
431                            struct ccs_profile *profile)
432    {
433            u8 i;
434            u8 config;
435            if (!strcmp(name, "CONFIG")) {
436                    i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
437                            + CCS_MAX_MAC_CATEGORY_INDEX;
438                    config = profile->default_config;
439            } else if (ccs_str_starts(&name, "CONFIG::")) {
440                    config = 0;
441                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
442                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
443                            if (strcmp(name, ccs_mac_keywords[i]))
444                                    continue;
445                            config = profile->config[i];
446                            break;
447                    }
448                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
449                        + CCS_MAX_MAC_CATEGORY_INDEX)
450                            return -EINVAL;
451            } else {
452                    return -EINVAL;
453            }
454            if (use_default) {
455                    config = CCS_CONFIG_USE_DEFAULT;
456            } else {
457                    u8 mode;
458                    for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
459                            if (strstr(value, ccs_mode[mode]))
460                                    /*
461                                     * Update lower 3 bits in order to distinguish
462                                     * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
463                                     */
464                                    config = (config & ~7) | mode;
465    #ifdef CONFIG_CCSECURITY_AUDIT
466                    if (config != CCS_CONFIG_USE_DEFAULT) {
467                            switch (ccs_find_yesno(value, "grant_log")) {
468                            case 1:
469                                    config |= CCS_CONFIG_WANT_GRANT_LOG;
470                                    break;
471                            case 0:
472                                    config &= ~CCS_CONFIG_WANT_GRANT_LOG;
473                                    break;
474                            }
475                            switch (ccs_find_yesno(value, "reject_log")) {
476                            case 1:
477                                    config |= CCS_CONFIG_WANT_REJECT_LOG;
478                                    break;
479                            case 0:
480                                    config &= ~CCS_CONFIG_WANT_REJECT_LOG;
481                                    break;
482                            }
483                    }
484    #endif
485            }
486            if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
487                + CCS_MAX_MAC_CATEGORY_INDEX)
488                    profile->config[i] = config;
489            else if (config != CCS_CONFIG_USE_DEFAULT)
490                    profile->default_config = config;
491            return 0;
492    }
493    
494    /**
495   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
496   *   *
497   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 147  struct ccs_profile *ccs_find_or_assign_n Line 501  struct ccs_profile *ccs_find_or_assign_n
501  static int ccs_write_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
502  {  {
503          char *data = head->write_buf;          char *data = head->write_buf;
504          unsigned int i;          bool use_default = false;
         unsigned int value;  
505          char *cp;          char *cp;
506          struct ccs_profile *ccs_profile;          int i;
507            struct ccs_profile *profile;
508            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
509                    return 0;
510          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
511          if (data != cp) {          if (data == cp) {
512                    profile = &ccs_default_profile;
513            } else {
514                  if (*cp != '-')                  if (*cp != '-')
515                          return -EINVAL;                          return -EINVAL;
516                  data = cp + 1;                  data = cp + 1;
517                    profile = ccs_assign_profile(i);
518                    if (!profile)
519                            return -EINVAL;
520          }          }
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
                 return -EINVAL;  
521          cp = strchr(data, '=');          cp = strchr(data, '=');
522          if (!cp)          if (!cp)
523                  return -EINVAL;                  return -EINVAL;
524          *cp = '\0';          *cp++ = '\0';
525            if (profile != &ccs_default_profile)
526                    use_default = strstr(cp, "use_default") != NULL;
527            if (ccs_str_starts(&data, "PREFERENCE::")) {
528                    ccs_set_pref(data, cp, use_default, profile);
529                    return 0;
530            }
531            if (profile == &ccs_default_profile)
532                    return -EINVAL;
533          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
534                  const struct ccs_path_info *new_comment                  const struct ccs_path_info *old_comment = profile->comment;
535                          = 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 *****/  
536                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
                 ccs_profile_entry_used[0] = true;  
537                  return 0;                  return 0;
538          }          }
539          if (ccs_str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {          return ccs_set_mode(data, cp, use_default, profile);
540                  if (sscanf(cp + 1, "%u", &value) != 1) {  }
541                          for (i = 0; i < 4; i++) {  
542                                  if (strcmp(cp + 1, ccs_mode_4[i]))  static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)
543                                          continue;  {
544                                  value = i;          struct ccs_preference *pref = &ccs_default_profile.preference;
545                                  break;          const struct ccs_profile *profile = idx >= 0 ?
546                          }                  ccs_profile_ptr[idx] : NULL;
547                          if (i == 4)          char buffer[16] = "";
548                                  return -EINVAL;          if (profile) {
549                  }                  buffer[sizeof(buffer) - 1] = '\0';
550                  if (value > 3)                  snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
                         value = 3;  
                 for (i = 0; i < CCS_MAX_CAPABILITY_INDEX; i++) {  
                         if (strcmp(data, ccs_capability_control_keyword[i]))  
                                 continue;  
                         ccs_profile->capability_value[i] = value;  
                         ccs_profile_entry_used[i + 1 + CCS_MAX_CONTROL_INDEX]  
                                 = true;  
                         return 0;  
                 }  
                 return -EINVAL;  
551          }          }
552          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {          if (profile) {
553                  if (strcmp(data, ccs_control_array[i].keyword))                  pref = profile->audit;
554                          continue;                  if (pref == &ccs_default_profile.preference)
555                  if (sscanf(cp + 1, "%u", &value) != 1) {                          pref = NULL;
                         int j;  
                         const char **modes;  
                         switch (i) {  
                         case CCS_RESTRICT_AUTOBIND:  
                         case CCS_VERBOSE:  
                                 modes = ccs_mode_2;  
                                 break;  
                         default:  
                                 modes = ccs_mode_4;  
                                 break;  
                         }  
                         for (j = 0; j < 4; j++) {  
                                 if (strcmp(cp + 1, modes[j]))  
                                         continue;  
                                 value = j;  
                                 break;  
                         }  
                         if (j == 4)  
                                 return -EINVAL;  
                 } else if (value > ccs_control_array[i].max_value) {  
                         value = ccs_control_array[i].max_value;  
                 }  
                 ccs_profile->value[i] = value;  
                 ccs_profile_entry_used[i + 1] = true;  
                 return 0;  
556          }          }
557          return -EINVAL;          if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "
558    #ifdef CONFIG_CCSECURITY_AUDIT
559                                       "max_grant_log=%u max_reject_log=%u "
560    #endif
561                                       "task_info=%s path_info=%s }\n", buffer,
562                                       "audit",
563    #ifdef CONFIG_CCSECURITY_AUDIT
564                                       pref->audit_max_grant_log,
565                                       pref->audit_max_reject_log,
566    #endif
567                                       ccs_yesno(pref->audit_task_info),
568                                       ccs_yesno(pref->audit_path_info)))
569                    return false;
570            if (profile) {
571                    pref = profile->learning;
572                    if (pref == &ccs_default_profile.preference)
573                            pref = NULL;
574            }
575            if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "
576                                       "verbose=%s max_entry=%u exec.realpath=%s "
577                                       "exec.argv0=%s symlink.target=%s }\n",
578                                       buffer, "learning",
579                                       ccs_yesno(pref->learning_verbose),
580                                       pref->learning_max_entry,
581                                       ccs_yesno(pref->learning_exec_realpath),
582                                       ccs_yesno(pref->learning_exec_argv0),
583                                       ccs_yesno(pref->learning_symlink_target)))
584                    return false;
585            if (profile) {
586                    pref = profile->permissive;
587                    if (pref == &ccs_default_profile.preference)
588                            pref = NULL;
589            }
590            if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
591                                       buffer, "permissive",
592                                       ccs_yesno(pref->permissive_verbose)))
593                    return false;
594            if (profile) {
595                    pref = profile->enforcing;
596                    if (pref == &ccs_default_profile.preference)
597                            pref = NULL;
598            }
599            return !pref || ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
600                                          "penalty=%u }\n", buffer, "enforcing",
601                                          ccs_yesno(pref->enforcing_verbose),
602                                          pref->enforcing_penalty);
603  }  }
604    
605  /**  /**
606   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
607   *   *
608   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
609   */   */
610  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
611  {  {
612          static const int ccs_total          int index;
                 = CCS_MAX_CONTROL_INDEX + CCS_MAX_CAPABILITY_INDEX + 1;  
         int step;  
613          if (head->read_eof)          if (head->read_eof)
614                  return 0;                  return;
615          for (step = head->read_step; step < MAX_PROFILES * ccs_total; step++) {          if (head->read_bit)
616                  const u8 index = step / ccs_total;                  goto body;
617                  u8 type = step % ccs_total;          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
618                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];          ccs_print_preference(head, -1);
619                  head->read_step = step;          head->read_bit = 1;
620                  if (!ccs_profile)   body:
621                          continue;          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {
622                  if (!ccs_profile_entry_used[type])                  bool done;
623                          continue;                  u8 config;
624                  if (!type) { /* Print profile' comment tag. */                  int i;
625                          bool done;                  int pos;
626                          /***** CRITICAL SECTION START *****/                  const struct ccs_profile *profile = ccs_profile_ptr[index];
627                          spin_lock(&ccs_profile_comment_lock);                  const struct ccs_path_info *comment;
628                          done = ccs_io_printf(head, "%u-COMMENT=%s\n",                  head->read_step = index;
629                                               index, ccs_profile->comment ?                  if (!profile)
                                              ccs_profile->comment->name : "");  
                         spin_unlock(&ccs_profile_comment_lock);  
                         /***** CRITICAL SECTION END *****/  
                         if (!done)  
                                 break;  
630                          continue;                          continue;
631                    pos = head->read_avail;
632                    comment = profile->comment;
633                    done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,
634                                         comment ? comment->name : "");
635                    if (!done)
636                            goto out;
637                    config = profile->default_config;
638    #ifdef CONFIG_CCSECURITY_AUDIT
639                    if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
640                                       "grant_log=%s reject_log=%s }\n", index,
641                                       "CONFIG", "", ccs_mode[config & 3],
642                                       ccs_yesno(config &
643                                                 CCS_CONFIG_WANT_GRANT_LOG),
644                                       ccs_yesno(config &
645                                                 CCS_CONFIG_WANT_REJECT_LOG)))
646                            goto out;
647    #else
648                    if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,
649                                       "CONFIG", "", ccs_mode[config & 3]))
650                            goto out;
651    #endif
652                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
653                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
654    #ifdef CONFIG_CCSECURITY_AUDIT
655                            const char *g;
656                            const char *r;
657    #endif
658                            config = profile->config[i];
659                            if (config == CCS_CONFIG_USE_DEFAULT)
660                                    continue;
661    #ifdef CONFIG_CCSECURITY_AUDIT
662                            g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);
663                            r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);
664                            if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
665                                               "grant_log=%s reject_log=%s }\n",
666                                               index, "CONFIG::",
667                                               ccs_mac_keywords[i],
668                                               ccs_mode[config & 3], g, r))
669                                    goto out;
670    #else
671                            if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",
672                                               index, "CONFIG::",
673                                               ccs_mac_keywords[i],
674                                               ccs_mode[config & 3]))
675                                    goto out;
676    #endif
677                  }                  }
678                  type--;                  if (!ccs_print_preference(head, index))
679                  if (type >= CCS_MAX_CONTROL_INDEX) {                          goto out;
680                          const int i = type - CCS_MAX_CONTROL_INDEX;                  continue;
681                          const u8 value = ccs_profile->capability_value[i];   out:
682                          if (!ccs_io_printf(head,                  head->read_avail = pos;
683                                             "%u-" KEYWORD_MAC_FOR_CAPABILITY                  break;
                                            "%s=%s\n", index,  
                                            ccs_capability_control_keyword[i],  
                                            ccs_mode_4[value]))  
                                 break;  
                 } else {  
                         const unsigned int value = ccs_profile->value[type];  
                         const char **modes = NULL;  
                         const char *keyword = ccs_control_array[type].keyword;  
                         switch (ccs_control_array[type].max_value) {  
                         case 3:  
                                 modes = ccs_mode_4;  
                                 break;  
                         case 1:  
                                 modes = ccs_mode_2;  
                                 break;  
                         }  
                         if (modes) {  
                                 if (!ccs_io_printf(head, "%u-%s=%s\n", index,  
                                                    keyword, modes[value]))  
                                         break;  
                         } else {  
                                 if (!ccs_io_printf(head, "%u-%s=%u\n", index,  
                                                    keyword, value))  
                                         break;  
                         }  
                 }  
684          }          }
685          if (step == MAX_PROFILES * ccs_total)          if (index == CCS_MAX_PROFILES)
686                  head->read_eof = true;                  head->read_eof = true;
         return 0;  
687  }  }
688    
689  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,
690  LIST_HEAD(ccs_policy_manager_list);                                     const struct ccs_acl_head *b)
691    {
692            return container_of(a, struct ccs_manager, head)->manager
693                    == container_of(b, struct ccs_manager, head)->manager;
694    }
695    
696  /**  /**
697   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 322  LIST_HEAD(ccs_policy_manager_list); Line 703  LIST_HEAD(ccs_policy_manager_list);
703   */   */
704  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)
705  {  {
706          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         const struct ccs_path_info *saved_manager;  
707          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
708          bool is_domain = false;          if (ccs_domain_def(manager)) {
709          if (ccs_is_domain_def(manager)) {                  if (!ccs_correct_domain(manager))
                 if (!ccs_is_correct_domain(manager))  
710                          return -EINVAL;                          return -EINVAL;
711                  is_domain = true;                  e.is_domain = true;
712          } else {          } else {
713                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
714                          return -EINVAL;                          return -EINVAL;
715          }          }
716          saved_manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
717          if (!saved_manager)          if (!e.manager)
718                  return -ENOMEM;                  return error;
719          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
720                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
721          mutex_lock(&ccs_policy_lock);                                    ccs_same_manager_entry);
722          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {          ccs_put_name(e.manager);
                 if (ptr->manager != saved_manager)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {  
                 entry->manager = saved_manager;  
                 saved_manager = NULL;  
                 entry->is_domain = is_domain;  
                 list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
         ccs_put_name(saved_manager);  
         kfree(entry);  
723          return error;          return error;
724  }  }
725    
726  /**  /**
727   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
728   *   *
729   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
730   *   *
731   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
732   */   */
733  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
734  {  {
735          char *data = head->write_buf;          char *data = head->write_buf;
736          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
737          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
738                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
739                  return 0;                  return 0;
# Line 381  static int ccs_write_manager_policy(stru Line 742  static int ccs_write_manager_policy(stru
742  }  }
743    
744  /**  /**
745   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
746   *   *
747   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
748   *   *
  * Returns 0.  
  *  
749   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
750   */   */
751  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
752  {  {
753          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
754          if (head->read_eof)          if (head->read_eof)
755                  return 0;                  return;
756          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(pos, head->read_var2,
757                  struct ccs_policy_manager_entry *ptr;                               &ccs_policy_list[CCS_ID_MANAGER]) {
758                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                  struct ccs_manager *ptr
759                  if (ptr->is_deleted)                          = list_entry(pos, typeof(*ptr), head.list);
760                    if (ptr->head.is_deleted)
761                          continue;                          continue;
762                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
763                          return 0;                          return;
764          }          }
765          head->read_eof = true;          head->read_eof = true;
         return 0;  
766  }  }
767    
768  /**  /**
769   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
770   *   *
771   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
772   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
773   *   *
774   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
775   */   */
776  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
777  {  {
778          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
779          const char *exe;          const char *exe;
780          struct task_struct *task = current;          struct task_struct *task = current;
781          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
782                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
783          bool found = false;          bool found = false;
         ccs_check_read_lock();  
784          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
785                  return true;                  return true;
786          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
787                  return true;                  return true;
788          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
789                  return false;                  return false;
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (!ptr->is_deleted && ptr->is_domain  
                     && !ccs_pathcmp(domainname, ptr->manager)) {  
                         /* Set manager flag. */  
                         task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;  
                         return true;  
                 }  
         }  
790          exe = ccs_get_exe();          exe = ccs_get_exe();
791          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
792                  return false;                                  head.list) {
793          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
794                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
795                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
796                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
797                          /* Set manager flag. */                                  continue;
798                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
799                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
800                                    continue;
801                  }                  }
802                    /* Set manager flag. */
803                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
804                    found = true;
805                    break;
806          }          }
807          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
808                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 474  static bool ccs_is_policy_manager(void) Line 828  static bool ccs_is_policy_manager(void)
828  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
829  {  {
830          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
831          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
832                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
833                  if (cp)          if (cp)
834                          *cp++ = '\0';                  *cp++ = '\0';
         }  
835          return cp;          return cp;
836  }  }
837    
838  /**  /**
839   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
840   *   *
841   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
842   * @data: String to parse.   * @data: String to parse.
# Line 500  static char *ccs_find_condition_part(cha Line 845  static char *ccs_find_condition_part(cha
845   *   *
846   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
847   */   */
848  static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)  static bool ccs_select_one(struct ccs_io_buffer *head, const char *data)
849  {  {
850          unsigned int pid;          unsigned int pid;
851          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
852          ccs_check_read_lock();          bool global_pid = false;
853          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "allow_execute")) {
854                  head->read_execute_only = true;                  head->read_execute_only = true;
855                  return true;                  return true;
856          }          }
857          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
858                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
859                  struct task_struct *p;                  struct task_struct *p;
860                  /***** CRITICAL SECTION START *****/                  ccs_tasklist_lock();
861                  read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
862                    if (global_pid)
863                            p = ccsecurity_exports.find_task_by_pid_ns(pid,
864                                                                   &init_pid_ns);
865                    else
866                            p = ccsecurity_exports.find_task_by_vpid(pid);
867    #else
868                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
869    #endif
870                  if (p)                  if (p)
871                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
872                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
                 /***** CRITICAL SECTION END *****/  
873          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
874                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
875                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
876          } else          } else
877                  return false;                  return false;
# Line 548  static bool ccs_is_select_one(struct ccs Line 900  static bool ccs_is_select_one(struct ccs
900          return true;          return true;
901  }  }
902    
903    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
904                                 const bool is_delete)
905    {
906            static const struct {
907                    const char *keyword;
908                    int (*write) (char *, struct ccs_domain_info *,
909                                  struct ccs_condition *, const bool);
910            } ccs_callback[5] = {
911                    { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
912                    { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
913                    { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
914                    { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
915                    { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
916            };
917            int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
918                          const bool) = ccs_write_file;
919            int error;
920            u8 i;
921            struct ccs_condition *cond = NULL;
922            char *cp = ccs_find_condition_part(data);
923            if (cp) {
924                    cond = ccs_get_condition(cp);
925                    if (!cond)
926                            return -EINVAL;
927            }
928            for (i = 0; i < 5; i++) {
929                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
930                            continue;
931                    write = ccs_callback[i].write;
932                    break;
933            }
934            error = write(data, domain, cond, is_delete);
935            if (cond)
936                    ccs_put_condition(cond);
937            return error;
938    }
939    
940    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
941            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
942            [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",
943            [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]
944            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",
945            [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]
946            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",
947            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
948    };
949            
950  /**  /**
951   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
952   *   *
953   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
954   *   *
955   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
956   */   */
957  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
958  {  {
959          char *data = head->write_buf;          char *data = head->write_buf;
960          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->write_var1;
961          bool is_delete = false;          bool is_delete = false;
962          bool is_select = false;          bool is_select = false;
963          unsigned int profile;          unsigned int profile;
964          struct ccs_condition *cond = NULL;          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
         char *cp;  
         int error;  
         if (ccs_str_starts(&data, KEYWORD_DELETE))  
965                  is_delete = true;                  is_delete = true;
966          else if (ccs_str_starts(&data, KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
967                  is_select = true;                  is_select = true;
968          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
969                  return 0;                  return 0;
970          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
971          if (!ccs_is_policy_manager())          if (!ccs_manager())
972                  return -EPERM;                  return -EPERM;
973          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
974                  domain = NULL;                  domain = NULL;
975                  if (is_delete)                  if (is_delete)
976                          ccs_delete_domain(data);                          ccs_delete_domain(data);
977                  else if (is_select)                  else if (is_select)
978                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
979                  else                  else
980                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
981                  head->write_var1 = domain;                  head->write_var1 = domain;
982                  return 0;                  return 0;
983          }          }
984          if (!domain)          if (!domain)
985                  return -EINVAL;                  return -EINVAL;
986    
987          if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
988              && profile < MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
989                  if (ccs_profile_ptr[profile] || !ccs_policy_loaded)                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
990                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
991                  return 0;                  return 0;
992          }          }
993          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
994                  domain->ignore_global_allow_read = !is_delete;                  const char *cp = ccs_dif[profile];
995                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
996          }                          continue;
997          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {                  domain->flags[profile] = !is_delete;
                 domain->ignore_global_allow_env = !is_delete;  
998                  return 0;                  return 0;
999          }          }
1000          cp = ccs_find_condition_part(data);          return ccs_write_domain2(data, domain, is_delete);
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         if (ccs_str_starts(&data, KEYWORD_ALLOW_CAPABILITY))  
                 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);  
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
 }  
   
 static bool ccs_print_name_union(struct ccs_io_buffer *head, bool is_group,  
                                  union ccs_name_union *group)  
 {  
         const int pos = head->read_avail;  
         if (pos && head->read_buf[pos - 1] == ' ')  
                 head->read_avail--;  
         if (is_group)  
                 return ccs_io_printf(head, " @%s", group->group->group_name->name);  
         return ccs_io_printf(head, " %s", group->filename->name);  
 }  
   
 static bool ccs_print_number_union(struct ccs_io_buffer *head, bool is_group,  
                                    union ccs_number_union *group)  
 {  
         unsigned int min;  
         unsigned int max;  
         if (is_group)  
                 return ccs_io_printf(head, " @%s", group->group->group_name->name);  
         min = group->value.min;  
         max = group->value.max;  
         if (min == max)  
                 return ccs_io_printf(head, " %u", min);  
         return ccs_io_printf(head, " %u-%u", min, max);  
1001  }  }
1002    
1003  /**  /**
1004   * ccs_print_single_path_acl - Print a single path ACL entry.   * ccs_print_name_union - Print a ccs_name_union.
1005   *   *
1006   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1007   * @ptr:  Pointer to "struct ccs_single_path_acl_record".   * @ptr:  Pointer to "struct ccs_name_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1008   *   *
1009   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1010   */   */
1011  static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_name_union(struct ccs_io_buffer *head,
1012                                        struct ccs_single_path_acl_record *ptr,                                   const struct ccs_name_union *ptr)
                                       const struct ccs_condition *cond)  
1013  {  {
1014          int pos;          int pos = head->read_avail;
1015          u8 bit;          if (pos && head->read_buf[pos - 1] == ' ')
1016          const u16 perm = ptr->perm;                  head->read_avail--;
1017          for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {          if (ptr->is_group)
1018                  const char *msg;                  return ccs_io_printf(head, " @%s",
1019                  if (!(perm & (1 << bit)))                                       ptr->group->group_name->name);
1020                          continue;          return ccs_io_printf(head, " %s", ptr->filename->name);
                 if (head->read_execute_only && bit != TYPE_EXECUTE_ACL)  
                         continue;  
                 /* Print "read/write" instead of "read" and "write". */  
                 if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)  
                     && (perm & (1 << TYPE_READ_WRITE_ACL)))  
                         continue;  
                 msg = ccs_sp2keyword(bit);  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", msg) ||  
                     !ccs_print_name_union(head, ptr->name_is_group,  
                                           &ptr->name) ||  
                     !ccs_print_condition(head, cond))  
                         goto out;  
         }  
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1021  }  }
1022    
1023  /**  /**
1024   * ccs_print_mkdev_acl - Print a mkdev ACL entry.   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.
1025   *   *
1026   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1027   * @ptr:  Pointer to "struct ccs_mkdev_acl_record".   * @ptr:  Pointer to "struct ccs_name_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1028   *   *
1029   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1030   */   */
1031  static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,  static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1032                                  struct ccs_mkdev_acl_record *ptr,                                          const struct ccs_name_union *ptr)
                                 const struct ccs_condition *cond)  
1033  {  {
1034          int pos;          if (ptr->is_group)
1035          u8 bit;                  return ccs_io_printf(head, "@%s",
1036          const u16 perm = ptr->perm;                                       ptr->group->group_name->name);
1037          for (bit = head->read_bit; bit < MAX_MKDEV_OPERATION; bit++) {          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1038                  const char *msg;  }
1039                  if (!(perm & (1 << bit)))  
1040                          continue;  static void ccs_print_number(char *buffer, int buffer_len,
1041                  msg = ccs_mkdev2keyword(bit);                               const struct ccs_number_union *ptr)
1042                  pos = head->read_avail;  {
1043                  if (!ccs_io_printf(head, "allow_%s", msg) ||          int i;
1044                      !ccs_print_name_union(head, ptr->name_is_group,          unsigned long min = ptr->values[0];
1045                                            &ptr->name) ||          const unsigned long max = ptr->values[1];
1046                      !ccs_print_number_union(head, ptr->major_is_group,          u8 min_type = ptr->value_type[0];
1047                                              &ptr->major) ||          const u8 max_type = ptr->value_type[1];
1048                      !ccs_print_number_union(head, ptr->minor_is_group,          memset(buffer, 0, buffer_len);
1049                                              &ptr->minor) ||          buffer_len -= 2;
1050                      !ccs_print_condition(head, cond))          for (i = 0; i < 2; i++) {
1051                          goto out;                  int len;
1052                    switch (min_type) {
1053                    case CCS_VALUE_TYPE_HEXADECIMAL:
1054                            snprintf(buffer, buffer_len, "0x%lX", min);
1055                            break;
1056                    case CCS_VALUE_TYPE_OCTAL:
1057                            snprintf(buffer, buffer_len, "0%lo", min);
1058                            break;
1059                    default:
1060                            snprintf(buffer, buffer_len, "%lu", min);
1061                            break;
1062                    }
1063                    if (min == max && min_type == max_type)
1064                            break;
1065                    len = strlen(buffer);
1066                    buffer[len++] = '-';
1067                    buffer += len;
1068                    buffer_len -= len;
1069                    min_type = max_type;
1070                    min = max;
1071          }          }
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1072  }  }
1073    
1074  /**  /**
1075   * ccs_print_double_path_acl - Print a double path ACL entry.   * ccs_print_number_union_common - Print a ccs_number_union.
1076   *   *
1077   * @head: Pointer to "struct ccs_io_buffer".   * @head:       Pointer to "struct ccs_io_buffer".
1078   * @ptr:  Pointer to "struct ccs_double_path_acl_record".   * @ptr:        Pointer to "struct ccs_number_union".
1079   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @need_space: True if a space character is needed.
1080   *   *
1081   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1082   */   */
1083  static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
1084                                        struct ccs_double_path_acl_record *ptr,                                            const struct ccs_number_union *ptr,
1085                                        const struct ccs_condition *cond)                                            const bool need_space)
1086  {  {
1087          int pos;          char buffer[128];
1088          u8 bit;          if (need_space && !ccs_io_printf(head, " "))
1089          const u8 perm = ptr->perm;                  return false;
1090          for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {          if (ptr->is_group)
1091                  const char *msg;                  return ccs_io_printf(head, "@%s",
1092                  if (!(perm & (1 << bit)))                                       ptr->group->group_name->name);
1093                          continue;          ccs_print_number(buffer, sizeof(buffer), ptr);
1094                  msg = ccs_dp2keyword(bit);          return ccs_io_printf(head, "%s", buffer);
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", msg) ||  
                     !ccs_print_name_union(head, ptr->name1_is_group,  
                                           &ptr->name1) ||  
                     !ccs_print_name_union(head, ptr->name2_is_group,  
                                           &ptr->name2) ||  
                     !ccs_print_condition(head, cond))  
                         goto out;  
         }  
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1095  }  }
1096    
1097  /**  /**
1098   * ccs_print_ioctl_acl - Print an ioctl ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1099   *   *
1100   * @head: Pointer to "struct ccs_io_buffer".   * @head:       Pointer to "struct ccs_io_buffer".
1101   * @ptr:  Pointer to "struct ccs_ioctl_acl_record".   * @ptr:        Pointer to "struct ccs_number_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1102   *   *
1103   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1104   */   */
1105  static bool ccs_print_ioctl_acl(struct ccs_io_buffer *head,  static bool ccs_print_number_union(struct ccs_io_buffer *head,
1106                                  struct ccs_ioctl_acl_record *ptr,                                     const struct ccs_number_union *ptr)
                                 const struct ccs_condition *cond)  
1107  {  {
1108          int pos = head->read_avail;          return ccs_print_number_union_common(head, ptr, true);
         if (!ccs_io_printf(head, KEYWORD_ALLOW_IOCTL) ||  
             !ccs_print_name_union(head, ptr->name_is_group, &ptr->name) ||  
             !ccs_print_number_union(head, ptr->cmd_is_group, &ptr->cmd) ||  
             !ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
1109  }  }
1110    
1111  /**  /**
1112   * ccs_print_argv0_acl - Print an argv[0] ACL entry.   * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.
1113   *   *
1114   * @head: Pointer to "struct ccs_io_buffer".   * @head:       Pointer to "struct ccs_io_buffer".
1115   * @ptr:  Pointer to "struct ccs_argv0_acl_record".   * @ptr:        Pointer to "struct ccs_number_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1116   *   *
1117   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1118   */   */
1119  static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1120                                  struct ccs_argv0_acl_record *ptr,                                             const struct ccs_number_union *ptr)
                                 const struct ccs_condition *cond)  
1121  {  {
1122          int pos = head->read_avail;          return ccs_print_number_union_common(head, ptr, false);
         if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",  
                            ptr->filename->name, ptr->argv0->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
1123  }  }
1124    
1125  /**  /**
1126   * ccs_print_env_acl - Print an evironment variable name's ACL entry.   * ccs_print_condition - Print condition part.
1127   *   *
1128   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1129   * @ptr:  Pointer to "struct ccs_env_acl_record".   * @cond: Pointer to "struct ccs_condition". Maybe NULL.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1130   *   *
1131   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1132   */   */
1133  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1134                                struct ccs_env_acl_record *ptr,                                  const struct ccs_condition *cond)
                               const struct ccs_condition *cond)  
1135  {  {
1136          int pos = head->read_avail;          const struct ccs_condition_element *condp;
1137          if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))          const struct ccs_number_union *numbers_p;
1138                  goto out;          const struct ccs_name_union *names_p;
1139          if (!ccs_print_condition(head, cond))          const struct ccs_argv *argv;
1140            const struct ccs_envp *envp;
1141            u16 condc;
1142            u16 i;
1143            u16 j;
1144            char buffer[32];
1145            if (!cond)
1146                    goto no_condition;
1147            condc = cond->condc;
1148            condp = (const struct ccs_condition_element *) (cond + 1);
1149            numbers_p = (const struct ccs_number_union *) (condp + condc);
1150            names_p = (const struct ccs_name_union *)
1151                    (numbers_p + cond->numbers_count);
1152            argv = (const struct ccs_argv *) (names_p + cond->names_count);
1153            envp = (const struct ccs_envp *) (argv + cond->argc);
1154            memset(buffer, 0, sizeof(buffer));
1155            if (condc && !ccs_io_printf(head, "%s", " if"))
1156                    goto out;
1157            for (i = 0; i < condc; i++) {
1158                    const u8 match = condp->equals;
1159                    const u8 left = condp->left;
1160                    const u8 right = condp->right;
1161                    condp++;
1162                    switch (left) {
1163                    case CCS_ARGV_ENTRY:
1164                            if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",
1165                                               argv->index, argv->is_not ?
1166                                               "!=" : "=", argv->value->name))
1167                                    goto out;
1168                            argv++;
1169                            continue;
1170                    case CCS_ENVP_ENTRY:
1171                            if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",
1172                                               envp->name->name, envp->is_not ?
1173                                               "!=" : "="))
1174                                    goto out;
1175                            if (envp->value) {
1176                                    if (!ccs_io_printf(head, "\"%s\"",
1177                                                       envp->value->name))
1178                                            goto out;
1179                            } else {
1180                                    if (!ccs_io_printf(head, "NULL"))
1181                                            goto out;
1182                            }
1183                            envp++;
1184                            continue;
1185                    case CCS_NUMBER_UNION:
1186                            if (!ccs_print_number_union(head, numbers_p++))
1187                                    goto out;
1188                            break;
1189                    default:
1190                            if (left >= CCS_MAX_CONDITION_KEYWORD)
1191                                    goto out;
1192                            if (!ccs_io_printf(head, " %s",
1193                                               ccs_condition_keyword[left]))
1194                                    goto out;
1195                            break;
1196                    }
1197                    if (!ccs_io_printf(head, "%s", match ? "=" : "!="))
1198                            goto out;
1199                    switch (right) {
1200                    case CCS_NAME_UNION:
1201                            if (!ccs_print_name_union_quoted(head, names_p++))
1202                                    goto out;
1203                            break;
1204                    case CCS_NUMBER_UNION:
1205                            if (!ccs_print_number_union_nospace(head, numbers_p++))
1206                                    goto out;
1207                            break;
1208                    default:
1209                            if (right >= CCS_MAX_CONDITION_KEYWORD)
1210                                    goto out;
1211                            if (!ccs_io_printf(head, "%s",
1212                                               ccs_condition_keyword[right]))
1213                                    goto out;
1214                            break;
1215                    }
1216            }
1217            i = cond->post_state[3];
1218            if (!i)
1219                    goto no_condition;
1220            if (!ccs_io_printf(head, " ; set"))
1221                  goto out;                  goto out;
1222          return true;          for (j = 0; j < 3; j++) {
1223                    if (!(i & (1 << j)))
1224                            continue;
1225                    if (!ccs_io_printf(head, " task.state[%u]=%u", j,
1226                                       cond->post_state[j]))
1227                            goto out;
1228            }
1229            if (i & (1 << 4)) {
1230                    if (!ccs_io_printf(head, " audit=%s",
1231                                       ccs_yesno(cond->post_state[4])))
1232                            goto out;
1233            }
1234     no_condition:
1235            if (ccs_io_printf(head, "\n"))
1236                    return true;
1237   out:   out:
         head->read_avail = pos;  
1238          return false;          return false;
1239  }  }
1240    
1241  /**  /**
1242   * ccs_print_capability_acl - Print a capability ACL entry.   * ccs_print_path_acl - Print a path ACL entry.
1243   *   *
1244   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1245   * @ptr:  Pointer to "struct ccs_capability_acl_record".   * @ptr:  Pointer to "struct ccs_path_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1246   *   *
1247   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1248   */   */
1249  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1250                                       struct ccs_capability_acl_record *ptr,                                 const struct ccs_path_acl *ptr)
                                      const struct ccs_condition *cond)  
1251  {  {
1252          int pos = head->read_avail;          int pos;
1253          if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",          u8 bit;
1254                             ccs_cap2keyword(ptr->operation)))          const u16 perm = ptr->perm;
1255                  goto out;          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
1256          if (!ccs_print_condition(head, cond))                  if (!(perm & (1 << bit)))
1257                  goto out;                          continue;
1258                    if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1259                        && bit != CCS_TYPE_TRANSIT)
1260                            continue;
1261                    /* Print "read/write" instead of "read" and "write". */
1262                    if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1263                        && (perm & (1 << CCS_TYPE_READ_WRITE)))
1264                            continue;
1265                    pos = head->read_avail;
1266                    if (!ccs_io_printf(head, "allow_%s", ccs_path_keyword[bit]) ||
1267                        !ccs_print_name_union(head, &ptr->name) ||
1268                        !ccs_print_condition(head, ptr->head.cond)) {
1269                            head->read_bit = bit;
1270                            head->read_avail = pos;
1271                            return false;
1272                    }
1273            }
1274            head->read_bit = 0;
1275          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1276  }  }
1277    
1278  /**  /**
1279   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.   * ccs_print_mkdev_acl - Print a mkdev ACL entry.
1280   *   *
1281   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1282   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_mkdev_acl".
1283   *   *
1284   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1285   */   */
1286  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,
1287                                   struct ccs_ip_network_acl_record *ptr)                                  const struct ccs_mkdev_acl *ptr)
1288  {  {
1289          const u32 min_address = ptr->address.ipv4.min;          int pos;
1290          const u32 max_address = ptr->address.ipv4.max;          u8 bit;
1291          if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))          const u16 perm = ptr->perm;
1292                  return false;          for (bit = head->read_bit; bit < CCS_MAX_MKDEV_OPERATION;
1293          if (min_address != max_address               bit++) {
1294              && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))                  if (!(perm & (1 << bit)))
1295                  return false;                          continue;
1296                    pos = head->read_avail;
1297                    if (!ccs_io_printf(head, "allow_%s", ccs_mkdev_keyword[bit]) ||
1298                        !ccs_print_name_union(head, &ptr->name) ||
1299                        !ccs_print_number_union(head, &ptr->mode) ||
1300                        !ccs_print_number_union(head, &ptr->major) ||
1301                        !ccs_print_number_union(head, &ptr->minor) ||
1302                        !ccs_print_condition(head, ptr->head.cond)) {
1303                            head->read_bit = bit;
1304                            head->read_avail = pos;
1305                            return false;
1306                    }
1307            }
1308            head->read_bit = 0;
1309          return true;          return true;
1310  }  }
1311    
1312  /**  /**
1313   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_print_path2_acl - Print a path2 ACL entry.
1314   *   *
1315   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1316   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_path2_acl".
1317   *   *
1318   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1319   */   */
1320  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1321                                   struct ccs_ip_network_acl_record *ptr)                                  const struct ccs_path2_acl *ptr)
1322  {  {
1323          char buf[64];          int pos;
1324          const struct in6_addr *min_address = ptr->address.ipv6.min;          u8 bit;
1325          const struct in6_addr *max_address = ptr->address.ipv6.max;          const u8 perm = ptr->perm;
1326          ccs_print_ipv6(buf, sizeof(buf), min_address);          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1327          if (!ccs_io_printf(head, "%s", buf))                  if (!(perm & (1 << bit)))
1328                  return false;                          continue;
1329          if (min_address != max_address) {                  pos = head->read_avail;
1330                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  if (!ccs_io_printf(head, "allow_%s", ccs_path2_keyword[bit]) ||
1331                  if (!ccs_io_printf(head, "-%s", buf))                      !ccs_print_name_union(head, &ptr->name1) ||
1332                        !ccs_print_name_union(head, &ptr->name2) ||
1333                        !ccs_print_condition(head, ptr->head.cond)) {
1334                            head->read_bit = bit;
1335                            head->read_avail = pos;
1336                          return false;                          return false;
1337                    }
1338          }          }
1339            head->read_bit = 0;
1340          return true;          return true;
1341  }  }
1342    
1343  /**  /**
1344   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_path_number_acl - Print a path_number ACL entry.
1345   *   *
1346   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1347   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @ptr:  Pointer to "struct ccs_path_number_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1348   *   *
1349   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1350   */   */
1351  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1352                                    struct ccs_ip_network_acl_record *ptr,                                        const struct ccs_path_number_acl *ptr)
                                   const struct ccs_condition *cond)  
1353  {  {
1354          int pos = head->read_avail;          int pos;
1355          if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",          u8 bit;
1356                             ccs_net2keyword(ptr->operation_type)))          const u8 perm = ptr->perm;
1357                  goto out;          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1358          switch (ptr->record_type) {               bit++) {
1359          case IP_RECORD_TYPE_ADDRESS_GROUP:                  if (!(perm & (1 << bit)))
1360                  if (!ccs_io_printf(head, "@%s",                          continue;
1361                                     ptr->address.group->group_name->name))                  pos = head->read_avail;
1362                          goto out;                  if (!ccs_io_printf(head, "allow_%s",
1363                  break;                                     ccs_path_number_keyword[bit]) ||
1364          case IP_RECORD_TYPE_IPv4:                      !ccs_print_name_union(head, &ptr->name) ||
1365                  if (!ccs_print_ipv4_entry(head, ptr))                      !ccs_print_number_union(head, &ptr->number) ||
1366                          goto out;                      !ccs_print_condition(head, ptr->head.cond)) {
1367                  break;                          head->read_bit = bit;
1368          case IP_RECORD_TYPE_IPv6:                          head->read_avail = pos;
1369                  if (!ccs_print_ipv6_entry(head, ptr))                          return false;
1370                          goto out;                  }
                 break;  
1371          }          }
1372          if (!ccs_print_number_union(head, ptr->port_is_group, &ptr->port) ||          head->read_bit = 0;
             !ccs_print_condition(head, cond))  
                 goto out;  
1373          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1374  }  }
1375    
1376  /**  /**
1377   * ccs_print_signal_acl - Print a signal ACL entry.   * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1378   *   *
1379   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1380   * @ptr:  Pointer to "struct signale_acl_record".   * @ptr:  Pointer to "struct ccs_env_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1381   *   *
1382   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1383   */   */
1384  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1385                                   struct ccs_signal_acl_record *ptr,                                const struct ccs_env_acl *ptr)
                                  const struct ccs_condition *cond)  
1386  {  {
1387          int pos = head->read_avail;          const int pos = head->read_avail;
1388          if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1389                             ptr->sig, ptr->domainname->name))              !ccs_print_condition(head, ptr->head.cond)) {
1390                  goto out;                  head->read_avail = pos;
1391          if (!ccs_print_condition(head, cond))                  return false;
1392                  goto out;          }
1393          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1394  }  }
1395    
1396  /**  /**
1397   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_print_capability_acl - Print a capability ACL entry.
1398   *   *
1399   * @head:    Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1400   * @keyword: Name of the keyword.   * @ptr:  Pointer to "struct ccs_capability_acl".
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
1401   *   *
1402   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1403   */   */
1404  static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1405                                               const char *keyword,                                       const struct ccs_capability_acl *ptr)
                                              struct ccs_execute_handler_record *  
                                              ptr)  
1406  {  {
1407          return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);          const int pos = head->read_avail;
1408            if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1409                               ccs_cap2keyword(ptr->operation)) ||
1410                !ccs_print_condition(head, ptr->head.cond)) {
1411                    head->read_avail = pos;
1412                    return false;
1413            }
1414            return true;
1415  }  }
1416    
1417  /**  /**
1418   * ccs_print_mount_acl - Print a mount ACL entry.   * ccs_print_network_acl - Print a network ACL entry.
1419   *   *
1420   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1421   * @ptr:  Pointer to "struct ccs_mount_acl_record".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1422   *   *
1423   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1424   */   */
1425  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1426                                  struct ccs_mount_acl_record *ptr,                                    const struct ccs_ip_network_acl *ptr)
                                 const struct ccs_condition *cond)  
1427  {  {
1428          int pos = head->read_avail;          int pos;
1429          if (!ccs_io_printf(head, KEYWORD_ALLOW_MOUNT "%s %s %s 0x%lX\n",          u8 bit;
1430                             ptr->dev_name->name, ptr->dir_name->name,          const u8 perm = ptr->perm;
1431                             ptr->fs_type->name, ptr->flags))          char buf[128];
1432                  goto out;          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1433          if (!ccs_print_condition(head, cond))                  const char *w[2] = { buf, "" };
1434                  goto out;                  if (!(perm & (1 << bit)))
1435                            continue;
1436                    pos = head->read_avail;
1437                    switch (ptr->address_type) {
1438                    case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1439                            w[0] = "@";
1440                            w[1] = ptr->address.group->group_name->name;
1441                            break;
1442                    case CCS_IP_ADDRESS_TYPE_IPv4:
1443                            ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1444                                           ptr->address.ipv4.max);
1445                            break;
1446                    case CCS_IP_ADDRESS_TYPE_IPv6:
1447                            ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1448                                           ptr->address.ipv6.max);
1449                            break;
1450                    }
1451                    if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1452                                       ccs_net_keyword[bit], w[0], w[1]) ||
1453                        !ccs_print_number_union(head, &ptr->port) ||
1454                        !ccs_print_condition(head, ptr->head.cond))
1455                            goto out;
1456            }
1457            head->read_bit = 0;
1458          return true;          return true;
1459   out:   out:
1460            head->read_bit = bit;
1461          head->read_avail = pos;          head->read_avail = pos;
1462          return false;          return false;
1463  }  }
1464    
1465  /**  /**
1466   * ccs_print_umount_acl - Print a mount ACL entry.   * ccs_print_signal_acl - Print a signal ACL entry.
1467   *   *
1468   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1469   * @ptr:  Pointer to "struct ccs_umount_acl_record".   * @ptr:  Pointer to "struct signale_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1470   *   *
1471   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1472   */   */
1473  static bool ccs_print_umount_acl(struct ccs_io_buffer *head,  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1474                                   struct ccs_umount_acl_record *ptr,                                   const struct ccs_signal_acl *ptr)
                                  const struct ccs_condition *cond)  
1475  {  {
1476          int pos = head->read_avail;          const int pos = head->read_avail;
1477          if (!ccs_io_printf(head, KEYWORD_ALLOW_UNMOUNT "%s\n",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1478                             ptr->dir->name))                             ptr->sig, ptr->domainname->name) ||
1479                  goto out;              !ccs_print_condition(head, ptr->head.cond)) {
1480          if (!ccs_print_condition(head, cond))                  head->read_avail = pos;
1481                  goto out;                  return false;
1482            }
1483          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1484  }  }
1485    
1486  /**  /**
1487   * ccs_print_chroot_acl - Print a chroot ACL entry.   * ccs_print_execute_handler - Print an execute handler ACL entry.
1488   *   *
1489   * @head: Pointer to "struct ccs_io_buffer".   * @head:    Pointer to "struct ccs_io_buffer".
1490   * @ptr:  Pointer to "struct ccs_chroot_acl_record".   * @keyword: Name of the keyword.
1491   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @ptr:     Pointer to "struct ccs_execute_handler".
1492   *   *
1493   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1494   */   */
1495  static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,  static bool ccs_print_execute_handler(struct ccs_io_buffer *head,
1496                                   struct ccs_chroot_acl_record *ptr,                                        const char *keyword,
1497                                   const struct ccs_condition *cond)                                        const struct ccs_execute_handler *ptr)
1498  {  {
1499          int pos = head->read_avail;          const int pos = head->read_avail;
1500          if (!ccs_io_printf(head, KEYWORD_ALLOW_CHROOT "%s\n",          if (!ccs_io_printf(head, "%s %s", keyword, ptr->handler->name) ||
1501                             ptr->dir->name))              !ccs_print_condition(head, ptr->head.cond)) {
1502                  goto out;                  head->read_avail = pos;
1503          if (!ccs_print_condition(head, cond))                  return false;
1504                  goto out;          }
1505          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1506  }  }
1507    
1508  /**  /**
1509   * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.   * ccs_print_mount_acl - Print a mount ACL entry.
1510   *   *
1511   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1512   * @ptr:  Pointer to "struct ccs_pivot_root_acl_record".   * @ptr:  Pointer to "struct ccs_mount_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1513   *   *
1514   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1515   */   */
1516  static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1517                                       struct ccs_pivot_root_acl_record *ptr,                                  const struct ccs_mount_acl *ptr)
                                      const struct ccs_condition *cond)  
1518  {  {
1519          int pos = head->read_avail;          const int pos = head->read_avail;
1520          if (!ccs_io_printf(head, KEYWORD_ALLOW_PIVOT_ROOT "%s %s\n",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1521                             ptr->new_root->name, ptr->old_root->name))              !ccs_print_name_union(head, &ptr->dev_name) ||
1522                  goto out;              !ccs_print_name_union(head, &ptr->dir_name) ||
1523          if (!ccs_print_condition(head, cond))              !ccs_print_name_union(head, &ptr->fs_type) ||
1524                  goto out;              !ccs_print_number_union(head, &ptr->flags) ||
1525                !ccs_print_condition(head, ptr->head.cond)) {
1526                    head->read_avail = pos;
1527                    return false;
1528            }
1529          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1530  }  }
1531    
1532  /**  /**
# Line 1120  static bool ccs_print_pivot_root_acl(str Line 1538  static bool ccs_print_pivot_root_acl(str
1538   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1539   */   */
1540  static bool ccs_print_entry(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1541                              struct ccs_acl_info *ptr)                              const struct ccs_acl_info *ptr)
1542  {  {
1543          const struct ccs_condition *cond = ptr->cond;          const u8 acl_type = ptr->type;
1544          const u8 acl_type = ccs_acl_type2(ptr);          if (ptr->is_deleted)
         if (acl_type & ACL_DELETED)  
1545                  return true;                  return true;
1546          if (acl_type == TYPE_SINGLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH_ACL) {
1547                  struct ccs_single_path_acl_record *acl                  struct ccs_path_acl *acl
1548                          = container_of(ptr, struct ccs_single_path_acl_record,                          = container_of(ptr, typeof(*acl), head);
1549                                         head);                  return ccs_print_path_acl(head, acl);
1550                  return ccs_print_single_path_acl(head, acl, cond);          }
1551          }          if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1552          if (acl_type == TYPE_EXECUTE_HANDLER) {              acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1553                  struct ccs_execute_handler_record *acl                  struct ccs_execute_handler *acl
1554                          = container_of(ptr, struct ccs_execute_handler_record,                          = container_of(ptr, typeof(*acl), head);
1555                                         head);                  const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1556                  const char *keyword = KEYWORD_EXECUTE_HANDLER;                          CCS_KEYWORD_EXECUTE_HANDLER :
1557                  return ccs_print_execute_handler_record(head, keyword, acl);                          CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1558          }                  return ccs_print_execute_handler(head, keyword, acl);
         if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
1559          }          }
1560          if (head->read_execute_only)          if (head->read_execute_only)
1561                  return true;                  return true;
1562          if (acl_type == TYPE_MKDEV_ACL) {          if (acl_type == CCS_TYPE_MKDEV_ACL) {
1563                  struct ccs_mkdev_acl_record *acl                  struct ccs_mkdev_acl *acl
1564                          = container_of(ptr, struct ccs_mkdev_acl_record, head);                          = container_of(ptr, typeof(*acl), head);
1565                  return ccs_print_mkdev_acl(head, acl, cond);                  return ccs_print_mkdev_acl(head, acl);
1566          }          }
1567          if (acl_type == TYPE_DOUBLE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH2_ACL) {
1568                  struct ccs_double_path_acl_record *acl                  struct ccs_path2_acl *acl
1569                          = container_of(ptr, struct ccs_double_path_acl_record,                          = container_of(ptr, typeof(*acl), head);
1570                                         head);                  return ccs_print_path2_acl(head, acl);
1571                  return ccs_print_double_path_acl(head, acl, cond);          }
1572          }          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1573          if (acl_type == TYPE_IOCTL_ACL) {                  struct ccs_path_number_acl *acl
1574                  struct ccs_ioctl_acl_record *acl                          = container_of(ptr, typeof(*acl), head);
1575                          = container_of(ptr, struct ccs_ioctl_acl_record, head);                  return ccs_print_path_number_acl(head, acl);
1576                  return ccs_print_ioctl_acl(head, acl, cond);          }
1577          }          if (acl_type == CCS_TYPE_ENV_ACL) {
1578          if (acl_type == TYPE_ARGV0_ACL) {                  struct ccs_env_acl *acl
1579                  struct ccs_argv0_acl_record *acl                          = container_of(ptr, typeof(*acl), head);
1580                          = container_of(ptr, struct ccs_argv0_acl_record, head);                  return ccs_print_env_acl(head, acl);
1581                  return ccs_print_argv0_acl(head, acl, cond);          }
1582          }          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1583          if (acl_type == TYPE_ENV_ACL) {                  struct ccs_capability_acl *acl
1584                  struct ccs_env_acl_record *acl                          = container_of(ptr, typeof(*acl), head);
1585                          = container_of(ptr, struct ccs_env_acl_record, head);                  return ccs_print_capability_acl(head, acl);
1586                  return ccs_print_env_acl(head, acl, cond);          }
1587          }          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1588          if (acl_type == TYPE_CAPABILITY_ACL) {                  struct ccs_ip_network_acl *acl
1589                  struct ccs_capability_acl_record *acl                          = container_of(ptr, typeof(*acl), head);
1590                          = container_of(ptr, struct ccs_capability_acl_record,                  return ccs_print_network_acl(head, acl);
1591                                         head);          }
1592                  return ccs_print_capability_acl(head, acl, cond);          if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1593          }                  struct ccs_signal_acl *acl
1594          if (acl_type == TYPE_IP_NETWORK_ACL) {                          = container_of(ptr, typeof(*acl), head);
1595                  struct ccs_ip_network_acl_record *acl                  return ccs_print_signal_acl(head, acl);
1596                          = container_of(ptr, struct ccs_ip_network_acl_record,          }
1597                                         head);          if (acl_type == CCS_TYPE_MOUNT_ACL) {
1598                  return ccs_print_network_acl(head, acl, cond);                  struct ccs_mount_acl *acl
1599          }                          = container_of(ptr, typeof(*acl), head);
1600          if (acl_type == TYPE_SIGNAL_ACL) {                  return ccs_print_mount_acl(head, acl);
                 struct ccs_signal_acl_record *acl  
                         = container_of(ptr, struct ccs_signal_acl_record, head);  
                 return ccs_print_signal_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl_record *acl  
                         = container_of(ptr, struct ccs_mount_acl_record, head);  
                 return ccs_print_mount_acl(head, acl, cond);  
         }  
         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);  
1601          }          }
         /* Workaround for gcc 3.2.2's inline bug. */  
         if (acl_type & ACL_DELETED)  
                 return true;  
1602          BUG(); /* This must not happen. */          BUG(); /* This must not happen. */
1603          return false;          return false;
1604  }  }
1605    
1606  /**  /**
1607   * ccs_read_domain_policy - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1608   *   *
1609   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1610     * @domain: Pointer to "struct ccs_domain_info".
1611   *   *
1612   * Returns 0.   * Caller holds ccs_read_lock().
1613     *
1614     * Returns true on success, false otherwise.
1615     */
1616    static bool ccs_read_domain2(struct ccs_io_buffer *head,
1617                                 struct ccs_domain_info *domain)
1618    {
1619            struct list_head *pos;
1620            /* Print ACL entries in the domain. */
1621            list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {
1622                    struct ccs_acl_info *ptr
1623                            = list_entry(pos, struct ccs_acl_info, list);
1624                    if (!ccs_print_entry(head, ptr))
1625                            return false;
1626            }
1627            return true;
1628    }
1629    
1630    /**
1631     * ccs_read_domain - Read domain policy.
1632     *
1633     * @head: Pointer to "struct ccs_io_buffer".
1634   *   *
1635   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1636   */   */
1637  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1638  {  {
1639          struct list_head *dpos;          struct list_head *pos;
         struct list_head *apos;  
         ccs_check_read_lock();  
1640          if (head->read_eof)          if (head->read_eof)
1641                  return 0;                  return;
1642          if (head->read_step == 0)          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1643                  head->read_step = 1;                  struct ccs_domain_info *domain =
1644          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(pos, struct ccs_domain_info, list);
1645                  struct ccs_domain_info *domain;                  switch (head->read_step) {
1646                  const char *quota_exceeded = "";                          u8 i;
1647                  const char *transition_failed = "";                          const char *w[CCS_MAX_DOMAIN_INFO_FLAGS];
1648                  const char *ignore_global_allow_read = "";                  case 0:
1649                  const char *ignore_global_allow_env = "";                          if (domain->is_deleted && !head->read_single_domain)
1650                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1651                  if (head->read_step != 1)                          /* Print domainname and flags. */
1652                          goto acl_loop;                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1653                  if (domain->is_deleted && !head->read_single_domain)                                  w[i] = domain->flags[i] ? ccs_dif[i] : "";
1654                          continue;                          if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE
1655                  /* Print domainname and flags. */                                             "%u\n%s%s%s%s%s\n",
1656                  if (domain->quota_warned)                                             domain->domainname->name,
1657                          quota_exceeded = "quota_exceeded\n";                                             domain->profile,
1658                  if (domain->domain_transition_failed)                                             w[CCS_DIF_QUOTA_WARNED],
1659                          transition_failed = "transition_failed\n";                                             w[CCS_DIF_IGNORE_GLOBAL],
1660                  if (domain->ignore_global_allow_read)                                             w[CCS_DIF_IGNORE_GLOBAL_ALLOW_READ],
1661                          ignore_global_allow_read                                             w[CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV],
1662                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                             w[CCS_DIF_TRANSITION_FAILED]))
1663                  if (domain->ignore_global_allow_env)                                  return;
1664                          ignore_global_allow_env                          head->read_step++;
1665                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          /* fall through */
1666                  if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"                  case 1:
1667                                     "%s%s%s%s\n", domain->domainname->name,                          if (!ccs_read_domain2(head, domain))
1668                                     domain->profile, quota_exceeded,                                  return;
1669                                     transition_failed,                          head->read_step++;
1670                                     ignore_global_allow_read,                          /* fall through */
1671                                     ignore_global_allow_env))                  case 2:
1672                          return 0;                          if (!ccs_io_printf(head, "\n"))
1673                  head->read_step = 2;                                  return;
1674   acl_loop:                          head->read_step = 0;
1675                  if (head->read_step == 3)                          if (head->read_single_domain)
1676                          goto tail_mark;                                  goto out;
1677                  /* Print ACL entries in the domain. */                  }
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
                                 return 0;  
                 }  
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return 0;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1678          }          }
1679     out:
1680          head->read_eof = true;          head->read_eof = true;
         return 0;  
1681  }  }
1682    
1683  /**  /**
# Line 1300  static int ccs_read_domain_policy(struct Line 1690  static int ccs_read_domain_policy(struct
1690   * This is equivalent to doing   * This is equivalent to doing
1691   *   *
1692   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1693   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1694   *   *
1695   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1696   */   */
# Line 1310  static int ccs_write_domain_profile(stru Line 1700  static int ccs_write_domain_profile(stru
1700          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1701          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1702          unsigned int profile;          unsigned int profile;
         ccs_check_read_lock();  
1703          if (!cp)          if (!cp)
1704                  return -EINVAL;                  return -EINVAL;
1705          *cp = '\0';          *cp = '\0';
1706          profile = simple_strtoul(data, NULL, 10);          profile = simple_strtoul(data, NULL, 10);
1707          if (profile >= MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1708                  return -EINVAL;                  return -EINVAL;
1709          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1710          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1711                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1712          return 0;          return 0;
1713  }  }
# Line 1328  static int ccs_write_domain_profile(stru Line 1717  static int ccs_write_domain_profile(stru
1717   *   *
1718   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1719   *   *
  * Returns list of profile number and domainname pairs.  
  *  
1720   * This is equivalent to doing   * This is equivalent to doing
1721   *   *
1722   *     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 1726  static int ccs_write_domain_profile(stru
1726   *   *
1727   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1728   */   */
1729  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1730  {  {
1731          struct list_head *pos;          struct list_head *pos;
         ccs_check_read_lock();  
1732          if (head->read_eof)          if (head->read_eof)
1733                  return 0;                  return;
1734          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1735                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain;
1736                  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 1738  static int ccs_read_domain_profile(struc
1738                          continue;                          continue;
1739                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1740                                     domain->domainname->name))                                     domain->domainname->name))
1741                          return 0;                          return;
1742          }          }
1743          head->read_eof = true;          head->read_eof = true;
         return 0;  
1744  }  }
1745    
1746  /**  /**
# Line 1382  static int ccs_write_pid(struct ccs_io_b Line 1767  static int ccs_write_pid(struct ccs_io_b
1767   *   *
1768   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1769   */   */
1770  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1771  {  {
1772          char *buf = head->write_buf;          char *buf = head->write_buf;
1773          bool task_info = false;          bool task_info = false;
1774            bool global_pid = false;
1775          unsigned int pid;          unsigned int pid;
1776          struct task_struct *p;          struct task_struct *p;
1777          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1778          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_check_read_lock();  
1779          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1780          if (!buf)          if (!buf) {
1781                  goto done; /* Do nothing if open(O_RDONLY). */                  head->read_eof = true;
1782                    return; /* Do nothing if open(O_RDONLY). */
1783            }
1784          if (head->read_avail || head->read_eof)          if (head->read_avail || head->read_eof)
1785                  goto done;                  return;
1786          head->read_eof = true;          head->read_eof = true;
1787          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1788                  task_info = true;                  task_info = true;
1789            if (ccs_str_starts(&buf, "global-pid "))
1790                    global_pid = true;
1791          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1792          /***** CRITICAL SECTION START *****/          ccs_tasklist_lock();
1793          read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1794            if (global_pid)
1795                    p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1796            else
1797                    p = ccsecurity_exports.find_task_by_vpid(pid);
1798    #else
1799          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1800    #endif
1801          if (p) {          if (p) {
1802                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1803                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1804          }          }
1805          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
         /***** CRITICAL SECTION END *****/  
1806          if (!domain)          if (!domain)
1807                  goto done;                  return;
1808          if (!task_info)          if (!task_info)
1809                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1810                                domain->domainname->name);                                domain->domainname->name);
1811          else          else
1812                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1813                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1814                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                ccs_yesno(ccs_flags &
1815                                "yes" : "no",                                          CCS_TASK_IS_MANAGER),
1816                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                ccs_yesno(ccs_flags &
1817                                "yes" : "no",                                          CCS_TASK_IS_EXECUTE_HANDLER),
1818                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1819                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1820                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
  done:  
         return 0;  
1821  }  }
1822    
1823    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1824            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1825            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1826            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1827            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1828            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1829    };
1830    
1831    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1832            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1833            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1834            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1835    };
1836    
1837  /**  /**
1838   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1839   *   *
1840   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1841   *   *
1842   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1843   */   */
1844  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1845  {  {
1846          char *data = head->write_buf;          char *data = head->write_buf;
1847          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1848          if (ccs_str_starts(&data, KEYWORD_KEEP_DOMAIN))          u8 i;
1849                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1850          if (ccs_str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1851                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1852          if (ccs_str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[4] = {
1853                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1854                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1855          if (ccs_str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1856                  return ccs_write_domain_initializer_policy(data, true,                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1857                                                             is_delete);          };
1858          if (ccs_str_starts(&data, KEYWORD_AGGREGATOR))          for (i = 0; i < 4; i++) {
1859                  return ccs_write_aggregator_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1860          if (ccs_str_starts(&data, KEYWORD_ALLOW_READ))                          return ccs_callback[i].write(data, is_delete);
1861                  return ccs_write_globally_readable_policy(data, is_delete);          }
1862          if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) {
1863                  return ccs_write_globally_usable_env_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1864          if (ccs_str_starts(&data, KEYWORD_FILE_PATTERN))                          return ccs_write_transition_control(data, is_delete,
1865                  return ccs_write_pattern_policy(data, is_delete);                                                              i);
1866          if (ccs_str_starts(&data, KEYWORD_PATH_GROUP))          }
1867                  return ccs_write_path_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++) {
1868          if (ccs_str_starts(&data, KEYWORD_NUMBER_GROUP))                  if (ccs_str_starts(&data, ccs_group_name[i]))
1869                  return ccs_write_number_group_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1870          if (ccs_str_starts(&data, KEYWORD_DENY_REWRITE))          }
1871                  return ccs_write_no_rewrite_policy(data, is_delete);          return ccs_write_domain2(data, &ccs_global_domain, is_delete);
         if (ccs_str_starts(&data, KEYWORD_ADDRESS_GROUP))  
                 return ccs_write_address_group_policy(data, is_delete);  
         if (ccs_str_starts(&data, KEYWORD_DENY_AUTOBIND))  
                 return ccs_write_reserved_port_policy(data, is_delete);  
         return -EINVAL;  
1872  }  }
1873    
1874  /**  /**
1875   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1876   *   *
1877   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1878     * @idx:  Index number.
1879   *   *
1880   * Returns 0 on success, -EINVAL otherwise.   * Returns true on success, false otherwise.
1881   *   *
1882   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1883   */   */
1884  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1885  {  {
1886          ccs_check_read_lock();          struct list_head *gpos;
1887          if (!head->read_eof) {          struct list_head *mpos;
1888                  switch (head->read_step) {          const char *w[3] = { "", "", "" };
1889                  case 0:          w[0] = ccs_group_name[idx];
1890                          head->read_var2 = NULL;          list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1891                          head->read_step = 1;                  struct ccs_group *group =
1892                  case 1:                          list_entry(gpos, struct ccs_group, head.list);
1893                          if (!ccs_read_domain_keeper_policy(head))                  w[1] = group->group_name->name;
1894                                  break;                  list_for_each_cookie(mpos, head->read_var2,
1895                          head->read_var2 = NULL;                                       &group->member_list) {
1896                          head->read_step = 2;                          char buffer[128];
1897                  case 2:                          struct ccs_acl_head *ptr =
1898                          if (!ccs_read_globally_readable_policy(head))                                  list_entry(mpos, struct ccs_acl_head, list);
1899                                  break;                          if (ptr->is_deleted)
1900                          head->read_var2 = NULL;                                  continue;
1901                          head->read_step = 3;                          w[2] = buffer;
1902                  case 3:                          if (idx == CCS_PATH_GROUP) {
1903                          if (!ccs_read_globally_usable_env_policy(head))                                  w[2] = container_of(ptr, struct ccs_path_group,
1904                                  break;                                                      head)->member_name->name;
1905                          head->read_var2 = NULL;                          } else if (idx == CCS_NUMBER_GROUP) {
1906                          head->read_step = 4;                                  ccs_print_number(buffer, sizeof(buffer),
1907                  case 4:                                                   &container_of
1908                          if (!ccs_read_domain_initializer_policy(head))                                                   (ptr, struct ccs_number_group,
1909                                  break;                                                    head)->number);
1910                          head->read_var2 = NULL;                          } else if (idx == CCS_ADDRESS_GROUP) {
1911                          head->read_step = 6;                                  struct ccs_address_group *member =
1912                  case 6:                                          container_of(ptr, typeof(*member),
1913                          if (!ccs_read_aggregator_policy(head))                                                       head);
1914                                  break;                                  if (member->is_ipv6)
1915                          head->read_var2 = NULL;                                          ccs_print_ipv6(buffer, sizeof(buffer),
1916                          head->read_step = 7;                                                         member->min.ipv6,
1917                  case 7:                                                         member->max.ipv6);
1918                          if (!ccs_read_file_pattern(head))                                  else
1919                                  break;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1920                          head->read_var2 = NULL;                                                         member->min.ipv4,
1921                          head->read_step = 8;                                                         member->max.ipv4);
1922                  case 8:                          }
1923                          if (!ccs_read_no_rewrite_policy(head))                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1924                                  break;                                             w[2]))
1925                          head->read_var2 = NULL;                                  return false;
1926                          head->read_step = 9;                  }
1927                  case 9:          }
1928                          if (!ccs_read_path_group_policy(head))          return true;
1929                                  break;  }
1930                          head->read_var1 = NULL;  
1931                          head->read_var2 = NULL;  /**
1932                          head->read_step = 10;   * ccs_read_policy - Read "struct ccs_..._entry" list.
1933                  case 10:   *
1934                          if (!ccs_read_number_group_policy(head))   * @head: Pointer to "struct ccs_io_buffer".
1935                                  break;   * @idx:  Index number.
1936                          head->read_var1 = NULL;   *
1937                          head->read_var2 = NULL;   * Returns true on success, false otherwise.
1938                          head->read_step = 11;   *
1939                  case 11:   * Caller holds ccs_read_lock().
1940                          if (!ccs_read_address_group_policy(head))   */
1941                                  break;  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1942                          head->read_var2 = NULL;  {
1943                          head->read_step = 12;          struct list_head *pos;
1944                  case 12:          list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1945                          if (!ccs_read_reserved_port_policy(head))                  const char *w[4] = { "", "", "", "" };
1946                                  break;                  char buffer[16];
1947                          head->read_eof = true;                  struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1948                                                            list);
1949                    if (acl->is_deleted)
1950                            continue;
1951                    switch (idx) {
1952                    case CCS_ID_TRANSITION_CONTROL:
1953                            {
1954                                    struct ccs_transition_control *ptr =
1955                                            container_of(acl, typeof(*ptr), head);
1956                                    w[0] = ccs_transition_type[ptr->type];
1957                                    w[1] = ptr->program ? ptr->program->name
1958                                            : "any";
1959                                    w[2] = " from ";
1960                                    w[3] = ptr->domainname ? ptr->domainname->name
1961                                            : "any";
1962                            }
1963                            break;
1964                    case CCS_ID_AGGREGATOR:
1965                            {
1966                                    struct ccs_aggregator *ptr =
1967                                            container_of(acl, typeof(*ptr), head);
1968                                    w[0] = CCS_KEYWORD_AGGREGATOR;
1969                                    w[1] = ptr->original_name->name;
1970                                    w[2] = " ";
1971                                    w[3] = ptr->aggregated_name->name;
1972                            }
1973                            break;
1974                    case CCS_ID_PATTERN:
1975                            {
1976                                    struct ccs_pattern *ptr =
1977                                            container_of(acl, typeof(*ptr), head);
1978                                    w[0] = CCS_KEYWORD_FILE_PATTERN;
1979                                    w[1] = ptr->pattern->name;
1980                            }
1981                            break;
1982                    case CCS_ID_NO_REWRITE:
1983                            {
1984                                    struct ccs_no_rewrite *ptr =
1985                                            container_of(acl, typeof(*ptr), head);
1986                                    w[0] = CCS_KEYWORD_DENY_REWRITE;
1987                                    w[1] = ptr->pattern->name;
1988                            }
1989                            break;
1990                    case CCS_ID_RESERVEDPORT:
1991                            {
1992                                    struct ccs_reserved *ptr =
1993                                            container_of(acl, typeof(*ptr), head);
1994                                    const u16 min_port = ptr->min_port;
1995                                    const u16 max_port = ptr->max_port;
1996                                    w[0] = CCS_KEYWORD_DENY_AUTOBIND;
1997                                    snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
1998                                             min_port, min_port != max_port ?
1999                                             '-' : '\0', max_port);
2000                                    buffer[sizeof(buffer) - 1] = '\0';
2001                                    w[1] = buffer;
2002                            }
2003                          break;                          break;
2004                  default:                  default:
2005                          return -EINVAL;                          continue;
2006                  }                  }
2007                    if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))
2008                            return false;
2009          }          }
2010          return 0;          return true;
2011    }
2012    
2013    static void ccs_read_global_domain(struct ccs_io_buffer *head)
2014    {
2015            if (!head->read_eof)
2016                    head->read_eof = ccs_read_domain2(head, &ccs_global_domain);
2017    }
2018    
2019    /**
2020     * ccs_read_exception - Read exception policy.
2021     *
2022     * @head: Pointer to "struct ccs_io_buffer".
2023     *
2024     * Caller holds ccs_read_lock().
2025     */
2026    static void ccs_read_exception(struct ccs_io_buffer *head)
2027    {
2028            if (head->read_eof)
2029                    return;
2030            while (head->read_step < CCS_MAX_POLICY &&
2031                   ccs_read_policy(head, head->read_step))
2032                    head->read_step++;
2033            if (head->read_step < CCS_MAX_POLICY)
2034                    return;
2035            while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2036                   ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
2037                    head->read_step++;
2038            if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2039                    return;
2040            head->read = ccs_read_global_domain;
2041  }  }
2042    
2043  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 1573  static LIST_HEAD(ccs_query_list); Line 2062  static LIST_HEAD(ccs_query_list);
2062  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
2063  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2064    
2065    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
2066    {
2067            va_list args;
2068            const int pos = strlen(buffer);
2069            va_start(args, fmt);
2070            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
2071            va_end(args);
2072    }
2073    
2074    static void ccs_truncate(char *str)
2075    {
2076            while (* (unsigned char *) str > (unsigned char) ' ')
2077                    str++;
2078            *str = '\0';
2079    }
2080    
2081  /**  /**
2082   * ccs_check_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
2083   *   *
2084   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
2085   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
2086   *   *
2087   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
2088   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2089   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
2090   * -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2091   */   */
2092  int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2093  {  {
2094          va_list args;          va_list args;
2095          int error = -EPERM;          int error = -EPERM;
# Line 1594  int ccs_check_supervisor(struct ccs_requ Line 2099  int ccs_check_supervisor(struct ccs_requ
2099          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query_entry *ccs_query_entry = NULL;
2100          bool quota_exceeded = false;          bool quota_exceeded = false;
2101          char *header;          char *header;
2102          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
2103                  r->domain = ccs_current_domain();          va_start(args, fmt);
2104            len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
2105            va_end(args);
2106            if (r->mode == CCS_CONFIG_LEARNING) {
2107                    char *buffer;
2108                    char *realpath = NULL;
2109                    char *argv0 = NULL;
2110                    char *symlink = NULL;
2111                    char *handler = NULL;
2112                    const struct ccs_preference *pref;
2113                    if (!ccs_domain_quota_ok(r))
2114                            return 0;
2115                    header = ccs_init_log(&len, r);
2116                    if (!header)
2117                            return 0;
2118                    pref = ccs_profile(r->profile)->learning;
2119                    /* strstr() will return NULL if ordering is wrong. */
2120                    if (r->param_type == CCS_TYPE_PATH_ACL &&
2121                        r->param.path.operation == CCS_TYPE_EXECUTE) {
2122                            if (pref->learning_exec_argv0) {
2123                                    argv0 = strstr(header, " argv[]={ \"");
2124                                    if (argv0) {
2125                                            argv0 += 10;
2126                                            ccs_truncate(argv0);
2127                                    }
2128                            }
2129                            if (pref->learning_exec_realpath) {
2130                                    realpath = strstr(header,
2131                                                      " exec={ realpath=\"");
2132                                    if (realpath) {
2133                                            realpath += 8;
2134                                            ccs_truncate(realpath);
2135                                    }
2136                            }
2137                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
2138                               r->param.path.operation == CCS_TYPE_SYMLINK &&
2139                               pref->learning_symlink_target) {
2140                            symlink = strstr(header, " symlink.target=\"");
2141                            if (symlink)
2142                                    ccs_truncate(symlink + 1);
2143                    }
2144                    handler = strstr(header, "type=execute_handler");
2145                    if (handler)
2146                            ccs_truncate(handler);
2147                    buffer = kmalloc(len, CCS_GFP_FLAGS);
2148                    if (buffer) {
2149                            va_start(args, fmt);
2150                            vsnprintf(buffer, len - 1, fmt, args);
2151                            va_end(args);
2152                            if (handler || realpath || argv0 || symlink) {
2153                                    ccs_addprintf(buffer, len, " if");
2154                                    if (handler)
2155                                            ccs_addprintf(buffer, len, " task.%s",
2156                                                          handler);
2157                                    if (realpath)
2158                                            ccs_addprintf(buffer, len, " exec.%s",
2159                                                          realpath);
2160                                    if (argv0)
2161                                            ccs_addprintf(buffer, len,
2162                                                          " exec.argv[0]=%s",
2163                                                          argv0);
2164                                    if (symlink)
2165                                            ccs_addprintf(buffer, len, "%s",
2166                                                          symlink);
2167                            }
2168                            ccs_normalize_line(buffer);
2169                            ccs_write_domain2(buffer, domain, false);
2170                            kfree(buffer);
2171                    }
2172                    kfree(header);
2173                    return 0;
2174            }
2175            if (r->mode != CCS_CONFIG_ENFORCING)
2176                    return 0;
2177          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2178                  int i;                  int i;
2179                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2180                          return -EPERM;                          return -EPERM;
2181                  for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2182                       i++) {                               enforcing_penalty; i++) {
2183                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2184                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2185                  }                  }
2186                  return -EPERM;                  return -EPERM;
2187          }          }
2188          va_start(args, fmt);          header = ccs_init_log(&len, r);
         len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;  
         va_end(args);  
         header = ccs_init_audit_log(&len, r);  
2189          if (!header)          if (!header)
2190                  goto out;                  goto out;
2191          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2192          if (!ccs_query_entry)          if (!ccs_query_entry)
2193                  goto out;                  goto out;
2194          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          len = ccs_round2(len);
2195            ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2196          if (!ccs_query_entry->query)          if (!ccs_query_entry->query)
2197                  goto out;                  goto out;
2198          INIT_LIST_HEAD(&ccs_query_entry->list);          INIT_LIST_HEAD(&ccs_query_entry->list);
         /***** CRITICAL SECTION START *****/  
2199          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2200          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2201              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 2205  int ccs_check_supervisor(struct ccs_requ
2205                  ccs_query_entry->serial = ccs_serial++;                  ccs_query_entry->serial = ccs_serial++;
2206          }          }
2207          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2208          if (quota_exceeded)          if (quota_exceeded)
2209                  goto out;                  goto out;
2210          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
2211                         ccs_query_entry->serial, r->retry, header);                   ccs_query_entry->serial, r->retry, header);
2212          kfree(header);          kfree(header);
2213          header = NULL;          header = NULL;
2214          va_start(args, fmt);          ccs_addprintf(ccs_query_entry->query, len, fmt, args);
         vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);  
2215          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
         va_end(args);  
         /***** CRITICAL SECTION START *****/  
2216          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2217          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2218          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2219          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2220          for (ccs_query_entry->timer = 0;          for (ccs_query_entry->timer = 0;
2221               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 2226  int ccs_check_supervisor(struct ccs_requ
2226                  if (ccs_query_entry->answer)                  if (ccs_query_entry->answer)
2227                          break;                          break;
2228          }          }
         /***** CRITICAL SECTION START *****/  
2229          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2230          list_del(&ccs_query_entry->list);          list_del(&ccs_query_entry->list);
2231          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
2232          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2233          switch (ccs_query_entry->answer) {          switch (ccs_query_entry->answer) {
2234          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2235                  error = 1;                  error = CCS_RETRY_REQUEST;
2236                  r->retry++;                  r->retry++;
2237                  break;                  break;
2238          case 1:          case 1:
# Line 1702  static int ccs_poll_query(struct file *f Line 2270  static int ccs_poll_query(struct file *f
2270          bool found = false;          bool found = false;
2271          u8 i;          u8 i;
2272          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
                 /***** CRITICAL SECTION START *****/  
2273                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2274                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2275                          struct ccs_query_entry *ptr                          struct ccs_query_entry *ptr =
2276                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, struct ccs_query_entry, list);
2277                          if (ptr->answer)                          if (ptr->answer)
2278                                  continue;                                  continue;
2279                          found = true;                          found = true;
2280                          break;                          break;
2281                  }                  }
2282                  spin_unlock(&ccs_query_list_lock);                  spin_unlock(&ccs_query_list_lock);
                 /***** CRITICAL SECTION END *****/  
2283                  if (found)                  if (found)
2284                          return POLLIN | POLLRDNORM;                          return POLLIN | POLLRDNORM;
2285                  if (i)                  if (i)
# Line 1727  static int ccs_poll_query(struct file *f Line 2293  static int ccs_poll_query(struct file *f
2293   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2294   *   *
2295   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
2296   */   */
2297  static int ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2298  {  {
2299          struct list_head *tmp;          struct list_head *tmp;
2300          int pos = 0;          int pos = 0;
2301          int len = 0;          int len = 0;
2302          char *buf;          char *buf;
2303          if (head->read_avail)          if (head->read_avail)
2304                  return 0;                  return;
2305          if (head->read_buf) {          if (head->read_buf) {
2306                  kfree(head->read_buf);                  kfree(head->read_buf);
2307                  head->read_buf = NULL;                  head->read_buf = NULL;
2308                  head->readbuf_size = 0;                  head->readbuf_size = 0;
2309          }          }
         /***** CRITICAL SECTION START *****/  
2310          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2311          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2312                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1756  static int ccs_read_query(struct ccs_io_ Line 2319  static int ccs_read_query(struct ccs_io_
2319                  break;                  break;
2320          }          }
2321          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2322          if (!len) {          if (!len) {
2323                  head->read_step = 0;                  head->read_step = 0;
2324                  return 0;                  return;
2325          }          }
2326          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2327          if (!buf)          if (!buf)
2328                  return 0;                  return;
2329          pos = 0;          pos = 0;
         /***** CRITICAL SECTION START *****/  
2330          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2331          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2332                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1783  static int ccs_read_query(struct ccs_io_ Line 2344  static int ccs_read_query(struct ccs_io_
2344                  break;                  break;
2345          }          }
2346          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2347          if (buf[0]) {          if (buf[0]) {
2348                  head->read_avail = len;                  head->read_avail = len;
2349                  head->readbuf_size = head->read_avail;                  head->readbuf_size = head->read_avail;
# Line 1792  static int ccs_read_query(struct ccs_io_ Line 2352  static int ccs_read_query(struct ccs_io_
2352          } else {          } else {
2353                  kfree(buf);                  kfree(buf);
2354          }          }
         return 0;  
2355  }  }
2356    
2357  /**  /**
# Line 1808  static int ccs_write_answer(struct ccs_i Line 2367  static int ccs_write_answer(struct ccs_i
2367          struct list_head *tmp;          struct list_head *tmp;
2368          unsigned int serial;          unsigned int serial;
2369          unsigned int answer;          unsigned int answer;
         /***** CRITICAL SECTION START *****/  
2370          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2371          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2372                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1816  static int ccs_write_answer(struct ccs_i Line 2374  static int ccs_write_answer(struct ccs_i
2374                  ptr->timer = 0;                  ptr->timer = 0;
2375          }          }
2376          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2377          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2378                  return -EINVAL;                  return -EINVAL;
         /***** CRITICAL SECTION START *****/  
2379          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2380          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2381                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1831  static int ccs_write_answer(struct ccs_i Line 2387  static int ccs_write_answer(struct ccs_i
2387                  break;                  break;
2388          }          }
2389          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2390          return 0;          return 0;
2391  }  }
2392    
# Line 1839  static int ccs_write_answer(struct ccs_i Line 2394  static int ccs_write_answer(struct ccs_i
2394   * ccs_read_version: Get version.   * ccs_read_version: Get version.
2395   *   *
2396   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns version information.  
2397   */   */
2398  static int ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2399  {  {
2400          if (!head->read_eof) {          if (head->read_eof)
2401                  ccs_io_printf(head, "1.7.0-pre");                  return;
2402                  head->read_eof = true;          ccs_io_printf(head, "1.7.2");
2403          }          head->read_eof = true;
         return 0;  
2404  }  }
2405    
2406  /**  /**
2407   * ccs_read_self_domain - Get the current process's domainname.   * ccs_read_self_domain - Get the current process's domainname.
2408   *   *
2409   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns the current process's domainname.  
2410   */   */
2411  static int ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2412  {  {
2413          if (!head->read_eof) {          if (head->read_eof)
2414                  /*                  return;
2415                   * ccs_current_domain()->domainname != NULL          /*
2416                   * because every process belongs to a domain and           * ccs_current_domain()->domainname != NULL because every process
2417                   * the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2418                   */           */
2419                  ccs_io_printf(head, "%s",          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2420                                ccs_current_domain()->domainname->name);          head->read_eof = true;
                 head->read_eof = true;  
         }  
         return 0;  
2421  }  }
2422    
2423  /**  /**
# Line 1883  static int ccs_read_self_domain(struct c Line 2430  static int ccs_read_self_domain(struct c
2430   */   */
2431  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2432  {  {
2433          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2434          if (!head)          if (!head)
2435                  return -ENOMEM;                  return -ENOMEM;
2436          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2437          head->type = type;          head->type = type;
2438          switch (type) {          switch (type) {
2439          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2440                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2441                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2442                  break;                  break;
2443          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2444                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2445                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2446                  break;                  break;
2447  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2448          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;  
2449          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2450                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2451                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2452                  break;                  break;
2453  #endif  #endif
2454          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 1944  int ccs_open_control(const u8 type, stru Line 2488  int ccs_open_control(const u8 type, stru
2488                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2489                  break;                  break;
2490          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2491                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2492                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2493                  break;                  break;
2494          }          }
2495          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 1955  int ccs_open_control(const u8 type, stru Line 2499  int ccs_open_control(const u8 type, stru
2499                   */                   */
2500                  head->read = NULL;                  head->read = NULL;
2501                  head->poll = NULL;                  head->poll = NULL;
2502          } else if (type != CCS_QUERY &&          } else if (!head->poll) {
2503                     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.  
                  */  
2504                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2505                          head->readbuf_size = 4096 * 2;                          head->readbuf_size = 4096;
2506                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2507                  if (!head->read_buf) {                  if (!head->read_buf) {
2508                          kfree(head);                          kfree(head);
2509                          return -ENOMEM;                          return -ENOMEM;
# Line 1976  int ccs_open_control(const u8 type, stru Line 2516  int ccs_open_control(const u8 type, stru
2516                   */                   */
2517                  head->write = NULL;                  head->write = NULL;
2518          } else if (head->write) {          } else if (head->write) {
2519                  head->writebuf_size = 4096 * 2;                  head->writebuf_size = 4096;
2520                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2521                  if (!head->write_buf) {                  if (!head->write_buf) {
2522                          kfree(head->read_buf);                          kfree(head->read_buf);
2523                          kfree(head);                          kfree(head);
# Line 1986  int ccs_open_control(const u8 type, stru Line 2526  int ccs_open_control(const u8 type, stru
2526          }          }
2527          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2528              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2529                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2530          file->private_data = head;          file->private_data = head;
2531          /*          /*
2532           * 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 2537  int ccs_open_control(const u8 type, stru
2537                  ccs_read_control(file, NULL, 0);                  ccs_read_control(file, NULL, 0);
2538          /*          /*
2539           * If the file is /proc/ccs/query , increment the observer counter.           * If the file is /proc/ccs/query , increment the observer counter.
2540           * The obserber counter is used by ccs_check_supervisor() to see if           * The obserber counter is used by ccs_supervisor() to see if
2541           * there is some process monitoring /proc/ccs/query.           * there is some process monitoring /proc/ccs/query.
2542           */           */
2543          else if (type == CCS_QUERY)          else if (type == CCS_QUERY)
# Line 2012  int ccs_open_control(const u8 type, stru Line 2552  int ccs_open_control(const u8 type, stru
2552   * @wait: Pointer to "poll_table".   * @wait: Pointer to "poll_table".
2553   *   *
2554   * Waits for read readiness.   * Waits for read readiness.
2555   * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2556   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2557   * /usr/lib/ccs/ccs-auditd.   * /usr/sbin/ccs-auditd .
2558   */   */
2559  int ccs_poll_control(struct file *file, poll_table *wait)  int ccs_poll_control(struct file *file, poll_table *wait)
2560  {  {
# Line 2039  int ccs_read_control(struct file *file, Line 2579  int ccs_read_control(struct file *file,
2579          int len = 0;          int len = 0;
2580          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2581          char *cp;          char *cp;
2582            int idx;
2583          if (!head->read)          if (!head->read)
2584                  return -ENOSYS;                  return -ENOSYS;
2585          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2586                  return -EFAULT;                  return -EFAULT;
2587          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2588                  return -EINTR;                  return -EINTR;
2589          /* Call the policy handler. */          idx = ccs_read_lock();
2590          len = head->read(head);          while (1) {
2591          if (len < 0)                  /* Call the policy handler. */
2592                  goto out;                  head->read(head);
2593          /* Write to buffer. */                  /* Write to buffer. */
2594          len = head->read_avail;                  len = head->read_avail;
2595                    if (len || head->poll || head->read_eof)
2596                            break;
2597                    len = head->readbuf_size * 2;
2598                    cp = kzalloc(len, CCS_GFP_FLAGS);
2599                    if (!cp) {
2600                            len = -ENOMEM;
2601                            goto out;
2602                    }
2603                    kfree(head->read_buf);
2604                    head->read_buf = cp;
2605                    head->readbuf_size = len;
2606            }
2607          if (len > buffer_len)          if (len > buffer_len)
2608                  len = buffer_len;                  len = buffer_len;
2609          if (!len)          if (!len)
# Line 2064  int ccs_read_control(struct file *file, Line 2617  int ccs_read_control(struct file *file,
2617          head->read_avail -= len;          head->read_avail -= len;
2618          memmove(cp, cp + len, head->read_avail);          memmove(cp, cp + len, head->read_avail);
2619   out:   out:
2620            ccs_read_unlock(idx);
2621          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2622          return len;          return len;
2623  }  }
# Line 2084  int ccs_write_control(struct file *file, Line 2638  int ccs_write_control(struct file *file,
2638          int error = buffer_len;          int error = buffer_len;
2639          int avail_len = buffer_len;          int avail_len = buffer_len;
2640          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2641            int idx;
2642          if (!head->write)          if (!head->write)
2643                  return -ENOSYS;                  return -ENOSYS;
2644          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2645                  return -EFAULT;                  return -EFAULT;
         /* Don't allow updating policies by non manager programs. */  
         if (head->write != ccs_write_pid &&  
             head->write != ccs_write_domain_policy &&  
             !ccs_is_policy_manager())  
                 return -EPERM;  
2646          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2647                  return -EINTR;                  return -EINTR;
2648            idx = ccs_read_lock();
2649            /* Don't allow updating policies by non manager programs. */
2650            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2651                !ccs_manager()) {
2652                    ccs_read_unlock(idx);
2653                    mutex_unlock(&head->io_sem);
2654                    return -EPERM;
2655            }
2656          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2657          while (avail_len > 0) {          while (avail_len > 0) {
2658                  char c;                  char c;
2659                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->write_avail >= head->writebuf_size - 1) {
2660                          error = -ENOMEM;                          const int len = head->writebuf_size * 2;
2661                          break;                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2662                  } else if (get_user(c, buffer)) {                          if (!cp) {
2663                                    error = -ENOMEM;
2664