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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c

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

trunk/1.7.x/ccs-patch/security/ccsecurity/policy_io.c revision 3275 by kumaneko, Sun Dec 20 07:07:04 2009 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 5052 by kumaneko, Sun May 22 07:03:36 2011 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/policy_io.c   * security/ccsecurity/policy_io.c
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2011  NTT DATA CORPORATION
  *  
  * Version: 1.7.1+   2009/12/20  
  *  
  * This file is applicable to both 2.4.30 and 2.6.11 and later.  
  * See README.ccs for ChangeLog.  
5   *   *
6     * Version: 1.8.2-pre   2011/05/22
7   */   */
8    
9  #include "internal.h"  #include "internal.h"
10    
11  static struct ccs_profile ccs_default_profile = {  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
12          .learning = &ccs_default_profile.preference,  
13          .permissive = &ccs_default_profile.preference,  /**
14          .enforcing = &ccs_default_profile.preference,   * __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
15          .audit = &ccs_default_profile.preference,   *
16  #ifdef CONFIG_CCSECURITY_AUDIT   * @wq:        The waitqueue to wait on.
17          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,   * @condition: A C expression for the event to wait for.
18          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,   * @ret:       Timeout, in jiffies.
19     *
20     * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
21     * signal, and the remaining jiffies otherwise if the condition evaluated to
22     * true before the timeout elapsed.
23     *
24     * This is for compatibility with older kernels.
25     */
26    #define __wait_event_interruptible_timeout(wq, condition, ret)          \
27    do {                                                                    \
28            wait_queue_t __wait;                                            \
29            init_waitqueue_entry(&__wait, current);                         \
30                                                                            \
31            add_wait_queue(&wq, &__wait);                                   \
32            for (;;) {                                                      \
33                    set_current_state(TASK_INTERRUPTIBLE);                  \
34                    if (condition)                                          \
35                            break;                                          \
36                    if (!signal_pending(current)) {                         \
37                            ret = schedule_timeout(ret);                    \
38                            if (!ret)                                       \
39                                    break;                                  \
40                            continue;                                       \
41                    }                                                       \
42                    ret = -ERESTARTSYS;                                     \
43                    break;                                                  \
44            }                                                               \
45            current->state = TASK_RUNNING;                                  \
46            remove_wait_queue(&wq, &__wait);                                \
47    } while (0)
48    
49    /**
50     * wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
51     *
52     * @wq:        The waitqueue to wait on.
53     * @condition: A C expression for the event to wait for.
54     * @timeout:   Timeout, in jiffies.
55     *
56     * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
57     * signal, and the remaining jiffies otherwise if the condition evaluated to
58     * true before the timeout elapsed.
59     *
60     * This is for compatibility with older kernels.
61     */
62    #define wait_event_interruptible_timeout(wq, condition, timeout)        \
63    ({                                                                      \
64            long __ret = timeout;                                           \
65            if (!(condition))                                               \
66                    __wait_event_interruptible_timeout(wq, condition, __ret); \
67            __ret;                                                          \
68    })
69    
70  #endif  #endif
71          .preference.audit_task_info = true,  
72          .preference.audit_path_info = true,  /**
73          .preference.enforcing_penalty = 0,   * list_for_each_cookie - iterate over a list with cookie.
74          .preference.enforcing_verbose = true,   *
75          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,   * @pos:  Pointer to "struct list_head".
76          .preference.learning_verbose = false,   * @head: Pointer to "struct list_head".
77          .preference.learning_exec_realpath = true,   */
78          .preference.learning_exec_argv0 = true,  #define list_for_each_cookie(pos, head)                                 \
79          .preference.learning_symlink_target = true,          for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \
80          .preference.permissive_verbose = true               pos != (head); pos = srcu_dereference(pos->next, &ccs_ss))
81    
82    /* String table for operation mode. */
83    const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
84            [CCS_CONFIG_DISABLED]   = "disabled",
85            [CCS_CONFIG_LEARNING]   = "learning",
86            [CCS_CONFIG_PERMISSIVE] = "permissive",
87            [CCS_CONFIG_ENFORCING]  = "enforcing"
88  };  };
89    
90  /* Profile version. Currently only 20090903 is defined. */  /* String table for /proc/ccs/profile interface. */
91  static unsigned int ccs_profile_version;  const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX
92                                        + CCS_MAX_MAC_CATEGORY_INDEX] = {
93            /* CONFIG::file group */
94            [CCS_MAC_FILE_EXECUTE]    = "execute",
95            [CCS_MAC_FILE_OPEN]       = "open",
96            [CCS_MAC_FILE_CREATE]     = "create",
97            [CCS_MAC_FILE_UNLINK]     = "unlink",
98            [CCS_MAC_FILE_GETATTR]    = "getattr",
99            [CCS_MAC_FILE_MKDIR]      = "mkdir",
100            [CCS_MAC_FILE_RMDIR]      = "rmdir",
101            [CCS_MAC_FILE_MKFIFO]     = "mkfifo",
102            [CCS_MAC_FILE_MKSOCK]     = "mksock",
103            [CCS_MAC_FILE_TRUNCATE]   = "truncate",
104            [CCS_MAC_FILE_SYMLINK]    = "symlink",
105            [CCS_MAC_FILE_MKBLOCK]    = "mkblock",
106            [CCS_MAC_FILE_MKCHAR]     = "mkchar",
107            [CCS_MAC_FILE_LINK]       = "link",
108            [CCS_MAC_FILE_RENAME]     = "rename",
109            [CCS_MAC_FILE_CHMOD]      = "chmod",
110            [CCS_MAC_FILE_CHOWN]      = "chown",
111            [CCS_MAC_FILE_CHGRP]      = "chgrp",
112            [CCS_MAC_FILE_IOCTL]      = "ioctl",
113            [CCS_MAC_FILE_CHROOT]     = "chroot",
114            [CCS_MAC_FILE_MOUNT]      = "mount",
115            [CCS_MAC_FILE_UMOUNT]     = "unmount",
116            [CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root",
117            /* CONFIG::misc group */
118            [CCS_MAC_ENVIRON] = "env",
119            /* CONFIG::network group */
120            [CCS_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
121            [CCS_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
122            [CCS_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
123            [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]     = "inet_stream_accept",
124            [CCS_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
125            [CCS_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
126            [CCS_MAC_NETWORK_INET_DGRAM_RECV]        = "inet_dgram_recv",
127            [CCS_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
128            [CCS_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
129            [CCS_MAC_NETWORK_INET_RAW_RECV]          = "inet_raw_recv",
130            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
131            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
132            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
133            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]     = "unix_stream_accept",
134            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
135            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
136            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]        = "unix_dgram_recv",
137            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
138            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
139            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
140            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  = "unix_seqpacket_accept",
141            /* CONFIG::ipc group */
142            [CCS_MAC_SIGNAL] = "signal",
143            /* CONFIG::capability group */
144            [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET]  = "use_route",
145            [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet",
146            [CCS_MAC_CAPABILITY_SYS_REBOOT]        = "SYS_REBOOT",
147            [CCS_MAC_CAPABILITY_SYS_VHANGUP]       = "SYS_VHANGUP",
148            [CCS_MAC_CAPABILITY_SYS_SETTIME]       = "SYS_TIME",
149            [CCS_MAC_CAPABILITY_SYS_NICE]          = "SYS_NICE",
150            [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME]   = "SYS_SETHOSTNAME",
151            [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module",
152            [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD]    = "SYS_KEXEC_LOAD",
153            [CCS_MAC_CAPABILITY_SYS_PTRACE]        = "SYS_PTRACE",
154            /* CONFIG group */
155            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE]       = "file",
156            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK]    = "network",
157            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC]       = "misc",
158            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC]        = "ipc",
159            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
160    };
161    
162  /* Profile table. Memory is allocated as needed. */  /* String table for path operation. */
163  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
164            [CCS_TYPE_EXECUTE]    = "execute",
165            [CCS_TYPE_READ]       = "read",
166            [CCS_TYPE_WRITE]      = "write",
167            [CCS_TYPE_APPEND]     = "append",
168            [CCS_TYPE_UNLINK]     = "unlink",
169            [CCS_TYPE_GETATTR]    = "getattr",
170            [CCS_TYPE_RMDIR]      = "rmdir",
171            [CCS_TYPE_TRUNCATE]   = "truncate",
172            [CCS_TYPE_SYMLINK]    = "symlink",
173            [CCS_TYPE_CHROOT]     = "chroot",
174            [CCS_TYPE_UMOUNT]     = "unmount",
175    };
176    
177    /* String table for categories. */
178    static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
179            [CCS_MAC_CATEGORY_FILE]       = "file",
180            [CCS_MAC_CATEGORY_NETWORK]    = "network",
181            [CCS_MAC_CATEGORY_MISC]       = "misc",
182            [CCS_MAC_CATEGORY_IPC]        = "ipc",
183            [CCS_MAC_CATEGORY_CAPABILITY] = "capability",
184    };
185    
186  /* String table for functionality that takes 4 modes. */  /* String table for conditions. */
187  static const char *ccs_mode_4[4] = {  const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
188          "disabled", "learning", "permissive", "enforcing"          [CCS_TASK_UID]             = "task.uid",
189            [CCS_TASK_EUID]            = "task.euid",
190            [CCS_TASK_SUID]            = "task.suid",
191            [CCS_TASK_FSUID]           = "task.fsuid",
192            [CCS_TASK_GID]             = "task.gid",
193            [CCS_TASK_EGID]            = "task.egid",
194            [CCS_TASK_SGID]            = "task.sgid",
195            [CCS_TASK_FSGID]           = "task.fsgid",
196            [CCS_TASK_PID]             = "task.pid",
197            [CCS_TASK_PPID]            = "task.ppid",
198            [CCS_EXEC_ARGC]            = "exec.argc",
199            [CCS_EXEC_ENVC]            = "exec.envc",
200            [CCS_TYPE_IS_SOCKET]       = "socket",
201            [CCS_TYPE_IS_SYMLINK]      = "symlink",
202            [CCS_TYPE_IS_FILE]         = "file",
203            [CCS_TYPE_IS_BLOCK_DEV]    = "block",
204            [CCS_TYPE_IS_DIRECTORY]    = "directory",
205            [CCS_TYPE_IS_CHAR_DEV]     = "char",
206            [CCS_TYPE_IS_FIFO]         = "fifo",
207            [CCS_MODE_SETUID]          = "setuid",
208            [CCS_MODE_SETGID]          = "setgid",
209            [CCS_MODE_STICKY]          = "sticky",
210            [CCS_MODE_OWNER_READ]      = "owner_read",
211            [CCS_MODE_OWNER_WRITE]     = "owner_write",
212            [CCS_MODE_OWNER_EXECUTE]   = "owner_execute",
213            [CCS_MODE_GROUP_READ]      = "group_read",
214            [CCS_MODE_GROUP_WRITE]     = "group_write",
215            [CCS_MODE_GROUP_EXECUTE]   = "group_execute",
216            [CCS_MODE_OTHERS_READ]     = "others_read",
217            [CCS_MODE_OTHERS_WRITE]    = "others_write",
218            [CCS_MODE_OTHERS_EXECUTE]  = "others_execute",
219            [CCS_TASK_TYPE]            = "task.type",
220            [CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
221            [CCS_EXEC_REALPATH]        = "exec.realpath",
222            [CCS_SYMLINK_TARGET]       = "symlink.target",
223            [CCS_PATH1_UID]            = "path1.uid",
224            [CCS_PATH1_GID]            = "path1.gid",
225            [CCS_PATH1_INO]            = "path1.ino",
226            [CCS_PATH1_MAJOR]          = "path1.major",
227            [CCS_PATH1_MINOR]          = "path1.minor",
228            [CCS_PATH1_PERM]           = "path1.perm",
229            [CCS_PATH1_TYPE]           = "path1.type",
230            [CCS_PATH1_DEV_MAJOR]      = "path1.dev_major",
231            [CCS_PATH1_DEV_MINOR]      = "path1.dev_minor",
232            [CCS_PATH2_UID]            = "path2.uid",
233            [CCS_PATH2_GID]            = "path2.gid",
234            [CCS_PATH2_INO]            = "path2.ino",
235            [CCS_PATH2_MAJOR]          = "path2.major",
236            [CCS_PATH2_MINOR]          = "path2.minor",
237            [CCS_PATH2_PERM]           = "path2.perm",
238            [CCS_PATH2_TYPE]           = "path2.type",
239            [CCS_PATH2_DEV_MAJOR]      = "path2.dev_major",
240            [CCS_PATH2_DEV_MINOR]      = "path2.dev_minor",
241            [CCS_PATH1_PARENT_UID]     = "path1.parent.uid",
242            [CCS_PATH1_PARENT_GID]     = "path1.parent.gid",
243            [CCS_PATH1_PARENT_INO]     = "path1.parent.ino",
244            [CCS_PATH1_PARENT_PERM]    = "path1.parent.perm",
245            [CCS_PATH2_PARENT_UID]     = "path2.parent.uid",
246            [CCS_PATH2_PARENT_GID]     = "path2.parent.gid",
247            [CCS_PATH2_PARENT_INO]     = "path2.parent.ino",
248            [CCS_PATH2_PARENT_PERM]    = "path2.parent.perm",
249  };  };
250    
251  /* String table for /proc/ccs/profile */  /* String table for PREFERENCE keyword. */
252  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +  static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
253                                      CCS_MAX_CAPABILITY_INDEX +          [CCS_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
254                                      CCS_MAX_MAC_CATEGORY_INDEX] = {          [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
255          [CCS_MAC_FILE_EXECUTE]          [CCS_PREF_ENFORCING_PENALTY]  = "enforcing_penalty",
         = "file::execute",  
         [CCS_MAC_FILE_OPEN]  
         = "file::open",  
         [CCS_MAC_FILE_CREATE]  
         = "file::create",  
         [CCS_MAC_FILE_UNLINK]  
         = "file::unlink",  
         [CCS_MAC_FILE_MKDIR]  
         = "file::mkdir",  
         [CCS_MAC_FILE_RMDIR]  
         = "file::rmdir",  
         [CCS_MAC_FILE_MKFIFO]  
         = "file::mkfifo",  
         [CCS_MAC_FILE_MKSOCK]  
         = "file::mksock",  
         [CCS_MAC_FILE_TRUNCATE]  
         = "file::truncate",  
         [CCS_MAC_FILE_SYMLINK]  
         = "file::symlink",  
         [CCS_MAC_FILE_REWRITE]  
         = "file::rewrite",  
         [CCS_MAC_FILE_MKBLOCK]  
         = "file::mkblock",  
         [CCS_MAC_FILE_MKCHAR]  
         = "file::mkchar",  
         [CCS_MAC_FILE_LINK]  
         = "file::link",  
         [CCS_MAC_FILE_RENAME]  
         = "file::rename",  
         [CCS_MAC_FILE_CHMOD]  
         = "file::chmod",  
         [CCS_MAC_FILE_CHOWN]  
         = "file::chown",  
         [CCS_MAC_FILE_CHGRP]  
         = "file::chgrp",  
         [CCS_MAC_FILE_IOCTL]  
         = "file::ioctl",  
         [CCS_MAC_FILE_CHROOT]  
         = "file::chroot",  
         [CCS_MAC_FILE_MOUNT]  
         = "file::mount",  
         [CCS_MAC_FILE_UMOUNT]  
         = "file::umount",  
         [CCS_MAC_FILE_PIVOT_ROOT]  
         = "file::pivot_root",  
         [CCS_MAC_ENVIRON]  
         = "misc::env",  
         [CCS_MAC_NETWORK_UDP_BIND]  
         = "network::inet_udp_bind",  
         [CCS_MAC_NETWORK_UDP_CONNECT]  
         = "network::inet_udp_connect",  
         [CCS_MAC_NETWORK_TCP_BIND]  
         = "network::inet_tcp_bind",  
         [CCS_MAC_NETWORK_TCP_LISTEN]  
         = "network::inet_tcp_listen",  
         [CCS_MAC_NETWORK_TCP_CONNECT]  
         = "network::inet_tcp_connect",  
         [CCS_MAC_NETWORK_TCP_ACCEPT]  
         = "network::inet_tcp_accept",  
         [CCS_MAC_NETWORK_RAW_BIND]  
         = "network::inet_raw_bind",  
         [CCS_MAC_NETWORK_RAW_CONNECT]  
         = "network::inet_raw_connect",  
         [CCS_MAC_SIGNAL]  
         = "ipc::signal",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  
         = "capability::inet_tcp_create",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]  
         = "capability::inet_tcp_listen",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]  
         = "capability::inet_tcp_connect",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]  
         = "capability::use_inet_udp",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]  
         = "capability::use_inet_ip",  
         [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]  
         = "capability::use_route",  
         [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]  
         = "capability::use_packet",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]  
         = "capability::SYS_MOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]  
         = "capability::SYS_UMOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]  
         = "capability::SYS_REBOOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]  
         = "capability::SYS_CHROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]  
         = "capability::SYS_KILL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]  
         = "capability::SYS_VHANGUP",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]  
         = "capability::SYS_TIME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]  
         = "capability::SYS_NICE",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]  
         = "capability::SYS_SETHOSTNAME",  
         [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]  
         = "capability::use_kernel_module",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]  
         = "capability::create_fifo",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]  
         = "capability::create_block_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]  
         = "capability::create_char_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]  
         = "capability::create_unix_socket",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]  
         = "capability::SYS_LINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]  
         = "capability::SYS_SYMLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]  
         = "capability::SYS_RENAME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]  
         = "capability::SYS_UNLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]  
         = "capability::SYS_CHMOD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]  
         = "capability::SYS_CHOWN",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]  
         = "capability::SYS_IOCTL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]  
         = "capability::SYS_KEXEC_LOAD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]  
         = "capability::SYS_PTRACE",  
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_FILE] = "file",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_NETWORK] = "network",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_MISC] = "misc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_IPC] = "ipc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_CAPABILITY] = "capability",  
256  };  };
257    
258  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
259  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
260    
261  /**  /**
262   * ccs_cap2keyword - Convert capability operation to capability name.   * ccs_yesno - Return "yes" or "no".
263   *   *
264   * @operation: The capability index.   * @value: Bool value.
265   *   *
266   * Returns the name of the specified capability's name.   * Returns "yes" if @value is not 0, "no" otherwise.
267   */   */
268  const char *ccs_cap2keyword(const u8 operation)  const char *ccs_yesno(const unsigned int value)
269  {  {
270          return operation < CCS_MAX_CAPABILITY_INDEX          return value ? "yes" : "no";
                 ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;  
271  }  }
272    
273    /* Prototype for ccs_addprintf(). */
274    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
275            __attribute__ ((format(printf, 3, 4)));
276    
277  /**  /**
278   * ccs_yesno - Return "yes" or "no".   * ccs_addprintf - strncat()-like-snprintf().
279   *   *
280   * @value: Bool value.   * @buffer: Buffer to write to. Must be '\0'-terminated.
281     * @len:    Size of @buffer.
282     * @fmt:    The printf()'s format string, followed by parameters.
283     *
284     * Returns nothing.
285   */   */
286  static const char *ccs_yesno(const unsigned int value)  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
287  {  {
288          return value ? "yes" : "no";          va_list args;
289            const int pos = strlen(buffer);
290            va_start(args, fmt);
291            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
292            va_end(args);
293  }  }
294    
295  /**  /**
296   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
297   *   *
298   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
299   *   *
300   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
301     */
302    static bool ccs_flush(struct ccs_io_buffer *head)
303    {
304            while (head->r.w_pos) {
305                    const char *w = head->r.w[0];
306                    size_t len = strlen(w);
307                    if (len) {
308                            if (len > head->read_user_buf_avail)
309                                    len = head->read_user_buf_avail;
310                            if (!len)
311                                    return false;
312                            if (copy_to_user(head->read_user_buf, w, len))
313                                    return false;
314                            head->read_user_buf_avail -= len;
315                            head->read_user_buf += len;
316                            w += len;
317                    }
318                    head->r.w[0] = w;
319                    if (*w)
320                            return false;
321                    /* Add '\0' for audit logs and query. */
322                    if (head->poll) {
323                            if (!head->read_user_buf_avail ||
324                                copy_to_user(head->read_user_buf, "", 1))
325                                    return false;
326                            head->read_user_buf_avail--;
327                            head->read_user_buf++;
328                    }
329                    head->r.w_pos--;
330                    for (len = 0; len < head->r.w_pos; len++)
331                            head->r.w[len] = head->r.w[len + 1];
332            }
333            head->r.avail = 0;
334            return true;
335    }
336    
337    /**
338     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
339     *
340     * @head:   Pointer to "struct ccs_io_buffer".
341     * @string: String to print.
342     *
343     * Returns nothing.
344     *
345     * Note that @string has to be kept valid until @head is kfree()d.
346     * This means that char[] allocated on stack memory cannot be passed to
347     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
348     */
349    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
350    {
351            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
352                    head->r.w[head->r.w_pos++] = string;
353                    ccs_flush(head);
354            } else
355                    printk(KERN_WARNING "Too many words in a line.\n");
356    }
357    
358    /* Prototype for ccs_io_printf(). */
359    static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
360            __attribute__ ((format(printf, 2, 3)));
361    
362    /**
363     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
364     *
365     * @head: Pointer to "struct ccs_io_buffer".
366     * @fmt:  The printf()'s format string, followed by parameters.
367   *   *
368   * The snprintf() will truncate, but ccs_io_printf() won't.   * Returns nothing.
369   */   */
370  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
371  {  {
372          va_list args;          va_list args;
373          int len;          size_t len;
374          int pos = head->read_avail;          size_t pos = head->r.avail;
375          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
376          if (size <= 0)          if (size <= 0)
377                  return false;                  return;
378          va_start(args, fmt);          va_start(args, fmt);
379          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
380          va_end(args);          va_end(args);
381          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
382                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
383          head->read_avail += len;                  return;
384          return true;          }
385            head->r.avail += len;
386            ccs_set_string(head, head->read_buf + pos);
387  }  }
388    
389  /**  /**
390   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_set_space - Put a space to "struct ccs_io_buffer" structure.
391   *   *
392     * @head: Pointer to "struct ccs_io_buffer".
393     *
394     * Returns nothing.
395     */
396    static void ccs_set_space(struct ccs_io_buffer *head)
397    {
398            ccs_set_string(head, " ");
399    }
400    
401    /**
402     * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure.
403     *
404     * @head: Pointer to "struct ccs_io_buffer".
405     *
406     * Returns nothing.
407     */
408    static bool ccs_set_lf(struct ccs_io_buffer *head)
409    {
410            ccs_set_string(head, "\n");
411            return !head->r.w_pos;
412    }
413    
414    /**
415     * ccs_set_slash - Put a shash to "struct ccs_io_buffer" structure.
416     *
417     * @head: Pointer to "struct ccs_io_buffer".
418     *
419     * Returns nothing.
420     */
421    static void ccs_set_slash(struct ccs_io_buffer *head)
422    {
423            ccs_set_string(head, "/");
424    }
425    
426    /* List of namespaces. */
427    LIST_HEAD(ccs_namespace_list);
428    /* True if namespace other than ccs_kernel_namespace is defined. */
429    static bool ccs_namespace_enabled;
430    
431    /**
432     * ccs_init_policy_namespace - Initialize namespace.
433     *
434     * @ns: Pointer to "struct ccs_policy_namespace".
435     *
436     * Returns nothing.
437     */
438    void ccs_init_policy_namespace(struct ccs_policy_namespace *ns)
439    {
440            unsigned int idx;
441            for (idx = 0; idx < CCS_MAX_ACL_GROUPS; idx++) {
442                    INIT_LIST_HEAD(&ns->acl_group[idx][0]);
443                    INIT_LIST_HEAD(&ns->acl_group[idx][1]);
444            }
445            for (idx = 0; idx < CCS_MAX_GROUP; idx++)
446                    INIT_LIST_HEAD(&ns->group_list[idx]);
447            for (idx = 0; idx < CCS_MAX_POLICY; idx++)
448                    INIT_LIST_HEAD(&ns->policy_list[idx]);
449            ns->profile_version = 20100903;
450            ccs_namespace_enabled = !list_empty(&ccs_namespace_list);
451            list_add_tail_rcu(&ns->namespace_list, &ccs_namespace_list);
452    }
453    
454    /**
455     * ccs_print_namespace - Print namespace header.
456     *
457     * @head: Pointer to "struct ccs_io_buffer".
458     *
459     * Returns nothing.
460     */
461    static void ccs_print_namespace(struct ccs_io_buffer *head)
462    {
463            if (!ccs_namespace_enabled)
464                    return;
465            ccs_set_string(head,
466                           container_of(head->r.ns, struct ccs_policy_namespace,
467                                        namespace_list)->name);
468            ccs_set_space(head);
469    }
470    
471    /**
472     * ccs_assign_profile - Create a new profile.
473     *
474     * @ns:      Pointer to "struct ccs_policy_namespace".
475   * @profile: Profile number to create.   * @profile: Profile number to create.
476   *   *
477   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
478   */   */
479  static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_assign_profile(struct ccs_policy_namespace *ns,
480                                                            profile)                                                const unsigned int profile)
481  {  {
482          struct ccs_profile *ptr;          struct ccs_profile *ptr;
483          struct ccs_profile *entry;          struct ccs_profile *entry;
484          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
485                  return NULL;                  return NULL;
486          ptr = ccs_profile_ptr[profile];          ptr = ns->profile_ptr[profile];
487          if (ptr)          if (ptr)
488                  return ptr;                  return ptr;
489          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
490          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
491          ptr = ccs_profile_ptr[profile];                  goto out;
492            ptr = ns->profile_ptr[profile];
493          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
494                  ptr = entry;                  ptr = entry;
                 ptr->audit = &ccs_default_profile.preference;  
                 ptr->learning = &ccs_default_profile.preference;  
                 ptr->permissive = &ccs_default_profile.preference;  
                 ptr->enforcing = &ccs_default_profile.preference;  
495                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
496                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
497                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
498                         sizeof(ptr->config));                         sizeof(ptr->config));
499                    ptr->pref[CCS_PREF_MAX_AUDIT_LOG] =
500                            CONFIG_CCSECURITY_MAX_AUDIT_LOG;
501                    ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] =
502                            CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY;
503                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
504                  ccs_profile_ptr[profile] = ptr;                  ns->profile_ptr[profile] = ptr;
505                  entry = NULL;                  entry = NULL;
506          }          }
507          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
508    out:
509          kfree(entry);          kfree(entry);
510          return ptr;          return ptr;
511  }  }
512    
513  /**  /**
514   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
515     *
516     * Returns nothing.
517   */   */
518  void ccs_check_profile(void)  static void ccs_check_profile(void)
519  {  {
520          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
521            const int idx = ccs_read_lock();
522          ccs_policy_loaded = true;          ccs_policy_loaded = true;
523          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          printk(KERN_INFO "CCSecurity: 1.8.2-pre   2011/05/22\n");
524            list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
525                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
526                  if (ccs_profile_ptr[profile])                  const struct ccs_policy_namespace *ns = domain->ns;
527                    if (ns->profile_version != 20100903)
528                            printk(KERN_ERR
529                                   "Profile version %u is not supported.\n",
530                                   ns->profile_version);
531                    else if (!ns->profile_ptr[profile])
532                            printk(KERN_ERR
533                                   "Profile %u (used by '%s') is not defined.\n",
534                                   profile, domain->domainname->name);
535                    else
536                          continue;                          continue;
537                  panic("Profile %u (used by '%s') not defined.\n",                  printk(KERN_ERR
538                        profile, domain->domainname->name);                         "Userland tools for TOMOYO 1.8 must be installed and "
539                           "policy must be initialized.\n");
540                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
541                           "for more information.\n");
542                    panic("STOP!");
543          }          }
544          if (ccs_profile_version != 20090903)          ccs_read_unlock(idx);
545                  panic("Profile version %u is not supported.\n",          printk(KERN_INFO "Mandatory Access Control activated.\n");
                       ccs_profile_version);  
