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

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 2890 by kumaneko, Tue Aug 11 00:44:58 2009 UTC revision 3832 by kumaneko, Wed Jul 21 06:01:31 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/07/21
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 12  Line 12 
12    
13  #include "internal.h"  #include "internal.h"
14    
15  /* Lock for protecting ccs_profile->comment  */  static struct ccs_profile ccs_default_profile = {
16  static DEFINE_SPINLOCK(ccs_profile_comment_lock);          .learning = &ccs_default_profile.preference,
17            .permissive = &ccs_default_profile.preference,
18            .enforcing = &ccs_default_profile.preference,
19            .audit = &ccs_default_profile.preference,
20    #ifdef CONFIG_CCSECURITY_AUDIT
21            .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
22            .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
23    #endif
24            .preference.audit_task_info = true,
25            .preference.audit_path_info = true,
26            .preference.enforcing_penalty = 0,
27            .preference.enforcing_verbose = true,
28            .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
29            .preference.learning_verbose = false,
30            .preference.learning_exec_realpath = true,
31            .preference.learning_exec_argv0 = true,
32            .preference.learning_symlink_target = true,
33            .preference.permissive_verbose = true
34    };
35    
36  static bool ccs_profile_entry_used[CCS_MAX_CONTROL_INDEX +  /* Profile version. Currently only 20090903 is defined. */
37                                     CCS_MAX_CAPABILITY_INDEX + 1];  static unsigned int ccs_profile_version;
38    
39    /* Profile table. Memory is allocated as needed. */
40    static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
42  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
43  static const char *ccs_mode_4[4] = {  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_FILEATTR]    = { "MAC_FOR_FILEATTR",    0, 3 },          [CCS_MAC_FILE_CREATE]
59          [CCS_MAC_FOR_ARGV0]       = { "MAC_FOR_ARGV0",       0, 3 },          = "file::create",
60          [CCS_MAC_FOR_ENV]         = { "MAC_FOR_ENV",         0, 3 },          [CCS_MAC_FILE_UNLINK]
61          [CCS_MAC_FOR_NETWORK]     = { "MAC_FOR_NETWORK",     0, 3 },          = "file::unlink",
62          [CCS_MAC_FOR_SIGNAL]      = { "MAC_FOR_SIGNAL",      0, 3 },          [CCS_MAC_FILE_MKDIR]
63          [CCS_MAC_FOR_NAMESPACE]   = { "MAC_FOR_NAMESPACE",   0, 3 },          = "file::mkdir",
64          [CCS_RESTRICT_AUTOBIND]   = { "RESTRICT_AUTOBIND",   0, 1 },          [CCS_MAC_FILE_RMDIR]
65          [CCS_MAX_ACCEPT_ENTRY]          = "file::rmdir",
66          = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },          [CCS_MAC_FILE_MKFIFO]
67  #ifdef CONFIG_CCSECURITY_AUDIT          = "file::mkfifo",
68          [CCS_MAX_GRANT_LOG]          [CCS_MAC_FILE_MKSOCK]
69          = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },          = "file::mksock",
70          [CCS_MAX_REJECT_LOG]          [CCS_MAC_FILE_TRUNCATE]
71          = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },          = "file::truncate",
72  #endif          [CCS_MAC_FILE_SYMLINK]
73          [CCS_VERBOSE]             = { "TOMOYO_VERBOSE",      1, 1 },          = "file::symlink",
74          [CCS_SLEEP_PERIOD]          [CCS_MAC_FILE_MKBLOCK]
75          = { "SLEEP_PERIOD",        0, 3000 }, /* in 0.1 second */          = "file::mkblock",
76            [CCS_MAC_FILE_MKCHAR]
77            = "file::mkchar",
78            [CCS_MAC_FILE_LINK]
79            = "file::link",
80            [CCS_MAC_FILE_RENAME]
81            = "file::rename",
82            [CCS_MAC_FILE_CHMOD]
83            = "file::chmod",
84            [CCS_MAC_FILE_CHOWN]
85            = "file::chown",
86            [CCS_MAC_FILE_CHGRP]
87            = "file::chgrp",
88            [CCS_MAC_FILE_IOCTL]
89            = "file::ioctl",
90            [CCS_MAC_FILE_CHROOT]
91            = "file::chroot",
92            [CCS_MAC_FILE_MOUNT]
93            = "file::mount",
94            [CCS_MAC_FILE_UMOUNT]
95            = "file::umount",
96            [CCS_MAC_FILE_PIVOT_ROOT]
97            = "file::pivot_root",
98            [CCS_MAC_FILE_TRANSIT]
99            = "file::transit",
100            [CCS_MAC_ENVIRON]
101            = "misc::env",
102            [CCS_MAC_NETWORK_UDP_BIND]
103            = "network::inet_udp_bind",
104            [CCS_MAC_NETWORK_UDP_CONNECT]
105            = "network::inet_udp_connect",
106            [CCS_MAC_NETWORK_TCP_BIND]
107            = "network::inet_tcp_bind",
108            [CCS_MAC_NETWORK_TCP_LISTEN]
109            = "network::inet_tcp_listen",
110            [CCS_MAC_NETWORK_TCP_CONNECT]
111            = "network::inet_tcp_connect",
112            [CCS_MAC_NETWORK_TCP_ACCEPT]
113            = "network::inet_tcp_accept",
114            [CCS_MAC_NETWORK_RAW_BIND]
115            = "network::inet_raw_bind",
116            [CCS_MAC_NETWORK_RAW_CONNECT]
117            = "network::inet_raw_connect",
118            [CCS_MAC_SIGNAL]
119            = "ipc::signal",
120            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]
121            = "capability::inet_tcp_create",
122            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]
123            = "capability::inet_tcp_listen",
124            [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]
125            = "capability::inet_tcp_connect",
126            [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]
127            = "capability::use_inet_udp",
128            [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]
129            = "capability::use_inet_ip",
130            [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
131            = "capability::use_route",
132            [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
133            = "capability::use_packet",
134            [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]
135            = "capability::SYS_MOUNT",
136            [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]
137            = "capability::SYS_UMOUNT",
138            [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
139            = "capability::SYS_REBOOT",
140            [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]
141            = "capability::SYS_CHROOT",
142            [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]
143            = "capability::SYS_KILL",
144            [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
145            = "capability::SYS_VHANGUP",
146            [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
147            = "capability::SYS_TIME",
148            [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]
149            = "capability::SYS_NICE",
150            [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]
151            = "capability::SYS_SETHOSTNAME",
152            [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
153            = "capability::use_kernel_module",
154            [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]
155            = "capability::create_fifo",
156            [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]
157            = "capability::create_block_dev",
158            [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]
159            = "capability::create_char_dev",
160            [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]
161            = "capability::create_unix_socket",
162            [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]
163            = "capability::SYS_LINK",
164            [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]
165            = "capability::SYS_SYMLINK",
166            [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]
167            = "capability::SYS_RENAME",
168            [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]
169            = "capability::SYS_UNLINK",
170            [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]
171            = "capability::SYS_CHMOD",
172            [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]
173            = "capability::SYS_CHOWN",
174            [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]
175            = "capability::SYS_IOCTL",
176            [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
177            = "capability::SYS_KEXEC_LOAD",
178            [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]
179            = "capability::SYS_PIVOT_ROOT",
180            [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
181            = "capability::SYS_PTRACE",
182            [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]
183            = "capability::conceal_mount",
184            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
185             + CCS_MAC_CATEGORY_FILE] = "file",
186            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
187             + CCS_MAC_CATEGORY_NETWORK] = "network",
188            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
189             + CCS_MAC_CATEGORY_MISC] = "misc",
190            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
191             + CCS_MAC_CATEGORY_IPC] = "ipc",
192            [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
193             + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
194  };  };
195    
196  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
197  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
198    
199  /**  /**
200   * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.   * ccs_cap2keyword - Convert capability operation to capability name.
201   *   *
202   * @str: Unused.   * @operation: The capability index.
203   *   *
204   * Returns 0.   * Returns the name of the specified capability's name.
205   */   */
206  static int __init ccs_quiet_setup(char *str)  const char *ccs_cap2keyword(const u8 operation)
207  {  {
208          ccs_control_array[CCS_VERBOSE].current_value = 0;          return operation < CCS_MAX_CAPABILITY_INDEX
209          return 0;                  ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;
210  }  }
211    
212  __setup("CCS_QUIET", ccs_quiet_setup);  /**
213     * ccs_yesno - Return "yes" or "no".
214     *
215     * @value: Bool value.
216     */
217    static const char *ccs_yesno(const unsigned int value)
218    {
219            return value ? "yes" : "no";
220    }
221    
222    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
223    {
224            va_list args;
225            const int pos = strlen(buffer);
226            va_start(args, fmt);
227            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
228            va_end(args);
229    }
230    
231  /**  /**
232   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
233   *   *
234   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
235   *   *
236   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
237     */
238    static bool ccs_flush(struct ccs_io_buffer *head)
239    {
240            while (head->r.w_pos) {
241                    const char *w = head->r.w[0];
242                    int len = strlen(w);
243                    if (len) {
244                            if (len > head->read_user_buf_avail)
245                                    len = head->read_user_buf_avail;
246                            if (!len)
247                                    return false;
248                            if (copy_to_user(head->read_user_buf, w, len))
249                                    return false;
250                            head->read_user_buf_avail -= len;
251                            head->read_user_buf += len;
252                            w += len;
253                    }
254                    if (*w) {
255                            head->r.w[0] = w;
256                            return false;
257                    }
258                    /* Add '\0' for audit logs and query. */
259                    if (head->poll) {
260                            if (!head->read_user_buf_avail ||
261                                copy_to_user(head->read_user_buf, "", 1))
262                                    return false;
263                            head->read_user_buf_avail--;
264                            head->read_user_buf++;
265                    }
266                    head->r.w_pos--;
267                    for (len = 0; len < head->r.w_pos; len++)
268                            head->r.w[len] = head->r.w[len + 1];
269            }
270            head->r.avail = 0;
271            return true;
272    }
273    
274    /**
275     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
276     *
277     * @head:   Pointer to "struct ccs_io_buffer".
278     * @string: String to print.
279   *   *
280   * The snprintf() will truncate, but ccs_io_printf() won't.   * Note that @string has to be kept valid until @head is kfree()d.
281     * This means that char[] allocated on stack memory cannot be passed to
282     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
283   */   */
284  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
285    {
286            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
287                    head->r.w[head->r.w_pos++] = string;
288                    ccs_flush(head);
289            } else
290                    printk(KERN_WARNING "Too many words in a line.\n");
291    }
292    
293    /**
294     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
295     *
296     * @head: Pointer to "struct ccs_io_buffer".
297     * @fmt:  The printf()'s format string, followed by parameters.
298     */
299    void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
300  {  {
301          va_list args;          va_list args;
302          int len;          int len;
303          int pos = head->read_avail;          int pos = head->r.avail;
304          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
305          if (size <= 0)          if (size <= 0)
306                  return false;                  return;
307          va_start(args, fmt);          va_start(args, fmt);
308          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
309          va_end(args);          va_end(args);
310          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
311                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
312          head->read_avail += len;                  return;
313          return true;          }
314            head->r.avail += len;
315            ccs_set_string(head, head->read_buf + pos);
316    }
317    
318    static void ccs_set_space(struct ccs_io_buffer *head)
319    {
320            ccs_set_string(head, " ");
321    }
322    
323    static bool ccs_set_lf(struct ccs_io_buffer *head)
324    {
325            ccs_set_string(head, "\n");
326            return !head->r.w_pos;
327  }  }
328    
329  /**  /**
330   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
331   *   *
332   * @profile: Profile number to create.   * @profile: Profile number to create.
333   *   *
334   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
335   */   */
336  struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_assign_profile(const unsigned int profile)
                                                    profile)  
