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

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

revision 3945 by kumaneko, Sat Sep 4 12:08:57 2010 UTC revision 4280 by kumaneko, Fri Dec 31 02:05:48 2010 UTC
# Line 3  Line 3 
3   *   *
4   * Copyright (C) 2005-2010  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.8.0-pre   2010/09/01   * Version: 1.8.0+   2010/12/31
  *  
  * This file is applicable to both 2.4.30 and 2.6.11 and later.  
  * See README.ccs for ChangeLog.  
  *  
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  #ifdef CONFIG_CCSECURITY_AUDIT  
13          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,  /**
14          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,   * __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
15     *
16     * @wq:        The waitqueue to wait on.
17     * @condition: A C expression for the event to wait for.
18     * @ret:       Timeout, in jiffies.
19     *
20     * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
21     * signal, and the remaining jiffies otherwise if the condition evaluated to
22     * true before the timeout elapsed.
23     *
24     * This is for compatibility with older kernels.
25     */
26    #define __wait_event_interruptible_timeout(wq, condition, ret)          \
27    do {                                                                    \
28            wait_queue_t __wait;                                            \
29            init_waitqueue_entry(&__wait, current);                         \
30                                                                            \
31            add_wait_queue(&wq, &__wait);                                   \
32            for (;;) {                                                      \
33                    set_current_state(TASK_INTERRUPTIBLE);                  \
34                    if (condition)                                          \
35                            break;                                          \
36                    if (!signal_pending(current)) {                         \
37                            ret = schedule_timeout(ret);                    \
38                            if (!ret)                                       \
39                                    break;                                  \
40                            continue;                                       \
41                    }                                                       \
42                    ret = -ERESTARTSYS;                                     \
43                    break;                                                  \
44            }                                                               \
45            current->state = TASK_RUNNING;                                  \
46            remove_wait_queue(&wq, &__wait);                                \
47    } while (0)
48    
49    /**
50     * wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
51     *
52     * @wq:        The waitqueue to wait on.
53     * @condition: A C expression for the event to wait for.
54     * @timeout:   Timeout, in jiffies.
55     *
56     * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
57     * signal, and the remaining jiffies otherwise if the condition evaluated to
58     * true before the timeout elapsed.
59     *
60     * This is for compatibility with older kernels.
61     */
62    #define wait_event_interruptible_timeout(wq, condition, timeout)        \
63    ({                                                                      \
64            long __ret = timeout;                                           \
65            if (!(condition))                                               \
66                    __wait_event_interruptible_timeout(wq, condition, __ret); \
67            __ret;                                                          \
68    })
69    
70  #endif  #endif
71          .preference.audit_task_info = true,  
72          .preference.audit_path_info = true,  /**
73          .preference.enforcing_penalty = 0,   * list_for_each_cookie - iterate over a list with cookie.
74          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,   *
75          .preference.learning_exec_realpath = true,   * @pos:  Pointer to "struct list_head".
76          .preference.learning_exec_argv0 = true,   * @head: Pointer to "struct list_head".
77          .preference.learning_symlink_target = true,   */
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. */  /* Profile version. Currently only 20100903 is defined. */
84  static unsigned int ccs_profile_version;  static unsigned int ccs_profile_version;
# Line 32  static unsigned int ccs_profile_version; Line 86  static unsigned int ccs_profile_version;
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  const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {  const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
91          [CCS_CONFIG_DISABLED] = "disabled",          [CCS_CONFIG_DISABLED]   = "disabled",
92          [CCS_CONFIG_LEARNING] = "learning",          [CCS_CONFIG_LEARNING]   = "learning",
93          [CCS_CONFIG_PERMISSIVE] = "permissive",          [CCS_CONFIG_PERMISSIVE] = "permissive",
94          [CCS_CONFIG_ENFORCING] = "enforcing"          [CCS_CONFIG_ENFORCING]  = "enforcing"
95    };
96    
97    /* String table for /proc/ccs/profile interface. */
98    const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX
99                                        + CCS_MAX_MAC_CATEGORY_INDEX] = {
100            /* CONFIG::file group */
101            [CCS_MAC_FILE_EXECUTE]    = "execute",
102            [CCS_MAC_FILE_OPEN]       = "open",
103            [CCS_MAC_FILE_CREATE]     = "create",
104            [CCS_MAC_FILE_UNLINK]     = "unlink",
105            [CCS_MAC_FILE_GETATTR]    = "getattr",
106            [CCS_MAC_FILE_MKDIR]      = "mkdir",
107            [CCS_MAC_FILE_RMDIR]      = "rmdir",
108            [CCS_MAC_FILE_MKFIFO]     = "mkfifo",
109            [CCS_MAC_FILE_MKSOCK]     = "mksock",
110            [CCS_MAC_FILE_TRUNCATE]   = "truncate",
111            [CCS_MAC_FILE_SYMLINK]    = "symlink",
112            [CCS_MAC_FILE_MKBLOCK]    = "mkblock",
113            [CCS_MAC_FILE_MKCHAR]     = "mkchar",
114            [CCS_MAC_FILE_LINK]       = "link",
115            [CCS_MAC_FILE_RENAME]     = "rename",
116            [CCS_MAC_FILE_CHMOD]      = "chmod",
117            [CCS_MAC_FILE_CHOWN]      = "chown",
118            [CCS_MAC_FILE_CHGRP]      = "chgrp",
119            [CCS_MAC_FILE_IOCTL]      = "ioctl",
120            [CCS_MAC_FILE_CHROOT]     = "chroot",
121            [CCS_MAC_FILE_MOUNT]      = "mount",
122            [CCS_MAC_FILE_UMOUNT]     = "unmount",
123            [CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root",
124            /* CONFIG::misc group */
125            [CCS_MAC_ENVIRON] = "env",
126            /* CONFIG::network group */
127            [CCS_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
128            [CCS_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
129            [CCS_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
130            [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]     = "inet_stream_accept",
131            [CCS_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
132            [CCS_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
133            [CCS_MAC_NETWORK_INET_DGRAM_RECV]        = "inet_dgram_recv",
134            [CCS_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
135            [CCS_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
136            [CCS_MAC_NETWORK_INET_RAW_RECV]          = "inet_raw_recv",
137            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
138            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
139            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
140            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]     = "unix_stream_accept",
141            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
142            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
143            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]        = "unix_dgram_recv",
144            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
145            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
146            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
147            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  = "unix_seqpacket_accept",
148            /* CONFIG::ipc group */
149            [CCS_MAC_SIGNAL] = "signal",
150            /* CONFIG::capability group */
151            [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET]  = "use_route",
152            [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet",
153            [CCS_MAC_CAPABILITY_SYS_REBOOT]        = "SYS_REBOOT",
154            [CCS_MAC_CAPABILITY_SYS_VHANGUP]       = "SYS_VHANGUP",
155            [CCS_MAC_CAPABILITY_SYS_SETTIME]       = "SYS_TIME",
156            [CCS_MAC_CAPABILITY_SYS_NICE]          = "SYS_NICE",
157            [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME]   = "SYS_SETHOSTNAME",
158            [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module",
159            [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD]    = "SYS_KEXEC_LOAD",
160            [CCS_MAC_CAPABILITY_SYS_PTRACE]        = "SYS_PTRACE",
161            /* CONFIG group */
162            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE]       = "file",
163            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK]    = "network",
164            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC]       = "misc",
165            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC]        = "ipc",
166            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
167    };
168    
169    /* String table for path operation. */
170    const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
171            [CCS_TYPE_EXECUTE]    = "execute",
172            [CCS_TYPE_READ]       = "read",
173            [CCS_TYPE_WRITE]      = "write",
174            [CCS_TYPE_APPEND]     = "append",
175            [CCS_TYPE_UNLINK]     = "unlink",
176            [CCS_TYPE_GETATTR]    = "getattr",
177            [CCS_TYPE_RMDIR]      = "rmdir",
178            [CCS_TYPE_TRUNCATE]   = "truncate",
179            [CCS_TYPE_SYMLINK]    = "symlink",
180            [CCS_TYPE_CHROOT]     = "chroot",
181            [CCS_TYPE_UMOUNT]     = "unmount",
182    };
183    
184    /* String table for categories. */
185    static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
186            [CCS_MAC_CATEGORY_FILE]       = "file",
187            [CCS_MAC_CATEGORY_NETWORK]    = "network",
188            [CCS_MAC_CATEGORY_MISC]       = "misc",
189            [CCS_MAC_CATEGORY_IPC]        = "ipc",
190            [CCS_MAC_CATEGORY_CAPABILITY] = "capability",
191    };
192    
193    /* String table for conditions. */
194    const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
195            [CCS_TASK_UID]             = "task.uid",
196            [CCS_TASK_EUID]            = "task.euid",
197            [CCS_TASK_SUID]            = "task.suid",
198            [CCS_TASK_FSUID]           = "task.fsuid",
199            [CCS_TASK_GID]             = "task.gid",
200            [CCS_TASK_EGID]            = "task.egid",
201            [CCS_TASK_SGID]            = "task.sgid",
202            [CCS_TASK_FSGID]           = "task.fsgid",
203            [CCS_TASK_PID]             = "task.pid",
204            [CCS_TASK_PPID]            = "task.ppid",
205            [CCS_EXEC_ARGC]            = "exec.argc",
206            [CCS_EXEC_ENVC]            = "exec.envc",
207            [CCS_TYPE_IS_SOCKET]       = "socket",
208            [CCS_TYPE_IS_SYMLINK]      = "symlink",
209            [CCS_TYPE_IS_FILE]         = "file",
210            [CCS_TYPE_IS_BLOCK_DEV]    = "block",
211            [CCS_TYPE_IS_DIRECTORY]    = "directory",
212            [CCS_TYPE_IS_CHAR_DEV]     = "char",
213            [CCS_TYPE_IS_FIFO]         = "fifo",
214            [CCS_MODE_SETUID]          = "setuid",
215            [CCS_MODE_SETGID]          = "setgid",
216            [CCS_MODE_STICKY]          = "sticky",
217            [CCS_MODE_OWNER_READ]      = "owner_read",
218            [CCS_MODE_OWNER_WRITE]     = "owner_write",
219            [CCS_MODE_OWNER_EXECUTE]   = "owner_execute",
220            [CCS_MODE_GROUP_READ]      = "group_read",
221            [CCS_MODE_GROUP_WRITE]     = "group_write",
222            [CCS_MODE_GROUP_EXECUTE]   = "group_execute",
223            [CCS_MODE_OTHERS_READ]     = "others_read",
224            [CCS_MODE_OTHERS_WRITE]    = "others_write",
225            [CCS_MODE_OTHERS_EXECUTE]  = "others_execute",
226            [CCS_TASK_TYPE]            = "task.type",
227            [CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
228            [CCS_EXEC_REALPATH]        = "exec.realpath",
229            [CCS_SYMLINK_TARGET]       = "symlink.target",
230            [CCS_PATH1_UID]            = "path1.uid",
231            [CCS_PATH1_GID]            = "path1.gid",
232            [CCS_PATH1_INO]            = "path1.ino",
233            [CCS_PATH1_MAJOR]          = "path1.major",
234            [CCS_PATH1_MINOR]          = "path1.minor",
235            [CCS_PATH1_PERM]           = "path1.perm",
236            [CCS_PATH1_TYPE]           = "path1.type",
237            [CCS_PATH1_DEV_MAJOR]      = "path1.dev_major",
238            [CCS_PATH1_DEV_MINOR]      = "path1.dev_minor",
239            [CCS_PATH2_UID]            = "path2.uid",
240            [CCS_PATH2_GID]            = "path2.gid",
241            [CCS_PATH2_INO]            = "path2.ino",
242            [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 /proc/ccs/profile */  /* String table for PREFERENCE keyword. */
259  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +  static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
260                                      CCS_MAX_CAPABILITY_INDEX +          [CCS_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
261                                      CCS_MAX_MAC_CATEGORY_INDEX] = {          [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
262          [CCS_MAC_FILE_EXECUTE]          [CCS_PREF_ENFORCING_PENALTY]  = "enforcing_penalty",
         = "file::execute",  
         [CCS_MAC_FILE_OPEN]  
         = "file::open",  
         [CCS_MAC_FILE_CREATE]  
         = "file::create",  
         [CCS_MAC_FILE_UNLINK]  
         = "file::unlink",  
         [CCS_MAC_FILE_MKDIR]  
         = "file::mkdir",  
         [CCS_MAC_FILE_RMDIR]  
         = "file::rmdir",  
         [CCS_MAC_FILE_MKFIFO]  
         = "file::mkfifo",  
         [CCS_MAC_FILE_MKSOCK]  
         = "file::mksock",  
         [CCS_MAC_FILE_TRUNCATE]  
         = "file::truncate",  
         [CCS_MAC_FILE_SYMLINK]  
         = "file::symlink",  
         [CCS_MAC_FILE_MKBLOCK]  
         = "file::mkblock",  
         [CCS_MAC_FILE_MKCHAR]  
         = "file::mkchar",  
         [CCS_MAC_FILE_LINK]  
         = "file::link",  
         [CCS_MAC_FILE_RENAME]  
         = "file::rename",  
         [CCS_MAC_FILE_CHMOD]  
         = "file::chmod",  
         [CCS_MAC_FILE_CHOWN]  
         = "file::chown",  
         [CCS_MAC_FILE_CHGRP]  
         = "file::chgrp",  
         [CCS_MAC_FILE_IOCTL]  
         = "file::ioctl",  
         [CCS_MAC_FILE_CHROOT]  
         = "file::chroot",  
         [CCS_MAC_FILE_MOUNT]  
         = "file::mount",  
         [CCS_MAC_FILE_UMOUNT]  
         = "file::umount",  
         [CCS_MAC_FILE_PIVOT_ROOT]  
         = "file::pivot_root",  
         [CCS_MAC_ENVIRON]  
         = "misc::env",  
         [CCS_MAC_NETWORK_INET_STREAM_BIND]  
         = "network::inet_stream_bind",  
         [CCS_MAC_NETWORK_INET_STREAM_LISTEN]  
         = "network::inet_stream_listen",  
         [CCS_MAC_NETWORK_INET_STREAM_CONNECT]  
         = "network::inet_stream_connect",  
         [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]  
         = "network::inet_stream_accept",  
         [CCS_MAC_NETWORK_INET_DGRAM_BIND]  
         = "network::inet_dgram_bind",  
         [CCS_MAC_NETWORK_INET_DGRAM_SEND]  
         = "network::inet_dgram_send",  
         [CCS_MAC_NETWORK_INET_DGRAM_RECV]  
         = "network::inet_dgram_recv",  
         [CCS_MAC_NETWORK_INET_RAW_BIND]  
         = "network::inet_raw_bind",  
         [CCS_MAC_NETWORK_INET_RAW_SEND]  
         = "network::inet_raw_send",  
         [CCS_MAC_NETWORK_INET_RAW_RECV]  
         = "network::inet_raw_recv",  
         [CCS_MAC_NETWORK_UNIX_STREAM_BIND]  
         = "network::unix_stream_bind",  
         [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]  
         = "network::unix_stream_listen",  
         [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]  
         = "network::unix_stream_connect",  
         [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]  
         = "network::unix_stream_accept",  
         [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]  
         = "network::unix_dgram_bind",  
         [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]  
         = "network::unix_dgram_send",  
         [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]  
         = "network::unix_dgram_recv",  
         [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]  
         = "network::unix_seqpacket_bind",  
         [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  
         = "network::unix_seqpacket_listen",  
         [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT]  
         = "network::unix_seqpacket_connect",  
         [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  
         = "network::unix_seqpacket_accept",  
         [CCS_MAC_SIGNAL]  
         = "ipc::signal",  
         [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]  
         = "capability::use_route",  
         [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]  
         = "capability::use_packet",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]  
         = "capability::SYS_REBOOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]  
         = "capability::SYS_VHANGUP",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]  
         = "capability::SYS_TIME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]  
         = "capability::SYS_NICE",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]  
         = "capability::SYS_SETHOSTNAME",  
         [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]  
         = "capability::use_kernel_module",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]  
         = "capability::SYS_KEXEC_LOAD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]  
         = "capability::SYS_PTRACE",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_FILE] = "file",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_NETWORK] = "network",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_MISC] = "misc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_IPC] = "ipc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_CAPABILITY] = "capability",  
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 fpr 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   */   */
 static const char *ccs_yesno(const unsigned int value)  
 {  
         return value ? "yes" : "no";  
 }  
   
