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

Subversion リポジトリの参照

Contents of /trunk/1.7.x/ccs-patch/security/ccsecurity/util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 332 - (show annotations) (download) (as text)
Thu Aug 9 12:15:49 2007 UTC (16 years, 9 months ago) by kumaneko
Original Path: trunk/1.5.x/ccs-patch/fs/ccs_common.c
File MIME type: text/x-csrc
File size: 49665 byte(s)


1 /*
2 * fs/ccs_common.c
3 *
4 * Common functions for SAKURA and TOMOYO.
5 *
6 * Copyright (C) 2005-2007 NTT DATA CORPORATION
7 *
8 * Version: 1.5.0-pre 2007/08/06
9 *
10 * This file is applicable to both 2.4.30 and 2.6.11 and later.
11 * See README.ccs for ChangeLog.
12 *
13 */
14
15 #include <linux/string.h>
16 #include <linux/mm.h>
17 #include <linux/utime.h>
18 #include <linux/file.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <asm/uaccess.h>
22 #include <stdarg.h>
23 #include <linux/version.h>
24 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
25 #include <linux/namei.h>
26 #include <linux/mount.h>
27 static const int lookup_flags = LOOKUP_FOLLOW;
28 #else
29 static const int lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE;
30 #endif
31 #include <linux/version.h>
32 #include <linux/realpath.h>
33 #include <linux/ccs_common.h>
34 #include <linux/ccs_proc.h>
35 #include <linux/tomoyo.h>
36
37 #ifdef CONFIG_TOMOYO_MAX_ACCEPT_ENTRY
38 #define MAX_ACCEPT_ENTRY (CONFIG_TOMOYO_MAX_ACCEPT_ENTRY)
39 #else
40 #define MAX_ACCEPT_ENTRY 2048
41 #endif
42 #ifdef CONFIG_TOMOYO_MAX_GRANT_LOG
43 #define MAX_GRANT_LOG (CONFIG_TOMOYO_MAX_GRANT_LOG)
44 #else
45 #define MAX_GRANT_LOG 1024
46 #endif
47 #ifdef CONFIG_TOMOYO_MAX_REJECT_LOG
48 #define MAX_REJECT_LOG (CONFIG_TOMOYO_MAX_REJECT_LOG)
49 #else
50 #define MAX_REJECT_LOG 1024
51 #endif
52
53 /************************* VARIABLES *************************/
54
55 /* /sbin/init started? */
56 int sbin_init_started = 0;
57
58 const char *ccs_log_level = KERN_DEBUG;
59
60 static struct {
61 const char *keyword;
62 unsigned int current_value;
63 const unsigned int max_value;
64 } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {
65 [CCS_PROFILE_COMMENT] = { "COMMENT", 0, 0 }, /* Reserved for string. */
66 [CCS_TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
67 [CCS_TOMOYO_MAC_FOR_ARGV0] = { "MAC_FOR_ARGV0", 0, 3 },
68 [CCS_TOMOYO_MAC_FOR_NETWORK] = { "MAC_FOR_NETWORK", 0, 3 },
69 [CCS_TOMOYO_MAC_FOR_SIGNAL] = { "MAC_FOR_SIGNAL", 0, 3 },
70 [CCS_SAKURA_DENY_CONCEAL_MOUNT] = { "DENY_CONCEAL_MOUNT", 0, 3 },
71 [CCS_SAKURA_RESTRICT_CHROOT] = { "RESTRICT_CHROOT", 0, 3 },
72 [CCS_SAKURA_RESTRICT_MOUNT] = { "RESTRICT_MOUNT", 0, 3 },
73 [CCS_SAKURA_RESTRICT_UNMOUNT] = { "RESTRICT_UNMOUNT", 0, 3 },
74 [CCS_SAKURA_RESTRICT_PIVOT_ROOT] = { "RESTRICT_PIVOT_ROOT", 0, 3 },
75 [CCS_SAKURA_RESTRICT_AUTOBIND] = { "RESTRICT_AUTOBIND", 0, 1 },
76 [CCS_TOMOYO_MAX_ACCEPT_ENTRY] = { "MAX_ACCEPT_ENTRY", MAX_ACCEPT_ENTRY, INT_MAX },
77 [CCS_TOMOYO_MAX_GRANT_LOG] = { "MAX_GRANT_LOG", MAX_GRANT_LOG, INT_MAX },
78 [CCS_TOMOYO_MAX_REJECT_LOG] = { "MAX_REJECT_LOG", MAX_REJECT_LOG, INT_MAX },
79 [CCS_TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
80 [CCS_ALLOW_ENFORCE_GRACE] = { "ALLOW_ENFORCE_GRACE", 0, 1 },
81 };
82
83 struct profile {
84 unsigned int value[CCS_MAX_CONTROL_INDEX];
85 const struct path_info *comment;
86 };
87
88 static struct profile *profile_ptr[MAX_PROFILES];
89
90 /************************* UTILITY FUNCTIONS *************************/
91
92 #ifdef CONFIG_TOMOYO
93 static int __init TOMOYO_Quiet_Setup(char *str)
94 {
95 ccs_control_array[CCS_TOMOYO_VERBOSE].current_value = 0;
96 return 0;
97 }
98
99 __setup("TOMOYO_QUIET", TOMOYO_Quiet_Setup);
100 #endif
101
102 /* Am I root? */
103 static int isRoot(void)
104 {
105 return !current->uid && !current->euid;
106 }
107
108 /*
109 * Format string.
110 * Leading and trailing whitespaces are removed.
111 * Multiple whitespaces are packed into single space.
112 */
113 static void NormalizeLine(unsigned char *buffer)
114 {
115 unsigned char *sp = buffer, *dp = buffer;
116 int first = 1;
117 while (*sp && (*sp <= ' ' || *sp >= 127)) sp++;
118 while (*sp) {
119 if (!first) *dp++ = ' ';
120 first = 0;
121 while (*sp > ' ' && *sp < 127) *dp++ = *sp++;
122 while (*sp && (*sp <= ' ' || *sp >= 127)) sp++;
123 }
124 *dp = '\0';
125 }
126
127 /*
128 * Check whether the given filename follows the naming rules.
129 * Returns nonzero if follows, zero otherwise.
130 */
131 int IsCorrectPath(const char *filename, const int start_type, const int pattern_type, const int end_type, const char *function)
132 {
133 int contains_pattern = 0;
134 char c, d, e;
135 const char *original_filename = filename;
136 if (!filename) goto out;
137 c = *filename;
138 if (start_type == 1) { /* Must start with '/' */
139 if (c != '/') goto out;
140 } else if (start_type == -1) { /* Must not start with '/' */
141 if (c == '/') goto out;
142 }
143 if (c) c = * (strchr(filename, '\0') - 1);
144 if (end_type == 1) { /* Must end with '/' */
145 if (c != '/') goto out;
146 } else if (end_type == -1) { /* Must not end with '/' */
147 if (c == '/') goto out;
148 }
149 while ((c = *filename++) != '\0') {
150 if (c == '\\') {
151 switch ((c = *filename++)) {
152 case '\\': /* "\\" */
153 continue;
154 case '$': /* "\$" */
155 case '+': /* "\+" */
156 case '?': /* "\?" */
157 case '*': /* "\*" */
158 case '@': /* "\@" */
159 case 'x': /* "\x" */
160 case 'X': /* "\X" */
161 case 'a': /* "\a" */
162 case 'A': /* "\A" */
163 case '-': /* "\-" */
164 if (pattern_type == -1) break; /* Must not contain pattern */
165 contains_pattern = 1;
166 continue;
167 case '0': /* "\ooo" */
168 case '1':
169 case '2':
170 case '3':
171 if ((d = *filename++) >= '0' && d <= '7' && (e = *filename++) >= '0' && e <= '7') {
172 const unsigned char f =
173 (((unsigned char) (c - '0')) << 6) +
174 (((unsigned char) (d - '0')) << 3) +
175 (((unsigned char) (e - '0')));
176 if (f && (f <= ' ' || f >= 127)) continue; /* pattern is not \000 */
177 }
178 }
179 goto out;
180 } else if (c <= ' ' || c >= 127) {
181 goto out;
182 }
183 }
184 if (pattern_type == 1) { /* Must contain pattern */
185 if (!contains_pattern) goto out;
186 }
187 return 1;
188 out:
189 printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function, original_filename);
190 return 0;
191 }
192
193 /*
194 * Check whether the given domainname follows the naming rules.
195 * Returns nonzero if follows, zero otherwise.
196 */
197 int IsCorrectDomain(const unsigned char *domainname, const char *function)
198 {
199 unsigned char c, d, e;
200 const char *org_domainname = domainname;
201 if (!domainname || strncmp(domainname, ROOT_NAME, ROOT_NAME_LEN)) goto out;
202 domainname += ROOT_NAME_LEN;
203 if (!*domainname) return 1;
204 do {
205 if (*domainname++ != ' ') goto out;
206 if (*domainname++ != '/') goto out;
207 while ((c = *domainname) != '\0' && c != ' ') {
208 domainname++;
209 if (c == '\\') {
210 switch ((c = *domainname++)) {
211 case '\\': /* "\\" */
212 continue;
213 case '0': /* "\ooo" */
214 case '1':
215 case '2':
216 case '3':
217 if ((d = *domainname++) >= '0' && d <= '7' && (e = *domainname++) >= '0' && e <= '7') {
218 const unsigned char f =
219 (((unsigned char) (c - '0')) << 6) +
220 (((unsigned char) (d - '0')) << 3) +
221 (((unsigned char) (e - '0')));
222 if (f && (f <= ' ' || f >= 127)) continue; /* pattern is not \000 */
223 }
224 }
225 goto out;
226 } else if (c < ' ' || c >= 127) {
227 goto out;
228 }
229 }
230 } while (*domainname);
231 return 1;
232 out:
233 printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function, org_domainname);
234 return 0;
235 }
236
237 static int PathDepth(const char *pathname)
238 {
239 int i = 0;
240 if (pathname) {
241 char *ep = strchr(pathname, '\0');
242 if (pathname < ep--) {
243 if (*ep != '/') i++;
244 while (pathname <= ep) if (*ep-- == '/') i += 2;
245 }
246 }
247 return i;
248 }
249
250 static int const_part_length(const char *filename)
251 {
252 int len = 0;
253 if (filename) {
254 char c;
255 while ((c = *filename++) != '\0') {
256 if (c != '\\') { len++; continue; }
257 switch (c = *filename++) {
258 case '\\': /* "\\" */
259 len += 2; continue;
260 case '0': /* "\ooo" */
261 case '1':
262 case '2':
263 case '3':
264 if ((c = *filename++) >= '0' && c <= '7' && (c = *filename++) >= '0' && c <= '7') { len += 4; continue; }
265 }
266 break;
267 }
268 }
269 return len;
270 }
271
272 void fill_path_info(struct path_info *ptr)
273 {
274 const char *name = ptr->name;
275 const int len = strlen(name);
276 ptr->total_len = len;
277 ptr->const_len = const_part_length(name);
278 ptr->is_dir = len && (name[len - 1] == '/');
279 ptr->is_patterned = (ptr->const_len < len);
280 ptr->hash = full_name_hash(name, len);
281 ptr->depth = PathDepth(name);
282 }
283
284 static int FileMatchesToPattern2(const char *filename, const char *filename_end, const char *pattern, const char *pattern_end)
285 {
286 while (filename < filename_end && pattern < pattern_end) {
287 if (*pattern != '\\') {
288 if (*filename++ != *pattern++) return 0;
289 } else {
290 char c = *filename;
291 pattern++;
292 switch (*pattern) {
293 case '?':
294 if (c == '/') {
295 return 0;
296 } else if (c == '\\') {
297 if ((c = filename[1]) == '\\') {
298 filename++; /* safe because filename is \\ */
299 } else if (c >= '0' && c <= '3' && (c = filename[2]) >= '0' && c <= '7' && (c = filename[3]) >= '0' && c <= '7') {
300 filename += 3; /* safe because filename is \ooo */
301 } else {
302 return 0;
303 }
304 }
305 break;
306 case '\\':
307 if (c != '\\') return 0;
308 if (*++filename != '\\') return 0; /* safe because *filename != '\0' */
309 break;
310 case '+':
311 if (c < '0' || c > '9') return 0;
312 break;
313 case 'x':
314 if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) return 0;
315 break;
316 case 'a':
317 if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) return 0;
318 break;
319 case '0':
320 case '1':
321 case '2':
322 case '3':
323 if (c == '\\' && (c = filename[1]) >= '0' && c <= '3' && c == *pattern
324 && (c = filename[2]) >= '0' && c <= '7' && c == pattern[1]
325 && (c = filename[3]) >= '0' && c <= '7' && c == pattern[2]) {
326 filename += 3; /* safe because filename is \ooo */
327 pattern += 2; /* safe because pattern is \ooo */
328 break;
329 }
330 return 0; /* Not matched. */
331 case '*':
332 case '@':
333 {
334 int i;
335 for (i = 0; i <= filename_end - filename; i++) {
336 if (FileMatchesToPattern2(filename + i, filename_end, pattern + 1, pattern_end)) return 1;
337 if ((c = filename[i]) == '.' && *pattern == '@') break;
338 if (c == '\\') {
339 if ((c = filename[i + 1]) == '\\') {
340 i++; /* safe because filename is \\ */
341 } else if (c >= '0' && c <= '3' && (c = filename[i + 2]) >= '0' && c <= '7' && (c = filename[i + 3]) >= '0' && c <= '7') {
342 i += 3; /* safe because filename is \ooo */
343 } else {
344 break; /* Bad pattern. */
345 }
346 }
347 }
348 return 0; /* Not matched. */
349 }
350 default:
351 {
352 int i, j = 0;
353 if ((c = *pattern) == '$') {
354 while ((c = filename[j]) >= '0' && c <= '9') j++;
355 } else if (c == 'X') {
356 while (((c = filename[j]) >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) j++;
357 } else if (c == 'A') {
358 while (((c = filename[j]) >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) j++;
359 }
360 for (i = 1; i <= j; i++) {
361 if (FileMatchesToPattern2(filename + i, filename_end, pattern + 1, pattern_end)) return 1;
362 }
363 }
364 return 0; /* Not matched or bad pattern. */
365 }
366 filename++; /* safe because *filename != '\0' */
367 pattern++; /* safe because *pattern != '\0' */
368 }
369 }
370 while (*pattern == '\\' && (*(pattern + 1) == '*' || *(pattern + 1) == '@')) pattern += 2;
371 return (filename == filename_end && pattern == pattern_end);
372 }
373
374 static int FileMatchesToPattern(const char *filename, const char *filename_end, const char *pattern, const char *pattern_end)
375 {
376 const char *pattern_start = pattern;
377 int first = 1;
378 int result;
379 while (pattern < pattern_end - 1) {
380 if (*pattern++ != '\\' || *pattern++ != '-') continue;
381 result = FileMatchesToPattern2(filename, filename_end, pattern_start, pattern - 2);
382 if (first) result = !result;
383 if (result) return 0;
384 first = 0;
385 pattern_start = pattern;
386 }
387 result = FileMatchesToPattern2(filename, filename_end, pattern_start, pattern_end);
388 return first ? result : !result;
389 }
390
391 /*
392 * Check whether the given pathname matches to the given pattern.
393 * Returns nonzero if matches, zero otherwise.
394 *
395 * The following patterns are available.
396 * \\ \ itself.
397 * \ooo Octal representation of a byte.
398 * \* More than or equals to 0 character other than '/'.
399 * \@ More than or equals to 0 character other than '/' or '.'.
400 * \? 1 byte character other than '/'.
401 * \$ More than or equals to 1 decimal digit.
402 * \+ 1 decimal digit.
403 * \X More than or equals to 1 hexadecimal digit.
404 * \x 1 hexadecimal digit.
405 * \A More than or equals to 1 alphabet character.
406 * \a 1 alphabet character.
407 * \- Subtraction operator.
408 */
409
410 int PathMatchesToPattern(const struct path_info *pathname0, const struct path_info *pattern0)
411 {
412 /* if (!pathname || !pattern) return 0; */
413 const char *pathname = pathname0->name, *pattern = pattern0->name;
414 const int len = pattern0->const_len;
415 if (!pattern0->is_patterned) return !pathcmp(pathname0, pattern0);
416 if (pathname0->depth != pattern0->depth) return 0;
417 if (strncmp(pathname, pattern, len)) return 0;
418 pathname += len; pattern += len;
419 while (*pathname && *pattern) {
420 const char *pathname_delimiter = strchr(pathname, '/'), *pattern_delimiter = strchr(pattern, '/');
421 if (!pathname_delimiter) pathname_delimiter = strchr(pathname, '\0');
422 if (!pattern_delimiter) pattern_delimiter = strchr(pattern, '\0');
423 if (!FileMatchesToPattern(pathname, pathname_delimiter, pattern, pattern_delimiter)) return 0;
424 pathname = *pathname_delimiter ? pathname_delimiter + 1 : pathname_delimiter;
425 pattern = *pattern_delimiter ? pattern_delimiter + 1 : pattern_delimiter;
426 }
427 while (*pattern == '\\' && (*(pattern + 1) == '*' || *(pattern + 1) == '@')) pattern += 2;
428 return (!*pathname && !*pattern);
429 }
430
431 /*
432 * Transactional printf() to struct io_buffer structure.
433 * snprintf() will truncate, but io_printf() won't.
434 * Returns zero on success, nonzero otherwise.
435 */
436 int io_printf(struct io_buffer *head, const char *fmt, ...)
437 {
438 va_list args;
439 int len, pos = head->read_avail, size = head->readbuf_size - pos;
440 if (size <= 0) return -ENOMEM;
441 va_start(args, fmt);
442 len = vsnprintf(head->read_buf + pos, size, fmt, args);
443 va_end(args);
444 if (pos + len >= head->readbuf_size) return -ENOMEM;
445 head->read_avail += len;
446 return 0;
447 }
448
449 /*
450 * Get realpath() of current process.
451 * This function uses ccs_alloc(), so caller must ccs_free() if this function didn't return NULL.
452 */
453 const char *GetEXE(void)
454 {
455 if (current->mm) {
456 struct vm_area_struct *vma = current->mm->mmap;
457 while (vma) {
458 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
459 return realpath_from_dentry(vma->vm_file->f_dentry, vma->vm_file->f_vfsmnt);
460 }
461 vma = vma->vm_next;
462 }
463 }
464 return NULL;
465 }
466
467 const char *GetMSG(const int is_enforce)
468 {
469 if (is_enforce) return "ERROR"; else return "WARNING";
470 }
471
472 /************************* DOMAIN POLICY HANDLER *************************/
473
474 /* Check whether the given access control is enabled. */
475 unsigned int CheckCCSFlags(const unsigned int index)
476 {
477 const u8 profile = current->domain_info->profile;
478 return sbin_init_started && index < CCS_MAX_CONTROL_INDEX
479 #if MAX_PROFILES != 256
480 && profile < MAX_PROFILES
481 #endif
482 && profile_ptr[profile] ? profile_ptr[profile]->value[index] : 0;
483 }
484 EXPORT_SYMBOL(CheckCCSFlags);
485
486 unsigned int TomoyoVerboseMode(void)
487 {
488 return CheckCCSFlags(CCS_TOMOYO_VERBOSE);
489 }
490
491 /* Check whether the given access control is enforce mode. */
492 unsigned int CheckCCSEnforce(const unsigned int index)
493 {
494 return CheckCCSFlags(index) == 3;
495 }
496 EXPORT_SYMBOL(CheckCCSEnforce);
497
498 /* Check whether the given access control is accept mode. */
499 unsigned int CheckCCSAccept(const unsigned int index)
500 {
501 return CheckCCSFlags(index) == 1;
502 }
503 EXPORT_SYMBOL(CheckCCSAccept);
504
505 static struct profile *FindOrAssignNewProfile(const unsigned int profile)
506 {
507 static DECLARE_MUTEX(profile_lock);
508 struct profile *ptr = NULL;
509 down(&profile_lock);
510 if (profile < MAX_PROFILES && (ptr = profile_ptr[profile]) == NULL) {
511 if ((ptr = alloc_element(sizeof(*ptr))) != NULL) {
512 int i;
513 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) ptr->value[i] = ccs_control_array[i].current_value;
514 mb(); /* Instead of using spinlock. */
515 profile_ptr[profile] = ptr;
516 }
517 }
518 up(&profile_lock);
519 return ptr;
520 }
521
522 static int SetStatus(struct io_buffer *head)
523 {
524 char *data = head->write_buf;
525 unsigned int i, value;
526 char *cp;
527 struct profile *profile;
528 if (!isRoot()) return -EPERM;
529 i = simple_strtoul(data, &cp, 10);
530 if (data != cp) {
531 if (*cp != '-') return -EINVAL;
532 data= cp + 1;
533 }
534 profile = FindOrAssignNewProfile(i);
535 if (!profile) return -EINVAL;
536 cp = strchr(data, '=');
537 if (!cp) return -EINVAL;
538 *cp = '\0';
539 UpdateCounter(CCS_UPDATES_COUNTER_STATUS);
540 if (strcmp(data, ccs_control_array[CCS_PROFILE_COMMENT].keyword) == 0) {
541 profile->comment = SaveName(cp + 1);
542 return 0;
543 }
544 if (sscanf(cp + 1, "%u", &value) != 1) return -EINVAL;
545 if (strncmp(data, KEYWORD_MAC_FOR_CAPABILITY, KEYWORD_MAC_FOR_CAPABILITY_LEN) == 0) {
546 return SetCapabilityStatus(data + KEYWORD_MAC_FOR_CAPABILITY_LEN, value, i);
547 }
548 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {
549 if (strcmp(data, ccs_control_array[i].keyword)) continue;
550 if (value > ccs_control_array[i].max_value) value = ccs_control_array[i].max_value;
551 profile->value[i] = value;
552 return 0;
553 }
554 return -EINVAL;
555 }
556
557 static int ReadStatus(struct io_buffer *head)
558 {
559 if (!head->read_eof) {
560 if (!isRoot()) return -EPERM;
561 if (!head->read_var2) {
562 int step;
563 for (step = head->read_step; step < MAX_PROFILES * CCS_MAX_CONTROL_INDEX; step++) {
564 const int i = step / CCS_MAX_CONTROL_INDEX, j = step % CCS_MAX_CONTROL_INDEX;
565 const struct profile *profile = profile_ptr[i];
566 head->read_step = step;
567 if (!profile) continue;
568 switch (j) {
569 case -1: /* Dummy */
570 #ifndef CONFIG_TOMOYO
571 case CCS_TOMOYO_MAX_ACCEPT_ENTRY:
572 case CCS_TOMOYO_MAX_GRANT_LOG:
573 case CCS_TOMOYO_MAX_REJECT_LOG:
574 case CCS_TOMOYO_VERBOSE:
575 #endif
576 continue;
577 }
578 if (j == CCS_PROFILE_COMMENT) {
579 if (io_printf(head, "%u-%s=%s\n", i, ccs_control_array[CCS_PROFILE_COMMENT].keyword, profile->comment ? profile->comment->name : "")) break;
580 } else {
581 if (io_printf(head, "%u-%s=%u\n", i, ccs_control_array[j].keyword, profile->value[j])) break;
582 }
583 }
584 if (step == MAX_PROFILES * CCS_MAX_CONTROL_INDEX) {
585 head->read_var2 = "";
586 head->read_step = 0;
587 }
588 }
589 if (head->read_var2) {
590 if (ReadCapabilityStatus(head) == 0) head->read_eof = 1;
591 }
592 }
593 return 0;
594 }
595
596 /************************* POLICY MANAGER HANDLER *************************/
597
598 struct policy_manager_entry {
599 struct policy_manager_entry *next;
600 const struct path_info *manager;
601 u8 is_domain;
602 u8 is_deleted;
603 };
604
605 static struct policy_manager_entry *policy_manager_list = NULL;
606
607 static int AddManagerEntry(const char *manager, u8 is_delete)
608 {
609 struct policy_manager_entry *new_entry, *ptr;
610 static DECLARE_MUTEX(lock);
611 const struct path_info *saved_manager;
612 int error = -ENOMEM;
613 u8 is_domain = 0;
614 if (!isRoot()) return -EPERM;
615 if (IsDomainDef(manager)) {
616 if (!IsCorrectDomain(manager, __FUNCTION__)) return -EINVAL;
617 is_domain = 1;
618 } else {
619 if (!IsCorrectPath(manager, 1, -1, -1, __FUNCTION__)) return -EINVAL;
620 }
621 if ((saved_manager = SaveName(manager)) == NULL) return -ENOMEM;
622 down(&lock);
623 for (ptr = policy_manager_list; ptr; ptr = ptr->next) {
624 if (ptr->manager == saved_manager) {
625 ptr->is_deleted = is_delete;
626 error = 0;
627 goto out;
628 }
629 }
630 if (is_delete) {
631 error = -ENOENT;
632 goto out;
633 }
634 if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;
635 new_entry->manager = saved_manager;
636 new_entry->is_domain = is_domain;
637 mb(); /* Instead of using spinlock. */
638 if ((ptr = policy_manager_list) != NULL) {
639 while (ptr->next) ptr = ptr->next; ptr->next = new_entry;
640 } else {
641 policy_manager_list = new_entry;
642 }
643 error = 0;
644 out:
645 up(&lock);
646 if (!error) UpdateCounter(CCS_UPDATES_COUNTER_MANAGER);
647 return error;
648 }
649
650 static int AddManagerPolicy(struct io_buffer *head)
651 {
652 const char *data = head->write_buf;
653 int is_delete = 0;
654 if (!isRoot()) return -EPERM;
655 if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {
656 data += KEYWORD_DELETE_LEN;
657 is_delete = 1;
658 }
659 return AddManagerEntry(data, is_delete);
660 }
661
662 static int ReadManagerPolicy(struct io_buffer *head)
663 {
664 if (!head->read_eof) {
665 struct policy_manager_entry *ptr = head->read_var2;
666 if (!isRoot()) return -EPERM;
667 if (!ptr) ptr = policy_manager_list;
668 while (ptr) {
669 head->read_var2 = ptr;
670 if (!ptr->is_deleted && io_printf(head, "%s\n", ptr->manager->name)) break;
671 ptr = ptr->next;
672 }
673 if (!ptr) head->read_eof = 1;
674 }
675 return 0;
676 }
677
678 /* Check whether the current process is a policy manager. */
679 static int IsPolicyManager(void)
680 {
681 struct policy_manager_entry *ptr;
682 const char *exe;
683 const struct path_info *domainname = current->domain_info->domainname;
684 if (!sbin_init_started) return 1;
685 for (ptr = policy_manager_list; ptr; ptr = ptr->next) {
686 if (!ptr->is_deleted && ptr->is_domain && !pathcmp(domainname, ptr->manager)) return 1;
687 }
688 if ((exe = GetEXE()) == NULL) return 0;
689 for (ptr = policy_manager_list; ptr; ptr = ptr->next) {
690 if (!ptr->is_deleted && !ptr->is_domain && !strcmp(exe, ptr->manager->name)) break;
691 }
692 if (!ptr) { /* Reduce error messages. */
693 static pid_t last_pid = 0;
694 const pid_t pid = current->pid;
695 if (last_pid != pid) {
696 printk("%s ( %s ) is not permitted to update policies.\n", domainname->name, exe);
697 last_pid = pid;
698 }
699 }
700 ccs_free(exe);
701 return ptr ? 1 : 0;
702 }
703
704 #ifdef CONFIG_TOMOYO
705
706 /************************* DOMAIN POLICY HANDLER *************************/
707
708 static int AddDomainPolicy(struct io_buffer *head)
709 {
710 char *data = head->write_buf;
711 struct domain_info *domain = head->write_var1;
712 int is_delete = 0, is_select = 0, is_undelete = 0;
713 unsigned int profile;
714 if (!isRoot()) return -EPERM;
715 if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {
716 data += KEYWORD_DELETE_LEN;
717 is_delete = 1;
718 } else if (strncmp(data, KEYWORD_SELECT, KEYWORD_SELECT_LEN) == 0) {
719 data += KEYWORD_SELECT_LEN;
720 is_select = 1;
721 } else if (strncmp(data, KEYWORD_UNDELETE, KEYWORD_UNDELETE_LEN) == 0) {
722 data += KEYWORD_UNDELETE_LEN;
723 is_undelete = 1;
724 }
725 UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
726 if (IsDomainDef(data)) {
727 if (is_delete) {
728 DeleteDomain(data);
729 domain = NULL;
730 } else if (is_select) {
731 domain = FindDomain(data);
732 } else if (is_undelete) {
733 domain = UndeleteDomain(data);
734 } else {
735 domain = FindOrAssignNewDomain(data, 0);
736 }
737 head->write_var1 = domain;
738 return 0;
739 }
740 if (!domain) return -EINVAL;
741 if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1 && profile < MAX_PROFILES) {
742 if (profile_ptr[profile] || !sbin_init_started) domain->profile = (u8) profile;
743 } else if (strncmp(data, KEYWORD_ALLOW_CAPABILITY, KEYWORD_ALLOW_CAPABILITY_LEN) == 0) {
744 return AddCapabilityPolicy(data + KEYWORD_ALLOW_CAPABILITY_LEN, domain, is_delete);
745 } else if (strncmp(data, KEYWORD_ALLOW_NETWORK, KEYWORD_ALLOW_NETWORK_LEN) == 0) {
746 return AddNetworkPolicy(data + KEYWORD_ALLOW_NETWORK_LEN, domain, is_delete);
747 } else if (strncmp(data, KEYWORD_ALLOW_SIGNAL, KEYWORD_ALLOW_SIGNAL_LEN) == 0) {
748 return AddSignalPolicy(data + KEYWORD_ALLOW_SIGNAL_LEN, domain, is_delete);
749 } else if (strncmp(data, KEYWORD_ALLOW_ARGV0, KEYWORD_ALLOW_ARGV0_LEN) == 0) {
750 return AddArgv0Policy(data + KEYWORD_ALLOW_ARGV0_LEN, domain, is_delete);
751 } else {
752 return AddFilePolicy(data, domain, is_delete);
753 }
754 return -EINVAL;
755 }
756
757 static int ReadDomainPolicy(struct io_buffer *head)
758 {
759 if (!head->read_eof) {
760 struct domain_info *domain = head->read_var1;
761 switch (head->read_step) {
762 case 0: break;
763 case 1: goto step1;
764 case 2: goto step2;
765 case 3: goto step3;
766 default: return -EINVAL;
767 }
768 if (!isRoot()) return -EPERM;
769 for (domain = &KERNEL_DOMAIN; domain; domain = domain->next) {
770 struct acl_info *ptr;
771 if (domain->is_deleted) continue;
772 head->read_var1 = domain;
773 head->read_var2 = NULL; head->read_step = 1;
774 step1:
775 if (io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n%s\n", domain->domainname->name, domain->profile, domain->quota_warned ? "quota_exceeded\n" : "")) break;
776 head->read_var2 = domain->first_acl_ptr; head->read_step = 2;
777 step2:
778 for (ptr = head->read_var2; ptr; ptr = ptr->next) {
779 const u8 acl_type = ptr->type;
780 const int pos = head->read_avail;
781 head->read_var2 = ptr;
782 if (ptr->is_deleted) continue;
783 if (acl_type == TYPE_FILE_ACL) {
784 struct file_acl_record *ptr2 = (struct file_acl_record *) ptr;
785 const unsigned char b = ptr2->u_is_group;
786 if (io_printf(head, "%d %s%s", ptr2->perm,
787 b ? "@" : "",
788 b ? ptr2->u.group->group_name->name : ptr2->u.filename->name)
789 || DumpCondition(head, ptr->cond)) {
790 head->read_avail = pos; break;
791 }
792 } else if (acl_type == TYPE_ARGV0_ACL) {
793 struct argv0_acl_record *ptr2 = (struct argv0_acl_record *) ptr;
794 if (io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
795 ptr2->filename->name, ptr2->argv0->name) ||
796 DumpCondition(head, ptr->cond)) {
797 head->read_avail = pos; break;
798 }
799 } else if (acl_type == TYPE_CAPABILITY_ACL) {
800 struct capability_acl_record *ptr2 = (struct capability_acl_record *) ptr;
801 if (io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s", capability2keyword(ptr2->capability)) ||
802 DumpCondition(head, ptr->cond)) {
803 head->read_avail = pos; break;
804 }
805 } else if (acl_type == TYPE_IP_NETWORK_ACL) {
806 struct ip_network_acl_record *ptr2 = (struct ip_network_acl_record *) ptr;
807 if (io_printf(head, KEYWORD_ALLOW_NETWORK "%s ", network2keyword(ptr2->operation_type))) break;
808 switch (ptr2->record_type) {
809 case IP_RECORD_TYPE_ADDRESS_GROUP:
810 if (io_printf(head, "@%s", ptr2->u.group->group_name->name)) goto print_ip_record_out;
811 break;
812 case IP_RECORD_TYPE_IPv4:
813 {
814 const u32 min_address = ptr2->u.ipv4.min, max_address = ptr2->u.ipv4.max;
815 if (io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address))) goto print_ip_record_out;
816 if (min_address != max_address && io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address))) goto print_ip_record_out;
817 }
818 break;
819 case IP_RECORD_TYPE_IPv6:
820 {
821 char buf[64];
822 const u16 *min_address = ptr2->u.ipv6.min, *max_address = ptr2->u.ipv6.max;
823 print_ipv6(buf, sizeof(buf), min_address);
824 if (io_printf(head, "%s", buf)) goto print_ip_record_out;
825 if (memcmp(min_address, max_address, 16)) {
826 print_ipv6(buf, sizeof(buf), max_address);
827 if (io_printf(head, "-%s", buf)) goto print_ip_record_out;
828 }
829 }
830 break;
831 }
832 {
833 const u16 min_port = ptr2->min_port, max_port = ptr2->max_port;
834 if (io_printf(head, " %u", min_port)) goto print_ip_record_out;
835 if (min_port != max_port && io_printf(head, "-%u", max_port)) goto print_ip_record_out;
836 }
837 if (DumpCondition(head, ptr->cond)) {
838 print_ip_record_out: ;
839 head->read_avail = pos; break;
840 }
841 } else if (acl_type == TYPE_SIGNAL_ACL) {
842 struct signal_acl_record *ptr2 = (struct signal_acl_record *) ptr;
843 if (io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s", ptr2->sig, ptr2->domainname->name) ||
844 DumpCondition(head, ptr->cond)) {
845 head->read_avail = pos; break;
846 }
847 } else {
848 const char *keyword = acltype2keyword(acl_type);
849 if (keyword) {
850 if (acltype2paths(acl_type) == 2) {
851 struct double_acl_record *ptr2 = (struct double_acl_record *) ptr;
852 const u8 b0 = ptr2->u1_is_group, b1 = ptr2->u2_is_group;
853 if (io_printf(head, "allow_%s %s%s %s%s", keyword,
854 b0 ? "@" : "", b0 ? ptr2->u1.group1->group_name->name : ptr2->u1.filename1->name,
855 b1 ? "@" : "", b1 ? ptr2->u2.group2->group_name->name : ptr2->u2.filename2->name)
856 || DumpCondition(head, ptr->cond)) {
857 head->read_avail = pos; break;
858 }
859 } else {
860 struct single_acl_record *ptr2 = (struct single_acl_record *) ptr;
861 const u8 b = ptr2->u_is_group;
862 if (io_printf(head, "allow_%s %s%s", keyword,
863 b ? "@" : "", b ? ptr2->u.group->group_name->name : ptr2->u.filename->name)
864 || DumpCondition(head, ptr->cond)) {
865 head->read_avail = pos; break;
866 }
867 }
868 }
869 }
870 }
871 if (ptr) break;
872 head->read_var2 = NULL; head->read_step = 3;
873 step3:
874 if (io_printf(head, "\n")) break;
875 }
876 if (!domain) head->read_eof = 1;
877 }
878 return 0;
879 }
880
881 static int ReadDomainProfile(struct io_buffer *head)
882 {
883 if (!head->read_eof) {
884 struct domain_info *domain;
885 if (head->read_step == 0) {
886 head->read_var1 = &KERNEL_DOMAIN;
887 head->read_step = 1;
888 }
889 if (!isRoot()) return -EPERM;
890 for (domain = head->read_var1; domain; domain = domain->next) {
891 if (domain->is_deleted) continue;
892 head->read_var1 = domain;
893 if (io_printf(head, "%u %s\n", domain->profile, domain->domainname->name)) break;
894 }
895 if (!domain) head->read_eof = 1;
896 }
897 return 0;
898 }
899
900 static int WritePID(struct io_buffer *head)
901 {
902 head->read_step = (int) simple_strtoul(head->write_buf, NULL, 10);
903 head->read_eof = 0;
904 return 0;
905 }
906
907 static int ReadPID(struct io_buffer *head)
908 {
909 if (head->read_avail == 0 && !head->read_eof) {
910 const int pid = head->read_step;
911 struct task_struct *p;
912 struct domain_info *domain = NULL;
913 /***** CRITICAL SECTION START *****/
914 read_lock(&tasklist_lock);
915 p = find_task_by_pid(pid);
916 if (p) domain = p->domain_info;
917 read_unlock(&tasklist_lock);
918 /***** CRITICAL SECTION END *****/
919 if (domain) io_printf(head, "%d %u %s", pid, domain->profile, domain->domainname->name);
920 head->read_eof = 1;
921 }
922 return 0;
923 }
924
925 static int UpdateDomainProfile(struct io_buffer *head)
926 {
927 char *data = head->write_buf;
928 char *cp = strchr(data, ' ');
929 struct domain_info *domain;
930 unsigned int profile;
931 if (!isRoot()) return -EPERM;
932 if (!cp) return -EINVAL;
933 *cp = '\0';
934 domain = FindDomain(cp + 1);
935 profile = simple_strtoul(data, NULL, 10);
936 if (domain && profile < MAX_PROFILES && (profile_ptr[profile] || !sbin_init_started)) domain->profile = (u8) profile;
937 UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
938 return 0;
939 }
940
941 #endif
942
943 /************************* EXCEPTION POLICY HANDLER *************************/
944
945 #ifdef CONFIG_TOMOYO
946
947 static int AddExceptionPolicy(struct io_buffer *head)
948 {
949 char *data = head->write_buf;
950 int is_delete = 0;
951 if (!isRoot()) return -EPERM;
952 UpdateCounter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);
953 if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {
954 data += KEYWORD_DELETE_LEN;
955 is_delete = 1;
956 }
957 if (strncmp(data, KEYWORD_KEEP_DOMAIN, KEYWORD_KEEP_DOMAIN_LEN) == 0) {
958 return AddDomainKeeperPolicy(data + KEYWORD_KEEP_DOMAIN_LEN, 0, is_delete);
959 } else if (strncmp(data, KEYWORD_NO_KEEP_DOMAIN, KEYWORD_NO_KEEP_DOMAIN_LEN) == 0) {
960 return AddDomainKeeperPolicy(data + KEYWORD_NO_KEEP_DOMAIN_LEN, 1, is_delete);
961 } else if (strncmp(data, KEYWORD_INITIALIZE_DOMAIN, KEYWORD_INITIALIZE_DOMAIN_LEN) == 0) {
962 return AddDomainInitializerPolicy(data + KEYWORD_INITIALIZE_DOMAIN_LEN, 0, is_delete, 0);
963 } else if (strncmp(data, KEYWORD_NO_INITIALIZE_DOMAIN, KEYWORD_NO_INITIALIZE_DOMAIN_LEN) == 0) {
964 return AddDomainInitializerPolicy(data + KEYWORD_NO_INITIALIZE_DOMAIN_LEN, 1, is_delete, 0);
965 } else if (strncmp(data, KEYWORD_INITIALIZER, KEYWORD_INITIALIZER_LEN) == 0) {
966 return AddDomainInitializerPolicy(data + KEYWORD_INITIALIZER_LEN, 0, is_delete, 1);
967 } else if (strncmp(data, KEYWORD_NO_INITIALIZER, KEYWORD_NO_INITIALIZER_LEN) == 0) {
968 return AddDomainInitializerPolicy(data + KEYWORD_NO_INITIALIZER_LEN, 1, is_delete, 1);
969 } else if (strncmp(data, KEYWORD_ALIAS, KEYWORD_ALIAS_LEN) == 0) {
970 return AddAliasPolicy(data + KEYWORD_ALIAS_LEN, is_delete);
971 } else if (strncmp(data, KEYWORD_AGGREGATOR, KEYWORD_AGGREGATOR_LEN) == 0) {
972 return AddAggregatorPolicy(data + KEYWORD_AGGREGATOR_LEN, is_delete);
973 } else if (strncmp(data, KEYWORD_ALLOW_READ, KEYWORD_ALLOW_READ_LEN) == 0) {
974 return AddGloballyReadablePolicy(data + KEYWORD_ALLOW_READ_LEN, is_delete);
975 } else if (strncmp(data, KEYWORD_FILE_PATTERN, KEYWORD_FILE_PATTERN_LEN) == 0) {
976 return AddPatternPolicy(data + KEYWORD_FILE_PATTERN_LEN, is_delete);
977 } else if (strncmp(data, KEYWORD_PATH_GROUP, KEYWORD_PATH_GROUP_LEN) == 0) {
978 return AddGroupPolicy(data + KEYWORD_PATH_GROUP_LEN, is_delete);
979 } else if (strncmp(data, KEYWORD_DENY_REWRITE, KEYWORD_DENY_REWRITE_LEN) == 0) {
980 return AddNoRewritePolicy(data + KEYWORD_DENY_REWRITE_LEN, is_delete);
981 } else if (strncmp(data, KEYWORD_ADDRESS_GROUP, KEYWORD_ADDRESS_GROUP_LEN) == 0) {
982 return AddAddressGroupPolicy(data + KEYWORD_ADDRESS_GROUP_LEN, is_delete);
983 }
984 return -EINVAL;
985 }
986
987 static int ReadExceptionPolicy(struct io_buffer *head)
988 {
989 if (!head->read_eof) {
990 switch (head->read_step) {
991 case 0:
992 if (!isRoot()) return -EPERM;
993 head->read_var2 = NULL; head->read_step = 1;
994 case 1:
995 if (ReadDomainKeeperPolicy(head)) break;
996 head->read_var2 = NULL; head->read_step = 2;
997 case 2:
998 if (ReadGloballyReadablePolicy(head)) break;
999 head->read_var2 = NULL; head->read_step = 3;
1000 case 3:
1001 if (ReadDomainInitializerPolicy(head)) break;
1002 head->read_var2 = NULL; head->read_step = 4;
1003 case 4:
1004 if (ReadAliasPolicy(head)) break;
1005 head->read_var2 = NULL; head->read_step = 5;
1006 case 5:
1007 if (ReadAggregatorPolicy(head)) break;
1008 head->read_var2 = NULL; head->read_step = 6;
1009 case 6:
1010 if (ReadPatternPolicy(head)) break;
1011 head->read_var2 = NULL; head->read_step = 7;
1012 case 7:
1013 if (ReadNoRewritePolicy(head)) break;
1014 head->read_var2 = NULL; head->read_step = 8;
1015 case 8:
1016 if (ReadGroupPolicy(head)) break;
1017 head->read_var1 = head->read_var2 = NULL; head->read_step = 9;
1018 case 9:
1019 if (ReadAddressGroupPolicy(head)) break;
1020 head->read_eof = 1;
1021 break;
1022 default:
1023 return -EINVAL;
1024 }
1025 }
1026 return 0;
1027 }
1028
1029 #endif
1030
1031 /************************* SYSTEM POLICY HANDLER *************************/
1032
1033 #ifdef CONFIG_SAKURA
1034
1035 static int AddSystemPolicy(struct io_buffer *head)
1036 {
1037 char *data = head->write_buf;
1038 int is_delete = 0;
1039 if (!isRoot()) return -EPERM;
1040 UpdateCounter(CCS_UPDATES_COUNTER_SYSTEM_POLICY);
1041 if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) {
1042 data += KEYWORD_DELETE_LEN;
1043 is_delete = 1;
1044 }
1045 if (strncmp(data, KEYWORD_ALLOW_MOUNT, KEYWORD_ALLOW_MOUNT_LEN) == 0)
1046 return AddMountPolicy(data + KEYWORD_ALLOW_MOUNT_LEN, is_delete);
1047 if (strncmp(data, KEYWORD_DENY_UNMOUNT, KEYWORD_DENY_UNMOUNT_LEN) == 0)
1048 return AddNoUmountPolicy(data + KEYWORD_DENY_UNMOUNT_LEN, is_delete);
1049 if (strncmp(data, KEYWORD_ALLOW_CHROOT, KEYWORD_ALLOW_CHROOT_LEN) == 0)
1050 return AddChrootPolicy(data + KEYWORD_ALLOW_CHROOT_LEN, is_delete);
1051 if (strncmp(data, KEYWORD_ALLOW_PIVOT_ROOT, KEYWORD_ALLOW_PIVOT_ROOT_LEN) == 0)
1052 return AddPivotRootPolicy(data + KEYWORD_ALLOW_PIVOT_ROOT_LEN, is_delete);
1053 if (strncmp(data, KEYWORD_DENY_AUTOBIND, KEYWORD_DENY_AUTOBIND_LEN) == 0)
1054 return AddReservedPortPolicy(data + KEYWORD_DENY_AUTOBIND_LEN, is_delete);
1055 return -EINVAL;
1056 }
1057
1058 static int ReadSystemPolicy(struct io_buffer *head)
1059 {
1060 if (!head->read_eof) {
1061 switch (head->read_step) {
1062 case 0:
1063 if (!isRoot()) return -EPERM;
1064 head->read_var2 = NULL; head->read_step = 1;
1065 case 1:
1066 if (ReadMountPolicy(head)) break;
1067 head->read_var2 = NULL; head->read_step = 2;
1068 case 2:
1069 if (ReadNoUmountPolicy(head)) break;
1070 head->read_var2 = NULL; head->read_step = 3;
1071 case 3:
1072 if (ReadChrootPolicy(head)) break;
1073 head->read_var2 = NULL; head->read_step = 4;
1074 case 4:
1075 if (ReadPivotRootPolicy(head)) break;
1076 head->read_var2 = NULL; head->read_step = 5;
1077 case 5:
1078 if (ReadReservedPortPolicy(head)) break;
1079 head->read_eof = 1;
1080 break;
1081 default:
1082 return -EINVAL;
1083 }
1084 }
1085 return 0;
1086 }
1087
1088 #endif
1089
1090 /************************* POLICY LOADER *************************/
1091
1092 static int profile_loaded = 0;
1093
1094 static const char *ccs_loader = NULL;
1095
1096 static int __init CCS_loader_Setup(char *str)
1097 {
1098 ccs_loader = str;
1099 return 0;
1100 }
1101
1102 __setup("CCS_loader=", CCS_loader_Setup);
1103
1104 void CCS_LoadPolicy(const char *filename)
1105 {
1106 if (sbin_init_started) return;
1107 /*
1108 * Check filename is /sbin/init or /sbin/ccs-start .
1109 * /sbin/ccs-start is a dummy filename in case where /sbin/init can't be passed.
1110 * You can create /sbin/ccs-start by "ln -s /bin/true /sbin/ccs-start", for
1111 * only the pathname is needed to activate Mandatory Access Control.
1112 */
1113 if (strcmp(filename, "/sbin/init") != 0 && strcmp(filename, "/sbin/ccs-start") != 0) return;
1114 /*
1115 * Don't activate MAC if the path given by 'CCS_loader=' option doesn't exist.
1116 * If initrd.img includes /sbin/init but real-root-dev has not mounted on / yet,
1117 * activating MAC will block the system since policies are not loaded yet.
1118 * So let do_execve() call this function everytime.
1119 */
1120 {
1121 struct nameidata nd;
1122 if (!ccs_loader) ccs_loader = "/sbin/ccs-init";
1123 if (path_lookup(ccs_loader, lookup_flags, &nd)) {
1124 printk("Not activating Mandatory Access Control now since %s doesn't exist.\n", ccs_loader);
1125 return;
1126 }
1127 path_release(&nd);
1128 }
1129 #ifdef CONFIG_SAKURA
1130 printk("SAKURA: 1.5.0-pre 2007/08/09\n");
1131 #endif
1132 #ifdef CONFIG_TOMOYO
1133 printk("TOMOYO: 1.5.0-pre 2007/08/09\n");
1134 #endif
1135 if (!profile_loaded) {
1136 char *argv[2], *envp[3];
1137 printk("Calling %s to load policy. Please wait.\n", ccs_loader);
1138 argv[0] = (char *) ccs_loader;
1139 argv[1] = NULL;
1140 envp[0] = "HOME=/";
1141 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1142 envp[2] = NULL;
1143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1144 call_usermodehelper(argv[0], argv, envp, 1);
1145 #else
1146 call_usermodehelper(argv[0], argv, envp);
1147 #endif
1148 while (!profile_loaded) {
1149 set_current_state(TASK_INTERRUPTIBLE);
1150 schedule_timeout(HZ / 10);
1151 }
1152 }
1153 //if (!profile_loaded) panic("No profiles loaded. Run policy loader using 'init=' option.\n");
1154 printk("Mandatory Access Control activated.\n");
1155 sbin_init_started = 1;
1156 ccs_log_level = KERN_WARNING;
1157 { /* Check all profiles currently assigned to domains are defined. */
1158 struct domain_info *domain;
1159 for (domain = &KERNEL_DOMAIN; domain; domain = domain->next) {
1160 const u8 profile = domain->profile;
1161 if (!profile_ptr[profile]) panic("Profile %u (used by '%s') not defined.\n", profile, domain->domainname->name);
1162 }
1163 }
1164 }
1165
1166
1167 /************************* MAC Decision Delayer *************************/
1168
1169 static DECLARE_WAIT_QUEUE_HEAD(query_wait);
1170
1171 static spinlock_t query_lock = SPIN_LOCK_UNLOCKED;
1172
1173 struct query_entry {
1174 struct list_head list;
1175 char *query;
1176 int query_len;
1177 unsigned int serial;
1178 int timer;
1179 int answer;
1180 };
1181
1182 static LIST_HEAD(query_list);
1183 static atomic_t queryd_watcher = ATOMIC_INIT(0);
1184
1185 int CheckSupervisor(const char *fmt, ...)
1186 {
1187 va_list args;
1188 int error = -EPERM;
1189 int pos, len;
1190 static unsigned int serial = 0;
1191 struct query_entry *query_entry;
1192 if (!CheckCCSFlags(CCS_ALLOW_ENFORCE_GRACE)) return -EPERM;
1193 if (!atomic_read(&queryd_watcher)) return -EPERM;
1194 va_start(args, fmt);
1195 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1196 va_end(args);
1197 if ((query_entry = ccs_alloc(sizeof(*query_entry))) == NULL ||
1198 (query_entry->query = ccs_alloc(len)) == NULL) goto out;
1199 INIT_LIST_HEAD(&query_entry->list);
1200 /***** CRITICAL SECTION START *****/
1201 spin_lock(&query_lock);
1202 query_entry->serial = serial++;
1203 spin_unlock(&query_lock);
1204 /***** CRITICAL SECTION END *****/
1205 pos = snprintf(query_entry->query, len - 1, "Q%u\n", query_entry->serial);
1206 va_start(args, fmt);
1207 vsnprintf(query_entry->query + pos, len - 1 - pos, fmt, args);
1208 query_entry->query_len = strlen(query_entry->query) + 1;
1209 va_end(args);
1210 /***** CRITICAL SECTION START *****/
1211 spin_lock(&query_lock);
1212 list_add_tail(&query_entry->list, &query_list);
1213 spin_unlock(&query_lock);
1214 /***** CRITICAL SECTION END *****/
1215 UpdateCounter(CCS_UPDATES_COUNTER_QUERY);
1216 /* Give 10 seconds for supervisor's opinion. */
1217 for (query_entry->timer = 0; atomic_read(&queryd_watcher) && CheckCCSFlags(CCS_ALLOW_ENFORCE_GRACE) && query_entry->timer < 100; query_entry->timer++) {
1218 wake_up(&query_wait);
1219 set_current_state(TASK_INTERRUPTIBLE);
1220 schedule_timeout(HZ / 10);
1221 if (query_entry->answer) break;
1222 }
1223 UpdateCounter(CCS_UPDATES_COUNTER_QUERY);
1224 /***** CRITICAL SECTION START *****/
1225 spin_lock(&query_lock);
1226 list_del(&query_entry->list);
1227 spin_unlock(&query_lock);
1228 /***** CRITICAL SECTION END *****/
1229 switch (query_entry->answer) {
1230 case 1:
1231 /* Granted by administrator. */
1232 error = 0;
1233 break;
1234 case 0:
1235 /* Timed out. */
1236 break;
1237 default:
1238 /* Rejected by administrator. */
1239 break;
1240 }
1241 out: ;
1242 if (query_entry) ccs_free(query_entry->query);
1243 ccs_free(query_entry);
1244 return error;
1245 }
1246
1247 static int PollQuery(struct file *file, poll_table *wait)
1248 {
1249 int found;
1250 /***** CRITICAL SECTION START *****/
1251 spin_lock(&query_lock);
1252 found = !list_empty(&query_list);
1253 spin_unlock(&query_lock);
1254 /***** CRITICAL SECTION END *****/
1255 if (found) return POLLIN | POLLRDNORM;
1256 poll_wait(file, &query_wait, wait);
1257 /***** CRITICAL SECTION START *****/
1258 spin_lock(&query_lock);
1259 found = !list_empty(&query_list);
1260 spin_unlock(&query_lock);
1261 /***** CRITICAL SECTION END *****/
1262 if (found) return POLLIN | POLLRDNORM;
1263 return 0;
1264 }
1265
1266 static int ReadQuery(struct io_buffer *head)
1267 {
1268 struct list_head *tmp;
1269 int pos = 0, len = 0;
1270 char *buf;
1271 if (head->read_avail) return 0;
1272 if (head->read_buf) {
1273 ccs_free(head->read_buf); head->read_buf = NULL;
1274 head->readbuf_size = 0;
1275 }
1276 /***** CRITICAL SECTION START *****/
1277 spin_lock(&query_lock);
1278 list_for_each(tmp, &query_list) {
1279 struct query_entry *ptr = list_entry(tmp, struct query_entry, list);
1280 if (pos++ == head->read_step) {
1281 len = ptr->query_len;
1282 break;
1283 }
1284 }
1285 spin_unlock(&query_lock);
1286 /***** CRITICAL SECTION END *****/
1287 if (!len) {
1288 head->read_step = 0;
1289 return 0;
1290 }
1291 if ((buf = ccs_alloc(len)) != NULL) {
1292 pos = 0;
1293 /***** CRITICAL SECTION START *****/
1294 spin_lock(&query_lock);
1295 list_for_each(tmp, &query_list) {
1296 struct query_entry *ptr = list_entry(tmp, struct query_entry, list);
1297 if (pos++ == head->read_step) {
1298 /* Some query can be skiipped since query_list can change, but I don't care. */
1299 if (len == ptr->query_len) memmove(buf, ptr->query, len);
1300 break;
1301 }
1302 }
1303 spin_unlock(&query_lock);
1304 /***** CRITICAL SECTION END *****/
1305 if (buf[0]) {
1306 head->readbuf_size = head->read_avail = len;
1307 head->read_buf = buf;
1308 head->read_step++;
1309 } else {
1310 ccs_free(buf);
1311 }
1312 }
1313 return 0;
1314 }
1315
1316 static int WriteAnswer(struct io_buffer *head)
1317 {
1318 char *data = head->write_buf;
1319 struct list_head *tmp;
1320 unsigned int serial, answer;
1321 /***** CRITICAL SECTION START *****/
1322 spin_lock(&query_lock);
1323 list_for_each(tmp, &query_list) {
1324 struct query_entry *ptr = list_entry(tmp, struct query_entry, list);
1325 ptr->timer = 0;
1326 }
1327 spin_unlock(&query_lock);
1328 /***** CRITICAL SECTION END *****/
1329 if (sscanf(data, "A%u=%u", &serial, &answer) != 2) return -EINVAL;
1330 /***** CRITICAL SECTION START *****/
1331 spin_lock(&query_lock);
1332 list_for_each(tmp, &query_list) {
1333 struct query_entry *ptr = list_entry(tmp, struct query_entry, list);
1334 if (ptr->serial != serial) continue;
1335 if (!ptr->answer) ptr->answer = answer;
1336 break;
1337 }
1338 spin_unlock(&query_lock);
1339 /***** CRITICAL SECTION END *****/
1340 return 0;
1341 }
1342
1343 /************************* /proc INTERFACE HANDLER *************************/
1344
1345 /* Policy updates counter. */
1346 static unsigned int updates_counter[MAX_CCS_UPDATES_COUNTER];
1347 static spinlock_t updates_counter_lock = SPIN_LOCK_UNLOCKED;
1348
1349 void UpdateCounter(const unsigned char index)
1350 {
1351 /***** CRITICAL SECTION START *****/
1352 spin_lock(&updates_counter_lock);
1353 if (index < MAX_CCS_UPDATES_COUNTER) updates_counter[index]++;
1354 spin_unlock(&updates_counter_lock);
1355 /***** CRITICAL SECTION END *****/
1356 }
1357
1358 static int ReadUpdatesCounter(struct io_buffer *head)
1359 {
1360 if (!head->read_eof) {
1361 unsigned int counter[MAX_CCS_UPDATES_COUNTER];
1362 /***** CRITICAL SECTION START *****/
1363 spin_lock(&updates_counter_lock);
1364 memmove(counter, updates_counter, sizeof(updates_counter));
1365 memset(updates_counter, 0, sizeof(updates_counter));
1366 spin_unlock(&updates_counter_lock);
1367 /***** CRITICAL SECTION END *****/
1368 io_printf(head,
1369 "/proc/ccs/policy/system_policy: %10u\n"
1370 "/proc/ccs/policy/domain_policy: %10u\n"
1371 "/proc/ccs/policy/exception_policy: %10u\n"
1372 "/proc/ccs/status: %10u\n"
1373 "/proc/ccs/policy/query: %10u\n"
1374 "/proc/ccs/policy/manager: %10u\n"
1375 "/proc/ccs/info/grant_log: %10u\n"
1376 "/proc/ccs/info/reject_log: %10u\n",
1377 counter[CCS_UPDATES_COUNTER_SYSTEM_POLICY],
1378 counter[CCS_UPDATES_COUNTER_DOMAIN_POLICY],
1379 counter[CCS_UPDATES_COUNTER_EXCEPTION_POLICY],
1380 counter[CCS_UPDATES_COUNTER_STATUS],
1381 counter[CCS_UPDATES_COUNTER_QUERY],
1382 counter[CCS_UPDATES_COUNTER_MANAGER],
1383 counter[CCS_UPDATES_COUNTER_GRANT_LOG],
1384 counter[CCS_UPDATES_COUNTER_REJECT_LOG]);
1385 head->read_eof = 1;
1386 }
1387 return 0;
1388 }
1389
1390 static int ReadVersion(struct io_buffer *head)
1391 {
1392 if (!head->read_eof) {
1393 if (io_printf(head, "1\n") == 0) head->read_eof = 1;
1394 }
1395 return 0;
1396 }
1397
1398 static int ReadMemoryCounter(struct io_buffer *head)
1399 {
1400 if (!head->read_eof) {
1401 const int shared = GetMemoryUsedForSaveName(), private = GetMemoryUsedForElements(), dynamic = GetMemoryUsedForDynamic();
1402 if (io_printf(head, "Shared: %10u\nPrivate: %10u\nDynamic: %10u\nTotal: %10u\n", shared, private, dynamic, shared + private + dynamic) == 0) head->read_eof = 1;
1403 }
1404 return 0;
1405 }
1406
1407 int CCS_OpenControl(const int type, struct file *file)
1408 {
1409 struct io_buffer *head = ccs_alloc(sizeof(*head));
1410 if (!head) return -ENOMEM;
1411 init_MUTEX(&head->read_sem);
1412 init_MUTEX(&head->write_sem);
1413 switch (type) {
1414 #ifdef CONFIG_TOMOYO
1415 case CCS_POLICY_DOMAINPOLICY:
1416 head->write = AddDomainPolicy;
1417 head->read = ReadDomainPolicy;
1418 break;
1419 case CCS_POLICY_EXCEPTIONPOLICY:
1420 head->write = AddExceptionPolicy;
1421 head->read = ReadExceptionPolicy;
1422 break;
1423 case CCS_POLICY_DOMAIN_STATUS:
1424 head->write = UpdateDomainProfile;
1425 head->read = ReadDomainProfile;
1426 break;
1427 case CCS_INFO_PROCESS_STATUS:
1428 head->write = WritePID;
1429 head->read = ReadPID;
1430 break;
1431 case CCS_INFO_GRANTLOG:
1432 head->poll = PollGrantLog;
1433 head->read = ReadGrantLog;
1434 break;
1435 case CCS_INFO_REJECTLOG:
1436 head->poll = PollRejectLog;
1437 head->read = ReadRejectLog;
1438 break;
1439 case CCS_INFO_SELFDOMAIN:
1440 head->read = ReadSelfDomain;
1441 break;
1442 #endif
1443 #ifdef CONFIG_SAKURA
1444 case CCS_POLICY_SYSTEMPOLICY:
1445 head->write = AddSystemPolicy;
1446 head->read = ReadSystemPolicy;
1447 break;
1448 #endif
1449 case CCS_VERSION:
1450 head->read = ReadVersion;
1451 head->readbuf_size = 128;
1452 break;
1453 case CCS_INFO_MEMINFO:
1454 head->read = ReadMemoryCounter;
1455 head->readbuf_size = 128;
1456 break;
1457 case CCS_STATUS:
1458 head->write = SetStatus;
1459 head->read = ReadStatus;
1460 break;
1461 case CCS_POLICY_QUERY:
1462 head->poll = PollQuery;
1463 head->write = WriteAnswer;
1464 head->read = ReadQuery;
1465 break;
1466 case CCS_POLICY_MANAGER:
1467 head->write = AddManagerPolicy;
1468 head->read = ReadManagerPolicy;
1469 break;
1470 case CCS_INFO_UPDATESCOUNTER:
1471 head->read = ReadUpdatesCounter;
1472 break;
1473 }
1474 if (type != CCS_INFO_GRANTLOG && type != CCS_INFO_REJECTLOG && type != CCS_POLICY_QUERY) {
1475 if (!head->readbuf_size) head->readbuf_size = PAGE_SIZE * 2;
1476 if ((head->read_buf = ccs_alloc(head->readbuf_size)) == NULL) {
1477 ccs_free(head);
1478 return -ENOMEM;
1479 }
1480 }
1481 if (head->write) {
1482 head->writebuf_size = PAGE_SIZE * 2;
1483 if ((head->write_buf = ccs_alloc(head->writebuf_size)) == NULL) {
1484 ccs_free(head->read_buf);
1485 ccs_free(head);
1486 return -ENOMEM;
1487 }
1488 }
1489 file->private_data = head;
1490 if (type == CCS_INFO_SELFDOMAIN) CCS_ReadControl(file, NULL, 0);
1491 else if (head->write == WriteAnswer) atomic_inc(&queryd_watcher);
1492 return 0;
1493 }
1494
1495 static int CopyToUser(struct io_buffer *head, char __user * buffer, int buffer_len)
1496 {
1497 int len = head->read_avail;
1498 char *cp = head->read_buf;
1499 if (len > buffer_len) len = buffer_len;
1500 if (len) {
1501 if (copy_to_user(buffer, cp, len)) return -EFAULT;
1502 head->read_avail -= len;
1503 memmove(cp, cp + len, head->read_avail);
1504 }
1505 return len;
1506 }
1507
1508 int CCS_PollControl(struct file *file, poll_table *wait)
1509 {
1510 struct io_buffer *head = file->private_data;
1511 if (!head->poll) return -ENOSYS;
1512 return head->poll(file, wait);
1513 }
1514
1515 int CCS_ReadControl(struct file *file, char __user *buffer, const int buffer_len)
1516 {
1517 int len = 0;
1518 struct io_buffer *head = file->private_data;
1519 if (!head->read) return -ENOSYS;
1520 if (!access_ok(VERIFY_WRITE, buffer, buffer_len)) return -EFAULT;
1521 if (down_interruptible(&head->read_sem)) return -EINTR;
1522 len = head->read(head);
1523 if (len >= 0) len = CopyToUser(head, buffer, buffer_len);
1524 up(&head->read_sem);
1525 return len;
1526 }
1527
1528 int CCS_WriteControl(struct file *file, const char __user *buffer, const int buffer_len)
1529 {
1530 struct io_buffer *head = file->private_data;
1531 int error = buffer_len;
1532 int avail_len = buffer_len;
1533 char *cp0 = head->write_buf;
1534 if (!head->write) return -ENOSYS;
1535 if (!access_ok(VERIFY_READ, buffer, buffer_len)) return -EFAULT;
1536 if (!isRoot()) return -EPERM;
1537 if (head->write != WritePID && !IsPolicyManager()) {
1538 return -EPERM; /* Forbid updating policies for non manager programs. */
1539 }
1540 if (down_interruptible(&head->write_sem)) return -EINTR;
1541 while (avail_len > 0) {
1542 char c;
1543 if (head->write_avail >= head->writebuf_size - 1) {
1544 error = -ENOMEM;
1545 break;
1546 } else if (get_user(c, buffer)) {
1547 error = -EFAULT;
1548 break;
1549 }
1550 buffer++; avail_len--;
1551 cp0[head->write_avail++] = c;
1552 if (c != '\n') continue;
1553 cp0[head->write_avail - 1] = '\0';
1554 head->write_avail = 0;
1555 NormalizeLine(cp0);
1556 head->write(head);
1557 }
1558 up(&head->write_sem);
1559 return error;
1560 }
1561
1562
1563 int CCS_CloseControl(struct file *file)
1564 {
1565 struct io_buffer *head = file->private_data;
1566 if (head->write == WriteAnswer) atomic_dec(&queryd_watcher);
1567 else if (head->read == ReadMemoryCounter) profile_loaded = 1;
1568 ccs_free(head->read_buf); head->read_buf = NULL;
1569 ccs_free(head->write_buf); head->write_buf = NULL;
1570 ccs_free(head); head = NULL;
1571 file->private_data = NULL;
1572 return 0;
1573 }

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