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

Subversion リポジトリの参照

Diff of /trunk/1.7.x/ccs-patch/security/ccsecurity/util.c

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

trunk/1.5.x/ccs-patch/fs/ccs_common.c revision 813 by kumaneko, Tue Dec 18 05:35:28 2007 UTC branches/ccs-patch/fs/ccsecurity/util.c revision 2854 by kumaneko, Wed Aug 5 11:57:20 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   * fs/ccs_common.c   * fs/ccsecurity/util.c
3   *   *
4   * Common functions for SAKURA and TOMOYO.   * Copyright (C) 2005-2009  NTT DATA CORPORATION
5   *   *
6   * Copyright (C) 2005-2007  NTT DATA CORPORATION   * Version: 1.7.0-pre   2009/07/03
  *  
  * Version: 1.5.3-pre   2007/12/18  
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.
10   *   *
11   */   */
12    
13    #include <linux/version.h>
14    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
15    #define __KERNEL_SYSCALLS__
16    #endif
17  #include <linux/string.h>  #include <linux/string.h>
18  #include <linux/mm.h>  #include <linux/mm.h>
19  #include <linux/utime.h>  #include <linux/utime.h>
# Line 20  Line 22 
22  #include <linux/slab.h>  #include <linux/slab.h>
23  #include <asm/uaccess.h>  #include <asm/uaccess.h>
24  #include <stdarg.h>  #include <stdarg.h>
25  #include <linux/version.h>  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)  
26  #include <linux/namei.h>  #include <linux/namei.h>
27  #include <linux/mount.h>  #include <linux/mount.h>
28  static const int lookup_flags = LOOKUP_FOLLOW;  static const int ccs_lookup_flags = LOOKUP_FOLLOW;
 #else  
 static const int lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE;  
 #endif  
 #include <linux/realpath.h>  
 #include <linux/ccs_common.h>  
 #include <linux/ccs_proc.h>  
 #include <linux/tomoyo.h>  
   
 #ifdef CONFIG_TOMOYO_MAX_ACCEPT_ENTRY  
 #define MAX_ACCEPT_ENTRY (CONFIG_TOMOYO_MAX_ACCEPT_ENTRY)  
29  #else  #else
30  #define MAX_ACCEPT_ENTRY 2048  static const int ccs_lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE;
31  #endif  #endif
32  #ifdef CONFIG_TOMOYO_MAX_GRANT_LOG  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
33  #define MAX_GRANT_LOG (CONFIG_TOMOYO_MAX_GRANT_LOG)  #include <linux/unistd.h>
 #else  
 #define MAX_GRANT_LOG 1024  
34  #endif  #endif
35  #ifdef CONFIG_TOMOYO_MAX_REJECT_LOG  #include "internal.h"
36  #define MAX_REJECT_LOG (CONFIG_TOMOYO_MAX_REJECT_LOG)  #include <linux/ccsecurity.h>
37  #else  
38  #define MAX_REJECT_LOG 1024  /* To support PID namespace. */
39    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
40    #define find_task_by_pid find_task_by_vpid
41  #endif  #endif
42    
43  /*************************  VARIABLES  *************************/  /* Set default specified by the kernel config. */
44    #define MAX_ACCEPT_ENTRY (CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY)
45    #define MAX_GRANT_LOG    (CONFIG_CCSECURITY_MAX_GRANT_LOG)
46    #define MAX_REJECT_LOG   (CONFIG_CCSECURITY_MAX_REJECT_LOG)
47    
48    DEFINE_MUTEX(ccs_policy_lock);
49    
50  /* /sbin/init started? */  /* Has /sbin/init started? */
51  int sbin_init_started = 0;  bool ccs_policy_loaded;
52    
53  const char *ccs_log_level = KERN_DEBUG;  /* String table for functionality that takes 4 modes. */
54    static const char *ccs_mode_4[4] = {
55            "disabled", "learning", "permissive", "enforcing"
56    };
57    /* String table for functionality that takes 2 modes. */
58    static const char *ccs_mode_2[4] = {
59            "disabled", "enabled", "enabled", "enabled"
60    };
61    
62    /* Table for profile. */
63  static struct {  static struct {
64          const char *keyword;          const char *keyword;
65          unsigned int current_value;          unsigned int current_value;
66          const unsigned int max_value;          const unsigned int max_value;
67  } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {  } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {
68          [CCS_PROFILE_COMMENT]            = { "COMMENT",             0, 0 }, /* Reserved for string. */          [CCS_MAC_FOR_FILE]        = { "MAC_FOR_FILE",        0, 3 },
69          [CCS_TOMOYO_MAC_FOR_FILE]        = { "MAC_FOR_FILE",        0, 3 },          [CCS_MAC_FOR_IOCTL]       = { "MAC_FOR_IOCTL",       0, 3 },
70          [CCS_TOMOYO_MAC_FOR_ARGV0]       = { "MAC_FOR_ARGV0",       0, 3 },          [CCS_MAC_FOR_ARGV0]       = { "MAC_FOR_ARGV0",       0, 3 },
71          [CCS_TOMOYO_MAC_FOR_ENV]         = { "MAC_FOR_ENV",         0, 3 },          [CCS_MAC_FOR_ENV]         = { "MAC_FOR_ENV",         0, 3 },
72          [CCS_TOMOYO_MAC_FOR_NETWORK]     = { "MAC_FOR_NETWORK",     0, 3 },          [CCS_MAC_FOR_NETWORK]     = { "MAC_FOR_NETWORK",     0, 3 },
73          [CCS_TOMOYO_MAC_FOR_SIGNAL]      = { "MAC_FOR_SIGNAL",      0, 3 },          [CCS_MAC_FOR_SIGNAL]      = { "MAC_FOR_SIGNAL",      0, 3 },
74          [CCS_SAKURA_DENY_CONCEAL_MOUNT]  = { "DENY_CONCEAL_MOUNT",  0, 3 },          [CCS_MAC_FOR_NAMESPACE]   = { "MAC_FOR_NAMESPACE",   0, 3 },
75          [CCS_SAKURA_RESTRICT_CHROOT]     = { "RESTRICT_CHROOT",     0, 3 },          [CCS_RESTRICT_AUTOBIND]   = { "RESTRICT_AUTOBIND",   0, 1 },
76          [CCS_SAKURA_RESTRICT_MOUNT]      = { "RESTRICT_MOUNT",      0, 3 },          [CCS_MAX_ACCEPT_ENTRY]
77          [CCS_SAKURA_RESTRICT_UNMOUNT]    = { "RESTRICT_UNMOUNT",    0, 3 },          = { "MAX_ACCEPT_ENTRY",    MAX_ACCEPT_ENTRY, INT_MAX },
78          [CCS_SAKURA_RESTRICT_PIVOT_ROOT] = { "RESTRICT_PIVOT_ROOT", 0, 3 },  #ifdef CONFIG_CCSECURITY_AUDIT
79          [CCS_SAKURA_RESTRICT_AUTOBIND]   = { "RESTRICT_AUTOBIND",   0, 1 },          [CCS_MAX_GRANT_LOG]
80          [CCS_TOMOYO_MAX_ACCEPT_ENTRY]    = { "MAX_ACCEPT_ENTRY",    MAX_ACCEPT_ENTRY, INT_MAX },          = { "MAX_GRANT_LOG",       MAX_GRANT_LOG, INT_MAX },
81          [CCS_TOMOYO_MAX_GRANT_LOG]       = { "MAX_GRANT_LOG",       MAX_GRANT_LOG, INT_MAX },          [CCS_MAX_REJECT_LOG]
82          [CCS_TOMOYO_MAX_REJECT_LOG]      = { "MAX_REJECT_LOG",      MAX_REJECT_LOG, INT_MAX },          = { "MAX_REJECT_LOG",      MAX_REJECT_LOG, INT_MAX },
83          [CCS_TOMOYO_VERBOSE]             = { "TOMOYO_VERBOSE",      1, 1 },  #endif
84          [CCS_ALLOW_ENFORCE_GRACE]        = { "ALLOW_ENFORCE_GRACE", 0, 1 },          [CCS_VERBOSE]             = { "TOMOYO_VERBOSE",      1, 1 },
85          [CCS_SLEEP_PERIOD]               = { "SLEEP_PERIOD",        0, 3000 }, /* in 0.1 second */          [CCS_SLEEP_PERIOD]
86          [CCS_TOMOYO_ALT_EXEC]            = { "ALT_EXEC",            0, 0 }, /* Reserved for string. */          = { "SLEEP_PERIOD",        0, 3000 }, /* in 0.1 second */
87  };  };
88    
89  struct profile {  /* Capability name used by domain policy. */
90          unsigned int value[CCS_MAX_CONTROL_INDEX];  static const char *ccs_capability_control_keyword[CCS_MAX_CAPABILITY_INDEX]
91          const struct path_info *comment;  = {
92          const struct path_info *alt_exec;          [CCS_INET_STREAM_SOCKET_CREATE]  = "inet_tcp_create",
93            [CCS_INET_STREAM_SOCKET_LISTEN]  = "inet_tcp_listen",
94            [CCS_INET_STREAM_SOCKET_CONNECT] = "inet_tcp_connect",
95            [CCS_USE_INET_DGRAM_SOCKET]      = "use_inet_udp",
96            [CCS_USE_INET_RAW_SOCKET]        = "use_inet_ip",
97            [CCS_USE_ROUTE_SOCKET]           = "use_route",
98            [CCS_USE_PACKET_SOCKET]          = "use_packet",
99            [CCS_SYS_MOUNT]                  = "SYS_MOUNT",
100            [CCS_SYS_UMOUNT]                 = "SYS_UMOUNT",
101            [CCS_SYS_REBOOT]                 = "SYS_REBOOT",
102            [CCS_SYS_CHROOT]                 = "SYS_CHROOT",
103            [CCS_SYS_KILL]                   = "SYS_KILL",
104            [CCS_SYS_VHANGUP]                = "SYS_VHANGUP",
105            [CCS_SYS_SETTIME]                = "SYS_TIME",
106            [CCS_SYS_NICE]                   = "SYS_NICE",
107            [CCS_SYS_SETHOSTNAME]            = "SYS_SETHOSTNAME",
108            [CCS_USE_KERNEL_MODULE]          = "use_kernel_module",
109            [CCS_CREATE_FIFO]                = "create_fifo",
110            [CCS_CREATE_BLOCK_DEV]           = "create_block_dev",
111            [CCS_CREATE_CHAR_DEV]            = "create_char_dev",
112            [CCS_CREATE_UNIX_SOCKET]         = "create_unix_socket",
113            [CCS_SYS_LINK]                   = "SYS_LINK",
114            [CCS_SYS_SYMLINK]                = "SYS_SYMLINK",
115            [CCS_SYS_RENAME]                 = "SYS_RENAME",
116            [CCS_SYS_UNLINK]                 = "SYS_UNLINK",
117            [CCS_SYS_CHMOD]                  = "SYS_CHMOD",
118            [CCS_SYS_CHOWN]                  = "SYS_CHOWN",
119            [CCS_SYS_IOCTL]                  = "SYS_IOCTL",
120            [CCS_SYS_KEXEC_LOAD]             = "SYS_KEXEC_LOAD",
121            [CCS_SYS_PIVOT_ROOT]             = "SYS_PIVOT_ROOT",
122            [CCS_SYS_PTRACE]                 = "SYS_PTRACE",
123            [CCS_CONCEAL_MOUNT]              = "conceal_mount",
124  };  };
125    
126  static struct profile *profile_ptr[MAX_PROFILES];  static bool ccs_profile_entry_used[CCS_MAX_CONTROL_INDEX +
127                                       CCS_MAX_CAPABILITY_INDEX + 1];
128    
129    /* Profile table. Memory is allocated as needed. */
130    static struct ccs_profile {
131            unsigned int value[CCS_MAX_CONTROL_INDEX];
132            const struct ccs_path_info *comment;
133            unsigned char capability_value[CCS_MAX_CAPABILITY_INDEX];
134    } *ccs_profile_ptr[MAX_PROFILES];
135    
136    /* Lock for protecting ccs_profile->comment  */
137    static DEFINE_SPINLOCK(ccs_profile_comment_lock);
138    
139    /* Permit policy management by non-root user? */
140    static bool ccs_manage_by_non_root;
141    
142  /*************************  UTILITY FUNCTIONS  *************************/  /* Utility functions. */
143    
144  #ifdef CONFIG_TOMOYO  /**
145  static int __init TOMOYO_Quiet_Setup(char *str)   * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.
146     *
147     * @str: Unused.
148     *
149     * Returns 0.
150     */
151    static int __init ccs_quiet_setup(char *str)
152  {  {
153          ccs_control_array[CCS_TOMOYO_VERBOSE].current_value = 0;          ccs_control_array[CCS_VERBOSE].current_value = 0;
154          return 0;          return 0;
155  }  }
156    
157  __setup("TOMOYO_QUIET", TOMOYO_Quiet_Setup);  __setup("CCS_QUIET", ccs_quiet_setup);
 #endif  
