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

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

branches/ccs-patch/security/ccsecurity/policy_io.c revision 3731 by kumaneko, Fri Jun 4 01:42:54 2010 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 4784 by kumaneko, Thu Mar 31 03:06:59 2011 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/policy_io.c   * security/ccsecurity/policy_io.c
3   *   *
4   * Copyright (C) 2005-2010  NTT DATA CORPORATION   * Copyright (C) 2005-2011  NTT DATA CORPORATION
  *  
  * Version: 1.7.2+   2010/06/04  
  *  
  * 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.1   2011/04/01
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
         .preference.audit_task_info = true,  
         .preference.audit_path_info = true,  
         .preference.enforcing_penalty = 0,  
         .preference.enforcing_verbose = true,  
         .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,  
         .preference.learning_verbose = false,  
         .preference.learning_exec_realpath = true,  
         .preference.learning_exec_argv0 = true,  
         .preference.learning_symlink_target = true,  
         .preference.permissive_verbose = true  
 };  
71    
72  /* Profile version. Currently only 20090903 is defined. */  /**
73     * list_for_each_cookie - iterate over a list with cookie.
74     *
75     * @pos:  Pointer to "struct list_head".
76     * @head: Pointer to "struct list_head".
77     */
78    #define list_for_each_cookie(pos, head)                                 \
79            for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \
80                 pos != (head); pos = srcu_dereference(pos->next, &ccs_ss))
81    
82    
83    /* Profile version. Currently only 20100903 is defined. */
84  static unsigned int ccs_profile_version;  static unsigned int ccs_profile_version;
85    
86  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
87  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
88    
89  /* String table for functionality that takes 4 modes. */  /* String table for operation mode. */
90  static const char *ccs_mode_4[4] = {  const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
91          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED]   = "disabled",
92            [CCS_CONFIG_LEARNING]   = "learning",
93            [CCS_CONFIG_PERMISSIVE] = "permissive",
94            [CCS_CONFIG_ENFORCING]  = "enforcing"
95  };  };
96    
97  /* String table for /proc/ccs/profile */  /* String table for /proc/ccs/profile interface. */
98  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +  const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX
99                                      CCS_MAX_CAPABILITY_INDEX +                                      + CCS_MAX_MAC_CATEGORY_INDEX] = {
100                                      CCS_MAX_MAC_CATEGORY_INDEX] = {          /* CONFIG::file group */
101          [CCS_MAC_FILE_EXECUTE]          [CCS_MAC_FILE_EXECUTE]    = "execute",
102          = "file::execute",          [CCS_MAC_FILE_OPEN]       = "open",
103          [CCS_MAC_FILE_OPEN]          [CCS_MAC_FILE_CREATE]     = "create",
104          = "file::open",          [CCS_MAC_FILE_UNLINK]     = "unlink",
105          [CCS_MAC_FILE_CREATE]          [CCS_MAC_FILE_GETATTR]    = "getattr",
106          = "file::create",          [CCS_MAC_FILE_MKDIR]      = "mkdir",
107          [CCS_MAC_FILE_UNLINK]          [CCS_MAC_FILE_RMDIR]      = "rmdir",
108          = "file::unlink",          [CCS_MAC_FILE_MKFIFO]     = "mkfifo",
109          [CCS_MAC_FILE_MKDIR]          [CCS_MAC_FILE_MKSOCK]     = "mksock",
110          = "file::mkdir",          [CCS_MAC_FILE_TRUNCATE]   = "truncate",
111          [CCS_MAC_FILE_RMDIR]          [CCS_MAC_FILE_SYMLINK]    = "symlink",
112          = "file::rmdir",          [CCS_MAC_FILE_MKBLOCK]    = "mkblock",
113          [CCS_MAC_FILE_MKFIFO]          [CCS_MAC_FILE_MKCHAR]     = "mkchar",
114          = "file::mkfifo",          [CCS_MAC_FILE_LINK]       = "link",
115          [CCS_MAC_FILE_MKSOCK]          [CCS_MAC_FILE_RENAME]     = "rename",
116          = "file::mksock",          [CCS_MAC_FILE_CHMOD]      = "chmod",
117          [CCS_MAC_FILE_TRUNCATE]          [CCS_MAC_FILE_CHOWN]      = "chown",
118          = "file::truncate",          [CCS_MAC_FILE_CHGRP]      = "chgrp",
119          [CCS_MAC_FILE_SYMLINK]          [CCS_MAC_FILE_IOCTL]      = "ioctl",
120          = "file::symlink",          [CCS_MAC_FILE_CHROOT]     = "chroot",
121          [CCS_MAC_FILE_REWRITE]          [CCS_MAC_FILE_MOUNT]      = "mount",
122          = "file::rewrite",          [CCS_MAC_FILE_UMOUNT]     = "unmount",
123          [CCS_MAC_FILE_MKBLOCK]          [CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root",
124          = "file::mkblock",          /* CONFIG::misc group */
125          [CCS_MAC_FILE_MKCHAR]          [CCS_MAC_ENVIRON] = "env",
126          = "file::mkchar",          /* CONFIG::network group */
127          [CCS_MAC_FILE_LINK]          [CCS_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
128          = "file::link",          [CCS_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
129          [CCS_MAC_FILE_RENAME]          [CCS_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
130          = "file::rename",          [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]     = "inet_stream_accept",
131          [CCS_MAC_FILE_CHMOD]          [CCS_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
132          = "file::chmod",          [CCS_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
133          [CCS_MAC_FILE_CHOWN]          [CCS_MAC_NETWORK_INET_DGRAM_RECV]        = "inet_dgram_recv",
134          = "file::chown",          [CCS_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
135          [CCS_MAC_FILE_CHGRP]          [CCS_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
136          = "file::chgrp",          [CCS_MAC_NETWORK_INET_RAW_RECV]          = "inet_raw_recv",
137          [CCS_MAC_FILE_IOCTL]          [CCS_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
138          = "file::ioctl",          [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
139          [CCS_MAC_FILE_CHROOT]          [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
140          = "file::chroot",          [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]     = "unix_stream_accept",
141          [CCS_MAC_FILE_MOUNT]          [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
142          = "file::mount",          [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
143          [CCS_MAC_FILE_UMOUNT]          [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]        = "unix_dgram_recv",
144          = "file::umount",          [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
145          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
146          = "file::pivot_root",          [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
147          [CCS_MAC_FILE_TRANSIT]          [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  = "unix_seqpacket_accept",
148          = "file::transit",          /* CONFIG::ipc group */
149          [CCS_MAC_ENVIRON]          [CCS_MAC_SIGNAL] = "signal",
150          = "misc::env",          /* CONFIG::capability group */
151          [CCS_MAC_NETWORK_UDP_BIND]          [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET]  = "use_route",
152          = "network::inet_udp_bind",          [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet",
153          [CCS_MAC_NETWORK_UDP_CONNECT]          [CCS_MAC_CAPABILITY_SYS_REBOOT]        = "SYS_REBOOT",
154          = "network::inet_udp_connect",          [CCS_MAC_CAPABILITY_SYS_VHANGUP]       = "SYS_VHANGUP",
155          [CCS_MAC_NETWORK_TCP_BIND]          [CCS_MAC_CAPABILITY_SYS_SETTIME]       = "SYS_TIME",
156          = "network::inet_tcp_bind",          [CCS_MAC_CAPABILITY_SYS_NICE]          = "SYS_NICE",
157          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME]   = "SYS_SETHOSTNAME",
158          = "network::inet_tcp_listen",          [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module",
159          [CCS_MAC_NETWORK_TCP_CONNECT]          [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD]    = "SYS_KEXEC_LOAD",
160          = "network::inet_tcp_connect",          [CCS_MAC_CAPABILITY_SYS_PTRACE]        = "SYS_PTRACE",
161          [CCS_MAC_NETWORK_TCP_ACCEPT]          /* CONFIG group */
162          = "network::inet_tcp_accept",          [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE]       = "file",
163          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK]    = "network",
164          = "network::inet_raw_bind",          [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC]       = "misc",
165          [CCS_MAC_NETWORK_RAW_CONNECT]          [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC]        = "ipc",
166          = "network::inet_raw_connect",          [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
167          [CCS_MAC_SIGNAL]  };
168          = "ipc::signal",  
169          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  /* String table for path operation. */
170          = "capability::inet_tcp_create",  const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
171          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]          [CCS_TYPE_EXECUTE]    = "execute",
172          = "capability::inet_tcp_listen",          [CCS_TYPE_READ]       = "read",
173          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]          [CCS_TYPE_WRITE]      = "write",
174          = "capability::inet_tcp_connect",          [CCS_TYPE_APPEND]     = "append",
175          [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]          [CCS_TYPE_UNLINK]     = "unlink",
176          = "capability::use_inet_udp",          [CCS_TYPE_GETATTR]    = "getattr",
177          [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]          [CCS_TYPE_RMDIR]      = "rmdir",
178          = "capability::use_inet_ip",          [CCS_TYPE_TRUNCATE]   = "truncate",
179          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]          [CCS_TYPE_SYMLINK]    = "symlink",
180          = "capability::use_route",          [CCS_TYPE_CHROOT]     = "chroot",
181          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]          [CCS_TYPE_UMOUNT]     = "unmount",
182          = "capability::use_packet",  };
183          [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]  
184          = "capability::SYS_MOUNT",  /* String table for categories. */
185          [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]  static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
186          = "capability::SYS_UMOUNT",          [CCS_MAC_CATEGORY_FILE]       = "file",
187          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]          [CCS_MAC_CATEGORY_NETWORK]    = "network",
188          = "capability::SYS_REBOOT",          [CCS_MAC_CATEGORY_MISC]       = "misc",
189          [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]          [CCS_MAC_CATEGORY_IPC]        = "ipc",
190          = "capability::SYS_CHROOT",          [CCS_MAC_CATEGORY_CAPABILITY] = "capability",
191          [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]  };
192          = "capability::SYS_KILL",  
193          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]  /* String table for conditions. */
194          = "capability::SYS_VHANGUP",  const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
195          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]          [CCS_TASK_UID]             = "task.uid",
196          = "capability::SYS_TIME",          [CCS_TASK_EUID]            = "task.euid",
197          [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]          [CCS_TASK_SUID]            = "task.suid",
198          = "capability::SYS_NICE",          [CCS_TASK_FSUID]           = "task.fsuid",
199          [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]          [CCS_TASK_GID]             = "task.gid",
200          = "capability::SYS_SETHOSTNAME",          [CCS_TASK_EGID]            = "task.egid",
201          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]          [CCS_TASK_SGID]            = "task.sgid",
202          = "capability::use_kernel_module",          [CCS_TASK_FSGID]           = "task.fsgid",
203          [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]          [CCS_TASK_PID]             = "task.pid",
204          = "capability::create_fifo",          [CCS_TASK_PPID]            = "task.ppid",
205          [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]          [CCS_EXEC_ARGC]            = "exec.argc",
206          = "capability::create_block_dev",          [CCS_EXEC_ENVC]            = "exec.envc",
207          [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]          [CCS_TYPE_IS_SOCKET]       = "socket",
208          = "capability::create_char_dev",          [CCS_TYPE_IS_SYMLINK]      = "symlink",
209          [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]          [CCS_TYPE_IS_FILE]         = "file",
210          = "capability::create_unix_socket",          [CCS_TYPE_IS_BLOCK_DEV]    = "block",
211          [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]          [CCS_TYPE_IS_DIRECTORY]    = "directory",
212          = "capability::SYS_LINK",          [CCS_TYPE_IS_CHAR_DEV]     = "char",
213          [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]          [CCS_TYPE_IS_FIFO]         = "fifo",
214          = "capability::SYS_SYMLINK",          [CCS_MODE_SETUID]          = "setuid",
215          [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]          [CCS_MODE_SETGID]          = "setgid",
216          = "capability::SYS_RENAME",          [CCS_MODE_STICKY]          = "sticky",
217          [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]          [CCS_MODE_OWNER_READ]      = "owner_read",
218          = "capability::SYS_UNLINK",          [CCS_MODE_OWNER_WRITE]     = "owner_write",
219          [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]          [CCS_MODE_OWNER_EXECUTE]   = "owner_execute",
220          = "capability::SYS_CHMOD",          [CCS_MODE_GROUP_READ]      = "group_read",
221          [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]          [CCS_MODE_GROUP_WRITE]     = "group_write",
222          = "capability::SYS_CHOWN",          [CCS_MODE_GROUP_EXECUTE]   = "group_execute",
223          [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]          [CCS_MODE_OTHERS_READ]     = "others_read",
224          = "capability::SYS_IOCTL",          [CCS_MODE_OTHERS_WRITE]    = "others_write",
225          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]          [CCS_MODE_OTHERS_EXECUTE]  = "others_execute",
226          = "capability::SYS_KEXEC_LOAD",          [CCS_TASK_TYPE]            = "task.type",
227          [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]          [CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
228          = "capability::SYS_PIVOT_ROOT",          [CCS_EXEC_REALPATH]        = "exec.realpath",
229          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]          [CCS_SYMLINK_TARGET]       = "symlink.target",
230          = "capability::SYS_PTRACE",          [CCS_PATH1_UID]            = "path1.uid",
231          [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]          [CCS_PATH1_GID]            = "path1.gid",
232          = "capability::conceal_mount",          [CCS_PATH1_INO]            = "path1.ino",
233          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_PATH1_MAJOR]          = "path1.major",
234           + CCS_MAC_CATEGORY_FILE] = "file",          [CCS_PATH1_MINOR]          = "path1.minor",
235          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_PATH1_PERM]           = "path1.perm",
236           + CCS_MAC_CATEGORY_NETWORK] = "network",          [CCS_PATH1_TYPE]           = "path1.type",
237          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_PATH1_DEV_MAJOR]      = "path1.dev_major",
238           + CCS_MAC_CATEGORY_MISC] = "misc",          [CCS_PATH1_DEV_MINOR]      = "path1.dev_minor",
239          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_PATH2_UID]            = "path2.uid",
240           + CCS_MAC_CATEGORY_IPC] = "ipc",          [CCS_PATH2_GID]            = "path2.gid",
241          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_PATH2_INO]            = "path2.ino",
242           + CCS_MAC_CATEGORY_CAPABILITY] = "capability",          [CCS_PATH2_MAJOR]          = "path2.major",
243            [CCS_PATH2_MINOR]          = "path2.minor",
244            [CCS_PATH2_PERM]           = "path2.perm",
245            [CCS_PATH2_TYPE]           = "path2.type",
246            [CCS_PATH2_DEV_MAJOR]      = "path2.dev_major",
247            [CCS_PATH2_DEV_MINOR]      = "path2.dev_minor",
248            [CCS_PATH1_PARENT_UID]     = "path1.parent.uid",
249            [CCS_PATH1_PARENT_GID]     = "path1.parent.gid",
250            [CCS_PATH1_PARENT_INO]     = "path1.parent.ino",
251            [CCS_PATH1_PARENT_PERM]    = "path1.parent.perm",
252            [CCS_PATH2_PARENT_UID]     = "path2.parent.uid",
253            [CCS_PATH2_PARENT_GID]     = "path2.parent.gid",
254            [CCS_PATH2_PARENT_INO]     = "path2.parent.ino",
255            [CCS_PATH2_PARENT_PERM]    = "path2.parent.perm",
256    };
257    
258    /* String table for PREFERENCE keyword. */
259    static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
260            [CCS_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
261            [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
262            [CCS_PREF_ENFORCING_PENALTY]  = "enforcing_penalty",
263  };  };
264    
265  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
266  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
267    
268  /**  /**
269   * ccs_cap2keyword - Convert capability operation to capability name.   * ccs_yesno - Return "yes" or "no".
270   *   *
271   * @operation: The capability index.   * @value: Bool value.
272   *   *
273   * Returns the name of the specified capability's name.   * Returns "yes" if @value is not 0, "no" otherwise.
274   */   */
275  const char *ccs_cap2keyword(const u8 operation)  const char *ccs_yesno(const unsigned int value)
276  {  {
277          return operation < CCS_MAX_CAPABILITY_INDEX          return value ? "yes" : "no";
                 ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;  
278  }  }
279    
280    /* Prototype for ccs_addprintf(). */
281    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
282            __attribute__ ((format(printf, 3, 4)));
283    
284  /**  /**
285   * ccs_yesno - Return "yes" or "no".   * ccs_addprintf - strncat()-like-snprintf().
286   *   *
287   * @value: Bool value.   * @buffer: Buffer to write to. Must be '\0'-terminated.
288     * @len:    Size of @buffer.
289     * @fmt:    The printf()'s format string, followed by parameters.
290     *
291     * Returns nothing.
292   */   */
293  static const char *ccs_yesno(const unsigned int value)  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
294  {  {
295          return value ? "yes" : "no";          va_list args;
296            const int pos = strlen(buffer);
297            va_start(args, fmt);
298            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
299            va_end(args);
300  }  }
301    
302  /**  /**
303   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
304   *   *
305   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
306   *   *
307   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
308     */
309    static bool ccs_flush(struct ccs_io_buffer *head)
310    {
311            while (head->r.w_pos) {
312                    const char *w = head->r.w[0];
313                    size_t len = strlen(w);
314                    if (len) {
315                            if (len > head->read_user_buf_avail)
316                                    len = head->read_user_buf_avail;
317                            if (!len)
318                                    return false;
319                            if (copy_to_user(head->read_user_buf, w, len))
320                                    return false;
321                            head->read_user_buf_avail -= len;
322                            head->read_user_buf += len;
323                            w += len;
324                    }
325                    head->r.w[0] = w;
326                    if (*w)
327                            return false;
328                    /* Add '\0' for audit logs and query. */
329                    if (head->poll) {
330                            if (!head->read_user_buf_avail ||
331                                copy_to_user(head->read_user_buf, "", 1))
332                                    return false;
333                            head->read_user_buf_avail--;
334                            head->read_user_buf++;
335                    }
336                    head->r.w_pos--;
337                    for (len = 0; len < head->r.w_pos; len++)
338                            head->r.w[len] = head->r.w[len + 1];
339            }
340            head->r.avail = 0;
341            return true;
342    }
343    
344    /**
345     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
346     *
347     * @head:   Pointer to "struct ccs_io_buffer".
348     * @string: String to print.
349     *
350     * Returns nothing.
351     *
352     * Note that @string has to be kept valid until @head is kfree()d.
353     * This means that char[] allocated on stack memory cannot be passed to
354     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
355     */
356    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
357    {
358            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
359                    head->r.w[head->r.w_pos++] = string;
360                    ccs_flush(head);
361            } else
362                    printk(KERN_WARNING "Too many words in a line.\n");
363    }
364    
365    /* Prototype for ccs_io_printf(). */
366    static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
367            __attribute__ ((format(printf, 2, 3)));
368    
369    /**
370     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
371   *   *
372   * The snprintf() will truncate, but ccs_io_printf() won't.   * @head: Pointer to "struct ccs_io_buffer".
373     * @fmt:  The printf()'s format string, followed by parameters.
374     *
375     * Returns nothing.
376   */   */
377  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, ...)
378  {  {
379          va_list args;          va_list args;
380          int len;          size_t len;
381          int pos = head->read_avail;          size_t pos = head->r.avail;
382          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
383          if (size <= 0)          if (size <= 0)
384                  return false;                  return;
385          va_start(args, fmt);          va_start(args, fmt);
386          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
387          va_end(args);          va_end(args);
388          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
389                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
390          head->read_avail += len;                  return;
391          return true;          }
392            head->r.avail += len;
393            ccs_set_string(head, head->read_buf + pos);
394    }
395    
396    /**
397     * ccs_set_space - Put a space to "struct ccs_io_buffer" structure.
398     *
399     * @head: Pointer to "struct ccs_io_buffer".
400     *
401     * Returns nothing.
402     */
403    static void ccs_set_space(struct ccs_io_buffer *head)
404    {
405            ccs_set_string(head, " ");
406    }
407    
408    /**
409     * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure.
410     *
411     * @head: Pointer to "struct ccs_io_buffer".
412     *
413     * Returns nothing.
414     */
415    static bool ccs_set_lf(struct ccs_io_buffer *head)
416    {
417            ccs_set_string(head, "\n");
418            return !head->r.w_pos;
419    }
420    
421    /**
422     * ccs_set_slash - Put a shash to "struct ccs_io_buffer" structure.
423     *
424     * @head: Pointer to "struct ccs_io_buffer".
425     *
426     * Returns nothing.
427     */
428    static void ccs_set_slash(struct ccs_io_buffer *head)
429    {
430            ccs_set_string(head, "/");
431  }  }
432    
433  /**  /**
# Line 267  static struct ccs_profile *ccs_assign_pr Line 452  static struct ccs_profile *ccs_assign_pr
452          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
453          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
454                  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;  
455                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
456                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
457                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
458                         sizeof(ptr->config));                         sizeof(ptr->config));
459                    ptr->pref[CCS_PREF_MAX_AUDIT_LOG] =
460                            CONFIG_CCSECURITY_MAX_AUDIT_LOG;
461                    ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] =
462                            CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY;
463                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
464                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
465                  entry = NULL;                  entry = NULL;
466          }          }
467          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
468   out:  out:
469          kfree(entry);          kfree(entry);
470          return ptr;          return ptr;
471  }  }
472    
473  /**  /**
474   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
475     *
476     * Returns nothing.
477   */   */
478  static void ccs_check_profile(void)  static void ccs_check_profile(void)
479  {  {
480          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
481          const int idx = ccs_read_lock();          const int idx = ccs_read_lock();
482          ccs_policy_loaded = true;          ccs_policy_loaded = true;
483          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
484                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
485                  if (ccs_profile_ptr[profile])                  if (ccs_profile_ptr[profile])
486                          continue;                          continue;
487                    printk(KERN_ERR "Profile %u must be defined before using it.\n",
488                           profile);
489                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
490                           "for more information.\n");
491                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
492                        profile, domain->domainname->name);                        profile, domain->domainname->name);
493          }          }
494          ccs_read_unlock(idx);          ccs_read_unlock(idx);
495          if (ccs_profile_version != 20090903)          if (ccs_profile_version != 20100903) {
496                    printk(KERN_ERR "Userland tools must be installed for "
497                           "TOMOYO 1.8, and policy must be initialized.\n");
498                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
499                           "for more information.\n");
500                  panic("Profile version %u is not supported.\n",                  panic("Profile version %u is not supported.\n",
501                        ccs_profile_version);                        ccs_profile_version);
502          printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");          }
503            printk(KERN_INFO "CCSecurity: 1.8.1   2011/04/01\n");
504          printk(KERN_INFO "Mandatory Access Control activated.\n");          printk(KERN_INFO "Mandatory Access Control activated.\n");
505  }  }
506    
# Line 317  static void ccs_check_profile(void) Line 513  static void ccs_check_profile(void)
513   */   */
514  struct ccs_profile *ccs_profile(const u8 profile)  struct ccs_profile *ccs_profile(const u8 profile)
515  {  {
516            static struct ccs_profile ccs_null_profile;
517          struct ccs_profile *ptr = ccs_profile_ptr[profile];          struct ccs_profile *ptr = ccs_profile_ptr[profile];
518          if (!ccs_policy_loaded)          if (!ptr)
519                  return &ccs_default_profile;                  ptr = &ccs_null_profile;
         BUG_ON(!ptr);  
520          return ptr;          return ptr;
521  }  }
522    
523  /**  /**
524   * ccs_write_profile - Write profile table.   * ccs_find_yesno - Find values for specified keyword.
525   *   *
526   * @head: Pointer to "struct ccs_io_buffer".   * @string: String to check.
527     * @find:   Name of keyword.
528   *   *
529   * Returns 0 on success, negative value otherwise.   * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
530   */   */
531  static int ccs_write_profile(struct ccs_io_buffer *head)  static s8 ccs_find_yesno(const char *string, const char *find)
532  {  {
533          char *data = head->write_buf;          const char *cp = strstr(string, find);
534          unsigned int i;          if (cp) {
535          int value;                  cp += strlen(find);
536          int mode;                  if (!strncmp(cp, "=yes", 4))
537          u8 config;                          return 1;
538          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_assign_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, CCS_KEYWORD_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, CCS_KEYWORD_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, CCS_KEYWORD_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, CCS_KEYWORD_PREFERENCE_LEARNING)) {  
                 char *cp2;  
                 if (use_default) {  
                         profile->learning = &ccs_default_profile.preference;  
539                          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;  
540          }          }
541          if (profile == &ccs_default_profile)          return -1;
542                  return -EINVAL;  }
543          if (!strcmp(data, "COMMENT")) {  
544                  const struct ccs_path_info *old_comment = profile->comment;  /**
545                  profile->comment = ccs_get_name(cp);   * ccs_set_uint - Set value for specified preference.
546                  ccs_put_name(old_comment);   *
547                  return 0;   * @i:      Pointer to "unsigned int".
548          }   * @string: String to check.
549          if (!strcmp(data, "CONFIG")) {   * @find:   Name of keyword.
550                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX   *
551                          + CCS_MAX_MAC_CATEGORY_INDEX;   * Returns nothing.
552     */
553    static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
554    {
555            const char *cp = strstr(string, find);
556            if (cp)
557                    sscanf(cp + strlen(find), "=%u", i);
558    }
559    
560    /**
561     * ccs_set_mode - Set mode for specified profile.
562     *
563     * @name:    Name of functionality.
564     * @value:   Mode for @name.
565     * @profile: Pointer to "struct ccs_profile".
566     *
567     * Returns 0 on success, negative value otherwise.
568     */
569    static int ccs_set_mode(char *name, const char *value,
570                            struct ccs_profile *profile)
571    {
572            u8 i;
573            u8 config;
574            if (!strcmp(name, "CONFIG")) {
575                    i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
576                  config = profile->default_config;                  config = profile->default_config;
577          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
578                  config = 0;                  config = 0;
579                  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;
580                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                       i++) {
581                          if (strcmp(data, ccs_mac_keywords[i]))                          int len = 0;
582                            if (i < CCS_MAX_MAC_INDEX) {
583                                    const u8 c = ccs_index2category[i];
584                                    const char *category =
585                                            ccs_category_keywords[c];
586                                    len = strlen(category);
587                                    if (strncmp(name, category, len) ||
588                                        name[len++] != ':' || name[len++] != ':')
589                                            continue;
590                            }
591                            if (strcmp(name + len, ccs_mac_keywords[i]))
592                                  continue;                                  continue;
593                          config = profile->config[i];                          config = profile->config[i];
594                          break;                          break;
595                  }                  }
596                  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)  
597                          return -EINVAL;                          return -EINVAL;
598          } else {          } else {
599                  return -EINVAL;                  return -EINVAL;
600          }          }
601          if (use_default) {          if (strstr(value, "use_default")) {
602                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
603          } else {          } else {
604                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
605                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
606                            if (strstr(value, ccs_mode[mode]))
607                                  /*                                  /*
608                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
609                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
610                                   */                                   */
611                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
612                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
613                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "grant_log")) {
614                            case 1:
615                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
616                          else if (strstr(cp, "grant_log=no"))                                  break;
617                            case 0:
618                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
619                          if (strstr(cp, "reject_log=yes"))                                  break;
620                            }
621                            switch (ccs_find_yesno(value, "reject_log")) {
622                            case 1:
623                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
624                          else if (strstr(cp, "reject_log=no"))                                  break;
625                            case 0:
626                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
627                                    break;
628                            }
629                  }                  }
 #endif  