293  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
294  {  {
295          va_list args;          va_list args;
# Line 204  static void ccs_addprintf(char *buffer, Line 302  static void ccs_addprintf(char *buffer,
302  /**  /**
303   * ccs_flush - Flush queued string to userspace's buffer.   * 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".
306   *   *
307   * Returns true if all data was flushed, false otherwise.   * Returns true if all data was flushed, false otherwise.
308   */   */
# Line 250  static bool ccs_flush(struct ccs_io_buff Line 348  static bool ccs_flush(struct ccs_io_buff
348   * @head:   Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
349   * @string: String to print.   * @string: String to print.
350   *   *
351     * Returns nothing.
352     *
353   * Note that @string has to be kept valid until @head is kfree()d.   * Note that @string has to be kept valid until @head is kfree()d.
354   * This means that char[] allocated on stack memory cannot be passed to   * This means that char[] allocated on stack memory cannot be passed to
355   * this function. Use ccs_io_printf() for char[] allocated on stack memory.   * this function. Use ccs_io_printf() for char[] allocated on stack memory.
# Line 268  static void ccs_set_string(struct ccs_io Line 368  static void ccs_set_string(struct ccs_io
368   *   *
369   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
370   * @fmt:  The printf()'s format string, followed by parameters.   * @fmt:  The printf()'s format string, followed by parameters.
371     *
372     * Returns nothing.
373   */   */
374  void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
375  {  {
# Line 288  void ccs_io_printf(struct ccs_io_buffer Line 390  void ccs_io_printf(struct ccs_io_buffer
390          ccs_set_string(head, head->read_buf + pos);          ccs_set_string(head, head->read_buf + pos);
391  }  }
392    
393    /**
394     * ccs_set_space - Put a space to "struct ccs_io_buffer" structure.
395     *
396     * @head: Pointer to "struct ccs_io_buffer".
397     *
398     * Returns nothing.
399     */
400  static void ccs_set_space(struct ccs_io_buffer *head)  static void ccs_set_space(struct ccs_io_buffer *head)
401  {  {
402          ccs_set_string(head, " ");          ccs_set_string(head, " ");
403  }  }
404    
405    /**
406     * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure.
407     *
408     * @head: Pointer to "struct ccs_io_buffer".
409     *
410     * Returns nothing.
411     */
412  static bool ccs_set_lf(struct ccs_io_buffer *head)  static bool ccs_set_lf(struct ccs_io_buffer *head)
413  {  {
414          ccs_set_string(head, "\n");          ccs_set_string(head, "\n");
# Line 322  static struct ccs_profile *ccs_assign_pr Line 438  static struct ccs_profile *ccs_assign_pr
438          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
439                  ptr = entry;                  ptr = entry;
440                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
                         CCS_CONFIG_VERBOSE |  
441                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
442                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
443                         sizeof(ptr->config));                         sizeof(ptr->config));
444                    ptr->pref[CCS_PREF_MAX_AUDIT_LOG] =
445                            CONFIG_CCSECURITY_MAX_AUDIT_LOG;
446                    ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] =
447                            CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY;
448                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
449                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
450                  entry = NULL;                  entry = NULL;
451          }          }
452          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
453   out:  out:
454          kfree(entry);          kfree(entry);
455          return ptr;          return ptr;
456  }  }
457    
458  /**  /**
459   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
460     *
461     * Returns nothing.
462   */   */
463  static void ccs_check_profile(void)  static void ccs_check_profile(void)
464  {  {
465          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
466          const int idx = ccs_read_lock();          const int idx = ccs_read_lock();
467          ccs_policy_loaded = true;          ccs_policy_loaded = true;
468          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
469                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
470                  if (ccs_profile_ptr[profile])                  if (ccs_profile_ptr[profile])
471                          continue;                          continue;
472                    printk(KERN_ERR "Profile %u must be defined before using it.\n",
473                           profile);
474                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
475                           "for more information.\n");
476                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
477                        profile, domain->domainname->name);                        profile, domain->domainname->name);
478          }          }
479          ccs_read_unlock(idx);          ccs_read_unlock(idx);
480          if (ccs_profile_version != 20100903)          if (ccs_profile_version != 20100903) {
481                    printk(KERN_ERR "Userland tools must be installed for "
482                           "TOMOYO 1.8, and policy must be initialized.\n");
483                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
484                           "for more information.\n");
485                  panic("Profile version %u is not supported.\n",                  panic("Profile version %u is not supported.\n",
486                        ccs_profile_version);                        ccs_profile_version);
487          printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/09/01\n");          }
488            printk(KERN_INFO "CCSecurity: 1.8.0+   2010/12/31\n");
489          printk(KERN_INFO "Mandatory Access Control activated.\n");          printk(KERN_INFO "Mandatory Access Control activated.\n");
490  }  }
491    
# Line 368  static void ccs_check_profile(void) Line 498  static void ccs_check_profile(void)
498   */   */
499  struct ccs_profile *ccs_profile(const u8 profile)  struct ccs_profile *ccs_profile(const u8 profile)
500  {  {
501            static struct ccs_profile ccs_null_profile;
502          struct ccs_profile *ptr = ccs_profile_ptr[profile];          struct ccs_profile *ptr = ccs_profile_ptr[profile];
503          if (!ccs_policy_loaded)          if (!ptr)
504                  return &ccs_default_profile;                  ptr = &ccs_null_profile;
         BUG_ON(!ptr);  
505          return ptr;          return ptr;
506  }  }
507    
508    /**
509     * ccs_find_yesno - Find values for specified keyword.
510     *
511     * @string: String to check.
512     * @find:   Name of keyword.
513     *
514     * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
515     */
516  static s8 ccs_find_yesno(const char *string, const char *find)  static s8 ccs_find_yesno(const char *string, const char *find)
517  {  {
518          const char *cp = strstr(string, find);          const char *cp = strstr(string, find);
# Line 388  static s8 ccs_find_yesno(const char *str Line 526  static s8 ccs_find_yesno(const char *str
526          return -1;          return -1;
527  }  }
528    
529  static void ccs_set_bool(bool *b, const char *string, const char *find)  /**
530  {   * ccs_set_uint - Set value for specified preference.
531          switch (ccs_find_yesno(string, find)) {   *
532          case 1:   * @i:      Pointer to "unsigned int".
533                  *b = true;   * @string: String to check.
534                  break;   * @find:   Name of keyword.
535          case 0:   *
536                  *b = false;   * Returns nothing.
537                  break;   */
         }  
 }  
   