158    
159  /* Am I root? */  /**
160  static int isRoot(void)   * ccs_is_byte_range - Check whether the string isa \ooo style octal value.
161     *
162     * @str: Pointer to the string.
163     *
164     * Returns true if @str is a \ooo style octal value, false otherwise.
165     */
166    static inline bool ccs_is_byte_range(const char *str)
167  {  {
168          return !current->uid && !current->euid;          return *str >= '0' && *str++ <= '3' &&
169                    *str >= '0' && *str++ <= '7' &&
170                    *str >= '0' && *str <= '7';
171  }  }
172    
173  /*  /**
174   * Format string.   * ccs_is_decimal - Check whether the character is a decimal character.
175     *
176     * @c: The character to check.
177     *
178     * Returns true if @c is a decimal character, false otherwise.
179     */
180    static inline bool ccs_is_decimal(const char c)
181    {
182            return c >= '0' && c <= '9';
183    }
184    
185    /**
186     * ccs_is_hexadecimal - Check whether the character is a hexadecimal character.
187     *
188     * @c: The character to check.
189     *
190     * Returns true if @c is a hexadecimal character, false otherwise.
191     */
192    static inline bool ccs_is_hexadecimal(const char c)
193    {
194            return (c >= '0' && c <= '9') ||
195                    (c >= 'A' && c <= 'F') ||
196                    (c >= 'a' && c <= 'f');
197    }
198    
199    /**
200     * ccs_is_alphabet_char - Check whether the character is an alphabet.
201     *
202     * @c: The character to check.
203     *
204     * Returns true if @c is an alphabet character, false otherwise.
205     */
206    static inline bool ccs_is_alphabet_char(const char c)
207    {
208            return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
209    }
210    
211    /**
212     * ccs_make_byte - Make byte value from three octal characters.
213     *
214     * @c1: The first character.
215     * @c2: The second character.
216     * @c3: The third character.
217     *
218     * Returns byte value.
219     */
220    static inline u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3)
221    {
222            return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
223    }
224    
225    /**
226     * ccs_str_starts - Check whether the given string starts with the given keyword.
227     *
228     * @src:  Pointer to pointer to the string.
229     * @find: Pointer to the keyword.
230     *
231     * Returns true if @src starts with @find, false otherwise.
232     *
233     * The @src is updated to point the first character after the @find
234     * if @src starts with @find.
235     */
236    static bool ccs_str_starts(char **src, const char *find)
237    {
238            const int len = strlen(find);
239            char *tmp = *src;
240            if (strncmp(tmp, find, len))
241                    return false;
242            tmp += len;
243            *src = tmp;
244            return true;
245    }
246    
247    /**
248     * ccs_normalize_line - Format string.
249     *
250     * @buffer: The line to normalize.
251     *
252   * Leading and trailing whitespaces are removed.   * Leading and trailing whitespaces are removed.
253   * Multiple whitespaces are packed into single space.   * Multiple whitespaces are packed into single space.
254     *
255     * Returns nothing.
256   */   */
257  static void NormalizeLine(unsigned char *buffer)  void ccs_normalize_line(unsigned char *buffer)
258  {  {
259          unsigned char *sp = buffer, *dp = buffer;          unsigned char *sp = buffer;
260          int first = 1;          unsigned char *dp = buffer;
261          while (*sp && (*sp <= ' ' || *sp >= 127)) sp++;          bool first = true;
262            while (*sp && (*sp <= ' ' || *sp >= 127))
263                    sp++;
264          while (*sp) {          while (*sp) {
265                  if (!first) *dp++ = ' ';                  if (!first)
266                  first = 0;                          *dp++ = ' ';
267                  while (*sp > ' ' && *sp < 127) *dp++ = *sp++;                  first = false;
268                  while (*sp && (*sp <= ' ' || *sp >= 127)) sp++;                  while (*sp > ' ' && *sp < 127)
269                            *dp++ = *sp++;
270                    while (*sp && (*sp <= ' ' || *sp >= 127))
271                            sp++;
272          }          }
273          *dp = '\0';          *dp = '\0';
274  }  }
275    
276  /*  /**
277   *  Check whether the given filename follows the naming rules.   * ccs_tokenize - Tokenize string.
278   *  Returns nonzero if follows, zero otherwise.   *
279     * @buffer: The line to tokenize.
280     * @w:      Pointer to "char *".
281     * @size:   Sizeof @w .
282     *
283     * Returns true on success, false otherwise.
284     */
285    bool ccs_tokenize(char *buffer, char *w[], size_t size)
286    {
287            int count = size / sizeof(char *);
288            int i;
289            for (i = 0; i < count; i++)
290                    w[i] = "";
291            for (i = 0; i < count; i++) {
292                    char *cp = strchr(buffer, ' ');
293                    if (cp)
294                            *cp = '\0';
295                    w[i] = buffer;
296                    if (!cp)
297                            break;
298                    buffer = cp + 1;
299            }
300            return i < count || !*buffer;
301    }
302    
303    /**
304     * ccs_is_correct_path - Validate a pathname.
305     * @filename:     The pathname to check.
306     * @start_type:   Should the pathname start with '/'?
307     *                1 = must / -1 = must not / 0 = don't care
308     * @pattern_type: Can the pathname contain a wildcard?
309     *                1 = must / -1 = must not / 0 = don't care
310     * @end_type:     Should the pathname end with '/'?
311     *                1 = must / -1 = must not / 0 = don't care
312     *
313     * Check whether the given filename follows the naming rules.
314     * Returns true if @filename follows the naming rules, false otherwise.
315   */   */
316  bool IsCorrectPath(const char *filename, const int start_type, const int pattern_type, const int end_type, const char *function)  bool ccs_is_correct_path(const char *filename, const s8 start_type,
317                             const s8 pattern_type, const s8 end_type)
318  {  {
319          int contains_pattern = 0;          bool contains_pattern = false;
320          char c, d, e;          unsigned char c;
321            unsigned char d;
322            unsigned char e;
323          const char *original_filename = filename;          const char *original_filename = filename;
324          if (!filename) goto out;          if (!filename)
325                    goto out;
326          c = *filename;          c = *filename;
327          if (start_type == 1) { /* Must start with '/' */          if (start_type == 1) { /* Must start with '/' */
328                  if (c != '/') goto out;                  if (c != '/')
329                            goto out;
330          } else if (start_type == -1) { /* Must not start with '/' */          } else if (start_type == -1) { /* Must not start with '/' */
331                  if (c == '/') goto out;                  if (c == '/')
332                            goto out;
333          }          }
334          if (c) c = * (strchr(filename, '\0') - 1);          if (c)
335                    c = *(filename + strlen(filename) - 1);
336          if (end_type == 1) { /* Must end with '/' */          if (end_type == 1) { /* Must end with '/' */
337                  if (c != '/') goto out;                  if (c != '/')
338                            goto out;
339          } else if (end_type == -1) { /* Must not end with '/' */          } else if (end_type == -1) { /* Must not end with '/' */
340                  if (c == '/') goto out;                  if (c == '/')
341                            goto out;
342          }          }
343          while ((c = *filename++) != '\0') {          while (1) {
344                    c = *filename++;
345                    if (!c)
346                            break;
347                  if (c == '\\') {                  if (c == '\\') {
348                          switch ((c = *filename++)) {                          c = *filename++;
349                            switch (c) {
350                          case '\\':  /* "\\" */                          case '\\':  /* "\\" */
351                                  continue;                                  continue;
352                          case '$':   /* "\$" */                          case '$':   /* "\$" */
# Line 164  bool IsCorrectPath(const char *filename, Line 359  bool IsCorrectPath(const char *filename,
359                          case 'a':   /* "\a" */                          case 'a':   /* "\a" */
360                          case 'A':   /* "\A" */                          case 'A':   /* "\A" */
361                          case '-':   /* "\-" */                          case '-':   /* "\-" */
362                                  if (pattern_type == -1) break; /* Must not contain pattern */                                  if (pattern_type == -1)
363                                  contains_pattern = 1;                                          break; /* Must not contain pattern */
364                                    contains_pattern = true;
365                                  continue;                                  continue;
366                          case '0':   /* "\ooo" */                          case '0':   /* "\ooo" */
367                          case '1':                          case '1':
368                          case '2':                          case '2':
369                          case '3':                          case '3':
370                                  if ((d = *filename++) >= '0' && d <= '7' && (e = *filename++) >= '0' && e <= '7') {                                  d = *filename++;
371                                          const unsigned char f =                                  if (d < '0' || d > '7')
372                                                  (((unsigned char) (c - '0')) << 6) +                                          break;
373                                                  (((unsigned char) (d - '0')) << 3) +                                  e = *filename++;
374                                                  (((unsigned char) (e - '0')));                                  if (e < '0' || e > '7')
375                                          if (f && (f <= ' ' || f >= 127)) continue; /* pattern is not \000 */                                          break;
376                                  }                                  c = ccs_make_byte(c, d, e);
377                                    if (c && (c <= ' ' || c >= 127))
378                                            continue; /* pattern is not \000 */
379                          }                          }
380                          goto out;                          goto out;
381                  } else if (c <= ' ' || c >= 127) {                  } else if (c <= ' ' || c >= 127) {
# Line 185  bool IsCorrectPath(const char *filename, Line 383  bool IsCorrectPath(const char *filename,
383                  }                  }
384          }          }
385          if (pattern_type == 1) { /* Must contain pattern */          if (pattern_type == 1) { /* Must contain pattern */
386                  if (!contains_pattern) goto out;                  if (!contains_pattern)
387                            goto out;
388          }          }
389          return 1;          return true;
390   out:   out:
391          printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function, original_filename);          printk(KERN_DEBUG "Invalid pathname '%s'\n", original_filename);
392          return 0;          return false;
393  }  }
394    
395  /*  /**
396   *  Check whether the given domainname follows the naming rules.   * ccs_is_correct_domain - Check whether the given domainname follows the naming rules.
397   *  Returns nonzero if follows, zero otherwise.   * @domainname:   The domainname to check.
398     *
399     * Returns true if @domainname follows the naming rules, false otherwise.
400   */   */
401  bool IsCorrectDomain(const unsigned char *domainname, const char *function)  bool ccs_is_correct_domain(const unsigned char *domainname)
402  {  {
403          unsigned char c, d, e;          unsigned char c;
404            unsigned char d;
405            unsigned char e;
406          const char *org_domainname = domainname;          const char *org_domainname = domainname;
407          if (!domainname || strncmp(domainname, ROOT_NAME, ROOT_NAME_LEN)) goto out;          if (!domainname || strncmp(domainname, ROOT_NAME, ROOT_NAME_LEN))
408                    goto out;
409          domainname += ROOT_NAME_LEN;          domainname += ROOT_NAME_LEN;
410          if (!*domainname) return 1;          if (!*domainname)
411                    return true;
412          do {          do {
413                  if (*domainname++ != ' ') goto out;                  if (*domainname++ != ' ')
414                  if (*domainname++ != '/') goto out;                          goto out;
415                  while ((c = *domainname) != '\0' && c != ' ') {                  if (*domainname++ != '/')
416                            goto out;
417                    while (1) {
418                            c = *domainname;
419                            if (!c || c == ' ')
420                                    break;
421                          domainname++;                          domainname++;
422                          if (c == '\\') {                          if (c == '\\') {
423                                  switch ((c = *domainname++)) {                                  c = *domainname++;
424                                    switch ((c)) {
425                                  case '\\':  /* "\\" */                                  case '\\':  /* "\\" */
426                                          continue;                                          continue;
427                                  case '0':   /* "\ooo" */                                  case '0':   /* "\ooo" */
428                                  case '1':                                  case '1':
429                                  case '2':                                  case '2':
430                                  case '3':                                  case '3':
431                                          if ((d = *domainname++) >= '0' && d <= '7' && (e = *domainname++) >= '0' && e <= '7') {                                          d = *domainname++;
432                                                  const unsigned char f =                                          if (d < '0' || d > '7')
433                                                          (((unsigned char) (c - '0')) << 6) +                                                  break;
434                                                          (((unsigned char) (d - '0')) << 3) +                                          e = *domainname++;
435                                                          (((unsigned char) (e - '0')));                                          if (e < '0' || e > '7')
436                                                  if (f && (f <= ' ' || f >= 127)) continue; /* pattern is not \000 */                                                  break;
437                                          }                                          c = ccs_make_byte(c, d, e);
438                                            if (c && (c <= ' ' || c >= 127))
439                                                    /* pattern is not \000 */
440                                                    continue;
441                                  }                                  }
442                                  goto out;                                  goto out;
443                          } else if (c < ' ' || c >= 127) {                          } else if (c < ' ' || c >= 127) {
# Line 231  bool IsCorrectDomain(const unsigned char Line 445  bool IsCorrectDomain(const unsigned char
445                          }                          }
446                  }                  }
447          } while (*domainname);          } while (*domainname);
448          return 1;          return true;
449   out:   out:
450          printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function, org_domainname);          printk(KERN_DEBUG "Invalid domainname '%s'\n", org_domainname);
451          return 0;          return false;
452  }  }
453    
454  bool IsDomainDef(const unsigned char *buffer)  /**
455     * ccs_is_domain_def - Check whether the given token can be a domainname.
456     *
457     * @buffer: The token to check.
458     *
459     * Returns true if @buffer possibly be a domainname, false otherwise.
460     */
461    bool ccs_is_domain_def(const unsigned char *buffer)
462  {  {
463          /* while (*buffer && (*buffer <= ' ' || *buffer >= 127)) buffer++; */          return !strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN);
         return strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN) == 0;  