337  {  {
338          struct ccs_profile *ptr;          struct ccs_profile *ptr;
339          struct ccs_profile *entry;          struct ccs_profile *entry;
340          int i;          if (profile >= CCS_MAX_PROFILES)
         if (profile >= MAX_PROFILES)  
341                  return NULL;                  return NULL;
342          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
343          if (ptr)          if (ptr)
344                  return ptr;                  return ptr;
345          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
346          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
347                    goto out;
348          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
349          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
350                  ptr = entry;                  ptr = entry;
351                  for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)                  ptr->audit = &ccs_default_profile.preference;
352                          ptr->value[i] = ccs_control_array[i].current_value;                  ptr->learning = &ccs_default_profile.preference;
353                  /*                  ptr->permissive = &ccs_default_profile.preference;
354                   * Needn't to initialize "ptr->capability_value"                  ptr->enforcing = &ccs_default_profile.preference;
355                   * because they are always 0.                  ptr->default_config = CCS_CONFIG_DISABLED |
356                   */                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
357                    memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
358                           sizeof(ptr->config));
359                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
360                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
361                  entry = NULL;                  entry = NULL;
362          }          }
363          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
364     out:
365          kfree(entry);          kfree(entry);
366          return ptr;          return ptr;
367  }  }
368    
369  /**  /**
370     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
371     */
372    static void ccs_check_profile(void)
373    {
374            struct ccs_domain_info *domain;
375            const int idx = ccs_read_lock();
376            ccs_policy_loaded = true;
377            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
378                    const u8 profile = domain->profile;
379                    if (ccs_profile_ptr[profile])
380                            continue;
381                    panic("Profile %u (used by '%s') not defined.\n",
382                          profile, domain->domainname->name);
383            }
384            ccs_read_unlock(idx);
385            if (ccs_profile_version != 20090903)
386                    panic("Profile version %u is not supported.\n",
387                          ccs_profile_version);
388            printk(KERN_INFO "CCSecurity: 1.7.2+   2010/07/21\n");
389            printk(KERN_INFO "Mandatory Access Control activated.\n");
390    }
391    
392    /**
393     * ccs_profile - Find a profile.
394     *
395     * @profile: Profile number to find.
396     *
397     * Returns pointer to "struct ccs_profile".
398     */
399    struct ccs_profile *ccs_profile(const u8 profile)
400    {
401            struct ccs_profile *ptr = ccs_profile_ptr[profile];
402            if (!ccs_policy_loaded)
403                    return &ccs_default_profile;
404            BUG_ON(!ptr);
405            return ptr;
406    }
407    
408    static s8 ccs_find_yesno(const char *string, const char *find)
409    {
410            const char *cp = strstr(string, find);
411            if (cp) {
412                    cp += strlen(find);
413                    if (!strncmp(cp, "=yes", 4))
414                            return 1;
415                    else if (!strncmp(cp, "=no", 3))
416                            return 0;
417            }
418            return -1;
419    }
420    
421    static void ccs_set_bool(bool *b, const char *string, const char *find)
422    {
423            switch (ccs_find_yesno(string, find)) {
424            case 1:
425                    *b = true;
426                    break;
427            case 0:
428                    *b = false;
429                    break;
430            }
431    }
432    
433    static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
434    {
435            const char *cp = strstr(string, find);
436            if (cp)
437                    sscanf(cp + strlen(find), "=%u", i);
438    }
439    
440    static void ccs_set_pref(const char *name, const char *value,
441                             const bool use_default, struct ccs_profile *profile)
442    {
443            struct ccs_preference **pref;
444            bool *verbose;
445            if (!strcmp(name, "audit")) {
446                    if (use_default) {
447                            pref = &profile->audit;
448                            goto set_default;
449                    }
450                    profile->audit = &profile->preference;
451    #ifdef CONFIG_CCSECURITY_AUDIT
452                    ccs_set_uint(&profile->preference.audit_max_grant_log, value,
453                                 "max_grant_log");
454                    ccs_set_uint(&profile->preference.audit_max_reject_log, value,
455                                 "max_reject_log");
456    #endif
457                    ccs_set_bool(&profile->preference.audit_task_info, value,
458                                 "task_info");
459                    ccs_set_bool(&profile->preference.audit_path_info, value,
460                                 "path_info");
461                    return;
462            }
463            if (!strcmp(name, "enforcing")) {
464                    if (use_default) {
465                            pref = &profile->enforcing;
466                            goto set_default;
467                    }
468                    profile->enforcing = &profile->preference;
469                    ccs_set_uint(&profile->preference.enforcing_penalty, value,
470                                 "penalty");
471                    verbose = &profile->preference.enforcing_verbose;
472                    goto set_verbose;
473            }
474            if (!strcmp(name, "permissive")) {
475                    if (use_default) {
476                            pref = &profile->permissive;
477                            goto set_default;
478                    }
479                    profile->permissive = &profile->preference;
480                    verbose = &profile->preference.permissive_verbose;
481                    goto set_verbose;
482            }
483            if (!strcmp(name, "learning")) {
484                    if (use_default) {
485                            pref = &profile->learning;
486                            goto set_default;
487                    }
488                    profile->learning = &profile->preference;
489                    ccs_set_uint(&profile->preference.learning_max_entry, value,
490                                 "max_entry");
491                    ccs_set_bool(&profile->preference.learning_exec_realpath,
492                                 value, "exec.realpath");
493                    ccs_set_bool(&profile->preference.learning_exec_argv0, value,
494                                 "exec.argv0");
495                    ccs_set_bool(&profile->preference.learning_symlink_target,
496                                 value, "symlink.target");
497                    verbose = &profile->preference.learning_verbose;
498                    goto set_verbose;
499            }
500            return;
501     set_default:
502            *pref = &ccs_default_profile.preference;
503            return;
504     set_verbose:
505            ccs_set_bool(verbose, value, "verbose");
506    }
507    
508    static int ccs_set_mode(char *name, const char *value, const bool use_default,
509                            struct ccs_profile *profile)
510    {
511            u8 i;
512            u8 config;
513            if (!strcmp(name, "CONFIG")) {
514                    i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
515                            + CCS_MAX_MAC_CATEGORY_INDEX;
516                    config = profile->default_config;
517            } else if (ccs_str_starts(&name, "CONFIG::")) {
518                    config = 0;
519                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
520                                 + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
521                            if (strcmp(name, ccs_mac_keywords[i]))
522                                    continue;
523                            config = profile->config[i];
524                            break;
525                    }
526                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
527                        + CCS_MAX_MAC_CATEGORY_INDEX)
528                            return -EINVAL;
529            } else {
530                    return -EINVAL;
531            }
532            if (use_default) {
533                    config = CCS_CONFIG_USE_DEFAULT;
534            } else {
535                    u8 mode;
536                    for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
537                            if (strstr(value, ccs_mode[mode]))
538                                    /*
539                                     * Update lower 3 bits in order to distinguish
540                                     * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
541                                     */
542                                    config = (config & ~7) | mode;
543    #ifdef CONFIG_CCSECURITY_AUDIT
544                    if (config != CCS_CONFIG_USE_DEFAULT) {
545                            switch (ccs_find_yesno(value, "grant_log")) {
546                            case 1:
547                                    config |= CCS_CONFIG_WANT_GRANT_LOG;
548                                    break;
549                            case 0:
550                                    config &= ~CCS_CONFIG_WANT_GRANT_LOG;
551                                    break;
552                            }
553                            switch (ccs_find_yesno(value, "reject_log")) {
554                            case 1:
555                                    config |= CCS_CONFIG_WANT_REJECT_LOG;
556                                    break;
557                            case 0:
558                                    config &= ~CCS_CONFIG_WANT_REJECT_LOG;
559                                    break;
560                            }
561                    }
562    #endif
563            }
564            if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
565                + CCS_MAX_MAC_CATEGORY_INDEX)
566                    profile->config[i] = config;
567            else if (config != CCS_CONFIG_USE_DEFAULT)
568                    profile->default_config = config;
569            return 0;
570    }
571    
572    /**
573   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
574   *   *
575   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 149  static int ccs_write_profile(struct ccs_ Line 580  static int ccs_write_profile(struct ccs_
580  {  {
581          char *data = head->write_buf;          char *data = head->write_buf;
582          unsigned int i;          unsigned int i;
583          unsigned int value;          bool use_default = false;
584          char *cp;          char *cp;
585          struct ccs_profile *ccs_profile;          struct ccs_profile *profile;
586            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
587                    return 0;
588          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
589          if (data != cp) {          if (data == cp) {
590                    profile = &ccs_default_profile;
591            } else {
592                  if (*cp != '-')                  if (*cp != '-')
593                          return -EINVAL;                          return -EINVAL;
594                  data = cp + 1;                  data = cp + 1;
595                    profile = ccs_assign_profile(i);
596                    if (!profile)
597                            return -EINVAL;
598          }          }
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
                 return -EINVAL;  
599          cp = strchr(data, '=');          cp = strchr(data, '=');
600          if (!cp)          if (!cp)
601                  return -EINVAL;                  return -EINVAL;
602          *cp = '\0';          *cp++ = '\0';
603            if (profile != &ccs_default_profile)
604                    use_default = strstr(cp, "use_default") != NULL;
605            if (ccs_str_starts(&data, "PREFERENCE::")) {
606                    ccs_set_pref(data, cp, use_default, profile);
607                    return 0;
608            }
609            if (profile == &ccs_default_profile)
610                    return -EINVAL;
611          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
612                  const struct ccs_path_info *new_comment                  const struct ccs_path_info *old_comment = profile->comment;
613                          = 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 *****/  
614                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
                 ccs_profile_entry_used[0] = true;  