630          }          }
631          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)  
632                  profile->config[i] = config;                  profile->config[i] = config;
633          else if (config != CCS_CONFIG_USE_DEFAULT)          else if (config != CCS_CONFIG_USE_DEFAULT)
634                  profile->default_config = config;                  profile->default_config = config;
635          return 0;          return 0;
636  }  }
637    
638  static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)  /**
639     * ccs_write_profile - Write profile table.
640     *
641     * @head: Pointer to "struct ccs_io_buffer".
642     *
643     * Returns 0 on success, negative value otherwise.
644     */
645    static int ccs_write_profile(struct ccs_io_buffer *head)
646  {  {
647          struct ccs_preference *pref = &ccs_default_profile.preference;          char *data = head->write_buf;
648          const struct ccs_profile *profile = idx >= 0 ?          unsigned int i;
649                  ccs_profile_ptr[idx] : NULL;          char *cp;
650          char buffer[16] = "";          struct ccs_profile *profile;
651          if (profile) {          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
652                  buffer[sizeof(buffer) - 1] = '\0';                  return 0;
653                  snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);          i = simple_strtoul(data, &cp, 10);
654          }          if (*cp != '-')
655          if (profile) {                  return -EINVAL;
656                  pref = profile->audit;          data = cp + 1;
657                  if (pref == &ccs_default_profile.preference)          profile = ccs_assign_profile(i);
658                          pref = NULL;          if (!profile)
659          }                  return -EINVAL;
660          if (pref && !ccs_io_printf(head, "%s%s={ "          cp = strchr(data, '=');
661  #ifdef CONFIG_CCSECURITY_AUDIT          if (!cp)
662                                     "max_grant_log=%u max_reject_log=%u "                  return -EINVAL;
663  #endif          *cp++ = '\0';
664                                     "task_info=%s path_info=%s }\n", buffer,          if (!strcmp(data, "COMMENT")) {
665                                     CCS_KEYWORD_PREFERENCE_AUDIT,                  static DEFINE_SPINLOCK(lock);
666  #ifdef CONFIG_CCSECURITY_AUDIT                  const struct ccs_path_info *new_comment = ccs_get_name(cp);
667                                     pref->audit_max_grant_log,                  const struct ccs_path_info *old_comment;
668                                     pref->audit_max_reject_log,                  if (!new_comment)
669  #endif                          return -ENOMEM;
670                                     ccs_yesno(pref->audit_task_info),                  spin_lock(&lock);
671                                     ccs_yesno(pref->audit_path_info)))                  old_comment = profile->comment;
672                  return false;                  profile->comment = new_comment;
673          if (profile) {                  spin_unlock(&lock);
674                  pref = profile->learning;                  ccs_put_name(old_comment);
675                  if (pref == &ccs_default_profile.preference)                  return 0;
676                          pref = NULL;          }
677          }          if (!strcmp(data, "PREFERENCE")) {
678          if (pref && !ccs_io_printf(head, "%s%s={ "                  for (i = 0; i < CCS_MAX_PREF; i++)
679                                     "verbose=%s max_entry=%u exec.realpath=%s "                          ccs_set_uint(&profile->pref[i], cp,
680                                     "exec.argv0=%s symlink.target=%s }\n",                                       ccs_pref_keywords[i]);
681                                     buffer, CCS_KEYWORD_PREFERENCE_LEARNING,                  return 0;
682                                     ccs_yesno(pref->learning_verbose),          }
683                                     pref->learning_max_entry,          return ccs_set_mode(data, cp, profile);
684                                     ccs_yesno(pref->learning_exec_realpath),  }
685                                     ccs_yesno(pref->learning_exec_argv0),  
686                                     ccs_yesno(pref->learning_symlink_target)))  /**
687                  return false;   * ccs_print_config - Print mode for specified functionality.
688          if (profile) {   *
689                  pref = profile->permissive;   * @head:   Pointer to "struct ccs_io_buffer".
690                  if (pref == &ccs_default_profile.preference)   * @config: Mode for that functionality.
691                          pref = NULL;   *
692          }   * Returns nothing.
693          if (pref && !ccs_io_printf(head, "%s%s={ verbose=%s }\n", buffer,   *
694                                     CCS_KEYWORD_PREFERENCE_PERMISSIVE,   * Caller prints functionality's name.
695                                     ccs_yesno(pref->permissive_verbose)))   */
696                  return false;  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
697          if (profile) {  {
698                  pref = profile->enforcing;          ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
699                  if (pref == &ccs_default_profile.preference)                        ccs_mode[config & 3],
700                          pref = NULL;                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
701          }                        ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
         return !pref || ccs_io_printf(head, "%s%s={ verbose=%s penalty=%u }\n",  
                                       buffer, CCS_KEYWORD_PREFERENCE_ENFORCING,  
                                       ccs_yesno(pref->enforcing_verbose),  
                                       pref->enforcing_penalty);  
702  }  }
703    
704  /**  /**
705   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
706   *   *
707   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
708     *
709     * Returns nothing.
710   */   */
711  static void ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
712  {  {
713          int index;          u8 index;
714          if (head->read_eof)          const struct ccs_profile *profile;
715                  return;  next:
716          if (head->read_bit)          index = head->r.index;
717                  goto body;          profile = ccs_profile_ptr[index];
718          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          switch (head->r.step) {
719          ccs_print_preference(head, -1);          case 0:
720          head->read_bit = 1;                  ccs_io_printf(head, "PROFILE_VERSION=%u\n", 20100903);
721   body:                  head->r.step++;
722          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {                  break;
723                  bool done;          case 1:
724                  u8 config;                  for ( ; head->r.index < CCS_MAX_PROFILES;
725                  int i;                        head->r.index++)
726                  int pos;                          if (ccs_profile_ptr[head->r.index])
727                  const struct ccs_profile *profile = ccs_profile_ptr[index];                                  break;
728                  const struct ccs_path_info *comment;                  if (head->r.index == CCS_MAX_PROFILES)
729                  head->read_step = index;                          return;
730                  if (!profile)                  head->r.step++;
731                          continue;                  break;
732                  pos = head->read_avail;          case 2:
733                  comment = profile->comment;                  {
734                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,                          u8 i;
735                                       comment ? comment->name : "");                          const struct ccs_path_info *comment = profile->comment;
736                  if (!done)                          ccs_io_printf(head, "%u-COMMENT=", index);
737                          goto out;                          ccs_set_string(head, comment ? comment->name : "");
738                  config = profile->default_config;                          ccs_set_lf(head);
739  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-PREFERENCE={ ", index);
740                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                          for (i = 0; i < CCS_MAX_PREF; i++)
741                                     "grant_log=%s reject_log=%s }\n", index,                                  ccs_io_printf(head, "%s=%u ",
742                                     "CONFIG", "", ccs_mode_4[config & 3],                                                ccs_pref_keywords[i],
743                                     ccs_yesno(config &                                                profile->pref[i]);
744                                               CCS_CONFIG_WANT_GRANT_LOG),                          ccs_set_string(head, " }\n");
745                                     ccs_yesno(config &                          head->r.step++;
746                                               CCS_CONFIG_WANT_REJECT_LOG)))                  }
747                          goto out;                  break;
748  #else          case 3:
749                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,                  {
750                                     "CONFIG", "", ccs_mode_4[config & 3]))                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
751                          goto out;                          ccs_print_config(head, profile->default_config);
752  #endif                          head->r.bit = 0;
753                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                          head->r.step++;
754                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                  }
755  #ifdef CONFIG_CCSECURITY_AUDIT                  break;
756                          const char *g;          case 4:
757                          const char *r;                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
758  #endif                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
759                          config = profile->config[i];                          const u8 i = head->r.bit;
760                            const u8 config = profile->config[i];
761                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
762                                  continue;                                  continue;
763  #ifdef CONFIG_CCSECURITY_AUDIT                          if (i < CCS_MAX_MAC_INDEX)
764                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                  ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
765                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                                                ccs_category_keywords
766                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                                                [ccs_index2category[i]],
767                                             "grant_log=%s reject_log=%s }\n",                                                ccs_mac_keywords[i]);
768                                             index, "CONFIG::",                          else
769                                             ccs_mac_keywords[i],                                  ccs_io_printf(head, "%u-CONFIG::%s", index,
770                                             ccs_mode_4[config & 3], g, r))                                                ccs_mac_keywords[i]);
771                                  goto out;                          ccs_print_config(head, config);
772  #else                          head->r.bit++;
773                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",                          break;
774                                             index, "CONFIG::",                  }
775                                             ccs_mac_keywords[i],                  if (head->r.bit == CCS_MAX_MAC_INDEX
776                                             ccs_mode_4[config & 3]))                      + CCS_MAX_MAC_CATEGORY_INDEX) {
777                                  goto out;                          head->r.index++;
778  #endif                          head->r.step = 1;
779                  }                  }
                 if (!ccs_print_preference(head, index))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