464  }  }
465    
466  struct domain_info *FindDomain(const char *domainname0)  /**
467     * ccs_find_domain - Find a domain by the given name.
468     *
469     * @domainname: The domainname to find.
470     *
471     * Returns pointer to "struct ccs_domain_info" if found, NULL otherwise.
472     *
473     * Caller holds ccs_read_lock().
474     */
475    struct ccs_domain_info *ccs_find_domain(const char *domainname)
476  {  {
477          struct domain_info *domain;          struct ccs_domain_info *domain;
478          struct path_info domainname;          struct ccs_path_info name;
479          domainname.name = domainname0;          ccs_check_read_lock();
480          fill_path_info(&domainname);          name.name = domainname;
481          list1_for_each_entry(domain, &domain_list, list) {          ccs_fill_path_info(&name);
482                  if (!domain->is_deleted && !pathcmp(&domainname, domain->domainname)) return domain;          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
483                    if (!domain->is_deleted &&
484                        !ccs_pathcmp(&name, domain->domainname))
485                            return domain;
486          }          }
487          return NULL;          return NULL;
488  }  }
489    
490  static int PathDepth(const char *pathname)  /**
491     * ccs_path_depth - Evaluate the number of '/' in a string.
492     *
493     * @pathname: The string to evaluate.
494     *
495     * Returns path depth of the string.
496     *
497     * I score 2 for each of the '/' in the @pathname
498     * and score 1 if the @pathname ends with '/'.
499     */
500    static int ccs_path_depth(const char *pathname)
501  {  {
502          int i = 0;          int i = 0;
503          if (pathname) {          if (pathname) {
504                  char *ep = strchr(pathname, '\0');                  const char *ep = pathname + strlen(pathname);
505                  if (pathname < ep--) {                  if (pathname < ep--) {
506                          if (*ep != '/') i++;                          if (*ep != '/')
507                          while (pathname <= ep) if (*ep-- == '/') i += 2;                                  i++;
508                            while (pathname <= ep)
509                                    if (*ep-- == '/')
510                                            i += 2;
511                  }                  }
512          }          }
513          return i;          return i;
514  }  }
515    
516  static int const_part_length(const char *filename)  /**
517     * ccs_const_part_length - Evaluate the initial length without a pattern in a token.
518     *
519     * @filename: The string to evaluate.
520     *
521     * Returns the initial length without a pattern in @filename.
522     */
523    static int ccs_const_part_length(const char *filename)
524  {  {
525            char c;
526          int len = 0;          int len = 0;
527          if (filename) {          if (!filename)
528                  char c;                  return 0;
529                  while ((c = *filename++) != '\0') {          while (1) {
530                          if (c != '\\') { len++; continue; }                  c = *filename++;
531                          switch (c = *filename++) {                  if (!c)
                         case '\\':  /* "\\" */  
                                 len += 2; continue;  
                         case '0':   /* "\ooo" */  
                         case '1':  
                         case '2':  
                         case '3':  
                                 if ((c = *filename++) >= '0' && c <= '7' && (c = *filename++) >= '0' && c <= '7') { len += 4; continue; }  
                         }  
532                          break;                          break;
533                    if (c != '\\') {
534                            len++;
535                            continue;
536                    }
537                    c = *filename++;
538                    switch (c) {
539                    case '\\':  /* "\\" */
540                            len += 2;
541                            continue;
542                    case '0':   /* "\ooo" */
543                    case '1':
544                    case '2':
545                    case '3':
546                            c = *filename++;
547                            if (c < '0' || c > '7')
548                                    break;
549                            c = *filename++;
550                            if (c < '0' || c > '7')
551                                    break;
552                            len += 4;
553                            continue;
554                  }                  }
555                    break;
556          }          }
557          return len;          return len;
558  }  }
559    
560  void fill_path_info(struct path_info *ptr)  /**
561     * ccs_fill_path_info - Fill in "struct ccs_path_info" members.
562     *
563     * @ptr: Pointer to "struct ccs_path_info" to fill in.
564     *
565     * The caller sets "struct ccs_path_info"->name.
566     */
567    void ccs_fill_path_info(struct ccs_path_info *ptr)
568  {  {
569          const char *name = ptr->name;          const char *name = ptr->name;
570          const int len = strlen(name);          const int len = strlen(name);
571          ptr->total_len = len;          ptr->total_len = len;
572          ptr->const_len = const_part_length(name);          ptr->const_len = ccs_const_part_length(name);
573          ptr->is_dir = len && (name[len - 1] == '/');          ptr->is_dir = len && (name[len - 1] == '/');
574          ptr->is_patterned = (ptr->const_len < len);          ptr->is_patterned = (ptr->const_len < len);
575          ptr->hash = full_name_hash(name, len);          ptr->hash = full_name_hash(name, len);
576          ptr->depth = PathDepth(name);          ptr->depth = ccs_path_depth(name);
577  }  }
578    
579  static int FileMatchesToPattern2(const char *filename, const char *filename_end, const char *pattern, const char *pattern_end)  /**
580     * ccs_file_matches_pattern2 - Pattern matching without '/' character
581     * and "\-" pattern.
582     *
583     * @filename:     The start of string to check.
584     * @filename_end: The end of string to check.
585     * @pattern:      The start of pattern to compare.
586     * @pattern_end:  The end of pattern to compare.
587     *
588     * Returns true if @filename matches @pattern, false otherwise.
589     */
590    static bool ccs_file_matches_pattern2(const char *filename,
591                                          const char *filename_end,
592                                          const char *pattern,
593                                          const char *pattern_end)
594  {  {
595          while (filename < filename_end && pattern < pattern_end) {          while (filename < filename_end && pattern < pattern_end) {
596                    char c;
597                  if (*pattern != '\\') {                  if (*pattern != '\\') {
598                          if (*filename++ != *pattern++) return 0;                          if (*filename++ != *pattern++)
599                  } else {                                  return false;
600                          char c = *filename;                          continue;
601                          pattern++;                  }
602                          switch (*pattern) {                  c = *filename;
603                          case '?':                  pattern++;
604                                  if (c == '/') {                  switch (*pattern) {
605                                          return 0;                          int i;
606                                  } else if (c == '\\') {                          int j;
607                                          if ((c = filename[1]) == '\\') {                  case '?':
608                                                  filename++; /* safe because filename is \\ */                          if (c == '/') {
609                                          } else if (c >= '0' && c <= '3' && (c = filename[2]) >= '0' && c <= '7' && (c = filename[3]) >= '0' && c <= '7') {                                  return false;
610                                                  filename += 3; /* safe because filename is \ooo */                          } else if (c == '\\') {
611                                          } else {                                  if (filename[1] == '\\')
612                                                  return 0;                                          filename++;
613                                          }                                  else if (ccs_is_byte_range(filename + 1))
614                                  }                                          filename += 3;
615                                  break;                                  else
616                          case '\\':                                          return false;
617                                  if (c != '\\') return 0;                          }
618                                  if (*++filename != '\\') return 0; /* safe because *filename != '\0' */                          break;
619                                  break;                  case '\\':
620                          case '+':                          if (c != '\\')
621                                  if (c < '0' || c > '9') return 0;                                  return false;
622                                  break;                          if (*++filename != '\\')
623                          case 'x':                                  return false;
624                                  if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) return 0;                          break;
625                                  break;                  case '+':
626                          case 'a':                          if (!ccs_is_decimal(c))
627                                  if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) return 0;                                  return false;
628                            break;
629                    case 'x':
630                            if (!ccs_is_hexadecimal(c))
631                                    return false;
632                            break;
633                    case 'a':
634                            if (!ccs_is_alphabet_char(c))
635                                    return false;
636                            break;
637                    case '0':
638                    case '1':
639                    case '2':
640                    case '3':
641                            if (c == '\\' && ccs_is_byte_range(filename + 1)
642                                && strncmp(filename + 1, pattern, 3) == 0) {
643                                    filename += 3;
644                                    pattern += 2;
645                                  break;                                  break;
646                          case '0':                          }
647                          case '1':                          return false; /* Not matched. */
648                          case '2':                  case '*':
649                          case '3':                  case '@':
650                                  if (c == '\\' && (c = filename[1]) >= '0' && c <= '3' && c == *pattern                          for (i = 0; i <= filename_end - filename; i++) {
651                                          && (c = filename[2]) >= '0' && c <= '7' && c == pattern[1]                                  if (ccs_file_matches_pattern2(filename + i,
652                                          && (c = filename[3]) >= '0' && c <= '7' && c == pattern[2]) {                                                                filename_end,
653                                          filename += 3; /* safe because filename is \ooo */                                                                pattern + 1,
654                                          pattern += 2; /* safe because pattern is \ooo  */                                                                pattern_end))
655                                            return true;
656                                    c = filename[i];
657                                    if (c == '.' && *pattern == '@')
658                                          break;                                          break;
659                                  }                                  if (c != '\\')
660                                  return 0; /* Not matched. */                                          continue;
661                          case '*':                                  if (filename[i + 1] == '\\')
662                          case '@':                                          i++;
663                                  {                                  else if (ccs_is_byte_range(filename + i + 1))
664                                          int i;                                          i += 3;
665                                          for (i = 0; i <= filename_end - filename; i++) {                                  else
666                                                  if (FileMatchesToPattern2(filename + i, filename_end, pattern + 1, pattern_end)) return 1;                                          break; /* Bad pattern. */
667                                                  if ((c = filename[i]) == '.' && *pattern == '@') break;                          }
668                                                  if (c == '\\') {                          return false; /* Not matched. */
669                                                          if ((c = filename[i + 1]) == '\\') {                  default:
670                                                                  i++; /* safe because filename is \\ */                          j = 0;
671                                                          } else if (c >= '0' && c <= '3' && (c = filename[i + 2]) >= '0' && c <= '7' && (c = filename[i + 3]) >= '0' && c <= '7') {                          c = *pattern;
672                                                                  i += 3; /* safe because filename is \ooo */                          if (c == '$') {
673                                                          } else {                                  while (ccs_is_decimal(filename[j]))
674                                                                  break; /* Bad pattern. */                                          j++;
675                                                          }                          } else if (c == 'X') {
676                                                  }                                  while (ccs_is_hexadecimal(filename[j]))
677                                          }                                          j++;
678                                          return 0; /* Not matched. */                          } else if (c == 'A') {
679                                  }                                  while (ccs_is_alphabet_char(filename[j]))
680                          default:                                          j++;
                                 {  
                                         int i, j = 0;  
                                         if ((c = *pattern) == '$') {  
                                                 while ((c = filename[j]) >= '0' && c <= '9') j++;  
                                         } else if (c == 'X') {  
                                                 while (((c = filename[j]) >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) j++;  
                                         } else if (c == 'A') {  
                                                 while (((c = filename[j]) >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) j++;  
                                         }  
                                         for (i = 1; i <= j; i++) {  
                                                 if (FileMatchesToPattern2(filename + i, filename_end, pattern + 1, pattern_end)) return 1;  
                                         }  
                                 }  
                                 return 0; /* Not matched or bad pattern. */  
681                          }                          }
682                          filename++; /* safe because *filename != '\0' */                          for (i = 1; i <= j; i++) {
683                          pattern++; /* safe because *pattern != '\0' */                                  if (ccs_file_matches_pattern2(filename + i,
684                                                                  filename_end,
685                                                                  pattern + 1,
686                                                                  pattern_end))
687                                            return true;
688                            }
689                            return false; /* Not matched or bad pattern. */
690                  }                  }
691                    filename++;
692                    pattern++;
693          }          }
694          while (*pattern == '\\' && (*(pattern + 1) == '*' || *(pattern + 1) == '@')) pattern += 2;          while (*pattern == '\\' &&
695          return (filename == filename_end && pattern == pattern_end);                 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
696                    pattern += 2;
697            return filename == filename_end && pattern == pattern_end;
698  }  }
699    
700  static int FileMatchesToPattern(const char *filename, const char *filename_end, const char *pattern, const char *pattern_end)  /**
701     * ccs_file_matches_pattern - Pattern matching without without '/' character.
702     *
703     * @filename:     The start of string to check.
704     * @filename_end: The end of string to check.
705     * @pattern:      The start of pattern to compare.
706     * @pattern_end:  The end of pattern to compare.
707     *
708     * Returns true if @filename matches @pattern, false otherwise.
709     */
710    static bool ccs_file_matches_pattern(const char *filename,
711                                         const char *filename_end,
712                                         const char *pattern,
713                                         const char *pattern_end)
714  {  {
715          const char *pattern_start = pattern;          const char *pattern_start = pattern;
716          int first = 1;          bool first = true;
717          int result;          bool result;
718          while (pattern < pattern_end - 1) {          while (pattern < pattern_end - 1) {
719                  if (*pattern++ != '\\' || *pattern++ != '-') continue;                  /* Split at "\-" pattern. */
720                  result = FileMatchesToPattern2(filename, filename_end, pattern_start, pattern - 2);                  if (*pattern++ != '\\' || *pattern++ != '-')
721                  if (first) result = !result;                          continue;
722                  if (result) return 0;                  result = ccs_file_matches_pattern2(filename, filename_end,
723                  first = 0;                                                     pattern_start, pattern - 2);
724                    if (first)
725                            result = !result;
726                    if (result)
727                            return false;
728                    first = false;
729                  pattern_start = pattern;                  pattern_start = pattern;
730          }          }
731          result = FileMatchesToPattern2(filename, filename_end, pattern_start, pattern_end);          result = ccs_file_matches_pattern2(filename, filename_end,
732                                               pattern_start, pattern_end);
733          return first ? result : !result;          return first ? result : !result;
734  }  }
735    
736  /*  /**
737   *  Check whether the given pathname matches to the given pattern.   * ccs_path_matches_pattern - Check whether the given filename matches the given pattern.
738   *  Returns nonzero if matches, zero otherwise.   * @filename: The filename to check.
739     * @pattern:  The pattern to compare.
740   *   *
741   *  The following patterns are available.   * Returns true if matches, false otherwise.
742   *    \\     \ itself.   *
743   *    \ooo   Octal representation of a byte.   * The following patterns are available.
744   *    \*     More than or equals to 0 character other than '/'.   *   \\     \ itself.
745   *    \@     More than or equals to 0 character other than '/' or '.'.   *   \ooo   Octal representation of a byte.
746   *    \?     1 byte character other than '/'.   *   \*     More than or equals to 0 character other than '/'.
747   *    \$     More than or equals to 1 decimal digit.   *   \@     More than or equals to 0 character other than '/' or '.'.
748   *    \+     1 decimal digit.   *   \?     1 byte character other than '/'.
749   *    \X     More than or equals to 1 hexadecimal digit.   *   \$     More than or equals to 1 decimal digit.
750   *    \x     1 hexadecimal digit.   *   \+     1 decimal digit.
751   *    \A     More than or equals to 1 alphabet character.   *   \X     More than or equals to 1 hexadecimal digit.
752   *    \a     1 alphabet character.   *   \x     1 hexadecimal digit.
753   *    \-     Subtraction operator.   *   \A     More than or equals to 1 alphabet character.
754   */   *   \a     1 alphabet character.
755     *   \-     Subtraction operator.
756  int PathMatchesToPattern(const struct path_info *pathname0, const struct path_info *pattern0)   */
757  {  bool ccs_path_matches_pattern(const struct ccs_path_info *filename,
758          /* if (!pathname || !pattern) return 0; */                                const struct ccs_path_info *pattern)
759          const char *pathname = pathname0->name, *pattern = pattern0->name;  {
760          const int len = pattern0->const_len;          /*
761          if (!pattern0->is_patterned) return !pathcmp(pathname0, pattern0);            if (!filename || !pattern)
762          if (pathname0->depth != pattern0->depth) return 0;            return false;
763          if (strncmp(pathname, pattern, len)) return 0;          */
764          pathname += len; pattern += len;          const char *f = filename->name;
765          while (*pathname && *pattern) {          const char *p = pattern->name;
766                  const char *pathname_delimiter = strchr(pathname, '/'), *pattern_delimiter = strchr(pattern, '/');          const int len = pattern->const_len;
767                  if (!pathname_delimiter) pathname_delimiter = strchr(pathname, '\0');          /* If @pattern doesn't contain pattern, I can use strcmp(). */
768                  if (!pattern_delimiter) pattern_delimiter = strchr(pattern, '\0');          if (!pattern->is_patterned)
769                  if (!FileMatchesToPattern(pathname, pathname_delimiter, pattern, pattern_delimiter)) return 0;                  return !ccs_pathcmp(filename, pattern);
770                  pathname = *pathname_delimiter ? pathname_delimiter + 1 : pathname_delimiter;          /* Dont compare if the number of '/' differs. */
771                  pattern = *pattern_delimiter ? pattern_delimiter + 1 : pattern_delimiter;          if (filename->depth != pattern->depth)
772          }                  return false;
773          while (*pattern == '\\' && (*(pattern + 1) == '*' || *(pattern + 1) == '@')) pattern += 2;          /* Compare the initial length without patterns. */
774          return (!*pathname && !*pattern);          if (strncmp(f, p, len))
775                    return false;
776            f += len;
777            p += len;
778            /* Main loop. Compare each directory component. */
779            while (*f && *p) {
780                    const char *f_delimiter = strchr(f, '/');
781                    const char *p_delimiter = strchr(p, '/');
782                    if (!f_delimiter)
783                            f_delimiter = f + strlen(f);
784                    if (!p_delimiter)
785                            p_delimiter = p + strlen(p);
786                    if (!ccs_file_matches_pattern(f, f_delimiter, p, p_delimiter))
787                            return false;
788                    f = f_delimiter;
789                    if (*f)
790                            f++;
791                    p = p_delimiter;
792                    if (*p)
793                            p++;
794            }
795            /* Ignore trailing "\*" and "\@" in @pattern. */
796            while (*p == '\\' &&
797                   (*(p + 1) == '*' || *(p + 1) == '@'))
798                    p += 2;
799            return !*f && !*p;
800  }  }
801    
802  /*  /**
803   *  Transactional printf() to struct io_buffer structure.   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
804   *  snprintf() will truncate, but io_printf() won't.   *
805   *  Returns zero on success, nonzero otherwise.   * @head: Pointer to "struct ccs_io_buffer".
806     * @fmt:  The printf()'s format string, followed by parameters.
807     *
808     * Returns true on success, false otherwise.
809     *
810     * The snprintf() will truncate, but ccs_io_printf() won't.
811   */   */
812  int io_printf(struct io_buffer *head, const char *fmt, ...)  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
813  {  {
814          va_list args;          va_list args;
815          int len, pos = head->read_avail, size = head->readbuf_size - pos;          int len;
816          if (size <= 0) return -ENOMEM;          int pos = head->read_avail;
817            int size = head->readbuf_size - pos;
818            if (size <= 0)
819                    return false;
820          va_start(args, fmt);          va_start(args, fmt);
821          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args);
822          va_end(args);          va_end(args);
823          if (pos + len >= head->readbuf_size) return -ENOMEM;          if (pos + len >= head->readbuf_size)
824                    return false;
825          head->read_avail += len;          head->read_avail += len;
826          return 0;          return true;
827  }  }
828    
829  /*  /**
830   * Get realpath() of current process.   * ccs_get_exe - Get ccs_realpath() of current process.
831   * This function uses ccs_alloc(), so caller must ccs_free() if this function didn't return NULL.   *
832     * Returns the ccs_realpath() of current process on success, NULL otherwise.
833     *
834     * This function uses kzalloc(), so the caller must kfree()
835     * if this function didn't return NULL.
836   */   */
837  const char *GetEXE(void)  const char *ccs_get_exe(void)
838  {  {
839          struct mm_struct *mm = current->mm;          struct mm_struct *mm = current->mm;
840          struct vm_area_struct *vma;          struct vm_area_struct *vma;
841          const char *cp = NULL;          const char *cp = NULL;
842          if (!mm) return NULL;          if (!mm)
843                    return NULL;
844          down_read(&mm->mmap_sem);          down_read(&mm->mmap_sem);
845          for (vma = mm->mmap; vma; vma = vma->vm_next) {          for (vma = mm->mmap; vma; vma = vma->vm_next) {
846                  if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {                  if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
847                          cp = realpath_from_dentry(vma->vm_file->f_dentry, vma->vm_file->f_vfsmnt);                          cp = ccs_realpath_from_dentry(vma->vm_file->f_dentry,
848                                                          vma->vm_file->f_vfsmnt);
849                          break;                          break;
850                  }                  }
851          }          }
# Line 488  const char *GetEXE(void) Line 853  const char *GetEXE(void)
853          return cp;          return cp;
854  }  }
855    
856  const char *GetMSG(const bool is_enforce)  /**
857     * ccs_get_msg - Get warning message.
858     *
859     * @is_enforce: Is it enforcing mode?
860     *
861     * Returns "ERROR" or "WARNING".
862     */
863    const char *ccs_get_msg(const bool is_enforce)
864  {  {
865          if (is_enforce) return "ERROR"; else return "WARNING";          if (is_enforce)
866                    return "ERROR";
867            else
868                    return "WARNING";
869  }  }
870    
871  const char *GetAltExec(void)  /**
872     * ccs_can_sleep - Check whether it is permitted to do operations that may sleep.
873     *
874     * Returns true if it is permitted to do operations that may sleep,
875     * false otherwise.
876     *
877     * TOMOYO Linux supports interactive enforcement that lets processes
878     * wait for the administrator's decision.
879     * All hooks but the one for ccs_may_autobind() are inserted where
880     * it is permitted to do operations that may sleep.
881     * Thus, this warning should not happen.
882     */
883    bool ccs_can_sleep(void)
884  {  {
885          const u8 profile = current->domain_info->profile;          static u8 count = 20;
886          const struct path_info *alt_exec = profile_ptr[profile] ? profile_ptr[profile]->alt_exec : NULL;          if (likely(!in_interrupt()))
887          return alt_exec ? alt_exec->name : NULL;                  return true;
888            if (count) {
889                    count--;
890                    printk(KERN_ERR "BUG: sleeping function called "
891                           "from invalid context.\n");
892                    dump_stack();
893            }
894            return false;
895  }  }
896    
897  /*************************  DOMAIN POLICY HANDLER  *************************/  /**
898     * ccs_check_flags - Check mode for specified functionality.
899     *
900     * @domain: Pointer to "struct ccs_domain_info". NULL for ccs_current_domain().
901     * @index:  The functionality to check mode.
902     *
903     * Returns the mode of specified functionality.
904     */
905    unsigned int ccs_check_flags(const struct ccs_domain_info *domain,
906                                 const u8 index)
907    {
908            u8 profile;
909            if (!domain)
910                    domain = ccs_current_domain();
911            profile = domain->profile;
912            return ccs_policy_loaded && index < CCS_MAX_CONTROL_INDEX
913    #if MAX_PROFILES != 256
914                    && profile < MAX_PROFILES
915    #endif
916                    && ccs_profile_ptr[profile] ?
917                    ccs_profile_ptr[profile]->value[index] : 0;
918    }
919    
920  /* Check whether the given access control is enabled. */  /**
921  unsigned int CheckCCSFlags(const unsigned int index)   * ccs_check_capability_flags - Check mode for specified capability.
922     *
923     * @domain: Pointer to "struct ccs_domain_info". NULL for ccs_current_domain().
924     * @index:  The capability to check mode.
925     *
926     * Returns the mode of specified capability.
927     */
928    static u8 ccs_check_capability_flags(const struct ccs_domain_info *domain,
929                                         const u8 index)
930  {  {
931          const u8 profile = current->domain_info->profile;          const u8 profile = domain ? domain->profile :
932          return sbin_init_started && index < CCS_MAX_CONTROL_INDEX                  ccs_current_domain()->profile;
933            return ccs_policy_loaded && index < CCS_MAX_CAPABILITY_INDEX
934  #if MAX_PROFILES != 256  #if MAX_PROFILES != 256
935                  && profile < MAX_PROFILES                  && profile < MAX_PROFILES
936  #endif  #endif
937                  && profile_ptr[profile] ? profile_ptr[profile]->value[index] : 0;                  && ccs_profile_ptr[profile] ?
938                    ccs_profile_ptr[profile]->capability_value[index] : 0;
939  }  }
940    
941  bool TomoyoVerboseMode(void)  /**
942     * ccs_cap2keyword - Convert capability operation to capability name.
943     *
944     * @operation: The capability index.
945     *
946     * Returns the name of the specified capability's name.
947     */
948    const char *ccs_cap2keyword(const u8 operation)
949  {  {
950          return CheckCCSFlags(CCS_TOMOYO_VERBOSE) != 0;          return operation < CCS_MAX_CAPABILITY_INDEX
951                    ? ccs_capability_control_keyword[operation] : NULL;
952  }  }
953    
954  /* Check whether the given access control is enforce mode. */  /**
955  bool CheckCCSEnforce(const unsigned int index)   * ccs_init_request_info - Initialize "struct ccs_request_info" members.
956     *
957     * @r:      Pointer to "struct ccs_request_info" to initialize.
958     * @domain: Pointer to "struct ccs_domain_info". NULL for ccs_current_domain().
959     * @index:  Index number of functionality.
960     */
961    void ccs_init_request_info(struct ccs_request_info *r,
962                               struct ccs_domain_info *domain, const u8 index)
963  {  {
964          return CheckCCSFlags(index) == 3;          memset(r, 0, sizeof(*r));
965            if (!domain)
966                    domain = ccs_current_domain();
967            r->domain = domain;
968            r->profile = domain->profile;
969            if (index < CCS_MAX_CONTROL_INDEX)
970                    r->mode = ccs_check_flags(domain, index);
971            else
972                    r->mode = ccs_check_capability_flags(domain, index
973                                                         - CCS_MAX_CONTROL_INDEX);
974  }  }
975    
976  bool CheckDomainQuota(struct domain_info * const domain)  /**
977     * ccs_verbose_mode - Check whether TOMOYO is verbose mode.
978     *
979     * @domain: Pointer to "struct ccs_domain_info". NULL for ccs_current_domain().
980     *
981     * Returns true if domain policy violation warning should be printed to
982     * console.
983     */
984    bool ccs_verbose_mode(const struct ccs_domain_info *domain)
985    {
986            return ccs_check_flags(domain, CCS_VERBOSE) != 0;
987    }
988    
989    /**
990     * ccs_domain_quota_ok - Check for domain's quota.
991     *
992     * @r: Pointer to "struct ccs_request_info".
993     *
994     * Returns true if the domain is not exceeded quota, false otherwise.
995     *
996     * Caller holds ccs_read_lock().
997     */
998    bool ccs_domain_quota_ok(struct ccs_request_info *r)
999  {  {
1000          unsigned int count = 0;          unsigned int count = 0;
1001          struct acl_info *ptr;          struct ccs_domain_info *domain = r->domain;
1002          if (!domain) return 1;          struct ccs_acl_info *ptr;
1003          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          ccs_check_read_lock();
1004                  if (!ptr->is_deleted) count++;          if (r->mode != 1)
1005                    return false;
1006            if (!domain)
1007                    return true;
1008            list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1009                    if (ptr->type & ACL_DELETED)
1010                            continue;
1011                    switch (ccs_acl_type2(ptr)) {
1012                            struct ccs_single_path_acl_record *acl1;
1013                            struct ccs_double_path_acl_record *acl2;
1014                            u16 perm;
1015                    case TYPE_SINGLE_PATH_ACL:
1016                            acl1 = container_of(ptr,
1017                                                struct ccs_single_path_acl_record,
1018                                                head);
1019                            perm = acl1->perm;
1020                            if (perm & (1 << TYPE_EXECUTE_ACL))
1021                                    count++;
1022                            if (perm &
1023                                ((1 << TYPE_READ_ACL) | (1 << TYPE_WRITE_ACL)))
1024                                    count++;
1025                            if (perm & (1 << TYPE_CREATE_ACL))
1026                                    count++;
1027                            if (perm & (1 << TYPE_UNLINK_ACL))
1028                                    count++;
1029                            if (perm & (1 << TYPE_MKDIR_ACL))
1030                                    count++;
1031                            if (perm & (1 << TYPE_RMDIR_ACL))
1032                                    count++;
1033                            if (perm & (1 << TYPE_MKFIFO_ACL))
1034                                    count++;
1035                            if (perm & (1 << TYPE_MKSOCK_ACL))
1036                                    count++;
1037                            if (perm & (1 << TYPE_MKBLOCK_ACL))
1038                                    count++;
1039                            if (perm & (1 << TYPE_MKCHAR_ACL))
1040                                    count++;
1041                            if (perm & (1 << TYPE_TRUNCATE_ACL))
1042                                    count++;
1043                            if (perm & (1 << TYPE_SYMLINK_ACL))
1044                                    count++;
1045                            if (perm & (1 << TYPE_REWRITE_ACL))
1046                                    count++;
1047                            break;
1048                    case TYPE_DOUBLE_PATH_ACL:
1049                            acl2 = container_of(ptr,
1050                                                struct ccs_double_path_acl_record,
1051                                                head);
1052                            perm = acl2->perm;
1053                            if (perm & (1 << TYPE_LINK_ACL))
1054                                    count++;
1055                            if (perm & (1 << TYPE_RENAME_ACL))
1056                                    count++;
1057                            break;
1058                    case TYPE_EXECUTE_HANDLER:
1059                    case TYPE_DENIED_EXECUTE_HANDLER:
1060                            break;
1061                    default:
1062                            count++;
1063                    }
1064          }          }
1065          if (count < CheckCCSFlags(CCS_TOMOYO_MAX_ACCEPT_ENTRY)) return 1;          if (count < ccs_check_flags(domain, CCS_MAX_ACCEPT_ENTRY))
1066                    return true;
1067          if (!domain->quota_warned) {          if (!domain->quota_warned) {
1068                  domain->quota_warned = 1;                  domain->quota_warned = true;
1069                  printk("TOMOYO-WARNING: Domain '%s' has so many ACLs to hold. Stopped learning mode.\n", domain->domainname->name);                  printk(KERN_WARNING "TOMOYO-WARNING: "
1070                           "Domain '%s' has so many ACLs to hold. "
1071                           "Stopped learning mode.\n", domain->domainname->name);
1072          }          }
1073          return 0;          return false;
 }  
   
 /* Check whether the given access control is learning mode. */  
 bool CheckCCSAccept(const unsigned int index, struct domain_info * const domain)  
 {  
         if (CheckCCSFlags(index) != 1) return 0;  
         return CheckDomainQuota(domain);  
1074  }  }
1075    
1076  static struct profile *FindOrAssignNewProfile(const unsigned int profile)  /**
1077     * ccs_find_or_assign_new_profile - Create a new profile.
1078     *
1079     * @profile: Profile number to create.
1080     *
1081     * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
1082     */
1083    static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int
1084                                                              profile)
1085  {  {
1086          static DEFINE_MUTEX(profile_lock);          struct ccs_profile *ptr;
1087          struct profile *ptr = NULL;          struct ccs_profile *entry;
1088          mutex_lock(&profile_lock);          int i;
1089          if (profile < MAX_PROFILES && (ptr = profile_ptr[profile]) == NULL) {          if (profile >= MAX_PROFILES)
1090                  if ((ptr = alloc_element(sizeof(*ptr))) != NULL) {                  return NULL;
1091                          int i;          ptr = ccs_profile_ptr[profile];
1092                          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) ptr->value[i] = ccs_control_array[i].current_value;          if (ptr)
1093                          mb(); /* Avoid out-of-order execution. */                  return ptr;
1094                          profile_ptr[profile] = ptr;          entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1095                  }          mutex_lock(&ccs_policy_lock);
1096            ptr = ccs_profile_ptr[profile];
1097            if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
1098                    ptr = entry;
1099                    for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)
1100                            ptr->value[i] = ccs_control_array[i].current_value;
1101                    /*
1102                     * Needn't to initialize "ptr->capability_value"
1103                     * because they are always 0.
1104                     */
1105                    mb(); /* Avoid out-of-order execution. */
1106                    ccs_profile_ptr[profile] = ptr;
1107                    entry = NULL;
1108          }          }
1109          mutex_unlock(&profile_lock);          mutex_unlock(&ccs_policy_lock);
1110            kfree(entry);
1111          return ptr;          return ptr;
1112  }  }
1113    
1114  /* #define ALT_EXEC */  /**
1115     * ccs_write_profile - Write profile table.
1116  static int SetProfile(struct io_buffer *head)   *
1117     * @head: Pointer to "struct ccs_io_buffer".
1118     *
1119     * Returns 0 on success, negative value otherwise.
1120     */
1121    static int ccs_write_profile(struct ccs_io_buffer *head)
1122  {  {
1123          char *data = head->write_buf;          char *data = head->write_buf;
1124          unsigned int i, value;          unsigned int i;
1125            unsigned int value;
1126          char *cp;          char *cp;
1127          struct profile *profile;          struct ccs_profile *ccs_profile;
         if (!isRoot()) return -EPERM;  
1128          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
1129          if (data != cp) {          if (data != cp) {
1130                  if (*cp != '-') return -EINVAL;                  if (*cp != '-')
1131                  data= cp + 1;                          return -EINVAL;
1132                    data = cp + 1;
1133          }          }
1134          profile = FindOrAssignNewProfile(i);          ccs_profile = ccs_find_or_assign_new_profile(i);
1135          if (!profile) return -EINVAL;          if (!ccs_profile)
1136                    return -EINVAL;
1137          cp = strchr(data, '=');          cp = strchr(data, '=');
1138          if (!cp) return -EINVAL;          if (!cp)
1139                    return -EINVAL;
1140          *cp = '\0';          *cp = '\0';
1141          UpdateCounter(CCS_UPDATES_COUNTER_PROFILE);          if (!strcmp(data, "COMMENT")) {
1142          if (strcmp(data, ccs_control_array[CCS_PROFILE_COMMENT].keyword) == 0) {                  const struct ccs_path_info *new_comment
1143                  profile->comment = SaveName(cp + 1);                          = ccs_get_name(cp + 1);
1144                  return 0;                  const struct ccs_path_info *old_comment;
1145          }                  /* Protect reader from ccs_put_name(). */
1146  #ifdef ALT_EXEC                  /***** CRITICAL SECTION START *****/
1147  #ifdef CONFIG_TOMOYO                  spin_lock(&ccs_profile_comment_lock);
1148          if (strcmp(data, ccs_control_array[CCS_TOMOYO_ALT_EXEC].keyword) == 0) {                  old_comment = ccs_profile->comment;
1149                  cp++;                  ccs_profile->comment = new_comment;
1150                  if (*cp && !IsCorrectPath(cp, 1, -1, -1, __FUNCTION__)) cp = "";                  spin_unlock(&ccs_profile_comment_lock);
1151                  profile->alt_exec = SaveName(cp);                  /***** CRITICAL SECTION END *****/
1152                    ccs_put_name(old_comment);
1153                    ccs_profile_entry_used[0] = true;
1154                  return 0;                  return 0;
1155          }          }
1156  #endif          if (ccs_str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {
1157  #endif                  if (sscanf(cp + 1, "%u", &value) != 1) {
1158          if (sscanf(cp + 1, "%u", &value) != 1) return -EINVAL;                          for (i = 0; i < 4; i++) {
1159  #ifdef CONFIG_TOMOYO                                  if (strcmp(cp + 1, ccs_mode_4[i]))
1160          if (strncmp(data, KEYWORD_MAC_FOR_CAPABILITY, KEYWORD_MAC_FOR_CAPABILITY_LEN) == 0) {                                          continue;
1161                  return SetCapabilityStatus(data + KEYWORD_MAC_FOR_CAPABILITY_LEN, value, i);                                  value = i;
1162                                    break;
1163                            }
1164                            if (i == 4)
1165                                    return -EINVAL;
1166                    }
1167                    if (value > 3)
1168                            value = 3;
1169                    for (i = 0; i < CCS_MAX_CAPABILITY_INDEX; i++) {
1170                            if (strcmp(data, ccs_capability_control_keyword[i]))
1171                                    continue;
1172                            ccs_profile->capability_value[i] = value;
1173                            ccs_profile_entry_used[i + 1 + CCS_MAX_CONTROL_INDEX]
1174                                    = true;
1175                            return 0;
1176                    }
1177                    return -EINVAL;
1178          }          }
 #endif  