615                  return 0;                  return 0;
616          }          }
617          if (ccs_str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {          return ccs_set_mode(data, cp, use_default, profile);
618                  if (sscanf(cp + 1, "%u", &value) != 1) {  }
619                          for (i = 0; i < 4; i++) {  
620                                  if (strcmp(cp + 1, ccs_mode_4[i]))  static void ccs_print_preference(struct ccs_io_buffer *head, const int idx)
621                                          continue;  {
622                                  value = i;          struct ccs_preference *pref = &ccs_default_profile.preference;
623                                  break;          const struct ccs_profile *profile = idx >= 0 ?
624                          }                  ccs_profile_ptr[idx] : NULL;
625                          if (i == 4)          char buffer[16] = "";
626                                  return -EINVAL;          if (profile) {
627                  }                  buffer[sizeof(buffer) - 1] = '\0';
628                  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;  
629          }          }
630          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {          if (profile) {
631                  if (strcmp(data, ccs_control_array[i].keyword))                  pref = profile->audit;
632                          continue;                  if (pref == &ccs_default_profile.preference)
633                  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;  
634          }          }
635          return -EINVAL;          ccs_io_printf(head, "%sPREFERENCE::%s={ "
636    #ifdef CONFIG_CCSECURITY_AUDIT
637                          "max_grant_log=%u max_reject_log=%u "
638    #endif
639                          "task_info=%s path_info=%s }\n", buffer,
640                          "audit",
641    #ifdef CONFIG_CCSECURITY_AUDIT
642                          pref->audit_max_grant_log,
643                          pref->audit_max_reject_log,
644    #endif
645                          ccs_yesno(pref->audit_task_info),
646                          ccs_yesno(pref->audit_path_info));
647     skip0:
648            if (profile) {
649                    pref = profile->learning;
650                    if (pref == &ccs_default_profile.preference)
651                            goto skip1;
652            }
653            ccs_io_printf(head, "%sPREFERENCE::%s={ "
654                          "verbose=%s max_entry=%u exec.realpath=%s "
655                          "exec.argv0=%s symlink.target=%s }\n",
656                          buffer, "learning",
657                          ccs_yesno(pref->learning_verbose),
658                          pref->learning_max_entry,
659                          ccs_yesno(pref->learning_exec_realpath),
660                          ccs_yesno(pref->learning_exec_argv0),
661                          ccs_yesno(pref->learning_symlink_target));
662     skip1:
663            if (profile) {
664                    pref = profile->permissive;
665                    if (pref == &ccs_default_profile.preference)
666                            goto skip2;
667            }
668            ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
669                          buffer, "permissive",
670                          ccs_yesno(pref->permissive_verbose));
671     skip2:
672            if (profile) {
673                    pref = profile->enforcing;
674                    if (pref == &ccs_default_profile.preference)
675                            return;
676            }
677            ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
678                          "penalty=%u }\n", buffer, "enforcing",
679                          ccs_yesno(pref->enforcing_verbose),
680                          pref->enforcing_penalty);
681    }
682    
683    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
684    {
685            ccs_io_printf(head, "={ mode=%s", ccs_mode[config & 3]);
686    #ifdef CONFIG_CCSECURITY_AUDIT
687            ccs_io_printf(head, " grant_log=%s reject_log=%s",
688                          ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
689                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
690    #endif
691            ccs_set_string(head, " }\n");
692  }  }
693    
694  /**  /**
695   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
696   *   *
697   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
698   */   */
699  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
700  {  {
701          static const int ccs_total          u8 index;
702                  = CCS_MAX_CONTROL_INDEX + CCS_MAX_CAPABILITY_INDEX + 1;          const struct ccs_profile *profile;
703          int step;   next:
704          if (head->read_eof)          index = head->r.index;
705                  return 0;          profile = ccs_profile_ptr[index];
706          for (step = head->read_step; step < MAX_PROFILES * ccs_total; step++) {          switch (head->r.step) {
707                  const u8 index = step / ccs_total;          case 0:
708                  u8 type = step % ccs_total;                  ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
709                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];                  ccs_print_preference(head, -1);
710                  head->read_step = step;                  head->r.step++;
711                  if (!ccs_profile)                  break;
712                          continue;          case 1:
713                  if (!ccs_profile_entry_used[type])                  for ( ; head->r.index < CCS_MAX_PROFILES;
714                          continue;                        head->r.index++)
715                  if (!type) { /* Print profile' comment tag. */                          if (ccs_profile_ptr[head->r.index])
716                          bool done;                                  break;
717                          /***** CRITICAL SECTION START *****/                  if (head->r.index == CCS_MAX_PROFILES)
718                          spin_lock(&ccs_profile_comment_lock);                          return;
719                          done = ccs_io_printf(head, "%u-COMMENT=%s\n",                  head->r.step++;
720                                               index, ccs_profile->comment ?                  break;
721                                               ccs_profile->comment->name : "");          case 2:
722                          spin_unlock(&ccs_profile_comment_lock);                  {
723                          /***** CRITICAL SECTION END *****/                          const struct ccs_path_info *comment = profile->comment;
724                          if (!done)                          ccs_io_printf(head, "%u-COMMENT=", index);
725                                  break;                          ccs_set_string(head, comment ? comment->name : "");
726                          continue;                          ccs_set_lf(head);
727                            head->r.step++;
728                  }                  }
729                  type--;                  break;
730                  if (type >= CCS_MAX_CONTROL_INDEX) {          case 3:
731                          const int i = type - CCS_MAX_CONTROL_INDEX;                  {
732                          const u8 value = ccs_profile->capability_value[i];                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
733                          if (!ccs_io_printf(head,                          ccs_print_config(head, profile->default_config);
734                                             "%u-" KEYWORD_MAC_FOR_CAPABILITY                          head->r.bit = 0;
735                                             "%s=%s\n", index,                          head->r.step++;
                                            ccs_capability_control_keyword[i],  
                                            ccs_mode_4[value]))  
                                 break;  
                 } else {  
                         const unsigned int value = ccs_profile->value[type];  
                         const char **modes = NULL;  
                         const char *keyword = ccs_control_array[type].keyword;  
                         switch (ccs_control_array[type].max_value) {  
                         case 3:  
                                 modes = ccs_mode_4;  
                                 break;  
                         case 1:  
                                 modes = ccs_mode_2;  
                                 break;  
                         }  
                         if (modes) {  
                                 if (!ccs_io_printf(head, "%u-%s=%s\n", index,  
                                                    keyword, modes[value]))  
                                         break;  
                         } else {  
                                 if (!ccs_io_printf(head, "%u-%s=%u\n", index,  
                                                    keyword, value))  
                                         break;  
                         }  
736                  }                  }
737                    break;
738            case 4:
739                    for ( ; head->r.bit < CCS_MAX_MAC_INDEX
740                                  + CCS_MAX_CAPABILITY_INDEX
741                                  + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
742                            const u8 i = head->r.bit;
743                            const u8 config = profile->config[i];
744                            if (config == CCS_CONFIG_USE_DEFAULT)
745                                    continue;
746                            ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
747                                          ccs_mac_keywords[i]);
748                            ccs_print_config(head, config);
749                            head->r.bit++;
750                            break;
751                    }
752                    if (head->r.bit == CCS_MAX_MAC_INDEX
753                        + CCS_MAX_CAPABILITY_INDEX
754                        + CCS_MAX_MAC_CATEGORY_INDEX) {
755                            ccs_print_preference(head, index);
756                            head->r.index++;
757                            head->r.step = 1;
758                    }
759                    break;
760          }          }
761          if (step == MAX_PROFILES * ccs_total)          if (ccs_flush(head))
762                  head->read_eof = true;                  goto next;
         return 0;  
763  }  }
764    
765  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager(const struct ccs_acl_head *a,
766  LIST_HEAD(ccs_policy_manager_list);                               const struct ccs_acl_head *b)
767    {
768            return container_of(a, struct ccs_manager, head)->manager
769                    == container_of(b, struct ccs_manager, head)->manager;
770    }
771    
772  /**  /**
773   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 323  LIST_HEAD(ccs_policy_manager_list); Line 779  LIST_HEAD(ccs_policy_manager_list);
779   */   */
780  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)
781  {  {
782          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         const struct ccs_path_info *saved_manager;  
783          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
784          bool is_domain = false;          if (ccs_domain_def(manager)) {
785          if (ccs_is_domain_def(manager)) {                  if (!ccs_correct_domain(manager))
                 if (!ccs_is_correct_domain(manager))  
786                          return -EINVAL;                          return -EINVAL;
787                  is_domain = true;                  e.is_domain = true;
788          } else {          } else {
789                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
790                          return -EINVAL;                          return -EINVAL;
791          }          }
792          saved_manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
793          if (!saved_manager)          if (!e.manager)
794                  return -ENOMEM;                  return error;
795          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
796                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
797          mutex_lock(&ccs_policy_lock);                                    ccs_same_manager);
798          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);  
799          return error;          return error;
800  }  }
801    
802  /**  /**
803   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
804   *   *
805   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
806   *   *
807   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
808   */   */
809  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
810  {  {
811          char *data = head->write_buf;          char *data = head->write_buf;
812          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
813          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
814                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
815                  return 0;                  return 0;
# Line 382  static int ccs_write_manager_policy(stru Line 818  static int ccs_write_manager_policy(stru
818  }  }
819    
820  /**  /**
821   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
822   *   *
823   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
824   *   *
  * Returns 0.  
  *  
825   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
826   */   */
827  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
828  {  {
829          struct list_head *pos;          if (head->r.eof)
830          ccs_check_read_lock();                  return;
831          if (head->read_eof)          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
832                  return 0;                  struct ccs_manager *ptr =
833          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {                          list_entry(head->r.acl, typeof(*ptr), head.list);
834                  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)  
835                          continue;                          continue;
836                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
837                          return 0;                          return;
838                    ccs_set_string(head, ptr->manager->name);
839                    ccs_set_lf(head);
840          }          }
841          head->read_eof = true;          head->r.eof = true;
         return 0;  
842  }  }
843    
844  /**  /**
845   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
846   *   *
847   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
848   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
849   *   *
850   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
851   */   */
852  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
853  {  {
854          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
855          const char *exe;          const char *exe;
856          struct task_struct *task = current;          struct task_struct *task = current;
857          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
858                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
859          bool found = false;          bool found = false;
         ccs_check_read_lock();  
860          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
861                  return true;                  return true;
862          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
863                  return true;                  return true;
864          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
865                  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;  
                 }  
         }  