546  }  }
547    
548  /**  /**
# Line 310  void ccs_check_profile(void) Line 554  void ccs_check_profile(void)
554   */   */
555  struct ccs_profile *ccs_profile(const u8 profile)  struct ccs_profile *ccs_profile(const u8 profile)
556  {  {
557          struct ccs_profile *ptr = ccs_profile_ptr[profile];          static struct ccs_profile ccs_null_profile;
558          if (!ccs_policy_loaded)          struct ccs_profile *ptr = ccs_current_namespace()->
559                  return &ccs_default_profile;                  profile_ptr[profile];
560          BUG_ON(!ptr);          if (!ptr)
561                    ptr = &ccs_null_profile;
562          return ptr;          return ptr;
563  }  }
564    
565  /**  /**
566   * ccs_write_profile - Write profile table.   * ccs_find_yesno - Find values for specified keyword.
567   *   *
568   * @head: Pointer to "struct ccs_io_buffer".   * @string: String to check.
569     * @find:   Name of keyword.
570   *   *
571   * Returns 0 on success, negative value otherwise.   * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
572   */   */
573  static int ccs_write_profile(struct ccs_io_buffer *head)  static s8 ccs_find_yesno(const char *string, const char *find)
574  {  {
575          char *data = head->write_buf;          const char *cp = strstr(string, find);
576          unsigned int i;          if (cp) {
577          int value;                  cp += strlen(find);
578          int mode;                  if (!strncmp(cp, "=yes", 4))
579          u8 config;                          return 1;
580          bool use_default = false;                  else if (!strncmp(cp, "=no", 3))
         char *cp;  
         struct ccs_profile *profile;  
         if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)  
                 return 0;  
         i = simple_strtoul(data, &cp, 10);  
         if (data == cp) {  
                 profile = &ccs_default_profile;  
         } else {  
                 if (*cp != '-')  
                         return -EINVAL;  
                 data = cp + 1;  
                 profile = ccs_find_or_assign_new_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
         }  
         cp = strchr(data, '=');  
         if (!cp)  
                 return -EINVAL;  
         *cp++ = '\0';  
         if (profile != &ccs_default_profile)  
                 use_default = strstr(cp, "use_default") != NULL;  
         if (strstr(cp, "verbose=yes"))  
                 value = 1;  
         else if (strstr(cp, "verbose=no"))  
                 value = 0;  
         else  
                 value = -1;  
         if (!strcmp(data, "PREFERENCE::audit")) {  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 char *cp2;  
 #endif  
                 if (use_default) {  
                         profile->audit = &ccs_default_profile.preference;  
                         return 0;  
                 }  
                 profile->audit = &profile->preference;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 cp2 = strstr(cp, "max_grant_log=");  
                 if (cp2)  
                         sscanf(cp2 + 14, "%u",  
                                &profile->preference.audit_max_grant_log);  
                 cp2 = strstr(cp, "max_reject_log=");  
                 if (cp2)  
                         sscanf(cp2 + 15, "%u",  
                                &profile->preference.audit_max_reject_log);  
 #endif  
                 if (strstr(cp, "task_info=yes"))  
                         profile->preference.audit_task_info = true;  
                 else if (strstr(cp, "task_info=no"))  
                         profile->preference.audit_task_info = false;  
                 if (strstr(cp, "path_info=yes"))  
                         profile->preference.audit_path_info = true;  
                 else if (strstr(cp, "path_info=no"))  
                         profile->preference.audit_path_info = false;  
                 return 0;  
         }  
         if (!strcmp(data, "PREFERENCE::enforcing")) {  
                 char *cp2;  
                 if (use_default) {  
                         profile->enforcing = &ccs_default_profile.preference;  
                         return 0;  
                 }  
                 profile->enforcing = &profile->preference;  
                 if (value >= 0)  
                         profile->preference.enforcing_verbose = value;  
                 cp2 = strstr(cp, "penalty=");  
                 if (cp2)  
                         sscanf(cp2 + 8, "%u",  
                                &profile->preference.enforcing_penalty);  
                 return 0;  
         }  
         if (!strcmp(data, "PREFERENCE::permissive")) {  
                 if (use_default) {  
                         profile->permissive = &ccs_default_profile.preference;  
                         return 0;  
                 }  
                 profile->permissive = &profile->preference;  
                 if (value >= 0)  
                         profile->preference.permissive_verbose = value;  
                 return 0;  
         }  
         if (!strcmp(data, "PREFERENCE::learning")) {  
                 char *cp2;  
                 if (use_default) {  
                         profile->learning = &ccs_default_profile.preference;  
581                          return 0;                          return 0;
                 }  
                 profile->learning = &profile->preference;  
                 if (value >= 0)  
                         profile->preference.learning_verbose = value;  
                 cp2 = strstr(cp, "max_entry=");  
                 if (cp2)  
                         sscanf(cp2 + 10, "%u",  
                                &profile->preference.learning_max_entry);  
                 if (strstr(cp, "exec.realpath=yes"))  
                         profile->preference.learning_exec_realpath = true;  
                 else if (strstr(cp, "exec.realpath=no"))  
                         profile->preference.learning_exec_realpath = false;  
                 if (strstr(cp, "exec.argv0=yes"))  
                         profile->preference.learning_exec_argv0 = true;  
                 else if (strstr(cp, "exec.argv0=no"))  
                         profile->preference.learning_exec_argv0 = false;  
                 if (strstr(cp, "symlink.target=yes"))  
                         profile->preference.learning_symlink_target = true;  
                 else if (strstr(cp, "symlink.target=no"))  
                         profile->preference.learning_symlink_target = false;  
                 return 0;  
         }  
         if (profile == &ccs_default_profile)  
                 return -EINVAL;  
         if (!strcmp(data, "COMMENT")) {  
                 const struct ccs_path_info *old_comment = profile->comment;  
                 profile->comment = ccs_get_name(cp);  
                 ccs_put_name(old_comment);  
                 return 0;  
582          }          }
583          if (!strcmp(data, "CONFIG")) {          return -1;
584                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  }
585                          + CCS_MAX_MAC_CATEGORY_INDEX;  
586    /**
587     * ccs_set_uint - Set value for specified preference.
588     *
589     * @i:      Pointer to "unsigned int".
590     * @string: String to check.
591     * @find:   Name of keyword.
592     *
593     * Returns nothing.
594     */
595    static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
596    {
597            const char *cp = strstr(string, find);
598            if (cp)
599                    sscanf(cp + strlen(find), "=%u", i);
600    }
601    
602    /**
603     * ccs_set_mode - Set mode for specified profile.
604     *
605     * @name:    Name of functionality.
606     * @value:   Mode for @name.
607     * @profile: Pointer to "struct ccs_profile".
608     *
609     * Returns 0 on success, negative value otherwise.
610     */
611    static int ccs_set_mode(char *name, const char *value,
612                            struct ccs_profile *profile)
613    {
614            u8 i;
615            u8 config;
616            if (!strcmp(name, "CONFIG")) {
617                    i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
618                  config = profile->default_config;                  config = profile->default_config;
619          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
620                  config = 0;                  config = 0;
621                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
622                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                       i++) {
623                          if (strcmp(data, ccs_mac_keywords[i]))                          int len = 0;
624                            if (i < CCS_MAX_MAC_INDEX) {
625                                    const u8 c = ccs_index2category[i];
626                                    const char *category =
627                                            ccs_category_keywords[c];
628                                    len = strlen(category);
629                                    if (strncmp(name, category, len) ||
630                                        name[len++] != ':' || name[len++] != ':')
631                                            continue;
632                            }
633                            if (strcmp(name + len, ccs_mac_keywords[i]))
634                                  continue;                                  continue;
635                          config = profile->config[i];                          config = profile->config[i];
636                          break;                          break;
637                  }                  }
638                  if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
                     + CCS_MAX_MAC_CATEGORY_INDEX)  