780                  break;                  break;
781          }          }
782          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
783                  head->read_eof = true;                  goto next;
784  }  }
785    
786  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,  /**
787                                     const struct ccs_acl_head *b)   * ccs_same_manager - Check for duplicated "struct ccs_manager" entry.
788     *
789     * @a: Pointer to "struct ccs_acl_head".
790     * @b: Pointer to "struct ccs_acl_head".
791     *
792     * Returns true if @a == @b, false otherwise.
793     */
794    static bool ccs_same_manager(const struct ccs_acl_head *a,
795                                 const struct ccs_acl_head *b)
796  {  {
797          return container_of(a, struct ccs_manager, head)->manager          return container_of(a, struct ccs_manager, head)->manager
798                  == container_of(b, struct ccs_manager, head)->manager;                  == container_of(b, struct ccs_manager, head)->manager;
# Line 683  static int ccs_update_manager_entry(cons Line 822  static int ccs_update_manager_entry(cons
822          if (!e.manager)          if (!e.manager)
823                  return error;                  return error;
824          error = ccs_update_policy(&e.head, sizeof(e), is_delete,          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
825                                    CCS_ID_MANAGER, ccs_same_manager_entry);                                    &ccs_policy_list[CCS_ID_MANAGER],
826                                      ccs_same_manager);
827          ccs_put_name(e.manager);          ccs_put_name(e.manager);
828          return error;          return error;
829  }  }
# Line 698  static int ccs_update_manager_entry(cons Line 838  static int ccs_update_manager_entry(cons
838  static int ccs_write_manager(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
839  {  {
840          char *data = head->write_buf;          char *data = head->write_buf;
841          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, "delete ");
842          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
843                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
844                  return 0;                  return 0;
# Line 711  static int ccs_write_manager(struct ccs_ Line 851  static int ccs_write_manager(struct ccs_
851   *   *
852   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
853   *   *
854     * Returns nothing.
855     *
856   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
857   */   */
858  static void ccs_read_manager(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
859  {  {
860          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
861                  return;                  return;
862          list_for_each_cookie(pos, head->read_var2,          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
863                               &ccs_policy_list[CCS_ID_MANAGER]) {                  struct ccs_manager *ptr =
864                  struct ccs_manager *ptr                          list_entry(head->r.acl, typeof(*ptr), head.list);
                         = list_entry(pos, typeof(*ptr), head.list);  
865                  if (ptr->head.is_deleted)                  if (ptr->head.is_deleted)
866                          continue;                          continue;
867                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
868                          return;                          return;
869                    ccs_set_string(head, ptr->manager->name);
870                    ccs_set_lf(head);
871          }          }
872          head->read_eof = true;          head->r.eof = true;
873  }  }
874    
875  /**  /**
# Line 742  static bool ccs_manager(void) Line 884  static bool ccs_manager(void)
884  {  {
885          struct ccs_manager *ptr;          struct ccs_manager *ptr;
886          const char *exe;          const char *exe;
887          struct task_struct *task = current;          struct ccs_security *task = ccs_current_security();
888          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
889                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
890          bool found = false;          bool found = false;
# Line 753  static bool ccs_manager(void) Line 895  static bool ccs_manager(void)
895          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
896                  return false;                  return false;
897          exe = ccs_get_exe();          exe = ccs_get_exe();
898          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],          list_for_each_entry_srcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
899                                  head.list) {                                   head.list, &ccs_ss) {
900                  if (ptr->head.is_deleted)                  if (ptr->head.is_deleted)
901                          continue;                          continue;
902                  if (ptr->is_domain) {                  if (ptr->is_domain) {
# Line 783  static bool ccs_manager(void) Line 925  static bool ccs_manager(void)
925  }  }
926    
927  /**  /**
  * ccs_find_condition_part - Find condition part from the statement.  
  *  
  * @data: String to parse.  
  *  
  * Returns pointer to the condition part if it was found in the statement,  
  * NULL otherwise.  
  */  
 static char *ccs_find_condition_part(char *data)  
 {  
         char *cp = strstr(data, " if ");  
         if (!cp)  
                 cp = strstr(data, " ; set ");  
         if (cp)  
                 *cp++ = '\0';  
         return cp;  
 }  
   
 /**  
928   * ccs_select_one - Parse select command.   * ccs_select_one - Parse select command.
929   *   *
930   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 815  static bool ccs_select_one(struct ccs_io Line 939  static bool ccs_select_one(struct ccs_io
939          unsigned int pid;          unsigned int pid;
940          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
941          bool global_pid = false;          bool global_pid = false;
942          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "transition_only")) {
943                  head->read_execute_only = true;                  head->r.print_transition_related_only = true;
944                  return true;                  return true;
945          }          }
946          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
# Line 840  static bool ccs_select_one(struct ccs_io Line 964  static bool ccs_select_one(struct ccs_io
964                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
965          } else          } else
966                  return false;                  return false;
967          head->write_var1 = domain;          head->w.domain = domain;
968          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
969          if (!head->read_buf)          if (!head->read_buf)
970                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
971          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
972            head->r.print_this_domain_only = true;
973            if (domain)
974                    head->r.domain = &domain->list;
975            else
976                    head->r.eof = true;
977          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
978          head->read_single_domain = true;          if (domain && domain->is_deleted)
979          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");  
         }  
980          return true;          return true;
981  }  }
982    
983    /**
984     * ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry.
985     *
986     * @a: Pointer to "struct ccs_acl_info".
987     * @b: Pointer to "struct ccs_acl_info".
988     *
989     * Returns true if @a == @b, false otherwise.
990     */
991    static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
992                                     const struct ccs_acl_info *b)
993    {
994            const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
995            const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
996            return p1->handler == p2->handler;
997    }
998    
999    /**
1000     * ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry.
1001     *
1002     * @a: Pointer to "struct ccs_acl_info".
1003     * @b: Pointer to "struct ccs_acl_info".
1004     *
1005     * Returns true if @a == @b, false otherwise.
1006     */
1007    static bool ccs_same_task_acl(const struct ccs_acl_info *a,
1008                                  const struct ccs_acl_info *b)
1009    {
1010            const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
1011            const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
1012            return p1->domainname == p2->domainname;
1013    }
1014    
1015    /**
1016     * ccs_write_task - Update task related list.
1017     *
1018     * @param: Pointer to "struct ccs_acl_param".
1019     *
1020     * Returns 0 on success, negative value otherwise.
1021     */
1022    static int ccs_write_task(struct ccs_acl_param *param)
1023    {
1024            int error;
1025            const bool is_auto = ccs_str_starts(&param->data,
1026                                                "auto_domain_transition ");
1027            if (!is_auto && !ccs_str_starts(&param->data,
1028                                            "manual_domain_transition ")) {
1029                    struct ccs_handler_acl e = { };
1030                    char *handler;
1031                    if (ccs_str_starts(&param->data, "auto_execute_handler "))
1032                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
1033                    else if (ccs_str_starts(&param->data,
1034                                            "denied_execute_handler "))
1035                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
1036                    else
1037                            return -EINVAL;
1038                    handler = ccs_read_token(param);
1039                    if (!ccs_correct_path(handler))
1040                            return -EINVAL;
1041                    e.handler = ccs_get_name(handler);
1042                    if (!e.handler)
1043                            return -ENOMEM;
1044                    if (e.handler->is_patterned)
1045                            error = -EINVAL; /* No patterns allowed. */
1046                    else
1047                            error = ccs_update_domain(&e.head, sizeof(e), param,
1048                                                      ccs_same_handler_acl, NULL);
1049                    ccs_put_name(e.handler);
1050            } else {
1051                    struct ccs_task_acl e = {
1052                            .head.type = is_auto ?
1053                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
1054                            .domainname = ccs_get_domainname(param),
1055                    };
1056                    if (!e.domainname)
1057                            error = -EINVAL;
1058                    else
1059                            error = ccs_update_domain(&e.head, sizeof(e), param,
1060                                                      ccs_same_task_acl, NULL);
1061                    ccs_put_name(e.domainname);
1062            }
1063            return error;
1064    }
1065    
1066    /**
1067     * ccs_write_domain2 - Write domain policy.
1068     *
1069     * @data:      Policy to be interpreted.
1070     * @domain:    Pointer to "struct ccs_domain_info".
1071     * @is_delete: True if it is a delete request.
1072     *
1073     * Returns 0 on success, negative value otherwise.
1074     */
1075  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
1076                               struct ccs_condition *cond, const bool is_delete)                               const bool is_delete)
1077  {  {
1078          u8 i;          struct ccs_acl_param param = {
1079                    .data = data,
1080                    .domain = domain,
1081                    .is_delete = is_delete,
1082            };
1083          static const struct {          static const struct {
1084                  const char *keyword;                  const char *keyword;
1085                  int (*write) (char *, struct ccs_domain_info *,                  int (*write) (struct ccs_acl_param *);
1086                                struct ccs_condition *, const bool);          } ccs_callback[7] = {
1087          } ccs_callback[5] = {                  { "file ", ccs_write_file },
1088                  { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },                  { "network inet ", ccs_write_inet_network },
1089                  { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },                  { "network unix ", ccs_write_unix_network },
1090                  { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },                  { "misc ", ccs_write_misc },
1091                  { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },                  { "capability ", ccs_write_capability },
1092                  { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }                  { "ipc ", ccs_write_ipc },
1093                    { "task ", ccs_write_task },
1094          };          };
1095          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,          u8 i;
1096                        const bool) = ccs_write_file;          for (i = 0; i < 7; i++) {
1097          for (i = 0; i < 5; i++) {                  if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
                 if (!ccs_str_starts(&data, ccs_callback[i].keyword))  
1098                          continue;                          continue;
1099                  write = ccs_callback[i].write;                  return ccs_callback[i].write(&param);
                 break;  
1100          }          }
1101          return write(data, domain, cond, is_delete);          return -EINVAL;
1102  }  }
1103    
1104    /* String table for domain flags. */
1105    const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1106            [CCS_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
1107            [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
1108    };
1109    
1110  /**  /**
1111   * ccs_write_domain - Write domain policy.   * ccs_write_domain - Write domain policy.
1112   *   *
# Line 901  static int ccs_write_domain2(char *data, Line 1117  static int ccs_write_domain2(char *data,
1117  static int ccs_write_domain(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
1118  {  {
1119          char *data = head->write_buf;          char *data = head->write_buf;
1120          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1121          bool is_delete = false;          bool is_delete = false;
1122          bool is_select = false;          bool is_select = false;
1123          unsigned int profile;          unsigned int profile;
1124          struct ccs_condition *cond = NULL;          if (ccs_str_starts(&data, "delete "))
         char *cp;  
         int error;  
         if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))  
1125                  is_delete = true;                  is_delete = true;
1126          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, "select "))
1127                  is_select = true;                  is_select = true;
1128          if (is_select && ccs_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1129                  return 0;                  return -EAGAIN;
1130          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1131          if (!ccs_manager())          if (!ccs_manager())
1132                  return -EPERM;                  return -EPERM;
# Line 924  static int ccs_write_domain(struct ccs_i Line 1137  static int ccs_write_domain(struct ccs_i
1137                  else if (is_select)                  else if (is_select)
1138                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1139                  else                  else
1140                          domain = ccs_assign_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
1141                  head->write_var1 = domain;                  head->w.domain = domain;
1142                  return 0;                  return 0;
1143          }          }
1144          if (!domain)          if (!domain)
1145                  return -EINVAL;                  return -EINVAL;
1146    
1147          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, "use_profile %u\n", &profile) == 1
1148              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
1149                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1150                          domain->profile = (u8) profile;                          if (!is_delete)
1151                  return 0;                                  domain->profile = (u8) profile;
         }  
         if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL)) {  
                 domain->ignore_global = !is_delete;  
1152                  return 0;                  return 0;
1153          }          }
1154          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, "use_group %u\n", &profile) == 1
1155                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
1156                    if (!is_delete)
1157                            domain->group = (u8) profile;
1158                  return 0;                  return 0;
1159          }          }
1160          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1161                  domain->ignore_global_allow_env = !is_delete;                  const char *cp = ccs_dif[profile];
1162                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
1163          }                          continue;
1164          if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {                  domain->flags[profile] = !is_delete;
                 domain->quota_warned = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {  
                 domain->domain_transition_failed = !is_delete;  
1165                  return 0;                  return 0;
1166          }          }
1167          cp = ccs_find_condition_part(data);          return ccs_write_domain2(data, domain, is_delete);
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         error = ccs_write_domain2(data, domain, cond, is_delete);  
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
1168  }  }
1169    
1170  /**  /**
# Line 975  static int ccs_write_domain(struct ccs_i Line 1173  static int ccs_write_domain(struct ccs_i
1173   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1174   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
1175   *   *
1176   * Returns true on success, false otherwise.   * Returns nothing.
1177   */   */
1178  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1179                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1180  {  {
1181          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1182          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1183                  head->read_avail--;                  ccs_set_space(head);
1184          if (ptr->is_group)          if (ptr->is_group) {
1185                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1186                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1187          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1188  }                  if (cond)
1189                            ccs_set_string(head, "\"");
1190  /**                  ccs_set_string(head, ptr->filename->name);
1191   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1192   *                          ccs_set_string(head, "\"");
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_name_union".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,  
                                         const struct ccs_name_union *ptr)  
 {  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         return ccs_io_printf(head, "\"%s\"", ptr->filename->name);  
 }  
   
 static void ccs_print_number(char *buffer, int buffer_len,  
                              const struct ccs_number_union *ptr)  
 {  
         int i;  
         unsigned long min = ptr->values[0];  
         const unsigned long max = ptr->values[1];  
         u8 min_type = ptr->value_type[0];  
         const u8 max_type = ptr->value_type[1];  
         memset(buffer, 0, buffer_len);  
         buffer_len -= 2;  
         for (i = 0; i < 2; i++) {  
                 int len;  
                 switch (min_type) {  
                 case CCS_VALUE_TYPE_HEXADECIMAL:  
                         snprintf(buffer, buffer_len, "0x%lX", min);  
                         break;  
                 case CCS_VALUE_TYPE_OCTAL:  
                         snprintf(buffer, buffer_len, "0%lo", min);  
                         break;  
                 default:  
                         snprintf(buffer, buffer_len, "%lu", min);  
                         break;  
                 }  
                 if (min == max && min_type == max_type)  
                         break;  
                 len = strlen(buffer);  
                 buffer[len++] = '-';  
                 buffer += len;  
                 buffer_len -= len;  
                 min_type = max_type;  
                 min = max;  
1193          }          }
1194  }  }
1195    
1196  /**  /**
  * ccs_print_number_union_common - Print a ccs_number_union.  
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
  * @need_space: True if a space character is needed.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,  
                                           const struct ccs_number_union *ptr,  
                                           const bool need_space)  
 {  
         char buffer[128];  
         if (need_space && !ccs_io_printf(head, " "))  
                 return false;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         ccs_print_number(buffer, sizeof(buffer), ptr);  
         return ccs_io_printf(head, "%s", buffer);  
 }  
   
 /**  
1197   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1198   *   *
1199   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1200   * @ptr:        Pointer to "struct ccs_number_union".   * @ptr:  Pointer to "struct ccs_number_union".
1201   *   *
1202   * Returns true on success, false otherwise.   * Returns nothing.
1203   */   */
1204  static bool ccs_print_number_union(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1205                                     const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1206  {  {
1207          return ccs_print_number_union_common(head, ptr, true);          if (!head->r.print_cond_part)
1208  }                  ccs_set_space(head);
1209            if (ptr->is_group) {
1210  /**                  ccs_set_string(head, "@");
1211   * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.                  ccs_set_string(head, ptr->group->group_name->name);
1212   *          } else {
1213   * @head:       Pointer to "struct ccs_io_buffer".                  int i;
1214   * @ptr:        Pointer to "struct ccs_number_union".                  unsigned long min = ptr->values[0];
1215   *                  const unsigned long max = ptr->values[1];
1216   * Returns true on success, false otherwise.                  u8 min_type = ptr->value_type[0];
1217   */                  const u8 max_type = ptr->value_type[1];
1218  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,                  char buffer[128];
1219                                             const struct ccs_number_union *ptr)                  buffer[0] = '\0';
1220  {                  for (i = 0; i < 2; i++) {
1221          return ccs_print_number_union_common(head, ptr, false);                          switch (min_type) {
1222                            case CCS_VALUE_TYPE_HEXADECIMAL:
1223                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1224                                                  min);
1225                                    break;
1226                            case CCS_VALUE_TYPE_OCTAL:
1227                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1228                                                  min);
1229                                    break;
1230                            default:
1231                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1232                                                  min);
1233                                    break;
1234                            }
1235                            if (min == max && min_type == max_type)
1236                                    break;
1237                            ccs_addprintf(buffer, sizeof(buffer), "-");
1238                            min_type = max_type;
1239                            min = max;
1240                    }
1241                    ccs_io_printf(head, "%s", buffer);
1242            }
1243  }  }
1244    
1245  /**  /**
1246   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1247   *   *
1248   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1249   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1250   *   *
1251   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1252   */   */
1253  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1254                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1255  {  {
1256          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1257          const struct ccs_number_union *numbers_p;          case 0:
1258          const struct ccs_name_union *names_p;                  head->r.cond_index = 0;
1259          const struct ccs_argv *argv;                  head->r.cond_step++;
1260          const struct ccs_envp *envp;                  /* fall through */
1261          u16 condc;          case 1:
1262          u16 i;                  {
1263          u16 j;                          const u16 condc = cond->condc;
1264          char buffer[32];                          const struct ccs_condition_element *condp =
1265          if (!cond)                                  (typeof(condp)) (cond + 1);
1266                  goto no_condition;                          const struct ccs_number_union *numbers_p =
1267          condc = cond->condc;                                  (typeof(numbers_p)) (condp + condc);
1268          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_name_union *names_p =
1269          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(names_p))
1270          names_p = (const struct ccs_name_union *)                                  (numbers_p + cond->numbers_count);
1271                  (numbers_p + cond->numbers_count);                          const struct ccs_argv *argv =
1272          argv = (const struct ccs_argv *) (names_p + cond->names_count);                                  (typeof(argv)) (names_p + cond->names_count);
1273          envp = (const struct ccs_envp *) (argv + cond->argc);                          const struct ccs_envp *envp =
1274          memset(buffer, 0, sizeof(buffer));                                  (typeof(envp)) (argv + cond->argc);
1275          if (condc && !ccs_io_printf(head, "%s", " if"))                          u16 skip;
1276                  goto out;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1277          for (i = 0; i < condc; i++) {                                  const u8 left = condp->left;
1278                  const u8 match = condp->equals;                                  const u8 right = condp->right;
1279                  const u8 left = condp->left;                                  condp++;
1280                  const u8 right = condp->right;                                  switch (left) {
1281                  condp++;                                  case CCS_ARGV_ENTRY:
1282                  switch (left) {                                          argv++;
1283                  case CCS_ARGV_ENTRY:                                          continue;
1284                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  case CCS_ENVP_ENTRY:
1285                                             argv->index, argv->is_not ?                                          envp++;
1286                                             "!=" : "=", argv->value->name))                                          continue;
1287                                  goto out;                                  case CCS_NUMBER_UNION:
1288                          argv++;                                          numbers_p++;
1289                          continue;                                          break;
1290                  case CCS_ENVP_ENTRY:                                  }
1291                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  switch (right) {
1292                                             envp->name->name, envp->is_not ?                                  case CCS_NAME_UNION:
1293                                             "!=" : "="))                                          names_p++;
1294                                  goto out;                                          break;
1295                          if (envp->value) {                                  case CCS_NUMBER_UNION:
1296                                  if (!ccs_io_printf(head, "\"%s\"",                                          numbers_p++;
1297                                                     envp->value->name))                                          break;
1298                                          goto out;                                  }
1299                          } else {                          }
1300                                  if (!ccs_io_printf(head, "NULL"))                          while (head->r.cond_index < condc) {
1301                                          goto out;                                  const u8 match = condp->equals;
1302                                    const u8 left = condp->left;
1303                                    const u8 right = condp->right;
1304                                    if (!ccs_flush(head))
1305                                            return false;
1306                                    condp++;
1307                                    head->r.cond_index++;
1308                                    ccs_set_space(head);
1309                                    switch (left) {
1310                                    case CCS_ARGV_ENTRY:
1311                                            ccs_io_printf(head,
1312                                                          "exec.argv[%u]%s\"%s\"",
1313                                                          argv->index,
1314                                                          argv->is_not ?
1315                                                          "!=" : "=",
1316                                                          argv->value->name);
1317                                            argv++;
1318                                            continue;
1319                                    case CCS_ENVP_ENTRY:
1320                                            ccs_io_printf(head,
1321                                                          "exec.envp[\"%s\"]%s",
1322                                                          envp->name->name,
1323                                                          envp->is_not ?
1324                                                          "!=" : "=");
1325                                            if (envp->value) {
1326                                                    ccs_set_string(head, "\"");
1327                                                    ccs_set_string(head, envp->
1328                                                                   value->name);
1329                                                    ccs_set_string(head, "\"");
1330                                            } else {
1331                                                    ccs_set_string(head, "NULL");
1332                                            }
1333                                            envp++;
1334                                            continue;
1335                                    case CCS_NUMBER_UNION:
1336                                            ccs_print_number_union(head,
1337                                                                   numbers_p++);
1338                                            break;
1339                                    default:
1340                                            ccs_set_string(head,
1341                                                   ccs_condition_keyword[left]);
1342                                            break;
1343                                    }
1344                                    ccs_set_string(head, match ? "=" : "!=");
1345                                    switch (right) {
1346                                    case CCS_NAME_UNION:
1347                                            ccs_print_name_union(head, names_p++);
1348                                            break;
1349                                    case CCS_NUMBER_UNION:
1350                                            ccs_print_number_union(head,
1351                                                                   numbers_p++);
1352                                            break;
1353                                    default:
1354                                            ccs_set_string(head,
1355                                                   ccs_condition_keyword[right]);
1356                                            break;
1357                                    }
1358                          }                          }
                         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;  
