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

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 2870 by kumaneko, Sat Aug 8 03:09:07 2009 UTC revision 3780 by kumaneko, Thu Jun 24 08:50:05 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/policy_io.c   * security/ccsecurity/policy_io.c
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.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    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
225    {
226            va_list args;
227            const int pos = strlen(buffer);
228            va_start(args, fmt);
229            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
230            va_end(args);
231    }
232    
233  /**  /**
234   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
235   *   *
236   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
237   *   *
238   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
239     */
240    static bool ccs_flush(struct ccs_io_buffer *head)
241    {
242            while (head->r.w_pos) {
243                    const char *w = head->r.w[0];
244                    int len = strlen(w);
245                    if (len) {
246                            if (len > head->read_user_buf_avail)
247                                    len = head->read_user_buf_avail;
248                            if (!len)
249                                    return false;
250                            if (copy_to_user(head->read_user_buf, w, len))
251                                    return false;
252                            head->read_user_buf_avail -= len;
253                            head->read_user_buf += len;
254                            w += len;
255                    }
256                    if (*w) {
257                            head->r.w[0] = w;
258                            return false;
259                    }
260                    /* Add '\0' for audit logs and query. */
261                    if (head->poll) {
262                            if (!head->read_user_buf_avail ||
263                                copy_to_user(head->read_user_buf, "", 1))
264                                    return false;
265                            head->read_user_buf_avail--;
266                            head->read_user_buf++;
267                    }
268                    head->r.w_pos--;
269                    for (len = 0; len < head->r.w_pos; len++)
270                            head->r.w[len] = head->r.w[len + 1];
271            }
272            head->r.avail = 0;
273            return true;
274    }
275    
276    /**
277     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
278     *
279     * @head:   Pointer to "struct ccs_io_buffer".
280     * @string: String to print.
281     *
282     * Note that @string has to be kept valid until @head is kfree()d.
283     * This means that char[] allocated on stack memory cannot be passed to
284     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
285     */
286    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
287    {
288            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
289                    head->r.w[head->r.w_pos++] = string;
290                    ccs_flush(head);
291            } else
292                    WARN_ON(1);
293    }
294    
295    /**
296     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
297   *   *
298   * The snprintf() will truncate, but ccs_io_printf() won't.   * @head: Pointer to "struct ccs_io_buffer".
299     * @fmt:  The printf()'s format string, followed by parameters.
300   */   */
301  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
302  {  {
303          va_list args;          va_list args;
304          int len;          int len;
305          int pos = head->read_avail;          int pos = head->r.avail;
306          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
307          if (size <= 0)          if (size <= 0)
308                  return false;                  return;
309          va_start(args, fmt);          va_start(args, fmt);
310          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
311          va_end(args);          va_end(args);
312          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
313                  return false;                  WARN_ON(1);
314          head->read_avail += len;                  return;
315          return true;          }
316            head->r.avail += len;
317            ccs_set_string(head, head->read_buf + pos);
318    }
319    
320    static void ccs_set_space(struct ccs_io_buffer *head)
321    {
322            ccs_set_string(head, " ");
323    }
324    
325    static bool ccs_set_lf(struct ccs_io_buffer *head)
326    {
327            ccs_set_string(head, "\n");
328            return !head->r.w_pos;
329  }  }
330    
331  /**  /**
332   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
333   *   *
334   * @profile: Profile number to create.   * @profile: Profile number to create.
335   *   *
336   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
337   */   */
338  struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_assign_profile(const unsigned int profile)
                                                    profile)  
339  {  {
340          struct ccs_profile *ptr;          struct ccs_profile *ptr;
341          struct ccs_profile *entry;          struct ccs_profile *entry;
342          int i;          if (profile >= CCS_MAX_PROFILES)
         if (profile >= MAX_PROFILES)  
343                  return NULL;                  return NULL;
344          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
345          if (ptr)          if (ptr)
346                  return ptr;                  return ptr;
347          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
348          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
349                    goto out;
350          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
351          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
352                  ptr = entry;                  ptr = entry;
353                  for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)                  ptr->audit = &ccs_default_profile.preference;
354                          ptr->value[i] = ccs_control_array[i].current_value;                  ptr->learning = &ccs_default_profile.preference;
355                  /*                  ptr->permissive = &ccs_default_profile.preference;
356                   * Needn't to initialize "ptr->capability_value"                  ptr->enforcing = &ccs_default_profile.preference;
357                   * because they are always 0.                  ptr->default_config = CCS_CONFIG_DISABLED |
358                   */                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
359                    memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
360                           sizeof(ptr->config));
361                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
362                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
363                  entry = NULL;                  entry = NULL;
364          }          }
365          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
366     out:
367          kfree(entry);          kfree(entry);
368          return ptr;          return ptr;
369  }  }
370    
371  /**  /**
372     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
373     */
374    static void ccs_check_profile(void)
375    {
376            struct ccs_domain_info *domain;
377            const int idx = ccs_read_lock();
378            ccs_policy_loaded = true;
379            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
380                    const u8 profile = domain->profile;
381                    if (ccs_profile_ptr[profile])
382                            continue;
383                    panic("Profile %u (used by '%s') not defined.\n",
384                          profile, domain->domainname->name);
385            }
386            ccs_read_unlock(idx);
387            if (ccs_profile_version != 20090903)
388                    panic("Profile version %u is not supported.\n",
389                          ccs_profile_version);
390            printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");
391            printk(KERN_INFO "Mandatory Access Control activated.\n");
392    }
393    
394    /**
395     * ccs_profile - Find a profile.
396     *
397     * @profile: Profile number to find.
398     *
399     * Returns pointer to "struct ccs_profile".
400     */
401    struct ccs_profile *ccs_profile(const u8 profile)
402    {
403            struct ccs_profile *ptr = ccs_profile_ptr[profile];
404            if (!ccs_policy_loaded)
405                    return &ccs_default_profile;
406            BUG_ON(!ptr);
407            return ptr;
408    }
409    
410    static s8 ccs_find_yesno(const char *string, const char *find)
411    {
412            const char *cp = strstr(string, find);
413            if (cp) {
414                    cp += strlen(find);
415                    if (!strncmp(cp, "=yes", 4))
416                            return 1;
417                    else if (!strncmp(cp, "=no", 3))
418                            return 0;
419            }
420            return -1;
421    }
422    
423    static void ccs_set_bool(bool *b, const char *string, const char *find)
424    {
425            switch (ccs_find_yesno(string, find)) {
426            case 1:
427                    *b = true;
428                    break;
429            case 0:
430                    *b = false;
431                    break;
432            }
433    }
434    
435    static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
436    {
437            const char *cp = strstr(string, find);
438            if (cp)
439                    sscanf(cp + strlen(find), "=%u", i);
440    }
441    
442    static void ccs_set_pref(const char *name, const char *value,
443                             const bool use_default, struct ccs_profile *profile)
444    {
445            struct ccs_preference **pref;
446            bool *verbose;
447            if (!strcmp(name, "audit")) {
448                    if (use_default) {
449                            pref = &profile->audit;
450                            goto set_default;
451                    }
452                    profile->audit = &profile->preference;
453    #ifdef CONFIG_CCSECURITY_AUDIT
454                    ccs_set_uint(&profile->preference.audit_max_grant_log, value,
455                                 "max_grant_log");
456                    ccs_set_uint(&profile->preference.audit_max_reject_log, value,
457                                 "max_reject_log");
458    #endif
459                    ccs_set_bool(&profile->preference.audit_task_info, value,
460                                 "task_info");
461                    ccs_set_bool(&profile->preference.audit_path_info, value,
462                                 "path_info");
463                    return;
464            }
465            if (!strcmp(name, "enforcing")) {
466                    if (use_default) {
467                            pref = &profile->enforcing;
468                            goto set_default;
469                    }
470                    profile->enforcing = &profile->preference;
471                    ccs_set_uint(&profile->preference.enforcing_penalty, value,
472                                 "penalty");
473                    verbose = &profile->preference.enforcing_verbose;
474                    goto set_verbose;
475            }
476            if (!strcmp(name, "permissive")) {
477                    if (use_default) {
478                            pref = &profile->permissive;
479                            goto set_default;
480                    }
481                    profile->permissive = &profile->preference;
482                    verbose = &profile->preference.permissive_verbose;
483                    goto set_verbose;
484            }
485            if (!strcmp(name, "learning")) {
486                    if (use_default) {
487                            pref = &profile->learning;
488                            goto set_default;
489                    }
490                    profile->learning = &profile->preference;
491                    ccs_set_uint(&profile->preference.learning_max_entry, value,
492                                 "max_entry");
493                    ccs_set_bool(&profile->preference.learning_exec_realpath,
494                                 value, "exec.realpath");
495                    ccs_set_bool(&profile->preference.learning_exec_argv0, value,
496                                 "exec.argv0");
497                    ccs_set_bool(&profile->preference.learning_symlink_target,
498                                 value, "symlink.target");
499                    verbose = &profile->preference.learning_verbose;
500                    goto set_verbose;
501            }
502            return;
503     set_default:
504            *pref = &ccs_default_profile.preference;
505            return;
506     set_verbose:
507            ccs_set_bool(verbose, value, "verbose");
508    }
509    
510    static int ccs_set_mode(char *name, const char *value, const bool use_default,
511                            struct ccs_profile *profile)
512    {
513            u8 i;
514            u8 config;
515            if (!strcmp(name, "CONFIG")) {
516                    i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
517                            + CCS_MAX_MAC_CATEGORY_INDEX;
518                    config = profile->default_config;
519            } else if (ccs_str_starts(&name, "CONFIG::")) {
520                    config = 0;
521                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
522                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
523                            if (strcmp(name, ccs_mac_keywords[i]))
524                                    continue;
525                            config = profile->config[i];
526                            break;
527                    }
528                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
529                        + CCS_MAX_MAC_CATEGORY_INDEX)
530                            return -EINVAL;
531            } else {
532                    return -EINVAL;
533            }
534            if (use_default) {
535                    config = CCS_CONFIG_USE_DEFAULT;
536            } else {
537                    u8 mode;
538                    for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
539                            if (strstr(value, ccs_mode[mode]))
540                                    /*
541                                     * Update lower 3 bits in order to distinguish
542                                     * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
543                                     */
544                                    config = (config & ~7) | mode;
545    #ifdef CONFIG_CCSECURITY_AUDIT
546                    if (config != CCS_CONFIG_USE_DEFAULT) {
547                            switch (ccs_find_yesno(value, "grant_log")) {
548                            case 1:
549                                    config |= CCS_CONFIG_WANT_GRANT_LOG;
550                                    break;
551                            case 0:
552                                    config &= ~CCS_CONFIG_WANT_GRANT_LOG;
553                                    break;
554                            }
555                            switch (ccs_find_yesno(value, "reject_log")) {
556                            case 1:
557                                    config |= CCS_CONFIG_WANT_REJECT_LOG;
558                                    break;
559                            case 0:
560                                    config &= ~CCS_CONFIG_WANT_REJECT_LOG;
561                                    break;
562                            }
563                    }
564    #endif
565            }
566            if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
567                + CCS_MAX_MAC_CATEGORY_INDEX)
568                    profile->config[i] = config;
569            else if (config != CCS_CONFIG_USE_DEFAULT)
570                    profile->default_config = config;
571            return 0;
572    }
573    
574    /**
575   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
576   *   *
577   * @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 581  struct ccs_profile *ccs_find_or_assign_n
581  static int ccs_write_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
582  {  {
583          char *data = head->write_buf;          char *data = head->write_buf;
584          unsigned int i;          bool use_default = false;
         unsigned int value;  
585          char *cp;          char *cp;
586          struct ccs_profile *ccs_profile;          int i;
587            struct ccs_profile *profile;
588            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
589                    return 0;
590          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
591          if (data != cp) {          if (data == cp) {
592                    profile = &ccs_default_profile;
593            } else {
594                  if (*cp != '-')                  if (*cp != '-')
595                          return -EINVAL;                          return -EINVAL;
596                  data = cp + 1;                  data = cp + 1;
597                    profile = ccs_assign_profile(i);
598                    if (!profile)
599                            return -EINVAL;
600          }          }
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
                 return -EINVAL;  
601          cp = strchr(data, '=');          cp = strchr(data, '=');
602          if (!cp)          if (!cp)
603                  return -EINVAL;                  return -EINVAL;
604          *cp = '\0';          *cp++ = '\0';
605            if (profile != &ccs_default_profile)
606                    use_default = strstr(cp, "use_default") != NULL;
607            if (ccs_str_starts(&data, "PREFERENCE::")) {
608                    ccs_set_pref(data, cp, use_default, profile);
609                    return 0;
610            }
611            if (profile == &ccs_default_profile)
612                    return -EINVAL;
613          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
614                  const struct ccs_path_info *new_comment                  const struct ccs_path_info *old_comment = profile->comment;
615                          = 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 *****/  
616                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
                 ccs_profile_entry_used[0] = true;  