1179          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {          for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {
1180                  if (strcmp(data, ccs_control_array[i].keyword)) continue;                  if (strcmp(data, ccs_control_array[i].keyword))
1181                  if (value > ccs_control_array[i].max_value) value = ccs_control_array[i].max_value;                          continue;
1182                  profile->value[i] = value;                  if (sscanf(cp + 1, "%u", &value) != 1) {
1183                            int j;
1184                            const char **modes;
1185                            switch (i) {
1186                            case CCS_RESTRICT_AUTOBIND:
1187                            case CCS_VERBOSE:
1188                                    modes = ccs_mode_2;
1189                                    break;
1190                            default:
1191                                    modes = ccs_mode_4;
1192                                    break;
1193                            }
1194                            for (j = 0; j < 4; j++) {
1195                                    if (strcmp(cp + 1, modes[j]))
1196                                            continue;
1197                                    value = j;
1198                                    break;
1199                            }
1200                            if (j == 4)
1201                                    return -EINVAL;
1202                    } else if (value > ccs_control_array[i].max_value) {
1203                            value = ccs_control_array[i].max_value;
1204                    }
1205                    ccs_profile->value[i] = value;
1206                    ccs_profile_entry_used[i + 1] = true;
1207                  return 0;                  return 0;
1208          }          }
1209          return -EINVAL;          return -EINVAL;
1210  }  }
1211    
1212  static int ReadProfile(struct io_buffer *head)  /**
1213     * ccs_read_profile - Read profile table.
1214     *
1215     * @head: Pointer to "struct ccs_io_buffer".
1216     *
1217     * Returns 0.
1218     */
1219    static int ccs_read_profile(struct ccs_io_buffer *head)
1220  {  {
1221          if (!head->read_eof) {          static const int ccs_total
1222                  if (!isRoot()) return -EPERM;                  = CCS_MAX_CONTROL_INDEX + CCS_MAX_CAPABILITY_INDEX + 1;
1223                  if (!head->read_var2) {          int step;
1224                          int step;          if (head->read_eof)
1225                          for (step = head->read_step; step < MAX_PROFILES * CCS_MAX_CONTROL_INDEX; step++) {                  return 0;
1226                                  const int i = step / CCS_MAX_CONTROL_INDEX, j = step % CCS_MAX_CONTROL_INDEX;          for (step = head->read_step; step < MAX_PROFILES * ccs_total; step++) {
1227                                  const struct profile *profile = profile_ptr[i];                  const u8 index = step / ccs_total;
1228                                  head->read_step = step;                  u8 type = step % ccs_total;
1229                                  if (!profile) continue;                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];
1230                                  switch (j) {                  head->read_step = step;
1231                                  case -1: /* Dummy */                  if (!ccs_profile)
1232  #ifndef CONFIG_SAKURA                          continue;
1233                                  case CCS_SAKURA_DENY_CONCEAL_MOUNT:                  if (!ccs_profile_entry_used[type])
1234                                  case CCS_SAKURA_RESTRICT_CHROOT:                          continue;
1235                                  case CCS_SAKURA_RESTRICT_MOUNT:                  if (!type) { /* Print profile' comment tag. */
1236                                  case CCS_SAKURA_RESTRICT_UNMOUNT:                          bool done;
1237                                  case CCS_SAKURA_RESTRICT_PIVOT_ROOT:                          /***** CRITICAL SECTION START *****/
1238                                  case CCS_SAKURA_RESTRICT_AUTOBIND:                          spin_lock(&ccs_profile_comment_lock);
1239  #endif                          done = ccs_io_printf(head, "%u-COMMENT=%s\n",
1240  #ifndef CONFIG_TOMOYO                                               index, ccs_profile->comment ?
1241                                  case CCS_TOMOYO_MAC_FOR_FILE:                                               ccs_profile->comment->name : "");
1242                                  case CCS_TOMOYO_MAC_FOR_ARGV0:                          spin_unlock(&ccs_profile_comment_lock);
1243                                  case CCS_TOMOYO_MAC_FOR_ENV:                          /***** CRITICAL SECTION END *****/
1244                                  case CCS_TOMOYO_MAC_FOR_NETWORK:                          if (!done)
1245                                  case CCS_TOMOYO_MAC_FOR_SIGNAL:                                  break;
1246                                  case CCS_TOMOYO_MAX_ACCEPT_ENTRY:                          continue;
1247                                  case CCS_TOMOYO_MAX_GRANT_LOG:                  }
1248                                  case CCS_TOMOYO_MAX_REJECT_LOG:                  type--;
1249                                  case CCS_TOMOYO_VERBOSE:                  if (type >= CCS_MAX_CONTROL_INDEX) {
1250  #endif                          const int i = type - CCS_MAX_CONTROL_INDEX;
1251  #ifndef ALT_EXEC                          const u8 value = ccs_profile->capability_value[i];
1252                                  case CCS_TOMOYO_ALT_EXEC:                          if (!ccs_io_printf(head,
1253                                  case CCS_SLEEP_PERIOD:                                             "%u-" KEYWORD_MAC_FOR_CAPABILITY
1254  #endif                                             "%s=%s\n", index,
1255                                          continue;                                             ccs_capability_control_keyword[i],
1256                                  }                                             ccs_mode_4[value]))
1257                                  if (j == CCS_PROFILE_COMMENT) {                                  break;
1258                                          if (io_printf(head, "%u-%s=%s\n", i, ccs_control_array[CCS_PROFILE_COMMENT].keyword, profile->comment ? profile->comment->name : "")) break;                  } else {
1259                                  } else if (j == CCS_TOMOYO_ALT_EXEC) {                          const unsigned int value = ccs_profile->value[type];
1260                                          const struct path_info *alt_exec = profile->alt_exec;                          const char **modes = NULL;
1261                                          if (io_printf(head, "%u-%s=%s\n", i, ccs_control_array[CCS_TOMOYO_ALT_EXEC].keyword, alt_exec ? alt_exec->name : "")) break;                          const char *keyword = ccs_control_array[type].keyword;
1262                                  } else {                          switch (ccs_control_array[type].max_value) {
1263                                          if (io_printf(head, "%u-%s=%u\n", i, ccs_control_array[j].keyword, profile->value[j])) break;                          case 3:
1264                                  }                                  modes = ccs_mode_4;
1265                                    break;
1266                            case 1:
1267                                    modes = ccs_mode_2;
1268                                    break;
1269                          }                          }
1270                          if (step == MAX_PROFILES * CCS_MAX_CONTROL_INDEX) {                          if (modes) {
1271                                  head->read_var2 = (void *) "";                                  if (!ccs_io_printf(head, "%u-%s=%s\n", index,
1272                                  head->read_step = 0;                                                     keyword, modes[value]))
1273                                            break;
1274                            } else {
1275                                    if (!ccs_io_printf(head, "%u-%s=%u\n", index,
1276                                                       keyword, value))
1277                                            break;
1278                          }                          }
1279                  }                  }
                 if (head->read_var2) {  
 #ifdef CONFIG_TOMOYO  
                         if (ReadCapabilityStatus(head) == 0)  
 #endif  
                                 head->read_eof = 1;  
                 }  
1280          }          }
1281            if (step == MAX_PROFILES * ccs_total)
1282                    head->read_eof = true;
1283          return 0;          return 0;
1284  }  }
1285    
1286  /*************************  POLICY MANAGER HANDLER  *************************/  /* The list for "struct ccs_policy_manager_entry". */
1287    LIST_HEAD(ccs_policy_manager_list);
1288    
1289  struct policy_manager_entry {  /**
1290          struct list1_head list;   * ccs_update_manager_entry - Add a manager entry.
1291          const struct path_info *manager;   *
1292          bool is_domain;   * @manager:   The path to manager or the domainnamme.
1293          bool is_deleted;   * @is_delete: True if it is a delete request.
1294  };   *
1295     * Returns 0 on success, negative value otherwise.
1296  static LIST1_HEAD(policy_manager_list);   */
1297    static int ccs_update_manager_entry(const char *manager, const bool is_delete)
 static int AddManagerEntry(const char *manager, const bool is_delete)  
1298  {  {
1299          struct policy_manager_entry *new_entry, *ptr;          struct ccs_policy_manager_entry *entry = NULL;
1300          static DEFINE_MUTEX(lock);          struct ccs_policy_manager_entry *ptr;
1301          const struct path_info *saved_manager;          const struct ccs_path_info *saved_manager;
1302          int error = -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
1303          bool is_domain = 0;          bool is_domain = false;
1304          if (!isRoot()) return -EPERM;          if (ccs_is_domain_def(manager)) {
1305          if (IsDomainDef(manager)) {                  if (!ccs_is_correct_domain(manager))
1306                  if (!IsCorrectDomain(manager, __FUNCTION__)) return -EINVAL;                          return -EINVAL;
1307                  is_domain = 1;                  is_domain = true;
1308          } else {          } else {
1309                  if (!IsCorrectPath(manager, 1, -1, -1, __FUNCTION__)) return -EINVAL;                  if (!ccs_is_correct_path(manager, 1, -1, -1))
1310                            return -EINVAL;
1311          }          }
1312          if ((saved_manager = SaveName(manager)) == NULL) return -ENOMEM;          saved_manager = ccs_get_name(manager);
1313          mutex_lock(&lock);          if (!saved_manager)
1314          list1_for_each_entry(ptr, &policy_manager_list, list) {                  return -ENOMEM;
1315                  if (ptr->manager == saved_manager) {          if (!is_delete)
1316                          ptr->is_deleted = is_delete;                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1317                          error = 0;          mutex_lock(&ccs_policy_lock);
1318                          goto out;          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
1319                  }                  if (ptr->manager != saved_manager)
1320                            continue;
1321                    ptr->is_deleted = is_delete;
1322                    error = 0;
1323                    break;
1324          }          }
1325          if (is_delete) {          if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {
1326                  error = -ENOENT;                  entry->manager = saved_manager;
1327                  goto out;                  saved_manager = NULL;
1328                    entry->is_domain = is_domain;
1329                    list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);
1330                    entry = NULL;
1331                    error = 0;
1332          }          }
1333          if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;          mutex_unlock(&ccs_policy_lock);
1334          new_entry->manager = saved_manager;          ccs_put_name(saved_manager);
1335          new_entry->is_domain = is_domain;          kfree(entry);
         list1_add_tail_mb(&new_entry->list, &policy_manager_list);  
         error = 0;  
  out:  
         mutex_unlock(&lock);  
         if (!error) UpdateCounter(CCS_UPDATES_COUNTER_MANAGER);  
1336          return error;          return error;
1337  }  }
1338    
1339  static int AddManagerPolicy(struct io_buffer *head)  /**
1340     * ccs_write_manager_policy - Write manager policy.
1341     *
1342     * @head: Pointer to "struct ccs_io_buffer".
1343     *
1344     * Returns 0 on success, negative value otherwise.
1345     */
1346    static int ccs_write_manager_policy(struct ccs_io_buffer *head)
1347  {  {
1348          const char *data = head->write_buf;          char *data = head->write_buf;
1349          bool is_delete = 0;          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);
1350          if (!isRoot()) return -EPERM;          if (!strcmp(data, "manage_by_non_root")) {
1351          if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {                  ccs_manage_by_non_root = !is_delete;
1352                  data += KEYWORD_DELETE_LEN;                  return 0;
                 is_delete = 1;  
         }  
         return AddManagerEntry(data, is_delete);  
 }  
   
 static int ReadManagerPolicy(struct io_buffer *head)  
 {  
         struct list1_head *pos;  
         if (head->read_eof) return 0;  
         if (!isRoot()) return -EPERM;  
         list1_for_each_cookie(pos, head->read_var2, &policy_manager_list) {  
                 struct policy_manager_entry *ptr;  
                 ptr = list1_entry(pos, struct policy_manager_entry, list);  
                 if (ptr->is_deleted) continue;  
                 if (io_printf(head, "%s\n", ptr->manager->name)) return 0;  
1353          }          }
1354          head->read_eof = 1;          return ccs_update_manager_entry(data, is_delete);
1355    }
1356    
1357    /**
1358     * ccs_read_manager_policy - Read manager policy.
1359     *
1360     * @head: Pointer to "struct ccs_io_buffer".
1361     *
1362     * Returns 0.
1363     *
1364     * Caller holds ccs_read_lock().
1365     */
1366    static int ccs_read_manager_policy(struct ccs_io_buffer *head)
1367    {
1368            struct list_head *pos;
1369            ccs_check_read_lock();
1370            if (head->read_eof)
1371                    return 0;
1372            list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {
1373                    struct ccs_policy_manager_entry *ptr;
1374                    ptr = list_entry(pos, struct ccs_policy_manager_entry, list);
1375                    if (ptr->is_deleted)
1376                            continue;
1377                    if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
1378                            return 0;
1379            }
1380            head->read_eof = true;
1381          return 0;          return 0;
1382  }  }
1383    
1384  /* Check whether the current process is a policy manager. */  /**
1385  static int IsPolicyManager(void)   * ccs_is_policy_manager - Check whether the current process is a policy manager.
1386     *
1387     * Returns true if the current process is permitted to modify policy
1388     * via /proc/ccs/ interface.
1389     *
1390     * Caller holds ccs_read_lock().
1391     */
1392    static bool ccs_is_policy_manager(void)
1393  {  {
1394          struct policy_manager_entry *ptr;          struct ccs_policy_manager_entry *ptr;
1395          const char *exe;          const char *exe;
1396          const struct path_info *domainname = current->domain_info->domainname;          struct task_struct *task = current;
1397          bool found = 0;          const struct ccs_path_info *domainname
1398          if (!sbin_init_started) return 1;                  = ccs_current_domain()->domainname;
1399          list1_for_each_entry(ptr, &policy_manager_list, list) {          bool found = false;
1400                  if (!ptr->is_deleted && ptr->is_domain && !pathcmp(domainname, ptr->manager)) return 1;          ccs_check_read_lock();
1401          }          if (!ccs_policy_loaded)
1402          if ((exe = GetEXE()) == NULL) return 0;                  return true;
1403          list1_for_each_entry(ptr, &policy_manager_list, list) {          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)
1404                  if (!ptr->is_deleted && !ptr->is_domain && !strcmp(exe, ptr->manager->name)) {                  return true;
1405                          found = 1;          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
1406                    return false;
1407            list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
1408                    if (!ptr->is_deleted && ptr->is_domain
1409                        && !ccs_pathcmp(domainname, ptr->manager)) {
1410                            /* Set manager flag. */
1411                            task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;
1412                            return true;
1413                    }
1414            }
1415            exe = ccs_get_exe();
1416            if (!exe)
1417                    return false;
1418            list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {
1419                    if (!ptr->is_deleted && !ptr->is_domain
1420                        && !strcmp(exe, ptr->manager->name)) {
1421                            found = true;
1422                            /* Set manager flag. */
1423                            task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;
1424                          break;                          break;
1425                  }                  }
1426          }          }
1427          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
1428                  static pid_t last_pid = 0;                  static pid_t ccs_last_pid;
1429                  const pid_t pid = current->pid;                  const pid_t pid = current->pid;
1430                  if (last_pid != pid) {                  if (ccs_last_pid != pid) {
1431                          printk("%s ( %s ) is not permitted to update policies.\n", domainname->name, exe);                          printk(KERN_WARNING "%s ( %s ) is not permitted to "
1432                          last_pid = pid;                                 "update policies.\n", domainname->name, exe);
1433                            ccs_last_pid = pid;
1434                  }                  }
1435          }          }
1436          ccs_free(exe);          kfree(exe);
1437          return found;          return found;
1438  }  }
1439    
1440  #ifdef CONFIG_TOMOYO  /**
1441     * ccs_find_condition_part - Find condition part from the statement.
1442  /*************************  DOMAIN POLICY HANDLER  *************************/   *
1443     * @data: String to parse.
1444  static char *FindConditionPart(char *data)   *
1445     * Returns pointer to the condition part if it was found in the statement,
1446     * NULL otherwise.
1447     */
1448    static char *ccs_find_condition_part(char *data)
1449  {  {
1450          char *cp = strstr(data, " if "), *cp2;          char *cp = strstr(data, " if ");
1451          if (cp) {          if (cp) {
1452                  while ((cp2 = strstr(cp + 3, " if ")) != NULL) cp = cp2;                  while (1) {
1453                            char *cp2 = strstr(cp + 3, " if ");
1454                            if (!cp2)
1455                                    break;
1456                            cp = cp2;
1457                    }
1458                  *cp++ = '\0';                  *cp++ = '\0';
1459            } else {
1460                    cp = strstr(data, " ; set ");
1461                    if (cp)
1462                            *cp++ = '\0';
1463          }          }
1464          return cp;          return cp;
1465  }  }
1466    
1467  static int AddDomainPolicy(struct io_buffer *head)  /**
1468     * ccs_is_select_one - Parse select command.
1469     *
1470     * @head: Pointer to "struct ccs_io_buffer".
1471     * @data: String to parse.
1472     *
1473     * Returns true on success, false otherwise.
1474     *
1475     * Caller holds ccs_read_lock().
1476     */
1477    static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)
1478    {
1479            unsigned int pid;
1480            struct ccs_domain_info *domain = NULL;
1481            ccs_check_read_lock();
1482            if (!strcmp(data, "allow_execute")) {
1483                    head->read_execute_only = true;
1484                    return true;
1485            }
1486            if (sscanf(data, "pid=%u", &pid) == 1) {
1487                    struct task_struct *p;
1488                    /***** CRITICAL SECTION START *****/
1489                    read_lock(&tasklist_lock);
1490                    p = find_task_by_pid(pid);
1491                    if (p)
1492                            domain = ccs_task_domain(p);
1493                    read_unlock(&tasklist_lock);
1494                    /***** CRITICAL SECTION END *****/
1495            } else if (!strncmp(data, "domain=", 7)) {
1496                    if (ccs_is_domain_def(data + 7))
1497                            domain = ccs_find_domain(data + 7);
1498            } else
1499                    return false;
1500            head->write_var1 = domain;
1501            /* Accessing read_buf is safe because head->io_sem is held. */
1502            if (!head->read_buf)
1503                    return true; /* Do nothing if open(O_WRONLY). */
1504            head->read_avail = 0;
1505            ccs_io_printf(head, "# select %s\n", data);
1506            head->read_single_domain = true;
1507            head->read_eof = !domain;
1508            if (domain) {
1509                    struct ccs_domain_info *d;
1510                    head->read_var1 = NULL;
1511                    list_for_each_entry_rcu(d, &ccs_domain_list, list) {
1512                            if (d == domain)
1513                                    break;
1514                            head->read_var1 = &d->list;
1515                    }
1516                    head->read_var2 = NULL;
1517                    head->read_bit = 0;
1518                    head->read_step = 0;
1519                    if (domain->is_deleted)
1520                            ccs_io_printf(head, "# This is a deleted domain.\n");
1521            }
1522            return true;
1523    }
1524    
1525    /**
1526     * ccs_write_domain_policy - Write domain policy.
1527     *
1528     * @head: Pointer to "struct ccs_io_buffer".
1529     *
1530     * Returns 0 on success, negative value otherwise.
1531     */
1532    static int ccs_write_domain_policy(struct ccs_io_buffer *head)
1533  {  {
1534          char *data = head->write_buf;          char *data = head->write_buf;
1535          struct domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->write_var1;
1536          bool is_delete = 0, is_select = 0, is_undelete = 0;          bool is_delete = false;
1537            bool is_select = false;
1538          unsigned int profile;          unsigned int profile;
1539          const struct condition_list *cond = NULL;          struct ccs_condition *cond = NULL;
1540          char *cp;                char *cp;
1541          if (!isRoot()) return -EPERM;          int error;
1542          if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_DELETE))
1543                  data += KEYWORD_DELETE_LEN;                  is_delete = true;
1544                  is_delete = 1;          else if (ccs_str_starts(&data, KEYWORD_SELECT))
1545          } else if (strncmp(data, KEYWORD_SELECT, KEYWORD_SELECT_LEN) == 0) {                  is_select = true;
1546                  data += KEYWORD_SELECT_LEN;          if (is_select && ccs_is_select_one(head, data))
1547                  is_select = 1;                  return 0;
1548          } else if (strncmp(data, KEYWORD_UNDELETE, KEYWORD_UNDELETE_LEN) == 0) {          /* Don't allow updating policies by non manager programs. */
1549                  data += KEYWORD_UNDELETE_LEN;          if (!ccs_is_policy_manager())
1550                  is_undelete = 1;                  return -EPERM;
1551          }          if (ccs_is_domain_def(data)) {
1552          UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);                  domain = NULL;
1553          if (IsDomainDef(data)) {                  if (is_delete)
1554                  if (is_delete) {                          ccs_delete_domain(data);
1555                          DeleteDomain(data);                  else if (is_select)
1556                          domain = NULL;                          domain = ccs_find_domain(data);
1557                  } else if (is_select) {                  else
1558                          domain = FindDomain(data);                          domain = ccs_find_or_assign_new_domain(data, 0);
                 } else if (is_undelete) {  
                         domain = UndeleteDomain(data);  
                 } else {  
                         domain = FindOrAssignNewDomain(data, 0);  
                 }  
1559                  head->write_var1 = domain;                  head->write_var1 = domain;
1560                  return 0;                  return 0;
1561          }          }
1562          if (!domain) return -EINVAL;          if (!domain)
1563                    return -EINVAL;
1564    
1565          if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1 && profile < MAX_PROFILES) {          if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1
1566                  if (profile_ptr[profile] || !sbin_init_started) domain->profile = (u8) profile;              && profile < MAX_PROFILES) {
1567                    if (ccs_profile_ptr[profile] || !ccs_policy_loaded)
1568                            domain->profile = (u8) profile;
1569                    return 0;
1570            }
1571            if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1572                    domain->ignore_global_allow_read = !is_delete;
1573                    return 0;
1574            }
1575            if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
1576                    domain->ignore_global_allow_env = !is_delete;
1577                  return 0;                  return 0;
1578          }          }
1579          cp = FindConditionPart(data);          cp = ccs_find_condition_part(data);
1580          if (cp && (cond = FindOrAssignNewCondition(cp)) == NULL) return -EINVAL;          if (cp) {
1581          if (strncmp(data, KEYWORD_ALLOW_CAPABILITY, KEYWORD_ALLOW_CAPABILITY_LEN) == 0) {                  cond = ccs_get_condition(cp);
1582                  return AddCapabilityPolicy(data + KEYWORD_ALLOW_CAPABILITY_LEN, domain, cond, is_delete);                  if (!cond)
1583          } else if (strncmp(data, KEYWORD_ALLOW_NETWORK, KEYWORD_ALLOW_NETWORK_LEN) == 0) {                          return -EINVAL;
1584                  return AddNetworkPolicy(data + KEYWORD_ALLOW_NETWORK_LEN, domain, cond, is_delete);          }
1585          } else if (strncmp(data, KEYWORD_ALLOW_SIGNAL, KEYWORD_ALLOW_SIGNAL_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_ALLOW_CAPABILITY))
1586                  return AddSignalPolicy(data + KEYWORD_ALLOW_SIGNAL_LEN, domain, cond, is_delete);                  error = ccs_write_capability_policy(data, domain, cond,
1587          } else if (strncmp(data, KEYWORD_ALLOW_ARGV0, KEYWORD_ALLOW_ARGV0_LEN) == 0) {                                                      is_delete);
1588                  return AddArgv0Policy(data + KEYWORD_ALLOW_ARGV0_LEN, domain, cond, is_delete);          else if (ccs_str_starts(&data, KEYWORD_ALLOW_NETWORK))
1589          } else if (strncmp(data, KEYWORD_ALLOW_ENV, KEYWORD_ALLOW_ENV_LEN) == 0) {                  error = ccs_write_network_policy(data, domain, cond, is_delete);
1590                  return AddEnvPolicy(data + KEYWORD_ALLOW_ENV_LEN, domain, cond, is_delete);          else if (ccs_str_starts(&data, KEYWORD_ALLOW_SIGNAL))
1591                    error = ccs_write_signal_policy(data, domain, cond, is_delete);
1592            else if (ccs_str_starts(&data, KEYWORD_ALLOW_ARGV0))
1593                    error = ccs_write_argv0_policy(data, domain, cond, is_delete);
1594            else if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))
1595                    error = ccs_write_env_policy(data, domain, cond, is_delete);
1596            else if (ccs_str_starts(&data, KEYWORD_ALLOW_IOCTL))
1597                    error = ccs_write_ioctl_policy(data, domain, cond, is_delete);
1598            else if (ccs_str_starts(&data, KEYWORD_ALLOW_MOUNT))
1599                    error = ccs_write_mount_policy(data, domain, cond, is_delete);
1600            else if (ccs_str_starts(&data, KEYWORD_ALLOW_UNMOUNT))
1601                    error = ccs_write_umount_policy(data, domain, cond, is_delete);
1602            else if (ccs_str_starts(&data, KEYWORD_ALLOW_CHROOT))
1603                    error = ccs_write_chroot_policy(data, domain, cond, is_delete);
1604            else if (ccs_str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT))
1605                    error = ccs_write_pivot_root_policy(data, domain, cond,
1606                                                        is_delete);
1607            else
1608                    error = ccs_write_file_policy(data, domain, cond, is_delete);
1609            if (cond)
1610                    ccs_put_condition(cond);
1611            return error;
1612    }
1613    
1614    /**
1615     * ccs_print_single_path_acl - Print a single path ACL entry.
1616     *
1617     * @head: Pointer to "struct ccs_io_buffer".
1618     * @ptr:  Pointer to "struct ccs_single_path_acl_record".
1619     * @cond: Pointer to "struct ccs_condition". May be NULL.
1620     *
1621     * Returns true on success, false otherwise.
1622     */
1623    static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,
1624                                          struct ccs_single_path_acl_record *ptr,
1625                                          const struct ccs_condition *cond)
1626    {
1627            int pos;
1628            u8 bit;
1629            const char *atmark = "";
1630            const char *filename;
1631            const u16 perm = ptr->perm;
1632            if (ptr->u_is_group) {
1633                    atmark = "@";
1634                    filename = ptr->u.group->group_name->name;
1635          } else {          } else {
1636                  return AddFilePolicy(data, domain, cond, is_delete);                  filename = ptr->u.filename->name;
1637          }          }
1638          return -EINVAL;          for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {
1639                    const char *msg;
1640                    if (!(perm & (1 << bit)))
1641                            continue;
1642                    if (head->read_execute_only && bit != TYPE_EXECUTE_ACL)
1643                            continue;
1644                    /* Print "read/write" instead of "read" and "write". */
1645                    if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)
1646                        && (perm & (1 << TYPE_READ_WRITE_ACL)))
1647                            continue;
1648                    msg = ccs_sp2keyword(bit);
1649                    pos = head->read_avail;
1650                    if (!ccs_io_printf(head, "allow_%s %s%s", msg,
1651                                       atmark, filename) ||
1652                        !ccs_print_condition(head, cond))
1653                            goto out;
1654            }
1655            head->read_bit = 0;
1656            return true;
1657     out:
1658            head->read_bit = bit;
1659            head->read_avail = pos;
1660            return false;
1661    }
1662    
1663    /**
1664     * ccs_print_mkdev_acl - Print a mkdev ACL entry.
1665     *
1666     * @head: Pointer to "struct ccs_io_buffer".
1667     * @ptr:  Pointer to "struct ccs_mkdev_acl_record".
1668     * @cond: Pointer to "struct ccs_condition". May be NULL.
1669     *
1670     * Returns true on success, false otherwise.
1671     */
1672    static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,
1673                                    struct ccs_mkdev_acl_record *ptr,
1674                                    const struct ccs_condition *cond)
1675    {
1676            int pos;
1677            u8 bit;
1678            const char *atmark = "";
1679            const char *filename;
1680            const u16 perm = ptr->perm;
1681            if (ptr->u_is_group) {
1682                    atmark = "@";
1683                    filename = ptr->u.group->group_name->name;
1684            } else {
1685                    filename = ptr->u.filename->name;
1686            }
1687            for (bit = head->read_bit; bit < MAX_MKDEV_OPERATION; bit++) {
1688                    const char *msg;
1689                    const unsigned int min_major = ptr->min_major;
1690                    const unsigned int max_major = ptr->max_major;
1691                    const unsigned int min_minor = ptr->min_minor;
1692                    const unsigned int max_minor = ptr->max_minor;
1693                    if (!(perm & (1 << bit)))
1694                            continue;
1695                    msg = ccs_mkdev2keyword(bit);
1696                    pos = head->read_avail;
1697                    if (!ccs_io_printf(head, "allow_%s %s%s %u", msg, atmark,
1698                                       filename, min_major) ||
1699                        (min_major != max_major &&
1700                         !ccs_io_printf(head, "-%u", max_major)) ||
1701                        !ccs_io_printf(head, " %u", min_minor) ||
1702                        (min_minor != max_minor &&
1703                         !ccs_io_printf(head, "-%u", max_minor)) ||
1704                        !ccs_print_condition(head, cond))
1705                            goto out;
1706            }
1707            head->read_bit = 0;
1708            return true;
1709     out:
1710            head->read_bit = bit;
1711            head->read_avail = pos;
1712            return false;
1713    }
1714    
1715    /**
1716     * ccs_print_double_path_acl - Print a double path ACL entry.
1717     *
1718     * @head: Pointer to "struct ccs_io_buffer".
1719     * @ptr:  Pointer to "struct ccs_double_path_acl_record".
1720     * @cond: Pointer to "struct ccs_condition". May be NULL.
1721     *
1722     * Returns true on success, false otherwise.
1723     */
1724    static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,
1725                                          struct ccs_double_path_acl_record *ptr,
1726                                          const struct ccs_condition *cond)
1727    {
1728            int pos;
1729            const char *atmark1 = "";
1730            const char *atmark2 = "";
1731            const char *filename1;
1732            const char *filename2;
1733            const u8 perm = ptr->perm;
1734            u8 bit;
1735            if (ptr->u1_is_group) {
1736                    atmark1 = "@";
1737                    filename1 = ptr->u1.group1->group_name->name;
1738            } else {
1739                    filename1 = ptr->u1.filename1->name;
1740            }
1741            if (ptr->u2_is_group) {
1742                    atmark2 = "@";
1743                    filename2 = ptr->u2.group2->group_name->name;
1744            } else {
1745                    filename2 = ptr->u2.filename2->name;
1746            }
1747            for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {
1748                    const char *msg;
1749                    if (!(perm & (1 << bit)))
1750                            continue;
1751                    msg = ccs_dp2keyword(bit);
1752                    pos = head->read_avail;
1753                    if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg,
1754                                       atmark1, filename1, atmark2, filename2) ||
1755                        !ccs_print_condition(head, cond))
1756                            goto out;
1757            }
1758            head->read_bit = 0;
1759            return true;
1760     out:
1761            head->read_bit = bit;
1762            head->read_avail = pos;
1763            return false;
1764    }
1765    
1766    /**
1767     * ccs_print_ioctl_acl - Print an ioctl ACL entry.
1768     *
1769     * @head: Pointer to "struct ccs_io_buffer".
1770     * @ptr:  Pointer to "struct ccs_ioctl_acl_record".
1771     * @cond: Pointer to "struct ccs_condition". May be NULL.
1772     *
1773     * Returns true on success, false otherwise.
1774     */
1775    static bool ccs_print_ioctl_acl(struct ccs_io_buffer *head,
1776                                    struct ccs_ioctl_acl_record *ptr,
1777                                    const struct ccs_condition *cond)
1778    {
1779            int pos = head->read_avail;
1780            const char *atmark = "";
1781            const char *filename;
1782            const unsigned int cmd_min = ptr->cmd_min;
1783            const unsigned int cmd_max = ptr->cmd_max;
1784            if (ptr->u_is_group) {
1785                    atmark = "@";
1786                    filename = ptr->u.group->group_name->name;
1787            } else {
1788                    filename = ptr->u.filename->name;
1789            }
1790            if (!ccs_io_printf(head, KEYWORD_ALLOW_IOCTL "%s%s ", atmark, filename))
1791                    goto out;
1792            if (!ccs_io_printf(head, "%u", cmd_min))
1793                    goto out;
1794            if (cmd_min != cmd_max && !ccs_io_printf(head, "-%u", cmd_max))
1795                    goto out;
1796            if (!ccs_print_condition(head, cond))
1797                    goto out;
1798            return true;
1799     out:
1800            head->read_avail = pos;
1801            return false;
1802    }
1803    
1804    /**
1805     * ccs_print_argv0_acl - Print an argv[0] ACL entry.
1806     *
1807     * @head: Pointer to "struct ccs_io_buffer".
1808     * @ptr:  Pointer to "struct ccs_argv0_acl_record".
1809     * @cond: Pointer to "struct ccs_condition". May be NULL.
1810     *
1811     * Returns true on success, false otherwise.
1812     */
1813    static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,
1814                                    struct ccs_argv0_acl_record *ptr,
1815                                    const struct ccs_condition *cond)
1816    {
1817            int pos = head->read_avail;
1818            if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
1819                               ptr->filename->name, ptr->argv0->name))
1820                    goto out;
1821            if (!ccs_print_condition(head, cond))
1822                    goto out;
1823            return true;
1824     out:
1825            head->read_avail = pos;
1826            return false;
1827  }  }
1828    
1829  static int ReadDomainPolicy(struct io_buffer *head)  /**
1830     * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1831     *
1832     * @head: Pointer to "struct ccs_io_buffer".
1833     * @ptr:  Pointer to "struct ccs_env_acl_record".
1834     * @cond: Pointer to "struct ccs_condition". May be NULL.
1835     *
1836     * Returns true on success, false otherwise.
1837     */
1838    static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1839                                  struct ccs_env_acl_record *ptr,
1840                                  const struct ccs_condition *cond)
1841  {  {
1842          struct list1_head *dpos;          int pos = head->read_avail;
1843          struct list1_head *apos;          if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))
1844          if (head->read_eof) return 0;                  goto out;
1845          if (head->read_step == 0) {          if (!ccs_print_condition(head, cond))
1846                  if (!isRoot()) return -EPERM;                  goto out;
1847                  head->read_step = 1;          return true;
1848     out:
1849            head->read_avail = pos;
1850            return false;
1851    }
1852    
1853    /**
1854     * ccs_print_capability_acl - Print a capability ACL entry.
1855     *
1856     * @head: Pointer to "struct ccs_io_buffer".
1857     * @ptr:  Pointer to "struct ccs_capability_acl_record".
1858     * @cond: Pointer to "struct ccs_condition". May be NULL.
1859     *
1860     * Returns true on success, false otherwise.
1861     */
1862    static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1863                                         struct ccs_capability_acl_record *ptr,
1864                                         const struct ccs_condition *cond)
1865    {
1866            int pos = head->read_avail;
1867            if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",
1868                               ccs_cap2keyword(ptr->operation)))
1869                    goto out;
1870            if (!ccs_print_condition(head, cond))
1871                    goto out;
1872            return true;
1873     out:
1874            head->read_avail = pos;
1875            return false;
1876    }
1877    
1878    /**
1879     * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.
1880     *
1881     * @head: Pointer to "struct ccs_io_buffer".
1882     * @ptr:  Pointer to "struct ccs_ip_network_acl_record".
1883     *
1884     * Returns true on success, false otherwise.
1885     */
1886    static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,
1887                                     struct ccs_ip_network_acl_record *ptr)
1888    {
1889            const u32 min_address = ptr->u.ipv4.min;
1890            const u32 max_address = ptr->u.ipv4.max;
1891            if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1892                    return false;
1893            if (min_address != max_address
1894                && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1895                    return false;
1896            return true;
1897    }
1898    
1899    /**
1900     * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.
1901     *
1902     * @head: Pointer to "struct ccs_io_buffer".
1903     * @ptr:  Pointer to "struct ccs_ip_network_acl_record".
1904     *
1905     * Returns true on success, false otherwise.
1906     */
1907    static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,
1908                                     struct ccs_ip_network_acl_record *ptr)
1909    {
1910            char buf[64];
1911            const struct in6_addr *min_address = ptr->u.ipv6.min;
1912            const struct in6_addr *max_address = ptr->u.ipv6.max;
1913            ccs_print_ipv6(buf, sizeof(buf), min_address);
1914            if (!ccs_io_printf(head, "%s", buf))
1915                    return false;
1916            if (min_address != max_address) {
1917                    ccs_print_ipv6(buf, sizeof(buf), max_address);
1918                    if (!ccs_io_printf(head, "-%s", buf))
1919                            return false;
1920          }          }
1921          list1_for_each_cookie(dpos, head->read_var1, &domain_list) {          return true;
1922                  struct domain_info *domain;  }
1923                  domain = list1_entry(dpos, struct domain_info, list);  
1924                  if (head->read_step != 1) goto acl_loop;  /**
1925                  if (domain->is_deleted) continue;   * ccs_print_port_entry - Print port number of a network ACL entry.
1926                  if (io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n%s\n", domain->domainname->name, domain->profile, domain->quota_warned ? "quota_exceeded\n" : "")) return 0;   *
1927                  head->read_step = 2;   * @head: Pointer to "struct ccs_io_buffer".
1928          acl_loop: ;   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".
1929                  if (head->read_step == 3) goto tail_mark;   *
1930                  list1_for_each_cookie(apos, head->read_var2, &domain->acl_info_list) {   * Returns true on success, false otherwise.
1931                          struct acl_info *ptr;   */
1932                          int pos;  static bool ccs_print_port_entry(struct ccs_io_buffer *head,
1933                          u8 acl_type;                                   struct ccs_ip_network_acl_record *ptr)
1934                          ptr = list1_entry(apos, struct acl_info, list);  {
1935                          if (ptr->is_deleted) continue;          const u16 min_port = ptr->min_port;
1936                          pos = head->read_avail;          const u16 max_port = ptr->max_port;
1937                          acl_type = ptr->type;          if (!ccs_io_printf(head, " %u", min_port))
1938                          if (acl_type == TYPE_FILE_ACL) {                  return false;
1939                                  struct file_acl_record *ptr2 = container_of(ptr, struct file_acl_record, head);          if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port))
1940                                  const unsigned char b = ptr2->u_is_group;                  return false;
1941                                  if (io_printf(head, "%d %s%s", ptr2->perm,          return true;
1942                                                b ? "@" : "",  }
1943                                                b ? ptr2->u.group->group_name->name : ptr2->u.filename->name)) goto print_acl_rollback;  
1944                          } else if (acl_type == TYPE_ARGV0_ACL) {  /**
1945                                  struct argv0_acl_record *ptr2 = container_of(ptr, struct argv0_acl_record, head);   * ccs_print_network_acl - Print a network ACL entry.
1946                                  if (io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",   *
1947                                                ptr2->filename->name, ptr2->argv0->name)) goto print_acl_rollback;   * @head: Pointer to "struct ccs_io_buffer".
1948                          } else if (acl_type == TYPE_ENV_ACL) {   * @ptr:  Pointer to "struct ccs_ip_network_acl_record".
1949                                  struct env_acl_record *ptr2 = container_of(ptr, struct env_acl_record, head);   * @cond: Pointer to "struct ccs_condition". May be NULL.
1950                                  if (io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr2->env->name)) goto print_acl_rollback;   *
1951                          } else if (acl_type == TYPE_CAPABILITY_ACL) {   * Returns true on success, false otherwise.
1952                                  struct capability_acl_record *ptr2 = container_of(ptr, struct capability_acl_record, head);   */
1953                                  if (io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s", capability2keyword(ptr2->capability))) goto print_acl_rollback;  static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1954                          } else if (acl_type == TYPE_IP_NETWORK_ACL) {                                    struct ccs_ip_network_acl_record *ptr,
1955                                  struct ip_network_acl_record *ptr2 = container_of(ptr, struct ip_network_acl_record, head);                                    const struct ccs_condition *cond)
1956                                  if (io_printf(head, KEYWORD_ALLOW_NETWORK "%s ", network2keyword(ptr2->operation_type))) goto print_acl_rollback;  {
1957                                  switch (ptr2->record_type) {          int pos = head->read_avail;
1958                                  case IP_RECORD_TYPE_ADDRESS_GROUP:          if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",
1959                                          if (io_printf(head, "@%s", ptr2->u.group->group_name->name)) goto print_acl_rollback;                             ccs_net2keyword(ptr->operation_type)))
1960                                          break;                  goto out;
1961                                  case IP_RECORD_TYPE_IPv4:          switch (ptr->record_type) {
1962                                          {          case IP_RECORD_TYPE_ADDRESS_GROUP:
1963                                                  const u32 min_address = ptr2->u.ipv4.min, max_address = ptr2->u.ipv4.max;                  if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name))
1964                                                  if (io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address))) goto print_acl_rollback;                          goto out;
1965                                                  if (min_address != max_address && io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address))) goto print_acl_rollback;                  break;
1966                                          }          case IP_RECORD_TYPE_IPv4:
1967                                          break;                  if (!ccs_print_ipv4_entry(head, ptr))
1968                                  case IP_RECORD_TYPE_IPv6:                          goto out;
1969                                          {                  break;
1970                                                  char buf[64];          case IP_RECORD_TYPE_IPv6:
1971                                                  const struct in6_addr *min_address = ptr2->u.ipv6.min, *max_address = ptr2->u.ipv6.max;                  if (!ccs_print_ipv6_entry(head, ptr))
1972                                                  print_ipv6(buf, sizeof(buf), min_address);                          goto out;
1973                                                  if (io_printf(head, "%s", buf)) goto print_acl_rollback;                  break;
1974                                                  if (min_address != max_address) {          }
1975                                                          print_ipv6(buf, sizeof(buf), max_address);          if (!ccs_print_port_entry(head, ptr))
1976                                                          if (io_printf(head, "-%s", buf)) goto print_acl_rollback;                  goto out;
1977                                                  }          if (!ccs_print_condition(head, cond))
1978                                          }                  goto out;
1979                                          break;          return true;
1980                                  }   out:
1981                                  {          head->read_avail = pos;
1982                                          const u16 min_port = ptr2->min_port, max_port = ptr2->max_port;          return false;
1983                                          if (io_printf(head, " %u", min_port)) goto print_acl_rollback;  }
1984                                          if (min_port != max_port && io_printf(head, "-%u", max_port)) goto print_acl_rollback;  
1985                                  }  /**
1986                          } else if (acl_type == TYPE_SIGNAL_ACL) {   * ccs_print_signal_acl - Print a signal ACL entry.
1987                                  struct signal_acl_record *ptr2 = container_of(ptr, struct signal_acl_record, head);   *
1988                                  if (io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s", ptr2->sig, ptr2->domainname->name)) goto print_acl_rollback;   * @head: Pointer to "struct ccs_io_buffer".
1989                          } else {   * @ptr:  Pointer to "struct signale_acl_record".
1990                                  const char *keyword = acltype2keyword(acl_type);   * @cond: Pointer to "struct ccs_condition". May be NULL.
1991                                  if (!keyword) continue;   *
1992                                  if (acltype2paths(acl_type) == 2) {   * Returns true on success, false otherwise.
1993                                          struct double_acl_record *ptr2 = container_of(ptr, struct double_acl_record, head);   */
1994                                          const bool b0 = ptr2->u1_is_group, b1 = ptr2->u2_is_group;  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1995                                          if (io_printf(head, "allow_%s %s%s %s%s", keyword,                                   struct ccs_signal_acl_record *ptr,
1996                                                        b0 ? "@" : "", b0 ? ptr2->u1.group1->group_name->name : ptr2->u1.filename1->name,                                   const struct ccs_condition *cond)
1997                                                        b1 ? "@" : "", b1 ? ptr2->u2.group2->group_name->name : ptr2->u2.filename2->name)) goto print_acl_rollback;  {
1998                                  } else {          int pos = head->read_avail;
1999                                          struct single_acl_record *ptr2 = container_of(ptr, struct single_acl_record, head);          if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",
2000                                          const bool b = ptr2->u_is_group;                             ptr->sig, ptr->domainname->name))
2001                                          if (io_printf(head, "allow_%s %s%s", keyword,                  goto out;
2002                                                        b ? "@" : "", b ? ptr2->u.group->group_name->name : ptr2->u.filename->name)) goto print_acl_rollback;          if (!ccs_print_condition(head, cond))
2003                                  }                  goto out;
2004                          }          return true;
2005                          if (DumpCondition(head, ptr->cond)) {   out:
2006                          print_acl_rollback: ;          head->read_avail = pos;
2007                          head->read_avail = pos;          return false;
2008    }
2009    
2010    /**
2011     * ccs_print_execute_handler_record - Print an execute handler ACL entry.
2012     *
2013     * @head:    Pointer to "struct ccs_io_buffer".
2014     * @keyword: Name of the keyword.
2015     * @ptr:     Pointer to "struct ccs_execute_handler_record".
2016     *
2017     * Returns true on success, false otherwise.
2018     */
2019    static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,
2020                                                 const char *keyword,
2021                                                 struct ccs_execute_handler_record *
2022                                                 ptr)
2023    {
2024            return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
2025    }
2026    
2027    /**
2028     * ccs_print_mount_acl - Print a mount ACL entry.
2029     *
2030     * @head: Pointer to "struct ccs_io_buffer".
2031     * @ptr:  Pointer to "struct ccs_mount_acl_record".
2032     * @cond: Pointer to "struct ccs_condition". May be NULL.
2033     *
2034     * Returns true on success, false otherwise.
2035     */
2036    static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
2037                                    struct ccs_mount_acl_record *ptr,
2038                                    const struct ccs_condition *cond)
2039    {
2040            int pos = head->read_avail;
2041            if (!ccs_io_printf(head, KEYWORD_ALLOW_MOUNT "%s %s %s 0x%lX\n",
2042                               ptr->dev_name->name, ptr->dir_name->name,
2043                               ptr->fs_type->name, ptr->flags))
2044                    goto out;
2045            if (!ccs_print_condition(head, cond))
2046                    goto out;
2047            return true;
2048     out:
2049            head->read_avail = pos;
2050            return false;
2051    }
2052    
2053    /**
2054     * ccs_print_umount_acl - Print a mount ACL entry.
2055     *
2056     * @head: Pointer to "struct ccs_io_buffer".
2057     * @ptr:  Pointer to "struct ccs_umount_acl_record".
2058     * @cond: Pointer to "struct ccs_condition". May be NULL.
2059     *
2060     * Returns true on success, false otherwise.
2061     */
2062    static bool ccs_print_umount_acl(struct ccs_io_buffer *head,
2063                                     struct ccs_umount_acl_record *ptr,
2064                                     const struct ccs_condition *cond)
2065    {
2066            int pos = head->read_avail;
2067            if (!ccs_io_printf(head, KEYWORD_ALLOW_UNMOUNT "%s\n",
2068                               ptr->dir->name))
2069                    goto out;
2070            if (!ccs_print_condition(head, cond))
2071                    goto out;
2072            return true;
2073     out:
2074            head->read_avail = pos;
2075            return false;
2076    }
2077    
2078    /**
2079     * ccs_print_chroot_acl - Print a chroot ACL entry.
2080     *
2081     * @head: Pointer to "struct ccs_io_buffer".
2082     * @ptr:  Pointer to "struct ccs_chroot_acl_record".
2083     * @cond: Pointer to "struct ccs_condition". May be NULL.
2084     *
2085     * Returns true on success, false otherwise.
2086     */
2087    static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,
2088                                     struct ccs_chroot_acl_record *ptr,
2089                                     const struct ccs_condition *cond)
2090    {
2091            int pos = head->read_avail;
2092            if (!ccs_io_printf(head, KEYWORD_ALLOW_CHROOT "%s\n",
2093                               ptr->dir->name))
2094                    goto out;
2095            if (!ccs_print_condition(head, cond))
2096                    goto out;
2097            return true;
2098     out:
2099            head->read_avail = pos;
2100            return false;
2101    }
2102    
2103    /**
2104     * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.
2105     *
2106     * @head: Pointer to "struct ccs_io_buffer".
2107     * @ptr:  Pointer to "struct ccs_pivot_root_acl_record".
2108     * @cond: Pointer to "struct ccs_condition". May be NULL.
2109     *
2110     * Returns true on success, false otherwise.
2111     */
2112    static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,
2113                                         struct ccs_pivot_root_acl_record *ptr,
2114                                         const struct ccs_condition *cond)
2115    {
2116            int pos = head->read_avail;
2117            if (!ccs_io_printf(head, KEYWORD_ALLOW_PIVOT_ROOT "%s %s\n",
2118                               ptr->new_root->name, ptr->old_root->name))
2119                    goto out;
2120            if (!ccs_print_condition(head, cond))
2121                    goto out;
2122            return true;
2123     out:
2124            head->read_avail = pos;
2125            return false;
2126    }
2127    
2128    /**
2129     * ccs_print_entry - Print an ACL entry.
2130     *
2131     * @head: Pointer to "struct ccs_io_buffer".
2132     * @ptr:  Pointer to an ACL entry.
2133     *
2134     * Returns true on success, false otherwise.
2135     */
2136    static bool ccs_print_entry(struct ccs_io_buffer *head,
2137                                struct ccs_acl_info *ptr)
2138    {
2139            const struct ccs_condition *cond = ptr->cond;
2140            const u8 acl_type = ccs_acl_type2(ptr);
2141            if (acl_type & ACL_DELETED)
2142                    return true;
2143            if (acl_type == TYPE_SINGLE_PATH_ACL) {
2144                    struct ccs_single_path_acl_record *acl
2145                            = container_of(ptr, struct ccs_single_path_acl_record,
2146                                           head);
2147                    return ccs_print_single_path_acl(head, acl, cond);
2148            }
2149            if (acl_type == TYPE_EXECUTE_HANDLER) {
2150                    struct ccs_execute_handler_record *acl
2151                            = container_of(ptr, struct ccs_execute_handler_record,
2152                                           head);
2153                    const char *keyword = KEYWORD_EXECUTE_HANDLER;
2154                    return ccs_print_execute_handler_record(head, keyword, acl);
2155            }
2156            if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {
2157                    struct ccs_execute_handler_record *acl
2158                            = container_of(ptr, struct ccs_execute_handler_record,
2159                                           head);
2160                    const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;
2161                    return ccs_print_execute_handler_record(head, keyword, acl);
2162            }
2163            if (head->read_execute_only)
2164                    return true;
2165            if (acl_type == TYPE_MKDEV_ACL) {
2166                    struct ccs_mkdev_acl_record *acl
2167                            = container_of(ptr, struct ccs_mkdev_acl_record, head);
2168                    return ccs_print_mkdev_acl(head, acl, cond);
2169            }
2170            if (acl_type == TYPE_DOUBLE_PATH_ACL) {
2171                    struct ccs_double_path_acl_record *acl
2172                            = container_of(ptr, struct ccs_double_path_acl_record,
2173                                           head);
2174                    return ccs_print_double_path_acl(head, acl, cond);
2175            }
2176            if (acl_type == TYPE_IOCTL_ACL) {
2177                    struct ccs_ioctl_acl_record *acl
2178                            = container_of(ptr, struct ccs_ioctl_acl_record, head);
2179                    return ccs_print_ioctl_acl(head, acl, cond);
2180            }
2181            if (acl_type == TYPE_ARGV0_ACL) {
2182                    struct ccs_argv0_acl_record *acl
2183                            = container_of(ptr, struct ccs_argv0_acl_record, head);
2184                    return ccs_print_argv0_acl(head, acl, cond);
2185            }
2186            if (acl_type == TYPE_ENV_ACL) {
2187                    struct ccs_env_acl_record *acl
2188                            = container_of(ptr, struct ccs_env_acl_record, head);
2189                    return ccs_print_env_acl(head, acl, cond);
2190            }
2191            if (acl_type == TYPE_CAPABILITY_ACL) {
2192                    struct ccs_capability_acl_record *acl
2193                            = container_of(ptr, struct ccs_capability_acl_record,
2194                                           head);
2195                    return ccs_print_capability_acl(head, acl, cond);
2196            }
2197            if (acl_type == TYPE_IP_NETWORK_ACL) {
2198                    struct ccs_ip_network_acl_record *acl
2199                            = container_of(ptr, struct ccs_ip_network_acl_record,
2200                                           head);
2201                    return ccs_print_network_acl(head, acl, cond);
2202            }
2203            if (acl_type == TYPE_SIGNAL_ACL) {
2204                    struct ccs_signal_acl_record *acl
2205                            = container_of(ptr, struct ccs_signal_acl_record, head);
2206                    return ccs_print_signal_acl(head, acl, cond);
2207            }
2208            if (acl_type == TYPE_MOUNT_ACL) {
2209                    struct ccs_mount_acl_record *acl
2210                            = container_of(ptr, struct ccs_mount_acl_record, head);
2211                    return ccs_print_mount_acl(head, acl, cond);
2212            }
2213            if (acl_type == TYPE_UMOUNT_ACL) {
2214                    struct ccs_umount_acl_record *acl
2215                            = container_of(ptr, struct ccs_umount_acl_record, head);
2216                    return ccs_print_umount_acl(head, acl, cond);
2217            }
2218            if (acl_type == TYPE_CHROOT_ACL) {
2219                    struct ccs_chroot_acl_record *acl
2220                            = container_of(ptr, struct ccs_chroot_acl_record, head);
2221                    return ccs_print_chroot_acl(head, acl, cond);
2222            }
2223            if (acl_type == TYPE_PIVOT_ROOT_ACL) {
2224                    struct ccs_pivot_root_acl_record *acl
2225                            = container_of(ptr, struct ccs_pivot_root_acl_record,
2226                                           head);
2227                    return ccs_print_pivot_root_acl(head, acl, cond);
2228            }
2229            /* Workaround for gcc 3.2.2's inline bug. */
2230            if (acl_type & ACL_DELETED)
2231                    return true;
2232            BUG(); /* This must not happen. */
2233            return false;
2234    }
2235    
2236    /**
2237     * ccs_read_domain_policy - Read domain policy.
2238     *
2239     * @head: Pointer to "struct ccs_io_buffer".
2240     *
2241     * Returns 0.
2242     *
2243     * Caller holds ccs_read_lock().
2244     */
2245    static int ccs_read_domain_policy(struct ccs_io_buffer *head)
2246    {
2247            struct list_head *dpos;
2248            struct list_head *apos;
2249            ccs_check_read_lock();
2250            if (head->read_eof)
2251                    return 0;
2252            if (head->read_step == 0)
2253                    head->read_step = 1;
2254            list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
2255                    struct ccs_domain_info *domain;
2256                    const char *quota_exceeded = "";
2257                    const char *transition_failed = "";
2258                    const char *ignore_global_allow_read = "";
2259                    const char *ignore_global_allow_env = "";
2260                    domain = list_entry(dpos, struct ccs_domain_info, list);
2261                    if (head->read_step != 1)
2262                            goto acl_loop;
2263                    if (domain->is_deleted && !head->read_single_domain)
2264                            continue;
2265                    /* Print domainname and flags. */
2266                    if (domain->quota_warned)
2267                            quota_exceeded = "quota_exceeded\n";
2268                    if (domain->domain_transition_failed)
2269                            transition_failed = "transition_failed\n";
2270                    if (domain->ignore_global_allow_read)
2271                            ignore_global_allow_read
2272                                    = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
2273                    if (domain->ignore_global_allow_env)
2274                            ignore_global_allow_env
2275                                    = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
2276                    if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"
2277                                       "%s%s%s%s\n", domain->domainname->name,
2278                                       domain->profile, quota_exceeded,
2279                                       transition_failed,
2280                                       ignore_global_allow_read,
2281                                       ignore_global_allow_env))
2282                          return 0;                          return 0;
2283                          }                  head->read_step = 2;
2284     acl_loop:
2285                    if (head->read_step == 3)
2286                            goto tail_mark;
2287                    /* Print ACL entries in the domain. */
2288                    list_for_each_cookie(apos, head->read_var2,
2289                                         &domain->acl_info_list) {
2290                            struct ccs_acl_info *ptr
2291                                    = list_entry(apos, struct ccs_acl_info, list);
2292                            if (!ccs_print_entry(head, ptr))
2293                                    return 0;
2294                  }                  }
2295                  head->read_step = 3;                  head->read_step = 3;
2296          tail_mark: ;   tail_mark:
2297                  if (io_printf(head, "\n")) return 0;                  if (!ccs_io_printf(head, "\n"))
2298                            return 0;
2299                  head->read_step = 1;                  head->read_step = 1;
2300                    if (head->read_single_domain)
2301                            break;
2302          }          }
2303          head->read_eof = 1;          head->read_eof = true;
2304          return 0;          return 0;
2305  }  }
2306    
2307  #endif  /**
2308     * ccs_write_domain_profile - Assign profile for specified domain.
2309  static int UpdateDomainProfile(struct io_buffer *head)   *
2310     * @head: Pointer to "struct ccs_io_buffer".
2311     *
2312     * Returns 0 on success, -EINVAL otherwise.
2313     *
2314     * This is equivalent to doing
2315     *
2316     *     ( echo "select " $domainname; echo "use_profile " $profile ) |
2317     *     /usr/lib/ccs/loadpolicy -d
2318     *
2319     * Caller holds ccs_read_lock().
2320     */
2321    static int ccs_write_domain_profile(struct ccs_io_buffer *head)
2322  {  {
2323          char *data = head->write_buf;          char *data = head->write_buf;
2324          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
2325          struct domain_info *domain;          struct ccs_domain_info *domain;
2326          unsigned int profile;          unsigned int profile;
2327          if (!isRoot()) return -EPERM;          ccs_check_read_lock();
2328          if (!cp) return -EINVAL;          if (!cp)
2329                    return -EINVAL;
2330          *cp = '\0';          *cp = '\0';
         domain = FindDomain(cp + 1);  
2331          profile = simple_strtoul(data, NULL, 10);          profile = simple_strtoul(data, NULL, 10);
2332          if (domain && profile < MAX_PROFILES && (profile_ptr[profile] || !sbin_init_started)) domain->profile = (u8) profile;          if (profile >= MAX_PROFILES)
2333          UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);                  return -EINVAL;
2334            domain = ccs_find_domain(cp + 1);
2335            if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))
2336                    domain->profile = (u8) profile;
2337          return 0;          return 0;
2338  }  }
2339    
2340  static int ReadDomainProfile(struct io_buffer *head)  /**
2341     * ccs_read_domain_profile - Read only domainname and profile.
2342     *
2343     * @head: Pointer to "struct ccs_io_buffer".
2344     *
2345     * Returns list of profile number and domainname pairs.
2346     *
2347     * This is equivalent to doing
2348     *
2349     *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
2350     *     awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
2351     *     domainname = $0; } else if ( $1 == "use_profile" ) {
2352     *     print $2 " " domainname; domainname = ""; } } ; '
2353     *
2354     * Caller holds ccs_read_lock().
2355     */
2356    static int ccs_read_domain_profile(struct ccs_io_buffer *head)
2357  {  {
2358          struct list1_head *pos;          struct list_head *pos;
2359          if (head->read_eof) return 0;          ccs_check_read_lock();
2360          if (!isRoot()) return -EPERM;          if (head->read_eof)
2361          list1_for_each_cookie(pos, head->read_var1, &domain_list) {                  return 0;
2362                  struct domain_info *domain;          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
2363                  domain = list1_entry(pos, struct domain_info, list);                  struct ccs_domain_info *domain;
2364                  if (domain->is_deleted) continue;                  domain = list_entry(pos, struct ccs_domain_info, list);
2365                  if (io_printf(head, "%u %s\n", domain->profile, domain->domainname->name)) return 0;                  if (domain->is_deleted)
2366                            continue;
2367                    if (!ccs_io_printf(head, "%u %s\n", domain->profile,
2368                                       domain->domainname->name))
2369                            return 0;
2370          }          }
2371          head->read_eof = 1;          head->read_eof = true;
2372          return 0;          return 0;
2373  }  }
2374    
2375  static int WritePID(struct io_buffer *head)  /**
2376     * ccs_write_pid: Specify PID to obtain domainname.
2377     *
2378     * @head: Pointer to "struct ccs_io_buffer".
2379     *
2380     * Returns 0.
2381     */
2382    static int ccs_write_pid(struct ccs_io_buffer *head)
2383  {  {
2384          head->read_step = (int) simple_strtoul(head->write_buf, NULL, 10);          head->read_eof = false;
         head->read_eof = 0;  
2385          return 0;          return 0;
2386  }  }
2387    
2388  static int ReadPID(struct io_buffer *head)  /**
2389     * ccs_read_pid - Read information of a process.
2390     *
2391     * @head: Pointer to "struct ccs_io_buffer".
2392     *
2393     * Returns the domainname which the specified PID is in or
2394     * process information of the specified PID on success,
2395     * empty string otherwise.
2396     *
2397     * Caller holds ccs_read_lock().
2398     */
2399    static int ccs_read_pid(struct ccs_io_buffer *head)
2400  {  {
2401          if (head->read_avail == 0 && !head->read_eof) {          char *buf = head->write_buf;
2402                  const int pid = head->read_step;          bool task_info = false;
2403                  struct task_struct *p;          unsigned int pid;
2404                  struct domain_info *domain = NULL;          struct task_struct *p;
2405                  /***** CRITICAL SECTION START *****/          struct ccs_domain_info *domain = NULL;
2406                  read_lock(&tasklist_lock);          u32 ccs_flags = 0;
2407                  p = find_task_by_pid(pid);          ccs_check_read_lock();
2408                  if (p) domain = p->domain_info;          /* Accessing write_buf is safe because head->io_sem is held. */
2409                  read_unlock(&tasklist_lock);          if (!buf)
2410                  /***** CRITICAL SECTION END *****/                  goto done; /* Do nothing if open(O_RDONLY). */
2411                  if (domain) io_printf(head, "%d %u %s", pid, domain->profile, domain->domainname->name);          if (head->read_avail || head->read_eof)
2412                  head->read_eof = 1;                  goto done;
2413            head->read_eof = true;
2414            if (ccs_str_starts(&buf, "info "))
2415                    task_info = true;
2416            pid = (unsigned int) simple_strtoul(buf, NULL, 10);
2417            /***** CRITICAL SECTION START *****/
2418            read_lock(&tasklist_lock);
2419            p = find_task_by_pid(pid);
2420            if (p) {
2421                    domain = ccs_task_domain(p);
2422                    ccs_flags = p->ccs_flags;
2423          }          }
2424            read_unlock(&tasklist_lock);
2425            /***** CRITICAL SECTION END *****/
2426            if (!domain)
2427                    goto done;
2428            if (!task_info)
2429                    ccs_io_printf(head, "%u %u %s", pid, domain->profile,
2430                                  domain->domainname->name);
2431            else
2432                    ccs_io_printf(head, "%u manager=%s execute_handler=%s "
2433                                  "state[0]=%u state[1]=%u state[2]=%u", pid,
2434                                  ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?
2435                                  "yes" : "no",
2436                                  ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?
2437                                  "yes" : "no",
2438                                  (u8) (ccs_flags >> 24),
2439                                  (u8) (ccs_flags >> 16),
2440                                  (u8) (ccs_flags >> 8));
2441     done:
2442          return 0;          return 0;
2443  }  }
2444    
2445  /*************************  EXCEPTION POLICY HANDLER  *************************/  /**
2446     * ccs_write_exception_policy - Write exception policy.
2447  #ifdef CONFIG_TOMOYO   *
2448     * @head: Pointer to "struct ccs_io_buffer".
2449  static int AddExceptionPolicy(struct io_buffer *head)   *
2450     * Returns 0 on success, negative value otherwise.
2451     */
2452    static int ccs_write_exception_policy(struct ccs_io_buffer *head)
2453  {  {
2454          char *data = head->write_buf;          char *data = head->write_buf;
2455          bool is_delete = 0;          bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);
2456          if (!isRoot()) return -EPERM;          if (ccs_str_starts(&data, KEYWORD_KEEP_DOMAIN))
2457          UpdateCounter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);                  return ccs_write_domain_keeper_policy(data, false, is_delete);
2458          if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))
2459                  data += KEYWORD_DELETE_LEN;                  return ccs_write_domain_keeper_policy(data, true, is_delete);
2460                  is_delete = 1;          if (ccs_str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))
2461          }                  return ccs_write_domain_initializer_policy(data, false,
2462          if (strncmp(data, KEYWORD_KEEP_DOMAIN, KEYWORD_KEEP_DOMAIN_LEN) == 0) {                                                             is_delete);
2463                  return AddDomainKeeperPolicy(data + KEYWORD_KEEP_DOMAIN_LEN, 0, is_delete);          if (ccs_str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))
2464          } else if (strncmp(data, KEYWORD_NO_KEEP_DOMAIN, KEYWORD_NO_KEEP_DOMAIN_LEN) == 0) {                  return ccs_write_domain_initializer_policy(data, true,
2465                  return AddDomainKeeperPolicy(data + KEYWORD_NO_KEEP_DOMAIN_LEN, 1, is_delete);                                                             is_delete);
2466          } else if (strncmp(data, KEYWORD_INITIALIZE_DOMAIN, KEYWORD_INITIALIZE_DOMAIN_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_AGGREGATOR))
2467                  return AddDomainInitializerPolicy(data + KEYWORD_INITIALIZE_DOMAIN_LEN, 0, is_delete);                  return ccs_write_aggregator_policy(data, is_delete);
2468          } else if (strncmp(data, KEYWORD_NO_INITIALIZE_DOMAIN, KEYWORD_NO_INITIALIZE_DOMAIN_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_ALLOW_READ))
2469                  return AddDomainInitializerPolicy(data + KEYWORD_NO_INITIALIZE_DOMAIN_LEN, 1, is_delete);                  return ccs_write_globally_readable_policy(data, is_delete);
2470          } else if (strncmp(data, KEYWORD_ALIAS, KEYWORD_ALIAS_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))
2471                  return AddAliasPolicy(data + KEYWORD_ALIAS_LEN, is_delete);                  return ccs_write_globally_usable_env_policy(data, is_delete);
2472          } else if (strncmp(data, KEYWORD_AGGREGATOR, KEYWORD_AGGREGATOR_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_FILE_PATTERN))
2473                  return AddAggregatorPolicy(data + KEYWORD_AGGREGATOR_LEN, is_delete);                  return ccs_write_pattern_policy(data, is_delete);
2474          } else if (strncmp(data, KEYWORD_ALLOW_READ, KEYWORD_ALLOW_READ_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_PATH_GROUP))
2475                  return AddGloballyReadablePolicy(data + KEYWORD_ALLOW_READ_LEN, is_delete);                  return ccs_write_path_group_policy(data, is_delete);
2476          } else if (strncmp(data, KEYWORD_ALLOW_ENV, KEYWORD_ALLOW_ENV_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_NUMBER_GROUP))
2477                  return AddGloballyUsableEnvPolicy(data + KEYWORD_ALLOW_ENV_LEN, is_delete);                  return ccs_write_number_group_policy(data, is_delete);
2478          } else if (strncmp(data, KEYWORD_FILE_PATTERN, KEYWORD_FILE_PATTERN_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_DENY_REWRITE))
2479                  return AddPatternPolicy(data + KEYWORD_FILE_PATTERN_LEN, is_delete);                  return ccs_write_no_rewrite_policy(data, is_delete);
2480          } else if (strncmp(data, KEYWORD_PATH_GROUP, KEYWORD_PATH_GROUP_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_ADDRESS_GROUP))
2481                  return AddPathGroupPolicy(data + KEYWORD_PATH_GROUP_LEN, is_delete);                  return ccs_write_address_group_policy(data, is_delete);
2482          } else if (strncmp(data, KEYWORD_DENY_REWRITE, KEYWORD_DENY_REWRITE_LEN) == 0) {          if (ccs_str_starts(&data, KEYWORD_DENY_AUTOBIND))
2483                  return AddNoRewritePolicy(data + KEYWORD_DENY_REWRITE_LEN, is_delete);                  return ccs_write_reserved_port_policy(data, is_delete);
         } else if (strncmp(data, KEYWORD_ADDRESS_GROUP, KEYWORD_ADDRESS_GROUP_LEN) == 0) {  
                 return AddAddressGroupPolicy(data + KEYWORD_ADDRESS_GROUP_LEN, is_delete);  
         }  
