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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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