538  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
539  {  {
540          const char *cp = strstr(string, find);          const char *cp = strstr(string, find);
# Line 407  static void ccs_set_uint(unsigned int *i Line 542  static void ccs_set_uint(unsigned int *i
542                  sscanf(cp + strlen(find), "=%u", i);                  sscanf(cp + strlen(find), "=%u", i);
543  }  }
544    
545  static void ccs_set_pref(const char *name, const char *value,  /**
546                           struct ccs_profile *profile)   * ccs_set_mode - Set mode for specified profile.
547  {   *
548          if (!strcmp(name, "audit")) {   * @name:    Name of functionality.
549  #ifdef CONFIG_CCSECURITY_AUDIT   * @value:   Mode for @name.
550                  ccs_set_uint(&profile->preference.audit_max_grant_log, value,   * @profile: Pointer to "struct ccs_profile".
551                               "max_grant_log");   *
552                  ccs_set_uint(&profile->preference.audit_max_reject_log, value,   * Returns 0 on success, negative value otherwise.
553                               "max_reject_log");   */
 #endif  
                 ccs_set_bool(&profile->preference.audit_task_info, value,  
                              "task_info");  
                 ccs_set_bool(&profile->preference.audit_path_info, value,  
                              "path_info");  
                 return;  
         }  
         if (!strcmp(name, "enforcing")) {  
                 ccs_set_uint(&profile->preference.enforcing_penalty, value,  
                              "penalty");  
                 return;  
         }  
         if (!strcmp(name, "learning")) {  
                 ccs_set_uint(&profile->preference.learning_max_entry, value,  
                              "max_entry");  
                 ccs_set_bool(&profile->preference.learning_exec_realpath,  
                              value, "exec.realpath");  
                 ccs_set_bool(&profile->preference.learning_exec_argv0, value,  
                              "exec.argv0");  
                 ccs_set_bool(&profile->preference.learning_symlink_target,  
                              value, "symlink.target");  
                 return;  
         }  
 }  
   
554  static int ccs_set_mode(char *name, const char *value,  static int ccs_set_mode(char *name, const char *value,
555                          struct ccs_profile *profile)                          struct ccs_profile *profile)
556  {  {
557          u8 i;          u8 i;
558          u8 config;          u8 config;
559          if (!strcmp(name, "CONFIG")) {          if (!strcmp(name, "CONFIG")) {
560                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
                         + CCS_MAX_MAC_CATEGORY_INDEX;  
561                  config = profile->default_config;                  config = profile->default_config;
562          } else if (ccs_str_starts(&name, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
563                  config = 0;                  config = 0;
564                  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;
565                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                       i++) {
566                          if (strcmp(name, ccs_mac_keywords[i]))                          int len = 0;
567                            if (i < CCS_MAX_MAC_INDEX) {
568                                    const u8 c = ccs_index2category[i];
569                                    const char *category =
570                                            ccs_category_keywords[c];
571                                    len = strlen(category);
572                                    if (strncmp(name, category, len) ||
573                                        name[len++] != ':' || name[len++] != ':')
574                                            continue;
575                            }
576                            if (strcmp(name + len, ccs_mac_keywords[i]))
577                                  continue;                                  continue;
578                          config = profile->config[i];                          config = profile->config[i];
579                          break;                          break;
580                  }                  }
581                  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)  
582                          return -EINVAL;                          return -EINVAL;
583          } else {          } else {
584                  return -EINVAL;                  return -EINVAL;
# Line 477  static int ccs_set_mode(char *name, cons Line 595  static int ccs_set_mode(char *name, cons
595                                   */                                   */
596                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
597                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
                         switch (ccs_find_yesno(value, "verbose")) {  
                         case 1:  
                                 config |= CCS_CONFIG_VERBOSE;  
                                 break;  
                         case 0:  
                                 config &= ~CCS_CONFIG_VERBOSE;  
                                 break;  
                         }  
 #ifdef CONFIG_CCSECURITY_AUDIT  
598                          switch (ccs_find_yesno(value, "grant_log")) {                          switch (ccs_find_yesno(value, "grant_log")) {
599                          case 1:                          case 1:
600                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
# Line 502  static int ccs_set_mode(char *name, cons Line 611  static int ccs_set_mode(char *name, cons
611                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
612                                  break;                                  break;
613                          }                          }
 #endif  
614                  }                  }
615          }          }
616          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)  
617                  profile->config[i] = config;                  profile->config[i] = config;
618          else if (config != CCS_CONFIG_USE_DEFAULT)          else if (config != CCS_CONFIG_USE_DEFAULT)
619                  profile->default_config = config;                  profile->default_config = config;
# Line 539  static int ccs_write_profile(struct ccs_ Line 646  static int ccs_write_profile(struct ccs_
646          if (!cp)          if (!cp)
647                  return -EINVAL;                  return -EINVAL;
648          *cp++ = '\0';          *cp++ = '\0';
         if (ccs_str_starts(&data, "PREFERENCE::")) {  
                 ccs_set_pref(data, cp, profile);  
                 return 0;  
         }  
649          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
650                  const struct ccs_path_info *old_comment = profile->comment;                  const struct ccs_path_info *old_comment = profile->comment;
651                  profile->comment = ccs_get_name(cp);                  profile->comment = ccs_get_name(cp);
652                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
653                  return 0;                  return 0;
654          }          }
655            if (!strcmp(data, "PREFERENCE")) {
656                    for (i = 0; i < CCS_MAX_PREF; i++)
657                            ccs_set_uint(&profile->pref[i], cp,
658                                         ccs_pref_keywords[i]);
659                    return 0;
660            }
661          return ccs_set_mode(data, cp, profile);          return ccs_set_mode(data, cp, profile);
662  }  }
663    
664  static void ccs_print_preference(struct ccs_io_buffer *head, const int index)  /**
665  {   * ccs_print_config - Print mode for specified functionality.
666          struct ccs_profile *profile = ccs_profile_ptr[index];   *
667          struct ccs_preference *pref = &profile->preference;   * @head:   Pointer to "struct ccs_io_buffer".
668          ccs_io_printf(head, "%u-PREFERENCE::%s={ "   * @config: Mode for that functionality.
669  #ifdef CONFIG_CCSECURITY_AUDIT   *
670                        "max_grant_log=%u max_reject_log=%u "   * Returns nothing.
671  #endif   *
672                        "task_info=%s path_info=%s }\n", index,   * Caller prints functionality's name.
673                        "audit",   */
 #ifdef CONFIG_CCSECURITY_AUDIT  
                       pref->audit_max_grant_log,  
                       pref->audit_max_reject_log,  
 #endif  
                       ccs_yesno(pref->audit_task_info),  
                       ccs_yesno(pref->audit_path_info));  
         ccs_io_printf(head, "%u-PREFERENCE::%s={ "  
                       "max_entry=%u exec.realpath=%s "  
                       "exec.argv0=%s symlink.target=%s }\n",  
                       index, "learning",  
                       pref->learning_max_entry,  
                       ccs_yesno(pref->learning_exec_realpath),  
                       ccs_yesno(pref->learning_exec_argv0),  
                       ccs_yesno(pref->learning_symlink_target));  
         ccs_io_printf(head, "%u-PREFERENCE::%s={ penalty=%u }\n", index,  
                       "enforcing", pref->enforcing_penalty);  
 }  
   
