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

Subversion リポジトリの参照

Diff of /branches/ccs-patch/security/ccsecurity/policy_io.c

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

branches/ccs-patch/security/ccsecurity/policy_io.c revision 3748 by kumaneko, Thu Jun 10 03:54:43 2010 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 3952 by kumaneko, Mon Sep 6 01:16:04 2010 UTC
# Line 3  Line 3 
3   *   *
4   * Copyright (C) 2005-2010  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.2+   2010/06/04   * Version: 1.8.0-pre   2010/09/01
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 13  Line 13 
13  #include "internal.h"  #include "internal.h"
14    
15  static struct ccs_profile ccs_default_profile = {  static struct ccs_profile ccs_default_profile = {
         .learning = &ccs_default_profile.preference,  
         .permissive = &ccs_default_profile.preference,  
         .enforcing = &ccs_default_profile.preference,  
         .audit = &ccs_default_profile.preference,  
16  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
17          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
18          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
# Line 24  static struct ccs_profile ccs_default_pr Line 20  static struct ccs_profile ccs_default_pr
20          .preference.audit_task_info = true,          .preference.audit_task_info = true,
21          .preference.audit_path_info = true,          .preference.audit_path_info = true,
22          .preference.enforcing_penalty = 0,          .preference.enforcing_penalty = 0,
         .preference.enforcing_verbose = true,  
23          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
         .preference.learning_verbose = false,  
24          .preference.learning_exec_realpath = true,          .preference.learning_exec_realpath = true,
25          .preference.learning_exec_argv0 = true,          .preference.learning_exec_argv0 = true,
26          .preference.learning_symlink_target = true,          .preference.learning_symlink_target = true,
         .preference.permissive_verbose = true  
27  };  };
28    
29  /* Profile version. Currently only 20090903 is defined. */  /* Profile version. Currently only 20100903 is defined. */
30  static unsigned int ccs_profile_version;  static unsigned int ccs_profile_version;
31    
32  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
33  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
34    
35  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
36  static const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {  const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {
37          [CCS_CONFIG_DISABLED] = "disabled",          [CCS_CONFIG_DISABLED] = "disabled",
38          [CCS_CONFIG_LEARNING] = "learning",          [CCS_CONFIG_LEARNING] = "learning",
39          [CCS_CONFIG_PERMISSIVE] = "permissive",          [CCS_CONFIG_PERMISSIVE] = "permissive",
# Line 71  static const char *ccs_mac_keywords[CCS_ Line 64  static const char *ccs_mac_keywords[CCS_
64          = "file::truncate",          = "file::truncate",
65          [CCS_MAC_FILE_SYMLINK]          [CCS_MAC_FILE_SYMLINK]
66          = "file::symlink",          = "file::symlink",
         [CCS_MAC_FILE_REWRITE]  
         = "file::rewrite",  
67          [CCS_MAC_FILE_MKBLOCK]          [CCS_MAC_FILE_MKBLOCK]
68          = "file::mkblock",          = "file::mkblock",
69          [CCS_MAC_FILE_MKCHAR]          [CCS_MAC_FILE_MKCHAR]
# Line 97  static const char *ccs_mac_keywords[CCS_ Line 88  static const char *ccs_mac_keywords[CCS_
88          = "file::umount",          = "file::umount",
89          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_FILE_PIVOT_ROOT]
90          = "file::pivot_root",          = "file::pivot_root",
         [CCS_MAC_FILE_TRANSIT]  
         = "file::transit",  
91          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
92          = "misc::env",          = "misc::env",
93          [CCS_MAC_NETWORK_UDP_BIND]          [CCS_MAC_NETWORK_INET_STREAM_BIND]
94          = "network::inet_udp_bind",          = "network::inet_stream_bind",
95          [CCS_MAC_NETWORK_UDP_CONNECT]          [CCS_MAC_NETWORK_INET_STREAM_LISTEN]
96          = "network::inet_udp_connect",          = "network::inet_stream_listen",
97          [CCS_MAC_NETWORK_TCP_BIND]          [CCS_MAC_NETWORK_INET_STREAM_CONNECT]
98          = "network::inet_tcp_bind",          = "network::inet_stream_connect",
99          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]
100          = "network::inet_tcp_listen",          = "network::inet_stream_accept",
101          [CCS_MAC_NETWORK_TCP_CONNECT]          [CCS_MAC_NETWORK_INET_DGRAM_BIND]
102          = "network::inet_tcp_connect",          = "network::inet_dgram_bind",
103          [CCS_MAC_NETWORK_TCP_ACCEPT]          [CCS_MAC_NETWORK_INET_DGRAM_SEND]
104          = "network::inet_tcp_accept",          = "network::inet_dgram_send",
105          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_MAC_NETWORK_INET_DGRAM_RECV]
106            = "network::inet_dgram_recv",
107            [CCS_MAC_NETWORK_INET_RAW_BIND]
108          = "network::inet_raw_bind",          = "network::inet_raw_bind",
109          [CCS_MAC_NETWORK_RAW_CONNECT]          [CCS_MAC_NETWORK_INET_RAW_SEND]
110          = "network::inet_raw_connect",          = "network::inet_raw_send",
111            [CCS_MAC_NETWORK_INET_RAW_RECV]
112            = "network::inet_raw_recv",
113            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]
114            = "network::unix_stream_bind",
115            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]
116            = "network::unix_stream_listen",
117            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]
118            = "network::unix_stream_connect",
119            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]
120            = "network::unix_stream_accept",
121            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]
122            = "network::unix_dgram_bind",
123            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]
124            = "network::unix_dgram_send",
125            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]
126            = "network::unix_dgram_recv",
127            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]
128            = "network::unix_seqpacket_bind",
129            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]
130            = "network::unix_seqpacket_listen",
131            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT]
132            = "network::unix_seqpacket_connect",
133            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]
134            = "network::unix_seqpacket_accept",
135          [CCS_MAC_SIGNAL]          [CCS_MAC_SIGNAL]
136          = "ipc::signal",          = "ipc::signal",
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  
         = "capability::inet_tcp_create",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]  
         = "capability::inet_tcp_listen",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]  
         = "capability::inet_tcp_connect",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]  
         = "capability::use_inet_udp",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]  
         = "capability::use_inet_ip",  
137          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
138          = "capability::use_route",          = "capability::use_route",
139          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
140          = "capability::use_packet",          = "capability::use_packet",
         [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]  
         = "capability::SYS_MOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]  
         = "capability::SYS_UMOUNT",  
141          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
142          = "capability::SYS_REBOOT",          = "capability::SYS_REBOOT",
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]  
         = "capability::SYS_CHROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]  
         = "capability::SYS_KILL",  
143          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
144          = "capability::SYS_VHANGUP",          = "capability::SYS_VHANGUP",
145          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
# Line 153  static const char *ccs_mac_keywords[CCS_ Line 150  static const char *ccs_mac_keywords[CCS_
150          = "capability::SYS_SETHOSTNAME",          = "capability::SYS_SETHOSTNAME",
151          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
152          = "capability::use_kernel_module",          = "capability::use_kernel_module",
         [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]  
         = "capability::create_fifo",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]  
         = "capability::create_block_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]  
         = "capability::create_char_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]  
         = "capability::create_unix_socket",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]  
         = "capability::SYS_LINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]  
         = "capability::SYS_SYMLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]  
         = "capability::SYS_RENAME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]  
         = "capability::SYS_UNLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]  
         = "capability::SYS_CHMOD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]  
         = "capability::SYS_CHOWN",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]  
         = "capability::SYS_IOCTL",  