866          exe = ccs_get_exe();          exe = ccs_get_exe();
867          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
868                  return false;                                  head.list) {
869          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
870                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
871                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
872                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
873                          /* Set manager flag. */                                  continue;
874                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
875                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
876                                    continue;
877                  }                  }
878                    /* Set manager flag. */
879                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
880                    found = true;
881                    break;
882          }          }
883          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
884                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 475  static bool ccs_is_policy_manager(void) Line 904  static bool ccs_is_policy_manager(void)
904  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
905  {  {
906          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
907          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
908                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
909                  if (cp)          if (cp)
910                          *cp++ = '\0';                  *cp++ = '\0';
         }  
911          return cp;          return cp;
912  }  }
913    
914  /**  /**
915   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
916   *   *
917   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
918   * @data: String to parse.   * @data: String to parse.
# Line 501  static char *ccs_find_condition_part(cha Line 921  static char *ccs_find_condition_part(cha
921   *   *
922   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
923   */   */
924  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)
925  {  {
926          unsigned int pid;          unsigned int pid;
927          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
928          ccs_check_read_lock();          bool global_pid = false;
929          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
930                  head->read_execute_only = true;                  head->r.print_execute_only = true;
931                  return true;                  return true;
932          }          }
933          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
934                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
935                  struct task_struct *p;                  struct task_struct *p;
936                  /***** CRITICAL SECTION START *****/                  ccs_tasklist_lock();
937                  read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
938                    if (global_pid)
939                            p = ccsecurity_exports.find_task_by_pid_ns(pid,
940                                                                   &init_pid_ns);
941                    else
942                            p = ccsecurity_exports.find_task_by_vpid(pid);
943    #else
944                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
945    #endif
946                  if (p)                  if (p)
947                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
948                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
                 /***** CRITICAL SECTION END *****/  
949          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
950                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
951                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
952          } else          } else
953                  return false;                  return false;
954          head->write_var1 = domain;          head->w.domain = domain;
955          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
956          if (!head->read_buf)          if (!head->read_buf)
957                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
958          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
959            head->r.print_this_domain_only = true;
960            head->r.eof = !domain;
961            head->r.domain = &domain->list;
962          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
963          head->read_single_domain = true;          if (domain && domain->is_deleted)
964          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");  
         }  
965          return true;          return true;
966  }  }
967    
968    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
969                                 const bool is_delete)
970    {
971            static const struct {
972                    const char *keyword;
973                    int (*write) (char *, struct ccs_domain_info *,
974                                  struct ccs_condition *, const bool);
975            } ccs_callback[4] = {
976                    { "network ", ccs_write_network },
977                    { "misc ", ccs_write_misc },
978                    { "capability ", ccs_write_capability },
979                    { "ipc ", ccs_write_ipc },
980            };
981            int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
982                          const bool) = ccs_write_file;
983            int error;
984            u8 i;
985            struct ccs_condition *cond = NULL;
986            char *cp = ccs_find_condition_part(data);
987            if (cp) {
988                    cond = ccs_get_condition(cp);
989                    if (!cond)
990                            return -EINVAL;
991            }
992            for (i = 0; i < 4; i++) {
993                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
994                            continue;
995                    write = ccs_callback[i].write;
996                    break;
997            }
998            error = write(data, domain, cond, is_delete);
999            if (cond)
1000                    ccs_put_condition(cond);
1001            return error;
1002    }
1003    
1004    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1005            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
1006            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
1007    };
1008            
1009  /**  /**
1010   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
1011   *   *
1012   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1013   *   *
1014   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1015   */   */
1016  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
1017  {  {
1018          char *data = head->write_buf;          char *data = head->write_buf;
1019          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1020          bool is_delete = false;          bool is_delete = false;
1021          bool is_select = false;          bool is_select = false;
1022          unsigned int profile;          unsigned int profile;
1023          struct ccs_condition *cond = NULL;          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
         char *cp;  
         int error;  
         if (ccs_str_starts(&data, KEYWORD_DELETE))  
1024                  is_delete = true;                  is_delete = true;
1025          else if (ccs_str_starts(&data, KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
1026                  is_select = true;                  is_select = true;
1027          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1028                  return 0;                  return 0;
1029          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1030          if (!ccs_is_policy_manager())          if (!ccs_manager())
1031                  return -EPERM;                  return -EPERM;
1032          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
1033                  domain = NULL;                  domain = NULL;
1034                  if (is_delete)                  if (is_delete)
1035                          ccs_delete_domain(data);                          ccs_delete_domain(data);
1036                  else if (is_select)                  else if (is_select)
1037                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1038                  else                  else
1039                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0);
1040                  head->write_var1 = domain;                  head->w.domain = domain;
1041                  return 0;                  return 0;
1042          }          }
1043          if (!domain)          if (!domain)
1044                  return -EINVAL;                  return -EINVAL;
1045    
1046          if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
1047              && profile < MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
1048                  if (ccs_profile_ptr[profile] || !ccs_policy_loaded)                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1049                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1050                  return 0;                  return 0;
1051          }          }
1052          if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
1053                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
1054                  return 0;                  domain->group = (u8) profile;
         }  
         if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {  
                 domain->ignore_global_allow_env = !is_delete;  
1055                  return 0;                  return 0;
1056          }          }
1057          cp = ccs_find_condition_part(data);          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1058          if (cp) {                  const char *cp = ccs_dif[profile];
1059                  cond = ccs_get_condition(cp);                  if (strncmp(data, cp, strlen(cp) - 1))
                 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_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,  
                                  struct ccs_name_union *ptr)  
 {  
         const int pos = head->read_avail;  
         if (pos && head->read_buf[pos - 1] == ' ')  
                 head->read_avail--;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, " @%s",  
                                      ptr->group->group_name->name);  
         return ccs_io_printf(head, " %s", ptr->filename->name);  
 }  
   
 static bool ccs_print_number_union(struct ccs_io_buffer *head,  
                                    struct ccs_number_union *ptr)  
 {  
         unsigned long min;  
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, " @%s",  
                                      ptr->group->group_name->name);  
         min_type = ptr->min_type;  
         max_type = ptr->max_type;  
         min = ptr->values[0];  
         max = ptr->values[1];  
         switch (min_type) {  
         case VALUE_TYPE_HEXADECIMAL:  
                 if (!ccs_io_printf(head, " 0x%lX", min))  
                         return false;  
                 break;  
         case VALUE_TYPE_OCTAL:  
                 if (!ccs_io_printf(head, " 0%lo", min))  
                         return false;  
                 break;  
         default:  
                 if (!ccs_io_printf(head, " %lu", min))  
                         return false;  
                 break;  
         }  
         if (min == max && min_type == max_type)  
                 return true;  
         switch (max_type) {  
         case VALUE_TYPE_HEXADECIMAL:  
                 return ccs_io_printf(head, "-0x%lX", max);  
         case VALUE_TYPE_OCTAL:  
                 return ccs_io_printf(head, "-0%lo", max);  
         default:  
                 return ccs_io_printf(head, "-%lu", 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)))  
                         continue;  
                 msg = ccs_sp2keyword(bit);  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", msg) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, cond))  
                         goto out;  
         }  
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_mkdev_acl - Print a mkdev ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mkdev_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mkdev_acl_record *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < MAX_MKDEV_OPERATION; bit++) {  
                 const char *msg;  
                 if (!(perm & (1 << bit)))  
1060                          continue;                          continue;
1061                  msg = ccs_mkdev2keyword(bit);                  domain->flags[profile] = !is_delete;
1062                  pos = head->read_avail;                  return 0;
                 if (!ccs_io_printf(head, "allow_%s", msg) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->major) ||  
                     !ccs_print_number_union(head, &ptr->minor) ||  
                     !ccs_print_condition(head, cond))  
                         goto out;  
1063          }          }
1064          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;  
1065  }  }
1066    
1067  /**  /**
1068   * ccs_print_double_path_acl - Print a double path ACL entry.   * ccs_print_name_union - Print a ccs_name_union.
1069   *   *
1070   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1071   * @ptr:  Pointer to "struct ccs_double_path_acl_record".   * @ptr:  Pointer to "struct ccs_name_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
1072   */   */
1073  static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1074                                        struct ccs_double_path_acl_record *ptr,                                   const struct ccs_name_union *ptr)
                                       const struct ccs_condition *cond)  
1075  {  {
1076          int pos;          const bool cond = head->r.print_cond_part;
1077          u8 bit;          if (!cond)
1078          const u8 perm = ptr->perm;                  ccs_set_space(head);
1079          for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {          if (ptr->is_group) {
1080                  const char *msg;                  ccs_set_string(head, "@");
1081                  if (!(perm & (1 << bit)))                  ccs_set_string(head, ptr->group->group_name->name);
1082                          continue;          } else {
1083                  msg = ccs_dp2keyword(bit);                  if (cond)
1084                  pos = head->read_avail;                          ccs_set_string(head, "\"");
1085                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  ccs_set_string(head, ptr->filename->name);
1086                      !ccs_print_name_union(head, &ptr->name1) ||                  if (cond)
1087                      !ccs_print_name_union(head, &ptr->name2) ||                          ccs_set_string(head, "\"");
                     !ccs_print_condition(head, cond))  
                         goto out;  
1088          }          }
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1089  }  }
1090    
1091  /**  /**
1092   * ccs_print_path_number_acl - Print an ioctl/chmod/chown/chgrp ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1093   *   *
1094   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1095   * @ptr:  Pointer to "struct ccs_path_number_acl_record".   * @ptr:  Pointer to "struct ccs_number_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
1096   */   */
1097  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1098                                        struct ccs_path_number_acl_record *ptr,                                     const struct ccs_number_union *ptr)
                                       const struct ccs_condition *cond)  
1099  {  {
1100          int pos;          if (!head->r.print_cond_part)
1101          u8 bit;                  ccs_set_space(head);
1102          const u8 perm = ptr->perm;          if (ptr->is_group) {
1103          for (bit = head->read_bit; bit < MAX_PATH_NUMBER_OPERATION; bit++) {                  ccs_set_string(head, "@");
1104                  const char *msg;                  ccs_set_string(head, ptr->group->group_name->name);
1105                  if (!(perm & (1 << bit)))          } else {
1106                          continue;                  int i;
1107                  msg = ccs_path_number2keyword(bit);                  unsigned long min = ptr->values[0];
1108                  pos = head->read_avail;                  const unsigned long max = ptr->values[1];
1109                  if (!ccs_io_printf(head, "allow_%s", msg) ||                  u8 min_type = ptr->value_type[0];
1110                      !ccs_print_name_union(head, &ptr->name) ||                  const u8 max_type = ptr->value_type[1];
1111                      !ccs_print_number_union(head, &ptr->number) ||                  char buffer[128];
1112                      !ccs_print_condition(head, cond))                  buffer[0] = '\0';
1113                          goto out;                  for (i = 0; i < 2; i++) {
1114                            switch (min_type) {
1115                            case CCS_VALUE_TYPE_HEXADECIMAL:
1116                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1117                                                  min);
1118                                    break;
1119                            case CCS_VALUE_TYPE_OCTAL:
1120                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1121                                                  min);
1122                                    break;
1123                            default:
1124                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1125                                                  min);
1126                                    break;
1127                            }
1128                            if (min == max && min_type == max_type)
1129                                    break;
1130                            ccs_addprintf(buffer, sizeof(buffer), "-");
1131                            min_type = max_type;
1132                            min = max;
1133                    }
1134                    ccs_io_printf(head, "%s", buffer);
1135          }          }
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
         return false;  