617                  return 0;                  return 0;
618          }          }
619          if (ccs_str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {          return ccs_set_mode(data, cp, use_default, profile);
620                  if (sscanf(cp + 1, "%u", &value) != 1) {  }
621                          for (i = 0; i < 4; i++) {  
622                                  if (strcmp(cp + 1, ccs_mode_4[i]))  static void ccs_print_preference(struct ccs_io_buffer *head, const int idx)
623                                          continue;  {
624                                  value = i;          struct ccs_preference *pref = &ccs_default_profile.preference;
625                                  break;          const struct ccs_profile *profile = idx >= 0 ?
626                          }                  ccs_profile_ptr[idx] : NULL;
627                          if (i == 4)          char buffer[16] = "";
628                                  return -EINVAL;          if (profile) {
629                  }                  buffer[sizeof(buffer) - 1] = '\0';
630                  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;  
631          }          }
632          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {          if (profile) {
633                  if (strcmp(data, ccs_control_array[i].keyword))                  pref = profile->audit;
634                          continue;                  if (pref == &ccs_default_profile.preference)
635                  if (sscanf(cp + 1, "%u", &value) != 1) {                          goto skip0;
                         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;  
636          }          }
637          return -EINVAL;          ccs_io_printf(head, "%sPREFERENCE::%s={ "
638    #ifdef CONFIG_CCSECURITY_AUDIT
639                          "max_grant_log=%u max_reject_log=%u "
640    #endif
641                          "task_info=%s path_info=%s }\n", buffer,
642                          "audit",
643    #ifdef CONFIG_CCSECURITY_AUDIT
644                          pref->audit_max_grant_log,
645                          pref->audit_max_reject_log,
646    #endif
647                          ccs_yesno(pref->audit_task_info),
648                          ccs_yesno(pref->audit_path_info));
649     skip0:
650            if (profile) {
651                    pref = profile->learning;
652                    if (pref == &ccs_default_profile.preference)
653                            goto skip1;
654            }
655            ccs_io_printf(head, "%sPREFERENCE::%s={ "
656                          "verbose=%s max_entry=%u exec.realpath=%s "
657                          "exec.argv0=%s symlink.target=%s }\n",
658                          buffer, "learning",
659                          ccs_yesno(pref->learning_verbose),
660                          pref->learning_max_entry,
661                          ccs_yesno(pref->learning_exec_realpath),
662                          ccs_yesno(pref->learning_exec_argv0),
663                          ccs_yesno(pref->learning_symlink_target));
664     skip1:
665            if (profile) {
666                    pref = profile->permissive;
667                    if (pref == &ccs_default_profile.preference)
668                            goto skip2;
669            }
670            ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
671                          buffer, "permissive",
672                          ccs_yesno(pref->permissive_verbose));
673     skip2:
674            if (profile) {
675                    pref = profile->enforcing;
676                    if (pref == &ccs_default_profile.preference)
677                            return;
678            }
679            ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
680                          "penalty=%u }\n", buffer, "enforcing",
681                          ccs_yesno(pref->enforcing_verbose),
682                          pref->enforcing_penalty);
683    }
684    
685    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
686    {
687            ccs_io_printf(head, "={ mode=%s", ccs_mode[config & 3]);
688    #ifdef CONFIG_CCSECURITY_AUDIT
689            ccs_io_printf(head, " grant_log=%s reject_log=%s",
690                          ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
691                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
692    #endif
693            ccs_set_string(head, " }\n");
694  }  }
695    
696  /**  /**
697   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
698   *   *
699   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
700   */   */
701  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
702  {  {
703          static const int ccs_total          u8 index;
704                  = CCS_MAX_CONTROL_INDEX + CCS_MAX_CAPABILITY_INDEX + 1;          const struct ccs_profile *profile;
705          int step;   next:
706          if (head->read_eof)          index = head->r.index;
707                  return 0;          profile = ccs_profile_ptr[index];
708          for (step = head->read_step; step < MAX_PROFILES * ccs_total; step++) {          switch (head->r.step) {
709                  const u8 index = step / ccs_total;          case 0:
710                  u8 type = step % ccs_total;                  ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
711                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];                  ccs_print_preference(head, -1);
712                  head->read_step = step;                  head->r.step++;
713                  if (!ccs_profile)                  break;
714                          continue;          case 1:
715                  if (!ccs_profile_entry_used[type])                  for ( ; head->r.index < CCS_MAX_PROFILES;
716                          continue;                        head->r.index++)
717                  if (!type) { /* Print profile' comment tag. */                          if (ccs_profile_ptr[head->r.index])
718                          bool done;                                  break;
719                          /***** CRITICAL SECTION START *****/                  if (head->r.index == CCS_MAX_PROFILES)
720                          spin_lock(&ccs_profile_comment_lock);                          return;
721                          done = ccs_io_printf(head, "%u-COMMENT=%s\n",                  head->r.step++;
722                                               index, ccs_profile->comment ?                  break;
723                                               ccs_profile->comment->name : "");          case 2:
724                          spin_unlock(&ccs_profile_comment_lock);                  {
725                          /***** CRITICAL SECTION END *****/                          const struct ccs_path_info *comment = profile->comment;
726                          if (!done)                          ccs_io_printf(head, "%u-COMMENT=", index);
727                                  break;                          ccs_set_string(head, comment ? comment->name : "");
728                          continue;                          ccs_set_lf(head);
729                            head->r.step++;
730                  }                  }
731                  type--;                  break;
732                  if (type >= CCS_MAX_CONTROL_INDEX) {          case 3:
733                          const int i = type - CCS_MAX_CONTROL_INDEX;                  {
734                          const u8 value = ccs_profile->capability_value[i];                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
735                          if (!ccs_io_printf(head,                          ccs_print_config(head, profile->default_config);
736                                             "%u-" KEYWORD_MAC_FOR_CAPABILITY                          head->r.bit = 0;
737                                             "%s=%s\n", index,                          head->r.step++;
738                                             ccs_capability_control_keyword[i],                  }
739                                             ccs_mode_4[value]))                  break;
740                                  break;          case 4:
741                  } else {                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
742                          const unsigned int value = ccs_profile->value[type];                                + CCS_MAX_CAPABILITY_INDEX
743                          const char **modes = NULL;                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
744                          const char *keyword = ccs_control_array[type].keyword;                          const u8 i = head->r.bit;
745                          switch (ccs_control_array[type].max_value) {                          const u8 config = profile->config[i];
746                          case 3:                          if (config == CCS_CONFIG_USE_DEFAULT)
747                                  modes = ccs_mode_4;                                  continue;
748                                  break;                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
749                          case 1:                                        ccs_mac_keywords[i]);
750                                  modes = ccs_mode_2;                          ccs_print_config(head, config);
751                                  break;                          head->r.bit++;
752                          }                          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;  
                         }  
753                  }                  }
754                    if (head->r.bit == CCS_MAX_MAC_INDEX
755                        + CCS_MAX_CAPABILITY_INDEX
756                        + CCS_MAX_MAC_CATEGORY_INDEX) {
757                            ccs_print_preference(head, index);
758                            head->r.index++;
759                            head->r.step = 1;
760                    }
761                    break;
762          }          }
763          if (step == MAX_PROFILES * ccs_total)          if (ccs_flush(head))
764                  head->read_eof = true;                  goto next;
         return 0;  
765  }  }
766    
767  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,
768  LIST_HEAD(ccs_policy_manager_list);                                     const struct ccs_acl_head *b)
769    {
770            return container_of(a, struct ccs_manager, head)->manager
771                    == container_of(b, struct ccs_manager, head)->manager;
772    }
773    
774  /**  /**
775   * 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 781  LIST_HEAD(ccs_policy_manager_list);
781   */   */
782  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)
783  {  {
784          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         const struct ccs_path_info *saved_manager;  
785          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
786          bool is_domain = false;          if (ccs_domain_def(manager)) {
787          if (ccs_is_domain_def(manager)) {                  if (!ccs_correct_domain(manager))
                 if (!ccs_is_correct_domain(manager))  
788                          return -EINVAL;                          return -EINVAL;
789                  is_domain = true;                  e.is_domain = true;
790          } else {          } else {
791                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
792                          return -EINVAL;                          return -EINVAL;
793          }          }
794          saved_manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
795          if (!saved_manager)          if (!e.manager)
796                  return -ENOMEM;                  return error;
797          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
798                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
799          mutex_lock(&ccs_policy_lock);                                    ccs_same_manager_entry);
800          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);  
801          return error;          return error;
802  }  }
803    
804  /**  /**
805   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
806   *   *
807   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
808   *   *
809   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
810   */   */
811  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
812  {  {
813          char *data = head->write_buf;          char *data = head->write_buf;
814          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
815          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
816                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
817                  return 0;                  return 0;
# Line 381  static int ccs_write_manager_policy(stru Line 820  static int ccs_write_manager_policy(stru
820  }  }
821    
822  /**  /**
823   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
824   *   *
825   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
826   *   *
  * Returns 0.  
  *  
827   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
828   */   */
829  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
830  {  {
831          struct list_head *pos;          if (head->r.eof)
832          ccs_check_read_lock();                  return;
833          if (head->read_eof)          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
834                  return 0;                  struct ccs_manager *ptr =
835          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {                          list_entry(head->r.acl, typeof(*ptr), head.list);
836                  struct ccs_policy_manager_entry *ptr;                  if (ptr->head.is_deleted)
                 ptr = list_entry(pos, struct ccs_policy_manager_entry, list);  
                 if (ptr->is_deleted)  
837                          continue;                          continue;
838                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
839                          return 0;                          return;
840                    ccs_set_string(head, ptr->manager->name);
841                    ccs_set_lf(head);
842          }          }
843          head->read_eof = true;          head->r.eof = true;
         return 0;  
844  }  }
845    
846  /**  /**
847   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
848   *   *
849   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
850   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
851   *   *
852   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
853   */   */
854  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
855  {  {
856          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
857          const char *exe;          const char *exe;
858          struct task_struct *task = current;          struct task_struct *task = current;
859          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
860                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
861          bool found = false;          bool found = false;
         ccs_check_read_lock();  
862          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
863                  return true;                  return true;
864          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
865                  return true;                  return true;
866          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
867                  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;  
                 }  
         }  
