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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3953 - (show annotations) (download) (as text)
Mon Sep 6 01:24:45 2010 UTC (13 years, 8 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 67175 byte(s)


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

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