3 |
* |
* |
4 |
* Copyright (C) 2005-2011 NTT DATA CORPORATION |
* Copyright (C) 2005-2011 NTT DATA CORPORATION |
5 |
* |
* |
6 |
* Version: 1.8.3 2011/09/29 |
* Version: 1.8.3+ 2011/11/11 |
7 |
*/ |
*/ |
8 |
|
|
9 |
#include "internal.h" |
#include "internal.h" |
10 |
|
|
11 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
/***** SECTION1: Constants definition *****/ |
12 |
|
|
13 |
/** |
/* Define this to enable debug mode. */ |
14 |
* __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses. |
/* #define DEBUG_CONDITION */ |
|
* |
|
|
* @wq: The waitqueue to wait on. |
|
|
* @condition: A C expression for the event to wait for. |
|
|
* @ret: Timeout, in jiffies. |
|
|
* |
|
|
* Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a |
|
|
* signal, and the remaining jiffies otherwise if the condition evaluated to |
|
|
* true before the timeout elapsed. |
|
|
* |
|
|
* This is for compatibility with older kernels. |
|
|
*/ |
|
|
#define __wait_event_interruptible_timeout(wq, condition, ret) \ |
|
|
do { \ |
|
|
wait_queue_t __wait; \ |
|
|
init_waitqueue_entry(&__wait, current); \ |
|
|
\ |
|
|
add_wait_queue(&wq, &__wait); \ |
|
|
for (;;) { \ |
|
|
set_current_state(TASK_INTERRUPTIBLE); \ |
|
|
if (condition) \ |
|
|
break; \ |
|
|
if (!signal_pending(current)) { \ |
|
|
ret = schedule_timeout(ret); \ |
|
|
if (!ret) \ |
|
|
break; \ |
|
|
continue; \ |
|
|
} \ |
|
|
ret = -ERESTARTSYS; \ |
|
|
break; \ |
|
|
} \ |
|
|
current->state = TASK_RUNNING; \ |
|
|
remove_wait_queue(&wq, &__wait); \ |
|
|
} while (0) |
|
|
|
|
|
/** |
|
|
* wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses. |
|
|
* |
|
|
* @wq: The waitqueue to wait on. |
|
|
* @condition: A C expression for the event to wait for. |
|
|
* @timeout: Timeout, in jiffies. |
|
|
* |
|
|
* Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a |
|
|
* signal, and the remaining jiffies otherwise if the condition evaluated to |
|
|
* true before the timeout elapsed. |
|
|
* |
|
|
* This is for compatibility with older kernels. |
|
|
*/ |
|
|
#define wait_event_interruptible_timeout(wq, condition, timeout) \ |
|
|
({ \ |
|
|
long __ret = timeout; \ |
|
|
if (!(condition)) \ |
|
|
__wait_event_interruptible_timeout(wq, condition, __ret); \ |
|
|
__ret; \ |
|
|
}) |
|
15 |
|
|
16 |
|
#ifdef DEBUG_CONDITION |
17 |
|
#define dprintk printk |
18 |
|
#else |
19 |
|
#define dprintk(...) do { } while (0) |
20 |
#endif |
#endif |
21 |
|
|
22 |
/** |
/* Mapping table from "enum ccs_mac_index" to "enum ccs_mac_category_index". */ |
23 |
* list_for_each_cookie - iterate over a list with cookie. |
static const u8 ccs_index2category[CCS_MAX_MAC_INDEX] = { |
24 |
* |
/* CONFIG::file group */ |
25 |
* @pos: Pointer to "struct list_head". |
[CCS_MAC_FILE_EXECUTE] = CCS_MAC_CATEGORY_FILE, |
26 |
* @head: Pointer to "struct list_head". |
[CCS_MAC_FILE_OPEN] = CCS_MAC_CATEGORY_FILE, |
27 |
*/ |
[CCS_MAC_FILE_CREATE] = CCS_MAC_CATEGORY_FILE, |
28 |
#define list_for_each_cookie(pos, head) \ |
[CCS_MAC_FILE_UNLINK] = CCS_MAC_CATEGORY_FILE, |
29 |
for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \ |
#ifdef CONFIG_CCSECURITY_FILE_GETATTR |
30 |
pos != (head); pos = srcu_dereference(pos->next, &ccs_ss)) |
[CCS_MAC_FILE_GETATTR] = CCS_MAC_CATEGORY_FILE, |
31 |
|
#endif |
32 |
|
[CCS_MAC_FILE_MKDIR] = CCS_MAC_CATEGORY_FILE, |
33 |
|
[CCS_MAC_FILE_RMDIR] = CCS_MAC_CATEGORY_FILE, |
34 |
|
[CCS_MAC_FILE_MKFIFO] = CCS_MAC_CATEGORY_FILE, |
35 |
|
[CCS_MAC_FILE_MKSOCK] = CCS_MAC_CATEGORY_FILE, |
36 |
|
[CCS_MAC_FILE_TRUNCATE] = CCS_MAC_CATEGORY_FILE, |
37 |
|
[CCS_MAC_FILE_SYMLINK] = CCS_MAC_CATEGORY_FILE, |
38 |
|
[CCS_MAC_FILE_MKBLOCK] = CCS_MAC_CATEGORY_FILE, |
39 |
|
[CCS_MAC_FILE_MKCHAR] = CCS_MAC_CATEGORY_FILE, |
40 |
|
[CCS_MAC_FILE_LINK] = CCS_MAC_CATEGORY_FILE, |
41 |
|
[CCS_MAC_FILE_RENAME] = CCS_MAC_CATEGORY_FILE, |
42 |
|
[CCS_MAC_FILE_CHMOD] = CCS_MAC_CATEGORY_FILE, |
43 |
|
[CCS_MAC_FILE_CHOWN] = CCS_MAC_CATEGORY_FILE, |
44 |
|
[CCS_MAC_FILE_CHGRP] = CCS_MAC_CATEGORY_FILE, |
45 |
|
[CCS_MAC_FILE_IOCTL] = CCS_MAC_CATEGORY_FILE, |
46 |
|
[CCS_MAC_FILE_CHROOT] = CCS_MAC_CATEGORY_FILE, |
47 |
|
[CCS_MAC_FILE_MOUNT] = CCS_MAC_CATEGORY_FILE, |
48 |
|
[CCS_MAC_FILE_UMOUNT] = CCS_MAC_CATEGORY_FILE, |
49 |
|
[CCS_MAC_FILE_PIVOT_ROOT] = CCS_MAC_CATEGORY_FILE, |
50 |
|
#ifdef CONFIG_CCSECURITY_MISC |
51 |
|
/* CONFIG::misc group */ |
52 |
|
[CCS_MAC_ENVIRON] = CCS_MAC_CATEGORY_MISC, |
53 |
|
#endif |
54 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
55 |
|
/* CONFIG::network group */ |
56 |
|
[CCS_MAC_NETWORK_INET_STREAM_BIND] = CCS_MAC_CATEGORY_NETWORK, |
57 |
|
[CCS_MAC_NETWORK_INET_STREAM_LISTEN] = CCS_MAC_CATEGORY_NETWORK, |
58 |
|
[CCS_MAC_NETWORK_INET_STREAM_CONNECT] = CCS_MAC_CATEGORY_NETWORK, |
59 |
|
[CCS_MAC_NETWORK_INET_STREAM_ACCEPT] = CCS_MAC_CATEGORY_NETWORK, |
60 |
|
[CCS_MAC_NETWORK_INET_DGRAM_BIND] = CCS_MAC_CATEGORY_NETWORK, |
61 |
|
[CCS_MAC_NETWORK_INET_DGRAM_SEND] = CCS_MAC_CATEGORY_NETWORK, |
62 |
|
#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG |
63 |
|
[CCS_MAC_NETWORK_INET_DGRAM_RECV] = CCS_MAC_CATEGORY_NETWORK, |
64 |
|
#endif |
65 |
|
[CCS_MAC_NETWORK_INET_RAW_BIND] = CCS_MAC_CATEGORY_NETWORK, |
66 |
|
[CCS_MAC_NETWORK_INET_RAW_SEND] = CCS_MAC_CATEGORY_NETWORK, |
67 |
|
#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG |
68 |
|
[CCS_MAC_NETWORK_INET_RAW_RECV] = CCS_MAC_CATEGORY_NETWORK, |
69 |
|
#endif |
70 |
|
[CCS_MAC_NETWORK_UNIX_STREAM_BIND] = CCS_MAC_CATEGORY_NETWORK, |
71 |
|
[CCS_MAC_NETWORK_UNIX_STREAM_LISTEN] = CCS_MAC_CATEGORY_NETWORK, |
72 |
|
[CCS_MAC_NETWORK_UNIX_STREAM_CONNECT] = CCS_MAC_CATEGORY_NETWORK, |
73 |
|
[CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT] = CCS_MAC_CATEGORY_NETWORK, |
74 |
|
[CCS_MAC_NETWORK_UNIX_DGRAM_BIND] = CCS_MAC_CATEGORY_NETWORK, |
75 |
|
[CCS_MAC_NETWORK_UNIX_DGRAM_SEND] = CCS_MAC_CATEGORY_NETWORK, |
76 |
|
#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG |
77 |
|
[CCS_MAC_NETWORK_UNIX_DGRAM_RECV] = CCS_MAC_CATEGORY_NETWORK, |
78 |
|
#endif |
79 |
|
[CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND] = CCS_MAC_CATEGORY_NETWORK, |
80 |
|
[CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = CCS_MAC_CATEGORY_NETWORK, |
81 |
|
[CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = CCS_MAC_CATEGORY_NETWORK, |
82 |
|
[CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT] = CCS_MAC_CATEGORY_NETWORK, |
83 |
|
#endif |
84 |
|
#ifdef CONFIG_CCSECURITY_IPC |
85 |
|
/* CONFIG::ipc group */ |
86 |
|
[CCS_MAC_SIGNAL] = CCS_MAC_CATEGORY_IPC, |
87 |
|
#endif |
88 |
|
#ifdef CONFIG_CCSECURITY_CAPABILITY |
89 |
|
/* CONFIG::capability group */ |
90 |
|
[CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET] = CCS_MAC_CATEGORY_CAPABILITY, |
91 |
|
[CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = CCS_MAC_CATEGORY_CAPABILITY, |
92 |
|
[CCS_MAC_CAPABILITY_SYS_REBOOT] = CCS_MAC_CATEGORY_CAPABILITY, |
93 |
|
[CCS_MAC_CAPABILITY_SYS_VHANGUP] = CCS_MAC_CATEGORY_CAPABILITY, |
94 |
|
[CCS_MAC_CAPABILITY_SYS_SETTIME] = CCS_MAC_CATEGORY_CAPABILITY, |
95 |
|
[CCS_MAC_CAPABILITY_SYS_NICE] = CCS_MAC_CATEGORY_CAPABILITY, |
96 |
|
[CCS_MAC_CAPABILITY_SYS_SETHOSTNAME] = CCS_MAC_CATEGORY_CAPABILITY, |
97 |
|
[CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = CCS_MAC_CATEGORY_CAPABILITY, |
98 |
|
[CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD] = CCS_MAC_CATEGORY_CAPABILITY, |
99 |
|
[CCS_MAC_CAPABILITY_SYS_PTRACE] = CCS_MAC_CATEGORY_CAPABILITY, |
100 |
|
#endif |
101 |
|
}; |
102 |
|
|
103 |
/* String table for operation mode. */ |
/* String table for operation mode. */ |
104 |
const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = { |
static const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = { |
105 |
[CCS_CONFIG_DISABLED] = "disabled", |
[CCS_CONFIG_DISABLED] = "disabled", |
106 |
[CCS_CONFIG_LEARNING] = "learning", |
[CCS_CONFIG_LEARNING] = "learning", |
107 |
[CCS_CONFIG_PERMISSIVE] = "permissive", |
[CCS_CONFIG_PERMISSIVE] = "permissive", |
116 |
[CCS_MAC_FILE_OPEN] = "open", |
[CCS_MAC_FILE_OPEN] = "open", |
117 |
[CCS_MAC_FILE_CREATE] = "create", |
[CCS_MAC_FILE_CREATE] = "create", |
118 |
[CCS_MAC_FILE_UNLINK] = "unlink", |
[CCS_MAC_FILE_UNLINK] = "unlink", |
119 |
|
#ifdef CONFIG_CCSECURITY_FILE_GETATTR |
120 |
[CCS_MAC_FILE_GETATTR] = "getattr", |
[CCS_MAC_FILE_GETATTR] = "getattr", |
121 |
|
#endif |
122 |
[CCS_MAC_FILE_MKDIR] = "mkdir", |
[CCS_MAC_FILE_MKDIR] = "mkdir", |
123 |
[CCS_MAC_FILE_RMDIR] = "rmdir", |
[CCS_MAC_FILE_RMDIR] = "rmdir", |
124 |
[CCS_MAC_FILE_MKFIFO] = "mkfifo", |
[CCS_MAC_FILE_MKFIFO] = "mkfifo", |
137 |
[CCS_MAC_FILE_MOUNT] = "mount", |
[CCS_MAC_FILE_MOUNT] = "mount", |
138 |
[CCS_MAC_FILE_UMOUNT] = "unmount", |
[CCS_MAC_FILE_UMOUNT] = "unmount", |
139 |
[CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root", |
[CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root", |
140 |
|
#ifdef CONFIG_CCSECURITY_MISC |
141 |
/* CONFIG::misc group */ |
/* CONFIG::misc group */ |
142 |
[CCS_MAC_ENVIRON] = "env", |
[CCS_MAC_ENVIRON] = "env", |
143 |
|
#endif |
144 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
145 |
/* CONFIG::network group */ |
/* CONFIG::network group */ |
146 |
[CCS_MAC_NETWORK_INET_STREAM_BIND] = "inet_stream_bind", |
[CCS_MAC_NETWORK_INET_STREAM_BIND] = "inet_stream_bind", |
147 |
[CCS_MAC_NETWORK_INET_STREAM_LISTEN] = "inet_stream_listen", |
[CCS_MAC_NETWORK_INET_STREAM_LISTEN] = "inet_stream_listen", |
149 |
[CCS_MAC_NETWORK_INET_STREAM_ACCEPT] = "inet_stream_accept", |
[CCS_MAC_NETWORK_INET_STREAM_ACCEPT] = "inet_stream_accept", |
150 |
[CCS_MAC_NETWORK_INET_DGRAM_BIND] = "inet_dgram_bind", |
[CCS_MAC_NETWORK_INET_DGRAM_BIND] = "inet_dgram_bind", |
151 |
[CCS_MAC_NETWORK_INET_DGRAM_SEND] = "inet_dgram_send", |
[CCS_MAC_NETWORK_INET_DGRAM_SEND] = "inet_dgram_send", |
152 |
|
#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG |
153 |
[CCS_MAC_NETWORK_INET_DGRAM_RECV] = "inet_dgram_recv", |
[CCS_MAC_NETWORK_INET_DGRAM_RECV] = "inet_dgram_recv", |
154 |
|
#endif |
155 |
[CCS_MAC_NETWORK_INET_RAW_BIND] = "inet_raw_bind", |
[CCS_MAC_NETWORK_INET_RAW_BIND] = "inet_raw_bind", |
156 |
[CCS_MAC_NETWORK_INET_RAW_SEND] = "inet_raw_send", |
[CCS_MAC_NETWORK_INET_RAW_SEND] = "inet_raw_send", |
157 |
|
#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG |
158 |
[CCS_MAC_NETWORK_INET_RAW_RECV] = "inet_raw_recv", |
[CCS_MAC_NETWORK_INET_RAW_RECV] = "inet_raw_recv", |
159 |
|
#endif |
160 |
[CCS_MAC_NETWORK_UNIX_STREAM_BIND] = "unix_stream_bind", |
[CCS_MAC_NETWORK_UNIX_STREAM_BIND] = "unix_stream_bind", |
161 |
[CCS_MAC_NETWORK_UNIX_STREAM_LISTEN] = "unix_stream_listen", |
[CCS_MAC_NETWORK_UNIX_STREAM_LISTEN] = "unix_stream_listen", |
162 |
[CCS_MAC_NETWORK_UNIX_STREAM_CONNECT] = "unix_stream_connect", |
[CCS_MAC_NETWORK_UNIX_STREAM_CONNECT] = "unix_stream_connect", |
163 |
[CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT] = "unix_stream_accept", |
[CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT] = "unix_stream_accept", |
164 |
[CCS_MAC_NETWORK_UNIX_DGRAM_BIND] = "unix_dgram_bind", |
[CCS_MAC_NETWORK_UNIX_DGRAM_BIND] = "unix_dgram_bind", |
165 |
[CCS_MAC_NETWORK_UNIX_DGRAM_SEND] = "unix_dgram_send", |
[CCS_MAC_NETWORK_UNIX_DGRAM_SEND] = "unix_dgram_send", |
166 |
|
#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG |
167 |
[CCS_MAC_NETWORK_UNIX_DGRAM_RECV] = "unix_dgram_recv", |
[CCS_MAC_NETWORK_UNIX_DGRAM_RECV] = "unix_dgram_recv", |
168 |
|
#endif |
169 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND] = "unix_seqpacket_bind", |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND] = "unix_seqpacket_bind", |
170 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = "unix_seqpacket_listen", |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = "unix_seqpacket_listen", |
171 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect", |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect", |
172 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT] = "unix_seqpacket_accept", |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT] = "unix_seqpacket_accept", |
173 |
|
#endif |
174 |
|
#ifdef CONFIG_CCSECURITY_IPC |
175 |
/* CONFIG::ipc group */ |
/* CONFIG::ipc group */ |
176 |
[CCS_MAC_SIGNAL] = "signal", |
[CCS_MAC_SIGNAL] = "signal", |
177 |
|
#endif |
178 |
|
#ifdef CONFIG_CCSECURITY_CAPABILITY |
179 |
/* CONFIG::capability group */ |
/* CONFIG::capability group */ |
180 |
[CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET] = "use_route", |
[CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET] = "use_route", |
181 |
[CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet", |
[CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet", |
187 |
[CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module", |
[CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module", |
188 |
[CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD", |
[CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD", |
189 |
[CCS_MAC_CAPABILITY_SYS_PTRACE] = "SYS_PTRACE", |
[CCS_MAC_CAPABILITY_SYS_PTRACE] = "SYS_PTRACE", |
190 |
|
#endif |
191 |
/* CONFIG group */ |
/* CONFIG group */ |
192 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE] = "file", |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE] = "file", |
193 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
194 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK] = "network", |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK] = "network", |
195 |
|
#endif |
196 |
|
#ifdef CONFIG_CCSECURITY_MISC |
197 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC] = "misc", |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC] = "misc", |
198 |
|
#endif |
199 |
|
#ifdef CONFIG_CCSECURITY_IPC |
200 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC] = "ipc", |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC] = "ipc", |
201 |
|
#endif |
202 |
|
#ifdef CONFIG_CCSECURITY_CAPABILITY |
203 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability", |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability", |
204 |
|
#endif |
205 |
}; |
}; |
206 |
|
|
207 |
/* String table for path operation. */ |
/* String table for path operation. */ |
211 |
[CCS_TYPE_WRITE] = "write", |
[CCS_TYPE_WRITE] = "write", |
212 |
[CCS_TYPE_APPEND] = "append", |
[CCS_TYPE_APPEND] = "append", |
213 |
[CCS_TYPE_UNLINK] = "unlink", |
[CCS_TYPE_UNLINK] = "unlink", |
214 |
|
#ifdef CONFIG_CCSECURITY_FILE_GETATTR |
215 |
[CCS_TYPE_GETATTR] = "getattr", |
[CCS_TYPE_GETATTR] = "getattr", |
216 |
|
#endif |
217 |
[CCS_TYPE_RMDIR] = "rmdir", |
[CCS_TYPE_RMDIR] = "rmdir", |
218 |
[CCS_TYPE_TRUNCATE] = "truncate", |
[CCS_TYPE_TRUNCATE] = "truncate", |
219 |
[CCS_TYPE_SYMLINK] = "symlink", |
[CCS_TYPE_SYMLINK] = "symlink", |
221 |
[CCS_TYPE_UMOUNT] = "unmount", |
[CCS_TYPE_UMOUNT] = "unmount", |
222 |
}; |
}; |
223 |
|
|
224 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
225 |
/* String table for socket's operation. */ |
/* String table for socket's operation. */ |
226 |
const char * const ccs_socket_keyword[CCS_MAX_NETWORK_OPERATION] = { |
const char * const ccs_socket_keyword[CCS_MAX_NETWORK_OPERATION] = { |
227 |
[CCS_NETWORK_BIND] = "bind", |
[CCS_NETWORK_BIND] = "bind", |
229 |
[CCS_NETWORK_CONNECT] = "connect", |
[CCS_NETWORK_CONNECT] = "connect", |
230 |
[CCS_NETWORK_ACCEPT] = "accept", |
[CCS_NETWORK_ACCEPT] = "accept", |
231 |
[CCS_NETWORK_SEND] = "send", |
[CCS_NETWORK_SEND] = "send", |
232 |
|
#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG |
233 |
[CCS_NETWORK_RECV] = "recv", |
[CCS_NETWORK_RECV] = "recv", |
234 |
|
#endif |
235 |
}; |
}; |
236 |
|
#endif |
237 |
|
|
238 |
/* String table for categories. */ |
/* String table for categories. */ |
239 |
static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = { |
static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = { |
240 |
[CCS_MAC_CATEGORY_FILE] = "file", |
[CCS_MAC_CATEGORY_FILE] = "file", |
241 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
242 |
[CCS_MAC_CATEGORY_NETWORK] = "network", |
[CCS_MAC_CATEGORY_NETWORK] = "network", |
243 |
|
#endif |
244 |
|
#ifdef CONFIG_CCSECURITY_MISC |
245 |
[CCS_MAC_CATEGORY_MISC] = "misc", |
[CCS_MAC_CATEGORY_MISC] = "misc", |
246 |
|
#endif |
247 |
|
#ifdef CONFIG_CCSECURITY_IPC |
248 |
[CCS_MAC_CATEGORY_IPC] = "ipc", |
[CCS_MAC_CATEGORY_IPC] = "ipc", |
249 |
|
#endif |
250 |
|
#ifdef CONFIG_CCSECURITY_CAPABILITY |
251 |
[CCS_MAC_CATEGORY_CAPABILITY] = "capability", |
[CCS_MAC_CATEGORY_CAPABILITY] = "capability", |
252 |
|
#endif |
253 |
}; |
}; |
254 |
|
|
255 |
/* String table for conditions. */ |
/* String table for conditions. */ |
256 |
const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = { |
static const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = { |
257 |
[CCS_TASK_UID] = "task.uid", |
[CCS_TASK_UID] = "task.uid", |
258 |
[CCS_TASK_EUID] = "task.euid", |
[CCS_TASK_EUID] = "task.euid", |
259 |
[CCS_TASK_SUID] = "task.suid", |
[CCS_TASK_SUID] = "task.suid", |
324 |
[CCS_PREF_ENFORCING_PENALTY] = "enforcing_penalty", |
[CCS_PREF_ENFORCING_PENALTY] = "enforcing_penalty", |
325 |
}; |
}; |
326 |
|
|
327 |
|
/* String table for domain flags. */ |
328 |
|
const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = { |
329 |
|
[CCS_DIF_QUOTA_WARNED] = "quota_exceeded\n", |
330 |
|
[CCS_DIF_TRANSITION_FAILED] = "transition_failed\n", |
331 |
|
}; |
332 |
|
|
333 |
|
/* String table for domain transition control keywords. */ |
334 |
|
static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = { |
335 |
|
[CCS_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ", |
336 |
|
[CCS_TRANSITION_CONTROL_RESET] = "reset_domain ", |
337 |
|
[CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ", |
338 |
|
[CCS_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ", |
339 |
|
[CCS_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ", |
340 |
|
[CCS_TRANSITION_CONTROL_KEEP] = "keep_domain ", |
341 |
|
}; |
342 |
|
|
343 |
|
/* String table for grouping keywords. */ |
344 |
|
static const char * const ccs_group_name[CCS_MAX_GROUP] = { |
345 |
|
[CCS_PATH_GROUP] = "path_group ", |
346 |
|
[CCS_NUMBER_GROUP] = "number_group ", |
347 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
348 |
|
[CCS_ADDRESS_GROUP] = "address_group ", |
349 |
|
#endif |
350 |
|
}; |
351 |
|
|
352 |
|
/* String table for /proc/ccs/stat interface. */ |
353 |
|
static const char * const ccs_policy_headers[CCS_MAX_POLICY_STAT] = { |
354 |
|
[CCS_STAT_POLICY_UPDATES] = "update:", |
355 |
|
[CCS_STAT_POLICY_LEARNING] = "violation in learning mode:", |
356 |
|
[CCS_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:", |
357 |
|
[CCS_STAT_POLICY_ENFORCING] = "violation in enforcing mode:", |
358 |
|
}; |
359 |
|
|
360 |
|
/* String table for /proc/ccs/stat interface. */ |
361 |
|
static const char * const ccs_memory_headers[CCS_MAX_MEMORY_STAT] = { |
362 |
|
[CCS_MEMORY_POLICY] = "policy:", |
363 |
|
[CCS_MEMORY_AUDIT] = "audit log:", |
364 |
|
[CCS_MEMORY_QUERY] = "query message:", |
365 |
|
}; |
366 |
|
|
367 |
|
/***** SECTION2: Structure definition *****/ |
368 |
|
|
369 |
|
struct iattr; |
370 |
|
|
371 |
|
/* Structure for query. */ |
372 |
|
struct ccs_query { |
373 |
|
struct list_head list; |
374 |
|
struct ccs_domain_info *domain; |
375 |
|
char *query; |
376 |
|
size_t query_len; |
377 |
|
unsigned int serial; |
378 |
|
u8 timer; |
379 |
|
u8 answer; |
380 |
|
u8 retry; |
381 |
|
}; |
382 |
|
|
383 |
|
/* Structure for audit log. */ |
384 |
|
struct ccs_log { |
385 |
|
struct list_head list; |
386 |
|
char *log; |
387 |
|
int size; |
388 |
|
}; |
389 |
|
|
390 |
|
/***** SECTION3: Prototype definition section *****/ |
391 |
|
|
392 |
|
int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...); |
393 |
|
u8 ccs_get_config(const u8 profile, const u8 index); |
394 |
|
void ccs_transition_failed(const char *domainname); |
395 |
|
void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...); |
396 |
|
|
397 |
|
static char *ccs_init_log(struct ccs_request_info *r, int len, const char *fmt, |
398 |
|
va_list args); |
399 |
|
static const char *ccs_yesno(const unsigned int value); |
400 |
|
static int ccs_poll_log(struct file *file, poll_table *wait); |
401 |
|
static void ccs_read_log(struct ccs_io_buffer *head); |
402 |
|
static void ccs_write_log2(struct ccs_request_info *r, int len, |
403 |
|
const char *fmt, va_list args); |
404 |
|
static bool ccs_get_audit(const u8 profile, const u8 index, |
405 |
|
const struct ccs_acl_info *matched_acl, |
406 |
|
const bool is_granted); |
407 |
|
static char *ccs_print_bprm(struct linux_binprm *bprm, |
408 |
|
struct ccs_page_dump *dump); |
409 |
|
static char *ccs_print_header(struct ccs_request_info *r); |
410 |
|
static char *ccs_read_token(struct ccs_acl_param *param); |
411 |
|
static void ccs_update_task_domain(struct ccs_request_info *r); |
412 |
|
|
413 |
|
static bool ccs_correct_domain(const unsigned char *domainname); |
414 |
|
static bool ccs_correct_path(const char *filename); |
415 |
|
static bool ccs_correct_word(const char *string); |
416 |
|
static bool ccs_correct_word2(const char *string, size_t len); |
417 |
|
static bool ccs_domain_def(const unsigned char *buffer); |
418 |
|
static bool ccs_domain_quota_ok(struct ccs_request_info *r); |
419 |
|
static bool ccs_flush(struct ccs_io_buffer *head); |
420 |
|
static bool ccs_has_more_namespace(struct ccs_io_buffer *head); |
421 |
|
static bool ccs_manager(void); |
422 |
|
static bool ccs_namespace_jump(const char *domainname); |
423 |
|
static bool ccs_parse_argv(char *left, char *right, struct ccs_argv *argv); |
424 |
|
static bool ccs_parse_envp(char *left, char *right, struct ccs_envp *envp); |
425 |
|
static bool ccs_parse_name_union(struct ccs_acl_param *param, |
426 |
|
struct ccs_name_union *ptr); |
427 |
|
static bool ccs_parse_name_union_quoted(struct ccs_acl_param *param, |
428 |
|
struct ccs_name_union *ptr); |
429 |
|
static bool ccs_parse_number_union(struct ccs_acl_param *param, |
430 |
|
struct ccs_number_union *ptr); |
431 |
|
static bool ccs_permstr(const char *string, const char *keyword); |
432 |
|
static bool ccs_print_condition(struct ccs_io_buffer *head, |
433 |
|
const struct ccs_condition *cond); |
434 |
|
static bool ccs_print_entry(struct ccs_io_buffer *head, |
435 |
|
const struct ccs_acl_info *acl); |
436 |
|
static bool ccs_read_domain2(struct ccs_io_buffer *head, |
437 |
|
struct list_head *list); |
438 |
|
static bool ccs_read_group(struct ccs_io_buffer *head, const int idx); |
439 |
|
static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx); |
440 |
|
static bool ccs_same_condition(const struct ccs_condition *a, |
441 |
|
const struct ccs_condition *b); |
442 |
|
static bool ccs_select_domain(struct ccs_io_buffer *head, const char *data); |
443 |
|
static bool ccs_set_lf(struct ccs_io_buffer *head); |
444 |
|
static bool ccs_str_starts(char **src, const char *find); |
445 |
|
static char *ccs_get_transit_preference(struct ccs_acl_param *param, |
446 |
|
struct ccs_condition *e); |
447 |
|
static const struct ccs_path_info *ccs_get_domainname |
448 |
|
(struct ccs_acl_param *param); |
449 |
|
static const struct ccs_path_info *ccs_get_dqword(char *start); |
450 |
|
static int __init ccs_init_module(void); |
451 |
|
static int ccs_delete_domain(char *domainname); |
452 |
|
static int ccs_open(struct inode *inode, struct file *file); |
453 |
|
static int ccs_parse_policy(struct ccs_io_buffer *head, char *line); |
454 |
|
static int ccs_poll_query(struct file *file, poll_table *wait); |
455 |
|
static int ccs_release(struct inode *inode, struct file *file); |
456 |
|
static int ccs_set_mode(char *name, const char *value, |
457 |
|
struct ccs_profile *profile); |
458 |
|
static int ccs_truncate(char *str); |
459 |
|
static int ccs_update_domain(const int size, struct ccs_acl_param *param); |
460 |
|
static int ccs_update_manager_entry(const char *manager, const bool is_delete); |
461 |
|
static int ccs_update_policy(const int size, struct ccs_acl_param *param); |
462 |
|
static int ccs_write_aggregator(struct ccs_acl_param *param); |
463 |
|
static int ccs_write_answer(struct ccs_io_buffer *head); |
464 |
|
static int ccs_write_domain(struct ccs_io_buffer *head); |
465 |
|
static int ccs_write_domain2(struct ccs_policy_namespace *ns, |
466 |
|
struct list_head *list, char *data, |
467 |
|
const bool is_delete); |
468 |
|
static int ccs_write_exception(struct ccs_io_buffer *head); |
469 |
|
static int ccs_write_file(struct ccs_acl_param *param); |
470 |
|
static int ccs_write_group(struct ccs_acl_param *param, const u8 type); |
471 |
|
static int ccs_write_manager(struct ccs_io_buffer *head); |
472 |
|
static int ccs_write_pid(struct ccs_io_buffer *head); |
473 |
|
static int ccs_write_profile(struct ccs_io_buffer *head); |
474 |
|
static int ccs_write_stat(struct ccs_io_buffer *head); |
475 |
|
static int ccs_write_task(struct ccs_acl_param *param); |
476 |
|
static int ccs_write_transition_control(struct ccs_acl_param *param, |
477 |
|
const u8 type); |
478 |
|
static s8 ccs_find_yesno(const char *string, const char *find); |
479 |
|
static ssize_t ccs_read(struct file *file, char __user *buf, size_t count, |
480 |
|
loff_t *ppos); |
481 |
|
static ssize_t ccs_read_self(struct file *file, char __user *buf, size_t count, |
482 |
|
loff_t *ppos); |
483 |
|
static ssize_t ccs_write(struct file *file, const char __user *buf, |
484 |
|
size_t count, loff_t *ppos); |
485 |
|
static struct ccs_condition *ccs_commit_condition(struct ccs_condition *entry); |
486 |
|
static struct ccs_condition *ccs_get_condition(struct ccs_acl_param *param); |
487 |
|
static struct ccs_domain_info *ccs_find_domain(const char *domainname); |
488 |
|
static struct ccs_domain_info *ccs_find_domain_by_qid(unsigned int serial); |
489 |
|
static struct ccs_group *ccs_get_group(struct ccs_acl_param *param, |
490 |
|
const u8 idx); |
491 |
|
static struct ccs_policy_namespace *ccs_assign_namespace |
492 |
|
(const char *domainname); |
493 |
|
static struct ccs_policy_namespace *ccs_find_namespace(const char *name, |
494 |
|
const unsigned int len); |
495 |
|
static struct ccs_profile *ccs_assign_profile(struct ccs_policy_namespace *ns, |
496 |
|
const unsigned int profile); |
497 |
|
static struct ccs_profile *ccs_profile(const u8 profile); |
498 |
|
static u8 ccs_condition_type(const char *word); |
499 |
|
static u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3); |
500 |
|
static u8 ccs_parse_ulong(unsigned long *result, char **str); |
501 |
|
static unsigned int ccs_poll(struct file *file, poll_table *wait); |
502 |
|
static void __init ccs_create_entry(const char *name, const mode_t mode, |
503 |
|
struct proc_dir_entry *parent, |
504 |
|
const u8 key); |
505 |
|
static void __init ccs_load_builtin_policy(void); |
506 |
|
static void __init ccs_policy_io_init(void); |
507 |
|
static void __init ccs_proc_init(void); |
508 |
|
static void ccs_add_entry(char *header); |
509 |
|
static void ccs_addprintf(char *buffer, int len, const char *fmt, ...) |
510 |
|
__printf(3, 4); |
511 |
|
static void ccs_addprintf(char *buffer, int len, const char *fmt, ...); |
512 |
|
static void ccs_check_profile(void); |
513 |
|
static void ccs_convert_time(time_t time, struct ccs_time *stamp); |
514 |
|
static void ccs_init_policy_namespace(struct ccs_policy_namespace *ns); |
515 |
|
static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...) |
516 |
|
__printf(2, 3); |
517 |
|
static void ccs_normalize_line(unsigned char *buffer); |
518 |
|
static void ccs_print_config(struct ccs_io_buffer *head, const u8 config); |
519 |
|
static void ccs_print_name_union(struct ccs_io_buffer *head, |
520 |
|
const struct ccs_name_union *ptr); |
521 |
|
static void ccs_print_name_union_quoted(struct ccs_io_buffer *head, |
522 |
|
const struct ccs_name_union *ptr); |
523 |
|
static void ccs_print_namespace(struct ccs_io_buffer *head); |
524 |
|
static void ccs_print_number_union(struct ccs_io_buffer *head, |
525 |
|
const struct ccs_number_union *ptr); |
526 |
|
static void ccs_print_number_union_nospace(struct ccs_io_buffer *head, |
527 |
|
const struct ccs_number_union *ptr); |
528 |
|
static void ccs_read_domain(struct ccs_io_buffer *head); |
529 |
|
static void ccs_read_exception(struct ccs_io_buffer *head); |
530 |
|
static void ccs_read_manager(struct ccs_io_buffer *head); |
531 |
|
static void ccs_read_pid(struct ccs_io_buffer *head); |
532 |
|
static void ccs_read_profile(struct ccs_io_buffer *head); |
533 |
|
static void ccs_read_query(struct ccs_io_buffer *head); |
534 |
|
static void ccs_read_stat(struct ccs_io_buffer *head); |
535 |
|
static void ccs_read_version(struct ccs_io_buffer *head); |
536 |
|
static void ccs_set_group(struct ccs_io_buffer *head, const char *category); |
537 |
|
static void ccs_set_namespace_cursor(struct ccs_io_buffer *head); |
538 |
|
static void ccs_set_slash(struct ccs_io_buffer *head); |
539 |
|
static void ccs_set_space(struct ccs_io_buffer *head); |
540 |
|
static void ccs_set_string(struct ccs_io_buffer *head, const char *string); |
541 |
|
static void ccs_set_uint(unsigned int *i, const char *string, |
542 |
|
const char *find); |
543 |
|
static void ccs_update_stat(const u8 index); |
544 |
|
|
545 |
|
#ifdef CONFIG_CCSECURITY_PORTRESERVE |
546 |
|
static bool __ccs_lport_reserved(const u16 port); |
547 |
|
static int ccs_write_reserved_port(struct ccs_acl_param *param); |
548 |
|
#endif |
549 |
|
|
550 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
551 |
|
static bool ccs_parse_ipaddr_union(struct ccs_acl_param *param, |
552 |
|
struct ccs_ipaddr_union *ptr); |
553 |
|
static int ccs_write_inet_network(struct ccs_acl_param *param); |
554 |
|
static int ccs_write_unix_network(struct ccs_acl_param *param); |
555 |
|
static void ccs_print_ip(char *buf, const unsigned int size, |
556 |
|
const struct ccs_ipaddr_union *ptr); |
557 |
|
#endif |
558 |
|
|
559 |
|
#ifdef CONFIG_CCSECURITY_CAPABILITY |
560 |
|
static int ccs_write_capability(struct ccs_acl_param *param); |
561 |
|
#endif |
562 |
|
|
563 |
|
#ifdef CONFIG_CCSECURITY_MISC |
564 |
|
static int ccs_write_misc(struct ccs_acl_param *param); |
565 |
|
#endif |
566 |
|
|
567 |
|
#ifdef CONFIG_CCSECURITY_IPC |
568 |
|
static int ccs_write_ipc(struct ccs_acl_param *param); |
569 |
|
#endif |
570 |
|
|
571 |
|
#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION |
572 |
|
static ssize_t ccs_write_self(struct file *file, const char __user *buf, |
573 |
|
size_t count, loff_t *ppos); |
574 |
|
#endif |
575 |
|
|
576 |
|
/***** SECTION4: Standalone functions section *****/ |
577 |
|
|
578 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) |
579 |
|
|
580 |
|
/** |
581 |
|
* fatal_signal_pending - Check whether SIGKILL is pending or not. |
582 |
|
* |
583 |
|
* @p: Pointer to "struct task_struct". |
584 |
|
* |
585 |
|
* Returns true if SIGKILL is pending on @p, false otherwise. |
586 |
|
* |
587 |
|
* This is for compatibility with older kernels. |
588 |
|
*/ |
589 |
|
#define fatal_signal_pending(p) (signal_pending(p) && \ |
590 |
|
sigismember(&p->pending.signal, SIGKILL)) |
591 |
|
|
592 |
|
#endif |
593 |
|
|
594 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
595 |
|
|
596 |
|
/** |
597 |
|
* __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses. |
598 |
|
* |
599 |
|
* @wq: The waitqueue to wait on. |
600 |
|
* @condition: A C expression for the event to wait for. |
601 |
|
* @ret: Timeout, in jiffies. |
602 |
|
* |
603 |
|
* Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a |
604 |
|
* signal, and the remaining jiffies otherwise if the condition evaluated to |
605 |
|
* true before the timeout elapsed. |
606 |
|
* |
607 |
|
* This is for compatibility with older kernels. |
608 |
|
*/ |
609 |
|
#define __wait_event_interruptible_timeout(wq, condition, ret) \ |
610 |
|
do { \ |
611 |
|
wait_queue_t __wait; \ |
612 |
|
init_waitqueue_entry(&__wait, current); \ |
613 |
|
\ |
614 |
|
add_wait_queue(&wq, &__wait); \ |
615 |
|
for (;;) { \ |
616 |
|
set_current_state(TASK_INTERRUPTIBLE); \ |
617 |
|
if (condition) \ |
618 |
|
break; \ |
619 |
|
if (!signal_pending(current)) { \ |
620 |
|
ret = schedule_timeout(ret); \ |
621 |
|
if (!ret) \ |
622 |
|
break; \ |
623 |
|
continue; \ |
624 |
|
} \ |
625 |
|
ret = -ERESTARTSYS; \ |
626 |
|
break; \ |
627 |
|
} \ |
628 |
|
current->state = TASK_RUNNING; \ |
629 |
|
remove_wait_queue(&wq, &__wait); \ |
630 |
|
} while (0) |
631 |
|
|
632 |
|
/** |
633 |
|
* wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses. |
634 |
|
* |
635 |
|
* @wq: The waitqueue to wait on. |
636 |
|
* @condition: A C expression for the event to wait for. |
637 |
|
* @timeout: Timeout, in jiffies. |
638 |
|
* |
639 |
|
* Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a |
640 |
|
* signal, and the remaining jiffies otherwise if the condition evaluated to |
641 |
|
* true before the timeout elapsed. |
642 |
|
* |
643 |
|
* This is for compatibility with older kernels. |
644 |
|
*/ |
645 |
|
#define wait_event_interruptible_timeout(wq, condition, timeout) \ |
646 |
|
({ \ |
647 |
|
long __ret = timeout; \ |
648 |
|
if (!(condition)) \ |
649 |
|
__wait_event_interruptible_timeout(wq, condition, __ret); \ |
650 |
|
__ret; \ |
651 |
|
}) |
652 |
|
|
653 |
|
#endif |
654 |
|
|
655 |
|
/** |
656 |
|
* ccs_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss. |
657 |
|
* |
658 |
|
* @time: Seconds since 1970/01/01 00:00:00. |
659 |
|
* @stamp: Pointer to "struct ccs_time". |
660 |
|
* |
661 |
|
* Returns nothing. |
662 |
|
* |
663 |
|
* This function does not handle Y2038 problem. |
664 |
|
*/ |
665 |
|
static void ccs_convert_time(time_t time, struct ccs_time *stamp) |
666 |
|
{ |
667 |
|
static const u16 ccs_eom[2][12] = { |
668 |
|
{ 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, |
669 |
|
{ 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } |
670 |
|
}; |
671 |
|
u16 y; |
672 |
|
u8 m; |
673 |
|
bool r; |
674 |
|
stamp->sec = time % 60; |
675 |
|
time /= 60; |
676 |
|
stamp->min = time % 60; |
677 |
|
time /= 60; |
678 |
|
stamp->hour = time % 24; |
679 |
|
time /= 24; |
680 |
|
for (y = 1970; ; y++) { |
681 |
|
const unsigned short days = (y & 3) ? 365 : 366; |
682 |
|
if (time < days) |
683 |
|
break; |
684 |
|
time -= days; |
685 |
|
} |
686 |
|
r = (y & 3) == 0; |
687 |
|
for (m = 0; m < 11 && time >= ccs_eom[r][m]; m++); |
688 |
|
if (m) |
689 |
|
time -= ccs_eom[r][m - 1]; |
690 |
|
stamp->year = y; |
691 |
|
stamp->month = ++m; |
692 |
|
stamp->day = ++time; |
693 |
|
} |
694 |
|
|
695 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 23) |
696 |
|
#if !defined(RHEL_VERSION) || RHEL_VERSION != 3 |
697 |
|
|
698 |
|
/** |
699 |
|
* PDE - Get "struct proc_dir_entry". |
700 |
|
* |
701 |
|
* @inode: Pointer to "struct inode". |
702 |
|
* |
703 |
|
* Returns pointer to "struct proc_dir_entry". |
704 |
|
* |
705 |
|
* This is for compatibility with older kernels. |
706 |
|
*/ |
707 |
|
static inline struct proc_dir_entry *PDE(const struct inode *inode) |
708 |
|
{ |
709 |
|
return (struct proc_dir_entry *) inode->u.generic_ip; |
710 |
|
} |
711 |
|
|
712 |
|
#endif |
713 |
|
#endif |
714 |
|
|
715 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
716 |
|
|
717 |
|
/** |
718 |
|
* proc_notify_change - Update inode's attributes and reflect to the dentry. |
719 |
|
* |
720 |
|
* @dentry: Pointer to "struct dentry". |
721 |
|
* @iattr: Pointer to "struct iattr". |
722 |
|
* |
723 |
|
* Returns 0 on success, negative value otherwise. |
724 |
|
* |
725 |
|
* The 2.4 kernels don't allow chmod()/chown() for files in /proc, |
726 |
|
* while the 2.6 kernels allow. |
727 |
|
* To permit management of /proc/ccs/ interface by non-root user, |
728 |
|
* I modified to allow chmod()/chown() of /proc/ccs/ interface like 2.6 kernels |
729 |
|
* by adding "struct inode_operations"->setattr hook. |
730 |
|
*/ |
731 |
|
static int proc_notify_change(struct dentry *dentry, struct iattr *iattr) |
732 |
|
{ |
733 |
|
struct inode *inode = dentry->d_inode; |
734 |
|
struct proc_dir_entry *de = PDE(inode); |
735 |
|
int error; |
736 |
|
|
737 |
|
error = inode_change_ok(inode, iattr); |
738 |
|
if (error) |
739 |
|
goto out; |
740 |
|
|
741 |
|
error = inode_setattr(inode, iattr); |
742 |
|
if (error) |
743 |
|
goto out; |
744 |
|
|
745 |
|
de->uid = inode->i_uid; |
746 |
|
de->gid = inode->i_gid; |
747 |
|
de->mode = inode->i_mode; |
748 |
|
out: |
749 |
|
return error; |
750 |
|
} |
751 |
|
|
752 |
|
#endif |
753 |
|
|
754 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
755 |
|
|
756 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) && defined(CONFIG_NET) |
757 |
|
#define ccs_in4_pton in4_pton |
758 |
|
#define ccs_in6_pton in6_pton |
759 |
|
#else |
760 |
|
/* |
761 |
|
* Routines for parsing IPv4 or IPv6 address. |
762 |
|
* These are copied from lib/hexdump.c net/core/utils.c . |
763 |
|
*/ |
764 |
|
#include <linux/ctype.h> |
765 |
|
|
766 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35) |
767 |
|
static int hex_to_bin(char ch) |
768 |
|
{ |
769 |
|
if ((ch >= '0') && (ch <= '9')) |
770 |
|
return ch - '0'; |
771 |
|
ch = tolower(ch); |
772 |
|
if ((ch >= 'a') && (ch <= 'f')) |
773 |
|
return ch - 'a' + 10; |
774 |
|
return -1; |
775 |
|
} |
776 |
|
#endif |
777 |
|
|
778 |
|
#define IN6PTON_XDIGIT 0x00010000 |
779 |
|
#define IN6PTON_DIGIT 0x00020000 |
780 |
|
#define IN6PTON_COLON_MASK 0x00700000 |
781 |
|
#define IN6PTON_COLON_1 0x00100000 /* single : requested */ |
782 |
|
#define IN6PTON_COLON_2 0x00200000 /* second : requested */ |
783 |
|
#define IN6PTON_COLON_1_2 0x00400000 /* :: requested */ |
784 |
|
#define IN6PTON_DOT 0x00800000 /* . */ |
785 |
|
#define IN6PTON_DELIM 0x10000000 |
786 |
|
#define IN6PTON_NULL 0x20000000 /* first/tail */ |
787 |
|
#define IN6PTON_UNKNOWN 0x40000000 |
788 |
|
|
789 |
|
static inline int xdigit2bin(char c, int delim) |
790 |
|
{ |
791 |
|
int val; |
792 |
|
|
793 |
|
if (c == delim || c == '\0') |
794 |
|
return IN6PTON_DELIM; |
795 |
|
if (c == ':') |
796 |
|
return IN6PTON_COLON_MASK; |
797 |
|
if (c == '.') |
798 |
|
return IN6PTON_DOT; |
799 |
|
|
800 |
|
val = hex_to_bin(c); |
801 |
|
if (val >= 0) |
802 |
|
return val | IN6PTON_XDIGIT | (val < 10 ? IN6PTON_DIGIT : 0); |
803 |
|
|
804 |
|
if (delim == -1) |
805 |
|
return IN6PTON_DELIM; |
806 |
|
return IN6PTON_UNKNOWN; |
807 |
|
} |
808 |
|
|
809 |
|
static int ccs_in4_pton(const char *src, int srclen, u8 *dst, int delim, |
810 |
|
const char **end) |
811 |
|
{ |
812 |
|
const char *s; |
813 |
|
u8 *d; |
814 |
|
u8 dbuf[4]; |
815 |
|
int ret = 0; |
816 |
|
int i; |
817 |
|
int w = 0; |
818 |
|
|
819 |
|
if (srclen < 0) |
820 |
|
srclen = strlen(src); |
821 |
|
s = src; |
822 |
|
d = dbuf; |
823 |
|
i = 0; |
824 |
|
while (1) { |
825 |
|
int c; |
826 |
|
c = xdigit2bin(srclen > 0 ? *s : '\0', delim); |
827 |
|
if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM | |
828 |
|
IN6PTON_COLON_MASK))) |
829 |
|
goto out; |
830 |
|
if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) { |
831 |
|
if (w == 0) |
832 |
|
goto out; |
833 |
|
*d++ = w & 0xff; |
834 |
|
w = 0; |
835 |
|
i++; |
836 |
|
if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) { |
837 |
|
if (i != 4) |
838 |
|
goto out; |
839 |
|
break; |
840 |
|
} |
841 |
|
goto cont; |
842 |
|
} |
843 |
|
w = (w * 10) + c; |
844 |
|
if ((w & 0xffff) > 255) |
845 |
|
goto out; |
846 |
|
cont: |
847 |
|
if (i >= 4) |
848 |
|
goto out; |
849 |
|
s++; |
850 |
|
srclen--; |
851 |
|
} |
852 |
|
ret = 1; |
853 |
|
memcpy(dst, dbuf, sizeof(dbuf)); |
854 |
|
out: |
855 |
|
if (end) |
856 |
|
*end = s; |
857 |
|
return ret; |
858 |
|
} |
859 |
|
|
860 |
|
static int ccs_in6_pton(const char *src, int srclen, u8 *dst, int delim, |
861 |
|
const char **end) |
862 |
|
{ |
863 |
|
const char *s, *tok = NULL; |
864 |
|
u8 *d, *dc = NULL; |
865 |
|
u8 dbuf[16]; |
866 |
|
int ret = 0; |
867 |
|
int i; |
868 |
|
int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL; |
869 |
|
int w = 0; |
870 |
|
|
871 |
|
memset(dbuf, 0, sizeof(dbuf)); |
872 |
|
|
873 |
|
s = src; |
874 |
|
d = dbuf; |
875 |
|
if (srclen < 0) |
876 |
|
srclen = strlen(src); |
877 |
|
|
878 |
|
while (1) { |
879 |
|
int c; |
880 |
|
|
881 |
|
c = xdigit2bin(srclen > 0 ? *s : '\0', delim); |
882 |
|
if (!(c & state)) |
883 |
|
goto out; |
884 |
|
if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) { |
885 |
|
/* process one 16-bit word */ |
886 |
|
if (!(state & IN6PTON_NULL)) { |
887 |
|
*d++ = (w >> 8) & 0xff; |
888 |
|
*d++ = w & 0xff; |
889 |
|
} |
890 |
|
w = 0; |
891 |
|
if (c & IN6PTON_DELIM) { |
892 |
|
/* We've processed last word */ |
893 |
|
break; |
894 |
|
} |
895 |
|
/* |
896 |
|
* COLON_1 => XDIGIT |
897 |
|
* COLON_2 => XDIGIT|DELIM |
898 |
|
* COLON_1_2 => COLON_2 |
899 |
|
*/ |
900 |
|
switch (state & IN6PTON_COLON_MASK) { |
901 |
|
case IN6PTON_COLON_2: |
902 |
|
dc = d; |
903 |
|
state = IN6PTON_XDIGIT | IN6PTON_DELIM; |
904 |
|
if (dc - dbuf >= sizeof(dbuf)) |
905 |
|
state |= IN6PTON_NULL; |
906 |
|
break; |
907 |
|
case IN6PTON_COLON_1|IN6PTON_COLON_1_2: |
908 |
|
state = IN6PTON_XDIGIT | IN6PTON_COLON_2; |
909 |
|
break; |
910 |
|
case IN6PTON_COLON_1: |
911 |
|
state = IN6PTON_XDIGIT; |
912 |
|
break; |
913 |
|
case IN6PTON_COLON_1_2: |
914 |
|
state = IN6PTON_COLON_2; |
915 |
|
break; |
916 |
|
default: |
917 |
|
state = 0; |
918 |
|
} |
919 |
|
tok = s + 1; |
920 |
|
goto cont; |
921 |
|
} |
922 |
|
|
923 |
|
if (c & IN6PTON_DOT) { |
924 |
|
ret = ccs_in4_pton(tok ? tok : s, srclen + |
925 |
|
(int)(s - tok), d, delim, &s); |
926 |
|
if (ret > 0) { |
927 |
|
d += 4; |
928 |
|
break; |
929 |
|
} |
930 |
|
goto out; |
931 |
|
} |
932 |
|
|
933 |
|
w = (w << 4) | (0xff & c); |
934 |
|
state = IN6PTON_COLON_1 | IN6PTON_DELIM; |
935 |
|
if (!(w & 0xf000)) |
936 |
|
state |= IN6PTON_XDIGIT; |
937 |
|
if (!dc && d + 2 < dbuf + sizeof(dbuf)) { |
938 |
|
state |= IN6PTON_COLON_1_2; |
939 |
|
state &= ~IN6PTON_DELIM; |
940 |
|
} |
941 |
|
if (d + 2 >= dbuf + sizeof(dbuf)) |
942 |
|
state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2); |
943 |
|
cont: |
944 |
|
if ((dc && d + 4 < dbuf + sizeof(dbuf)) || |
945 |
|
d + 4 == dbuf + sizeof(dbuf)) |
946 |
|
state |= IN6PTON_DOT; |
947 |
|
if (d >= dbuf + sizeof(dbuf)) |
948 |
|
state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK); |
949 |
|
s++; |
950 |
|
srclen--; |
951 |
|
} |
952 |
|
|
953 |
|
i = 15; d--; |
954 |
|
|
955 |
|
if (dc) { |
956 |
|
while (d >= dc) |
957 |
|
dst[i--] = *d--; |
958 |
|
while (i >= dc - dbuf) |
959 |
|
dst[i--] = 0; |
960 |
|
while (i >= 0) |
961 |
|
dst[i--] = *d--; |
962 |
|
} else |
963 |
|
memcpy(dst, dbuf, sizeof(dbuf)); |
964 |
|
|
965 |
|
ret = 1; |
966 |
|
out: |
967 |
|
if (end) |
968 |
|
*end = s; |
969 |
|
return ret; |
970 |
|
} |
971 |
|
#endif |
972 |
|
|
973 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32) |
974 |
|
|
975 |
|
/* |
976 |
|
* Routines for printing IPv4 or IPv6 address. |
977 |
|
* These are copied from include/linux/kernel.h include/net/ipv6.h |
978 |
|
* include/net/addrconf.h lib/hexdump.c lib/vsprintf.c and simplified. |
979 |
|
*/ |
980 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26) |
981 |
|
static const char hex_asc[] = "0123456789abcdef"; |
982 |
|
#define hex_asc_lo(x) hex_asc[((x) & 0x0f)] |
983 |
|
#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] |
984 |
|
|
985 |
|
static inline char *pack_hex_byte(char *buf, u8 byte) |
986 |
|
{ |
987 |
|
*buf++ = hex_asc_hi(byte); |
988 |
|
*buf++ = hex_asc_lo(byte); |
989 |
|
return buf; |
990 |
|
} |
991 |
|
#endif |
992 |
|
|
993 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) |
994 |
|
static inline int ipv6_addr_v4mapped(const struct in6_addr *a) |
995 |
|
{ |
996 |
|
return (a->s6_addr32[0] | a->s6_addr32[1] | |
997 |
|
(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0; |
998 |
|
} |
999 |
|
#endif |
1000 |
|
|
1001 |
|
static inline int ipv6_addr_is_isatap(const struct in6_addr *addr) |
1002 |
|
{ |
1003 |
|
return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE); |
1004 |
|
} |
1005 |
|
|
1006 |
|
static char *ip4_string(char *p, const u8 *addr) |
1007 |
|
{ |
1008 |
|
/* |
1009 |
|
* Since this function is called outside vsnprintf(), I can use |
1010 |
|
* sprintf() here. |
1011 |
|
*/ |
1012 |
|
return p + |
1013 |
|
sprintf(p, "%u.%u.%u.%u", addr[0], addr[1], addr[2], addr[3]); |
1014 |
|
} |
1015 |
|
|
1016 |
|
static char *ip6_compressed_string(char *p, const char *addr) |
1017 |
|
{ |
1018 |
|
int i, j, range; |
1019 |
|
unsigned char zerolength[8]; |
1020 |
|
int longest = 1; |
1021 |
|
int colonpos = -1; |
1022 |
|
u16 word; |
1023 |
|
u8 hi, lo; |
1024 |
|
bool needcolon = false; |
1025 |
|
bool useIPv4; |
1026 |
|
struct in6_addr in6; |
1027 |
|
|
1028 |
|
memcpy(&in6, addr, sizeof(struct in6_addr)); |
1029 |
|
|
1030 |
|
useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); |
1031 |
|
|
1032 |
|
memset(zerolength, 0, sizeof(zerolength)); |
1033 |
|
|
1034 |
|
if (useIPv4) |
1035 |
|
range = 6; |
1036 |
|
else |
1037 |
|
range = 8; |
1038 |
|
|
1039 |
|
/* find position of longest 0 run */ |
1040 |
|
for (i = 0; i < range; i++) { |
1041 |
|
for (j = i; j < range; j++) { |
1042 |
|
if (in6.s6_addr16[j] != 0) |
1043 |
|
break; |
1044 |
|
zerolength[i]++; |
1045 |
|
} |
1046 |
|
} |
1047 |
|
for (i = 0; i < range; i++) { |
1048 |
|
if (zerolength[i] > longest) { |
1049 |
|
longest = zerolength[i]; |
1050 |
|
colonpos = i; |
1051 |
|
} |
1052 |
|
} |
1053 |
|
if (longest == 1) /* don't compress a single 0 */ |
1054 |
|
colonpos = -1; |
1055 |
|
|
1056 |
|
/* emit address */ |
1057 |
|
for (i = 0; i < range; i++) { |
1058 |
|
if (i == colonpos) { |
1059 |
|
if (needcolon || i == 0) |
1060 |
|
*p++ = ':'; |
1061 |
|
*p++ = ':'; |
1062 |
|
needcolon = false; |
1063 |
|
i += longest - 1; |
1064 |
|
continue; |
1065 |
|
} |
1066 |
|
if (needcolon) { |
1067 |
|
*p++ = ':'; |
1068 |
|
needcolon = false; |
1069 |
|
} |
1070 |
|
/* hex u16 without leading 0s */ |
1071 |
|
word = ntohs(in6.s6_addr16[i]); |
1072 |
|
hi = word >> 8; |
1073 |
|
lo = word & 0xff; |
1074 |
|
if (hi) { |
1075 |
|
if (hi > 0x0f) |
1076 |
|
p = pack_hex_byte(p, hi); |
1077 |
|
else |
1078 |
|
*p++ = hex_asc_lo(hi); |
1079 |
|
p = pack_hex_byte(p, lo); |
1080 |
|
} else if (lo > 0x0f) |
1081 |
|
p = pack_hex_byte(p, lo); |
1082 |
|
else |
1083 |
|
*p++ = hex_asc_lo(lo); |
1084 |
|
needcolon = true; |
1085 |
|
} |
1086 |
|
|
1087 |
|
if (useIPv4) { |
1088 |
|
if (needcolon) |
1089 |
|
*p++ = ':'; |
1090 |
|
p = ip4_string(p, &in6.s6_addr[12]); |
1091 |
|
} |
1092 |
|
*p = '\0'; |
1093 |
|
|
1094 |
|
return p; |
1095 |
|
} |
1096 |
|
#endif |
1097 |
|
|
1098 |
|
/** |
1099 |
|
* ccs_print_ipv4 - Print an IPv4 address. |
1100 |
|
* |
1101 |
|
* @buffer: Buffer to write to. |
1102 |
|
* @buffer_len: Size of @buffer. |
1103 |
|
* @ip: Pointer to "u32 in network byte order". |
1104 |
|
* |
1105 |
|
* Returns written length. |
1106 |
|
*/ |
1107 |
|
int ccs_print_ipv4(char *buffer, const unsigned int buffer_len, const u32 *ip) |
1108 |
|
{ |
1109 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) |
1110 |
|
return snprintf(buffer, buffer_len, "%pI4", ip); |
1111 |
|
#else |
1112 |
|
char addr[sizeof("255.255.255.255")]; |
1113 |
|
ip4_string(addr, (const u8 *) ip); |
1114 |
|
return snprintf(buffer, buffer_len, "%s", addr); |
1115 |
|
#endif |
1116 |
|
} |
1117 |
|
|
1118 |
|
/** |
1119 |
|
* ccs_print_ipv6 - Print an IPv6 address. |
1120 |
|
* |
1121 |
|
* @buffer: Buffer to write to. |
1122 |
|
* @buffer_len: Size of @buffer. |
1123 |
|
* @ip: Pointer to "struct in6_addr". |
1124 |
|
* |
1125 |
|
* Returns written length. |
1126 |
|
*/ |
1127 |
|
int ccs_print_ipv6(char *buffer, const unsigned int buffer_len, |
1128 |
|
const struct in6_addr *ip) |
1129 |
|
{ |
1130 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) |
1131 |
|
return snprintf(buffer, buffer_len, "%pI6c", ip); |
1132 |
|
#else |
1133 |
|
char addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
1134 |
|
ip6_compressed_string(addr, (const u8 *) ip); |
1135 |
|
return snprintf(buffer, buffer_len, "%s", addr); |
1136 |
|
#endif |
1137 |
|
} |
1138 |
|
|
1139 |
|
#endif |
1140 |
|
|
1141 |
|
/***** SECTION5: Variables definition section *****/ |
1142 |
|
|
1143 |
/* Permit policy management by non-root user? */ |
/* Permit policy management by non-root user? */ |
1144 |
static bool ccs_manage_by_non_root; |
static bool ccs_manage_by_non_root; |
1145 |
|
|
1146 |
|
/* Lock for protecting policy. */ |
1147 |
|
DEFINE_MUTEX(ccs_policy_lock); |
1148 |
|
|
1149 |
|
/* Has /sbin/init started? */ |
1150 |
|
bool ccs_policy_loaded; |
1151 |
|
|
1152 |
|
/* List of namespaces. */ |
1153 |
|
LIST_HEAD(ccs_namespace_list); |
1154 |
|
/* True if namespace other than ccs_kernel_namespace is defined. */ |
1155 |
|
static bool ccs_namespace_enabled; |
1156 |
|
|
1157 |
|
/* Initial namespace.*/ |
1158 |
|
static struct ccs_policy_namespace ccs_kernel_namespace; |
1159 |
|
|
1160 |
|
/* List of "struct ccs_condition". */ |
1161 |
|
LIST_HEAD(ccs_condition_list); |
1162 |
|
|
1163 |
|
#ifdef CONFIG_CCSECURITY_PORTRESERVE |
1164 |
|
/* Bitmap for reserved local port numbers.*/ |
1165 |
|
static u8 ccs_reserved_port_map[8192]; |
1166 |
|
#endif |
1167 |
|
|
1168 |
|
/* Wait queue for kernel -> userspace notification. */ |
1169 |
|
static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait); |
1170 |
|
/* Wait queue for userspace -> kernel notification. */ |
1171 |
|
static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait); |
1172 |
|
|
1173 |
|
/* The list for "struct ccs_query". */ |
1174 |
|
static LIST_HEAD(ccs_query_list); |
1175 |
|
|
1176 |
|
/* Lock for manipulating ccs_query_list. */ |
1177 |
|
static DEFINE_SPINLOCK(ccs_query_list_lock); |
1178 |
|
|
1179 |
|
/* Number of "struct file" referring /proc/ccs/query interface. */ |
1180 |
|
static atomic_t ccs_query_observers = ATOMIC_INIT(0); |
1181 |
|
|
1182 |
|
/* Wait queue for /proc/ccs/audit. */ |
1183 |
|
static DECLARE_WAIT_QUEUE_HEAD(ccs_log_wait); |
1184 |
|
|
1185 |
|
/* The list for "struct ccs_log". */ |
1186 |
|
static LIST_HEAD(ccs_log); |
1187 |
|
|
1188 |
|
/* Lock for "struct list_head ccs_log". */ |
1189 |
|
static DEFINE_SPINLOCK(ccs_log_lock); |
1190 |
|
|
1191 |
|
/* Length of "stuct list_head ccs_log". */ |
1192 |
|
static unsigned int ccs_log_count; |
1193 |
|
|
1194 |
|
/* Timestamp counter for last updated. */ |
1195 |
|
static unsigned int ccs_stat_updated[CCS_MAX_POLICY_STAT]; |
1196 |
|
|
1197 |
|
/* Counter for number of updates. */ |
1198 |
|
static unsigned int ccs_stat_modified[CCS_MAX_POLICY_STAT]; |
1199 |
|
|
1200 |
|
/* Operations for /proc/ccs/self_domain interface. */ |
1201 |
|
static |
1202 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17) |
1203 |
|
const |
1204 |
|
#endif |
1205 |
|
struct file_operations ccs_self_operations = { |
1206 |
|
#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION |
1207 |
|
.write = ccs_write_self, |
1208 |
|
#endif |
1209 |
|
.read = ccs_read_self, |
1210 |
|
}; |
1211 |
|
|
1212 |
|
/* Operations for /proc/ccs/ interface. */ |
1213 |
|
static |
1214 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17) |
1215 |
|
const |
1216 |
|
#endif |
1217 |
|
struct file_operations ccs_operations = { |
1218 |
|
.open = ccs_open, |
1219 |
|
.release = ccs_release, |
1220 |
|
.poll = ccs_poll, |
1221 |
|
.read = ccs_read, |
1222 |
|
.write = ccs_write, |
1223 |
|
}; |
1224 |
|
|
1225 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
1226 |
|
|
1227 |
|
/* The inode operations for /proc/ccs/ directory. */ |
1228 |
|
static struct inode_operations ccs_dir_inode_operations; |
1229 |
|
|
1230 |
|
/* The inode operations for files under /proc/ccs/ directory. */ |
1231 |
|
static struct inode_operations ccs_file_inode_operations; |
1232 |
|
|
1233 |
|
#endif |
1234 |
|
|
1235 |
|
/***** SECTION6: Dependent functions section *****/ |
1236 |
|
|
1237 |
|
/** |
1238 |
|
* list_for_each_cookie - iterate over a list with cookie. |
1239 |
|
* |
1240 |
|
* @pos: Pointer to "struct list_head". |
1241 |
|
* @head: Pointer to "struct list_head". |
1242 |
|
*/ |
1243 |
|
#define list_for_each_cookie(pos, head) \ |
1244 |
|
for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \ |
1245 |
|
pos != (head); pos = srcu_dereference(pos->next, &ccs_ss)) |
1246 |
|
|
1247 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
1248 |
|
|
1249 |
|
/** |
1250 |
|
* ccs_print_ip - Print an IP address. |
1251 |
|
* |
1252 |
|
* @buf: Buffer to write to. |
1253 |
|
* @size: Size of @buf. |
1254 |
|
* @ptr: Pointer to "struct ipaddr_union". |
1255 |
|
* |
1256 |
|
* Returns nothing. |
1257 |
|
*/ |
1258 |
|
static void ccs_print_ip(char *buf, const unsigned int size, |
1259 |
|
const struct ccs_ipaddr_union *ptr) |
1260 |
|
{ |
1261 |
|
int len; |
1262 |
|
if (ptr->is_ipv6) |
1263 |
|
len = ccs_print_ipv6(buf, size, &ptr->ip[0]); |
1264 |
|
else |
1265 |
|
len = ccs_print_ipv4(buf, size, &ptr->ip[0].s6_addr32[0]); |
1266 |
|
if (!memcmp(&ptr->ip[0], &ptr->ip[1], 16) || len >= size / 2) |
1267 |
|
return; |
1268 |
|
buf[len++] = '-'; |
1269 |
|
if (ptr->is_ipv6) |
1270 |
|
ccs_print_ipv6(buf + len, size - len, &ptr->ip[1]); |
1271 |
|
else |
1272 |
|
ccs_print_ipv4(buf + len, size - len, |
1273 |
|
&ptr->ip[1].s6_addr32[0]); |
1274 |
|
} |
1275 |
|
|
1276 |
|
#endif |
1277 |
|
|
1278 |
|
/** |
1279 |
|
* ccs_read_token - Read a word from a line. |
1280 |
|
* |
1281 |
|
* @param: Pointer to "struct ccs_acl_param". |
1282 |
|
* |
1283 |
|
* Returns a word on success, "" otherwise. |
1284 |
|
* |
1285 |
|
* To allow the caller to skip NULL check, this function returns "" rather than |
1286 |
|
* NULL if there is no more words to read. |
1287 |
|
*/ |
1288 |
|
static char *ccs_read_token(struct ccs_acl_param *param) |
1289 |
|
{ |
1290 |
|
char *pos = param->data; |
1291 |
|
char *del = strchr(pos, ' '); |
1292 |
|
if (del) |
1293 |
|
*del++ = '\0'; |
1294 |
|
else |
1295 |
|
del = pos + strlen(pos); |
1296 |
|
param->data = del; |
1297 |
|
return pos; |
1298 |
|
} |
1299 |
|
|
1300 |
|
/** |
1301 |
|
* ccs_make_byte - Make byte value from three octal characters. |
1302 |
|
* |
1303 |
|
* @c1: The first character. |
1304 |
|
* @c2: The second character. |
1305 |
|
* @c3: The third character. |
1306 |
|
* |
1307 |
|
* Returns byte value. |
1308 |
|
*/ |
1309 |
|
static u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3) |
1310 |
|
{ |
1311 |
|
return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0'); |
1312 |
|
} |
1313 |
|
|
1314 |
|
/** |
1315 |
|
* ccs_correct_word2 - Check whether the given string follows the naming rules. |
1316 |
|
* |
1317 |
|
* @string: The byte sequence to check. Not '\0'-terminated. |
1318 |
|
* @len: Length of @string. |
1319 |
|
* |
1320 |
|
* Returns true if @string follows the naming rules, false otherwise. |
1321 |
|
*/ |
1322 |
|
static bool ccs_correct_word2(const char *string, size_t len) |
1323 |
|
{ |
1324 |
|
const char *const start = string; |
1325 |
|
bool in_repetition = false; |
1326 |
|
unsigned char c; |
1327 |
|
unsigned char d; |
1328 |
|
unsigned char e; |
1329 |
|
if (!len) |
1330 |
|
goto out; |
1331 |
|
while (len--) { |
1332 |
|
c = *string++; |
1333 |
|
if (c == '\\') { |
1334 |
|
if (!len--) |
1335 |
|
goto out; |
1336 |
|
c = *string++; |
1337 |
|
switch (c) { |
1338 |
|
case '\\': /* "\\" */ |
1339 |
|
continue; |
1340 |
|
case '$': /* "\$" */ |
1341 |
|
case '+': /* "\+" */ |
1342 |
|
case '?': /* "\?" */ |
1343 |
|
case '*': /* "\*" */ |
1344 |
|
case '@': /* "\@" */ |
1345 |
|
case 'x': /* "\x" */ |
1346 |
|
case 'X': /* "\X" */ |
1347 |
|
case 'a': /* "\a" */ |
1348 |
|
case 'A': /* "\A" */ |
1349 |
|
case '-': /* "\-" */ |
1350 |
|
continue; |
1351 |
|
case '{': /* "/\{" */ |
1352 |
|
if (string - 3 < start || *(string - 3) != '/') |
1353 |
|
break; |
1354 |
|
in_repetition = true; |
1355 |
|
continue; |
1356 |
|
case '}': /* "\}/" */ |
1357 |
|
if (*string != '/') |
1358 |
|
break; |
1359 |
|
if (!in_repetition) |
1360 |
|
break; |
1361 |
|
in_repetition = false; |
1362 |
|
continue; |
1363 |
|
case '0': /* "\ooo" */ |
1364 |
|
case '1': |
1365 |
|
case '2': |
1366 |
|
case '3': |
1367 |
|
if (!len-- || !len--) |
1368 |
|
break; |
1369 |
|
d = *string++; |
1370 |
|
e = *string++; |
1371 |
|
if (d < '0' || d > '7' || e < '0' || e > '7') |
1372 |
|
break; |
1373 |
|
c = ccs_make_byte(c, d, e); |
1374 |
|
if (c <= ' ' || c >= 127) |
1375 |
|
continue; |
1376 |
|
} |
1377 |
|
goto out; |
1378 |
|
} else if (in_repetition && c == '/') { |
1379 |
|
goto out; |
1380 |
|
} else if (c <= ' ' || c >= 127) { |
1381 |
|
goto out; |
1382 |
|
} |
1383 |
|
} |
1384 |
|
if (in_repetition) |
1385 |
|
goto out; |
1386 |
|
return true; |
1387 |
|
out: |
1388 |
|
return false; |
1389 |
|
} |
1390 |
|
|
1391 |
|
/** |
1392 |
|
* ccs_correct_word - Check whether the given string follows the naming rules. |
1393 |
|
* |
1394 |
|
* @string: The string to check. |
1395 |
|
* |
1396 |
|
* Returns true if @string follows the naming rules, false otherwise. |
1397 |
|
*/ |
1398 |
|
static bool ccs_correct_word(const char *string) |
1399 |
|
{ |
1400 |
|
return ccs_correct_word2(string, strlen(string)); |
1401 |
|
} |
1402 |
|
|
1403 |
|
/** |
1404 |
|
* ccs_get_group - Allocate memory for "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group". |
1405 |
|
* |
1406 |
|
* @param: Pointer to "struct ccs_acl_param". |
1407 |
|
* @idx: Index number. |
1408 |
|
* |
1409 |
|
* Returns pointer to "struct ccs_group" on success, NULL otherwise. |
1410 |
|
*/ |
1411 |
|
static struct ccs_group *ccs_get_group(struct ccs_acl_param *param, |
1412 |
|
const u8 idx) |
1413 |
|
{ |
1414 |
|
struct ccs_group e = { }; |
1415 |
|
struct ccs_group *group = NULL; |
1416 |
|
struct list_head *list; |
1417 |
|
const char *group_name = ccs_read_token(param); |
1418 |
|
bool found = false; |
1419 |
|
if (!ccs_correct_word(group_name) || idx >= CCS_MAX_GROUP) |
1420 |
|
return NULL; |
1421 |
|
e.group_name = ccs_get_name(group_name); |
1422 |
|
if (!e.group_name) |
1423 |
|
return NULL; |
1424 |
|
if (mutex_lock_interruptible(&ccs_policy_lock)) |
1425 |
|
goto out; |
1426 |
|
list = ¶m->ns->group_list[idx]; |
1427 |
|
list_for_each_entry(group, list, head.list) { |
1428 |
|
if (e.group_name != group->group_name || |
1429 |
|
atomic_read(&group->head.users) == CCS_GC_IN_PROGRESS) |
1430 |
|
continue; |
1431 |
|
atomic_inc(&group->head.users); |
1432 |
|
found = true; |
1433 |
|
break; |
1434 |
|
} |
1435 |
|
if (!found) { |
1436 |
|
struct ccs_group *entry = ccs_commit_ok(&e, sizeof(e)); |
1437 |
|
if (entry) { |
1438 |
|
INIT_LIST_HEAD(&entry->member_list); |
1439 |
|
atomic_set(&entry->head.users, 1); |
1440 |
|
list_add_tail_rcu(&entry->head.list, list); |
1441 |
|
group = entry; |
1442 |
|
found = true; |
1443 |
|
} |
1444 |
|
} |
1445 |
|
mutex_unlock(&ccs_policy_lock); |
1446 |
|
out: |
1447 |
|
ccs_put_name(e.group_name); |
1448 |
|
return found ? group : NULL; |
1449 |
|
} |
1450 |
|
|
1451 |
|
/** |
1452 |
|
* ccs_parse_name_union - Parse a ccs_name_union. |
1453 |
|
* |
1454 |
|
* @param: Pointer to "struct ccs_acl_param". |
1455 |
|
* @ptr: Pointer to "struct ccs_name_union". |
1456 |
|
* |
1457 |
|
* Returns true on success, false otherwise. |
1458 |
|
*/ |
1459 |
|
static bool ccs_parse_name_union(struct ccs_acl_param *param, |
1460 |
|
struct ccs_name_union *ptr) |
1461 |
|
{ |
1462 |
|
char *filename; |
1463 |
|
if (param->data[0] == '@') { |
1464 |
|
param->data++; |
1465 |
|
ptr->group = ccs_get_group(param, CCS_PATH_GROUP); |
1466 |
|
return ptr->group != NULL; |
1467 |
|
} |
1468 |
|
filename = ccs_read_token(param); |
1469 |
|
if (!ccs_correct_word(filename)) |
1470 |
|
return false; |
1471 |
|
ptr->filename = ccs_get_name(filename); |
1472 |
|
return ptr->filename != NULL; |
1473 |
|
} |
1474 |
|
|
1475 |
|
/** |
1476 |
|
* ccs_parse_ulong - Parse an "unsigned long" value. |
1477 |
|
* |
1478 |
|
* @result: Pointer to "unsigned long". |
1479 |
|
* @str: Pointer to string to parse. |
1480 |
|
* |
1481 |
|
* Returns one of values in "enum ccs_value_type". |
1482 |
|
* |
1483 |
|
* The @src is updated to point the first character after the value |
1484 |
|
* on success. |
1485 |
|
*/ |
1486 |
|
static u8 ccs_parse_ulong(unsigned long *result, char **str) |
1487 |
|
{ |
1488 |
|
const char *cp = *str; |
1489 |
|
char *ep; |
1490 |
|
int base = 10; |
1491 |
|
if (*cp == '0') { |
1492 |
|
char c = *(cp + 1); |
1493 |
|
if (c == 'x' || c == 'X') { |
1494 |
|
base = 16; |
1495 |
|
cp += 2; |
1496 |
|
} else if (c >= '0' && c <= '7') { |
1497 |
|
base = 8; |
1498 |
|
cp++; |
1499 |
|
} |
1500 |
|
} |
1501 |
|
*result = simple_strtoul(cp, &ep, base); |
1502 |
|
if (cp == ep) |
1503 |
|
return CCS_VALUE_TYPE_INVALID; |
1504 |
|
*str = ep; |
1505 |
|
switch (base) { |
1506 |
|
case 16: |
1507 |
|
return CCS_VALUE_TYPE_HEXADECIMAL; |
1508 |
|
case 8: |
1509 |
|
return CCS_VALUE_TYPE_OCTAL; |
1510 |
|
default: |
1511 |
|
return CCS_VALUE_TYPE_DECIMAL; |
1512 |
|
} |
1513 |
|
} |
1514 |
|
|
1515 |
|
/** |
1516 |
|
* ccs_parse_number_union - Parse a ccs_number_union. |
1517 |
|
* |
1518 |
|
* @param: Pointer to "struct ccs_acl_param". |
1519 |
|
* @ptr: Pointer to "struct ccs_number_union". |
1520 |
|
* |
1521 |
|
* Returns true on success, false otherwise. |
1522 |
|
*/ |
1523 |
|
static bool ccs_parse_number_union(struct ccs_acl_param *param, |
1524 |
|
struct ccs_number_union *ptr) |
1525 |
|
{ |
1526 |
|
char *data; |
1527 |
|
u8 type; |
1528 |
|
unsigned long v; |
1529 |
|
memset(ptr, 0, sizeof(*ptr)); |
1530 |
|
if (param->data[0] == '@') { |
1531 |
|
param->data++; |
1532 |
|
ptr->group = ccs_get_group(param, CCS_NUMBER_GROUP); |
1533 |
|
return ptr->group != NULL; |
1534 |
|
} |
1535 |
|
data = ccs_read_token(param); |
1536 |
|
type = ccs_parse_ulong(&v, &data); |
1537 |
|
if (type == CCS_VALUE_TYPE_INVALID) |
1538 |
|
return false; |
1539 |
|
ptr->values[0] = v; |
1540 |
|
ptr->value_type[0] = type; |
1541 |
|
if (!*data) { |
1542 |
|
ptr->values[1] = v; |
1543 |
|
ptr->value_type[1] = type; |
1544 |
|
return true; |
1545 |
|
} |
1546 |
|
if (*data++ != '-') |
1547 |
|
return false; |
1548 |
|
type = ccs_parse_ulong(&v, &data); |
1549 |
|
if (type == CCS_VALUE_TYPE_INVALID || *data || ptr->values[0] > v) |
1550 |
|
return false; |
1551 |
|
ptr->values[1] = v; |
1552 |
|
ptr->value_type[1] = type; |
1553 |
|
return true; |
1554 |
|
} |
1555 |
|
|
1556 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
1557 |
|
|
1558 |
|
/** |
1559 |
|
* ccs_parse_ipaddr_union - Parse an IP address. |
1560 |
|
* |
1561 |
|
* @param: Pointer to "struct ccs_acl_param". |
1562 |
|
* @ptr: Pointer to "struct ccs_ipaddr_union". |
1563 |
|
* |
1564 |
|
* Returns true on success, false otherwise. |
1565 |
|
*/ |
1566 |
|
static bool ccs_parse_ipaddr_union(struct ccs_acl_param *param, |
1567 |
|
struct ccs_ipaddr_union *ptr) |
1568 |
|
{ |
1569 |
|
u8 * const min = ptr->ip[0].in6_u.u6_addr8; |
1570 |
|
u8 * const max = ptr->ip[1].in6_u.u6_addr8; |
1571 |
|
char *address = ccs_read_token(param); |
1572 |
|
const char *end; |
1573 |
|
if (!strchr(address, ':') && |
1574 |
|
ccs_in4_pton(address, -1, min, '-', &end) > 0) { |
1575 |
|
ptr->is_ipv6 = false; |
1576 |
|
if (!*end) |
1577 |
|
ptr->ip[1].s6_addr32[0] = ptr->ip[0].s6_addr32[0]; |
1578 |
|
else if (*end++ != '-' || |
1579 |
|
ccs_in4_pton(end, -1, max, '\0', &end) <= 0 || *end) |
1580 |
|
return false; |
1581 |
|
return true; |
1582 |
|
} |
1583 |
|
if (ccs_in6_pton(address, -1, min, '-', &end) > 0) { |
1584 |
|
ptr->is_ipv6 = true; |
1585 |
|
if (!*end) |
1586 |
|
memmove(max, min, sizeof(u16) * 8); |
1587 |
|
else if (*end++ != '-' || |
1588 |
|
ccs_in6_pton(end, -1, max, '\0', &end) <= 0 || *end) |
1589 |
|
return false; |
1590 |
|
return true; |
1591 |
|
} |
1592 |
|
return false; |
1593 |
|
} |
1594 |
|
|
1595 |
|
#endif |
1596 |
|
|
1597 |
|
/** |
1598 |
|
* ccs_get_dqword - ccs_get_name() for a quoted string. |
1599 |
|
* |
1600 |
|
* @start: String to save. |
1601 |
|
* |
1602 |
|
* Returns pointer to "struct ccs_path_info" on success, NULL otherwise. |
1603 |
|
*/ |
1604 |
|
static const struct ccs_path_info *ccs_get_dqword(char *start) |
1605 |
|
{ |
1606 |
|
char *cp = start + strlen(start) - 1; |
1607 |
|
if (cp == start || *start++ != '"' || *cp != '"') |
1608 |
|
return NULL; |
1609 |
|
*cp = '\0'; |
1610 |
|
if (*start && !ccs_correct_word(start)) |
1611 |
|
return NULL; |
1612 |
|
return ccs_get_name(start); |
1613 |
|
} |
1614 |
|
|
1615 |
|
/** |
1616 |
|
* ccs_parse_name_union_quoted - Parse a quoted word. |
1617 |
|
* |
1618 |
|
* @param: Pointer to "struct ccs_acl_param". |
1619 |
|
* @ptr: Pointer to "struct ccs_name_union". |
1620 |
|
* |
1621 |
|
* Returns true on success, false otherwise. |
1622 |
|
*/ |
1623 |
|
static bool ccs_parse_name_union_quoted(struct ccs_acl_param *param, |
1624 |
|
struct ccs_name_union *ptr) |
1625 |
|
{ |
1626 |
|
char *filename = param->data; |
1627 |
|
if (*filename == '@') |
1628 |
|
return ccs_parse_name_union(param, ptr); |
1629 |
|
ptr->filename = ccs_get_dqword(filename); |
1630 |
|
return ptr->filename != NULL; |
1631 |
|
} |
1632 |
|
|
1633 |
|
/** |
1634 |
|
* ccs_parse_argv - Parse an argv[] condition part. |
1635 |
|
* |
1636 |
|
* @left: Lefthand value. |
1637 |
|
* @right: Righthand value. |
1638 |
|
* @argv: Pointer to "struct ccs_argv". |
1639 |
|
* |
1640 |
|
* Returns true on success, false otherwise. |
1641 |
|
*/ |
1642 |
|
static bool ccs_parse_argv(char *left, char *right, struct ccs_argv *argv) |
1643 |
|
{ |
1644 |
|
if (ccs_parse_ulong(&argv->index, &left) != CCS_VALUE_TYPE_DECIMAL || |
1645 |
|
*left++ != ']' || *left) |
1646 |
|
return false; |
1647 |
|
argv->value = ccs_get_dqword(right); |
1648 |
|
return argv->value != NULL; |
1649 |
|
} |
1650 |
|
|
1651 |
|
/** |
1652 |
|
* ccs_parse_envp - Parse an envp[] condition part. |
1653 |
|
* |
1654 |
|
* @left: Lefthand value. |
1655 |
|
* @right: Righthand value. |
1656 |
|
* @envp: Pointer to "struct ccs_envp". |
1657 |
|
* |
1658 |
|
* Returns true on success, false otherwise. |
1659 |
|
*/ |
1660 |
|
static bool ccs_parse_envp(char *left, char *right, struct ccs_envp *envp) |
1661 |
|
{ |
1662 |
|
const struct ccs_path_info *name; |
1663 |
|
const struct ccs_path_info *value; |
1664 |
|
char *cp = left + strlen(left) - 1; |
1665 |
|
if (*cp-- != ']' || *cp != '"') |
1666 |
|
goto out; |
1667 |
|
*cp = '\0'; |
1668 |
|
if (!ccs_correct_word(left)) |
1669 |
|
goto out; |
1670 |
|
name = ccs_get_name(left); |
1671 |
|
if (!name) |
1672 |
|
goto out; |
1673 |
|
if (!strcmp(right, "NULL")) { |
1674 |
|
value = NULL; |
1675 |
|
} else { |
1676 |
|
value = ccs_get_dqword(right); |
1677 |
|
if (!value) { |
1678 |
|
ccs_put_name(name); |
1679 |
|
goto out; |
1680 |
|
} |
1681 |
|
} |
1682 |
|
envp->name = name; |
1683 |
|
envp->value = value; |
1684 |
|
return true; |
1685 |
|
out: |
1686 |
|
return false; |
1687 |
|
} |
1688 |
|
|
1689 |
|
/** |
1690 |
|
* ccs_same_condition - Check for duplicated "struct ccs_condition" entry. |
1691 |
|
* |
1692 |
|
* @a: Pointer to "struct ccs_condition". |
1693 |
|
* @b: Pointer to "struct ccs_condition". |
1694 |
|
* |
1695 |
|
* Returns true if @a == @b, false otherwise. |
1696 |
|
*/ |
1697 |
|
static bool ccs_same_condition(const struct ccs_condition *a, |
1698 |
|
const struct ccs_condition *b) |
1699 |
|
{ |
1700 |
|
return a->size == b->size && a->condc == b->condc && |
1701 |
|
a->numbers_count == b->numbers_count && |
1702 |
|
a->names_count == b->names_count && |
1703 |
|
a->argc == b->argc && a->envc == b->envc && |
1704 |
|
a->grant_log == b->grant_log && |
1705 |
|
a->exec_transit == b->exec_transit && a->transit == b->transit |
1706 |
|
&& !memcmp(a + 1, b + 1, a->size - sizeof(*a)); |
1707 |
|
} |
1708 |
|
|
1709 |
|
/** |
1710 |
|
* ccs_condition_type - Get condition type. |
1711 |
|
* |
1712 |
|
* @word: Keyword string. |
1713 |
|
* |
1714 |
|
* Returns one of values in "enum ccs_conditions_index" on success, |
1715 |
|
* CCS_MAX_CONDITION_KEYWORD otherwise. |
1716 |
|
*/ |
1717 |
|
static u8 ccs_condition_type(const char *word) |
1718 |
|
{ |
1719 |
|
u8 i; |
1720 |
|
for (i = 0; i < CCS_MAX_CONDITION_KEYWORD; i++) { |
1721 |
|
if (!strcmp(word, ccs_condition_keyword[i])) |
1722 |
|
break; |
1723 |
|
} |
1724 |
|
return i; |
1725 |
|
} |
1726 |
|
|
1727 |
|
/** |
1728 |
|
* ccs_commit_condition - Commit "struct ccs_condition". |
1729 |
|
* |
1730 |
|
* @entry: Pointer to "struct ccs_condition". |
1731 |
|
* |
1732 |
|
* Returns pointer to "struct ccs_condition" on success, NULL otherwise. |
1733 |
|
* |
1734 |
|
* This function merges duplicated entries. This function returns NULL if |
1735 |
|
* @entry is not duplicated but memory quota for policy has exceeded. |
1736 |
|
*/ |
1737 |
|
static struct ccs_condition *ccs_commit_condition(struct ccs_condition *entry) |
1738 |
|
{ |
1739 |
|
struct ccs_condition *ptr; |
1740 |
|
bool found = false; |
1741 |
|
if (mutex_lock_interruptible(&ccs_policy_lock)) { |
1742 |
|
dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__); |
1743 |
|
ptr = NULL; |
1744 |
|
found = true; |
1745 |
|
goto out; |
1746 |
|
} |
1747 |
|
list_for_each_entry(ptr, &ccs_condition_list, head.list) { |
1748 |
|
if (!ccs_same_condition(ptr, entry) || |
1749 |
|
atomic_read(&ptr->head.users) == CCS_GC_IN_PROGRESS) |
1750 |
|
continue; |
1751 |
|
/* Same entry found. Share this entry. */ |
1752 |
|
atomic_inc(&ptr->head.users); |
1753 |
|
found = true; |
1754 |
|
break; |
1755 |
|
} |
1756 |
|
if (!found) { |
1757 |
|
if (ccs_memory_ok(entry, entry->size)) { |
1758 |
|
atomic_set(&entry->head.users, 1); |
1759 |
|
list_add(&entry->head.list, &ccs_condition_list); |
1760 |
|
} else { |
1761 |
|
found = true; |
1762 |
|
ptr = NULL; |
1763 |
|
} |
1764 |
|
} |
1765 |
|
mutex_unlock(&ccs_policy_lock); |
1766 |
|
out: |
1767 |
|
if (found) { |
1768 |
|
ccs_del_condition(&entry->head.list); |
1769 |
|
kfree(entry); |
1770 |
|
entry = ptr; |
1771 |
|
} |
1772 |
|
return entry; |
1773 |
|
} |
1774 |
|
|
1775 |
|
/** |
1776 |
|
* ccs_correct_path - Check whether the given pathname follows the naming rules. |
1777 |
|
* |
1778 |
|
* @filename: The pathname to check. |
1779 |
|
* |
1780 |
|
* Returns true if @filename follows the naming rules, false otherwise. |
1781 |
|
*/ |
1782 |
|
static bool ccs_correct_path(const char *filename) |
1783 |
|
{ |
1784 |
|
return *filename == '/' && ccs_correct_word(filename); |
1785 |
|
} |
1786 |
|
|
1787 |
|
/** |
1788 |
|
* ccs_domain_def - Check whether the given token can be a domainname. |
1789 |
|
* |
1790 |
|
* @buffer: The token to check. |
1791 |
|
* |
1792 |
|
* Returns true if @buffer possibly be a domainname, false otherwise. |
1793 |
|
*/ |
1794 |
|
static bool ccs_domain_def(const unsigned char *buffer) |
1795 |
|
{ |
1796 |
|
const unsigned char *cp; |
1797 |
|
int len; |
1798 |
|
if (*buffer != '<') |
1799 |
|
return false; |
1800 |
|
cp = strchr(buffer, ' '); |
1801 |
|
if (!cp) |
1802 |
|
len = strlen(buffer); |
1803 |
|
else |
1804 |
|
len = cp - buffer; |
1805 |
|
if (buffer[len - 1] != '>' || !ccs_correct_word2(buffer + 1, len - 2)) |
1806 |
|
return false; |
1807 |
|
return true; |
1808 |
|
} |
1809 |
|
|
1810 |
|
/** |
1811 |
|
* ccs_correct_domain - Check whether the given domainname follows the naming rules. |
1812 |
|
* |
1813 |
|
* @domainname: The domainname to check. |
1814 |
|
* |
1815 |
|
* Returns true if @domainname follows the naming rules, false otherwise. |
1816 |
|
*/ |
1817 |
|
static bool ccs_correct_domain(const unsigned char *domainname) |
1818 |
|
{ |
1819 |
|
if (!domainname || !ccs_domain_def(domainname)) |
1820 |
|
return false; |
1821 |
|
domainname = strchr(domainname, ' '); |
1822 |
|
if (!domainname++) |
1823 |
|
return true; |
1824 |
|
while (1) { |
1825 |
|
const unsigned char *cp = strchr(domainname, ' '); |
1826 |
|
if (!cp) |
1827 |
|
break; |
1828 |
|
if (*domainname != '/' || |
1829 |
|
!ccs_correct_word2(domainname, cp - domainname)) |
1830 |
|
return false; |
1831 |
|
domainname = cp + 1; |
1832 |
|
} |
1833 |
|
return ccs_correct_path(domainname); |
1834 |
|
} |
1835 |
|
|
1836 |
|
/** |
1837 |
|
* ccs_normalize_line - Format string. |
1838 |
|
* |
1839 |
|
* @buffer: The line to normalize. |
1840 |
|
* |
1841 |
|
* Returns nothing. |
1842 |
|
* |
1843 |
|
* Leading and trailing whitespaces are removed. |
1844 |
|
* Multiple whitespaces are packed into single space. |
1845 |
|
*/ |
1846 |
|
static void ccs_normalize_line(unsigned char *buffer) |
1847 |
|
{ |
1848 |
|
unsigned char *sp = buffer; |
1849 |
|
unsigned char *dp = buffer; |
1850 |
|
bool first = true; |
1851 |
|
while (*sp && (*sp <= ' ' || *sp >= 127)) |
1852 |
|
sp++; |
1853 |
|
while (*sp) { |
1854 |
|
if (!first) |
1855 |
|
*dp++ = ' '; |
1856 |
|
first = false; |
1857 |
|
while (*sp > ' ' && *sp < 127) |
1858 |
|
*dp++ = *sp++; |
1859 |
|
while (*sp && (*sp <= ' ' || *sp >= 127)) |
1860 |
|
sp++; |
1861 |
|
} |
1862 |
|
*dp = '\0'; |
1863 |
|
} |
1864 |
|
|
1865 |
|
/** |
1866 |
|
* ccs_get_domainname - Read a domainname from a line. |
1867 |
|
* |
1868 |
|
* @param: Pointer to "struct ccs_acl_param". |
1869 |
|
* |
1870 |
|
* Returns a domainname on success, NULL otherwise. |
1871 |
|
*/ |
1872 |
|
static const struct ccs_path_info *ccs_get_domainname |
1873 |
|
(struct ccs_acl_param *param) |
1874 |
|
{ |
1875 |
|
char *start = param->data; |
1876 |
|
char *pos = start; |
1877 |
|
while (*pos) { |
1878 |
|
if (*pos++ != ' ' || *pos++ == '/') |
1879 |
|
continue; |
1880 |
|
pos -= 2; |
1881 |
|
*pos++ = '\0'; |
1882 |
|
break; |
1883 |
|
} |
1884 |
|
param->data = pos; |
1885 |
|
if (ccs_correct_domain(start)) |
1886 |
|
return ccs_get_name(start); |
1887 |
|
return NULL; |
1888 |
|
} |
1889 |
|
|
1890 |
|
/** |
1891 |
|
* ccs_get_transit_preference - Parse domain transition preference for execve(). |
1892 |
|
* |
1893 |
|
* @param: Pointer to "struct ccs_acl_param". |
1894 |
|
* @e: Pointer to "struct ccs_condition". |
1895 |
|
* |
1896 |
|
* Returns the condition string part. |
1897 |
|
*/ |
1898 |
|
static char *ccs_get_transit_preference(struct ccs_acl_param *param, |
1899 |
|
struct ccs_condition *e) |
1900 |
|
{ |
1901 |
|
char * const pos = param->data; |
1902 |
|
bool flag; |
1903 |
|
if (*pos == '<') { |
1904 |
|
e->transit = ccs_get_domainname(param); |
1905 |
|
goto done; |
1906 |
|
} |
1907 |
|
{ |
1908 |
|
char *cp = strchr(pos, ' '); |
1909 |
|
if (cp) |
1910 |
|
*cp = '\0'; |
1911 |
|
flag = ccs_correct_path(pos) || !strcmp(pos, "keep") || |
1912 |
|
!strcmp(pos, "initialize") || !strcmp(pos, "reset") || |
1913 |
|
!strcmp(pos, "child") || !strcmp(pos, "parent"); |
1914 |
|
if (cp) |
1915 |
|
*cp = ' '; |
1916 |
|
} |
1917 |
|
if (!flag) |
1918 |
|
return pos; |
1919 |
|
e->transit = ccs_get_name(ccs_read_token(param)); |
1920 |
|
done: |
1921 |
|
if (e->transit) { |
1922 |
|
e->exec_transit = true; |
1923 |
|
return param->data; |
1924 |
|
} |
1925 |
|
/* |
1926 |
|
* Return a bad read-only condition string that will let |
1927 |
|
* ccs_get_condition() return NULL. |
1928 |
|
*/ |
1929 |
|
return "/"; |
1930 |
|
} |
1931 |
|
|
1932 |
|
/** |
1933 |
|
* ccs_get_condition - Parse condition part. |
1934 |
|
* |
1935 |
|
* @param: Pointer to "struct ccs_acl_param". |
1936 |
|
* |
1937 |
|
* Returns pointer to "struct ccs_condition" on success, NULL otherwise. |
1938 |
|
*/ |
1939 |
|
struct ccs_condition *ccs_get_condition(struct ccs_acl_param *param) |
1940 |
|
{ |
1941 |
|
struct ccs_condition *entry = NULL; |
1942 |
|
struct ccs_condition_element *condp = NULL; |
1943 |
|
struct ccs_number_union *numbers_p = NULL; |
1944 |
|
struct ccs_name_union *names_p = NULL; |
1945 |
|
struct ccs_argv *argv = NULL; |
1946 |
|
struct ccs_envp *envp = NULL; |
1947 |
|
struct ccs_condition e = { }; |
1948 |
|
char * const start_of_string = ccs_get_transit_preference(param, &e); |
1949 |
|
char * const end_of_string = start_of_string + strlen(start_of_string); |
1950 |
|
char *pos; |
1951 |
|
rerun: |
1952 |
|
pos = start_of_string; |
1953 |
|
while (1) { |
1954 |
|
u8 left = -1; |
1955 |
|
u8 right = -1; |
1956 |
|
char *left_word = pos; |
1957 |
|
char *cp; |
1958 |
|
char *right_word; |
1959 |
|
bool is_not; |
1960 |
|
if (!*left_word) |
1961 |
|
break; |
1962 |
|
/* |
1963 |
|
* Since left-hand condition does not allow use of "path_group" |
1964 |
|
* or "number_group" and environment variable's names do not |
1965 |
|
* accept '=', it is guaranteed that the original line consists |
1966 |
|
* of one or more repetition of $left$operator$right blocks |
1967 |
|
* where "$left is free from '=' and ' '" and "$operator is |
1968 |
|
* either '=' or '!='" and "$right is free from ' '". |
1969 |
|
* Therefore, we can reconstruct the original line at the end |
1970 |
|
* of dry run even if we overwrite $operator with '\0'. |
1971 |
|
*/ |
1972 |
|
cp = strchr(pos, ' '); |
1973 |
|
if (cp) { |
1974 |
|
*cp = '\0'; /* Will restore later. */ |
1975 |
|
pos = cp + 1; |
1976 |
|
} else { |
1977 |
|
pos = ""; |
1978 |
|
} |
1979 |
|
right_word = strchr(left_word, '='); |
1980 |
|
if (!right_word || right_word == left_word) |
1981 |
|
goto out; |
1982 |
|
is_not = *(right_word - 1) == '!'; |
1983 |
|
if (is_not) |
1984 |
|
*(right_word++ - 1) = '\0'; /* Will restore later. */ |
1985 |
|
else if (*(right_word + 1) != '=') |
1986 |
|
*right_word++ = '\0'; /* Will restore later. */ |
1987 |
|
else |
1988 |
|
goto out; |
1989 |
|
dprintk(KERN_WARNING "%u: <%s>%s=<%s>\n", __LINE__, left_word, |
1990 |
|
is_not ? "!" : "", right_word); |
1991 |
|
if (!strcmp(left_word, "grant_log")) { |
1992 |
|
if (entry) { |
1993 |
|
if (is_not || |
1994 |
|
entry->grant_log != CCS_GRANTLOG_AUTO) |
1995 |
|
goto out; |
1996 |
|
else if (!strcmp(right_word, "yes")) |
1997 |
|
entry->grant_log = CCS_GRANTLOG_YES; |
1998 |
|
else if (!strcmp(right_word, "no")) |
1999 |
|
entry->grant_log = CCS_GRANTLOG_NO; |
2000 |
|
else |
2001 |
|
goto out; |
2002 |
|
} |
2003 |
|
continue; |
2004 |
|
} |
2005 |
|
if (!strcmp(left_word, "auto_domain_transition")) { |
2006 |
|
if (entry) { |
2007 |
|
if (is_not || entry->transit) |
2008 |
|
goto out; |
2009 |
|
entry->transit = ccs_get_dqword(right_word); |
2010 |
|
if (!entry->transit || |
2011 |
|
(entry->transit->name[0] != '/' && |
2012 |
|
!ccs_domain_def(entry->transit->name))) |
2013 |
|
goto out; |
2014 |
|
} |
2015 |
|
continue; |
2016 |
|
} |
2017 |
|
if (!strncmp(left_word, "exec.argv[", 10)) { |
2018 |
|
if (!argv) { |
2019 |
|
e.argc++; |
2020 |
|
e.condc++; |
2021 |
|
} else { |
2022 |
|
e.argc--; |
2023 |
|
e.condc--; |
2024 |
|
left = CCS_ARGV_ENTRY; |
2025 |
|
argv->is_not = is_not; |
2026 |
|
if (!ccs_parse_argv(left_word + 10, |
2027 |
|
right_word, argv++)) |
2028 |
|
goto out; |
2029 |
|
} |
2030 |
|
goto store_value; |
2031 |
|
} |
2032 |
|
if (!strncmp(left_word, "exec.envp[\"", 11)) { |
2033 |
|
if (!envp) { |
2034 |
|
e.envc++; |
2035 |
|
e.condc++; |
2036 |
|
} else { |
2037 |
|
e.envc--; |
2038 |
|
e.condc--; |
2039 |
|
left = CCS_ENVP_ENTRY; |
2040 |
|
envp->is_not = is_not; |
2041 |
|
if (!ccs_parse_envp(left_word + 11, |
2042 |
|
right_word, envp++)) |
2043 |
|
goto out; |
2044 |
|
} |
2045 |
|
goto store_value; |
2046 |
|
} |
2047 |
|
left = ccs_condition_type(left_word); |
2048 |
|
dprintk(KERN_WARNING "%u: <%s> left=%u\n", __LINE__, left_word, |
2049 |
|
left); |
2050 |
|
if (left == CCS_MAX_CONDITION_KEYWORD) { |
2051 |
|
if (!numbers_p) { |
2052 |
|
e.numbers_count++; |
2053 |
|
} else { |
2054 |
|
e.numbers_count--; |
2055 |
|
left = CCS_NUMBER_UNION; |
2056 |
|
param->data = left_word; |
2057 |
|
if (*left_word == '@' || |
2058 |
|
!ccs_parse_number_union(param, |
2059 |
|
numbers_p++)) |
2060 |
|
goto out; |
2061 |
|
} |
2062 |
|
} |
2063 |
|
if (!condp) |
2064 |
|
e.condc++; |
2065 |
|
else |
2066 |
|
e.condc--; |
2067 |
|
if (left == CCS_EXEC_REALPATH || left == CCS_SYMLINK_TARGET) { |
2068 |
|
if (!names_p) { |
2069 |
|
e.names_count++; |
2070 |
|
} else { |
2071 |
|
e.names_count--; |
2072 |
|
right = CCS_NAME_UNION; |
2073 |
|
param->data = right_word; |
2074 |
|
if (!ccs_parse_name_union_quoted(param, |
2075 |
|
names_p++)) |
2076 |
|
goto out; |
2077 |
|
} |
2078 |
|
goto store_value; |
2079 |
|
} |
2080 |
|
right = ccs_condition_type(right_word); |
2081 |
|
if (right == CCS_MAX_CONDITION_KEYWORD) { |
2082 |
|
if (!numbers_p) { |
2083 |
|
e.numbers_count++; |
2084 |
|
} else { |
2085 |
|
e.numbers_count--; |
2086 |
|
right = CCS_NUMBER_UNION; |
2087 |
|
param->data = right_word; |
2088 |
|
if (!ccs_parse_number_union(param, |
2089 |
|
numbers_p++)) |
2090 |
|
goto out; |
2091 |
|
} |
2092 |
|
} |
2093 |
|
store_value: |
2094 |
|
if (!condp) { |
2095 |
|
dprintk(KERN_WARNING "%u: dry_run left=%u right=%u " |
2096 |
|
"match=%u\n", __LINE__, left, right, !is_not); |
2097 |
|
continue; |
2098 |
|
} |
2099 |
|
condp->left = left; |
2100 |
|
condp->right = right; |
2101 |
|
condp->equals = !is_not; |
2102 |
|
dprintk(KERN_WARNING "%u: left=%u right=%u match=%u\n", |
2103 |
|
__LINE__, condp->left, condp->right, |
2104 |
|
condp->equals); |
2105 |
|
condp++; |
2106 |
|
} |
2107 |
|
dprintk(KERN_INFO "%u: cond=%u numbers=%u names=%u ac=%u ec=%u\n", |
2108 |
|
__LINE__, e.condc, e.numbers_count, e.names_count, e.argc, |
2109 |
|
e.envc); |
2110 |
|
if (entry) { |
2111 |
|
BUG_ON(e.names_count | e.numbers_count | e.argc | e.envc | |
2112 |
|
e.condc); |
2113 |
|
return ccs_commit_condition(entry); |
2114 |
|
} |
2115 |
|
e.size = sizeof(*entry) |
2116 |
|
+ e.condc * sizeof(struct ccs_condition_element) |
2117 |
|
+ e.numbers_count * sizeof(struct ccs_number_union) |
2118 |
|
+ e.names_count * sizeof(struct ccs_name_union) |
2119 |
|
+ e.argc * sizeof(struct ccs_argv) |
2120 |
|
+ e.envc * sizeof(struct ccs_envp); |
2121 |
|
entry = kzalloc(e.size, CCS_GFP_FLAGS); |
2122 |
|
if (!entry) |
2123 |
|
goto out2; |
2124 |
|
*entry = e; |
2125 |
|
e.transit = NULL; |
2126 |
|
condp = (struct ccs_condition_element *) (entry + 1); |
2127 |
|
numbers_p = (struct ccs_number_union *) (condp + e.condc); |
2128 |
|
names_p = (struct ccs_name_union *) (numbers_p + e.numbers_count); |
2129 |
|
argv = (struct ccs_argv *) (names_p + e.names_count); |
2130 |
|
envp = (struct ccs_envp *) (argv + e.argc); |
2131 |
|
{ |
2132 |
|
bool flag = false; |
2133 |
|
for (pos = start_of_string; pos < end_of_string; pos++) { |
2134 |
|
if (*pos) |
2135 |
|
continue; |
2136 |
|
if (flag) /* Restore " ". */ |
2137 |
|
*pos = ' '; |
2138 |
|
else if (*(pos + 1) == '=') /* Restore "!=". */ |
2139 |
|
*pos = '!'; |
2140 |
|
else /* Restore "=". */ |
2141 |
|
*pos = '='; |
2142 |
|
flag = !flag; |
2143 |
|
} |
2144 |
|
} |
2145 |
|
goto rerun; |
2146 |
|
out: |
2147 |
|
dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__); |
2148 |
|
if (entry) { |
2149 |
|
ccs_del_condition(&entry->head.list); |
2150 |
|
kfree(entry); |
2151 |
|
} |
2152 |
|
out2: |
2153 |
|
ccs_put_name(e.transit); |
2154 |
|
return NULL; |
2155 |
|
} |
2156 |
|
|
2157 |
/** |
/** |
2158 |
* ccs_yesno - Return "yes" or "no". |
* ccs_yesno - Return "yes" or "no". |
2159 |
* |
* |
2161 |
* |
* |
2162 |
* Returns "yes" if @value is not 0, "no" otherwise. |
* Returns "yes" if @value is not 0, "no" otherwise. |
2163 |
*/ |
*/ |
2164 |
const char *ccs_yesno(const unsigned int value) |
static const char *ccs_yesno(const unsigned int value) |
2165 |
{ |
{ |
2166 |
return value ? "yes" : "no"; |
return value ? "yes" : "no"; |
2167 |
} |
} |
2168 |
|
|
|
/* Prototype for ccs_addprintf(). */ |
|
|
static void ccs_addprintf(char *buffer, int len, const char *fmt, ...) |
|
|
__attribute__ ((format(printf, 3, 4))); |
|
|
|
|
2169 |
/** |
/** |
2170 |
* ccs_addprintf - strncat()-like-snprintf(). |
* ccs_addprintf - strncat()-like-snprintf(). |
2171 |
* |
* |
2247 |
printk(KERN_WARNING "Too many words in a line.\n"); |
printk(KERN_WARNING "Too many words in a line.\n"); |
2248 |
} |
} |
2249 |
|
|
|
/* Prototype for ccs_io_printf(). */ |
|
|
static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...) |
|
|
__attribute__ ((format(printf, 2, 3))); |
|
|
|
|
2250 |
/** |
/** |
2251 |
* ccs_io_printf - printf() to "struct ccs_io_buffer" structure. |
* ccs_io_printf - printf() to "struct ccs_io_buffer" structure. |
2252 |
* |
* |
2311 |
ccs_set_string(head, "/"); |
ccs_set_string(head, "/"); |
2312 |
} |
} |
2313 |
|
|
|
/* List of namespaces. */ |
|
|
LIST_HEAD(ccs_namespace_list); |
|
|
/* True if namespace other than ccs_kernel_namespace is defined. */ |
|
|
static bool ccs_namespace_enabled; |
|
|
|
|
2314 |
/** |
/** |
2315 |
* ccs_init_policy_namespace - Initialize namespace. |
* ccs_init_policy_namespace - Initialize namespace. |
2316 |
* |
* |
2318 |
* |
* |
2319 |
* Returns nothing. |
* Returns nothing. |
2320 |
*/ |
*/ |
2321 |
void ccs_init_policy_namespace(struct ccs_policy_namespace *ns) |
static void ccs_init_policy_namespace(struct ccs_policy_namespace *ns) |
2322 |
{ |
{ |
2323 |
unsigned int idx; |
unsigned int idx; |
2324 |
for (idx = 0; idx < CCS_MAX_ACL_GROUPS; idx++) |
for (idx = 0; idx < CCS_MAX_ACL_GROUPS; idx++) |
2401 |
struct ccs_domain_info *domain; |
struct ccs_domain_info *domain; |
2402 |
const int idx = ccs_read_lock(); |
const int idx = ccs_read_lock(); |
2403 |
ccs_policy_loaded = true; |
ccs_policy_loaded = true; |
2404 |
printk(KERN_INFO "CCSecurity: 1.8.3 2011/09/29\n"); |
printk(KERN_INFO "CCSecurity: 1.8.3+ 2011/11/11\n"); |
2405 |
list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) { |
list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) { |
2406 |
const u8 profile = domain->profile; |
const u8 profile = domain->profile; |
2407 |
const struct ccs_policy_namespace *ns = domain->ns; |
const struct ccs_policy_namespace *ns = domain->ns; |
2433 |
* |
* |
2434 |
* Returns pointer to "struct ccs_profile". |
* Returns pointer to "struct ccs_profile". |
2435 |
*/ |
*/ |
2436 |
struct ccs_profile *ccs_profile(const u8 profile) |
static struct ccs_profile *ccs_profile(const u8 profile) |
2437 |
{ |
{ |
2438 |
static struct ccs_profile ccs_null_profile; |
static struct ccs_profile ccs_null_profile; |
2439 |
struct ccs_profile *ptr = ccs_current_namespace()-> |
struct ccs_profile *ptr = ccs_current_namespace()-> |
2443 |
return ptr; |
return ptr; |
2444 |
} |
} |
2445 |
|
|
2446 |
|
|
2447 |
|
/** |
2448 |
|
* ccs_get_config - Get config for specified profile's specified functionality. |
2449 |
|
* |
2450 |
|
* @profile: Profile number. |
2451 |
|
* @index: Index number of functionality. |
2452 |
|
* |
2453 |
|
* Returns config. |
2454 |
|
* |
2455 |
|
* First, check for CONFIG::category::functionality. |
2456 |
|
* If CONFIG::category::functionality is set to use default, then check |
2457 |
|
* CONFIG::category. If CONFIG::category is set to use default, then use |
2458 |
|
* CONFIG. CONFIG cannot be set to use default. |
2459 |
|
*/ |
2460 |
|
u8 ccs_get_config(const u8 profile, const u8 index) |
2461 |
|
{ |
2462 |
|
u8 config; |
2463 |
|
const struct ccs_profile *p; |
2464 |
|
if (!ccs_policy_loaded) |
2465 |
|
return CCS_CONFIG_DISABLED; |
2466 |
|
p = ccs_profile(profile); |
2467 |
|
config = p->config[index]; |
2468 |
|
if (config == CCS_CONFIG_USE_DEFAULT) |
2469 |
|
config = p->config[ccs_index2category[index] |
2470 |
|
+ CCS_MAX_MAC_INDEX]; |
2471 |
|
if (config == CCS_CONFIG_USE_DEFAULT) |
2472 |
|
config = p->default_config; |
2473 |
|
return config; |
2474 |
|
} |
2475 |
|
|
2476 |
/** |
/** |
2477 |
* ccs_find_yesno - Find values for specified keyword. |
* ccs_find_yesno - Find values for specified keyword. |
2478 |
* |
* |
2511 |
} |
} |
2512 |
|
|
2513 |
/** |
/** |
2514 |
|
* ccs_str_starts - Check whether the given string starts with the given keyword. |
2515 |
|
* |
2516 |
|
* @src: Pointer to pointer to the string. |
2517 |
|
* @find: Pointer to the keyword. |
2518 |
|
* |
2519 |
|
* Returns true if @src starts with @find, false otherwise. |
2520 |
|
* |
2521 |
|
* The @src is updated to point the first character after the @find |
2522 |
|
* if @src starts with @find. |
2523 |
|
*/ |
2524 |
|
static bool ccs_str_starts(char **src, const char *find) |
2525 |
|
{ |
2526 |
|
const int len = strlen(find); |
2527 |
|
char *tmp = *src; |
2528 |
|
if (strncmp(tmp, find, len)) |
2529 |
|
return false; |
2530 |
|
tmp += len; |
2531 |
|
*src = tmp; |
2532 |
|
return true; |
2533 |
|
} |
2534 |
|
|
2535 |
|
/** |
2536 |
* ccs_set_mode - Set mode for specified profile. |
* ccs_set_mode - Set mode for specified profile. |
2537 |
* |
* |
2538 |
* @name: Name of functionality. |
* @name: Name of functionality. |
2771 |
} |
} |
2772 |
|
|
2773 |
/** |
/** |
2774 |
* ccs_same_manager - Check for duplicated "struct ccs_manager" entry. |
* ccs_update_policy - Update an entry for exception policy. |
2775 |
* |
* |
2776 |
* @a: Pointer to "struct ccs_acl_head". |
* @size: Size of new entry in bytes. |
2777 |
* @b: Pointer to "struct ccs_acl_head". |
* @param: Pointer to "struct ccs_acl_param". |
2778 |
* |
* |
2779 |
* Returns true if @a == @b, false otherwise. |
* Returns 0 on success, negative value otherwise. |
2780 |
|
* |
2781 |
|
* Caller holds ccs_read_lock(). |
2782 |
*/ |
*/ |
2783 |
static bool ccs_same_manager(const struct ccs_acl_head *a, |
static int ccs_update_policy(const int size, struct ccs_acl_param *param) |
|
const struct ccs_acl_head *b) |
|
2784 |
{ |
{ |
2785 |
return container_of(a, struct ccs_manager, head)->manager |
struct ccs_acl_head *new_entry = ¶m->e.acl_head; |
2786 |
== container_of(b, struct ccs_manager, head)->manager; |
int error = param->is_delete ? -ENOENT : -ENOMEM; |
2787 |
|
struct ccs_acl_head *entry; |
2788 |
|
struct list_head *list = param->list; |
2789 |
|
BUG_ON(size < sizeof(*entry)); |
2790 |
|
if (mutex_lock_interruptible(&ccs_policy_lock)) |
2791 |
|
return -ENOMEM; |
2792 |
|
list_for_each_entry_srcu(entry, list, list, &ccs_ss) { |
2793 |
|
if (entry->is_deleted == CCS_GC_IN_PROGRESS) |
2794 |
|
continue; |
2795 |
|
if (memcmp(entry + 1, new_entry + 1, size - sizeof(*entry))) |
2796 |
|
continue; |
2797 |
|
entry->is_deleted = param->is_delete; |
2798 |
|
error = 0; |
2799 |
|
break; |
2800 |
|
} |
2801 |
|
if (error && !param->is_delete) { |
2802 |
|
entry = ccs_commit_ok(new_entry, size); |
2803 |
|
if (entry) { |
2804 |
|
list_add_tail_rcu(&entry->list, list); |
2805 |
|
error = 0; |
2806 |
|
} |
2807 |
|
} |
2808 |
|
mutex_unlock(&ccs_policy_lock); |
2809 |
|
return error; |
2810 |
} |
} |
2811 |
|
|
2812 |
/** |
/** |
2817 |
* |
* |
2818 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
2819 |
*/ |
*/ |
2820 |
static inline int ccs_update_manager_entry(const char *manager, |
static int ccs_update_manager_entry(const char *manager, |
2821 |
const bool is_delete) |
const bool is_delete) |
2822 |
{ |
{ |
|
struct ccs_manager e = { }; |
|
2823 |
struct ccs_acl_param param = { |
struct ccs_acl_param param = { |
2824 |
/* .ns = &ccs_kernel_namespace, */ |
/* .ns = &ccs_kernel_namespace, */ |
2825 |
.is_delete = is_delete, |
.is_delete = is_delete, |
2826 |
.list = &ccs_kernel_namespace.policy_list[CCS_ID_MANAGER], |
.list = &ccs_kernel_namespace.policy_list[CCS_ID_MANAGER], |
2827 |
}; |
}; |
2828 |
|
struct ccs_manager *e = ¶m.e.manager; |
2829 |
int error = is_delete ? -ENOENT : -ENOMEM; |
int error = is_delete ? -ENOENT : -ENOMEM; |
2830 |
|
/* Forced zero clear for using memcmp() at ccs_update_policy(). */ |
2831 |
|
memset(¶m.e, 0, sizeof(param.e)); |
2832 |
if (ccs_domain_def(manager)) { |
if (ccs_domain_def(manager)) { |
2833 |
if (!ccs_correct_domain(manager)) |
if (!ccs_correct_domain(manager)) |
2834 |
return -EINVAL; |
return -EINVAL; |
2835 |
e.is_domain = true; |
e->is_domain = true; |
2836 |
} else { |
} else { |
2837 |
if (!ccs_correct_path(manager)) |
if (!ccs_correct_path(manager)) |
2838 |
return -EINVAL; |
return -EINVAL; |
2839 |
} |
} |
2840 |
e.manager = ccs_get_name(manager); |
e->manager = ccs_get_name(manager); |
2841 |
if (e.manager) { |
if (e->manager) { |
2842 |
error = ccs_update_policy(&e.head, sizeof(e), ¶m, |
error = ccs_update_policy(sizeof(*e), ¶m); |
2843 |
ccs_same_manager); |
ccs_put_name(e->manager); |
|
ccs_put_name(e.manager); |
|
2844 |
} |
} |
2845 |
return error; |
return error; |
2846 |
} |
} |
2943 |
} |
} |
2944 |
|
|
2945 |
/** |
/** |
2946 |
|
* ccs_find_domain - Find a domain by the given name. |
2947 |
|
* |
2948 |
|
* @domainname: The domainname to find. |
2949 |
|
* |
2950 |
|
* Returns pointer to "struct ccs_domain_info" if found, NULL otherwise. |
2951 |
|
* |
2952 |
|
* Caller holds ccs_read_lock(). |
2953 |
|
*/ |
2954 |
|
static struct ccs_domain_info *ccs_find_domain(const char *domainname) |
2955 |
|
{ |
2956 |
|
struct ccs_domain_info *domain; |
2957 |
|
struct ccs_path_info name; |
2958 |
|
name.name = domainname; |
2959 |
|
ccs_fill_path_info(&name); |
2960 |
|
list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) { |
2961 |
|
if (!domain->is_deleted && |
2962 |
|
!ccs_pathcmp(&name, domain->domainname)) |
2963 |
|
return domain; |
2964 |
|
} |
2965 |
|
return NULL; |
2966 |
|
} |
2967 |
|
|
2968 |
|
/** |
2969 |
* ccs_select_domain - Parse select command. |
* ccs_select_domain - Parse select command. |
2970 |
* |
* |
2971 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
3002 |
} else if (!strncmp(data, "domain=", 7)) { |
} else if (!strncmp(data, "domain=", 7)) { |
3003 |
if (*(data + 7) == '<') |
if (*(data + 7) == '<') |
3004 |
domain = ccs_find_domain(data + 7); |
domain = ccs_find_domain(data + 7); |
3005 |
|
} else if (sscanf(data, "Q=%u", &pid) == 1) { |
3006 |
|
domain = ccs_find_domain_by_qid(pid); |
3007 |
} else |
} else |
3008 |
return false; |
return false; |
3009 |
head->w.domain = domain; |
head->w.domain = domain; |
3023 |
} |
} |
3024 |
|
|
3025 |
/** |
/** |
3026 |
* ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry. |
* ccs_update_domain - Update an entry for domain policy. |
3027 |
|
* |
3028 |
|
* @size: Size of new entry in bytes. |
3029 |
|
* @param: Pointer to "struct ccs_acl_param". |
3030 |
* |
* |
3031 |
* @a: Pointer to "struct ccs_acl_info". |
* Returns 0 on success, negative value otherwise. |
|
* @b: Pointer to "struct ccs_acl_info". |
|
3032 |
* |
* |
3033 |
* Returns true if @a == @b, false otherwise. |
* Caller holds ccs_read_lock(). |
3034 |
*/ |
*/ |
3035 |
static bool ccs_same_handler_acl(const struct ccs_acl_info *a, |
static int ccs_update_domain(const int size, struct ccs_acl_param *param) |
|
const struct ccs_acl_info *b) |
|
3036 |
{ |
{ |
3037 |
const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head); |
struct ccs_acl_info *new_entry = ¶m->e.acl_info; |
3038 |
const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head); |
const bool is_delete = param->is_delete; |
3039 |
return p1->handler == p2->handler; |
int error = is_delete ? -ENOENT : -ENOMEM; |
3040 |
|
struct ccs_acl_info *entry; |
3041 |
|
struct list_head * const list = param->list; |
3042 |
|
BUG_ON(size < sizeof(*entry)); |
3043 |
|
if (param->data[0]) { |
3044 |
|
new_entry->cond = ccs_get_condition(param); |
3045 |
|
if (!new_entry->cond) |
3046 |
|
return -EINVAL; |
3047 |
|
/* |
3048 |
|
* Domain transition preference is allowed for only |
3049 |
|
* "file execute"/"task auto_execute_handler"/ |
3050 |
|
* "task denied_auto_execute_handler" entries. |
3051 |
|
*/ |
3052 |
|
if (new_entry->cond->exec_transit && |
3053 |
|
!(new_entry->type == CCS_TYPE_PATH_ACL && |
3054 |
|
new_entry->perm == 1 << CCS_TYPE_EXECUTE) |
3055 |
|
#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER |
3056 |
|
&& new_entry->type != CCS_TYPE_AUTO_EXECUTE_HANDLER && |
3057 |
|
new_entry->type != CCS_TYPE_DENIED_EXECUTE_HANDLER |
3058 |
|
#endif |
3059 |
|
) |
3060 |
|
return -EINVAL; |
3061 |
|
} |
3062 |
|
if (mutex_lock_interruptible(&ccs_policy_lock)) |
3063 |
|
return -ENOMEM; |
3064 |
|
list_for_each_entry_srcu(entry, list, list, &ccs_ss) { |
3065 |
|
if (entry->is_deleted == CCS_GC_IN_PROGRESS) |
3066 |
|
continue; |
3067 |
|
if (entry->type != new_entry->type || |
3068 |
|
entry->cond != new_entry->cond || |
3069 |
|
memcmp(entry + 1, new_entry + 1, size - sizeof(*entry))) |
3070 |
|
continue; |
3071 |
|
if (is_delete) |
3072 |
|
entry->perm &= ~new_entry->perm; |
3073 |
|
else |
3074 |
|
entry->perm |= new_entry->perm; |
3075 |
|
entry->is_deleted = !entry->perm; |
3076 |
|
error = 0; |
3077 |
|
break; |
3078 |
|
} |
3079 |
|
if (error && !is_delete) { |
3080 |
|
entry = ccs_commit_ok(new_entry, size); |
3081 |
|
if (entry) { |
3082 |
|
list_add_tail_rcu(&entry->list, list); |
3083 |
|
error = 0; |
3084 |
|
} |
3085 |
|
} |
3086 |
|
mutex_unlock(&ccs_policy_lock); |
3087 |
|
return error; |
3088 |
} |
} |
3089 |
|
|
3090 |
/** |
/** |
3091 |
* ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry. |
* ccs_permstr - Find permission keywords. |
3092 |
* |
* |
3093 |
* @a: Pointer to "struct ccs_acl_info". |
* @string: String representation for permissions in foo/bar/buz format. |
3094 |
* @b: Pointer to "struct ccs_acl_info". |
* @keyword: Keyword to find from @string/ |
3095 |
* |
* |
3096 |
* Returns true if @a == @b, false otherwise. |
* Returns ture if @keyword was found in @string, false otherwise. |
3097 |
|
* |
3098 |
|
* This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2. |
3099 |
*/ |
*/ |
3100 |
static bool ccs_same_task_acl(const struct ccs_acl_info *a, |
static bool ccs_permstr(const char *string, const char *keyword) |
|
const struct ccs_acl_info *b) |
|
3101 |
{ |
{ |
3102 |
const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head); |
const char *cp = strstr(string, keyword); |
3103 |
const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head); |
if (cp) |
3104 |
return p1->domainname == p2->domainname; |
return cp == string || *(cp - 1) == '/'; |
3105 |
|
return false; |
3106 |
} |
} |
3107 |
|
|
3108 |
/** |
/** |
3121 |
"auto_domain_transition "); |
"auto_domain_transition "); |
3122 |
if (!is_auto && !ccs_str_starts(¶m->data, |
if (!is_auto && !ccs_str_starts(¶m->data, |
3123 |
"manual_domain_transition ")) { |
"manual_domain_transition ")) { |
3124 |
struct ccs_handler_acl e = { }; |
#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER |
3125 |
|
struct ccs_handler_acl *e = ¶m->e.handler_acl; |
3126 |
char *handler; |
char *handler; |
3127 |
if (ccs_str_starts(¶m->data, "auto_execute_handler ")) |
if (ccs_str_starts(¶m->data, "auto_execute_handler ")) |
3128 |
e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER; |
e->head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER; |
3129 |
else if (ccs_str_starts(¶m->data, |
else if (ccs_str_starts(¶m->data, |
3130 |
"denied_execute_handler ")) |
"denied_execute_handler ")) |
3131 |
e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER; |
e->head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER; |
3132 |
else |
else |
3133 |
return -EINVAL; |
return -EINVAL; |
3134 |
handler = ccs_read_token(param); |
handler = ccs_read_token(param); |
3135 |
if (!ccs_correct_path(handler)) |
if (!ccs_correct_path(handler)) |
3136 |
return -EINVAL; |
return -EINVAL; |
3137 |
e.handler = ccs_get_name(handler); |
e->handler = ccs_get_name(handler); |
3138 |
if (!e.handler) |
if (!e->handler) |
3139 |
return -ENOMEM; |
return -ENOMEM; |
3140 |
if (e.handler->is_patterned) |
if (e->handler->is_patterned) |
3141 |
error = -EINVAL; /* No patterns allowed. */ |
return -EINVAL; /* No patterns allowed. */ |
3142 |
else |
return ccs_update_domain(sizeof(*e), param); |
3143 |
error = ccs_update_domain(&e.head, sizeof(e), param, |
#else |
3144 |
ccs_same_handler_acl, NULL); |
error = -EINVAL; |
3145 |
ccs_put_name(e.handler); |
#endif |
3146 |
} else { |
} else { |
3147 |
struct ccs_task_acl e = { |
#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION |
3148 |
.head.type = is_auto ? |
struct ccs_task_acl *e = ¶m->e.task_acl; |
3149 |
CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL, |
e->head.type = is_auto ? |
3150 |
.domainname = ccs_get_domainname(param), |
CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL; |
3151 |
}; |
e->domainname = ccs_get_domainname(param); |
3152 |
if (!e.domainname) |
if (!e->domainname) |
3153 |
error = -EINVAL; |
return -EINVAL; |
3154 |
else |
return ccs_update_domain(sizeof(*e), param); |
3155 |
error = ccs_update_domain(&e.head, sizeof(e), param, |
#else |
3156 |
ccs_same_task_acl, NULL); |
error = -EINVAL; |
3157 |
ccs_put_name(e.domainname); |
#endif |
3158 |
} |
} |
3159 |
return error; |
return error; |
3160 |
} |
} |
3161 |
|
|
3162 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
3163 |
|
|
3164 |
|
/** |
3165 |
|
* ccs_write_inet_network - Write "struct ccs_inet_acl" list. |
3166 |
|
* |
3167 |
|
* @param: Pointer to "struct ccs_acl_param". |
3168 |
|
* |
3169 |
|
* Returns 0 on success, negative value otherwise. |
3170 |
|
* |
3171 |
|
* Caller holds ccs_read_lock(). |
3172 |
|
*/ |
3173 |
|
static int ccs_write_inet_network(struct ccs_acl_param *param) |
3174 |
|
{ |
3175 |
|
struct ccs_inet_acl *e = ¶m->e.inet_acl; |
3176 |
|
u8 type; |
3177 |
|
const char *protocol = ccs_read_token(param); |
3178 |
|
const char *operation = ccs_read_token(param); |
3179 |
|
e->head.type = CCS_TYPE_INET_ACL; |
3180 |
|
for (type = 0; type < CCS_SOCK_MAX; type++) |
3181 |
|
if (!strcmp(protocol, ccs_proto_keyword[type])) |
3182 |
|
break; |
3183 |
|
if (type == CCS_SOCK_MAX) |
3184 |
|
return -EINVAL; |
3185 |
|
e->protocol = type; |
3186 |
|
e->head.perm = 0; |
3187 |
|
for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++) |
3188 |
|
if (ccs_permstr(operation, ccs_socket_keyword[type])) |
3189 |
|
e->head.perm |= 1 << type; |
3190 |
|
if (!e->head.perm) |
3191 |
|
return -EINVAL; |
3192 |
|
if (param->data[0] == '@') { |
3193 |
|
param->data++; |
3194 |
|
e->address.group = ccs_get_group(param, CCS_ADDRESS_GROUP); |
3195 |
|
if (!e->address.group) |
3196 |
|
return -ENOMEM; |
3197 |
|
} else { |
3198 |
|
if (!ccs_parse_ipaddr_union(param, &e->address)) |
3199 |
|
return -EINVAL; |
3200 |
|
} |
3201 |
|
if (!ccs_parse_number_union(param, &e->port) || |
3202 |
|
e->port.values[1] > 65535) |
3203 |
|
return -EINVAL; |
3204 |
|
return ccs_update_domain(sizeof(*e), param); |
3205 |
|
} |
3206 |
|
|
3207 |
|
/** |
3208 |
|
* ccs_write_unix_network - Write "struct ccs_unix_acl" list. |
3209 |
|
* |
3210 |
|
* @param: Pointer to "struct ccs_acl_param". |
3211 |
|
* |
3212 |
|
* Returns 0 on success, negative value otherwise. |
3213 |
|
*/ |
3214 |
|
static int ccs_write_unix_network(struct ccs_acl_param *param) |
3215 |
|
{ |
3216 |
|
struct ccs_unix_acl *e = ¶m->e.unix_acl; |
3217 |
|
u8 type; |
3218 |
|
const char *protocol = ccs_read_token(param); |
3219 |
|
const char *operation = ccs_read_token(param); |
3220 |
|
e->head.type = CCS_TYPE_UNIX_ACL; |
3221 |
|
for (type = 0; type < CCS_SOCK_MAX; type++) |
3222 |
|
if (!strcmp(protocol, ccs_proto_keyword[type])) |
3223 |
|
break; |
3224 |
|
if (type == CCS_SOCK_MAX) |
3225 |
|
return -EINVAL; |
3226 |
|
e->protocol = type; |
3227 |
|
e->head.perm = 0; |
3228 |
|
for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++) |
3229 |
|
if (ccs_permstr(operation, ccs_socket_keyword[type])) |
3230 |
|
e->head.perm |= 1 << type; |
3231 |
|
if (!e->head.perm) |
3232 |
|
return -EINVAL; |
3233 |
|
if (!ccs_parse_name_union(param, &e->name)) |
3234 |
|
return -EINVAL; |
3235 |
|
return ccs_update_domain(sizeof(*e), param); |
3236 |
|
} |
3237 |
|
|
3238 |
|
#endif |
3239 |
|
|
3240 |
|
/** |
3241 |
|
* ccs_write_file - Update file related list. |
3242 |
|
* |
3243 |
|
* @param: Pointer to "struct ccs_acl_param". |
3244 |
|
* |
3245 |
|
* Returns 0 on success, negative value otherwise. |
3246 |
|
* |
3247 |
|
* Caller holds ccs_read_lock(). |
3248 |
|
*/ |
3249 |
|
static int ccs_write_file(struct ccs_acl_param *param) |
3250 |
|
{ |
3251 |
|
u16 perm = 0; |
3252 |
|
u8 type; |
3253 |
|
const char *operation = ccs_read_token(param); |
3254 |
|
for (type = 0; type < CCS_MAX_PATH_OPERATION; type++) |
3255 |
|
if (ccs_permstr(operation, ccs_path_keyword[type])) |
3256 |
|
perm |= 1 << type; |
3257 |
|
if (perm) { |
3258 |
|
struct ccs_path_acl *e = ¶m->e.path_acl; |
3259 |
|
e->head.type = CCS_TYPE_PATH_ACL; |
3260 |
|
e->head.perm = perm; |
3261 |
|
if (!ccs_parse_name_union(param, &e->name)) |
3262 |
|
return -EINVAL; |
3263 |
|
return ccs_update_domain(sizeof(*e), param); |
3264 |
|
} |
3265 |
|
for (type = 0; type < CCS_MAX_PATH2_OPERATION; type++) |
3266 |
|
if (ccs_permstr(operation, ccs_mac_keywords[ccs_pp2mac[type]])) |
3267 |
|
perm |= 1 << type; |
3268 |
|
if (perm) { |
3269 |
|
struct ccs_path2_acl *e = ¶m->e.path2_acl; |
3270 |
|
e->head.type = CCS_TYPE_PATH2_ACL; |
3271 |
|
e->head.perm = perm; |
3272 |
|
if (!ccs_parse_name_union(param, &e->name1) || |
3273 |
|
!ccs_parse_name_union(param, &e->name2)) |
3274 |
|
return -EINVAL; |
3275 |
|
return ccs_update_domain(sizeof(*e), param); |
3276 |
|
} |
3277 |
|
for (type = 0; type < CCS_MAX_PATH_NUMBER_OPERATION; type++) |
3278 |
|
if (ccs_permstr(operation, ccs_mac_keywords[ccs_pn2mac[type]])) |
3279 |
|
perm |= 1 << type; |
3280 |
|
if (perm) { |
3281 |
|
struct ccs_path_number_acl *e = ¶m->e.path_number_acl; |
3282 |
|
e->head.type = CCS_TYPE_PATH_NUMBER_ACL; |
3283 |
|
e->head.perm = perm; |
3284 |
|
if (!ccs_parse_name_union(param, &e->name) || |
3285 |
|
!ccs_parse_number_union(param, &e->number)) |
3286 |
|
return -EINVAL; |
3287 |
|
return ccs_update_domain(sizeof(*e), param); |
3288 |
|
} |
3289 |
|
for (type = 0; type < CCS_MAX_MKDEV_OPERATION; type++) |
3290 |
|
if (ccs_permstr(operation, |
3291 |
|
ccs_mac_keywords[ccs_pnnn2mac[type]])) |
3292 |
|
perm |= 1 << type; |
3293 |
|
if (perm) { |
3294 |
|
struct ccs_mkdev_acl *e = ¶m->e.mkdev_acl; |
3295 |
|
e->head.type = CCS_TYPE_MKDEV_ACL; |
3296 |
|
e->head.perm = perm; |
3297 |
|
if (!ccs_parse_name_union(param, &e->name) || |
3298 |
|
!ccs_parse_number_union(param, &e->mode) || |
3299 |
|
!ccs_parse_number_union(param, &e->major) || |
3300 |
|
!ccs_parse_number_union(param, &e->minor)) |
3301 |
|
return -EINVAL; |
3302 |
|
return ccs_update_domain(sizeof(*e), param); |
3303 |
|
} |
3304 |
|
if (ccs_permstr(operation, ccs_mac_keywords[CCS_MAC_FILE_MOUNT])) { |
3305 |
|
struct ccs_mount_acl *e = ¶m->e.mount_acl; |
3306 |
|
e->head.type = CCS_TYPE_MOUNT_ACL; |
3307 |
|
if (!ccs_parse_name_union(param, &e->dev_name) || |
3308 |
|
!ccs_parse_name_union(param, &e->dir_name) || |
3309 |
|
!ccs_parse_name_union(param, &e->fs_type) || |
3310 |
|
!ccs_parse_number_union(param, &e->flags)) |
3311 |
|
return -EINVAL; |
3312 |
|
return ccs_update_domain(sizeof(*e), param); |
3313 |
|
} |
3314 |
|
return -EINVAL; |
3315 |
|
} |
3316 |
|
|
3317 |
|
#ifdef CONFIG_CCSECURITY_MISC |
3318 |
|
|
3319 |
|
/** |
3320 |
|
* ccs_write_misc - Update environment variable list. |
3321 |
|
* |
3322 |
|
* @param: Pointer to "struct ccs_acl_param". |
3323 |
|
* |
3324 |
|
* Returns 0 on success, negative value otherwise. |
3325 |
|
*/ |
3326 |
|
static int ccs_write_misc(struct ccs_acl_param *param) |
3327 |
|
{ |
3328 |
|
if (ccs_str_starts(¶m->data, "env ")) { |
3329 |
|
struct ccs_env_acl *e = ¶m->e.env_acl; |
3330 |
|
const char *data = ccs_read_token(param); |
3331 |
|
e->head.type = CCS_TYPE_ENV_ACL; |
3332 |
|
if (!ccs_correct_word(data) || strchr(data, '=')) |
3333 |
|
return -EINVAL; |
3334 |
|
e->env = ccs_get_name(data); |
3335 |
|
if (!e->env) |
3336 |
|
return -ENOMEM; |
3337 |
|
return ccs_update_domain(sizeof(*e), param); |
3338 |
|
} |
3339 |
|
return -EINVAL; |
3340 |
|
} |
3341 |
|
|
3342 |
|
#endif |
3343 |
|
|
3344 |
|
#ifdef CONFIG_CCSECURITY_IPC |
3345 |
|
|
3346 |
|
/** |
3347 |
|
* ccs_write_ipc - Update "struct ccs_signal_acl" list. |
3348 |
|
* |
3349 |
|
* @param: Pointer to "struct ccs_acl_param". |
3350 |
|
* |
3351 |
|
* Returns 0 on success, negative value otherwise. |
3352 |
|
*/ |
3353 |
|
static int ccs_write_ipc(struct ccs_acl_param *param) |
3354 |
|
{ |
3355 |
|
struct ccs_signal_acl *e = ¶m->e.signal_acl; |
3356 |
|
e->head.type = CCS_TYPE_SIGNAL_ACL; |
3357 |
|
if (!ccs_parse_number_union(param, &e->sig)) |
3358 |
|
return -EINVAL; |
3359 |
|
e->domainname = ccs_get_domainname(param); |
3360 |
|
if (!e->domainname) |
3361 |
|
return -EINVAL; |
3362 |
|
return ccs_update_domain(sizeof(*e), param); |
3363 |
|
} |
3364 |
|
|
3365 |
|
#endif |
3366 |
|
|
3367 |
|
#ifdef CONFIG_CCSECURITY_CAPABILITY |
3368 |
|
|
3369 |
|
/** |
3370 |
|
* ccs_write_capability - Write "struct ccs_capability_acl" list. |
3371 |
|
* |
3372 |
|
* @param: Pointer to "struct ccs_acl_param". |
3373 |
|
* |
3374 |
|
* Returns 0 on success, negative value otherwise. |
3375 |
|
* |
3376 |
|
* Caller holds ccs_read_lock(). |
3377 |
|
*/ |
3378 |
|
static int ccs_write_capability(struct ccs_acl_param *param) |
3379 |
|
{ |
3380 |
|
struct ccs_capability_acl *e = ¶m->e.capability_acl; |
3381 |
|
const char *operation = ccs_read_token(param); |
3382 |
|
u8 type; |
3383 |
|
e->head.type = CCS_TYPE_CAPABILITY_ACL; |
3384 |
|
for (type = 0; type < CCS_MAX_CAPABILITY_INDEX; type++) { |
3385 |
|
if (strcmp(operation, ccs_mac_keywords[ccs_c2mac[type]])) |
3386 |
|
continue; |
3387 |
|
e->operation = type; |
3388 |
|
return ccs_update_domain(sizeof(*e), param); |
3389 |
|
} |
3390 |
|
return -EINVAL; |
3391 |
|
} |
3392 |
|
|
3393 |
|
#endif |
3394 |
|
|
3395 |
/** |
/** |
3396 |
* ccs_write_domain2 - Write domain policy. |
* ccs_write_domain2 - Write domain policy. |
3397 |
* |
* |
3417 |
static const struct { |
static const struct { |
3418 |
const char *keyword; |
const char *keyword; |
3419 |
int (*write) (struct ccs_acl_param *); |
int (*write) (struct ccs_acl_param *); |
3420 |
} ccs_callback[7] = { |
} ccs_callback[] = { |
3421 |
{ "file ", ccs_write_file }, |
{ "file ", ccs_write_file }, |
3422 |
|
#ifdef CONFIG_CCSECURITY_NETWORK |
3423 |
{ "network inet ", ccs_write_inet_network }, |
{ "network inet ", ccs_write_inet_network }, |
3424 |
{ "network unix ", ccs_write_unix_network }, |
{ "network unix ", ccs_write_unix_network }, |
3425 |
|
#endif |
3426 |
|
#ifdef CONFIG_CCSECURITY_MISC |
3427 |
{ "misc ", ccs_write_misc }, |
{ "misc ", ccs_write_misc }, |
3428 |
|
#endif |
3429 |
|
#ifdef CONFIG_CCSECURITY_CAPABILITY |
3430 |
{ "capability ", ccs_write_capability }, |
{ "capability ", ccs_write_capability }, |
3431 |
|
#endif |
3432 |
|
#ifdef CONFIG_CCSECURITY_IPC |
3433 |
{ "ipc signal ", ccs_write_ipc }, |
{ "ipc signal ", ccs_write_ipc }, |
3434 |
|
#endif |
3435 |
{ "task ", ccs_write_task }, |
{ "task ", ccs_write_task }, |
3436 |
}; |
}; |
3437 |
u8 i; |
u8 i; |
3438 |
|
/* Forced zero clear for using memcmp() at ccs_update_domain(). */ |
3439 |
|
memset(¶m.e, 0, sizeof(param.e)); |
3440 |
|
param.e.acl_info.perm = 1; |
3441 |
for (i = 0; i < ARRAY_SIZE(ccs_callback); i++) { |
for (i = 0; i < ARRAY_SIZE(ccs_callback); i++) { |
3442 |
|
int error; |
3443 |
if (!ccs_str_starts(¶m.data, ccs_callback[i].keyword)) |
if (!ccs_str_starts(¶m.data, ccs_callback[i].keyword)) |
3444 |
continue; |
continue; |
3445 |
return ccs_callback[i].write(¶m); |
error = ccs_callback[i].write(¶m); |
3446 |
|
ccs_del_acl(¶m.e.acl_info.list); |
3447 |
|
return error; |
3448 |
} |
} |
3449 |
return -EINVAL; |
return -EINVAL; |
3450 |
} |
} |
3479 |
return 0; |
return 0; |
3480 |
} |
} |
3481 |
|
|
|
/* String table for domain flags. */ |
|
|
const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = { |
|
|
[CCS_DIF_QUOTA_WARNED] = "quota_exceeded\n", |
|
|
[CCS_DIF_TRANSITION_FAILED] = "transition_failed\n", |
|
|
}; |
|
|
|
|
3482 |
/** |
/** |
3483 |
* ccs_write_domain - Write domain policy. |
* ccs_write_domain - Write domain policy. |
3484 |
* |
* |
3821 |
else if (acl_type == CCS_TYPE_PATH_ACL) { |
else if (acl_type == CCS_TYPE_PATH_ACL) { |
3822 |
struct ccs_path_acl *ptr |
struct ccs_path_acl *ptr |
3823 |
= container_of(acl, typeof(*ptr), head); |
= container_of(acl, typeof(*ptr), head); |
|
const u16 perm = ptr->perm; |
|
3824 |
for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) { |
for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) { |
3825 |
if (!(perm & (1 << bit))) |
if (!(acl->perm & (1 << bit))) |
3826 |
continue; |
continue; |
3827 |
if (head->r.print_transition_related_only && |
if (head->r.print_transition_related_only && |
3828 |
bit != CCS_TYPE_EXECUTE && !may_trigger_transition) |
bit != CCS_TYPE_EXECUTE && !may_trigger_transition) |
3838 |
if (first) |
if (first) |
3839 |
return true; |
return true; |
3840 |
ccs_print_name_union(head, &ptr->name); |
ccs_print_name_union(head, &ptr->name); |
3841 |
|
#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER |
3842 |
} else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER || |
} else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER || |
3843 |
acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) { |
acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) { |
3844 |
struct ccs_handler_acl *ptr |
struct ccs_handler_acl *ptr |
3848 |
? "auto_execute_handler " : |
? "auto_execute_handler " : |
3849 |
"denied_execute_handler "); |
"denied_execute_handler "); |
3850 |
ccs_set_string(head, ptr->handler->name); |
ccs_set_string(head, ptr->handler->name); |
3851 |
|
#endif |
3852 |
|
#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION |
3853 |
} else if (acl_type == CCS_TYPE_AUTO_TASK_ACL || |
} else if (acl_type == CCS_TYPE_AUTO_TASK_ACL || |
|