1136  }  }
1137    
1138  /**  /**
1139   * ccs_print_argv0_acl - Print an argv[0] ACL entry.   * ccs_print_condition - Print condition part.
1140   *   *
1141   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1142   * @ptr:  Pointer to "struct ccs_argv0_acl_record".   * @cond: Pointer to "struct ccs_condition".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1143   *   *
1144   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1145   */   */
1146  static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
                                 struct ccs_argv0_acl_record *ptr,  
1147                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1148  {  {
1149          int pos = head->read_avail;          switch (head->r.cond_step) {
1150          if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",          case 0:
1151                             ptr->filename->name, ptr->argv0->name))                  {
1152                  goto out;                          if (cond->condc)
1153          if (!ccs_print_condition(head, cond))                                  ccs_set_string(head, " if");
1154                  goto out;                          head->r.cond_index = 0;
1155          return true;                          head->r.cond_step++;
1156   out:                  }
1157          head->read_avail = pos;                  /* fall through */
1158            case 1:
1159                    {
1160                            const u16 condc = cond->condc;
1161                            const struct ccs_condition_element *condp =
1162                                    (typeof(condp)) (cond + 1);
1163                            const struct ccs_number_union *numbers_p =
1164                                    (typeof(numbers_p)) (condp + condc);
1165                            const struct ccs_name_union *names_p =
1166                                    (typeof(names_p))
1167                                    (numbers_p + cond->numbers_count);
1168                            const struct ccs_argv *argv =
1169                                    (typeof(argv)) (names_p + cond->names_count);
1170                            const struct ccs_envp *envp =
1171                                    (typeof(envp)) (argv + cond->argc);
1172                            u16 skip;
1173                            for (skip = 0; skip < head->r.cond_index; skip++) {
1174                                    const u8 left = condp->left;
1175                                    const u8 right = condp->right;
1176                                    condp++;
1177                                    switch (left) {
1178                                    case CCS_ARGV_ENTRY:
1179                                            argv++;
1180                                            continue;
1181                                    case CCS_ENVP_ENTRY:
1182                                            envp++;
1183                                            continue;
1184                                    case CCS_NUMBER_UNION:
1185                                            numbers_p++;
1186                                            break;
1187                                    }
1188                                    switch (right) {
1189                                    case CCS_NAME_UNION:
1190                                            names_p++;
1191                                            break;
1192                                    case CCS_NUMBER_UNION:
1193                                            numbers_p++;
1194                                            break;
1195                                    }
1196                            }
1197                            while (head->r.cond_index < condc) {
1198                                    const u8 match = condp->equals;
1199                                    const u8 left = condp->left;
1200                                    const u8 right = condp->right;
1201                                    if (!ccs_flush(head))
1202                                            return false;
1203                                    condp++;
1204                                    head->r.cond_index++;
1205                                    ccs_set_space(head);
1206                                    switch (left) {
1207                                    case CCS_ARGV_ENTRY:
1208                                            ccs_io_printf(head,
1209                                                          "exec.argv[%u]%s\"%s\"",
1210                                                          argv->index,
1211                                                          argv->is_not ?
1212                                                          "!=" : "=",
1213                                                          argv->value->name);
1214                                            argv++;
1215                                            continue;
1216                                    case CCS_ENVP_ENTRY:
1217                                            ccs_io_printf(head,
1218                                                          "exec.envp[\"%s\"]%s",
1219                                                          envp->name->name,
1220                                                          envp->is_not ?
1221                                                          "!=" : "=");
1222                                            if (envp->value) {
1223                                                    ccs_set_string(head, "\"");
1224                                                    ccs_set_string(head, envp->
1225                                                                   value->name);
1226                                                    ccs_set_string(head, "\"");
1227                                            } else {
1228                                                    ccs_set_string(head, "NULL");
1229                                            }
1230                                            envp++;
1231                                            continue;
1232                                    case CCS_NUMBER_UNION:
1233                                            ccs_print_number_union(head,
1234                                                                   numbers_p++);
1235                                            break;
1236                                    default:
1237                                            ccs_set_string(head,
1238                                                   ccs_condition_keyword[left]);
1239                                            break;
1240                                    }
1241                                    ccs_set_string(head, match ? "=" : "!=");
1242                                    switch (right) {
1243                                    case CCS_NAME_UNION:
1244                                            ccs_print_name_union(head, names_p++);
1245                                            break;
1246                                    case CCS_NUMBER_UNION:
1247                                            ccs_print_number_union(head,
1248                                                                   numbers_p++);
1249                                            break;
1250                                    default:
1251                                            ccs_set_string(head,
1252                                                   ccs_condition_keyword[right]);
1253                                            break;
1254                                    }
1255                            }
1256                    }
1257                    head->r.cond_step++;
1258                    /* fall through */
1259            case 2:
1260                    if (!ccs_flush(head))
1261                            break;
1262                    head->r.cond_step++;
1263                    /* fall through */
1264            case 3:
1265                    {
1266                            u8 j;
1267                            const u8 i = cond->post_state[3];
1268                            if (i)
1269                                    ccs_set_string(head, " ; set");
1270                            for (j = 0; j < 3; j++)
1271                                    if ((i & (1 << j)))
1272                                            ccs_io_printf(head,
1273                                                          " task.state[%u]=%u", j,
1274                                                          cond->post_state[j]);
1275                            if (i & (1 << 4))
1276                                    ccs_io_printf(head, " audit=%s",
1277                                                  ccs_yesno(cond->post_state[4]));
1278                    }
1279                    ccs_set_lf(head);
1280                    return true;
1281            }
1282          return false;          return false;
1283  }  }
1284    
1285  /**  /**
1286   * ccs_print_env_acl - Print an evironment variable name's ACL entry.   * ccs_fns - Find next set bit.
1287   *   *
1288   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1289   * @ptr:  Pointer to "struct ccs_env_acl_record".   * @bit:  First bit to find.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1290   *   *
1291   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1292   */   */
1293  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                               struct ccs_env_acl_record *ptr,  
                               const struct ccs_condition *cond)  
1294  {  {
1295          int pos = head->read_avail;          for ( ; bit < 8; bit++)
1296          if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))                  if (perm & (1 << bit))
1297                  goto out;                          break;
1298          if (!ccs_print_condition(head, cond))          return bit;
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
 }  
   
 /**  
  * ccs_print_capability_acl - Print a capability ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_capability_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  
                                      struct ccs_capability_acl_record *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         int pos = head->read_avail;  
         if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",  
                            ccs_cap2keyword(ptr->operation)))  
                 goto out;  
         if (!ccs_print_condition(head, cond))  
                 goto out;  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
1299  }  }
1300    
1301  /**  static void ccs_set_group(struct ccs_io_buffer *head)
  * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl_record".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl_record *ptr)  
1302  {  {
1303          const u32 min_address = ptr->address.ipv4.min;          if (head->type == CCS_EXCEPTIONPOLICY)
1304          const u32 max_address = ptr->address.ipv4.max;                  ccs_io_printf(head, "acl_group %u ", head->r.group_index);
         if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))  
                 return false;  
         if (min_address != max_address  
             && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))  
                 return false;  
         return true;  
1305  }  }
1306    
1307  /**  /**
1308   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_print_entry - Print an ACL entry.
1309   *   *
1310   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1311   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".   * @acl:  Pointer to an ACL entry.
1312   *   *
1313   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1314   */   */
1315  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1316                                   struct ccs_ip_network_acl_record *ptr)                              const struct ccs_acl_info *acl)
1317  {  {
1318          char buf[64];          const u8 acl_type = acl->type;
1319          const struct in6_addr *min_address = ptr->address.ipv6.min;          u8 bit;
1320          const struct in6_addr *max_address = ptr->address.ipv6.max;          if (head->r.print_cond_part)
1321          ccs_print_ipv6(buf, sizeof(buf), min_address);                  goto print_cond_part;
1322          if (!ccs_io_printf(head, "%s", buf))          if (acl->is_deleted)
1323                    return true;
1324     next:
1325            bit = head->r.bit;
1326            if (!ccs_flush(head))
1327                  return false;                  return false;
1328          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1329                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1330                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1331                    const u16 perm = ptr->perm;
1332                    for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1333                            if (!(perm & (1 << bit)))
1334                                    continue;
1335                            if (head->r.print_execute_only &&
1336                                bit != CCS_TYPE_EXECUTE && bit != CCS_TYPE_TRANSIT)
1337                                    continue;
1338                            break;
1339                    }
1340                    if (bit >= CCS_MAX_PATH_OPERATION)
1341                            goto done;
1342                    ccs_set_group(head);
1343                    ccs_set_string(head, "file ");
1344                    ccs_set_string(head, ccs_path_keyword[bit]);
1345                    ccs_print_name_union(head, &ptr->name);
1346            } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1347                       acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1348                    struct ccs_execute_handler *ptr
1349                            = container_of(acl, typeof(*ptr), head);
1350                    ccs_set_group(head);
1351                    ccs_io_printf(head, "%s ",
1352                                  acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1353                                  CCS_KEYWORD_EXECUTE_HANDLER :
1354                                  CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1355                    ccs_set_string(head, ptr->handler->name);
1356            } else if (head->r.print_execute_only) {
1357                    return true;
1358            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1359                    struct ccs_mkdev_acl *ptr =
1360                            container_of(acl, typeof(*ptr), head);
1361                    bit = ccs_fns(ptr->perm, bit);
1362                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1363                            goto done;
1364                    ccs_set_group(head);
1365                    ccs_set_string(head, "file ");
1366                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1367                    ccs_print_name_union(head, &ptr->name);
1368                    ccs_print_number_union(head, &ptr->mode);
1369                    ccs_print_number_union(head, &ptr->major);
1370                    ccs_print_number_union(head, &ptr->minor);
1371            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1372                    struct ccs_path2_acl *ptr =
1373                            container_of(acl, typeof(*ptr), head);
1374                    bit = ccs_fns(ptr->perm, bit);
1375                    if (bit >= CCS_MAX_PATH2_OPERATION)
1376                            goto done;
1377                    ccs_set_group(head);
1378                    ccs_set_string(head, "file ");
1379                    ccs_set_string(head, ccs_path2_keyword[bit]);
1380                    ccs_print_name_union(head, &ptr->name1);
1381                    ccs_print_name_union(head, &ptr->name2);
1382            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1383                    struct ccs_path_number_acl *ptr =
1384                            container_of(acl, typeof(*ptr), head);
1385                    bit = ccs_fns(ptr->perm, bit);
1386                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1387                            goto done;
1388                    ccs_set_group(head);
1389                    ccs_set_string(head, "file ");
1390                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1391                    ccs_print_name_union(head, &ptr->name);
1392                    ccs_print_number_union(head, &ptr->number);
1393            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1394                    struct ccs_env_acl *ptr =
1395                            container_of(acl, typeof(*ptr), head);
1396                    ccs_set_group(head);
1397                    ccs_set_string(head, "misc env ");
1398                    ccs_set_string(head, ptr->env->name);
1399            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1400                    struct ccs_capability_acl *ptr =
1401                            container_of(acl, typeof(*ptr), head);
1402                    ccs_set_group(head);
1403                    ccs_set_string(head, "capability ");
1404                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1405            } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1406                    struct ccs_ip_network_acl *ptr =
1407                            container_of(acl, typeof(*ptr), head);
1408                    bit = ccs_fns(ptr->perm, bit);
1409                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1410                            goto done;
1411                    ccs_set_group(head);
1412                    ccs_set_string(head, "network ");
1413                    ccs_set_string(head, ccs_net_keyword[bit]);
1414                    ccs_set_space(head);
1415                    switch (ptr->address_type) {
1416                            char buf[128];
1417                    case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1418                            ccs_set_string(head, "@");
1419                            ccs_set_string(head,
1420                                           ptr->address.group->group_name->name);
1421                            break;
1422                    case CCS_IP_ADDRESS_TYPE_IPv4:
1423                            ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1424                                           ptr->address.ipv4.max);
1425                            ccs_io_printf(head, "%s", buf);
1426                            break;
1427                    case CCS_IP_ADDRESS_TYPE_IPv6:
1428                            ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1429                                           ptr->address.ipv6.max);
1430                            ccs_io_printf(head, "%s", buf);
1431                            break;
1432                    }
1433                    ccs_print_number_union(head, &ptr->port);
1434            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1435                    struct ccs_signal_acl *ptr =
1436                            container_of(acl, typeof(*ptr), head);
1437                    ccs_set_group(head);
1438                    ccs_set_string(head, "ipc signal ");
1439                    ccs_io_printf(head, "%u ", ptr->sig);
1440                    ccs_set_string(head, ptr->domainname->name);
1441            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1442                    struct ccs_mount_acl *ptr =
1443                            container_of(acl, typeof(*ptr), head);
1444                    ccs_set_group(head);
1445                    ccs_io_printf(head, "file mount");
1446                    ccs_print_name_union(head, &ptr->dev_name);
1447                    ccs_print_name_union(head, &ptr->dir_name);
1448                    ccs_print_name_union(head, &ptr->fs_type);
1449                    ccs_print_number_union(head, &ptr->flags);
1450            }
1451            head->r.bit = bit + 1;
1452            if (acl->cond) {
1453                    head->r.print_cond_part = true;
1454                    head->r.cond_step = 0;
1455                    if (!ccs_flush(head))
1456                            return false;
1457     print_cond_part:
1458                    if (!ccs_print_condition(head, acl->cond))
1459                          return false;                          return false;
1460                    head->r.print_cond_part = false;
1461            } else {
1462                    ccs_set_lf(head);
1463          }          }
1464          return true;          switch (acl_type) {
1465  }          case CCS_TYPE_PATH_ACL:
1466            case CCS_TYPE_MKDEV_ACL:
1467  /**          case CCS_TYPE_PATH2_ACL:
1468   * ccs_print_network_acl - Print a network ACL entry.          case CCS_TYPE_PATH_NUMBER_ACL:
1469   *          case CCS_TYPE_IP_NETWORK_ACL:
1470   * @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;  
1471          }          }
1472          if (!ccs_print_number_union(head, &ptr->port) ||   done:
1473              !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;  
1474          return true;          return true;
  out:  
         head->read_avail = pos;  
         return false;  
1475  }  }
1476    
1477  /**  /**
1478   * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.   * ccs_read_domain2 - Read domain policy.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_pivot_root_acl_record".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1479   *   *
1480   * Returns true on success, false otherwise.   * @head:   Pointer to "struct ccs_io_buffer".
1481   */   * @domain: Pointer to "struct ccs_domain_info".
 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.  