868          exe = ccs_get_exe();          exe = ccs_get_exe();
869          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
870                  return false;                                  head.list) {
871          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
872                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
873                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
874                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
875                          /* Set manager flag. */                                  continue;
876                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
877                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
878                                    continue;
879                  }                  }
880                    /* Set manager flag. */
881                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
882                    found = true;
883                    break;
884          }          }
885          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
886                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 474  static bool ccs_is_policy_manager(void) Line 906  static bool ccs_is_policy_manager(void)
906  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
907  {  {
908          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
909          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
910                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
911                  if (cp)          if (cp)
912                          *cp++ = '\0';                  *cp++ = '\0';
         }  
913          return cp;          return cp;
914  }  }
915    
916  /**  /**
917   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
918   *   *
919   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
920   * @data: String to parse.   * @data: String to parse.
# Line 500  static char *ccs_find_condition_part(cha Line 923  static char *ccs_find_condition_part(cha
923   *   *
924   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
925   */   */
926  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)
927  {  {
928          unsigned int pid;          unsigned int pid;
929          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
930          ccs_check_read_lock();          bool global_pid = false;
931          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "allow_execute")) {
932                  head->read_execute_only = true;                  head->r.print_execute_only = true;
933                  return true;                  return true;
934          }          }
935          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
936                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
937                  struct task_struct *p;                  struct task_struct *p;
938                  /***** CRITICAL SECTION START *****/                  ccs_tasklist_lock();
939                  read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
940                    if (global_pid)
941                            p = ccsecurity_exports.find_task_by_pid_ns(pid,
942                                                                   &init_pid_ns);
943                    else
944                            p = ccsecurity_exports.find_task_by_vpid(pid);
945    #else
946                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
947    #endif
948                  if (p)                  if (p)
949                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
950                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
                 /***** CRITICAL SECTION END *****/  
951          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
952                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
953                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
954          } else          } else
955                  return false;                  return false;
956          head->write_var1 = domain;          head->w.domain = domain;
957          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
958          if (!head->read_buf)          if (!head->read_buf)
959                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
960          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
961            head->r.print_this_domain_only = true;
962            head->r.eof = !domain;
963            head->r.domain = &domain->list;
964          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
965          head->read_single_domain = true;          if (domain && domain->is_deleted)
966          head->read_eof = !domain;                  ccs_set_string(head, "# This is a deleted domain.\n");
         if (domain) {  
                 struct ccs_domain_info *d;  
                 head->read_var1 = NULL;  
                 list_for_each_entry_rcu(d, &ccs_domain_list, list) {  
                         if (d == domain)  
                                 break;  
                         head->read_var1 = &d->list;  
                 }  
                 head->read_var2 = NULL;  
                 head->read_bit = 0;  
                 head->read_step = 0;  
                 if (domain->is_deleted)  
                         ccs_io_printf(head, "# This is a deleted domain.\n");  
         }  
967          return true;          return true;
968  }  }
969    
970    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
971                                 const bool is_delete)
972    {
973            static const struct {
974                    const char *keyword;
975                    int (*write) (char *, struct ccs_domain_info *,
976                                  struct ccs_condition *, const bool);
977            } ccs_callback[5] = {
978                    { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
979                    { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
980                    { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
981                    { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
982                    { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
983            };
984            int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
985                          const bool) = ccs_write_file;
986            int error;
987            u8 i;
988            struct ccs_condition *cond = NULL;
989            char *cp = ccs_find_condition_part(data);
990            if (cp) {
991                    cond = ccs_get_condition(cp);
992                    if (!cond)
993                            return -EINVAL;
994            }
995            for (i = 0; i < 5; i++) {
996                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
997                            continue;
998                    write = ccs_callback[i].write;
999                    break;
1000            }
1001            error = write(data, domain, cond, is_delete);
1002            if (cond)
1003                    ccs_put_condition(cond);
1004            return error;
1005    }
1006    
1007    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1008            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
1009            [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",
1010            [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]
1011            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",
1012            [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]
1013            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",
1014            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
1015    };
1016            
1017  /**  /**
1018   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
1019   *   *
1020   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1021   *   *
1022   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1023   */   */
1024  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
1025  {  {
1026          char *data = head->write_buf;          char *data = head->write_buf;
1027          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1028          bool is_delete = false;          bool is_delete = false;
1029          bool is_select = false;          bool is_select = false;
1030          unsigned int profile;          unsigned int profile;
1031          struct ccs_condition *cond = NULL;          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
         char *cp;  
         int error;  
         if (ccs_str_starts(&data, KEYWORD_DELETE))  
1032                  is_delete = true;                  is_delete = true;
1033          else if (ccs_str_starts(&data, KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
1034                  is_select = true;                  is_select = true;
1035          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1036                  return 0;                  return 0;
1037          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1038          if (!ccs_is_policy_manager())          if (!ccs_manager())
1039                  return -EPERM;                  return -EPERM;
1040          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
1041                  domain = NULL;                  domain = NULL;
1042                  if (is_delete)                  if (is_delete)
1043                          ccs_delete_domain(data);                          ccs_delete_domain(data);
1044                  else if (is_select)                  else if (is_select)
1045                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1046                  else                  else
1047                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
1048                  head->write_var1 = domain;                  head->w.domain = domain;
1049                  return 0;                  return 0;
1050          }          }
1051          if (!domain)          if (!domain)
1052                  return -EINVAL;                  return -EINVAL;
1053    
1054          if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
1055              && profile < MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
1056                  if (ccs_profile_ptr[profile] || !ccs_policy_loaded)                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1057                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1058                  return 0;                  return 0;
1059          }          }
1060          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1061                  domain->ignore_global_allow_read = !is_delete;                  const char *cp = ccs_dif[profile];
1062                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
         }  
         if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {  
                 domain->ignore_global_allow_env = !is_delete;  
                 return 0;  
         }  
         cp = ccs_find_condition_part(data);  
         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);  
 }  
   
 /**  
  * ccs_print_single_path_acl - Print a single path ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_single_path_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,  
                                       struct ccs_single_path_acl_record *ptr,  
                                       const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {  
                 const char *msg;  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 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)))  
1063                          continue;                          continue;
1064                  msg = ccs_sp2keyword(bit);                  domain->flags[profile] = !is_delete;
1065                  pos = head->read_avail;                  return 0;
                 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;  
1066          }          }
1067          head->read_bit = 0;          return ccs_write_domain2(data, domain, is_delete);
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1068  }  }
1069    
1070  /**  /**
1071   * ccs_print_mkdev_acl - Print a mkdev ACL entry.   * ccs_print_name_union - Print a ccs_name_union.
1072   *   *
1073   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1074   * @ptr:  Pointer to "struct ccs_mkdev_acl_record".   * @ptr:  Pointer to "struct ccs_name_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
1075   */   */
1076  static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1077                                  struct ccs_mkdev_acl_record *ptr,                                   const struct ccs_name_union *ptr)
                                 const struct ccs_condition *cond)  
1078  {  {
1079          int pos;          const bool cond = head->r.print_cond_part;
1080          u8 bit;          if (!cond)
1081          const u16 perm = ptr->perm;                  ccs_set_space(head);
1082          for (bit = head->read_bit; bit < MAX_MKDEV_OPERATION; bit++) {          if (ptr->is_group) {
1083                  const char *msg;                  ccs_set_string(head, "@");
1084                  if (!(perm & (1 << bit)))                  ccs_set_string(head, ptr->group->group_name->name);
1085                          continue;          } else {
1086                  msg = ccs_mkdev2keyword(bit);                  if (cond)
1087                  pos = head->read_avail;                          ccs_set_string(head, "\"");
1088                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  ccs_set_string(head, ptr->filename->name);
1089                      !ccs_print_name_union(head, ptr->name_is_group,                  if (cond)
1090                                            &ptr->name) ||                          ccs_set_string(head, "\"");
                     !ccs_print_number_union(head, ptr->major_is_group,  
                                             &ptr->major) ||  
                     !ccs_print_number_union(head, ptr->minor_is_group,  
                                             &ptr->minor) ||  
                     !ccs_print_condition(head, cond))  
                         goto out;  
1091          }          }
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1092  }  }
1093    
1094  /**  /**
1095   * ccs_print_double_path_acl - Print a double path ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1096   *   *
1097   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1098   * @ptr:  Pointer to "struct ccs_double_path_acl_record".   * @ptr:  Pointer to "struct ccs_number_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
1099   */   */
1100  static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1101                                        struct ccs_double_path_acl_record *ptr,                                     const struct ccs_number_union *ptr)
                                       const struct ccs_condition *cond)  