153          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
154          = "capability::SYS_KEXEC_LOAD",          = "capability::SYS_KEXEC_LOAD",
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
155          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
156          = "capability::SYS_PTRACE",          = "capability::SYS_PTRACE",
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
157          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
158           + CCS_MAC_CATEGORY_FILE] = "file",           + CCS_MAC_CATEGORY_FILE] = "file",
159          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
# Line 221  static const char *ccs_yesno(const unsig Line 192  static const char *ccs_yesno(const unsig
192          return value ? "yes" : "no";          return value ? "yes" : "no";
193  }  }
194    
195    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
196    {
197            va_list args;
198            const int pos = strlen(buffer);
199            va_start(args, fmt);
200            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
201            va_end(args);
202    }
203    
204  /**  /**
205   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
206   *   *
207   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
208   *   *
209   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
210     */
211    static bool ccs_flush(struct ccs_io_buffer *head)
212    {
213            while (head->r.w_pos) {
214                    const char *w = head->r.w[0];
215                    int len = strlen(w);
216                    if (len) {
217                            if (len > head->read_user_buf_avail)
218                                    len = head->read_user_buf_avail;
219                            if (!len)
220                                    return false;
221                            if (copy_to_user(head->read_user_buf, w, len))
222                                    return false;
223                            head->read_user_buf_avail -= len;
224                            head->read_user_buf += len;
225                            w += len;
226                    }
227                    if (*w) {
228                            head->r.w[0] = w;
229                            return false;
230                    }
231                    /* Add '\0' for audit logs and query. */
232                    if (head->poll) {
233                            if (!head->read_user_buf_avail ||
234                                copy_to_user(head->read_user_buf, "", 1))
235                                    return false;
236                            head->read_user_buf_avail--;
237                            head->read_user_buf++;
238                    }
239                    head->r.w_pos--;
240                    for (len = 0; len < head->r.w_pos; len++)
241                            head->r.w[len] = head->r.w[len + 1];
242            }
243            head->r.avail = 0;
244            return true;
245    }
246    
247    /**
248     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
249     *
250     * @head:   Pointer to "struct ccs_io_buffer".
251     * @string: String to print.
252   *   *
253   * The snprintf() will truncate, but ccs_io_printf() won't.   * Note that @string has to be kept valid until @head is kfree()d.
254     * This means that char[] allocated on stack memory cannot be passed to
255     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
256   */   */
257  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)
258    {
259            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
260                    head->r.w[head->r.w_pos++] = string;
261                    ccs_flush(head);
262            } else
263                    printk(KERN_WARNING "Too many words in a line.\n");
264    }
265    
266    /**
267     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
268     *
269     * @head: Pointer to "struct ccs_io_buffer".
270     * @fmt:  The printf()'s format string, followed by parameters.
271     */
272    void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
273  {  {
274          va_list args;          va_list args;
275          int len;          int len;
276          int pos = head->read_avail;          int pos = head->r.avail;
277          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
278          if (size <= 0)          if (size <= 0)
279                  return false;                  return;
280          va_start(args, fmt);          va_start(args, fmt);
281          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
282          va_end(args);          va_end(args);
283          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
284                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
285          head->read_avail += len;                  return;
286          return true;          }
287            head->r.avail += len;
288            ccs_set_string(head, head->read_buf + pos);
289    }
290    
291    static void ccs_set_space(struct ccs_io_buffer *head)
292    {
293            ccs_set_string(head, " ");
294    }
295    
296    static bool ccs_set_lf(struct ccs_io_buffer *head)
297    {
298            ccs_set_string(head, "\n");
299            return !head->r.w_pos;
300  }  }
301    
302  /**  /**
# Line 270  static struct ccs_profile *ccs_assign_pr Line 321  static struct ccs_profile *ccs_assign_pr
321          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
322          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
323                  ptr = entry;                  ptr = entry;
                 ptr->audit = &ccs_default_profile.preference;  
                 ptr->learning = &ccs_default_profile.preference;  
                 ptr->permissive = &ccs_default_profile.preference;  
                 ptr->enforcing = &ccs_default_profile.preference;  
324                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
325                            CCS_CONFIG_VERBOSE |
326                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
327                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
328                         sizeof(ptr->config));                         sizeof(ptr->config));
# Line 304  static void ccs_check_profile(void) Line 352  static void ccs_check_profile(void)
352                        profile, domain->domainname->name);                        profile, domain->domainname->name);
353          }          }
354          ccs_read_unlock(idx);          ccs_read_unlock(idx);
355          if (ccs_profile_version != 20090903)          if (ccs_profile_version != 20100903)
356                  panic("Profile version %u is not supported.\n",                  panic("Profile version %u is not supported.\n",
357                        ccs_profile_version);                        ccs_profile_version);
358          printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");          printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/09/01\n");
359          printk(KERN_INFO "Mandatory Access Control activated.\n");          printk(KERN_INFO "Mandatory Access Control activated.\n");
360  }  }
361    
# Line 332  static s8 ccs_find_yesno(const char *str Line 380  static s8 ccs_find_yesno(const char *str
380          const char *cp = strstr(string, find);          const char *cp = strstr(string, find);
381          if (cp) {          if (cp) {
382                  cp += strlen(find);                  cp += strlen(find);
383                  if (strncmp(cp, "=yes", 4))                  if (!strncmp(cp, "=yes", 4))
384                          return 1;                          return 1;
385                  else if (strncmp(cp, "=no", 3))                  else if (!strncmp(cp, "=no", 3))
386                          return 0;                          return 0;
387          }          }
388          return -1;          return -1;
# Line 359  static void ccs_set_uint(unsigned int *i Line 407  static void ccs_set_uint(unsigned int *i
407                  sscanf(cp + strlen(find), "=%u", i);                  sscanf(cp + strlen(find), "=%u", i);
408  }  }
409    
410  static void ccs_set_pref(const char *cp, const char *data,  static void ccs_set_pref(const char *name, const char *value,
411                           struct ccs_profile *profile, const bool use_default)                           struct ccs_profile *profile)
412  {  {
413          struct ccs_preference **pref;          if (!strcmp(name, "audit")) {
         bool *verbose;  
         if (!strcmp(data, "audit")) {  
                 if (use_default) {  
                         pref = &profile->audit;  
                         goto set_default;  
                 }  
                 profile->audit = &profile->preference;  
414  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
415                  ccs_set_uint(&profile->preference.audit_max_grant_log, cp,                  ccs_set_uint(&profile->preference.audit_max_grant_log, value,
416                               "max_grant_log");                               "max_grant_log");
417                  ccs_set_uint(&profile->preference.audit_max_reject_log, cp,                  ccs_set_uint(&profile->preference.audit_max_reject_log, value,
418                               "max_reject_log");                               "max_reject_log");
419  #endif  #endif
420                  ccs_set_bool(&profile->preference.audit_task_info, cp,                  ccs_set_bool(&profile->preference.audit_task_info, value,
421                               "task_info");                               "task_info");
422                  ccs_set_bool(&profile->preference.audit_path_info, cp,                  ccs_set_bool(&profile->preference.audit_path_info, value,
423                               "path_info");                               "path_info");
424                  return;                  return;
425          }          }
426          if (!strcmp(data, "enforcing")) {          if (!strcmp(name, "enforcing")) {
427                  if (use_default) {                  ccs_set_uint(&profile->preference.enforcing_penalty, value,
                         pref = &profile->enforcing;  
                         goto set_default;  
                 }  
                 profile->enforcing = &profile->preference;  
                 ccs_set_uint(&profile->preference.enforcing_penalty, cp,  
428                               "penalty");                               "penalty");
429                  verbose = &profile->preference.enforcing_verbose;                  return;
                 goto set_verbose;  
430          }          }
431          if (!strcmp(data, "permissive")) {          if (!strcmp(name, "learning")) {
432                  if (use_default) {                  ccs_set_uint(&profile->preference.learning_max_entry, value,
                         pref = &profile->permissive;  
                         goto set_default;  
                 }  
                 profile->permissive = &profile->preference;  
                 verbose = &profile->preference.permissive_verbose;  
                 goto set_verbose;  
         }  
         if (!strcmp(data, "learning")) {  
                 if (use_default) {  
                         pref = &profile->learning;  
                         goto set_default;  
                 }  
                 profile->learning = &profile->preference;  
                 ccs_set_uint(&profile->preference.learning_max_entry, cp,  
433                               "max_entry");                               "max_entry");
434                  ccs_set_bool(&profile->preference.learning_exec_realpath, cp,                  ccs_set_bool(&profile->preference.learning_exec_realpath,
435                               "exec.realpath");                               value, "exec.realpath");
436                  ccs_set_bool(&profile->preference.learning_exec_argv0, cp,                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
437                               "exec.argv0");                               "exec.argv0");
438                  ccs_set_bool(&profile->preference.learning_symlink_target, cp,                  ccs_set_bool(&profile->preference.learning_symlink_target,
439                               "symlink.target");                               value, "symlink.target");
440                  verbose = &profile->preference.learning_verbose;                  return;
441                  goto set_verbose;          }
         }  
         return;  
  set_default:  
         *pref = &ccs_default_profile.preference;  
         return;  
  set_verbose:  
         ccs_set_bool(verbose, cp, "verbose");  
442  }  }
443    
444  static int ccs_set_mode(const char *cp, char *data,  static int ccs_set_mode(char *name, const char *value,
445                          struct ccs_profile *profile, const bool use_default)                          struct ccs_profile *profile)
446  {  {
447          u8 i;          u8 i;
448          u8 config;          u8 config;
449          if (!strcmp(data, "CONFIG")) {          if (!strcmp(name, "CONFIG")) {
450                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
451                          + CCS_MAX_MAC_CATEGORY_INDEX;                          + CCS_MAX_MAC_CATEGORY_INDEX;
452                  config = profile->default_config;                  config = profile->default_config;
453          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
454                  config = 0;                  config = 0;
455                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
456                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
457                          if (strcmp(data, ccs_mac_keywords[i]))                          if (strcmp(name, ccs_mac_keywords[i]))
458                                  continue;                                  continue;
459                          config = profile->config[i];                          config = profile->config[i];
460                          break;                          break;
# Line 451  static int ccs_set_mode(const char *cp, Line 465  static int ccs_set_mode(const char *cp,
465          } else {          } else {
466                  return -EINVAL;                  return -EINVAL;
467          }          }
468          if (use_default) {          if (strstr(value, "use_default")) {
469                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
470          } else {          } else {
471                  u8 mode;                  u8 mode;
472                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode--)                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
473                          if (strstr(cp, ccs_mode[mode]))                          if (strstr(value, ccs_mode[mode]))
474                                  /*                                  /*
475                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
476                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
477                                   */                                   */
478                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
479                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
480                          switch (ccs_find_yesno(cp, "grant_log")) {                          switch (ccs_find_yesno(value, "verbose")) {
481                            case 1:
482                                    config |= CCS_CONFIG_VERBOSE;
483                                    break;
484                            case 0:
485                                    config &= ~CCS_CONFIG_VERBOSE;
486                                    break;
487                            }
488    #ifdef CONFIG_CCSECURITY_AUDIT
489                            switch (ccs_find_yesno(value, "grant_log")) {
490                          case 1:                          case 1:
491                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
492                                  break;                                  break;
# Line 472  static int ccs_set_mode(const char *cp, Line 494  static int ccs_set_mode(const char *cp,
494                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
495                                  break;                                  break;
496                          }                          }
497                          switch (ccs_find_yesno(cp, "reject_log")) {                          switch (ccs_find_yesno(value, "reject_log")) {
498                          case 1:                          case 1:
499                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
500                                  break;                                  break;
# Line 480  static int ccs_set_mode(const char *cp, Line 502  static int ccs_set_mode(const char *cp,
502                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
503                                  break;                                  break;
504                          }                          }
                 }  
505  #endif  #endif
506                    }
507          }          }
508          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
509              + CCS_MAX_MAC_CATEGORY_INDEX)              + CCS_MAX_MAC_CATEGORY_INDEX)
# Line 501  static int ccs_set_mode(const char *cp, Line 523  static int ccs_set_mode(const char *cp,
523  static int ccs_write_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
524  {  {
525          char *data = head->write_buf;          char *data = head->write_buf;
526          bool use_default = false;          unsigned int i;
527          char *cp;          char *cp;
         int i;  
528          struct ccs_profile *profile;          struct ccs_profile *profile;
529          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
530                  return 0;                  return 0;
531          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
532          if (data == cp) {          if (*cp != '-')
533                  profile = &ccs_default_profile;                  return -EINVAL;
534          } else {          data = cp + 1;
535                  if (*cp != '-')          profile = ccs_assign_profile(i);
536                          return -EINVAL;          if (!profile)
537                  data = cp + 1;                  return -EINVAL;
                 profile = ccs_assign_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
         }  
538          cp = strchr(data, '=');          cp = strchr(data, '=');
539          if (!cp)          if (!cp)
540                  return -EINVAL;                  return -EINVAL;
541          *cp++ = '\0';          *cp++ = '\0';
         if (profile != &ccs_default_profile)  
                 use_default = strstr(cp, "use_default") != NULL;  
542          if (ccs_str_starts(&data, "PREFERENCE::")) {          if (ccs_str_starts(&data, "PREFERENCE::")) {
543                  ccs_set_pref(cp, data, profile, use_default);                  ccs_set_pref(data, cp, profile);
544                  return 0;                  return 0;
545          }          }
         if (profile == &ccs_default_profile)  
                 return -EINVAL;  
546          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
547                  const struct ccs_path_info *old_comment = profile->comment;                  const struct ccs_path_info *old_comment = profile->comment;
548                  profile->comment = ccs_get_name(cp);                  profile->comment = ccs_get_name(cp);
549                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
550                  return 0;                  return 0;
551          }          }
552          return ccs_set_mode(cp, data, profile, use_default);          return ccs_set_mode(data, cp, profile);
553  }  }
554    
555  static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)  static void ccs_print_preference(struct ccs_io_buffer *head, const int index)
556  {  {
557          struct ccs_preference *pref = &ccs_default_profile.preference;          struct ccs_profile *profile = ccs_profile_ptr[index];
558          const struct ccs_profile *profile = idx >= 0 ?          struct ccs_preference *pref = &profile->preference;
559                  ccs_profile_ptr[idx] : NULL;          ccs_io_printf(head, "%u-PREFERENCE::%s={ "
         char buffer[16] = "";  
         if (profile) {  
                 buffer[sizeof(buffer) - 1] = '\0';  
                 snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);  
         }  
         if (profile) {  
                 pref = profile->audit;  
                 if (pref == &ccs_default_profile.preference)  
                         pref = NULL;  
         }  
         if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "  
560  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
561                                     "max_grant_log=%u max_reject_log=%u "                        "max_grant_log=%u max_reject_log=%u "
562  #endif  #endif
563                                     "task_info=%s path_info=%s }\n", buffer,                        "task_info=%s path_info=%s }\n", index,
564                                     "audit",                        "audit",
565  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
566                                     pref->audit_max_grant_log,                        pref->audit_max_grant_log,
567                                     pref->audit_max_reject_log,                        pref->audit_max_reject_log,
568  #endif  #endif
569                                     ccs_yesno(pref->audit_task_info),                        ccs_yesno(pref->audit_task_info),
570                                     ccs_yesno(pref->audit_path_info)))                        ccs_yesno(pref->audit_path_info));
571                  return false;          ccs_io_printf(head, "%u-PREFERENCE::%s={ "
572          if (profile) {                        "max_entry=%u exec.realpath=%s "
573                  pref = profile->learning;                        "exec.argv0=%s symlink.target=%s }\n",
574                  if (pref == &ccs_default_profile.preference)                        index, "learning",
575                          pref = NULL;                        pref->learning_max_entry,
576          }                        ccs_yesno(pref->learning_exec_realpath),
577          if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "                        ccs_yesno(pref->learning_exec_argv0),
578                                     "verbose=%s max_entry=%u exec.realpath=%s "                        ccs_yesno(pref->learning_symlink_target));
579                                     "exec.argv0=%s symlink.target=%s }\n",          ccs_io_printf(head, "%u-PREFERENCE::%s={ penalty=%u }\n", index,
580                                     buffer, "learning",                        "enforcing", pref->enforcing_penalty);
581                                     ccs_yesno(pref->learning_verbose),  }
582                                     pref->learning_max_entry,  
583                                     ccs_yesno(pref->learning_exec_realpath),  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
584                                     ccs_yesno(pref->learning_exec_argv0),  {
585                                     ccs_yesno(pref->learning_symlink_target)))          ccs_io_printf(head, "={ mode=%s verbose=%s", ccs_mode[config & 3],
586                  return false;                        ccs_yesno(config & CCS_CONFIG_VERBOSE));
587          if (profile) {  #ifdef CONFIG_CCSECURITY_AUDIT
588                  pref = profile->permissive;          ccs_io_printf(head, " grant_log=%s reject_log=%s",
589                  if (pref == &ccs_default_profile.preference)                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
590                          pref = NULL;                        ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
591          }  #endif
592          if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",          ccs_set_string(head, " }\n");
                                    buffer, "permissive",  
                                    ccs_yesno(pref->permissive_verbose)))  
                 return false;  
         if (profile) {  
                 pref = profile->enforcing;  
                 if (pref == &ccs_default_profile.preference)  
                         pref = NULL;  
         }  
         return !pref || ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "  
                                       "penalty=%u }\n", buffer, "enforcing",  
                                       ccs_yesno(pref->enforcing_verbose),  
                                       pref->enforcing_penalty);  