639                          return -EINVAL;                          return -EINVAL;
640          } else {          } else {
641                  return -EINVAL;                  return -EINVAL;
642          }          }
643          if (use_default) {          if (strstr(value, "use_default")) {
644                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
645          } else {          } else {
646                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
647                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
648                            if (strstr(value, ccs_mode[mode]))
649                                  /*                                  /*
650                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
651                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
652                                   */                                   */
653                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
654                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
655                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "grant_log")) {
656                            case 1:
657                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
658                          else if (strstr(cp, "grant_log=no"))                                  break;
659                            case 0:
660                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
661                          if (strstr(cp, "reject_log=yes"))                                  break;
662                            }
663                            switch (ccs_find_yesno(value, "reject_log")) {
664                            case 1:
665                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
666                          else if (strstr(cp, "reject_log=no"))                                  break;
667                            case 0:
668                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
669                                    break;
670                            }
671                  }                  }
 #endif  
672          }          }
673          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
             + CCS_MAX_MAC_CATEGORY_INDEX)  
674                  profile->config[i] = config;                  profile->config[i] = config;
675          else if (config != CCS_CONFIG_USE_DEFAULT)          else if (config != CCS_CONFIG_USE_DEFAULT)
676                  profile->default_config = config;                  profile->default_config = config;
# Line 499  static int ccs_write_profile(struct ccs_ Line 678  static int ccs_write_profile(struct ccs_
678  }  }
679    
680  /**  /**
681     * ccs_write_profile - Write profile table.
682     *
683     * @head: Pointer to "struct ccs_io_buffer".
684     *
685     * Returns 0 on success, negative value otherwise.
686     */
687    static int ccs_write_profile(struct ccs_io_buffer *head)
688    {
689            char *data = head->write_buf;
690            unsigned int i;
691            char *cp;
692            struct ccs_profile *profile;
693            if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
694                == 1)
695                    return 0;
696            i = simple_strtoul(data, &cp, 10);
697            if (*cp != '-')
698                    return -EINVAL;
699            data = cp + 1;
700            profile = ccs_assign_profile(head->w.ns, i);
701            if (!profile)
702                    return -EINVAL;
703            cp = strchr(data, '=');
704            if (!cp)
705                    return -EINVAL;
706            *cp++ = '\0';
707            if (!strcmp(data, "COMMENT")) {
708                    static DEFINE_SPINLOCK(lock);
709                    const struct ccs_path_info *new_comment = ccs_get_name(cp);
710                    const struct ccs_path_info *old_comment;
711                    if (!new_comment)
712                            return -ENOMEM;
713                    spin_lock(&lock);
714                    old_comment = profile->comment;
715                    profile->comment = new_comment;
716                    spin_unlock(&lock);
717                    ccs_put_name(old_comment);
718                    return 0;
719            }
720            if (!strcmp(data, "PREFERENCE")) {
721                    for (i = 0; i < CCS_MAX_PREF; i++)
722                            ccs_set_uint(&profile->pref[i], cp,
723                                         ccs_pref_keywords[i]);
724                    return 0;
725            }
726            return ccs_set_mode(data, cp, profile);
727    }
728    
729    /**
730     * ccs_print_config - Print mode for specified functionality.
731     *
732     * @head:   Pointer to "struct ccs_io_buffer".
733     * @config: Mode for that functionality.
734     *
735     * Returns nothing.
736     *
737     * Caller prints functionality's name.
738     */
739    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
740    {
741            ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
742                          ccs_mode[config & 3],
743                          ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
744                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
745    }
746    
747    /**
748   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
749   *   *
750   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
751     *
752     * Returns nothing.
753   */   */
754  static void ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
755  {  {
756          int index;          u8 index;
757          if (head->read_eof)          struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
758                                                           namespace_list);
759            const struct ccs_profile *profile;
760            if (head->r.eof)
761                  return;                  return;
762          if (head->read_bit)  next:
763                  goto body;          index = head->r.index;
764          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          profile = ns->profile_ptr[index];
765          ccs_io_printf(head, "PREFERENCE::audit={ "          switch (head->r.step) {
766  #ifdef CONFIG_CCSECURITY_AUDIT          case 0:
767                        "max_grant_log=%u max_reject_log=%u "                  ccs_print_namespace(head);
768  #endif                  ccs_io_printf(head, "PROFILE_VERSION=%u\n", 20100903);
769                        "task_info=%s path_info=%s }\n",                  head->r.step++;
770  #ifdef CONFIG_CCSECURITY_AUDIT                  break;
771                        ccs_default_profile.preference.audit_max_grant_log,          case 1:
772                        ccs_default_profile.preference.audit_max_reject_log,                  for ( ; head->r.index < CCS_MAX_PROFILES; head->r.index++)
773  #endif                          if (ns->profile_ptr[head->r.index])
774                        ccs_yesno(ccs_default_profile.preference.                                  break;
775                                  audit_task_info),                  if (head->r.index == CCS_MAX_PROFILES) {
776                        ccs_yesno(ccs_default_profile.preference.                          head->r.eof = true;
777                                  audit_path_info));                          return;
778          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "                  }
779                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",                  head->r.step++;
780                        ccs_yesno(ccs_default_profile.preference.                  break;
781                                  learning_verbose),          case 2:
782                        ccs_default_profile.preference.learning_max_entry,                  {
783                        ccs_yesno(ccs_default_profile.preference.                          u8 i;
784                                  learning_exec_realpath),                          const struct ccs_path_info *comment = profile->comment;
785                        ccs_yesno(ccs_default_profile.preference.                          ccs_print_namespace(head);
786                                  learning_exec_argv0),                          ccs_io_printf(head, "%u-COMMENT=", index);
787                        ccs_yesno(ccs_default_profile.preference.                          ccs_set_string(head, comment ? comment->name : "");
788                                  learning_symlink_target));                          ccs_set_lf(head);
789          ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",                          ccs_print_namespace(head);
790                        ccs_yesno(ccs_default_profile.preference.                          ccs_io_printf(head, "%u-PREFERENCE={ ", index);
791                                  permissive_verbose));                          for (i = 0; i < CCS_MAX_PREF; i++)
792          ccs_io_printf(head, "PREFERENCE::enforcing={ verbose=%s penalty=%u "                                  ccs_io_printf(head, "%s=%u ",
793                        "}\n",                                                ccs_pref_keywords[i],
794                        ccs_yesno(ccs_default_profile.preference.                                                profile->pref[i]);
795                                  enforcing_verbose),                          ccs_set_string(head, "}\n");
796                        ccs_default_profile.preference.enforcing_penalty);                          head->r.step++;
797          head->read_bit = 1;                  }
798   body:                  break;
799          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {          case 3:
800                  bool done;                  {
801                  u8 config;                          ccs_print_namespace(head);
802                  int i;                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
803                  int pos;                          ccs_print_config(head, profile->default_config);
804                  const struct ccs_profile *profile = ccs_profile_ptr[index];                          head->r.bit = 0;
805                  const struct ccs_path_info *comment;                          head->r.step++;
806                  head->read_step = index;                  }
807                  if (!profile)                  break;
808                          continue;          case 4:
809                  pos = head->read_avail;                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
810                  comment = profile->comment;                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
811                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,                          const u8 i = head->r.bit;
812                                       comment ? comment->name : "");                          const u8 config = profile->config[i];
                 if (!done)  
                         goto out;  
                 config = profile->default_config;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "  
                                    "reject_log=%s }\n", index,  
                                    ccs_mode_4[config & 3],  
                                    ccs_yesno(config &  
                                              CCS_CONFIG_WANT_GRANT_LOG),  
                                    ccs_yesno(config &  
                                              CCS_CONFIG_WANT_REJECT_LOG)))  
                         goto out;  
 #else  
                 if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,  
                                    ccs_mode_4[config & 3]))  
                         goto out;  
 #endif  
                 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
                              + CCS_MAX_MAC_CATEGORY_INDEX; i++) {  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                         const char *g;  
                         const char *r;  
 #endif  
                         config = profile->config[i];  
813                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
814                                  continue;                                  continue;
815  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_print_namespace(head);
816                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                          if (i < CCS_MAX_MAC_INDEX)
817                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                                  ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
818                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                                                ccs_category_keywords
819                                             "grant_log=%s reject_log=%s }\n",                                                [ccs_index2category[i]],
820                                             index, ccs_mac_keywords[i],                                                ccs_mac_keywords[i]);
821                                             ccs_mode_4[config & 3], g, r))                          else
822                                  goto out;                                  ccs_io_printf(head, "%u-CONFIG::%s", index,
823  #else                                                ccs_mac_keywords[i]);
824                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          ccs_print_config(head, config);
825                                             index, ccs_mac_keywords[i],                          head->r.bit++;
826                                             ccs_mode_4[config & 3]))                          break;
827                                  goto out;                  }
828  #endif                  if (head->r.bit == CCS_MAX_MAC_INDEX
829                        + CCS_MAX_MAC_CATEGORY_INDEX) {
830                            head->r.index++;
831                            head->r.step = 1;
832                  }                  }
                 if (profile->audit != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::audit={ "  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    "max_grant_log=%u max_reject_log=%u "  
 #endif  
                                    "task_info=%s path_info=%s }\n", index,  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    profile->preference.audit_max_grant_log,  
                                    profile->preference.audit_max_reject_log,  
 #endif  
                                    ccs_yesno(profile->preference.  
                                              audit_task_info),  
                                    ccs_yesno(profile->preference.  
                                              audit_path_info)))  
                         goto out;  
                 if (profile->learning != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::learning={ "  
                                    "verbose=%s max_entry=%u exec.realpath=%s "  
                                    "exec.argv0=%s symlink.target=%s }\n",  
                                    index,  
                                    ccs_yesno(profile->preference.  
                                              learning_verbose),  
                                    profile->preference.learning_max_entry,  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_realpath),  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_argv0),  
                                    ccs_yesno(profile->preference.  
                                              learning_symlink_target)))  
                         goto out;  
                 if (profile->permissive != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::permissive={ "  
                                    "verbose=%s }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              permissive_verbose)))  
                         goto out;  
                 if (profile->enforcing != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::enforcing={ "  
                                    "verbose=%s penalty=%u }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              enforcing_verbose),  
                                    profile->preference.enforcing_penalty))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
833                  break;                  break;
834          }          }
835          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
836                  head->read_eof = true;                  goto next;
837  }  }
838    
839  /* The list for "struct ccs_policy_manager_entry". */  /**
840  LIST_HEAD(ccs_policy_manager_list);   * ccs_same_manager - Check for duplicated "struct ccs_manager" entry.
841     *
842     * @a: Pointer to "struct ccs_acl_head".
843     * @b: Pointer to "struct ccs_acl_head".
844     *
845     * Returns true if @a == @b, false otherwise.
846     */
847    static bool ccs_same_manager(const struct ccs_acl_head *a,
848                                 const struct ccs_acl_head *b)
849    {
850            return container_of(a, struct ccs_manager, head)->manager
851                    == container_of(b, struct ccs_manager, head)->manager;
852    }
853    
854  /**  /**
855   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 663  LIST_HEAD(ccs_policy_manager_list); Line 859  LIST_HEAD(ccs_policy_manager_list);
859   *   *
860   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
861   */   */
862  static int ccs_update_manager_entry(const char *manager, const bool is_delete)  static inline int ccs_update_manager_entry(const char *manager,
863                                               const bool is_delete)
864  {  {
865          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
866          struct ccs_policy_manager_entry *ptr;          struct ccs_acl_param param = {
867          struct ccs_policy_manager_entry e = { };                  /* .ns = &ccs_kernel_namespace, */
868                    .is_delete = is_delete,
869                    .list = &ccs_kernel_namespace.policy_list[CCS_ID_MANAGER],
870            };
871          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
872          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
873                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
874                          return -EINVAL;                          return -EINVAL;
875                  e.is_domain = true;                  e.is_domain = true;
876          } else {          } else {
877                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
878                          return -EINVAL;                          return -EINVAL;
879          }          }
880          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
881          if (!e.manager)          if (e.manager) {
882                  return -ENOMEM;                  error = ccs_update_policy(&e.head, sizeof(e), &param,
883          if (!is_delete)                                            ccs_same_manager);
884                  entry = kmalloc(sizeof(e), GFP_KERNEL);                  ccs_put_name(e.manager);
         mutex_lock(&ccs_policy_lock);  
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (ptr->manager != e.manager)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {  
                 list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);  
                 entry = NULL;  
                 error = 0;  
885          }          }
         mutex_unlock(&ccs_policy_lock);  
         ccs_put_name(e.manager);  
         kfree(entry);  