1359                  }                  }
1360                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1361                          goto out;                  /* fall through */
1362                  switch (right) {          case 2:
1363                  case CCS_NAME_UNION:                  if (!ccs_flush(head))
                         if (!ccs_print_name_union_quoted(head, names_p++))  
                                 goto out;  
                         break;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union_nospace(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (right >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, "%s",  
                                            ccs_condition_keyword[right]))  
                                 goto out;  
1364                          break;                          break;
1365                    head->r.cond_step++;
1366                    /* fall through */
1367            case 3:
1368                    if (cond->grant_log != CCS_GRANTLOG_AUTO)
1369                            ccs_io_printf(head, " grant_log=%s",
1370                                          ccs_yesno(cond->grant_log ==
1371                                                    CCS_GRANTLOG_YES));
1372                    if (cond->transit) {
1373                            ccs_set_string(head, " auto_domain_transition=\"");
1374                            ccs_set_string(head, cond->transit->name);
1375                            ccs_set_string(head, "\"");
1376                  }                  }
1377          }                  ccs_set_lf(head);
         i = cond->post_state[3];  
         if (!i)  
                 goto no_condition;  
         if (!ccs_io_printf(head, " ; set"))  
                 goto out;  
         for (j = 0; j < 3; j++) {  
                 if (!(i & (1 << j)))  
                         continue;  
                 if (!ccs_io_printf(head, " task.state[%u]=%u", j,  
                                    cond->post_state[j]))  
                         goto out;  
         }  
         if (i & (1 << 4)) {  
                 if (!ccs_io_printf(head, " audit=%s",  
                                    ccs_yesno(cond->post_state[4])))  
                         goto out;  
         }  
  no_condition:  
         if (ccs_io_printf(head, "\n"))  