593  }  }
594    
595  /**  /**
# Line 609  static bool ccs_print_preference(struct Line 599  static bool ccs_print_preference(struct
599   */   */
600  static void ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
601  {  {
602          int index;          u8 index;
603          if (head->read_eof)          const struct ccs_profile *profile;
604                  return;   next:
605          if (head->read_bit)          index = head->r.index;
606                  goto body;          profile = ccs_profile_ptr[index];
607          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          switch (head->r.step) {
608          ccs_print_preference(head, -1);          case 0:
609          head->read_bit = 1;                  ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20100903");
610   body:                  head->r.step++;
611          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {                  break;
612                  bool done;          case 1:
613                  u8 config;                  for ( ; head->r.index < CCS_MAX_PROFILES;
614                  int i;                        head->r.index++)
615                  int pos;                          if (ccs_profile_ptr[head->r.index])
616                  const struct ccs_profile *profile = ccs_profile_ptr[index];                                  break;
617                  const struct ccs_path_info *comment;                  if (head->r.index == CCS_MAX_PROFILES)
618                  head->read_step = index;                          return;
619                  if (!profile)                  head->r.step++;
620                          continue;                  break;
621                  pos = head->read_avail;          case 2:
622                  comment = profile->comment;                  {
623                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,                          const struct ccs_path_info *comment = profile->comment;
624                                       comment ? comment->name : "");                          ccs_io_printf(head, "%u-COMMENT=", index);
625                  if (!done)                          ccs_set_string(head, comment ? comment->name : "");
626                          goto out;                          ccs_set_lf(head);
627                  config = profile->default_config;                          head->r.step++;
628  #ifdef CONFIG_CCSECURITY_AUDIT                  }
629                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                  break;
630                                     "grant_log=%s reject_log=%s }\n", index,          case 3:
631                                     "CONFIG", "", ccs_mode[config & 3],                  {
632                                     ccs_yesno(config &                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
633                                               CCS_CONFIG_WANT_GRANT_LOG),                          ccs_print_config(head, profile->default_config);
634                                     ccs_yesno(config &                          head->r.bit = 0;
635                                               CCS_CONFIG_WANT_REJECT_LOG)))                          head->r.step++;
636                          goto out;                  }
637  #else                  break;
638                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,          case 4:
639                                     "CONFIG", "", ccs_mode[config & 3]))                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
640                          goto out;                                + CCS_MAX_CAPABILITY_INDEX
641  #endif                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
642                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                          const u8 i = head->r.bit;
643                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                          const u8 config = profile->config[i];
 #ifdef CONFIG_CCSECURITY_AUDIT  
                         const char *g;  
                         const char *r;  
 #endif  
                         config = profile->config[i];  
644                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
645                                  continue;                                  continue;
646  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
647                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                        ccs_mac_keywords[i]);
648                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          ccs_print_config(head, config);
649                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                          head->r.bit++;
650                                             "grant_log=%s reject_log=%s }\n",                          break;
651                                             index, "CONFIG::",                  }
652                                             ccs_mac_keywords[i],                  if (head->r.bit == CCS_MAX_MAC_INDEX
653                                             ccs_mode[config & 3], g, r))                      + CCS_MAX_CAPABILITY_INDEX
654                                  goto out;                      + CCS_MAX_MAC_CATEGORY_INDEX) {
655  #else                          ccs_print_preference(head, index);
656                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",                          head->r.index++;
657                                             index, "CONFIG::",                          head->r.step = 1;
                                            ccs_mac_keywords[i],  
                                            ccs_mode[config & 3]))  
                                 goto out;  
 #endif  
658                  }                  }
                 if (!ccs_print_preference(head, index))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