674  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
675  {  {
676          ccs_io_printf(head, "={ mode=%s verbose=%s", ccs_mode[config & 3],          ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
677                        ccs_yesno(config & CCS_CONFIG_VERBOSE));                        ccs_mode[config & 3],
 #ifdef CONFIG_CCSECURITY_AUDIT  
         ccs_io_printf(head, " grant_log=%s reject_log=%s",  
678                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
679                        ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));                        ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
 #endif  
         ccs_set_string(head, " }\n");  
680  }  }
681    
682  /**  /**
683   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
684   *   *
685   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
686     *
687     * Returns nothing.
688   */   */
689  static void ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
690  {  {
691          u8 index;          u8 index;
692          const struct ccs_profile *profile;          const struct ccs_profile *profile;
693   next:  next:
694          index = head->r.index;          index = head->r.index;
695          profile = ccs_profile_ptr[index];          profile = ccs_profile_ptr[index];
696          switch (head->r.step) {          switch (head->r.step) {
# Line 620  static void ccs_read_profile(struct ccs_ Line 709  static void ccs_read_profile(struct ccs_
709                  break;                  break;
710          case 2:          case 2:
711                  {                  {
712                            u8 i;
713                          const struct ccs_path_info *comment = profile->comment;                          const struct ccs_path_info *comment = profile->comment;
714                          ccs_io_printf(head, "%u-COMMENT=", index);                          ccs_io_printf(head, "%u-COMMENT=", index);
715                          ccs_set_string(head, comment ? comment->name : "");                          ccs_set_string(head, comment ? comment->name : "");
716                          ccs_set_lf(head);                          ccs_set_lf(head);
717                            ccs_io_printf(head, "%u-PREFERENCE={ ", index);
718                            for (i = 0; i < CCS_MAX_PREF; i++)
719                                    ccs_io_printf(head, "%s=%u ",
720                                                  ccs_pref_keywords[i],
721                                                  profile->pref[i]);
722                            ccs_set_string(head, " }\n");
723                          head->r.step++;                          head->r.step++;
724                  }                  }
725                  break;                  break;
# Line 637  static void ccs_read_profile(struct ccs_ Line 733  static void ccs_read_profile(struct ccs_
733                  break;                  break;
734          case 4:          case 4:
735                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
                               + CCS_MAX_CAPABILITY_INDEX  
736                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
737                          const u8 i = head->r.bit;                          const u8 i = head->r.bit;
738                          const u8 config = profile->config[i];                          const u8 config = profile->config[i];
739                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
740                                  continue;                                  continue;
741                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",                          if (i < CCS_MAX_MAC_INDEX)
742                                        ccs_mac_keywords[i]);                                  ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
743                                                  ccs_category_keywords
744                                                  [ccs_index2category[i]],
745                                                  ccs_mac_keywords[i]);
746                            else
747                                    ccs_io_printf(head, "%u-CONFIG::%s", index,
748                                                  ccs_mac_keywords[i]);
749                          ccs_print_config(head, config);                          ccs_print_config(head, config);
750                          head->r.bit++;                          head->r.bit++;
751                          break;                          break;
752                  }                  }
753                  if (head->r.bit == CCS_MAX_MAC_INDEX                  if (head->r.bit == CCS_MAX_MAC_INDEX
                     + CCS_MAX_CAPABILITY_INDEX  
754                      + CCS_MAX_MAC_CATEGORY_INDEX) {                      + CCS_MAX_MAC_CATEGORY_INDEX) {
                         ccs_print_preference(head, index);  
755                          head->r.index++;                          head->r.index++;
756                          head->r.step = 1;                          head->r.step = 1;
757                  }                  }
# Line 662  static void ccs_read_profile(struct ccs_ Line 761  static void ccs_read_profile(struct ccs_
761                  goto next;                  goto next;
762  }  }
763    
764    /**
765     * ccs_same_manager - Check for duplicated "struct ccs_manager" entry.
766     *
767     * @a: Pointer to "struct ccs_acl_head".
768     * @b: Pointer to "struct ccs_acl_head".
769     *
770     * Returns true if @a == @b, false otherwise.
771     */
772  static bool ccs_same_manager(const struct ccs_acl_head *a,  static bool ccs_same_manager(const struct ccs_acl_head *a,
773                               const struct ccs_acl_head *b)                               const struct ccs_acl_head *b)
774  {  {
# Line 709  static int ccs_update_manager_entry(cons Line 816  static int ccs_update_manager_entry(cons
816  static int ccs_write_manager(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
817  {  {
818          char *data = head->write_buf;          char *data = head->write_buf;
819          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, "delete ");
820          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
821                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
822                  return 0;                  return 0;
# Line 722  static int ccs_write_manager(struct ccs_ Line 829  static int ccs_write_manager(struct ccs_
829   *   *
830   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
831   *   *
832     * Returns nothing.
833     *
834   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
835   */   */
836  static void ccs_read_manager(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
# Line 753  static bool ccs_manager(void) Line 862  static bool ccs_manager(void)
862  {  {
863          struct ccs_manager *ptr;          struct ccs_manager *ptr;
864          const char *exe;          const char *exe;
865          struct task_struct *task = current;          struct ccs_security *task = ccs_current_security();
866          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
867                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
868          bool found = false;          bool found = false;
# Line 764  static bool ccs_manager(void) Line 873  static bool ccs_manager(void)
873          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
874                  return false;                  return false;
875          exe = ccs_get_exe();          exe = ccs_get_exe();
876          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],          list_for_each_entry_srcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
877                                  head.list) {                                   head.list, &ccs_ss) {
878                  if (ptr->head.is_deleted)                  if (ptr->head.is_deleted)
879                          continue;                          continue;
880                  if (ptr->is_domain) {                  if (ptr->is_domain) {
# Line 794  static bool ccs_manager(void) Line 903  static bool ccs_manager(void)
903  }  }
904    
905  /**  /**
  * 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) {  
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp = '\0';  
                 cp += 4;  
         }  
         return cp;  
 }  
   
 /**  
906   * ccs_select_one - Parse select command.   * ccs_select_one - Parse select command.
907   *   *
908   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 873  static bool ccs_select_one(struct ccs_io Line 958  static bool ccs_select_one(struct ccs_io
958          return true;          return true;
959  }  }
960    
961    /**
962     * ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry.
963     *
964     * @a: Pointer to "struct ccs_acl_info".
965     * @b: Pointer to "struct ccs_acl_info".
966     *
967     * Returns true if @a == @b, false otherwise.
968     */
969  static bool ccs_same_handler_acl(const struct ccs_acl_info *a,  static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
970                                   const struct ccs_acl_info *b)                                   const struct ccs_acl_info *b)
971  {  {
972          const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);          const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
973          const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);          const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
974          return ccs_same_acl_head(&p1->head, &p2->head) &&          return p1->handler == p2->handler;
                 p1->handler == p2->handler;  
975  }  }
976    
977    /**
978     * ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry.
979     *
980     * @a: Pointer to "struct ccs_acl_info".
981     * @b: Pointer to "struct ccs_acl_info".
982     *
983     * Returns true if @a == @b, false otherwise.
984     */
985  static bool ccs_same_task_acl(const struct ccs_acl_info *a,  static bool ccs_same_task_acl(const struct ccs_acl_info *a,
986                                const struct ccs_acl_info *b)                                const struct ccs_acl_info *b)
987  {  {
988          const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);          const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
989          const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);          const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
990          return ccs_same_acl_head(&p1->head, &p2->head) &&          return p1->domainname == p2->domainname;
                 p1->domainname == p2->domainname;  
991  }  }
992    
993  /**  /**
994   * ccs_write_task - Update task related list.   * ccs_write_task - Update task related list.
995   *   *
996   * @data:      String to parse.   * @param: Pointer to "struct ccs_acl_param".
  * @domain:    Pointer to "struct ccs_domain_info".  
  * @condition: Pointer to "struct ccs_condition". Maybe NULL.  
  * @is_delete: True if it is a delete request.  
997   *   *
998   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
999   */   */
1000  static int ccs_write_task(char *data, struct ccs_domain_info *domain,  static int ccs_write_task(struct ccs_acl_param *param)
                           struct ccs_condition *condition,  
                           const bool is_delete)  
1001  {  {
1002          int error;          int error;
1003          const bool is_auto = ccs_str_starts(&data, "auto_domain_transition ");          const bool is_auto = ccs_str_starts(&param->data,
1004          if (!is_auto && !ccs_str_starts(&data, "manual_domain_transition ")) {                                              "auto_domain_transition ");
1005                  struct ccs_handler_acl e = {          if (!is_auto && !ccs_str_starts(&param->data,
1006                          .head.cond = condition,                                          "manual_domain_transition ")) {
1007                  };                  struct ccs_handler_acl e = { };
1008                  if (ccs_str_starts(&data, "auto_execute_handler "))                  char *handler;
1009                    if (ccs_str_starts(&param->data, "auto_execute_handler "))
1010                          e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;                          e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
1011                  else if (ccs_str_starts(&data, "denied_execute_handler "))                  else if (ccs_str_starts(&param->data,
1012                                            "denied_execute_handler "))
1013                          e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;                          e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
1014                  else                  else
1015                          return -EINVAL;                          return -EINVAL;
1016                  if (!ccs_correct_path(data))                  handler = ccs_read_token(param);
1017                    if (!ccs_correct_path(handler))
1018                          return -EINVAL;                          return -EINVAL;
1019                  e.handler = ccs_get_name(data);                  e.handler = ccs_get_name(handler);
1020                  if (!e.handler)                  if (!e.handler)
1021                          return -ENOMEM;                          return -ENOMEM;
1022                  if (e.handler->is_patterned)                  if (e.handler->is_patterned)
1023                          error = -EINVAL; /* No patterns allowed. */                          error = -EINVAL; /* No patterns allowed. */
1024                  else                  else
1025                          error = ccs_update_domain(&e.head, sizeof(e),                          error = ccs_update_domain(&e.head, sizeof(e), param,
                                                   is_delete, domain,  
1026                                                    ccs_same_handler_acl, NULL);                                                    ccs_same_handler_acl, NULL);
1027                  ccs_put_name(e.handler);                  ccs_put_name(e.handler);
1028          } else {          } else {
1029                  struct ccs_task_acl e = {                  struct ccs_task_acl e = {
1030                          .head.type = is_auto ?                          .head.type = is_auto ?
1031                          CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,                          CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
1032                          .head.cond = condition,                          .domainname = ccs_get_domainname(param),
1033                  };                  };
                 if (!ccs_correct_domain(data))  
                         return -EINVAL;  
                 e.domainname = ccs_get_name(data);  
1034                  if (!e.domainname)                  if (!e.domainname)
1035                          return -ENOMEM;                          error = -EINVAL;
1036                  error = ccs_update_domain(&e.head, sizeof(e), is_delete,                  else
1037                                            domain, ccs_same_task_acl, NULL);                          error = ccs_update_domain(&e.head, sizeof(e), param,
1038                                                      ccs_same_task_acl, NULL);
1039                  ccs_put_name(e.domainname);                  ccs_put_name(e.domainname);
1040          }          }
1041          return error;          return error;
1042  }  }
1043    
1044    /**
1045     * ccs_write_domain2 - Write domain policy.
1046     *
1047     * @data:      Policy to be interpreted.
1048     * @domain:    Pointer to "struct ccs_domain_info".
1049     * @is_delete: True if it is a delete request.
1050     *
1051     * Returns 0 on success, negative value otherwise.
1052     */
1053  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
1054                               const bool is_delete)                               const bool is_delete)
1055  {  {
1056            struct ccs_acl_param param = {
1057                    .data = data,
1058                    .domain = domain,
1059                    .is_delete = is_delete,
1060            };
1061          static const struct {          static const struct {
1062                  const char *keyword;                  const char *keyword;
1063                  int (*write) (char *, struct ccs_domain_info *,                  int (*write) (struct ccs_acl_param *);
                               struct ccs_condition *, const bool);  
1064          } ccs_callback[7] = {          } ccs_callback[7] = {
1065                  { "file ", ccs_write_file },                  { "file ", ccs_write_file },
1066                  { "network inet ", ccs_write_inet_network },                  { "network inet ", ccs_write_inet_network },
# Line 963  static int ccs_write_domain2(char *data, Line 1070  static int ccs_write_domain2(char *data,
1070                  { "ipc ", ccs_write_ipc },                  { "ipc ", ccs_write_ipc },
1071                  { "task ", ccs_write_task },                  { "task ", ccs_write_task },
1072          };          };
         int error = -EINVAL;  
1073          u8 i;          u8 i;
         struct ccs_condition *cond = NULL;  
         char *cp = ccs_find_condition_part(data);  
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
1074          for (i = 0; i < 7; i++) {          for (i = 0; i < 7; i++) {
1075                  if (!ccs_str_starts(&data, ccs_callback[i].keyword))                  if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
1076                          continue;                          continue;
1077                  error = ccs_callback[i].write(data, domain, cond, is_delete);                  return ccs_callback[i].write(&param);
                 break;  
1078          }          }
1079          if (cond)          return -EINVAL;
                 ccs_put_condition(cond);  
         return error;  
1080  }  }
1081    
1082  static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {  /* String table for domain flags. */
1083          [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",  const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1084          [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"          [CCS_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
1085            [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
1086  };  };
1087            
1088  /**  /**
1089   * ccs_write_domain - Write domain policy.   * ccs_write_domain - Write domain policy.
1090   *   *
# Line 1002  static int ccs_write_domain(struct ccs_i Line 1099  static int ccs_write_domain(struct ccs_i
1099          bool is_delete = false;          bool is_delete = false;
1100          bool is_select = false;          bool is_select = false;
1101          unsigned int profile;          unsigned int profile;
1102          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, "delete "))
1103                  is_delete = true;                  is_delete = true;
1104          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, "select "))
1105                  is_select = true;                  is_select = true;
1106          if (is_select && ccs_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1107                  return 0;                  return 0;
# Line 1025  static int ccs_write_domain(struct ccs_i Line 1122  static int ccs_write_domain(struct ccs_i
1122          if (!domain)          if (!domain)
1123                  return -EINVAL;                  return -EINVAL;
1124    
1125          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, "use_profile %u\n", &profile) == 1
1126              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
1127                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1128                          domain->profile = (u8) profile;                          if (!is_delete)
1129                                    domain->profile = (u8) profile;
1130                  return 0;                  return 0;
1131          }          }
1132          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1          if (sscanf(data, "use_group %u\n", &profile) == 1
1133              && profile < CCS_MAX_ACL_GROUPS) {              && profile < CCS_MAX_ACL_GROUPS) {
1134                  domain->group = (u8) profile;                  if (!is_delete)
1135                            domain->group = (u8) profile;
1136                  return 0;                  return 0;
1137          }          }
1138          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
# Line 1051  static int ccs_write_domain(struct ccs_i Line 1150  static int ccs_write_domain(struct ccs_i
1150   *   *
1151   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1152   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
1153     *
1154     * Returns nothing.
1155   */   */
1156  static void ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1157                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
# Line 1075  static void ccs_print_name_union(struct Line 1176  static void ccs_print_name_union(struct
1176   *   *
1177   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1178   * @ptr:  Pointer to "struct ccs_number_union".   * @ptr:  Pointer to "struct ccs_number_union".
1179     *
1180     * Returns nothing.
1181   */   */
1182  static void ccs_print_number_union(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1183                                     const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
# Line 1130  static bool ccs_print_condition(struct c Line 1233  static bool ccs_print_condition(struct c
1233  {  {
1234          switch (head->r.cond_step) {          switch (head->r.cond_step) {
1235          case 0:          case 0:
1236                  {                  head->r.cond_index = 0;
1237                          ccs_set_string(head, " if");                  head->r.cond_step++;
                         head->r.cond_index = 0;  
                         head->r.cond_step++;  
                 }  
1238                  /* fall through */                  /* fall through */
1239          case 1:          case 1:
1240                  {                  {
# Line 1243  static bool ccs_print_condition(struct c Line 1343  static bool ccs_print_condition(struct c
1343                  head->r.cond_step++;                  head->r.cond_step++;
1344                  /* fall through */                  /* fall through */
1345          case 3:          case 3:
1346                  if (cond->audit)                  if (cond->grant_log != CCS_GRANTLOG_AUTO)
1347                          ccs_io_printf(head, " audit=%s",                          ccs_io_printf(head, " grant_log=%s",
1348                                        ccs_yesno(cond->audit == 2));                                        ccs_yesno(cond->grant_log ==
1349                                                    CCS_GRANTLOG_YES));
1350                  if (cond->transit) {                  if (cond->transit) {
1351                          ccs_set_string(head, " auto_domain_transitition=\"");                          ccs_set_string(head, " auto_domain_transition=\"");
1352                          ccs_set_string(head, cond->transit->name);                          ccs_set_string(head, cond->transit->name);
1353                          ccs_set_string(head, "\"");                          ccs_set_string(head, "\"");
1354                  }                  }
# Line 1273  static u8 ccs_fns(const u8 perm, u8 bit) Line 1374  static u8 ccs_fns(const u8 perm, u8 bit)
1374          return bit;          return bit;
1375  }  }
1376    
1377    /**
1378     * ccs_set_group - Print "acl_group " header keyword.
1379     *
1380     * @head: Pointer to "struct ccs_io_buffer".
1381     *
1382     * Returns nothing.
1383     */
1384  static void ccs_set_group(struct ccs_io_buffer *head)  static void ccs_set_group(struct ccs_io_buffer *head)
1385  {  {
1386          if (head->type == CCS_EXCEPTIONPOLICY)          if (head->type == CCS_EXCEPTIONPOLICY)
# Line 1296  static bool ccs_print_entry(struct ccs_i Line 1404  static bool ccs_print_entry(struct ccs_i
1404                  goto print_cond_part;                  goto print_cond_part;
1405          if (acl->is_deleted)          if (acl->is_deleted)
1406                  return true;                  return true;
1407   next:  next:
1408          bit = head->r.bit;          bit = head->r.bit;
1409          if (!ccs_flush(head))          if (!ccs_flush(head))
1410                  return false;                  return false;
# Line 1349  static bool ccs_print_entry(struct ccs_i Line 1457  static bool ccs_print_entry(struct ccs_i
1457                          goto done;                          goto done;
1458                  ccs_set_group(head);                  ccs_set_group(head);
1459                  ccs_set_string(head, "file ");                  ccs_set_string(head, "file ");
1460                  ccs_set_string(head, ccs_mkdev_keyword[bit]);                  ccs_set_string(head, ccs_mac_keywords[ccs_pnnn2mac[bit]]);
1461                  ccs_print_name_union(head, &ptr->name);                  ccs_print_name_union(head, &ptr->name);
1462                  ccs_print_number_union(head, &ptr->mode);                  ccs_print_number_union(head, &ptr->mode);
1463                  ccs_print_number_union(head, &ptr->major);                  ccs_print_number_union(head, &ptr->major);
# Line 1362  static bool ccs_print_entry(struct ccs_i Line 1470  static bool ccs_print_entry(struct ccs_i
1470                          goto done;                          goto done;
1471                  ccs_set_group(head);                  ccs_set_group(head);
1472                  ccs_set_string(head, "file ");                  ccs_set_string(head, "file ");
1473                  ccs_set_string(head, ccs_path2_keyword[bit]);                  ccs_set_string(head, ccs_mac_keywords[ccs_pp2mac[bit]]);
1474                  ccs_print_name_union(head, &ptr->name1);                  ccs_print_name_union(head, &ptr->name1);
1475                  ccs_print_name_union(head, &ptr->name2);                  ccs_print_name_union(head, &ptr->name2);
1476          } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {          } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
# Line 1373  static bool ccs_print_entry(struct ccs_i Line 1481  static bool ccs_print_entry(struct ccs_i
1481                          goto done;                          goto done;
1482                  ccs_set_group(head);                  ccs_set_group(head);
1483                  ccs_set_string(head, "file ");                  ccs_set_string(head, "file ");
1484                  ccs_set_string(head, ccs_path_number_keyword[bit]);                  ccs_set_string(head, ccs_mac_keywords[ccs_pn2mac[bit]]);
1485                  ccs_print_name_union(head, &ptr->name);                  ccs_print_name_union(head, &ptr->name);
1486                  ccs_print_number_union(head, &ptr->number);                  ccs_print_number_union(head, &ptr->number);
1487          } else if (acl_type == CCS_TYPE_ENV_ACL) {          } else if (acl_type == CCS_TYPE_ENV_ACL) {
# Line 1387  static bool ccs_print_entry(struct ccs_i Line 1495  static bool ccs_print_entry(struct ccs_i
1495                          container_of(acl, typeof(*ptr), head);                          container_of(acl, typeof(*ptr), head);
1496                  ccs_set_group(head);                  ccs_set_group(head);
1497                  ccs_set_string(head, "capability ");                  ccs_set_string(head, "capability ");
1498                  ccs_set_string(head, ccs_cap2keyword(ptr->operation));                  ccs_set_string(head,
1499                                   ccs_mac_keywords[ccs_c2mac[ptr->operation]]);
1500          } else if (acl_type == CCS_TYPE_INET_ACL) {          } else if (acl_type == CCS_TYPE_INET_ACL) {
1501                  struct ccs_inet_acl *ptr =                  struct ccs_inet_acl *ptr =
1502                          container_of(acl, typeof(*ptr), head);                          container_of(acl, typeof(*ptr), head);
# Line 1454  static bool ccs_print_entry(struct ccs_i Line 1563  static bool ccs_print_entry(struct ccs_i
1563                  head->r.cond_step = 0;                  head->r.cond_step = 0;
1564                  if (!ccs_flush(head))                  if (!ccs_flush(head))
1565                          return false;                          return false;
1566   print_cond_part:  print_cond_part:
1567                  if (!ccs_print_condition(head, acl->cond))                  if (!ccs_print_condition(head, acl->cond))
1568                          return false;                          return false;
1569                  head->r.print_cond_part = false;                  head->r.print_cond_part = false;
# Line 1470  static bool ccs_print_entry(struct ccs_i Line 1579  static bool ccs_print_entry(struct ccs_i
1579          case CCS_TYPE_UNIX_ACL:          case CCS_TYPE_UNIX_ACL:
1580                  goto next;                  goto next;
1581          }          }
1582   done:  done:
1583          head->r.bit = 0;          head->r.bit = 0;
1584          return true;          return true;
1585  }  }
# Line 1482  static bool ccs_print_entry(struct ccs_i Line 1591  static bool ccs_print_entry(struct ccs_i
1591   * @domain: Pointer to "struct ccs_domain_info".   * @domain: Pointer to "struct ccs_domain_info".
1592   * @index:  Index number.   * @index:  Index number.
1593   *   *
  * Caller holds ccs_read_lock().  
  *  
1594   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1595     *
1596     * Caller holds ccs_read_lock().
1597   */   */
1598  static bool ccs_read_domain2(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1599                               struct ccs_domain_info *domain,                               struct ccs_domain_info *domain,
# Line 1505  static bool ccs_read_domain2(struct ccs_ Line 1614  static bool ccs_read_domain2(struct ccs_
1614   *   *
1615   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1616   *   *
1617     * Returns nothing.
1618     *
1619   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1620   */   */
1621  static void ccs_read_domain(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
# Line 1523  static void ccs_read_domain(struct ccs_i Line 1634  static void ccs_read_domain(struct ccs_i
1634                          /* Print domainname and flags. */                          /* Print domainname and flags. */
1635                          ccs_set_string(head, domain->domainname->name);                          ccs_set_string(head, domain->domainname->name);
1636                          ccs_set_lf(head);                          ccs_set_lf(head);
1637                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",                          ccs_io_printf(head, "use_profile %u\n",
1638                                        domain->profile);                                        domain->profile);
1639                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",                          ccs_io_printf(head, "use_group %u\n", domain->group);
                                       domain->group);  
1640                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1641                                  if (domain->flags[i])                                  if (domain->flags[i])
1642                                          ccs_set_string(head, ccs_dif[i]);                                          ccs_set_string(head, ccs_dif[i]);
# Line 1551  static void ccs_read_domain(struct ccs_i Line 1661  static void ccs_read_domain(struct ccs_i
1661                                  goto done;                                  goto done;
1662                  }                  }
1663          }          }
1664   done:  done:
1665          head->r.eof = true;          head->r.eof = true;
1666  }  }
1667    
# Line 1592  static int ccs_write_domain_profile(stru Line 1702  static int ccs_write_domain_profile(stru
1702   *   *
1703   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1704   *   *
1705     * Returns nothing.
1706     *
1707   * This is equivalent to doing   * This is equivalent to doing
1708   *   *
1709   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1620  static void ccs_read_domain_profile(stru Line 1732  static void ccs_read_domain_profile(stru
1732  }  }
1733    
1734  /**  /**
1735   * ccs_write_pid: Specify PID to obtain domainname.   * ccs_write_pid - Specify PID to obtain domainname.
1736   *   *
1737   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1738   *   *
# Line 1676  static void ccs_read_pid(struct ccs_io_b Line 1788  static void ccs_read_pid(struct ccs_io_b
1788  #endif  #endif
1789          if (p) {          if (p) {
1790                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1791                  ccs_flags = p->ccs_flags;                  ccs_flags = ccs_task_flags(p);
1792          }          }
1793          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1794          if (!domain)          if (!domain)
# Line 1693  static void ccs_read_pid(struct ccs_io_b Line 1805  static void ccs_read_pid(struct ccs_io_b
1805          }          }
1806  }  }
1807    
1808  static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {  /* String table for domain transition control keywords. */
1809          [CCS_TRANSITION_CONTROL_NO_INITIALIZE]  static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1810          = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,          [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1811          [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,          [CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1812          [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,          [CCS_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1813          [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN          [CCS_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
1814  };  };
1815    
1816  static const char *ccs_group_name[CCS_MAX_GROUP] = {  /* String table for grouping keywords. */
1817          [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,  static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1818          [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,          [CCS_PATH_GROUP]    = "path_group ",
1819          [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP          [CCS_NUMBER_GROUP]  = "number_group ",
1820            [CCS_ADDRESS_GROUP] = "address_group ",
1821  };  };
1822    
1823  /**  /**
# Line 1717  static const char *ccs_group_name[CCS_MA Line 1830  static const char *ccs_group_name[CCS_MA
1830  static int ccs_write_exception(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1831  {  {
1832          char *data = head->write_buf;          char *data = head->write_buf;
1833          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, "delete ");
1834          u8 i;          u8 i;
1835          static const struct {          static const struct {
1836                  const char *keyword;                  const char *keyword;
1837                  int (*write) (char *, const bool);                  int (*write) (char *, const bool);
1838          } ccs_callback[3] = {          } ccs_callback[2] = {
1839                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },                  { "aggregator ",    ccs_write_aggregator },
1840                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },                  { "deny_autobind ", ccs_write_reserved_port },
                 { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }  
1841          };          };
1842          for (i = 0; i < 3; i++)          for (i = 0; i < 2; i++)
1843                  if (ccs_str_starts(&data, ccs_callback[i].keyword))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1844                          return ccs_callback[i].write(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1845          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
# Line 1782  static bool ccs_read_group(struct ccs_io Line 1894  static bool ccs_read_group(struct ccs_io
1894                                                  head)->member_name->name);                                                  head)->member_name->name);
1895                          } else if (idx == CCS_NUMBER_GROUP) {                          } else if (idx == CCS_NUMBER_GROUP) {
1896                                  ccs_print_number_union(head, &container_of                                  ccs_print_number_union(head, &container_of
1897                                                         (ptr, struct ccs_number_group,                                                 (ptr, struct ccs_number_group,
1898                                                          head)->number);                                                  head)->number);
1899                          } else if (idx == CCS_ADDRESS_GROUP) {                          } else if (idx == CCS_ADDRESS_GROUP) {
1900                                  char buffer[128];                                  char buffer[128];
1901                                  struct ccs_address_group *member =                                  struct ccs_address_group *member =
# Line 1844  static bool ccs_read_policy(struct ccs_i Line 1956  static bool ccs_read_policy(struct ccs_i
1956                          {                          {
1957                                  struct ccs_aggregator *ptr =                                  struct ccs_aggregator *ptr =
1958                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1959                                  ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);                                  ccs_set_string(head, "aggregator ");
1960                                  ccs_set_string(head, ptr->original_name->name);                                  ccs_set_string(head, ptr->original_name->name);
1961                                  ccs_set_space(head);                                  ccs_set_space(head);
1962                                  ccs_set_string(head,                                  ccs_set_string(head,
1963                                                 ptr->aggregated_name->name);                                                 ptr->aggregated_name->name);
1964                          }                          }
1965                          break;                          break;
                 case CCS_ID_PATTERN:  
                         {  
                                 struct ccs_pattern *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);  
                                 ccs_set_string(head, ptr->pattern->name);  
                         }  
                         break;  
1966                  case CCS_ID_RESERVEDPORT:                  case CCS_ID_RESERVEDPORT:
1967                          {                          {
1968                                  struct ccs_reserved *ptr =                                  struct ccs_reserved *ptr =
1969                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1970                                  const u16 min_port = ptr->min_port;                                  const u16 min_port = ptr->min_port;
1971                                  const u16 max_port = ptr->max_port;                                  const u16 max_port = ptr->max_port;
1972                                  ccs_set_string(head,                                  ccs_set_string(head, "deny_autobind ");
                                                CCS_KEYWORD_DENY_AUTOBIND);  
1973                                  ccs_io_printf(head, "%u", min_port);                                  ccs_io_printf(head, "%u", min_port);
1974                                  if (min_port != max_port)                                  if (min_port != max_port)
1975                                          ccs_io_printf(head, "-%u", max_port);                                          ccs_io_printf(head, "-%u", max_port);
# Line 1886  static bool ccs_read_policy(struct ccs_i Line 1989  static bool ccs_read_policy(struct ccs_i
1989   *   *
1990   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1991   *   *
1992     * Returns nothing.
1993     *
1994   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1995   */   */
1996  static void ccs_read_exception(struct ccs_io_buffer *head)  static void ccs_read_exception(struct ccs_io_buffer *head)
# Line 1915  static void ccs_read_exception(struct cc Line 2020  static void ccs_read_exception(struct cc
2020          head->r.eof = true;          head->r.eof = true;
2021  }  }
2022    
2023  /* Wait queue for ccs_query_list. */  /* Wait queue for kernel -> userspace notification. */
2024  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2025    /* Wait queue for userspace -> kernel notification. */
2026  /* Lock for manipulating ccs_query_list. */  static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
 static DEFINE_SPINLOCK(ccs_query_list_lock);  
2027    
2028  /* Structure for query. */  /* Structure for query. */
2029  struct ccs_query {  struct ccs_query {
# Line 1929  struct ccs_query { Line 2033  struct ccs_query {
2033          unsigned int serial;          unsigned int serial;
2034          int timer;          int timer;
2035          int answer;          int answer;
2036            u8 retry;
2037  };  };
2038    
2039  /* The list for "struct ccs_query". */  /* The list for "struct ccs_query". */
2040  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
2041    
2042    /* Lock for manipulating ccs_query_list. */
2043    static DEFINE_SPINLOCK(ccs_query_list_lock);
2044    
2045  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
2046  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2047    
2048  static void ccs_truncate(char *str)  /**
2049     * ccs_truncate - Truncate a line.
2050     *
2051     * @str: String to truncate.
2052     *
2053     * Returns length of truncated @str.
2054     */
2055    static int ccs_truncate(char *str)
2056  {  {
2057          while (* (unsigned char *) str > (unsigned char) ' ')          char *start = str;
2058            while (*(unsigned char *) str > (unsigned char) ' ')
2059                  str++;                  str++;
2060          *str = '\0';          *str = '\0';
2061            return strlen(start) + 1;
2062    }
2063    
2064    /**
2065     * ccs_add_entry - Add an ACL to current thread's domain. Used by learning mode.
2066     *
2067     * @header: Lines containing ACL.
2068     *
2069     * Returns nothing.
2070     */
2071    static void ccs_add_entry(char *header)
2072    {
2073            char *buffer;
2074            char *realpath = NULL;
2075            char *argv0 = NULL;
2076            char *symlink = NULL;
2077            char *handler;
2078            char *cp = strchr(header, '\n');
2079            int len;
2080            if (!cp)
2081                    return;
2082            cp = strchr(cp + 1, '\n');
2083            if (!cp)
2084                    return;
2085            *cp++ = '\0';
2086            len = strlen(cp) + 1;
2087            /* strstr() will return NULL if ordering is wrong. */
2088            if (*cp == 'f') {
2089                    argv0 = strstr(header, " argv[]={ \"");
2090                    if (argv0) {
2091                            argv0 += 10;
2092                            len += ccs_truncate(argv0) + 14;
2093                    }
2094                    realpath = strstr(header, " exec={ realpath=\"");
2095                    if (realpath) {
2096                            realpath += 8;
2097                            len += ccs_truncate(realpath) + 6;
2098                    }
2099                    symlink = strstr(header, " symlink.target=\"");
2100                    if (symlink)
2101                            len += ccs_truncate(symlink + 1) + 1;
2102            }
2103            handler = strstr(header, "type=execute_handler");
2104            if (handler)
2105                    len += ccs_truncate(handler) + 6;
2106            buffer = kmalloc(len, CCS_GFP_FLAGS);
2107            if (!buffer)
2108                    return;
2109            snprintf(buffer, len - 1, "%s", cp);
2110            if (handler)
2111                    ccs_addprintf(buffer, len, " task.%s", handler);
2112            if (realpath)
2113                    ccs_addprintf(buffer, len, " exec.%s", realpath);
2114            if (argv0)
2115                    ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
2116            if (symlink)
2117                    ccs_addprintf(buffer, len, "%s", symlink);
2118            ccs_normalize_line(buffer);
2119            if (!ccs_write_domain2(buffer, ccs_current_domain(), false))
2120                    ccs_update_stat(CCS_STAT_POLICY_UPDATES);
2121            kfree(buffer);
2122  }  }
2123    
2124  /**  /**
# Line 1958  static void ccs_truncate(char *str) Line 2135  static void ccs_truncate(char *str)
2135  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2136  {  {
2137          va_list args;          va_list args;
2138          int error = -EPERM;          int error;
         int pos;  
2139          int len;          int len;
2140          static unsigned int ccs_serial;          static unsigned int ccs_serial;
2141          struct ccs_query *entry = NULL;          struct ccs_query entry = { };
2142          bool quota_exceeded = false;          bool quota_exceeded = false;
         char *header;  
         struct ccs_domain_info * const domain = ccs_current_domain();  
2143          va_start(args, fmt);          va_start(args, fmt);
2144          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;          len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2145          va_end(args);          va_end(args);
2146          if (r->mode == CCS_CONFIG_LEARNING) {          /* Write /proc/ccs/audit. */
2147                  char *buffer;          va_start(args, fmt);
2148                  char *realpath = NULL;          ccs_write_log2(r, len, fmt, args);
2149                  char *argv0 = NULL;          va_end(args);
2150                  char *symlink = NULL;          /* Nothing more to do if granted. */
2151                  char *handler = NULL;          if (r->granted)
                 const struct ccs_preference *pref;  
                 if (!ccs_domain_quota_ok(r))  
                         return 0;  
                 header = ccs_init_log(&len, r);  
                 if (!header)  
                         return 0;  
                 pref = &ccs_profile(r->profile)->preference;  
                 /* strstr() will return NULL if ordering is wrong. */  
                 if (r->param_type == CCS_TYPE_PATH_ACL &&  
                     r->param.path.operation == CCS_TYPE_EXECUTE) {  
                         if (pref->learning_exec_argv0) {  
                                 argv0 = strstr(header, " argv[]={ \"");  
                                 if (argv0) {  
                                         argv0 += 10;  
                                         ccs_truncate(argv0);  
                                 }  
                         }  
                         if (pref->learning_exec_realpath) {  
                                 realpath = strstr(header,  
                                                   " exec={ realpath=\"");  
                                 if (realpath) {  
                                         realpath += 8;  
                                         ccs_truncate(realpath);  
                                 }  
                         }  
                 } else if (r->param_type == CCS_TYPE_PATH_ACL &&  
                            r->param.path.operation == CCS_TYPE_SYMLINK &&  
                            pref->learning_symlink_target) {  
                         symlink = strstr(header, " symlink.target=\"");  
                         if (symlink)  
                                 ccs_truncate(symlink + 1);  
                 }  
                 handler = strstr(header, "type=execute_handler");  
                 if (handler)  
                         ccs_truncate(handler);  
                 buffer = kmalloc(len, CCS_GFP_FLAGS);  
                 if (buffer) {  
                         va_start(args, fmt);  
                         vsnprintf(buffer, len - 1, fmt, args);  
                         va_end(args);  
                         if (handler || realpath || argv0 || symlink) {  
                                 ccs_addprintf(buffer, len, " if");  
                                 if (handler)  
                                         ccs_addprintf(buffer, len, " task.%s",  
                                                       handler);  
                                 if (realpath)  
                                         ccs_addprintf(buffer, len, " exec.%s",  
                                                       realpath);  
                                 if (argv0)  
                                         ccs_addprintf(buffer, len,  
                                                       " exec.argv[0]=%s",  
                                                       argv0);  
                                 if (symlink)  
                                         ccs_addprintf(buffer, len, "%s",  
                                                       symlink);  
                         }  
                         ccs_normalize_line(buffer);  
                         ccs_write_domain2(buffer, domain, false);  
                         kfree(buffer);  
                 }  
                 kfree(header);  
                 return 0;  
         }  
         if (r->mode != CCS_CONFIG_ENFORCING)  
2152                  return 0;                  return 0;
2153          if (!atomic_read(&ccs_query_observers)) {          if (r->mode)
2154                    ccs_update_stat(r->mode);
2155            switch (r->mode) {
2156                  int i;                  int i;
2157                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  struct ccs_profile *p;
2158                          return -EPERM;          case CCS_CONFIG_ENFORCING:
2159                  for (i = 0; i < ccs_profile(domain->profile)->preference.                  error = -EPERM;
2160                               enforcing_penalty; i++) {                  if (atomic_read(&ccs_query_observers))
2161                            break;
2162                    if (r->dont_sleep_on_enforce_error)
2163                            goto out;
2164                    p = ccs_profile(r->profile);
2165                    /* Check enforcing_penalty parameter. */
2166                    for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) {
2167                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2168                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2169                  }                  }
                 return -EPERM;  
         }  
         header = ccs_init_log(&len, r);  
         if (!header)  
2170                  goto out;                  goto out;
2171          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);          case CCS_CONFIG_LEARNING:
2172          if (!entry)                  error = 0;
2173                    /* Check mac_learning_entry parameter. */
2174                    if (ccs_domain_quota_ok(r))
2175                            break;
2176                    /* fall through */
2177            default:
2178                    return 0;
2179            }
2180            /* Get message. */
2181            va_start(args, fmt);
2182            entry.query = ccs_init_log(r, len, fmt, args);
2183            va_end(args);
2184            if (!entry.query)
2185                  goto out;                  goto out;
2186          len = ccs_round2(len);          entry.query_len = strlen(entry.query) + 1;
2187          entry->query = kzalloc(len, CCS_GFP_FLAGS);          if (!error) {
2188          if (!entry->query)                  ccs_add_entry(entry.query);
2189                  goto out;                  goto out;
2190            }
2191            len = ccs_round2(entry.query_len);
2192          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2193          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_memory_quota[CCS_MEMORY_QUERY] &&
2194              sizeof(*entry) >= ccs_quota_for_query) {              ccs_memory_used[CCS_MEMORY_QUERY] + len
2195                >= ccs_memory_quota[CCS_MEMORY_QUERY]) {
2196                  quota_exceeded = true;                  quota_exceeded = true;
2197          } else {          } else {
2198                  ccs_query_memory_size += len + sizeof(*entry);                  entry.serial = ccs_serial++;
2199                  entry->serial = ccs_serial++;                  entry.retry = r->retry;
2200                    ccs_memory_used[CCS_MEMORY_QUERY] += len;
2201                    list_add_tail(&entry.list, &ccs_query_list);
2202          }          }
2203          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2204          if (quota_exceeded)          if (quota_exceeded)
2205                  goto out;                  goto out;
         pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",  
                        entry->serial, r->retry, header);  
         kfree(header);  
         header = NULL;  
         va_start(args, fmt);  
         vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);  
         entry->query_len = strlen(entry->query) + 1;  
         va_end(args);  
         spin_lock(&ccs_query_list_lock);  
         list_add_tail(&entry->list, &ccs_query_list);  
         spin_unlock(&ccs_query_list_lock);  