1378                  return true;                  return true;
1379   out:          }
1380          return false;          return false;
1381  }  }
1382    
1383  /**  /**
1384   * ccs_print_path_acl - Print a path ACL entry.   * ccs_set_group - Print "acl_group " header keyword and category name.
1385   *   *
1386   * @head: Pointer to "struct ccs_io_buffer".   * @head:     Pointer to "struct ccs_io_buffer".
1387   * @ptr:  Pointer to "struct ccs_path_acl".   * @category: Category name.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1388   *   *
1389   * Returns true on success, false otherwise.   * Returns nothing.
1390   */   */
1391  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static void ccs_set_group(struct ccs_io_buffer *head, const char *category)
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
1392  {  {
1393          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1394          u8 bit;                  ccs_io_printf(head, "acl_group %u ", head->r.acl_group_index);
1395          const u16 perm = ptr->perm;          ccs_set_string(head, category);
         for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE  
                     && bit != CCS_TYPE_TRANSIT)  
                         continue;  
                 /* Print "read/write" instead of "read" and "write". */  
                 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)  
                     && (perm & (1 << CCS_TYPE_READ_WRITE)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1396  }  }
1397    
1398  /**  /**
1399   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_print_entry - Print an ACL entry.
1400   *   *
1401   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1402   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1403   *   *
1404   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1405   */   */
1406  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1407                                         struct ccs_path_number3_acl *ptr,                              const struct ccs_acl_info *acl)
                                        const struct ccs_condition *cond)  
