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

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