2484          return -EINVAL;          return -EINVAL;
2485  }  }
2486    
2487  static int ReadExceptionPolicy(struct io_buffer *head)  /**
2488     * ccs_read_exception_policy - Read exception policy.
2489     *
2490     * @head: Pointer to "struct ccs_io_buffer".
2491     *
2492     * Returns 0 on success, -EINVAL otherwise.
2493     *
2494     * Caller holds ccs_read_lock().
2495     */
2496    static int ccs_read_exception_policy(struct ccs_io_buffer *head)
2497  {  {
2498            ccs_check_read_lock();
2499          if (!head->read_eof) {          if (!head->read_eof) {
2500                  switch (head->read_step) {                  switch (head->read_step) {
2501                  case 0:                  case 0:
2502                          if (!isRoot()) return -EPERM;                          head->read_var2 = NULL;
2503                          head->read_var2 = NULL; head->read_step = 1;                          head->read_step = 1;
2504                  case 1:                  case 1:
2505                          if (ReadDomainKeeperPolicy(head)) break;                          if (!ccs_read_domain_keeper_policy(head))
2506                          head->read_var2 = NULL; head->read_step = 2;                                  break;
2507                            head->read_var2 = NULL;
2508                            head->read_step = 2;
2509                  case 2:                  case 2:
2510                          if (ReadGloballyReadablePolicy(head)) break;                          if (!ccs_read_globally_readable_policy(head))
2511                          head->read_var2 = NULL; head->read_step = 3;                                  break;
2512                            head->read_var2 = NULL;
2513                            head->read_step = 3;
2514                  case 3:                  case 3:
2515                          if (ReadGloballyUsableEnvPolicy(head)) break;                          if (!ccs_read_globally_usable_env_policy(head))
2516                          head->read_var2 = NULL; head->read_step = 4;                                  break;
2517                            head->read_var2 = NULL;
2518                            head->read_step = 4;
2519                  case 4:                  case 4:
2520                          if (ReadDomainInitializerPolicy(head)) break;                          if (!ccs_read_domain_initializer_policy(head))
2521                          head->read_var2 = NULL; head->read_step = 5;                                  break;
2522                  case 5:                          head->read_var2 = NULL;
2523                          if (ReadAliasPolicy(head)) break;                          head->read_step = 6;
                         head->read_var2 = NULL; head->read_step = 6;  
2524                  case 6:                  case 6:
2525                          if (ReadAggregatorPolicy(head)) break;                          if (!ccs_read_aggregator_policy(head))
2526                          head->read_var2 = NULL; head->read_step = 7;                                  break;
2527                            head->read_var2 = NULL;
2528                            head->read_step = 7;
2529                  case 7:                  case 7:
2530                          if (ReadPatternPolicy(head)) break;                          if (!ccs_read_file_pattern(head))
2531                          head->read_var2 = NULL; head->read_step = 8;                                  break;
2532                            head->read_var2 = NULL;
2533                            head->read_step = 8;
2534                  case 8:                  case 8:
2535                          if (ReadNoRewritePolicy(head)) break;                          if (!ccs_read_no_rewrite_policy(head))
2536                          head->read_var2 = NULL; head->read_step = 9;                                  break;
2537                            head->read_var2 = NULL;
2538                            head->read_step = 9;
2539                  case 9:                  case 9:
2540                          if (ReadPathGroupPolicy(head)) break;                          if (!ccs_read_path_group_policy(head))
2541                          head->read_var1 = head->read_var2 = NULL; head->read_step = 10;                                  break;
2542                            head->read_var1 = NULL;
2543                            head->read_var2 = NULL;
2544                            head->read_step = 10;
2545                  case 10:                  case 10:
2546                          if (ReadAddressGroupPolicy(head)) break;                          if (!ccs_read_number_group_policy(head))
2547                          head->read_eof = 1;                                  break;
2548                            head->read_var1 = NULL;
2549                            head->read_var2 = NULL;
2550                            head->read_step = 11;
2551                    case 11:
2552                            if (!ccs_read_address_group_policy(head))
2553                                    break;
2554                            head->read_var2 = NULL;
2555                            head->read_step = 12;
2556                    case 12:
2557                            if (!ccs_read_reserved_port_policy(head))
2558                                    break;
2559                            head->read_eof = true;
2560                          break;                          break;
2561                  default:                  default:
2562                          return -EINVAL;                          return -EINVAL;
# Line 1106  static int ReadExceptionPolicy(struct io Line 2565  static int ReadExceptionPolicy(struct io
2565          return 0;          return 0;
2566  }  }
2567    
2568  #endif  /* Path to the policy loader. The default is /sbin/ccs-init. */
2569    static const char *ccs_loader;
 /*************************  SYSTEM POLICY HANDLER  *************************/  
2570    
2571  #ifdef CONFIG_SAKURA  /**
2572     * ccs_loader_setup - Specify the policy loader to use.
2573  static int AddSystemPolicy(struct io_buffer *head)   *
2574     * @str: Path to the policy loader.
2575     *
2576     * Returns 0.
2577     */
2578    static int __init ccs_loader_setup(char *str)
2579  {  {
2580          char *data = head->write_buf;          ccs_loader = str;
2581          bool is_delete = 0;          return 0;
         if (!isRoot()) return -EPERM;  
         UpdateCounter(CCS_UPDATES_COUNTER_SYSTEM_POLICY);  
         if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {  
                 data += KEYWORD_DELETE_LEN;  
                 is_delete = 1;  
         }  
         if (strncmp(data, KEYWORD_ALLOW_MOUNT, KEYWORD_ALLOW_MOUNT_LEN) == 0)  
                 return AddMountPolicy(data + KEYWORD_ALLOW_MOUNT_LEN, is_delete);  
         if (strncmp(data, KEYWORD_DENY_UNMOUNT, KEYWORD_DENY_UNMOUNT_LEN) == 0)  
                 return AddNoUmountPolicy(data + KEYWORD_DENY_UNMOUNT_LEN, is_delete);  
         if (strncmp(data, KEYWORD_ALLOW_CHROOT, KEYWORD_ALLOW_CHROOT_LEN) == 0)  
                 return AddChrootPolicy(data + KEYWORD_ALLOW_CHROOT_LEN, is_delete);  
         if (strncmp(data, KEYWORD_ALLOW_PIVOT_ROOT, KEYWORD_ALLOW_PIVOT_ROOT_LEN) == 0)  
                 return AddPivotRootPolicy(data + KEYWORD_ALLOW_PIVOT_ROOT_LEN, is_delete);  
         if (strncmp(data, KEYWORD_DENY_AUTOBIND, KEYWORD_DENY_AUTOBIND_LEN) == 0)  
                 return AddReservedPortPolicy(data + KEYWORD_DENY_AUTOBIND_LEN, is_delete);  
         return -EINVAL;  
2582  }  }
2583    
2584  static int ReadSystemPolicy(struct io_buffer *head)  __setup("CCS_loader=", ccs_loader_setup);
2585    
2586    /**
2587     * ccs_policy_loader_exists - Check whether /sbin/ccs-init exists.
2588     *
2589     * Returns true if /sbin/ccs-init exists, false otherwise.
2590     */
2591    static bool ccs_policy_loader_exists(void)
2592  {  {
2593          if (!head->read_eof) {          /*
2594                  switch (head->read_step) {           * Don't activate MAC if the path given by 'CCS_loader=' option doesn't
2595                  case 0:           * exist. If the initrd includes /sbin/init but real-root-dev has not
2596                          if (!isRoot()) return -EPERM;           * mounted on / yet, activating MAC will block the system since
2597                          head->read_var2 = NULL; head->read_step = 1;           * policies are not loaded yet.
2598                  case 1:           * Thus, let do_execve() call this function everytime.
2599                          if (ReadMountPolicy(head)) break;           */
2600                          head->read_var2 = NULL; head->read_step = 2;          struct nameidata nd;
2601                  case 2:          if (!ccs_loader)
2602                          if (ReadNoUmountPolicy(head)) break;                  ccs_loader = "/sbin/ccs-init";
2603                          head->read_var2 = NULL; head->read_step = 3;          if (path_lookup(ccs_loader, ccs_lookup_flags, &nd)) {
2604                  case 3:                  printk(KERN_INFO "Not activating Mandatory Access Control now "
2605                          if (ReadChrootPolicy(head)) break;                         "since %s doesn't exist.\n", ccs_loader);
2606                          head->read_var2 = NULL; head->read_step = 4;                  return false;
                 case 4:  
                         if (ReadPivotRootPolicy(head)) break;  
                         head->read_var2 = NULL; head->read_step = 5;  
                 case 5:  
                         if (ReadReservedPortPolicy(head)) break;  
                         head->read_eof = 1;  
                         break;  
                 default:  
                         return -EINVAL;  
                 }  
2607          }          }
2608          return 0;  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
2609  }          path_put(&nd.path);
2610    #else
2611            path_release(&nd);
2612  #endif  #endif
2613            return true;
2614    }
2615    
2616  /*************************  POLICY LOADER  *************************/  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
2617    /**
2618  static int profile_loaded = 0;   * ccs_run_loader - Start /sbin/ccs-init .
2619     *
2620  static const char *ccs_loader = NULL;   * @unused: Not used.
2621     *
2622  static int __init CCS_loader_Setup(char *str)   * Returns PID of /sbin/ccs-init on success, negative value otherwise.
2623     */
2624    static int ccs_run_loader(void *unused)
2625  {  {
2626          ccs_loader = str;          char *argv[2];
2627          return 0;          char *envp[3];
2628            printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2629                   ccs_loader);
2630            argv[0] = (char *) ccs_loader;
2631            argv[1] = NULL;
2632            envp[0] = "HOME=/";
2633            envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";