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