659                  break;                  break;
660          }          }
661          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
662                  head->read_eof = true;                  goto next;
663  }  }
664    
665  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,  static bool ccs_same_manager(const struct ccs_acl_head *a,
666                                     const struct ccs_acl_head *b)                               const struct ccs_acl_head *b)
667  {  {
668          return container_of(a, struct ccs_manager, head)->manager          return container_of(a, struct ccs_manager, head)->manager
669                  == container_of(b, struct ccs_manager, head)->manager;                  == container_of(b, struct ccs_manager, head)->manager;
# Line 717  static int ccs_update_manager_entry(cons Line 693  static int ccs_update_manager_entry(cons
693          if (!e.manager)          if (!e.manager)
694                  return error;                  return error;
695          error = ccs_update_policy(&e.head, sizeof(e), is_delete,          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
696                                    CCS_ID_MANAGER, ccs_same_manager_entry);                                    &ccs_policy_list[CCS_ID_MANAGER],
697                                      ccs_same_manager);
698          ccs_put_name(e.manager);          ccs_put_name(e.manager);
699          return error;          return error;
700  }  }
# Line 749  static int ccs_write_manager(struct ccs_ Line 726  static int ccs_write_manager(struct ccs_
726   */   */
727  static void ccs_read_manager(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
728  {  {
729          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
730                  return;                  return;
731          list_for_each_cookie(pos, head->read_var2,          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
732                               &ccs_policy_list[CCS_ID_MANAGER]) {                  struct ccs_manager *ptr =
733                  struct ccs_manager *ptr                          list_entry(head->r.acl, typeof(*ptr), head.list);
                         = list_entry(pos, typeof(*ptr), head.list);  
734                  if (ptr->head.is_deleted)                  if (ptr->head.is_deleted)
735                          continue;                          continue;
736                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
737                          return;                          return;
738                    ccs_set_string(head, ptr->manager->name);
739                    ccs_set_lf(head);
740          }          }
741          head->read_eof = true;          head->r.eof = true;
742  }  }
743    
744  /**  /**
# Line 817  static bool ccs_manager(void) Line 794  static bool ccs_manager(void)
794  }  }
795    
796  /**  /**
  * ccs_find_condition_part - Find condition part from the statement.  
  *  
  * @data: String to parse.  
  *  
  * Returns pointer to the condition part if it was found in the statement,  
  * NULL otherwise.  
  */  
 static char *ccs_find_condition_part(char *data)  
 {  
         char *cp = strstr(data, " if ");  
         if (!cp)  
                 cp = strstr(data, " ; set ");  
         if (cp)  
                 *cp++ = '\0';  
         return cp;  
 }  
   
 /**  
797   * ccs_select_one - Parse select command.   * ccs_select_one - Parse select command.
798   *   *
799   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 849  static bool ccs_select_one(struct ccs_io Line 808  static bool ccs_select_one(struct ccs_io
808          unsigned int pid;          unsigned int pid;
809          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
810          bool global_pid = false;          bool global_pid = false;
811          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
812                  head->read_execute_only = true;                  head->r.print_execute_only = true;
813                  return true;                  return true;
814          }          }
815          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
# Line 874  static bool ccs_select_one(struct ccs_io Line 833  static bool ccs_select_one(struct ccs_io
833                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
834          } else          } else
835                  return false;                  return false;
836          head->write_var1 = domain;          head->w.domain = domain;
837          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
838          if (!head->read_buf)          if (!head->read_buf)
839                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
840          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
841            head->r.print_this_domain_only = true;
842            if (domain)
843                    head->r.domain = &domain->list;
844            else
845                    head->r.eof = true;
846          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
847          head->read_single_domain = true;          if (domain && domain->is_deleted)
848          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");  
         }  
849          return true;          return true;
850  }  }
851    
852    static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
853                                     const struct ccs_acl_info *b)
854    {
855            const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
856            const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
857            return p1->handler == p2->handler;
858    }
859    
860    static bool ccs_same_task_acl(const struct ccs_acl_info *a,
861                                  const struct ccs_acl_info *b)
862    {
863            const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
864            const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
865            return p1->domainname == p2->domainname;
866    }
867    
868    /**
869     * ccs_write_task - Update task related list.
870     *
871     * @param: Pointer to "struct ccs_acl_param".
872     *
873     * Returns 0 on success, negative value otherwise.
874     */
875    static int ccs_write_task(struct ccs_acl_param *param)
876    {
877            int error;
878            const bool is_auto = ccs_str_starts(&param->data,
879                                                "auto_domain_transition ");
880            if (!is_auto && !ccs_str_starts(&param->data,
881                                            "manual_domain_transition ")) {
882                    struct ccs_handler_acl e = { };
883                    char *handler;
884                    if (ccs_str_starts(&param->data, "auto_execute_handler "))
885                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
886                    else if (ccs_str_starts(&param->data,
887                                            "denied_execute_handler "))
888                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
889                    else
890                            return -EINVAL;
891                    handler = ccs_read_token(param);
892                    if (!ccs_correct_path(handler))
893                            return -EINVAL;
894                    e.handler = ccs_get_name(handler);
895                    if (!e.handler)
896                            return -ENOMEM;
897                    if (e.handler->is_patterned)
898                            error = -EINVAL; /* No patterns allowed. */
899                    else
900                            error = ccs_update_domain(&e.head, sizeof(e), param,
901                                                      ccs_same_handler_acl, NULL);
902                    ccs_put_name(e.handler);
903            } else {
904                    struct ccs_task_acl e = {
905                            .head.type = is_auto ?
906                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
907                            .domainname = ccs_get_domainname(param),
908                    };
909                    if (!e.domainname)
910                            error = -EINVAL;
911                    else
912                            error = ccs_update_domain(&e.head, sizeof(e), param,
913                                                      ccs_same_task_acl, NULL);
914                    ccs_put_name(e.domainname);
915            }
916            return error;
917    }
918    
919  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
920                               const bool is_delete)                               const bool is_delete)
921  {  {
922            struct ccs_acl_param param = {
923                    .data = data,
924                    .domain = domain,
925                    .is_delete = is_delete,
926            };
927          static const struct {          static const struct {
928                  const char *keyword;                  const char *keyword;
929                  int (*write) (char *, struct ccs_domain_info *,                  int (*write) (struct ccs_acl_param *);
930                                struct ccs_condition *, const bool);          } ccs_callback[7] = {
931          } ccs_callback[5] = {                  { "file ", ccs_write_file },
932                  { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },                  { "network inet ", ccs_write_inet_network },
933                  { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },                  { "network unix ", ccs_write_unix_network },
934                  { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },                  { "misc ", ccs_write_misc },
935                  { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },                  { "capability ", ccs_write_capability },
936                  { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }                  { "ipc ", ccs_write_ipc },
937                    { "task ", ccs_write_task },
938          };          };
         int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,  
                       const bool) = ccs_write_file;  
         int error;  
939          u8 i;          u8 i;
940          struct ccs_condition *cond = NULL;          for (i = 0; i < 7; i++) {
941          char *cp = ccs_find_condition_part(data);                  if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         for (i = 0; i < 5; i++) {  
                 if (!ccs_str_starts(&data, ccs_callback[i].keyword))  
942                          continue;                          continue;
943                  write = ccs_callback[i].write;                  return ccs_callback[i].write(&param);
                 break;  
944          }          }
945          error = write(data, domain, cond, is_delete);          return -EINVAL;
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
946  }  }
947    
948  static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {  static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
949          [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",          [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
         [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",  
         [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]  
         = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",  
         [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]  
         = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",  
950          [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"          [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
951  };  };
952                    
# Line 956  static const char *ccs_dif[CCS_MAX_DOMAI Line 960  static const char *ccs_dif[CCS_MAX_DOMAI
960  static int ccs_write_domain(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
961  {  {
962          char *data = head->write_buf;          char *data = head->write_buf;
963          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
964          bool is_delete = false;          bool is_delete = false;
965          bool is_select = false;          bool is_select = false;
966          unsigned int profile;          unsigned int profile;
# Line 976  static int ccs_write_domain(struct ccs_i Line 980  static int ccs_write_domain(struct ccs_i
980                  else if (is_select)                  else if (is_select)
981                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
982                  else                  else
983                          domain = ccs_assign_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
984                  head->write_var1 = domain;                  head->w.domain = domain;
985                  return 0;                  return 0;
986          }          }
987          if (!domain)          if (!domain)
# Line 989  static int ccs_write_domain(struct ccs_i Line 993  static int ccs_write_domain(struct ccs_i
993                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
994                  return 0;                  return 0;
995          }          }
996            if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
997                && profile < CCS_MAX_ACL_GROUPS) {
998                    domain->group = (u8) profile;
999                    return 0;
1000            }
1001          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1002                  const char *cp = ccs_dif[profile];                  const char *cp = ccs_dif[profile];
1003                  if (strncmp(data, cp, strlen(cp) - 1))                  if (strncmp(data, cp, strlen(cp) - 1))
# Line 1004  static int ccs_write_domain(struct ccs_i Line 1013  static int ccs_write_domain(struct ccs_i
1013   *   *
1014   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1015   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
1016   */   */
1017  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1018                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1019  {  {
1020          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1021          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1022                  head->read_avail--;                  ccs_set_space(head);
1023          if (ptr->is_group)          if (ptr->is_group) {
1024                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1025                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1026          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1027  }                  if (cond)
1028                            ccs_set_string(head, "\"");
1029  /**                  ccs_set_string(head, ptr->filename->name);
1030   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1031   *                          ccs_set_string(head, "\"");
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_name_union".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,  
                                         const struct ccs_name_union *ptr)  
 {  
         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 void ccs_print_number(char *buffer, int buffer_len,  
                              const struct ccs_number_union *ptr)  
 {  
         int i;  
         unsigned long min = ptr->values[0];  
         const unsigned long max = ptr->values[1];  
         u8 min_type = ptr->value_type[0];  
         const u8 max_type = ptr->value_type[1];  
         memset(buffer, 0, buffer_len);  
         buffer_len -= 2;  
         for (i = 0; i < 2; i++) {  
                 int len;  
                 switch (min_type) {  
                 case CCS_VALUE_TYPE_HEXADECIMAL:  
                         snprintf(buffer, buffer_len, "0x%lX", min);  
                         break;  
                 case CCS_VALUE_TYPE_OCTAL:  
                         snprintf(buffer, buffer_len, "0%lo", min);  
                         break;  
                 default:  
                         snprintf(buffer, buffer_len, "%lu", min);  
                         break;  
                 }  
                 if (min == max && min_type == max_type)  
                         break;  
                 len = strlen(buffer);  
                 buffer[len++] = '-';  
                 buffer += len;  
                 buffer_len -= len;  
                 min_type = max_type;  
                 min = max;  
1032          }          }
1033  }  }
1034    
1035  /**  /**
  * ccs_print_number_union_common - Print a ccs_number_union.  
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
  * @need_space: True if a space character is needed.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,  
                                           const struct ccs_number_union *ptr,  
                                           const bool need_space)  
 {  
         char buffer[128];  
         if (need_space && !ccs_io_printf(head, " "))  
                 return false;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         ccs_print_number(buffer, sizeof(buffer), ptr);  
         return ccs_io_printf(head, "%s", buffer);  
 }  
   
 /**  
1036   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1037   *   *
1038   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1039   * @ptr:        Pointer to "struct ccs_number_union".   * @ptr:  Pointer to "struct ccs_number_union".
  *  
  * Returns true on success, false otherwise.  
1040   */   */
1041  static bool ccs_print_number_union(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1042                                     const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1043  {  {
1044          return ccs_print_number_union_common(head, ptr, true);          if (!head->r.print_cond_part)
1045  }                  ccs_set_space(head);
1046            if (ptr->is_group) {
1047  /**                  ccs_set_string(head, "@");
1048   * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.                  ccs_set_string(head, ptr->group->group_name->name);
1049   *          } else {
1050   * @head:       Pointer to "struct ccs_io_buffer".                  int i;
1051   * @ptr:        Pointer to "struct ccs_number_union".                  unsigned long min = ptr->values[0];
1052   *                  const unsigned long max = ptr->values[1];
1053   * Returns true on success, false otherwise.                  u8 min_type = ptr->value_type[0];
1054   */                  const u8 max_type = ptr->value_type[1];
1055  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,                  char buffer[128];
1056                                             const struct ccs_number_union *ptr)                  buffer[0] = '\0';
1057  {                  for (i = 0; i < 2; i++) {
1058          return ccs_print_number_union_common(head, ptr, false);                          switch (min_type) {
1059                            case CCS_VALUE_TYPE_HEXADECIMAL:
1060                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1061                                                  min);
1062                                    break;
1063                            case CCS_VALUE_TYPE_OCTAL:
1064                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1065                                                  min);
1066                                    break;
1067                            default:
1068                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1069                                                  min);
1070                                    break;
1071                            }
1072                            if (min == max && min_type == max_type)
1073                                    break;
1074                            ccs_addprintf(buffer, sizeof(buffer), "-");
1075                            min_type = max_type;
1076                            min = max;
1077                    }
1078                    ccs_io_printf(head, "%s", buffer);
1079            }
1080  }  }
1081    
1082  /**  /**
1083   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1084   *   *
1085   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1086   * @cond: Pointer to "struct ccs_condition". Maybe NULL.   * @cond: Pointer to "struct ccs_condition".
1087   *   *
1088   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1089   */   */
1090  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1091                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1092  {  {
1093          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1094          const struct ccs_number_union *numbers_p;          case 0:
1095          const struct ccs_name_union *names_p;                  head->r.cond_index = 0;
1096          const struct ccs_argv *argv;                  head->r.cond_step++;
1097          const struct ccs_envp *envp;                  /* fall through */
1098          u16 condc;          case 1:
1099          u16 i;                  {
1100          u16 j;                          const u16 condc = cond->condc;
1101          char buffer[32];                          const struct ccs_condition_element *condp =
1102          if (!cond)                                  (typeof(condp)) (cond + 1);
1103                  goto no_condition;                          const struct ccs_number_union *numbers_p =
1104          condc = cond->condc;                                  (typeof(numbers_p)) (condp + condc);
1105          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_name_union *names_p =
1106          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(names_p))
1107          names_p = (const struct ccs_name_union *)                                  (numbers_p + cond->numbers_count);
1108                  (numbers_p + cond->numbers_count);                          const struct ccs_argv *argv =
1109          argv = (const struct ccs_argv *) (names_p + cond->names_count);                                  (typeof(argv)) (names_p + cond->names_count);
1110          envp = (const struct ccs_envp *) (argv + cond->argc);                          const struct ccs_envp *envp =
1111          memset(buffer, 0, sizeof(buffer));                                  (typeof(envp)) (argv + cond->argc);
1112          if (condc && !ccs_io_printf(head, "%s", " if"))                          u16 skip;
1113                  goto out;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1114          for (i = 0; i < condc; i++) {                                  const u8 left = condp->left;
1115                  const u8 match = condp->equals;                                  const u8 right = condp->right;
1116                  const u8 left = condp->left;                                  condp++;
1117                  const u8 right = condp->right;                                  switch (left) {
1118                  condp++;                                  case CCS_ARGV_ENTRY:
1119                  switch (left) {                                          argv++;
1120                  case CCS_ARGV_ENTRY:                                          continue;
1121                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  case CCS_ENVP_ENTRY:
1122                                             argv->index, argv->is_not ?                                          envp++;
1123                                             "!=" : "=", argv->value->name))                                          continue;
1124                                  goto out;                                  case CCS_NUMBER_UNION:
1125                          argv++;                                          numbers_p++;
1126                          continue;                                          break;
1127                  case CCS_ENVP_ENTRY:                                  }
1128                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  switch (right) {
1129                                             envp->name->name, envp->is_not ?                                  case CCS_NAME_UNION:
1130                                             "!=" : "="))                                          names_p++;
1131                                  goto out;                                          break;
1132                          if (envp->value) {                                  case CCS_NUMBER_UNION:
1133                                  if (!ccs_io_printf(head, "\"%s\"",                                          numbers_p++;
1134                                                     envp->value->name))                                          break;
1135                                          goto out;                                  }
1136                          } else {                          }
1137                                  if (!ccs_io_printf(head, "NULL"))                          while (head->r.cond_index < condc) {
1138                                          goto out;                                  const u8 match = condp->equals;
1139                                    const u8 left = condp->left;
1140                                    const u8 right = condp->right;
1141                                    if (!ccs_flush(head))
1142                                            return false;
1143                                    condp++;
1144                                    head->r.cond_index++;
1145                                    ccs_set_space(head);
1146                                    switch (left) {
1147                                    case CCS_ARGV_ENTRY:
1148                                            ccs_io_printf(head,
1149                                                          "exec.argv[%u]%s\"%s\"",
1150                                                          argv->index,
1151                                                          argv->is_not ?
1152                                                          "!=" : "=",
1153                                                          argv->value->name);
1154                                            argv++;
1155                                            continue;
1156                                    case CCS_ENVP_ENTRY:
1157                                            ccs_io_printf(head,
1158                                                          "exec.envp[\"%s\"]%s",
1159                                                          envp->name->name,
1160                                                          envp->is_not ?
1161                                                          "!=" : "=");
1162                                            if (envp->value) {
1163                                                    ccs_set_string(head, "\"");
1164                                                    ccs_set_string(head, envp->
1165                                                                   value->name);
1166                                                    ccs_set_string(head, "\"");
1167                                            } else {
1168                                                    ccs_set_string(head, "NULL");
1169                                            }
1170                                            envp++;
1171                                            continue;
1172                                    case CCS_NUMBER_UNION:
1173                                            ccs_print_number_union(head,
1174                                                                   numbers_p++);
1175                                            break;
1176                                    default:
1177                                            ccs_set_string(head,
1178                                                   ccs_condition_keyword[left]);
1179                                            break;
1180                                    }
1181                                    ccs_set_string(head, match ? "=" : "!=");
1182                                    switch (right) {
1183                                    case CCS_NAME_UNION:
1184                                            ccs_print_name_union(head, names_p++);
1185                                            break;
1186                                    case CCS_NUMBER_UNION:
1187                                            ccs_print_number_union(head,
1188                                                                   numbers_p++);
1189                                            break;
1190                                    default:
1191                                            ccs_set_string(head,
1192                                                   ccs_condition_keyword[right]);
1193                                            break;
1194                                    }
1195                          }                          }
                         envp++;  
                         continue;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (left >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, " %s",  
                                            ccs_condition_keyword[left]))  
                                 goto out;  
                         break;  
1196                  }                  }
1197                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1198                          goto out;                  /* fall through */
1199                  switch (right) {          case 2:
1200                  case CCS_NAME_UNION:                  if (!ccs_flush(head))
                         if (!ccs_print_name_union_quoted(head, names_p++))  
                                 goto out;  
                         break;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union_nospace(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (right >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, "%s",  
                                            ccs_condition_keyword[right]))  
                                 goto out;  
1201                          break;                          break;
1202                    head->r.cond_step++;
1203                    /* fall through */
1204            case 3:
1205                    if (cond->audit)
1206                            ccs_io_printf(head, " audit=%s",
1207                                          ccs_yesno(cond->audit == 2));
1208                    if (cond->transit) {
1209                            ccs_set_string(head, " auto_domain_transitition=\"");
1210                            ccs_set_string(head, cond->transit->name);
1211                            ccs_set_string(head, "\"");
1212                  }                  }
1213          }                  ccs_set_lf(head);
         i = cond->post_state[3];  
         if (!i)  
                 goto no_condition;  
         if (!ccs_io_printf(head, " ; set"))  
                 goto out;  
         for (j = 0; j < 3; j++) {  
                 if (!(i & (1 << j)))  
                         continue;  
                 if (!ccs_io_printf(head, " task.state[%u]=%u", j,  
                                    cond->post_state[j]))  
                         goto out;  
         }  
         if (i & (1 << 4)) {  
                 if (!ccs_io_printf(head, " audit=%s",  
                                    ccs_yesno(cond->post_state[4])))  
                         goto out;  
         }  
  no_condition:  
         if (ccs_io_printf(head, "\n"))  
1214                  return true;                  return true;
  out:  
         return false;  
 }  
   
 /**  
  * ccs_print_path_acl - Print a path ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_acl(struct ccs_io_buffer *head,  
                                struct ccs_path_acl *ptr)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE  
                     && bit != CCS_TYPE_TRANSIT)  
                         continue;  
                 /* Print "read/write" instead of "read" and "write". */  
                 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)  
                     && (perm & (1 << CCS_TYPE_READ_WRITE)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, ptr->head.cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1215          }          }
1216          head->read_bit = 0;          return false;
         return true;  
