1 |
/* |
2 |
* security/ccsecurity/policy_io.c |
3 |
* |
4 |
* Copyright (C) 2005-2010 NTT DATA CORPORATION |
5 |
* |
6 |
* Version: 1.8.0-pre 2010/10/10 |
7 |
* |
8 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
9 |
* See README.ccs for ChangeLog. |
10 |
* |
11 |
*/ |
12 |
|
13 |
#include "internal.h" |
14 |
|
15 |
/* Profile version. Currently only 20100903 is defined. */ |
16 |
static unsigned int ccs_profile_version; |
17 |
|
18 |
/* Profile table. Memory is allocated as needed. */ |
19 |
static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES]; |
20 |
|
21 |
/* String table for functionality that takes 4 modes. */ |
22 |
const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = { |
23 |
[CCS_CONFIG_DISABLED] = "disabled", |
24 |
[CCS_CONFIG_LEARNING] = "learning", |
25 |
[CCS_CONFIG_PERMISSIVE] = "permissive", |
26 |
[CCS_CONFIG_ENFORCING] = "enforcing" |
27 |
}; |
28 |
|
29 |
/* String table for /proc/ccs/profile */ |
30 |
const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX |
31 |
+ CCS_MAX_MAC_CATEGORY_INDEX] = { |
32 |
[CCS_MAC_FILE_EXECUTE] = "execute", |
33 |
[CCS_MAC_FILE_OPEN] = "open", |
34 |
[CCS_MAC_FILE_CREATE] = "create", |
35 |
[CCS_MAC_FILE_UNLINK] = "unlink", |
36 |
[CCS_MAC_FILE_MKDIR] = "mkdir", |
37 |
[CCS_MAC_FILE_RMDIR] = "rmdir", |
38 |
[CCS_MAC_FILE_MKFIFO] = "mkfifo", |
39 |
[CCS_MAC_FILE_MKSOCK] = "mksock", |
40 |
[CCS_MAC_FILE_TRUNCATE] = "truncate", |
41 |
[CCS_MAC_FILE_SYMLINK] = "symlink", |
42 |
[CCS_MAC_FILE_MKBLOCK] = "mkblock", |
43 |
[CCS_MAC_FILE_MKCHAR] = "mkchar", |
44 |
[CCS_MAC_FILE_LINK] = "link", |
45 |
[CCS_MAC_FILE_RENAME] = "rename", |
46 |
[CCS_MAC_FILE_CHMOD] = "chmod", |
47 |
[CCS_MAC_FILE_CHOWN] = "chown", |
48 |
[CCS_MAC_FILE_CHGRP] = "chgrp", |
49 |
[CCS_MAC_FILE_IOCTL] = "ioctl", |
50 |
[CCS_MAC_FILE_CHROOT] = "chroot", |
51 |
[CCS_MAC_FILE_MOUNT] = "mount", |
52 |
[CCS_MAC_FILE_UMOUNT] = "unmount", |
53 |
[CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root", |
54 |
[CCS_MAC_ENVIRON] = "env", |
55 |
[CCS_MAC_NETWORK_INET_STREAM_BIND] = "inet_stream_bind", |
56 |
[CCS_MAC_NETWORK_INET_STREAM_LISTEN] = "inet_stream_listen", |
57 |
[CCS_MAC_NETWORK_INET_STREAM_CONNECT] = "inet_stream_connect", |
58 |
[CCS_MAC_NETWORK_INET_STREAM_ACCEPT] = "inet_stream_accept", |
59 |
[CCS_MAC_NETWORK_INET_DGRAM_BIND] = "inet_dgram_bind", |
60 |
[CCS_MAC_NETWORK_INET_DGRAM_SEND] = "inet_dgram_send", |
61 |
[CCS_MAC_NETWORK_INET_DGRAM_RECV] = "inet_dgram_recv", |
62 |
[CCS_MAC_NETWORK_INET_RAW_BIND] = "inet_raw_bind", |
63 |
[CCS_MAC_NETWORK_INET_RAW_SEND] = "inet_raw_send", |
64 |
[CCS_MAC_NETWORK_INET_RAW_RECV] = "inet_raw_recv", |
65 |
[CCS_MAC_NETWORK_UNIX_STREAM_BIND] = "unix_stream_bind", |
66 |
[CCS_MAC_NETWORK_UNIX_STREAM_LISTEN] = "unix_stream_listen", |
67 |
[CCS_MAC_NETWORK_UNIX_STREAM_CONNECT] = "unix_stream_connect", |
68 |
[CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT] = "unix_stream_accept", |
69 |
[CCS_MAC_NETWORK_UNIX_DGRAM_BIND] = "unix_dgram_bind", |
70 |
[CCS_MAC_NETWORK_UNIX_DGRAM_SEND] = "unix_dgram_send", |
71 |
[CCS_MAC_NETWORK_UNIX_DGRAM_RECV] = "unix_dgram_recv", |
72 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND] = "unix_seqpacket_bind", |
73 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = "unix_seqpacket_listen", |
74 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect", |
75 |
[CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT] = "unix_seqpacket_accept", |
76 |
[CCS_MAC_SIGNAL] = "signal", |
77 |
[CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET] = "use_route", |
78 |
[CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet", |
79 |
[CCS_MAC_CAPABILITY_SYS_REBOOT] = "SYS_REBOOT", |
80 |
[CCS_MAC_CAPABILITY_SYS_VHANGUP] = "SYS_VHANGUP", |
81 |
[CCS_MAC_CAPABILITY_SYS_SETTIME] = "SYS_TIME", |
82 |
[CCS_MAC_CAPABILITY_SYS_NICE] = "SYS_NICE", |
83 |
[CCS_MAC_CAPABILITY_SYS_SETHOSTNAME] = "SYS_SETHOSTNAME", |
84 |
[CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module", |
85 |
[CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD", |
86 |
[CCS_MAC_CAPABILITY_SYS_PTRACE] = "SYS_PTRACE", |
87 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE] = "file", |
88 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK] = "network", |
89 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC] = "misc", |
90 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC] = "ipc", |
91 |
[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability", |
92 |
}; |
93 |
|
94 |
const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = { |
95 |
[CCS_TYPE_EXECUTE] = "execute", |
96 |
[CCS_TYPE_READ] = "read", |
97 |
[CCS_TYPE_WRITE] = "write", |
98 |
[CCS_TYPE_APPEND] = "append", |
99 |
[CCS_TYPE_UNLINK] = "unlink", |
100 |
[CCS_TYPE_RMDIR] = "rmdir", |
101 |
[CCS_TYPE_TRUNCATE] = "truncate", |
102 |
[CCS_TYPE_SYMLINK] = "symlink", |
103 |
[CCS_TYPE_CHROOT] = "chroot", |
104 |
[CCS_TYPE_UMOUNT] = "unmount", |
105 |
}; |
106 |
|
107 |
static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = { |
108 |
[CCS_MAC_CATEGORY_FILE] = "file", |
109 |
[CCS_MAC_CATEGORY_NETWORK] = "network", |
110 |
[CCS_MAC_CATEGORY_MISC] = "misc", |
111 |
[CCS_MAC_CATEGORY_IPC] = "ipc", |
112 |
[CCS_MAC_CATEGORY_CAPABILITY] = "capability", |
113 |
}; |
114 |
|
115 |
const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = { |
116 |
[CCS_TASK_UID] = "task.uid", |
117 |
[CCS_TASK_EUID] = "task.euid", |
118 |
[CCS_TASK_SUID] = "task.suid", |
119 |
[CCS_TASK_FSUID] = "task.fsuid", |
120 |
[CCS_TASK_GID] = "task.gid", |
121 |
[CCS_TASK_EGID] = "task.egid", |
122 |
[CCS_TASK_SGID] = "task.sgid", |
123 |
[CCS_TASK_FSGID] = "task.fsgid", |
124 |
[CCS_TASK_PID] = "task.pid", |
125 |
[CCS_TASK_PPID] = "task.ppid", |
126 |
[CCS_EXEC_ARGC] = "exec.argc", |
127 |
[CCS_EXEC_ENVC] = "exec.envc", |
128 |
[CCS_TYPE_IS_SOCKET] = "socket", |
129 |
[CCS_TYPE_IS_SYMLINK] = "symlink", |
130 |
[CCS_TYPE_IS_FILE] = "file", |
131 |
[CCS_TYPE_IS_BLOCK_DEV] = "block", |
132 |
[CCS_TYPE_IS_DIRECTORY] = "directory", |
133 |
[CCS_TYPE_IS_CHAR_DEV] = "char", |
134 |
[CCS_TYPE_IS_FIFO] = "fifo", |
135 |
[CCS_MODE_SETUID] = "setuid", |
136 |
[CCS_MODE_SETGID] = "setgid", |
137 |
[CCS_MODE_STICKY] = "sticky", |
138 |
[CCS_MODE_OWNER_READ] = "owner_read", |
139 |
[CCS_MODE_OWNER_WRITE] = "owner_write", |
140 |
[CCS_MODE_OWNER_EXECUTE] = "owner_execute", |
141 |
[CCS_MODE_GROUP_READ] = "group_read", |
142 |
[CCS_MODE_GROUP_WRITE] = "group_write", |
143 |
[CCS_MODE_GROUP_EXECUTE] = "group_execute", |
144 |
[CCS_MODE_OTHERS_READ] = "others_read", |
145 |
[CCS_MODE_OTHERS_WRITE] = "others_write", |
146 |
[CCS_MODE_OTHERS_EXECUTE] = "others_execute", |
147 |
[CCS_TASK_TYPE] = "task.type", |
148 |
[CCS_TASK_EXECUTE_HANDLER] = "execute_handler", |
149 |
[CCS_EXEC_REALPATH] = "exec.realpath", |
150 |
[CCS_SYMLINK_TARGET] = "symlink.target", |
151 |
[CCS_PATH1_UID] = "path1.uid", |
152 |
[CCS_PATH1_GID] = "path1.gid", |
153 |
[CCS_PATH1_INO] = "path1.ino", |
154 |
[CCS_PATH1_MAJOR] = "path1.major", |
155 |
[CCS_PATH1_MINOR] = "path1.minor", |
156 |
[CCS_PATH1_PERM] = "path1.perm", |
157 |
[CCS_PATH1_TYPE] = "path1.type", |
158 |
[CCS_PATH1_DEV_MAJOR] = "path1.dev_major", |
159 |
[CCS_PATH1_DEV_MINOR] = "path1.dev_minor", |
160 |
[CCS_PATH2_UID] = "path2.uid", |
161 |
[CCS_PATH2_GID] = "path2.gid", |
162 |
[CCS_PATH2_INO] = "path2.ino", |
163 |
[CCS_PATH2_MAJOR] = "path2.major", |
164 |
[CCS_PATH2_MINOR] = "path2.minor", |
165 |
[CCS_PATH2_PERM] = "path2.perm", |
166 |
[CCS_PATH2_TYPE] = "path2.type", |
167 |
[CCS_PATH2_DEV_MAJOR] = "path2.dev_major", |
168 |
[CCS_PATH2_DEV_MINOR] = "path2.dev_minor", |
169 |
[CCS_PATH1_PARENT_UID] = "path1.parent.uid", |
170 |
[CCS_PATH1_PARENT_GID] = "path1.parent.gid", |
171 |
[CCS_PATH1_PARENT_INO] = "path1.parent.ino", |
172 |
[CCS_PATH1_PARENT_PERM] = "path1.parent.perm", |
173 |
[CCS_PATH2_PARENT_UID] = "path2.parent.uid", |
174 |
[CCS_PATH2_PARENT_GID] = "path2.parent.gid", |
175 |
[CCS_PATH2_PARENT_INO] = "path2.parent.ino", |
176 |
[CCS_PATH2_PARENT_PERM] = "path2.parent.perm", |
177 |
}; |
178 |
|
179 |
static const char * const ccs_pref_keywords[CCS_MAX_PREF] = { |
180 |
[CCS_PREF_MAX_GRANT_LOG] = "max_grant_log", |
181 |
[CCS_PREF_MAX_REJECT_LOG] = "max_reject_log", |
182 |
[CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry", |
183 |
[CCS_PREF_ENFORCING_PENALTY] = "enforcing_penalty", |
184 |
}; |
185 |
|
186 |
/* Permit policy management by non-root user? */ |
187 |
static bool ccs_manage_by_non_root; |
188 |
|
189 |
/** |
190 |
* ccs_yesno - Return "yes" or "no". |
191 |
* |
192 |
* @value: Bool value. |
193 |
*/ |
194 |
static const char *ccs_yesno(const unsigned int value) |
195 |
{ |
196 |
return value ? "yes" : "no"; |
197 |
} |
198 |
|
199 |
static void ccs_addprintf(char *buffer, int len, const char *fmt, ...) |
200 |
__attribute__ ((format(printf, 3, 4))); |
201 |
|
202 |
static void ccs_addprintf(char *buffer, int len, const char *fmt, ...) |
203 |
{ |
204 |
va_list args; |
205 |
const int pos = strlen(buffer); |
206 |
va_start(args, fmt); |
207 |
vsnprintf(buffer + pos, len - pos - 1, fmt, args); |
208 |
va_end(args); |
209 |
} |
210 |
|
211 |
/** |
212 |
* ccs_flush - Flush queued string to userspace's buffer. |
213 |
* |
214 |
* @head: Pointer to "struct ccs_io_buffer". |
215 |
* |
216 |
* Returns true if all data was flushed, false otherwise. |
217 |
*/ |
218 |
static bool ccs_flush(struct ccs_io_buffer *head) |
219 |
{ |
220 |
while (head->r.w_pos) { |
221 |
const char *w = head->r.w[0]; |
222 |
int len = strlen(w); |
223 |
if (len) { |
224 |
if (len > head->read_user_buf_avail) |
225 |
len = head->read_user_buf_avail; |
226 |
if (!len) |
227 |
return false; |
228 |
if (copy_to_user(head->read_user_buf, w, len)) |
229 |
return false; |
230 |
head->read_user_buf_avail -= len; |
231 |
head->read_user_buf += len; |
232 |
w += len; |
233 |
} |
234 |
if (*w) { |
235 |
head->r.w[0] = w; |
236 |
return false; |
237 |
} |
238 |
/* Add '\0' for audit logs and query. */ |
239 |
if (head->poll) { |
240 |
if (!head->read_user_buf_avail || |
241 |
copy_to_user(head->read_user_buf, "", 1)) |
242 |
return false; |
243 |
head->read_user_buf_avail--; |
244 |
head->read_user_buf++; |
245 |
} |
246 |
head->r.w_pos--; |
247 |
for (len = 0; len < head->r.w_pos; len++) |
248 |
head->r.w[len] = head->r.w[len + 1]; |
249 |
} |
250 |
head->r.avail = 0; |
251 |
return true; |
252 |
} |
253 |
|
254 |
/** |
255 |
* ccs_set_string - Queue string to "struct ccs_io_buffer" structure. |
256 |
* |
257 |
* @head: Pointer to "struct ccs_io_buffer". |
258 |
* @string: String to print. |
259 |
* |
260 |
* Note that @string has to be kept valid until @head is kfree()d. |
261 |
* This means that char[] allocated on stack memory cannot be passed to |
262 |
* this function. Use ccs_io_printf() for char[] allocated on stack memory. |
263 |
*/ |
264 |
static void ccs_set_string(struct ccs_io_buffer *head, const char *string) |
265 |
{ |
266 |
if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) { |
267 |
head->r.w[head->r.w_pos++] = string; |
268 |
ccs_flush(head); |
269 |
} else |
270 |
printk(KERN_WARNING "Too many words in a line.\n"); |
271 |
} |
272 |
|
273 |
/** |
274 |
* ccs_io_printf - printf() to "struct ccs_io_buffer" structure. |
275 |
* |
276 |
* @head: Pointer to "struct ccs_io_buffer". |
277 |
* @fmt: The printf()'s format string, followed by parameters. |
278 |
*/ |
279 |
void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...) |
280 |
{ |
281 |
va_list args; |
282 |
int len; |
283 |
int pos = head->r.avail; |
284 |
int size = head->readbuf_size - pos; |
285 |
if (size <= 0) |
286 |
return; |
287 |
va_start(args, fmt); |
288 |
len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1; |
289 |
va_end(args); |
290 |
if (pos + len >= head->readbuf_size) { |
291 |
printk(KERN_WARNING "Too many words in a line.\n"); |
292 |
return; |
293 |
} |
294 |
head->r.avail += len; |
295 |
ccs_set_string(head, head->read_buf + pos); |
296 |
} |
297 |
|
298 |
static void ccs_set_space(struct ccs_io_buffer *head) |
299 |
{ |
300 |
ccs_set_string(head, " "); |
301 |
} |
302 |
|
303 |
static bool ccs_set_lf(struct ccs_io_buffer *head) |
304 |
{ |
305 |
ccs_set_string(head, "\n"); |
306 |
return !head->r.w_pos; |
307 |
} |
308 |
|
309 |
/** |
310 |
* ccs_assign_profile - Create a new profile. |
311 |
* |
312 |
* @profile: Profile number to create. |
313 |
* |
314 |
* Returns pointer to "struct ccs_profile" on success, NULL otherwise. |
315 |
*/ |
316 |
static struct ccs_profile *ccs_assign_profile(const unsigned int profile) |
317 |
{ |
318 |
struct ccs_profile *ptr; |
319 |
struct ccs_profile *entry; |
320 |
if (profile >= CCS_MAX_PROFILES) |
321 |
return NULL; |
322 |
ptr = ccs_profile_ptr[profile]; |
323 |
if (ptr) |
324 |
return ptr; |
325 |
entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS); |
326 |
if (mutex_lock_interruptible(&ccs_policy_lock)) |
327 |
goto out; |
328 |
ptr = ccs_profile_ptr[profile]; |
329 |
if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) { |
330 |
ptr = entry; |
331 |
ptr->default_config = CCS_CONFIG_DISABLED | |
332 |
CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG; |
333 |
memset(ptr->config, CCS_CONFIG_USE_DEFAULT, |
334 |
sizeof(ptr->config)); |
335 |
ptr->pref[CCS_PREF_MAX_GRANT_LOG] = |
336 |
CONFIG_CCSECURITY_MAX_GRANT_LOG; |
337 |
ptr->pref[CCS_PREF_MAX_REJECT_LOG] = |
338 |
CONFIG_CCSECURITY_MAX_REJECT_LOG; |
339 |
ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] = |
340 |
CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY; |
341 |
mb(); /* Avoid out-of-order execution. */ |
342 |
ccs_profile_ptr[profile] = ptr; |
343 |
entry = NULL; |
344 |
} |
345 |
mutex_unlock(&ccs_policy_lock); |
346 |
out: |
347 |
kfree(entry); |
348 |
return ptr; |
349 |
} |
350 |
|
351 |
/** |
352 |
* ccs_check_profile - Check all profiles currently assigned to domains are defined. |
353 |
*/ |
354 |
static void ccs_check_profile(void) |
355 |
{ |
356 |
struct ccs_domain_info *domain; |
357 |
const int idx = ccs_read_lock(); |
358 |
ccs_policy_loaded = true; |
359 |
list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) { |
360 |
const u8 profile = domain->profile; |
361 |
if (ccs_profile_ptr[profile]) |
362 |
continue; |
363 |
printk(KERN_ERR "You need to define profile %u before using it.\n", |
364 |
profile); |
365 |
printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ " |
366 |
"for more information.\n"); |
367 |
panic("Profile %u (used by '%s') not defined.\n", |
368 |
profile, domain->domainname->name); |
369 |
} |
370 |
ccs_read_unlock(idx); |
371 |
if (ccs_profile_version != 20100903) { |
372 |
printk(KERN_ERR "You need to install userland programs for " |
373 |
"TOMOYO 1.8 and initialize policy configuration.\n"); |
374 |
printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ " |
375 |
"for more information.\n"); |
376 |
panic("Profile version %u is not supported.\n", |
377 |
ccs_profile_version); |
378 |
} |
379 |
printk(KERN_INFO "CCSecurity: 1.8.0-pre 2010/10/10\n"); |
380 |
printk(KERN_INFO "Mandatory Access Control activated.\n"); |
381 |
} |
382 |
|
383 |
/** |
384 |
* ccs_profile - Find a profile. |
385 |
* |
386 |
* @profile: Profile number to find. |
387 |
* |
388 |
* Returns pointer to "struct ccs_profile". |
389 |
*/ |
390 |
struct ccs_profile *ccs_profile(const u8 profile) |
391 |
{ |
392 |
static struct ccs_profile ccs_null_profile; |
393 |
struct ccs_profile *ptr = ccs_profile_ptr[profile]; |
394 |
if (!ptr) |
395 |
ptr = &ccs_null_profile; |
396 |
return ptr; |
397 |
} |
398 |
|
399 |
static s8 ccs_find_yesno(const char *string, const char *find) |
400 |
{ |
401 |
const char *cp = strstr(string, find); |
402 |
if (cp) { |
403 |
cp += strlen(find); |
404 |
if (!strncmp(cp, "=yes", 4)) |
405 |
return 1; |
406 |
else if (!strncmp(cp, "=no", 3)) |
407 |
return 0; |
408 |
} |
409 |
return -1; |
410 |
} |
411 |
|
412 |
static void ccs_set_uint(unsigned int *i, const char *string, const char *find) |
413 |
{ |
414 |
const char *cp = strstr(string, find); |
415 |
if (cp) |
416 |
sscanf(cp + strlen(find), "=%u", i); |
417 |
} |
418 |
|
419 |
static int ccs_set_mode(char *name, const char *value, |
420 |
struct ccs_profile *profile) |
421 |
{ |
422 |
u8 i; |
423 |
u8 config; |
424 |
if (!strcmp(name, "CONFIG")) { |
425 |
i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX; |
426 |
config = profile->default_config; |
427 |
} else if (ccs_str_starts(&name, "CONFIG::")) { |
428 |
config = 0; |
429 |
for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX; |
430 |
i++) { |
431 |
int len = 0; |
432 |
if (i < CCS_MAX_MAC_INDEX) { |
433 |
const u8 c = ccs_index2category[i]; |
434 |
const char *category = |
435 |
ccs_category_keywords[c]; |
436 |
len = strlen(category); |
437 |
if (strncmp(name, category, len) || |
438 |
name[len++] != ':' || name[len++] != ':') |
439 |
continue; |
440 |
} |
441 |
if (strcmp(name + len, ccs_mac_keywords[i])) |
442 |
continue; |
443 |
config = profile->config[i]; |
444 |
break; |
445 |
} |
446 |
if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX) |
447 |
return -EINVAL; |
448 |
} else { |
449 |
return -EINVAL; |
450 |
} |
451 |
if (strstr(value, "use_default")) { |
452 |
config = CCS_CONFIG_USE_DEFAULT; |
453 |
} else { |
454 |
u8 mode; |
455 |
for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++) |
456 |
if (strstr(value, ccs_mode[mode])) |
457 |
/* |
458 |
* Update lower 3 bits in order to distinguish |
459 |
* 'config' from 'CCS_CONFIG_USE_DEAFULT'. |
460 |
*/ |
461 |
config = (config & ~7) | mode; |
462 |
if (config != CCS_CONFIG_USE_DEFAULT) { |
463 |
switch (ccs_find_yesno(value, "grant_log")) { |
464 |
case 1: |
465 |
config |= CCS_CONFIG_WANT_GRANT_LOG; |
466 |
break; |
467 |
case 0: |
468 |
config &= ~CCS_CONFIG_WANT_GRANT_LOG; |
469 |
break; |
470 |
} |
471 |
switch (ccs_find_yesno(value, "reject_log")) { |
472 |
case 1: |
473 |
config |= CCS_CONFIG_WANT_REJECT_LOG; |
474 |
break; |
475 |
case 0: |
476 |
config &= ~CCS_CONFIG_WANT_REJECT_LOG; |
477 |
break; |
478 |
} |
479 |
} |
480 |
} |
481 |
if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX) |
482 |
profile->config[i] = config; |
483 |
else if (config != CCS_CONFIG_USE_DEFAULT) |
484 |
profile->default_config = config; |
485 |
return 0; |
486 |
} |
487 |
|
488 |
/** |
489 |
* ccs_write_profile - Write profile table. |
490 |
* |
491 |
* @head: Pointer to "struct ccs_io_buffer". |
492 |
* |
493 |
* Returns 0 on success, negative value otherwise. |
494 |
*/ |
495 |
static int ccs_write_profile(struct ccs_io_buffer *head) |
496 |
{ |
497 |
char *data = head->write_buf; |
498 |
unsigned int i; |
499 |
char *cp; |
500 |
struct ccs_profile *profile; |
501 |
if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1) |
502 |
return 0; |
503 |
i = simple_strtoul(data, &cp, 10); |
504 |
if (*cp != '-') |
505 |
return -EINVAL; |
506 |
data = cp + 1; |
507 |
profile = ccs_assign_profile(i); |
508 |
if (!profile) |
509 |
return -EINVAL; |
510 |
cp = strchr(data, '='); |
511 |
if (!cp) |
512 |
return -EINVAL; |
513 |
*cp++ = '\0'; |
514 |
if (!strcmp(data, "COMMENT")) { |
515 |
const struct ccs_path_info *old_comment = profile->comment; |
516 |
profile->comment = ccs_get_name(cp); |
517 |
ccs_put_name(old_comment); |
518 |
return 0; |
519 |
} |
520 |
if (!strcmp(data, "PREFERENCE")) { |
521 |
for (i = 0; i < CCS_MAX_PREF; i++) |
522 |
ccs_set_uint(&profile->pref[i], cp, |
523 |
ccs_pref_keywords[i]); |
524 |
return 0; |
525 |
} |
526 |
return ccs_set_mode(data, cp, profile); |
527 |
} |
528 |
|
529 |
static void ccs_print_config(struct ccs_io_buffer *head, const u8 config) |
530 |
{ |
531 |
ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n", |
532 |
ccs_mode[config & 3], |
533 |
ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG), |
534 |
ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG)); |
535 |
} |
536 |
|
537 |
/** |
538 |
* ccs_read_profile - Read profile table. |
539 |
* |
540 |
* @head: Pointer to "struct ccs_io_buffer". |
541 |
*/ |
542 |
static void ccs_read_profile(struct ccs_io_buffer *head) |
543 |
{ |
544 |
u8 index; |
545 |
const struct ccs_profile *profile; |
546 |
next: |
547 |
index = head->r.index; |
548 |
profile = ccs_profile_ptr[index]; |
549 |
switch (head->r.step) { |
550 |
case 0: |
551 |
ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20100903"); |
552 |
head->r.step++; |
553 |
break; |
554 |
case 1: |
555 |
for ( ; head->r.index < CCS_MAX_PROFILES; |
556 |
head->r.index++) |
557 |
if (ccs_profile_ptr[head->r.index]) |
558 |
break; |
559 |
if (head->r.index == CCS_MAX_PROFILES) |
560 |
return; |
561 |
head->r.step++; |
562 |
break; |
563 |
case 2: |
564 |
{ |
565 |
u8 i; |
566 |
const struct ccs_path_info *comment = profile->comment; |
567 |
ccs_io_printf(head, "%u-COMMENT=", index); |
568 |
ccs_set_string(head, comment ? comment->name : ""); |
569 |
ccs_set_lf(head); |
570 |
ccs_io_printf(head, "%u-PREFERENCE={ ", index); |
571 |
for (i = 0; i < CCS_MAX_PREF; i++) |
572 |
ccs_io_printf(head, "%s=%u ", |
573 |
ccs_pref_keywords[i], |
574 |
profile->pref[i]); |
575 |
ccs_set_string(head, " }\n"); |
576 |
head->r.step++; |
577 |
} |
578 |
break; |
579 |
case 3: |
580 |
{ |
581 |
ccs_io_printf(head, "%u-%s", index, "CONFIG"); |
582 |
ccs_print_config(head, profile->default_config); |
583 |
head->r.bit = 0; |
584 |
head->r.step++; |
585 |
} |
586 |
break; |
587 |
case 4: |
588 |
for ( ; head->r.bit < CCS_MAX_MAC_INDEX |
589 |
+ CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) { |
590 |
const u8 i = head->r.bit; |
591 |
const u8 config = profile->config[i]; |
592 |
if (config == CCS_CONFIG_USE_DEFAULT) |
593 |
continue; |
594 |
if (i < CCS_MAX_MAC_INDEX) |
595 |
ccs_io_printf(head, "%u-CONFIG::%s::%s", index, |
596 |
ccs_category_keywords |
597 |
[ccs_index2category[i]], |
598 |
ccs_mac_keywords[i]); |
599 |
else |
600 |
ccs_io_printf(head, "%u-CONFIG::%s", index, |
601 |
ccs_mac_keywords[i]); |
602 |
ccs_print_config(head, config); |
603 |
head->r.bit++; |
604 |
break; |
605 |
} |
606 |
if (head->r.bit == CCS_MAX_MAC_INDEX |
607 |
+ CCS_MAX_MAC_CATEGORY_INDEX) { |
608 |
head->r.index++; |
609 |
head->r.step = 1; |
610 |
} |
611 |
break; |
612 |
} |
613 |
if (ccs_flush(head)) |
614 |
goto next; |
615 |
} |
616 |
|
617 |
/** |
618 |
* ccs_same_manager - Check for duplicated "struct ccs_manager" entry. |
619 |
* |
620 |
* @a: Pointer to "struct ccs_acl_head". |
621 |
* @b: Pointer to "struct ccs_acl_head". |
622 |
* |
623 |
* Returns true if @a and @b are duplicated, false otherwise. |
624 |
*/ |
625 |
static bool ccs_same_manager(const struct ccs_acl_head *a, |
626 |
const struct ccs_acl_head *b) |
627 |
{ |
628 |
return container_of(a, struct ccs_manager, head)->manager |
629 |
== container_of(b, struct ccs_manager, head)->manager; |
630 |
} |
631 |
|
632 |
/** |
633 |
* ccs_update_manager_entry - Add a manager entry. |
634 |
* |
635 |
* @manager: The path to manager or the domainnamme. |
636 |
* @is_delete: True if it is a delete request. |
637 |
* |
638 |
* Returns 0 on success, negative value otherwise. |
639 |
*/ |
640 |
static int ccs_update_manager_entry(const char *manager, const bool is_delete) |
641 |
{ |
642 |
struct ccs_manager e = { }; |
643 |
int error = is_delete ? -ENOENT : -ENOMEM; |
644 |
if (ccs_domain_def(manager)) { |
645 |
if (!ccs_correct_domain(manager)) |
646 |
return -EINVAL; |
647 |
e.is_domain = true; |
648 |
} else { |
649 |
if (!ccs_correct_path(manager)) |
650 |
return -EINVAL; |
651 |
} |
652 |
e.manager = ccs_get_name(manager); |
653 |
if (!e.manager) |
654 |
return error; |
655 |
error = ccs_update_policy(&e.head, sizeof(e), is_delete, |
656 |
&ccs_policy_list[CCS_ID_MANAGER], |
657 |
ccs_same_manager); |
658 |
ccs_put_name(e.manager); |
659 |
return error; |
660 |
} |
661 |
|
662 |
/** |
663 |
* ccs_write_manager - Write manager policy. |
664 |
* |
665 |
* @head: Pointer to "struct ccs_io_buffer". |
666 |
* |
667 |
* Returns 0 on success, negative value otherwise. |
668 |
*/ |
669 |
static int ccs_write_manager(struct ccs_io_buffer *head) |
670 |
{ |
671 |
char *data = head->write_buf; |
672 |
bool is_delete = ccs_str_starts(&data, "delete "); |
673 |
if (!strcmp(data, "manage_by_non_root")) { |
674 |
ccs_manage_by_non_root = !is_delete; |
675 |
return 0; |
676 |
} |
677 |
return ccs_update_manager_entry(data, is_delete); |
678 |
} |
679 |
|
680 |
/** |
681 |
* ccs_read_manager - Read manager policy. |
682 |
* |
683 |
* @head: Pointer to "struct ccs_io_buffer". |
684 |
* |
685 |
* Caller holds ccs_read_lock(). |
686 |
*/ |
687 |
static void ccs_read_manager(struct ccs_io_buffer *head) |
688 |
{ |
689 |
if (head->r.eof) |
690 |
return; |
691 |
list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) { |
692 |
struct ccs_manager *ptr = |
693 |
list_entry(head->r.acl, typeof(*ptr), head.list); |
694 |
if (ptr->head.is_deleted) |
695 |
continue; |
696 |
if (!ccs_flush(head)) |
697 |
return; |
698 |
ccs_set_string(head, ptr->manager->name); |
699 |
ccs_set_lf(head); |
700 |
} |
701 |
head->r.eof = true; |
702 |
} |
703 |
|
704 |
/** |
705 |
* ccs_manager - Check whether the current process is a policy manager. |
706 |
* |
707 |
* Returns true if the current process is permitted to modify policy |
708 |
* via /proc/ccs/ interface. |
709 |
* |
710 |
* Caller holds ccs_read_lock(). |
711 |
*/ |
712 |
static bool ccs_manager(void) |
713 |
{ |
714 |
struct ccs_manager *ptr; |
715 |
const char *exe; |
716 |
struct ccs_security *task = ccs_current_security(); |
717 |
const struct ccs_path_info *domainname |
718 |
= ccs_current_domain()->domainname; |
719 |
bool found = false; |
720 |
if (!ccs_policy_loaded) |
721 |
return true; |
722 |
if (task->ccs_flags & CCS_TASK_IS_MANAGER) |
723 |
return true; |
724 |
if (!ccs_manage_by_non_root && (current_uid() || current_euid())) |
725 |
return false; |
726 |
exe = ccs_get_exe(); |
727 |
list_for_each_entry_srcu(ptr, &ccs_policy_list[CCS_ID_MANAGER], |
728 |
head.list, &ccs_ss) { |
729 |
if (ptr->head.is_deleted) |
730 |
continue; |
731 |
if (ptr->is_domain) { |
732 |
if (ccs_pathcmp(domainname, ptr->manager)) |
733 |
continue; |
734 |
} else { |
735 |
if (!exe || strcmp(exe, ptr->manager->name)) |
736 |
continue; |
737 |
} |
738 |
/* Set manager flag. */ |
739 |
task->ccs_flags |= CCS_TASK_IS_MANAGER; |
740 |
found = true; |
741 |
break; |
742 |
} |
743 |
if (!found) { /* Reduce error messages. */ |
744 |
static pid_t ccs_last_pid; |
745 |
const pid_t pid = current->pid; |
746 |
if (ccs_last_pid != pid) { |
747 |
printk(KERN_WARNING "%s ( %s ) is not permitted to " |
748 |
"update policies.\n", domainname->name, exe); |
749 |
ccs_last_pid = pid; |
750 |
} |
751 |
} |
752 |
kfree(exe); |
753 |
return found; |
754 |
} |
755 |
|
756 |
/** |
757 |
* ccs_select_one - Parse select command. |
758 |
* |
759 |
* @head: Pointer to "struct ccs_io_buffer". |
760 |
* @data: String to parse. |
761 |
* |
762 |
* Returns true on success, false otherwise. |
763 |
* |
764 |
* Caller holds ccs_read_lock(). |
765 |
*/ |
766 |
static bool ccs_select_one(struct ccs_io_buffer *head, const char *data) |
767 |
{ |
768 |
unsigned int pid; |
769 |
struct ccs_domain_info *domain = NULL; |
770 |
bool global_pid = false; |
771 |
if (!strcmp(data, "execute")) { |
772 |
head->r.print_execute_only = true; |
773 |
return true; |
774 |
} |
775 |
if (sscanf(data, "pid=%u", &pid) == 1 || |
776 |
(global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { |
777 |
struct task_struct *p; |
778 |
ccs_tasklist_lock(); |
779 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) |
780 |
if (global_pid) |
781 |
p = ccsecurity_exports.find_task_by_pid_ns(pid, |
782 |
&init_pid_ns); |
783 |
else |
784 |
p = ccsecurity_exports.find_task_by_vpid(pid); |
785 |
#else |
786 |
p = find_task_by_pid(pid); |
787 |
#endif |
788 |
if (p) |
789 |
domain = ccs_task_domain(p); |
790 |
ccs_tasklist_unlock(); |
791 |
} else if (!strncmp(data, "domain=", 7)) { |
792 |
if (ccs_domain_def(data + 7)) |
793 |
domain = ccs_find_domain(data + 7); |
794 |
} else |
795 |
return false; |
796 |
head->w.domain = domain; |
797 |
/* Accessing read_buf is safe because head->io_sem is held. */ |
798 |
if (!head->read_buf) |
799 |
return true; /* Do nothing if open(O_WRONLY). */ |
800 |
memset(&head->r, 0, sizeof(head->r)); |
801 |
head->r.print_this_domain_only = true; |
802 |
if (domain) |
803 |
head->r.domain = &domain->list; |
804 |
else |
805 |
head->r.eof = true; |
806 |
ccs_io_printf(head, "# select %s\n", data); |
807 |
if (domain && domain->is_deleted) |
808 |
ccs_set_string(head, "# This is a deleted domain.\n"); |
809 |
return true; |
810 |
} |
811 |
|
812 |
/** |
813 |
* ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry. |
814 |
* |
815 |
* @a: Pointer to "struct ccs_acl_info". |
816 |
* @b: Pointer to "struct ccs_acl_info". |
817 |
* |
818 |
* Returns true if @a and @b are duplicated, false otherwise. |
819 |
*/ |
820 |
static bool ccs_same_handler_acl(const struct ccs_acl_info *a, |
821 |
const struct ccs_acl_info *b) |
822 |
{ |
823 |
const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head); |
824 |
const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head); |
825 |
return p1->handler == p2->handler; |
826 |
} |
827 |
|
828 |
/** |
829 |
* ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry. |
830 |
* |
831 |
* @a: Pointer to "struct ccs_acl_info". |
832 |
* @b: Pointer to "struct ccs_acl_info". |
833 |
* |
834 |
* Returns true if @a and @b are duplicated, false otherwise. |
835 |
*/ |
836 |
static bool ccs_same_task_acl(const struct ccs_acl_info *a, |
837 |
const struct ccs_acl_info *b) |
838 |
{ |
839 |
const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head); |
840 |
const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head); |
841 |
return p1->domainname == p2->domainname; |
842 |
} |
843 |
|
844 |
/** |
845 |
* ccs_write_task - Update task related list. |
846 |
* |
847 |
* @param: Pointer to "struct ccs_acl_param". |
848 |
* |
849 |
* Returns 0 on success, negative value otherwise. |
850 |
*/ |
851 |
static int ccs_write_task(struct ccs_acl_param *param) |
852 |
{ |
853 |
int error; |
854 |
const bool is_auto = ccs_str_starts(¶m->data, |
855 |
"auto_domain_transition "); |
856 |
if (!is_auto && !ccs_str_starts(¶m->data, |
857 |
"manual_domain_transition ")) { |
858 |
struct ccs_handler_acl e = { }; |
859 |
char *handler; |
860 |
if (ccs_str_starts(¶m->data, "auto_execute_handler ")) |
861 |
e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER; |
862 |
else if (ccs_str_starts(¶m->data, |
863 |
"denied_execute_handler ")) |
864 |
e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER; |
865 |
else |
866 |
return -EINVAL; |
867 |
handler = ccs_read_token(param); |
868 |
if (!ccs_correct_path(handler)) |
869 |
return -EINVAL; |
870 |
e.handler = ccs_get_name(handler); |
871 |
if (!e.handler) |
872 |
return -ENOMEM; |
873 |
if (e.handler->is_patterned) |
874 |
error = -EINVAL; /* No patterns allowed. */ |
875 |
else |
876 |
error = ccs_update_domain(&e.head, sizeof(e), param, |
877 |
ccs_same_handler_acl, NULL); |
878 |
ccs_put_name(e.handler); |
879 |
} else { |
880 |
struct ccs_task_acl e = { |
881 |
.head.type = is_auto ? |
882 |
CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL, |
883 |
.domainname = ccs_get_domainname(param), |
884 |
}; |
885 |
if (!e.domainname) |
886 |
error = -EINVAL; |
887 |
else |
888 |
error = ccs_update_domain(&e.head, sizeof(e), param, |
889 |
ccs_same_task_acl, NULL); |
890 |
ccs_put_name(e.domainname); |
891 |
} |
892 |
return error; |
893 |
} |
894 |
|
895 |
static int ccs_write_domain2(char *data, struct ccs_domain_info *domain, |
896 |
const bool is_delete) |
897 |
{ |
898 |
struct ccs_acl_param param = { |
899 |
.data = data, |
900 |
.domain = domain, |
901 |
.is_delete = is_delete, |
902 |
}; |
903 |
static const struct { |
904 |
const char *keyword; |
905 |
int (*write) (struct ccs_acl_param *); |
906 |
} ccs_callback[7] = { |
907 |
{ "file ", ccs_write_file }, |
908 |
{ "network inet ", ccs_write_inet_network }, |
909 |
{ "network unix ", ccs_write_unix_network }, |
910 |
{ "misc ", ccs_write_misc }, |
911 |
{ "capability ", ccs_write_capability }, |
912 |
{ "ipc ", ccs_write_ipc }, |
913 |
{ "task ", ccs_write_task }, |
914 |
}; |
915 |
u8 i; |
916 |
for (i = 0; i < 7; i++) { |
917 |
if (!ccs_str_starts(¶m.data, ccs_callback[i].keyword)) |
918 |
continue; |
919 |
return ccs_callback[i].write(¶m); |
920 |
} |
921 |
return -EINVAL; |
922 |
} |
923 |
|
924 |
const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = { |
925 |
[CCS_DIF_QUOTA_WARNED] = "quota_exceeded\n", |
926 |
[CCS_DIF_TRANSITION_FAILED] = "transition_failed\n", |
927 |
}; |
928 |
|
929 |
/** |
930 |
* ccs_write_domain - Write domain policy. |
931 |
* |
932 |
* @head: Pointer to "struct ccs_io_buffer". |
933 |
* |
934 |
* Returns 0 on success, negative value otherwise. |
935 |
*/ |
936 |
static int ccs_write_domain(struct ccs_io_buffer *head) |
937 |
{ |
938 |
char *data = head->write_buf; |
939 |
struct ccs_domain_info *domain = head->w.domain; |
940 |
bool is_delete = false; |
941 |
bool is_select = false; |
942 |
unsigned int profile; |
943 |
if (ccs_str_starts(&data, "delete ")) |
944 |
is_delete = true; |
945 |
else if (ccs_str_starts(&data, "select ")) |
946 |
is_select = true; |
947 |
if (is_select && ccs_select_one(head, data)) |
948 |
return 0; |
949 |
/* Don't allow updating policies by non manager programs. */ |
950 |
if (!ccs_manager()) |
951 |
return -EPERM; |
952 |
if (ccs_domain_def(data)) { |
953 |
domain = NULL; |
954 |
if (is_delete) |
955 |
ccs_delete_domain(data); |
956 |
else if (is_select) |
957 |
domain = ccs_find_domain(data); |
958 |
else |
959 |
domain = ccs_assign_domain(data, 0, 0, false); |
960 |
head->w.domain = domain; |
961 |
return 0; |
962 |
} |
963 |
if (!domain) |
964 |
return -EINVAL; |
965 |
|
966 |
if (sscanf(data, "use_profile %u\n", &profile) == 1 |
967 |
&& profile < CCS_MAX_PROFILES) { |
968 |
if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]) |
969 |
domain->profile = (u8) profile; |
970 |
return 0; |
971 |
} |
972 |
if (sscanf(data, "use_group %u\n", &profile) == 1 |
973 |
&& profile < CCS_MAX_ACL_GROUPS) { |
974 |
domain->group = (u8) profile; |
975 |
return 0; |
976 |
} |
977 |
for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) { |
978 |
const char *cp = ccs_dif[profile]; |
979 |
if (strncmp(data, cp, strlen(cp) - 1)) |
980 |
continue; |
981 |
domain->flags[profile] = !is_delete; |
982 |
return 0; |
983 |
} |
984 |
return ccs_write_domain2(data, domain, is_delete); |
985 |
} |
986 |
|
987 |
/** |
988 |
* ccs_print_name_union - Print a ccs_name_union. |
989 |
* |
990 |
* @head: Pointer to "struct ccs_io_buffer". |
991 |
* @ptr: Pointer to "struct ccs_name_union". |
992 |
*/ |
993 |
static void ccs_print_name_union(struct ccs_io_buffer *head, |
994 |
const struct ccs_name_union *ptr) |
995 |
{ |
996 |
const bool cond = head->r.print_cond_part; |
997 |
if (!cond) |
998 |
ccs_set_space(head); |
999 |
if (ptr->is_group) { |
1000 |
ccs_set_string(head, "@"); |
1001 |
ccs_set_string(head, ptr->group->group_name->name); |
1002 |
} else { |
1003 |
if (cond) |
1004 |
ccs_set_string(head, "\""); |
1005 |
ccs_set_string(head, ptr->filename->name); |
1006 |
if (cond) |
1007 |
ccs_set_string(head, "\""); |
1008 |
} |
1009 |
} |
1010 |
|
1011 |
/** |
1012 |
* ccs_print_number_union - Print a ccs_number_union. |
1013 |
* |
1014 |
* @head: Pointer to "struct ccs_io_buffer". |
1015 |
* @ptr: Pointer to "struct ccs_number_union". |
1016 |
*/ |
1017 |
static void ccs_print_number_union(struct ccs_io_buffer *head, |
1018 |
const struct ccs_number_union *ptr) |
1019 |
{ |
1020 |
if (!head->r.print_cond_part) |
1021 |
ccs_set_space(head); |
1022 |
if (ptr->is_group) { |
1023 |
ccs_set_string(head, "@"); |
1024 |
ccs_set_string(head, ptr->group->group_name->name); |
1025 |
} else { |
1026 |
int i; |
1027 |
unsigned long min = ptr->values[0]; |
1028 |
const unsigned long max = ptr->values[1]; |
1029 |
u8 min_type = ptr->value_type[0]; |
1030 |
const u8 max_type = ptr->value_type[1]; |
1031 |
char buffer[128]; |
1032 |
buffer[0] = '\0'; |
1033 |
for (i = 0; i < 2; i++) { |
1034 |
switch (min_type) { |
1035 |
case CCS_VALUE_TYPE_HEXADECIMAL: |
1036 |
ccs_addprintf(buffer, sizeof(buffer), "0x%lX", |
1037 |
min); |
1038 |
break; |
1039 |
case CCS_VALUE_TYPE_OCTAL: |
1040 |
ccs_addprintf(buffer, sizeof(buffer), "0%lo", |
1041 |
min); |
1042 |
break; |
1043 |
default: |
1044 |
ccs_addprintf(buffer, sizeof(buffer), "%lu", |
1045 |
min); |
1046 |
break; |
1047 |
} |
1048 |
if (min == max && min_type == max_type) |
1049 |
break; |
1050 |
ccs_addprintf(buffer, sizeof(buffer), "-"); |
1051 |
min_type = max_type; |
1052 |
min = max; |
1053 |
} |
1054 |
ccs_io_printf(head, "%s", buffer); |
1055 |
} |
1056 |
} |
1057 |
|
1058 |
/** |
1059 |
* ccs_print_condition - Print condition part. |
1060 |
* |
1061 |
* @head: Pointer to "struct ccs_io_buffer". |
1062 |
* @cond: Pointer to "struct ccs_condition". |
1063 |
* |
1064 |
* Returns true on success, false otherwise. |
1065 |
*/ |
1066 |
static bool ccs_print_condition(struct ccs_io_buffer *head, |
1067 |
const struct ccs_condition *cond) |
1068 |
{ |
1069 |
switch (head->r.cond_step) { |
1070 |
case 0: |
1071 |
head->r.cond_index = 0; |
1072 |
head->r.cond_step++; |
1073 |
/* fall through */ |
1074 |
case 1: |
1075 |
{ |
1076 |
const u16 condc = cond->condc; |
1077 |
const struct ccs_condition_element *condp = |
1078 |
(typeof(condp)) (cond + 1); |
1079 |
const struct ccs_number_union *numbers_p = |
1080 |
(typeof(numbers_p)) (condp + condc); |
1081 |
const struct ccs_name_union *names_p = |
1082 |
(typeof(names_p)) |
1083 |
(numbers_p + cond->numbers_count); |
1084 |
const struct ccs_argv *argv = |
1085 |
(typeof(argv)) (names_p + cond->names_count); |
1086 |
const struct ccs_envp *envp = |
1087 |
(typeof(envp)) (argv + cond->argc); |
1088 |
u16 skip; |
1089 |
for (skip = 0; skip < head->r.cond_index; skip++) { |
1090 |
const u8 left = condp->left; |
1091 |
const u8 right = condp->right; |
1092 |
condp++; |
1093 |
switch (left) { |
1094 |
case CCS_ARGV_ENTRY: |
1095 |
argv++; |
1096 |
continue; |
1097 |
case CCS_ENVP_ENTRY: |
1098 |
envp++; |
1099 |
continue; |
1100 |
case CCS_NUMBER_UNION: |
1101 |
numbers_p++; |
1102 |
break; |
1103 |
} |
1104 |
switch (right) { |
1105 |
case CCS_NAME_UNION: |
1106 |
names_p++; |
1107 |
break; |
1108 |
case CCS_NUMBER_UNION: |
1109 |
numbers_p++; |
1110 |
break; |
1111 |
} |
1112 |
} |
1113 |
while (head->r.cond_index < condc) { |
1114 |
const u8 match = condp->equals; |
1115 |
const u8 left = condp->left; |
1116 |
const u8 right = condp->right; |
1117 |
if (!ccs_flush(head)) |
1118 |
return false; |
1119 |
condp++; |
1120 |
head->r.cond_index++; |
1121 |
ccs_set_space(head); |
1122 |
switch (left) { |
1123 |
case CCS_ARGV_ENTRY: |
1124 |
ccs_io_printf(head, |
1125 |
"exec.argv[%u]%s\"%s\"", |
1126 |
argv->index, |
1127 |
argv->is_not ? |
1128 |
"!=" : "=", |
1129 |
argv->value->name); |
1130 |
argv++; |
1131 |
continue; |
1132 |
case CCS_ENVP_ENTRY: |
1133 |
ccs_io_printf(head, |
1134 |
"exec.envp[\"%s\"]%s", |
1135 |
envp->name->name, |
1136 |
envp->is_not ? |
1137 |
"!=" : "="); |
1138 |
if (envp->value) { |
1139 |
ccs_set_string(head, "\""); |
1140 |
ccs_set_string(head, envp-> |
1141 |
value->name); |
1142 |
ccs_set_string(head, "\""); |
1143 |
} else { |
1144 |
ccs_set_string(head, "NULL"); |
1145 |
} |
1146 |
envp++; |
1147 |
continue; |
1148 |
case CCS_NUMBER_UNION: |
1149 |
ccs_print_number_union(head, |
1150 |
numbers_p++); |
1151 |
break; |
1152 |
default: |
1153 |
ccs_set_string(head, |
1154 |
ccs_condition_keyword[left]); |
1155 |
break; |
1156 |
} |
1157 |
ccs_set_string(head, match ? "=" : "!="); |
1158 |
switch (right) { |
1159 |
case CCS_NAME_UNION: |
1160 |
ccs_print_name_union(head, names_p++); |
1161 |
break; |
1162 |
case CCS_NUMBER_UNION: |
1163 |
ccs_print_number_union(head, |
1164 |
numbers_p++); |
1165 |
break; |
1166 |
default: |
1167 |
ccs_set_string(head, |
1168 |
ccs_condition_keyword[right]); |
1169 |
break; |
1170 |
} |
1171 |
} |
1172 |
} |
1173 |
head->r.cond_step++; |
1174 |
/* fall through */ |
1175 |
case 2: |
1176 |
if (!ccs_flush(head)) |
1177 |
break; |
1178 |
head->r.cond_step++; |
1179 |
/* fall through */ |
1180 |
case 3: |
1181 |
if (cond->grant_log != CCS_GRANTLOG_AUTO) |
1182 |
ccs_io_printf(head, " grant_log=%s", |
1183 |
ccs_yesno(cond->grant_log == |
1184 |
CCS_GRANTLOG_YES)); |
1185 |
if (cond->transit) { |
1186 |
ccs_set_string(head, " auto_domain_transitition=\""); |
1187 |
ccs_set_string(head, cond->transit->name); |
1188 |
ccs_set_string(head, "\""); |
1189 |
} |
1190 |
ccs_set_lf(head); |
1191 |
return true; |
1192 |
} |
1193 |
return false; |
1194 |
} |
1195 |
|
1196 |
/** |
1197 |
* ccs_fns - Find next set bit. |
1198 |
* |
1199 |
* @perm: 8 bits value. |
1200 |
* @bit: First bit to find. |
1201 |
* |
1202 |
* Returns next set bit on success, 8 otherwise. |
1203 |
*/ |
1204 |
static u8 ccs_fns(const u8 perm, u8 bit) |
1205 |
{ |
1206 |
for ( ; bit < 8; bit++) |
1207 |
if (perm & (1 << bit)) |
1208 |
break; |
1209 |
return bit; |
1210 |
} |
1211 |
|
1212 |
static void ccs_set_group(struct ccs_io_buffer *head) |
1213 |
{ |
1214 |
if (head->type == CCS_EXCEPTIONPOLICY) |
1215 |
ccs_io_printf(head, "acl_group %u ", head->r.group_index); |
1216 |
} |
1217 |
|
1218 |
/** |
1219 |
* ccs_print_entry - Print an ACL entry. |
1220 |
* |
1221 |
* @head: Pointer to "struct ccs_io_buffer". |
1222 |
* @acl: Pointer to an ACL entry. |
1223 |
* |
1224 |
* Returns true on success, false otherwise. |
1225 |
*/ |
1226 |
static bool ccs_print_entry(struct ccs_io_buffer *head, |
1227 |
const struct ccs_acl_info *acl) |
1228 |
{ |
1229 |
const u8 acl_type = acl->type; |
1230 |
u8 bit; |
1231 |
if (head->r.print_cond_part) |
1232 |
goto print_cond_part; |
1233 |
if (acl->is_deleted) |
1234 |
return true; |
1235 |
next: |
1236 |
bit = head->r.bit; |
1237 |
if (!ccs_flush(head)) |
1238 |
return false; |
1239 |
else if (acl_type == CCS_TYPE_PATH_ACL) { |
1240 |
struct ccs_path_acl *ptr |
1241 |
= container_of(acl, typeof(*ptr), head); |
1242 |
const u16 perm = ptr->perm; |
1243 |
for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) { |
1244 |
if (!(perm & (1 << bit))) |
1245 |
continue; |
1246 |
if (head->r.print_execute_only && |
1247 |
bit != CCS_TYPE_EXECUTE |
1248 |
/* && bit != CCS_TYPE_TRANSIT */) |
1249 |
continue; |
1250 |
break; |
1251 |
} |
1252 |
if (bit >= CCS_MAX_PATH_OPERATION) |
1253 |
goto done; |
1254 |
ccs_set_group(head); |
1255 |
ccs_set_string(head, "file "); |
1256 |
ccs_set_string(head, ccs_path_keyword[bit]); |
1257 |
ccs_print_name_union(head, &ptr->name); |
1258 |
} else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER || |
1259 |
acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) { |
1260 |
struct ccs_handler_acl *ptr |
1261 |
= container_of(acl, typeof(*ptr), head); |
1262 |
ccs_set_group(head); |
1263 |
ccs_set_string(head, "task "); |
1264 |
ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER |
1265 |
? "auto_execute_handler " : |
1266 |
"denied_execute_handler "); |
1267 |
ccs_set_string(head, ptr->handler->name); |
1268 |
} else if (acl_type == CCS_TYPE_AUTO_TASK_ACL || |
1269 |
acl_type == CCS_TYPE_MANUAL_TASK_ACL) { |
1270 |
struct ccs_task_acl *ptr = |
1271 |
container_of(acl, typeof(*ptr), head); |
1272 |
ccs_set_group(head); |
1273 |
ccs_set_string(head, "task "); |
1274 |
ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ? |
1275 |
"auto_domain_transition " : |
1276 |
"manual_domain_transition "); |
1277 |
ccs_set_string(head, ptr->domainname->name); |
1278 |
} else if (head->r.print_execute_only) { |
1279 |
return true; |
1280 |
} else if (acl_type == CCS_TYPE_MKDEV_ACL) { |
1281 |
struct ccs_mkdev_acl *ptr = |
1282 |
container_of(acl, typeof(*ptr), head); |
1283 |
bit = ccs_fns(ptr->perm, bit); |
1284 |
if (bit >= CCS_MAX_MKDEV_OPERATION) |
1285 |
goto done; |
1286 |
ccs_set_group(head); |
1287 |
ccs_set_string(head, "file "); |
1288 |
ccs_set_string(head, ccs_mac_keywords[ccs_pnnn2mac[bit]]); |
1289 |
ccs_print_name_union(head, &ptr->name); |
1290 |
ccs_print_number_union(head, &ptr->mode); |
1291 |
ccs_print_number_union(head, &ptr->major); |
1292 |
ccs_print_number_union(head, &ptr->minor); |
1293 |
} else if (acl_type == CCS_TYPE_PATH2_ACL) { |
1294 |
struct ccs_path2_acl *ptr = |
1295 |
container_of(acl, typeof(*ptr), head); |
1296 |
bit = ccs_fns(ptr->perm, bit); |
1297 |
if (bit >= CCS_MAX_PATH2_OPERATION) |
1298 |
goto done; |
1299 |
ccs_set_group(head); |
1300 |
ccs_set_string(head, "file "); |
1301 |
ccs_set_string(head, ccs_mac_keywords[ccs_pp2mac[bit]]); |
1302 |
ccs_print_name_union(head, &ptr->name1); |
1303 |
ccs_print_name_union(head, &ptr->name2); |
1304 |
} else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) { |
1305 |
struct ccs_path_number_acl *ptr = |
1306 |
container_of(acl, typeof(*ptr), head); |
1307 |
bit = ccs_fns(ptr->perm, bit); |
1308 |
if (bit >= CCS_MAX_PATH_NUMBER_OPERATION) |
1309 |
goto done; |
1310 |
ccs_set_group(head); |
1311 |
ccs_set_string(head, "file "); |
1312 |
ccs_set_string(head, ccs_mac_keywords[ccs_pn2mac[bit]]); |
1313 |
ccs_print_name_union(head, &ptr->name); |
1314 |
ccs_print_number_union(head, &ptr->number); |
1315 |
} else if (acl_type == CCS_TYPE_ENV_ACL) { |
1316 |
struct ccs_env_acl *ptr = |
1317 |
container_of(acl, typeof(*ptr), head); |
1318 |
ccs_set_group(head); |
1319 |
ccs_set_string(head, "misc env "); |
1320 |
ccs_set_string(head, ptr->env->name); |
1321 |
} else if (acl_type == CCS_TYPE_CAPABILITY_ACL) { |
1322 |
struct ccs_capability_acl *ptr = |
1323 |
container_of(acl, typeof(*ptr), head); |
1324 |
ccs_set_group(head); |
1325 |
ccs_set_string(head, "capability "); |
1326 |
ccs_set_string(head, |
1327 |
ccs_mac_keywords[ccs_c2mac[ptr->operation]]); |
1328 |
} else if (acl_type == CCS_TYPE_INET_ACL) { |
1329 |
struct ccs_inet_acl *ptr = |
1330 |
container_of(acl, typeof(*ptr), head); |
1331 |
bit = ccs_fns(ptr->perm, bit); |
1332 |
if (bit >= CCS_MAX_NETWORK_OPERATION) |
1333 |
goto done; |
1334 |
ccs_set_group(head); |
1335 |
ccs_set_string(head, "network inet "); |
1336 |
ccs_set_string(head, ccs_proto_keyword[ptr->protocol]); |
1337 |
ccs_set_space(head); |
1338 |
ccs_set_string(head, ccs_socket_keyword[bit]); |
1339 |
ccs_set_space(head); |
1340 |
switch (ptr->address_type) { |
1341 |
char buf[128]; |
1342 |
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
1343 |
ccs_set_string(head, "@"); |
1344 |
ccs_set_string(head, |
1345 |
ptr->address.group->group_name->name); |
1346 |
break; |
1347 |
case CCS_IP_ADDRESS_TYPE_IPv4: |
1348 |
ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min, |
1349 |
ptr->address.ipv4.max); |
1350 |
ccs_io_printf(head, "%s", buf); |
1351 |
break; |
1352 |
case CCS_IP_ADDRESS_TYPE_IPv6: |
1353 |
ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min, |
1354 |
ptr->address.ipv6.max); |
1355 |
ccs_io_printf(head, "%s", buf); |
1356 |
break; |
1357 |
} |
1358 |
ccs_print_number_union(head, &ptr->port); |
1359 |
} else if (acl_type == CCS_TYPE_UNIX_ACL) { |
1360 |
struct ccs_unix_acl *ptr = |
1361 |
container_of(acl, typeof(*ptr), head); |
1362 |
bit = ccs_fns(ptr->perm, bit); |
1363 |
if (bit >= CCS_MAX_NETWORK_OPERATION) |
1364 |
goto done; |
1365 |
ccs_set_group(head); |
1366 |
ccs_set_string(head, "network unix "); |
1367 |
ccs_set_string(head, ccs_proto_keyword[ptr->protocol]); |
1368 |
ccs_set_space(head); |
1369 |
ccs_set_string(head, ccs_socket_keyword[bit]); |
1370 |
ccs_print_name_union(head, &ptr->name); |
1371 |
} else if (acl_type == CCS_TYPE_SIGNAL_ACL) { |
1372 |
struct ccs_signal_acl *ptr = |
1373 |
container_of(acl, typeof(*ptr), head); |
1374 |
ccs_set_group(head); |
1375 |
ccs_set_string(head, "ipc signal "); |
1376 |
ccs_io_printf(head, "%u ", ptr->sig); |
1377 |
ccs_set_string(head, ptr->domainname->name); |
1378 |
} else if (acl_type == CCS_TYPE_MOUNT_ACL) { |
1379 |
struct ccs_mount_acl *ptr = |
1380 |
container_of(acl, typeof(*ptr), head); |
1381 |
ccs_set_group(head); |
1382 |
ccs_io_printf(head, "file mount"); |
1383 |
ccs_print_name_union(head, &ptr->dev_name); |
1384 |
ccs_print_name_union(head, &ptr->dir_name); |
1385 |
ccs_print_name_union(head, &ptr->fs_type); |
1386 |
ccs_print_number_union(head, &ptr->flags); |
1387 |
} |
1388 |
head->r.bit = bit + 1; |
1389 |
if (acl->cond) { |
1390 |
head->r.print_cond_part = true; |
1391 |
head->r.cond_step = 0; |
1392 |
if (!ccs_flush(head)) |
1393 |
return false; |
1394 |
print_cond_part: |
1395 |
if (!ccs_print_condition(head, acl->cond)) |
1396 |
return false; |
1397 |
head->r.print_cond_part = false; |
1398 |
} else { |
1399 |
ccs_set_lf(head); |
1400 |
} |
1401 |
switch (acl_type) { |
1402 |
case CCS_TYPE_PATH_ACL: |
1403 |
case CCS_TYPE_MKDEV_ACL: |
1404 |
case CCS_TYPE_PATH2_ACL: |
1405 |
case CCS_TYPE_PATH_NUMBER_ACL: |
1406 |
case CCS_TYPE_INET_ACL: |
1407 |
case CCS_TYPE_UNIX_ACL: |
1408 |
goto next; |
1409 |
} |
1410 |
done: |
1411 |
head->r.bit = 0; |
1412 |
return true; |
1413 |
} |
1414 |
|
1415 |
/** |
1416 |
* ccs_read_domain2 - Read domain policy. |
1417 |
* |
1418 |
* @head: Pointer to "struct ccs_io_buffer". |
1419 |
* @domain: Pointer to "struct ccs_domain_info". |
1420 |
* @index: Index number. |
1421 |
* |
1422 |
* Caller holds ccs_read_lock(). |
1423 |
* |
1424 |
* Returns true on success, false otherwise. |
1425 |
*/ |
1426 |
static bool ccs_read_domain2(struct ccs_io_buffer *head, |
1427 |
struct ccs_domain_info *domain, |
1428 |
const u8 index) |
1429 |
{ |
1430 |
list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) { |
1431 |
struct ccs_acl_info *ptr = |
1432 |
list_entry(head->r.acl, typeof(*ptr), list); |
1433 |
if (!ccs_print_entry(head, ptr)) |
1434 |
return false; |
1435 |
} |
1436 |
head->r.acl = NULL; |
1437 |
return true; |
1438 |
} |
1439 |
|
1440 |
/** |
1441 |
* ccs_read_domain - Read domain policy. |
1442 |
* |
1443 |
* @head: Pointer to "struct ccs_io_buffer". |
1444 |
* |
1445 |
* Caller holds ccs_read_lock(). |
1446 |
*/ |
1447 |
static void ccs_read_domain(struct ccs_io_buffer *head) |
1448 |
{ |
1449 |
if (head->r.eof) |
1450 |
return; |
1451 |
list_for_each_cookie(head->r.domain, &ccs_domain_list) { |
1452 |
struct ccs_domain_info *domain = |
1453 |
list_entry(head->r.domain, typeof(*domain), list); |
1454 |
switch (head->r.step) { |
1455 |
u8 i; |
1456 |
case 0: |
1457 |
if (domain->is_deleted && |
1458 |
!head->r.print_this_domain_only) |
1459 |
continue; |
1460 |
/* Print domainname and flags. */ |
1461 |
ccs_set_string(head, domain->domainname->name); |
1462 |
ccs_set_lf(head); |
1463 |
ccs_io_printf(head, "use_profile %u\n", |
1464 |
domain->profile); |
1465 |
ccs_io_printf(head, "use_group %u\n", domain->group); |
1466 |
for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++) |
1467 |
if (domain->flags[i]) |
1468 |
ccs_set_string(head, ccs_dif[i]); |
1469 |
head->r.step++; |
1470 |
ccs_set_lf(head); |
1471 |
/* fall through */ |
1472 |
case 1: |
1473 |
if (!ccs_read_domain2(head, domain, 0)) |
1474 |
return; |
1475 |
head->r.step++; |
1476 |
/* fall through */ |
1477 |
case 2: |
1478 |
if (!ccs_read_domain2(head, domain, 1)) |
1479 |
return; |
1480 |
head->r.step++; |
1481 |
if (!ccs_set_lf(head)) |
1482 |
return; |
1483 |
/* fall through */ |
1484 |
case 3: |
1485 |
head->r.step = 0; |
1486 |
if (head->r.print_this_domain_only) |
1487 |
goto done; |
1488 |
} |
1489 |
} |
1490 |
done: |
1491 |
head->r.eof = true; |
1492 |
} |
1493 |
|
1494 |
/** |
1495 |
* ccs_write_domain_profile - Assign profile for specified domain. |
1496 |
* |
1497 |
* @head: Pointer to "struct ccs_io_buffer". |
1498 |
* |
1499 |
* Returns 0 on success, -EINVAL otherwise. |
1500 |
* |
1501 |
* This is equivalent to doing |
1502 |
* |
1503 |
* ( echo "select " $domainname; echo "use_profile " $profile ) | |
1504 |
* /usr/sbin/ccs-loadpolicy -d |
1505 |
* |
1506 |
* Caller holds ccs_read_lock(). |
1507 |
*/ |
1508 |
static int ccs_write_domain_profile(struct ccs_io_buffer *head) |
1509 |
{ |
1510 |
char *data = head->write_buf; |
1511 |
char *cp = strchr(data, ' '); |
1512 |
struct ccs_domain_info *domain; |
1513 |
unsigned int profile; |
1514 |
if (!cp) |
1515 |
return -EINVAL; |
1516 |
*cp = '\0'; |
1517 |
profile = simple_strtoul(data, NULL, 10); |
1518 |
if (profile >= CCS_MAX_PROFILES) |
1519 |
return -EINVAL; |
1520 |
domain = ccs_find_domain(cp + 1); |
1521 |
if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])) |
1522 |
domain->profile = (u8) profile; |
1523 |
return 0; |
1524 |
} |
1525 |
|
1526 |
/** |
1527 |
* ccs_read_domain_profile - Read only domainname and profile. |
1528 |
* |
1529 |
* @head: Pointer to "struct ccs_io_buffer". |
1530 |
* |
1531 |
* This is equivalent to doing |
1532 |
* |
1533 |
* grep -A 1 '^<kernel>' /proc/ccs/domain_policy | |
1534 |
* awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" ) |
1535 |
* domainname = $0; } else if ( $1 == "use_profile" ) { |
1536 |
* print $2 " " domainname; domainname = ""; } } ; ' |
1537 |
* |
1538 |
* Caller holds ccs_read_lock(). |
1539 |
*/ |
1540 |
static void ccs_read_domain_profile(struct ccs_io_buffer *head) |
1541 |
{ |
1542 |
if (head->r.eof) |
1543 |
return; |
1544 |
list_for_each_cookie(head->r.domain, &ccs_domain_list) { |
1545 |
struct ccs_domain_info *domain = |
1546 |
list_entry(head->r.domain, typeof(*domain), list); |
1547 |
if (domain->is_deleted) |
1548 |
continue; |
1549 |
if (!ccs_flush(head)) |
1550 |
return; |
1551 |
ccs_io_printf(head, "%u ", domain->profile); |
1552 |
ccs_set_string(head, domain->domainname->name); |
1553 |
ccs_set_lf(head); |
1554 |
} |
1555 |
head->r.eof = true; |
1556 |
} |
1557 |
|
1558 |
/** |
1559 |
* ccs_write_pid: Specify PID to obtain domainname. |
1560 |
* |
1561 |
* @head: Pointer to "struct ccs_io_buffer". |
1562 |
* |
1563 |
* Returns 0. |
1564 |
*/ |
1565 |
static int ccs_write_pid(struct ccs_io_buffer *head) |
1566 |
{ |
1567 |
head->r.eof = false; |
1568 |
return 0; |
1569 |
} |
1570 |
|
1571 |
/** |
1572 |
* ccs_read_pid - Read information of a process. |
1573 |
* |
1574 |
* @head: Pointer to "struct ccs_io_buffer". |
1575 |
* |
1576 |
* Returns the domainname which the specified PID is in or |
1577 |
* process information of the specified PID on success, |
1578 |
* empty string otherwise. |
1579 |
* |
1580 |
* Caller holds ccs_read_lock(). |
1581 |
*/ |
1582 |
static void ccs_read_pid(struct ccs_io_buffer *head) |
1583 |
{ |
1584 |
char *buf = head->write_buf; |
1585 |
bool task_info = false; |
1586 |
bool global_pid = false; |
1587 |
unsigned int pid; |
1588 |
struct task_struct *p; |
1589 |
struct ccs_domain_info *domain = NULL; |
1590 |
u32 ccs_flags = 0; |
1591 |
/* Accessing write_buf is safe because head->io_sem is held. */ |
1592 |
if (!buf) { |
1593 |
head->r.eof = true; |
1594 |
return; /* Do nothing if open(O_RDONLY). */ |
1595 |
} |
1596 |
if (head->r.w_pos || head->r.eof) |
1597 |
return; |
1598 |
head->r.eof = true; |
1599 |
if (ccs_str_starts(&buf, "info ")) |
1600 |
task_info = true; |
1601 |
if (ccs_str_starts(&buf, "global-pid ")) |
1602 |
global_pid = true; |
1603 |
pid = (unsigned int) simple_strtoul(buf, NULL, 10); |
1604 |
ccs_tasklist_lock(); |
1605 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) |
1606 |
if (global_pid) |
1607 |
p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns); |
1608 |
else |
1609 |
p = ccsecurity_exports.find_task_by_vpid(pid); |
1610 |
#else |
1611 |
p = find_task_by_pid(pid); |
1612 |
#endif |
1613 |
if (p) { |
1614 |
domain = ccs_task_domain(p); |
1615 |
ccs_flags = ccs_task_flags(p); |
1616 |
} |
1617 |
ccs_tasklist_unlock(); |
1618 |
if (!domain) |
1619 |
return; |
1620 |
if (!task_info) { |
1621 |
ccs_io_printf(head, "%u %u ", pid, domain->profile); |
1622 |
ccs_set_string(head, domain->domainname->name); |
1623 |
} else { |
1624 |
ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid, |
1625 |
ccs_yesno(ccs_flags & |
1626 |
CCS_TASK_IS_MANAGER), |
1627 |
ccs_yesno(ccs_flags & |
1628 |
CCS_TASK_IS_EXECUTE_HANDLER)); |
1629 |
} |
1630 |
} |
1631 |
|
1632 |
static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = { |
1633 |
[CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ", |
1634 |
[CCS_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ", |
1635 |
[CCS_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ", |
1636 |
[CCS_TRANSITION_CONTROL_KEEP] = "keep_domain ", |
1637 |
}; |
1638 |
|
1639 |
static const char * const ccs_group_name[CCS_MAX_GROUP] = { |
1640 |
[CCS_PATH_GROUP] = "path_group ", |
1641 |
[CCS_NUMBER_GROUP] = "number_group ", |
1642 |
[CCS_ADDRESS_GROUP] = "address_group ", |
1643 |
}; |
1644 |
|
1645 |
/** |
1646 |
* ccs_write_exception - Write exception policy. |
1647 |
* |
1648 |
* @head: Pointer to "struct ccs_io_buffer". |
1649 |
* |
1650 |
* Returns 0 on success, negative value otherwise. |
1651 |
*/ |
1652 |
static int ccs_write_exception(struct ccs_io_buffer *head) |
1653 |
{ |
1654 |
char *data = head->write_buf; |
1655 |
const bool is_delete = ccs_str_starts(&data, "delete "); |
1656 |
u8 i; |
1657 |
static const struct { |
1658 |
const char *keyword; |
1659 |
int (*write) (char *, const bool); |
1660 |
} ccs_callback[2] = { |
1661 |
{ "aggregator ", ccs_write_aggregator }, |
1662 |
{ "deny_autobind ", ccs_write_reserved_port }, |
1663 |
}; |
1664 |
for (i = 0; i < 2; i++) |
1665 |
if (ccs_str_starts(&data, ccs_callback[i].keyword)) |
1666 |
return ccs_callback[i].write(data, is_delete); |
1667 |
for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) |
1668 |
if (ccs_str_starts(&data, ccs_transition_type[i])) |
1669 |
return ccs_write_transition_control(data, is_delete, |
1670 |
i); |
1671 |
for (i = 0; i < CCS_MAX_GROUP; i++) |
1672 |
if (ccs_str_starts(&data, ccs_group_name[i])) |
1673 |
return ccs_write_group(data, is_delete, i); |
1674 |
if (ccs_str_starts(&data, "acl_group ")) { |
1675 |
unsigned int group; |
1676 |
if (sscanf(data, "%u", &group) == 1 && |
1677 |
group < CCS_MAX_ACL_GROUPS) { |
1678 |
data = strchr(data, ' '); |
1679 |
if (data) |
1680 |
return ccs_write_domain2(data + 1, |
1681 |
&ccs_acl_group[group], |
1682 |
is_delete); |
1683 |
} |
1684 |
} |
1685 |
return -EINVAL; |
1686 |
} |
1687 |
|
1688 |
/** |
1689 |
* ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list. |
1690 |
* |
1691 |
* @head: Pointer to "struct ccs_io_buffer". |
1692 |
* @idx: Index number. |
1693 |
* |
1694 |
* Returns true on success, false otherwise. |
1695 |
* |
1696 |
* Caller holds ccs_read_lock(). |
1697 |
*/ |
1698 |
static bool ccs_read_group(struct ccs_io_buffer *head, const int idx) |
1699 |
{ |
1700 |
list_for_each_cookie(head->r.group, &ccs_group_list[idx]) { |
1701 |
struct ccs_group *group = |
1702 |
list_entry(head->r.group, typeof(*group), head.list); |
1703 |
list_for_each_cookie(head->r.acl, &group->member_list) { |
1704 |
struct ccs_acl_head *ptr = |
1705 |
list_entry(head->r.acl, typeof(*ptr), list); |
1706 |
if (ptr->is_deleted) |
1707 |
continue; |
1708 |
if (!ccs_flush(head)) |
1709 |
return false; |
1710 |
ccs_set_string(head, ccs_group_name[idx]); |
1711 |
ccs_set_string(head, group->group_name->name); |
1712 |
if (idx == CCS_PATH_GROUP) { |
1713 |
ccs_set_space(head); |
1714 |
ccs_set_string(head, container_of |
1715 |
(ptr, struct ccs_path_group, |
1716 |
head)->member_name->name); |
1717 |
} else if (idx == CCS_NUMBER_GROUP) { |
1718 |
ccs_print_number_union(head, &container_of |
1719 |
(ptr, struct ccs_number_group, |
1720 |
head)->number); |
1721 |
} else if (idx == CCS_ADDRESS_GROUP) { |
1722 |
char buffer[128]; |
1723 |
struct ccs_address_group *member = |
1724 |
container_of(ptr, typeof(*member), |
1725 |
head); |
1726 |
if (member->is_ipv6) |
1727 |
ccs_print_ipv6(buffer, sizeof(buffer), |
1728 |
member->min.ipv6, |
1729 |
member->max.ipv6); |
1730 |
else |
1731 |
ccs_print_ipv4(buffer, sizeof(buffer), |
1732 |
member->min.ipv4, |
1733 |
member->max.ipv4); |
1734 |
ccs_io_printf(head, " %s", buffer); |
1735 |
} |
1736 |
ccs_set_lf(head); |
1737 |
} |
1738 |
head->r.acl = NULL; |
1739 |
} |
1740 |
head->r.group = NULL; |
1741 |
return true; |
1742 |
} |
1743 |
|
1744 |
/** |
1745 |
* ccs_read_policy - Read "struct ccs_..._entry" list. |
1746 |
* |
1747 |
* @head: Pointer to "struct ccs_io_buffer". |
1748 |
* @idx: Index number. |
1749 |
* |
1750 |
* Returns true on success, false otherwise. |
1751 |
* |
1752 |
* Caller holds ccs_read_lock(). |
1753 |
*/ |
1754 |
static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx) |
1755 |
{ |
1756 |
list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) { |
1757 |
struct ccs_acl_head *acl = |
1758 |
container_of(head->r.acl, typeof(*acl), list); |
1759 |
if (acl->is_deleted) |
1760 |
continue; |
1761 |
if (!ccs_flush(head)) |
1762 |
return false; |
1763 |
switch (idx) { |
1764 |
case CCS_ID_TRANSITION_CONTROL: |
1765 |
{ |
1766 |
struct ccs_transition_control *ptr = |
1767 |
container_of(acl, typeof(*ptr), head); |
1768 |
ccs_set_string(head, |
1769 |
ccs_transition_type[ptr->type]); |
1770 |
ccs_set_string(head, ptr->program ? |
1771 |
ptr->program->name : "any"); |
1772 |
ccs_set_string(head, " from "); |
1773 |
ccs_set_string(head, ptr->domainname ? |
1774 |
ptr->domainname->name : "any"); |
1775 |
} |
1776 |
break; |
1777 |
case CCS_ID_AGGREGATOR: |
1778 |
{ |
1779 |
struct ccs_aggregator *ptr = |
1780 |
container_of(acl, typeof(*ptr), head); |
1781 |
ccs_set_string(head, "aggregator "); |
1782 |
ccs_set_string(head, ptr->original_name->name); |
1783 |
ccs_set_space(head); |
1784 |
ccs_set_string(head, |
1785 |
ptr->aggregated_name->name); |
1786 |
} |
1787 |
break; |
1788 |
case CCS_ID_RESERVEDPORT: |
1789 |
{ |
1790 |
struct ccs_reserved *ptr = |
1791 |
container_of(acl, typeof(*ptr), head); |
1792 |
const u16 min_port = ptr->min_port; |
1793 |
const u16 max_port = ptr->max_port; |
1794 |
ccs_set_string(head, "deny_autobind "); |
1795 |
ccs_io_printf(head, "%u", min_port); |
1796 |
if (min_port != max_port) |
1797 |
ccs_io_printf(head, "-%u", max_port); |
1798 |
} |
1799 |
break; |
1800 |
default: |
1801 |
continue; |
1802 |
} |
1803 |
ccs_set_lf(head); |
1804 |
} |
1805 |
head->r.acl = NULL; |
1806 |
return true; |
1807 |
} |
1808 |
|
1809 |
/** |
1810 |
* ccs_read_exception - Read exception policy. |
1811 |
* |
1812 |
* @head: Pointer to "struct ccs_io_buffer". |
1813 |
* |
1814 |
* Caller holds ccs_read_lock(). |
1815 |
*/ |
1816 |
static void ccs_read_exception(struct ccs_io_buffer *head) |
1817 |
{ |
1818 |
if (head->r.eof) |
1819 |
return; |
1820 |
while (head->r.step < CCS_MAX_POLICY && |
1821 |
ccs_read_policy(head, head->r.step)) |
1822 |
head->r.step++; |
1823 |
if (head->r.step < CCS_MAX_POLICY) |
1824 |
return; |
1825 |
while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP && |
1826 |
ccs_read_group(head, head->r.step - CCS_MAX_POLICY)) |
1827 |
head->r.step++; |
1828 |
if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP) |
1829 |
return; |
1830 |
while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP |
1831 |
+ CCS_MAX_ACL_GROUPS * 2) { |
1832 |
head->r.group_index = (head->r.step - CCS_MAX_POLICY |
1833 |
- CCS_MAX_GROUP) / 2; |
1834 |
if (!ccs_read_domain2(head, |
1835 |
&ccs_acl_group[head->r.group_index], |
1836 |
head->r.step & 1)) |
1837 |
return; |
1838 |
head->r.step++; |
1839 |
} |
1840 |
head->r.eof = true; |
1841 |
} |
1842 |
|
1843 |
/* Wait queue for ccs_query_list. */ |
1844 |
static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait); |
1845 |
static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait); |
1846 |
|
1847 |
/* Lock for manipulating ccs_query_list. */ |
1848 |
static DEFINE_SPINLOCK(ccs_query_list_lock); |
1849 |
|
1850 |
/* Structure for query. */ |
1851 |
struct ccs_query { |
1852 |
struct list_head list; |
1853 |
char *query; |
1854 |
int query_len; |
1855 |
unsigned int serial; |
1856 |
int timer; |
1857 |
int answer; |
1858 |
u8 retry; |
1859 |
}; |
1860 |
|
1861 |
/* The list for "struct ccs_query". */ |
1862 |
static LIST_HEAD(ccs_query_list); |
1863 |
|
1864 |
/* Number of "struct file" referring /proc/ccs/query interface. */ |
1865 |
static atomic_t ccs_query_observers = ATOMIC_INIT(0); |
1866 |
|
1867 |
static int ccs_truncate(char *str) |
1868 |
{ |
1869 |
char *start = str; |
1870 |
while (*(unsigned char *) str > (unsigned char) ' ') |
1871 |
str++; |
1872 |
*str = '\0'; |
1873 |
return strlen(start) + 1; |
1874 |
} |
1875 |
|
1876 |
static void ccs_add_entry(char *header) |
1877 |
{ |
1878 |
char *buffer; |
1879 |
char *realpath = NULL; |
1880 |
char *argv0 = NULL; |
1881 |
char *symlink = NULL; |
1882 |
char *handler; |
1883 |
char *cp = strchr(header, '\n'); |
1884 |
int len; |
1885 |
if (!cp) |
1886 |
return; |
1887 |
cp = strchr(cp + 1, '\n'); |
1888 |
if (!cp) |
1889 |
return; |
1890 |
*cp++ = '\0'; |
1891 |
len = strlen(cp) + 1; |
1892 |
/* strstr() will return NULL if ordering is wrong. */ |
1893 |
if (*cp == 'f') { |
1894 |
argv0 = strstr(header, " argv[]={ \""); |
1895 |
if (argv0) { |
1896 |
argv0 += 10; |
1897 |
len += ccs_truncate(argv0) + 14; |
1898 |
} |
1899 |
realpath = strstr(header, " exec={ realpath=\""); |
1900 |
if (realpath) { |
1901 |
realpath += 8; |
1902 |
len += ccs_truncate(realpath) + 6; |
1903 |
} |
1904 |
symlink = strstr(header, " symlink.target=\""); |
1905 |
if (symlink) |
1906 |
len += ccs_truncate(symlink + 1) + 1; |
1907 |
} |
1908 |
handler = strstr(header, "type=execute_handler"); |
1909 |
if (handler) |
1910 |
len += ccs_truncate(handler) + 6; |
1911 |
buffer = kmalloc(len, CCS_GFP_FLAGS); |
1912 |
if (!buffer) |
1913 |
return; |
1914 |
snprintf(buffer, len - 1, "%s", cp); |
1915 |
if (handler) |
1916 |
ccs_addprintf(buffer, len, " task.%s", handler); |
1917 |
if (realpath) |
1918 |
ccs_addprintf(buffer, len, " exec.%s", realpath); |
1919 |
if (argv0) |
1920 |
ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0); |
1921 |
if (symlink) |
1922 |
ccs_addprintf(buffer, len, "%s", symlink); |
1923 |
ccs_normalize_line(buffer); |
1924 |
if (!ccs_write_domain2(buffer, ccs_current_domain(), false)) |
1925 |
ccs_update_stat(CCS_STAT_POLICY_UPDATES); |
1926 |
kfree(buffer); |
1927 |
} |
1928 |
|
1929 |
/** |
1930 |
* ccs_supervisor - Ask for the supervisor's decision. |
1931 |
* |
1932 |
* @r: Pointer to "struct ccs_request_info". |
1933 |
* @fmt: The printf()'s format string, followed by parameters. |
1934 |
* |
1935 |
* Returns 0 if the supervisor decided to permit the access request which |
1936 |
* violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor |
1937 |
* decided to retry the access request which violated the policy in enforcing |
1938 |
* mode, 0 if it is not in enforcing mode, -EPERM otherwise. |
1939 |
*/ |
1940 |
int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...) |
1941 |
{ |
1942 |
va_list args; |
1943 |
int error; |
1944 |
int len; |
1945 |
static unsigned int ccs_serial; |
1946 |
struct ccs_query entry = { }; |
1947 |
bool quota_exceeded = false; |
1948 |
va_start(args, fmt); |
1949 |
len = vsnprintf((char *) &len, 1, fmt, args) + 1; |
1950 |
va_end(args); |
1951 |
/* Write /proc/ccs/grant_log or /proc/ccs/reject_log . */ |
1952 |
va_start(args, fmt); |
1953 |
ccs_write_log2(r, len, fmt, args); |
1954 |
va_end(args); |
1955 |
/* Nothing more to do if granted. */ |
1956 |
if (r->granted) |
1957 |
return 0; |
1958 |
if (r->mode) |
1959 |
ccs_update_stat(r->mode); |
1960 |
switch (r->mode) { |
1961 |
int i; |
1962 |
struct ccs_profile *p; |
1963 |
case CCS_CONFIG_ENFORCING: |
1964 |
error = -EPERM; |
1965 |
if (atomic_read(&ccs_query_observers)) |
1966 |
break; |
1967 |
if (ccs_current_flags() & CCS_DONT_SLEEP_ON_ENFORCE_ERROR) |
1968 |
goto out; |
1969 |
p = ccs_profile(r->profile); |
1970 |
/* Check enforcing_penalty parameter. */ |
1971 |
for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) { |
1972 |
set_current_state(TASK_INTERRUPTIBLE); |
1973 |
schedule_timeout(HZ / 10); |
1974 |
} |
1975 |
goto out; |
1976 |
case CCS_CONFIG_LEARNING: |
1977 |
error = 0; |
1978 |
/* Check mac_learning_entry parameter. */ |
1979 |
if (ccs_domain_quota_ok(r)) |
1980 |
break; |
1981 |
/* fall through */ |
1982 |
default: |
1983 |
return 0; |
1984 |
} |
1985 |
/* Get message. */ |
1986 |
va_start(args, fmt); |
1987 |
entry.query = ccs_init_log(r, len, fmt, args); |
1988 |
va_end(args); |
1989 |
if (!entry.query) |
1990 |
goto out; |
1991 |
entry.query_len = strlen(entry.query) + 1; |
1992 |
if (!error) { |
1993 |
ccs_add_entry(entry.query); |
1994 |
goto out; |
1995 |
} |
1996 |
len = ccs_round2(entry.query_len); |
1997 |
spin_lock(&ccs_query_list_lock); |
1998 |
if (ccs_memory_quota[CCS_MEMORY_QUERY] && |
1999 |
ccs_memory_used[CCS_MEMORY_QUERY] + len |
2000 |
>= ccs_memory_quota[CCS_MEMORY_QUERY]) { |
2001 |
quota_exceeded = true; |
2002 |
} else { |
2003 |
entry.serial = ccs_serial++; |
2004 |
entry.retry = r->retry; |
2005 |
ccs_memory_used[CCS_MEMORY_QUERY] += len; |
2006 |
list_add_tail(&entry.list, &ccs_query_list); |
2007 |
} |
2008 |
spin_unlock(&ccs_query_list_lock); |
2009 |
if (quota_exceeded) |
2010 |
goto out; |
2011 |
/* Give 10 seconds for supervisor's opinion. */ |
2012 |
while (entry.timer < 10) { |
2013 |
wake_up_all(&ccs_query_wait); |
2014 |
if (wait_event_interruptible_timeout |
2015 |
(ccs_answer_wait, entry.answer || |
2016 |
!atomic_read(&ccs_query_observers), HZ)) |
2017 |
break; |
2018 |
else |
2019 |
entry.timer++; |
2020 |
} |
2021 |
spin_lock(&ccs_query_list_lock); |
2022 |
list_del(&entry.list); |
2023 |
ccs_memory_used[CCS_MEMORY_QUERY] -= len; |
2024 |
spin_unlock(&ccs_query_list_lock); |
2025 |
switch (entry.answer) { |
2026 |
case 3: /* Asked to retry by administrator. */ |
2027 |
error = CCS_RETRY_REQUEST; |
2028 |
r->retry++; |
2029 |
break; |
2030 |
case 1: |
2031 |
/* Granted by administrator. */ |
2032 |
error = 0; |
2033 |
break; |
2034 |
default: |
2035 |
/* Timed out or rejected by administrator. */ |
2036 |
break; |
2037 |
} |
2038 |
out: |
2039 |
kfree(entry.query); |
2040 |
return error; |
2041 |
} |
2042 |
|
2043 |
/** |
2044 |
* ccs_poll_query - poll() for /proc/ccs/query. |
2045 |
* |
2046 |
* @file: Pointer to "struct file". |
2047 |
* @wait: Pointer to "poll_table". |
2048 |
* |
2049 |
* Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise. |
2050 |
* |
2051 |
* Waits for access requests which violated policy in enforcing mode. |
2052 |
*/ |
2053 |
static int ccs_poll_query(struct file *file, poll_table *wait) |
2054 |
{ |
2055 |
struct list_head *tmp; |
2056 |
bool found = false; |
2057 |
u8 i; |
2058 |
for (i = 0; i < 2; i++) { |
2059 |
spin_lock(&ccs_query_list_lock); |
2060 |
list_for_each(tmp, &ccs_query_list) { |
2061 |
struct ccs_query *ptr = |
2062 |
list_entry(tmp, typeof(*ptr), list); |
2063 |
if (ptr->answer) |
2064 |
continue; |
2065 |
found = true; |
2066 |
break; |
2067 |
} |
2068 |
spin_unlock(&ccs_query_list_lock); |
2069 |
if (found) |
2070 |
return POLLIN | POLLRDNORM; |
2071 |
if (i) |
2072 |
break; |
2073 |
poll_wait(file, &ccs_query_wait, wait); |
2074 |
} |
2075 |
return 0; |
2076 |
} |
2077 |
|
2078 |
/** |
2079 |
* ccs_read_query - Read access requests which violated policy in enforcing mode. |
2080 |
* |
2081 |
* @head: Pointer to "struct ccs_io_buffer". |
2082 |
*/ |
2083 |
static void ccs_read_query(struct ccs_io_buffer *head) |
2084 |
{ |
2085 |
struct list_head *tmp; |
2086 |
int pos = 0; |
2087 |
int len = 0; |
2088 |
char *buf; |
2089 |
if (head->r.w_pos) |
2090 |
return; |
2091 |
if (head->read_buf) { |
2092 |
kfree(head->read_buf); |
2093 |
head->read_buf = NULL; |
2094 |
} |
2095 |
spin_lock(&ccs_query_list_lock); |
2096 |
list_for_each(tmp, &ccs_query_list) { |
2097 |
struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); |
2098 |
if (ptr->answer) |
2099 |
continue; |
2100 |
if (pos++ != head->r.query_index) |
2101 |
continue; |
2102 |
len = ptr->query_len; |
2103 |
break; |
2104 |
} |
2105 |
spin_unlock(&ccs_query_list_lock); |
2106 |
if (!len) { |
2107 |
head->r.query_index = 0; |
2108 |
return; |
2109 |
} |
2110 |
buf = kzalloc(len + 32, CCS_GFP_FLAGS); |
2111 |
if (!buf) |
2112 |
return; |
2113 |
pos = 0; |
2114 |
spin_lock(&ccs_query_list_lock); |
2115 |
list_for_each(tmp, &ccs_query_list) { |
2116 |
struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); |
2117 |
if (ptr->answer) |
2118 |
continue; |
2119 |
if (pos++ != head->r.query_index) |
2120 |
continue; |
2121 |
/* |
2122 |
* Some query can be skipped because ccs_query_list |
2123 |
* can change, but I don't care. |
2124 |
*/ |
2125 |
if (len == ptr->query_len) |
2126 |
snprintf(buf, len + 32, "Q%u-%hu\n%s", ptr->serial, |
2127 |
ptr->retry, ptr->query); |
2128 |
break; |
2129 |
} |
2130 |
spin_unlock(&ccs_query_list_lock); |
2131 |
if (buf[0]) { |
2132 |
head->read_buf = buf; |
2133 |
head->r.w[head->r.w_pos++] = buf; |
2134 |
head->r.query_index++; |
2135 |
} else { |
2136 |
kfree(buf); |
2137 |
} |
2138 |
} |
2139 |
|
2140 |
/** |
2141 |
* ccs_write_answer - Write the supervisor's decision. |
2142 |
* |
2143 |
* @head: Pointer to "struct ccs_io_buffer". |
2144 |
* |
2145 |
* Returns 0 on success, -EINVAL otherwise. |
2146 |
*/ |
2147 |
static int ccs_write_answer(struct ccs_io_buffer *head) |
2148 |
{ |
2149 |
char *data = head->write_buf; |
2150 |
struct list_head *tmp; |
2151 |
unsigned int serial; |
2152 |
unsigned int answer; |
2153 |
spin_lock(&ccs_query_list_lock); |
2154 |
list_for_each(tmp, &ccs_query_list) { |
2155 |
struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); |
2156 |
ptr->timer = 0; |
2157 |
} |
2158 |
spin_unlock(&ccs_query_list_lock); |
2159 |
if (sscanf(data, "A%u=%u", &serial, &answer) != 2) |
2160 |
return -EINVAL; |
2161 |
spin_lock(&ccs_query_list_lock); |
2162 |
list_for_each(tmp, &ccs_query_list) { |
2163 |
struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); |
2164 |
if (ptr->serial != serial) |
2165 |
continue; |
2166 |
if (!ptr->answer) |
2167 |
ptr->answer = answer; |
2168 |
break; |
2169 |
} |
2170 |
spin_unlock(&ccs_query_list_lock); |
2171 |
wake_up_all(&ccs_answer_wait); |
2172 |
return 0; |
2173 |
} |
2174 |
|
2175 |
/** |
2176 |
* ccs_read_version: Get version. |
2177 |
* |
2178 |
* @head: Pointer to "struct ccs_io_buffer". |
2179 |
*/ |
2180 |
static void ccs_read_version(struct ccs_io_buffer *head) |
2181 |
{ |
2182 |
if (head->r.eof) |
2183 |
return; |
2184 |
ccs_set_string(head, "1.8.0-pre"); |
2185 |
head->r.eof = true; |
2186 |
} |
2187 |
|
2188 |
/** |
2189 |
* ccs_open_control - open() for /proc/ccs/ interface. |
2190 |
* |
2191 |
* @type: Type of interface. |
2192 |
* @file: Pointer to "struct file". |
2193 |
* |
2194 |
* Associates policy handler and returns 0 on success, -ENOMEM otherwise. |
2195 |
*/ |
2196 |
int ccs_open_control(const u8 type, struct file *file) |
2197 |
{ |
2198 |
struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS); |
2199 |
if (!head) |
2200 |
return -ENOMEM; |
2201 |
mutex_init(&head->io_sem); |
2202 |
head->type = type; |
2203 |
switch (type) { |
2204 |
case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */ |
2205 |
head->write = ccs_write_domain; |
2206 |
head->read = ccs_read_domain; |
2207 |
break; |
2208 |
case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */ |
2209 |
head->write = ccs_write_exception; |
2210 |
head->read = ccs_read_exception; |
2211 |
break; |
2212 |
case CCS_GRANTLOG: /* /proc/ccs/grant_log */ |
2213 |
case CCS_REJECTLOG: /* /proc/ccs/reject_log */ |
2214 |
head->poll = ccs_poll_log; |
2215 |
head->read = ccs_read_log; |
2216 |
break; |
2217 |
case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */ |
2218 |
head->write = ccs_write_domain_profile; |
2219 |
head->read = ccs_read_domain_profile; |
2220 |
break; |
2221 |
case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */ |
2222 |
/* Allow execute_handler to read process's status. */ |
2223 |
if (!(ccs_current_flags() & CCS_TASK_IS_EXECUTE_HANDLER)) { |
2224 |
kfree(head); |
2225 |
return -EPERM; |
2226 |
} |
2227 |
/* fall through */ |
2228 |
case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */ |
2229 |
head->write = ccs_write_pid; |
2230 |
head->read = ccs_read_pid; |
2231 |
break; |
2232 |
case CCS_VERSION: /* /proc/ccs/version */ |
2233 |
head->read = ccs_read_version; |
2234 |
head->readbuf_size = 128; |
2235 |
break; |
2236 |
case CCS_MEMINFO: /* /proc/ccs/meminfo */ |
2237 |
head->write = ccs_write_memory_quota; |
2238 |
head->read = ccs_read_memory_counter; |
2239 |
head->readbuf_size = 512; |
2240 |
break; |
2241 |
case CCS_PROFILE: /* /proc/ccs/profile */ |
2242 |
head->write = ccs_write_profile; |
2243 |
head->read = ccs_read_profile; |
2244 |
break; |
2245 |
case CCS_QUERY: /* /proc/ccs/query */ |
2246 |
head->poll = ccs_poll_query; |
2247 |
head->write = ccs_write_answer; |
2248 |
head->read = ccs_read_query; |
2249 |
break; |
2250 |
case CCS_MANAGER: /* /proc/ccs/manager */ |
2251 |
head->write = ccs_write_manager; |
2252 |
head->read = ccs_read_manager; |
2253 |
break; |
2254 |
} |
2255 |
if (!(file->f_mode & FMODE_READ)) { |
2256 |
/* |
2257 |
* No need to allocate read_buf since it is not opened |
2258 |
* for reading. |
2259 |
*/ |
2260 |
head->read = NULL; |
2261 |
head->poll = NULL; |
2262 |
} else if (!head->poll) { |
2263 |
/* Don't allocate read_buf for poll() access. */ |
2264 |
if (!head->readbuf_size) |
2265 |
head->readbuf_size = 4096; |
2266 |
head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS); |
2267 |
if (!head->read_buf) { |
2268 |
kfree(head); |
2269 |
return -ENOMEM; |
2270 |
} |
2271 |
} |
2272 |
if (!(file->f_mode & FMODE_WRITE)) { |
2273 |
/* |
2274 |
* No need to allocate write_buf since it is not opened |
2275 |
* for writing. |
2276 |
*/ |
2277 |
head->write = NULL; |
2278 |
} else if (head->write) { |
2279 |
head->writebuf_size = 4096; |
2280 |
head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS); |
2281 |
if (!head->write_buf) { |
2282 |
kfree(head->read_buf); |
2283 |
kfree(head); |
2284 |
return -ENOMEM; |
2285 |
} |
2286 |
} |
2287 |
/* |
2288 |
* If the file is /proc/ccs/query , increment the observer counter. |
2289 |
* The obserber counter is used by ccs_supervisor() to see if |
2290 |
* there is some process monitoring /proc/ccs/query. |
2291 |
*/ |
2292 |
if (type == CCS_QUERY) |
2293 |
atomic_inc(&ccs_query_observers); |
2294 |
else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG) |
2295 |
head->reader_idx = ccs_lock(); |
2296 |
file->private_data = head; |
2297 |
return 0; |
2298 |
} |
2299 |
|
2300 |
/** |
2301 |
* ccs_poll_control - poll() for /proc/ccs/ interface. |
2302 |
* |
2303 |
* @file: Pointer to "struct file". |
2304 |
* @wait: Pointer to "poll_table". |
2305 |
* |
2306 |
* Waits for read readiness. |
2307 |
* /proc/ccs/query is handled by /usr/sbin/ccs-queryd and |
2308 |
* /proc/ccs/grant_log and /proc/ccs/reject_log are handled by |
2309 |
* /usr/sbin/ccs-auditd . |
2310 |
*/ |
2311 |
int ccs_poll_control(struct file *file, poll_table *wait) |
2312 |
{ |
2313 |
struct ccs_io_buffer *head = file->private_data; |
2314 |
if (!head->poll) |
2315 |
return -ENOSYS; |
2316 |
return head->poll(file, wait); |
2317 |
} |
2318 |
|
2319 |
/** |
2320 |
* ccs_read_control - read() for /proc/ccs/ interface. |
2321 |
* |
2322 |
* @file: Pointer to "struct file". |
2323 |
* @buffer: Poiner to buffer to write to. |
2324 |
* @buffer_len: Size of @buffer. |
2325 |
* |
2326 |
* Returns bytes read on success, negative value otherwise. |
2327 |
*/ |
2328 |
int ccs_read_control(struct file *file, char __user *buffer, |
2329 |
const int buffer_len) |
2330 |
{ |
2331 |
int len; |
2332 |
struct ccs_io_buffer *head = file->private_data; |
2333 |
int idx; |
2334 |
if (!head->read) |
2335 |
return -ENOSYS; |
2336 |
if (!access_ok(VERIFY_WRITE, buffer, buffer_len)) |
2337 |
return -EFAULT; |
2338 |
if (mutex_lock_interruptible(&head->io_sem)) |
2339 |
return -EINTR; |
2340 |
head->read_user_buf = buffer; |
2341 |
head->read_user_buf_avail = buffer_len; |
2342 |
idx = ccs_read_lock(); |
2343 |
if (ccs_flush(head)) |
2344 |
/* Call the policy handler. */ |
2345 |
head->read(head); |
2346 |
ccs_flush(head); |
2347 |
ccs_read_unlock(idx); |
2348 |
len = head->read_user_buf - buffer; |
2349 |
mutex_unlock(&head->io_sem); |
2350 |
return len; |
2351 |
} |
2352 |
|
2353 |
/** |
2354 |
* ccs_write_control - write() for /proc/ccs/ interface. |
2355 |
* |
2356 |
* @file: Pointer to "struct file". |
2357 |
* @buffer: Pointer to buffer to read from. |
2358 |
* @buffer_len: Size of @buffer. |
2359 |
* |
2360 |
* Returns @buffer_len on success, negative value otherwise. |
2361 |
*/ |
2362 |
int ccs_write_control(struct file *file, const char __user *buffer, |
2363 |
const int buffer_len) |
2364 |
{ |
2365 |
struct ccs_io_buffer *head = file->private_data; |
2366 |
int error = buffer_len; |
2367 |
int avail_len = buffer_len; |
2368 |
char *cp0 = head->write_buf; |
2369 |
int idx; |
2370 |
if (!head->write) |
2371 |
return -ENOSYS; |
2372 |
if (!access_ok(VERIFY_READ, buffer, buffer_len)) |
2373 |
return -EFAULT; |
2374 |
if (mutex_lock_interruptible(&head->io_sem)) |
2375 |
return -EINTR; |
2376 |
idx = ccs_read_lock(); |
2377 |
/* Don't allow updating policies by non manager programs. */ |
2378 |
if (head->write != ccs_write_pid && head->write != ccs_write_domain && |
2379 |
!ccs_manager()) { |
2380 |
ccs_read_unlock(idx); |
2381 |
mutex_unlock(&head->io_sem); |
2382 |
return -EPERM; |
2383 |
} |
2384 |
/* Read a line and dispatch it to the policy handler. */ |
2385 |
while (avail_len > 0) { |
2386 |
char c; |
2387 |
if (head->w.avail >= head->writebuf_size - 1) { |
2388 |
const int len = head->writebuf_size * 2; |
2389 |
char *cp = kzalloc(len, CCS_GFP_FLAGS); |
2390 |
if (!cp) { |
2391 |
error = -ENOMEM; |
2392 |
break; |
2393 |
} |
2394 |
memmove(cp, cp0, head->w.avail); |
2395 |
kfree(cp0); |
2396 |
head->write_buf = cp; |
2397 |
cp0 = cp; |
2398 |
head->writebuf_size = len; |
2399 |
} |
2400 |
if (get_user(c, buffer)) { |
2401 |
error = -EFAULT; |
2402 |
break; |
2403 |
} |
2404 |
buffer++; |
2405 |
avail_len--; |
2406 |
cp0[head->w.avail++] = c; |
2407 |
if (c != '\n') |
2408 |
continue; |
2409 |
cp0[head->w.avail - 1] = '\0'; |
2410 |
head->w.avail = 0; |
2411 |
ccs_normalize_line(cp0); |
2412 |
if (head->write(head)) |
2413 |
continue; |
2414 |
switch (head->type) { |
2415 |
case CCS_DOMAINPOLICY: |
2416 |
case CCS_EXCEPTIONPOLICY: |
2417 |
case CCS_DOMAIN_STATUS: |
2418 |
case CCS_MEMINFO: |
2419 |
case CCS_PROFILE: |
2420 |
case CCS_MANAGER: |
2421 |
ccs_update_stat(CCS_STAT_POLICY_UPDATES); |
2422 |
} |
2423 |
} |
2424 |
ccs_read_unlock(idx); |
2425 |
mutex_unlock(&head->io_sem); |
2426 |
return error; |
2427 |
} |
2428 |
|
2429 |
/** |
2430 |
* ccs_close_control - close() for /proc/ccs/ interface. |
2431 |
* |
2432 |
* @file: Pointer to "struct file". |
2433 |
* |
2434 |
* Releases memory and returns 0. |
2435 |
*/ |
2436 |
int ccs_close_control(struct file *file) |
2437 |
{ |
2438 |
struct ccs_io_buffer *head = file->private_data; |
2439 |
const bool is_write = head->write_buf != NULL; |
2440 |
const u8 type = head->type; |
2441 |
/* |
2442 |
* If the file is /proc/ccs/query , decrement the observer counter. |
2443 |
*/ |
2444 |
if (type == CCS_QUERY) { |
2445 |
if (atomic_dec_and_test(&ccs_query_observers)) |
2446 |
wake_up_all(&ccs_answer_wait); |
2447 |
} else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG) |
2448 |
ccs_unlock(head->reader_idx); |
2449 |
/* Release memory used for policy I/O. */ |
2450 |
kfree(head->read_buf); |
2451 |
head->read_buf = NULL; |
2452 |
kfree(head->write_buf); |
2453 |
head->write_buf = NULL; |
2454 |
kfree(head); |
2455 |
head = NULL; |
2456 |
file->private_data = NULL; |
2457 |
if (is_write) |
2458 |
ccs_run_gc(); |
2459 |
return 0; |
2460 |
} |
2461 |
|
2462 |
void __init ccs_policy_io_init(void) |
2463 |
{ |
2464 |
ccsecurity_ops.check_profile = ccs_check_profile; |
2465 |
} |