1102  {  {
1103          int pos;          if (!head->r.print_cond_part)
1104          u8 bit;                  ccs_set_space(head);
1105          const u8 perm = ptr->perm;          if (ptr->is_group) {
1106          for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {                  ccs_set_string(head, "@");
1107                  const char *msg;                  ccs_set_string(head, ptr->group->group_name->name);
1108                  if (!(perm & (1 << bit)))          } else {
1109                          continue;                  int i;
1110                  msg = ccs_dp2keyword(bit);                  unsigned long min = ptr->values[0];
1111                  pos = head->read_avail;                  const unsigned long max = ptr->values[1];
1112                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  u8 min_type = ptr->value_type[0];
1113                      !ccs_print_name_union(head, ptr->name1_is_group,                  const u8 max_type = ptr->value_type[1];
1114                                            &ptr->name1) ||                  char buffer[128];
1115                      !ccs_print_name_union(head, ptr->name2_is_group,                  buffer[0] = '\0';
1116                                            &ptr->name2) ||                  for (i = 0; i < 2; i++) {
1117                      !ccs_print_condition(head, cond))                          switch (min_type) {
1118                          goto out;                          case CCS_VALUE_TYPE_HEXADECIMAL:
1119                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1120                                                  min);
1121                                    break;
1122                            case CCS_VALUE_TYPE_OCTAL:
1123                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1124                                                  min);
1125                                    break;
1126                            default:
1127                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1128                                                  min);
1129                                    break;
1130                            }
1131                            if (min == max && min_type == max_type)
1132                                    break;
1133                            ccs_addprintf(buffer, sizeof(buffer), "-");
1134                            min_type = max_type;
1135                            min = max;
1136                    }
1137                    ccs_io_printf(head, "%s", buffer);
1138          }          }
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1139  }  }
1140    
1141  /**  /**
1142   * ccs_print_ioctl_acl - Print an ioctl ACL entry.   * ccs_print_condition - Print condition part.
1143   *   *
1144   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1145   * @ptr:  Pointer to "struct ccs_ioctl_acl_record".   * @cond: Pointer to "struct ccs_condition".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1146   *   *
1147   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1148   */   */
1149  static bool ccs_print_ioctl_acl(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
                                 struct ccs_ioctl_acl_record *ptr,  
1150                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1151  {  {
1152          int pos = head->read_avail;          switch (head->r.cond_step) {
1153          if (!ccs_io_printf(head, KEYWORD_ALLOW_IOCTL) ||          case 0:
1154              !ccs_print_name_union(head, ptr->name_is_group, &ptr->name) ||                  {
1155              !ccs_print_number_union(head, ptr->cmd_is_group, &ptr->cmd) ||                          if (cond->condc)
1156              !ccs_print_condition(head, cond))                                  ccs_set_string(head, " if");
1157                  goto out;                          head->r.cond_index = 0;
1158          return true;                          head->r.cond_step++;
1159   out:                  }
1160          head->read_avail = pos;                  /* fall through */
1161          return false;          case 1:
1162  }                  {
1163                            const u16 condc = cond->condc;
1164  /**                          const struct ccs_condition_element *condp =
1165   * ccs_print_argv0_acl - Print an argv[0] ACL entry.                                  (typeof(condp)) (cond + 1);
1166   *                          const struct ccs_number_union *numbers_p =
1167   * @head: Pointer to "struct ccs_io_buffer".                                  (typeof(numbers_p)) (condp + condc);
1168   * @ptr:  Pointer to "struct ccs_argv0_acl_record".                          const struct ccs_name_union *names_p =
1169   * @cond: Pointer to "struct ccs_condition". May be NULL.                                  (typeof(names_p))
1170   *                                  (numbers_p + cond->numbers_count);
1171   * Returns true on success, false otherwise.                          const struct ccs_argv *argv =
1172   */                                  (typeof(argv)) (names_p + cond->names_count);
1173  static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,                          const struct ccs_envp *envp =
1174                                  struct ccs_argv0_acl_record *ptr,                                  (typeof(envp)) (argv + cond->argc);
1175                                  const struct ccs_condition *cond)                          u16 skip;
1176  {                          for (skip = 0; skip < head->r.cond_index; skip++) {
1177          int pos = head->read_avail;                                  const u8 left = condp->left;
1178          if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",                                  const u8 right = condp->right;
1179                             ptr->filename->name, ptr->argv0->name))                                  condp++;
1180                  goto out;                                  switch (left) {
1181          if (!ccs_print_condition(head, cond))                                  case CCS_ARGV_ENTRY:
1182                  goto out;                                          argv++;
1183          return true;                                          continue;
1184   out:                                  case CCS_ENVP_ENTRY:
1185          head->read_avail = pos;                                          envp++;
1186          return false;                                          continue;
1187  }                                  case CCS_NUMBER_UNION:
1188                                            numbers_p++;
1189  /**                                          break;
1190   * ccs_print_env_acl - Print an evironment variable name's ACL entry.                                  }
1191   *                                  switch (right) {
1192   * @head: Pointer to "struct ccs_io_buffer".                                  case CCS_NAME_UNION:
1193   * @ptr:  Pointer to "struct ccs_env_acl_record".                                          names_p++;
1194   * @cond: Pointer to "struct ccs_condition". May be NULL.                                          break;
1195   *                                  case CCS_NUMBER_UNION:
1196   * Returns true on success, false otherwise.                                          numbers_p++;
1197   */                                          break;
1198  static bool ccs_print_env_acl(struct ccs_io_buffer *head,                                  }
1199                                struct ccs_env_acl_record *ptr,                          }
1200                                const struct ccs_condition *cond)                          while (head->r.cond_index < condc) {
1201  {                                  const u8 match = condp->equals;
1202          int pos = head->read_avail;                                  const u8 left = condp->left;
1203          if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))                                  const u8 right = condp->right;
1204                  goto out;                                  if (!ccs_flush(head))
1205          if (!ccs_print_condition(head, cond))                                          return false;
1206                  goto out;                                  condp++;
1207          return true;                                  head->r.cond_index++;
1208   out:                                  ccs_set_space(head);
1209          head->read_avail = pos;                                  switch (left) {
1210          return false;                                  case CCS_ARGV_ENTRY:
1211  }                                          ccs_io_printf(head,
1212                                                          "exec.argv[%u]%s\"%s\"",
1213  /**                                                        argv->index,
1214   * ccs_print_capability_acl - Print a capability ACL entry.                                                        argv->is_not ?
1215   *                                                        "!=" : "=",
1216   * @head: Pointer to "struct ccs_io_buffer".                                                        argv->value->name);
1217   * @ptr:  Pointer to "struct ccs_capability_acl_record".                                          argv++;
1218   * @cond: Pointer to "struct ccs_condition". May be NULL.                                          continue;
1219   *                                  case CCS_ENVP_ENTRY:
1220   * Returns true on success, false otherwise.                                          ccs_io_printf(head,
1221   */                                                        "exec.envp[\"%s\"]%s",
1222  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,                                                        envp->name->name,
1223                                       struct ccs_capability_acl_record *ptr,                                                        envp->is_not ?
1224                                       const struct ccs_condition *cond)                                                        "!=" : "=");
1225  {                                          if (envp->value) {
1226          int pos = head->read_avail;                                                  ccs_set_string(head, "\"");
1227          if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",                                                  ccs_set_string(head, envp->
1228                             ccs_cap2keyword(ptr->operation)))                                                                 value->name);
1229                  goto out;                                                  ccs_set_string(head, "\"");
1230          if (!ccs_print_condition(head, cond))                                          } else {
1231                  goto out;                                                  ccs_set_string(head, "NULL");
1232          return true;                                          }
1233   out:                                          envp++;
1234          head->read_avail = pos;                                          continue;
1235                                    case CCS_NUMBER_UNION:
1236                                            ccs_print_number_union(head,
1237                                                                   numbers_p++);
1238                                            break;
1239                                    default:
1240                                            ccs_set_string(head,
1241                                                   ccs_condition_keyword[left]);
1242                                            break;
1243                                    }
1244                                    ccs_set_string(head, match ? "=" : "!=");
1245                                    switch (right) {
1246                                    case CCS_NAME_UNION:
1247                                            ccs_print_name_union(head, names_p++);
1248                                            break;
1249                                    case CCS_NUMBER_UNION:
1250                                            ccs_print_number_union(head,
1251                                                                   numbers_p++);
1252                                            break;
1253                                    default:
1254                                            ccs_set_string(head,
1255                                                   ccs_condition_keyword[right]);
1256                                            break;
1257                                    }
1258                            }
1259                    }
1260                    head->r.cond_step++;
1261                    /* fall through */
1262            case 2:
1263                    if (!ccs_flush(head))
1264                            break;
1265                    head->r.cond_step++;
1266                    /* fall through */
1267            case 3:
1268                    {
1269                            u8 j;
1270                            const u8 i = cond->post_state[3];
1271                            if (i)
1272                                    ccs_set_string(head, " ; set");
1273                            for (j = 0; j < 3; j++)
1274                                    if ((i & (1 << j)))
1275                                            ccs_io_printf(head,
1276                                                          " task.state[%u]=%u", j,
1277                                                          cond->post_state[j]);
1278                            if (i & (1 << 4))
1279                                    ccs_io_printf(head, " audit=%s",
1280                                                  ccs_yesno(cond->post_state[4]));
1281                    }
1282                    ccs_set_lf(head);
1283                    return true;
1284            }
1285          return false;          return false;
1286  }  }
1287    
1288  /**  /**
1289   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.   * ccs_fns - Find next set bit.
1290   *   *
1291   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1292   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @bit:  First bit to find.
1293   *   *
1294   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1295   */   */
1296  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                                  struct ccs_ip_network_acl_record *ptr)  
1297  {  {
1298          const u32 min_address = ptr->address.ipv4.min;          for ( ; bit < 8; bit++)
1299          const u32 max_address = ptr->address.ipv4.max;                  if (perm & (1 << bit))
1300          if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))                          break;
1301                  return false;          return bit;
         if (min_address != max_address  
             && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))  
                 return false;  
         return true;  