1217  }  }
1218    
1219  /**  /**
1220   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_fns - Find next set bit.
1221   *   *
1222   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1223   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @bit:  First bit to find.
1224   *   *
1225   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1226   */   */
1227  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                                        struct ccs_path_number3_acl *ptr)  
1228  {  {
1229          int pos;          for ( ; bit < 8; bit++)
1230          u8 bit;                  if (perm & (1 << bit))
1231          const u16 perm = ptr->perm;                          break;
1232          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;          return bit;
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path_number32keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->mode) ||  
                     !ccs_print_number_union(head, &ptr->major) ||  
                     !ccs_print_number_union(head, &ptr->minor) ||  
                     !ccs_print_condition(head, ptr->head.cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1233  }  }
1234    
1235  /**  static void ccs_set_group(struct ccs_io_buffer *head)
  * ccs_print_path2_acl - Print a path2 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path2_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  
                                 struct ccs_path2_acl *ptr)  
1236  {  {
1237          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1238          u8 bit;                  ccs_io_printf(head, "acl_group %u ", head->r.group_index);
         const u8 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path22keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name1) ||  
                     !ccs_print_name_union(head, &ptr->name2) ||  
                     !ccs_print_condition(head, ptr->head.cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1239  }  }
1240    
1241  /**  /**
1242   * ccs_print_path_number_acl - Print a path_number ACL entry.   * ccs_print_entry - Print an ACL entry.
1243   *   *
1244   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1245   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
1246   *   *
1247   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1248   */   */
1249  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1250                                        struct ccs_path_number_acl *ptr)                              const struct ccs_acl_info *acl)
1251  {  {
1252          int pos;          const u8 acl_type = acl->type;
1253          u8 bit;          u8 bit;
1254          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1255          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1256               bit++) {          if (acl->is_deleted)
1257                  if (!(perm & (1 << bit)))                  return true;
1258                          continue;   next:
1259                  pos = head->read_avail;          bit = head->r.bit;
1260                  if (!ccs_io_printf(head, "allow_%s",          if (!ccs_flush(head))
                                    ccs_path_number2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->number) ||  
                     !ccs_print_condition(head, ptr->head.cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
 }  
   
 /**  
  * ccs_print_env_acl - Print an evironment variable name's ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_env_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_env_acl(struct ccs_io_buffer *head,  
                               struct ccs_env_acl *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||  
             !ccs_print_condition(head, ptr->head.cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_capability_acl - Print a capability ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_capability_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  
                                      struct ccs_capability_acl *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",  
                            ccs_cap2keyword(ptr->operation)) ||  
             !ccs_print_condition(head, ptr->head.cond)) {  
                 head->read_avail = pos;  
1261                  return false;                  return false;
1262          }          else if (acl_type == CCS_TYPE_PATH_ACL) {
1263          return true;                  struct ccs_path_acl *ptr
1264  }                          = container_of(acl, typeof(*ptr), head);
1265                    const u16 perm = ptr->perm;
1266  /**                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1267   * ccs_print_network_acl - Print a network ACL entry.                          if (!(perm & (1 << bit)))
1268   *                                  continue;
1269   * @head: Pointer to "struct ccs_io_buffer".                          if (head->r.print_execute_only &&
1270   * @ptr:  Pointer to "struct ccs_ip_network_acl".                              bit != CCS_TYPE_EXECUTE
1271   *                              /* && bit != CCS_TYPE_TRANSIT */)
1272   * Returns true on success, false otherwise.                                  continue;
1273   */                          break;
1274  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                  }
1275                                    struct ccs_ip_network_acl *ptr)                  if (bit >= CCS_MAX_PATH_OPERATION)
1276  {                          goto done;
1277          int pos;                  ccs_set_group(head);
1278          u8 bit;                  ccs_set_string(head, "file ");
1279          const u8 perm = ptr->perm;                  ccs_set_string(head, ccs_path_keyword[bit]);
1280          char buf[128];                  ccs_print_name_union(head, &ptr->name);
1281          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {          } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1282                  const char *w[2] = { "", "" };                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1283                  if (!(perm & (1 << bit)))                  struct ccs_handler_acl *ptr
1284                          continue;                          = container_of(acl, typeof(*ptr), head);
1285                  pos = head->read_avail;                  ccs_set_group(head);
1286                    ccs_set_string(head, "task ");
1287                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1288                                   ? "auto_execute_handler " :
1289                                   "denied_execute_handler ");
1290                    ccs_set_string(head, ptr->handler->name);
1291            } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1292                       acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1293                    struct ccs_task_acl *ptr =
1294                            container_of(acl, typeof(*ptr), head);
1295                    ccs_set_group(head);
1296                    ccs_set_string(head, "task ");
1297                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1298                                   "auto_domain_transition " :
1299                                   "manual_domain_transition ");
1300                    ccs_set_string(head, ptr->domainname->name);
1301            } else if (head->r.print_execute_only) {
1302                    return true;
1303            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1304                    struct ccs_mkdev_acl *ptr =
1305                            container_of(acl, typeof(*ptr), head);
1306                    bit = ccs_fns(ptr->perm, bit);
1307                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1308                            goto done;
1309                    ccs_set_group(head);
1310                    ccs_set_string(head, "file ");
1311                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1312                    ccs_print_name_union(head, &ptr->name);
1313                    ccs_print_number_union(head, &ptr->mode);
1314                    ccs_print_number_union(head, &ptr->major);
1315                    ccs_print_number_union(head, &ptr->minor);
1316            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1317                    struct ccs_path2_acl *ptr =
1318                            container_of(acl, typeof(*ptr), head);
1319                    bit = ccs_fns(ptr->perm, bit);
1320                    if (bit >= CCS_MAX_PATH2_OPERATION)
1321                            goto done;
1322                    ccs_set_group(head);
1323                    ccs_set_string(head, "file ");
1324                    ccs_set_string(head, ccs_path2_keyword[bit]);
1325                    ccs_print_name_union(head, &ptr->name1);
1326                    ccs_print_name_union(head, &ptr->name2);
1327            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1328                    struct ccs_path_number_acl *ptr =
1329                            container_of(acl, typeof(*ptr), head);
1330                    bit = ccs_fns(ptr->perm, bit);
1331                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1332                            goto done;
1333                    ccs_set_group(head);
1334                    ccs_set_string(head, "file ");
1335                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1336                    ccs_print_name_union(head, &ptr->name);
1337                    ccs_print_number_union(head, &ptr->number);
1338            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1339                    struct ccs_env_acl *ptr =
1340                            container_of(acl, typeof(*ptr), head);
1341                    ccs_set_group(head);
1342                    ccs_set_string(head, "misc env ");
1343                    ccs_set_string(head, ptr->env->name);
1344            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1345                    struct ccs_capability_acl *ptr =
1346                            container_of(acl, typeof(*ptr), head);
1347                    ccs_set_group(head);
1348                    ccs_set_string(head, "capability ");
1349                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1350            } else if (acl_type == CCS_TYPE_INET_ACL) {
1351                    struct ccs_inet_acl *ptr =
1352                            container_of(acl, typeof(*ptr), head);
1353                    bit = ccs_fns(ptr->perm, bit);
1354                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1355                            goto done;
1356                    ccs_set_group(head);
1357                    ccs_set_string(head, "network inet ");
1358                    ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1359                    ccs_set_space(head);
1360                    ccs_set_string(head, ccs_socket_keyword[bit]);
1361                    ccs_set_space(head);
1362                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1363                            char buf[128];
1364                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1365                          w[0] = "@";                          ccs_set_string(head, "@");
1366                          w[1] = ptr->address.group->group_name->name;                          ccs_set_string(head,
1367                                           ptr->address.group->group_name->name);
1368                          break;                          break;
1369                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1370                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1371                                         ptr->address.ipv4.max);                                         ptr->address.ipv4.max);
1372                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1373                          break;                          break;
1374                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1375                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1376                                         ptr->address.ipv6.max);                                         ptr->address.ipv6.max);
1377                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1378                          break;                          break;
1379                  }                  }
1380                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",                  ccs_print_number_union(head, &ptr->port);
1381                                     ccs_net2keyword(bit), w[0], w[1]) ||          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1382                      !ccs_print_number_union(head, &ptr->port) ||                  struct ccs_unix_acl *ptr =
1383                      !ccs_print_condition(head, ptr->head.cond))                          container_of(acl, typeof(*ptr), head);
1384                          goto out;                  bit = ccs_fns(ptr->perm, bit);
1385          }                  if (bit >= CCS_MAX_NETWORK_OPERATION)
1386          head->read_bit = 0;                          goto done;
1387          return true;                  ccs_set_group(head);
1388   out:                  ccs_set_string(head, "network unix ");
1389          head->read_bit = bit;                  ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1390          head->read_avail = pos;                  ccs_set_space(head);
1391          return false;                  ccs_set_string(head, ccs_socket_keyword[bit]);
1392  }                  ccs_print_name_union(head, &ptr->name);
1393            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1394  /**                  struct ccs_signal_acl *ptr =
1395   * ccs_print_signal_acl - Print a signal ACL entry.                          container_of(acl, typeof(*ptr), head);
1396   *                  ccs_set_group(head);
1397   * @head: Pointer to "struct ccs_io_buffer".                  ccs_set_string(head, "ipc signal ");
1398   * @ptr:  Pointer to "struct signale_acl".                  ccs_io_printf(head, "%u ", ptr->sig);
1399   *                  ccs_set_string(head, ptr->domainname->name);
1400   * Returns true on success, false otherwise.          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1401   */                  struct ccs_mount_acl *ptr =
1402  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,                          container_of(acl, typeof(*ptr), head);
1403                                   struct ccs_signal_acl *ptr)                  ccs_set_group(head);
1404  {                  ccs_io_printf(head, "file mount");
1405          const int pos = head->read_avail;                  ccs_print_name_union(head, &ptr->dev_name);
1406          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",                  ccs_print_name_union(head, &ptr->dir_name);
1407                             ptr->sig, ptr->domainname->name) ||                  ccs_print_name_union(head, &ptr->fs_type);
1408              !ccs_print_condition(head, ptr->head.cond)) {                  ccs_print_number_union(head, &ptr->flags);
1409                  head->read_avail = pos;          }
1410                  return false;          head->r.bit = bit + 1;
1411          }          if (acl->cond) {
1412          return true;                  head->r.print_cond_part = true;
1413  }                  head->r.cond_step = 0;
1414                    if (!ccs_flush(head))
1415  /**                          return false;
1416   * ccs_print_execute_handler - Print an execute handler ACL entry.   print_cond_part:
1417   *                  if (!ccs_print_condition(head, acl->cond))
1418   * @head:    Pointer to "struct ccs_io_buffer".                          return false;
1419   * @keyword: Name of the keyword.                  head->r.print_cond_part = false;
1420   * @ptr:     Pointer to "struct ccs_execute_handler".          } else {
1421   *                  ccs_set_lf(head);
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_execute_handler(struct ccs_io_buffer *head,  
                                       const char *keyword,  
                                       struct ccs_execute_handler *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, "%s %s", keyword, ptr->handler->name) ||  
             !ccs_print_condition(head, ptr->head.cond)) {  
                 head->read_avail = pos;  
                 return false;  
1422          }          }
1423          return true;          switch (acl_type) {
1424  }          case CCS_TYPE_PATH_ACL:
1425            case CCS_TYPE_MKDEV_ACL:
1426  /**          case CCS_TYPE_PATH2_ACL:
1427   * ccs_print_mount_acl - Print a mount ACL entry.          case CCS_TYPE_PATH_NUMBER_ACL:
1428   *          case CCS_TYPE_INET_ACL:
1429   * @head: Pointer to "struct ccs_io_buffer".          case CCS_TYPE_UNIX_ACL:
1430   * @ptr:  Pointer to "struct ccs_mount_acl".                  goto next;
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mount_acl *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||  
             !ccs_print_name_union(head, &ptr->dev_name) ||  
             !ccs_print_name_union(head, &ptr->dir_name) ||  
             !ccs_print_name_union(head, &ptr->fs_type) ||  
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, ptr->head.cond)) {  
                 head->read_avail = pos;  
                 return false;  
1431          }          }
1432     done:
1433            head->r.bit = 0;
1434          return true;          return true;
1435  }  }
1436    
1437  /**  /**
  * ccs_print_entry - Print an ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to an ACL entry.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_entry(struct ccs_io_buffer *head,  
                             struct ccs_acl_info *ptr)  
 {  
         const u8 acl_type = ptr->type;  
         if (ptr->is_deleted)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_ACL) {  
                 struct ccs_path_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_path_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||  
             acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?  
                         CCS_KEYWORD_EXECUTE_HANDLER :  
                         CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_path_number3_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_path2_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_path_number_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_env_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_capability_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_network_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_signal_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_mount_acl(head, acl);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
1438   * ccs_read_domain2 - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1439   *   *
1440   * @head:   Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1441   * @domain: Pointer to "struct ccs_domain_info".   * @domain: Pointer to "struct ccs_domain_info".
1442     * @index:  Index number.
1443   *   *
1444   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1445   *   *
1446   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1447   */   */
1448  static bool ccs_read_domain2(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1449                               struct ccs_domain_info *domain)                               struct ccs_domain_info *domain,
1450                                 const u8 index)
1451  {  {
1452          struct list_head *pos;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1453          /* Print ACL entries in the domain. */                  struct ccs_acl_info *ptr =
1454          list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {                          list_entry(head->r.acl, typeof(*ptr), list);
                 struct ccs_acl_info *ptr  
                         = list_entry(pos, struct ccs_acl_info, list);  
1455                  if (!ccs_print_entry(head, ptr))                  if (!ccs_print_entry(head, ptr))
1456                          return false;                          return false;
1457          }          }
1458            head->r.acl = NULL;
1459          return true;          return true;
1460  }  }
1461    
# Line 1639  static bool ccs_read_domain2(struct ccs_ Line 1468  static bool ccs_read_domain2(struct ccs_
1468   */   */
1469  static void ccs_read_domain(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1470  {  {
1471          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1472                  return;                  return;
1473          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1474                  struct ccs_domain_info *domain =                  struct ccs_domain_info *domain =
1475                          list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1476                  switch (head->read_step) {                  switch (head->r.step) {
1477                          u8 i;                          u8 i;
                         const char *w[CCS_MAX_DOMAIN_INFO_FLAGS];  
1478                  case 0:                  case 0:
1479                          if (domain->is_deleted && !head->read_single_domain)                          if (domain->is_deleted &&
1480                                !head->r.print_this_domain_only)
1481                                  continue;                                  continue;
1482                          /* Print domainname and flags. */                          /* Print domainname and flags. */
1483                            ccs_set_string(head, domain->domainname->name);
1484                            ccs_set_lf(head);
1485                            ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1486                                          domain->profile);
1487                            ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1488                                          domain->group);
1489                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1490                                  w[i] = domain->flags[i] ? ccs_dif[i] : "";                                  if (domain->flags[i])
1491                          if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE                                          ccs_set_string(head, ccs_dif[i]);
1492                                             "%u\n%s%s%s%s%s\n",                          head->r.step++;
1493                                             domain->domainname->name,                          ccs_set_lf(head);
                                            domain->profile,  
                                            w[CCS_DIF_QUOTA_WARNED],  
                                            w[CCS_DIF_IGNORE_GLOBAL],  
                                            w[CCS_DIF_IGNORE_GLOBAL_ALLOW_READ],  
                                            w[CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV],  
                                            w[CCS_DIF_TRANSITION_FAILED]))  
                                 return;  
                         head->read_step++;  
1494                          /* fall through */                          /* fall through */
1495                  case 1:                  case 1:
1496                          if (!ccs_read_domain2(head, domain))                          if (!ccs_read_domain2(head, domain, 0))
1497                                  return;                                  return;
1498                          head->read_step++;                          head->r.step++;
1499                          /* fall through */                          /* fall through */
1500                  case 2:                  case 2:
1501                          if (!ccs_io_printf(head, "\n"))                          if (!ccs_read_domain2(head, domain, 1))
1502                                    return;
1503                            head->r.step++;
1504                            if (!ccs_set_lf(head))
1505                                  return;                                  return;
1506                          head->read_step = 0;                          /* fall through */
1507                          if (head->read_single_domain)                  case 3:
1508                                  goto out;                          head->r.step = 0;
1509                            if (head->r.print_this_domain_only)
1510                                    goto done;
1511                  }                  }
1512          }          }
1513   out:   done:
1514          head->read_eof = true;          head->r.eof = true;
1515  }  }
1516    
1517  /**  /**
# Line 1731  static int ccs_write_domain_profile(stru Line 1562  static int ccs_write_domain_profile(stru
1562   */   */
1563  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1564  {  {
1565          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1566                  return;                  return;
1567          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1568                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1569                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1570                  if (domain->is_deleted)                  if (domain->is_deleted)
1571                          continue;                          continue;
1572                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1573                          return;                          return;
1574                    ccs_io_printf(head, "%u ", domain->profile);
1575                    ccs_set_string(head, domain->domainname->name);
1576                    ccs_set_lf(head);
1577          }          }
1578          head->read_eof = true;          head->r.eof = true;
1579  }  }
1580    
1581  /**  /**
# Line 1755  static void ccs_read_domain_profile(stru Line 1587  static void ccs_read_domain_profile(stru
1587   */   */
1588  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1589  {  {
1590          head->read_eof = false;          head->r.eof = false;
1591          return 0;          return 0;
1592  }  }
1593    
# Line 1781  static void ccs_read_pid(struct ccs_io_b Line 1613  static void ccs_read_pid(struct ccs_io_b
1613          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1614          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1615          if (!buf) {          if (!buf) {
1616                  head->read_eof = true;                  head->r.eof = true;
1617                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1618          }          }
1619          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1620                  return;                  return;
1621          head->read_eof = true;          head->r.eof = true;
1622          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1623                  task_info = true;                  task_info = true;
1624          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1808  static void ccs_read_pid(struct ccs_io_b Line 1640  static void ccs_read_pid(struct ccs_io_b
1640          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1641          if (!domain)          if (!domain)
1642                  return;                  return;
1643          if (!task_info)          if (!task_info) {
1644                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1645                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1646          else          } else {
1647                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
                               "state[0]=%u state[1]=%u state[2]=%u", pid,  
1648                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1649                                          CCS_TASK_IS_MANAGER),                                          CCS_TASK_IS_MANAGER),
1650                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1651                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER));
1652                                (u8) (ccs_flags >> 24),          }
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
1653  }  }
1654    
1655    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1656            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1657            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1658            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1659            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1660            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1661    };
1662    
1663    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1664            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1665            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1666            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1667    };
1668    
1669  /**  /**
1670   * ccs_write_exception - Write exception policy.   * ccs_write_exception - Write exception policy.
1671   *   *
# Line 1837  static int ccs_write_exception(struct cc Line 1680  static int ccs_write_exception(struct cc
1680          u8 i;          u8 i;
1681          static const struct {          static const struct {
1682                  const char *keyword;                  const char *keyword;
1683                  int (*write) (char *, const bool, const u8);                  int (*write) (char *, const bool);
1684          } ccs_callback[8] = {          } ccs_callback[3] = {
                 { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper },  
                 { CCS_KEYWORD_NO_INITIALIZE_DOMAIN,  
                   ccs_write_domain_initializer },  
                 { CCS_KEYWORD_KEEP_DOMAIN, ccs_write_domain_keeper },  
                 { CCS_KEYWORD_INITIALIZE_DOMAIN,  
                   ccs_write_domain_initializer },  
1685                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1686                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
                 { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },  
1687                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1688          };          };
1689          static const char *ccs_name[CCS_MAX_GROUP] = {          for (i = 0; i < 3; i++)
                 [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,  
                 [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,  
                 [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP  
         };  
         for (i = 0; i < 8; i++) {  
1690                  if (ccs_str_starts(&data, ccs_callback[i].keyword))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1691                          return ccs_callback[i].write(data, is_delete, i < 2);                          return ccs_callback[i].write(data, is_delete);
1692          }          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1693          for (i = 0; i < CCS_MAX_GROUP; i++) {                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1694                  if (ccs_str_starts(&data, ccs_name[i]))                          return ccs_write_transition_control(data, is_delete,
1695                                                                i);
1696            for (i = 0; i < CCS_MAX_GROUP; i++)
1697                    if (ccs_str_starts(&data, ccs_group_name[i]))
1698                          return ccs_write_group(data, is_delete, i);                          return ccs_write_group(data, is_delete, i);
1699            if (ccs_str_starts(&data, "acl_group ")) {
1700                    unsigned int group;
1701                    if (sscanf(data, "%u", &group) == 1 &&
1702                        group < CCS_MAX_ACL_GROUPS) {
1703                            data = strchr(data, ' ');
1704                            if (data)
1705                                    return ccs_write_domain2(data + 1,
1706                                                             &ccs_acl_group[group],
1707                                                             is_delete);
1708                    }
1709          }          }
1710          return ccs_write_domain2(data, &ccs_global_domain, is_delete);          return -EINVAL;
1711  }  }
1712    
1713  /**  /**
# Line 1878  static int ccs_write_exception(struct cc Line 1722  static int ccs_write_exception(struct cc
1722   */   */
1723  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1724  {  {
1725          struct list_head *gpos;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
         struct list_head *mpos;  
         const char *w[3] = { "", "", "" };  
         if (idx == CCS_PATH_GROUP)  
                 w[0] = CCS_KEYWORD_PATH_GROUP;  
         else if (idx == CCS_NUMBER_GROUP)  
                 w[0] = CCS_KEYWORD_NUMBER_GROUP;  
         else if (idx == CCS_ADDRESS_GROUP)  
                 w[0] = CCS_KEYWORD_ADDRESS_GROUP;  
         list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {  
1726                  struct ccs_group *group =                  struct ccs_group *group =
1727                          list_entry(gpos, struct ccs_group, head.list);                          list_entry(head->r.group, typeof(*group), head.list);
1728                  w[1] = group->group_name->name;                  list_for_each_cookie(head->r.acl, &group->member_list) {
                 list_for_each_cookie(mpos, head->read_var2,  
                                      &group->member_list) {  
                         char buffer[128];  
1729                          struct ccs_acl_head *ptr =                          struct ccs_acl_head *ptr =
1730                                  list_entry(mpos, struct ccs_acl_head, list);                                  list_entry(head->r.acl, typeof(*ptr), list);
1731                          if (ptr->is_deleted)                          if (ptr->is_deleted)
1732                                  continue;                                  continue;
1733                            if (!ccs_flush(head))
1734                                    return false;
1735                            ccs_set_string(head, ccs_group_name[idx]);
1736                            ccs_set_string(head, group->group_name->name);
1737                          if (idx == CCS_PATH_GROUP) {                          if (idx == CCS_PATH_GROUP) {
1738                                  w[2] = container_of(ptr, struct ccs_path_group,                                  ccs_set_space(head);
1739                                                      head)->member_name->name;                                  ccs_set_string(head, container_of
1740                                                   (ptr, struct ccs_path_group,
1741                                                    head)->member_name->name);
1742                          } else if (idx == CCS_NUMBER_GROUP) {                          } else if (idx == CCS_NUMBER_GROUP) {
1743                                  w[2] = buffer;                                  ccs_print_number_union(head, &container_of
1744                                  ccs_print_number(buffer, sizeof(buffer),                                                         (ptr, struct ccs_number_group,
1745                                                   &container_of                                                          head)->number);
                                                  (ptr, struct ccs_number_group,  
                                                   head)->number);  
1746                          } else if (idx == CCS_ADDRESS_GROUP) {                          } else if (idx == CCS_ADDRESS_GROUP) {
1747                                    char buffer[128];
1748                                  struct ccs_address_group *member =                                  struct ccs_address_group *member =
1749                                          container_of(ptr, typeof(*member),                                          container_of(ptr, typeof(*member),
1750                                                       head);                                                       head);
                                 w[2] = buffer;  
1751                                  if (member->is_ipv6)                                  if (member->is_ipv6)
1752                                          ccs_print_ipv6(buffer, sizeof(buffer),                                          ccs_print_ipv6(buffer, sizeof(buffer),
1753                                                         member->min.ipv6,                                                         member->min.ipv6,
# Line 1920  static bool ccs_read_group(struct ccs_io Line 1756  static bool ccs_read_group(struct ccs_io
1756                                          ccs_print_ipv4(buffer, sizeof(buffer),                                          ccs_print_ipv4(buffer, sizeof(buffer),
1757                                                         member->min.ipv4,                                                         member->min.ipv4,
1758                                                         member->max.ipv4);                                                         member->max.ipv4);
1759                                    ccs_io_printf(head, " %s", buffer);
1760                          }                          }
1761                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],                          ccs_set_lf(head);
                                            w[2]))  
                                 return false;  
1762                  }                  }
1763                    head->r.acl = NULL;
1764          }          }
1765            head->r.group = NULL;
1766          return true;          return true;
1767  }  }
1768    
# Line 1941  static bool ccs_read_group(struct ccs_io Line 1778  static bool ccs_read_group(struct ccs_io
1778   */   */
1779  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1780  {  {
1781          struct list_head *pos;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1782          list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {                  struct ccs_acl_head *acl =
1783                  const char *w[4] = { "", "", "", "" };                          container_of(head->r.acl, typeof(*acl), list);
                 char buffer[16];  
                 struct ccs_acl_head *acl = container_of(pos, typeof(*acl),  
                                                         list);  
1784                  if (acl->is_deleted)                  if (acl->is_deleted)
1785                          continue;                          continue;
1786                    if (!ccs_flush(head))
1787                            return false;
1788                  switch (idx) {                  switch (idx) {
1789                  case CCS_ID_DOMAIN_KEEPER:                  case CCS_ID_TRANSITION_CONTROL:
                         {  
                                 struct ccs_domain_keeper *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 w[0] = ptr->is_not ?  
                                         CCS_KEYWORD_NO_KEEP_DOMAIN :  
                                         CCS_KEYWORD_KEEP_DOMAIN;  
                                 if (ptr->program) {  
                                         w[1] = ptr->program->name;  
                                         w[2] = " from ";  
                                 }  
                                 w[3] = ptr->domainname->name;  
                         }  
                         break;  
                 case CCS_ID_DOMAIN_INITIALIZER:  
1790                          {                          {
1791                                  struct ccs_domain_initializer *ptr =                                  struct ccs_transition_control *ptr =
1792                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1793                                  w[0] = ptr->is_not ?                                  ccs_set_string(head,
1794                                          CCS_KEYWORD_NO_INITIALIZE_DOMAIN :                                                 ccs_transition_type[ptr->type]);
1795                                          CCS_KEYWORD_INITIALIZE_DOMAIN;                                  ccs_set_string(head, ptr->program ?
1796                                  w[1] = ptr->program->name;                                                 ptr->program->name : "any");
1797                                  if (ptr->domainname) {                                  ccs_set_string(head, " from ");
1798                                          w[2] = " from ";                                  ccs_set_string(head, ptr->domainname ?
1799                                          w[3] = ptr->domainname->name;                                                 ptr->domainname->name : "any");
                                 }  
1800                          }                          }
1801                          break;                          break;
1802                  case CCS_ID_AGGREGATOR:                  case CCS_ID_AGGREGATOR:
1803                          {                          {
1804                                  struct ccs_aggregator *ptr =                                  struct ccs_aggregator *ptr =
1805                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1806                                  w[0] = CCS_KEYWORD_AGGREGATOR;                                  ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1807                                  w[1] = ptr->original_name->name;                                  ccs_set_string(head, ptr->original_name->name);
1808                                  w[2] = " ";                                  ccs_set_space(head);
1809                                  w[3] = ptr->aggregated_name->name;                                  ccs_set_string(head,
1810                                                   ptr->aggregated_name->name);
1811                          }                          }
1812                          break;                          break;
1813                  case CCS_ID_PATTERN:                  case CCS_ID_PATTERN:
1814                          {                          {
1815                                  struct ccs_pattern *ptr =                                  struct ccs_pattern *ptr =
1816                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1817                                  w[0] = CCS_KEYWORD_FILE_PATTERN;                                  ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1818                                  w[1] = ptr->pattern->name;                                  ccs_set_string(head, ptr->pattern->name);
                         }  
                         break;  
                 case CCS_ID_NO_REWRITE:  
                         {  
                                 struct ccs_no_rewrite *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 w[0] = CCS_KEYWORD_DENY_REWRITE;  
                                 w[1] = ptr->pattern->name;  
1819                          }                          }
1820                          break;                          break;
1821                  case CCS_ID_RESERVEDPORT:                  case CCS_ID_RESERVEDPORT:
# Line 2010  static bool ccs_read_policy(struct ccs_i Line 1824  static bool ccs_read_policy(struct ccs_i
1824                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1825                                  const u16 min_port = ptr->min_port;                                  const u16 min_port = ptr->min_port;
1826                                  const u16 max_port = ptr->max_port;                                  const u16 max_port = ptr->max_port;
1827                                  w[0] = CCS_KEYWORD_DENY_AUTOBIND;                                  ccs_set_string(head,
1828                                  snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",                                                 CCS_KEYWORD_DENY_AUTOBIND);
1829                                           min_port, min_port != max_port ?                                  ccs_io_printf(head, "%u", min_port);
1830                                           '-' : '\0', max_port);                                  if (min_port != max_port)
1831                                  buffer[sizeof(buffer) - 1] = '\0';                                          ccs_io_printf(head, "-%u", max_port);
                                 w[1] = buffer;  
1832                          }                          }
1833                          break;                          break;
1834                  default:                  default:
1835                          continue;                          continue;
1836                  }                  }
1837                  if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))                  ccs_set_lf(head);
                         return false;  