2206          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2207          for (entry->timer = 0;          while (entry.timer < 10) {
2208               atomic_read(&ccs_query_observers) && entry->timer < 100;                  wake_up_all(&ccs_query_wait);
2209               entry->timer++) {                  if (wait_event_interruptible_timeout
2210                  wake_up(&ccs_query_wait);                      (ccs_answer_wait, entry.answer ||
2211                  set_current_state(TASK_INTERRUPTIBLE);                       !atomic_read(&ccs_query_observers), HZ))
                 schedule_timeout(HZ / 10);  
                 if (entry->answer)  
2212                          break;                          break;
2213                    else
2214                            entry.timer++;
2215          }          }
2216          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2217          list_del(&entry->list);          list_del(&entry.list);
2218          ccs_query_memory_size -= len + sizeof(*entry);          ccs_memory_used[CCS_MEMORY_QUERY] -= len;
2219          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2220          switch (entry->answer) {          switch (entry.answer) {
2221          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2222                  error = CCS_RETRY_REQUEST;                  error = CCS_RETRY_REQUEST;
2223                  r->retry++;                  r->retry++;
# Line 2106  int ccs_supervisor(struct ccs_request_in Line 2226  int ccs_supervisor(struct ccs_request_in
2226                  /* Granted by administrator. */                  /* Granted by administrator. */
2227                  error = 0;                  error = 0;
2228                  break;                  break;
         case 0:  
                 /* Timed out. */  
                 break;  
2229          default:          default:
2230                  /* Rejected by administrator. */                  /* Timed out or rejected by administrator. */
2231                  break;                  break;
2232          }          }
2233   out:  out:
2234          if (entry)          kfree(entry.query);
                 kfree(entry->query);  
         kfree(entry);  
         kfree(header);  