886          return error;          return error;
887  }  }
888    
889  /**  /**
890   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
891   *   *
892   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
893   *   *
894   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
895   */   */
896  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
897  {  {
898          char *data = head->write_buf;          const char *data = head->write_buf;
         bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);  
899          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
900                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !head->w.is_delete;
901                  return 0;                  return 0;
902          }          }
903          return ccs_update_manager_entry(data, is_delete);          return ccs_update_manager_entry(data, head->w.is_delete);
904  }  }
905    
906  /**  /**
907   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
908   *   *
909   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
910   *   *
911     * Returns nothing.
912     *
913   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
914   */   */
915  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
916  {  {
917          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
918                  return;                  return;
919          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(head->r.acl, &ccs_kernel_namespace.
920                  struct ccs_policy_manager_entry *ptr;                               policy_list[CCS_ID_MANAGER]) {
921                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                  struct ccs_manager *ptr =
922                  if (ptr->is_deleted)                          list_entry(head->r.acl, typeof(*ptr), head.list);
923                    if (ptr->head.is_deleted)
924                          continue;                          continue;
925                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
926                          return;                          return;
927                    ccs_set_string(head, ptr->manager->name);
928                    ccs_set_lf(head);
929          }          }
930          head->read_eof = true;          head->r.eof = true;
931  }  }
932    
933  /**  /**
934   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
935   *   *
936   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
937   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
938   *   *
939   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
940   */   */
941  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
942  {  {
943          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
944          const char *exe;          const char *exe;
945          struct task_struct *task = current;          struct ccs_security *task = ccs_current_security();
946          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
947                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
948          bool found = false;          bool found = false;
949          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
950                  return true;                  return true;
951          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
952                  return true;                  return true;
953          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
954                  return false;                  return false;
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (!ptr->is_deleted && ptr->is_domain  
                     && !ccs_pathcmp(domainname, ptr->manager)) {  
                         /* Set manager flag. */  
                         task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;  
                         return true;  
                 }  
         }  
955          exe = ccs_get_exe();          exe = ccs_get_exe();
956          if (!exe)          list_for_each_entry_srcu(ptr, &ccs_kernel_namespace.
957                  return false;                                   policy_list[CCS_ID_MANAGER], head.list,
958          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                                   &ccs_ss) {
959                  if (!ptr->is_deleted && !ptr->is_domain                  if (ptr->head.is_deleted)
960                      && !strcmp(exe, ptr->manager->name)) {                          continue;
961                          found = true;                  if (ptr->is_domain) {
962                          /* Set manager flag. */                          if (ccs_pathcmp(domainname, ptr->manager))
963                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                                  continue;
964                          break;                  } else {
965                            if (!exe || strcmp(exe, ptr->manager->name))
966                                    continue;
967                  }                  }
968                    /* Set manager flag. */
969                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
970                    found = true;
971                    break;
972          }          }
973          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
974                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 798  static bool ccs_is_policy_manager(void) Line 984  static bool ccs_is_policy_manager(void)
984  }  }
985    
986  /**  /**
987   * ccs_find_condition_part - Find condition part from the statement.   * ccs_select_domain - Parse select command.
  *  
  * @data: String to parse.  
  *  
  * Returns pointer to the condition part if it was found in the statement,  
  * NULL otherwise.  
  */  
 static char *ccs_find_condition_part(char *data)  
 {  
         char *cp = strstr(data, " if ");  
         if (cp) {  
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
                 cp = strstr(data, " ; set ");  
                 if (cp)  
                         *cp++ = '\0';  
         }  
         return cp;  
 }  
   
 /**  
  * ccs_is_select_one - Parse select command.  
988   *   *
989   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
990   * @data: String to parse.   * @data: String to parse.
# Line 834  static char *ccs_find_condition_part(cha Line 993  static char *ccs_find_condition_part(cha
993   *   *
994   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
995   */   */
996  static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)  static bool ccs_select_domain(struct ccs_io_buffer *head, const char *data)
997  {  {
998          unsigned int pid;          unsigned int pid;
999          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1000          bool global_pid = false;          bool global_pid = false;
1001          if (!strcmp(data, "allow_execute")) {          if (strncmp(data, "select ", 7))
1002                  head->read_execute_only = true;                  return false;
1003                  return true;          data += 7;
         }  
1004          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
1005              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
1006                  struct task_struct *p;                  struct task_struct *p;
1007                  ccs_tasklist_lock();                  ccs_tasklist_lock();
1008  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1009                  if (global_pid)                  if (global_pid)
1010                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
1011                                                                   &init_pid_ns);
1012                  else                  else
1013                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
1014  #else  #else
1015                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
1016  #endif  #endif
# Line 859  static bool ccs_is_select_one(struct ccs Line 1018  static bool ccs_is_select_one(struct ccs
1018                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
1019                  ccs_tasklist_unlock();                  ccs_tasklist_unlock();
1020          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
1021                  if (ccs_is_domain_def(data + 7))                  if (*(data + 7) == '<')
1022                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
1023          } else          } else
1024                  return false;                  return false;
1025          head->write_var1 = domain;          head->w.domain = domain;
1026          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
1027          if (!head->read_buf)          if (!head->read_buf)
1028                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
1029          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
1030            head->r.print_this_domain_only = true;
1031            if (domain)
1032                    head->r.domain = &domain->list;
1033            else
1034                    head->r.eof = true;
1035          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
1036          head->read_single_domain = true;          if (domain && domain->is_deleted)
1037          head->read_eof = !domain;                  ccs_set_string(head, "# This is a deleted domain.\n");
         if (domain) {  
                 struct ccs_domain_info *d;  
                 head->read_var1 = NULL;  
                 list_for_each_entry_rcu(d, &ccs_domain_list, list) {  
                         if (d == domain)  
                                 break;  
                         head->read_var1 = &d->list;  
                 }  
                 head->read_var2 = NULL;  
                 head->read_bit = 0;  
                 head->read_step = 0;  
                 if (domain->is_deleted)  
                         ccs_io_printf(head, "# This is a deleted domain.\n");  
         }  
