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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3696 - (show annotations) (download) (as text)
Mon May 24 07:52:11 2010 UTC (14 years ago) by kumaneko
Original Path: branches/ccs-patch/security/ccsecurity/policy_io.c
File MIME type: text/x-csrc
File size: 77828 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 list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
756 head.list) {
757 if (!ptr->head.is_deleted && ptr->is_domain
758 && !ccs_pathcmp(domainname, ptr->manager)) {
759 /* Set manager flag. */
760 task->ccs_flags |= CCS_TASK_IS_MANAGER;
761 return true;
762 }
763 }
764 exe = ccs_get_exe();
765 if (!exe)
766 return false;
767 list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
768 head.list) {
769 if (!ptr->head.is_deleted && !ptr->is_domain
770 && !strcmp(exe, ptr->manager->name)) {
771 found = true;
772 /* Set manager flag. */
773 task->ccs_flags |= CCS_TASK_IS_MANAGER;
774 break;
775 }
776 }
777 if (!found) { /* Reduce error messages. */
778 static pid_t ccs_last_pid;
779 const pid_t pid = current->pid;
780 if (ccs_last_pid != pid) {
781 printk(KERN_WARNING "%s ( %s ) is not permitted to "
782 "update policies.\n", domainname->name, exe);
783 ccs_last_pid = pid;
784 }
785 }
786 kfree(exe);
787 return found;
788 }
789
790 /**
791 * ccs_find_condition_part - Find condition part from the statement.
792 *
793 * @data: String to parse.
794 *
795 * Returns pointer to the condition part if it was found in the statement,
796 * NULL otherwise.
797 */
798 char *ccs_find_condition_part(char *data)
799 {
800 char *cp = strstr(data, " if ");
801 if (!cp)
802 cp = strstr(data, " ; set ");
803 if (cp)
804 *cp++ = '\0';
805 return cp;
806 }
807
808 /**
809 * ccs_select_one - Parse select command.
810 *
811 * @head: Pointer to "struct ccs_io_buffer".
812 * @data: String to parse.
813 *
814 * Returns true on success, false otherwise.
815 *
816 * Caller holds ccs_read_lock().
817 */
818 static bool ccs_select_one(struct ccs_io_buffer *head, const char *data)
819 {
820 unsigned int pid;
821 struct ccs_domain_info *domain = NULL;
822 bool global_pid = false;
823 if (!strcmp(data, "allow_execute")) {
824 head->read_execute_only = true;
825 return true;
826 }
827 if (sscanf(data, "pid=%u", &pid) == 1 ||
828 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
829 struct task_struct *p;
830 ccs_tasklist_lock();
831 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
832 if (global_pid)
833 p = ccsecurity_exports.find_task_by_pid_ns(pid,
834 &init_pid_ns);
835 else
836 p = ccsecurity_exports.find_task_by_vpid(pid);
837 #else
838 p = find_task_by_pid(pid);
839 #endif
840 if (p)
841 domain = ccs_task_domain(p);
842 ccs_tasklist_unlock();
843 } else if (!strncmp(data, "domain=", 7)) {
844 if (ccs_domain_def(data + 7))
845 domain = ccs_find_domain(data + 7);
846 } else
847 return false;
848 head->write_var1 = domain;
849 /* Accessing read_buf is safe because head->io_sem is held. */
850 if (!head->read_buf)
851 return true; /* Do nothing if open(O_WRONLY). */
852 head->read_avail = 0;
853 ccs_io_printf(head, "# select %s\n", data);
854 head->read_single_domain = true;
855 head->read_eof = !domain;
856 if (domain) {
857 struct ccs_domain_info *d;
858 head->read_var1 = NULL;
859 list_for_each_entry_rcu(d, &ccs_domain_list, list) {
860 if (d == domain)
861 break;
862 head->read_var1 = &d->list;
863 }
864 head->read_var2 = NULL;
865 head->read_bit = 0;
866 head->read_step = 0;
867 if (domain->is_deleted)
868 ccs_io_printf(head, "# This is a deleted domain.\n");
869 }
870 return true;
871 }
872
873 static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
874 struct ccs_condition *cond, const bool is_delete)
875 {
876 u8 i;
877 static const struct {
878 const char *keyword;
879 int (*write) (char *, struct ccs_domain_info *,
880 struct ccs_condition *, const bool);
881 } ccs_callback[5] = {
882 { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
883 { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
884 { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
885 { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
886 { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
887 };
888 int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
889 const bool) = ccs_write_file;
890 for (i = 0; i < 5; i++) {
891 if (!ccs_str_starts(&data, ccs_callback[i].keyword))
892 continue;
893 write = ccs_callback[i].write;
894 break;
895 }
896 return write(data, domain, cond, is_delete);
897 }
898
899 /**
900 * ccs_write_domain - Write domain policy.
901 *
902 * @head: Pointer to "struct ccs_io_buffer".
903 *
904 * Returns 0 on success, negative value otherwise.
905 */
906 static int ccs_write_domain(struct ccs_io_buffer *head)
907 {
908 char *data = head->write_buf;
909 struct ccs_domain_info *domain = head->write_var1;
910 bool is_delete = false;
911 bool is_select = false;
912 unsigned int profile;
913 struct ccs_condition *cond = NULL;
914 char *cp;
915 int error;
916 if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
917 is_delete = true;
918 else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
919 is_select = true;
920 if (is_select && ccs_select_one(head, data))
921 return 0;
922 /* Don't allow updating policies by non manager programs. */
923 if (!ccs_manager())
924 return -EPERM;
925 if (ccs_domain_def(data)) {
926 domain = NULL;
927 if (is_delete)
928 ccs_delete_domain(data);
929 else if (is_select)
930 domain = ccs_find_domain(data);
931 else
932 domain = ccs_assign_domain(data, 0);
933 head->write_var1 = domain;
934 return 0;
935 }
936 if (!domain)
937 return -EINVAL;
938
939 if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
940 && profile < CCS_MAX_PROFILES) {
941 if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
942 domain->profile = (u8) profile;
943 return 0;
944 }
945 if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
946 domain->ignore_global_allow_read = !is_delete;
947 return 0;
948 }
949 if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
950 domain->ignore_global_allow_env = !is_delete;
951 return 0;
952 }
953 if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {
954 domain->quota_warned = !is_delete;
955 return 0;
956 }
957 if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {
958 domain->domain_transition_failed = !is_delete;
959 return 0;
960 }
961 cp = ccs_find_condition_part(data);
962 if (cp) {
963 cond = ccs_get_condition(cp);
964 if (!cond)
965 return -EINVAL;
966 }
967 error = ccs_write_domain2(data, domain, cond, is_delete);
968 if (cond)
969 ccs_put_condition(cond);
970 return error;
971 }
972
973 /**
974 * ccs_print_name_union - Print a ccs_name_union.
975 *
976 * @head: Pointer to "struct ccs_io_buffer".
977 * @ptr: Pointer to "struct ccs_name_union".
978 *
979 * Returns true on success, false otherwise.
980 */
981 static bool ccs_print_name_union(struct ccs_io_buffer *head,
982 const struct ccs_name_union *ptr)
983 {
984 int pos = head->read_avail;
985 if (pos && head->read_buf[pos - 1] == ' ')
986 head->read_avail--;
987 if (ptr->is_group)
988 return ccs_io_printf(head, " @%s",
989 ptr->group->group_name->name);
990 return ccs_io_printf(head, " %s", ptr->filename->name);
991 }
992
993 /**
994 * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.
995 *
996 * @head: Pointer to "struct ccs_io_buffer".
997 * @ptr: Pointer to "struct ccs_name_union".
998 *
999 * Returns true on success, false otherwise.
1000 */
1001 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1002 const struct ccs_name_union *ptr)
1003 {
1004 if (ptr->is_group)
1005 return ccs_io_printf(head, "@%s",
1006 ptr->group->group_name->name);
1007 return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1008 }
1009
1010 static void ccs_print_number(char *buffer, int buffer_len,
1011 const struct ccs_number_union *ptr)
1012 {
1013 int i;
1014 unsigned long min = ptr->values[0];
1015 const unsigned long max = ptr->values[1];
1016 u8 min_type = ptr->value_type[0];
1017 const u8 max_type = ptr->value_type[1];
1018 memset(buffer, 0, buffer_len);
1019 buffer_len -= 2;
1020 for (i = 0; i < 2; i++) {
1021 int len;
1022 switch (min_type) {
1023 case CCS_VALUE_TYPE_HEXADECIMAL:
1024 snprintf(buffer, buffer_len, "0x%lX", min);
1025 break;
1026 case CCS_VALUE_TYPE_OCTAL:
1027 snprintf(buffer, buffer_len, "0%lo", min);
1028 break;
1029 default:
1030 snprintf(buffer, buffer_len, "%lu", min);
1031 break;
1032 }
1033 if (min == max && min_type == max_type)
1034 break;
1035 len = strlen(buffer);
1036 buffer[len++] = '-';
1037 buffer += len;
1038 buffer_len -= len;
1039 min_type = max_type;
1040 min = max;
1041 }
1042 }
1043
1044 /**
1045 * ccs_print_number_union_common - Print a ccs_number_union.
1046 *
1047 * @head: Pointer to "struct ccs_io_buffer".
1048 * @ptr: Pointer to "struct ccs_number_union".
1049 * @need_space: True if a space character is needed.
1050 *
1051 * Returns true on success, false otherwise.
1052 */
1053 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,
1054 const struct ccs_number_union *ptr,
1055 const bool need_space)
1056 {
1057 char buffer[128];
1058 if (need_space && !ccs_io_printf(head, " "))
1059 return false;
1060 if (ptr->is_group)
1061 return ccs_io_printf(head, "@%s",
1062 ptr->group->group_name->name);
1063 ccs_print_number(buffer, sizeof(buffer), ptr);
1064 return ccs_io_printf(head, "%s", buffer);
1065 }
1066
1067 /**
1068 * ccs_print_number_union - Print a ccs_number_union.
1069 *
1070 * @head: Pointer to "struct ccs_io_buffer".
1071 * @ptr: Pointer to "struct ccs_number_union".
1072 *
1073 * Returns true on success, false otherwise.
1074 */
1075 static bool ccs_print_number_union(struct ccs_io_buffer *head,
1076 const struct ccs_number_union *ptr)
1077 {
1078 return ccs_print_number_union_common(head, ptr, true);
1079 }
1080
1081 /**
1082 * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.
1083 *
1084 * @head: Pointer to "struct ccs_io_buffer".
1085 * @ptr: Pointer to "struct ccs_number_union".
1086 *
1087 * Returns true on success, false otherwise.
1088 */
1089 static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1090 const struct ccs_number_union *ptr)
1091 {
1092 return ccs_print_number_union_common(head, ptr, false);
1093 }
1094
1095 /**
1096 * ccs_print_condition - Print condition part.
1097 *
1098 * @head: Pointer to "struct ccs_io_buffer".
1099 * @cond: Pointer to "struct ccs_condition". May be NULL.
1100 *
1101 * Returns true on success, false otherwise.
1102 */
1103 static bool ccs_print_condition(struct ccs_io_buffer *head,
1104 const struct ccs_condition *cond)
1105 {
1106 const struct ccs_condition_element *condp;
1107 const struct ccs_number_union *numbers_p;
1108 const struct ccs_name_union *names_p;
1109 const struct ccs_argv *argv;
1110 const struct ccs_envp *envp;
1111 u16 condc;
1112 u16 i;
1113 u16 j;
1114 char buffer[32];
1115 if (!cond)
1116 goto no_condition;
1117 condc = cond->condc;
1118 condp = (const struct ccs_condition_element *) (cond + 1);
1119 numbers_p = (const struct ccs_number_union *) (condp + condc);
1120 names_p = (const struct ccs_name_union *)
1121 (numbers_p + cond->numbers_count);
1122 argv = (const struct ccs_argv *) (names_p + cond->names_count);
1123 envp = (const struct ccs_envp *) (argv + cond->argc);
1124 memset(buffer, 0, sizeof(buffer));
1125 if (condc && !ccs_io_printf(head, "%s", " if"))
1126 goto out;
1127 for (i = 0; i < condc; i++) {
1128 const u8 match = condp->equals;
1129 const u8 left = condp->left;
1130 const u8 right = condp->right;
1131 condp++;
1132 switch (left) {
1133 case CCS_ARGV_ENTRY:
1134 if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",
1135 argv->index, argv->is_not ?
1136 "!=" : "=", argv->value->name))
1137 goto out;
1138 argv++;
1139 continue;
1140 case CCS_ENVP_ENTRY:
1141 if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",
1142 envp->name->name, envp->is_not ?
1143 "!=" : "="))
1144 goto out;
1145 if (envp->value) {
1146 if (!ccs_io_printf(head, "\"%s\"",
1147 envp->value->name))
1148 goto out;
1149 } else {
1150 if (!ccs_io_printf(head, "NULL"))
1151 goto out;
1152 }
1153 envp++;
1154 continue;
1155 case CCS_NUMBER_UNION:
1156 if (!ccs_print_number_union(head, numbers_p++))
1157 goto out;
1158 break;
1159 default:
1160 if (left >= CCS_MAX_CONDITION_KEYWORD)
1161 goto out;
1162 if (!ccs_io_printf(head, " %s",
1163 ccs_condition_keyword[left]))
1164 goto out;
1165 break;
1166 }
1167 if (!ccs_io_printf(head, "%s", match ? "=" : "!="))
1168 goto out;
1169 switch (right) {
1170 case CCS_NAME_UNION:
1171 if (!ccs_print_name_union_quoted(head, names_p++))
1172 goto out;
1173 break;
1174 case CCS_NUMBER_UNION:
1175 if (!ccs_print_number_union_nospace(head, numbers_p++))
1176 goto out;
1177 break;
1178 default:
1179 if (right >= CCS_MAX_CONDITION_KEYWORD)
1180 goto out;
1181 if (!ccs_io_printf(head, "%s",
1182 ccs_condition_keyword[right]))
1183 goto out;
1184 break;
1185 }
1186 }
1187 i = cond->post_state[3];
1188 if (!i)
1189 goto no_condition;
1190 if (!ccs_io_printf(head, " ; set"))
1191 goto out;
1192 for (j = 0; j < 3; j++) {
1193 if (!(i & (1 << j)))
1194 continue;
1195 if (!ccs_io_printf(head, " task.state[%u]=%u", j,
1196 cond->post_state[j]))
1197 goto out;
1198 }
1199 if (i & (1 << 4)) {
1200 if (!ccs_io_printf(head, " audit=%s",
1201 ccs_yesno(cond->post_state[4])))
1202 goto out;
1203 }
1204 no_condition:
1205 if (ccs_io_printf(head, "\n"))
1206 return true;
1207 out:
1208 return false;
1209 }
1210
1211 /**
1212 * ccs_print_path_acl - Print a path ACL entry.
1213 *
1214 * @head: Pointer to "struct ccs_io_buffer".
1215 * @ptr: Pointer to "struct ccs_path_acl".
1216 * @cond: Pointer to "struct ccs_condition". May be NULL.
1217 *
1218 * Returns true on success, false otherwise.
1219 */
1220 static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1221 struct ccs_path_acl *ptr,
1222 const struct ccs_condition *cond)
1223 {
1224 int pos;
1225 u8 bit;
1226 const u16 perm = ptr->perm;
1227 for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
1228 if (!(perm & (1 << bit)))
1229 continue;
1230 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1231 && bit != CCS_TYPE_TRANSIT)
1232 continue;
1233 /* Print "read/write" instead of "read" and "write". */
1234 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1235 && (perm & (1 << CCS_TYPE_READ_WRITE)))
1236 continue;
1237 pos = head->read_avail;
1238 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||
1239 !ccs_print_name_union(head, &ptr->name) ||
1240 !ccs_print_condition(head, cond)) {
1241 head->read_bit = bit;
1242 head->read_avail = pos;
1243 return false;
1244 }
1245 }
1246 head->read_bit = 0;
1247 return true;
1248 }
1249
1250 /**
1251 * ccs_print_path_number3_acl - Print a path_number3 ACL entry.
1252 *
1253 * @head: Pointer to "struct ccs_io_buffer".
1254 * @ptr: Pointer to "struct ccs_path_number3_acl".
1255 * @cond: Pointer to "struct ccs_condition". May be NULL.
1256 *
1257 * Returns true on success, false otherwise.
1258 */
1259 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,
1260 struct ccs_path_number3_acl *ptr,
1261 const struct ccs_condition *cond)
1262 {
1263 int pos;
1264 u8 bit;
1265 const u16 perm = ptr->perm;
1266 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;
1267 bit++) {
1268 if (!(perm & (1 << bit)))
1269 continue;
1270 pos = head->read_avail;
1271 if (!ccs_io_printf(head, "allow_%s",
1272 ccs_path_number32keyword(bit)) ||
1273 !ccs_print_name_union(head, &ptr->name) ||
1274 !ccs_print_number_union(head, &ptr->mode) ||
1275 !ccs_print_number_union(head, &ptr->major) ||
1276 !ccs_print_number_union(head, &ptr->minor) ||
1277 !ccs_print_condition(head, cond)) {
1278 head->read_bit = bit;
1279 head->read_avail = pos;
1280 return false;
1281 }
1282 }
1283 head->read_bit = 0;
1284 return true;
1285 }
1286
1287 /**
1288 * ccs_print_path2_acl - Print a path2 ACL entry.
1289 *
1290 * @head: Pointer to "struct ccs_io_buffer".
1291 * @ptr: Pointer to "struct ccs_path2_acl".
1292 * @cond: Pointer to "struct ccs_condition". May be NULL.
1293 *
1294 * Returns true on success, false otherwise.
1295 */
1296 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1297 struct ccs_path2_acl *ptr,
1298 const struct ccs_condition *cond)
1299 {
1300 int pos;
1301 u8 bit;
1302 const u8 perm = ptr->perm;
1303 for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1304 if (!(perm & (1 << bit)))
1305 continue;
1306 pos = head->read_avail;
1307 if (!ccs_io_printf(head, "allow_%s",
1308 ccs_path22keyword(bit)) ||
1309 !ccs_print_name_union(head, &ptr->name1) ||
1310 !ccs_print_name_union(head, &ptr->name2) ||
1311 !ccs_print_condition(head, cond)) {
1312 head->read_bit = bit;
1313 head->read_avail = pos;
1314 return false;
1315 }
1316 }
1317 head->read_bit = 0;
1318 return true;
1319 }
1320
1321 /**
1322 * ccs_print_path_number_acl - Print a path_number ACL entry.
1323 *
1324 * @head: Pointer to "struct ccs_io_buffer".
1325 * @ptr: Pointer to "struct ccs_path_number_acl".
1326 * @cond: Pointer to "struct ccs_condition". May be NULL.
1327 *
1328 * Returns true on success, false otherwise.
1329 */
1330 static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1331 struct ccs_path_number_acl *ptr,
1332 const struct ccs_condition *cond)
1333 {
1334 int pos;
1335 u8 bit;
1336 const u8 perm = ptr->perm;
1337 for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;
1338 bit++) {
1339 if (!(perm & (1 << bit)))
1340 continue;
1341 pos = head->read_avail;
1342 if (!ccs_io_printf(head, "allow_%s",
1343 ccs_path_number2keyword(bit)) ||
1344 !ccs_print_name_union(head, &ptr->name) ||
1345 !ccs_print_number_union(head, &ptr->number) ||
1346 !ccs_print_condition(head, cond)) {
1347 head->read_bit = bit;
1348 head->read_avail = pos;
1349 return false;
1350 }
1351 }
1352 head->read_bit = 0;
1353 return true;
1354 }
1355
1356 /**
1357 * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1358 *
1359 * @head: Pointer to "struct ccs_io_buffer".
1360 * @ptr: Pointer to "struct ccs_env_acl".
1361 * @cond: Pointer to "struct ccs_condition". May be NULL.
1362 *
1363 * Returns true on success, false otherwise.
1364 */
1365 static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1366 struct ccs_env_acl *ptr,
1367 const struct ccs_condition *cond)
1368 {
1369 const int pos = head->read_avail;
1370 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1371 !ccs_print_condition(head, cond)) {
1372 head->read_avail = pos;
1373 return false;
1374 }
1375 return true;
1376 }
1377
1378 /**
1379 * ccs_print_capability_acl - Print a capability ACL entry.
1380 *
1381 * @head: Pointer to "struct ccs_io_buffer".
1382 * @ptr: Pointer to "struct ccs_capability_acl".
1383 * @cond: Pointer to "struct ccs_condition". May be NULL.
1384 *
1385 * Returns true on success, false otherwise.
1386 */
1387 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1388 struct ccs_capability_acl *ptr,
1389 const struct ccs_condition *cond)
1390 {
1391 const int pos = head->read_avail;
1392 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1393 ccs_cap2keyword(ptr->operation)) ||
1394 !ccs_print_condition(head, cond)) {
1395 head->read_avail = pos;
1396 return false;
1397 }
1398 return true;
1399 }
1400
1401 /**
1402 * ccs_print_network_acl - Print a network ACL entry.
1403 *
1404 * @head: Pointer to "struct ccs_io_buffer".
1405 * @ptr: Pointer to "struct ccs_ip_network_acl".
1406 * @cond: Pointer to "struct ccs_condition". May be NULL.
1407 *
1408 * Returns true on success, false otherwise.
1409 */
1410 static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1411 struct ccs_ip_network_acl *ptr,
1412 const struct ccs_condition *cond)
1413 {
1414 int pos;
1415 u8 bit;
1416 const u8 perm = ptr->perm;
1417 char buf[128];
1418 for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1419 const char *w[2] = { "", "" };
1420 if (!(perm & (1 << bit)))
1421 continue;
1422 pos = head->read_avail;
1423 switch (ptr->address_type) {
1424 case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1425 w[0] = "@";
1426 w[1] = ptr->address.group->group_name->name;
1427 break;
1428 case CCS_IP_ADDRESS_TYPE_IPv4:
1429 ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1430 ptr->address.ipv4.max);
1431 w[0] = buf;
1432 break;
1433 case CCS_IP_ADDRESS_TYPE_IPv6:
1434 ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1435 ptr->address.ipv6.max);
1436 w[0] = buf;
1437 break;
1438 }
1439 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1440 ccs_net2keyword(bit), w[0], w[1]) ||
1441 !ccs_print_number_union(head, &ptr->port) ||
1442 !ccs_print_condition(head, cond))
1443 goto out;
1444 }
1445 head->read_bit = 0;
1446 return true;
1447 out:
1448 head->read_bit = bit;
1449 head->read_avail = pos;
1450 return false;
1451 }
1452
1453 /**
1454 * ccs_print_signal_acl - Print a signal ACL entry.
1455 *
1456 * @head: Pointer to "struct ccs_io_buffer".
1457 * @ptr: Pointer to "struct signale_acl".
1458 * @cond: Pointer to "struct ccs_condition". May be NULL.
1459 *
1460 * Returns true on success, false otherwise.
1461 */
1462 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1463 struct ccs_signal_acl *ptr,
1464 const struct ccs_condition *cond)
1465 {
1466 const int pos = head->read_avail;
1467 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1468 ptr->sig, ptr->domainname->name) ||
1469 !ccs_print_condition(head, cond)) {
1470 head->read_avail = pos;
1471 return false;
1472 }
1473 return true;
1474 }
1475
1476 /**
1477 * ccs_print_execute_handler_record - Print an execute handler ACL entry.
1478 *
1479 * @head: Pointer to "struct ccs_io_buffer".
1480 * @keyword: Name of the keyword.
1481 * @ptr: Pointer to "struct ccs_execute_handler_record".
1482 *
1483 * Returns true on success, false otherwise.
1484 */
1485 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,
1486 const char *keyword,
1487 struct ccs_execute_handler_record
1488 *ptr)
1489 {
1490 return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1491 }
1492
1493 /**
1494 * ccs_print_mount_acl - Print a mount ACL entry.
1495 *
1496 * @head: Pointer to "struct ccs_io_buffer".
1497 * @ptr: Pointer to "struct ccs_mount_acl".
1498 * @cond: Pointer to "struct ccs_condition". May be NULL.
1499 *
1500 * Returns true on success, false otherwise.
1501 */
1502 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1503 struct ccs_mount_acl *ptr,
1504 const struct ccs_condition *cond)
1505 {
1506 const int pos = head->read_avail;
1507 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1508 !ccs_print_name_union(head, &ptr->dev_name) ||
1509 !ccs_print_name_union(head, &ptr->dir_name) ||
1510 !ccs_print_name_union(head, &ptr->fs_type) ||
1511 !ccs_print_number_union(head, &ptr->flags) ||
1512 !ccs_print_condition(head, cond)) {
1513 head->read_avail = pos;
1514 return false;
1515 }
1516 return true;
1517 }
1518
1519 /**
1520 * ccs_print_entry - Print an ACL entry.
1521 *
1522 * @head: Pointer to "struct ccs_io_buffer".
1523 * @ptr: Pointer to an ACL entry.
1524 *
1525 * Returns true on success, false otherwise.
1526 */
1527 static bool ccs_print_entry(struct ccs_io_buffer *head,
1528 struct ccs_acl_info *ptr)
1529 {
1530 const struct ccs_condition *cond = ptr->cond;
1531 const u8 acl_type = ptr->type;
1532 if (ptr->is_deleted)
1533 return true;
1534 if (acl_type == CCS_TYPE_PATH_ACL) {
1535 struct ccs_path_acl *acl
1536 = container_of(ptr, struct ccs_path_acl, head);
1537 return ccs_print_path_acl(head, acl, cond);
1538 }
1539 if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {
1540 struct ccs_execute_handler_record *acl
1541 = container_of(ptr, struct ccs_execute_handler_record,
1542 head);
1543 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;
1544 return ccs_print_execute_handler_record(head, keyword, acl);
1545 }
1546 if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1547 struct ccs_execute_handler_record *acl
1548 = container_of(ptr, struct ccs_execute_handler_record,
1549 head);
1550 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1551 return ccs_print_execute_handler_record(head, keyword, acl);
1552 }
1553 if (head->read_execute_only)
1554 return true;
1555 if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {
1556 struct ccs_path_number3_acl *acl
1557 = container_of(ptr, struct ccs_path_number3_acl, head);
1558 return ccs_print_path_number3_acl(head, acl, cond);
1559 }
1560 if (acl_type == CCS_TYPE_PATH2_ACL) {
1561 struct ccs_path2_acl *acl
1562 = container_of(ptr, struct ccs_path2_acl, head);
1563 return ccs_print_path2_acl(head, acl, cond);
1564 }
1565 if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1566 struct ccs_path_number_acl *acl
1567 = container_of(ptr, struct ccs_path_number_acl, head);
1568 return ccs_print_path_number_acl(head, acl, cond);
1569 }
1570 if (acl_type == CCS_TYPE_ENV_ACL) {
1571 struct ccs_env_acl *acl
1572 = container_of(ptr, struct ccs_env_acl, head);
1573 return ccs_print_env_acl(head, acl, cond);
1574 }
1575 if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1576 struct ccs_capability_acl *acl
1577 = container_of(ptr, struct ccs_capability_acl, head);
1578 return ccs_print_capability_acl(head, acl, cond);
1579 }
1580 if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1581 struct ccs_ip_network_acl *acl
1582 = container_of(ptr, struct ccs_ip_network_acl, head);
1583 return ccs_print_network_acl(head, acl, cond);
1584 }
1585 if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1586 struct ccs_signal_acl *acl
1587 = container_of(ptr, struct ccs_signal_acl, head);
1588 return ccs_print_signal_acl(head, acl, cond);
1589 }
1590 if (acl_type == CCS_TYPE_MOUNT_ACL) {
1591 struct ccs_mount_acl *acl
1592 = container_of(ptr, struct ccs_mount_acl, head);
1593 return ccs_print_mount_acl(head, acl, cond);
1594 }
1595 BUG(); /* This must not happen. */
1596 return false;
1597 }
1598
1599 /**
1600 * ccs_read_domain - Read domain policy.
1601 *
1602 * @head: Pointer to "struct ccs_io_buffer".
1603 *
1604 * Caller holds ccs_read_lock().
1605 */
1606 static void ccs_read_domain(struct ccs_io_buffer *head)
1607 {
1608 struct list_head *dpos;
1609 struct list_head *apos;
1610 if (head->read_eof)
1611 return;
1612 if (head->read_step == 0)
1613 head->read_step = 1;
1614 list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
1615 struct ccs_domain_info *domain;
1616 const char *quota_exceeded = "";
1617 const char *transition_failed = "";
1618 const char *ignore_global_allow_read = "";
1619 const char *ignore_global_allow_env = "";
1620 domain = list_entry(dpos, struct ccs_domain_info, list);
1621 if (head->read_step != 1)
1622 goto acl_loop;
1623 if (domain->is_deleted && !head->read_single_domain)
1624 continue;
1625 /* Print domainname and flags. */
1626 if (domain->quota_warned)
1627 quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";
1628 if (domain->domain_transition_failed)
1629 transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";
1630 if (domain->ignore_global_allow_read)
1631 ignore_global_allow_read
1632 = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
1633 if (domain->ignore_global_allow_env)
1634 ignore_global_allow_env
1635 = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
1636 if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"
1637 "%s%s%s%s\n", domain->domainname->name,
1638 domain->profile, quota_exceeded,
1639 transition_failed,
1640 ignore_global_allow_read,
1641 ignore_global_allow_env))
1642 return;
1643 head->read_step = 2;
1644 acl_loop:
1645 if (head->read_step == 3)
1646 goto tail_mark;
1647 /* Print ACL entries in the domain. */
1648 list_for_each_cookie(apos, head->read_var2,
1649 &domain->acl_info_list) {
1650 struct ccs_acl_info *ptr
1651 = list_entry(apos, struct ccs_acl_info, list);
1652 if (!ccs_print_entry(head, ptr))
1653 return;
1654 }
1655 head->read_step = 3;
1656 tail_mark:
1657 if (!ccs_io_printf(head, "\n"))
1658 return;
1659 head->read_step = 1;
1660 if (head->read_single_domain)
1661 break;
1662 }
1663 head->read_eof = true;
1664 }
1665
1666 /**
1667 * ccs_write_domain_profile - Assign profile for specified domain.
1668 *
1669 * @head: Pointer to "struct ccs_io_buffer".
1670 *
1671 * Returns 0 on success, -EINVAL otherwise.
1672 *
1673 * This is equivalent to doing
1674 *
1675 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1676 * /usr/sbin/ccs-loadpolicy -d
1677 *
1678 * Caller holds ccs_read_lock().
1679 */
1680 static int ccs_write_domain_profile(struct ccs_io_buffer *head)
1681 {
1682 char *data = head->write_buf;
1683 char *cp = strchr(data, ' ');
1684 struct ccs_domain_info *domain;
1685 unsigned int profile;
1686 if (!cp)
1687 return -EINVAL;
1688 *cp = '\0';
1689 profile = simple_strtoul(data, NULL, 10);
1690 if (profile >= CCS_MAX_PROFILES)
1691 return -EINVAL;
1692 domain = ccs_find_domain(cp + 1);
1693 if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1694 domain->profile = (u8) profile;
1695 return 0;
1696 }
1697
1698 /**
1699 * ccs_read_domain_profile - Read only domainname and profile.
1700 *
1701 * @head: Pointer to "struct ccs_io_buffer".
1702 *
1703 * This is equivalent to doing
1704 *
1705 * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1706 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1707 * domainname = $0; } else if ( $1 == "use_profile" ) {
1708 * print $2 " " domainname; domainname = ""; } } ; '
1709 *
1710 * Caller holds ccs_read_lock().
1711 */
1712 static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1713 {
1714 struct list_head *pos;
1715 if (head->read_eof)
1716 return;
1717 list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1718 struct ccs_domain_info *domain;
1719 domain = list_entry(pos, struct ccs_domain_info, list);
1720 if (domain->is_deleted)
1721 continue;
1722 if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1723 domain->domainname->name))
1724 return;
1725 }
1726 head->read_eof = true;
1727 }
1728
1729 /**
1730 * ccs_write_pid: Specify PID to obtain domainname.
1731 *
1732 * @head: Pointer to "struct ccs_io_buffer".
1733 *
1734 * Returns 0.
1735 */
1736 static int ccs_write_pid(struct ccs_io_buffer *head)
1737 {
1738 head->read_eof = false;
1739 return 0;
1740 }
1741
1742 /**
1743 * ccs_read_pid - Read information of a process.
1744 *
1745 * @head: Pointer to "struct ccs_io_buffer".
1746 *
1747 * Returns the domainname which the specified PID is in or
1748 * process information of the specified PID on success,
1749 * empty string otherwise.
1750 *
1751 * Caller holds ccs_read_lock().
1752 */
1753 static void ccs_read_pid(struct ccs_io_buffer *head)
1754 {
1755 char *buf = head->write_buf;
1756 bool task_info = false;
1757 bool global_pid = false;
1758 unsigned int pid;
1759 struct task_struct *p;
1760 struct ccs_domain_info *domain = NULL;
1761 u32 ccs_flags = 0;
1762 /* Accessing write_buf is safe because head->io_sem is held. */
1763 if (!buf) {
1764 head->read_eof = true;
1765 return; /* Do nothing if open(O_RDONLY). */
1766 }
1767 if (head->read_avail || head->read_eof)
1768 return;
1769 head->read_eof = true;
1770 if (ccs_str_starts(&buf, "info "))
1771 task_info = true;
1772 if (ccs_str_starts(&buf, "global-pid "))
1773 global_pid = true;
1774 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1775 ccs_tasklist_lock();
1776 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1777 if (global_pid)
1778 p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1779 else
1780 p = ccsecurity_exports.find_task_by_vpid(pid);
1781 #else
1782 p = find_task_by_pid(pid);
1783 #endif
1784 if (p) {
1785 domain = ccs_task_domain(p);
1786 ccs_flags = p->ccs_flags;
1787 }
1788 ccs_tasklist_unlock();
1789 if (!domain)
1790 return;
1791 if (!task_info)
1792 ccs_io_printf(head, "%u %u %s", pid, domain->profile,
1793 domain->domainname->name);
1794 else
1795 ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1796 "state[0]=%u state[1]=%u state[2]=%u", pid,
1797 ccs_yesno(ccs_flags &
1798 CCS_TASK_IS_MANAGER),
1799 ccs_yesno(ccs_flags &
1800 CCS_TASK_IS_EXECUTE_HANDLER),
1801 (u8) (ccs_flags >> 24),
1802 (u8) (ccs_flags >> 16),
1803 (u8) (ccs_flags >> 8));
1804 }
1805
1806 /**
1807 * ccs_write_exception - Write exception policy.
1808 *
1809 * @head: Pointer to "struct ccs_io_buffer".
1810 *
1811 * Returns 0 on success, negative value otherwise.
1812 */
1813 static int ccs_write_exception(struct ccs_io_buffer *head)
1814 {
1815 char *data = head->write_buf;
1816 const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1817 u8 i;
1818 static const struct {
1819 const char *keyword;
1820 int (*write) (char *, const bool, const u8);
1821 } ccs_callback[10] = {
1822 { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper },
1823 { CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1824 ccs_write_domain_initializer },
1825 { CCS_KEYWORD_KEEP_DOMAIN, ccs_write_domain_keeper },
1826 { CCS_KEYWORD_INITIALIZE_DOMAIN,
1827 ccs_write_domain_initializer },
1828 { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1829 { CCS_KEYWORD_ALLOW_READ, ccs_write_global_read },
1830 { CCS_KEYWORD_ALLOW_ENV, ccs_write_global_env },
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 < 10; 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 return -EINVAL;
1849 }
1850
1851 /**
1852 * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1853 *
1854 * @head: Pointer to "struct ccs_io_buffer".
1855 * @idx: Index number.
1856 *
1857 * Returns true on success, false otherwise.
1858 *
1859 * Caller holds ccs_read_lock().
1860 */
1861 static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1862 {
1863 struct list_head *gpos;
1864 struct list_head *mpos;
1865 const char *w[3] = { "", "", "" };
1866 if (idx == CCS_PATH_GROUP)
1867 w[0] = CCS_KEYWORD_PATH_GROUP;
1868 else if (idx == CCS_NUMBER_GROUP)
1869 w[0] = CCS_KEYWORD_NUMBER_GROUP;
1870 else if (idx == CCS_ADDRESS_GROUP)
1871 w[0] = CCS_KEYWORD_ADDRESS_GROUP;
1872 list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1873 struct ccs_group *group =
1874 list_entry(gpos, struct ccs_group, head.list);
1875 w[1] = group->group_name->name;
1876 list_for_each_cookie(mpos, head->read_var2,
1877 &group->member_list) {
1878 char buffer[128];
1879 struct ccs_acl_head *ptr =
1880 list_entry(mpos, struct ccs_acl_head, list);
1881 if (ptr->is_deleted)
1882 continue;
1883 if (idx == CCS_PATH_GROUP) {
1884 w[2] = container_of(ptr, struct ccs_path_group,
1885 head)->member_name->name;
1886 } else if (idx == CCS_NUMBER_GROUP) {
1887 w[2] = buffer;
1888 ccs_print_number(buffer, sizeof(buffer),
1889 &container_of
1890 (ptr, struct ccs_number_group,
1891 head)->number);
1892 } else if (idx == CCS_ADDRESS_GROUP) {
1893 struct ccs_address_group *member =
1894 container_of(ptr, typeof(*member),
1895 head);
1896 w[2] = buffer;
1897 if (member->is_ipv6)
1898 ccs_print_ipv6(buffer, sizeof(buffer),
1899 member->min.ipv6,
1900 member->max.ipv6);
1901 else
1902 ccs_print_ipv4(buffer, sizeof(buffer),
1903 member->min.ipv4,
1904 member->max.ipv4);
1905 }
1906 if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1907 w[2]))
1908 return false;
1909 }
1910 }
1911 return true;
1912 }
1913
1914 /**
1915 * ccs_read_policy - Read "struct ccs_..._entry" list.
1916 *
1917 * @head: Pointer to "struct ccs_io_buffer".
1918 * @idx: Index number.
1919 *
1920 * Returns true on success, false otherwise.
1921 *
1922 * Caller holds ccs_read_lock().
1923 */
1924 static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1925 {
1926 struct list_head *pos;
1927 list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1928 const char *w[4] = { "", "", "", "" };
1929 char buffer[16];
1930 struct ccs_condition *cond = NULL;
1931 struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1932 list);
1933 if (acl->is_deleted)
1934 continue;
1935 switch (idx) {
1936 case CCS_ID_DOMAIN_KEEPER:
1937 {
1938 struct ccs_domain_keeper *ptr =
1939 container_of(acl, typeof(*ptr), head);
1940 w[0] = ptr->is_not ?
1941 CCS_KEYWORD_NO_KEEP_DOMAIN :
1942 CCS_KEYWORD_KEEP_DOMAIN;
1943 if (ptr->program) {
1944 w[1] = ptr->program->name;
1945 w[2] = " from ";
1946 }
1947 w[3] = ptr->domainname->name;
1948 }
1949 break;
1950 case CCS_ID_DOMAIN_INITIALIZER:
1951 {
1952 struct ccs_domain_initializer *ptr =
1953 container_of(acl, typeof(*ptr), head);
1954 w[0] = ptr->is_not ?
1955 CCS_KEYWORD_NO_INITIALIZE_DOMAIN :
1956 CCS_KEYWORD_INITIALIZE_DOMAIN;
1957 w[1] = ptr->program->name;
1958 if (ptr->domainname) {
1959 w[2] = " from ";
1960 w[3] = ptr->domainname->name;
1961 }
1962 }
1963 break;
1964 case CCS_ID_AGGREGATOR:
1965 {
1966 struct ccs_aggregator *ptr =
1967 container_of(acl, typeof(*ptr), head);
1968 w[0] = CCS_KEYWORD_AGGREGATOR;
1969 w[1] = ptr->original_name->name;
1970 w[2] = " ";
1971 w[3] = ptr->aggregated_name->name;
1972 }
1973 break;
1974 case CCS_ID_GLOBAL_READ:
1975 {
1976 struct ccs_global_read *ptr =
1977 container_of(acl, typeof(*ptr), head);
1978 w[0] = CCS_KEYWORD_ALLOW_READ;
1979 w[1] = ptr->filename->name;
1980 cond = ptr->cond;
1981 }
1982 break;
1983 case CCS_ID_PATTERN:
1984 {
1985 struct ccs_pattern *ptr =
1986 container_of(acl, typeof(*ptr), head);
1987 w[0] = CCS_KEYWORD_FILE_PATTERN;
1988 w[1] = ptr->pattern->name;
1989 }
1990 break;
1991 case CCS_ID_NO_REWRITE:
1992 {
1993 struct ccs_no_rewrite *ptr =
1994 container_of(acl, typeof(*ptr), head);
1995 w[0] = CCS_KEYWORD_DENY_REWRITE;
1996 w[1] = ptr->pattern->name;
1997 }
1998 break;
1999 case CCS_ID_GLOBAL_ENV:
2000 {
2001 struct ccs_global_env *ptr =
2002 container_of(acl, typeof(*ptr), head);
2003 w[0] = CCS_KEYWORD_ALLOW_ENV;
2004 w[1] = ptr->env->name;
2005 cond = ptr->cond;
2006 }
2007 break;
2008 case CCS_ID_RESERVEDPORT:
2009 {
2010 struct ccs_reserved *ptr =
2011 container_of(acl, typeof(*ptr), head);
2012 const u16 min_port = ptr->min_port;
2013 const u16 max_port = ptr->max_port;
2014 w[0] = CCS_KEYWORD_DENY_AUTOBIND;
2015 snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
2016 min_port, min_port != max_port ?
2017 '-' : '\0', max_port);
2018 buffer[sizeof(buffer) - 1] = '\0';
2019 w[1] = buffer;
2020 }
2021 break;
2022 default:
2023 continue;
2024 }
2025 {
2026 const int pos = head->read_avail;
2027 if (!ccs_io_printf(head, "%s%s%s%s", w[0], w[1],
2028 w[2], w[3])
2029 || !ccs_print_condition(head, cond)) {
2030 head->read_avail = pos;
2031 return false;
2032 }
2033 }
2034 }
2035 return true;
2036 }
2037
2038 /**
2039 * ccs_read_exception - Read exception policy.
2040 *
2041 * @head: Pointer to "struct ccs_io_buffer".
2042 *
2043 * Caller holds ccs_read_lock().
2044 */
2045 static void ccs_read_exception(struct ccs_io_buffer *head)
2046 {
2047 if (head->read_eof)
2048 return;
2049 while (head->read_step < CCS_MAX_POLICY &&
2050 ccs_read_policy(head, head->read_step))
2051 head->read_step++;
2052 if (head->read_step < CCS_MAX_POLICY)
2053 return;
2054 while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2055 ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
2056 head->read_step++;
2057 head->read_eof = head->read_step == CCS_MAX_POLICY + CCS_MAX_GROUP;
2058 }
2059
2060 /**
2061 * ccs_get_argv0 - Get argv[0].
2062 *
2063 * @ee: Pointer to "struct ccs_execve".
2064 *
2065 * Returns true on success, false otherwise.
2066 */
2067 static bool ccs_get_argv0(struct ccs_execve *ee)
2068 {
2069 struct linux_binprm *bprm = ee->bprm;
2070 char *arg_ptr = ee->tmp;
2071 int arg_len = 0;
2072 unsigned long pos = bprm->p;
2073 int offset = pos % PAGE_SIZE;
2074 bool done = false;
2075 if (!bprm->argc)
2076 goto out;
2077 while (1) {
2078 if (!ccs_dump_page(bprm, pos, &ee->dump))
2079 goto out;
2080 pos += PAGE_SIZE - offset;
2081 /* Read. */
2082 while (offset < PAGE_SIZE) {
2083 const char *kaddr = ee->dump.data;
2084 const unsigned char c = kaddr[offset++];
2085 if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
2086 if (c == '\\') {
2087 arg_ptr[arg_len++] = '\\';
2088 arg_ptr[arg_len++] = '\\';
2089 } else if (c > ' ' && c < 127) {
2090 arg_ptr[arg_len++] = c;
2091 } else {
2092 arg_ptr[arg_len++] = '\\';
2093 arg_ptr[arg_len++] = (c >> 6) + '0';
2094 arg_ptr[arg_len++]
2095 = ((c >> 3) & 7) + '0';
2096 arg_ptr[arg_len++] = (c & 7) + '0';
2097 }
2098 } else {
2099 arg_ptr[arg_len] = '\0';
2100 done = true;
2101 break;
2102 }
2103 }
2104 offset = 0;
2105 if (done)
2106 break;
2107 }
2108 return true;
2109 out:
2110 return false;
2111 }
2112
2113 /**
2114 * ccs_get_execute_condition - Get condition part for execute requests.
2115 *
2116 * @ee: Pointer to "struct ccs_execve".
2117 *
2118 * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2119 */
2120 static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve
2121 *ee)
2122 {
2123 struct ccs_condition *cond;
2124 char *buf;
2125 int len = 256;
2126 char *realpath = NULL;
2127 char *argv0 = NULL;
2128 const struct ccs_profile *profile = ccs_profile(ccs_current_domain()->
2129 profile);
2130 if (profile->learning->learning_exec_realpath) {
2131 struct file *file = ee->bprm->file;
2132 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2133 struct path path = { file->f_vfsmnt, file->f_dentry };
2134 realpath = ccs_realpath_from_path(&path);
2135 #else
2136 realpath = ccs_realpath_from_path(&file->f_path);
2137 #endif
2138 if (realpath)
2139 len += strlen(realpath) + 17;
2140 }
2141 if (profile->learning->learning_exec_argv0) {
2142 if (ccs_get_argv0(ee)) {
2143 argv0 = ee->tmp;
2144 len += strlen(argv0) + 16;
2145 }
2146 }
2147 buf = kmalloc(len, CCS_GFP_FLAGS);
2148 if (!buf) {
2149 kfree(realpath);
2150 return NULL;
2151 }
2152 snprintf(buf, len - 1, "if");
2153 if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2154 const int pos = strlen(buf);
2155 snprintf(buf + pos, len - pos - 1,
2156 " task.type=execute_handler");
2157 }
2158 if (realpath) {
2159 const int pos = strlen(buf);
2160 snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",
2161 realpath);
2162 kfree(realpath);
2163 }
2164 if (argv0) {
2165 const int pos = strlen(buf);
2166 snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",
2167 argv0);
2168 }
2169 cond = ccs_get_condition(buf);
2170 kfree(buf);
2171 return cond;
2172 }
2173
2174 /**
2175 * ccs_get_symlink_condition - Get condition part for symlink requests.
2176 *
2177 * @r: Pointer to "struct ccs_request_info".
2178 *
2179 * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2180 */
2181 static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info
2182 *r)
2183 {
2184 struct ccs_condition *cond;
2185 char *buf;
2186 int len = 256;
2187 const char *symlink = NULL;
2188 const struct ccs_profile *profile = ccs_profile(r->profile);
2189 if (profile->learning->learning_symlink_target) {
2190 symlink = r->obj->symlink_target->name;
2191 len += strlen(symlink) + 18;
2192 }
2193 buf = kmalloc(len, CCS_GFP_FLAGS);
2194 if (!buf)
2195 return NULL;
2196 snprintf(buf, len - 1, "if");
2197 if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {
2198 const int pos = strlen(buf);
2199 snprintf(buf + pos, len - pos - 1,
2200 " task.type=execute_handler");
2201 }
2202 if (symlink) {
2203 const int pos = strlen(buf);
2204 snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",
2205 symlink);
2206 }
2207 cond = ccs_get_condition(buf);
2208 kfree(buf);
2209 return cond;
2210 }
2211
2212 /* Wait queue for ccs_query_list. */
2213 static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2214
2215 /* Lock for manipulating ccs_query_list. */
2216 static DEFINE_SPINLOCK(ccs_query_list_lock);
2217
2218 /* Structure for query. */
2219 struct ccs_query_entry {
2220 struct list_head list;
2221 char *query;
2222 int query_len;
2223 unsigned int serial;
2224 int timer;
2225 int answer;
2226 };
2227
2228 /* The list for "struct ccs_query_entry". */
2229 static LIST_HEAD(ccs_query_list);
2230
2231 /* Number of "struct file" referring /proc/ccs/query interface. */
2232 static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2233
2234 /**
2235 * ccs_supervisor - Ask for the supervisor's decision.
2236 *
2237 * @r: Pointer to "struct ccs_request_info".
2238 * @fmt: The printf()'s format string, followed by parameters.
2239 *
2240 * Returns 0 if the supervisor decided to permit the access request which
2241 * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2242 * decided to retry the access request which violated the policy in enforcing
2243 * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2244 */
2245 int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2246 {
2247 va_list args;
2248 int error = -EPERM;
2249 int pos;
2250 int len;
2251 static unsigned int ccs_serial;
2252 struct ccs_query_entry *ccs_query_entry = NULL;
2253 bool quota_exceeded = false;
2254 char *header;
2255 struct ccs_domain_info * const domain = ccs_current_domain();
2256 switch (r->mode) {
2257 char *buffer;
2258 struct ccs_condition *cond;
2259 case CCS_CONFIG_LEARNING:
2260 if (!ccs_domain_quota_ok(r))
2261 return 0;
2262 va_start(args, fmt);
2263 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
2264 va_end(args);
2265 buffer = kmalloc(len, CCS_GFP_FLAGS);
2266 if (!buffer)
2267 return 0;
2268 va_start(args, fmt);
2269 vsnprintf(buffer, len - 1, fmt, args);
2270 va_end(args);
2271 ccs_normalize_line(buffer);
2272 if (r->ee && !strncmp(buffer, "allow_execute ", 14))
2273 cond = ccs_get_execute_condition(r->ee);
2274 else if (r->obj && r->obj->symlink_target)
2275 cond = ccs_get_symlink_condition(r);
2276 else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
2277 char str[] = "if task.type=execute_handler";
2278 cond = ccs_get_condition(str);
2279 } else
2280 cond = NULL;
2281 ccs_write_domain2(buffer, domain, cond, false);
2282 ccs_put_condition(cond);
2283 kfree(buffer);
2284 /* fall through */
2285 case CCS_CONFIG_PERMISSIVE:
2286 return 0;
2287 }
2288 if (!atomic_read(&ccs_query_observers)) {
2289 int i;
2290 if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2291 return -EPERM;
2292 for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2293 enforcing_penalty; i++) {
2294 set_current_state(TASK_INTERRUPTIBLE);
2295 schedule_timeout(HZ / 10);
2296 }
2297 return -EPERM;
2298 }
2299 va_start(args, fmt);
2300 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2301 va_end(args);
2302 header = ccs_init_log(&len, r);
2303 if (!header)
2304 goto out;
2305 ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2306 if (!ccs_query_entry)
2307 goto out;
2308 len = ccs_round2(len);
2309 ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2310 if (!ccs_query_entry->query)
2311 goto out;
2312 INIT_LIST_HEAD(&ccs_query_entry->list);
2313 spin_lock(&ccs_query_list_lock);
2314 if (ccs_quota_for_query && ccs_query_memory_size + len +
2315 sizeof(*ccs_query_entry) >= ccs_quota_for_query) {
2316 quota_exceeded = true;
2317 } else {
2318 ccs_query_memory_size += len + sizeof(*ccs_query_entry);
2319 ccs_query_entry->serial = ccs_serial++;
2320 }
2321 spin_unlock(&ccs_query_list_lock);
2322 if (quota_exceeded)
2323 goto out;
2324 pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
2325 ccs_query_entry->serial, r->retry, header);
2326 kfree(header);
2327 header = NULL;
2328 va_start(args, fmt);
2329 vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);
2330 ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
2331 va_end(args);
2332 spin_lock(&ccs_query_list_lock);
2333 list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2334 spin_unlock(&ccs_query_list_lock);
2335 /* Give 10 seconds for supervisor's opinion. */
2336 for (ccs_query_entry->timer = 0;
2337 atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;
2338 ccs_query_entry->timer++) {
2339 wake_up(&ccs_query_wait);
2340 set_current_state(TASK_INTERRUPTIBLE);
2341 schedule_timeout(HZ / 10);
2342 if (ccs_query_entry->answer)
2343 break;
2344 }
2345 spin_lock(&ccs_query_list_lock);
2346 list_del(&ccs_query_entry->list);
2347 ccs_query_memory_size -= len + sizeof(*ccs_query_entry);
2348 spin_unlock(&ccs_query_list_lock);
2349 switch (ccs_query_entry->answer) {
2350 case 3: /* Asked to retry by administrator. */
2351 error = CCS_RETRY_REQUEST;
2352 r->retry++;
2353 break;
2354 case 1:
2355 /* Granted by administrator. */
2356 error = 0;
2357 break;
2358 case 0:
2359 /* Timed out. */
2360 break;
2361 default:
2362 /* Rejected by administrator. */
2363 break;
2364 }
2365 out:
2366 if (ccs_query_entry)
2367 kfree(ccs_query_entry->query);
2368 kfree(ccs_query_entry);
2369 kfree(header);
2370 return error;
2371 }
2372
2373 /**
2374 * ccs_poll_query - poll() for /proc/ccs/query.
2375 *
2376 * @file: Pointer to "struct file".
2377 * @wait: Pointer to "poll_table".
2378 *
2379 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2380 *
2381 * Waits for access requests which violated policy in enforcing mode.
2382 */
2383 static int ccs_poll_query(struct file *file, poll_table *wait)
2384 {
2385 struct list_head *tmp;
2386 bool found = false;
2387 u8 i;
2388 for (i = 0; i < 2; i++) {
2389 spin_lock(&ccs_query_list_lock);
2390 list_for_each(tmp, &ccs_query_list) {
2391 struct ccs_query_entry *ptr =
2392 list_entry(tmp, struct ccs_query_entry, list);
2393 if (ptr->answer)
2394 continue;
2395 found = true;
2396 break;
2397 }
2398 spin_unlock(&ccs_query_list_lock);
2399 if (found)
2400 return POLLIN | POLLRDNORM;
2401 if (i)
2402 break;
2403 poll_wait(file, &ccs_query_wait, wait);
2404 }
2405 return 0;
2406 }
2407
2408 /**
2409 * ccs_read_query - Read access requests which violated policy in enforcing mode.
2410 *
2411 * @head: Pointer to "struct ccs_io_buffer".
2412 */
2413 static void ccs_read_query(struct ccs_io_buffer *head)
2414 {
2415 struct list_head *tmp;
2416 int pos = 0;
2417 int len = 0;
2418 char *buf;
2419 if (head->read_avail)
2420 return;
2421 if (head->read_buf) {
2422 kfree(head->read_buf);
2423 head->read_buf = NULL;
2424 head->readbuf_size = 0;
2425 }
2426 spin_lock(&ccs_query_list_lock);
2427 list_for_each(tmp, &ccs_query_list) {
2428 struct ccs_query_entry *ptr
2429 = list_entry(tmp, struct ccs_query_entry, list);
2430 if (ptr->answer)
2431 continue;
2432 if (pos++ != head->read_step)
2433 continue;
2434 len = ptr->query_len;
2435 break;
2436 }
2437 spin_unlock(&ccs_query_list_lock);
2438 if (!len) {
2439 head->read_step = 0;
2440 return;
2441 }
2442 buf = kzalloc(len, CCS_GFP_FLAGS);
2443 if (!buf)
2444 return;
2445 pos = 0;
2446 spin_lock(&ccs_query_list_lock);
2447 list_for_each(tmp, &ccs_query_list) {
2448 struct ccs_query_entry *ptr
2449 = list_entry(tmp, struct ccs_query_entry, list);
2450 if (ptr->answer)
2451 continue;
2452 if (pos++ != head->read_step)
2453 continue;
2454 /*
2455 * Some query can be skipped because ccs_query_list
2456 * can change, but I don't care.
2457 */
2458 if (len == ptr->query_len)
2459 memmove(buf, ptr->query, len);
2460 break;
2461 }
2462 spin_unlock(&ccs_query_list_lock);
2463 if (buf[0]) {
2464 head->read_avail = len;
2465 head->readbuf_size = head->read_avail;
2466 head->read_buf = buf;
2467 head->read_step++;
2468 } else {
2469 kfree(buf);
2470 }
2471 }
2472
2473 /**
2474 * ccs_write_answer - Write the supervisor's decision.
2475 *
2476 * @head: Pointer to "struct ccs_io_buffer".
2477 *
2478 * Returns 0 on success, -EINVAL otherwise.
2479 */
2480 static int ccs_write_answer(struct ccs_io_buffer *head)
2481 {
2482 char *data = head->write_buf;
2483 struct list_head *tmp;
2484 unsigned int serial;
2485 unsigned int answer;
2486 spin_lock(&ccs_query_list_lock);
2487 list_for_each(tmp, &ccs_query_list) {
2488 struct ccs_query_entry *ptr
2489 = list_entry(tmp, struct ccs_query_entry, list);
2490 ptr->timer = 0;
2491 }
2492 spin_unlock(&ccs_query_list_lock);
2493 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2494 return -EINVAL;
2495 spin_lock(&ccs_query_list_lock);
2496 list_for_each(tmp, &ccs_query_list) {
2497 struct ccs_query_entry *ptr
2498 = list_entry(tmp, struct ccs_query_entry, list);
2499 if (ptr->serial != serial)
2500 continue;
2501 if (!ptr->answer)
2502 ptr->answer = answer;
2503 break;
2504 }
2505 spin_unlock(&ccs_query_list_lock);
2506 return 0;
2507 }
2508
2509 /**
2510 * ccs_read_version: Get version.
2511 *
2512 * @head: Pointer to "struct ccs_io_buffer".
2513 */
2514 static void ccs_read_version(struct ccs_io_buffer *head)
2515 {
2516 if (head->read_eof)
2517 return;
2518 ccs_io_printf(head, "1.7.2");
2519 head->read_eof = true;
2520 }
2521
2522 /**
2523 * ccs_read_self_domain - Get the current process's domainname.
2524 *
2525 * @head: Pointer to "struct ccs_io_buffer".
2526 */
2527 static void ccs_read_self_domain(struct ccs_io_buffer *head)
2528 {
2529 if (head->read_eof)
2530 return;
2531 /*
2532 * ccs_current_domain()->domainname != NULL because every process
2533 * belongs to a domain and the domain's name cannot be NULL.
2534 */
2535 ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2536 head->read_eof = true;
2537 }
2538
2539 /**
2540 * ccs_open_control - open() for /proc/ccs/ interface.
2541 *
2542 * @type: Type of interface.
2543 * @file: Pointer to "struct file".
2544 *
2545 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
2546 */
2547 int ccs_open_control(const u8 type, struct file *file)
2548 {
2549 struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2550 if (!head)
2551 return -ENOMEM;
2552 mutex_init(&head->io_sem);
2553 head->type = type;
2554 switch (type) {
2555 case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2556 head->write = ccs_write_domain;
2557 head->read = ccs_read_domain;
2558 break;
2559 case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2560 head->write = ccs_write_exception;
2561 head->read = ccs_read_exception;
2562 break;
2563 #ifdef CONFIG_CCSECURITY_AUDIT
2564 case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2565 case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2566 head->poll = ccs_poll_log;
2567 head->read = ccs_read_log;
2568 break;
2569 #endif
2570 case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
2571 head->read = ccs_read_self_domain;
2572 break;
2573 case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2574 head->write = ccs_write_domain_profile;
2575 head->read = ccs_read_domain_profile;
2576 break;
2577 case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */
2578 /* Allow execute_handler to read process's status. */
2579 if (!(current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {
2580 kfree(head);
2581 return -EPERM;
2582 }
2583 /* fall through */
2584 case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */
2585 head->write = ccs_write_pid;
2586 head->read = ccs_read_pid;
2587 break;
2588 case CCS_VERSION: /* /proc/ccs/version */
2589 head->read = ccs_read_version;
2590 head->readbuf_size = 128;
2591 break;
2592 case CCS_MEMINFO: /* /proc/ccs/meminfo */
2593 head->write = ccs_write_memory_quota;
2594 head->read = ccs_read_memory_counter;
2595 head->readbuf_size = 512;
2596 break;
2597 case CCS_PROFILE: /* /proc/ccs/profile */
2598 head->write = ccs_write_profile;
2599 head->read = ccs_read_profile;
2600 break;
2601 case CCS_QUERY: /* /proc/ccs/query */
2602 head->poll = ccs_poll_query;
2603 head->write = ccs_write_answer;
2604 head->read = ccs_read_query;
2605 break;
2606 case CCS_MANAGER: /* /proc/ccs/manager */
2607 head->write = ccs_write_manager;
2608 head->read = ccs_read_manager;
2609 break;
2610 }
2611 if (!(file->f_mode & FMODE_READ)) {
2612 /*
2613 * No need to allocate read_buf since it is not opened
2614 * for reading.
2615 */
2616 head->read = NULL;
2617 head->poll = NULL;
2618 } else if (!head->poll) {
2619 /* Don't allocate read_buf for poll() access. */
2620 if (!head->readbuf_size)
2621 head->readbuf_size = 4096;
2622 head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2623 if (!head->read_buf) {
2624 kfree(head);
2625 return -ENOMEM;
2626 }
2627 }
2628 if (!(file->f_mode & FMODE_WRITE)) {
2629 /*
2630 * No need to allocate write_buf since it is not opened
2631 * for writing.
2632 */
2633 head->write = NULL;
2634 } else if (head->write) {
2635 head->writebuf_size = 4096;
2636 head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2637 if (!head->write_buf) {
2638 kfree(head->read_buf);
2639 kfree(head);
2640 return -ENOMEM;
2641 }
2642 }
2643 if (type != CCS_QUERY &&
2644 type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2645 head->reader_idx = ccs_lock();
2646 file->private_data = head;
2647 /*
2648 * Call the handler now if the file is /proc/ccs/self_domain
2649 * so that the user can use "cat < /proc/ccs/self_domain" to
2650 * know the current process's domainname.
2651 */
2652 if (type == CCS_SELFDOMAIN)
2653 ccs_read_control(file, NULL, 0);
2654 /*
2655 * If the file is /proc/ccs/query , increment the observer counter.
2656 * The obserber counter is used by ccs_supervisor() to see if
2657 * there is some process monitoring /proc/ccs/query.
2658 */
2659 else if (type == CCS_QUERY)
2660 atomic_inc(&ccs_query_observers);
2661 return 0;
2662 }
2663
2664 /**
2665 * ccs_poll_control - poll() for /proc/ccs/ interface.
2666 *
2667 * @file: Pointer to "struct file".
2668 * @wait: Pointer to "poll_table".
2669 *
2670 * Waits for read readiness.
2671 * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2672 * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2673 * /usr/sbin/ccs-auditd .
2674 */
2675 int ccs_poll_control(struct file *file, poll_table *wait)
2676 {
2677 struct ccs_io_buffer *head = file->private_data;
2678 if (!head->poll)
2679 return -ENOSYS;
2680 return head->poll(file, wait);
2681 }
2682
2683 /**
2684 * ccs_read_control - read() for /proc/ccs/ interface.
2685 *
2686 * @file: Pointer to "struct file".
2687 * @buffer: Poiner to buffer to write to.
2688 * @buffer_len: Size of @buffer.
2689 *
2690 * Returns bytes read on success, negative value otherwise.
2691 */
2692 int ccs_read_control(struct file *file, char __user *buffer,
2693 const int buffer_len)
2694 {
2695 int len = 0;
2696 struct ccs_io_buffer *head = file->private_data;
2697 char *cp;
2698 int idx;
2699 if (!head->read)
2700 return -ENOSYS;
2701 if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2702 return -EFAULT;
2703 if (mutex_lock_interruptible(&head->io_sem))
2704 return -EINTR;
2705 idx = ccs_read_lock();
2706 while (1) {
2707 /* Call the policy handler. */
2708 head->read(head);
2709 /* Write to buffer. */
2710 len = head->read_avail;
2711 if (len || head->poll || head->read_eof)
2712 break;
2713 len = head->readbuf_size * 2;
2714 cp = kzalloc(len, CCS_GFP_FLAGS);
2715 if (!cp) {
2716 len = -ENOMEM;
2717 goto out;
2718 }
2719 kfree(head->read_buf);
2720 head->read_buf = cp;
2721 head->readbuf_size = len;
2722 }
2723 if (len > buffer_len)
2724 len = buffer_len;
2725 if (!len)
2726 goto out;
2727 /* head->read_buf changes by some functions. */
2728 cp = head->read_buf;
2729 if (copy_to_user(buffer, cp, len)) {
2730 len = -EFAULT;
2731 goto out;
2732 }
2733 head->read_avail -= len;
2734 memmove(cp, cp + len, head->read_avail);
2735 out:
2736 ccs_read_unlock(idx);
2737 mutex_unlock(&head->io_sem);
2738 return len;
2739 }
2740
2741 /**
2742 * ccs_write_control - write() for /proc/ccs/ interface.
2743 *
2744 * @file: Pointer to "struct file".
2745 * @buffer: Pointer to buffer to read from.
2746 * @buffer_len: Size of @buffer.
2747 *
2748 * Returns @buffer_len on success, negative value otherwise.
2749 */
2750 int ccs_write_control(struct file *file, const char __user *buffer,
2751 const int buffer_len)
2752 {
2753 struct ccs_io_buffer *head = file->private_data;
2754 int error = buffer_len;
2755 int avail_len = buffer_len;
2756 char *cp0 = head->write_buf;
2757 int idx;
2758 if (!head->write)
2759 return -ENOSYS;
2760 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2761 return -EFAULT;
2762 if (mutex_lock_interruptible(&head->io_sem))
2763 return -EINTR;
2764 idx = ccs_read_lock();
2765 /* Don't allow updating policies by non manager programs. */
2766 if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2767 !ccs_manager()) {
2768 ccs_read_unlock(idx);
2769 mutex_unlock(&head->io_sem);
2770 return -EPERM;
2771 }
2772 /* Read a line and dispatch it to the policy handler. */
2773 while (avail_len > 0) {
2774 char c;
2775 if (head->write_avail >= head->writebuf_size - 1) {
2776 const int len = head->writebuf_size * 2;
2777 char *cp = kzalloc(len, CCS_GFP_FLAGS);
2778 if (!cp) {
2779 error = -ENOMEM;
2780 break;
2781 }
2782 memmove(cp, cp0, head->write_avail);
2783 kfree(cp0);
2784 head->write_buf = cp;
2785 cp0 = cp;
2786 head->writebuf_size = len;
2787 }
2788 if (get_user(c, buffer)) {
2789 error = -EFAULT;
2790 break;
2791 }
2792 buffer++;
2793 avail_len--;
2794 cp0[head->write_avail++] = c;
2795 if (c != '\n')
2796 continue;
2797 cp0[head->write_avail - 1] = '\0';
2798 head->write_avail = 0;
2799 ccs_normalize_line(cp0);
2800 head->write(head);
2801 }
2802 ccs_read_unlock(idx);
2803 mutex_unlock(&head->io_sem);
2804 return error;
2805 }
2806
2807 /**
2808 * ccs_close_control - close() for /proc/ccs/ interface.
2809 *
2810 * @file: Pointer to "struct file".
2811 *
2812 * Releases memory and returns 0.
2813 */
2814 int ccs_close_control(struct file *file)
2815 {
2816 struct ccs_io_buffer *head = file->private_data;
2817 const bool is_write = head->write_buf != NULL;
2818 const u8 type = head->type;
2819 /*
2820 * If the file is /proc/ccs/query , decrement the observer counter.
2821 */
2822 if (type == CCS_QUERY)
2823 atomic_dec(&ccs_query_observers);
2824 if (type != CCS_QUERY &&
2825 type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2826 ccs_unlock(head->reader_idx);
2827 /* Release memory used for policy I/O. */
2828 kfree(head->read_buf);
2829 head->read_buf = NULL;
2830 kfree(head->write_buf);
2831 head->write_buf = NULL;
2832 kfree(head);
2833 head = NULL;
2834 file->private_data = NULL;
2835 if (is_write)
2836 ccs_run_gc();
2837 return 0;
2838 }
2839
2840 void __init ccs_policy_io_init(void)
2841 {
2842 ccsecurity_ops.check_profile = ccs_check_profile;
2843 }

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