1482   *   *
1483   * @head: Pointer to "struct ccs_io_buffer".   * Caller holds ccs_read_lock().
  * @ptr:  Pointer to an ACL entry.  
1484   *   *
1485   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1486   */   */
1487  static bool ccs_print_entry(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1488                              struct ccs_acl_info *ptr)                               struct ccs_domain_info *domain)
1489  {  {
1490          const struct ccs_condition *cond = ptr->cond;          list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1491          const u8 acl_type = ccs_acl_type2(ptr);                  struct ccs_acl_info *ptr =
1492          if (acl_type & ACL_DELETED)                          list_entry(head->r.acl, typeof(*ptr), list);
1493                  return true;                  if (!ccs_print_entry(head, ptr))
1494          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_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl_record *acl  
                         = container_of(ptr, struct ccs_path_number_acl_record,  
                                        head);  
                 return ccs_print_path_number_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);  
1495          }          }
1496          /* Workaround for gcc 3.2.2's inline bug. */          head->r.acl = NULL;
1497          if (acl_type & ACL_DELETED)          return true;
                 return true;  
         BUG(); /* This must not happen. */  
         return false;  
1498  }  }
1499    
1500  /**  /**
1501   * ccs_read_domain_policy - Read domain policy.   * ccs_read_domain - Read domain policy.
1502   *   *
1503   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1504   *   *
  * Returns 0.  
  *  
1505   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1506   */   */
1507  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1508  {  {
1509          struct list_head *dpos;          if (head->r.eof)
1510          struct list_head *apos;                  return;
1511          ccs_check_read_lock();          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1512          if (head->read_eof)                  struct ccs_domain_info *domain =
1513                  return 0;                          list_entry(head->r.domain, typeof(*domain), list);
1514          if (head->read_step == 0)                  switch (head->r.step) {
1515                  head->read_step = 1;                          u8 i;
1516          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                  case 0:
1517                  struct ccs_domain_info *domain;                          if (domain->is_deleted &&
1518                  const char *quota_exceeded = "";                              !head->r.print_this_domain_only)
1519                  const char *transition_failed = "";                                  continue;
1520                  const char *ignore_global_allow_read = "";                          /* Print domainname and flags. */
1521                  const char *ignore_global_allow_env = "";                          ccs_set_string(head, domain->domainname->name);
1522                  domain = list_entry(dpos, struct ccs_domain_info, list);                          ccs_set_lf(head);
1523                  if (head->read_step != 1)                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1524                          goto acl_loop;                                        domain->profile);
1525                  if (domain->is_deleted && !head->read_single_domain)                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1526                          continue;                                        domain->group);
1527                  /* Print domainname and flags. */                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1528                  if (domain->quota_warned)                                  if (domain->flags[i])
1529                          quota_exceeded = "quota_exceeded\n";                                          ccs_set_string(head, ccs_dif[i]);
1530                  if (domain->domain_transition_failed)                          head->r.step++;
1531                          transition_failed = "transition_failed\n";                          ccs_set_lf(head);
1532                  if (domain->ignore_global_allow_read)                          /* fall through */
1533                          ignore_global_allow_read                  case 1:
1534                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          if (!ccs_read_domain2(head, domain))
1535                  if (domain->ignore_global_allow_env)                                  return;
1536                          ignore_global_allow_env                          head->r.step++;
1537                                  = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          if (!ccs_set_lf(head))
1538                  if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"                                  return;
1539                                     "%s%s%s%s\n", domain->domainname->name,                          /* fall through */
1540                                     domain->profile, quota_exceeded,                  case 2:
1541                                     transition_failed,                          head->r.step = 0;
1542                                     ignore_global_allow_read,                          if (head->r.print_this_domain_only)
1543                                     ignore_global_allow_env))                                  goto done;
1544                          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;  
1545          }          }
1546          head->read_eof = true;   done:
1547          return 0;          head->r.eof = true;
1548  }  }
1549    
1550  /**  /**
# Line 1332  static int ccs_read_domain_policy(struct Line 1557  static int ccs_read_domain_policy(struct
1557   * This is equivalent to doing   * This is equivalent to doing
1558   *   *
1559   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1560   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1561   *   *
1562   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1563   */   */
# Line 1342  static int ccs_write_domain_profile(stru Line 1567  static int ccs_write_domain_profile(stru
1567          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1568          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1569          unsigned int profile;          unsigned int profile;
         ccs_check_read_lock();  
1570          if (!cp)          if (!cp)
1571                  return -EINVAL;                  return -EINVAL;
1572          *cp = '\0';          *cp = '\0';
1573          profile = simple_strtoul(data, NULL, 10);          profile = simple_strtoul(data, NULL, 10);
1574          if (profile >= MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1575                  return -EINVAL;                  return -EINVAL;
1576          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1577          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1578                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1579          return 0;          return 0;
1580  }  }
# Line 1360  static int ccs_write_domain_profile(stru Line 1584  static int ccs_write_domain_profile(stru
1584   *   *
1585   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1586   *   *
  * Returns list of profile number and domainname pairs.  
  *  
1587   * This is equivalent to doing   * This is equivalent to doing
1588   *   *
1589   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1371  static int ccs_write_domain_profile(stru Line 1593  static int ccs_write_domain_profile(stru
1593   *   *
1594   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1595   */   */
1596  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1597  {  {
1598          struct list_head *pos;          if (head->r.eof)
1599          ccs_check_read_lock();                  return;
1600          if (head->read_eof)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1601                  return 0;                  struct ccs_domain_info *domain =
1602          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);  
1603                  if (domain->is_deleted)                  if (domain->is_deleted)
1604                          continue;                          continue;
1605                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
1606                                     domain->domainname->name))                          return;
1607                          return 0;                  ccs_io_printf(head, "%u ", domain->profile);
1608                    ccs_set_string(head, domain->domainname->name);
1609                    ccs_set_lf(head);
1610          }          }
1611          head->read_eof = true;          head->r.eof = true;
         return 0;  
