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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3136 - (show annotations) (download) (as text)
Wed Nov 4 00:31:28 2009 UTC (14 years, 7 months ago) by kumaneko
Original Path: trunk/1.7.x/ccs-patch/security/ccsecurity/policy_io.c
File MIME type: text/x-csrc
File size: 75562 byte(s)


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

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