1408  {  {
1409          int pos;          const u8 acl_type = acl->type;
1410            const bool may_trigger_transition = acl->cond && acl->cond->transit;
1411            bool first = true;
1412          u8 bit;          u8 bit;
1413          const u16 perm = ptr->perm;          if (head->r.print_cond_part)
1414          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;                  goto print_cond_part;
1415               bit++) {          if (acl->is_deleted)
1416                  if (!(perm & (1 << bit)))                  return true;
1417                          continue;          if (!ccs_flush(head))
1418                  pos = head->read_avail;                  return false;
1419                  if (!ccs_io_printf(head, "allow_%s",          else if (acl_type == CCS_TYPE_PATH_ACL) {
1420                                     ccs_path_number32keyword(bit)) ||                  struct ccs_path_acl *ptr
1421                      !ccs_print_name_union(head, &ptr->name) ||                          = container_of(acl, typeof(*ptr), head);
1422                      !ccs_print_number_union(head, &ptr->mode) ||                  const u16 perm = ptr->perm;
1423                      !ccs_print_number_union(head, &ptr->major) ||                  for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) {
1424                      !ccs_print_number_union(head, &ptr->minor) ||                          if (!(perm & (1 << bit)))
1425                      !ccs_print_condition(head, cond)) {                                  continue;
1426                          head->read_bit = bit;                          if (head->r.print_transition_related_only &&
1427                          head->read_avail = pos;                              bit != CCS_TYPE_EXECUTE && !may_trigger_transition)
1428                          return false;                                  continue;
1429                            if (first) {
1430                                    ccs_set_group(head, "file ");
1431                                    first = false;
1432                            } else {
1433                                    ccs_set_slash(head);
1434                            }
1435                            ccs_set_string(head, ccs_path_keyword[bit]);
1436                  }                  }
1437          }                  if (first)
1438          head->read_bit = 0;                          return true;
1439          return true;                  ccs_print_name_union(head, &ptr->name);
1440  }          } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1441                       acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1442  /**                  struct ccs_handler_acl *ptr
1443   * ccs_print_path2_acl - Print a path2 ACL entry.                          = container_of(acl, typeof(*ptr), head);
1444   *                  ccs_set_group(head, "task ");
1445   * @head: Pointer to "struct ccs_io_buffer".                  ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1446   * @ptr:  Pointer to "struct ccs_path2_acl".                                 ? "auto_execute_handler " :
1447   * @cond: Pointer to "struct ccs_condition". May be NULL.                                 "denied_execute_handler ");
1448   *                  ccs_set_string(head, ptr->handler->name);
1449   * Returns true on success, false otherwise.          } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1450   */                     acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1451  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,                  struct ccs_task_acl *ptr =
1452                                  struct ccs_path2_acl *ptr,                          container_of(acl, typeof(*ptr), head);
1453                                  const struct ccs_condition *cond)                  ccs_set_group(head, "task ");
1454  {                  ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1455          int pos;                                 "auto_domain_transition " :
1456          u8 bit;                                 "manual_domain_transition ");
1457          const u8 perm = ptr->perm;                  ccs_set_string(head, ptr->domainname->name);
1458          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {          } else if (head->r.print_transition_related_only &&
1459                  if (!(perm & (1 << bit)))                     !may_trigger_transition) {
1460                          continue;                  return true;
1461                  pos = head->read_avail;          } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1462                  if (!ccs_io_printf(head, "allow_%s",                  struct ccs_mkdev_acl *ptr =
1463                                     ccs_path22keyword(bit)) ||                          container_of(acl, typeof(*ptr), head);
1464                      !ccs_print_name_union(head, &ptr->name1) ||                  const u8 perm = ptr->perm;
1465                      !ccs_print_name_union(head, &ptr->name2) ||                  for (bit = 0; bit < CCS_MAX_MKDEV_OPERATION; bit++) {
1466                      !ccs_print_condition(head, cond)) {                          if (!(perm & (1 << bit)))
1467                          head->read_bit = bit;                                  continue;
1468                          head->read_avail = pos;                          if (first) {
1469                          return false;                                  ccs_set_group(head, "file ");
1470                                    first = false;
1471                            } else {
1472                                    ccs_set_slash(head);
1473                            }
1474                            ccs_set_string(head, ccs_mac_keywords
1475                                           [ccs_pnnn2mac[bit]]);
1476                  }                  }
1477          }                  if (first)
1478          head->read_bit = 0;                          return true;
1479          return true;                  ccs_print_name_union(head, &ptr->name);
1480  }                  ccs_print_number_union(head, &ptr->mode);
1481                    ccs_print_number_union(head, &ptr->major);
1482  /**                  ccs_print_number_union(head, &ptr->minor);
1483   * ccs_print_path_number_acl - Print a path_number ACL entry.          } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1484   *                  struct ccs_path2_acl *ptr =
1485   * @head: Pointer to "struct ccs_io_buffer".                          container_of(acl, typeof(*ptr), head);
1486   * @ptr:  Pointer to "struct ccs_path_number_acl".                  const u8 perm = ptr->perm;
1487   * @cond: Pointer to "struct ccs_condition". May be NULL.                  for (bit = 0; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1488   *                          if (!(perm & (1 << bit)))
1489   * Returns true on success, false otherwise.                                  continue;
1490   */                          if (first) {
1491  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,                                  ccs_set_group(head, "file ");
1492                                        struct ccs_path_number_acl *ptr,                                  first = false;
1493                                        const struct ccs_condition *cond)                          } else {
1494  {                                  ccs_set_slash(head);
1495          int pos;                          }
1496          u8 bit;                          ccs_set_string(head, ccs_mac_keywords
1497          const u8 perm = ptr->perm;                                         [ccs_pp2mac[bit]]);
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    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;  
1498                  }                  }
1499          }                  if (first)
1500          head->read_bit = 0;                          return true;
1501          return true;                  ccs_print_name_union(head, &ptr->name1);
1502  }                  ccs_print_name_union(head, &ptr->name2);
1503            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1504  /**                  struct ccs_path_number_acl *ptr =
1505   * ccs_print_env_acl - Print an evironment variable name's ACL entry.                          container_of(acl, typeof(*ptr), head);
1506   *                  const u8 perm = ptr->perm;
1507   * @head: Pointer to "struct ccs_io_buffer".                  for (bit = 0; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {
1508   * @ptr:  Pointer to "struct ccs_env_acl".                          if (!(perm & (1 << bit)))
1509   * @cond: Pointer to "struct ccs_condition". May be NULL.                                  continue;
1510   *                          if (first) {
1511   * Returns true on success, false otherwise.                                  ccs_set_group(head, "file ");
1512   */                                  first = false;
1513  static bool ccs_print_env_acl(struct ccs_io_buffer *head,                          } else {
1514                                struct ccs_env_acl *ptr,                                  ccs_set_slash(head);
1515                                const struct ccs_condition *cond)                          }
1516  {                          ccs_set_string(head, ccs_mac_keywords
1517          const int pos = head->read_avail;                                         [ccs_pn2mac[bit]]);
1518          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||                  }
1519              !ccs_print_condition(head, cond)) {                  if (first)
1520                  head->read_avail = pos;                          return true;
1521                  return false;                  ccs_print_name_union(head, &ptr->name);
1522          }                  ccs_print_number_union(head, &ptr->number);
1523          return true;          } else if (acl_type == CCS_TYPE_ENV_ACL) {
1524  }                  struct ccs_env_acl *ptr =
1525                            container_of(acl, typeof(*ptr), head);
1526  /**                  ccs_set_group(head, "misc env ");
1527   * ccs_print_capability_acl - Print a capability ACL entry.                  ccs_set_string(head, ptr->env->name);
1528   *          } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1529   * @head: Pointer to "struct ccs_io_buffer".                  struct ccs_capability_acl *ptr =
1530   * @ptr:  Pointer to "struct ccs_capability_acl".                          container_of(acl, typeof(*ptr), head);
1531   * @cond: Pointer to "struct ccs_condition". May be NULL.                  ccs_set_group(head, "capability ");
1532   *                  ccs_set_string(head, ccs_mac_keywords
1533   * Returns true on success, false otherwise.                                 [ccs_c2mac[ptr->operation]]);
1534   */          } else if (acl_type == CCS_TYPE_INET_ACL) {
1535  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,                  struct ccs_inet_acl *ptr =
1536                                       struct ccs_capability_acl *ptr,                          container_of(acl, typeof(*ptr), head);
1537                                       const struct ccs_condition *cond)                  const u8 perm = ptr->perm;
1538  {                  for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1539          const int pos = head->read_avail;                          if (!(perm & (1 << bit)))
1540          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",                                  continue;
1541                             ccs_cap2keyword(ptr->operation)) ||                          if (first) {
1542              !ccs_print_condition(head, cond)) {                                  ccs_set_group(head, "network inet ");
1543                  head->read_avail = pos;                                  ccs_set_string(head, ccs_proto_keyword
1544                  return false;                                                 [ptr->protocol]);
1545          }                                  ccs_set_space(head);
1546          return true;                                  first = false;
1547  }                          } else {
1548                                    ccs_set_slash(head);
1549  /**                          }
1550   * ccs_print_network_acl - Print a network ACL entry.                          ccs_set_string(head, ccs_socket_keyword[bit]);
1551   *                  }
1552   * @head: Pointer to "struct ccs_io_buffer".                  if (first)
1553   * @ptr:  Pointer to "struct ccs_ip_network_acl".                          return true;
1554   * @cond: Pointer to "struct ccs_condition". May be NULL.                  ccs_set_space(head);
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_network_acl(struct ccs_io_buffer *head,  
                                   struct ccs_ip_network_acl *ptr,  
                                   const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u8 perm = ptr->perm;  
         char buf[128];  
         for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {  
                 const char *w[2] = { "", "" };  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
1555                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1556                            char buf[128];
1557                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1558                          w[0] = "@";                          ccs_set_string(head, "@");
1559                          w[1] = ptr->address.group->group_name->name;                          ccs_set_string(head,
1560                                           ptr->address.group->group_name->name);
1561                          break;                          break;
1562                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1563                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1564                                         ptr->address.ipv4.max);                                         ptr->address.ipv4.max);
1565                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1566                          break;                          break;
1567                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1568                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1569                                         ptr->address.ipv6.max);                                         ptr->address.ipv6.max);
1570                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1571                          break;                          break;
1572                  }                  }
1573                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",                  ccs_print_number_union(head, &ptr->port);
1574                                     ccs_net2keyword(bit), w[0], w[1]) ||          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1575                      !ccs_print_number_union(head, &ptr->port) ||                  struct ccs_unix_acl *ptr =
1576                      !ccs_print_condition(head, cond))                          container_of(acl, typeof(*ptr), head);
1577                          goto out;                  const u8 perm = ptr->perm;
1578          }                  for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1579          head->read_bit = 0;                          if (!(perm & (1 << bit)))
1580          return true;                                  continue;
1581   out:                          if (first) {
1582          head->read_bit = bit;                                  ccs_set_group(head, "network unix ");
1583          head->read_avail = pos;                                  ccs_set_string(head, ccs_proto_keyword
1584          return false;                                                 [ptr->protocol]);
1585  }                                  ccs_set_space(head);
1586                                    first = false;
1587  /**                          } else {
1588   * ccs_print_signal_acl - Print a signal ACL entry.                                  ccs_set_slash(head);
1589   *                          }
1590   * @head: Pointer to "struct ccs_io_buffer".                          ccs_set_string(head, ccs_socket_keyword[bit]);
1591   * @ptr:  Pointer to "struct signale_acl".                  }
1592   * @cond: Pointer to "struct ccs_condition". May be NULL.                  if (first)
1593   *                          return true;
1594   * Returns true on success, false otherwise.                  ccs_print_name_union(head, &ptr->name);
1595   */          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1596  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,                  struct ccs_signal_acl *ptr =
1597                                   struct ccs_signal_acl *ptr,                          container_of(acl, typeof(*ptr), head);
1598                                   const struct ccs_condition *cond)                  ccs_set_group(head, "ipc signal ");
1599  {                  ccs_io_printf(head, "%u ", ptr->sig);
1600          const int pos = head->read_avail;                  ccs_set_string(head, ptr->domainname->name);
1601          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1602                             ptr->sig, ptr->domainname->name) ||                  struct ccs_mount_acl *ptr =
1603              !ccs_print_condition(head, cond)) {                          container_of(acl, typeof(*ptr), head);
1604                  head->read_avail = pos;                  ccs_set_group(head, "file mount");
1605                  return false;                  ccs_print_name_union(head, &ptr->dev_name);
1606          }                  ccs_print_name_union(head, &ptr->dir_name);
1607          return true;                  ccs_print_name_union(head, &ptr->fs_type);
1608  }                  ccs_print_number_union(head, &ptr->flags);
1609            }
1610  /**          if (acl->cond) {
1611   * ccs_print_execute_handler - Print an execute handler ACL entry.                  head->r.print_cond_part = true;
1612   *                  head->r.cond_step = 0;
1613   * @head:    Pointer to "struct ccs_io_buffer".                  if (!ccs_flush(head))
1614   * @keyword: Name of the keyword.                          return false;
1615   * @ptr:     Pointer to "struct ccs_execute_handler".  print_cond_part:
1616   *                  if (!ccs_print_condition(head, acl->cond))
1617   * Returns true on success, false otherwise.                          return false;
1618   */                  head->r.print_cond_part = false;
1619  static bool ccs_print_execute_handler(struct ccs_io_buffer *head,          } else {
1620                                        const char *keyword,                  ccs_set_lf(head);
                                       struct ccs_execute_handler *ptr)  
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mount_acl *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||  
             !ccs_print_name_union(head, &ptr->dev_name) ||  
             !ccs_print_name_union(head, &ptr->dir_name) ||  
             !ccs_print_name_union(head, &ptr->fs_type) ||  
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1621          }          }
1622          return true;          return true;
1623  }  }
1624    
1625  /**  /**
  * ccs_print_entry - Print an ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to an ACL entry.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_entry(struct ccs_io_buffer *head,  
                             struct ccs_acl_info *ptr)  
 {  
         const struct ccs_condition *cond = ptr->cond;  
         const u8 acl_type = ptr->type;  
         if (ptr->is_deleted)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_ACL) {  
                 struct ccs_path_acl *acl  
                         = container_of(ptr, struct ccs_path_acl, head);  
                 return ccs_print_path_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||  
             acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler *acl  
                         = container_of(ptr, struct ccs_execute_handler, head);  
                 const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?  
                         CCS_KEYWORD_EXECUTE_HANDLER :  
                         CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, 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);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
1626   * ccs_read_domain2 - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1627   *   *
1628   * @head:   Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1629   * @domain: Pointer to "struct ccs_domain_info".   * @domain: Pointer to "struct ccs_domain_info".
1630   *   * @index:  Index number.
  * Caller holds ccs_read_lock().  
1631   *   *
1632   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1633     *
1634     * Caller holds ccs_read_lock().
1635   */   */
1636  static bool ccs_read_domain2(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1637                               struct ccs_domain_info *domain)                               struct ccs_domain_info *domain,
1638                                 const u8 index)
1639  {  {
1640          struct list_head *pos;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1641          /* Print ACL entries in the domain. */                  struct ccs_acl_info *ptr =
1642          list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {                          list_entry(head->r.acl, typeof(*ptr), list);
                 struct ccs_acl_info *ptr  
                         = list_entry(pos, struct ccs_acl_info, list);  
1643                  if (!ccs_print_entry(head, ptr))                  if (!ccs_print_entry(head, ptr))
1644                          return false;                          return false;
1645          }          }
1646            head->r.acl = NULL;
1647          return true;          return true;
1648  }  }
1649    
# Line 1618  static bool ccs_read_domain2(struct ccs_ Line 1652  static bool ccs_read_domain2(struct ccs_
1652   *   *
1653   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1654   *   *
1655     * Returns nothing.
1656     *
1657   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1658   */   */
1659  static void ccs_read_domain(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1660  {  {
1661          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1662                  return;                  return;
1663          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1664                  struct ccs_domain_info *domain =                  struct ccs_domain_info *domain =
1665                          list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1666                  const char *w[5] = { "", "", "", "", "" };                  switch (head->r.step) {
1667                  switch (head->read_step) {                          u8 i;
1668                  case 0:                  case 0:
1669                          if (domain->is_deleted && !head->read_single_domain)                          if (domain->is_deleted &&
1670                                !head->r.print_this_domain_only)
1671                                  continue;                                  continue;
1672                          /* Print domainname and flags. */                          /* Print domainname and flags. */
1673                          if (domain->quota_warned)                          ccs_set_string(head, domain->domainname->name);
1674                                  w[0] = CCS_KEYWORD_QUOTA_EXCEEDED "\n";                          ccs_set_lf(head);
1675                          if (domain->domain_transition_failed)                          ccs_io_printf(head, "use_profile %u\n",
1676                                  w[1] = CCS_KEYWORD_TRANSITION_FAILED "\n";                                        domain->profile);
1677                          if (domain->ignore_global)                          ccs_io_printf(head, "use_group %u\n", domain->group);
1678                                  w[2] = CCS_KEYWORD_IGNORE_GLOBAL "\n";                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1679                          if (domain->ignore_global_allow_read)                                  if (domain->flags[i])
1680                                  w[3] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ                                          ccs_set_string(head, ccs_dif[i]);
1681                                          "\n";                          head->r.step++;
1682                          if (domain->ignore_global_allow_env)                          ccs_set_lf(head);
                                 w[4] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV  
                                         "\n";  
                         if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE  
                                            "%u\n%s%s%s%s%s\n",  
                                            domain->domainname->name,  
                                            domain->profile, w[0], w[1], w[2],  
                                            w[3], w[4]))  
                                 return;  
                         head->read_step++;  
1683                          /* fall through */                          /* fall through */
1684                  case 1:                  case 1:
1685                          if (!ccs_read_domain2(head, domain))                          if (!ccs_read_domain2(head, domain, 0))
1686                                  return;                                  return;
1687                          head->read_step++;                          head->r.step++;
1688                          /* fall through */                          /* fall through */
1689                  case 2:                  case 2:
1690                          if (!ccs_io_printf(head, "\n"))                          if (!ccs_read_domain2(head, domain, 1))
1691                                  return;                                  return;
1692                          head->read_step = 0;                          head->r.step++;
1693                          if (head->read_single_domain)                          if (!ccs_set_lf(head))
1694                                  goto out;                                  return;
1695                            /* fall through */
1696                    case 3:
1697                            head->r.step = 0;
1698                            if (head->r.print_this_domain_only)
1699                                    goto done;
1700                  }                  }
1701          }          }
1702   out:  done:
1703          head->read_eof = true;          head->r.eof = true;
1704  }  }
1705    
1706  /**  /**
# Line 1708  static int ccs_write_domain_profile(stru Line 1740  static int ccs_write_domain_profile(stru
1740   *   *
1741   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1742   *   *
1743     * Returns nothing.
1744     *
1745   * This is equivalent to doing   * This is equivalent to doing
1746   *   *
1747   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1719  static int ccs_write_domain_profile(stru Line 1753  static int ccs_write_domain_profile(stru
1753   */   */
1754  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1755  {  {
1756          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1757                  return;                  return;
1758          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1759                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1760                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1761                  if (domain->is_deleted)                  if (domain->is_deleted)
1762                          continue;                          continue;
1763                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1764                          return;                          return;
1765                    ccs_io_printf(head, "%u ", domain->profile);
1766                    ccs_set_string(head, domain->domainname->name);
1767                    ccs_set_lf(head);
1768          }          }
1769          head->read_eof = true;          head->r.eof = true;
1770  }  }
1771    
1772  /**  /**
1773   * ccs_write_pid: Specify PID to obtain domainname.   * ccs_write_pid - Specify PID to obtain domainname.
1774   *   *
1775   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1776   *   *
# Line 1743  static void ccs_read_domain_profile(stru Line 1778  static void ccs_read_domain_profile(stru
1778   */   */
1779  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1780  {  {
1781          head->read_eof = false;          head->r.eof = false;
1782          return 0;          return 0;
1783  }  }
1784    
# Line 1769  static void ccs_read_pid(struct ccs_io_b Line 1804  static void ccs_read_pid(struct ccs_io_b
1804          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1805          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1806          if (!buf) {          if (!buf) {
1807                  head->read_eof = true;                  head->r.eof = true;
1808                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1809          }          }
1810          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1811                  return;                  return;
1812          head->read_eof = true;          head->r.eof = true;
1813          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1814                  task_info = true;                  task_info = true;
1815          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1791  static void ccs_read_pid(struct ccs_io_b Line 1826  static void ccs_read_pid(struct ccs_io_b
1826  #endif  #endif
1827          if (p) {          if (p) {
1828                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1829                  ccs_flags = p->ccs_flags;                  ccs_flags = ccs_task_flags(p);
1830          }          }
1831          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1832          if (!domain)          if (!domain)
1833                  return;                  return;
1834          if (!task_info)          if (!task_info) {
1835                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1836                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1837          else          } else {
1838                  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,  
1839                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1840                                          CCS_TASK_IS_MANAGER),                                          CCS_TASK_IS_MANAGER),
1841                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1842                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER));
1843                                (u8) (ccs_flags >> 24),          }
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
1844  }  }
1845    
1846    /* String table for domain transition control keywords. */
1847    static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1848            [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1849            [CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1850            [CCS_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1851            [CCS_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
1852    };
1853    
1854    /* String table for grouping keywords. */
1855    static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1856            [CCS_PATH_GROUP]    = "path_group ",
1857            [CCS_NUMBER_GROUP]  = "number_group ",
1858            [CCS_ADDRESS_GROUP] = "address_group ",
1859    };
1860    
1861  /**  /**
1862   * ccs_write_exception - Write exception policy.   * ccs_write_exception - Write exception policy.
1863   *   *
# Line 1821  static void ccs_read_pid(struct ccs_io_b Line 1868  static void ccs_read_pid(struct ccs_io_b
1868  static int ccs_write_exception(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1869  {  {
1870          char *data = head->write_buf;          char *data = head->write_buf;
1871          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, "delete ");
1872          u8 i;          u8 i;
1873          static const struct {          static const struct {
1874                  const char *keyword;                  const char *keyword;
1875                  int (*write) (char *, const bool, const u8);                  int (*write) (char *, const bool);
1876          } ccs_callback[8] = {          } ccs_callback[2] = {
1877                  { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper },                  { "aggregator ",    ccs_write_aggregator },
1878                  { CCS_KEYWORD_NO_INITIALIZE_DOMAIN,                  { "deny_autobind ", ccs_write_reserved_port },
                   ccs_write_domain_initializer },  
                 { CCS_KEYWORD_KEEP_DOMAIN, ccs_write_domain_keeper },  
                 { CCS_KEYWORD_INITIALIZE_DOMAIN,  
                   ccs_write_domain_initializer },  
                 { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },  
                 { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },  
                 { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },  
                 { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }  
1879          };          };
1880          static const char *ccs_name[CCS_MAX_GROUP] = {          if (!is_delete && ccs_str_starts(&data, "select ") &&
1881                  [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,              !strcmp(data, "transition_only")) {
1882                  [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,                  head->r.print_transition_related_only = true;
1883                  [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP                  return -EAGAIN;
         };  
         for (i = 0; i < 8; i++) {  
                 if (ccs_str_starts(&data, ccs_callback[i].keyword))  
                         return ccs_callback[i].write(data, is_delete, i < 2);  
1884          }          }
1885          for (i = 0; i < CCS_MAX_GROUP; i++) {          /* Don't allow updating policies by non manager programs. */
1886                  if (ccs_str_starts(&data, ccs_name[i]))          if (!ccs_manager())
1887                    return -EPERM;
1888            for (i = 0; i < 2; i++)
1889                    if (ccs_str_starts(&data, ccs_callback[i].keyword))
1890                            return ccs_callback[i].write(data, is_delete);
1891            for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1892                    if (ccs_str_starts(&data, ccs_transition_type[i]))
1893                            return ccs_write_transition_control(data, is_delete,
1894                                                                i);
1895            for (i = 0; i < CCS_MAX_GROUP; i++)
1896                    if (ccs_str_starts(&data, ccs_group_name[i]))
1897                          return ccs_write_group(data, is_delete, i);                          return ccs_write_group(data, is_delete, i);
1898          }          if (ccs_str_starts(&data, "acl_group ")) {
1899          {                  unsigned int group;
1900                  int error;                  if (sscanf(data, "%u", &group) == 1 &&
1901                  struct ccs_condition *cond = NULL;                      group < CCS_MAX_ACL_GROUPS) {
1902                  char *cp = ccs_find_condition_part(data);                          data = strchr(data, ' ');
1903                  if (cp) {                          if (data)
1904                          cond = ccs_get_condition(cp);                                  return ccs_write_domain2(data + 1,
1905                          if (!cond)                                                           &ccs_acl_group[group],
1906                                  return -EINVAL;                                                           is_delete);
1907                  }                  }
                 error = ccs_write_domain2(data, &ccs_global_domain, cond,  
                                           is_delete);  
                 if (cond)  
                         ccs_put_condition(cond);  
                 return error;  
1908          }          }
1909            return -EINVAL;
1910  }  }
1911    
1912  /**  /**
# Line 1880  static int ccs_write_exception(struct cc Line 1921  static int ccs_write_exception(struct cc
1921   */   */
1922  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1923  {  {
1924          struct list_head *gpos;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
         struct list_head *mpos;  
         const char *w[3] = { "", "", "" };  
         if (idx == CCS_PATH_GROUP)  
                 w[0] = CCS_KEYWORD_PATH_GROUP;  
         else if (idx == CCS_NUMBER_GROUP)  
                 w[0] = CCS_KEYWORD_NUMBER_GROUP;  
         else if (idx == CCS_ADDRESS_GROUP)  
                 w[0] = CCS_KEYWORD_ADDRESS_GROUP;  
         list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {  
1925                  struct ccs_group *group =                  struct ccs_group *group =
1926                          list_entry(gpos, struct ccs_group, head.list);                          list_entry(head->r.group, typeof(*group), head.list);
1927                  w[1] = group->group_name->name;                  list_for_each_cookie(head->r.acl, &group->member_list) {
                 list_for_each_cookie(mpos, head->read_var2,  
                                      &group->member_list) {  
                         char buffer[128];  
1928                          struct ccs_acl_head *ptr =                          struct ccs_acl_head *ptr =
1929                                  list_entry(mpos, struct ccs_acl_head, list);                                  list_entry(head->r.acl, typeof(*ptr), list);
1930                          if (ptr->is_deleted)                          if (ptr->is_deleted)
1931                                  continue;                                  continue;
1932                            if (!ccs_flush(head))
1933                                    return false;
1934                            ccs_set_string(head, ccs_group_name[idx]);
1935                            ccs_set_string(head, group->group_name->name);
1936                          if (idx == CCS_PATH_GROUP) {                          if (idx == CCS_PATH_GROUP) {
1937                                  w[2] = container_of(ptr, struct ccs_path_group,                                  ccs_set_space(head);
1938                                                      head)->member_name->name;                                  ccs_set_string(head, container_of
1939                                                   (ptr, struct ccs_path_group,
1940                                                    head)->member_name->name);
1941                          } else if (idx == CCS_NUMBER_GROUP) {                          } else if (idx == CCS_NUMBER_GROUP) {
1942                                  w[2] = buffer;                                  ccs_print_number_union(head, &container_of
1943                                  ccs_print_number(buffer, sizeof(buffer),                                                 (ptr, struct ccs_number_group,
1944                                                   &container_of                                                  head)->number);
                                                  (ptr, struct ccs_number_group,  
                                                   head)->number);  
1945                          } else if (idx == CCS_ADDRESS_GROUP) {                          } else if (idx == CCS_ADDRESS_GROUP) {
1946                                    char buffer[128];
1947                                  struct ccs_address_group *member =                                  struct ccs_address_group *member =
1948                                          container_of(ptr, typeof(*member),                                          container_of(ptr, typeof(*member),
1949                                                       head);                                                       head);
                                 w[2] = buffer;  
1950                                  if (member->is_ipv6)                                  if (member->is_ipv6)
1951                                          ccs_print_ipv6(buffer, sizeof(buffer),                                          ccs_print_ipv6(buffer, sizeof(buffer),
1952                                                         member->min.ipv6,                                                         member->min.ipv6,
# Line 1922  static bool ccs_read_group(struct ccs_io Line 1955  static bool ccs_read_group(struct ccs_io
1955                                          ccs_print_ipv4(buffer, sizeof(buffer),                                          ccs_print_ipv4(buffer, sizeof(buffer),
1956                                                         member->min.ipv4,                                                         member->min.ipv4,
1957                                                         member->max.ipv4);                                                         member->max.ipv4);
1958                                    ccs_io_printf(head, " %s", buffer);
1959                          }                          }
1960                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],                          ccs_set_lf(head);
                                            w[2]))  
                                 return false;  
1961                  }                  }
1962                    head->r.acl = NULL;
1963          }          }
1964            head->r.group = NULL;
1965          return true;          return true;
1966  }  }
1967    
# Line 1943  static bool ccs_read_group(struct ccs_io Line 1977  static bool ccs_read_group(struct ccs_io
1977   */   */
1978  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1979  {  {
1980          struct list_head *pos;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1981          list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {                  struct ccs_acl_head *acl =
1982                  const char *w[4] = { "", "", "", "" };                          container_of(head->r.acl, typeof(*acl), list);
                 char buffer[16];  
                 struct ccs_acl_head *acl = container_of(pos, typeof(*acl),  
                                                         list);  
1983                  if (acl->is_deleted)                  if (acl->is_deleted)
1984                          continue;                          continue;
1985                    if (head->r.print_transition_related_only &&
1986                        idx != CCS_ID_TRANSITION_CONTROL)
1987                            continue;
1988                    if (!ccs_flush(head))
1989                            return false;
1990                  switch (idx) {                  switch (idx) {
1991                  case CCS_ID_DOMAIN_KEEPER:                  case CCS_ID_TRANSITION_CONTROL:
                         {  
                                 struct ccs_domain_keeper *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 w[0] = ptr->is_not ?  
                                         CCS_KEYWORD_NO_KEEP_DOMAIN :  
                                         CCS_KEYWORD_KEEP_DOMAIN;  
                                 if (ptr->program) {  
                                         w[1] = ptr->program->name;  
                                         w[2] = " from ";  
                                 }  
                                 w[3] = ptr->domainname->name;  
                         }  
                         break;  
                 case CCS_ID_DOMAIN_INITIALIZER:  
1992                          {                          {
1993                                  struct ccs_domain_initializer *ptr =                                  struct ccs_transition_control *ptr =
1994                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1995                                  w[0] = ptr->is_not ?                                  ccs_set_string(head,
1996                                          CCS_KEYWORD_NO_INITIALIZE_DOMAIN :                                                 ccs_transition_type[ptr->type]);
1997                                          CCS_KEYWORD_INITIALIZE_DOMAIN;                                  ccs_set_string(head, ptr->program ?
1998                                  w[1] = ptr->program->name;                                                 ptr->program->name : "any");
1999                                  if (ptr->domainname) {                                  ccs_set_string(head, " from ");
2000                                          w[2] = " from ";                                  ccs_set_string(head, ptr->domainname ?
2001                                          w[3] = ptr->domainname->name;                                                 ptr->domainname->name : "any");
                                 }  
2002                          }                          }
2003                          break;                          break;
2004                  case CCS_ID_AGGREGATOR:                  case CCS_ID_AGGREGATOR:
2005                          {                          {
2006                                  struct ccs_aggregator *ptr =                                  struct ccs_aggregator *ptr =
2007                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
2008                                  w[0] = CCS_KEYWORD_AGGREGATOR;                                  ccs_set_string(head, "aggregator ");
2009                                  w[1] = ptr->original_name->name;                                  ccs_set_string(head, ptr->original_name->name);
2010                                  w[2] = " ";                                  ccs_set_space(head);
2011                                  w[3] = ptr->aggregated_name->name;                                  ccs_set_string(head,
2012                          }                                                 ptr->aggregated_name->name);
                         break;  
                 case CCS_ID_PATTERN:  
                         {  
                                 struct ccs_pattern *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 w[0] = CCS_KEYWORD_FILE_PATTERN;  
                                 w[1] = ptr->pattern->name;  
                         }  
                         break;  
                 case CCS_ID_NO_REWRITE:  
                         {  
                                 struct ccs_no_rewrite *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 w[0] = CCS_KEYWORD_DENY_REWRITE;  
                                 w[1] = ptr->pattern->name;  
2013                          }                          }
2014                          break;                          break;
2015                  case CCS_ID_RESERVEDPORT:                  case CCS_ID_RESERVEDPORT:
# Line 2012  static bool ccs_read_policy(struct ccs_i Line 2018  static bool ccs_read_policy(struct ccs_i
2018                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
2019                                  const u16 min_port = ptr->min_port;                                  const u16 min_port = ptr->min_port;
2020                                  const u16 max_port = ptr->max_port;                                  const u16 max_port = ptr->max_port;
2021                                  w[0] = CCS_KEYWORD_DENY_AUTOBIND;                                  ccs_set_string(head, "deny_autobind ");
2022                                  snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",                                  ccs_io_printf(head, "%u", min_port);
2023                                           min_port, min_port != max_port ?                                  if (min_port != max_port)
2024                                           '-' : '\0', max_port);                                          ccs_io_printf(head, "-%u", max_port);
                                 buffer[sizeof(buffer) - 1] = '\0';  
                                 w[1] = buffer;  
2025                          }                          }
2026                          break;                          break;
2027                  default:                  default:
2028                          continue;                          continue;
2029                  }                  }
2030                  if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))                  ccs_set_lf(head);
                         return false;  
