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

Subversion リポジトリの参照

Contents of /branches/ccs-patch/security/ccsecurity/policy_io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3829 - (show annotations) (download) (as text)
Tue Jul 20 09:08:40 2010 UTC (13 years, 10 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 68293 byte(s)


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

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