1038          return true;          return true;
1039  }  }
1040    
 static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,  
                                     struct ccs_condition *cond,  
                                     const bool is_delete)  
 {  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))  
                 return ccs_write_capability_policy(data, domain, cond,  
                                                    is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))  
                 return ccs_write_network_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))  
                 return ccs_write_signal_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))  
                 return ccs_write_env_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))  
                 return ccs_write_mount_policy(data, domain, cond, is_delete);  
         return ccs_write_file_policy(data, domain, cond, is_delete);  
 }  
   
 /**  
  * ccs_write_domain_policy - Write domain policy.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_write_domain_policy(struct ccs_io_buffer *head)  
 {  
         char *data = head->write_buf;  
         struct ccs_domain_info *domain = head->write_var1;  
         bool is_delete = false;  
         bool is_select = false;  
         unsigned int profile;  
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
         if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))  
                 is_delete = true;  
         else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))  
                 is_select = true;  
         if (is_select && ccs_is_select_one(head, data))  
                 return 0;  
         /* Don't allow updating policies by non manager programs. */  
         if (!ccs_is_policy_manager())  
                 return -EPERM;  
         if (ccs_is_domain_def(data)) {  
                 domain = NULL;  
                 if (is_delete)  
                         ccs_delete_domain(data);  
                 else if (is_select)  
                         domain = ccs_find_domain(data);  
                 else  
                         domain = ccs_find_or_assign_new_domain(data, 0);  
                 head->write_var1 = domain;  
                 return 0;  
         }  
         if (!domain)  
                 return -EINVAL;  
   
         if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1  
             && profile < CCS_MAX_PROFILES) {  
                 if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])  
                         domain->profile = (u8) profile;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {  
                 domain->ignore_global_allow_read = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {  
                 domain->ignore_global_allow_env = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {  
                 domain->quota_warned = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {  
                 domain->domain_transition_failed = !is_delete;  
                 return 0;  
         }  
         cp = ccs_find_condition_part(data);  
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         error = ccs_write_domain_policy2(data, domain, cond, is_delete);  
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
 }  
   
1041  /**  /**
1042   * ccs_print_name_union - Print a ccs_name_union.   * ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry.
1043   *   *
1044   * @head: Pointer to "struct ccs_io_buffer".   * @a: Pointer to "struct ccs_acl_info".
1045   * @ptr:  Pointer to "struct ccs_name_union".   * @b: Pointer to "struct ccs_acl_info".
1046   *   *
1047   * Returns true on success, false otherwise.   * Returns true if @a == @b, false otherwise.
1048   */   */
1049  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
1050                                   const struct ccs_name_union *ptr)                                   const struct ccs_acl_info *b)
1051  {  {
1052          int pos = head->read_avail;          const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
1053          if (pos && head->read_buf[pos - 1] == ' ')          const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
1054                  head->read_avail--;          return p1->handler == p2->handler;
         if (ptr->is_group)  
                 return ccs_io_printf(head, " @%s",  
                                      ptr->group->group_name->name);  
         return ccs_io_printf(head, " %s", ptr->filename->name);  
1055  }  }
1056    
1057  /**  /**
1058   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.   * ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry.
1059   *   *
1060   * @head: Pointer to "struct ccs_io_buffer".   * @a: Pointer to "struct ccs_acl_info".
1061   * @ptr:  Pointer to "struct ccs_name_union".   * @b: Pointer to "struct ccs_acl_info".
1062   *   *
1063   * Returns true on success, false otherwise.   * Returns true if @a == @b, false otherwise.
1064   */   */
1065  static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,  static bool ccs_same_task_acl(const struct ccs_acl_info *a,
1066                                          const struct ccs_name_union *ptr)                                const struct ccs_acl_info *b)
1067  {  {
1068          if (ptr->is_group)          const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
1069                  return ccs_io_printf(head, "@%s",          const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
1070                                       ptr->group->group_name->name);          return p1->domainname == p2->domainname;
         return ccs_io_printf(head, "\"%s\"", ptr->filename->name);  
1071  }  }
1072    
1073  /**  /**
1074   * ccs_print_number_union_common - Print a ccs_number_union.   * ccs_write_task - Update task related list.
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
  * @need_space: True if a space character is needed.  
1075   *   *
1076   * Returns true on success, false otherwise.   * @param: Pointer to "struct ccs_acl_param".
  */  
 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,  
                                           const struct ccs_number_union *ptr,  
                                           const bool need_space)  
 {  
         unsigned long min;  
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
         if (need_space && !ccs_io_printf(head, " "))  
                 return false;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         min_type = ptr->min_type;  
         max_type = ptr->max_type;  
         min = ptr->values[0];  
         max = ptr->values[1];  
         switch (min_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 if (!ccs_io_printf(head, "0x%lX", min))  
                         return false;  
                 break;  
         case CCS_VALUE_TYPE_OCTAL:  
                 if (!ccs_io_printf(head, "0%lo", min))  
                         return false;  
                 break;  
         default:  
                 if (!ccs_io_printf(head, "%lu", min))  
                         return false;  
                 break;  
         }  
         if (min == max && min_type == max_type)  
                 return true;  
         switch (max_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 return ccs_io_printf(head, "-0x%lX", max);  
         case CCS_VALUE_TYPE_OCTAL:  
                 return ccs_io_printf(head, "-0%lo", max);  
         default:  
                 return ccs_io_printf(head, "-%lu", max);  
         }  
 }  
   
 /**  
  * ccs_print_number_union - Print a ccs_number_union.  
1077   *   *
1078   * @head:       Pointer to "struct ccs_io_buffer".   * Returns 0 on success, negative value otherwise.
  * @ptr:        Pointer to "struct ccs_number_union".  
1079   *   *
1080   * Returns true on success, false otherwise.   * Caller holds ccs_read_lock().
1081   */   */
1082  bool ccs_print_number_union(struct ccs_io_buffer *head,  static int ccs_write_task(struct ccs_acl_param *param)
                             const struct ccs_number_union *ptr)  
1083  {  {
1084          return ccs_print_number_union_common(head, ptr, true);          int error;
1085            const bool is_auto = ccs_str_starts(&param->data,
1086                                                "auto_domain_transition ");
1087            if (!is_auto && !ccs_str_starts(&param->data,
1088                                            "manual_domain_transition ")) {
1089                    struct ccs_handler_acl e = { };
1090                    char *handler;
1091                    if (ccs_str_starts(&param->data, "auto_execute_handler "))
1092                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
1093                    else if (ccs_str_starts(&param->data,
1094                                            "denied_execute_handler "))
1095                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
1096                    else
1097                            return -EINVAL;
1098                    handler = ccs_read_token(param);
1099                    if (!ccs_correct_path(handler))
1100                            return -EINVAL;
1101                    e.handler = ccs_get_name(handler);
1102                    if (!e.handler)
1103                            return -ENOMEM;
1104                    if (e.handler->is_patterned)
1105                            error = -EINVAL; /* No patterns allowed. */
1106                    else
1107                            error = ccs_update_domain(&e.head, sizeof(e), param,
1108                                                      ccs_same_handler_acl, NULL);
1109                    ccs_put_name(e.handler);
1110            } else {
1111                    struct ccs_task_acl e = {
1112                            .head.type = is_auto ?
1113                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
1114                            .domainname = ccs_get_domainname(param),
1115                    };
1116                    if (!e.domainname)
1117                            error = -EINVAL;
1118                    else
1119                            error = ccs_update_domain(&e.head, sizeof(e), param,
1120                                                      ccs_same_task_acl, NULL);
1121                    ccs_put_name(e.domainname);
1122            }
1123            return error;
1124  }  }
1125    
1126  /**  /**
1127   * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.   * ccs_write_domain2 - Write domain policy.
1128   *   *
1129   * @head:       Pointer to "struct ccs_io_buffer".   * @ns:        Pointer to "struct ccs_policy_namespace".
1130   * @ptr:        Pointer to "struct ccs_number_union".   * @list:      Pointer to "struct list_head [2]".
1131   *   * @data:      Policy to be interpreted.
1132   * Returns true on success, false otherwise.   * @is_delete: True if it is a delete request.
  */  
 static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  
                                            const struct ccs_number_union *ptr)  
 {  
         return ccs_print_number_union_common(head, ptr, false);  
 }  
   
 /**  
  * ccs_print_condition - Print condition part.  
1133   *   *
1134   * @head: Pointer to "struct ccs_io_buffer".   * Returns 0 on success, negative value otherwise.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1135   *   *
1136   * Returns true on success, false otherwise.   * Caller holds ccs_read_lock().
1137   */   */
1138  static bool ccs_print_condition(struct ccs_io_buffer *head,  static int ccs_write_domain2(struct ccs_policy_namespace *ns,
1139                                  const struct ccs_condition *cond)                               struct list_head list[2], char *data,
1140  {                               const bool is_delete)
1141          const struct ccs_condition_element *condp;  {
1142          const struct ccs_number_union *numbers_p;          struct ccs_acl_param param = {
1143          const struct ccs_name_union *names_p;                  .ns = ns,
1144          const struct ccs_argv_entry *argv;                  .list = list,
1145          const struct ccs_envp_entry *envp;                  .data = data,
1146          u16 condc;                  .is_delete = is_delete,
1147          u16 i;          };
1148          u16 j;          static const struct {
1149          char buffer[32];                  const char *keyword;
1150          if (!cond)                  int (*write) (struct ccs_acl_param *);
1151                  goto no_condition;          } ccs_callback[7] = {
1152          condc = cond->condc;                  { "file ", ccs_write_file },
1153          condp = (const struct ccs_condition_element *) (cond + 1);                  { "network inet ", ccs_write_inet_network },
1154          numbers_p = (const struct ccs_number_union *) (condp + condc);                  { "network unix ", ccs_write_unix_network },
1155          names_p = (const struct ccs_name_union *)                  { "misc ", ccs_write_misc },
1156                  (numbers_p + cond->numbers_count);                  { "capability ", ccs_write_capability },
1157          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                  { "ipc signal ", ccs_write_ipc },
1158          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                  { "task ", ccs_write_task },
1159          memset(buffer, 0, sizeof(buffer));          };
1160          if (condc && !ccs_io_printf(head, "%s", " if"))          u8 i;
1161                  goto out;          for (i = 0; i < 7; i++) {
1162          for (i = 0; i < condc; i++) {                  if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
                 const u8 match = condp->equals;  
                 const u8 left = condp->left;  
                 const u8 right = condp->right;  
                 condp++;  
                 switch (left) {  
                 case CCS_ARGV_ENTRY:  
                         if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",  
                                            argv->index, argv->is_not ?  
                                            "!=" : "=", argv->value->name))  
                                 goto out;  
                         argv++;  
                         continue;  
                 case CCS_ENVP_ENTRY:  
                         if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",  
                                            envp->name->name, envp->is_not ?  
                                            "!=" : "="))  
                                 goto out;  
                         if (envp->value) {  
                                 if (!ccs_io_printf(head, "\"%s\"",  
                                                    envp->value->name))  
                                         goto out;  
                         } else {  
                                 if (!ccs_io_printf(head, "NULL"))  
                                         goto out;  
                         }  
                         envp++;  
                         continue;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (left >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, " %s",  
                                            ccs_condition_keyword[left]))  
                                 goto out;  
                         break;  
                 }  
                 if (!ccs_io_printf(head, "%s", match ? "=" : "!="))  
                         goto out;  
                 switch (right) {  
                 case CCS_NAME_UNION:  
                         if (!ccs_print_name_union_quoted(head, names_p++))  
                                 goto out;  
                         break;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union_nospace(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (right >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, "%s",  
                                            ccs_condition_keyword[right]))  
                                 goto out;  
                         break;  
                 }  
         }  
         i = cond->post_state[3];  
         if (!i)  
                 goto no_condition;  
         if (!ccs_io_printf(head, " ; set"))  
                 goto out;  
         for (j = 0; j < 3; j++) {  
                 if (!(i & (1 << j)))  
1163                          continue;                          continue;
1164                  if (!ccs_io_printf(head, " task.state[%u]=%u", j,                  return ccs_callback[i].write(&param);
                                    cond->post_state[j]))  
                         goto out;  
1165          }          }
1166   no_condition:          return -EINVAL;
         if (ccs_io_printf(head, "\n"))  
                 return true;  
  out:  
         return false;  
1167  }  }
1168    
1169  /**  /**
1170   * ccs_print_path_acl - Print a path ACL entry.   * ccs_delete_domain - Delete a domain.
1171   *   *
1172   * @head: Pointer to "struct ccs_io_buffer".   * @domainname: The name of domain.
  * @ptr:  Pointer to "struct ccs_path_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1173   *   *
1174   * Returns true on success, false otherwise.   * Returns 0.
1175   */   */
1176  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static int ccs_delete_domain(char *domainname)
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
1177  {  {
1178          int pos;          struct ccs_domain_info *domain;
1179          u8 bit;          struct ccs_path_info name;
1180          const u16 perm = ptr->perm;          name.name = domainname;
1181          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {          ccs_fill_path_info(&name);
1182                  if (!(perm & (1 << bit)))          if (mutex_lock_interruptible(&ccs_policy_lock))
1183                          continue;                  return 0;
1184                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)          /* Is there an active domain? */
1185            list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
1186                    /* Never delete ccs_kernel_domain. */
1187                    if (domain == &ccs_kernel_domain)
1188                          continue;                          continue;
1189                  /* Print "read/write" instead of "read" and "write". */                  if (domain->is_deleted ||
1190                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)                      ccs_pathcmp(domain->domainname, &name))
                     && (perm & (1 << CCS_TYPE_READ_WRITE)))  
1191                          continue;                          continue;
1192                  pos = head->read_avail;                  domain->is_deleted = true;
1193                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||                  break;
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1194          }          }
1195          head->read_bit = 0;          mutex_unlock(&ccs_policy_lock);
1196          return true;          return 0;
1197  }  }
1198    
1199  /**  /* String table for domain flags. */
1200   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.  const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1201   *          [CCS_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
1202   * @head: Pointer to "struct ccs_io_buffer".          [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
1203   * @ptr:  Pointer to "struct ccs_path_number3_acl".  };
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  
                                        struct ccs_path_number3_acl *ptr,  
                                        const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path_number32keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->mode) ||  
                     !ccs_print_number_union(head, &ptr->major) ||  
                     !ccs_print_number_union(head, &ptr->minor) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
 }  
