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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4049 - (show annotations) (download) (as text)
Thu Oct 7 07:14:01 2010 UTC (13 years, 6 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 66946 byte(s)
Merge branches/ccs-patch/ into trunk/1.8.x/ccs-patch/
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/05
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 panic("Profile %u (used by '%s') not defined.\n",
364 profile, domain->domainname->name);
365 }
366 ccs_read_unlock(idx);
367 if (ccs_profile_version != 20100903)
368 panic("Profile version %u is not supported.\n",
369 ccs_profile_version);
370 printk(KERN_INFO "CCSecurity: 1.8.0-pre 2010/10/05\n");
371 printk(KERN_INFO "Mandatory Access Control activated.\n");
372 }
373
374 /**
375 * ccs_profile - Find a profile.
376 *
377 * @profile: Profile number to find.
378 *
379 * Returns pointer to "struct ccs_profile".
380 */
381 struct ccs_profile *ccs_profile(const u8 profile)
382 {
383 static struct ccs_profile ccs_null_profile;
384 struct ccs_profile *ptr = ccs_profile_ptr[profile];
385 if (!ptr)
386 ptr = &ccs_null_profile;
387 return ptr;
388 }
389
390 static s8 ccs_find_yesno(const char *string, const char *find)
391 {
392 const char *cp = strstr(string, find);
393 if (cp) {
394 cp += strlen(find);
395 if (!strncmp(cp, "=yes", 4))
396 return 1;
397 else if (!strncmp(cp, "=no", 3))
398 return 0;
399 }
400 return -1;
401 }
402
403 static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
404 {
405 const char *cp = strstr(string, find);
406 if (cp)
407 sscanf(cp + strlen(find), "=%u", i);
408 }
409
410 static int ccs_set_mode(char *name, const char *value,
411 struct ccs_profile *profile)
412 {
413 u8 i;
414 u8 config;
415 if (!strcmp(name, "CONFIG")) {
416 i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
417 config = profile->default_config;
418 } else if (ccs_str_starts(&name, "CONFIG::")) {
419 config = 0;
420 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
421 i++) {
422 int len = 0;
423 if (i < CCS_MAX_MAC_INDEX) {
424 const u8 c = ccs_index2category[i];
425 const char *category =
426 ccs_category_keywords[c];
427 len = strlen(category);
428 if (strncmp(name, category, len) ||
429 name[len++] != ':' || name[len++] != ':')
430 continue;
431 }
432 if (strcmp(name + len, ccs_mac_keywords[i]))
433 continue;
434 config = profile->config[i];
435 break;
436 }
437 if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
438 return -EINVAL;
439 } else {
440 return -EINVAL;
441 }
442 if (strstr(value, "use_default")) {
443 config = CCS_CONFIG_USE_DEFAULT;
444 } else {
445 u8 mode;
446 for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
447 if (strstr(value, ccs_mode[mode]))
448 /*
449 * Update lower 3 bits in order to distinguish
450 * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
451 */
452 config = (config & ~7) | mode;
453 if (config != CCS_CONFIG_USE_DEFAULT) {
454 switch (ccs_find_yesno(value, "grant_log")) {
455 case 1:
456 config |= CCS_CONFIG_WANT_GRANT_LOG;
457 break;
458 case 0:
459 config &= ~CCS_CONFIG_WANT_GRANT_LOG;
460 break;
461 }
462 switch (ccs_find_yesno(value, "reject_log")) {
463 case 1:
464 config |= CCS_CONFIG_WANT_REJECT_LOG;
465 break;
466 case 0:
467 config &= ~CCS_CONFIG_WANT_REJECT_LOG;
468 break;
469 }
470 }
471 }
472 if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
473 profile->config[i] = config;
474 else if (config != CCS_CONFIG_USE_DEFAULT)
475 profile->default_config = config;
476 return 0;
477 }
478
479 /**
480 * ccs_write_profile - Write profile table.
481 *
482 * @head: Pointer to "struct ccs_io_buffer".
483 *
484 * Returns 0 on success, negative value otherwise.
485 */
486 static int ccs_write_profile(struct ccs_io_buffer *head)
487 {
488 char *data = head->write_buf;
489 unsigned int i;
490 char *cp;
491 struct ccs_profile *profile;
492 if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
493 return 0;
494 i = simple_strtoul(data, &cp, 10);
495 if (*cp != '-')
496 return -EINVAL;
497 data = cp + 1;
498 profile = ccs_assign_profile(i);
499 if (!profile)
500 return -EINVAL;
501 cp = strchr(data, '=');
502 if (!cp)
503 return -EINVAL;
504 *cp++ = '\0';
505 if (!strcmp(data, "COMMENT")) {
506 const struct ccs_path_info *old_comment = profile->comment;
507 profile->comment = ccs_get_name(cp);
508 ccs_put_name(old_comment);
509 return 0;
510 }
511 if (!strcmp(data, "PREFERENCE")) {
512 for (i = 0; i < CCS_MAX_PREF; i++)
513 ccs_set_uint(&profile->pref[i], cp,
514 ccs_pref_keywords[i]);
515 return 0;
516 }
517 return ccs_set_mode(data, cp, profile);
518 }
519
520 static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
521 {
522 ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
523 ccs_mode[config & 3],
524 ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
525 ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
526 }
527
528 /**
529 * ccs_read_profile - Read profile table.
530 *
531 * @head: Pointer to "struct ccs_io_buffer".
532 */
533 static void ccs_read_profile(struct ccs_io_buffer *head)
534 {
535 u8 index;
536 const struct ccs_profile *profile;
537 next:
538 index = head->r.index;
539 profile = ccs_profile_ptr[index];
540 switch (head->r.step) {
541 case 0:
542 ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20100903");
543 head->r.step++;
544 break;
545 case 1:
546 for ( ; head->r.index < CCS_MAX_PROFILES;
547 head->r.index++)
548 if (ccs_profile_ptr[head->r.index])
549 break;
550 if (head->r.index == CCS_MAX_PROFILES)
551 return;
552 head->r.step++;
553 break;
554 case 2:
555 {
556 u8 i;
557 const struct ccs_path_info *comment = profile->comment;
558 ccs_io_printf(head, "%u-COMMENT=", index);
559 ccs_set_string(head, comment ? comment->name : "");
560 ccs_set_lf(head);
561 ccs_io_printf(head, "%u-PREFERENCE={ ", index);
562 for (i = 0; i < CCS_MAX_PREF; i++)
563 ccs_io_printf(head, "%s=%u ",
564 ccs_pref_keywords[i],
565 profile->pref[i]);
566 ccs_set_string(head, " }\n");
567 head->r.step++;
568 }
569 break;
570 case 3:
571 {
572 ccs_io_printf(head, "%u-%s", index, "CONFIG");
573 ccs_print_config(head, profile->default_config);
574 head->r.bit = 0;
575 head->r.step++;
576 }
577 break;
578 case 4:
579 for ( ; head->r.bit < CCS_MAX_MAC_INDEX
580 + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
581 const u8 i = head->r.bit;
582 const u8 config = profile->config[i];
583 if (config == CCS_CONFIG_USE_DEFAULT)
584 continue;
585 if (i < CCS_MAX_MAC_INDEX)
586 ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
587 ccs_category_keywords
588 [ccs_index2category[i]],
589 ccs_mac_keywords[i]);
590 else
591 ccs_io_printf(head, "%u-CONFIG::%s", index,
592 ccs_mac_keywords[i]);
593 ccs_print_config(head, config);
594 head->r.bit++;
595 break;
596 }
597 if (head->r.bit == CCS_MAX_MAC_INDEX
598 + CCS_MAX_MAC_CATEGORY_INDEX) {
599 head->r.index++;
600 head->r.step = 1;
601 }
602 break;
603 }
604 if (ccs_flush(head))
605 goto next;
606 }
607
608 static bool ccs_same_manager(const struct ccs_acl_head *a,
609 const struct ccs_acl_head *b)
610 {
611 return container_of(a, struct ccs_manager, head)->manager
612 == container_of(b, struct ccs_manager, head)->manager;
613 }
614
615 /**
616 * ccs_update_manager_entry - Add a manager entry.
617 *
618 * @manager: The path to manager or the domainnamme.
619 * @is_delete: True if it is a delete request.
620 *
621 * Returns 0 on success, negative value otherwise.
622 */
623 static int ccs_update_manager_entry(const char *manager, const bool is_delete)
624 {
625 struct ccs_manager e = { };
626 int error = is_delete ? -ENOENT : -ENOMEM;
627 if (ccs_domain_def(manager)) {
628 if (!ccs_correct_domain(manager))
629 return -EINVAL;
630 e.is_domain = true;
631 } else {
632 if (!ccs_correct_path(manager))
633 return -EINVAL;
634 }
635 e.manager = ccs_get_name(manager);
636 if (!e.manager)
637 return error;
638 error = ccs_update_policy(&e.head, sizeof(e), is_delete,
639 &ccs_policy_list[CCS_ID_MANAGER],
640 ccs_same_manager);
641 ccs_put_name(e.manager);
642 return error;
643 }
644
645 /**
646 * ccs_write_manager - Write manager policy.
647 *
648 * @head: Pointer to "struct ccs_io_buffer".
649 *
650 * Returns 0 on success, negative value otherwise.
651 */
652 static int ccs_write_manager(struct ccs_io_buffer *head)
653 {
654 char *data = head->write_buf;
655 bool is_delete = ccs_str_starts(&data, "delete ");
656 if (!strcmp(data, "manage_by_non_root")) {
657 ccs_manage_by_non_root = !is_delete;
658 return 0;
659 }
660 return ccs_update_manager_entry(data, is_delete);
661 }
662
663 /**
664 * ccs_read_manager - Read manager policy.
665 *
666 * @head: Pointer to "struct ccs_io_buffer".
667 *
668 * Caller holds ccs_read_lock().
669 */
670 static void ccs_read_manager(struct ccs_io_buffer *head)
671 {
672 if (head->r.eof)
673 return;
674 list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
675 struct ccs_manager *ptr =
676 list_entry(head->r.acl, typeof(*ptr), head.list);
677 if (ptr->head.is_deleted)
678 continue;
679 if (!ccs_flush(head))
680 return;
681 ccs_set_string(head, ptr->manager->name);
682 ccs_set_lf(head);
683 }
684 head->r.eof = true;
685 }
686
687 /**
688 * ccs_manager - Check whether the current process is a policy manager.
689 *
690 * Returns true if the current process is permitted to modify policy
691 * via /proc/ccs/ interface.
692 *
693 * Caller holds ccs_read_lock().
694 */
695 static bool ccs_manager(void)
696 {
697 struct ccs_manager *ptr;
698 const char *exe;
699 struct ccs_security *task = ccs_current_security();
700 const struct ccs_path_info *domainname
701 = ccs_current_domain()->domainname;
702 bool found = false;
703 if (!ccs_policy_loaded)
704 return true;
705 if (task->ccs_flags & CCS_TASK_IS_MANAGER)
706 return true;
707 if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
708 return false;
709 exe = ccs_get_exe();
710 list_for_each_entry_srcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
711 head.list, &ccs_ss) {
712 if (ptr->head.is_deleted)
713 continue;
714 if (ptr->is_domain) {
715 if (ccs_pathcmp(domainname, ptr->manager))
716 continue;
717 } else {
718 if (!exe || strcmp(exe, ptr->manager->name))
719 continue;
720 }
721 /* Set manager flag. */
722 task->ccs_flags |= CCS_TASK_IS_MANAGER;
723 found = true;
724 break;
725 }
726 if (!found) { /* Reduce error messages. */
727 static pid_t ccs_last_pid;
728 const pid_t pid = current->pid;
729 if (ccs_last_pid != pid) {
730 printk(KERN_WARNING "%s ( %s ) is not permitted to "
731 "update policies.\n", domainname->name, exe);
732 ccs_last_pid = pid;
733 }
734 }
735 kfree(exe);
736 return found;
737 }
738
739 /**
740 * ccs_select_one - Parse select command.
741 *
742 * @head: Pointer to "struct ccs_io_buffer".
743 * @data: String to parse.
744 *
745 * Returns true on success, false otherwise.
746 *
747 * Caller holds ccs_read_lock().
748 */
749 static bool ccs_select_one(struct ccs_io_buffer *head, const char *data)
750 {
751 unsigned int pid;
752 struct ccs_domain_info *domain = NULL;
753 bool global_pid = false;
754 if (!strcmp(data, "execute")) {
755 head->r.print_execute_only = true;
756 return true;
757 }
758 if (sscanf(data, "pid=%u", &pid) == 1 ||
759 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
760 struct task_struct *p;
761 ccs_tasklist_lock();
762 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
763 if (global_pid)
764 p = ccsecurity_exports.find_task_by_pid_ns(pid,
765 &init_pid_ns);
766 else
767 p = ccsecurity_exports.find_task_by_vpid(pid);
768 #else
769 p = find_task_by_pid(pid);
770 #endif
771 if (p)
772 domain = ccs_task_domain(p);
773 ccs_tasklist_unlock();
774 } else if (!strncmp(data, "domain=", 7)) {
775 if (ccs_domain_def(data + 7))
776 domain = ccs_find_domain(data + 7);
777 } else
778 return false;
779 head->w.domain = domain;
780 /* Accessing read_buf is safe because head->io_sem is held. */
781 if (!head->read_buf)
782 return true; /* Do nothing if open(O_WRONLY). */
783 memset(&head->r, 0, sizeof(head->r));
784 head->r.print_this_domain_only = true;
785 if (domain)
786 head->r.domain = &domain->list;
787 else
788 head->r.eof = true;
789 ccs_io_printf(head, "# select %s\n", data);
790 if (domain && domain->is_deleted)
791 ccs_set_string(head, "# This is a deleted domain.\n");
792 return true;
793 }
794
795 static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
796 const struct ccs_acl_info *b)
797 {
798 const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
799 const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
800 return p1->handler == p2->handler;
801 }
802
803 static bool ccs_same_task_acl(const struct ccs_acl_info *a,
804 const struct ccs_acl_info *b)
805 {
806 const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
807 const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
808 return p1->domainname == p2->domainname;
809 }
810
811 /**
812 * ccs_write_task - Update task related list.
813 *
814 * @param: Pointer to "struct ccs_acl_param".
815 *
816 * Returns 0 on success, negative value otherwise.
817 */
818 static int ccs_write_task(struct ccs_acl_param *param)
819 {
820 int error;
821 const bool is_auto = ccs_str_starts(&param->data,
822 "auto_domain_transition ");
823 if (!is_auto && !ccs_str_starts(&param->data,
824 "manual_domain_transition ")) {
825 struct ccs_handler_acl e = { };
826 char *handler;
827 if (ccs_str_starts(&param->data, "auto_execute_handler "))
828 e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
829 else if (ccs_str_starts(&param->data,
830 "denied_execute_handler "))
831 e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
832 else
833 return -EINVAL;
834 handler = ccs_read_token(param);
835 if (!ccs_correct_path(handler))
836 return -EINVAL;
837 e.handler = ccs_get_name(handler);
838 if (!e.handler)
839 return -ENOMEM;
840 if (e.handler->is_patterned)
841 error = -EINVAL; /* No patterns allowed. */
842 else
843 error = ccs_update_domain(&e.head, sizeof(e), param,
844 ccs_same_handler_acl, NULL);
845 ccs_put_name(e.handler);
846 } else {
847 struct ccs_task_acl e = {
848 .head.type = is_auto ?
849 CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
850 .domainname = ccs_get_domainname(param),
851 };
852 if (!e.domainname)
853 error = -EINVAL;
854 else
855 error = ccs_update_domain(&e.head, sizeof(e), param,
856 ccs_same_task_acl, NULL);
857 ccs_put_name(e.domainname);
858 }
859 return error;
860 }
861
862 static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
863 const bool is_delete)
864 {
865 struct ccs_acl_param param = {
866 .data = data,
867 .domain = domain,
868 .is_delete = is_delete,
869 };
870 static const struct {
871 const char *keyword;
872 int (*write) (struct ccs_acl_param *);
873 } ccs_callback[7] = {
874 { "file ", ccs_write_file },
875 { "network inet ", ccs_write_inet_network },
876 { "network unix ", ccs_write_unix_network },
877 { "misc ", ccs_write_misc },
878 { "capability ", ccs_write_capability },
879 { "ipc ", ccs_write_ipc },
880 { "task ", ccs_write_task },
881 };
882 u8 i;
883 for (i = 0; i < 7; i++) {
884 if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
885 continue;
886 return ccs_callback[i].write(&param);
887 }
888 return -EINVAL;
889 }
890
891 const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
892 [CCS_DIF_QUOTA_WARNED] = "quota_exceeded\n",
893 [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
894 };
895
896 /**
897 * ccs_write_domain - Write domain policy.
898 *
899 * @head: Pointer to "struct ccs_io_buffer".
900 *
901 * Returns 0 on success, negative value otherwise.
902 */
903 static int ccs_write_domain(struct ccs_io_buffer *head)
904 {
905 char *data = head->write_buf;
906 struct ccs_domain_info *domain = head->w.domain;
907 bool is_delete = false;
908 bool is_select = false;
909 unsigned int profile;
910 if (ccs_str_starts(&data, "delete "))
911 is_delete = true;
912 else if (ccs_str_starts(&data, "select "))
913 is_select = true;
914 if (is_select && ccs_select_one(head, data))
915 return 0;
916 /* Don't allow updating policies by non manager programs. */
917 if (!ccs_manager())
918 return -EPERM;
919 if (ccs_domain_def(data)) {
920 domain = NULL;
921 if (is_delete)
922 ccs_delete_domain(data);
923 else if (is_select)
924 domain = ccs_find_domain(data);
925 else
926 domain = ccs_assign_domain(data, 0, 0, false);
927 head->w.domain = domain;
928 return 0;
929 }
930 if (!domain)
931 return -EINVAL;
932
933 if (sscanf(data, "use_profile %u\n", &profile) == 1
934 && profile < CCS_MAX_PROFILES) {
935 if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
936 domain->profile = (u8) profile;
937 return 0;
938 }
939 if (sscanf(data, "use_group %u\n", &profile) == 1
940 && profile < CCS_MAX_ACL_GROUPS) {
941 domain->group = (u8) profile;
942 return 0;
943 }
944 for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
945 const char *cp = ccs_dif[profile];
946 if (strncmp(data, cp, strlen(cp) - 1))
947 continue;
948 domain->flags[profile] = !is_delete;
949 return 0;
950 }
951 return ccs_write_domain2(data, domain, is_delete);
952 }
953
954 /**
955 * ccs_print_name_union - Print a ccs_name_union.
956 *
957 * @head: Pointer to "struct ccs_io_buffer".
958 * @ptr: Pointer to "struct ccs_name_union".
959 */
960 static void ccs_print_name_union(struct ccs_io_buffer *head,
961 const struct ccs_name_union *ptr)
962 {
963 const bool cond = head->r.print_cond_part;
964 if (!cond)
965 ccs_set_space(head);
966 if (ptr->is_group) {
967 ccs_set_string(head, "@");
968 ccs_set_string(head, ptr->group->group_name->name);
969 } else {
970 if (cond)
971 ccs_set_string(head, "\"");
972 ccs_set_string(head, ptr->filename->name);
973 if (cond)
974 ccs_set_string(head, "\"");
975 }
976 }
977
978 /**
979 * ccs_print_number_union - Print a ccs_number_union.
980 *
981 * @head: Pointer to "struct ccs_io_buffer".
982 * @ptr: Pointer to "struct ccs_number_union".
983 */
984 static void ccs_print_number_union(struct ccs_io_buffer *head,
985 const struct ccs_number_union *ptr)
986 {
987 if (!head->r.print_cond_part)
988 ccs_set_space(head);
989 if (ptr->is_group) {
990 ccs_set_string(head, "@");
991 ccs_set_string(head, ptr->group->group_name->name);
992 } else {
993 int i;
994 unsigned long min = ptr->values[0];
995 const unsigned long max = ptr->values[1];
996 u8 min_type = ptr->value_type[0];
997 const u8 max_type = ptr->value_type[1];
998 char buffer[128];
999 buffer[0] = '\0';
1000 for (i = 0; i < 2; i++) {
1001 switch (min_type) {
1002 case CCS_VALUE_TYPE_HEXADECIMAL:
1003 ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1004 min);
1005 break;
1006 case CCS_VALUE_TYPE_OCTAL:
1007 ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1008 min);
1009 break;
1010 default:
1011 ccs_addprintf(buffer, sizeof(buffer), "%lu",
1012 min);
1013 break;
1014 }
1015 if (min == max && min_type == max_type)
1016 break;
1017 ccs_addprintf(buffer, sizeof(buffer), "-");
1018 min_type = max_type;
1019 min = max;
1020 }
1021 ccs_io_printf(head, "%s", buffer);
1022 }
1023 }
1024
1025 /**
1026 * ccs_print_condition - Print condition part.
1027 *
1028 * @head: Pointer to "struct ccs_io_buffer".
1029 * @cond: Pointer to "struct ccs_condition".
1030 *
1031 * Returns true on success, false otherwise.
1032 */
1033 static bool ccs_print_condition(struct ccs_io_buffer *head,
1034 const struct ccs_condition *cond)
1035 {
1036 switch (head->r.cond_step) {
1037 case 0:
1038 head->r.cond_index = 0;
1039 head->r.cond_step++;
1040 /* fall through */
1041 case 1:
1042 {
1043 const u16 condc = cond->condc;
1044 const struct ccs_condition_element *condp =
1045 (typeof(condp)) (cond + 1);
1046 const struct ccs_number_union *numbers_p =
1047 (typeof(numbers_p)) (condp + condc);
1048 const struct ccs_name_union *names_p =
1049 (typeof(names_p))
1050 (numbers_p + cond->numbers_count);
1051 const struct ccs_argv *argv =
1052 (typeof(argv)) (names_p + cond->names_count);
1053 const struct ccs_envp *envp =
1054 (typeof(envp)) (argv + cond->argc);
1055 u16 skip;
1056 for (skip = 0; skip < head->r.cond_index; skip++) {
1057 const u8 left = condp->left;
1058 const u8 right = condp->right;
1059 condp++;
1060 switch (left) {
1061 case CCS_ARGV_ENTRY:
1062 argv++;
1063 continue;
1064 case CCS_ENVP_ENTRY:
1065 envp++;
1066 continue;
1067 case CCS_NUMBER_UNION:
1068 numbers_p++;
1069 break;
1070 }
1071 switch (right) {
1072 case CCS_NAME_UNION:
1073 names_p++;
1074 break;
1075 case CCS_NUMBER_UNION:
1076 numbers_p++;
1077 break;
1078 }
1079 }
1080 while (head->r.cond_index < condc) {
1081 const u8 match = condp->equals;
1082 const u8 left = condp->left;
1083 const u8 right = condp->right;
1084 if (!ccs_flush(head))
1085 return false;
1086 condp++;
1087 head->r.cond_index++;
1088 ccs_set_space(head);
1089 switch (left) {
1090 case CCS_ARGV_ENTRY:
1091 ccs_io_printf(head,
1092 "exec.argv[%u]%s\"%s\"",
1093 argv->index,
1094 argv->is_not ?
1095 "!=" : "=",
1096 argv->value->name);
1097 argv++;
1098 continue;
1099 case CCS_ENVP_ENTRY:
1100 ccs_io_printf(head,
1101 "exec.envp[\"%s\"]%s",
1102 envp->name->name,
1103 envp->is_not ?
1104 "!=" : "=");
1105 if (envp->value) {
1106 ccs_set_string(head, "\"");
1107 ccs_set_string(head, envp->
1108 value->name);
1109 ccs_set_string(head, "\"");
1110 } else {
1111 ccs_set_string(head, "NULL");
1112 }
1113 envp++;
1114 continue;
1115 case CCS_NUMBER_UNION:
1116 ccs_print_number_union(head,
1117 numbers_p++);
1118 break;
1119 default:
1120 ccs_set_string(head,
1121 ccs_condition_keyword[left]);
1122 break;
1123 }
1124 ccs_set_string(head, match ? "=" : "!=");
1125 switch (right) {
1126 case CCS_NAME_UNION:
1127 ccs_print_name_union(head, names_p++);
1128 break;
1129 case CCS_NUMBER_UNION:
1130 ccs_print_number_union(head,
1131 numbers_p++);
1132 break;
1133 default:
1134 ccs_set_string(head,
1135 ccs_condition_keyword[right]);
1136 break;
1137 }
1138 }
1139 }
1140 head->r.cond_step++;
1141 /* fall through */
1142 case 2:
1143 if (!ccs_flush(head))
1144 break;
1145 head->r.cond_step++;
1146 /* fall through */
1147 case 3:
1148 if (cond->grant_log)
1149 ccs_io_printf(head, " grant_log=%s",
1150 ccs_yesno(cond->grant_log == 2));
1151 if (cond->transit) {
1152 ccs_set_string(head, " auto_domain_transitition=\"");
1153 ccs_set_string(head, cond->transit->name);
1154 ccs_set_string(head, "\"");
1155 }
1156 ccs_set_lf(head);
1157 return true;
1158 }
1159 return false;
1160 }
1161
1162 /**
1163 * ccs_fns - Find next set bit.
1164 *
1165 * @perm: 8 bits value.
1166 * @bit: First bit to find.
1167 *
1168 * Returns next set bit on success, 8 otherwise.
1169 */
1170 static u8 ccs_fns(const u8 perm, u8 bit)
1171 {
1172 for ( ; bit < 8; bit++)
1173 if (perm & (1 << bit))
1174 break;
1175 return bit;
1176 }
1177
1178 static void ccs_set_group(struct ccs_io_buffer *head)
1179 {
1180 if (head->type == CCS_EXCEPTIONPOLICY)
1181 ccs_io_printf(head, "acl_group %u ", head->r.group_index);
1182 }
1183
1184 /**
1185 * ccs_print_entry - Print an ACL entry.
1186 *
1187 * @head: Pointer to "struct ccs_io_buffer".
1188 * @acl: Pointer to an ACL entry.
1189 *
1190 * Returns true on success, false otherwise.
1191 */
1192 static bool ccs_print_entry(struct ccs_io_buffer *head,
1193 const struct ccs_acl_info *acl)
1194 {
1195 const u8 acl_type = acl->type;
1196 u8 bit;
1197 if (head->r.print_cond_part)
1198 goto print_cond_part;
1199 if (acl->is_deleted)
1200 return true;
1201 next:
1202 bit = head->r.bit;
1203 if (!ccs_flush(head))
1204 return false;
1205 else if (acl_type == CCS_TYPE_PATH_ACL) {
1206 struct ccs_path_acl *ptr
1207 = container_of(acl, typeof(*ptr), head);
1208 const u16 perm = ptr->perm;
1209 for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1210 if (!(perm & (1 << bit)))
1211 continue;
1212 if (head->r.print_execute_only &&
1213 bit != CCS_TYPE_EXECUTE
1214 /* && bit != CCS_TYPE_TRANSIT */)
1215 continue;
1216 break;
1217 }
1218 if (bit >= CCS_MAX_PATH_OPERATION)
1219 goto done;
1220 ccs_set_group(head);
1221 ccs_set_string(head, "file ");
1222 ccs_set_string(head, ccs_path_keyword[bit]);
1223 ccs_print_name_union(head, &ptr->name);
1224 } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1225 acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1226 struct ccs_handler_acl *ptr
1227 = container_of(acl, typeof(*ptr), head);
1228 ccs_set_group(head);
1229 ccs_set_string(head, "task ");
1230 ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1231 ? "auto_execute_handler " :
1232 "denied_execute_handler ");
1233 ccs_set_string(head, ptr->handler->name);
1234 } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1235 acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1236 struct ccs_task_acl *ptr =
1237 container_of(acl, typeof(*ptr), head);
1238 ccs_set_group(head);
1239 ccs_set_string(head, "task ");
1240 ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1241 "auto_domain_transition " :
1242 "manual_domain_transition ");
1243 ccs_set_string(head, ptr->domainname->name);
1244 } else if (head->r.print_execute_only) {
1245 return true;
1246 } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1247 struct ccs_mkdev_acl *ptr =
1248 container_of(acl, typeof(*ptr), head);
1249 bit = ccs_fns(ptr->perm, bit);
1250 if (bit >= CCS_MAX_MKDEV_OPERATION)
1251 goto done;
1252 ccs_set_group(head);
1253 ccs_set_string(head, "file ");
1254 ccs_set_string(head, ccs_mac_keywords[ccs_pnnn2mac[bit]]);
1255 ccs_print_name_union(head, &ptr->name);
1256 ccs_print_number_union(head, &ptr->mode);
1257 ccs_print_number_union(head, &ptr->major);
1258 ccs_print_number_union(head, &ptr->minor);
1259 } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1260 struct ccs_path2_acl *ptr =
1261 container_of(acl, typeof(*ptr), head);
1262 bit = ccs_fns(ptr->perm, bit);
1263 if (bit >= CCS_MAX_PATH2_OPERATION)
1264 goto done;
1265 ccs_set_group(head);
1266 ccs_set_string(head, "file ");
1267 ccs_set_string(head, ccs_mac_keywords[ccs_pp2mac[bit]]);
1268 ccs_print_name_union(head, &ptr->name1);
1269 ccs_print_name_union(head, &ptr->name2);
1270 } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1271 struct ccs_path_number_acl *ptr =
1272 container_of(acl, typeof(*ptr), head);
1273 bit = ccs_fns(ptr->perm, bit);
1274 if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1275 goto done;
1276 ccs_set_group(head);
1277 ccs_set_string(head, "file ");
1278 ccs_set_string(head, ccs_mac_keywords[ccs_pn2mac[bit]]);
1279 ccs_print_name_union(head, &ptr->name);
1280 ccs_print_number_union(head, &ptr->number);
1281 } else if (acl_type == CCS_TYPE_ENV_ACL) {
1282 struct ccs_env_acl *ptr =
1283 container_of(acl, typeof(*ptr), head);
1284 ccs_set_group(head);
1285 ccs_set_string(head, "misc env ");
1286 ccs_set_string(head, ptr->env->name);
1287 } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1288 struct ccs_capability_acl *ptr =
1289 container_of(acl, typeof(*ptr), head);
1290 ccs_set_group(head);
1291 ccs_set_string(head, "capability ");
1292 ccs_set_string(head,
1293 ccs_mac_keywords[ccs_c2mac[ptr->operation]]);
1294 } else if (acl_type == CCS_TYPE_INET_ACL) {
1295 struct ccs_inet_acl *ptr =
1296 container_of(acl, typeof(*ptr), head);
1297 bit = ccs_fns(ptr->perm, bit);
1298 if (bit >= CCS_MAX_NETWORK_OPERATION)
1299 goto done;
1300 ccs_set_group(head);
1301 ccs_set_string(head, "network inet ");
1302 ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1303 ccs_set_space(head);
1304 ccs_set_string(head, ccs_socket_keyword[bit]);
1305 ccs_set_space(head);
1306 switch (ptr->address_type) {
1307 char buf[128];
1308 case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1309 ccs_set_string(head, "@");
1310 ccs_set_string(head,
1311 ptr->address.group->group_name->name);
1312 break;
1313 case CCS_IP_ADDRESS_TYPE_IPv4:
1314 ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1315 ptr->address.ipv4.max);
1316 ccs_io_printf(head, "%s", buf);
1317 break;
1318 case CCS_IP_ADDRESS_TYPE_IPv6:
1319 ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1320 ptr->address.ipv6.max);
1321 ccs_io_printf(head, "%s", buf);
1322 break;
1323 }
1324 ccs_print_number_union(head, &ptr->port);
1325 } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1326 struct ccs_unix_acl *ptr =
1327 container_of(acl, typeof(*ptr), head);
1328 bit = ccs_fns(ptr->perm, bit);
1329 if (bit >= CCS_MAX_NETWORK_OPERATION)
1330 goto done;
1331 ccs_set_group(head);
1332 ccs_set_string(head, "network unix ");
1333 ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1334 ccs_set_space(head);
1335 ccs_set_string(head, ccs_socket_keyword[bit]);
1336 ccs_print_name_union(head, &ptr->name);
1337 } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1338 struct ccs_signal_acl *ptr =
1339 container_of(acl, typeof(*ptr), head);
1340 ccs_set_group(head);
1341 ccs_set_string(head, "ipc signal ");
1342 ccs_io_printf(head, "%u ", ptr->sig);
1343 ccs_set_string(head, ptr->domainname->name);
1344 } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1345 struct ccs_mount_acl *ptr =
1346 container_of(acl, typeof(*ptr), head);
1347 ccs_set_group(head);
1348 ccs_io_printf(head, "file mount");
1349 ccs_print_name_union(head, &ptr->dev_name);
1350 ccs_print_name_union(head, &ptr->dir_name);
1351 ccs_print_name_union(head, &ptr->fs_type);
1352 ccs_print_number_union(head, &ptr->flags);
1353 }
1354 head->r.bit = bit + 1;
1355 if (acl->cond) {
1356 head->r.print_cond_part = true;
1357 head->r.cond_step = 0;
1358 if (!ccs_flush(head))
1359 return false;
1360 print_cond_part:
1361 if (!ccs_print_condition(head, acl->cond))
1362 return false;
1363 head->r.print_cond_part = false;
1364 } else {
1365 ccs_set_lf(head);
1366 }
1367 switch (acl_type) {
1368 case CCS_TYPE_PATH_ACL:
1369 case CCS_TYPE_MKDEV_ACL:
1370 case CCS_TYPE_PATH2_ACL:
1371 case CCS_TYPE_PATH_NUMBER_ACL:
1372 case CCS_TYPE_INET_ACL:
1373 case CCS_TYPE_UNIX_ACL:
1374 goto next;
1375 }
1376 done:
1377 head->r.bit = 0;
1378 return true;
1379 }
1380
1381 /**
1382 * ccs_read_domain2 - Read domain policy.
1383 *
1384 * @head: Pointer to "struct ccs_io_buffer".
1385 * @domain: Pointer to "struct ccs_domain_info".
1386 * @index: Index number.
1387 *
1388 * Caller holds ccs_read_lock().
1389 *
1390 * Returns true on success, false otherwise.
1391 */
1392 static bool ccs_read_domain2(struct ccs_io_buffer *head,
1393 struct ccs_domain_info *domain,
1394 const u8 index)
1395 {
1396 list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1397 struct ccs_acl_info *ptr =
1398 list_entry(head->r.acl, typeof(*ptr), list);
1399 if (!ccs_print_entry(head, ptr))
1400 return false;
1401 }
1402 head->r.acl = NULL;
1403 return true;
1404 }
1405
1406 /**
1407 * ccs_read_domain - Read domain policy.
1408 *
1409 * @head: Pointer to "struct ccs_io_buffer".
1410 *
1411 * Caller holds ccs_read_lock().
1412 */
1413 static void ccs_read_domain(struct ccs_io_buffer *head)
1414 {
1415 if (head->r.eof)
1416 return;
1417 list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1418 struct ccs_domain_info *domain =
1419 list_entry(head->r.domain, typeof(*domain), list);
1420 switch (head->r.step) {
1421 u8 i;
1422 case 0:
1423 if (domain->is_deleted &&
1424 !head->r.print_this_domain_only)
1425 continue;
1426 /* Print domainname and flags. */
1427 ccs_set_string(head, domain->domainname->name);
1428 ccs_set_lf(head);
1429 ccs_io_printf(head, "use_profile %u\n",
1430 domain->profile);
1431 ccs_io_printf(head, "use_group %u\n", domain->group);
1432 for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1433 if (domain->flags[i])
1434 ccs_set_string(head, ccs_dif[i]);
1435 head->r.step++;
1436 ccs_set_lf(head);
1437 /* fall through */
1438 case 1:
1439 if (!ccs_read_domain2(head, domain, 0))
1440 return;
1441 head->r.step++;
1442 /* fall through */
1443 case 2:
1444 if (!ccs_read_domain2(head, domain, 1))
1445 return;
1446 head->r.step++;
1447 if (!ccs_set_lf(head))
1448 return;
1449 /* fall through */
1450 case 3:
1451 head->r.step = 0;
1452 if (head->r.print_this_domain_only)
1453 goto done;
1454 }
1455 }
1456 done:
1457 head->r.eof = true;
1458 }
1459
1460 /**
1461 * ccs_write_domain_profile - Assign profile for specified domain.
1462 *
1463 * @head: Pointer to "struct ccs_io_buffer".
1464 *
1465 * Returns 0 on success, -EINVAL otherwise.
1466 *
1467 * This is equivalent to doing
1468 *
1469 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1470 * /usr/sbin/ccs-loadpolicy -d
1471 *
1472 * Caller holds ccs_read_lock().
1473 */
1474 static int ccs_write_domain_profile(struct ccs_io_buffer *head)
1475 {
1476 char *data = head->write_buf;
1477 char *cp = strchr(data, ' ');
1478 struct ccs_domain_info *domain;
1479 unsigned int profile;
1480 if (!cp)
1481 return -EINVAL;
1482 *cp = '\0';
1483 profile = simple_strtoul(data, NULL, 10);
1484 if (profile >= CCS_MAX_PROFILES)
1485 return -EINVAL;
1486 domain = ccs_find_domain(cp + 1);
1487 if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1488 domain->profile = (u8) profile;
1489 return 0;
1490 }
1491
1492 /**
1493 * ccs_read_domain_profile - Read only domainname and profile.
1494 *
1495 * @head: Pointer to "struct ccs_io_buffer".
1496 *
1497 * This is equivalent to doing
1498 *
1499 * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1500 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1501 * domainname = $0; } else if ( $1 == "use_profile" ) {
1502 * print $2 " " domainname; domainname = ""; } } ; '
1503 *
1504 * Caller holds ccs_read_lock().
1505 */
1506 static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1507 {
1508 if (head->r.eof)
1509 return;
1510 list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1511 struct ccs_domain_info *domain =
1512 list_entry(head->r.domain, typeof(*domain), list);
1513 if (domain->is_deleted)
1514 continue;
1515 if (!ccs_flush(head))
1516 return;
1517 ccs_io_printf(head, "%u ", domain->profile);
1518 ccs_set_string(head, domain->domainname->name);
1519 ccs_set_lf(head);
1520 }
1521 head->r.eof = true;
1522 }
1523
1524 /**
1525 * ccs_write_pid: Specify PID to obtain domainname.
1526 *
1527 * @head: Pointer to "struct ccs_io_buffer".
1528 *
1529 * Returns 0.
1530 */
1531 static int ccs_write_pid(struct ccs_io_buffer *head)
1532 {
1533 head->r.eof = false;
1534 return 0;
1535 }
1536
1537 /**
1538 * ccs_read_pid - Read information of a process.
1539 *
1540 * @head: Pointer to "struct ccs_io_buffer".
1541 *
1542 * Returns the domainname which the specified PID is in or
1543 * process information of the specified PID on success,
1544 * empty string otherwise.
1545 *
1546 * Caller holds ccs_read_lock().
1547 */
1548 static void ccs_read_pid(struct ccs_io_buffer *head)
1549 {
1550 char *buf = head->write_buf;
1551 bool task_info = false;
1552 bool global_pid = false;
1553 unsigned int pid;
1554 struct task_struct *p;
1555 struct ccs_domain_info *domain = NULL;
1556 u32 ccs_flags = 0;
1557 /* Accessing write_buf is safe because head->io_sem is held. */
1558 if (!buf) {
1559 head->r.eof = true;
1560 return; /* Do nothing if open(O_RDONLY). */
1561 }
1562 if (head->r.w_pos || head->r.eof)
1563 return;
1564 head->r.eof = true;
1565 if (ccs_str_starts(&buf, "info "))
1566 task_info = true;
1567 if (ccs_str_starts(&buf, "global-pid "))
1568 global_pid = true;
1569 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1570 ccs_tasklist_lock();
1571 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1572 if (global_pid)
1573 p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1574 else
1575 p = ccsecurity_exports.find_task_by_vpid(pid);
1576 #else
1577 p = find_task_by_pid(pid);
1578 #endif
1579 if (p) {
1580 domain = ccs_task_domain(p);
1581 ccs_flags = ccs_task_flags(p);
1582 }
1583 ccs_tasklist_unlock();
1584 if (!domain)
1585 return;
1586 if (!task_info) {
1587 ccs_io_printf(head, "%u %u ", pid, domain->profile);
1588 ccs_set_string(head, domain->domainname->name);
1589 } else {
1590 ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
1591 ccs_yesno(ccs_flags &
1592 CCS_TASK_IS_MANAGER),
1593 ccs_yesno(ccs_flags &
1594 CCS_TASK_IS_EXECUTE_HANDLER));
1595 }
1596 }
1597
1598 static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1599 [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1600 [CCS_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
1601 [CCS_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
1602 [CCS_TRANSITION_CONTROL_KEEP] = "keep_domain ",
1603 };
1604
1605 static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1606 [CCS_PATH_GROUP] = "path_group ",
1607 [CCS_NUMBER_GROUP] = "number_group ",
1608 [CCS_ADDRESS_GROUP] = "address_group ",
1609 };
1610
1611 /**
1612 * ccs_write_exception - Write exception policy.
1613 *
1614 * @head: Pointer to "struct ccs_io_buffer".
1615 *
1616 * Returns 0 on success, negative value otherwise.
1617 */
1618 static int ccs_write_exception(struct ccs_io_buffer *head)
1619 {
1620 char *data = head->write_buf;
1621 const bool is_delete = ccs_str_starts(&data, "delete ");
1622 u8 i;
1623 static const struct {
1624 const char *keyword;
1625 int (*write) (char *, const bool);
1626 } ccs_callback[2] = {
1627 { "aggregator ", ccs_write_aggregator },
1628 { "deny_autobind ", ccs_write_reserved_port },
1629 };
1630 for (i = 0; i < 2; i++)
1631 if (ccs_str_starts(&data, ccs_callback[i].keyword))
1632 return ccs_callback[i].write(data, is_delete);
1633 for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1634 if (ccs_str_starts(&data, ccs_transition_type[i]))
1635 return ccs_write_transition_control(data, is_delete,
1636 i);
1637 for (i = 0; i < CCS_MAX_GROUP; i++)
1638 if (ccs_str_starts(&data, ccs_group_name[i]))
1639 return ccs_write_group(data, is_delete, i);
1640 if (ccs_str_starts(&data, "acl_group ")) {
1641 unsigned int group;
1642 if (sscanf(data, "%u", &group) == 1 &&
1643 group < CCS_MAX_ACL_GROUPS) {
1644 data = strchr(data, ' ');
1645 if (data)
1646 return ccs_write_domain2(data + 1,
1647 &ccs_acl_group[group],
1648 is_delete);
1649 }
1650 }
1651 return -EINVAL;
1652 }
1653
1654 /**
1655 * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1656 *
1657 * @head: Pointer to "struct ccs_io_buffer".
1658 * @idx: Index number.
1659 *
1660 * Returns true on success, false otherwise.
1661 *
1662 * Caller holds ccs_read_lock().
1663 */
1664 static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1665 {
1666 list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1667 struct ccs_group *group =
1668 list_entry(head->r.group, typeof(*group), head.list);
1669 list_for_each_cookie(head->r.acl, &group->member_list) {
1670 struct ccs_acl_head *ptr =
1671 list_entry(head->r.acl, typeof(*ptr), list);
1672 if (ptr->is_deleted)
1673 continue;
1674 if (!ccs_flush(head))
1675 return false;
1676 ccs_set_string(head, ccs_group_name[idx]);
1677 ccs_set_string(head, group->group_name->name);
1678 if (idx == CCS_PATH_GROUP) {
1679 ccs_set_space(head);
1680 ccs_set_string(head, container_of
1681 (ptr, struct ccs_path_group,
1682 head)->member_name->name);
1683 } else if (idx == CCS_NUMBER_GROUP) {
1684 ccs_print_number_union(head, &container_of
1685 (ptr, struct ccs_number_group,
1686 head)->number);
1687 } else if (idx == CCS_ADDRESS_GROUP) {
1688 char buffer[128];
1689 struct ccs_address_group *member =
1690 container_of(ptr, typeof(*member),
1691 head);
1692 if (member->is_ipv6)
1693 ccs_print_ipv6(buffer, sizeof(buffer),
1694 member->min.ipv6,
1695 member->max.ipv6);
1696 else
1697 ccs_print_ipv4(buffer, sizeof(buffer),
1698 member->min.ipv4,
1699 member->max.ipv4);
1700 ccs_io_printf(head, " %s", buffer);
1701 }
1702 ccs_set_lf(head);
1703 }
1704 head->r.acl = NULL;
1705 }
1706 head->r.group = NULL;
1707 return true;
1708 }
1709
1710 /**
1711 * ccs_read_policy - Read "struct ccs_..._entry" list.
1712 *
1713 * @head: Pointer to "struct ccs_io_buffer".
1714 * @idx: Index number.
1715 *
1716 * Returns true on success, false otherwise.
1717 *
1718 * Caller holds ccs_read_lock().
1719 */
1720 static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1721 {
1722 list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1723 struct ccs_acl_head *acl =
1724 container_of(head->r.acl, typeof(*acl), list);
1725 if (acl->is_deleted)
1726 continue;
1727 if (!ccs_flush(head))
1728 return false;
1729 switch (idx) {
1730 case CCS_ID_TRANSITION_CONTROL:
1731 {
1732 struct ccs_transition_control *ptr =
1733 container_of(acl, typeof(*ptr), head);
1734 ccs_set_string(head,
1735 ccs_transition_type[ptr->type]);
1736 ccs_set_string(head, ptr->program ?
1737 ptr->program->name : "any");
1738 ccs_set_string(head, " from ");
1739 ccs_set_string(head, ptr->domainname ?
1740 ptr->domainname->name : "any");
1741 }
1742 break;
1743 case CCS_ID_AGGREGATOR:
1744 {
1745 struct ccs_aggregator *ptr =
1746 container_of(acl, typeof(*ptr), head);
1747 ccs_set_string(head, "aggregator ");
1748 ccs_set_string(head, ptr->original_name->name);
1749 ccs_set_space(head);
1750 ccs_set_string(head,
1751 ptr->aggregated_name->name);
1752 }
1753 break;
1754 case CCS_ID_RESERVEDPORT:
1755 {
1756 struct ccs_reserved *ptr =
1757 container_of(acl, typeof(*ptr), head);
1758 const u16 min_port = ptr->min_port;
1759 const u16 max_port = ptr->max_port;
1760 ccs_set_string(head, "deny_autobind ");
1761 ccs_io_printf(head, "%u", min_port);
1762 if (min_port != max_port)
1763 ccs_io_printf(head, "-%u", max_port);
1764 }
1765 break;
1766 default:
1767 continue;
1768 }
1769 ccs_set_lf(head);
1770 }
1771 head->r.acl = NULL;
1772 return true;
1773 }
1774
1775 /**
1776 * ccs_read_exception - Read exception policy.
1777 *
1778 * @head: Pointer to "struct ccs_io_buffer".
1779 *
1780 * Caller holds ccs_read_lock().
1781 */
1782 static void ccs_read_exception(struct ccs_io_buffer *head)
1783 {
1784 if (head->r.eof)
1785 return;
1786 while (head->r.step < CCS_MAX_POLICY &&
1787 ccs_read_policy(head, head->r.step))
1788 head->r.step++;
1789 if (head->r.step < CCS_MAX_POLICY)
1790 return;
1791 while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1792 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1793 head->r.step++;
1794 if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1795 return;
1796 while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1797 + CCS_MAX_ACL_GROUPS * 2) {
1798 head->r.group_index = (head->r.step - CCS_MAX_POLICY
1799 - CCS_MAX_GROUP) / 2;
1800 if (!ccs_read_domain2(head,
1801 &ccs_acl_group[head->r.group_index],
1802 head->r.step & 1))
1803 return;
1804 head->r.step++;
1805 }
1806 head->r.eof = true;
1807 }
1808
1809 /* Wait queue for ccs_query_list. */
1810 static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
1811 static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
1812
1813 /* Lock for manipulating ccs_query_list. */
1814 static DEFINE_SPINLOCK(ccs_query_list_lock);
1815
1816 /* Structure for query. */
1817 struct ccs_query {
1818 struct list_head list;
1819 char *query;
1820 int query_len;
1821 unsigned int serial;
1822 int timer;
1823 int answer;
1824 u8 retry;
1825 };
1826
1827 /* The list for "struct ccs_query". */
1828 static LIST_HEAD(ccs_query_list);
1829
1830 /* Number of "struct file" referring /proc/ccs/query interface. */
1831 static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1832
1833 static int ccs_truncate(char *str)
1834 {
1835 char *start = str;
1836 while (*(unsigned char *) str > (unsigned char) ' ')
1837 str++;
1838 *str = '\0';
1839 return strlen(start) + 1;
1840 }
1841
1842 static void ccs_add_entry(char *header)
1843 {
1844 char *buffer;
1845 char *realpath = NULL;
1846 char *argv0 = NULL;
1847 char *symlink = NULL;
1848 char *handler;
1849 char *cp = strchr(header, '\n');
1850 int len;
1851 if (!cp)
1852 return;
1853 cp = strchr(cp + 1, '\n');
1854 if (!cp)
1855 return;
1856 *cp++ = '\0';
1857 len = strlen(cp) + 1;
1858 /* strstr() will return NULL if ordering is wrong. */
1859 if (*cp == 'f') {
1860 argv0 = strstr(header, " argv[]={ \"");
1861 if (argv0) {
1862 argv0 += 10;
1863 len += ccs_truncate(argv0) + 14;
1864 }
1865 realpath = strstr(header, " exec={ realpath=\"");
1866 if (realpath) {
1867 realpath += 8;
1868 len += ccs_truncate(realpath) + 6;
1869 }
1870 symlink = strstr(header, " symlink.target=\"");
1871 if (symlink)
1872 len += ccs_truncate(symlink + 1) + 1;
1873 }
1874 handler = strstr(header, "type=execute_handler");
1875 if (handler)
1876 len += ccs_truncate(handler) + 6;
1877 buffer = kmalloc(len, CCS_GFP_FLAGS);
1878 if (!buffer)
1879 return;
1880 snprintf(buffer, len - 1, "%s", cp);
1881 if (handler)
1882 ccs_addprintf(buffer, len, " task.%s", handler);
1883 if (realpath)
1884 ccs_addprintf(buffer, len, " exec.%s", realpath);
1885 if (argv0)
1886 ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
1887 if (symlink)
1888 ccs_addprintf(buffer, len, "%s", symlink);
1889 ccs_normalize_line(buffer);
1890 if (!ccs_write_domain2(buffer, ccs_current_domain(), false))
1891 ccs_update_stat(CCS_STAT_POLICY_UPDATES);
1892 kfree(buffer);
1893 }
1894
1895 /**
1896 * ccs_supervisor - Ask for the supervisor's decision.
1897 *
1898 * @r: Pointer to "struct ccs_request_info".
1899 * @fmt: The printf()'s format string, followed by parameters.
1900 *
1901 * Returns 0 if the supervisor decided to permit the access request which
1902 * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1903 * decided to retry the access request which violated the policy in enforcing
1904 * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1905 */
1906 int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1907 {
1908 va_list args;
1909 int error;
1910 int len;
1911 static unsigned int ccs_serial;
1912 struct ccs_query entry = { };
1913 bool quota_exceeded = false;
1914 /* Write /proc/ccs/grant_log or /proc/ccs/reject_log . */
1915 va_start(args, fmt);
1916 ccs_write_log2(r, fmt, args);
1917 va_end(args);
1918 /* Nothing more to do if granted. */
1919 if (r->granted)
1920 return 0;
1921 if (r->mode)
1922 ccs_update_stat(r->mode);
1923 switch (r->mode) {
1924 int i;
1925 struct ccs_profile *p;
1926 case CCS_CONFIG_ENFORCING:
1927 error = -EPERM;
1928 if (atomic_read(&ccs_query_observers))
1929 break;
1930 if (ccs_current_flags() & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
1931 goto out;
1932 p = ccs_profile(r->profile);
1933 /* Check enforcing_penalty parameter. */
1934 for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) {
1935 set_current_state(TASK_INTERRUPTIBLE);
1936 schedule_timeout(HZ / 10);
1937 }
1938 goto out;
1939 case CCS_CONFIG_LEARNING:
1940 error = 0;
1941 /* Check mac_learning_entry parameter. */
1942 if (ccs_domain_quota_ok(r))
1943 break;
1944 /* fall through */
1945 default:
1946 return 0;
1947 }
1948 /* Get message. */
1949 va_start(args, fmt);
1950 entry.query = ccs_init_log(&len, r, fmt, args);
1951 va_end(args);
1952 if (!entry.query)
1953 goto out;
1954 entry.query_len = strlen(entry.query) + 1;
1955 if (!error) {
1956 ccs_add_entry(entry.query);
1957 goto out;
1958 }
1959 spin_lock(&ccs_query_list_lock);
1960 if (ccs_memory_quota[CCS_MEMORY_QUERY] &&
1961 ccs_memory_used[CCS_MEMORY_QUERY] + len
1962 >= ccs_memory_quota[CCS_MEMORY_QUERY]) {
1963 quota_exceeded = true;
1964 } else {
1965 entry.serial = ccs_serial++;
1966 entry.retry = r->retry;
1967 ccs_memory_used[CCS_MEMORY_QUERY] += len;
1968 list_add_tail(&entry.list, &ccs_query_list);
1969 }
1970 spin_unlock(&ccs_query_list_lock);
1971 if (quota_exceeded)
1972 goto out;
1973 /* Give 10 seconds for supervisor's opinion. */
1974 while (entry.timer < 10) {
1975 wake_up_all(&ccs_query_wait);
1976 if (wait_event_interruptible_timeout
1977 (ccs_answer_wait, entry.answer ||
1978 !atomic_read(&ccs_query_observers), HZ))
1979 break;
1980 else
1981 entry.timer++;
1982 }
1983 spin_lock(&ccs_query_list_lock);
1984 list_del(&entry.list);
1985 ccs_memory_used[CCS_MEMORY_QUERY] -= len;
1986 spin_unlock(&ccs_query_list_lock);
1987 switch (entry.answer) {
1988 case 3: /* Asked to retry by administrator. */
1989 error = CCS_RETRY_REQUEST;
1990 r->retry++;
1991 break;
1992 case 1:
1993 /* Granted by administrator. */
1994 error = 0;
1995 break;
1996 default:
1997 /* Timed out or rejected by administrator. */
1998 break;
1999 }
2000 out:
2001 kfree(entry.query);
2002 return error;
2003 }
2004
2005 /**
2006 * ccs_poll_query - poll() for /proc/ccs/query.
2007 *
2008 * @file: Pointer to "struct file".
2009 * @wait: Pointer to "poll_table".
2010 *
2011 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2012 *
2013 * Waits for access requests which violated policy in enforcing mode.
2014 */
2015 static int ccs_poll_query(struct file *file, poll_table *wait)
2016 {
2017 struct list_head *tmp;
2018 bool found = false;
2019 u8 i;
2020 for (i = 0; i < 2; i++) {
2021 spin_lock(&ccs_query_list_lock);
2022 list_for_each(tmp, &ccs_query_list) {
2023 struct ccs_query *ptr =
2024 list_entry(tmp, typeof(*ptr), list);
2025 if (ptr->answer)
2026 continue;
2027 found = true;
2028 break;
2029 }
2030 spin_unlock(&ccs_query_list_lock);
2031 if (found)
2032 return POLLIN | POLLRDNORM;
2033 if (i)
2034 break;
2035 poll_wait(file, &ccs_query_wait, wait);
2036 }
2037 return 0;
2038 }
2039
2040 /**
2041 * ccs_read_query - Read access requests which violated policy in enforcing mode.
2042 *
2043 * @head: Pointer to "struct ccs_io_buffer".
2044 */
2045 static void ccs_read_query(struct ccs_io_buffer *head)
2046 {
2047 struct list_head *tmp;
2048 int pos = 0;
2049 int len = 0;
2050 char *buf;
2051 if (head->r.w_pos)
2052 return;
2053 if (head->read_buf) {
2054 kfree(head->read_buf);
2055 head->read_buf = NULL;
2056 }
2057 spin_lock(&ccs_query_list_lock);
2058 list_for_each(tmp, &ccs_query_list) {
2059 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2060 if (ptr->answer)
2061 continue;
2062 if (pos++ != head->r.query_index)
2063 continue;
2064 len = ptr->query_len;
2065 break;
2066 }
2067 spin_unlock(&ccs_query_list_lock);
2068 if (!len) {
2069 head->r.query_index = 0;
2070 return;
2071 }
2072 buf = kzalloc(len + 32, CCS_GFP_FLAGS);
2073 if (!buf)
2074 return;
2075 pos = 0;
2076 spin_lock(&ccs_query_list_lock);
2077 list_for_each(tmp, &ccs_query_list) {
2078 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2079 if (ptr->answer)
2080 continue;
2081 if (pos++ != head->r.query_index)
2082 continue;
2083 /*
2084 * Some query can be skipped because ccs_query_list
2085 * can change, but I don't care.
2086 */
2087 if (len == ptr->query_len)
2088 snprintf(buf, len + 32, "Q%u-%hu\n%s", ptr->serial,
2089 ptr->retry, ptr->query);
2090 break;
2091 }
2092 spin_unlock(&ccs_query_list_lock);
2093 if (buf[0]) {
2094 head->read_buf = buf;
2095 head->r.w[head->r.w_pos++] = buf;
2096 head->r.query_index++;
2097 } else {
2098 kfree(buf);
2099 }
2100 }
2101
2102 /**
2103 * ccs_write_answer - Write the supervisor's decision.
2104 *
2105 * @head: Pointer to "struct ccs_io_buffer".
2106 *
2107 * Returns 0 on success, -EINVAL otherwise.
2108 */
2109 static int ccs_write_answer(struct ccs_io_buffer *head)
2110 {
2111 char *data = head->write_buf;
2112 struct list_head *tmp;
2113 unsigned int serial;
2114 unsigned int answer;
2115 spin_lock(&ccs_query_list_lock);
2116 list_for_each(tmp, &ccs_query_list) {
2117 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2118 ptr->timer = 0;
2119 }
2120 spin_unlock(&ccs_query_list_lock);
2121 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2122 return -EINVAL;
2123 spin_lock(&ccs_query_list_lock);
2124 list_for_each(tmp, &ccs_query_list) {
2125 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2126 if (ptr->serial != serial)
2127 continue;
2128 if (!ptr->answer)
2129 ptr->answer = answer;
2130 break;
2131 }
2132 spin_unlock(&ccs_query_list_lock);
2133 wake_up_all(&ccs_answer_wait);
2134 return 0;
2135 }
2136
2137 /**
2138 * ccs_read_version: Get version.
2139 *
2140 * @head: Pointer to "struct ccs_io_buffer".
2141 */
2142 static void ccs_read_version(struct ccs_io_buffer *head)
2143 {
2144 if (head->r.eof)
2145 return;
2146 ccs_set_string(head, "1.8.0-pre");
2147 head->r.eof = true;
2148 }
2149
2150 /**
2151 * ccs_open_control - open() for /proc/ccs/ interface.
2152 *
2153 * @type: Type of interface.
2154 * @file: Pointer to "struct file".
2155 *
2156 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
2157 */
2158 int ccs_open_control(const u8 type, struct file *file)
2159 {
2160 struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2161 if (!head)
2162 return -ENOMEM;
2163 mutex_init(&head->io_sem);
2164 head->type = type;
2165 switch (type) {
2166 case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2167 head->write = ccs_write_domain;
2168 head->read = ccs_read_domain;
2169 break;
2170 case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2171 head->write = ccs_write_exception;
2172 head->read = ccs_read_exception;
2173 break;
2174 case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2175 case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2176 head->poll = ccs_poll_log;
2177 head->read = ccs_read_log;
2178 break;
2179 case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2180 head->write = ccs_write_domain_profile;
2181 head->read = ccs_read_domain_profile;
2182 break;
2183 case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */
2184 /* Allow execute_handler to read process's status. */
2185 if (!(ccs_current_flags() & CCS_TASK_IS_EXECUTE_HANDLER)) {
2186 kfree(head);
2187 return -EPERM;
2188 }
2189 /* fall through */
2190 case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */
2191 head->write = ccs_write_pid;
2192 head->read = ccs_read_pid;
2193 break;
2194 case CCS_VERSION: /* /proc/ccs/version */
2195 head->read = ccs_read_version;
2196 head->readbuf_size = 128;
2197 break;
2198 case CCS_MEMINFO: /* /proc/ccs/meminfo */
2199 head->write = ccs_write_memory_quota;
2200 head->read = ccs_read_memory_counter;
2201 head->readbuf_size = 512;
2202 break;
2203 case CCS_PROFILE: /* /proc/ccs/profile */
2204 head->write = ccs_write_profile;
2205 head->read = ccs_read_profile;
2206 break;
2207 case CCS_QUERY: /* /proc/ccs/query */
2208 head->poll = ccs_poll_query;
2209 head->write = ccs_write_answer;
2210 head->read = ccs_read_query;
2211 break;
2212 case CCS_MANAGER: /* /proc/ccs/manager */
2213 head->write = ccs_write_manager;
2214 head->read = ccs_read_manager;
2215 break;
2216 }
2217 if (!(file->f_mode & FMODE_READ)) {
2218 /*
2219 * No need to allocate read_buf since it is not opened
2220 * for reading.
2221 */
2222 head->read = NULL;
2223 head->poll = NULL;
2224 } else if (!head->poll) {
2225 /* Don't allocate read_buf for poll() access. */
2226 if (!head->readbuf_size)
2227 head->readbuf_size = 4096;
2228 head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2229 if (!head->read_buf) {
2230 kfree(head);
2231 return -ENOMEM;
2232 }
2233 }
2234 if (!(file->f_mode & FMODE_WRITE)) {
2235 /*
2236 * No need to allocate write_buf since it is not opened
2237 * for writing.
2238 */
2239 head->write = NULL;
2240 } else if (head->write) {
2241 head->writebuf_size = 4096;
2242 head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2243 if (!head->write_buf) {
2244 kfree(head->read_buf);
2245 kfree(head);
2246 return -ENOMEM;
2247 }
2248 }
2249 /*
2250 * If the file is /proc/ccs/query , increment the observer counter.
2251 * The obserber counter is used by ccs_supervisor() to see if
2252 * there is some process monitoring /proc/ccs/query.
2253 */
2254 if (type == CCS_QUERY)
2255 atomic_inc(&ccs_query_observers);
2256 else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2257 head->reader_idx = ccs_lock();
2258 file->private_data = head;
2259 return 0;
2260 }
2261
2262 /**
2263 * ccs_poll_control - poll() for /proc/ccs/ interface.
2264 *
2265 * @file: Pointer to "struct file".
2266 * @wait: Pointer to "poll_table".
2267 *
2268 * Waits for read readiness.
2269 * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2270 * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2271 * /usr/sbin/ccs-auditd .
2272 */
2273 int ccs_poll_control(struct file *file, poll_table *wait)
2274 {
2275 struct ccs_io_buffer *head = file->private_data;
2276 if (!head->poll)
2277 return -ENOSYS;
2278 return head->poll(file, wait);
2279 }
2280
2281 /**
2282 * ccs_read_control - read() for /proc/ccs/ interface.
2283 *
2284 * @file: Pointer to "struct file".
2285 * @buffer: Poiner to buffer to write to.
2286 * @buffer_len: Size of @buffer.
2287 *
2288 * Returns bytes read on success, negative value otherwise.
2289 */
2290 int ccs_read_control(struct file *file, char __user *buffer,
2291 const int buffer_len)
2292 {
2293 int len;
2294 struct ccs_io_buffer *head = file->private_data;
2295 int idx;
2296 if (!head->read)
2297 return -ENOSYS;
2298 if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2299 return -EFAULT;
2300 if (mutex_lock_interruptible(&head->io_sem))
2301 return -EINTR;
2302 head->read_user_buf = buffer;
2303 head->read_user_buf_avail = buffer_len;
2304 idx = ccs_read_lock();
2305 if (ccs_flush(head))
2306 /* Call the policy handler. */
2307 head->read(head);
2308 ccs_flush(head);
2309 ccs_read_unlock(idx);
2310 len = head->read_user_buf - buffer;
2311 mutex_unlock(&head->io_sem);
2312 return len;
2313 }
2314
2315 /**
2316 * ccs_write_control - write() for /proc/ccs/ interface.
2317 *
2318 * @file: Pointer to "struct file".
2319 * @buffer: Pointer to buffer to read from.
2320 * @buffer_len: Size of @buffer.
2321 *
2322 * Returns @buffer_len on success, negative value otherwise.
2323 */
2324 int ccs_write_control(struct file *file, const char __user *buffer,
2325 const int buffer_len)
2326 {
2327 struct ccs_io_buffer *head = file->private_data;
2328 int error = buffer_len;
2329 int avail_len = buffer_len;
2330 char *cp0 = head->write_buf;
2331 int idx;
2332 if (!head->write)
2333 return -ENOSYS;
2334 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2335 return -EFAULT;
2336 if (mutex_lock_interruptible(&head->io_sem))
2337 return -EINTR;
2338 idx = ccs_read_lock();
2339 /* Don't allow updating policies by non manager programs. */
2340 if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2341 !ccs_manager()) {
2342 ccs_read_unlock(idx);
2343 mutex_unlock(&head->io_sem);
2344 return -EPERM;
2345 }
2346 /* Read a line and dispatch it to the policy handler. */
2347 while (avail_len > 0) {
2348 char c;
2349 if (head->w.avail >= head->writebuf_size - 1) {
2350 const int len = head->writebuf_size * 2;
2351 char *cp = kzalloc(len, CCS_GFP_FLAGS);
2352 if (!cp) {
2353 error = -ENOMEM;
2354 break;
2355 }
2356 memmove(cp, cp0, head->w.avail);
2357 kfree(cp0);
2358 head->write_buf = cp;
2359 cp0 = cp;
2360 head->writebuf_size = len;
2361 }
2362 if (get_user(c, buffer)) {
2363 error = -EFAULT;
2364 break;
2365 }
2366 buffer++;
2367 avail_len--;
2368 cp0[head->w.avail++] = c;
2369 if (c != '\n')
2370 continue;
2371 cp0[head->w.avail - 1] = '\0';
2372 head->w.avail = 0;
2373 ccs_normalize_line(cp0);
2374 if (head->write(head))
2375 continue;
2376 switch (head->type) {
2377 case CCS_DOMAINPOLICY:
2378 case CCS_EXCEPTIONPOLICY:
2379 case CCS_DOMAIN_STATUS:
2380 case CCS_MEMINFO:
2381 case CCS_PROFILE:
2382 case CCS_MANAGER:
2383 ccs_update_stat(CCS_STAT_POLICY_UPDATES);
2384 }
2385 }
2386 ccs_read_unlock(idx);
2387 mutex_unlock(&head->io_sem);
2388 return error;
2389 }
2390
2391 /**
2392 * ccs_close_control - close() for /proc/ccs/ interface.
2393 *
2394 * @file: Pointer to "struct file".
2395 *
2396 * Releases memory and returns 0.
2397 */
2398 int ccs_close_control(struct file *file)
2399 {
2400 struct ccs_io_buffer *head = file->private_data;
2401 const bool is_write = head->write_buf != NULL;
2402 const u8 type = head->type;
2403 /*
2404 * If the file is /proc/ccs/query , decrement the observer counter.
2405 */
2406 if (type == CCS_QUERY) {
2407 if (atomic_dec_and_test(&ccs_query_observers))
2408 wake_up_all(&ccs_answer_wait);
2409 } else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2410 ccs_unlock(head->reader_idx);
2411 /* Release memory used for policy I/O. */
2412 kfree(head->read_buf);
2413 head->read_buf = NULL;
2414 kfree(head->write_buf);
2415 head->write_buf = NULL;
2416 kfree(head);
2417 head = NULL;
2418 file->private_data = NULL;
2419 if (is_write)
2420 ccs_run_gc();
2421 return 0;
2422 }
2423
2424 void __init ccs_policy_io_init(void)
2425 {
2426 ccsecurity_ops.check_profile = ccs_check_profile;
2427 }

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