2031          }          }
2032            head->r.acl = NULL;
2033          return true;          return true;
2034  }  }
2035    
 static void ccs_read_global_domain(struct ccs_io_buffer *head)  
 {  
         if (!head->read_eof)  
                 head->read_eof = ccs_read_domain2(head, &ccs_global_domain);  
 }  
   
2036  /**  /**
2037   * ccs_read_exception - Read exception policy.   * ccs_read_exception - Read exception policy.
2038   *   *
2039   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
2040   *   *
2041     * Returns nothing.
2042     *
2043   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
2044   */   */
2045  static void ccs_read_exception(struct ccs_io_buffer *head)  static void ccs_read_exception(struct ccs_io_buffer *head)
2046  {  {
2047          if (head->read_eof)          if (head->r.eof)
2048                  return;                  return;
2049          while (head->read_step < CCS_MAX_POLICY &&          while (head->r.step < CCS_MAX_POLICY &&
2050                 ccs_read_policy(head, head->read_step))                 ccs_read_policy(head, head->r.step))
2051                  head->read_step++;                  head->r.step++;
2052          if (head->read_step < CCS_MAX_POLICY)          if (head->r.step < CCS_MAX_POLICY)
2053                  return;                  return;
2054          while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2055                 ccs_read_group(head, head->read_step - CCS_MAX_POLICY))                 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
2056                  head->read_step++;                  head->r.step++;
2057          if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)          if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2058                  return;                  return;
2059          head->read = ccs_read_global_domain;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
2060                   + CCS_MAX_ACL_GROUPS * 2) {
2061                    head->r.acl_group_index = (head->r.step - CCS_MAX_POLICY
2062                                               - CCS_MAX_GROUP) / 2;
2063                    if (!ccs_read_domain2(head,
2064                                          &ccs_acl_group[head->r.acl_group_index],
2065                                          head->r.step & 1))
2066                            return;
2067                    head->r.step++;
2068            }
2069            head->r.eof = true;
2070  }  }
2071    
2072    /* Wait queue for kernel -> userspace notification. */
2073    static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2074    /* Wait queue for userspace -> kernel notification. */
2075    static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
2076    
2077    /* Structure for query. */
2078    struct ccs_query {
2079            struct list_head list;
2080            char *query;
2081            size_t query_len;
2082            unsigned int serial;
2083            u8 timer;
2084            u8 answer;
2085            u8 retry;
2086    };
2087    
2088    /* The list for "struct ccs_query". */
2089    static LIST_HEAD(ccs_query_list);
2090    
2091    /* Lock for manipulating ccs_query_list. */
2092    static DEFINE_SPINLOCK(ccs_query_list_lock);
2093    
2094    /* Number of "struct file" referring /proc/ccs/query interface. */
2095    static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2096    
2097  /**  /**
2098   * ccs_get_argv0 - Get argv[0].   * ccs_truncate - Truncate a line.
2099   *   *
2100   * @ee: Pointer to "struct ccs_execve".   * @str: String to truncate.
2101   *   *
2102   * Returns true on success, false otherwise.   * Returns length of truncated @str.
2103   */   */
2104  static bool ccs_get_argv0(struct ccs_execve *ee)  static int ccs_truncate(char *str)
2105  {  {
2106          struct linux_binprm *bprm = ee->bprm;          char *start = str;
2107          char *arg_ptr = ee->tmp;          while (*(unsigned char *) str > (unsigned char) ' ')
2108          int arg_len = 0;                  str++;
2109          unsigned long pos = bprm->p;          *str = '\0';
2110          int offset = pos % PAGE_SIZE;          return strlen(start) + 1;
         bool done = false;  
         if (!bprm->argc)  
                 goto out;  
         while (1) {  
                 if (!ccs_dump_page(bprm, pos, &ee->dump))  
                         goto out;  
                 pos += PAGE_SIZE - offset;  
                 /* Read. */  
                 while (offset < PAGE_SIZE) {  
                         const char *kaddr = ee->dump.data;  
                         const unsigned char c = kaddr[offset++];  
                         if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {  
                                 if (c == '\\') {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = '\\';  
                                 } else if (c > ' ' && c < 127) {  
                                         arg_ptr[arg_len++] = c;  
                                 } else {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = (c >> 6) + '0';  
                                         arg_ptr[arg_len++]  
                                                 = ((c >> 3) & 7) + '0';  
                                         arg_ptr[arg_len++] = (c & 7) + '0';  
                                 }  
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
                         }  
                 }  
                 offset = 0;  
                 if (done)  
                         break;  
         }  
         return true;  
  out:  
         return false;  
2111  }  }
2112    
2113  /**  /**
2114   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_add_entry - Add an ACL to current thread's domain. Used by learning mode.
2115   *   *
2116   * @ee: Pointer to "struct ccs_execve".   * @header: Lines containing ACL.
2117   *   *
2118   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Returns nothing.
2119   */   */
2120  static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve *ee)  static void ccs_add_entry(char *header)
2121  {  {
2122          struct ccs_condition *cond;          char *buffer;
         char *buf;  
         int len = 256;  
2123          char *realpath = NULL;          char *realpath = NULL;
2124          char *argv0 = NULL;          char *argv0 = NULL;
2125          const struct ccs_profile *profile = ccs_profile(ccs_current_domain()->          char *symlink = NULL;
2126                                                          profile);          char *handler;
2127          if (profile->learning->learning_exec_realpath) {          char *cp = strchr(header, '\n');
2128                  struct file *file = ee->bprm->file;          int len;
2129  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)          if (!cp)
2130                  struct path path = { file->f_vfsmnt, file->f_dentry };                  return;
2131                  realpath = ccs_realpath_from_path(&path);          cp = strchr(cp + 1, '\n');
2132  #else          if (!cp)
2133                  realpath = ccs_realpath_from_path(&file->f_path);                  return;
2134  #endif          *cp++ = '\0';
2135                  if (realpath)          len = strlen(cp) + 1;
2136                          len += strlen(realpath) + 17;          /* strstr() will return NULL if ordering is wrong. */
2137          }          if (*cp == 'f') {
2138          if (profile->learning->learning_exec_argv0) {                  argv0 = strstr(header, " argv[]={ \"");
2139                  if (ccs_get_argv0(ee)) {                  if (argv0) {
2140                          argv0 = ee->tmp;                          argv0 += 10;
2141                          len += strlen(argv0) + 16;                          len += ccs_truncate(argv0) + 14;
2142                  }                  }
2143          }                  realpath = strstr(header, " exec={ realpath=\"");
2144          buf = kmalloc(len, CCS_GFP_FLAGS);                  if (realpath) {
2145          if (!buf) {                          realpath += 8;
2146                  kfree(realpath);                          len += ccs_truncate(realpath) + 6;
2147                  return NULL;                  }
2148          }                  symlink = strstr(header, " symlink.target=\"");
2149          snprintf(buf, len - 1, "if");                  if (symlink)
2150          if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {                          len += ccs_truncate(symlink + 1) + 1;
2151                  const int pos = strlen(buf);          }
2152                  snprintf(buf + pos, len - pos - 1,          handler = strstr(header, "type=execute_handler");
2153                           " task.type=execute_handler");          if (handler)
2154          }                  len += ccs_truncate(handler) + 6;
2155          if (realpath) {          buffer = kmalloc(len, CCS_GFP_FLAGS);
2156                  const int pos = strlen(buf);          if (!buffer)
2157                  snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",                  return;
2158                           realpath);          snprintf(buffer, len - 1, "%s", cp);
2159                  kfree(realpath);          if (handler)
2160          }                  ccs_addprintf(buffer, len, " task.%s", handler);
2161          if (argv0) {          if (realpath)
2162                  const int pos = strlen(buf);                  ccs_addprintf(buffer, len, " exec.%s", realpath);
2163                  snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",          if (argv0)
2164                           argv0);                  ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
2165          }          if (symlink)
2166          cond = ccs_get_condition(buf);                  ccs_addprintf(buffer, len, "%s", symlink);
2167          kfree(buf);          ccs_normalize_line(buffer);
2168          return cond;          if (!ccs_write_domain2(buffer, ccs_current_domain(), false))
2169  }                  ccs_update_stat(CCS_STAT_POLICY_UPDATES);
2170            kfree(buffer);
 /**  
  * ccs_get_symlink_condition - Get condition part for symlink requests.  
  *  
  * @r: Pointer to "struct ccs_request_info".  
  *  
  * Returns pointer to "struct ccs_condition" on success, NULL otherwise.  
  */  
 static struct ccs_condition *ccs_get_symlink_condition  
 (const struct ccs_request_info *r)  
 {  
         struct ccs_condition *cond;  
         char *buf;  
         int len = 256;  
         const char *symlink = NULL;  
         const struct ccs_profile *profile = ccs_profile(r->profile);  
         if (profile->learning->learning_symlink_target) {  
                 symlink = r->obj->symlink_target->name;  
                 len += strlen(symlink) + 18;  
         }  
         buf = kmalloc(len, CCS_GFP_FLAGS);  
         if (!buf)  
                 return NULL;  
         snprintf(buf, len - 1, "if");  
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (symlink) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",  
                          symlink);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