1204    
1205  /**  /**
1206   * ccs_print_path2_acl - Print a path2 ACL entry.   * ccs_write_domain - Write domain policy.
1207   *   *
1208   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to "struct ccs_path2_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  
                                 struct ccs_path2_acl *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u8 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path22keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name1) ||  
                     !ccs_print_name_union(head, &ptr->name2) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
 }  
   
 /**  
  * ccs_print_path_number_acl - Print a path_number ACL entry.  
1209   *   *
1210   * @head: Pointer to "struct ccs_io_buffer".   * Returns 0 on success, negative value otherwise.
  * @ptr:  Pointer to "struct ccs_path_number_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1211   *   *
1212   * Returns true on success, false otherwise.   * Caller holds ccs_read_lock().
1213   */   */
1214  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static int ccs_write_domain(struct ccs_io_buffer *head)
                                       struct ccs_path_number_acl *ptr,  
                                       const struct ccs_condition *cond)  
1215  {  {
1216          int pos;          char *data = head->write_buf;
1217          u8 bit;          struct ccs_policy_namespace *ns;
1218          const u8 perm = ptr->perm;          struct ccs_domain_info *domain = head->w.domain;
1219          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;          const bool is_delete = head->w.is_delete;
1220               bit++) {          const bool is_select = !is_delete && ccs_str_starts(&data, "select ");
1221                  if (!(perm & (1 << bit)))          unsigned int profile;
1222            if (*data == '<') {
1223                    domain = NULL;
1224                    if (is_delete)
1225                            ccs_delete_domain(data);
1226                    else if (is_select)
1227                            domain = ccs_find_domain(data);
1228                    else
1229                            domain = ccs_assign_domain(data, false);
1230                    head->w.domain = domain;
1231                    return 0;
1232            }
1233            if (!domain)
1234                    return -EINVAL;
1235            ns = domain->ns;
1236            if (sscanf(data, "use_profile %u\n", &profile) == 1
1237                && profile < CCS_MAX_PROFILES) {
1238                    if (!ccs_policy_loaded || ns->profile_ptr[(u8) profile])
1239                            if (!is_delete)
1240                                    domain->profile = (u8) profile;
1241                    return 0;
1242            }
1243            if (sscanf(data, "use_group %u\n", &profile) == 1
1244                && profile < CCS_MAX_ACL_GROUPS) {
1245                    if (!is_delete)
1246                            domain->group = (u8) profile;
1247                    return 0;
1248            }
1249            for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1250                    const char *cp = ccs_dif[profile];
1251                    if (strncmp(data, cp, strlen(cp) - 1))
1252                          continue;                          continue;
1253                  pos = head->read_avail;                  domain->flags[profile] = !is_delete;
1254                  if (!ccs_io_printf(head, "allow_%s",                  return 0;
                                    ccs_path_number2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->number) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1255          }          }
1256          head->read_bit = 0;          return ccs_write_domain2(ns, domain->acl_info_list, data, is_delete);
         return true;  
1257  }  }
1258    
1259  /**  /**
1260   * ccs_print_env_acl - Print an evironment variable name's ACL entry.   * ccs_print_name_union - Print a ccs_name_union.
1261   *   *
1262   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1263   * @ptr:  Pointer to "struct ccs_env_acl".   * @ptr:  Pointer to "struct ccs_name_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1264   *   *
1265   * Returns true on success, false otherwise.   * Returns nothing.
1266   */   */
1267  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1268                                struct ccs_env_acl *ptr,                                   const struct ccs_name_union *ptr)
1269                                const struct ccs_condition *cond)  {
1270  {          ccs_set_space(head);
1271          const int pos = head->read_avail;          if (ptr->is_group) {
1272          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||                  ccs_set_string(head, "@");
1273              !ccs_print_condition(head, cond)) {                  ccs_set_string(head, ptr->group->group_name->name);
1274                  head->read_avail = pos;          } else {
1275                  return false;                  ccs_set_string(head, ptr->filename->name);
1276          }          }
         return true;  
1277  }  }
1278    
1279  /**  /**
1280   * ccs_print_capability_acl - Print a capability ACL entry.   * ccs_print_name_union_quoted - Print a ccs_name_union with a quote.
1281   *   *
1282   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1283   * @ptr:  Pointer to "struct ccs_capability_acl".   * @ptr:  Pointer to "struct ccs_name_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1284   *   *
1285   * Returns true on success, false otherwise.   * Returns nothing.
1286   */   */
1287  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  static void ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1288                                       struct ccs_capability_acl *ptr,                                          const struct ccs_name_union *ptr)
1289                                       const struct ccs_condition *cond)  {
1290  {          if (ptr->is_group) {
1291          const int pos = head->read_avail;                  ccs_set_string(head, "@");
1292          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",                  ccs_set_string(head, ptr->group->group_name->name);
1293                             ccs_cap2keyword(ptr->operation)) ||          } else {
1294              !ccs_print_condition(head, cond)) {                  ccs_set_string(head, "\"");
1295                  head->read_avail = pos;                  ccs_set_string(head, ptr->filename->name);
1296                  return false;                  ccs_set_string(head, "\"");
1297          }          }
         return true;  
1298  }  }
1299    
1300  /**  /**
1301   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.   * ccs_print_number_union_nospace - Print a ccs_number_union without a space.
1302   *   *
1303   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1304   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @ptr:  Pointer to "struct ccs_number_union".
1305   *   *
1306   * Returns true on success, false otherwise.   * Returns nothing.
1307   */   */
1308  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  static void ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1309                                   struct ccs_ip_network_acl *ptr)                                             const struct ccs_number_union *ptr)
1310  {  {
1311          const u32 min_address = ptr->address.ipv4.min;          if (ptr->is_group) {
1312          const u32 max_address = ptr->address.ipv4.max;                  ccs_set_string(head, "@");
1313          if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))                  ccs_set_string(head, ptr->group->group_name->name);
1314                  return false;          } else {
1315          if (min_address != max_address                  int i;
1316              && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))                  unsigned long min = ptr->values[0];
1317                  return false;                  const unsigned long max = ptr->values[1];
1318          return true;                  u8 min_type = ptr->value_type[0];
1319                    const u8 max_type = ptr->value_type[1];
1320                    char buffer[128];
1321                    buffer[0] = '\0';
1322                    for (i = 0; i < 2; i++) {
1323                            switch (min_type) {
1324                            case CCS_VALUE_TYPE_HEXADECIMAL:
1325                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1326                                                  min);
1327                                    break;
1328                            case CCS_VALUE_TYPE_OCTAL:
1329                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1330                                                  min);
1331                                    break;
1332                            default:
1333                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1334                                                  min);
1335                                    break;
1336                            }
1337                            if (min == max && min_type == max_type)
1338                                    break;
1339                            ccs_addprintf(buffer, sizeof(buffer), "-");
1340                            min_type = max_type;
1341                            min = max;
1342                    }
1343                    ccs_io_printf(head, "%s", buffer);
1344            }
1345  }  }
1346    
1347  /**  /**
1348   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1349   *   *
1350   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1351   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @ptr:  Pointer to "struct ccs_number_union".
1352   *   *
1353   * Returns true on success, false otherwise.   * Returns nothing.
1354   */   */
1355  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1356                                   struct ccs_ip_network_acl *ptr)                                     const struct ccs_number_union *ptr)
1357  {  {
1358          char buf[64];          ccs_set_space(head);
1359          const struct in6_addr *min_address = ptr->address.ipv6.min;          ccs_print_number_union_nospace(head, ptr);
         const struct in6_addr *max_address = ptr->address.ipv6.max;  
         ccs_print_ipv6(buf, sizeof(buf), min_address);  
         if (!ccs_io_printf(head, "%s", buf))  
                 return false;  
         if (min_address != max_address) {  
                 ccs_print_ipv6(buf, sizeof(buf), max_address);  
                 if (!ccs_io_printf(head, "-%s", buf))  
                         return false;  
         }  
         return true;  
1360  }  }
1361    
1362  /**  /**
1363   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_condition - Print condition part.
1364   *   *
1365   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1366   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @cond: Pointer to "struct ccs_condition".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1367   *   *
1368   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1369   */   */
1370  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1371                                    struct ccs_ip_network_acl *ptr,                                  const struct ccs_condition *cond)
                                   const struct ccs_condition *cond)  
1372  {  {
1373          int pos;          switch (head->r.cond_step) {
1374          u8 bit;          case 0:
1375          const u16 perm = ptr->perm;                  head->r.cond_index = 0;
1376          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                  head->r.cond_step++;
1377                  if (!(perm & (1 << bit)))                  /* fall through */
1378                          continue;          case 1:
1379                  pos = head->read_avail;                  {
1380                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",                          const u16 condc = cond->condc;
1381                                     ccs_net2keyword(bit)))                          const struct ccs_condition_element *condp =
1382                          goto out;                                  (typeof(condp)) (cond + 1);
1383                  switch (ptr->address_type) {                          const struct ccs_number_union *numbers_p =
1384                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                                  (typeof(numbers_p)) (condp + condc);
1385                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          const struct ccs_name_union *names_p =
1386                                             group_name->name))                                  (typeof(names_p))
1387                                  goto out;                                  (numbers_p + cond->numbers_count);
1388                          break;                          const struct ccs_argv *argv =
1389                  case CCS_IP_ADDRESS_TYPE_IPv4:                                  (typeof(argv)) (names_p + cond->names_count);
1390                          if (!ccs_print_ipv4_entry(head, ptr))                          const struct ccs_envp *envp =
1391                                  goto out;                                  (typeof(envp)) (argv + cond->argc);
1392                          break;                          u16 skip;
1393                  case CCS_IP_ADDRESS_TYPE_IPv6:                          for (skip = 0; skip < head->r.cond_index; skip++) {
1394                          if (!ccs_print_ipv6_entry(head, ptr))                                  const u8 left = condp->left;
1395                                  goto out;                                  const u8 right = condp->right;
1396                                    condp++;
1397                                    switch (left) {
1398                                    case CCS_ARGV_ENTRY:
1399                                            argv++;
1400                                            continue;
1401                                    case CCS_ENVP_ENTRY:
1402                                            envp++;
1403                                            continue;
1404                                    case CCS_NUMBER_UNION:
1405                                            numbers_p++;
1406                                            break;
1407                                    }
1408                                    switch (right) {
1409                                    case CCS_NAME_UNION:
1410                                            names_p++;
1411                                            break;
1412                                    case CCS_NUMBER_UNION:
1413                                            numbers_p++;
1414                                            break;
1415                                    }
1416                            }
1417                            while (head->r.cond_index < condc) {
1418                                    const u8 match = condp->equals;
1419                                    const u8 left = condp->left;
1420                                    const u8 right = condp->right;
1421                                    if (!ccs_flush(head))
1422                                            return false;
1423                                    condp++;
1424                                    head->r.cond_index++;
1425                                    ccs_set_space(head);
1426                                    switch (left) {
1427                                    case CCS_ARGV_ENTRY:
1428                                            ccs_io_printf(head,
1429                                                          "exec.argv[%lu]%s=\"",
1430                                                          argv->index,
1431                                                          argv->is_not ? "!" : "");
1432                                            ccs_set_string(head,
1433                                                           argv->value->name);
1434                                            ccs_set_string(head, "\"");
1435                                            argv++;
1436                                            continue;
1437                                    case CCS_ENVP_ENTRY:
1438                                            ccs_set_string(head, "exec.envp[\"");
1439                                            ccs_set_string(head, envp->name->name);
1440                                            ccs_io_printf(head, "\"]%s=",
1441                                                          envp->is_not ? "!" : "");
1442                                            if (envp->value) {
1443                                                    ccs_set_string(head, "\"");
1444                                                    ccs_set_string(head, envp->
1445                                                                   value->name);
1446                                                    ccs_set_string(head, "\"");
1447                                            } else {
1448                                                    ccs_set_string(head, "NULL");
1449                                            }
1450                                            envp++;
1451                                            continue;
1452                                    case CCS_NUMBER_UNION:
1453                                            ccs_print_number_union_nospace
1454                                                    (head, numbers_p++);
1455                                            break;
1456                                    default:
1457                                            ccs_set_string(head,
1458                                                   ccs_condition_keyword[left]);
1459                                            break;
1460                                    }
1461                                    ccs_set_string(head, match ? "=" : "!=");
1462                                    switch (right) {
1463                                    case CCS_NAME_UNION:
1464                                            ccs_print_name_union_quoted
1465                                                    (head, names_p++);
1466                                            break;
1467                                    case CCS_NUMBER_UNION:
1468                                            ccs_print_number_union_nospace
1469                                                    (head, numbers_p++);
1470                                            break;
1471                                    default:
1472                                            ccs_set_string(head,
1473                                                   ccs_condition_keyword[right]);
1474                                            break;
1475                                    }
1476                            }
1477                    }
1478                    head->r.cond_step++;
1479                    /* fall through */
1480            case 2:
1481                    if (!ccs_flush(head))
1482                          break;                          break;
1483                    head->r.cond_step++;
1484                    /* fall through */
1485            case 3:
1486                    if (cond->grant_log != CCS_GRANTLOG_AUTO)
1487                            ccs_io_printf(head, " grant_log=%s",
1488                                          ccs_yesno(cond->grant_log ==
1489                                                    CCS_GRANTLOG_YES));
1490                    if (cond->transit) {
1491                            const char *name = cond->transit->name;
1492                            ccs_set_string(head, *name == '<' ?
1493                                           " auto_namespace_transition=\"" :
1494                                           " auto_domain_transition=\"");
1495                            ccs_set_string(head, name);
1496                            ccs_set_string(head, "\"");
1497                  }                  }
1498                  if (!ccs_print_number_union(head, &ptr->port) ||                  ccs_set_lf(head);
1499                      !ccs_print_condition(head, cond))                  return true;
                         goto out;  