1838          }          }
1839            head->r.acl = NULL;
1840          return true;          return true;
1841  }  }
1842    
 static void ccs_read_global_domain(struct ccs_io_buffer *head)  
 {  
         if (!head->read_eof)  
                 head->read_eof = ccs_read_domain2(head, &ccs_global_domain);  
 }  
   
1843  /**  /**
1844   * ccs_read_exception - Read exception policy.   * ccs_read_exception - Read exception policy.
1845   *   *
# Line 2042  static void ccs_read_global_domain(struc Line 1849  static void ccs_read_global_domain(struc
1849   */   */
1850  static void ccs_read_exception(struct ccs_io_buffer *head)  static void ccs_read_exception(struct ccs_io_buffer *head)
1851  {  {
1852          if (head->read_eof)          if (head->r.eof)
1853                  return;                  return;
1854          while (head->read_step < CCS_MAX_POLICY &&          while (head->r.step < CCS_MAX_POLICY &&
1855                 ccs_read_policy(head, head->read_step))                 ccs_read_policy(head, head->r.step))
1856                  head->read_step++;                  head->r.step++;
1857          if (head->read_step < CCS_MAX_POLICY)          if (head->r.step < CCS_MAX_POLICY)
1858                  return;                  return;
1859          while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1860                 ccs_read_group(head, head->read_step - CCS_MAX_POLICY))                 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1861                  head->read_step++;                  head->r.step++;
1862          if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)          if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1863                  return;                  return;
1864          head->read = ccs_read_global_domain;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1865  }                 + CCS_MAX_ACL_GROUPS * 2) {
1866                    head->r.group_index = (head->r.step - CCS_MAX_POLICY
1867  /**                                         - CCS_MAX_GROUP) / 2;
1868   * ccs_get_argv0 - Get argv[0].                  if (!ccs_read_domain2(head,
1869   *                                        &ccs_acl_group[head->r.group_index],
1870   * @ee: Pointer to "struct ccs_execve".                                        head->r.step & 1))
1871   *                          return;
1872   * Returns true on success, false otherwise.                  head->r.step++;
  */  
 static bool ccs_get_argv0(struct ccs_execve *ee)  
 {  
         struct linux_binprm *bprm = ee->bprm;  
         char *arg_ptr = ee->tmp;  
         int arg_len = 0;  
         unsigned long pos = bprm->p;  
         int offset = pos % PAGE_SIZE;  
         bool done = false;  
         if (!bprm->argc)  
                 goto out;  
         while (1) {  
                 if (!ccs_dump_page(bprm, pos, &ee->dump))  
                         goto out;  
                 pos += PAGE_SIZE - offset;  
                 /* Read. */  
                 while (offset < PAGE_SIZE) {  
                         const char *kaddr = ee->dump.data;  
                         const unsigned char c = kaddr[offset++];  
                         if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {  
                                 if (c == '\\') {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = '\\';  
                                 } else if (c > ' ' && c < 127) {  
                                         arg_ptr[arg_len++] = c;  
                                 } else {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = (c >> 6) + '0';  
                                         arg_ptr[arg_len++]  
                                                 = ((c >> 3) & 7) + '0';  
                                         arg_ptr[arg_len++] = (c & 7) + '0';  
                                 }  
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
                         }  
                 }  
                 offset = 0;  
                 if (done)  
                         break;  
1873          }          }
1874          return true;          head->r.eof = true;
  out:  
         return false;  
1875  }  }
1876    
1877  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2117  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1881  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1881  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1882    
1883  /* Structure for query. */  /* Structure for query. */
1884  struct ccs_query_entry {  struct ccs_query {
1885          struct list_head list;          struct list_head list;
1886          char *query;          char *query;
1887          int query_len;          int query_len;
# Line 2126  struct ccs_query_entry { Line 1890  struct ccs_query_entry {
1890          int answer;          int answer;
1891  };  };
1892    
1893  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1894  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1895    
1896  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1897  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1898    
1899  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)  static void ccs_truncate(char *str)
1900  {  {
1901          va_list args;          while (* (unsigned char *) str > (unsigned char) ' ')
1902          const int pos = strlen(buffer);                  str++;
1903          va_start(args, fmt);          *str = '\0';
         vsnprintf(buffer + pos, len - pos - 1, fmt, args);  
         va_end(args);  
1904  }  }
1905                            
1906  /**  /**
1907   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1908   *   *
# Line 2159  int ccs_supervisor(struct ccs_request_in Line 1921  int ccs_supervisor(struct ccs_request_in
1921          int pos;          int pos;
1922          int len;          int len;
1923          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1924          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1925          bool quota_exceeded = false;          bool quota_exceeded = false;
1926          char *header;          char *header;
1927          struct ccs_domain_info * const domain = ccs_current_domain();          struct ccs_domain_info * const domain = ccs_current_domain();
# Line 2170  int ccs_supervisor(struct ccs_request_in Line 1932  int ccs_supervisor(struct ccs_request_in
1932                  char *buffer;                  char *buffer;
1933                  char *realpath = NULL;                  char *realpath = NULL;
1934                  char *argv0 = NULL;                  char *argv0 = NULL;
1935                  const char *symlink = NULL;                  char *symlink = NULL;
1936                    char *handler = NULL;
1937                  const struct ccs_preference *pref;                  const struct ccs_preference *pref;
1938                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1939                          return 0;                          return 0;
1940                  pref = ccs_profile(r->profile)->learning;                  header = ccs_init_log(&len, r);
1941                    if (!header)
1942                            return 0;
1943                    pref = &ccs_profile(r->profile)->preference;
1944                    /* strstr() will return NULL if ordering is wrong. */
1945                  if (r->param_type == CCS_TYPE_PATH_ACL &&                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1946                      r->param.path.operation == CCS_TYPE_EXECUTE) {                      r->param.path.operation == CCS_TYPE_EXECUTE) {
                         if (pref->learning_exec_realpath) {  
                                 struct file *file = r->ee->bprm->file;  
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)  
                                 struct path path = { file->f_vfsmnt,  
                                                      file->f_dentry };  
                                 realpath = ccs_realpath_from_path(&path);  
 #else  
                                 realpath = ccs_realpath_from_path(&file->  
                                                                   f_path);  
 #endif  
                                 if (realpath)  
                                         len += strlen(realpath);  
                         }  