2235          return error;          return error;
2236  }  }
2237    
# Line 2160  static int ccs_poll_query(struct file *f Line 2274  static int ccs_poll_query(struct file *f
2274   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2275   *   *
2276   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
2277     *
2278     * Returns nothing.
2279   */   */
2280  static void ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2281  {  {
# Line 2169  static void ccs_read_query(struct ccs_io Line 2285  static void ccs_read_query(struct ccs_io
2285          char *buf;          char *buf;
2286          if (head->r.w_pos)          if (head->r.w_pos)
2287                  return;                  return;
2288          if (head->read_buf) {          kfree(head->read_buf);
2289                  kfree(head->read_buf);          head->read_buf = NULL;
                 head->read_buf = NULL;  
         }  
2290          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2291          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2292                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
# Line 2188  static void ccs_read_query(struct ccs_io Line 2302  static void ccs_read_query(struct ccs_io
2302                  head->r.query_index = 0;                  head->r.query_index = 0;
2303                  return;                  return;
2304          }          }
2305          buf = kzalloc(len, CCS_GFP_FLAGS);          buf = kzalloc(len + 32, CCS_GFP_FLAGS);
2306          if (!buf)          if (!buf)
2307                  return;                  return;
2308          pos = 0;          pos = 0;
# Line 2204  static void ccs_read_query(struct ccs_io Line 2318  static void ccs_read_query(struct ccs_io
2318                   * can change, but I don't care.                   * can change, but I don't care.
2319                   */                   */
2320                  if (len == ptr->query_len)                  if (len == ptr->query_len)
2321                          memmove(buf, ptr->query, len);                          snprintf(buf, len + 32, "Q%u-%hu\n%s", ptr->serial,
2322                                     ptr->retry, ptr->query);
2323                  break;                  break;
2324          }          }
2325          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2248  static int ccs_write_answer(struct ccs_i Line 2363  static int ccs_write_answer(struct ccs_i
2363                  break;                  break;
2364          }          }
2365          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2366            wake_up_all(&ccs_answer_wait);
2367          return 0;          return 0;
2368  }  }
2369    
2370  /**  /**
2371   * ccs_read_version: Get version.   * ccs_read_version - Get version.
2372   *   *
2373   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
2374     *
2375     * Returns nothing.
2376   */   */
2377  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2378  {  {
2379          if (head->r.eof)          if (head->r.eof)
2380                  return;                  return;
2381          ccs_set_string(head, "1.8.0-pre");          ccs_set_string(head, "1.8.0");
2382            head->r.eof = true;
2383    }
2384    
2385    /* String table for /proc/ccs/meminfo interface. */
2386    static const char * const ccs_policy_headers[CCS_MAX_POLICY_STAT] = {
2387            [CCS_STAT_POLICY_UPDATES]    = "update:",
2388            [CCS_STAT_POLICY_LEARNING]   = "violation in learning mode:",
2389            [CCS_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2390            [CCS_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
2391    };
2392    
2393    /* String table for /proc/ccs/meminfo interface. */
2394    static const char * const ccs_memory_headers[CCS_MAX_MEMORY_STAT] = {
2395            [CCS_MEMORY_POLICY] = "policy:",
2396            [CCS_MEMORY_AUDIT]  = "audit log:",
2397            [CCS_MEMORY_QUERY]  = "query message:",
2398    };
2399    
2400    /* Timestamp counter for last updated. */
2401    static unsigned int ccs_stat_updated[CCS_MAX_POLICY_STAT];
2402    /* Counter for number of updates. */
2403    static unsigned int ccs_stat_modified[CCS_MAX_POLICY_STAT];
2404    
2405    /**
2406     * ccs_update_stat - Update statistic counters.
2407     *
2408     * @index: Index for policy type.
2409     *
2410     * Returns nothing.
2411     */
2412    void ccs_update_stat(const u8 index)
2413    {
2414            struct timeval tv;
2415            do_gettimeofday(&tv);
2416            /*
2417             * I don't use atomic operations because race condition is not fatal.
2418             */
2419            ccs_stat_updated[index]++;
2420            ccs_stat_modified[index] = tv.tv_sec;
2421    }
2422    
2423    /**
2424     * ccs_read_stat - Read statistic data.
2425     *
2426     * @head: Pointer to "struct ccs_io_buffer".
2427     *
2428     * Returns nothing.
2429     */
2430    static void ccs_read_stat(struct ccs_io_buffer *head)
2431    {
2432            u8 i;
2433            unsigned int total = 0;
2434            if (head->r.eof)
2435                    return;
2436            for (i = 0; i < CCS_MAX_POLICY_STAT; i++) {
2437                    ccs_io_printf(head, "Policy %-30s %10u", ccs_policy_headers[i],
2438                                  ccs_stat_updated[i]);
2439                    if (ccs_stat_modified[i]) {
2440                            struct ccs_time stamp;
2441                            ccs_convert_time(ccs_stat_modified[i], &stamp);
2442                            ccs_io_printf(head, " (Last: %04u/%02u/%02u "
2443                                          "%02u:%02u:%02u)",
2444                                          stamp.year, stamp.month, stamp.day,
2445                                          stamp.hour, stamp.min, stamp.sec);
2446                    }
2447                    ccs_set_lf(head);
2448            }
2449            for (i = 0; i < CCS_MAX_MEMORY_STAT; i++) {
2450                    unsigned int used = ccs_memory_used[i];
2451                    total += used;
2452                    ccs_io_printf(head, "Memory used by %-22s %10u",
2453                                  ccs_memory_headers[i], used);
2454                    used = ccs_memory_quota[i];
2455                    if (used)
2456                            ccs_io_printf(head, " (Quota: %10u)", used);
2457                    ccs_set_lf(head);
2458            }
2459            ccs_io_printf(head, "Total memory used:                    %10u\n",
2460                          total);
2461          head->r.eof = true;          head->r.eof = true;
2462  }  }
2463    
2464  /**  /**
2465   * ccs_read_self_domain - Get the current process's domainname.   * ccs_write_stat - Set memory quota.
2466   *   *
2467   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
2468     *
2469     * Returns 0.
2470   */   */
2471  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static int ccs_write_stat(struct ccs_io_buffer *head)
2472  {  {
2473            char *data = head->write_buf;
2474            u8 i;
2475            if (ccs_str_starts(&data, "Memory used by "))
2476                    for (i = 0; i < CCS_MAX_MEMORY_STAT; i++)
2477                            if (ccs_str_starts(&data, ccs_memory_headers[i]))
2478                                    sscanf(data, "%u", &ccs_memory_quota[i]);
2479            return 0;
2480    }
2481    
2482    /* String table for /proc/ccs/meminfo interface. */
2483    static const char * const ccs_old_memory_header[CCS_MAX_MEMORY_STAT] = {
2484            [CCS_MEMORY_POLICY] = "Policy:",
2485            [CCS_MEMORY_AUDIT]  = "Audit logs:",
2486            [CCS_MEMORY_QUERY]  = "Query lists:",
2487    };
2488    
2489    /**
2490     * ccs_read_memory_counter - Read memory usage.
2491     *
2492     * @head: Pointer to "struct ccs_io_buffer".
2493     *
2494     * Returns nothing.
2495     */
2496    static void ccs_read_memory_counter(struct ccs_io_buffer *head)
2497    {
2498            unsigned int total = 0;
2499            int i;
2500          if (head->r.eof)          if (head->r.eof)
2501                  return;                  return;
2502          /*          for (i = 0; i < CCS_MAX_MEMORY_STAT; i++) {
2503           * ccs_current_domain()->domainname != NULL because every process                  unsigned int used = ccs_memory_used[i];
2504           * belongs to a domain and the domain's name cannot be NULL.                  total += used;
2505           */                  ccs_io_printf(head, "%-12s %10u", ccs_old_memory_header[i],
2506          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);                                used);
2507                    if (ccs_memory_quota[i])
2508                            ccs_io_printf(head, "   (Quota: %10u)",
2509                                          ccs_memory_quota[i]);
2510                    ccs_io_printf(head, "\n");
2511            }
2512            ccs_io_printf(head, "%-12s %10u\n", "Total:", total);
2513          head->r.eof = true;          head->r.eof = true;
2514  }  }
2515    
2516  /**  /**
2517     * ccs_write_memory_quota - Set memory quota.
2518     *
2519     * @head: Pointer to "struct ccs_io_buffer".
2520     *
2521     * Returns 0.
2522     */
2523    static int ccs_write_memory_quota(struct ccs_io_buffer *head)
2524    {
2525            char *data = head->write_buf;
2526            u8 i;
2527            for (i = 0; i < CCS_MAX_MEMORY_STAT; i++)
2528                    if (ccs_str_starts(&data, ccs_old_memory_header[i]))
2529                            sscanf(data, "%u", &ccs_memory_quota[i]);
2530            return 0;
2531    }
2532    
2533    /**
2534   * ccs_open_control - open() for /proc/ccs/ interface.   * ccs_open_control - open() for /proc/ccs/ interface.
2535   *   *
2536   * @type: Type of interface.   * @type: Type of interface.
2537   * @file: Pointer to "struct file".   * @file: Pointer to "struct file".
2538   *   *
2539   * Associates policy handler and returns 0 on success, -ENOMEM otherwise.   * Returns 0 on success, negative value otherwise.
2540   */   */
2541  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2542  {  {
# Line 2305  int ccs_open_control(const u8 type, stru Line 2554  int ccs_open_control(const u8 type, stru
2554                  head->write = ccs_write_exception;                  head->write = ccs_write_exception;
2555                  head->read = ccs_read_exception;                  head->read = ccs_read_exception;
2556                  break;                  break;
2557  #ifdef CONFIG_CCSECURITY_AUDIT          case CCS_AUDIT: /* /proc/ccs/audit */
         case CCS_GRANTLOG: /* /proc/ccs/grant_log */  
         case CCS_REJECTLOG: /* /proc/ccs/reject_log */  
2558                  head->poll = ccs_poll_log;                  head->poll = ccs_poll_log;
2559                  head->read = ccs_read_log;                  head->read = ccs_read_log;
2560                  break;                  break;
 #endif  
         case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */  
                 head->read = ccs_read_self_domain;  
                 break;  
2561          case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */          case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2562                  head->write = ccs_write_domain_profile;                  head->write = ccs_write_domain_profile;
2563                  head->read = ccs_read_domain_profile;                  head->read = ccs_read_domain_profile;
2564                  break;                  break;
2565          case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */          case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */
2566                  /* Allow execute_handler to read process's status. */                  /* Allow execute_handler to read process's status. */
2567                  if (!(current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                  if (!(ccs_current_flags() & CCS_TASK_IS_EXECUTE_HANDLER)) {
2568                          kfree(head);                          kfree(head);
2569                          return -EPERM;                          return -EPERM;
2570                  }                  }
# Line 2334  int ccs_open_control(const u8 type, stru Line 2577  int ccs_open_control(const u8 type, stru
2577                  head->read = ccs_read_version;                  head->read = ccs_read_version;
2578                  head->readbuf_size = 128;                  head->readbuf_size = 128;
2579                  break;                  break;
2580            case CCS_STAT: /* /proc/ccs/stat */
2581                    head->write = ccs_write_stat;
2582                    head->read = ccs_read_stat;
2583                    head->readbuf_size = 1024;
2584                    break;
2585          case CCS_MEMINFO: /* /proc/ccs/meminfo */          case CCS_MEMINFO: /* /proc/ccs/meminfo */
2586                  head->write = ccs_write_memory_quota;                  head->write = ccs_write_memory_quota;
2587                  head->read = ccs_read_memory_counter;                  head->read = ccs_read_memory_counter;
# Line 2385  int ccs_open_control(const u8 type, stru Line 2633  int ccs_open_control(const u8 type, stru
2633                          return -ENOMEM;                          return -ENOMEM;
2634                  }                  }
2635          }          }
         if (type != CCS_QUERY &&  
             type != CCS_GRANTLOG && type != CCS_REJECTLOG)  
                 head->reader_idx = ccs_lock();  
         file->private_data = head;  
         /*  
          * Call the handler now if the file is /proc/ccs/self_domain  
          * so that the user can use "cat < /proc/ccs/self_domain" to  
          * know the current process's domainname.  
          */  
         if (type == CCS_SELFDOMAIN)  
                 ccs_read_control(file, NULL, 0);  