1500          }          }
         head->read_bit = 0;  
         return true;  
  out:  
         head->read_bit = bit;  
         head->read_avail = pos;  
1501          return false;          return false;
1502  }  }
1503    
1504  /**  /**
1505   * ccs_print_signal_acl - Print a signal ACL entry.   * ccs_set_group - Print "acl_group " header keyword and category name.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct signale_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  
                                  struct ccs_signal_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",  
                            ptr->sig, ptr->domainname->name) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_execute_handler_record - Print an execute handler ACL entry.  
1506   *   *
1507   * @head:    Pointer to "struct ccs_io_buffer".   * @head:     Pointer to "struct ccs_io_buffer".
1508   * @keyword: Name of the keyword.   * @category: Category name.
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
1509   *   *
1510   * Returns true on success, false otherwise.   * Returns nothing.
1511   */   */
1512  static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  static void ccs_set_group(struct ccs_io_buffer *head, const char *category)
                                              const char *keyword,  
                                              struct ccs_execute_handler_record *  
                                              ptr)  
1513  {  {
1514          return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);          if (head->type == CCS_EXCEPTIONPOLICY) {
1515                    ccs_print_namespace(head);
1516                    ccs_io_printf(head, "acl_group %u ", head->r.acl_group_index);
1517            }
1518            ccs_set_string(head, category);
1519  }  }
1520    
1521  /**  /**
1522   * ccs_print_mount_acl - Print a mount ACL entry.   * ccs_print_entry - Print an ACL entry.
1523   *   *
1524   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1525   * @ptr:  Pointer to "struct ccs_mount_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1526   *   *
1527   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1528   */   */
1529  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1530                                  struct ccs_mount_acl *ptr,                              const struct ccs_acl_info *acl)
                                 const struct ccs_condition *cond)  
1531  {  {
1532          const int pos = head->read_avail;          const u8 acl_type = acl->type;
1533          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||          const bool may_trigger_transition = acl->cond && acl->cond->transit;
1534              !ccs_print_name_union(head, &ptr->dev_name) ||          bool first = true;
1535              !ccs_print_name_union(head, &ptr->dir_name) ||          u8 bit;
1536              !ccs_print_name_union(head, &ptr->fs_type) ||          if (head->r.print_cond_part)
1537              !ccs_print_number_union(head, &ptr->flags) ||                  goto print_cond_part;
1538              !ccs_print_condition(head, cond)) {          if (acl->is_deleted)
1539                  head->read_avail = pos;                  return true;
1540            if (!ccs_flush(head))
1541                  return false;                  return false;
1542            else if (acl_type == CCS_TYPE_PATH_ACL) {
1543                    struct ccs_path_acl *ptr
1544                            = container_of(acl, typeof(*ptr), head);
1545                    const u16 perm = ptr->perm;
1546                    for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) {
1547                            if (!(perm & (1 << bit)))
1548                                    continue;
1549                            if (head->r.print_transition_related_only &&
1550                                bit != CCS_TYPE_EXECUTE && !may_trigger_transition)
1551                                    continue;
1552                            if (first) {
1553                                    ccs_set_group(head, "file ");
1554                                    first = false;
1555                            } else {
1556                                    ccs_set_slash(head);
1557                            }
1558                            ccs_set_string(head, ccs_path_keyword[bit]);
1559                    }
1560                    if (first)
1561                            return true;
1562                    ccs_print_name_union(head, &ptr->name);
1563            } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1564                       acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1565                    struct ccs_handler_acl *ptr
1566                            = container_of(acl, typeof(*ptr), head);
1567                    ccs_set_group(head, "task ");
1568                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1569                                   ? "auto_execute_handler " :
1570                                   "denied_execute_handler ");
1571                    ccs_set_string(head, ptr->handler->name);
1572            } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1573                       acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1574                    struct ccs_task_acl *ptr =
1575                            container_of(acl, typeof(*ptr), head);
1576                    ccs_set_group(head, "task ");
1577                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1578                                   "auto_domain_transition " :
1579                                   "manual_domain_transition ");
1580                    ccs_set_string(head, ptr->domainname->name);
1581            } else if (head->r.print_transition_related_only &&
1582                       !may_trigger_transition) {
1583                    return true;
1584            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1585                    struct ccs_mkdev_acl *ptr =
1586                            container_of(acl, typeof(*ptr), head);
1587                    const u8 perm = ptr->perm;
1588                    for (bit = 0; bit < CCS_MAX_MKDEV_OPERATION; bit++) {
1589                            if (!(perm & (1 << bit)))
1590                                    continue;
1591                            if (first) {
1592                                    ccs_set_group(head, "file ");
1593                                    first = false;
1594                            } else {
1595                                    ccs_set_slash(head);
1596                            }
1597                            ccs_set_string(head, ccs_mac_keywords
1598                                           [ccs_pnnn2mac[bit]]);
1599                    }
1600                    if (first)
1601                            return true;
1602                    ccs_print_name_union(head, &ptr->name);
1603                    ccs_print_number_union(head, &ptr->mode);
1604                    ccs_print_number_union(head, &ptr->major);
1605                    ccs_print_number_union(head, &ptr->minor);
1606            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1607                    struct ccs_path2_acl *ptr =
1608                            container_of(acl, typeof(*ptr), head);
1609                    const u8 perm = ptr->perm;
1610                    for (bit = 0; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1611                            if (!(perm & (1 << bit)))
1612                                    continue;
1613                            if (first) {
1614                                    ccs_set_group(head, "file ");
1615                                    first = false;
1616                            } else {
1617                                    ccs_set_slash(head);
1618                            }
1619                            ccs_set_string(head, ccs_mac_keywords
1620                                           [ccs_pp2mac[bit]]);
1621                    }
1622                    if (first)
1623                            return true;
1624                    ccs_print_name_union(head, &ptr->name1);
1625                    ccs_print_name_union(head, &ptr->name2);
1626            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1627                    struct ccs_path_number_acl *ptr =
1628                            container_of(acl, typeof(*ptr), head);
1629                    const u8 perm = ptr->perm;
1630                    for (bit = 0; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {
1631                            if (!(perm & (1 << bit)))
1632                                    continue;
1633                            if (first) {
1634                                    ccs_set_group(head, "file ");
1635                                    first = false;
1636                            } else {
1637                                    ccs_set_slash(head);
1638                            }
1639                            ccs_set_string(head, ccs_mac_keywords
1640                                           [ccs_pn2mac[bit]]);
1641                    }
1642                    if (first)
1643                            return true;
1644                    ccs_print_name_union(head, &ptr->name);
1645                    ccs_print_number_union(head, &ptr->number);
1646            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1647                    struct ccs_env_acl *ptr =
1648                            container_of(acl, typeof(*ptr), head);
1649                    ccs_set_group(head, "misc env ");
1650                    ccs_set_string(head, ptr->env->name);
1651            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1652                    struct ccs_capability_acl *ptr =
1653                            container_of(acl, typeof(*ptr), head);
1654                    ccs_set_group(head, "capability ");
1655                    ccs_set_string(head, ccs_mac_keywords
1656                                   [ccs_c2mac[ptr->operation]]);
1657            } else if (acl_type == CCS_TYPE_INET_ACL) {
1658                    struct ccs_inet_acl *ptr =
1659                            container_of(acl, typeof(*ptr), head);
1660                    const u8 perm = ptr->perm;
1661                    for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1662                            if (!(perm & (1 << bit)))
1663                                    continue;
1664                            if (first) {
1665                                    ccs_set_group(head, "network inet ");
1666                                    ccs_set_string(head, ccs_proto_keyword
1667                                                   [ptr->protocol]);
1668                                    ccs_set_space(head);
1669                                    first = false;
1670                            } else {
1671                                    ccs_set_slash(head);
1672                            }
1673                            ccs_set_string(head, ccs_socket_keyword[bit]);
1674                    }
1675                    if (first)
1676                            return true;
1677                    ccs_set_space(head);
1678                    if (ptr->address.group) {
1679                            ccs_set_string(head, "@");
1680                            ccs_set_string(head,
1681                                           ptr->address.group->group_name->name);
1682                    } else {
1683                            char buf[128];
1684                            ccs_print_ip(buf, sizeof(buf), &ptr->address);
1685                            ccs_io_printf(head, "%s", buf);
1686                    }
1687                    ccs_print_number_union(head, &ptr->port);
1688            } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1689                    struct ccs_unix_acl *ptr =
1690                            container_of(acl, typeof(*ptr), head);
1691                    const u8 perm = ptr->perm;
1692                    for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1693                            if (!(perm & (1 << bit)))
1694                                    continue;
1695                            if (first) {
1696                                    ccs_set_group(head, "network unix ");
1697                                    ccs_set_string(head, ccs_proto_keyword
1698                                                   [ptr->protocol]);
1699                                    ccs_set_space(head);
1700                                    first = false;
1701                            } else {
1702                                    ccs_set_slash(head);
1703                            }
1704                            ccs_set_string(head, ccs_socket_keyword[bit]);
1705                    }
1706                    if (first)
1707                            return true;
1708                    ccs_print_name_union(head, &ptr->name);
1709            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1710                    struct ccs_signal_acl *ptr =
1711                            container_of(acl, typeof(*ptr), head);
1712                    ccs_set_group(head, "ipc signal ");
1713                    ccs_print_number_union_nospace(head, &ptr->sig);
1714                    ccs_set_space(head);
1715                    ccs_set_string(head, ptr->domainname->name);
1716            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1717                    struct ccs_mount_acl *ptr =
1718                            container_of(acl, typeof(*ptr), head);
1719                    ccs_set_group(head, "file mount");
1720                    ccs_print_name_union(head, &ptr->dev_name);
1721                    ccs_print_name_union(head, &ptr->dir_name);
1722                    ccs_print_name_union(head, &ptr->fs_type);
1723                    ccs_print_number_union(head, &ptr->flags);
1724            }
1725            if (acl->cond) {
1726                    head->r.print_cond_part = true;
1727                    head->r.cond_step = 0;
1728                    if (!ccs_flush(head))
1729                            return false;
1730    print_cond_part:
1731                    if (!ccs_print_condition(head, acl->cond))
1732                            return false;
1733                    head->r.print_cond_part = false;
1734            } else {
1735                    ccs_set_lf(head);
1736          }          }
1737          return true;          return true;
1738  }  }
1739    
1740  /**  /**
1741   * ccs_print_entry - Print an ACL entry.   * ccs_read_domain2 - Read domain policy.
1742   *   *
1743   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1744   * @ptr:  Pointer to an ACL entry.   * @list: Pointer to "struct list_head".
1745   *   *
1746   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1747     *
1748     * Caller holds ccs_read_lock().
1749   */   */
1750  static bool ccs_print_entry(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1751                              struct ccs_acl_info *ptr)                               struct list_head *list)
1752  {  {
1753          const struct ccs_condition *cond = ptr->cond;          list_for_each_cookie(head->r.acl, list) {
1754          const u8 acl_type = ptr->type;                  struct ccs_acl_info *ptr =
1755          if (ptr->is_deleted)                          list_entry(head->r.acl, typeof(*ptr), list);
1756                  return true;                  if (!ccs_print_entry(head, ptr))
1757          if (acl_type == CCS_TYPE_PATH_ACL) {                          return false;
                 struct ccs_path_acl *acl  
                         = container_of(ptr, struct ccs_path_acl, head);  
                 return ccs_print_path_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, struct ccs_path_number3_acl, head);  
                 return ccs_print_path_number3_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, struct ccs_path2_acl, head);  
                 return ccs_print_path2_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, struct ccs_path_number_acl, head);  
                 return ccs_print_path_number_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, struct ccs_env_acl, head);  
                 return ccs_print_env_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, struct ccs_capability_acl, head);  
                 return ccs_print_capability_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, struct ccs_ip_network_acl, head);  
                 return ccs_print_network_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, struct ccs_signal_acl, head);  
                 return ccs_print_signal_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, struct ccs_mount_acl, head);  
                 return ccs_print_mount_acl(head, acl, cond);  