1302  }  }
1303    
1304  /**  /**
1305   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_print_entry - Print an ACL entry.
1306   *   *
1307   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1308   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @acl:  Pointer to an ACL entry.
1309   *   *
1310   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1311   */   */
1312  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1313                                   struct ccs_ip_network_acl_record *ptr)                              const struct ccs_acl_info *acl)
1314  {  {
1315          char buf[64];          const u8 acl_type = acl->type;
1316          const struct in6_addr *min_address = ptr->address.ipv6.min;          u8 bit;
1317          const struct in6_addr *max_address = ptr->address.ipv6.max;          if (head->r.print_cond_part)
1318          ccs_print_ipv6(buf, sizeof(buf), min_address);                  goto print_cond_part;
1319          if (!ccs_io_printf(head, "%s", buf))          if (acl->is_deleted)
1320                    return true;
1321     next:
1322            bit = head->r.bit;
1323            if (!ccs_flush(head))
1324                  return false;                  return false;
1325          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1326                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1327                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1328                    const u16 perm = ptr->perm;
1329                    for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1330                            if (!(perm & (1 << bit)))
1331                                    continue;
1332                            if (head->r.print_execute_only &&
1333                                bit != CCS_TYPE_EXECUTE && bit != CCS_TYPE_TRANSIT)
1334                                    continue;
1335                            /* Print "read/write" instead of "read" and "write". */
1336                            if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1337                                && (perm & (1 << CCS_TYPE_READ_WRITE)))
1338                                    continue;
1339                            break;
1340                    }
1341                    if (bit >= CCS_MAX_PATH_OPERATION)
1342                            goto done;
1343                    ccs_io_printf(head, "allow_%s", ccs_path_keyword[bit]);
1344                    ccs_print_name_union(head, &ptr->name);
1345            } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1346                       acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1347                    struct ccs_execute_handler *ptr
1348                            = container_of(acl, typeof(*ptr), head);
1349                    ccs_io_printf(head, "%s ",
1350                                  acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1351                                  CCS_KEYWORD_EXECUTE_HANDLER :
1352                                  CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1353                    ccs_set_string(head, ptr->handler->name);
1354            } else if (head->r.print_execute_only) {
1355                    return true;
1356            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1357                    struct ccs_mkdev_acl *ptr =
1358                            container_of(acl, typeof(*ptr), head);
1359                    bit = ccs_fns(ptr->perm, bit);
1360                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1361                            goto done;
1362                    ccs_io_printf(head, "allow_%s", ccs_mkdev_keyword[bit]);
1363                    ccs_print_name_union(head, &ptr->name);
1364                    ccs_print_number_union(head, &ptr->mode);
1365                    ccs_print_number_union(head, &ptr->major);
1366                    ccs_print_number_union(head, &ptr->minor);
1367            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1368                    struct ccs_path2_acl *ptr =
1369                            container_of(acl, typeof(*ptr), head);
1370                    bit = ccs_fns(ptr->perm, bit);
1371                    if (bit >= CCS_MAX_PATH2_OPERATION)
1372                            goto done;
1373                    ccs_io_printf(head, "allow_%s", ccs_path2_keyword[bit]);
1374                    ccs_print_name_union(head, &ptr->name1);
1375                    ccs_print_name_union(head, &ptr->name2);
1376            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1377                    struct ccs_path_number_acl *ptr =
1378                            container_of(acl, typeof(*ptr), head);
1379                    bit = ccs_fns(ptr->perm, bit);
1380                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1381                            goto done;
1382                    ccs_io_printf(head, "allow_%s",
1383                                  ccs_path_number_keyword[bit]);
1384                    ccs_print_name_union(head, &ptr->name);
1385                    ccs_print_number_union(head, &ptr->number);
1386            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1387                    struct ccs_env_acl *ptr =
1388                            container_of(acl, typeof(*ptr), head);
1389                    ccs_set_string(head, CCS_KEYWORD_ALLOW_ENV);
1390                    ccs_set_string(head, ptr->env->name);
1391            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1392                    struct ccs_capability_acl *ptr =
1393                            container_of(acl, typeof(*ptr), head);
1394                    ccs_set_string(head, CCS_KEYWORD_ALLOW_CAPABILITY);
1395                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1396            } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1397                    struct ccs_ip_network_acl *ptr =
1398                            container_of(acl, typeof(*ptr), head);
1399                    bit = ccs_fns(ptr->perm, bit);
1400                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1401                            goto done;
1402                    ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",
1403                                  ccs_net_keyword[bit]);
1404                    switch (ptr->address_type) {
1405                            char buf[128];
1406                    case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1407                            ccs_set_string(head, "@");
1408                            ccs_set_string(head,
1409                                           ptr->address.group->group_name->name);
1410                            break;
1411                    case CCS_IP_ADDRESS_TYPE_IPv4:
1412                            ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1413                                           ptr->address.ipv4.max);
1414                            ccs_io_printf(head, "%s", buf);
1415                            break;
1416                    case CCS_IP_ADDRESS_TYPE_IPv6:
1417                            ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1418                                           ptr->address.ipv6.max);
1419                            ccs_io_printf(head, "%s", buf);
1420                            break;
1421                    }
1422                    ccs_print_number_union(head, &ptr->port);
1423            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1424                    struct ccs_signal_acl *ptr =
1425                            container_of(acl, typeof(*ptr), head);
1426                    ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u ", ptr->sig);
1427                    ccs_set_string(head, ptr->domainname->name);
1428            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1429                    struct ccs_mount_acl *ptr =
1430                            container_of(acl, typeof(*ptr), head);
1431                    ccs_io_printf(head, "allow_mount");
1432                    ccs_print_name_union(head, &ptr->dev_name);
1433                    ccs_print_name_union(head, &ptr->dir_name);
1434                    ccs_print_name_union(head, &ptr->fs_type);
1435                    ccs_print_number_union(head, &ptr->flags);
1436            }
1437            head->r.bit = bit + 1;
1438            if (acl->cond) {
1439                    head->r.print_cond_part = true;
1440                    head->r.cond_step = 0;
1441                    if (!ccs_flush(head))
1442                            return false;
1443     print_cond_part:
1444                    if (!ccs_print_condition(head, acl->cond))
1445                          return false;                          return false;
1446                    head->r.print_cond_part = false;
1447            } else {
1448                    ccs_set_lf(head);
1449          }          }
1450          return true;          switch (acl_type) {
1451  }          case CCS_TYPE_PATH_ACL:
1452            case CCS_TYPE_MKDEV_ACL:
1453  /**          case CCS_TYPE_PATH2_ACL:
1454   * ccs_print_network_acl - Print a network ACL entry.          case CCS_TYPE_PATH_NUMBER_ACL:
1455   *          case CCS_TYPE_IP_NETWORK_ACL:
1456   * @head: Pointer to "struct ccs_io_buffer".                  goto next;
  * @ptr:  Pointer to "struct ccs_ip_network_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_network_acl(struct ccs_io_buffer *head,  
                                   struct ccs_ip_network_acl_record *ptr,  
                                   const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",  
                            ccs_net2keyword(ptr->operation_type)))  
                 goto out;  
         switch (ptr->record_type) {  
         case IP_RECORD_TYPE_ADDRESS_GROUP:  
                 if (!ccs_io_printf(head, "@%s",  
                                    ptr->address.group->group_name->name))  
                         goto out;  
                 break;  
         case IP_RECORD_TYPE_IPv4:  
                 if (!ccs_print_ipv4_entry(head, ptr))  
                         goto out;  
                 break;  
         case IP_RECORD_TYPE_IPv6:  
                 if (!ccs_print_ipv6_entry(head, ptr))  
                         goto out;  
                 break;  
1457          }          }
1458          if (!ccs_print_number_union(head, ptr->port_is_group, &ptr->port) ||   done:
1459              !ccs_print_condition(head, cond))          head->r.bit = 0;
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_signal_acl - Print a signal ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct signale_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  
                                  struct ccs_signal_acl_record *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",  
                            ptr->sig, ptr->domainname->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_execute_handler_record - Print an execute handler ACL entry.  
  *  
  * @head:    Pointer to "struct ccs_io_buffer".  
  * @keyword: Name of the keyword.  
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  
                                              const char *keyword,  
                                              struct ccs_execute_handler_record *  
                                              ptr)  
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mount_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mount_acl_record *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_MOUNT "%s %s %s 0x%lX\n",  
                            ptr->dev_name->name, ptr->dir_name->name,  
                            ptr->fs_type->name, ptr->flags))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_umount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_umount_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_umount_acl(struct ccs_io_buffer *head,  
                                  struct ccs_umount_acl_record *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_UNMOUNT "%s\n",  
                            ptr->dir->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_chroot_acl - Print a chroot ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_chroot_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,  
                                  struct ccs_chroot_acl_record *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_CHROOT "%s\n",  
                            ptr->dir->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
1460          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1461  }  }
1462    
1463  /**  /**
1464   * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.   * ccs_read_domain2 - Read domain policy.
1465   *   *
1466   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1467   * @ptr:  Pointer to "struct ccs_pivot_root_acl_record".   * @domain: Pointer to "struct ccs_domain_info".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1468   *   *
1469   * Returns true on success, false otherwise.   * Caller holds ccs_read_lock().
  */  
 static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  
                                      struct ccs_pivot_root_acl_record *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_PIVOT_ROOT "%s %s\n",  
                            ptr->new_root->name, ptr->old_root->name))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_entry - Print an ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to an ACL entry.  
1470   *   *
1471   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1472   */   */
1473  static bool ccs_print_entry(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1474                              struct ccs_acl_info *ptr)                               struct ccs_domain_info *domain)
1475  {  {
1476          const struct ccs_condition *cond = ptr->cond;          list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1477          const u8 acl_type = ccs_acl_type2(ptr);                  struct ccs_acl_info *ptr =
1478          if (acl_type & ACL_DELETED)                          list_entry(head->r.acl, typeof(*ptr), list);
1479                  return true;                  if (!ccs_print_entry(head, ptr))
1480          if (acl_type == TYPE_SINGLE_PATH_ACL) {                          return false;
                 struct ccs_single_path_acl_record *acl  
                         = container_of(ptr, struct ccs_single_path_acl_record,  
                                        head);  
                 return ccs_print_single_path_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = KEYWORD_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(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);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == TYPE_MKDEV_ACL) {  
                 struct ccs_mkdev_acl_record *acl  
                         = container_of(ptr, struct ccs_mkdev_acl_record, head);  
                 return ccs_print_mkdev_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_DOUBLE_PATH_ACL) {  
                 struct ccs_double_path_acl_record *acl  
                         = container_of(ptr, struct ccs_double_path_acl_record,  
                                        head);  
                 return ccs_print_double_path_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_IOCTL_ACL) {  
                 struct ccs_ioctl_acl_record *acl  
                         = container_of(ptr, struct ccs_ioctl_acl_record, head);  
                 return ccs_print_ioctl_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_ARGV0_ACL) {  
                 struct ccs_argv0_acl_record *acl  
                         = container_of(ptr, struct ccs_argv0_acl_record, head);  
                 return ccs_print_argv0_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_ENV_ACL) {  
                 struct ccs_env_acl_record *acl  
                         = container_of(ptr, struct ccs_env_acl_record, head);  
                 return ccs_print_env_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl_record *acl  
                         = container_of(ptr, struct ccs_capability_acl_record,  
                                        head);  
                 return ccs_print_capability_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl_record *acl  
                         = container_of(ptr, struct ccs_ip_network_acl_record,  
                                        head);  
                 return ccs_print_network_acl(head, acl, cond);  
         }  
         if (acl_type == TYPE_SIGNAL_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);  
1481          }          }
1482          /* Workaround for gcc 3.2.2's inline bug. */          head->r.acl = NULL;
1483          if (acl_type & ACL_DELETED)          return true;
                 return true;  
         BUG(); /* This must not happen. */  
         return false;  
1484  }  }
1485    
1486  /**  /**
1487   * ccs_read_domain_policy - Read domain policy.   * ccs_read_domain - Read domain policy.
1488   *   *
1489   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1490   *   *
  * Returns 0.  
  *  
1491   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1492   */   */
1493  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1494  {  {
1495          struct list_head *dpos;          if (head->r.eof)
1496          struct list_head *apos;                  return;
1497          ccs_check_read_lock();          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1498          if (head->read_eof)                  struct ccs_domain_info *domain =
1499                  return 0;                          list_entry(head->r.domain, typeof(*domain), list);
1500          if (head->read_step == 0)                  switch (head->r.step) {
1501                  head->read_step = 1;                          u8 i;
1502          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                  case 0:
1503                  struct ccs_domain_info *domain;                          if (domain->is_deleted &&
1504                  const char *quota_exceeded = "";                              !head->r.print_this_domain_only)
1505                  const char *transition_failed = "";                                  continue;
1506                  const char *ignore_global_allow_read = "";                          /* Print domainname and flags. */
1507                  const char *ignore_global_allow_env = "";                          ccs_set_string(head, domain->domainname->name);
1508                  domain = list_entry(dpos, struct ccs_domain_info, list);                          ccs_set_lf(head);
1509                  if (head->read_step != 1)                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1510                          goto acl_loop;                                        domain->profile);
1511                  if (domain->is_deleted && !head->read_single_domain)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1512                          continue;                                  if (domain->flags[i])
1513                  /* Print domainname and flags. */                                          ccs_set_string(head, ccs_dif[i]);
1514                  if (domain->quota_warned)                          head->r.step++;
1515                          quota_exceeded = "quota_exceeded\n";                          ccs_set_lf(head);
1516                  if (domain->domain_transition_failed)                          /* fall through */
1517                          transition_failed = "transition_failed\n";                  case 1:
1518                  if (domain->ignore_global_allow_read)                          if (!ccs_read_domain2(head, domain))
1519                          ignore_global_allow_read                                  return;
1520                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          head->r.step++;
1521                  if (domain->ignore_global_allow_env)                          if (!ccs_set_lf(head))
1522                          ignore_global_allow_env                                  return;
1523                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          /* fall through */
1524                  if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"                  case 2:
1525                                     "%s%s%s%s\n", domain->domainname->name,                          head->r.step = 0;
1526                                     domain->profile, quota_exceeded,                          if (head->r.print_this_domain_only)
1527                                     transition_failed,                                  goto done;
1528                                     ignore_global_allow_read,                  }
                                    ignore_global_allow_env))  
                         return 0;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         goto tail_mark;  
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
                                 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;  