1947                          if (pref->learning_exec_argv0) {                          if (pref->learning_exec_argv0) {
1948                                  if (ccs_get_argv0(r->ee)) {                                  argv0 = strstr(header, " argv[]={ \"");
1949                                          argv0 = r->ee->tmp;                                  if (argv0) {
1950                                          len += strlen(argv0);                                          argv0 += 10;
1951                                            ccs_truncate(argv0);
1952                                  }                                  }
1953                          }                          }
1954                  }                          if (pref->learning_exec_realpath) {
1955                  if (r->param_type == CCS_TYPE_PATH_ACL &&                                  realpath = strstr(header,
1956                      r->param.path.operation == CCS_TYPE_SYMLINK &&                                                    " exec={ realpath=\"");
1957                      pref->learning_symlink_target) {                                  if (realpath) {
1958                          symlink = r->obj->symlink_target->name;                                          realpath += 8;
1959                          len += strlen(symlink);                                          ccs_truncate(realpath);
1960                  }                                  }
1961                            }
1962                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1963                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1964                               pref->learning_symlink_target) {
1965                            symlink = strstr(header, " symlink.target=\"");
1966                            if (symlink)
1967                                    ccs_truncate(symlink + 1);
1968                    }
1969                    handler = strstr(header, "type=execute_handler");
1970                    if (handler)
1971                            ccs_truncate(handler);
1972                  buffer = kmalloc(len, CCS_GFP_FLAGS);                  buffer = kmalloc(len, CCS_GFP_FLAGS);
1973                  if (buffer) {                  if (buffer) {
                         const bool handler = (current->ccs_flags &  
                                               CCS_TASK_IS_EXECUTE_HANDLER)  
                                 != 0;  
1974                          va_start(args, fmt);                          va_start(args, fmt);
1975                          vsnprintf(buffer, len - 1, fmt, args);                          vsnprintf(buffer, len - 1, fmt, args);
1976                          va_end(args);                          va_end(args);
1977                          if (handler || realpath || argv0 || symlink) {                          if (handler || realpath || argv0 || symlink) {
                                 ccs_addprintf(buffer, len, " if");  
1978                                  if (handler)                                  if (handler)
1979                                          ccs_addprintf(buffer, len, " task.type"                                          ccs_addprintf(buffer, len, " task.%s",
1980                                                        "=execute_handler");                                                        handler);
1981                                  if (realpath)                                  if (realpath)
1982                                          ccs_addprintf(buffer, len,                                          ccs_addprintf(buffer, len, " exec.%s",
                                                       " exec.realpath=\"%s\"",  
1983                                                        realpath);                                                        realpath);
1984                                  if (argv0)                                  if (argv0)
1985                                          ccs_addprintf(buffer, len,                                          ccs_addprintf(buffer, len,
1986                                                        " exec.argv[0]=\"%s\"",                                                        " exec.argv[0]=%s",
1987                                                        argv0);                                                        argv0);
1988                                  if (symlink)                                  if (symlink)
1989                                          ccs_addprintf(buffer, len,                                          ccs_addprintf(buffer, len, "%s",
                                                       " symlink.target=\"%s\"",  
1990                                                        symlink);                                                        symlink);
1991                          }                          }
1992                          ccs_normalize_line(buffer);                          ccs_normalize_line(buffer);
1993                          ccs_write_domain2(buffer, domain, false);                          ccs_write_domain2(buffer, domain, false);
1994                          kfree(buffer);                          kfree(buffer);
1995                  }                  }
1996                  kfree(realpath);                  kfree(header);
1997                  return 0;                  return 0;
1998          }          }
1999          if (r->mode != CCS_CONFIG_ENFORCING)          if (r->mode != CCS_CONFIG_ENFORCING)
# Line 2242  int ccs_supervisor(struct ccs_request_in Line 2002  int ccs_supervisor(struct ccs_request_in
2002                  int i;                  int i;
2003                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2004                          return -EPERM;                          return -EPERM;
2005                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->preference.
2006                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
2007                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2008                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
# Line 2252  int ccs_supervisor(struct ccs_request_in Line 2012  int ccs_supervisor(struct ccs_request_in
2012          header = ccs_init_log(&len, r);          header = ccs_init_log(&len, r);
2013          if (!header)          if (!header)
2014                  goto out;                  goto out;
2015          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2016          if (!ccs_query_entry)          if (!entry)
2017                  goto out;                  goto out;
2018          len = ccs_round2(len);          len = ccs_round2(len);
2019          ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
2020          if (!ccs_query_entry->query)          if (!entry->query)
2021                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
2022          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2023          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2024              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
2025                  quota_exceeded = true;                  quota_exceeded = true;
2026          } else {          } else {
2027                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
2028                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
2029          }          }
2030          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2031          if (quota_exceeded)          if (quota_exceeded)
2032                  goto out;                  goto out;
2033          snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2034                   ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
2035          kfree(header);          kfree(header);
2036          header = NULL;          header = NULL;
2037          ccs_addprintf(ccs_query_entry->query, len, fmt, args);          va_start(args, fmt);
2038          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2039            entry->query_len = strlen(entry->query) + 1;
2040            va_end(args);
2041          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2042          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2043          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2044          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2045          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2046               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2047               ccs_query_entry->timer++) {               entry->timer++) {
2048                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2049                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2050                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2051                  if (ccs_query_entry->answer)                  if (entry->answer)
2052                          break;                          break;
2053          }          }
2054          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2055          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2056          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2057          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2058          switch (ccs_query_entry->answer) {          switch (entry->answer) {
2059          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2060                  error = CCS_RETRY_REQUEST;                  error = CCS_RETRY_REQUEST;
2061                  r->retry++;                  r->retry++;
# Line 2311  int ccs_supervisor(struct ccs_request_in Line 2072  int ccs_supervisor(struct ccs_request_in
2072                  break;                  break;
2073          }          }
2074   out:   out:
2075          if (ccs_query_entry)          if (entry)
2076                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2077          kfree(ccs_query_entry);          kfree(entry);
2078          kfree(header);          kfree(header);
2079          return error;          return error;
2080  }  }
# Line 2336  static int ccs_poll_query(struct file *f Line 2097  static int ccs_poll_query(struct file *f
2097          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2098                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2099                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2100                          struct ccs_query_entry *ptr =                          struct ccs_query *ptr =
2101                                  list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2102                          if (ptr->answer)                          if (ptr->answer)
2103                                  continue;                                  continue;
2104                          found = true;                          found = true;
# Line 2364  static void ccs_read_query(struct ccs_io Line 2125  static void ccs_read_query(struct ccs_io
2125          int pos = 0;          int pos = 0;
2126          int len = 0;          int len = 0;
2127          char *buf;          char *buf;
2128          if (head->read_avail)          if (head->r.w_pos)
2129                  return;                  return;
2130          if (head->read_buf) {          if (head->read_buf) {
2131                  kfree(head->read_buf);                  kfree(head->read_buf);
2132                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2133          }          }
2134          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2135          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2136                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2137                  if (ptr->answer)                  if (ptr->answer)
2138                          continue;                          continue;
2139                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2140                          continue;                          continue;
2141                  len = ptr->query_len;                  len = ptr->query_len;
2142                  break;                  break;
2143          }          }
2144          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2145          if (!len) {          if (!len) {
2146                  head->read_step = 0;                  head->r.query_index = 0;
2147                  return;                  return;
2148          }          }
2149          buf = kzalloc(len, CCS_GFP_FLAGS);          buf = kzalloc(len, CCS_GFP_FLAGS);
# Line 2393  static void ccs_read_query(struct ccs_io Line 2152  static void ccs_read_query(struct ccs_io
2152          pos = 0;          pos = 0;
2153          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2154          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2155                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2156                  if (ptr->answer)                  if (ptr->answer)
2157                          continue;                          continue;
2158                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2159                          continue;                          continue;
2160                  /*                  /*
2161                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2409  static void ccs_read_query(struct ccs_io Line 2167  static void ccs_read_query(struct ccs_io
2167          }          }
2168          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2169          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2170                  head->read_buf = buf;                  head->read_buf = buf;
2171                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2172                    head->r.query_index++;
2173          } else {          } else {
2174                  kfree(buf);                  kfree(buf);
2175          }          }
# Line 2433  static int ccs_write_answer(struct ccs_i Line 2190  static int ccs_write_answer(struct ccs_i
2190          unsigned int answer;          unsigned int answer;
2191          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2192          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2193                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2194                  ptr->timer = 0;                  ptr->timer = 0;
2195          }          }
2196          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2442  static int ccs_write_answer(struct ccs_i Line 2198  static int ccs_write_answer(struct ccs_i
2198                  return -EINVAL;                  return -EINVAL;
2199          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2200          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2201                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2202                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2203                          continue;                          continue;
2204                  if (!ptr->answer)                  if (!ptr->answer)
# Line 2461  static int ccs_write_answer(struct ccs_i Line 2216  static int ccs_write_answer(struct ccs_i
2216   */   */
2217  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2218  {  {
2219          if (head->read_eof)          if (head->r.eof)
2220                  return;                  return;
2221          ccs_io_printf(head, "1.7.2");          ccs_set_string(head, "1.8.0-pre");
2222          head->read_eof = true;          head->r.eof = true;
2223  }  }
2224    
2225  /**  /**
# Line 2474  static void ccs_read_version(struct ccs_ Line 2229  static void ccs_read_version(struct ccs_
2229   */   */
2230  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2231  {  {
2232          if (head->read_eof)          if (head->r.eof)
2233                  return;                  return;
2234          /*          /*
2235           * ccs_current_domain()->domainname != NULL because every process           * ccs_current_domain()->domainname != NULL because every process
2236           * belongs to a domain and the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2237           */           */
2238          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2239          head->read_eof = true;          head->r.eof = true;
2240  }  }
2241    
2242  /**  /**
# Line 2640  int ccs_poll_control(struct file *file, Line 2395  int ccs_poll_control(struct file *file,
2395  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2396                       const int buffer_len)                       const int buffer_len)
2397  {  {
2398          int len = 0;          int len;
2399          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
         char *cp;  
2400          int idx;          int idx;
2401          if (!head->read)          if (!head->read)
2402                  return -ENOSYS;                  return -ENOSYS;
# Line 2650  int ccs_read_control(struct file *file, Line 2404  int ccs_read_control(struct file *file,
2404                  return -EFAULT;                  return -EFAULT;
2405          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2406                  return -EINTR;                  return -EINTR;
2407            head->read_user_buf = buffer;
2408            head->read_user_buf_avail = buffer_len;
2409          idx = ccs_read_lock();          idx = ccs_read_lock();
2410          while (1) {          if (ccs_flush(head))
2411                  /* Call the policy handler. */                  /* Call the policy handler. */
2412                  head->read(head);                  head->read(head);
2413                  /* Write to buffer. */          ccs_flush(head);
                 len = head->read_avail;  
                 if (len || head->poll || head->read_eof)  
                         break;  
                 len = head->readbuf_size * 2;  
                 cp = kzalloc(len, CCS_GFP_FLAGS);  
                 if (!cp) {  
                         len = -ENOMEM;  
                         goto out;  
                 }  
                 kfree(head->read_buf);  
                 head->read_buf = cp;  
                 head->readbuf_size = len;  
         }  
         if (len > buffer_len)  
                 len = buffer_len;  
         if (!len)  
                 goto out;  
         /* head->read_buf changes by some functions. */  
         cp = head->read_buf;  
         if (copy_to_user(buffer, cp, len)) {  
                 len = -EFAULT;  
                 goto out;  
         }  
         head->read_avail -= len;  
         memmove(cp, cp + len, head->read_avail);  
  out:  