1758          }          }
1759          BUG(); /* This must not happen. */          head->r.acl = NULL;
1760          return false;          return true;
1761  }  }
1762    
1763  /**  /**
1764   * ccs_read_domain_policy - Read domain policy.   * ccs_read_domain - Read domain policy.
1765   *   *
1766   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1767   *   *
1768     * Returns nothing.
1769     *
1770   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1771   */   */
1772  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1773  {  {
1774          struct list_head *dpos;          if (head->r.eof)
1775          struct list_head *apos;                  return;
1776          if (head->read_eof)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1777                  return;                  struct ccs_domain_info *domain =
1778          if (head->read_step == 0)                          list_entry(head->r.domain, typeof(*domain), list);
1779                  head->read_step = 1;                  switch (head->r.step) {
1780          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          u8 i;
1781                  struct ccs_domain_info *domain;                  case 0:
1782                  const char *quota_exceeded = "";                          if (domain->is_deleted &&
1783                  const char *transition_failed = "";                              !head->r.print_this_domain_only)
1784                  const char *ignore_global_allow_read = "";                                  continue;
1785                  const char *ignore_global_allow_env = "";                          /* Print domainname and flags. */
1786                  domain = list_entry(dpos, struct ccs_domain_info, list);                          ccs_set_string(head, domain->domainname->name);
1787                  if (head->read_step != 1)                          ccs_set_lf(head);
1788                          goto acl_loop;                          ccs_io_printf(head, "use_profile %u\n",
1789                  if (domain->is_deleted && !head->read_single_domain)                                        domain->profile);
1790                          continue;                          ccs_io_printf(head, "use_group %u\n", domain->group);
1791                  /* Print domainname and flags. */                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1792                  if (domain->quota_warned)                                  if (domain->flags[i])
1793                          quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";                                          ccs_set_string(head, ccs_dif[i]);
1794                  if (domain->domain_transition_failed)                          head->r.step++;
1795                          transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";                          ccs_set_lf(head);
1796                  if (domain->ignore_global_allow_read)                          /* fall through */
1797                          ignore_global_allow_read                  case 1:
1798                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          if (!ccs_read_domain2(head, &domain->acl_info_list[0]))
1799                  if (domain->ignore_global_allow_env)                                  return;
1800                          ignore_global_allow_env                          head->r.step++;
1801                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          /* fall through */
1802                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                  case 2:
1803                                     "%s%s%s%s\n", domain->domainname->name,                          if (!ccs_read_domain2(head, &domain->acl_info_list[1]))
1804                                     domain->profile, quota_exceeded,                                  return;
1805                                     transition_failed,                          head->r.step++;
1806                                     ignore_global_allow_read,                          if (!ccs_set_lf(head))
                                    ignore_global_allow_env))  
                         return;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         goto tail_mark;  
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
1807                                  return;                                  return;
1808                            /* fall through */
1809                    case 3:
1810                            head->r.step = 0;
1811                            if (head->r.print_this_domain_only)
1812                                    goto done;
1813                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1814          }          }
1815          head->read_eof = true;  done:
1816            head->r.eof = true;
1817  }  }
1818    
1819  /**  /**
# Line 1733  static int ccs_write_domain_profile(stru Line 1843  static int ccs_write_domain_profile(stru
1843          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1844                  return -EINVAL;                  return -EINVAL;
1845          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1846          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))          if (domain && (!ccs_policy_loaded ||
1847                           head->w.ns->profile_ptr[(u8) profile]))
1848                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1849          return 0;          return 0;
1850  }  }
# Line 1743  static int ccs_write_domain_profile(stru Line 1854  static int ccs_write_domain_profile(stru
1854   *   *
1855   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1856   *   *
1857     * Returns nothing.
1858     *
1859   * This is equivalent to doing   * This is equivalent to doing
1860   *   *
1861   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1754  static int ccs_write_domain_profile(stru Line 1867  static int ccs_write_domain_profile(stru
1867   */   */
1868  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1869  {  {
1870          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1871                  return;                  return;
1872          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1873                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1874                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1875                  if (domain->is_deleted)                  if (domain->is_deleted)
1876                          continue;                          continue;
1877                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1878                          return;                          return;
1879                    ccs_io_printf(head, "%u ", domain->profile);
1880                    ccs_set_string(head, domain->domainname->name);
1881                    ccs_set_lf(head);
1882          }          }
1883          head->read_eof = true;          head->r.eof = true;
1884  }  }
1885    
1886  /**  /**
1887   * ccs_write_pid: Specify PID to obtain domainname.   * ccs_write_pid - Specify PID to obtain domainname.
1888   *   *
1889   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1890   *   *
# Line 1778  static void ccs_read_domain_profile(stru Line 1892  static void ccs_read_domain_profile(stru
1892   */   */
1893  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1894  {  {
1895          head->read_eof = false;          head->r.eof = false;
1896          return 0;          return 0;
1897  }  }
1898    
# Line 1804  static void ccs_read_pid(struct ccs_io_b Line 1918  static void ccs_read_pid(struct ccs_io_b
1918          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1919          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1920          if (!buf) {          if (!buf) {
1921                  head->read_eof = true;                  head->r.eof = true;
1922                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1923          }          }
1924          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1925                  return;                  return;
1926          head->read_eof = true;          head->r.eof = true;
1927          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1928                  task_info = true;                  task_info = true;
1929          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1818  static void ccs_read_pid(struct ccs_io_b Line 1932  static void ccs_read_pid(struct ccs_io_b
1932          ccs_tasklist_lock();          ccs_tasklist_lock();
1933  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1934          if (global_pid)          if (global_pid)
1935                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1936          else          else
1937                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1938  #else  #else
1939          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1940  #endif  #endif
1941          if (p) {          if (p) {
1942                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1943                  ccs_flags = p->ccs_flags;                  ccs_flags = ccs_task_flags(p);
1944          }          }
1945          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1946          if (!domain)          if (!domain)
1947                  return;                  return;
1948          if (!task_info)          if (!task_info) {
1949                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1950                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1951          else          } else {
1952                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
                               "state[0]=%u state[1]=%u state[2]=%u", pid,  
1953                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1954                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1955                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1956                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER));
1957                                (u8) (ccs_flags >> 24),          }
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
1958  }  }
1959    
1960    /* String table for domain transition control keywords. */
1961    static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1962            [CCS_TRANSITION_CONTROL_NAMESPACE]     = "move_namespace ",
1963            [CCS_TRANSITION_CONTROL_NO_NAMESPACE]  = "no_move_namespace ",
1964            [CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1965            [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1966            [CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1967            [CCS_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1968            [CCS_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
1969    };
1970    
1971    /* String table for grouping keywords. */
1972    static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1973            [CCS_PATH_GROUP]    = "path_group ",
1974            [CCS_NUMBER_GROUP]  = "number_group ",
1975            [CCS_ADDRESS_GROUP] = "address_group ",
1976    };
1977    
1978  /**  /**
1979   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1980   *   *
1981   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1982   *   *
1983   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1984   */   */
1985  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1986  {  {
1987          char *data = head->write_buf;          const bool is_delete = head->w.is_delete;
1988          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          struct ccs_acl_param param = {
1989          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))                  .ns = head->w.ns,
1990                  return ccs_write_domain_keeper_policy(data, false, is_delete);                  .is_delete = is_delete,
1991          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  .data = head->write_buf,
1992                  return ccs_write_domain_keeper_policy(data, true, is_delete);          };
1993          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          u8 i;
1994                  return ccs_write_domain_initializer_policy(data, false,          if (ccs_str_starts(&param.data, "aggregator "))
1995                                                             is_delete);                  return ccs_write_aggregator(&param);
1996          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))          if (ccs_str_starts(&param.data, "deny_autobind "))
1997                  return ccs_write_domain_initializer_policy(data, true,                  return ccs_write_reserved_port(&param);
1998                                                             is_delete);          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1999          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&param.data, ccs_transition_type[i]))
2000                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_write_transition_control(&param, i);
2001          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_GROUP; i++)
2002                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&param.data, ccs_group_name[i]))
2003          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_group(&param, i);
2004                  return ccs_write_globally_usable_env_policy(data, is_delete);          if (ccs_str_starts(&param.data, "acl_group ")) {
2005          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))                  unsigned int group;
2006                  return ccs_write_pattern_policy(data, is_delete);                  char *data;
2007          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                  group = simple_strtoul(param.data, &data, 10);
2008                  return ccs_write_path_group_policy(data, is_delete);                  if (group < CCS_MAX_ACL_GROUPS && *data++ == ' ')
2009          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                          return ccs_write_domain2(head->w.ns,
2010                  return ccs_write_number_group_policy(data, is_delete);                                                   head->w.ns->acl_group[group],
2011          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                                                   data, is_delete);
2012                  return ccs_write_no_rewrite_policy(data, is_delete);          }
         if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))  
                 return ccs_write_address_group_policy(data, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))  
                 return ccs_write_reserved_port_policy(data, is_delete);  
2013          return -EINVAL;          return -EINVAL;
2014  }  }
2015    
2016  /**  /**
2017   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
2018   *   *
2019   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
2020     * @idx:  Index number.
2021     *
2022     * Returns true on success, false otherwise.
2023   *   *
2024   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
2025   */   */
2026  static void ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
2027  {  {
2028          if (head->read_eof)          struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
2029                  return;                                                         namespace_list);
2030          switch (head->read_step) {          struct list_head *list = &ns->group_list[idx];
2031          case 0:          list_for_each_cookie(head->r.group, list) {
2032                  head->read_var2 = NULL;                  struct ccs_group *group =
2033                  head->read_step = 1;                          list_entry(head->r.group, typeof(*group), head.list);
2034          case 1:                  list_for_each_cookie(head->r.acl, &group->member_list) {
2035                  if (!ccs_read_domain_keeper_policy(head))                          struct ccs_acl_head *ptr =
2036                          break;                                  list_entry(head->r.acl, typeof(*ptr), list);
2037                  head->read_var2 = NULL;                          if (ptr->is_deleted)
2038                  head->read_step = 2;                                  continue;
2039          case 2:                          if (!ccs_flush(head))
2040                  if (!ccs_read_globally_readable_policy(head))                                  return false;
2041                          break;                          ccs_print_namespace(head);
2042                  head->read_var2 = NULL;                          ccs_set_string(head, ccs_group_name[idx]);
2043                  head->read_step = 3;                          ccs_set_string(head, group->group_name->name);
2044          case 3:                          if (idx == CCS_PATH_GROUP) {
2045                  if (!ccs_read_globally_usable_env_policy(head))                                  ccs_set_space(head);
2046                          break;                                  ccs_set_string(head, container_of
2047                  head->read_var2 = NULL;                                                 (ptr, struct ccs_path_group,
2048                  head->read_step = 4;                                                  head)->member_name->name);
2049          case 4:                          } else if (idx == CCS_NUMBER_GROUP) {
2050                  if (!ccs_read_domain_initializer_policy(head))                                  ccs_print_number_union(head, &container_of
2051                          break;                                                 (ptr, struct ccs_number_group,
2052                  head->read_var2 = NULL;                                                  head)->number);
2053                  head->read_step = 6;                          } else if (idx == CCS_ADDRESS_GROUP) {
2054          case 6:                                  char buffer[128];
2055                  if (!ccs_read_aggregator_policy(head))                                  struct ccs_address_group *member =
2056                          break;                                          container_of(ptr, typeof(*member),
2057                  head->read_var2 = NULL;                                                       head);
2058                  head->read_step = 7;                                  ccs_print_ip(buffer, sizeof(buffer),
2059          case 7:                                               &member->address);
2060                  if (!ccs_read_file_pattern(head))                                  ccs_io_printf(head, " %s", buffer);
2061                          break;                          }
2062                  head->read_var2 = NULL;                          ccs_set_lf(head);
2063                  head->read_step = 8;                  }
2064          case 8:                  head->r.acl = NULL;
                 if (!ccs_read_no_rewrite_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 9;  
         case 9:  
                 if (!ccs_read_path_group_policy(head))  
                         break;  
                 head->read_var1 = NULL;  
                 head->read_var2 = NULL;  
                 head->read_step = 10;  
         case 10:  
                 if (!ccs_read_number_group_policy(head))  
                         break;  
                 head->read_var1 = NULL;  
                 head->read_var2 = NULL;  
                 head->read_step = 11;  
         case 11:  
                 if (!ccs_read_address_group_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 12;  
         case 12:  
                 if (!ccs_read_reserved_port_policy(head))  
                         break;  
                 head->read_eof = true;  
2065          }          }
2066            head->r.group = NULL;
2067            return true;
2068  }  }
2069    
2070  /**  /**
2071   * ccs_get_argv0 - Get argv[0].   * ccs_read_policy - Read "struct ccs_..._entry" list.
2072   *   *
2073   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
2074     * @idx:  Index number.
2075   *   *
2076   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
2077     *
2078     * Caller holds ccs_read_lock().
2079   */   */
2080  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
2081  {  {
2082          struct linux_binprm *bprm = ee->bprm;          struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
2083          char *arg_ptr = ee->tmp;                                                         namespace_list);
2084          int arg_len = 0;          struct list_head *list = &ns->policy_list[idx];
2085          unsigned long pos = bprm->p;          list_for_each_cookie(head->r.acl, list) {
2086          int offset = pos % PAGE_SIZE;                  struct ccs_acl_head *acl =
2087          bool done = false;                          container_of(head->r.acl, typeof(*acl), list);
2088          if (!bprm->argc)                  if (acl->is_deleted)
2089                  goto out;                          continue;
2090          while (1) {                  if (head->r.print_transition_related_only &&
2091                  if (!ccs_dump_page(bprm, pos, &ee->dump))                      idx != CCS_ID_TRANSITION_CONTROL)
2092                          goto out;                          continue;
2093                  pos += PAGE_SIZE - offset;                  if (!ccs_flush(head))
2094                  /* Read. */                          return false;
2095                  while (offset < PAGE_SIZE) {                  switch (idx) {
2096                          const char *kaddr = ee->dump.data;                  case CCS_ID_TRANSITION_CONTROL:
2097                          const unsigned char c = kaddr[offset++];                          {
2098                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                  struct ccs_transition_control *ptr =
2099                                  if (c == '\\') {                                          container_of(acl, typeof(*ptr), head);
2100                                          arg_ptr[arg_len++] = '\\';                                  ccs_print_namespace(head);
2101                                          arg_ptr[arg_len++] = '\\';                                  ccs_set_string(head,
2102                                  } else if (c > ' ' && c < 127) {                                                 ccs_transition_type[ptr->type]);
2103                                          arg_ptr[arg_len++] = c;                                  ccs_set_string(head, ptr->program ?
2104                                  } else {                                                 ptr->program->name : "any");
2105                                          arg_ptr[arg_len++] = '\\';                                  ccs_set_string(head, " from ");
2106                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                  ccs_set_string(head, ptr->domainname ?
2107                                          arg_ptr[arg_len++]                                                 ptr->domainname->name : "any");
2108                                                  = ((c >> 3) & 7) + '0';                          }
2109                                          arg_ptr[arg_len++] = (c & 7) + '0';                          break;
2110                                  }                  case CCS_ID_AGGREGATOR:
2111                          } else {                          {
2112                                  arg_ptr[arg_len] = '\0';                                  struct ccs_aggregator *ptr =
2113                                  done = true;                                          container_of(acl, typeof(*ptr), head);
2114                                  break;                                  ccs_print_namespace(head);
2115                                    ccs_set_string(head, "aggregator ");
2116                                    ccs_set_string(head, ptr->original_name->name);
2117