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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3698 - (show annotations) (download) (as text)
Tue May 25 12:22:08 2010 UTC (14 years ago) by kumaneko
File MIME type: text/x-csrc
File size: 77297 byte(s)


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

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