1612  }  }
1613    
1614  /**  /**
# Line 1399  static int ccs_read_domain_profile(struc Line 1620  static int ccs_read_domain_profile(struc
1620   */   */
1621  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1622  {  {
1623          head->read_eof = false;          head->r.eof = false;
1624          return 0;          return 0;
1625  }  }
1626    
# Line 1414  static int ccs_write_pid(struct ccs_io_b Line 1635  static int ccs_write_pid(struct ccs_io_b
1635   *   *
1636   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1637   */   */
1638  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1639  {  {
1640          char *buf = head->write_buf;          char *buf = head->write_buf;
1641          bool task_info = false;          bool task_info = false;
1642            bool global_pid = false;
1643          unsigned int pid;          unsigned int pid;
1644          struct task_struct *p;          struct task_struct *p;
1645          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1646          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_check_read_lock();  
1647          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1648          if (!buf)          if (!buf) {
1649                  goto done; /* Do nothing if open(O_RDONLY). */                  head->r.eof = true;
1650          if (head->read_avail || head->read_eof)                  return; /* Do nothing if open(O_RDONLY). */
1651                  goto done;          }
1652          head->read_eof = true;          if (head->r.w_pos || head->r.eof)
1653                    return;
1654            head->r.eof = true;
1655          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1656                  task_info = true;                  task_info = true;
1657            if (ccs_str_starts(&buf, "global-pid "))
1658                    global_pid = true;
1659          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1660          /***** CRITICAL SECTION START *****/          ccs_tasklist_lock();
1661          read_lock(&tasklist_lock);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1662            if (global_pid)
1663                    p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1664            else
1665                    p = ccsecurity_exports.find_task_by_vpid(pid);
1666    #else
1667          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1668    #endif
1669          if (p) {          if (p) {
1670                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1671                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1672          }          }
1673          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
         /***** CRITICAL SECTION END *****/  
1674          if (!domain)          if (!domain)
1675                  goto done;                  return;
1676          if (!task_info)          if (!task_info) {
1677                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1678                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1679          else          } else {
1680                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1681                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1682                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                ccs_yesno(ccs_flags &
1683                                "yes" : "no",                                          CCS_TASK_IS_MANAGER),
1684                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                ccs_yesno(ccs_flags &
1685                                "yes" : "no",                                          CCS_TASK_IS_EXECUTE_HANDLER),
1686                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1687                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1688                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1689   done:          }
         return 0;  
1690  }  }
1691    
1692    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1693            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1694            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1695            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1696            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1697            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1698    };
1699    
1700    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1701            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1702            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1703            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1704    };
1705    
1706  /**  /**
1707   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1708   *   *
1709   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1710   *   *
1711   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1712   */   */
1713  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1714  {  {
1715          char *data = head->write_buf;          char *data = head->write_buf;
1716          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1717          if (ccs_str_starts(&data, KEYWORD_KEEP_DOMAIN))          u8 i;
1718                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1719          if (ccs_str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1720                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1721          if (ccs_str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1722                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1723                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1724          if (ccs_str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1725                  return ccs_write_domain_initializer_policy(data, true,          };
1726                                                             is_delete);          for (i = 0; i < 3; i++)
1727          if (ccs_str_starts(&data, KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1728                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1729          if (ccs_str_starts(&data, KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1730                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1731          if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1732                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1733          if (ccs_str_starts(&data, KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1734                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1735          if (ccs_str_starts(&data, KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1736                  return ccs_write_path_group_policy(data, is_delete);          if (ccs_str_starts(&data, "acl_group ")) {
1737          if (ccs_str_starts(&data, KEYWORD_NUMBER_GROUP))                  unsigned int group;
1738                  return ccs_write_number_group_policy(data, is_delete);                  if (sscanf(data, "%u", &group) == 1 &&
1739          if (ccs_str_starts(&data, KEYWORD_DENY_REWRITE))                      group < CCS_MAX_ACL_GROUPS) {
1740                  return ccs_write_no_rewrite_policy(data, is_delete);                          data = strchr(data, ' ');
1741          if (ccs_str_starts(&data, KEYWORD_ADDRESS_GROUP))                          if (data)
1742                  return ccs_write_address_group_policy(data, is_delete);                                  return ccs_write_domain2(data + 1,
1743          if (ccs_str_starts(&data, KEYWORD_DENY_AUTOBIND))                                                           &ccs_acl_group[group],
1744                  return ccs_write_reserved_port_policy(data, is_delete);                                                           is_delete);
1745                    }
1746            }
1747          return -EINVAL;          return -EINVAL;
1748  }  }
1749    
1750  /**  /**
1751   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1752   *   *
1753   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1754     * @idx:  Index number.
1755   *   *
1756   * Returns 0 on success, -EINVAL otherwise.   * Returns true on success, false otherwise.
1757   *   *
1758   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1759   */   */
1760  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1761  {  {
1762          ccs_check_read_lock();          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1763          if (!head->read_eof) {                  struct ccs_group *group =
1764                  switch (head->read_step) {                          list_entry(head->r.group, typeof(*group), head.list);
1765                  case 0:                  list_for_each_cookie(head->r.acl, &group->member_list) {
1766                          head->read_var2 = NULL;                          struct ccs_acl_head *ptr =
1767                          head->read_step = 1;                                  list_entry(head->r.acl, typeof(*ptr), list);
1768                  case 1:                          if (ptr->is_deleted)
1769                          if (!ccs_read_domain_keeper_policy(head))                                  continue;
1770                                  break;                          if (!ccs_flush(head))
1771                          head->read_var2 = NULL;                                  return false;
1772                          head->read_step = 2;                          ccs_set_string(head, ccs_group_name[idx]);
1773                  case 2:                          ccs_set_string(head, group->group_name->name);
1774                          if (!ccs_read_globally_readable_policy(head))                          if (idx == CCS_PATH_GROUP) {
1775                                  break;                                  ccs_set_space(head);
1776                          head->read_var2 = NULL;                                  ccs_set_string(head, container_of
1777                          head->read_step = 3;                                                 (ptr, struct ccs_path_group,
1778                  case 3:                                                  head)->member_name->name);
1779                          if (!ccs_read_globally_usable_env_policy(head))                          } else if (idx == CCS_NUMBER_GROUP) {
1780                                  break;                                  ccs_print_number_union(head, &container_of
1781                          head->read_var2 = NULL;                                                         (ptr, struct ccs_number_group,
1782                          head->read_step = 4;                                                          head)->number);
1783                  case 4:                          } else if (idx == CCS_ADDRESS_GROUP) {
1784                          if (!ccs_read_domain_initializer_policy(head))                                  char buffer[128];
1785                                  break;                                  struct ccs_address_group *member =
1786                          head->read_var2 = NULL;                                          container_of(ptr, typeof(*member),
1787                          head->read_step = 6;                                                       head);
1788                  case 6:                                  if (member->is_ipv6)
1789                          if (!ccs_read_aggregator_policy(head))                                          ccs_print_ipv6(buffer, sizeof(buffer),
1790                                  break;                                                         member->min.ipv6,
1791                          head->read_var2 = NULL;                                                         member->max.ipv6);
1792                          head->read_step = 7;                                  else
1793                  case 7:                                          ccs_print_ipv4(buffer, sizeof(buffer),
1794                          if (!ccs_read_file_pattern(head))                                                         member->min.ipv4,
1795                                  break;                                                         member->max.ipv4);
1796                          head->read_var2 = NULL;                                  ccs_io_printf(head, " %s", buffer);
1797                          head->read_step = 8;                          }
1798                  case 8:                          ccs_set_lf(head);
1799                          if (!ccs_read_no_rewrite_policy(head))                  }
1800                                  break;                  head->r.acl = NULL;
1801                          head->read_var2 = NULL;          }
1802                          head->read_step = 9;          head->r.group = NULL;
1803                  case 9:          return true;
1804                          if (!ccs_read_path_group_policy(head))  }
1805                                  break;  
1806                          head->read_var1 = NULL;  /**
1807                          head->read_var2 = NULL;   * ccs_read_policy - Read "struct ccs_..._entry" list.
1808                          head->read_step = 10;   *
1809                  case 10:   * @head: Pointer to "struct ccs_io_buffer".
1810                          if (!ccs_read_number_group_policy(head))   * @idx:  Index number.
1811                                  break;   *
1812                          head->read_var1 = NULL;   * Returns true on success, false otherwise.
1813                          head->read_var2 = NULL;   *
1814                          head->read_step = 11;   * Caller holds ccs_read_lock().
1815                  case 11:   */
1816                          if (!ccs_read_address_group_policy(head))  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1817                                  break;  {
1818                          head->read_var2 = NULL;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1819                          head->read_step = 12;                  struct ccs_acl_head *acl =
1820                  case 12:                          container_of(head->r.acl, typeof(*acl), list);
1821                          if (!ccs_read_reserved_port_policy(head))                  if (acl->is_deleted)
1822                                  break;                          continue;
1823                          head->read_eof = true;                  if (!ccs_flush(head))
1824                            return false;
1825                    switch (idx) {
1826                    case CCS_ID_TRANSITION_CONTROL:
1827                            {
1828                                    struct ccs_transition_control *ptr =
1829                                            container_of(acl, typeof(*ptr), head);
1830                                    ccs_set_string(head,
1831                                                   ccs_transition_type[ptr->type]);
1832                                    ccs_set_string(head, ptr->program ?
1833                                                   ptr->program->name : "any");
1834                                    ccs_set_string(head, " from ");
1835                                    ccs_set_string(head, ptr->domainname ?
1836                                                   ptr->domainname->name : "any");
1837                            }
1838                            break;
1839                    case CCS_ID_AGGREGATOR:
1840                            {
1841                                    struct ccs_aggregator *ptr =
1842                                            container_of(acl, typeof(*ptr), head);
1843                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1844                                    ccs_set_string(head, ptr->original_name->name);
1845                                    ccs_set_space(head);
1846                                    ccs_set_string(head,
1847                                                   ptr->aggregated_name->name);
1848                            }
1849                            break;
1850                    case CCS_ID_PATTERN:
1851                            {
1852                                    struct ccs_pattern *ptr =
1853                                            container_of(acl, typeof(*ptr), head);
1854                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1855                                    ccs_set_string(head, ptr->pattern->name);
1856                            }
1857                            break;
1858                    case CCS_ID_RESERVEDPORT:
1859                            {
1860                                    struct ccs_reserved *ptr =
1861                                            container_of(acl, typeof(*ptr), head);
1862                                    const u16 min_port = ptr->min_port;
1863                                    const u16 max_port = ptr->max_port;
1864                                    ccs_set_string(head,
1865                                                   CCS_KEYWORD_DENY_AUTOBIND);
1866                                    ccs_io_printf(head, "%u", min_port);
1867                                    if (min_port != max_port)
1868                                            ccs_io_printf(head, "-%u", max_port);
1869                            }
1870                          break;                          break;
1871                  default:                  default:
1872                          return -EINVAL;                          continue;
1873                  }                  }
1874                    ccs_set_lf(head);
1875          }          }
1876          return 0;          head->r.acl = NULL;
1877            return true;
1878    }
1879    
1880    /**
1881     * ccs_read_exception - Read exception policy.
1882     *
1883     * @head: Pointer to "struct ccs_io_buffer".
1884     *
1885     * Caller holds ccs_read_lock().
1886     */
1887    static void ccs_read_exception(struct ccs_io_buffer *head)
1888    {
1889            if (head->r.eof)
1890                    return;
1891            while (head->r.step < CCS_MAX_POLICY &&
1892                   ccs_read_policy(head, head->r.step))
1893                    head->r.step++;
1894            if (head->r.step < CCS_MAX_POLICY)
1895                    return;
1896            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1897                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1898                    head->r.step++;
1899            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1900                    return;
1901            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1902                   + CCS_MAX_ACL_GROUPS) {
1903                    head->r.group_index = head->r.step - CCS_MAX_POLICY
1904                            - CCS_MAX_GROUP;
1905                    if (!ccs_read_domain2(head,
1906                                          &ccs_acl_group[head->r.group_index]))
1907                            return;
1908                    head->r.step++;
1909            }
1910            head->r.eof = true;
1911  }  }
1912    
1913  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 1590  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1917  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1917  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1918    
1919  /* Structure for query. */  /* Structure for query. */
1920  struct ccs_query_entry {  struct ccs_query {
1921          struct list_head list;          struct list_head list;
1922          char *query;          char *query;
1923          int query_len;          int query_len;
# Line 1599  struct ccs_query_entry { Line 1926  struct ccs_query_entry {
1926          int answer;          int answer;
1927  };  };
1928    
1929  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1930  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1931    
1932  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1933  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1934    
1935    static void ccs_truncate(char *str)
1936    {
1937            while (* (unsigned char *) str > (unsigned char) ' ')
1938                    str++;
1939            *str = '\0';
1940    }
1941    
1942  /**  /**
1943   * ccs_check_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1944   *   *
1945   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1946   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1947   *   *
1948   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1949   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1950   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1951   * -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1952   */   */
1953  int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1954  {  {
1955          va_list args;          va_list args;
1956          int error = -EPERM;          int error = -EPERM;
1957          int pos;          int pos;
1958          int len;          int len;
1959          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1960          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1961          bool quota_exceeded = false;          bool quota_exceeded = false;
1962          char *header;          char *header;
1963          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1964                  r->domain = ccs_current_domain();          va_start(args, fmt);
1965            len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1966            va_end(args);
1967            if (r->mode == CCS_CONFIG_LEARNING) {
1968                    char *buffer;
1969                    char *realpath = NULL;
1970                    char *argv0 = NULL;
1971                    char *symlink = NULL;
1972                    char *handler = NULL;
1973                    const struct ccs_preference *pref;
1974                    if (!ccs_domain_quota_ok(r))
1975                            return 0;
1976                    header = ccs_init_log(&len, r);
1977                    if (!header)
1978                            return 0;
1979                    pref = ccs_profile(r->profile)->learning;
1980                    /* strstr() will return NULL if ordering is wrong. */
1981                    if (r->param_type == CCS_TYPE_PATH_ACL &&
1982                        r->param.path.operation == CCS_TYPE_EXECUTE) {
1983                            if (pref->learning_exec_argv0) {
1984                                    argv0 = strstr(header, " argv[]={ \"");
1985                                    if (argv0) {
1986                                            argv0 += 10;
1987                                            ccs_truncate(argv0);
1988                                    }
1989                            }
1990                            if (pref->learning_exec_realpath) {
1991                                    realpath = strstr(header,
1992                                                      " exec={ realpath=\"");
1993                                    if (realpath) {
1994                                            realpath += 8;
1995                                            ccs_truncate(realpath);
1996                                    }
1997                            }
1998                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1999                               r->param.path.operation == CCS_TYPE_SYMLINK &&
2000                               pref->learning_symlink_target) {
2001                            symlink = strstr(header, " symlink.target=\"");
2002                            if (symlink)
2003                                    ccs_truncate(symlink + 1);
2004                    }
2005                    handler = strstr(header, "type=execute_handler");
2006                    if (handler)
2007                            ccs_truncate(handler);
2008                    buffer = kmalloc(len, CCS_GFP_FLAGS);
2009                    if (buffer) {
2010                            va_start(args, fmt);
2011                            vsnprintf(buffer, len - 1, fmt, args);
2012                            va_end(args);
2013                            if (handler || realpath || argv0 || symlink) {
2014                                    ccs_addprintf(buffer, len, " if");
2015                                    if (handler)
2016                                            ccs_addprintf(buffer, len, " task.%s",
2017                                                          handler);
2018                                    if (realpath)
2019                                            ccs_addprintf(buffer, len, " exec.%s",
2020                                                          realpath);
2021                                    if (argv0)
2022                                            ccs_addprintf(buffer, len,
2023                                                          " exec.argv[0]=%s",
2024                                                          argv0);
2025                                    if (symlink)
2026                                            ccs_addprintf(buffer, len, "%s",
2027                                                          symlink);
2028                            }
2029                            ccs_normalize_line(buffer);
2030                            ccs_write_domain2(buffer, domain, false);
2031                            kfree(buffer);
2032                    }
2033                    kfree(header);
2034                    return 0;
2035            }
2036            if (r->mode != CCS_CONFIG_ENFORCING)
2037                    return 0;
2038          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2039                  int i;                  int i;
2040                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2041                          return -EPERM;                          return -EPERM;
2042                  for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2043                       i++) {                               enforcing_penalty; i++) {
2044                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2045                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2046                  }                  }
2047                  return -EPERM;                  return -EPERM;
2048          }          }
2049          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);  
2050          if (!header)          if (!header)
2051                  goto out;                  goto out;
2052          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2053          if (!ccs_query_entry)          if (!entry)
2054                  goto out;                  goto out;
2055          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          len = ccs_round2(len);
2056          if (!ccs_query_entry->query)          entry->query = kzalloc(len, CCS_GFP_FLAGS);
2057            if (!entry->query)
2058                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
         /***** CRITICAL SECTION START *****/  
2059          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2060          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2061              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
2062                  quota_exceeded = true;                  quota_exceeded = true;
2063          } else {          } else {
2064                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
2065                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
2066          }          }
2067          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2068          if (quota_exceeded)          if (quota_exceeded)
2069                  goto out;                  goto out;
2070          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2071                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
2072          kfree(header);          kfree(header);
2073          header = NULL;          header = NULL;
2074          va_start(args, fmt);          va_start(args, fmt);
2075          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2076          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
2077          va_end(args);          va_end(args);
         /***** CRITICAL SECTION START *****/  
2078          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2079          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2080          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2081          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2082          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2083               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2084               ccs_query_entry->timer++) {               entry->timer++) {
2085                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2086                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2087                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2088                  if (ccs_query_entry->answer)                  if (entry->answer)
2089                          break;                          break;
2090          }          }
         /***** CRITICAL SECTION START *****/  