1529          }          }
1530          head->read_eof = true;   done:
1531          return 0;          head->r.eof = true;
1532  }  }
1533    
1534  /**  /**
# Line 1302  static int ccs_read_domain_policy(struct Line 1541  static int ccs_read_domain_policy(struct
1541   * This is equivalent to doing   * This is equivalent to doing
1542   *   *
1543   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1544   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1545   *   *
1546   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1547   */   */
# Line 1312  static int ccs_write_domain_profile(stru Line 1551  static int ccs_write_domain_profile(stru
1551          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1552          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1553          unsigned int profile;          unsigned int profile;
         ccs_check_read_lock();  
1554          if (!cp)          if (!cp)
1555                  return -EINVAL;                  return -EINVAL;
1556          *cp = '\0';          *cp = '\0';
1557          profile = simple_strtoul(data, NULL, 10);          profile = simple_strtoul(data, NULL, 10);
1558          if (profile >= MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1559                  return -EINVAL;                  return -EINVAL;
1560          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1561          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1562                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1563          return 0;          return 0;
1564  }  }
# Line 1330  static int ccs_write_domain_profile(stru Line 1568  static int ccs_write_domain_profile(stru
1568   *   *
1569   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1570   *   *
  * Returns list of profile number and domainname pairs.  
  *  
1571   * This is equivalent to doing   * This is equivalent to doing
1572   *   *
1573   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1341  static int ccs_write_domain_profile(stru Line 1577  static int ccs_write_domain_profile(stru
1577   *   *
1578   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1579   */   */
1580  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1581  {  {
1582          struct list_head *pos;          if (head->r.eof)
1583          ccs_check_read_lock();                  return;
1584          if (head->read_eof)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1585                  return 0;                  struct ccs_domain_info *domain =
1586          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
                 struct ccs_domain_info *domain;  
                 domain = list_entry(pos, struct ccs_domain_info, list);  
1587                  if (domain->is_deleted)                  if (domain->is_deleted)
1588                          continue;                          continue;
1589                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
1590                                     domain->domainname->name))                          return;
1591                          return 0;                  ccs_io_printf(head, "%u ", domain->profile);
1592                    ccs_set_string(head, domain->domainname->name);
1593                    ccs_set_lf(head);
1594          }          }
1595          head->read_eof = true;          head->r.eof = true;
         return 0;  