2171  }  }
2172    
 /* Wait queue for ccs_query_list. */  
 static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);  
   
 /* Lock for manipulating ccs_query_list. */  
 static DEFINE_SPINLOCK(ccs_query_list_lock);  
   
 /* Structure for query. */  
 struct ccs_query_entry {  
         struct list_head list;  
         char *query;  
         int query_len;  
         unsigned int serial;  
         int timer;  
         int answer;  
 };  
   
 /* The list for "struct ccs_query_entry". */  
 static LIST_HEAD(ccs_query_list);  
   
 /* Number of "struct file" referring /proc/ccs/query interface. */  
 static atomic_t ccs_query_observers = ATOMIC_INIT(0);  
   
2173  /**  /**
2174   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
2175   *   *
# Line 2246  static atomic_t ccs_query_observers = AT Line 2184  static atomic_t ccs_query_observers = AT
2184  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2185  {  {
2186          va_list args;          va_list args;
2187          int error = -EPERM;          int error;
         int pos;  
2188          int len;          int len;
2189          static unsigned int ccs_serial;          static unsigned int ccs_serial;
2190          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query entry = { };
2191          bool quota_exceeded = false;          bool quota_exceeded = false;
2192          char *header;          va_start(args, fmt);
2193          struct ccs_domain_info * const domain = ccs_current_domain();          len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2194          switch (r->mode) {          va_end(args);
2195                  char *buffer;          /* Write /proc/ccs/audit. */
2196                  struct ccs_condition *cond;          va_start(args, fmt);
2197          case CCS_CONFIG_LEARNING:          ccs_write_log2(r, len, fmt, args);
2198                  if (!ccs_domain_quota_ok(r))          va_end(args);
2199                          return 0;          /* Nothing more to do if granted. */
2200                  va_start(args, fmt);          if (r->granted)
                 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;  
                 va_end(args);  
                 buffer = kmalloc(len, CCS_GFP_FLAGS);  
                 if (!buffer)  
                         return 0;  
                 va_start(args, fmt);  
                 vsnprintf(buffer, len - 1, fmt, args);  
                 va_end(args);  
                 ccs_normalize_line(buffer);  
                 if (r->param_type == CCS_TYPE_PATH_ACL &&  
                     r->param.path.operation == CCS_TYPE_EXECUTE)  
                         cond = ccs_get_execute_condition(r->ee);  
                 else if (r->param_type == CCS_TYPE_PATH_ACL &&  
                          r->param.path.operation == CCS_TYPE_SYMLINK)  
                         cond = ccs_get_symlink_condition(r);  
                 else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {  
                         char str[] = "if task.type=execute_handler";  
                         cond = ccs_get_condition(str);  
                 } else  
                         cond = NULL;  
                 ccs_write_domain2(buffer, domain, cond, false);  
                 ccs_put_condition(cond);  
                 kfree(buffer);  
                 /* fall through */  
         case CCS_CONFIG_PERMISSIVE:  
2201                  return 0;                  return 0;
2202          }          if (r->mode)
2203          if (!atomic_read(&ccs_query_observers)) {                  ccs_update_stat(r->mode);
2204            switch (r->mode) {
2205                  int i;                  int i;
2206                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  struct ccs_profile *p;
2207                          return -EPERM;          case CCS_CONFIG_ENFORCING:
2208                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->                  error = -EPERM;
2209                               enforcing_penalty; i++) {                  if (atomic_read(&ccs_query_observers))
2210                            break;
2211                    if (r->dont_sleep_on_enforce_error)
2212                            goto out;
2213                    p = ccs_profile(r->profile);
2214                    /* Check enforcing_penalty parameter. */
2215                    for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) {
2216                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2217                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2218                  }                  }
2219                  return -EPERM;                  goto out;
2220            case CCS_CONFIG_LEARNING:
2221                    error = 0;
2222                    /* Check mac_learning_entry parameter. */
2223                    if (ccs_domain_quota_ok(r))
2224                            break;
2225                    /* fall through */
2226            default:
2227                    return 0;
2228          }          }
2229            /* Get message. */
2230          va_start(args, fmt);          va_start(args, fmt);
2231          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;          entry.query = ccs_init_log(r, len, fmt, args);
2232          va_end(args);          va_end(args);
2233          header = ccs_init_log(&len, r);          if (!entry.query)
         if (!header)  
                 goto out;  
         ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);  
         if (!ccs_query_entry)  
2234                  goto out;                  goto out;
2235          len = ccs_round2(len);          entry.query_len = strlen(entry.query) + 1;
2236          ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);          if (!error) {
2237          if (!ccs_query_entry->query)                  ccs_add_entry(entry.query);
2238                  goto out;                  goto out;
2239          INIT_LIST_HEAD(&ccs_query_entry->list);          }
2240            len = ccs_round2(entry.query_len);
2241          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2242          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_memory_quota[CCS_MEMORY_QUERY] &&
2243              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              ccs_memory_used[CCS_MEMORY_QUERY] + len
2244                >= ccs_memory_quota[CCS_MEMORY_QUERY]) {
2245                  quota_exceeded = true;                  quota_exceeded = true;
2246          } else {          } else {
2247                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  entry.serial = ccs_serial++;
2248                  ccs_query_entry->serial = ccs_serial++;                  entry.retry = r->retry;
2249                    ccs_memory_used[CCS_MEMORY_QUERY] += len;
2250                    list_add_tail(&entry.list, &ccs_query_list);
2251          }          }
2252          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2253          if (quota_exceeded)          if (quota_exceeded)
2254                  goto out;                  goto out;
         pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",  
                        ccs_query_entry->serial, r->retry, header);  
         kfree(header);  
         header = NULL;  
         va_start(args, fmt);  
         vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);  
         ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;  
         va_end(args);  
         spin_lock(&ccs_query_list_lock);  
         list_add_tail(&ccs_query_entry->list, &ccs_query_list);  
         spin_unlock(&ccs_query_list_lock);  
2255          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2256          for (ccs_query_entry->timer = 0;          while (entry.timer < 10) {
2257               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;                  wake_up_all(&ccs_query_wait);
2258               ccs_query_entry->timer++) {                  if (wait_event_interruptible_timeout
2259                  wake_up(&ccs_query_wait);                      (ccs_answer_wait, entry.answer ||
2260                  set_current_state(TASK_INTERRUPTIBLE);                       !atomic_read(&ccs_query_observers), HZ))
                 schedule_timeout(HZ / 10);  
                 if (ccs_query_entry->answer)  
2261                          break;                          break;