2636          /*          /*
2637           * If the file is /proc/ccs/query , increment the observer counter.           * If the file is /proc/ccs/query, increment the observer counter.
2638           * The obserber counter is used by ccs_supervisor() to see if           * The obserber counter is used by ccs_supervisor() to see if
2639           * there is some process monitoring /proc/ccs/query.           * there is some process monitoring /proc/ccs/query.
2640           */           */
2641          else if (type == CCS_QUERY)          if (type == CCS_QUERY)
2642                  atomic_inc(&ccs_query_observers);                  atomic_inc(&ccs_query_observers);
2643            else if (type != CCS_AUDIT && type != CCS_VERSION &&
2644                     type != CCS_MEMINFO && type != CCS_STAT)
2645                    head->reader_idx = ccs_lock();
2646            file->private_data = head;
2647          return 0;          return 0;
2648  }  }
2649    
# Line 2412  int ccs_open_control(const u8 type, stru Line 2653  int ccs_open_control(const u8 type, stru
2653   * @file: Pointer to "struct file".   * @file: Pointer to "struct file".
2654   * @wait: Pointer to "poll_table".   * @wait: Pointer to "poll_table".
2655   *   *
2656     * Returns return value of poll().
2657     *
2658   * Waits for read readiness.   * Waits for read readiness.
2659   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2660   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by   * /proc/ccs/audit is handled by /usr/sbin/ccs-auditd.
  * /usr/sbin/ccs-auditd .  
