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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5294 - (show annotations) (download) (as text)
Fri Jul 22 03:17:33 2011 UTC (12 years, 10 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 81153 byte(s)


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

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