2414          ccs_read_unlock(idx);          ccs_read_unlock(idx);
2415            len = head->read_user_buf - buffer;
2416          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2417          return len;          return len;
2418  }  }
# Line 2720  int ccs_write_control(struct file *file, Line 2451  int ccs_write_control(struct file *file,
2451          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2452          while (avail_len > 0) {          while (avail_len > 0) {
2453                  char c;                  char c;
2454                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->w.avail >= head->writebuf_size - 1) {
2455                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2456                          char *cp = kzalloc(len, CCS_GFP_FLAGS);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2457                          if (!cp) {                          if (!cp) {
2458                                  error = -ENOMEM;                                  error = -ENOMEM;
2459                                  break;                                  break;
2460                          }                          }
2461                          memmove(cp, cp0, head->write_avail);                          memmove(cp, cp0, head->w.avail);
2462                          kfree(cp0);                          kfree(cp0);
2463                          head->write_buf = cp;                          head->write_buf = cp;
2464                          cp0 = cp;                          cp0 = cp;
# Line 2739  int ccs_write_control(struct file *file, Line 2470  int ccs_write_control(struct file *file,
2470                  }                  }
2471                  buffer++;                  buffer++;
2472                  avail_len--;                  avail_len--;
2473                  cp0[head->write_avail++] = c;                  cp0[head->w.avail++] = c;
2474                  if (c != '\n')                  if (c != '\n')
2475                          continue;                          continue;
2476                  cp0[head->write_avail - 1] = '\0';                  cp0[head->w.avail - 1] = '\0';
2477                  head->write_avail = 0;                  head->w.avail = 0;
2478                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2479                  head->write(head);                  head->write(head);
2480          }          }

Legend:
Removed from v.3748  
changed lines
  Added in v.3952

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