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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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