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/24 |
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 SetProfile(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_PROFILE); |
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 ReadProfile(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); |
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); |
965 |
} else if (strncmp(data, KEYWORD_ALIAS, KEYWORD_ALIAS_LEN) == 0) { |
966 |
return AddAliasPolicy(data + KEYWORD_ALIAS_LEN, is_delete); |
967 |
} else if (strncmp(data, KEYWORD_AGGREGATOR, KEYWORD_AGGREGATOR_LEN) == 0) { |
968 |
return AddAggregatorPolicy(data + KEYWORD_AGGREGATOR_LEN, is_delete); |
969 |
} else if (strncmp(data, KEYWORD_ALLOW_READ, KEYWORD_ALLOW_READ_LEN) == 0) { |
970 |
return AddGloballyReadablePolicy(data + KEYWORD_ALLOW_READ_LEN, is_delete); |
971 |
} else if (strncmp(data, KEYWORD_FILE_PATTERN, KEYWORD_FILE_PATTERN_LEN) == 0) { |
972 |
return AddPatternPolicy(data + KEYWORD_FILE_PATTERN_LEN, is_delete); |
973 |
} else if (strncmp(data, KEYWORD_PATH_GROUP, KEYWORD_PATH_GROUP_LEN) == 0) { |
974 |
return AddGroupPolicy(data + KEYWORD_PATH_GROUP_LEN, is_delete); |
975 |
} else if (strncmp(data, KEYWORD_DENY_REWRITE, KEYWORD_DENY_REWRITE_LEN) == 0) { |
976 |
return AddNoRewritePolicy(data + KEYWORD_DENY_REWRITE_LEN, is_delete); |
977 |
} else if (strncmp(data, KEYWORD_ADDRESS_GROUP, KEYWORD_ADDRESS_GROUP_LEN) == 0) { |
978 |
return AddAddressGroupPolicy(data + KEYWORD_ADDRESS_GROUP_LEN, is_delete); |
979 |
} |
980 |
return -EINVAL; |
981 |
} |
982 |
|
983 |
static int ReadExceptionPolicy(struct io_buffer *head) |
984 |
{ |
985 |
if (!head->read_eof) { |
986 |
switch (head->read_step) { |
987 |
case 0: |
988 |
if (!isRoot()) return -EPERM; |
989 |
head->read_var2 = NULL; head->read_step = 1; |
990 |
case 1: |
991 |
if (ReadDomainKeeperPolicy(head)) break; |
992 |
head->read_var2 = NULL; head->read_step = 2; |
993 |
case 2: |
994 |
if (ReadGloballyReadablePolicy(head)) break; |
995 |
head->read_var2 = NULL; head->read_step = 3; |
996 |
case 3: |
997 |
if (ReadDomainInitializerPolicy(head)) break; |
998 |
head->read_var2 = NULL; head->read_step = 4; |
999 |
case 4: |
1000 |
if (ReadAliasPolicy(head)) break; |
1001 |
head->read_var2 = NULL; head->read_step = 5; |
1002 |
case 5: |
1003 |
if (ReadAggregatorPolicy(head)) break; |
1004 |
head->read_var2 = NULL; head->read_step = 6; |
1005 |
case 6: |
1006 |
if (ReadPatternPolicy(head)) break; |
1007 |
head->read_var2 = NULL; head->read_step = 7; |
1008 |
case 7: |
1009 |
if (ReadNoRewritePolicy(head)) break; |
1010 |
head->read_var2 = NULL; head->read_step = 8; |
1011 |
case 8: |
1012 |
if (ReadGroupPolicy(head)) break; |
1013 |
head->read_var1 = head->read_var2 = NULL; head->read_step = 9; |
1014 |
case 9: |
1015 |
if (ReadAddressGroupPolicy(head)) break; |
1016 |
head->read_eof = 1; |
1017 |
break; |
1018 |
default: |
1019 |
return -EINVAL; |
1020 |
} |
1021 |
} |
1022 |
return 0; |
1023 |
} |
1024 |
|
1025 |
#endif |
1026 |
|
1027 |
/************************* SYSTEM POLICY HANDLER *************************/ |
1028 |
|
1029 |
#ifdef CONFIG_SAKURA |
1030 |
|
1031 |
static int AddSystemPolicy(struct io_buffer *head) |
1032 |
{ |
1033 |
char *data = head->write_buf; |
1034 |
int is_delete = 0; |
1035 |
if (!isRoot()) return -EPERM; |
1036 |
UpdateCounter(CCS_UPDATES_COUNTER_SYSTEM_POLICY); |
1037 |
if (strncmp(data, KEYWORD_DELETE, KEYWORD_DELETE_LEN) == 0) { |
1038 |
data += KEYWORD_DELETE_LEN; |
1039 |
is_delete = 1; |
1040 |
} |
1041 |
if (strncmp(data, KEYWORD_ALLOW_MOUNT, KEYWORD_ALLOW_MOUNT_LEN) == 0) |
1042 |
return AddMountPolicy(data + KEYWORD_ALLOW_MOUNT_LEN, is_delete); |
1043 |
if (strncmp(data, KEYWORD_DENY_UNMOUNT, KEYWORD_DENY_UNMOUNT_LEN) == 0) |
1044 |
return AddNoUmountPolicy(data + KEYWORD_DENY_UNMOUNT_LEN, is_delete); |
1045 |
if (strncmp(data, KEYWORD_ALLOW_CHROOT, KEYWORD_ALLOW_CHROOT_LEN) == 0) |
1046 |
return AddChrootPolicy(data + KEYWORD_ALLOW_CHROOT_LEN, is_delete); |
1047 |
if (strncmp(data, KEYWORD_ALLOW_PIVOT_ROOT, KEYWORD_ALLOW_PIVOT_ROOT_LEN) == 0) |
1048 |
return AddPivotRootPolicy(data + KEYWORD_ALLOW_PIVOT_ROOT_LEN, is_delete); |
1049 |
if (strncmp(data, KEYWORD_DENY_AUTOBIND, KEYWORD_DENY_AUTOBIND_LEN) == 0) |
1050 |
return AddReservedPortPolicy(data + KEYWORD_DENY_AUTOBIND_LEN, is_delete); |
1051 |
return -EINVAL; |
1052 |
} |
1053 |
|
1054 |
static int ReadSystemPolicy(struct io_buffer *head) |
1055 |
{ |
1056 |
if (!head->read_eof) { |
1057 |
switch (head->read_step) { |
1058 |
case 0: |
1059 |
if (!isRoot()) return -EPERM; |
1060 |
head->read_var2 = NULL; head->read_step = 1; |
1061 |
case 1: |
1062 |
if (ReadMountPolicy(head)) break; |
1063 |
head->read_var2 = NULL; head->read_step = 2; |
1064 |
case 2: |
1065 |
if (ReadNoUmountPolicy(head)) break; |
1066 |
head->read_var2 = NULL; head->read_step = 3; |
1067 |
case 3: |
1068 |
if (ReadChrootPolicy(head)) break; |
1069 |
head->read_var2 = NULL; head->read_step = 4; |
1070 |
case 4: |
1071 |
if (ReadPivotRootPolicy(head)) break; |
1072 |
head->read_var2 = NULL; head->read_step = 5; |
1073 |
case 5: |
1074 |
if (ReadReservedPortPolicy(head)) break; |
1075 |
head->read_eof = 1; |
1076 |
break; |
1077 |
default: |
1078 |
return -EINVAL; |
1079 |
} |
1080 |
} |
1081 |
return 0; |
1082 |
} |
1083 |
|
1084 |
#endif |
1085 |
|
1086 |
/************************* POLICY LOADER *************************/ |
1087 |
|
1088 |
static int profile_loaded = 0; |
1089 |
|
1090 |
static const char *ccs_loader = NULL; |
1091 |
|
1092 |
static int __init CCS_loader_Setup(char *str) |
1093 |
{ |
1094 |
ccs_loader = str; |
1095 |
return 0; |
1096 |
} |
1097 |
|
1098 |
__setup("CCS_loader=", CCS_loader_Setup); |
1099 |
|
1100 |
void CCS_LoadPolicy(const char *filename) |
1101 |
{ |
1102 |
if (sbin_init_started) return; |
1103 |
/* |
1104 |
* Check filename is /sbin/init or /sbin/ccs-start . |
1105 |
* /sbin/ccs-start is a dummy filename in case where /sbin/init can't be passed. |
1106 |
* You can create /sbin/ccs-start by "ln -s /bin/true /sbin/ccs-start", for |
1107 |
* only the pathname is needed to activate Mandatory Access Control. |
1108 |
*/ |
1109 |
if (strcmp(filename, "/sbin/init") != 0 && strcmp(filename, "/sbin/ccs-start") != 0) return; |
1110 |
/* |
1111 |
* Don't activate MAC if the path given by 'CCS_loader=' option doesn't exist. |
1112 |
* If initrd.img includes /sbin/init but real-root-dev has not mounted on / yet, |
1113 |
* activating MAC will block the system since policies are not loaded yet. |
1114 |
* So let do_execve() call this function everytime. |
1115 |
*/ |
1116 |
{ |
1117 |
struct nameidata nd; |
1118 |
if (!ccs_loader) ccs_loader = "/sbin/ccs-init"; |
1119 |
if (path_lookup(ccs_loader, lookup_flags, &nd)) { |
1120 |
printk("Not activating Mandatory Access Control now since %s doesn't exist.\n", ccs_loader); |
1121 |
return; |
1122 |
} |
1123 |
path_release(&nd); |
1124 |
} |
1125 |
#ifdef CONFIG_SAKURA |
1126 |
printk("SAKURA: 1.5.0-pre 2007/08/24\n"); |
1127 |
#endif |
1128 |
#ifdef CONFIG_TOMOYO |
1129 |
printk("TOMOYO: 1.5.0-pre 2007/08/24\n"); |
1130 |
#endif |
1131 |
if (!profile_loaded) { |
1132 |
char *argv[2], *envp[3]; |
1133 |
printk("Calling %s to load policy. Please wait.\n", ccs_loader); |
1134 |
argv[0] = (char *) ccs_loader; |
1135 |
argv[1] = NULL; |
1136 |
envp[0] = "HOME=/"; |
1137 |
envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
1138 |
envp[2] = NULL; |
1139 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
1140 |
call_usermodehelper(argv[0], argv, envp, 1); |
1141 |
#else |
1142 |
call_usermodehelper(argv[0], argv, envp); |
1143 |
#endif |
1144 |
while (!profile_loaded) { |
1145 |
set_current_state(TASK_INTERRUPTIBLE); |
1146 |
schedule_timeout(HZ / 10); |
1147 |
} |
1148 |
} |
1149 |
//if (!profile_loaded) panic("No profiles loaded. Run policy loader using 'init=' option.\n"); |
1150 |
printk("Mandatory Access Control activated.\n"); |
1151 |
sbin_init_started = 1; |
1152 |
ccs_log_level = KERN_WARNING; |
1153 |
{ /* Check all profiles currently assigned to domains are defined. */ |
1154 |
struct domain_info *domain; |
1155 |
for (domain = &KERNEL_DOMAIN; domain; domain = domain->next) { |
1156 |
const u8 profile = domain->profile; |
1157 |
if (!profile_ptr[profile]) panic("Profile %u (used by '%s') not defined.\n", profile, domain->domainname->name); |
1158 |
} |
1159 |
} |
1160 |
} |
1161 |
|
1162 |
|
1163 |
/************************* MAC Decision Delayer *************************/ |
1164 |
|
1165 |
static DECLARE_WAIT_QUEUE_HEAD(query_wait); |
1166 |
|
1167 |
static spinlock_t query_lock = SPIN_LOCK_UNLOCKED; |
1168 |
|
1169 |
struct query_entry { |
1170 |
struct list_head list; |
1171 |
char *query; |
1172 |
int query_len; |
1173 |
unsigned int serial; |
1174 |
int timer; |
1175 |
int answer; |
1176 |
}; |
1177 |
|
1178 |
static LIST_HEAD(query_list); |
1179 |
static atomic_t queryd_watcher = ATOMIC_INIT(0); |
1180 |
|
1181 |
int CheckSupervisor(const char *fmt, ...) |
1182 |
{ |
1183 |
va_list args; |
1184 |
int error = -EPERM; |
1185 |
int pos, len; |
1186 |
static unsigned int serial = 0; |
1187 |
struct query_entry *query_entry; |
1188 |
if (!CheckCCSFlags(CCS_ALLOW_ENFORCE_GRACE)) return -EPERM; |
1189 |
if (!atomic_read(&queryd_watcher)) return -EPERM; |
1190 |
va_start(args, fmt); |
1191 |
len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32; |
1192 |
va_end(args); |
1193 |
if ((query_entry = ccs_alloc(sizeof(*query_entry))) == NULL || |
1194 |
(query_entry->query = ccs_alloc(len)) == NULL) goto out; |
1195 |
INIT_LIST_HEAD(&query_entry->list); |
1196 |
/***** CRITICAL SECTION START *****/ |
1197 |
spin_lock(&query_lock); |
1198 |
query_entry->serial = serial++; |
1199 |
spin_unlock(&query_lock); |
1200 |
/***** CRITICAL SECTION END *****/ |
1201 |
pos = snprintf(query_entry->query, len - 1, "Q%u\n", query_entry->serial); |
1202 |
va_start(args, fmt); |
1203 |
vsnprintf(query_entry->query + pos, len - 1 - pos, fmt, args); |
1204 |
query_entry->query_len = strlen(query_entry->query) + 1; |
1205 |
va_end(args); |
1206 |
/***** CRITICAL SECTION START *****/ |
1207 |
spin_lock(&query_lock); |
1208 |
list_add_tail(&query_entry->list, &query_list); |
1209 |
spin_unlock(&query_lock); |
1210 |
/***** CRITICAL SECTION END *****/ |
1211 |
UpdateCounter(CCS_UPDATES_COUNTER_QUERY); |
1212 |
/* Give 10 seconds for supervisor's opinion. */ |
1213 |
for (query_entry->timer = 0; atomic_read(&queryd_watcher) && CheckCCSFlags(CCS_ALLOW_ENFORCE_GRACE) && query_entry->timer < 100; query_entry->timer++) { |
1214 |
wake_up(&query_wait); |
1215 |
set_current_state(TASK_INTERRUPTIBLE); |
1216 |
schedule_timeout(HZ / 10); |
1217 |
if (query_entry->answer) break; |
1218 |
} |
1219 |
UpdateCounter(CCS_UPDATES_COUNTER_QUERY); |
1220 |
/***** CRITICAL SECTION START *****/ |
1221 |
spin_lock(&query_lock); |
1222 |
list_del(&query_entry->list); |
1223 |
spin_unlock(&query_lock); |
1224 |
/***** CRITICAL SECTION END *****/ |
1225 |
switch (query_entry->answer) { |
1226 |
case 1: |
1227 |
/* Granted by administrator. */ |
1228 |
error = 0; |
1229 |
break; |
1230 |
case 0: |
1231 |
/* Timed out. */ |
1232 |
break; |
1233 |
default: |
1234 |
/* Rejected by administrator. */ |
1235 |
break; |
1236 |
} |
1237 |
out: ; |
1238 |
if (query_entry) ccs_free(query_entry->query); |
1239 |
ccs_free(query_entry); |
1240 |
return error; |
1241 |
} |
1242 |
|
1243 |
static int PollQuery(struct file *file, poll_table *wait) |
1244 |
{ |
1245 |
int found; |
1246 |
/***** CRITICAL SECTION START *****/ |
1247 |
spin_lock(&query_lock); |
1248 |
found = !list_empty(&query_list); |
1249 |
spin_unlock(&query_lock); |
1250 |
/***** CRITICAL SECTION END *****/ |
1251 |
if (found) return POLLIN | POLLRDNORM; |
1252 |
poll_wait(file, &query_wait, wait); |
1253 |
/***** CRITICAL SECTION START *****/ |
1254 |
spin_lock(&query_lock); |
1255 |
found = !list_empty(&query_list); |
1256 |
spin_unlock(&query_lock); |
1257 |
/***** CRITICAL SECTION END *****/ |
1258 |
if (found) return POLLIN | POLLRDNORM; |
1259 |
return 0; |
1260 |
} |
1261 |
|
1262 |
static int ReadQuery(struct io_buffer *head) |
1263 |
{ |
1264 |
struct list_head *tmp; |
1265 |
int pos = 0, len = 0; |
1266 |
char *buf; |
1267 |
if (head->read_avail) return 0; |
1268 |
if (head->read_buf) { |
1269 |
ccs_free(head->read_buf); head->read_buf = NULL; |
1270 |
head->readbuf_size = 0; |
1271 |
} |
1272 |
/***** CRITICAL SECTION START *****/ |
1273 |
spin_lock(&query_lock); |
1274 |
list_for_each(tmp, &query_list) { |
1275 |
struct query_entry *ptr = list_entry(tmp, struct query_entry, list); |
1276 |
if (pos++ == head->read_step) { |
1277 |
len = ptr->query_len; |
1278 |
break; |
1279 |
} |
1280 |
} |
1281 |
spin_unlock(&query_lock); |
1282 |
/***** CRITICAL SECTION END *****/ |
1283 |
if (!len) { |
1284 |
head->read_step = 0; |
1285 |
return 0; |
1286 |
} |
1287 |
if ((buf = ccs_alloc(len)) != NULL) { |
1288 |
pos = 0; |
1289 |
/***** CRITICAL SECTION START *****/ |
1290 |
spin_lock(&query_lock); |
1291 |
list_for_each(tmp, &query_list) { |
1292 |
struct query_entry *ptr = list_entry(tmp, struct query_entry, list); |
1293 |
if (pos++ == head->read_step) { |
1294 |
/* Some query can be skiipped since query_list can change, but I don't care. */ |
1295 |
if (len == ptr->query_len) memmove(buf, ptr->query, len); |
1296 |
break; |
1297 |
} |
1298 |
} |
1299 |
spin_unlock(&query_lock); |
1300 |
/***** CRITICAL SECTION END *****/ |
1301 |
if (buf[0]) { |
1302 |
head->readbuf_size = head->read_avail = len; |
1303 |
head->read_buf = buf; |
1304 |
head->read_step++; |
1305 |
} else { |
1306 |
ccs_free(buf); |
1307 |
} |
1308 |
} |
1309 |
return 0; |
1310 |
} |
1311 |
|
1312 |
static int WriteAnswer(struct io_buffer *head) |
1313 |
{ |
1314 |
char *data = head->write_buf; |
1315 |
struct list_head *tmp; |
1316 |
unsigned int serial, answer; |
1317 |
/***** CRITICAL SECTION START *****/ |
1318 |
spin_lock(&query_lock); |
1319 |
list_for_each(tmp, &query_list) { |
1320 |
struct query_entry *ptr = list_entry(tmp, struct query_entry, list); |
1321 |
ptr->timer = 0; |
1322 |
} |
1323 |
spin_unlock(&query_lock); |
1324 |
/***** CRITICAL SECTION END *****/ |
1325 |
if (sscanf(data, "A%u=%u", &serial, &answer) != 2) return -EINVAL; |
1326 |
/***** CRITICAL SECTION START *****/ |
1327 |
spin_lock(&query_lock); |
1328 |
list_for_each(tmp, &query_list) { |
1329 |
struct query_entry *ptr = list_entry(tmp, struct query_entry, list); |
1330 |
if (ptr->serial != serial) continue; |
1331 |
if (!ptr->answer) ptr->answer = answer; |
1332 |
break; |
1333 |
} |
1334 |
spin_unlock(&query_lock); |
1335 |
/***** CRITICAL SECTION END *****/ |
1336 |
return 0; |
1337 |
} |
1338 |
|
1339 |
/************************* /proc INTERFACE HANDLER *************************/ |
1340 |
|
1341 |
/* Policy updates counter. */ |
1342 |
static unsigned int updates_counter[MAX_CCS_UPDATES_COUNTER]; |
1343 |
static spinlock_t updates_counter_lock = SPIN_LOCK_UNLOCKED; |
1344 |
|
1345 |
void UpdateCounter(const unsigned char index) |
1346 |
{ |
1347 |
/***** CRITICAL SECTION START *****/ |
1348 |
spin_lock(&updates_counter_lock); |
1349 |
if (index < MAX_CCS_UPDATES_COUNTER) updates_counter[index]++; |
1350 |
spin_unlock(&updates_counter_lock); |
1351 |
/***** CRITICAL SECTION END *****/ |
1352 |
} |
1353 |
|
1354 |
static int ReadUpdatesCounter(struct io_buffer *head) |
1355 |
{ |
1356 |
if (!head->read_eof) { |
1357 |
unsigned int counter[MAX_CCS_UPDATES_COUNTER]; |
1358 |
/***** CRITICAL SECTION START *****/ |
1359 |
spin_lock(&updates_counter_lock); |
1360 |
memmove(counter, updates_counter, sizeof(updates_counter)); |
1361 |
memset(updates_counter, 0, sizeof(updates_counter)); |
1362 |
spin_unlock(&updates_counter_lock); |
1363 |
/***** CRITICAL SECTION END *****/ |
1364 |
io_printf(head, |
1365 |
"/proc/ccs/system_policy: %10u\n" |
1366 |
"/proc/ccs/domain_policy: %10u\n" |
1367 |
"/proc/ccs/exception_policy: %10u\n" |
1368 |
"/proc/ccs/profile: %10u\n" |
1369 |
"/proc/ccs/query: %10u\n" |
1370 |
"/proc/ccs/manager: %10u\n" |
1371 |
"/proc/ccs/grant_log: %10u\n" |
1372 |
"/proc/ccs/reject_log: %10u\n", |
1373 |
counter[CCS_UPDATES_COUNTER_SYSTEM_POLICY], |
1374 |
counter[CCS_UPDATES_COUNTER_DOMAIN_POLICY], |
1375 |
counter[CCS_UPDATES_COUNTER_EXCEPTION_POLICY], |
1376 |
counter[CCS_UPDATES_COUNTER_PROFILE], |
1377 |
counter[CCS_UPDATES_COUNTER_QUERY], |
1378 |
counter[CCS_UPDATES_COUNTER_MANAGER], |
1379 |
counter[CCS_UPDATES_COUNTER_GRANT_LOG], |
1380 |
counter[CCS_UPDATES_COUNTER_REJECT_LOG]); |
1381 |
head->read_eof = 1; |
1382 |
} |
1383 |
return 0; |
1384 |
} |
1385 |
|
1386 |
static int ReadVersion(struct io_buffer *head) |
1387 |
{ |
1388 |
if (!head->read_eof) { |
1389 |
if (io_printf(head, "1.5.0-pre") == 0) head->read_eof = 1; |
1390 |
} |
1391 |
return 0; |
1392 |
} |
1393 |
|
1394 |
static int ReadMemoryCounter(struct io_buffer *head) |
1395 |
{ |
1396 |
if (!head->read_eof) { |
1397 |
const int shared = GetMemoryUsedForSaveName(), private = GetMemoryUsedForElements(), dynamic = GetMemoryUsedForDynamic(); |
1398 |
if (io_printf(head, "Shared: %10u\nPrivate: %10u\nDynamic: %10u\nTotal: %10u\n", shared, private, dynamic, shared + private + dynamic) == 0) head->read_eof = 1; |
1399 |
} |
1400 |
return 0; |
1401 |
} |
1402 |
|
1403 |
int CCS_OpenControl(const int type, struct file *file) |
1404 |
{ |
1405 |
struct io_buffer *head = ccs_alloc(sizeof(*head)); |
1406 |
if (!head) return -ENOMEM; |
1407 |
init_MUTEX(&head->read_sem); |
1408 |
init_MUTEX(&head->write_sem); |
1409 |
switch (type) { |
1410 |
#ifdef CONFIG_TOMOYO |
1411 |
case CCS_DOMAINPOLICY: |
1412 |
head->write = AddDomainPolicy; |
1413 |
head->read = ReadDomainPolicy; |
1414 |
break; |
1415 |
case CCS_EXCEPTIONPOLICY: |
1416 |
head->write = AddExceptionPolicy; |
1417 |
head->read = ReadExceptionPolicy; |
1418 |
break; |
1419 |
case CCS_DOMAIN_STATUS: |
1420 |
head->write = UpdateDomainProfile; |
1421 |
head->read = ReadDomainProfile; |
1422 |
break; |
1423 |
case CCS_PROCESS_STATUS: |
1424 |
head->write = WritePID; |
1425 |
head->read = ReadPID; |
1426 |
break; |
1427 |
case CCS_GRANTLOG: |
1428 |
head->poll = PollGrantLog; |
1429 |
head->read = ReadGrantLog; |
1430 |
break; |
1431 |
case CCS_REJECTLOG: |
1432 |
head->poll = PollRejectLog; |
1433 |
head->read = ReadRejectLog; |
1434 |
break; |
1435 |
case CCS_SELFDOMAIN: |
1436 |
head->read = ReadSelfDomain; |
1437 |
break; |
1438 |
#endif |
1439 |
#ifdef CONFIG_SAKURA |
1440 |
case CCS_SYSTEMPOLICY: |
1441 |
head->write = AddSystemPolicy; |
1442 |
head->read = ReadSystemPolicy; |
1443 |
break; |
1444 |
#endif |
1445 |
case CCS_VERSION: |
1446 |
head->read = ReadVersion; |
1447 |
head->readbuf_size = 128; |
1448 |
break; |
1449 |
case CCS_MEMINFO: |
1450 |
head->read = ReadMemoryCounter; |
1451 |
head->readbuf_size = 128; |
1452 |
break; |
1453 |
case CCS_PROFILE: |
1454 |
head->write = SetProfile; |
1455 |
head->read = ReadProfile; |
1456 |
break; |
1457 |
case CCS_QUERY: |
1458 |
head->poll = PollQuery; |
1459 |
head->write = WriteAnswer; |
1460 |
head->read = ReadQuery; |
1461 |
break; |
1462 |
case CCS_MANAGER: |
1463 |
head->write = AddManagerPolicy; |
1464 |
head->read = ReadManagerPolicy; |
1465 |
break; |
1466 |
case CCS_UPDATESCOUNTER: |
1467 |
head->read = ReadUpdatesCounter; |
1468 |
break; |
1469 |
} |
1470 |
if (type != CCS_GRANTLOG && type != CCS_REJECTLOG && type != CCS_QUERY) { |
1471 |
if (!head->readbuf_size) head->readbuf_size = PAGE_SIZE * 2; |
1472 |
if ((head->read_buf = ccs_alloc(head->readbuf_size)) == NULL) { |
1473 |
ccs_free(head); |
1474 |
return -ENOMEM; |
1475 |
} |
1476 |
} |
1477 |
if (head->write) { |
1478 |
head->writebuf_size = PAGE_SIZE * 2; |
1479 |
if ((head->write_buf = ccs_alloc(head->writebuf_size)) == NULL) { |
1480 |
ccs_free(head->read_buf); |
1481 |
ccs_free(head); |
1482 |
return -ENOMEM; |
1483 |
} |
1484 |
} |
1485 |
file->private_data = head; |
1486 |
if (type == CCS_SELFDOMAIN) CCS_ReadControl(file, NULL, 0); |
1487 |
else if (head->write == WriteAnswer) atomic_inc(&queryd_watcher); |
1488 |
return 0; |
1489 |
} |
1490 |
|
1491 |
static int CopyToUser(struct io_buffer *head, char __user * buffer, int buffer_len) |
1492 |
{ |
1493 |
int len = head->read_avail; |
1494 |
char *cp = head->read_buf; |
1495 |
if (len > buffer_len) len = buffer_len; |
1496 |
if (len) { |
1497 |
if (copy_to_user(buffer, cp, len)) return -EFAULT; |
1498 |
head->read_avail -= len; |
1499 |
memmove(cp, cp + len, head->read_avail); |
1500 |
} |
1501 |
return len; |
1502 |
} |
1503 |
|
1504 |
int CCS_PollControl(struct file *file, poll_table *wait) |
1505 |
{ |
1506 |
struct io_buffer *head = file->private_data; |
1507 |
if (!head->poll) return -ENOSYS; |
1508 |
return head->poll(file, wait); |
1509 |
} |
1510 |
|
1511 |
int CCS_ReadControl(struct file *file, char __user *buffer, const int buffer_len) |
1512 |
{ |
1513 |
int len = 0; |
1514 |
struct io_buffer *head = file->private_data; |
1515 |
if (!head->read) return -ENOSYS; |
1516 |
if (!access_ok(VERIFY_WRITE, buffer, buffer_len)) return -EFAULT; |
1517 |
if (down_interruptible(&head->read_sem)) return -EINTR; |
1518 |
len = head->read(head); |
1519 |
if (len >= 0) len = CopyToUser(head, buffer, buffer_len); |
1520 |
up(&head->read_sem); |
1521 |
return len; |
1522 |
} |
1523 |
|
1524 |
int CCS_WriteControl(struct file *file, const char __user *buffer, const int buffer_len) |
1525 |
{ |
1526 |
struct io_buffer *head = file->private_data; |
1527 |
int error = buffer_len; |
1528 |
int avail_len = buffer_len; |
1529 |
char *cp0 = head->write_buf; |
1530 |
if (!head->write) return -ENOSYS; |
1531 |
if (!access_ok(VERIFY_READ, buffer, buffer_len)) return -EFAULT; |
1532 |
if (!isRoot()) return -EPERM; |
1533 |
if (head->write != WritePID && !IsPolicyManager()) { |
1534 |
return -EPERM; /* Forbid updating policies for non manager programs. */ |
1535 |
} |
1536 |
if (down_interruptible(&head->write_sem)) return -EINTR; |
1537 |
while (avail_len > 0) { |
1538 |
char c; |
1539 |
if (head->write_avail >= head->writebuf_size - 1) { |
1540 |
error = -ENOMEM; |
1541 |
break; |
1542 |
} else if (get_user(c, buffer)) { |
1543 |
error = -EFAULT; |
1544 |
break; |
1545 |
} |
1546 |
buffer++; avail_len--; |
1547 |
cp0[head->write_avail++] = c; |
1548 |
if (c != '\n') continue; |
1549 |
cp0[head->write_avail - 1] = '\0'; |
1550 |
head->write_avail = 0; |
1551 |
NormalizeLine(cp0); |
1552 |
head->write(head); |
1553 |
} |
1554 |
up(&head->write_sem); |
1555 |
return error; |
1556 |
} |
1557 |
|
1558 |
|
1559 |
int CCS_CloseControl(struct file *file) |
1560 |
{ |
1561 |
struct io_buffer *head = file->private_data; |
1562 |
if (head->write == WriteAnswer) atomic_dec(&queryd_watcher); |
1563 |
else if (head->read == ReadMemoryCounter) profile_loaded = 1; |
1564 |
ccs_free(head->read_buf); head->read_buf = NULL; |
1565 |
ccs_free(head->write_buf); head->write_buf = NULL; |
1566 |
ccs_free(head); head = NULL; |
1567 |
file->private_data = NULL; |
1568 |
return 0; |
1569 |
} |