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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5075 - (show annotations) (download) (as text)
Wed Jun 1 06:06:50 2011 UTC (12 years, 11 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 83449 byte(s)
Change move_namespace directive to invitation model.
1 /*
2 * security/ccsecurity/policy_io.c
3 *
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
5 *
6 * Version: 1.8.2-pre 2011/06/01
7 */
8
9 #include "internal.h"
10
11 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
12
13 /**
14 * __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
15 *
16 * @wq: The waitqueue to wait on.
17 * @condition: A C expression for the event to wait for.
18 * @ret: Timeout, in jiffies.
19 *
20 * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
21 * signal, and the remaining jiffies otherwise if the condition evaluated to
22 * true before the timeout elapsed.
23 *
24 * This is for compatibility with older kernels.
25 */
26 #define __wait_event_interruptible_timeout(wq, condition, ret) \
27 do { \
28 wait_queue_t __wait; \
29 init_waitqueue_entry(&__wait, current); \
30 \
31 add_wait_queue(&wq, &__wait); \
32 for (;;) { \
33 set_current_state(TASK_INTERRUPTIBLE); \
34 if (condition) \
35 break; \
36 if (!signal_pending(current)) { \
37 ret = schedule_timeout(ret); \
38 if (!ret) \
39 break; \
40 continue; \
41 } \
42 ret = -ERESTARTSYS; \
43 break; \
44 } \
45 current->state = TASK_RUNNING; \
46 remove_wait_queue(&wq, &__wait); \
47 } while (0)
48
49 /**
50 * wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
51 *
52 * @wq: The waitqueue to wait on.
53 * @condition: A C expression for the event to wait for.
54 * @timeout: Timeout, in jiffies.
55 *
56 * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
57 * signal, and the remaining jiffies otherwise if the condition evaluated to
58 * true before the timeout elapsed.
59 *
60 * This is for compatibility with older kernels.
61 */
62 #define wait_event_interruptible_timeout(wq, condition, timeout) \
63 ({ \
64 long __ret = timeout; \
65 if (!(condition)) \
66 __wait_event_interruptible_timeout(wq, condition, __ret); \
67 __ret; \
68 })
69
70 #endif
71
72 /**
73 * list_for_each_cookie - iterate over a list with cookie.
74 *
75 * @pos: Pointer to "struct list_head".
76 * @head: Pointer to "struct list_head".
77 */
78 #define list_for_each_cookie(pos, head) \
79 for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \
80 pos != (head); pos = srcu_dereference(pos->next, &ccs_ss))
81
82 /* String table for operation mode. */
83 const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
84 [CCS_CONFIG_DISABLED] = "disabled",
85 [CCS_CONFIG_LEARNING] = "learning",
86 [CCS_CONFIG_PERMISSIVE] = "permissive",
87 [CCS_CONFIG_ENFORCING] = "enforcing"
88 };
89
90 /* String table for /proc/ccs/profile interface. */
91 const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX
92 + CCS_MAX_MAC_CATEGORY_INDEX] = {
93 /* CONFIG::file group */
94 [CCS_MAC_FILE_EXECUTE] = "execute",
95 [CCS_MAC_FILE_OPEN] = "open",
96 [CCS_MAC_FILE_CREATE] = "create",
97 [CCS_MAC_FILE_UNLINK] = "unlink",
98 [CCS_MAC_FILE_GETATTR] = "getattr",
99 [CCS_MAC_FILE_MKDIR] = "mkdir",
100 [CCS_MAC_FILE_RMDIR] = "rmdir",
101 [CCS_MAC_FILE_MKFIFO] = "mkfifo",
102 [CCS_MAC_FILE_MKSOCK] = "mksock",
103 [CCS_MAC_FILE_TRUNCATE] = "truncate",
104 [CCS_MAC_FILE_SYMLINK] = "symlink",
105 [CCS_MAC_FILE_MKBLOCK] = "mkblock",
106 [CCS_MAC_FILE_MKCHAR] = "mkchar",
107 [CCS_MAC_FILE_LINK] = "link",
108 [CCS_MAC_FILE_RENAME] = "rename",
109 [CCS_MAC_FILE_CHMOD] = "chmod",
110 [CCS_MAC_FILE_CHOWN] = "chown",
111 [CCS_MAC_FILE_CHGRP] = "chgrp",
112 [CCS_MAC_FILE_IOCTL] = "ioctl",
113 [CCS_MAC_FILE_CHROOT] = "chroot",
114 [CCS_MAC_FILE_MOUNT] = "mount",
115 [CCS_MAC_FILE_UMOUNT] = "unmount",
116 [CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root",
117 /* CONFIG::misc group */
118 [CCS_MAC_ENVIRON] = "env",
119 /* CONFIG::network group */
120 [CCS_MAC_NETWORK_INET_STREAM_BIND] = "inet_stream_bind",
121 [CCS_MAC_NETWORK_INET_STREAM_LISTEN] = "inet_stream_listen",
122 [CCS_MAC_NETWORK_INET_STREAM_CONNECT] = "inet_stream_connect",
123 [CCS_MAC_NETWORK_INET_STREAM_ACCEPT] = "inet_stream_accept",
124 [CCS_MAC_NETWORK_INET_DGRAM_BIND] = "inet_dgram_bind",
125 [CCS_MAC_NETWORK_INET_DGRAM_SEND] = "inet_dgram_send",
126 [CCS_MAC_NETWORK_INET_DGRAM_RECV] = "inet_dgram_recv",
127 [CCS_MAC_NETWORK_INET_RAW_BIND] = "inet_raw_bind",
128 [CCS_MAC_NETWORK_INET_RAW_SEND] = "inet_raw_send",
129 [CCS_MAC_NETWORK_INET_RAW_RECV] = "inet_raw_recv",
130 [CCS_MAC_NETWORK_UNIX_STREAM_BIND] = "unix_stream_bind",
131 [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN] = "unix_stream_listen",
132 [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT] = "unix_stream_connect",
133 [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT] = "unix_stream_accept",
134 [CCS_MAC_NETWORK_UNIX_DGRAM_BIND] = "unix_dgram_bind",
135 [CCS_MAC_NETWORK_UNIX_DGRAM_SEND] = "unix_dgram_send",
136 [CCS_MAC_NETWORK_UNIX_DGRAM_RECV] = "unix_dgram_recv",
137 [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND] = "unix_seqpacket_bind",
138 [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = "unix_seqpacket_listen",
139 [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
140 [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT] = "unix_seqpacket_accept",
141 /* CONFIG::ipc group */
142 [CCS_MAC_SIGNAL] = "signal",
143 /* CONFIG::capability group */
144 [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET] = "use_route",
145 [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet",
146 [CCS_MAC_CAPABILITY_SYS_REBOOT] = "SYS_REBOOT",
147 [CCS_MAC_CAPABILITY_SYS_VHANGUP] = "SYS_VHANGUP",
148 [CCS_MAC_CAPABILITY_SYS_SETTIME] = "SYS_TIME",
149 [CCS_MAC_CAPABILITY_SYS_NICE] = "SYS_NICE",
150 [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME] = "SYS_SETHOSTNAME",
151 [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module",
152 [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD",
153 [CCS_MAC_CAPABILITY_SYS_PTRACE] = "SYS_PTRACE",
154 /* CONFIG group */
155 [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE] = "file",
156 [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK] = "network",
157 [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC] = "misc",
158 [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC] = "ipc",
159 [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
160 };
161
162 /* String table for path operation. */
163 const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
164 [CCS_TYPE_EXECUTE] = "execute",
165 [CCS_TYPE_READ] = "read",
166 [CCS_TYPE_WRITE] = "write",
167 [CCS_TYPE_APPEND] = "append",
168 [CCS_TYPE_UNLINK] = "unlink",
169 [CCS_TYPE_GETATTR] = "getattr",
170 [CCS_TYPE_RMDIR] = "rmdir",
171 [CCS_TYPE_TRUNCATE] = "truncate",
172 [CCS_TYPE_SYMLINK] = "symlink",
173 [CCS_TYPE_CHROOT] = "chroot",
174 [CCS_TYPE_UMOUNT] = "unmount",
175 };
176
177 /* String table for categories. */
178 static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
179 [CCS_MAC_CATEGORY_FILE] = "file",
180 [CCS_MAC_CATEGORY_NETWORK] = "network",
181 [CCS_MAC_CATEGORY_MISC] = "misc",
182 [CCS_MAC_CATEGORY_IPC] = "ipc",
183 [CCS_MAC_CATEGORY_CAPABILITY] = "capability",
184 };
185
186 /* String table for conditions. */
187 const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
188 [CCS_TASK_UID] = "task.uid",
189 [CCS_TASK_EUID] = "task.euid",
190 [CCS_TASK_SUID] = "task.suid",
191 [CCS_TASK_FSUID] = "task.fsuid",
192 [CCS_TASK_GID] = "task.gid",
193 [CCS_TASK_EGID] = "task.egid",
194 [CCS_TASK_SGID] = "task.sgid",
195 [CCS_TASK_FSGID] = "task.fsgid",
196 [CCS_TASK_PID] = "task.pid",
197 [CCS_TASK_PPID] = "task.ppid",
198 [CCS_EXEC_ARGC] = "exec.argc",
199 [CCS_EXEC_ENVC] = "exec.envc",
200 [CCS_TYPE_IS_SOCKET] = "socket",
201 [CCS_TYPE_IS_SYMLINK] = "symlink",
202 [CCS_TYPE_IS_FILE] = "file",
203 [CCS_TYPE_IS_BLOCK_DEV] = "block",
204 [CCS_TYPE_IS_DIRECTORY] = "directory",
205 [CCS_TYPE_IS_CHAR_DEV] = "char",
206 [CCS_TYPE_IS_FIFO] = "fifo",
207 [CCS_MODE_SETUID] = "setuid",
208 [CCS_MODE_SETGID] = "setgid",
209 [CCS_MODE_STICKY] = "sticky",
210 [CCS_MODE_OWNER_READ] = "owner_read",
211 [CCS_MODE_OWNER_WRITE] = "owner_write",
212 [CCS_MODE_OWNER_EXECUTE] = "owner_execute",
213 [CCS_MODE_GROUP_READ] = "group_read",
214 [CCS_MODE_GROUP_WRITE] = "group_write",
215 [CCS_MODE_GROUP_EXECUTE] = "group_execute",
216 [CCS_MODE_OTHERS_READ] = "others_read",
217 [CCS_MODE_OTHERS_WRITE] = "others_write",
218 [CCS_MODE_OTHERS_EXECUTE] = "others_execute",
219 [CCS_TASK_TYPE] = "task.type",
220 [CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
221 [CCS_EXEC_REALPATH] = "exec.realpath",
222 [CCS_SYMLINK_TARGET] = "symlink.target",
223 [CCS_PATH1_UID] = "path1.uid",
224 [CCS_PATH1_GID] = "path1.gid",
225 [CCS_PATH1_INO] = "path1.ino",
226 [CCS_PATH1_MAJOR] = "path1.major",
227 [CCS_PATH1_MINOR] = "path1.minor",
228 [CCS_PATH1_PERM] = "path1.perm",
229 [CCS_PATH1_TYPE] = "path1.type",
230 [CCS_PATH1_DEV_MAJOR] = "path1.dev_major",
231 [CCS_PATH1_DEV_MINOR] = "path1.dev_minor",
232 [CCS_PATH2_UID] = "path2.uid",
233 [CCS_PATH2_GID] = "path2.gid",
234 [CCS_PATH2_INO] = "path2.ino",
235 [CCS_PATH2_MAJOR] = "path2.major",
236 [CCS_PATH2_MINOR] = "path2.minor",
237 [CCS_PATH2_PERM] = "path2.perm",
238 [CCS_PATH2_TYPE] = "path2.type",
239 [CCS_PATH2_DEV_MAJOR] = "path2.dev_major",
240 [CCS_PATH2_DEV_MINOR] = "path2.dev_minor",
241 [CCS_PATH1_PARENT_UID] = "path1.parent.uid",
242 [CCS_PATH1_PARENT_GID] = "path1.parent.gid",
243 [CCS_PATH1_PARENT_INO] = "path1.parent.ino",
244 [CCS_PATH1_PARENT_PERM] = "path1.parent.perm",
245 [CCS_PATH2_PARENT_UID] = "path2.parent.uid",
246 [CCS_PATH2_PARENT_GID] = "path2.parent.gid",
247 [CCS_PATH2_PARENT_INO] = "path2.parent.ino",
248 [CCS_PATH2_PARENT_PERM] = "path2.parent.perm",
249 };
250
251 /* String table for PREFERENCE keyword. */
252 static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
253 [CCS_PREF_MAX_AUDIT_LOG] = "max_audit_log",
254 [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
255 [CCS_PREF_ENFORCING_PENALTY] = "enforcing_penalty",
256 };
257
258 /* Permit policy management by non-root user? */
259 static bool ccs_manage_by_non_root;
260
261 /**
262 * ccs_yesno - Return "yes" or "no".
263 *
264 * @value: Bool value.
265 *
266 * Returns "yes" if @value is not 0, "no" otherwise.
267 */
268 const char *ccs_yesno(const unsigned int value)
269 {
270 return value ? "yes" : "no";
271 }
272
273 /* Prototype for ccs_addprintf(). */
274 static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
275 __attribute__ ((format(printf, 3, 4)));
276
277 /**
278 * ccs_addprintf - strncat()-like-snprintf().
279 *
280 * @buffer: Buffer to write to. Must be '\0'-terminated.
281 * @len: Size of @buffer.
282 * @fmt: The printf()'s format string, followed by parameters.
283 *
284 * Returns nothing.
285 */
286 static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
287 {
288 va_list args;
289 const int pos = strlen(buffer);
290 va_start(args, fmt);
291 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
292 va_end(args);
293 }
294
295 /**
296 * ccs_flush - Flush queued string to userspace's buffer.
297 *
298 * @head: Pointer to "struct ccs_io_buffer".
299 *
300 * Returns true if all data was flushed, false otherwise.
301 */
302 static bool ccs_flush(struct ccs_io_buffer *head)
303 {
304 while (head->r.w_pos) {
305 const char *w = head->r.w[0];
306 size_t len = strlen(w);
307 if (len) {
308 if (len > head->read_user_buf_avail)
309 len = head->read_user_buf_avail;
310 if (!len)
311 return false;
312 if (copy_to_user(head->read_user_buf, w, len))
313 return false;
314 head->read_user_buf_avail -= len;
315 head->read_user_buf += len;
316 w += len;
317 }
318 head->r.w[0] = w;
319 if (*w)
320 return false;
321 /* Add '\0' for audit logs and query. */
322 if (head->poll) {
323 if (!head->read_user_buf_avail ||
324 copy_to_user(head->read_user_buf, "", 1))
325 return false;
326 head->read_user_buf_avail--;
327 head->read_user_buf++;
328 }
329 head->r.w_pos--;
330 for (len = 0; len < head->r.w_pos; len++)
331 head->r.w[len] = head->r.w[len + 1];
332 }
333 head->r.avail = 0;
334 return true;
335 }
336
337 /**
338 * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
339 *
340 * @head: Pointer to "struct ccs_io_buffer".
341 * @string: String to print.
342 *
343 * Returns nothing.
344 *
345 * Note that @string has to be kept valid until @head is kfree()d.
346 * This means that char[] allocated on stack memory cannot be passed to
347 * this function. Use ccs_io_printf() for char[] allocated on stack memory.
348 */
349 static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
350 {
351 if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
352 head->r.w[head->r.w_pos++] = string;
353 ccs_flush(head);
354 } else
355 printk(KERN_WARNING "Too many words in a line.\n");
356 }
357
358 /* Prototype for ccs_io_printf(). */
359 static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
360 __attribute__ ((format(printf, 2, 3)));
361
362 /**
363 * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
364 *
365 * @head: Pointer to "struct ccs_io_buffer".
366 * @fmt: The printf()'s format string, followed by parameters.
367 *
368 * Returns nothing.
369 */
370 static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
371 {
372 va_list args;
373 size_t len;
374 size_t pos = head->r.avail;
375 int size = head->readbuf_size - pos;
376 if (size <= 0)
377 return;
378 va_start(args, fmt);
379 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
380 va_end(args);
381 if (pos + len >= head->readbuf_size) {
382 printk(KERN_WARNING "Too many words in a line.\n");
383 return;
384 }
385 head->r.avail += len;
386 ccs_set_string(head, head->read_buf + pos);
387 }
388
389 /**
390 * ccs_set_space - Put a space to "struct ccs_io_buffer" structure.
391 *
392 * @head: Pointer to "struct ccs_io_buffer".
393 *
394 * Returns nothing.
395 */
396 static void ccs_set_space(struct ccs_io_buffer *head)
397 {
398 ccs_set_string(head, " ");
399 }
400
401 /**
402 * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure.
403 *
404 * @head: Pointer to "struct ccs_io_buffer".
405 *
406 * Returns nothing.
407 */
408 static bool ccs_set_lf(struct ccs_io_buffer *head)
409 {
410 ccs_set_string(head, "\n");
411 return !head->r.w_pos;
412 }
413
414 /**
415 * ccs_set_slash - Put a shash to "struct ccs_io_buffer" structure.
416 *
417 * @head: Pointer to "struct ccs_io_buffer".
418 *
419 * Returns nothing.
420 */
421 static void ccs_set_slash(struct ccs_io_buffer *head)
422 {
423 ccs_set_string(head, "/");
424 }
425
426 /* List of namespaces. */
427 LIST_HEAD(ccs_namespace_list);
428 /* True if namespace other than ccs_kernel_namespace is defined. */
429 static bool ccs_namespace_enabled;
430
431 /**
432 * ccs_init_policy_namespace - Initialize namespace.
433 *
434 * @ns: Pointer to "struct ccs_policy_namespace".
435 *
436 * Returns nothing.
437 */
438 void ccs_init_policy_namespace(struct ccs_policy_namespace *ns)
439 {
440 unsigned int idx;
441 for (idx = 0; idx < CCS_MAX_ACL_GROUPS; idx++) {
442 INIT_LIST_HEAD(&ns->acl_group[idx][0]);
443 INIT_LIST_HEAD(&ns->acl_group[idx][1]);
444 }
445 for (idx = 0; idx < CCS_MAX_GROUP; idx++)
446 INIT_LIST_HEAD(&ns->group_list[idx]);
447 for (idx = 0; idx < CCS_MAX_POLICY; idx++)
448 INIT_LIST_HEAD(&ns->policy_list[idx]);
449 ns->profile_version = 20100903;
450 ccs_namespace_enabled = !list_empty(&ccs_namespace_list);
451 list_add_tail_rcu(&ns->namespace_list, &ccs_namespace_list);
452 }
453
454 /**
455 * ccs_print_namespace - Print namespace header.
456 *
457 * @head: Pointer to "struct ccs_io_buffer".
458 *
459 * Returns nothing.
460 */
461 static void ccs_print_namespace(struct ccs_io_buffer *head)
462 {
463 if (!ccs_namespace_enabled)
464 return;
465 ccs_set_string(head,
466 container_of(head->r.ns, struct ccs_policy_namespace,
467 namespace_list)->name);
468 ccs_set_space(head);
469 }
470
471 /**
472 * ccs_assign_profile - Create a new profile.
473 *
474 * @ns: Pointer to "struct ccs_policy_namespace".
475 * @profile: Profile number to create.
476 *
477 * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
478 */
479 static struct ccs_profile *ccs_assign_profile(struct ccs_policy_namespace *ns,
480 const unsigned int profile)
481 {
482 struct ccs_profile *ptr;
483 struct ccs_profile *entry;
484 if (profile >= CCS_MAX_PROFILES)
485 return NULL;
486 ptr = ns->profile_ptr[profile];
487 if (ptr)
488 return ptr;
489 entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
490 if (mutex_lock_interruptible(&ccs_policy_lock))
491 goto out;
492 ptr = ns->profile_ptr[profile];
493 if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
494 ptr = entry;
495 ptr->default_config = CCS_CONFIG_DISABLED |
496 CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
497 memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
498 sizeof(ptr->config));
499 ptr->pref[CCS_PREF_MAX_AUDIT_LOG] =
500 CONFIG_CCSECURITY_MAX_AUDIT_LOG;
501 ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] =
502 CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY;
503 mb(); /* Avoid out-of-order execution. */
504 ns->profile_ptr[profile] = ptr;
505 entry = NULL;
506 }
507 mutex_unlock(&ccs_policy_lock);
508 out:
509 kfree(entry);
510 return ptr;
511 }
512
513 /**
514 * ccs_check_profile - Check all profiles currently assigned to domains are defined.
515 *
516 * Returns nothing.
517 */
518 static void ccs_check_profile(void)
519 {
520 struct ccs_domain_info *domain;
521 const int idx = ccs_read_lock();
522 ccs_policy_loaded = true;
523 printk(KERN_INFO "CCSecurity: 1.8.2-pre 2011/06/01\n");
524 list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
525 const u8 profile = domain->profile;
526 const struct ccs_policy_namespace *ns = domain->ns;
527 if (ns->profile_version != 20100903)
528 printk(KERN_ERR
529 "Profile version %u is not supported.\n",
530 ns->profile_version);
531 else if (!ns->profile_ptr[profile])
532 printk(KERN_ERR
533 "Profile %u (used by '%s') is not defined.\n",
534 profile, domain->domainname->name);
535 else
536 continue;
537 printk(KERN_ERR
538 "Userland tools for TOMOYO 1.8 must be installed and "
539 "policy must be initialized.\n");
540 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
541 "for more information.\n");
542 panic("STOP!");
543 }
544 ccs_read_unlock(idx);
545 printk(KERN_INFO "Mandatory Access Control activated.\n");
546 }
547
548 /**
549 * ccs_profile - Find a profile.
550 *
551 * @profile: Profile number to find.
552 *
553 * Returns pointer to "struct ccs_profile".
554 */
555 struct ccs_profile *ccs_profile(const u8 profile)
556 {
557 static struct ccs_profile ccs_null_profile;
558 struct ccs_profile *ptr = ccs_current_namespace()->
559 profile_ptr[profile];
560 if (!ptr)
561 ptr = &ccs_null_profile;
562 return ptr;
563 }
564
565 /**
566 * ccs_find_yesno - Find values for specified keyword.
567 *
568 * @string: String to check.
569 * @find: Name of keyword.
570 *
571 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
572 */
573 static s8 ccs_find_yesno(const char *string, const char *find)
574 {
575 const char *cp = strstr(string, find);
576 if (cp) {
577 cp += strlen(find);
578 if (!strncmp(cp, "=yes", 4))
579 return 1;
580 else if (!strncmp(cp, "=no", 3))
581 return 0;
582 }
583 return -1;
584 }
585
586 /**
587 * ccs_set_uint - Set value for specified preference.
588 *
589 * @i: Pointer to "unsigned int".
590 * @string: String to check.
591 * @find: Name of keyword.
592 *
593 * Returns nothing.
594 */
595 static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
596 {
597 const char *cp = strstr(string, find);
598 if (cp)
599 sscanf(cp + strlen(find), "=%u", i);
600 }
601
602 /**
603 * ccs_set_mode - Set mode for specified profile.
604 *
605 * @name: Name of functionality.
606 * @value: Mode for @name.
607 * @profile: Pointer to "struct ccs_profile".
608 *
609 * Returns 0 on success, negative value otherwise.
610 */
611 static int ccs_set_mode(char *name, const char *value,
612 struct ccs_profile *profile)
613 {
614 u8 i;
615 u8 config;
616 if (!strcmp(name, "CONFIG")) {
617 i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
618 config = profile->default_config;
619 } else if (ccs_str_starts(&name, "CONFIG::")) {
620 config = 0;
621 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
622 i++) {
623 int len = 0;
624 if (i < CCS_MAX_MAC_INDEX) {
625 const u8 c = ccs_index2category[i];
626 const char *category =
627 ccs_category_keywords[c];
628 len = strlen(category);
629 if (strncmp(name, category, len) ||
630 name[len++] != ':' || name[len++] != ':')
631 continue;
632 }
633 if (strcmp(name + len, ccs_mac_keywords[i]))
634 continue;
635 config = profile->config[i];
636 break;
637 }
638 if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
639 return -EINVAL;
640 } else {
641 return -EINVAL;
642 }
643 if (strstr(value, "use_default")) {
644 config = CCS_CONFIG_USE_DEFAULT;
645 } else {
646 u8 mode;
647 for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
648 if (strstr(value, ccs_mode[mode]))
649 /*
650 * Update lower 3 bits in order to distinguish
651 * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
652 */
653 config = (config & ~7) | mode;
654 if (config != CCS_CONFIG_USE_DEFAULT) {
655 switch (ccs_find_yesno(value, "grant_log")) {
656 case 1:
657 config |= CCS_CONFIG_WANT_GRANT_LOG;
658 break;
659 case 0:
660 config &= ~CCS_CONFIG_WANT_GRANT_LOG;
661 break;
662 }
663 switch (ccs_find_yesno(value, "reject_log")) {
664 case 1:
665 config |= CCS_CONFIG_WANT_REJECT_LOG;
666 break;
667 case 0:
668 config &= ~CCS_CONFIG_WANT_REJECT_LOG;
669 break;
670 }
671 }
672 }
673 if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
674 profile->config[i] = config;
675 else if (config != CCS_CONFIG_USE_DEFAULT)
676 profile->default_config = config;
677 return 0;
678 }
679
680 /**
681 * ccs_write_profile - Write profile table.
682 *
683 * @head: Pointer to "struct ccs_io_buffer".
684 *
685 * Returns 0 on success, negative value otherwise.
686 */
687 static int ccs_write_profile(struct ccs_io_buffer *head)
688 {
689 char *data = head->write_buf;
690 unsigned int i;
691 char *cp;
692 struct ccs_profile *profile;
693 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
694 == 1)
695 return 0;
696 i = simple_strtoul(data, &cp, 10);
697 if (*cp != '-')
698 return -EINVAL;
699 data = cp + 1;
700 profile = ccs_assign_profile(head->w.ns, i);
701 if (!profile)
702 return -EINVAL;
703 cp = strchr(data, '=');
704 if (!cp)
705 return -EINVAL;
706 *cp++ = '\0';
707 if (!strcmp(data, "COMMENT")) {
708 static DEFINE_SPINLOCK(lock);
709 const struct ccs_path_info *new_comment = ccs_get_name(cp);
710 const struct ccs_path_info *old_comment;
711 if (!new_comment)
712 return -ENOMEM;
713 spin_lock(&lock);
714 old_comment = profile->comment;
715 profile->comment = new_comment;
716 spin_unlock(&lock);
717 ccs_put_name(old_comment);
718 return 0;
719 }
720 if (!strcmp(data, "PREFERENCE")) {
721 for (i = 0; i < CCS_MAX_PREF; i++)
722 ccs_set_uint(&profile->pref[i], cp,
723 ccs_pref_keywords[i]);
724 return 0;
725 }
726 return ccs_set_mode(data, cp, profile);
727 }
728
729 /**
730 * ccs_print_config - Print mode for specified functionality.
731 *
732 * @head: Pointer to "struct ccs_io_buffer".
733 * @config: Mode for that functionality.
734 *
735 * Returns nothing.
736 *
737 * Caller prints functionality's name.
738 */
739 static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
740 {
741 ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
742 ccs_mode[config & 3],
743 ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
744 ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
745 }
746
747 /**
748 * ccs_read_profile - Read profile table.
749 *
750 * @head: Pointer to "struct ccs_io_buffer".
751 *
752 * Returns nothing.
753 */
754 static void ccs_read_profile(struct ccs_io_buffer *head)
755 {
756 u8 index;
757 struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
758 namespace_list);
759 const struct ccs_profile *profile;
760 if (head->r.eof)
761 return;
762 next:
763 index = head->r.index;
764 profile = ns->profile_ptr[index];
765 switch (head->r.step) {
766 case 0:
767 ccs_print_namespace(head);
768 ccs_io_printf(head, "PROFILE_VERSION=%u\n", 20100903);
769 head->r.step++;
770 break;
771 case 1:
772 for ( ; head->r.index < CCS_MAX_PROFILES; head->r.index++)
773 if (ns->profile_ptr[head->r.index])
774 break;
775 if (head->r.index == CCS_MAX_PROFILES) {
776 head->r.eof = true;
777 return;
778 }
779 head->r.step++;
780 break;
781 case 2:
782 {
783 u8 i;
784 const struct ccs_path_info *comment = profile->comment;
785 ccs_print_namespace(head);
786 ccs_io_printf(head, "%u-COMMENT=", index);
787 ccs_set_string(head, comment ? comment->name : "");
788 ccs_set_lf(head);
789 ccs_print_namespace(head);
790 ccs_io_printf(head, "%u-PREFERENCE={ ", index);
791 for (i = 0; i < CCS_MAX_PREF; i++)
792 ccs_io_printf(head, "%s=%u ",
793 ccs_pref_keywords[i],
794 profile->pref[i]);
795 ccs_set_string(head, "}\n");
796 head->r.step++;
797 }
798 break;
799 case 3:
800 {
801 ccs_print_namespace(head);
802 ccs_io_printf(head, "%u-%s", index, "CONFIG");
803 ccs_print_config(head, profile->default_config);
804 head->r.bit = 0;
805 head->r.step++;
806 }
807 break;
808 case 4:
809 for ( ; head->r.bit < CCS_MAX_MAC_INDEX
810 + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
811 const u8 i = head->r.bit;
812 const u8 config = profile->config[i];
813 if (config == CCS_CONFIG_USE_DEFAULT)
814 continue;
815 ccs_print_namespace(head);
816 if (i < CCS_MAX_MAC_INDEX)
817 ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
818 ccs_category_keywords
819 [ccs_index2category[i]],
820 ccs_mac_keywords[i]);
821 else
822 ccs_io_printf(head, "%u-CONFIG::%s", index,
823 ccs_mac_keywords[i]);
824 ccs_print_config(head, config);
825 head->r.bit++;
826 break;
827 }
828 if (head->r.bit == CCS_MAX_MAC_INDEX
829 + CCS_MAX_MAC_CATEGORY_INDEX) {
830 head->r.index++;
831 head->r.step = 1;
832 }
833 break;
834 }
835 if (ccs_flush(head))
836 goto next;
837 }
838
839 /**
840 * ccs_same_manager - Check for duplicated "struct ccs_manager" entry.
841 *
842 * @a: Pointer to "struct ccs_acl_head".
843 * @b: Pointer to "struct ccs_acl_head".
844 *
845 * Returns true if @a == @b, false otherwise.
846 */
847 static bool ccs_same_manager(const struct ccs_acl_head *a,
848 const struct ccs_acl_head *b)
849 {
850 return container_of(a, struct ccs_manager, head)->manager
851 == container_of(b, struct ccs_manager, head)->manager;
852 }
853
854 /**
855 * ccs_update_manager_entry - Add a manager entry.
856 *
857 * @manager: The path to manager or the domainnamme.
858 * @is_delete: True if it is a delete request.
859 *
860 * Returns 0 on success, negative value otherwise.
861 */
862 static inline int ccs_update_manager_entry(const char *manager,
863 const bool is_delete)
864 {
865 struct ccs_manager e = { };
866 struct ccs_acl_param param = {
867 /* .ns = &ccs_kernel_namespace, */
868 .is_delete = is_delete,
869 .list = &ccs_kernel_namespace.policy_list[CCS_ID_MANAGER],
870 };
871 int error = is_delete ? -ENOENT : -ENOMEM;
872 if (ccs_domain_def(manager)) {
873 if (!ccs_correct_domain(manager))
874 return -EINVAL;
875 e.is_domain = true;
876 } else {
877 if (!ccs_correct_path(manager))
878 return -EINVAL;
879 }
880 e.manager = ccs_get_name(manager);
881 if (e.manager) {
882 error = ccs_update_policy(&e.head, sizeof(e), &param,
883 ccs_same_manager);
884 ccs_put_name(e.manager);
885 }
886 return error;
887 }
888
889 /**
890 * ccs_write_manager - Write manager policy.
891 *
892 * @head: Pointer to "struct ccs_io_buffer".
893 *
894 * Returns 0 on success, negative value otherwise.
895 */
896 static int ccs_write_manager(struct ccs_io_buffer *head)
897 {
898 const char *data = head->write_buf;
899 if (!strcmp(data, "manage_by_non_root")) {
900 ccs_manage_by_non_root = !head->w.is_delete;
901 return 0;
902 }
903 return ccs_update_manager_entry(data, head->w.is_delete);
904 }
905
906 /**
907 * ccs_read_manager - Read manager policy.
908 *
909 * @head: Pointer to "struct ccs_io_buffer".
910 *
911 * Returns nothing.
912 *
913 * Caller holds ccs_read_lock().
914 */
915 static void ccs_read_manager(struct ccs_io_buffer *head)
916 {
917 if (head->r.eof)
918 return;
919 list_for_each_cookie(head->r.acl, &ccs_kernel_namespace.
920 policy_list[CCS_ID_MANAGER]) {
921 struct ccs_manager *ptr =
922 list_entry(head->r.acl, typeof(*ptr), head.list);
923 if (ptr->head.is_deleted)
924 continue;
925 if (!ccs_flush(head))
926 return;
927 ccs_set_string(head, ptr->manager->name);
928 ccs_set_lf(head);
929 }
930 head->r.eof = true;
931 }
932
933 /**
934 * ccs_manager - Check whether the current process is a policy manager.
935 *
936 * Returns true if the current process is permitted to modify policy
937 * via /proc/ccs/ interface.
938 *
939 * Caller holds ccs_read_lock().
940 */
941 static bool ccs_manager(void)
942 {
943 struct ccs_manager *ptr;
944 const char *exe;
945 struct ccs_security *task = ccs_current_security();
946 const struct ccs_path_info *domainname
947 = ccs_current_domain()->domainname;
948 bool found = false;
949 if (!ccs_policy_loaded)
950 return true;
951 if (task->ccs_flags & CCS_TASK_IS_MANAGER)
952 return true;
953 if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
954 return false;
955 exe = ccs_get_exe();
956 list_for_each_entry_srcu(ptr, &ccs_kernel_namespace.
957 policy_list[CCS_ID_MANAGER], head.list,
958 &ccs_ss) {
959 if (ptr->head.is_deleted)
960 continue;
961 if (ptr->is_domain) {
962 if (ccs_pathcmp(domainname, ptr->manager))
963 continue;
964 } else {
965 if (!exe || strcmp(exe, ptr->manager->name))
966 continue;
967 }
968 /* Set manager flag. */
969 task->ccs_flags |= CCS_TASK_IS_MANAGER;
970 found = true;
971 break;
972 }
973 if (!found) { /* Reduce error messages. */
974 static pid_t ccs_last_pid;
975 const pid_t pid = current->pid;
976 if (ccs_last_pid != pid) {
977 printk(KERN_WARNING "%s ( %s ) is not permitted to "
978 "update policies.\n", domainname->name, exe);
979 ccs_last_pid = pid;
980 }
981 }
982 kfree(exe);
983 return found;
984 }
985
986 /**
987 * ccs_select_domain - Parse select command.
988 *
989 * @head: Pointer to "struct ccs_io_buffer".
990 * @data: String to parse.
991 *
992 * Returns true on success, false otherwise.
993 *
994 * Caller holds ccs_read_lock().
995 */
996 static bool ccs_select_domain(struct ccs_io_buffer *head, const char *data)
997 {
998 unsigned int pid;
999 struct ccs_domain_info *domain = NULL;
1000 bool global_pid = false;
1001 if (strncmp(data, "select ", 7))
1002 return false;
1003 data += 7;
1004 if (sscanf(data, "pid=%u", &pid) == 1 ||
1005 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
1006 struct task_struct *p;
1007 ccs_tasklist_lock();
1008 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1009 if (global_pid)
1010 p = ccsecurity_exports.find_task_by_pid_ns(pid,
1011 &init_pid_ns);
1012 else
1013 p = ccsecurity_exports.find_task_by_vpid(pid);
1014 #else
1015 p = find_task_by_pid(pid);
1016 #endif
1017 if (p)
1018 domain = ccs_task_domain(p);
1019 ccs_tasklist_unlock();
1020 } else if (!strncmp(data, "domain=", 7)) {
1021 if (*(data + 7) == '<')
1022 domain = ccs_find_domain(data + 7);
1023 } else
1024 return false;
1025 head->w.domain = domain;
1026 /* Accessing read_buf is safe because head->io_sem is held. */
1027 if (!head->read_buf)
1028 return true; /* Do nothing if open(O_WRONLY). */
1029 memset(&head->r, 0, sizeof(head->r));
1030 head->r.print_this_domain_only = true;
1031 if (domain)
1032 head->r.domain = &domain->list;
1033 else
1034 head->r.eof = true;
1035 ccs_io_printf(head, "# select %s\n", data);
1036 if (domain && domain->is_deleted)
1037 ccs_set_string(head, "# This is a deleted domain.\n");
1038 return true;
1039 }
1040
1041 /**
1042 * ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry.
1043 *
1044 * @a: Pointer to "struct ccs_acl_info".
1045 * @b: Pointer to "struct ccs_acl_info".
1046 *
1047 * Returns true if @a == @b, false otherwise.
1048 */
1049 static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
1050 const struct ccs_acl_info *b)
1051 {
1052 const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
1053 const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
1054 return p1->handler == p2->handler;
1055 }
1056
1057 /**
1058 * ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry.
1059 *
1060 * @a: Pointer to "struct ccs_acl_info".
1061 * @b: Pointer to "struct ccs_acl_info".
1062 *
1063 * Returns true if @a == @b, false otherwise.
1064 */
1065 static bool ccs_same_task_acl(const struct ccs_acl_info *a,
1066 const struct ccs_acl_info *b)
1067 {
1068 const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
1069 const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
1070 return p1->domainname == p2->domainname;
1071 }
1072
1073 /**
1074 * ccs_write_task - Update task related list.
1075 *
1076 * @param: Pointer to "struct ccs_acl_param".
1077 *
1078 * Returns 0 on success, negative value otherwise.
1079 *
1080 * Caller holds ccs_read_lock().
1081 */
1082 static int ccs_write_task(struct ccs_acl_param *param)
1083 {
1084 int error;
1085 const bool is_auto = ccs_str_starts(&param->data,
1086 "auto_domain_transition ");
1087 if (!is_auto && !ccs_str_starts(&param->data,
1088 "manual_domain_transition ")) {
1089 struct ccs_handler_acl e = { };
1090 char *handler;
1091 if (ccs_str_starts(&param->data, "auto_execute_handler "))
1092 e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
1093 else if (ccs_str_starts(&param->data,
1094 "denied_execute_handler "))
1095 e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
1096 else
1097 return -EINVAL;
1098 handler = ccs_read_token(param);
1099 if (!ccs_correct_path(handler))
1100 return -EINVAL;
1101 e.handler = ccs_get_name(handler);
1102 if (!e.handler)
1103 return -ENOMEM;
1104 if (e.handler->is_patterned)
1105 error = -EINVAL; /* No patterns allowed. */
1106 else
1107 error = ccs_update_domain(&e.head, sizeof(e), param,
1108 ccs_same_handler_acl, NULL);
1109 ccs_put_name(e.handler);
1110 } else {
1111 struct ccs_task_acl e = {
1112 .head.type = is_auto ?
1113 CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
1114 .domainname = ccs_get_domainname(param),
1115 };
1116 if (!e.domainname)
1117 error = -EINVAL;
1118 else
1119 error = ccs_update_domain(&e.head, sizeof(e), param,
1120 ccs_same_task_acl, NULL);
1121 ccs_put_name(e.domainname);
1122 }
1123 return error;
1124 }
1125
1126 /**
1127 * ccs_write_domain2 - Write domain policy.
1128 *
1129 * @ns: Pointer to "struct ccs_policy_namespace".
1130 * @list: Pointer to "struct list_head [2]".
1131 * @data: Policy to be interpreted.
1132 * @is_delete: True if it is a delete request.
1133 *
1134 * Returns 0 on success, negative value otherwise.
1135 *
1136 * Caller holds ccs_read_lock().
1137 */
1138 static int ccs_write_domain2(struct ccs_policy_namespace *ns,
1139 struct list_head list[2], char *data,
1140 const bool is_delete)
1141 {
1142 struct ccs_acl_param param = {
1143 .ns = ns,
1144 .list = list,
1145 .data = data,
1146 .is_delete = is_delete,
1147 };
1148 static const struct {
1149 const char *keyword;
1150 int (*write) (struct ccs_acl_param *);
1151 } ccs_callback[7] = {
1152 { "file ", ccs_write_file },
1153 { "network inet ", ccs_write_inet_network },
1154 { "network unix ", ccs_write_unix_network },
1155 { "misc ", ccs_write_misc },
1156 { "capability ", ccs_write_capability },
1157 { "ipc signal ", ccs_write_ipc },
1158 { "task ", ccs_write_task },
1159 };
1160 u8 i;
1161 for (i = 0; i < 7; i++) {
1162 if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
1163 continue;
1164 return ccs_callback[i].write(&param);
1165 }
1166 return -EINVAL;
1167 }
1168
1169 /**
1170 * ccs_delete_domain - Delete a domain.
1171 *
1172 * @domainname: The name of domain.
1173 *
1174 * Returns 0.
1175 */
1176 static int ccs_delete_domain(char *domainname)
1177 {
1178 struct ccs_domain_info *domain;
1179 struct ccs_path_info name;
1180 name.name = domainname;
1181 ccs_fill_path_info(&name);
1182 if (mutex_lock_interruptible(&ccs_policy_lock))
1183 return 0;
1184 /* Is there an active domain? */
1185 list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
1186 /* Never delete ccs_kernel_domain. */
1187 if (domain == &ccs_kernel_domain)
1188 continue;
1189 if (domain->is_deleted ||
1190 ccs_pathcmp(domain->domainname, &name))
1191 continue;
1192 domain->is_deleted = true;
1193 break;
1194 }
1195 mutex_unlock(&ccs_policy_lock);
1196 return 0;
1197 }
1198
1199 /* String table for domain flags. */
1200 const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1201 [CCS_DIF_QUOTA_WARNED] = "quota_exceeded\n",
1202 [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
1203 };
1204
1205 /**
1206 * ccs_write_domain - Write domain policy.
1207 *
1208 * @head: Pointer to "struct ccs_io_buffer".
1209 *
1210 * Returns 0 on success, negative value otherwise.
1211 *
1212 * Caller holds ccs_read_lock().
1213 */
1214 static int ccs_write_domain(struct ccs_io_buffer *head)
1215 {
1216 char *data = head->write_buf;
1217 struct ccs_policy_namespace *ns;
1218 struct ccs_domain_info *domain = head->w.domain;
1219 const bool is_delete = head->w.is_delete;
1220 const bool is_select = !is_delete && ccs_str_starts(&data, "select ");
1221 unsigned int profile;
1222 if (*data == '<') {
1223 domain = NULL;
1224 if (is_delete)
1225 ccs_delete_domain(data);
1226 else if (is_select)
1227 domain = ccs_find_domain(data);
1228 else
1229 domain = ccs_assign_domain(data, false);
1230 head->w.domain = domain;
1231 return 0;
1232 }
1233 if (!domain)
1234 return -EINVAL;
1235 ns = domain->ns;
1236 if (sscanf(data, "use_profile %u\n", &profile) == 1
1237 && profile < CCS_MAX_PROFILES) {
1238 if (!ccs_policy_loaded || ns->profile_ptr[(u8) profile])
1239 if (!is_delete)
1240 domain->profile = (u8) profile;
1241 return 0;
1242 }
1243 if (sscanf(data, "use_group %u\n", &profile) == 1
1244 && profile < CCS_MAX_ACL_GROUPS) {
1245 if (!is_delete)
1246 domain->group = (u8) profile;
1247 return 0;
1248 }
1249 for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1250 const char *cp = ccs_dif[profile];
1251 if (strncmp(data, cp, strlen(cp) - 1))
1252 continue;
1253 domain->flags[profile] = !is_delete;
1254 return 0;
1255 }
1256 return ccs_write_domain2(ns, domain->acl_info_list, data, is_delete);
1257 }
1258
1259 /**
1260 * ccs_print_name_union - Print a ccs_name_union.
1261 *
1262 * @head: Pointer to "struct ccs_io_buffer".
1263 * @ptr: Pointer to "struct ccs_name_union".
1264 *
1265 * Returns nothing.
1266 */
1267 static void ccs_print_name_union(struct ccs_io_buffer *head,
1268 const struct ccs_name_union *ptr)
1269 {
1270 ccs_set_space(head);
1271 if (ptr->is_group) {
1272 ccs_set_string(head, "@");
1273 ccs_set_string(head, ptr->group->group_name->name);
1274 } else {
1275 ccs_set_string(head, ptr->filename->name);
1276 }
1277 }
1278
1279 /**
1280 * ccs_print_name_union_quoted - Print a ccs_name_union with a quote.
1281 *
1282 * @head: Pointer to "struct ccs_io_buffer".
1283 * @ptr: Pointer to "struct ccs_name_union".
1284 *
1285 * Returns nothing.
1286 */
1287 static void ccs_print_name_union_quoted(struct ccs_io_buffer *head,
1288 const struct ccs_name_union *ptr)
1289 {
1290 if (ptr->is_group) {
1291 ccs_set_string(head, "@");
1292 ccs_set_string(head, ptr->group->group_name->name);
1293 } else {
1294 ccs_set_string(head, "\"");
1295 ccs_set_string(head, ptr->filename->name);
1296 ccs_set_string(head, "\"");
1297 }
1298 }
1299
1300 /**
1301 * ccs_print_number_union_nospace - Print a ccs_number_union without a space.
1302 *
1303 * @head: Pointer to "struct ccs_io_buffer".
1304 * @ptr: Pointer to "struct ccs_number_union".
1305 *
1306 * Returns nothing.
1307 */
1308 static void ccs_print_number_union_nospace(struct ccs_io_buffer *head,
1309 const struct ccs_number_union *ptr)
1310 {
1311 if (ptr->is_group) {
1312 ccs_set_string(head, "@");
1313 ccs_set_string(head, ptr->group->group_name->name);
1314 } else {
1315 int i;
1316 unsigned long min = ptr->values[0];
1317 const unsigned long max = ptr->values[1];
1318 u8 min_type = ptr->value_type[0];
1319 const u8 max_type = ptr->value_type[1];
1320 char buffer[128];
1321 buffer[0] = '\0';
1322 for (i = 0; i < 2; i++) {
1323 switch (min_type) {
1324 case CCS_VALUE_TYPE_HEXADECIMAL:
1325 ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1326 min);
1327 break;
1328 case CCS_VALUE_TYPE_OCTAL:
1329 ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1330 min);
1331 break;
1332 default:
1333 ccs_addprintf(buffer, sizeof(buffer), "%lu",
1334 min);
1335 break;
1336 }
1337 if (min == max && min_type == max_type)
1338 break;
1339 ccs_addprintf(buffer, sizeof(buffer), "-");
1340 min_type = max_type;
1341 min = max;
1342 }
1343 ccs_io_printf(head, "%s", buffer);
1344 }
1345 }
1346
1347 /**
1348 * ccs_print_number_union - Print a ccs_number_union.
1349 *
1350 * @head: Pointer to "struct ccs_io_buffer".
1351 * @ptr: Pointer to "struct ccs_number_union".
1352 *
1353 * Returns nothing.
1354 */
1355 static void ccs_print_number_union(struct ccs_io_buffer *head,
1356 const struct ccs_number_union *ptr)
1357 {
1358 ccs_set_space(head);
1359 ccs_print_number_union_nospace(head, ptr);
1360 }
1361
1362 /**
1363 * ccs_print_condition - Print condition part.
1364 *
1365 * @head: Pointer to "struct ccs_io_buffer".
1366 * @cond: Pointer to "struct ccs_condition".
1367 *
1368 * Returns true on success, false otherwise.
1369 */
1370 static bool ccs_print_condition(struct ccs_io_buffer *head,
1371 const struct ccs_condition *cond)
1372 {
1373 switch (head->r.cond_step) {
1374 case 0:
1375 head->r.cond_index = 0;
1376 head->r.cond_step++;
1377 /* fall through */
1378 case 1:
1379 {
1380 const u16 condc = cond->condc;
1381 const struct ccs_condition_element *condp =
1382 (typeof(condp)) (cond + 1);
1383 const struct ccs_number_union *numbers_p =
1384 (typeof(numbers_p)) (condp + condc);
1385 const struct ccs_name_union *names_p =
1386 (typeof(names_p))
1387 (numbers_p + cond->numbers_count);
1388 const struct ccs_argv *argv =
1389 (typeof(argv)) (names_p + cond->names_count);
1390 const struct ccs_envp *envp =
1391 (typeof(envp)) (argv + cond->argc);
1392 u16 skip;
1393 for (skip = 0; skip < head->r.cond_index; skip++) {
1394 const u8 left = condp->left;
1395 const u8 right = condp->right;
1396 condp++;
1397 switch (left) {
1398 case CCS_ARGV_ENTRY:
1399 argv++;
1400 continue;
1401 case CCS_ENVP_ENTRY:
1402 envp++;
1403 continue;
1404 case CCS_NUMBER_UNION:
1405 numbers_p++;
1406 break;
1407 }
1408 switch (right) {
1409 case CCS_NAME_UNION:
1410 names_p++;
1411 break;
1412 case CCS_NUMBER_UNION:
1413 numbers_p++;
1414 break;
1415 }
1416 }
1417 while (head->r.cond_index < condc) {
1418 const u8 match = condp->equals;
1419 const u8 left = condp->left;
1420 const u8 right = condp->right;
1421 if (!ccs_flush(head))
1422 return false;
1423 condp++;
1424 head->r.cond_index++;
1425 ccs_set_space(head);
1426 switch (left) {
1427 case CCS_ARGV_ENTRY:
1428 ccs_io_printf(head,
1429 "exec.argv[%lu]%s=\"",
1430 argv->index,
1431 argv->is_not ? "!" : "");
1432 ccs_set_string(head,
1433 argv->value->name);
1434 ccs_set_string(head, "\"");
1435 argv++;
1436 continue;
1437 case CCS_ENVP_ENTRY:
1438 ccs_set_string(head, "exec.envp[\"");
1439 ccs_set_string(head, envp->name->name);
1440 ccs_io_printf(head, "\"]%s=",
1441 envp->is_not ? "!" : "");
1442 if (envp->value) {
1443 ccs_set_string(head, "\"");
1444 ccs_set_string(head, envp->
1445 value->name);
1446 ccs_set_string(head, "\"");
1447 } else {
1448 ccs_set_string(head, "NULL");
1449 }
1450 envp++;
1451 continue;
1452 case CCS_NUMBER_UNION:
1453 ccs_print_number_union_nospace
1454 (head, numbers_p++);
1455 break;
1456 default:
1457 ccs_set_string(head,
1458 ccs_condition_keyword[left]);
1459 break;
1460 }
1461 ccs_set_string(head, match ? "=" : "!=");
1462 switch (right) {
1463 case CCS_NAME_UNION:
1464 ccs_print_name_union_quoted
1465 (head, names_p++);
1466 break;
1467 case CCS_NUMBER_UNION:
1468 ccs_print_number_union_nospace
1469 (head, numbers_p++);
1470 break;
1471 default:
1472 ccs_set_string(head,
1473 ccs_condition_keyword[right]);
1474 break;
1475 }
1476 }
1477 }
1478 head->r.cond_step++;
1479 /* fall through */
1480 case 2:
1481 if (!ccs_flush(head))
1482 break;
1483 head->r.cond_step++;
1484 /* fall through */
1485 case 3:
1486 if (cond->grant_log != CCS_GRANTLOG_AUTO)
1487 ccs_io_printf(head, " grant_log=%s",
1488 ccs_yesno(cond->grant_log ==
1489 CCS_GRANTLOG_YES));
1490 if (cond->transit) {
1491 const char *name = cond->transit->name;
1492 ccs_set_string(head, *name == '<' ?
1493 " auto_namespace_transition=\"" :
1494 " auto_domain_transition=\"");
1495 ccs_set_string(head, name);
1496 ccs_set_string(head, "\"");
1497 }
1498 ccs_set_lf(head);
1499 return true;
1500 }
1501 return false;
1502 }
1503
1504 /**
1505 * ccs_set_group - Print "acl_group " header keyword and category name.
1506 *
1507 * @head: Pointer to "struct ccs_io_buffer".
1508 * @category: Category name.
1509 *
1510 * Returns nothing.
1511 */
1512 static void ccs_set_group(struct ccs_io_buffer *head, const char *category)
1513 {
1514 if (head->type == CCS_EXCEPTIONPOLICY) {
1515 ccs_print_namespace(head);
1516 ccs_io_printf(head, "acl_group %u ", head->r.acl_group_index);
1517 }
1518 ccs_set_string(head, category);
1519 }
1520
1521 /**
1522 * ccs_print_entry - Print an ACL entry.
1523 *
1524 * @head: Pointer to "struct ccs_io_buffer".
1525 * @acl: Pointer to an ACL entry.
1526 *
1527 * Returns true on success, false otherwise.
1528 */
1529 static bool ccs_print_entry(struct ccs_io_buffer *head,
1530 const struct ccs_acl_info *acl)
1531 {
1532 const u8 acl_type = acl->type;
1533 const bool may_trigger_transition = acl->cond && acl->cond->transit;
1534 bool first = true;
1535 u8 bit;
1536 if (head->r.print_cond_part)
1537 goto print_cond_part;
1538 if (acl->is_deleted)
1539 return true;
1540 if (!ccs_flush(head))
1541 return false;
1542 else if (acl_type == CCS_TYPE_PATH_ACL) {
1543 struct ccs_path_acl *ptr
1544 = container_of(acl, typeof(*ptr), head);
1545 const u16 perm = ptr->perm;
1546 for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) {
1547 if (!(perm & (1 << bit)))
1548 continue;
1549 if (head->r.print_transition_related_only &&
1550 bit != CCS_TYPE_EXECUTE && !may_trigger_transition)
1551 continue;
1552 if (first) {
1553 ccs_set_group(head, "file ");
1554 first = false;
1555 } else {
1556 ccs_set_slash(head);
1557 }
1558 ccs_set_string(head, ccs_path_keyword[bit]);
1559 }
1560 if (first)
1561 return true;
1562 ccs_print_name_union(head, &ptr->name);
1563 } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1564 acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1565 struct ccs_handler_acl *ptr
1566 = container_of(acl, typeof(*ptr), head);
1567 ccs_set_group(head, "task ");
1568 ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1569 ? "auto_execute_handler " :
1570 "denied_execute_handler ");
1571 ccs_set_string(head, ptr->handler->name);
1572 } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1573 acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1574 struct ccs_task_acl *ptr =
1575 container_of(acl, typeof(*ptr), head);
1576 ccs_set_group(head, "task ");
1577 ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1578 "auto_domain_transition " :
1579 "manual_domain_transition ");
1580 ccs_set_string(head, ptr->domainname->name);
1581 } else if (head->r.print_transition_related_only &&
1582 !may_trigger_transition) {
1583 return true;
1584 } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1585 struct ccs_mkdev_acl *ptr =
1586 container_of(acl, typeof(*ptr), head);
1587 const u8 perm = ptr->perm;
1588 for (bit = 0; bit < CCS_MAX_MKDEV_OPERATION; bit++) {
1589 if (!(perm & (1 << bit)))
1590 continue;
1591 if (first) {
1592 ccs_set_group(head, "file ");
1593 first = false;
1594 } else {
1595 ccs_set_slash(head);
1596 }
1597 ccs_set_string(head, ccs_mac_keywords
1598 [ccs_pnnn2mac[bit]]);
1599 }
1600 if (first)
1601 return true;
1602 ccs_print_name_union(head, &ptr->name);
1603 ccs_print_number_union(head, &ptr->mode);
1604 ccs_print_number_union(head, &ptr->major);
1605 ccs_print_number_union(head, &ptr->minor);
1606 } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1607 struct ccs_path2_acl *ptr =
1608 container_of(acl, typeof(*ptr), head);
1609 const u8 perm = ptr->perm;
1610 for (bit = 0; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1611 if (!(perm & (1 << bit)))
1612 continue;
1613 if (first) {
1614 ccs_set_group(head, "file ");
1615 first = false;
1616 } else {
1617 ccs_set_slash(head);
1618 }
1619 ccs_set_string(head, ccs_mac_keywords
1620 [ccs_pp2mac[bit]]);
1621 }
1622 if (first)
1623 return true;
1624 ccs_print_name_union(head, &ptr->name1);
1625 ccs_print_name_union(head, &ptr->name2);
1626 } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1627 struct ccs_path_number_acl *ptr =
1628 container_of(acl, typeof(*ptr), head);
1629 const u8 perm = ptr->perm;
1630 for (bit = 0; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {
1631 if (!(perm & (1 << bit)))
1632 continue;
1633 if (first) {
1634 ccs_set_group(head, "file ");
1635 first = false;
1636 } else {
1637 ccs_set_slash(head);
1638 }
1639 ccs_set_string(head, ccs_mac_keywords
1640 [ccs_pn2mac[bit]]);
1641 }
1642 if (first)
1643 return true;
1644 ccs_print_name_union(head, &ptr->name);
1645 ccs_print_number_union(head, &ptr->number);
1646 } else if (acl_type == CCS_TYPE_ENV_ACL) {
1647 struct ccs_env_acl *ptr =
1648 container_of(acl, typeof(*ptr), head);
1649 ccs_set_group(head, "misc env ");
1650 ccs_set_string(head, ptr->env->name);
1651 } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1652 struct ccs_capability_acl *ptr =
1653 container_of(acl, typeof(*ptr), head);
1654 ccs_set_group(head, "capability ");
1655 ccs_set_string(head, ccs_mac_keywords
1656 [ccs_c2mac[ptr->operation]]);
1657 } else if (acl_type == CCS_TYPE_INET_ACL) {
1658 struct ccs_inet_acl *ptr =
1659 container_of(acl, typeof(*ptr), head);
1660 const u8 perm = ptr->perm;
1661 for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1662 if (!(perm & (1 << bit)))
1663 continue;
1664 if (first) {
1665 ccs_set_group(head, "network inet ");
1666 ccs_set_string(head, ccs_proto_keyword
1667 [ptr->protocol]);
1668 ccs_set_space(head);
1669 first = false;
1670 } else {
1671 ccs_set_slash(head);
1672 }
1673 ccs_set_string(head, ccs_socket_keyword[bit]);
1674 }
1675 if (first)
1676 return true;
1677 ccs_set_space(head);
1678 if (ptr->address.group) {
1679 ccs_set_string(head, "@");
1680 ccs_set_string(head,
1681 ptr->address.group->group_name->name);
1682 } else {
1683 char buf[128];
1684 ccs_print_ip(buf, sizeof(buf), &ptr->address);
1685 ccs_io_printf(head, "%s", buf);
1686 }
1687 ccs_print_number_union(head, &ptr->port);
1688 } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1689 struct ccs_unix_acl *ptr =
1690 container_of(acl, typeof(*ptr), head);
1691 const u8 perm = ptr->perm;
1692 for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1693 if (!(perm & (1 << bit)))
1694 continue;
1695 if (first) {
1696 ccs_set_group(head, "network unix ");
1697 ccs_set_string(head, ccs_proto_keyword
1698 [ptr->protocol]);
1699 ccs_set_space(head);
1700 first = false;
1701 } else {
1702 ccs_set_slash(head);
1703 }
1704 ccs_set_string(head, ccs_socket_keyword[bit]);
1705 }
1706 if (first)
1707 return true;
1708 ccs_print_name_union(head, &ptr->name);
1709 } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1710 struct ccs_signal_acl *ptr =
1711 container_of(acl, typeof(*ptr), head);
1712 ccs_set_group(head, "ipc signal ");
1713 ccs_print_number_union_nospace(head, &ptr->sig);
1714 ccs_set_space(head);
1715 ccs_set_string(head, ptr->domainname->name);
1716 } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1717 struct ccs_mount_acl *ptr =
1718 container_of(acl, typeof(*ptr), head);
1719 ccs_set_group(head, "file mount");
1720 ccs_print_name_union(head, &ptr->dev_name);
1721 ccs_print_name_union(head, &ptr->dir_name);
1722 ccs_print_name_union(head, &ptr->fs_type);
1723 ccs_print_number_union(head, &ptr->flags);
1724 }
1725 if (acl->cond) {
1726 head->r.print_cond_part = true;
1727 head->r.cond_step = 0;
1728 if (!ccs_flush(head))
1729 return false;
1730 print_cond_part:
1731 if (!ccs_print_condition(head, acl->cond))
1732 return false;
1733 head->r.print_cond_part = false;
1734 } else {
1735 ccs_set_lf(head);
1736 }
1737 return true;
1738 }
1739
1740 /**
1741 * ccs_read_domain2 - Read domain policy.
1742 *
1743 * @head: Pointer to "struct ccs_io_buffer".
1744 * @list: Pointer to "struct list_head".
1745 *
1746 * Returns true on success, false otherwise.
1747 *
1748 * Caller holds ccs_read_lock().
1749 */
1750 static bool ccs_read_domain2(struct ccs_io_buffer *head,
1751 struct list_head *list)
1752 {
1753 list_for_each_cookie(head->r.acl, list) {
1754 struct ccs_acl_info *ptr =
1755 list_entry(head->r.acl, typeof(*ptr), list);
1756 if (!ccs_print_entry(head, ptr))
1757 return false;
1758 }
1759 head->r.acl = NULL;
1760 return true;
1761 }
1762
1763 /**
1764 * ccs_read_domain - Read domain policy.
1765 *
1766 * @head: Pointer to "struct ccs_io_buffer".
1767 *
1768 * Returns nothing.
1769 *
1770 * Caller holds ccs_read_lock().
1771 */
1772 static void ccs_read_domain(struct ccs_io_buffer *head)
1773 {
1774 if (head->r.eof)
1775 return;
1776 list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1777 struct ccs_domain_info *domain =
1778 list_entry(head->r.domain, typeof(*domain), list);
1779 switch (head->r.step) {
1780 u8 i;
1781 case 0:
1782 if (domain->is_deleted &&
1783 !head->r.print_this_domain_only)
1784 continue;
1785 /* Print domainname and flags. */
1786 ccs_set_string(head, domain->domainname->name);
1787 ccs_set_lf(head);
1788 ccs_io_printf(head, "use_profile %u\n",
1789 domain->profile);
1790 ccs_io_printf(head, "use_group %u\n", domain->group);
1791 for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1792 if (domain->flags[i])
1793 ccs_set_string(head, ccs_dif[i]);
1794 head->r.step++;
1795 ccs_set_lf(head);
1796 /* fall through */
1797 case 1:
1798 if (!ccs_read_domain2(head, &domain->acl_info_list[0]))
1799 return;
1800 head->r.step++;
1801 /* fall through */
1802 case 2:
1803 if (!ccs_read_domain2(head, &domain->acl_info_list[1]))
1804 return;
1805 head->r.step++;
1806 if (!ccs_set_lf(head))
1807 return;
1808 /* fall through */
1809 case 3:
1810 head->r.step = 0;
1811 if (head->r.print_this_domain_only)
1812 goto done;
1813 }
1814 }
1815 done:
1816 head->r.eof = true;
1817 }
1818
1819 /**
1820 * ccs_write_domain_profile - Assign profile for specified domain.
1821 *
1822 * @head: Pointer to "struct ccs_io_buffer".
1823 *
1824 * Returns 0 on success, -EINVAL otherwise.
1825 *
1826 * This is equivalent to doing
1827 *
1828 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1829 * /usr/sbin/ccs-loadpolicy -d
1830 *
1831 * Caller holds ccs_read_lock().
1832 */
1833 static int ccs_write_domain_profile(struct ccs_io_buffer *head)
1834 {
1835 char *data = head->write_buf;
1836 char *cp = strchr(data, ' ');
1837 struct ccs_domain_info *domain;
1838 unsigned int profile;
1839 if (!cp)
1840 return -EINVAL;
1841 *cp = '\0';
1842 profile = simple_strtoul(data, NULL, 10);
1843 if (profile >= CCS_MAX_PROFILES)
1844 return -EINVAL;
1845 domain = ccs_find_domain(cp + 1);
1846 if (domain && (!ccs_policy_loaded ||
1847 head->w.ns->profile_ptr[(u8) profile]))
1848 domain->profile = (u8) profile;
1849 return 0;
1850 }
1851
1852 /**
1853 * ccs_read_domain_profile - Read only domainname and profile.
1854 *
1855 * @head: Pointer to "struct ccs_io_buffer".
1856 *
1857 * Returns nothing.
1858 *
1859 * This is equivalent to doing
1860 *
1861 * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1862 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1863 * domainname = $0; } else if ( $1 == "use_profile" ) {
1864 * print $2 " " domainname; domainname = ""; } } ; '
1865 *
1866 * Caller holds ccs_read_lock().
1867 */
1868 static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1869 {
1870 if (head->r.eof)
1871 return;
1872 list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1873 struct ccs_domain_info *domain =
1874 list_entry(head->r.domain, typeof(*domain), list);
1875 if (domain->is_deleted)
1876 continue;
1877 if (!ccs_flush(head))
1878 return;
1879 ccs_io_printf(head, "%u ", domain->profile);
1880 ccs_set_string(head, domain->domainname->name);
1881 ccs_set_lf(head);
1882 }
1883 head->r.eof = true;
1884 }
1885
1886 /**
1887 * ccs_write_pid - Specify PID to obtain domainname.
1888 *
1889 * @head: Pointer to "struct ccs_io_buffer".
1890 *
1891 * Returns 0.
1892 */
1893 static int ccs_write_pid(struct ccs_io_buffer *head)
1894 {
1895 head->r.eof = false;
1896 return 0;
1897 }
1898
1899 /**
1900 * ccs_read_pid - Read information of a process.
1901 *
1902 * @head: Pointer to "struct ccs_io_buffer".
1903 *
1904 * Returns the domainname which the specified PID is in or
1905 * process information of the specified PID on success,
1906 * empty string otherwise.
1907 *
1908 * Caller holds ccs_read_lock().
1909 */
1910 static void ccs_read_pid(struct ccs_io_buffer *head)
1911 {
1912 char *buf = head->write_buf;
1913 bool task_info = false;
1914 bool global_pid = false;
1915 unsigned int pid;
1916 struct task_struct *p;
1917 struct ccs_domain_info *domain = NULL;
1918 u32 ccs_flags = 0;
1919 /* Accessing write_buf is safe because head->io_sem is held. */
1920 if (!buf) {
1921 head->r.eof = true;
1922 return; /* Do nothing if open(O_RDONLY). */
1923 }
1924 if (head->r.w_pos || head->r.eof)
1925 return;
1926 head->r.eof = true;
1927 if (ccs_str_starts(&buf, "info "))
1928 task_info = true;
1929 if (ccs_str_starts(&buf, "global-pid "))
1930 global_pid = true;
1931 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1932 ccs_tasklist_lock();
1933 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1934 if (global_pid)
1935 p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1936 else
1937 p = ccsecurity_exports.find_task_by_vpid(pid);
1938 #else
1939 p = find_task_by_pid(pid);
1940 #endif
1941 if (p) {
1942 domain = ccs_task_domain(p);
1943 ccs_flags = ccs_task_flags(p);
1944 }
1945 ccs_tasklist_unlock();
1946 if (!domain)
1947 return;
1948 if (!task_info) {
1949 ccs_io_printf(head, "%u %u ", pid, domain->profile);
1950 ccs_set_string(head, domain->domainname->name);
1951 } else {
1952 ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
1953 ccs_yesno(ccs_flags &
1954 CCS_TASK_IS_MANAGER),
1955 ccs_yesno(ccs_flags &
1956 CCS_TASK_IS_EXECUTE_HANDLER));
1957 }
1958 }
1959
1960 /* String table for domain transition control keywords. */
1961 static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1962 [CCS_TRANSITION_CONTROL_NO_NAMESPACE] = "no_move_namespace ",
1963 [CCS_TRANSITION_CONTROL_NAMESPACE] = "move_namespace ",
1964 [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1965 [CCS_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
1966 [CCS_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
1967 [CCS_TRANSITION_CONTROL_KEEP] = "keep_domain ",
1968 };
1969
1970 /* String table for grouping keywords. */
1971 static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1972 [CCS_PATH_GROUP] = "path_group ",
1973 [CCS_NUMBER_GROUP] = "number_group ",
1974 [CCS_ADDRESS_GROUP] = "address_group ",
1975 };
1976
1977 /**
1978 * ccs_write_exception - Write exception policy.
1979 *
1980 * @head: Pointer to "struct ccs_io_buffer".
1981 *
1982 * Returns 0 on success, negative value otherwise.
1983 */
1984 static int ccs_write_exception(struct ccs_io_buffer *head)
1985 {
1986 const bool is_delete = head->w.is_delete;
1987 struct ccs_acl_param param = {
1988 .ns = head->w.ns,
1989 .is_delete = is_delete,
1990 .data = head->write_buf,
1991 };
1992 u8 i;
1993 if (ccs_str_starts(&param.data, "aggregator "))
1994 return ccs_write_aggregator(&param);
1995 if (ccs_str_starts(&param.data, "deny_autobind "))
1996 return ccs_write_reserved_port(&param);
1997 for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1998 if (ccs_str_starts(&param.data, ccs_transition_type[i]))
1999 return ccs_write_transition_control(&param, i);
2000 for (i = 0; i < CCS_MAX_GROUP; i++)
2001 if (ccs_str_starts(&param.data, ccs_group_name[i]))
2002 return ccs_write_group(&param, i);
2003 if (ccs_str_starts(&param.data, "acl_group ")) {
2004 unsigned int group;
2005 char *data;
2006 group = simple_strtoul(param.data, &data, 10);
2007 if (group < CCS_MAX_ACL_GROUPS && *data++ == ' ')
2008 return ccs_write_domain2(head->w.ns,
2009 head->w.ns->acl_group[group],
2010 data, is_delete);
2011 }
2012 return -EINVAL;
2013 }
2014
2015 /**
2016 * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
2017 *
2018 * @head: Pointer to "struct ccs_io_buffer".
2019 * @idx: Index number.
2020 *
2021 * Returns true on success, false otherwise.
2022 *
2023 * Caller holds ccs_read_lock().
2024 */
2025 static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
2026 {
2027 struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
2028 namespace_list);
2029 struct list_head *list = &ns->group_list[idx];
2030 list_for_each_cookie(head->r.group, list) {
2031 struct ccs_group *group =
2032 list_entry(head->r.group, typeof(*group), head.list);
2033 list_for_each_cookie(head->r.acl, &group->member_list) {
2034 struct ccs_acl_head *ptr =
2035 list_entry(head->r.acl, typeof(*ptr), list);
2036 if (ptr->is_deleted)
2037 continue;
2038 if (!ccs_flush(head))
2039 return false;
2040 ccs_print_namespace(head);
2041 ccs_set_string(head, ccs_group_name[idx]);
2042 ccs_set_string(head, group->group_name->name);
2043 if (idx == CCS_PATH_GROUP) {
2044 ccs_set_space(head);
2045 ccs_set_string(head, container_of
2046 (ptr, struct ccs_path_group,
2047 head)->member_name->name);
2048 } else if (idx == CCS_NUMBER_GROUP) {
2049 ccs_print_number_union(head, &container_of
2050 (ptr, struct ccs_number_group,
2051 head)->number);
2052 } else if (idx == CCS_ADDRESS_GROUP) {
2053 char buffer[128];
2054 struct ccs_address_group *member =
2055 container_of(ptr, typeof(*member),
2056 head);
2057 ccs_print_ip(buffer, sizeof(buffer),
2058 &member->address);
2059 ccs_io_printf(head, " %s", buffer);
2060 }
2061 ccs_set_lf(head);
2062 }
2063 head->r.acl = NULL;
2064 }
2065 head->r.group = NULL;
2066 return true;
2067 }
2068
2069 /**
2070 * ccs_read_policy - Read "struct ccs_..._entry" list.
2071 *
2072 * @head: Pointer to "struct ccs_io_buffer".
2073 * @idx: Index number.
2074 *
2075 * Returns true on success, false otherwise.
2076 *
2077 * Caller holds ccs_read_lock().
2078 */
2079 static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
2080 {
2081 struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
2082 namespace_list);
2083 struct list_head *list = &ns->policy_list[idx];
2084 list_for_each_cookie(head->r.acl, list) {
2085 struct ccs_acl_head *acl =
2086 container_of(head->r.acl, typeof(*acl), list);
2087 if (acl->is_deleted)
2088 continue;
2089 if (head->r.print_transition_related_only &&
2090 idx != CCS_ID_TRANSITION_CONTROL)
2091 continue;
2092 if (!ccs_flush(head))
2093 return false;
2094 switch (idx) {
2095 case CCS_ID_TRANSITION_CONTROL:
2096 {
2097 struct ccs_transition_control *ptr =
2098 container_of(acl, typeof(*ptr), head);
2099 ccs_print_namespace(head);
2100 ccs_set_string(head,
2101 ccs_transition_type[ptr->type]);
2102 ccs_set_string(head, ptr->program ?
2103 ptr->program->name : "any");
2104 ccs_set_string(head, " from ");
2105 ccs_set_string(head, ptr->domainname ?
2106 ptr->domainname->name : "any");
2107 }
2108 break;
2109 case CCS_ID_AGGREGATOR:
2110 {
2111 struct ccs_aggregator *ptr =
2112 container_of(acl, typeof(*ptr), head);
2113 ccs_print_namespace(head);
2114 ccs_set_string(head, "aggregator ");
2115 ccs_set_string(head, ptr->original_name->name);
2116 ccs_set_space(head);
2117 ccs_set_string(head,
2118 ptr->aggregated_name->name);
2119 }
2120 break;
2121 case CCS_ID_RESERVEDPORT:
2122 {
2123 struct ccs_reserved *ptr =
2124 container_of(acl, typeof(*ptr), head);
2125 ccs_print_namespace(head);
2126 ccs_set_string(head, "deny_autobind ");
2127 ccs_print_number_union_nospace(head,
2128 &ptr->port);
2129 }
2130 break;
2131 default:
2132 continue;
2133 }
2134 ccs_set_lf(head);
2135 }
2136 head->r.acl = NULL;
2137 return true;
2138 }
2139
2140 /**
2141 * ccs_read_exception - Read exception policy.
2142 *
2143 * @head: Pointer to "struct ccs_io_buffer".
2144 *
2145 * Returns nothing.
2146 *
2147 * Caller holds ccs_read_lock().
2148 */
2149 static void ccs_read_exception(struct ccs_io_buffer *head)
2150 {
2151 struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
2152 namespace_list);
2153 if (head->r.eof)
2154 return;
2155 while (head->r.step < CCS_MAX_POLICY &&
2156 ccs_read_policy(head, head->r.step))
2157 head->r.step++;
2158 if (head->r.step < CCS_MAX_POLICY)
2159 return;
2160 while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2161 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
2162 head->r.step++;
2163 if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2164 return;
2165 while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
2166 + CCS_MAX_ACL_GROUPS * 2) {
2167 head->r.acl_group_index = (head->r.step - CCS_MAX_POLICY
2168 - CCS_MAX_GROUP) / 2;
2169 if (!ccs_read_domain2(head, &ns->acl_group
2170 [head->r.acl_group_index]
2171 [head->r.step & 1]))
2172 return;
2173 head->r.step++;
2174 }
2175 head->r.eof = true;
2176 }
2177
2178 /* Wait queue for kernel -> userspace notification. */
2179 static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2180 /* Wait queue for userspace -> kernel notification. */
2181 static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
2182
2183 /* Structure for query. */
2184 struct ccs_query {
2185 struct list_head list;
2186 char *query;
2187 size_t query_len;
2188 unsigned int serial;
2189 u8 timer;
2190 u8 answer;
2191 u8 retry;
2192 };
2193
2194 /* The list for "struct ccs_query". */
2195 static LIST_HEAD(ccs_query_list);
2196
2197 /* Lock for manipulating ccs_query_list. */
2198 static DEFINE_SPINLOCK(ccs_query_list_lock);
2199
2200 /* Number of "struct file" referring /proc/ccs/query interface. */
2201 static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2202
2203 /**
2204 * ccs_truncate - Truncate a line.
2205 *
2206 * @str: String to truncate.
2207 *
2208 * Returns length of truncated @str.
2209 */
2210 static int ccs_truncate(char *str)
2211 {
2212 char *start = str;
2213 while (*(unsigned char *) str > (unsigned char) ' ')
2214 str++;
2215 *str = '\0';
2216 return strlen(start) + 1;
2217 }
2218
2219 /**
2220 * ccs_add_entry - Add an ACL to current thread's domain. Used by learning mode.
2221 *
2222 * @header: Lines containing ACL.
2223 *
2224 * Returns nothing.
2225 */
2226 static void ccs_add_entry(char *header)
2227 {
2228 char *buffer;
2229 char *realpath = NULL;
2230 char *argv0 = NULL;
2231 char *symlink = NULL;
2232 char *handler;
2233 char *cp = strchr(header, '\n');
2234 int len;
2235 if (!cp)
2236 return;
2237 cp = strchr(cp + 1, '\n');
2238 if (!cp)
2239 return;
2240 *cp++ = '\0';
2241 len = strlen(cp) + 1;
2242 /* strstr() will return NULL if ordering is wrong. */
2243 if (*cp == 'f') {
2244 argv0 = strstr(header, " argv[]={ \"");
2245 if (argv0) {
2246 argv0 += 10;
2247 len += ccs_truncate(argv0) + 14;
2248 }
2249 realpath = strstr(header, " exec={ realpath=\"");
2250 if (realpath) {
2251 realpath += 8;
2252 len += ccs_truncate(realpath) + 6;
2253 }
2254 symlink = strstr(header, " symlink.target=\"");
2255 if (symlink)
2256 len += ccs_truncate(symlink + 1) + 1;
2257 }
2258 handler = strstr(header, "type=execute_handler");
2259 if (handler)
2260 len += ccs_truncate(handler) + 6;
2261 buffer = kmalloc(len, CCS_GFP_FLAGS);
2262 if (!buffer)
2263 return;
2264 snprintf(buffer, len - 1, "%s", cp);
2265 if (handler)
2266 ccs_addprintf(buffer, len, " task.%s", handler);
2267 if (realpath)
2268 ccs_addprintf(buffer, len, " exec.%s", realpath);
2269 if (argv0)
2270 ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
2271 if (symlink)
2272 ccs_addprintf(buffer, len, "%s", symlink);
2273 ccs_normalize_line(buffer);
2274 {
2275 struct ccs_domain_info *domain = ccs_current_domain();
2276 if (!ccs_write_domain2(domain->ns, domain->acl_info_list,
2277 buffer, false))
2278 ccs_update_stat(CCS_STAT_POLICY_UPDATES);
2279 }
2280 kfree(buffer);
2281 }
2282
2283 /**
2284 * ccs_supervisor - Ask for the supervisor's decision.
2285 *
2286 * @r: Pointer to "struct ccs_request_info".
2287 * @fmt: The printf()'s format string, followed by parameters.
2288 *
2289 * Returns 0 if the supervisor decided to permit the access request which
2290 * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2291 * decided to retry the access request which violated the policy in enforcing
2292 * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2293 */
2294 int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2295 {
2296 va_list args;
2297 int error;
2298 int len;
2299 static unsigned int ccs_serial;
2300 struct ccs_query entry = { };
2301 bool quota_exceeded = false;
2302 va_start(args, fmt);
2303 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2304 va_end(args);
2305 /* Write /proc/ccs/audit. */
2306 va_start(args, fmt);
2307 ccs_write_log2(r, len, fmt, args);
2308 va_end(args);
2309 /* Nothing more to do if granted. */
2310 if (r->granted)
2311 return 0;
2312 if (r->mode)
2313 ccs_update_stat(r->mode);
2314 switch (r->mode) {
2315 int i;
2316 struct ccs_profile *p;
2317 case CCS_CONFIG_ENFORCING:
2318 error = -EPERM;
2319 if (atomic_read(&ccs_query_observers))
2320 break;
2321 if (r->dont_sleep_on_enforce_error)
2322 goto out;
2323 p = ccs_profile(r->profile);
2324 /* Check enforcing_penalty parameter. */
2325 for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) {
2326 set_current_state(TASK_INTERRUPTIBLE);
2327 schedule_timeout(HZ / 10);
2328 }
2329 goto out;
2330 case CCS_CONFIG_LEARNING:
2331 error = 0;
2332 /* Check max_learning_entry parameter. */
2333 if (ccs_domain_quota_ok(r))
2334 break;
2335 /* fall through */
2336 default:
2337 return 0;
2338 }
2339 /* Get message. */
2340 va_start(args, fmt);
2341 entry.query = ccs_init_log(r, len, fmt, args);
2342 va_end(args);
2343 if (!entry.query)
2344 goto out;
2345 entry.query_len = strlen(entry.query) + 1;
2346 if (!error) {
2347 ccs_add_entry(entry.query);
2348 goto out;
2349 }
2350 len = ccs_round2(entry.query_len);
2351 spin_lock(&ccs_query_list_lock);
2352 if (ccs_memory_quota[CCS_MEMORY_QUERY] &&
2353 ccs_memory_used[CCS_MEMORY_QUERY] + len
2354 >= ccs_memory_quota[CCS_MEMORY_QUERY]) {
2355 quota_exceeded = true;
2356 } else {
2357 entry.serial = ccs_serial++;
2358 entry.retry = r->retry;
2359 ccs_memory_used[CCS_MEMORY_QUERY] += len;
2360 list_add_tail(&entry.list, &ccs_query_list);
2361 }
2362 spin_unlock(&ccs_query_list_lock);
2363 if (quota_exceeded)
2364 goto out;
2365 /* Give 10 seconds for supervisor's opinion. */
2366 while (entry.timer < 10) {
2367 wake_up_all(&ccs_query_wait);
2368 if (wait_event_interruptible_timeout
2369 (ccs_answer_wait, entry.answer ||
2370 !atomic_read(&ccs_query_observers), HZ))
2371 break;
2372 else
2373 entry.timer++;
2374 }
2375 spin_lock(&ccs_query_list_lock);
2376 list_del(&entry.list);
2377 ccs_memory_used[CCS_MEMORY_QUERY] -= len;
2378 spin_unlock(&ccs_query_list_lock);
2379 switch (entry.answer) {
2380 case 3: /* Asked to retry by administrator. */
2381 error = CCS_RETRY_REQUEST;
2382 r->retry++;
2383 break;
2384 case 1:
2385 /* Granted by administrator. */
2386 error = 0;
2387 break;
2388 default:
2389 /* Timed out or rejected by administrator. */
2390 break;
2391 }
2392 out:
2393 kfree(entry.query);
2394 return error;
2395 }
2396
2397 /**
2398 * ccs_poll_query - poll() for /proc/ccs/query.
2399 *
2400 * @file: Pointer to "struct file".
2401 * @wait: Pointer to "poll_table".
2402 *
2403 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2404 *
2405 * Waits for access requests which violated policy in enforcing mode.
2406 */
2407 static int ccs_poll_query(struct file *file, poll_table *wait)
2408 {
2409 struct list_head *tmp;
2410 bool found = false;
2411 u8 i;
2412 for (i = 0; i < 2; i++) {
2413 spin_lock(&ccs_query_list_lock);
2414 list_for_each(tmp, &ccs_query_list) {
2415 struct ccs_query *ptr =
2416 list_entry(tmp, typeof(*ptr), list);
2417 if (ptr->answer)
2418 continue;
2419 found = true;
2420 break;
2421 }
2422 spin_unlock(&ccs_query_list_lock);
2423 if (found)
2424 return POLLIN | POLLRDNORM;
2425 if (i)
2426 break;
2427 poll_wait(file, &ccs_query_wait, wait);
2428 }
2429 return 0;
2430 }
2431
2432 /**
2433 * ccs_read_query - Read access requests which violated policy in enforcing mode.
2434 *
2435 * @head: Pointer to "struct ccs_io_buffer".
2436 *
2437 * Returns nothing.
2438 */
2439 static void ccs_read_query(struct ccs_io_buffer *head)
2440 {
2441 struct list_head *tmp;
2442 unsigned int pos = 0;
2443 size_t len = 0;
2444 char *buf;
2445 if (head->r.w_pos)
2446 return;
2447 kfree(head->read_buf);
2448 head->read_buf = NULL;
2449 spin_lock(&ccs_query_list_lock);
2450 list_for_each(tmp, &ccs_query_list) {
2451 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2452 if (ptr->answer)
2453 continue;
2454 if (pos++ != head->r.query_index)
2455 continue;
2456 len = ptr->query_len;
2457 break;
2458 }
2459 spin_unlock(&ccs_query_list_lock);
2460 if (!len) {
2461 head->r.query_index = 0;
2462 return;
2463 }
2464 buf = kzalloc(len + 32, CCS_GFP_FLAGS);
2465 if (!buf)
2466 return;
2467 pos = 0;
2468 spin_lock(&ccs_query_list_lock);
2469 list_for_each(tmp, &ccs_query_list) {
2470 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2471 if (ptr->answer)
2472 continue;
2473 if (pos++ != head->r.query_index)
2474 continue;
2475 /*
2476 * Some query can be skipped because ccs_query_list
2477 * can change, but I don't care.
2478 */
2479 if (len == ptr->query_len)
2480 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2481 ptr->retry, ptr->query);
2482 break;
2483 }
2484 spin_unlock(&ccs_query_list_lock);
2485 if (buf[0]) {
2486 head->read_buf = buf;
2487 head->r.w[head->r.w_pos++] = buf;
2488 head->r.query_index++;
2489 } else {
2490 kfree(buf);
2491 }
2492 }
2493
2494 /**
2495 * ccs_write_answer - Write the supervisor's decision.
2496 *
2497 * @head: Pointer to "struct ccs_io_buffer".
2498 *
2499 * Returns 0 on success, -EINVAL otherwise.
2500 */
2501 static int ccs_write_answer(struct ccs_io_buffer *head)
2502 {
2503 char *data = head->write_buf;
2504 struct list_head *tmp;
2505 unsigned int serial;
2506 unsigned int answer;
2507 spin_lock(&ccs_query_list_lock);
2508 list_for_each(tmp, &ccs_query_list) {
2509 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2510 ptr->timer = 0;
2511 }
2512 spin_unlock(&ccs_query_list_lock);
2513 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2514 return -EINVAL;
2515 spin_lock(&ccs_query_list_lock);
2516 list_for_each(tmp, &ccs_query_list) {
2517 struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
2518 if (ptr->serial != serial)
2519 continue;
2520 if (!ptr->answer)
2521 ptr->answer = (u8) answer;
2522 break;
2523 }
2524 spin_unlock(&ccs_query_list_lock);
2525 wake_up_all(&ccs_answer_wait);
2526 return 0;
2527 }
2528
2529 /**
2530 * ccs_read_version - Get version.
2531 *
2532 * @head: Pointer to "struct ccs_io_buffer".
2533 *
2534 * Returns nothing.
2535 */
2536 static void ccs_read_version(struct ccs_io_buffer *head)
2537 {
2538 if (head->r.eof)
2539 return;
2540 ccs_set_string(head, "1.8.2-pre");
2541 head->r.eof = true;
2542 }
2543
2544 /* String table for /proc/ccs/stat interface. */
2545 static const char * const ccs_policy_headers[CCS_MAX_POLICY_STAT] = {
2546 [CCS_STAT_POLICY_UPDATES] = "update:",
2547 [CCS_STAT_POLICY_LEARNING] = "violation in learning mode:",
2548 [CCS_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2549 [CCS_STAT_POLICY_ENFORCING] = "violation in enforcing mode:",
2550 };
2551
2552 /* String table for /proc/ccs/stat interface. */
2553 static const char * const ccs_memory_headers[CCS_MAX_MEMORY_STAT] = {
2554 [CCS_MEMORY_POLICY] = "policy:",
2555 [CCS_MEMORY_AUDIT] = "audit log:",
2556 [CCS_MEMORY_QUERY] = "query message:",
2557 };
2558
2559 /* Timestamp counter for last updated. */
2560 static unsigned int ccs_stat_updated[CCS_MAX_POLICY_STAT];
2561 /* Counter for number of updates. */
2562 static unsigned int ccs_stat_modified[CCS_MAX_POLICY_STAT];
2563
2564 /**
2565 * ccs_update_stat - Update statistic counters.
2566 *
2567 * @index: Index for policy type.
2568 *
2569 * Returns nothing.
2570 */
2571 void ccs_update_stat(const u8 index)
2572 {
2573 struct timeval tv;
2574 do_gettimeofday(&tv);
2575 /*
2576 * I don't use atomic operations because race condition is not fatal.
2577 */
2578 ccs_stat_updated[index]++;
2579 ccs_stat_modified[index] = tv.tv_sec;
2580 }
2581
2582 /**
2583 * ccs_read_stat - Read statistic data.
2584 *
2585 * @head: Pointer to "struct ccs_io_buffer".
2586 *
2587 * Returns nothing.
2588 */
2589 static void ccs_read_stat(struct ccs_io_buffer *head)
2590 {
2591 u8 i;
2592 unsigned int total = 0;
2593 if (head->r.eof)
2594 return;
2595 for (i = 0; i < CCS_MAX_POLICY_STAT; i++) {
2596 ccs_io_printf(head, "Policy %-30s %10u", ccs_policy_headers[i],
2597 ccs_stat_updated[i]);
2598 if (ccs_stat_modified[i]) {
2599 struct ccs_time stamp;
2600 ccs_convert_time(ccs_stat_modified[i], &stamp);
2601 ccs_io_printf(head, " (Last: %04u/%02u/%02u "
2602 "%02u:%02u:%02u)",
2603 stamp.year, stamp.month, stamp.day,
2604 stamp.hour, stamp.min, stamp.sec);
2605 }
2606 ccs_set_lf(head);
2607 }
2608 for (i = 0; i < CCS_MAX_MEMORY_STAT; i++) {
2609 unsigned int used = ccs_memory_used[i];
2610 total += used;
2611 ccs_io_printf(head, "Memory used by %-22s %10u",
2612 ccs_memory_headers[i], used);
2613 used = ccs_memory_quota[i];
2614 if (used)
2615 ccs_io_printf(head, " (Quota: %10u)", used);
2616 ccs_set_lf(head);
2617 }
2618 ccs_io_printf(head, "Total memory used: %10u\n",
2619 total);
2620 head->r.eof = true;
2621 }
2622
2623 /**
2624 * ccs_write_stat - Set memory quota.
2625 *
2626 * @head: Pointer to "struct ccs_io_buffer".
2627 *
2628 * Returns 0.
2629 */
2630 static int ccs_write_stat(struct ccs_io_buffer *head)
2631 {
2632 char *data = head->write_buf;
2633 u8 i;
2634 if (ccs_str_starts(&data, "Memory used by "))
2635 for (i = 0; i < CCS_MAX_MEMORY_STAT; i++)
2636 if (ccs_str_starts(&data, ccs_memory_headers[i]))
2637 ccs_memory_quota[i] =
2638 simple_strtoul(data, NULL, 10);
2639 return 0;
2640 }
2641
2642 /**
2643 * ccs_open_control - open() for /proc/ccs/ interface.
2644 *
2645 * @type: Type of interface.
2646 * @file: Pointer to "struct file".
2647 *
2648 * Returns 0 on success, negative value otherwise.
2649 */
2650 int ccs_open_control(const u8 type, struct file *file)
2651 {
2652 struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2653 if (!head)
2654 return -ENOMEM;
2655 head->original_ns = ccs_current_namespace();
2656 mutex_init(&head->io_sem);
2657 head->type = type;
2658 switch (type) {
2659 case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2660 head->write = ccs_write_domain;
2661 head->read = ccs_read_domain;
2662 break;
2663 case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2664 head->write = ccs_write_exception;
2665 head->read = ccs_read_exception;
2666 break;
2667 case CCS_AUDIT: /* /proc/ccs/audit */
2668 head->poll = ccs_poll_log;
2669 head->read = ccs_read_log;
2670 break;
2671 case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2672 head->write = ccs_write_domain_profile;
2673 head->read = ccs_read_domain_profile;
2674 break;
2675 case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */
2676 /* Allow execute_handler to read process's status. */
2677 if (!(ccs_current_flags() & CCS_TASK_IS_EXECUTE_HANDLER)) {
2678 kfree(head);
2679 return -EPERM;
2680 }
2681 /* fall through */
2682 case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */
2683 head->write = ccs_write_pid;
2684 head->read = ccs_read_pid;
2685 break;
2686 case CCS_VERSION: /* /proc/ccs/version */
2687 head->read = ccs_read_version;
2688 head->readbuf_size = 128;
2689 break;
2690 case CCS_STAT: /* /proc/ccs/stat */
2691 head->write = ccs_write_stat;
2692 head->read = ccs_read_stat;
2693 head->readbuf_size = 1024;
2694 break;
2695 case CCS_PROFILE: /* /proc/ccs/profile */
2696 head->write = ccs_write_profile;
2697 head->read = ccs_read_profile;
2698 break;
2699 case CCS_QUERY: /* /proc/ccs/query */
2700 head->poll = ccs_poll_query;
2701 head->write = ccs_write_answer;
2702 head->read = ccs_read_query;
2703 break;
2704 case CCS_MANAGER: /* /proc/ccs/manager */
2705 head->write = ccs_write_manager;
2706 head->read = ccs_read_manager;
2707 break;
2708 }
2709 if (!(file->f_mode & FMODE_READ)) {
2710 /*
2711 * No need to allocate read_buf since it is not opened
2712 * for reading.
2713 */
2714 head->read = NULL;
2715 head->poll = NULL;
2716 } else if (!head->poll) {
2717 /* Don't allocate read_buf for poll() access. */
2718 if (!head->readbuf_size)
2719 head->readbuf_size = 4096;
2720 head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2721 if (!head->read_buf) {
2722 kfree(head);
2723 return -ENOMEM;
2724 }
2725 }
2726 if (!(file->f_mode & FMODE_WRITE)) {
2727 /*
2728 * No need to allocate write_buf since it is not opened
2729 * for writing.
2730 */
2731 head->write = NULL;
2732 } else if (head->write) {
2733 head->writebuf_size = 4096;
2734 head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2735 if (!head->write_buf) {
2736 kfree(head->read_buf);
2737 kfree(head);
2738 return -ENOMEM;
2739 }
2740 }
2741 /*
2742 * If the file is /proc/ccs/query, increment the observer counter.
2743 * The obserber counter is used by ccs_supervisor() to see if
2744 * there is some process monitoring /proc/ccs/query.
2745 */
2746 if (type == CCS_QUERY)
2747 atomic_inc(&ccs_query_observers);
2748 file->private_data = head;
2749 ccs_notify_gc(head, true);
2750 return 0;
2751 }
2752
2753 /**
2754 * ccs_poll_control - poll() for /proc/ccs/ interface.
2755 *
2756 * @file: Pointer to "struct file".
2757 * @wait: Pointer to "poll_table".
2758 *
2759 * Returns return value of poll().
2760 *
2761 * Waits for read readiness.
2762 * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2763 * /proc/ccs/audit is handled by /usr/sbin/ccs-auditd.
2764 */
2765 int ccs_poll_control(struct file *file, poll_table *wait)
2766 {
2767 struct ccs_io_buffer *head = file->private_data;
2768 if (!head->poll)
2769 return -ENOSYS;
2770 return head->poll(file, wait);
2771 }
2772
2773 /**
2774 * ccs_move_namespace_cursor - Print namespace delimiter if needed.
2775 *
2776 * @head: Pointer to "struct ccs_io_buffer".
2777 *
2778 * Returns nothing.
2779 */
2780 static inline void ccs_move_namespace_cursor(struct ccs_io_buffer *head)
2781 {
2782 struct list_head *ns;
2783 if (head->type != CCS_EXCEPTIONPOLICY && head->type != CCS_PROFILE)
2784 return;
2785 /*
2786 * If this is the first read, or reading previous namespace finished
2787 * and has more namespaces to read, update the namespace cursor.
2788 */
2789 ns = head->r.ns;
2790 if (!ns || (head->r.eof && ns->next != &ccs_namespace_list)) {
2791 /* Clearing is OK because ccs_flush() returned true. */
2792 memset(&head->r, 0, sizeof(head->r));
2793 head->r.ns = ns ? ns->next : ccs_namespace_list.next;
2794 }
2795 }
2796
2797 /**
2798 * ccs_has_more_namespace - Check for unread namespaces.
2799 *
2800 * @head: Pointer to "struct ccs_io_buffer".
2801 *
2802 * Returns true if we have more entries to print, false otherwise.
2803 */
2804 static inline bool ccs_has_more_namespace(struct ccs_io_buffer *head)
2805 {
2806 return (head->type == CCS_EXCEPTIONPOLICY ||
2807 head->type == CCS_PROFILE) && head->r.eof &&
2808 head->r.ns->next != &ccs_namespace_list;
2809 }
2810
2811 /**
2812 * ccs_read_control - read() for /proc/ccs/ interface.
2813 *
2814 * @file: Pointer to "struct file".
2815 * @buffer: Poiner to buffer to write to.
2816 * @buffer_len: Size of @buffer.
2817 *
2818 * Returns bytes read on success, negative value otherwise.
2819 */
2820 ssize_t ccs_read_control(struct file *file, char __user *buffer,
2821 const size_t buffer_len)
2822 {
2823 int len;
2824 struct ccs_io_buffer *head = file->private_data;
2825 int idx;
2826 if (!head->read)
2827 return -ENOSYS;
2828 if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2829 return -EFAULT;
2830 if (mutex_lock_interruptible(&head->io_sem))
2831 return -EINTR;
2832 head->read_user_buf = buffer;
2833 head->read_user_buf_avail = buffer_len;
2834 idx = ccs_read_lock();
2835 if (ccs_flush(head))
2836 /* Call the policy handler. */
2837 do {
2838 ccs_move_namespace_cursor(head);
2839 head->read(head);
2840 } while (ccs_flush(head) && ccs_has_more_namespace(head));
2841 ccs_read_unlock(idx);
2842 len = head->read_user_buf - buffer;
2843 mutex_unlock(&head->io_sem);
2844 return len;
2845 }
2846
2847 /**
2848 * ccs_write_control - write() for /proc/ccs/ interface.
2849 *
2850 * @file: Pointer to "struct file".
2851 * @buffer: Pointer to buffer to read from.
2852 * @buffer_len: Size of @buffer.
2853 *
2854 * Returns @buffer_len on success, negative value otherwise.
2855 */
2856 ssize_t ccs_write_control(struct file *file, const char __user *buffer,
2857 const size_t buffer_len)
2858 {
2859 struct ccs_io_buffer *head = file->private_data;
2860 int error = buffer_len;
2861 size_t avail_len = buffer_len;
2862 char *cp0 = head->write_buf;
2863 int idx;
2864 if (!head->write)
2865 return -ENOSYS;
2866 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2867 return -EFAULT;
2868 if (mutex_lock_interruptible(&head->io_sem))
2869 return -EINTR;
2870 idx = ccs_read_lock();
2871 /* Read a line and dispatch it to the policy handler. */
2872 while (avail_len > 0) {
2873 char c;
2874 if (head->w.avail >= head->writebuf_size - 1) {
2875 const int len = head->writebuf_size * 2;
2876 char *cp = kzalloc(len, CCS_GFP_FLAGS);
2877 if (!cp) {
2878 error = -ENOMEM;
2879 break;
2880 }
2881 memmove(cp, cp0, head->w.avail);
2882 kfree(cp0);
2883 head->write_buf = cp;
2884 cp0 = cp;
2885 head->writebuf_size = len;
2886 }
2887 if (get_user(c, buffer)) {
2888 error = -EFAULT;
2889 break;
2890 }
2891 buffer++;
2892 avail_len--;
2893 cp0[head->w.avail++] = c;
2894 if (c != '\n')
2895 continue;
2896 cp0[head->w.avail - 1] = '\0';
2897 head->w.avail = 0;
2898 ccs_normalize_line(cp0);
2899 if (!strcmp(cp0, "reset")) {
2900 head->w.ns = head->original_ns;
2901 head->w.domain = NULL;
2902 memset(&head->r, 0, sizeof(head->r));
2903 continue;
2904 }
2905 /* Don't allow updating policies by non manager programs. */
2906 switch (head->type) {
2907 case CCS_PROCESS_STATUS:
2908 /* This does not write anything. */
2909 break;
2910 case CCS_DOMAINPOLICY:
2911 if (ccs_select_domain(head, cp0))
2912 continue;
2913 /* fall through */
2914 case CCS_EXCEPTIONPOLICY:
2915 if (!strcmp(cp0, "select transition_only")) {
2916 head->r.print_transition_related_only = true;
2917 continue;
2918 }
2919 /* fall through */
2920 default:
2921 if (!ccs_manager()) {
2922 error = -EPERM;
2923 goto out;
2924 }
2925 }
2926 /* Delete request? */
2927 head->w.is_delete = !strncmp(cp0, "delete ", 7);
2928 if (head->w.is_delete)
2929 memmove(cp0, cp0 + 7, strlen(cp0 + 7) + 1);
2930 /* Selecting namespace to update. */
2931 if (head->type == CCS_EXCEPTIONPOLICY ||
2932 head->type == CCS_PROFILE) {
2933 if (*cp0 == '<') {
2934 char *cp = strchr(cp0, ' ');
2935 if (cp) {
2936 *cp++ = '\0';
2937 head->w.ns = ccs_assign_namespace(cp0);
2938 memmove(cp0, cp, strlen(cp) + 1);
2939 } else
2940 head->w.ns = NULL;
2941 } else
2942 head->w.ns = head->original_ns;
2943 /* Don't allow updating if namespace is invalid. */
2944 if (!head->w.ns) {
2945 error = -ENOENT;
2946 goto out;
2947 }
2948 }
2949 {
2950 /* Do the update. */
2951 const int ret = head->write(head);
2952 if (ret == -EPERM) {
2953 error = -EPERM;
2954 break;
2955 }
2956 /* Do not update statistics if not updated. */
2957 if (ret)
2958 continue;
2959 }
2960 /* Update statistics. */
2961 switch (head->type) {
2962 case CCS_DOMAINPOLICY:
2963 case CCS_EXCEPTIONPOLICY:
2964 case CCS_DOMAIN_STATUS:
2965 case CCS_STAT:
2966 case CCS_PROFILE:
2967 case CCS_MANAGER:
2968 ccs_update_stat(CCS_STAT_POLICY_UPDATES);
2969 break;
2970 default:
2971 break;
2972 }
2973 }
2974 out:
2975 ccs_read_unlock(idx);
2976 mutex_unlock(&head->io_sem);
2977 return error;
2978 }
2979
2980 /**
2981 * ccs_close_control - close() for /proc/ccs/ interface.
2982 *
2983 * @file: Pointer to "struct file".
2984 *
2985 * Returns 0.
2986 */
2987 int ccs_close_control(struct file *file)
2988 {
2989 struct ccs_io_buffer *head = file->private_data;
2990 file->private_data = NULL;
2991 /*
2992 * If the file is /proc/ccs/query, decrement the observer counter.
2993 */
2994 if (head->type == CCS_QUERY &&
2995 atomic_dec_and_test(&ccs_query_observers))
2996 wake_up_all(&ccs_answer_wait);
2997 ccs_notify_gc(head, false);
2998 return 0;
2999 }
3000
3001 /**
3002 * ccs_policy_io_init - Register hooks for policy I/O.
3003 *
3004 * Returns nothing.
3005 */
3006 void __init ccs_policy_io_init(void)
3007 {
3008 ccsecurity_ops.check_profile = ccs_check_profile;
3009 }
3010
3011 #ifdef CONFIG_CCSECURITY_USE_BUILTIN_POLICY
3012 /**
3013 * ccs_load_builtin_policy - Load built-in policy.
3014 *
3015 * Returns nothing.
3016 */
3017 void __init ccs_load_builtin_policy(void)
3018 {
3019 /*
3020 * This include file is manually created and contains built-in policy
3021 * named "ccs_builtin_profile", "ccs_builtin_exception_policy",
3022 * "ccs_builtin_domain_policy", "ccs_builtin_manager",
3023 * "ccs_builtin_stat" in the form of "static char [] __initdata".
3024 */
3025 #include "builtin-policy.h"
3026 struct ccs_io_buffer head;
3027 u8 i;
3028 const int idx = ccs_read_lock();
3029 for (i = 0; i < 5; i++) {
3030 char *start = "";
3031 memset(&head, 0, sizeof(head));
3032 switch (i) {
3033 case 0:
3034 start = ccs_builtin_profile;
3035 head.write = ccs_write_profile;
3036 break;
3037 case 1:
3038 start = ccs_builtin_exception_policy;
3039 head.write = ccs_write_exception;
3040 break;
3041 case 2:
3042 start = ccs_builtin_domain_policy;
3043 head.write = ccs_write_domain;
3044 break;
3045 case 3:
3046 start = ccs_builtin_manager;
3047 head.write = ccs_write_manager;
3048 break;
3049 case 4:
3050 start = ccs_builtin_stat;
3051 head.write = ccs_write_stat;
3052 break;
3053 }
3054 while (1) {
3055 char *end = strchr(start, '\n');
3056 if (!end)
3057 break;
3058 *end = '\0';
3059 ccs_normalize_line(start);
3060 head.write_buf = start;
3061 head.write(&head);
3062 start = end + 1;
3063 }
3064 }
3065 ccs_read_unlock(idx);
3066 #ifdef CONFIG_CCSECURITY_ACTIVATE_FROM_THE_BEGINNING
3067 ccs_check_profile();
3068 #endif
3069 }
3070 #endif

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