2091          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2092          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2093          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2094          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2095          /***** CRITICAL SECTION END *****/          switch (entry->answer) {
         switch (ccs_query_entry->answer) {  
2096          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2097                  error = 1;                  error = CCS_RETRY_REQUEST;
2098                  r->retry++;                  r->retry++;
2099                  break;                  break;
2100          case 1:          case 1:
# Line 1711  int ccs_check_supervisor(struct ccs_requ Line 2109  int ccs_check_supervisor(struct ccs_requ
2109                  break;                  break;
2110          }          }
2111   out:   out:
2112          if (ccs_query_entry)          if (entry)
2113                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2114          kfree(ccs_query_entry);          kfree(entry);
2115          kfree(header);          kfree(header);
2116          return error;          return error;
2117  }  }
# Line 1734  static int ccs_poll_query(struct file *f Line 2132  static int ccs_poll_query(struct file *f
2132          bool found = false;          bool found = false;
2133          u8 i;          u8 i;
2134          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
                 /***** CRITICAL SECTION START *****/  
2135                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2136                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2137                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2138                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2139                          if (ptr->answer)                          if (ptr->answer)
2140                                  continue;                                  continue;
2141                          found = true;                          found = true;
2142                          break;                          break;
2143                  }                  }
2144                  spin_unlock(&ccs_query_list_lock);                  spin_unlock(&ccs_query_list_lock);
                 /***** CRITICAL SECTION END *****/  
2145                  if (found)                  if (found)
2146                          return POLLIN | POLLRDNORM;                          return POLLIN | POLLRDNORM;
2147                  if (i)                  if (i)
# Line 1759  static int ccs_poll_query(struct file *f Line 2155  static int ccs_poll_query(struct file *f
2155   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2156   *   *
2157   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns 0.  
2158   */   */
2159  static int ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2160  {  {
2161          struct list_head *tmp;          struct list_head *tmp;
2162          int pos = 0;          int pos = 0;
2163          int len = 0;          int len = 0;
2164          char *buf;          char *buf;
2165          if (head->read_avail)          if (head->r.w_pos)
2166                  return 0;                  return;
2167          if (head->read_buf) {          if (head->read_buf) {
2168                  kfree(head->read_buf);                  kfree(head->read_buf);
2169                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2170          }          }
         /***** CRITICAL SECTION START *****/  
2171          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2172          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2173                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2174                  if (ptr->answer)                  if (ptr->answer)
2175                          continue;                          continue;
2176                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2177                          continue;                          continue;
2178                  len = ptr->query_len;                  len = ptr->query_len;
2179                  break;                  break;
2180          }          }
2181          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2182          if (!len) {          if (!len) {
2183                  head->read_step = 0;                  head->r.query_index = 0;
2184                  return 0;                  return;
2185          }          }
2186          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2187          if (!buf)          if (!buf)
2188                  return 0;                  return;
2189          pos = 0;          pos = 0;
         /***** CRITICAL SECTION START *****/  
2190          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2191          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2192                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2193                  if (ptr->answer)                  if (ptr->answer)
2194                          continue;                          continue;
2195                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2196                          continue;                          continue;
2197                  /*                  /*
2198                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 1815  static int ccs_read_query(struct ccs_io_ Line 2203  static int ccs_read_query(struct ccs_io_
2203                  break;                  break;
2204          }          }
2205          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2206          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2207                  head->read_buf = buf;                  head->read_buf = buf;
2208                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2209                    head->r.query_index++;
2210          } else {          } else {
2211                  kfree(buf);                  kfree(buf);
2212          }          }
         return 0;  
2213  }  }
2214    
2215  /**  /**
# Line 1840  static int ccs_write_answer(struct ccs_i Line 2225  static int ccs_write_answer(struct ccs_i
2225          struct list_head *tmp;          struct list_head *tmp;
2226          unsigned int serial;          unsigned int serial;
2227          unsigned int answer;          unsigned int answer;
         /***** CRITICAL SECTION START *****/  
2228          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2229          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2230                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2231                  ptr->timer = 0;                  ptr->timer = 0;
2232          }          }
2233          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2234          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)          if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2235                  return -EINVAL;                  return -EINVAL;
         /***** CRITICAL SECTION START *****/  
2236          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2237          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2238                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2239                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2240                          continue;                          continue;
2241                  if (!ptr->answer)                  if (!ptr->answer)
# Line 1863  static int ccs_write_answer(struct ccs_i Line 2243  static int ccs_write_answer(struct ccs_i
2243                  break;                  break;
2244          }          }
2245          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
         /***** CRITICAL SECTION END *****/  
2246          return 0;          return 0;
2247  }  }
2248    
# Line 1871  static int ccs_write_answer(struct ccs_i Line 2250  static int ccs_write_answer(struct ccs_i
2250   * ccs_read_version: Get version.   * ccs_read_version: Get version.
2251   *   *
2252   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  *  
  * Returns version information.  
2253   */   */
2254  static int ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2255  {  {
2256          if (!head->read_eof) {          if (head->r.eof)
2257                  ccs_io_printf(head, "1.7.0-pre");                  return;
2258                  head->read_eof = true;          ccs_set_string(head, "1.7.2");
2259          }          head->r.eof = true;
         return 0;  
2260  }  }
2261    
2262  /**  /**
2263   * ccs_read_self_domain - Get the current process's domainname.   * ccs_read_self_domain -