1596  }  }
1597    
1598  /**  /**
# Line 1369  static int ccs_read_domain_profile(struc Line 1604  static int ccs_read_domain_profile(struc
1604   */   */
1605  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1606  {  {
1607          head->read_eof = false;          head->r.eof = false;
1608          return 0;          return 0;
1609  }  }
1610    
# Line 1384  static int ccs_write_pid(struct ccs_io_b Line 1619  static int ccs_write_pid(struct ccs_io_b
1619   *   *
1620   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1621   */   */
1622  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1623  {  {
1624          char *buf = head->write_buf;          char *buf = head->write_buf;
1625          bool task_info = false;          bool task_info = false;
1626            bool global_pid = false;
1627          unsigned int pid;          unsigned int pid;
1628          struct task_struct *p;          struct task_struct *p;
1629          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1630          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_check_read_lock();  
1631          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1632          if (!buf)          if (!buf) {
1633                  goto done; /* Do nothing if open(O_RDONLY). */                  head->r.eof = true;
1634          if (head->read_avail || head->read_eof)                  return; /* Do nothing if open(O_RDONLY). */
1635                  goto done;          }
1636          head->read_eof = true;          if (head->r.w_pos || head->r.eof)
1637                    return;
1638            head->r.eof = true;
1639          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1640                  task_info = true;                  task_info = true;
1641            if (ccs_str_starts(&buf, "global-pid "))
1642                    global_pid = true;
1643          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1644          /***** CRITICAL SECTION START *****/          ccs_tasklist_lock();
1645          read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1646            if (global_pid)
1647                    p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1648            else
1649                    p = ccsecurity_exports.find_task_by_vpid(pid);
1650    #else
1651          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1652    #endif
1653          if (p) {          if (p) {
1654                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1655                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1656          }          }
1657          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
         /***** CRITICAL SECTION END *****/  
1658          if (!domain)          if (!domain)
1659                  goto done;                  return;
1660          if (!task_info)          if (!task_info) {
1661                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1662                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1663          else          } else {
1664                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1665                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1666                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                ccs_yesno(ccs_flags &
1667                                "yes" : "no",                                          CCS_TASK_IS_MANAGER),
1668                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                ccs_yesno(ccs_flags &
1669                                "yes" : "no",                                          CCS_TASK_IS_EXECUTE_HANDLER),
1670                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1671                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1672                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1673   done:          }
         return 0;  
1674  }  }
1675    
1676    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1677            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1678            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1679            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1680            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1681            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1682    };
1683    
1684    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1685            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1686            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1687            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1688    };
1689    
1690  /**  /**
1691   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1692   *   *
1693   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1694   *   *
1695   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1696   */   */
1697  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1698  {  {
1699          char *data = head->write_buf;          char *data = head->write_buf;
1700          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1701          if (ccs_str_starts(&data, KEYWORD_KEEP_DOMAIN))          u8 i;
1702                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1703          if (ccs_str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1704                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1705          if (ccs_str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[4] = {
1706                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1707                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1708          if (ccs_str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1709                  return ccs_write_domain_initializer_policy(data, true,                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1710                                                             is_delete);          };
1711          if (ccs_str_starts(&data, KEYWORD_AGGREGATOR))          for (i = 0; i < 4; i++) {
1712                  return ccs_write_aggregator_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1713          if (ccs_str_starts(&data, KEYWORD_ALLOW_READ))                          return ccs_callback[i].write(data, is_delete);
1714                  return ccs_write_globally_readable_policy(data, is_delete);          }
1715          if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) {
1716                  return ccs_write_globally_usable_env_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1717          if (ccs_str_starts(&data, KEYWORD_FILE_PATTERN))                          return ccs_write_transition_control(data, is_delete,
1718                  return ccs_write_pattern_policy(data, is_delete);                                                              i);
1719          if (ccs_str_starts(&data, KEYWORD_PATH_GROUP))          }
1720                  return ccs_write_path_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++) {
1721          if (ccs_str_starts(&data, KEYWORD_NUMBER_GROUP))                  if (ccs_str_starts(&data, ccs_group_name[i]))
1722                  return ccs_write_number_group_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1723          if (ccs_str_starts(&data, KEYWORD_DENY_REWRITE))          }
1724                  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;  
1725  }  }
1726    
1727  /**  /**
1728   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1729   *   *
1730   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1731     * @idx:  Index number.
1732   *   *
1733   * Returns 0 on success, -EINVAL otherwise.   * Returns true on success, false otherwise.
1734   *   *
1735   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1736   */   */
1737  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1738  {  {
1739          ccs_check_read_lock();          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1740          if (!head->read_eof) {                  struct ccs_group *group =
1741                  switch (head->read_step) {                          list_entry(head->r.group, typeof(*group), head.list);
1742                  case 0:                  list_for_each_cookie(head->r.acl, &group->member_list) {
1743                          head->read_var2 = NULL;                          struct ccs_acl_head *ptr =
1744                          head->read_step = 1;                                  list_entry(head->r.acl, typeof(*ptr), list);
1745                  case 1:                          if (ptr->is_deleted)
1746                          if (!ccs_read_domain_keeper_policy(head))                                  continue;
1747                                  break;                          if (!ccs_flush(head))
1748                          head->read_var2 = NULL;                                  return false;
1749                          head->read_step = 2;                          ccs_set_string(head, ccs_group_name[idx]);
1750                  case 2:                          ccs_set_string(head, group->group_name->name);
1751                          if (!ccs_read_globally_readable_policy(head))                          if (idx == CCS_PATH_GROUP) {
1752                                  break;                                  ccs_set_space(head);
1753                          head->read_var2 = NULL;                                  ccs_set_string(head, container_of
1754                          head->read_step = 3;                                                 (ptr, struct ccs_path_group,
1755                  case 3:                                                  head)->member_name->name);
1756                          if (!ccs_read_globally_usable_env_policy(head))                          } else if (idx == CCS_NUMBER_GROUP) {
1757                                  break;                                  ccs_print_number_union(head, &container_of
1758                          head->read_var2 = NULL;                                                         (ptr, struct ccs_number_group,
1759                          head->read_step = 4;                                                          head)->number);
1760                  case 4:                          } else if (idx == CCS_ADDRESS_GROUP) {
1761                          if (!ccs_read_domain_initializer_policy(head))                                  char buffer[128];
1762                                  break;                                  struct ccs_address_group *member =
1763                          head->read_var2 = NULL;                                          container_of(ptr, typeof(*member),
1764                          head->read_step = 6;                                                       head);
1765                  case 6:                                  if (member->is_ipv6)
1766                          if (!ccs_read_aggregator_policy(head))                                          ccs_print_ipv6(buffer, sizeof(buffer),
1767                                  break;                                                         member->min.ipv6,
1768                          head->read_var2 = NULL;                                                         member->max.ipv6);
1769                          head->read_step = 7;                                  else
1770                  case 7:                                          ccs_print_ipv4(buffer, sizeof(buffer),
1771                          if (!ccs_read_file_pattern(head))                                                         member->min.ipv4,
1772                                  break;                                                         member->max.ipv4);
1773                          head->read_var2 = NULL;                                  ccs_io_printf(head, " %s", buffer);
1774                          head->read_step = 8;                          }
1775                  case 8:                          ccs_set_lf(head);
1776                          if (!ccs_read_no_rewrite_policy(head))                  }
1777                                  break;                  head->r.acl = NULL;
1778                          head->read_var2 = NULL;          }
1779                          head->read_step = 9;          head->r.group = NULL;
1780                  case 9:          return true;
1781                          if (!ccs_read_path_group_policy(head))  }
1782                                  break;  
1783                          head->read_var1 = NULL;  /**
1784                          head->read_var2 = NULL;   * ccs_read_policy - Read "struct ccs_..._entry" list.
1785                          head->read_step = 10;   *
1786                  case 10:   * @head: Pointer to "struct ccs_io_buffer".
1787                          if (!ccs_read_number_group_policy(head))   * @idx:  Index number.
1788                                  break;   *
1789                          head->read_var1 = NULL;   * Returns true on success, false otherwise.
1790                          head->read_var2 = NULL;   *
1791                          head->read_step = 11;   * Caller holds ccs_read_lock().
1792                  case 11:   */
1793                          if (!ccs_read_address_group_policy(head))  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1794                                  break;  {
1795                          head->read_var2 = NULL;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1796                          head->read_step = 12;                  struct ccs_acl_head *acl =
1797                  case 12:                          container_of(head->r.acl, typeof(*acl), list);
1798                          if (!ccs_read_reserved_port_policy(head))                  if (acl->is_deleted)
1799                                  break;                          continue;
1800                          head->read_eof = true;                  if (!ccs_flush(head))
1801                            return false;
1802                    switch (idx) {
1803                    case CCS_ID_TRANSITION_CONTROL:
1804                            {
1805                                    struct ccs_transition_control *ptr =
1806                                            container_of(acl, typeof(*ptr), head);
1807                                    ccs_set_string(head,
1808                                                   ccs_transition_type[ptr->type]);
1809                                    ccs_set_string(head, ptr->program ?
1810                                                   ptr->program->name : "any");
1811                                    ccs_set_string(head, " from ");
1812                                    ccs_set_string(head, ptr->domainname ?
1813                                                   ptr->domainname->name : "any");
1814                            }
1815                            break;
1816                    case CCS_ID_AGGREGATOR:
1817                            {
1818                                    struct ccs_aggregator *ptr =
1819                                            container_of(acl, typeof(*ptr), head);
1820                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1821                                    ccs_set_string(head, ptr->original_name->name);
1822                                    ccs_set_space(head);
1823                                    ccs_set_string(head,
1824                                                   ptr->aggregated_name->name);
1825                            }
1826                            break;
1827                    case CCS_ID_PATTERN:
1828                            {
1829                                    struct ccs_pattern *ptr =
1830                                            container_of(acl, typeof(*ptr), head);
1831                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1832                                    ccs_set_string(head, ptr->pattern->name);
1833                            }
1834                            break;
1835                    case CCS_ID_NO_REWRITE:
1836                            {
1837                                    struct ccs_no_rewrite *ptr =
1838                                            container_of(acl, typeof(*ptr), head);
1839                                    ccs_set_string(head, CCS_KEYWORD_DENY_REWRITE);
1840                                    ccs_set_string(head, ptr->pattern->name);
1841                            }
1842                            break;
1843                    case CCS_ID_RESERVEDPORT:
1844                            {
1845                                    struct ccs_reserved *ptr =
1846                                            container_of(acl, typeof(*ptr), head);
1847                                    const u16 min_port = ptr->min_port;
1848                                    const u16 max_port = ptr->max_port;
1849                                    ccs_set_string(head,
1850                                                   CCS_KEYWORD_DENY_AUTOBIND);
1851                                    ccs_io_printf(head, "%u", min_port);
1852                                    if (min_port != max_port)
1853                                            ccs_io_printf(head, "-%u", max_port);
1854                            }
1855                          break;                          break;
1856                  default:                  default:
1857                          return -EINVAL;                          continue;
1858                  }                  }
1859                    ccs_set_lf(head);
1860          }          }
1861          return 0;          head->r.acl = NULL;
1862            return true;
1863    }
1864    
1865    /**
1866     * ccs_read_exception - Read exception policy.
1867     *
1868     * @head: Pointer to "struct ccs_io_buffer".
1869     *
1870     * Caller holds ccs_read_lock().
1871     */
1872    static void ccs_read_exception(struct ccs_io_buffer *head)
1873    {
1874            if (head->r.eof)
1875                    return;
1876            while (head->r.step < CCS_MAX_POLICY &&
1877                   ccs_read_policy(head, head->r.step))
1878                    head->r.step++;
1879            if (head->r.step < CCS_MAX_POLICY)
1880                    return;
1881            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1882                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1883                    head->r.step++;
1884            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1885                    return;
1886            head->r.eof = ccs_read_domain2(head, &ccs_global_domain);
1887  }  }
1888    
1889  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 1575  static LIST_HEAD(ccs_query_list); Line 1908  static LIST_HEAD(ccs_query_list);
1908  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1909  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1910    
1911    static void ccs_truncate(char *str)
1912    {
1913            while (* (unsigned char *) str > (unsigned char) ' ')
1914                    str++;
1915            *str = '\0';
1916    }
1917    
1918  /**  /**
1919   * ccs_check_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1920   *   *
1921   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1922   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1923   *   *
1924   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1925   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1926   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1927   * -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1928   */   */
1929  int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1930  {  {
1931          va_list args;          va_list args;
1932          int error = -EPERM;          int error = -EPERM;
# Line 1596  int ccs_check_supervisor(struct ccs_requ Line 1936  int ccs_check_supervisor(struct ccs_requ
1936          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query_entry *ccs_query_entry = NULL;
1937          bool quota_exceeded = false;          bool quota_exceeded = false;
1938          char *header;          char *header;
1939          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1940                  r->domain = ccs_current_domain();          va_start(args, fmt);
1941            len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1942            va_end(args);
1943            if (r->mode == CCS_CONFIG_LEARNING) {
1944                    char *buffer;
1945                    char *realpath = NULL;
1946                    char *argv0 = NULL;
1947                    char *symlink = NULL;
1948                    char *handler = NULL;
1949                    const struct ccs_preference *pref;
1950                    if (!ccs_domain_quota_ok(r))
1951                            return 0;
1952                    header = ccs_init_log(&len, r);
1953                    if (!header)
1954                            return 0;
1955                    pref = ccs_profile(r->profile)->learning;
1956                    /* strstr() will return NULL if ordering is wrong. */
1957                    if (r->param_type == CCS_TYPE_PATH_ACL &&
1958                        r->param.path.operation == CCS_TYPE_EXECUTE) {
1959                            if (pref->learning_exec_argv0) {
1960                                    argv0 = strstr(header, " argv[]={ \"");
1961                                    if (argv0) {
1962                                            argv0 += 10;
1963                                            ccs_truncate(argv0);
1964                                    }
1965                            }
1966                            if (pref->learning_exec_realpath) {
1967                                    realpath = strstr(header,
1968                                                      " exec={ realpath=\"");
1969                                    if (realpath) {
1970                                            realpath += 8;
1971                                            ccs_truncate(realpath);
1972                                    }
1973                            }
1974                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1975                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1976                               pref->learning_symlink_target) {
1977                            symlink = strstr(header, " symlink.target=\"");
1978                            if (symlink)
1979                                    ccs_truncate(symlink + 1);
1980                    }
1981                    handler = strstr(header, "type=execute_handler");
1982                    if (handler)
1983                            ccs_truncate(handler);
1984                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1985                    if (buffer) {
1986                            va_start(args, fmt);
1987                            vsnprintf(buffer, len - 1, fmt, args);
1988                            va_end(args);
1989                            if (handler || realpath || argv0 || symlink) {
1990                                    ccs_addprintf(buffer, len, " if");
1991                                    if (handler)
1992                                            ccs_addprintf(buffer, len, " task.%s",
1993                                                          handler);
1994                                    if (realpath)
1995                                            ccs_addprintf(buffer, len, " exec.%s",
1996                                                          realpath);
1997                                    if (argv0)
1998                                            ccs_addprintf(buffer, len,
1999                                                          " exec.argv[0]=%s",
2000                                                          argv0);
2001                                    if (symlink)
2002                                            ccs_addprintf(buffer, len, "%s",
2003                                                          symlink);
2004                            }
2005                            ccs_normalize_line(buffer);
2006                            ccs_write_domain2(buffer, domain, false);
2007                            kfree(buffer);
2008                    }
2009                    kfree(header);
2010                    return 0;
2011            }
2012            if (r->mode != CCS_CONFIG_ENFORCING)
2013                    return 0;
2014          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2015                  int i;                  int i;
2016                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2017                          return -EPERM;                          return -EPERM;
2018                  for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2019                       i++) {                               enforcing_penalty; i++) {
2020                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2021                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2022                  }                  }
2023                  return -EPERM;                  return -EPERM;
2024          }          }
2025          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);  
2026          if (!header)          if (!header)
2027                  goto out;                  goto out;
2028          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2029          if (!ccs_query_entry)          if (!ccs_query_entry)
2030                  goto out;                  goto out;
2031          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          len = ccs_round2(len);
2032            ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2033          if (!ccs_query_entry->query)          if (!ccs_query_entry->query)
2034                  goto out;                  goto out;
2035          INIT_LIST_HEAD(&ccs_query_entry->list);          INIT_LIST_HEAD(&ccs_query_entry->list);
         /***** CRITICAL SECTION START *****/  
2036          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2037          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2038              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
# Line 1632  int ccs_check_supervisor(struct ccs_requ Line 2042  int ccs_check_supervisor(struct ccs_requ
2042                  ccs_query_entry->serial = ccs_serial++;                  ccs_query_entry->serial = ccs_serial++;
2043          }          }
2044          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2045          if (quota_exceeded)          if (quota_exceeded)
2046                  goto out;                  goto out;
2047          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
# Line 1643  int ccs_check_supervisor(struct ccs_requ Line 2052  int ccs_check_supervisor(struct ccs_requ
2052          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);
2053          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
2054          va_end(args);          va_end(args);
         /***** CRITICAL SECTION START *****/  
2055          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2056          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2057          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2058          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2059          for (ccs_query_entry->timer = 0;          for (ccs_query_entry->timer = 0;
2060               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;
# Line 1658  int ccs_check_supervisor(struct ccs_requ Line 2065  int ccs_check_supervisor(struct ccs_requ
2065                  if (ccs_query_entry->answer)                  if (ccs_query_entry->answer)
2066                          break;                          break;
2067          }          }
         /***** CRITICAL SECTION START *****/  
2068          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2069          list_del(&ccs_query_entry->list);          list_del(&ccs_query_entry->list);
2070          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
2071          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2072          switch (ccs_query_entry->answer) {          switch (ccs_query_entry->answer) {
2073          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2074                  error = 1;                  error = CCS_RETRY_REQUEST;
2075                  r->retry++;                  r->retry++;
2076                  break;                  break;
2077          case 1:          case 1:
# Line 1704  static int ccs_poll_query(struct file *f Line 2109  static int ccs_poll_query(struct file *f
2109          bool found = false;          bool found = false;
2110          u8 i;          u8 i;
2111          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
                 /***** CRITICAL SECTION START *****/  
2112                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2113                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2114                          struct ccs_query_entry *ptr                          struct ccs_query_entry *ptr =
2115                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, struct ccs_query_entry, list);
2116                          if (ptr->answer)                          if (ptr->answer)
2117                                  continue;                                  continue;
2118                          found = true;                          found = true;
2119                          break;                          break;
2120                  }                  }
2121                  spin_unlock(&ccs_query_list_lock);                  spin_unlock(&ccs_query_list_lock);
                 /***** CRITICAL SECTION END *****/  