2661   */   */
2662  int ccs_poll_control(struct file *file, poll_table *wait)  int ccs_poll_control(struct file *file, poll_table *wait)
2663  {  {
# Line 2518  int ccs_write_control(struct file *file, Line 2760  int ccs_write_control(struct file *file,
2760                  cp0[head->w.avail - 1] = '\0';                  cp0[head->w.avail - 1] = '\0';
2761                  head->w.avail = 0;                  head->w.avail = 0;
2762                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2763                  head->write(head);                  if (head->write(head))
2764                            continue;
2765                    switch (head->type) {
2766                    case CCS_DOMAINPOLICY:
2767                    case CCS_EXCEPTIONPOLICY:
2768                    case CCS_DOMAIN_STATUS:
2769                    case CCS_MEMINFO:
2770                    case CCS_PROFILE:
2771                    case CCS_MANAGER:
2772                            ccs_update_stat(CCS_STAT_POLICY_UPDATES);
2773                    }
2774          }          }
2775          ccs_read_unlock(idx);          ccs_read_unlock(idx);
2776          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
# Line 2530  int ccs_write_control(struct file *file, Line 2782  int ccs_write_control(struct file *file,
2782   *   *
2783   * @file: Pointer to "struct file".   * @file: Pointer to "struct file".
2784   *   *
2785   * Releases memory and returns 0.   * Returns 0.
2786   */   */
2787  int ccs_close_control(struct file *file)  int ccs_close_control(struct file *file)
2788  {  {
# Line 2538  int ccs_close_control(struct file *file) Line 2790  int ccs_close_control(struct file *file)
2790          const bool is_write = head->write_buf != NULL;          const bool is_write = head->write_buf != NULL;
2791          const u8 type = head->type;          const u8 type = head->type;
2792          /*          /*
2793           * If the file is /proc/ccs/query , decrement the observer counter.           * If the file is /proc/ccs/query, decrement the observer counter.
2794           */           */
2795          if (type == CCS_QUERY)          if (type == CCS_QUERY) {
2796                  atomic_dec(&ccs_query_observers);                  if (atomic_dec_and_test(&ccs_query_observers))
2797          if (type != CCS_QUERY &&                          wake_up_all(&ccs_answer_wait);
2798              type != CCS_GRANTLOG && type != CCS_REJECTLOG)          } else if (type != CCS_AUDIT && type != CCS_VERSION &&
2799                       type != CCS_MEMINFO && type != CCS_STAT)
2800                  ccs_unlock(head->reader_idx);                  ccs_unlock(head->reader_idx);
2801          /* Release memory used for policy I/O. */          /* Release memory used for policy I/O. */
2802          kfree(head->read_buf);          kfree(head->read_buf);
# Line 2558  int ccs_close_control(struct file *file) Line 2811  int ccs_close_control(struct file *file)
2811          return 0;          return 0;
2812  }  }
2813    
2814    /**
2815     * ccs_policy_io_init - Register hooks for policy I/O.
2816     *
2817     * Returns nothing.
2818     */
2819  void __init ccs_policy_io_init(void)  void __init ccs_policy_io_init(void)
2820  {  {
2821          ccsecurity_ops.check_profile = ccs_check_profile;          ccsecurity_ops.check_profile = ccs_check_profile;

Legend:
Removed from v.3945  
changed lines
  Added in v.4280

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