2122                  if (found)                  if (found)
2123                          return POLLIN | POLLRDNORM;                          return POLLIN | POLLRDNORM;
2124                  if (i)                  if (i)
# Line 1729  static int ccs_poll_query(struct file *f Line 2132  static int ccs_poll_query(struct file *f
2132   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2133   *   *
2134   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
2135   */   */
2136  static int ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2137  {  {
2138          struct list_head *tmp;          struct list_head *tmp;
2139          int pos = 0;          int pos = 0;
2140          int len = 0;          int len = 0;
2141          char *buf;          char *buf;
2142          if (head->read_avail)          if (head->r.w_pos)
2143                  return 0;                  return;
2144          if (head->read_buf) {          if (head->read_buf) {
2145                  kfree(head->read_buf);                  kfree(head->read_buf);
2146                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2147          }          }
         /***** CRITICAL SECTION START *****/  
2148          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2149          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2150                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
2151                          = list_entry(tmp, struct ccs_query_entry, list);                          = list_entry(tmp, struct ccs_query_entry, list);
2152                  if (ptr->answer)                  if (ptr->answer)
2153                          continue;                          continue;
2154                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2155                          continue;                          continue;
2156                  len = ptr->query_len;                  len = ptr->query_len;
2157                  break;                  break;
2158          }          }
2159          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2160          if (!len) {          if (!len) {
2161                  head->read_step = 0;                  head->r.query_index = 0;
2162                  return 0;                  return;
2163          }          }
2164          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2165          if (!buf)          if (!buf)
2166                  return 0;                  return;
2167          pos = 0;          pos = 0;
         /***** CRITICAL SECTION START *****/  
2168          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2169          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2170                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
2171                          = list_entry(tmp, struct ccs_query_entry, list);                          = list_entry(tmp, struct ccs_query_entry, list);
2172                  if (ptr->answer)                  if (ptr->answer)
2173                          continue;                          continue;
2174                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2175                          continue;                          continue;
2176                  /*                  /*
2177                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 1785  static int ccs_read_query(struct ccs_io_ Line 2182  static int ccs_read_query(struct ccs_io_
2182                  break;                  break;
2183          }          }
2184          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2185          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2186                  head->read_buf = buf;                  head->read_buf = buf;
2187                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2188                    head->r.query_index++;
2189          } else {          } else {
2190                  kfree(buf);                  kfree(buf);
2191          }          }
         return 0;  
2192  }  }
2193    
2194  /**  /**
# Line 1810  static int ccs_write_answer(struct ccs_i Line 2204  static int ccs_write_answer(struct ccs_i
2204          struct list_head *tmp;          struct list_head *tmp;
2205          unsigned int serial;          unsigned int serial;
2206          unsigned int answer;          unsigned int answer;
         /***** CRITICAL SECTION START *****/  
2207          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2208          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2209                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1818  static int ccs_write_answer(struct ccs_i Line 2211  static int ccs_write_answer(struct ccs_i
2211                  ptr->timer = 0;                  ptr->timer = 0;
2212          }          }
2213          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2214          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2215                  return -EINVAL;                  return -EINVAL;
         /***** CRITICAL SECTION START *****/  
2216          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2217          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2218                  struct ccs_query_entry *ptr                  struct ccs_query_entry *ptr
# Line 1833  static int ccs_write_answer(struct ccs_i Line 2224  static int ccs_write_answer(struct ccs_i
2224                  break;                  break;
2225          }          }
2226          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2227          return 0;          return 0;
2228  }  }
2229    
# Line 1841  static int ccs_write_answer(struct ccs_i Line 2231  static int ccs_write_answer(struct ccs_i
2231   * ccs_read_version: Get version.   * ccs_read_version: Get version.
2232   *   *
2233   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns version information.  
2234   */   */
2235  static int ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2236  {  {
2237          if (!head->read_eof) {          if (head->r.eof)
2238                  ccs_io_printf(head, "1.7.0-pre");                  return;
2239                  head->read_eof = true;          ccs_set_string(head, "1.7.2");
2240          }          head->r.eof = true;
         return 0;  
2241  }  }
2242    
2243  /**  /**
2244   * ccs_read_self_domain - Get the current process's domainname.   * ccs_read_self_domain - Get the current process's domainname.
2245   *   *
2246   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns the current process's domainname.  
2247   */   */
2248  static int ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2249  {  {
2250          if (!head->read_eof) {          if (head->r.eof)
2251                  /*                  return;
2252                   * ccs_current_domain()->domainname != NULL          /*
2253                   * because every process belongs to a domain and           * ccs_current_domain()->domainname != NULL because every process
2254                   * the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2255                   */           */
2256                  ccs_io_printf(head, "%s",          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2257                                ccs_current_domain()->domainname->name);          head->r.eof = true;
                 head->read_eof = true;  
         }  
         return 0;  
2258  }  }
2259    
2260  /**  /**
# Line 1885  static int ccs_read_self_domain(struct c Line 2267  static int ccs_read_self_domain(struct c
2267   */   */
2268  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2269  {  {
2270          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2271          if (!head)          if (!head)
2272                  return -ENOMEM;                  return -ENOMEM;
2273          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2274          head->type = type;          head->type = type;
2275          switch (type) {          switch (type) {
2276          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2277                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2278                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2279                  break;                  break;
2280          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2281                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2282                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2283                  break;                  break;
2284  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2285          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;  
2286          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2287                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2288                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2289                  break;                  break;
2290  #endif  #endif
2291          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 1946  int ccs_open_control(const u8 type, stru Line 2325  int ccs_open_control(const u8 type, stru
2325                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2326                  break;                  break;
2327          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2328                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2329                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2330                  break;                  break;
2331          }          }
2332          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 1957  int ccs_open_control(const u8 type, stru Line 2336  int ccs_open_control(const u8 type, stru
2336                   */                   */
2337                  head->read = NULL;                  head->read = NULL;
2338                  head->poll = NULL;                  head->poll = NULL;
2339          } else if (type != CCS_QUERY &&          } else if (!head->poll) {
2340                     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.  
                  */  
2341                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2342                          head->readbuf_size = 4096 * 2;                          head->readbuf_size = 4096;
2343                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2344                  if (!head->read_buf) {                  if (!head->read_buf) {
2345                          kfree(head);                          kfree(head);
2346                          return -ENOMEM;                          return -ENOMEM;
# Line 1978  int ccs_open_control(const u8 type, stru Line 2353  int ccs_open_control(const u8 type, stru
2353                   */                   */
2354                  head->write = NULL;                  head->write = NULL;
2355          } else if (head->write) {          } else if (head->write) {
2356                  head->writebuf_size = 4096 * 2;                  head->writebuf_size = 4096;
2357                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2358                  if (!head->write_buf) {                  if (!head->write_buf) {
2359                          kfree(head->read_buf);                          kfree(head->read_buf);
2360                          kfree(head);                          kfree(head);
# Line 1988  int ccs_open_control(const u8 type, stru Line 2363  int ccs_open_control(const u8 type, stru
2363          }          }
2364          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2365              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2366                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2367          file->private_data = head;          file->private_data = head;
2368          /*          /*
2369           * 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 1999  int ccs_open_control(const u8 type, stru Line 2374  int ccs_open_control(const u8 type, stru
2374                  ccs_read_control(file, NULL, 0);                  ccs_read_control(file, NULL, 0);
2375          /*          /*
2376           * If the file is /proc/ccs/query , increment the observer counter.           * If the file is /proc/ccs/query , increment the observer counter.
2377           * The obserber counter is used by ccs_check_supervisor() to see if           * The obserber counter is used by ccs_supervisor() to see if
2378           * there is some process monitoring /proc/ccs/query.           * there is some process monitoring /proc/ccs/query.
2379           */           */
2380          else if (type == CCS_QUERY)          else if (type == CCS_QUERY)
# Line 2014  int ccs_open_control(const u8 type, stru Line 2389  int ccs_open_control(const u8 type, stru
2389   * @wait: Pointer to "poll_table".   * @wait: Pointer to "poll_table".
2390   *   *
2391   * Waits for read readiness.   * Waits for read readiness.
2392   * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2393   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2394   * /usr/lib/ccs/ccs-auditd.   * /usr/sbin/ccs-auditd .
2395   */   */
2396  int ccs_poll_control(struct file *file, poll_table *wait)  int ccs_poll_control(struct file *file, poll_table *wait)
2397  {  {
# Line 2038  int ccs_poll_control(struct file *file, Line 2413  int ccs_poll_control(struct file *file,
2413  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2414                       const int buffer_len)                       const int buffer_len)
2415  {  {
2416          int len = 0;          int len;
2417          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2418          char *cp;          int idx;
2419          if (!head->read)          if (!head->read)
2420                  return -ENOSYS;                  return -ENOSYS;
2421          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2422                  return -EFAULT;                  return -EFAULT;
2423          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2424                  return -EINTR;                  return -EINTR;
2425          /* Call the policy handler. */          head->read_user_buf = buffer;
2426          len = head->read(head);          head->read_user_buf_avail = buffer_len;
2427          if (len < 0)          idx = ccs_read_lock();
2428                  goto out;          if (ccs_flush(head))
2429          /* Write to buffer. */                  /* Call the policy handler. */
2430          len = head->read_avail;                  head->read(head);
2431          if (len > buffer_len)          ccs_flush(head);
2432                  len = buffer_len;          ccs_read_unlock(idx);
2433          if (!len)          len = head->read_user_buf - buffer;
                 goto out;  
         /* head->read_buf changes by some functions. */  
         cp = head->read_buf;  
         if (copy_to_user(buffer, cp, len)) {  
                 len = -EFAULT;  
                 goto out;  
         }  
         head->read_avail -= len;  
         memmove(cp, cp + len, head->read_avail);  
  out:  
2434          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2435          return len;          return len;
2436  }  }
# Line 2086  int ccs_write_control(struct file *file, Line 2451  int ccs_write_control(struct file *file,
2451          int error = buffer_len;          int error = buffer_len;
2452          int avail_len = buffer_len;          int avail_len = buffer_len;
2453          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2454            int idx;
2455          if (!head->write)          if (!head->write)
2456                  return -ENOSYS;                  return -ENOSYS;
2457          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2458                  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;  
2459          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2460                  return -EINTR;                  return -EINTR;
2461            idx = ccs_read_lock();
2462            /* Don't allow updating policies by non manager programs. */
2463            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2464                !ccs_manager()) {
2465                    ccs_read_unlock(idx);
2466                    mutex_unlock(&head->io_sem);
2467                    return -EPERM;
2468            }
2469          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2470          while (avail_len > 0) {          while (avail_len > 0) {
2471                  char c;                  char c;
2472                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->w.avail >= head->writebuf_size - 1) {
2473                          error = -ENOMEM;                          const int len = head->writebuf_size * 2;
2474                          break;                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2475                  } else if (get_user(c, buffer)) {                          if (!cp) {
2476                                    error = -ENOMEM;
2477                                    break;
2478                            }
2479                            memmove(cp, cp0, head->w.avail);
2480                            kfree(cp0);
2481                            head->write_buf = cp;
2482                            cp0 = cp;