1 |
/* |
2 |
* fs/ccs_common.c |
3 |
* |
4 |
* Common functions for SAKURA and TOMOYO. |
5 |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
* |
8 |
* Version: 1.6.5-pre 2008/10/07 |
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/version.h> |
16 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
17 |
#define __KERNEL_SYSCALLS__ |
18 |
#endif |
19 |
#include <linux/string.h> |
20 |
#include <linux/mm.h> |
21 |
#include <linux/utime.h> |
22 |
#include <linux/file.h> |
23 |
#include <linux/module.h> |
24 |
#include <linux/slab.h> |
25 |
#include <asm/uaccess.h> |
26 |
#include <stdarg.h> |
27 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
28 |
#include <linux/namei.h> |
29 |
#include <linux/mount.h> |
30 |
static const int lookup_flags = LOOKUP_FOLLOW; |
31 |
#else |
32 |
static const int lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE; |
33 |
#endif |
34 |
#include <linux/realpath.h> |
35 |
#include <linux/ccs_common.h> |
36 |
#include <linux/ccs_proc.h> |
37 |
#include <linux/tomoyo.h> |
38 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
39 |
#include <linux/unistd.h> |
40 |
#endif |
41 |
|
42 |
/* To support PID namespace. */ |
43 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) |
44 |
#define find_task_by_pid find_task_by_vpid |
45 |
#endif |
46 |
|
47 |
/* Set default specified by the kernel config. */ |
48 |
#ifdef CONFIG_TOMOYO |
49 |
#define MAX_ACCEPT_ENTRY (CONFIG_TOMOYO_MAX_ACCEPT_ENTRY) |
50 |
#define MAX_GRANT_LOG (CONFIG_TOMOYO_MAX_GRANT_LOG) |
51 |
#define MAX_REJECT_LOG (CONFIG_TOMOYO_MAX_REJECT_LOG) |
52 |
#else |
53 |
#define MAX_ACCEPT_ENTRY 0 |
54 |
#define MAX_GRANT_LOG 0 |
55 |
#define MAX_REJECT_LOG 0 |
56 |
#endif |
57 |
|
58 |
/* Has /sbin/init started? */ |
59 |
bool sbin_init_started; |
60 |
|
61 |
/* Log level for SAKURA's printk(). */ |
62 |
const char *ccs_log_level = KERN_DEBUG; |
63 |
|
64 |
/* String table for functionality that takes 4 modes. */ |
65 |
static const char *mode_4[4] = { |
66 |
"disabled", "learning", "permissive", "enforcing" |
67 |
}; |
68 |
/* String table for functionality that takes 2 modes. */ |
69 |
static const char *mode_2[4] = { |
70 |
"disabled", "enabled", "enabled", "enabled" |
71 |
}; |
72 |
|
73 |
/* Table for profile. */ |
74 |
static struct { |
75 |
const char *keyword; |
76 |
unsigned int current_value; |
77 |
const unsigned int max_value; |
78 |
} ccs_control_array[CCS_MAX_CONTROL_INDEX] = { |
79 |
[CCS_TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 }, |
80 |
[CCS_TOMOYO_MAC_FOR_ARGV0] = { "MAC_FOR_ARGV0", 0, 3 }, |
81 |
[CCS_TOMOYO_MAC_FOR_ENV] = { "MAC_FOR_ENV", 0, 3 }, |
82 |
[CCS_TOMOYO_MAC_FOR_NETWORK] = { "MAC_FOR_NETWORK", 0, 3 }, |
83 |
[CCS_TOMOYO_MAC_FOR_SIGNAL] = { "MAC_FOR_SIGNAL", 0, 3 }, |
84 |
[CCS_SAKURA_DENY_CONCEAL_MOUNT] = { "DENY_CONCEAL_MOUNT", 0, 3 }, |
85 |
[CCS_SAKURA_RESTRICT_CHROOT] = { "RESTRICT_CHROOT", 0, 3 }, |
86 |
[CCS_SAKURA_RESTRICT_MOUNT] = { "RESTRICT_MOUNT", 0, 3 }, |
87 |
[CCS_SAKURA_RESTRICT_UNMOUNT] = { "RESTRICT_UNMOUNT", 0, 3 }, |
88 |
[CCS_SAKURA_RESTRICT_PIVOT_ROOT] = { "RESTRICT_PIVOT_ROOT", 0, 3 }, |
89 |
[CCS_SAKURA_RESTRICT_AUTOBIND] = { "RESTRICT_AUTOBIND", 0, 1 }, |
90 |
[CCS_TOMOYO_MAX_ACCEPT_ENTRY] |
91 |
= { "MAX_ACCEPT_ENTRY", MAX_ACCEPT_ENTRY, INT_MAX }, |
92 |
[CCS_TOMOYO_MAX_GRANT_LOG] |
93 |
= { "MAX_GRANT_LOG", MAX_GRANT_LOG, INT_MAX }, |
94 |
[CCS_TOMOYO_MAX_REJECT_LOG] |
95 |
= { "MAX_REJECT_LOG", MAX_REJECT_LOG, INT_MAX }, |
96 |
[CCS_TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 }, |
97 |
[CCS_SLEEP_PERIOD] |
98 |
= { "SLEEP_PERIOD", 0, 3000 }, /* in 0.1 second */ |
99 |
}; |
100 |
|
101 |
#ifdef CONFIG_TOMOYO |
102 |
/* Capability name used by domain policy. */ |
103 |
static const char *capability_control_keyword[TOMOYO_MAX_CAPABILITY_INDEX] = { |
104 |
[TOMOYO_INET_STREAM_SOCKET_CREATE] = "inet_tcp_create", |
105 |
[TOMOYO_INET_STREAM_SOCKET_LISTEN] = "inet_tcp_listen", |
106 |
[TOMOYO_INET_STREAM_SOCKET_CONNECT] = "inet_tcp_connect", |
107 |
[TOMOYO_USE_INET_DGRAM_SOCKET] = "use_inet_udp", |
108 |
[TOMOYO_USE_INET_RAW_SOCKET] = "use_inet_ip", |
109 |
[TOMOYO_USE_ROUTE_SOCKET] = "use_route", |
110 |
[TOMOYO_USE_PACKET_SOCKET] = "use_packet", |
111 |
[TOMOYO_SYS_MOUNT] = "SYS_MOUNT", |
112 |
[TOMOYO_SYS_UMOUNT] = "SYS_UMOUNT", |
113 |
[TOMOYO_SYS_REBOOT] = "SYS_REBOOT", |
114 |
[TOMOYO_SYS_CHROOT] = "SYS_CHROOT", |
115 |
[TOMOYO_SYS_KILL] = "SYS_KILL", |
116 |
[TOMOYO_SYS_VHANGUP] = "SYS_VHANGUP", |
117 |
[TOMOYO_SYS_SETTIME] = "SYS_TIME", |
118 |
[TOMOYO_SYS_NICE] = "SYS_NICE", |
119 |
[TOMOYO_SYS_SETHOSTNAME] = "SYS_SETHOSTNAME", |
120 |
[TOMOYO_USE_KERNEL_MODULE] = "use_kernel_module", |
121 |
[TOMOYO_CREATE_FIFO] = "create_fifo", |
122 |
[TOMOYO_CREATE_BLOCK_DEV] = "create_block_dev", |
123 |
[TOMOYO_CREATE_CHAR_DEV] = "create_char_dev", |
124 |
[TOMOYO_CREATE_UNIX_SOCKET] = "create_unix_socket", |
125 |
[TOMOYO_SYS_LINK] = "SYS_LINK", |
126 |
[TOMOYO_SYS_SYMLINK] = "SYS_SYMLINK", |
127 |
[TOMOYO_SYS_RENAME] = "SYS_RENAME", |
128 |
[TOMOYO_SYS_UNLINK] = "SYS_UNLINK", |
129 |
[TOMOYO_SYS_CHMOD] = "SYS_CHMOD", |
130 |
[TOMOYO_SYS_CHOWN] = "SYS_CHOWN", |
131 |
[TOMOYO_SYS_IOCTL] = "SYS_IOCTL", |
132 |
[TOMOYO_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD", |
133 |
[TOMOYO_SYS_PIVOT_ROOT] = "SYS_PIVOT_ROOT", |
134 |
[TOMOYO_SYS_PTRACE] = "SYS_PTRACE", |
135 |
}; |
136 |
#endif |
137 |
|
138 |
/* Profile table. Memory is allocated as needed. */ |
139 |
static struct profile { |
140 |
unsigned int value[CCS_MAX_CONTROL_INDEX]; |
141 |
const struct path_info *comment; |
142 |
#ifdef CONFIG_TOMOYO |
143 |
unsigned char capability_value[TOMOYO_MAX_CAPABILITY_INDEX]; |
144 |
#endif |
145 |
} *profile_ptr[MAX_PROFILES]; |
146 |
|
147 |
/* Permit policy management by non-root user? */ |
148 |
static bool manage_by_non_root; |
149 |
|
150 |
/* Utility functions. */ |
151 |
|
152 |
#ifdef CONFIG_TOMOYO |
153 |
/** |
154 |
* tomoyo_quiet_setup - Set TOMOYO_VERBOSE=0 by default. |
155 |
* |
156 |
* @str: Unused. |
157 |
* |
158 |
* Returns 0. |
159 |
*/ |
160 |
static int __init tomoyo_quiet_setup(char *str) |
161 |
{ |
162 |
ccs_control_array[CCS_TOMOYO_VERBOSE].current_value = 0; |
163 |
return 0; |
164 |
} |
165 |
|
166 |
__setup("TOMOYO_QUIET", tomoyo_quiet_setup); |
167 |
#endif |
168 |
|
169 |
/** |
170 |
* is_byte_range - Check whether the string isa \ooo style octal value. |
171 |
* |
172 |
* @str: Pointer to the string. |
173 |
* |
174 |
* Returns true if @str is a \ooo style octal value, false otherwise. |
175 |
*/ |
176 |
static bool is_byte_range(const char *str) |
177 |
{ |
178 |
return *str >= '0' && *str++ <= '3' && |
179 |
*str >= '0' && *str++ <= '7' && |
180 |
*str >= '0' && *str <= '7'; |
181 |
} |
182 |
|
183 |
/** |
184 |
* is_decimal - Check whether the character is a decimal character. |
185 |
* |
186 |
* @c: The character to check. |
187 |
* |
188 |
* Returns true if @c is a decimal character, false otherwise. |
189 |
*/ |
190 |
static bool is_decimal(const char c) |
191 |
{ |
192 |
return c >= '0' && c <= '9'; |
193 |
} |
194 |
|
195 |
/** |
196 |
* is_hexadecimal - Check whether the character is a hexadecimal character. |
197 |
* |
198 |
* @c: The character to check. |
199 |
* |
200 |
* Returns true if @c is a hexadecimal character, false otherwise. |
201 |
*/ |
202 |
static bool is_hexadecimal(const char c) |
203 |
{ |
204 |
return (c >= '0' && c <= '9') || |
205 |
(c >= 'A' && c <= 'F') || |
206 |
(c >= 'a' && c <= 'f'); |
207 |
} |
208 |
|
209 |
/** |
210 |
* is_alphabet_char - Check whether the character is an alphabet. |
211 |
* |
212 |
* @c: The character to check. |
213 |
* |
214 |
* Returns true if @c is an alphabet character, false otherwise. |
215 |
*/ |
216 |
static bool is_alphabet_char(const char c) |
217 |
{ |
218 |
return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); |
219 |
} |
220 |
|
221 |
/** |
222 |
* make_byte - Make byte value from three octal characters. |
223 |
* |
224 |
* @c1: The first character. |
225 |
* @c2: The second character. |
226 |
* @c3: The third character. |
227 |
* |
228 |
* Returns byte value. |
229 |
*/ |
230 |
static u8 make_byte(const u8 c1, const u8 c2, const u8 c3) |
231 |
{ |
232 |
return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0'); |
233 |
} |
234 |
|
235 |
/** |
236 |
* str_starts - Check whether the given string starts with the given keyword. |
237 |
* |
238 |
* @src: Pointer to pointer to the string. |
239 |
* @find: Pointer to the keyword. |
240 |
* |
241 |
* Returns true if @src starts with @find, false otherwise. |
242 |
* |
243 |
* The @src is updated to point the first character after the @find |
244 |
* if @src starts with @find. |
245 |
*/ |
246 |
static bool str_starts(char **src, const char *find) |
247 |
{ |
248 |
const int len = strlen(find); |
249 |
char *tmp = *src; |
250 |
if (strncmp(tmp, find, len)) |
251 |
return false; |
252 |
tmp += len; |
253 |
*src = tmp; |
254 |
return true; |
255 |
} |
256 |
|
257 |
/** |
258 |
* normalize_line - Format string. |
259 |
* |
260 |
* @buffer: The line to normalize. |
261 |
* |
262 |
* Leading and trailing whitespaces are removed. |
263 |
* Multiple whitespaces are packed into single space. |
264 |
* |
265 |
* Returns nothing. |
266 |
*/ |
267 |
static void normalize_line(unsigned char *buffer) |
268 |
{ |
269 |
unsigned char *sp = buffer; |
270 |
unsigned char *dp = buffer; |
271 |
bool first = true; |
272 |
while (*sp && (*sp <= ' ' || *sp >= 127)) |
273 |
sp++; |
274 |
while (*sp) { |
275 |
if (!first) |
276 |
*dp++ = ' '; |
277 |
first = false; |
278 |
while (*sp > ' ' && *sp < 127) |
279 |
*dp++ = *sp++; |
280 |
while (*sp && (*sp <= ' ' || *sp >= 127)) |
281 |
sp++; |
282 |
} |
283 |
*dp = '\0'; |
284 |
} |
285 |
|
286 |
/** |
287 |
* ccs_is_correct_path - Validate a pathname. |
288 |
* @filename: The pathname to check. |
289 |
* @start_type: Should the pathname start with '/'? |
290 |
* 1 = must / -1 = must not / 0 = don't care |
291 |
* @pattern_type: Can the pathname contain a wildcard? |
292 |
* 1 = must / -1 = must not / 0 = don't care |
293 |
* @end_type: Should the pathname end with '/'? |
294 |
* 1 = must / -1 = must not / 0 = don't care |
295 |
* @function: The name of function calling me. |
296 |
* |
297 |
* Check whether the given filename follows the naming rules. |
298 |
* Returns true if @filename follows the naming rules, false otherwise. |
299 |
*/ |
300 |
bool ccs_is_correct_path(const char *filename, const s8 start_type, |
301 |
const s8 pattern_type, const s8 end_type, |
302 |
const char *function) |
303 |
{ |
304 |
bool contains_pattern = false; |
305 |
unsigned char c; |
306 |
unsigned char d; |
307 |
unsigned char e; |
308 |
const char *original_filename = filename; |
309 |
if (!filename) |
310 |
goto out; |
311 |
c = *filename; |
312 |
if (start_type == 1) { /* Must start with '/' */ |
313 |
if (c != '/') |
314 |
goto out; |
315 |
} else if (start_type == -1) { /* Must not start with '/' */ |
316 |
if (c == '/') |
317 |
goto out; |
318 |
} |
319 |
if (c) |
320 |
c = *(strchr(filename, '\0') - 1); |
321 |
if (end_type == 1) { /* Must end with '/' */ |
322 |
if (c != '/') |
323 |
goto out; |
324 |
} else if (end_type == -1) { /* Must not end with '/' */ |
325 |
if (c == '/') |
326 |
goto out; |
327 |
} |
328 |
while ((c = *filename++) != '\0') { |
329 |
if (c == '\\') { |
330 |
switch ((c = *filename++)) { |
331 |
case '\\': /* "\\" */ |
332 |
continue; |
333 |
case '$': /* "\$" */ |
334 |
case '+': /* "\+" */ |
335 |
case '?': /* "\?" */ |
336 |
case '*': /* "\*" */ |
337 |
case '@': /* "\@" */ |
338 |
case 'x': /* "\x" */ |
339 |
case 'X': /* "\X" */ |
340 |
case 'a': /* "\a" */ |
341 |
case 'A': /* "\A" */ |
342 |
case '-': /* "\-" */ |
343 |
if (pattern_type == -1) |
344 |
break; /* Must not contain pattern */ |
345 |
contains_pattern = true; |
346 |
continue; |
347 |
case '0': /* "\ooo" */ |
348 |
case '1': |
349 |
case '2': |
350 |
case '3': |
351 |
d = *filename++; |
352 |
if (d < '0' || d > '7') |
353 |
break; |
354 |
e = *filename++; |
355 |
if (e < '0' || e > '7') |
356 |
break; |
357 |
c = make_byte(c, d, e); |
358 |
if (c && (c <= ' ' || c >= 127)) |
359 |
continue; /* pattern is not \000 */ |
360 |
} |
361 |
goto out; |
362 |
} else if (c <= ' ' || c >= 127) { |
363 |
goto out; |
364 |
} |
365 |
} |
366 |
if (pattern_type == 1) { /* Must contain pattern */ |
367 |
if (!contains_pattern) |
368 |
goto out; |
369 |
} |
370 |
return true; |
371 |
out: |
372 |
printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function, |
373 |
original_filename); |
374 |
return false; |
375 |
} |
376 |
|
377 |
/** |
378 |
* ccs_is_correct_domain - Check whether the given domainname follows the naming rules. |
379 |
* @domainname: The domainname to check. |
380 |
* @function: The name of function calling me. |
381 |
* |
382 |
* Returns true if @domainname follows the naming rules, false otherwise. |
383 |
*/ |
384 |
bool ccs_is_correct_domain(const unsigned char *domainname, |
385 |
const char *function) |
386 |
{ |
387 |
unsigned char c; |
388 |
unsigned char d; |
389 |
unsigned char e; |
390 |
const char *org_domainname = domainname; |
391 |
if (!domainname || strncmp(domainname, ROOT_NAME, ROOT_NAME_LEN)) |
392 |
goto out; |
393 |
domainname += ROOT_NAME_LEN; |
394 |
if (!*domainname) |
395 |
return true; |
396 |
do { |
397 |
if (*domainname++ != ' ') |
398 |
goto out; |
399 |
if (*domainname++ != '/') |
400 |
goto out; |
401 |
while ((c = *domainname) != '\0' && c != ' ') { |
402 |
domainname++; |
403 |
if (c == '\\') { |
404 |
c = *domainname++; |
405 |
switch ((c)) { |
406 |
case '\\': /* "\\" */ |
407 |
continue; |
408 |
case '0': /* "\ooo" */ |
409 |
case '1': |
410 |
case '2': |
411 |
case '3': |
412 |
d = *domainname++; |
413 |
if (d < '0' || d > '7') |
414 |
break; |
415 |
e = *domainname++; |
416 |
if (e < '0' || e > '7') |
417 |
break; |
418 |
c = make_byte(c, d, e); |
419 |
if (c && (c <= ' ' || c >= 127)) |
420 |
/* pattern is not \000 */ |
421 |
continue; |
422 |
} |
423 |
goto out; |
424 |
} else if (c < ' ' || c >= 127) { |
425 |
goto out; |
426 |
} |
427 |
} |
428 |
} while (*domainname); |
429 |
return true; |
430 |
out: |
431 |
printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function, |
432 |
org_domainname); |
433 |
return false; |
434 |
} |
435 |
|
436 |
/** |
437 |
* ccs_is_domain_def - Check whether the given token can be a domainname. |
438 |
* |
439 |
* @buffer: The token to check. |
440 |
* |
441 |
* Returns true if @buffer possibly be a domainname, false otherwise. |
442 |
*/ |
443 |
bool ccs_is_domain_def(const unsigned char *buffer) |
444 |
{ |
445 |
return !strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN); |
446 |
} |
447 |
|
448 |
/** |
449 |
* ccs_find_domain - Find a domain by the given name. |
450 |
* |
451 |
* @domainname: The domainname to find. |
452 |
* |
453 |
* Returns pointer to "struct domain_info" if found, NULL otherwise. |
454 |
*/ |
455 |
struct domain_info *ccs_find_domain(const char *domainname) |
456 |
{ |
457 |
struct domain_info *domain; |
458 |
struct path_info name; |
459 |
name.name = domainname; |
460 |
ccs_fill_path_info(&name); |
461 |
list1_for_each_entry(domain, &domain_list, list) { |
462 |
if (!domain->is_deleted && |
463 |
!ccs_pathcmp(&name, domain->domainname)) |
464 |
return domain; |
465 |
} |
466 |
return NULL; |
467 |
} |
468 |
|
469 |
/** |
470 |
* path_depth - Evaluate the number of '/' in a string. |
471 |
* |
472 |
* @pathname: The string to evaluate. |
473 |
* |
474 |
* Returns path depth of the string. |
475 |
* |
476 |
* I score 2 for each of the '/' in the @pathname |
477 |
* and score 1 if the @pathname ends with '/'. |
478 |
*/ |
479 |
static int path_depth(const char *pathname) |
480 |
{ |
481 |
int i = 0; |
482 |
if (pathname) { |
483 |
char *ep = strchr(pathname, '\0'); |
484 |
if (pathname < ep--) { |
485 |
if (*ep != '/') |
486 |
i++; |
487 |
while (pathname <= ep) |
488 |
if (*ep-- == '/') |
489 |
i += 2; |
490 |
} |
491 |
} |
492 |
return i; |
493 |
} |
494 |
|
495 |
/** |
496 |
* const_part_length - Evaluate the initial length without a pattern in a token. |
497 |
* |
498 |
* @filename: The string to evaluate. |
499 |
* |
500 |
* Returns the initial length without a pattern in @filename. |
501 |
*/ |
502 |
static int const_part_length(const char *filename) |
503 |
{ |
504 |
char c; |
505 |
int len = 0; |
506 |
if (!filename) |
507 |
return 0; |
508 |
while ((c = *filename++) != '\0') { |
509 |
if (c != '\\') { |
510 |
len++; |
511 |
continue; |
512 |
} |
513 |
c = *filename++; |
514 |
switch (c) { |
515 |
case '\\': /* "\\" */ |
516 |
len += 2; |
517 |
continue; |
518 |
case '0': /* "\ooo" */ |
519 |
case '1': |
520 |
case '2': |
521 |
case '3': |
522 |
c = *filename++; |
523 |
if (c < '0' || c > '7') |
524 |
break; |
525 |
c = *filename++; |
526 |
if (c < '0' || c > '7') |
527 |
break; |
528 |
len += 4; |
529 |
continue; |
530 |
} |
531 |
break; |
532 |
} |
533 |
return len; |
534 |
} |
535 |
|
536 |
/** |
537 |
* ccs_fill_path_info - Fill in "struct path_info" members. |
538 |
* |
539 |
* @ptr: Pointer to "struct path_info" to fill in. |
540 |
* |
541 |
* The caller sets "struct path_info"->name. |
542 |
*/ |
543 |
void ccs_fill_path_info(struct path_info *ptr) |
544 |
{ |
545 |
const char *name = ptr->name; |
546 |
const int len = strlen(name); |
547 |
ptr->total_len = len; |
548 |
ptr->const_len = const_part_length(name); |
549 |
ptr->is_dir = len && (name[len - 1] == '/'); |
550 |
ptr->is_patterned = (ptr->const_len < len); |
551 |
ptr->hash = full_name_hash(name, len); |
552 |
ptr->depth = path_depth(name); |
553 |
} |
554 |
|
555 |
/** |
556 |
* file_matches_to_pattern2 - Pattern matching without '/' character |
557 |
* and "\-" pattern. |
558 |
* |
559 |
* @filename: The start of string to check. |
560 |
* @filename_end: The end of string to check. |
561 |
* @pattern: The start of pattern to compare. |
562 |
* @pattern_end: The end of pattern to compare. |
563 |
* |
564 |
* Returns true if @filename matches @pattern, false otherwise. |
565 |
*/ |
566 |
static bool file_matches_to_pattern2(const char *filename, |
567 |
const char *filename_end, |
568 |
const char *pattern, |
569 |
const char *pattern_end) |
570 |
{ |
571 |
while (filename < filename_end && pattern < pattern_end) { |
572 |
char c; |
573 |
if (*pattern != '\\') { |
574 |
if (*filename++ != *pattern++) |
575 |
return false; |
576 |
continue; |
577 |
} |
578 |
c = *filename; |
579 |
pattern++; |
580 |
switch (*pattern) { |
581 |
int i; |
582 |
int j; |
583 |
case '?': |
584 |
if (c == '/') { |
585 |
return false; |
586 |
} else if (c == '\\') { |
587 |
if (filename[1] == '\\') |
588 |
filename++; |
589 |
else if (is_byte_range(filename + 1)) |
590 |
filename += 3; |
591 |
else |
592 |
return false; |
593 |
} |
594 |
break; |
595 |
case '\\': |
596 |
if (c != '\\') |
597 |
return false; |
598 |
if (*++filename != '\\') |
599 |
return false; |
600 |
break; |
601 |
case '+': |
602 |
if (!is_decimal(c)) |
603 |
return false; |
604 |
break; |
605 |
case 'x': |
606 |
if (!is_hexadecimal(c)) |
607 |
return false; |
608 |
break; |
609 |
case 'a': |
610 |
if (!is_alphabet_char(c)) |
611 |
return false; |
612 |
break; |
613 |
case '0': |
614 |
case '1': |
615 |
case '2': |
616 |
case '3': |
617 |
if (c == '\\' && is_byte_range(filename + 1) |
618 |
&& strncmp(filename + 1, pattern, 3) == 0) { |
619 |
filename += 3; |
620 |
pattern += 2; |
621 |
break; |
622 |
} |
623 |
return false; /* Not matched. */ |
624 |
case '*': |
625 |
case '@': |
626 |
for (i = 0; i <= filename_end - filename; i++) { |
627 |
if (file_matches_to_pattern2(filename + i, |
628 |
filename_end, |
629 |
pattern + 1, |
630 |
pattern_end)) |
631 |
return true; |
632 |
c = filename[i]; |
633 |
if (c == '.' && *pattern == '@') |
634 |
break; |
635 |
if (c != '\\') |
636 |
continue; |
637 |
if (filename[i + 1] == '\\') |
638 |
i++; |
639 |
else if (is_byte_range(filename + i + 1)) |
640 |
i += 3; |
641 |
else |
642 |
break; /* Bad pattern. */ |
643 |
} |
644 |
return false; /* Not matched. */ |
645 |
default: |
646 |
j = 0; |
647 |
c = *pattern; |
648 |
if (c == '$') { |
649 |
while (is_decimal(filename[j])) |
650 |
j++; |
651 |
} else if (c == 'X') { |
652 |
while (is_hexadecimal(filename[j])) |
653 |
j++; |
654 |
} else if (c == 'A') { |
655 |
while (is_alphabet_char(filename[j])) |
656 |
j++; |
657 |
} |
658 |
for (i = 1; i <= j; i++) { |
659 |
if (file_matches_to_pattern2(filename + i, |
660 |
filename_end, |
661 |
pattern + 1, |
662 |
pattern_end)) |
663 |
return true; |
664 |
} |
665 |
return false; /* Not matched or bad pattern. */ |
666 |
} |
667 |
filename++; |
668 |
pattern++; |
669 |
} |
670 |
while (*pattern == '\\' && |
671 |
(*(pattern + 1) == '*' || *(pattern + 1) == '@')) |
672 |
pattern += 2; |
673 |
return filename == filename_end && pattern == pattern_end; |
674 |
} |
675 |
|
676 |
/** |
677 |
* file_matches_to_pattern - Pattern matching without without '/' character. |
678 |
* |
679 |
* @filename: The start of string to check. |
680 |
* @filename_end: The end of string to check. |
681 |
* @pattern: The start of pattern to compare. |
682 |
* @pattern_end: The end of pattern to compare. |
683 |
* |
684 |
* Returns true if @filename matches @pattern, false otherwise. |
685 |
*/ |
686 |
static bool file_matches_to_pattern(const char *filename, |
687 |
const char *filename_end, |
688 |
const char *pattern, |
689 |
const char *pattern_end) |
690 |
{ |
691 |
const char *pattern_start = pattern; |
692 |
bool first = true; |
693 |
bool result; |
694 |
while (pattern < pattern_end - 1) { |
695 |
/* Split at "\-" pattern. */ |
696 |
if (*pattern++ != '\\' || *pattern++ != '-') |
697 |
continue; |
698 |
result = file_matches_to_pattern2(filename, filename_end, |
699 |
pattern_start, pattern - 2); |
700 |
if (first) |
701 |
result = !result; |
702 |
if (result) |
703 |
return false; |
704 |
first = false; |
705 |
pattern_start = pattern; |
706 |
} |
707 |
result = file_matches_to_pattern2(filename, filename_end, |
708 |
pattern_start, pattern_end); |
709 |
return first ? result : !result; |
710 |
} |
711 |
|
712 |
/** |
713 |
* ccs_path_matches_pattern - Check whether the given filename matches the given pattern. |
714 |
* @filename: The filename to check. |
715 |
* @pattern: The pattern to compare. |
716 |
* |
717 |
* Returns true if matches, false otherwise. |
718 |
* |
719 |
* The following patterns are available. |
720 |
* \\ \ itself. |
721 |
* \ooo Octal representation of a byte. |
722 |
* \* More than or equals to 0 character other than '/'. |
723 |
* \@ More than or equals to 0 character other than '/' or '.'. |
724 |
* \? 1 byte character other than '/'. |
725 |
* \$ More than or equals to 1 decimal digit. |
726 |
* \+ 1 decimal digit. |
727 |
* \X More than or equals to 1 hexadecimal digit. |
728 |
* \x 1 hexadecimal digit. |
729 |
* \A More than or equals to 1 alphabet character. |
730 |
* \a 1 alphabet character. |
731 |
* \- Subtraction operator. |
732 |
*/ |
733 |
bool ccs_path_matches_pattern(const struct path_info *filename, |
734 |
const struct path_info *pattern) |
735 |
{ |
736 |
/* |
737 |
if (!filename || !pattern) |
738 |
return false; |
739 |
*/ |
740 |
const char *f = filename->name; |
741 |
const char *p = pattern->name; |
742 |
const int len = pattern->const_len; |
743 |
/* If @pattern doesn't contain pattern, I can use strcmp(). */ |
744 |
if (!pattern->is_patterned) |
745 |
return !ccs_pathcmp(filename, pattern); |
746 |
/* Dont compare if the number of '/' differs. */ |
747 |
if (filename->depth != pattern->depth) |
748 |
return false; |
749 |
/* Compare the initial length without patterns. */ |
750 |
if (strncmp(f, p, len)) |
751 |
return false; |
752 |
f += len; |
753 |
p += len; |
754 |
/* Main loop. Compare each directory component. */ |
755 |
while (*f && *p) { |
756 |
const char *f_delimiter = strchr(f, '/'); |
757 |
const char *p_delimiter = strchr(p, '/'); |
758 |
if (!f_delimiter) |
759 |
f_delimiter = strchr(f, '\0'); |
760 |
if (!p_delimiter) |
761 |
p_delimiter = strchr(p, '\0'); |
762 |
if (!file_matches_to_pattern(f, f_delimiter, p, p_delimiter)) |
763 |
return false; |
764 |
f = f_delimiter; |
765 |
if (*f) |
766 |
f++; |
767 |
p = p_delimiter; |
768 |
if (*p) |
769 |
p++; |
770 |
} |
771 |
/* Ignore trailing "\*" and "\@" in @pattern. */ |
772 |
while (*p == '\\' && |
773 |
(*(p + 1) == '*' || *(p + 1) == '@')) |
774 |
p += 2; |
775 |
return !*f && !*p; |
776 |
} |
777 |
|
778 |
/** |
779 |
* ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure. |
780 |
* |
781 |
* @head: Pointer to "struct ccs_io_buffer". |
782 |
* @fmt: The printf()'s format string, followed by parameters. |
783 |
* |
784 |
* Returns true on success, false otherwise. |
785 |
* |
786 |
* The snprintf() will truncate, but ccs_io_printf() won't. |
787 |
*/ |
788 |
bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...) |
789 |
{ |
790 |
va_list args; |
791 |
int len; |
792 |
int pos = head->read_avail; |
793 |
int size = head->readbuf_size - pos; |
794 |
if (size <= 0) |
795 |
return false; |
796 |
va_start(args, fmt); |
797 |
len = vsnprintf(head->read_buf + pos, size, fmt, args); |
798 |
va_end(args); |
799 |
if (pos + len >= head->readbuf_size) |
800 |
return false; |
801 |
head->read_avail += len; |
802 |
return true; |
803 |
} |
804 |
|
805 |
/** |
806 |
* ccs_get_exe - Get ccs_realpath() of current process. |
807 |
* |
808 |
* Returns the ccs_realpath() of current process on success, NULL otherwise. |
809 |
* |
810 |
* This function uses ccs_alloc(), so the caller must ccs_free() |
811 |
* if this function didn't return NULL. |
812 |
*/ |
813 |
const char *ccs_get_exe(void) |
814 |
{ |
815 |
struct mm_struct *mm = current->mm; |
816 |
struct vm_area_struct *vma; |
817 |
const char *cp = NULL; |
818 |
if (!mm) |
819 |
return NULL; |
820 |
down_read(&mm->mmap_sem); |
821 |
for (vma = mm->mmap; vma; vma = vma->vm_next) { |
822 |
if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) { |
823 |
cp = ccs_realpath_from_dentry(vma->vm_file->f_dentry, |
824 |
vma->vm_file->f_vfsmnt); |
825 |
break; |
826 |
} |
827 |
} |
828 |
up_read(&mm->mmap_sem); |
829 |
return cp; |
830 |
} |
831 |
|
832 |
/** |
833 |
* ccs_get_msg - Get warning message. |
834 |
* |
835 |
* @is_enforce: Is it enforcing mode? |
836 |
* |
837 |
* Returns "ERROR" or "WARNING". |
838 |
*/ |
839 |
const char *ccs_get_msg(const bool is_enforce) |
840 |
{ |
841 |
if (is_enforce) |
842 |
return "ERROR"; |
843 |
else |
844 |
return "WARNING"; |
845 |
} |
846 |
|
847 |
/** |
848 |
* ccs_can_sleep - Check whether it is permitted to do operations that may sleep. |
849 |
* |
850 |
* Returns true if it is permitted to do operations that may sleep, |
851 |
* false otherwise. |
852 |
* |
853 |
* TOMOYO Linux supports interactive enforcement that lets processes |
854 |
* wait for the administrator's decision. |
855 |
* All hooks but the one for ccs_may_autobind() are inserted where |
856 |
* it is permitted to do operations that may sleep. |
857 |
* Thus, this warning should not happen. |
858 |
*/ |
859 |
bool ccs_can_sleep(void) |
860 |
{ |
861 |
static u8 count = 20; |
862 |
if (likely(!in_interrupt())) |
863 |
return true; |
864 |
if (count) { |
865 |
count--; |
866 |
printk(KERN_ERR "BUG: sleeping function called " |
867 |
"from invalid context.\n"); |
868 |
dump_stack(); |
869 |
} |
870 |
return false; |
871 |
} |
872 |
|
873 |
/** |
874 |
* ccs_check_flags - Check mode for specified functionality. |
875 |
* |
876 |
* @domain: Pointer to "struct domain_info". NULL for current->domain_info. |
877 |
* @index: The functionality to check mode. |
878 |
* |
879 |
* Returns the mode of specified functionality. |
880 |
*/ |
881 |
unsigned int ccs_check_flags(const struct domain_info *domain, const u8 index) |
882 |
{ |
883 |
u8 profile; |
884 |
if (!domain) |
885 |
domain = current->domain_info; |
886 |
profile = domain->profile; |
887 |
return sbin_init_started && index < CCS_MAX_CONTROL_INDEX |
888 |
#if MAX_PROFILES != 256 |
889 |
&& profile < MAX_PROFILES |
890 |
#endif |
891 |
&& profile_ptr[profile] ? |
892 |
profile_ptr[profile]->value[index] : 0; |
893 |
} |
894 |
|
895 |
#ifdef CONFIG_TOMOYO |
896 |
/** |
897 |
* ccs_check_capability_flags - Check mode for specified capability. |
898 |
* |
899 |
* @domain: Pointer to "struct domain_info". NULL for current->domain_info. |
900 |
* @index: The capability to check mode. |
901 |
* |
902 |
* Returns the mode of specified capability. |
903 |
*/ |
904 |
static u8 ccs_check_capability_flags(const struct domain_info *domain, |
905 |
const u8 index) |
906 |
{ |
907 |
const u8 profile = domain ? domain->profile : |
908 |
current->domain_info->profile; |
909 |
return sbin_init_started && index < TOMOYO_MAX_CAPABILITY_INDEX |
910 |
#if MAX_PROFILES != 256 |
911 |
&& profile < MAX_PROFILES |
912 |
#endif |
913 |
&& profile_ptr[profile] ? |
914 |
profile_ptr[profile]->capability_value[index] : 0; |
915 |
} |
916 |
|
917 |
/** |
918 |
* ccs_cap2keyword - Convert capability operation to capability name. |
919 |
* |
920 |
* @operation: The capability index. |
921 |
* |
922 |
* Returns the name of the specified capability's name. |
923 |
*/ |
924 |
const char *ccs_cap2keyword(const u8 operation) |
925 |
{ |
926 |
return operation < TOMOYO_MAX_CAPABILITY_INDEX |
927 |
? capability_control_keyword[operation] : NULL; |
928 |
} |
929 |
|
930 |
#endif |
931 |
|
932 |
/** |
933 |
* ccs_init_request_info - Initialize "struct ccs_request_info" members. |
934 |
* |
935 |
* @r: Pointer to "struct ccs_request_info" to initialize. |
936 |
* @domain: Pointer to "struct domain_info". NULL for current->domain_info. |
937 |
* @index: Index number of functionality. |
938 |
*/ |
939 |
void ccs_init_request_info(struct ccs_request_info *r, |
940 |
struct domain_info *domain, const u8 index) |
941 |
{ |
942 |
memset(r, 0, sizeof(*r)); |
943 |
if (!domain) |
944 |
domain = current->domain_info; |
945 |
r->domain = domain; |
946 |
r->profile = domain->profile; |
947 |
if (index < CCS_MAX_CONTROL_INDEX) |
948 |
r->mode = ccs_check_flags(domain, index); |
949 |
#ifdef CONFIG_TOMOYO |
950 |
else |
951 |
r->mode = ccs_check_capability_flags(domain, index |
952 |
- CCS_MAX_CONTROL_INDEX); |
953 |
#endif |
954 |
r->tomoyo_flags = current->tomoyo_flags; |
955 |
} |
956 |
|
957 |
/** |
958 |
* ccs_verbose_mode - Check whether TOMOYO is verbose mode. |
959 |
* |
960 |
* @domain: Pointer to "struct domain_info". NULL for current->domain_info. |
961 |
* |
962 |
* Returns true if domain policy violation warning should be printed to |
963 |
* console. |
964 |
*/ |
965 |
bool ccs_verbose_mode(const struct domain_info *domain) |
966 |
{ |
967 |
return ccs_check_flags(domain, CCS_TOMOYO_VERBOSE) != 0; |
968 |
} |
969 |
|
970 |
/** |
971 |
* ccs_check_domain_quota - Check for domain's quota. |
972 |
* |
973 |
* @domain: Pointer to "struct domain_info". |
974 |
* |
975 |
* Returns true if the domain is not exceeded quota, false otherwise. |
976 |
*/ |
977 |
bool ccs_check_domain_quota(struct domain_info * const domain) |
978 |
{ |
979 |
unsigned int count = 0; |
980 |
struct acl_info *ptr; |
981 |
if (!domain) |
982 |
return true; |
983 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
984 |
if (ptr->type & ACL_DELETED) |
985 |
continue; |
986 |
switch (ccs_acl_type2(ptr)) { |
987 |
struct single_path_acl_record *acl1; |
988 |
struct double_path_acl_record *acl2; |
989 |
u16 perm; |
990 |
case TYPE_SINGLE_PATH_ACL: |
991 |
acl1 = container_of(ptr, struct single_path_acl_record, |
992 |
head); |
993 |
perm = acl1->perm; |
994 |
if (perm & (1 << TYPE_EXECUTE_ACL)) |
995 |
count++; |
996 |
if (perm & |
997 |
((1 << TYPE_READ_ACL) | (1 << TYPE_WRITE_ACL))) |
998 |
count++; |
999 |
if (perm & (1 << TYPE_CREATE_ACL)) |
1000 |
count++; |
1001 |
if (perm & (1 << TYPE_UNLINK_ACL)) |
1002 |
count++; |
1003 |
if (perm & (1 << TYPE_MKDIR_ACL)) |
1004 |
count++; |
1005 |
if (perm & (1 << TYPE_RMDIR_ACL)) |
1006 |
count++; |
1007 |
if (perm & (1 << TYPE_MKFIFO_ACL)) |
1008 |
count++; |
1009 |
if (perm & (1 << TYPE_MKSOCK_ACL)) |
1010 |
count++; |
1011 |
if (perm & (1 << TYPE_MKBLOCK_ACL)) |
1012 |
count++; |
1013 |
if (perm & (1 << TYPE_MKCHAR_ACL)) |
1014 |
count++; |
1015 |
if (perm & (1 << TYPE_TRUNCATE_ACL)) |
1016 |
count++; |
1017 |
if (perm & (1 << TYPE_SYMLINK_ACL)) |
1018 |
count++; |
1019 |
if (perm & (1 << TYPE_REWRITE_ACL)) |
1020 |
count++; |
1021 |
break; |
1022 |
case TYPE_DOUBLE_PATH_ACL: |
1023 |
acl2 = container_of(ptr, struct double_path_acl_record, |
1024 |
head); |
1025 |
perm = acl2->perm; |
1026 |
if (perm & (1 << TYPE_LINK_ACL)) |
1027 |
count++; |
1028 |
if (perm & (1 << TYPE_RENAME_ACL)) |
1029 |
count++; |
1030 |
break; |
1031 |
case TYPE_EXECUTE_HANDLER: |
1032 |
case TYPE_DENIED_EXECUTE_HANDLER: |
1033 |
break; |
1034 |
default: |
1035 |
count++; |
1036 |
} |
1037 |
} |
1038 |
if (count < ccs_check_flags(domain, CCS_TOMOYO_MAX_ACCEPT_ENTRY)) |
1039 |
return true; |
1040 |
if (!domain->quota_warned) { |
1041 |
domain->quota_warned = true; |
1042 |
printk(KERN_WARNING "TOMOYO-WARNING: " |
1043 |
"Domain '%s' has so many ACLs to hold. " |
1044 |
"Stopped learning mode.\n", domain->domainname->name); |
1045 |
} |
1046 |
return false; |
1047 |
} |
1048 |
|
1049 |
/** |
1050 |
* ccs_find_or_assign_new_profile - Create a new profile. |
1051 |
* |
1052 |
* @profile: Profile number to create. |
1053 |
* |
1054 |
* Returns pointer to "struct profile" on success, NULL otherwise. |
1055 |
*/ |
1056 |
static struct profile *ccs_find_or_assign_new_profile(const unsigned int |
1057 |
profile) |
1058 |
{ |
1059 |
static DEFINE_MUTEX(profile_lock); |
1060 |
struct profile *ptr = NULL; |
1061 |
mutex_lock(&profile_lock); |
1062 |
if (profile < MAX_PROFILES) { |
1063 |
ptr = profile_ptr[profile]; |
1064 |
if (ptr) |
1065 |
goto ok; |
1066 |
ptr = ccs_alloc_element(sizeof(*ptr)); |
1067 |
if (ptr) { |
1068 |
int i; |
1069 |
for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) |
1070 |
ptr->value[i] |
1071 |
= ccs_control_array[i].current_value; |
1072 |
/* |
1073 |
* Needn't to initialize "ptr->capability_value" |
1074 |
* because they are always 0. |
1075 |
*/ |
1076 |
mb(); /* Avoid out-of-order execution. */ |
1077 |
profile_ptr[profile] = ptr; |
1078 |
} |
1079 |
} |
1080 |
ok: |
1081 |
mutex_unlock(&profile_lock); |
1082 |
return ptr; |
1083 |
} |
1084 |
|
1085 |
/** |
1086 |
* write_profile - Write profile table. |
1087 |
* |
1088 |
* @head: Pointer to "struct ccs_io_buffer". |
1089 |
* |
1090 |
* Returns 0 on success, negative value otherwise. |
1091 |
*/ |
1092 |
static int write_profile(struct ccs_io_buffer *head) |
1093 |
{ |
1094 |
char *data = head->write_buf; |
1095 |
unsigned int i; |
1096 |
unsigned int value; |
1097 |
char *cp; |
1098 |
struct profile *profile; |
1099 |
i = simple_strtoul(data, &cp, 10); |
1100 |
if (data != cp) { |
1101 |
if (*cp != '-') |
1102 |
return -EINVAL; |
1103 |
data = cp + 1; |
1104 |
} |
1105 |
profile = ccs_find_or_assign_new_profile(i); |
1106 |
if (!profile) |
1107 |
return -EINVAL; |
1108 |
cp = strchr(data, '='); |
1109 |
if (!cp) |
1110 |
return -EINVAL; |
1111 |
*cp = '\0'; |
1112 |
ccs_update_counter(CCS_UPDATES_COUNTER_PROFILE); |
1113 |
if (!strcmp(data, "COMMENT")) { |
1114 |
profile->comment = ccs_save_name(cp + 1); |
1115 |
return 0; |
1116 |
} |
1117 |
#ifdef CONFIG_TOMOYO |
1118 |
if (str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) { |
1119 |
if (sscanf(cp + 1, "%u", &value) != 1) { |
1120 |
for (i = 0; i < 4; i++) { |
1121 |
if (strcmp(cp + 1, mode_4[i])) |
1122 |
continue; |
1123 |
value = i; |
1124 |
break; |
1125 |
} |
1126 |
if (i == 4) |
1127 |
return -EINVAL; |
1128 |
} |
1129 |
if (value > 3) |
1130 |
value = 3; |
1131 |
for (i = 0; i < TOMOYO_MAX_CAPABILITY_INDEX; i++) { |
1132 |
if (strcmp(data, capability_control_keyword[i])) |
1133 |
continue; |
1134 |
profile->capability_value[i] = value; |
1135 |
return 0; |
1136 |
} |
1137 |
return -EINVAL; |
1138 |
} |
1139 |
#endif |
1140 |
for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) { |
1141 |
if (strcmp(data, ccs_control_array[i].keyword)) |
1142 |
continue; |
1143 |
if (sscanf(cp + 1, "%u", &value) != 1) { |
1144 |
int j; |
1145 |
const char **modes; |
1146 |
switch (i) { |
1147 |
case CCS_SAKURA_RESTRICT_AUTOBIND: |
1148 |
case CCS_TOMOYO_VERBOSE: |
1149 |
modes = mode_2; |
1150 |
break; |
1151 |
default: |
1152 |
modes = mode_4; |
1153 |
break; |
1154 |
} |
1155 |
for (j = 0; j < 4; j++) { |
1156 |
if (strcmp(cp + 1, modes[j])) |
1157 |
continue; |
1158 |
value = j; |
1159 |
break; |
1160 |
} |
1161 |
if (j == 4) |
1162 |
return -EINVAL; |
1163 |
} else if (value > ccs_control_array[i].max_value) { |
1164 |
value = ccs_control_array[i].max_value; |
1165 |
} |
1166 |
switch (i) { |
1167 |
case CCS_SAKURA_DENY_CONCEAL_MOUNT: |
1168 |
case CCS_SAKURA_RESTRICT_UNMOUNT: |
1169 |
if (value == 1) |
1170 |
value = 2; /* learning mode is not supported. */ |
1171 |
} |
1172 |
profile->value[i] = value; |
1173 |
return 0; |
1174 |
} |
1175 |
return -EINVAL; |
1176 |
} |
1177 |
|
1178 |
/** |
1179 |
* read_profile - Read profile table. |
1180 |
* |
1181 |
* @head: Pointer to "struct ccs_io_buffer". |
1182 |
* |
1183 |
* Returns 0. |
1184 |
*/ |
1185 |
static int read_profile(struct ccs_io_buffer *head) |
1186 |
{ |
1187 |
static const int total |
1188 |
= CCS_MAX_CONTROL_INDEX + TOMOYO_MAX_CAPABILITY_INDEX + 1; |
1189 |
int step; |
1190 |
if (head->read_eof) |
1191 |
return 0; |
1192 |
for (step = head->read_step; step < MAX_PROFILES * total; step++) { |
1193 |
const u8 index = step / total; |
1194 |
u8 type = step % total; |
1195 |
const struct profile *profile = profile_ptr[index]; |
1196 |
head->read_step = step; |
1197 |
if (!profile) |
1198 |
continue; |
1199 |
#if !defined(CONFIG_SAKURA) || !defined(CONFIG_TOMOYO) |
1200 |
switch (type) { |
1201 |
#ifndef CONFIG_SAKURA |
1202 |
case CCS_SAKURA_DENY_CONCEAL_MOUNT: |
1203 |
case CCS_SAKURA_RESTRICT_CHROOT: |
1204 |
case CCS_SAKURA_RESTRICT_MOUNT: |
1205 |
case CCS_SAKURA_RESTRICT_UNMOUNT: |
1206 |
case CCS_SAKURA_RESTRICT_PIVOT_ROOT: |
1207 |
case CCS_SAKURA_RESTRICT_AUTOBIND: |
1208 |
#endif |
1209 |
#ifndef CONFIG_TOMOYO |
1210 |
case CCS_TOMOYO_MAC_FOR_FILE: |
1211 |
case CCS_TOMOYO_MAC_FOR_ARGV0: |
1212 |
case CCS_TOMOYO_MAC_FOR_ENV: |
1213 |
case CCS_TOMOYO_MAC_FOR_NETWORK: |
1214 |
case CCS_TOMOYO_MAC_FOR_SIGNAL: |
1215 |
case CCS_TOMOYO_MAX_ACCEPT_ENTRY: |
1216 |
case CCS_TOMOYO_MAX_GRANT_LOG: |
1217 |
case CCS_TOMOYO_MAX_REJECT_LOG: |
1218 |
case CCS_TOMOYO_VERBOSE: |
1219 |
#endif |
1220 |
continue; |
1221 |
} |
1222 |
#endif |
1223 |
if (!type) { /* Print profile' comment tag. */ |
1224 |
if (!ccs_io_printf(head, "%u-COMMENT=%s\n", |
1225 |
index, profile->comment ? |
1226 |
profile->comment->name : "")) |
1227 |
break; |
1228 |
continue; |
1229 |
} |
1230 |
type--; |
1231 |
if (type >= CCS_MAX_CONTROL_INDEX) { |
1232 |
#ifdef CONFIG_TOMOYO |
1233 |
const int i = type - CCS_MAX_CONTROL_INDEX; |
1234 |
const u8 value = profile->capability_value[i]; |
1235 |
if (!ccs_io_printf(head, |
1236 |
"%u-" KEYWORD_MAC_FOR_CAPABILITY |
1237 |
"%s=%s\n", index, |
1238 |
capability_control_keyword[i], |
1239 |
mode_4[value])) |
1240 |
break; |
1241 |
#endif |
1242 |
} else { |
1243 |
const unsigned int value = profile->value[type]; |
1244 |
const char **modes = NULL; |
1245 |
const char *keyword = ccs_control_array[type].keyword; |
1246 |
switch (ccs_control_array[type].max_value) { |
1247 |
case 3: |
1248 |
modes = mode_4; |
1249 |
break; |
1250 |
case 1: |
1251 |
modes = mode_2; |
1252 |
break; |
1253 |
} |
1254 |
if (modes) { |
1255 |
if (!ccs_io_printf(head, "%u-%s=%s\n", index, |
1256 |
keyword, modes[value])) |
1257 |
break; |
1258 |
} else { |
1259 |
if (!ccs_io_printf(head, "%u-%s=%u\n", index, |
1260 |
keyword, value)) |
1261 |
break; |
1262 |
} |
1263 |
} |
1264 |
} |
1265 |
if (step == MAX_PROFILES * total) |
1266 |
head->read_eof = true; |
1267 |
return 0; |
1268 |
} |
1269 |
|
1270 |
/* Structure for policy manager. */ |
1271 |
struct policy_manager_entry { |
1272 |
struct list1_head list; |
1273 |
/* A path to program or a domainname. */ |
1274 |
const struct path_info *manager; |
1275 |
bool is_domain; /* True if manager is a domainname. */ |
1276 |
bool is_deleted; /* True if this entry is deleted. */ |
1277 |
}; |
1278 |
|
1279 |
/* The list for "struct policy_manager_entry". */ |
1280 |
static LIST1_HEAD(policy_manager_list); |
1281 |
|
1282 |
/** |
1283 |
* update_manager_entry - Add a manager entry. |
1284 |
* |
1285 |
* @manager: The path to manager or the domainnamme. |
1286 |
* @is_delete: True if it is a delete request. |
1287 |
* |
1288 |
* Returns 0 on success, negative value otherwise. |
1289 |
*/ |
1290 |
static int update_manager_entry(const char *manager, const bool is_delete) |
1291 |
{ |
1292 |
struct policy_manager_entry *new_entry; |
1293 |
struct policy_manager_entry *ptr; |
1294 |
static DEFINE_MUTEX(lock); |
1295 |
const struct path_info *saved_manager; |
1296 |
int error = -ENOMEM; |
1297 |
bool is_domain = false; |
1298 |
if (ccs_is_domain_def(manager)) { |
1299 |
if (!ccs_is_correct_domain(manager, __func__)) |
1300 |
return -EINVAL; |
1301 |
is_domain = true; |
1302 |
} else { |
1303 |
if (!ccs_is_correct_path(manager, 1, -1, -1, __func__)) |
1304 |
return -EINVAL; |
1305 |
} |
1306 |
saved_manager = ccs_save_name(manager); |
1307 |
if (!saved_manager) |
1308 |
return -ENOMEM; |
1309 |
mutex_lock(&lock); |
1310 |
list1_for_each_entry(ptr, &policy_manager_list, list) { |
1311 |
if (ptr->manager != saved_manager) |
1312 |
continue; |
1313 |
ptr->is_deleted = is_delete; |
1314 |
error = 0; |
1315 |
goto out; |
1316 |
} |
1317 |
if (is_delete) { |
1318 |
error = -ENOENT; |
1319 |
goto out; |
1320 |
} |
1321 |
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
1322 |
if (!new_entry) |
1323 |
goto out; |
1324 |
new_entry->manager = saved_manager; |
1325 |
new_entry->is_domain = is_domain; |
1326 |
list1_add_tail_mb(&new_entry->list, &policy_manager_list); |
1327 |
error = 0; |
1328 |
out: |
1329 |
mutex_unlock(&lock); |
1330 |
if (!error) |
1331 |
ccs_update_counter(CCS_UPDATES_COUNTER_MANAGER); |
1332 |
return error; |
1333 |
} |
1334 |
|
1335 |
/** |
1336 |
* write_manager_policy - Write manager policy. |
1337 |
* |
1338 |
* @head: Pointer to "struct ccs_io_buffer". |
1339 |
* |
1340 |
* Returns 0 on success, negative value otherwise. |
1341 |
*/ |
1342 |
static int write_manager_policy(struct ccs_io_buffer *head) |
1343 |
{ |
1344 |
char *data = head->write_buf; |
1345 |
bool is_delete = str_starts(&data, KEYWORD_DELETE); |
1346 |
if (!strcmp(data, "manage_by_non_root")) { |
1347 |
manage_by_non_root = !is_delete; |
1348 |
return 0; |
1349 |
} |
1350 |
return update_manager_entry(data, is_delete); |
1351 |
} |
1352 |
|
1353 |
/** |
1354 |
* read_manager_policy - Read manager policy. |
1355 |
* |
1356 |
* @head: Pointer to "struct ccs_io_buffer". |
1357 |
* |
1358 |
* Returns 0. |
1359 |
*/ |
1360 |
static int read_manager_policy(struct ccs_io_buffer *head) |
1361 |
{ |
1362 |
struct list1_head *pos; |
1363 |
if (head->read_eof) |
1364 |
return 0; |
1365 |
list1_for_each_cookie(pos, head->read_var2, &policy_manager_list) { |
1366 |
struct policy_manager_entry *ptr; |
1367 |
ptr = list1_entry(pos, struct policy_manager_entry, list); |
1368 |
if (ptr->is_deleted) |
1369 |
continue; |
1370 |
if (!ccs_io_printf(head, "%s\n", ptr->manager->name)) |
1371 |
return 0; |
1372 |
} |
1373 |
head->read_eof = true; |
1374 |
return 0; |
1375 |
} |
1376 |
|
1377 |
/** |
1378 |
* is_policy_manager - Check whether the current process is a policy manager. |
1379 |
* |
1380 |
* Returns true if the current process is permitted to modify policy |
1381 |
* via /proc/ccs/ interface. |
1382 |
*/ |
1383 |
static bool is_policy_manager(void) |
1384 |
{ |
1385 |
struct policy_manager_entry *ptr; |
1386 |
const char *exe; |
1387 |
struct task_struct *task = current; |
1388 |
const struct path_info *domainname = task->domain_info->domainname; |
1389 |
bool found = false; |
1390 |
if (!sbin_init_started) |
1391 |
return true; |
1392 |
if (task->tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER) |
1393 |
return true; |
1394 |
if (!manage_by_non_root && (task->uid || task->euid)) |
1395 |
return false; |
1396 |
list1_for_each_entry(ptr, &policy_manager_list, list) { |
1397 |
if (!ptr->is_deleted && ptr->is_domain |
1398 |
&& !ccs_pathcmp(domainname, ptr->manager)) { |
1399 |
/* Set manager flag. */ |
1400 |
task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER; |
1401 |
return true; |
1402 |
} |
1403 |
} |
1404 |
exe = ccs_get_exe(); |
1405 |
if (!exe) |
1406 |
return false; |
1407 |
list1_for_each_entry(ptr, &policy_manager_list, list) { |
1408 |
if (!ptr->is_deleted && !ptr->is_domain |
1409 |
&& !strcmp(exe, ptr->manager->name)) { |
1410 |
found = true; |
1411 |
/* Set manager flag. */ |
1412 |
task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER; |
1413 |
break; |
1414 |
} |
1415 |
} |
1416 |
if (!found) { /* Reduce error messages. */ |
1417 |
static pid_t last_pid; |
1418 |
const pid_t pid = current->pid; |
1419 |
if (last_pid != pid) { |
1420 |
printk(KERN_WARNING "%s ( %s ) is not permitted to " |
1421 |
"update policies.\n", domainname->name, exe); |
1422 |
last_pid = pid; |
1423 |
} |
1424 |
} |
1425 |
ccs_free(exe); |
1426 |
return found; |
1427 |
} |
1428 |
|
1429 |
#ifdef CONFIG_TOMOYO |
1430 |
|
1431 |
/** |
1432 |
* ccs_find_condition_part - Find condition part from the statement. |
1433 |
* |
1434 |
* @data: String to parse. |
1435 |
* |
1436 |
* Returns pointer to the condition part if it was found in the statement, |
1437 |
* NULL otherwise. |
1438 |
*/ |
1439 |
static char *ccs_find_condition_part(char *data) |
1440 |
{ |
1441 |
char *cp = strstr(data, " if "); |
1442 |
if (cp) { |
1443 |
char *cp2; |
1444 |
while ((cp2 = strstr(cp + 3, " if ")) != NULL) |
1445 |
cp = cp2; |
1446 |
*cp++ = '\0'; |
1447 |
} else { |
1448 |
cp = strstr(data, " ; set "); |
1449 |
if (cp) |
1450 |
*cp++ = '\0'; |
1451 |
} |
1452 |
return cp; |
1453 |
} |
1454 |
|
1455 |
/** |
1456 |
* is_select_one - Parse select command. |
1457 |
* |
1458 |
* @head: Pointer to "struct ccs_io_buffer". |
1459 |
* @data: String to parse. |
1460 |
* |
1461 |
* Returns true on success, false otherwise. |
1462 |
*/ |
1463 |
static bool is_select_one(struct ccs_io_buffer *head, const char *data) |
1464 |
{ |
1465 |
unsigned int pid; |
1466 |
struct domain_info *domain = NULL; |
1467 |
if (sscanf(data, "pid=%u", &pid) == 1) { |
1468 |
struct task_struct *p; |
1469 |
/***** CRITICAL SECTION START *****/ |
1470 |
read_lock(&tasklist_lock); |
1471 |
p = find_task_by_pid(pid); |
1472 |
if (p) |
1473 |
domain = p->domain_info; |
1474 |
read_unlock(&tasklist_lock); |
1475 |
/***** CRITICAL SECTION END *****/ |
1476 |
} else if (!strncmp(data, "domain=", 7)) { |
1477 |
if (ccs_is_domain_def(data + 7)) |
1478 |
domain = ccs_find_domain(data + 7); |
1479 |
} else |
1480 |
return false; |
1481 |
head->read_avail = 0; |
1482 |
ccs_io_printf(head, "# select %s\n", data); |
1483 |
head->read_single_domain = true; |
1484 |
head->read_eof = !domain; |
1485 |
if (domain) { |
1486 |
struct domain_info *d; |
1487 |
head->read_var1 = NULL; |
1488 |
list1_for_each_entry(d, &domain_list, list) { |
1489 |
if (d == domain) |
1490 |
break; |
1491 |
head->read_var1 = &d->list; |
1492 |
} |
1493 |
head->read_var2 = NULL; |
1494 |
head->read_bit = 0; |
1495 |
head->read_step = 0; |
1496 |
if (domain->is_deleted) |
1497 |
ccs_io_printf(head, "# This is a deleted domain.\n"); |
1498 |
} |
1499 |
head->write_var1 = domain; |
1500 |
return true; |
1501 |
} |
1502 |
|
1503 |
/** |
1504 |
* write_domain_policy - Write domain policy. |
1505 |
* |
1506 |
* @head: Pointer to "struct ccs_io_buffer". |
1507 |
* |
1508 |
* Returns 0 on success, negative value otherwise. |
1509 |
*/ |
1510 |
static int write_domain_policy(struct ccs_io_buffer *head) |
1511 |
{ |
1512 |
char *data = head->write_buf; |
1513 |
struct domain_info *domain = head->write_var1; |
1514 |
bool is_delete = false; |
1515 |
bool is_select = false; |
1516 |
bool is_undelete = false; |
1517 |
unsigned int profile; |
1518 |
const struct condition_list *cond = NULL; |
1519 |
char *cp; |
1520 |
if (str_starts(&data, KEYWORD_DELETE)) |
1521 |
is_delete = true; |
1522 |
else if (str_starts(&data, KEYWORD_SELECT)) |
1523 |
is_select = true; |
1524 |
else if (str_starts(&data, KEYWORD_UNDELETE)) |
1525 |
is_undelete = true; |
1526 |
if (is_select && is_select_one(head, data)) |
1527 |
return 0; |
1528 |
/* Don't allow updating policies by non manager programs. */ |
1529 |
if (!is_policy_manager()) |
1530 |
return -EPERM; |
1531 |
if (ccs_is_domain_def(data)) { |
1532 |
domain = NULL; |
1533 |
if (is_delete) |
1534 |
ccs_delete_domain(data); |
1535 |
else if (is_select) |
1536 |
domain = ccs_find_domain(data); |
1537 |
else if (is_undelete) |
1538 |
domain = ccs_undelete_domain(data); |
1539 |
else |
1540 |
domain = ccs_find_or_assign_new_domain(data, 0); |
1541 |
head->write_var1 = domain; |
1542 |
ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
1543 |
return 0; |
1544 |
} |
1545 |
if (!domain) |
1546 |
return -EINVAL; |
1547 |
|
1548 |
if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1 |
1549 |
&& profile < MAX_PROFILES) { |
1550 |
if (profile_ptr[profile] || !sbin_init_started) |
1551 |
domain->profile = (u8) profile; |
1552 |
return 0; |
1553 |
} |
1554 |
if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) { |
1555 |
ccs_set_domain_flag(domain, is_delete, |
1556 |
DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ); |
1557 |
return 0; |
1558 |
} |
1559 |
if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) { |
1560 |
ccs_set_domain_flag(domain, is_delete, |
1561 |
DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV); |
1562 |
return 0; |
1563 |
} |
1564 |
cp = ccs_find_condition_part(data); |
1565 |
if (cp) { |
1566 |
cond = ccs_find_or_assign_new_condition(cp); |
1567 |
if (!cond) |
1568 |
return -EINVAL; |
1569 |
} |
1570 |
if (str_starts(&data, KEYWORD_ALLOW_CAPABILITY)) |
1571 |
return ccs_write_capability_policy(data, domain, cond, |
1572 |
is_delete); |
1573 |
else if (str_starts(&data, KEYWORD_ALLOW_NETWORK)) |
1574 |
return ccs_write_network_policy(data, domain, cond, is_delete); |
1575 |
else if (str_starts(&data, KEYWORD_ALLOW_SIGNAL)) |
1576 |
return ccs_write_signal_policy(data, domain, cond, is_delete); |
1577 |
else if (str_starts(&data, KEYWORD_ALLOW_ARGV0)) |
1578 |
return ccs_write_argv0_policy(data, domain, cond, is_delete); |
1579 |
else if (str_starts(&data, KEYWORD_ALLOW_ENV)) |
1580 |
return ccs_write_env_policy(data, domain, cond, is_delete); |
1581 |
else |
1582 |
return ccs_write_file_policy(data, domain, cond, is_delete); |
1583 |
} |
1584 |
|
1585 |
/** |
1586 |
* print_single_path_acl - Print a single path ACL entry. |
1587 |
* |
1588 |
* @head: Pointer to "struct ccs_io_buffer". |
1589 |
* @ptr: Pointer to "struct single_path_acl_record". |
1590 |
* @cond: Pointer to "struct condition_list". May be NULL. |
1591 |
* |
1592 |
* Returns true on success, false otherwise. |
1593 |
*/ |
1594 |
static bool print_single_path_acl(struct ccs_io_buffer *head, |
1595 |
struct single_path_acl_record *ptr, |
1596 |
const struct condition_list *cond) |
1597 |
{ |
1598 |
int pos; |
1599 |
u8 bit; |
1600 |
const char *atmark = ""; |
1601 |
const char *filename; |
1602 |
const u16 perm = ptr->perm; |
1603 |
if (ptr->u_is_group) { |
1604 |
atmark = "@"; |
1605 |
filename = ptr->u.group->group_name->name; |
1606 |
} else { |
1607 |
filename = ptr->u.filename->name; |
1608 |
} |
1609 |
for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) { |
1610 |
const char *msg; |
1611 |
if (!(perm & (1 << bit))) |
1612 |
continue; |
1613 |
/* Print "read/write" instead of "read" and "write". */ |
1614 |
if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL) |
1615 |
&& (perm & (1 << TYPE_READ_WRITE_ACL))) |
1616 |
continue; |
1617 |
msg = ccs_sp2keyword(bit); |
1618 |
pos = head->read_avail; |
1619 |
if (!ccs_io_printf(head, "allow_%s %s%s", msg, |
1620 |
atmark, filename) || |
1621 |
!ccs_print_condition(head, cond)) |
1622 |
goto out; |
1623 |
} |
1624 |
head->read_bit = 0; |
1625 |
return true; |
1626 |
out: |
1627 |
head->read_bit = bit; |
1628 |
head->read_avail = pos; |
1629 |
return false; |
1630 |
} |
1631 |
|
1632 |
/** |
1633 |
* print_double_path_acl - Print a double path ACL entry. |
1634 |
* |
1635 |
* @head: Pointer to "struct ccs_io_buffer". |
1636 |
* @ptr: Pointer to "struct double_path_acl_record". |
1637 |
* @cond: Pointer to "struct condition_list". May be NULL. |
1638 |
* |
1639 |
* Returns true on success, false otherwise. |
1640 |
*/ |
1641 |
static bool print_double_path_acl(struct ccs_io_buffer *head, |
1642 |
struct double_path_acl_record *ptr, |
1643 |
const struct condition_list *cond) |
1644 |
{ |
1645 |
int pos; |
1646 |
const char *atmark1 = ""; |
1647 |
const char *atmark2 = ""; |
1648 |
const char *filename1; |
1649 |
const char *filename2; |
1650 |
const u8 perm = ptr->perm; |
1651 |
u8 bit; |
1652 |
if (ptr->u1_is_group) { |
1653 |
atmark1 = "@"; |
1654 |
filename1 = ptr->u1.group1->group_name->name; |
1655 |
} else { |
1656 |
filename1 = ptr->u1.filename1->name; |
1657 |
} |
1658 |
if (ptr->u2_is_group) { |
1659 |
atmark2 = "@"; |
1660 |
filename2 = ptr->u2.group2->group_name->name; |
1661 |
} else { |
1662 |
filename2 = ptr->u2.filename2->name; |
1663 |
} |
1664 |
for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) { |
1665 |
const char *msg; |
1666 |
if (!(perm & (1 << bit))) |
1667 |
continue; |
1668 |
msg = ccs_dp2keyword(bit); |
1669 |
pos = head->read_avail; |
1670 |
if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg, |
1671 |
atmark1, filename1, atmark2, filename2) || |
1672 |
!ccs_print_condition(head, cond)) |
1673 |
goto out; |
1674 |
} |
1675 |
head->read_bit = 0; |
1676 |
return true; |
1677 |
out: |
1678 |
head->read_bit = bit; |
1679 |
head->read_avail = pos; |
1680 |
return false; |
1681 |
} |
1682 |
|
1683 |
/** |
1684 |
* print_argv0_acl - Print an argv[0] ACL entry. |
1685 |
* |
1686 |
* @head: Pointer to "struct ccs_io_buffer". |
1687 |
* @ptr: Pointer to "struct argv0_acl_record". |
1688 |
* @cond: Pointer to "struct condition_list". May be NULL. |
1689 |
* |
1690 |
* Returns true on success, false otherwise. |
1691 |
*/ |
1692 |
static bool print_argv0_acl(struct ccs_io_buffer *head, |
1693 |
struct argv0_acl_record *ptr, |
1694 |
const struct condition_list *cond) |
1695 |
{ |
1696 |
int pos = head->read_avail; |
1697 |
if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s", |
1698 |
ptr->filename->name, ptr->argv0->name)) |
1699 |
goto out; |
1700 |
if (!ccs_print_condition(head, cond)) |
1701 |
goto out; |
1702 |
return true; |
1703 |
out: |
1704 |
head->read_avail = pos; |
1705 |
return false; |
1706 |
} |
1707 |
|
1708 |
/** |
1709 |
* print_env_acl - Print an evironment variable name's ACL entry. |
1710 |
* |
1711 |
* @head: Pointer to "struct ccs_io_buffer". |
1712 |
* @ptr: Pointer to "struct env_acl_record". |
1713 |
* @cond: Pointer to "struct condition_list". May be NULL. |
1714 |
* |
1715 |
* Returns true on success, false otherwise. |
1716 |
*/ |
1717 |
static bool print_env_acl(struct ccs_io_buffer *head, |
1718 |
struct env_acl_record *ptr, |
1719 |
const struct condition_list *cond) |
1720 |
{ |
1721 |
int pos = head->read_avail; |
1722 |
if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name)) |
1723 |
goto out; |
1724 |
if (!ccs_print_condition(head, cond)) |
1725 |
goto out; |
1726 |
return true; |
1727 |
out: |
1728 |
head->read_avail = pos; |
1729 |
return false; |
1730 |
} |
1731 |
|
1732 |
/** |
1733 |
* print_capability_acl - Print a capability ACL entry. |
1734 |
* |
1735 |
* @head: Pointer to "struct ccs_io_buffer". |
1736 |
* @ptr: Pointer to "struct capability_acl_record". |
1737 |
* @cond: Pointer to "struct condition_list". May be NULL. |
1738 |
* |
1739 |
* Returns true on success, false otherwise. |
1740 |
*/ |
1741 |
static bool print_capability_acl(struct ccs_io_buffer *head, |
1742 |
struct capability_acl_record *ptr, |
1743 |
const struct condition_list *cond) |
1744 |
{ |
1745 |
int pos = head->read_avail; |
1746 |
if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s", |
1747 |
ccs_cap2keyword(ptr->operation))) |
1748 |
goto out; |
1749 |
if (!ccs_print_condition(head, cond)) |
1750 |
goto out; |
1751 |
return true; |
1752 |
out: |
1753 |
head->read_avail = pos; |
1754 |
return false; |
1755 |
} |
1756 |
|
1757 |
/** |
1758 |
* print_ipv4_entry - Print IPv4 address of a network ACL entry. |
1759 |
* |
1760 |
* @head: Pointer to "struct ccs_io_buffer". |
1761 |
* @ptr: Pointer to "struct ip_network_acl_record". |
1762 |
* |
1763 |
* Returns true on success, false otherwise. |
1764 |
*/ |
1765 |
static bool print_ipv4_entry(struct ccs_io_buffer *head, |
1766 |
struct ip_network_acl_record *ptr) |
1767 |
{ |
1768 |
const u32 min_address = ptr->u.ipv4.min; |
1769 |
const u32 max_address = ptr->u.ipv4.max; |
1770 |
if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address))) |
1771 |
return false; |
1772 |
if (min_address != max_address |
1773 |
&& !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address))) |
1774 |
return false; |
1775 |
return true; |
1776 |
} |
1777 |
|
1778 |
/** |
1779 |
* print_ipv6_entry - Print IPv6 address of a network ACL entry. |
1780 |
* |
1781 |
* @head: Pointer to "struct ccs_io_buffer". |
1782 |
* @ptr: Pointer to "struct ip_network_acl_record". |
1783 |
* |
1784 |
* Returns true on success, false otherwise. |
1785 |
*/ |
1786 |
static bool print_ipv6_entry(struct ccs_io_buffer *head, |
1787 |
struct ip_network_acl_record *ptr) |
1788 |
{ |
1789 |
char buf[64]; |
1790 |
const struct in6_addr *min_address = ptr->u.ipv6.min; |
1791 |
const struct in6_addr *max_address = ptr->u.ipv6.max; |
1792 |
ccs_print_ipv6(buf, sizeof(buf), min_address); |
1793 |
if (!ccs_io_printf(head, "%s", buf)) |
1794 |
return false; |
1795 |
if (min_address != max_address) { |
1796 |
ccs_print_ipv6(buf, sizeof(buf), max_address); |
1797 |
if (!ccs_io_printf(head, "-%s", buf)) |
1798 |
return false; |
1799 |
} |
1800 |
return true; |
1801 |
} |
1802 |
|
1803 |
/** |
1804 |
* print_port_entry - Print port number of a network ACL entry. |
1805 |
* |
1806 |
* @head: Pointer to "struct ccs_io_buffer". |
1807 |
* @ptr: Pointer to "struct ip_network_acl_record". |
1808 |
* |
1809 |
* Returns true on success, false otherwise. |
1810 |
*/ |
1811 |
static bool print_port_entry(struct ccs_io_buffer *head, |
1812 |
struct ip_network_acl_record *ptr) |
1813 |
{ |
1814 |
const u16 min_port = ptr->min_port, max_port = ptr->max_port; |
1815 |
if (!ccs_io_printf(head, " %u", min_port)) |
1816 |
return false; |
1817 |
if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port)) |
1818 |
return false; |
1819 |
return true; |
1820 |
} |
1821 |
|
1822 |
/** |
1823 |
* print_network_acl - Print a network ACL entry. |
1824 |
* |
1825 |
* @head: Pointer to "struct ccs_io_buffer". |
1826 |
* @ptr: Pointer to "struct ip_network_acl_record". |
1827 |
* @cond: Pointer to "struct condition_list". May be NULL. |
1828 |
* |
1829 |
* Returns true on success, false otherwise. |
1830 |
*/ |
1831 |
static bool print_network_acl(struct ccs_io_buffer *head, |
1832 |
struct ip_network_acl_record *ptr, |
1833 |
const struct condition_list *cond) |
1834 |
{ |
1835 |
int pos = head->read_avail; |
1836 |
if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ", |
1837 |
ccs_net2keyword(ptr->operation_type))) |
1838 |
goto out; |
1839 |
switch (ptr->record_type) { |
1840 |
case IP_RECORD_TYPE_ADDRESS_GROUP: |
1841 |
if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name)) |
1842 |
goto out; |
1843 |
break; |
1844 |
case IP_RECORD_TYPE_IPv4: |
1845 |
if (!print_ipv4_entry(head, ptr)) |
1846 |
goto out; |
1847 |
break; |
1848 |
case IP_RECORD_TYPE_IPv6: |
1849 |
if (!print_ipv6_entry(head, ptr)) |
1850 |
goto out; |
1851 |
break; |
1852 |
} |
1853 |
if (!print_port_entry(head, ptr)) |
1854 |
goto out; |
1855 |
if (!ccs_print_condition(head, cond)) |
1856 |
goto out; |
1857 |
return true; |
1858 |
out: |
1859 |
head->read_avail = pos; |
1860 |
return false; |
1861 |
} |
1862 |
|
1863 |
/** |
1864 |
* print_signal_acl - Print a signal ACL entry. |
1865 |
* |
1866 |
* @head: Pointer to "struct ccs_io_buffer". |
1867 |
* @ptr: Pointer to "struct signale_acl_record". |
1868 |
* @cond: Pointer to "struct condition_list". May be NULL. |
1869 |
* |
1870 |
* Returns true on success, false otherwise. |
1871 |
*/ |
1872 |
static bool print_signal_acl(struct ccs_io_buffer *head, |
1873 |
struct signal_acl_record *ptr, |
1874 |
const struct condition_list *cond) |
1875 |
{ |
1876 |
int pos = head->read_avail; |
1877 |
if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s", |
1878 |
ptr->sig, ptr->domainname->name)) |
1879 |
goto out; |
1880 |
if (!ccs_print_condition(head, cond)) |
1881 |
goto out; |
1882 |
return true; |
1883 |
out: |
1884 |
head->read_avail = pos; |
1885 |
return false; |
1886 |
} |
1887 |
|
1888 |
/** |
1889 |
* print_execute_handler_record - Print an execute handler ACL entry. |
1890 |
* |
1891 |
* @head: Pointer to "struct ccs_io_buffer". |
1892 |
* @keyword: Name of the keyword. |
1893 |
* @ptr: Pointer to "struct execute_handler_record". |
1894 |
* |
1895 |
* Returns true on success, false otherwise. |
1896 |
*/ |
1897 |
static bool print_execute_handler_record(struct ccs_io_buffer *head, |
1898 |
const char *keyword, |
1899 |
struct execute_handler_record *ptr) |
1900 |
{ |
1901 |
return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name); |
1902 |
} |
1903 |
|
1904 |
/** |
1905 |
* print_entry - Print an ACL entry. |
1906 |
* |
1907 |
* @head: Pointer to "struct ccs_io_buffer". |
1908 |
* @ptr: Pointer to an ACL entry. |
1909 |
* |
1910 |
* Returns true on success, false otherwise. |
1911 |
*/ |
1912 |
static bool print_entry(struct ccs_io_buffer *head, struct acl_info *ptr) |
1913 |
{ |
1914 |
const struct condition_list *cond = ccs_get_condition_part(ptr); |
1915 |
const u8 acl_type = ccs_acl_type2(ptr); |
1916 |
if (acl_type & ACL_DELETED) |
1917 |
return true; |
1918 |
if (acl_type == TYPE_SINGLE_PATH_ACL) { |
1919 |
struct single_path_acl_record *acl |
1920 |
= container_of(ptr, struct single_path_acl_record, |
1921 |
head); |
1922 |
return print_single_path_acl(head, acl, cond); |
1923 |
} |
1924 |
if (acl_type == TYPE_DOUBLE_PATH_ACL) { |
1925 |
struct double_path_acl_record *acl |
1926 |
= container_of(ptr, struct double_path_acl_record, |
1927 |
head); |
1928 |
return print_double_path_acl(head, acl, cond); |
1929 |
} |
1930 |
if (acl_type == TYPE_ARGV0_ACL) { |
1931 |
struct argv0_acl_record *acl |
1932 |
= container_of(ptr, struct argv0_acl_record, head); |
1933 |
return print_argv0_acl(head, acl, cond); |
1934 |
} |
1935 |
if (acl_type == TYPE_ENV_ACL) { |
1936 |
struct env_acl_record *acl |
1937 |
= container_of(ptr, struct env_acl_record, head); |
1938 |
return print_env_acl(head, acl, cond); |
1939 |
} |
1940 |
if (acl_type == TYPE_CAPABILITY_ACL) { |
1941 |
struct capability_acl_record *acl |
1942 |
= container_of(ptr, struct capability_acl_record, head); |
1943 |
return print_capability_acl(head, acl, cond); |
1944 |
} |
1945 |
if (acl_type == TYPE_IP_NETWORK_ACL) { |
1946 |
struct ip_network_acl_record *acl |
1947 |
= container_of(ptr, struct ip_network_acl_record, head); |
1948 |
return print_network_acl(head, acl, cond); |
1949 |
} |
1950 |
if (acl_type == TYPE_SIGNAL_ACL) { |
1951 |
struct signal_acl_record *acl |
1952 |
= container_of(ptr, struct signal_acl_record, head); |
1953 |
return print_signal_acl(head, acl, cond); |
1954 |
} |
1955 |
if (acl_type == TYPE_EXECUTE_HANDLER) { |
1956 |
struct execute_handler_record *acl |
1957 |
= container_of(ptr, struct execute_handler_record, |
1958 |
head); |
1959 |
const char *keyword = KEYWORD_EXECUTE_HANDLER; |
1960 |
return print_execute_handler_record(head, keyword, acl); |
1961 |
} |
1962 |
if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) { |
1963 |
struct execute_handler_record *acl |
1964 |
= container_of(ptr, struct execute_handler_record, |
1965 |
head); |
1966 |
const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER; |
1967 |
return print_execute_handler_record(head, keyword, acl); |
1968 |
} |
1969 |
/* Workaround for gcc 3.2.2's inline bug. */ |
1970 |
if (acl_type & ACL_DELETED) |
1971 |
return true; |
1972 |
BUG(); /* This must not happen. */ |
1973 |
return false; |
1974 |
} |
1975 |
|
1976 |
/** |
1977 |
* read_domain_policy - Read domain policy. |
1978 |
* |
1979 |
* @head: Pointer to "struct ccs_io_buffer". |
1980 |
* |
1981 |
* Returns 0. |
1982 |
*/ |
1983 |
static int read_domain_policy(struct ccs_io_buffer *head) |
1984 |
{ |
1985 |
struct list1_head *dpos; |
1986 |
struct list1_head *apos; |
1987 |
if (head->read_eof) |
1988 |
return 0; |
1989 |
if (head->read_step == 0) |
1990 |
head->read_step = 1; |
1991 |
list1_for_each_cookie(dpos, head->read_var1, &domain_list) { |
1992 |
struct domain_info *domain; |
1993 |
const char *quota_exceeded = ""; |
1994 |
const char *transition_failed = ""; |
1995 |
const char *ignore_global_allow_read = ""; |
1996 |
const char *ignore_global_allow_env = ""; |
1997 |
domain = list1_entry(dpos, struct domain_info, list); |
1998 |
if (head->read_step != 1) |
1999 |
goto acl_loop; |
2000 |
if (domain->is_deleted && !head->read_single_domain) |
2001 |
continue; |
2002 |
/* Print domainname and flags. */ |
2003 |
if (domain->quota_warned) |
2004 |
quota_exceeded = "quota_exceeded\n"; |
2005 |
if (domain->flags & DOMAIN_FLAGS_TRANSITION_FAILED) |
2006 |
transition_failed = "transition_failed\n"; |
2007 |
if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ) |
2008 |
ignore_global_allow_read |
2009 |
= KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n"; |
2010 |
if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV) |
2011 |
ignore_global_allow_env |
2012 |
= KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n"; |
2013 |
if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n" |
2014 |
"%s%s%s%s\n", domain->domainname->name, |
2015 |
domain->profile, quota_exceeded, |
2016 |
transition_failed, |
2017 |
ignore_global_allow_read, |
2018 |
ignore_global_allow_env)) |
2019 |
return 0; |
2020 |
head->read_step = 2; |
2021 |
acl_loop: |
2022 |
if (head->read_step == 3) |
2023 |
goto tail_mark; |
2024 |
/* Print ACL entries in the domain. */ |
2025 |
list1_for_each_cookie(apos, head->read_var2, |
2026 |
&domain->acl_info_list) { |
2027 |
struct acl_info *ptr |
2028 |
= list1_entry(apos, struct acl_info, list); |
2029 |
if (!print_entry(head, ptr)) |
2030 |
return 0; |
2031 |
} |
2032 |
head->read_step = 3; |
2033 |
tail_mark: |
2034 |
if (!ccs_io_printf(head, "\n")) |
2035 |
return 0; |
2036 |
head->read_step = 1; |
2037 |
if (head->read_single_domain) |
2038 |
break; |
2039 |
} |
2040 |
head->read_eof = true; |
2041 |
return 0; |
2042 |
} |
2043 |
|
2044 |
#endif |
2045 |
|
2046 |
/** |
2047 |
* write_domain_profile - Assign profile for specified domain. |
2048 |
* |
2049 |
* @head: Pointer to "struct ccs_io_buffer". |
2050 |
* |
2051 |
* Returns 0 on success, -EINVAL otherwise. |
2052 |
* |
2053 |
* This is equivalent to doing |
2054 |
* |
2055 |
* ( echo "select " $domainname; echo "use_profile " $profile ) | |
2056 |
* /usr/lib/ccs/loadpolicy -d |
2057 |
*/ |
2058 |
static int write_domain_profile(struct ccs_io_buffer *head) |
2059 |
{ |
2060 |
char *data = head->write_buf; |
2061 |
char *cp = strchr(data, ' '); |
2062 |
struct domain_info *domain; |
2063 |
unsigned int profile; |
2064 |
if (!cp) |
2065 |
return -EINVAL; |
2066 |
*cp = '\0'; |
2067 |
domain = ccs_find_domain(cp + 1); |
2068 |
profile = simple_strtoul(data, NULL, 10); |
2069 |
if (domain && profile < MAX_PROFILES |
2070 |
&& (profile_ptr[profile] || !sbin_init_started)) |
2071 |
domain->profile = (u8) profile; |
2072 |
ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
2073 |
return 0; |
2074 |
} |
2075 |
|
2076 |
/** |
2077 |
* read_domain_profile - Read only domainname and profile. |
2078 |
* |
2079 |
* @head: Pointer to "struct ccs_io_buffer". |
2080 |
* |
2081 |
* Returns list of profile number and domainname pairs. |
2082 |
* |
2083 |
* This is equivalent to doing |
2084 |
* |
2085 |
* grep -A 1 '^<kernel>' /proc/ccs/domain_policy | |
2086 |
* awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" ) |
2087 |
* domainname = $0; } else if ( $1 == "use_profile" ) { |
2088 |
* print $2 " " domainname; domainname = ""; } } ; ' |
2089 |
*/ |
2090 |
static int read_domain_profile(struct ccs_io_buffer *head) |
2091 |
{ |
2092 |
struct list1_head *pos; |
2093 |
if (head->read_eof) |
2094 |
return 0; |
2095 |
list1_for_each_cookie(pos, head->read_var1, &domain_list) { |
2096 |
struct domain_info *domain; |
2097 |
domain = list1_entry(pos, struct domain_info, list); |
2098 |
if (domain->is_deleted) |
2099 |
continue; |
2100 |
if (!ccs_io_printf(head, "%u %s\n", domain->profile, |
2101 |
domain->domainname->name)) |
2102 |
return 0; |
2103 |
} |
2104 |
head->read_eof = true; |
2105 |
return 0; |
2106 |
} |
2107 |
|
2108 |
/** |
2109 |
* write_pid: Specify PID to obtain domainname. |
2110 |
* |
2111 |
* @head: Pointer to "struct ccs_io_buffer". |
2112 |
* |
2113 |
* Returns 0. |
2114 |
*/ |
2115 |
static int write_pid(struct ccs_io_buffer *head) |
2116 |
{ |
2117 |
head->read_step = (int) simple_strtoul(head->write_buf, NULL, 10); |
2118 |
head->read_eof = false; |
2119 |
return 0; |
2120 |
} |
2121 |
|
2122 |
/** |
2123 |
* read_pid - Get domainname of the specified PID. |
2124 |
* |
2125 |
* @head: Pointer to "struct ccs_io_buffer". |
2126 |
* |
2127 |
* Returns the domainname which the specified PID is in on success, |
2128 |
* empty string otherwise. |
2129 |
* The PID is specified by write_pid() so that the user can obtain |
2130 |
* using read()/write() interface rather than sysctl() interface. |
2131 |
*/ |
2132 |
static int read_pid(struct ccs_io_buffer *head) |
2133 |
{ |
2134 |
if (head->read_avail == 0 && !head->read_eof) { |
2135 |
const char *is_manager = ""; |
2136 |
const char *is_execute_handler = ""; |
2137 |
const int pid = head->read_step; |
2138 |
struct task_struct *p; |
2139 |
struct domain_info *domain = NULL; |
2140 |
u32 tomoyo_flags = 0; |
2141 |
/***** CRITICAL SECTION START *****/ |
2142 |
read_lock(&tasklist_lock); |
2143 |
p = find_task_by_pid(pid); |
2144 |
if (p) { |
2145 |
domain = p->domain_info; |
2146 |
tomoyo_flags = p->tomoyo_flags; |
2147 |
} |
2148 |
read_unlock(&tasklist_lock); |
2149 |
/***** CRITICAL SECTION END *****/ |
2150 |
if (tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER) |
2151 |
is_execute_handler = "(execute_handler)"; |
2152 |
if (tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER) |
2153 |
is_manager = "(manager)"; |
2154 |
if (domain) |
2155 |
ccs_io_printf(head, "%d%s%s %u %s", pid, is_manager, |
2156 |
is_execute_handler, domain->profile, |
2157 |
domain->domainname->name); |
2158 |
head->read_eof = true; |
2159 |
} |
2160 |
return 0; |
2161 |
} |
2162 |
|
2163 |
#ifdef CONFIG_TOMOYO |
2164 |
|
2165 |
/** |
2166 |
* write_exception_policy - Write exception policy. |
2167 |
* |
2168 |
* @head: Pointer to "struct ccs_io_buffer". |
2169 |
* |
2170 |
* Returns 0 on success, negative value otherwise. |
2171 |
*/ |
2172 |
static int write_exception_policy(struct ccs_io_buffer *head) |
2173 |
{ |
2174 |
char *data = head->write_buf; |
2175 |
bool is_delete = str_starts(&data, KEYWORD_DELETE); |
2176 |
if (str_starts(&data, KEYWORD_KEEP_DOMAIN)) |
2177 |
return ccs_write_domain_keeper_policy(data, false, is_delete); |
2178 |
if (str_starts(&data, KEYWORD_NO_KEEP_DOMAIN)) |
2179 |
return ccs_write_domain_keeper_policy(data, true, is_delete); |
2180 |
if (str_starts(&data, KEYWORD_INITIALIZE_DOMAIN)) |
2181 |
return ccs_write_domain_initializer_policy(data, false, |
2182 |
is_delete); |
2183 |
if (str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN)) |
2184 |
return ccs_write_domain_initializer_policy(data, true, |
2185 |
is_delete); |
2186 |
if (str_starts(&data, KEYWORD_ALIAS)) |
2187 |
return ccs_write_alias_policy(data, is_delete); |
2188 |
if (str_starts(&data, KEYWORD_AGGREGATOR)) |
2189 |
return ccs_write_aggregator_policy(data, is_delete); |
2190 |
if (str_starts(&data, KEYWORD_ALLOW_READ)) |
2191 |
return ccs_write_globally_readable_policy(data, is_delete); |
2192 |
if (str_starts(&data, KEYWORD_ALLOW_ENV)) |
2193 |
return ccs_write_globally_usable_env_policy(data, is_delete); |
2194 |
if (str_starts(&data, KEYWORD_FILE_PATTERN)) |
2195 |
return ccs_write_pattern_policy(data, is_delete); |
2196 |
if (str_starts(&data, KEYWORD_PATH_GROUP)) |
2197 |
return ccs_write_path_group_policy(data, is_delete); |
2198 |
if (str_starts(&data, KEYWORD_DENY_REWRITE)) |
2199 |
return ccs_write_no_rewrite_policy(data, is_delete); |
2200 |
if (str_starts(&data, KEYWORD_ADDRESS_GROUP)) |
2201 |
return ccs_write_address_group_policy(data, is_delete); |
2202 |
return -EINVAL; |
2203 |
} |
2204 |
|
2205 |
/** |
2206 |
* read_exception_policy - Read exception policy. |
2207 |
* |
2208 |
* @head: Pointer to "struct ccs_io_buffer". |
2209 |
* |
2210 |
* Returns 0 on success, -EINVAL otherwise. |
2211 |
*/ |
2212 |
static int read_exception_policy(struct ccs_io_buffer *head) |
2213 |
{ |
2214 |
if (!head->read_eof) { |
2215 |
switch (head->read_step) { |
2216 |
case 0: |
2217 |
head->read_var2 = NULL; |
2218 |
head->read_step = 1; |
2219 |
case 1: |
2220 |
if (!ccs_read_domain_keeper_policy(head)) |
2221 |
break; |
2222 |
head->read_var2 = NULL; |
2223 |
head->read_step = 2; |
2224 |
case 2: |
2225 |
if (!ccs_read_globally_readable_policy(head)) |
2226 |
break; |
2227 |
head->read_var2 = NULL; |
2228 |
head->read_step = 3; |
2229 |
case 3: |
2230 |
if (!ccs_read_globally_usable_env_policy(head)) |
2231 |
break; |
2232 |
head->read_var2 = NULL; |
2233 |
head->read_step = 4; |
2234 |
case 4: |
2235 |
if (!ccs_read_domain_initializer_policy(head)) |
2236 |
break; |
2237 |
head->read_var2 = NULL; |
2238 |
head->read_step = 5; |
2239 |
case 5: |
2240 |
if (!ccs_read_alias_policy(head)) |
2241 |
break; |
2242 |
head->read_var2 = NULL; |
2243 |
head->read_step = 6; |
2244 |
case 6: |
2245 |
if (!ccs_read_aggregator_policy(head)) |
2246 |
break; |
2247 |
head->read_var2 = NULL; |
2248 |
head->read_step = 7; |
2249 |
case 7: |
2250 |
if (!ccs_read_file_pattern(head)) |
2251 |
break; |
2252 |
head->read_var2 = NULL; |
2253 |
head->read_step = 8; |
2254 |
case 8: |
2255 |
if (!ccs_read_no_rewrite_policy(head)) |
2256 |
break; |
2257 |
head->read_var2 = NULL; |
2258 |
head->read_step = 9; |
2259 |
case 9: |
2260 |
if (!ccs_read_path_group_policy(head)) |
2261 |
break; |
2262 |
head->read_var1 = NULL; |
2263 |
head->read_var2 = NULL; |
2264 |
head->read_step = 10; |
2265 |
case 10: |
2266 |
if (!ccs_read_address_group_policy(head)) |
2267 |
break; |
2268 |
head->read_eof = true; |
2269 |
break; |
2270 |
default: |
2271 |
return -EINVAL; |
2272 |
} |
2273 |
} |
2274 |
return 0; |
2275 |
} |
2276 |
|
2277 |
#endif |
2278 |
|
2279 |
#ifdef CONFIG_SAKURA |
2280 |
|
2281 |
/** |
2282 |
* write_system_policy - Write system policy. |
2283 |
* |
2284 |
* @head: Pointer to "struct ccs_io_buffer". |
2285 |
* |
2286 |
* Returns 0 on success, negative value otherwise. |
2287 |
*/ |
2288 |
static int write_system_policy(struct ccs_io_buffer *head) |
2289 |
{ |
2290 |
char *data = head->write_buf; |
2291 |
bool is_delete = false; |
2292 |
if (str_starts(&data, KEYWORD_DELETE)) |
2293 |
is_delete = true; |
2294 |
if (str_starts(&data, KEYWORD_ALLOW_MOUNT)) |
2295 |
return ccs_write_mount_policy(data, is_delete); |
2296 |
if (str_starts(&data, KEYWORD_DENY_UNMOUNT)) |
2297 |
return ccs_write_no_umount_policy(data, is_delete); |
2298 |
if (str_starts(&data, KEYWORD_ALLOW_CHROOT)) |
2299 |
return ccs_write_chroot_policy(data, is_delete); |
2300 |
if (str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT)) |
2301 |
return ccs_write_pivot_root_policy(data, is_delete); |
2302 |
if (str_starts(&data, KEYWORD_DENY_AUTOBIND)) |
2303 |
return ccs_write_reserved_port_policy(data, is_delete); |
2304 |
return -EINVAL; |
2305 |
} |
2306 |
|
2307 |
/** |
2308 |
* read_system_policy - Read system policy. |
2309 |
* |
2310 |
* @head: Pointer to "struct ccs_io_buffer". |
2311 |
* |
2312 |
* Returns 0 on success, -EINVAL otherwise. |
2313 |
*/ |
2314 |
static int read_system_policy(struct ccs_io_buffer *head) |
2315 |
{ |
2316 |
if (!head->read_eof) { |
2317 |
switch (head->read_step) { |
2318 |
case 0: |
2319 |
head->read_var2 = NULL; |
2320 |
head->read_step = 1; |
2321 |
case 1: |
2322 |
if (!ccs_read_mount_policy(head)) |
2323 |
break; |
2324 |
head->read_var2 = NULL; |
2325 |
head->read_step = 2; |
2326 |
case 2: |
2327 |
if (!ccs_read_no_umount_policy(head)) |
2328 |
break; |
2329 |
head->read_var2 = NULL; |
2330 |
head->read_step = 3; |
2331 |
case 3: |
2332 |
if (!ccs_read_chroot_policy(head)) |
2333 |
break; |
2334 |
head->read_var2 = NULL; |
2335 |
head->read_step = 4; |
2336 |
case 4: |
2337 |
if (!ccs_read_pivot_root_policy(head)) |
2338 |
break; |
2339 |
head->read_var2 = NULL; |
2340 |
head->read_step = 5; |
2341 |
case 5: |
2342 |
if (!ccs_read_reserved_port_policy(head)) |
2343 |
break; |
2344 |
head->read_eof = true; |
2345 |
break; |
2346 |
default: |
2347 |
return -EINVAL; |
2348 |
} |
2349 |
} |
2350 |
return 0; |
2351 |
} |
2352 |
|
2353 |
#endif |
2354 |
|
2355 |
/* Path to the policy loader. The default is /sbin/ccs-init. */ |
2356 |
static const char *ccs_loader; |
2357 |
|
2358 |
/** |
2359 |
* loader_setup - Specify the policy loader to use. |
2360 |
* |
2361 |
* @str: Path to the policy loader. |
2362 |
* |
2363 |
* Returns 0. |
2364 |
*/ |
2365 |
static int __init loader_setup(char *str) |
2366 |
{ |
2367 |
ccs_loader = str; |
2368 |
return 0; |
2369 |
} |
2370 |
|
2371 |
__setup("CCS_loader=", loader_setup); |
2372 |
|
2373 |
/** |
2374 |
* policy_loader_exists - Check whether /sbin/ccs-init exists. |
2375 |
* |
2376 |
* Returns true if /sbin/ccs-init exists, false otherwise. |
2377 |
*/ |
2378 |
static bool policy_loader_exists(void) |
2379 |
{ |
2380 |
/* |
2381 |
* Don't activate MAC if the path given by 'CCS_loader=' option doesn't |
2382 |
* exist. If the initrd includes /sbin/init but real-root-dev has not |
2383 |
* mounted on / yet, activating MAC will block the system since |
2384 |
* policies are not loaded yet. |
2385 |
* Thus, let do_execve() call this function everytime. |
2386 |
*/ |
2387 |
struct nameidata nd; |
2388 |
if (!ccs_loader) |
2389 |
ccs_loader = "/sbin/ccs-init"; |
2390 |
if (path_lookup(ccs_loader, lookup_flags, &nd)) { |
2391 |
printk(KERN_INFO "Not activating Mandatory Access Control now " |
2392 |
"since %s doesn't exist.\n", ccs_loader); |
2393 |
return false; |
2394 |
} |
2395 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
2396 |
path_put(&nd.path); |
2397 |
#else |
2398 |
path_release(&nd); |
2399 |
#endif |
2400 |
return true; |
2401 |
} |
2402 |
|
2403 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
2404 |
/** |
2405 |
* run_ccs_loader - Start /sbin/ccs-init . |
2406 |
* |
2407 |
* @unused: Not used. |
2408 |
* |
2409 |
* Returns PID of /sbin/ccs-init on success, negative value otherwise. |
2410 |
*/ |
2411 |
static int run_ccs_loader(void *unused) |
2412 |
{ |
2413 |
char *argv[2]; |
2414 |
char *envp[3]; |
2415 |
printk(KERN_INFO "Calling %s to load policy. Please wait.\n", |
2416 |
ccs_loader); |
2417 |
argv[0] = (char *) ccs_loader; |
2418 |
argv[1] = NULL; |
2419 |
envp[0] = "HOME=/"; |
2420 |
envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
2421 |
envp[2] = NULL; |
2422 |
return exec_usermodehelper(argv[0], argv, envp); |
2423 |
} |
2424 |
#endif |
2425 |
|
2426 |
/** |
2427 |
* ccs_load_policy - Run external policy loader to load policy. |
2428 |
* |
2429 |
* @filename: The program about to start. |
2430 |
* |
2431 |
* This function checks whether @filename is /sbin/init , and if so |
2432 |
* invoke /sbin/ccs-init and wait for the termination of /sbin/ccs-init |
2433 |
* and then continues invocation of /sbin/init. |
2434 |
* /sbin/ccs-init reads policy files in /etc/ccs/ directory and |
2435 |
* writes to /proc/ccs/ interfaces. |
2436 |
* |
2437 |
* Returns nothing. |
2438 |
*/ |
2439 |
void ccs_load_policy(const char *filename) |
2440 |
{ |
2441 |
if (sbin_init_started) |
2442 |
return; |
2443 |
/* |
2444 |
* Check filename is /sbin/init or /sbin/ccs-start. |
2445 |
* /sbin/ccs-start is a dummy filename in case where /sbin/init can't |
2446 |
* be passed. |
2447 |
* You can create /sbin/ccs-start by "ln -s /bin/true /sbin/ccs-start". |
2448 |
*/ |
2449 |
if (strcmp(filename, "/sbin/init") && |
2450 |
strcmp(filename, "/sbin/ccs-start")) |
2451 |
return; |
2452 |
if (!policy_loader_exists()) |
2453 |
return; |
2454 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
2455 |
{ |
2456 |
char *argv[2]; |
2457 |
char *envp[3]; |
2458 |
printk(KERN_INFO "Calling %s to load policy. Please wait.\n", |
2459 |
ccs_loader); |
2460 |
argv[0] = (char *) ccs_loader; |
2461 |
argv[1] = NULL; |
2462 |
envp[0] = "HOME=/"; |
2463 |
envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
2464 |
envp[2] = NULL; |
2465 |
call_usermodehelper(argv[0], argv, envp, 1); |
2466 |
} |
2467 |
#elif defined(TASK_DEAD) |
2468 |
{ |
2469 |
/* Copied from kernel/kmod.c */ |
2470 |
struct task_struct *task = current; |
2471 |
pid_t pid = kernel_thread(run_ccs_loader, NULL, 0); |
2472 |
sigset_t tmpsig; |
2473 |
spin_lock_irq(&task->sighand->siglock); |
2474 |
tmpsig = task->blocked; |
2475 |
siginitsetinv(&task->blocked, |
2476 |
sigmask(SIGKILL) | sigmask(SIGSTOP)); |
2477 |
recalc_sigpending(); |
2478 |
spin_unlock_irq(¤t->sighand->siglock); |
2479 |
if (pid >= 0) |
2480 |
waitpid(pid, NULL, __WCLONE); |
2481 |
spin_lock_irq(&task->sighand->siglock); |
2482 |
task->blocked = tmpsig; |
2483 |
recalc_sigpending(); |
2484 |
spin_unlock_irq(&task->sighand->siglock); |
2485 |
} |
2486 |
#else |
2487 |
{ |
2488 |
/* Copied from kernel/kmod.c */ |
2489 |
struct task_struct *task = current; |
2490 |
pid_t pid = kernel_thread(run_ccs_loader, NULL, 0); |
2491 |
sigset_t tmpsig; |
2492 |
spin_lock_irq(&task->sigmask_lock); |
2493 |
tmpsig = task->blocked; |
2494 |
siginitsetinv(&task->blocked, |
2495 |
sigmask(SIGKILL) | sigmask(SIGSTOP)); |
2496 |
recalc_sigpending(task); |
2497 |
spin_unlock_irq(&task->sigmask_lock); |
2498 |
if (pid >= 0) |
2499 |
waitpid(pid, NULL, __WCLONE); |
2500 |
spin_lock_irq(&task->sigmask_lock); |
2501 |
task->blocked = tmpsig; |
2502 |
recalc_sigpending(task); |
2503 |
spin_unlock_irq(&task->sigmask_lock); |
2504 |
} |
2505 |
#endif |
2506 |
#ifdef CONFIG_SAKURA |
2507 |
printk(KERN_INFO "SAKURA: 1.6.5-pre 2008/10/07\n"); |
2508 |
#endif |
2509 |
#ifdef CONFIG_TOMOYO |
2510 |
printk(KERN_INFO "TOMOYO: 1.6.5-pre 2008/10/07\n"); |
2511 |
#endif |
2512 |
printk(KERN_INFO "Mandatory Access Control activated.\n"); |
2513 |
sbin_init_started = true; |
2514 |
ccs_log_level = KERN_WARNING; |
2515 |
{ /* Check all profiles currently assigned to domains are defined. */ |
2516 |
struct domain_info *domain; |
2517 |
list1_for_each_entry(domain, &domain_list, list) { |
2518 |
const u8 profile = domain->profile; |
2519 |
if (profile_ptr[profile]) |
2520 |
continue; |
2521 |
panic("Profile %u (used by '%s') not defined.\n", |
2522 |
profile, domain->domainname->name); |
2523 |
} |
2524 |
} |
2525 |
} |
2526 |
|
2527 |
/* Wait queue for query_list. */ |
2528 |
static DECLARE_WAIT_QUEUE_HEAD(query_wait); |
2529 |
|
2530 |
/* Lock for manipurating query_list. */ |
2531 |
static DEFINE_SPINLOCK(query_lock); |
2532 |
|
2533 |
/* Structure for query. */ |
2534 |
struct query_entry { |
2535 |
struct list_head list; |
2536 |
char *query; |
2537 |
int query_len; |
2538 |
unsigned int serial; |
2539 |
int timer; |
2540 |
int answer; |
2541 |
}; |
2542 |
|
2543 |
/* The list for "struct query_entry". */ |
2544 |
static LIST_HEAD(query_list); |
2545 |
|
2546 |
/* Number of "struct file" referring /proc/ccs/query interface. */ |
2547 |
static atomic_t queryd_watcher = ATOMIC_INIT(0); |
2548 |
|
2549 |
/** |
2550 |
* ccs_check_supervisor - Ask for the supervisor's decision. |
2551 |
* |
2552 |
* @r: Pointer to "struct ccs_request_info". |
2553 |
* @fmt: The printf()'s format string, followed by parameters. |
2554 |
* |
2555 |
* Returns 0 if the supervisor decided to permit the access request which |
2556 |
* violated the policy in enforcing mode, 1 if the supervisor decided to |
2557 |
* retry the access request which violated the policy in enforcing mode, |
2558 |
* -EPERM otherwise. |
2559 |
*/ |
2560 |
int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...) |
2561 |
{ |
2562 |
va_list args; |
2563 |
int error = -EPERM; |
2564 |
int pos; |
2565 |
int len; |
2566 |
static unsigned int serial; |
2567 |
struct query_entry *query_entry = NULL; |
2568 |
char *header; |
2569 |
if (!r->domain) |
2570 |
r->domain = current->domain_info; |
2571 |
if (!atomic_read(&queryd_watcher)) { |
2572 |
int i; |
2573 |
if (current->tomoyo_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR) |
2574 |
return -EPERM; |
2575 |
for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD); |
2576 |
i++) { |
2577 |
set_current_state(TASK_INTERRUPTIBLE); |
2578 |
schedule_timeout(HZ / 10); |
2579 |
} |
2580 |
return -EPERM; |
2581 |
} |
2582 |
va_start(args, fmt); |
2583 |
len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32; |
2584 |
va_end(args); |
2585 |
#ifdef CONFIG_TOMOYO |
2586 |
header = ccs_init_audit_log(&len, r); |
2587 |
#else |
2588 |
header = ccs_alloc(1); |
2589 |
#endif |
2590 |
if (!header) |
2591 |
goto out; |
2592 |
query_entry = ccs_alloc(sizeof(*query_entry)); |
2593 |
if (!query_entry) |
2594 |
goto out; |
2595 |
query_entry->query = ccs_alloc(len); |
2596 |
if (!query_entry->query) |
2597 |
goto out; |
2598 |
INIT_LIST_HEAD(&query_entry->list); |
2599 |
/***** CRITICAL SECTION START *****/ |
2600 |
spin_lock(&query_lock); |
2601 |
query_entry->serial = serial++; |
2602 |
spin_unlock(&query_lock); |
2603 |
/***** CRITICAL SECTION END *****/ |
2604 |
pos = snprintf(query_entry->query, len - 1, "Q%u-%hu\n%s", |
2605 |
query_entry->serial, r->retry, header); |
2606 |
ccs_free(header); |
2607 |
header = NULL; |
2608 |
va_start(args, fmt); |
2609 |
vsnprintf(query_entry->query + pos, len - 1 - pos, fmt, args); |
2610 |
query_entry->query_len = strlen(query_entry->query) + 1; |
2611 |
va_end(args); |
2612 |
/***** CRITICAL SECTION START *****/ |
2613 |
spin_lock(&query_lock); |
2614 |
list_add_tail(&query_entry->list, &query_list); |
2615 |
spin_unlock(&query_lock); |
2616 |
/***** CRITICAL SECTION END *****/ |
2617 |
ccs_update_counter(CCS_UPDATES_COUNTER_QUERY); |
2618 |
/* Give 10 seconds for supervisor's opinion. */ |
2619 |
for (query_entry->timer = 0; atomic_read(&queryd_watcher) |
2620 |
&& query_entry->timer < 100; query_entry->timer++) { |
2621 |
wake_up(&query_wait); |
2622 |
set_current_state(TASK_INTERRUPTIBLE); |
2623 |
schedule_timeout(HZ / 10); |
2624 |
if (query_entry->answer) |
2625 |
break; |
2626 |
} |
2627 |
ccs_update_counter(CCS_UPDATES_COUNTER_QUERY); |
2628 |
/***** CRITICAL SECTION START *****/ |
2629 |
spin_lock(&query_lock); |
2630 |
list_del(&query_entry->list); |
2631 |
spin_unlock(&query_lock); |
2632 |
/***** CRITICAL SECTION END *****/ |
2633 |
switch (query_entry->answer) { |
2634 |
case 3: /* Asked to retry by administrator. */ |
2635 |
error = 1; |
2636 |
break; |
2637 |
case 1: |
2638 |
/* Granted by administrator. */ |
2639 |
error = 0; |
2640 |
break; |
2641 |
case 0: |
2642 |
/* Timed out. */ |
2643 |
break; |
2644 |
default: |
2645 |
/* Rejected by administrator. */ |
2646 |
break; |
2647 |
} |
2648 |
out: |
2649 |
if (query_entry) |
2650 |
ccs_free(query_entry->query); |
2651 |
ccs_free(query_entry); |
2652 |
ccs_free(header); |
2653 |
return error; |
2654 |
} |
2655 |
|
2656 |
/** |
2657 |
* poll_query - poll() for /proc/ccs/query. |
2658 |
* |
2659 |
* @file: Pointer to "struct file". |
2660 |
* @wait: Pointer to "poll_table". |
2661 |
* |
2662 |
* Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise. |
2663 |
* |
2664 |
* Waits for access requests which violated policy in enforcing mode. |
2665 |
*/ |
2666 |
static int poll_query(struct file *file, poll_table *wait) |
2667 |
{ |
2668 |
struct list_head *tmp; |
2669 |
bool found = false; |
2670 |
u8 i; |
2671 |
for (i = 0; i < 2; i++) { |
2672 |
/***** CRITICAL SECTION START *****/ |
2673 |
spin_lock(&query_lock); |
2674 |
list_for_each(tmp, &query_list) { |
2675 |
struct query_entry *ptr |
2676 |
= list_entry(tmp, struct query_entry, list); |
2677 |
if (ptr->answer) |
2678 |
continue; |
2679 |
found = true; |
2680 |
break; |
2681 |
} |
2682 |
spin_unlock(&query_lock); |
2683 |
/***** CRITICAL SECTION END *****/ |
2684 |
if (found) |
2685 |
return POLLIN | POLLRDNORM; |
2686 |
if (i) |
2687 |
break; |
2688 |
poll_wait(file, &query_wait, wait); |
2689 |
} |
2690 |
return 0; |
2691 |
} |
2692 |
|
2693 |
/** |
2694 |
* read_query - Read access requests which violated policy in enforcing mode. |
2695 |
* |
2696 |
* @head: Pointer to "struct ccs_io_buffer". |
2697 |
* |
2698 |
* Returns 0. |
2699 |
*/ |
2700 |
static int read_query(struct ccs_io_buffer *head) |
2701 |
{ |
2702 |
struct list_head *tmp; |
2703 |
int pos = 0; |
2704 |
int len = 0; |
2705 |
char *buf; |
2706 |
if (head->read_avail) |
2707 |
return 0; |
2708 |
if (head->read_buf) { |
2709 |
ccs_free(head->read_buf); |
2710 |
head->read_buf = NULL; |
2711 |
head->readbuf_size = 0; |
2712 |
} |
2713 |
/***** CRITICAL SECTION START *****/ |
2714 |
spin_lock(&query_lock); |
2715 |
list_for_each(tmp, &query_list) { |
2716 |
struct query_entry *ptr |
2717 |
= list_entry(tmp, struct query_entry, list); |
2718 |
if (ptr->answer) |
2719 |
continue; |
2720 |
if (pos++ != head->read_step) |
2721 |
continue; |
2722 |
len = ptr->query_len; |
2723 |
break; |
2724 |
} |
2725 |
spin_unlock(&query_lock); |
2726 |
/***** CRITICAL SECTION END *****/ |
2727 |
if (!len) { |
2728 |
head->read_step = 0; |
2729 |
return 0; |
2730 |
} |
2731 |
buf = ccs_alloc(len); |
2732 |
if (!buf) |
2733 |
return 0; |
2734 |
pos = 0; |
2735 |
/***** CRITICAL SECTION START *****/ |
2736 |
spin_lock(&query_lock); |
2737 |
list_for_each(tmp, &query_list) { |
2738 |
struct query_entry *ptr |
2739 |
= list_entry(tmp, struct query_entry, list); |
2740 |
if (ptr->answer) |
2741 |
continue; |
2742 |
if (pos++ != head->read_step) |
2743 |
continue; |
2744 |
/* |
2745 |
* Some query can be skipped because query_list |
2746 |
* can change, but I don't care. |
2747 |
*/ |
2748 |
if (len == ptr->query_len) |
2749 |
memmove(buf, ptr->query, len); |
2750 |
break; |
2751 |
} |
2752 |
spin_unlock(&query_lock); |
2753 |
/***** CRITICAL SECTION END *****/ |
2754 |
if (buf[0]) { |
2755 |
head->read_avail = len; |
2756 |
head->readbuf_size = head->read_avail; |
2757 |
head->read_buf = buf; |
2758 |
head->read_step++; |
2759 |
} else { |
2760 |
ccs_free(buf); |
2761 |
} |
2762 |
return 0; |
2763 |
} |
2764 |
|
2765 |
/** |
2766 |
* write_answer - Write the supervisor's decision. |
2767 |
* |
2768 |
* @head: Pointer to "struct ccs_io_buffer". |
2769 |
* |
2770 |
* Returns 0 on success, -EINVAL otherwise. |
2771 |
*/ |
2772 |
static int write_answer(struct ccs_io_buffer *head) |
2773 |
{ |
2774 |
char *data = head->write_buf; |
2775 |
struct list_head *tmp; |
2776 |
unsigned int serial; |
2777 |
unsigned int answer; |
2778 |
/***** CRITICAL SECTION START *****/ |
2779 |
spin_lock(&query_lock); |
2780 |
list_for_each(tmp, &query_list) { |
2781 |
struct query_entry *ptr |
2782 |
= list_entry(tmp, struct query_entry, list); |
2783 |
ptr->timer = 0; |
2784 |
} |
2785 |
spin_unlock(&query_lock); |
2786 |
/***** CRITICAL SECTION END *****/ |
2787 |
if (sscanf(data, "A%u=%u", &serial, &answer) != 2) |
2788 |
return -EINVAL; |
2789 |
/***** CRITICAL SECTION START *****/ |
2790 |
spin_lock(&query_lock); |
2791 |
list_for_each(tmp, &query_list) { |
2792 |
struct query_entry *ptr |
2793 |
= list_entry(tmp, struct query_entry, list); |
2794 |
if (ptr->serial != serial) |
2795 |
continue; |
2796 |
if (!ptr->answer) |
2797 |
ptr->answer = answer; |
2798 |
break; |
2799 |
} |
2800 |
spin_unlock(&query_lock); |
2801 |
/***** CRITICAL SECTION END *****/ |
2802 |
return 0; |
2803 |
} |
2804 |
|
2805 |
/* Policy updates counter. */ |
2806 |
static unsigned int updates_counter[MAX_CCS_UPDATES_COUNTER]; |
2807 |
|
2808 |
/* Policy updates counter lock. */ |
2809 |
static DEFINE_SPINLOCK(updates_counter_lock); |
2810 |
|
2811 |
/** |
2812 |
* ccs_update_counter - Increment policy change counter. |
2813 |
* |
2814 |
* @index: Type of policy. |
2815 |
* |
2816 |
* Returns nothing. |
2817 |
*/ |
2818 |
void ccs_update_counter(const unsigned char index) |
2819 |
{ |
2820 |
/***** CRITICAL SECTION START *****/ |
2821 |
spin_lock(&updates_counter_lock); |
2822 |
if (index < MAX_CCS_UPDATES_COUNTER) |
2823 |
updates_counter[index]++; |
2824 |
spin_unlock(&updates_counter_lock); |
2825 |
/***** CRITICAL SECTION END *****/ |
2826 |
} |
2827 |
|
2828 |
/** |
2829 |
* read_updates_counter - Check for policy change counter. |
2830 |
* |
2831 |
* @head: Pointer to "struct ccs_io_buffer". |
2832 |
* |
2833 |
* Returns how many times policy has changed since the previous check. |
2834 |
*/ |
2835 |
static int read_updates_counter(struct ccs_io_buffer *head) |
2836 |
{ |
2837 |
if (!head->read_eof) { |
2838 |
unsigned int counter[MAX_CCS_UPDATES_COUNTER]; |
2839 |
/***** CRITICAL SECTION START *****/ |
2840 |
spin_lock(&updates_counter_lock); |
2841 |
memmove(counter, updates_counter, sizeof(updates_counter)); |
2842 |
memset(updates_counter, 0, sizeof(updates_counter)); |
2843 |
spin_unlock(&updates_counter_lock); |
2844 |
/***** CRITICAL SECTION END *****/ |
2845 |
ccs_io_printf(head, |
2846 |
"/proc/ccs/system_policy: %10u\n" |
2847 |
"/proc/ccs/domain_policy: %10u\n" |
2848 |
"/proc/ccs/exception_policy: %10u\n" |
2849 |
"/proc/ccs/profile: %10u\n" |
2850 |
"/proc/ccs/query: %10u\n" |
2851 |
"/proc/ccs/manager: %10u\n" |
2852 |
"/proc/ccs/grant_log: %10u\n" |
2853 |
"/proc/ccs/reject_log: %10u\n", |
2854 |
counter[CCS_UPDATES_COUNTER_SYSTEM_POLICY], |
2855 |
counter[CCS_UPDATES_COUNTER_DOMAIN_POLICY], |
2856 |
counter[CCS_UPDATES_COUNTER_EXCEPTION_POLICY], |
2857 |
counter[CCS_UPDATES_COUNTER_PROFILE], |
2858 |
counter[CCS_UPDATES_COUNTER_QUERY], |
2859 |
counter[CCS_UPDATES_COUNTER_MANAGER], |
2860 |
counter[CCS_UPDATES_COUNTER_GRANT_LOG], |
2861 |
counter[CCS_UPDATES_COUNTER_REJECT_LOG]); |
2862 |
head->read_eof = true; |
2863 |
} |
2864 |
return 0; |
2865 |
} |
2866 |
|
2867 |
/** |
2868 |
* read_version: Get version. |
2869 |
* |
2870 |
* @head: Pointer to "struct ccs_io_buffer". |
2871 |
* |
2872 |
* Returns version information. |
2873 |
*/ |
2874 |
static int read_version(struct ccs_io_buffer *head) |
2875 |
{ |
2876 |
if (!head->read_eof) { |
2877 |
ccs_io_printf(head, "1.6.4"); |
2878 |
head->read_eof = true; |
2879 |
} |
2880 |
return 0; |
2881 |
} |
2882 |
|
2883 |
/** |
2884 |
* read_self_domain - Get the current process's domainname. |
2885 |
* |
2886 |
* @head: Pointer to "struct ccs_io_buffer". |
2887 |
* |
2888 |
* Returns the current process's domainname. |
2889 |
*/ |
2890 |
static int read_self_domain(struct ccs_io_buffer *head) |
2891 |
{ |
2892 |
if (!head->read_eof) { |
2893 |
/* |
2894 |
* current->domain_info->domainname != NULL |
2895 |
* because every process belongs to a domain and |
2896 |
* the domain's name cannot be NULL. |
2897 |
*/ |
2898 |
ccs_io_printf(head, "%s", |
2899 |
current->domain_info->domainname->name); |
2900 |
head->read_eof = true; |
2901 |
} |
2902 |
return 0; |
2903 |
} |
2904 |
|
2905 |
/** |
2906 |
* ccs_open_control - open() for /proc/ccs/ interface. |
2907 |
* |
2908 |
* @type: Type of interface. |
2909 |
* @file: Pointer to "struct file". |
2910 |
* |
2911 |
* Associates policy handler and returns 0 on success, -ENOMEM otherwise. |
2912 |
*/ |
2913 |
int ccs_open_control(const u8 type, struct file *file) |
2914 |
{ |
2915 |
struct ccs_io_buffer *head = ccs_alloc(sizeof(*head)); |
2916 |
if (!head) |
2917 |
return -ENOMEM; |
2918 |
mutex_init(&head->io_sem); |
2919 |
switch (type) { |
2920 |
#ifdef CONFIG_SAKURA |
2921 |
case CCS_SYSTEMPOLICY: /* /proc/ccs/system_policy */ |
2922 |
head->write = write_system_policy; |
2923 |
head->read = read_system_policy; |
2924 |
break; |
2925 |
#endif |
2926 |
#ifdef CONFIG_TOMOYO |
2927 |
case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */ |
2928 |
head->write = write_domain_policy; |
2929 |
head->read = read_domain_policy; |
2930 |
break; |
2931 |
case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */ |
2932 |
head->write = write_exception_policy; |
2933 |
head->read = read_exception_policy; |
2934 |
break; |
2935 |
case CCS_GRANTLOG: /* /proc/ccs/grant_log */ |
2936 |
head->poll = ccs_poll_grant_log; |
2937 |
head->read = ccs_read_grant_log; |
2938 |
break; |
2939 |
case CCS_REJECTLOG: /* /proc/ccs/reject_log */ |
2940 |
head->poll = ccs_poll_reject_log; |
2941 |
head->read = ccs_read_reject_log; |
2942 |
break; |
2943 |
#endif |
2944 |
case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */ |
2945 |
head->read = read_self_domain; |
2946 |
break; |
2947 |
case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */ |
2948 |
head->write = write_domain_profile; |
2949 |
head->read = read_domain_profile; |
2950 |
break; |
2951 |
case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */ |
2952 |
head->write = write_pid; |
2953 |
head->read = read_pid; |
2954 |
break; |
2955 |
case CCS_VERSION: /* /proc/ccs/version */ |
2956 |
head->read = read_version; |
2957 |
head->readbuf_size = 128; |
2958 |
break; |
2959 |
case CCS_MEMINFO: /* /proc/ccs/meminfo */ |
2960 |
head->write = ccs_write_memory_quota; |
2961 |
head->read = ccs_read_memory_counter; |
2962 |
head->readbuf_size = 512; |
2963 |
break; |
2964 |
case CCS_PROFILE: /* /proc/ccs/profile */ |
2965 |
head->write = write_profile; |
2966 |
head->read = read_profile; |
2967 |
break; |
2968 |
case CCS_QUERY: /* /proc/ccs/query */ |
2969 |
head->poll = poll_query; |
2970 |
head->write = write_answer; |
2971 |
head->read = read_query; |
2972 |
break; |
2973 |
case CCS_MANAGER: /* /proc/ccs/manager */ |
2974 |
head->write = write_manager_policy; |
2975 |
head->read = read_manager_policy; |
2976 |
break; |
2977 |
case CCS_UPDATESCOUNTER: /* /proc/ccs/.updates_counter */ |
2978 |
head->read = read_updates_counter; |
2979 |
break; |
2980 |
} |
2981 |
if (!(file->f_mode & FMODE_READ)) { |
2982 |
/* |
2983 |
* No need to allocate read_buf since it is not opened |
2984 |
* for reading. |
2985 |
*/ |
2986 |
head->read = NULL; |
2987 |
head->poll = NULL; |
2988 |
} else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG |
2989 |
&& type != CCS_QUERY) { |
2990 |
/* |
2991 |
* Don't allocate buffer for reading if the file is one of |
2992 |
* /proc/ccs/grant_log , /proc/ccs/reject_log , /proc/ccs/query. |
2993 |
*/ |
2994 |
if (!head->readbuf_size) |
2995 |
head->readbuf_size = 4096 * 2; |
2996 |
head->read_buf = ccs_alloc(head->readbuf_size); |
2997 |
if (!head->read_buf) { |
2998 |
ccs_free(head); |
2999 |
return -ENOMEM; |
3000 |
} |
3001 |
} |
3002 |
if (!(file->f_mode & FMODE_WRITE)) { |
3003 |
/* |
3004 |
* No need to allocate write_buf since it is not opened |
3005 |
* for writing. |
3006 |
*/ |
3007 |
head->write = NULL; |
3008 |
} else if (head->write) { |
3009 |
head->writebuf_size = 4096 * 2; |
3010 |
head->write_buf = ccs_alloc(head->writebuf_size); |
3011 |
if (!head->write_buf) { |
3012 |
ccs_free(head->read_buf); |
3013 |
ccs_free(head); |
3014 |
return -ENOMEM; |
3015 |
} |
3016 |
} |
3017 |
file->private_data = head; |
3018 |
/* |
3019 |
* Call the handler now if the file is /proc/ccs/self_domain |
3020 |
* so that the user can use "cat < /proc/ccs/self_domain" to |
3021 |
* know the current process's domainname. |
3022 |
*/ |
3023 |
if (type == CCS_SELFDOMAIN) |
3024 |
ccs_read_control(file, NULL, 0); |
3025 |
/* |
3026 |
* If the file is /proc/ccs/query , increment the monitor count. |
3027 |
* The monitor count is used by ccs_check_supervisor() to see if |
3028 |
* there is some process monitoring /proc/ccs/query. |
3029 |
*/ |
3030 |
else if (head->write == write_answer || head->read == read_query) |
3031 |
atomic_inc(&queryd_watcher); |
3032 |
return 0; |
3033 |
} |
3034 |
|
3035 |
/** |
3036 |
* ccs_poll_control - poll() for /proc/ccs/ interface. |
3037 |
* |
3038 |
* @file: Pointer to "struct file". |
3039 |
* @wait: Pointer to "poll_table". |
3040 |
* |
3041 |
* Waits for read readiness. |
3042 |
* /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and |
3043 |
* /proc/ccs/grant_log and /proc/ccs/reject_log are handled by |
3044 |
* /usr/lib/ccs/ccs-auditd. |
3045 |
*/ |
3046 |
int ccs_poll_control(struct file *file, poll_table *wait) |
3047 |
{ |
3048 |
struct ccs_io_buffer *head = file->private_data; |
3049 |
if (!head->poll) |
3050 |
return -ENOSYS; |
3051 |
return head->poll(file, wait); |
3052 |
} |
3053 |
|
3054 |
/** |
3055 |
* ccs_read_control - read() for /proc/ccs/ interface. |
3056 |
* |
3057 |
* @file: Pointer to "struct file". |
3058 |
* @buffer: Poiner to buffer to write to. |
3059 |
* @buffer_len: Size of @buffer. |
3060 |
* |
3061 |
* Returns bytes read on success, negative value otherwise. |
3062 |
*/ |
3063 |
int ccs_read_control(struct file *file, char __user *buffer, |
3064 |
const int buffer_len) |
3065 |
{ |
3066 |
int len = 0; |
3067 |
struct ccs_io_buffer *head = file->private_data; |
3068 |
char *cp; |
3069 |
if (!head->read) |
3070 |
return -ENOSYS; |
3071 |
if (!access_ok(VERIFY_WRITE, buffer, buffer_len)) |
3072 |
return -EFAULT; |
3073 |
if (mutex_lock_interruptible(&head->io_sem)) |
3074 |
return -EINTR; |
3075 |
/* Call the policy handler. */ |
3076 |
len = head->read(head); |
3077 |
if (len < 0) |
3078 |
goto out; |
3079 |
/* Write to buffer. */ |
3080 |
len = head->read_avail; |
3081 |
if (len > buffer_len) |
3082 |
len = buffer_len; |
3083 |
if (!len) |
3084 |
goto out; |
3085 |
/* head->read_buf changes by some functions. */ |
3086 |
cp = head->read_buf; |
3087 |
if (copy_to_user(buffer, cp, len)) { |
3088 |
len = -EFAULT; |
3089 |
goto out; |
3090 |
} |
3091 |
head->read_avail -= len; |
3092 |
memmove(cp, cp + len, head->read_avail); |
3093 |
out: |
3094 |
mutex_unlock(&head->io_sem); |
3095 |
return len; |
3096 |
} |
3097 |
|
3098 |
/** |
3099 |
* ccs_write_control - write() for /proc/ccs/ interface. |
3100 |
* |
3101 |
* @file: Pointer to "struct file". |
3102 |
* @buffer: Pointer to buffer to read from. |
3103 |
* @buffer_len: Size of @buffer. |
3104 |
* |
3105 |
* Returns @buffer_len on success, negative value otherwise. |
3106 |
*/ |
3107 |
int ccs_write_control(struct file *file, const char __user *buffer, |
3108 |
const int buffer_len) |
3109 |
{ |
3110 |
struct ccs_io_buffer *head = file->private_data; |
3111 |
int error = buffer_len; |
3112 |
int avail_len = buffer_len; |
3113 |
char *cp0 = head->write_buf; |
3114 |
if (!head->write) |
3115 |
return -ENOSYS; |
3116 |
if (!access_ok(VERIFY_READ, buffer, buffer_len)) |
3117 |
return -EFAULT; |
3118 |
/* Don't allow updating policies by non manager programs. */ |
3119 |
if (head->write != write_pid && |
3120 |
#ifdef CONFIG_TOMOYO |
3121 |
head->write != write_domain_policy && |
3122 |
#endif |
3123 |
!is_policy_manager()) |
3124 |
return -EPERM; |
3125 |
if (mutex_lock_interruptible(&head->io_sem)) |
3126 |
return -EINTR; |
3127 |
/* Read a line and dispatch it to the policy handler. */ |
3128 |
while (avail_len > 0) { |
3129 |
char c; |
3130 |
if (head->write_avail >= head->writebuf_size - 1) { |
3131 |
error = -ENOMEM; |
3132 |
break; |
3133 |
} else if (get_user(c, buffer)) { |
3134 |
error = -EFAULT; |
3135 |
break; |
3136 |
} |
3137 |
buffer++; |
3138 |
avail_len--; |
3139 |
cp0[head->write_avail++] = c; |
3140 |
if (c != '\n') |
3141 |
continue; |
3142 |
cp0[head->write_avail - 1] = '\0'; |
3143 |
head->write_avail = 0; |
3144 |
normalize_line(cp0); |
3145 |
head->write(head); |
3146 |
} |
3147 |
mutex_unlock(&head->io_sem); |
3148 |
return error; |
3149 |
} |
3150 |
|
3151 |
/** |
3152 |
* ccs_close_control - close() for /proc/ccs/ interface. |
3153 |
* |
3154 |
* @file: Pointer to "struct file". |
3155 |
* |
3156 |
* Releases memory and returns 0. |
3157 |
*/ |
3158 |
int ccs_close_control(struct file *file) |
3159 |
{ |
3160 |
struct ccs_io_buffer *head = file->private_data; |
3161 |
/* |
3162 |
* If the file is /proc/ccs/query , decrement the monitor count. |
3163 |
*/ |
3164 |
if (head->write == write_answer || head->read == read_query) |
3165 |
atomic_dec(&queryd_watcher); |
3166 |
/* Release memory used for policy I/O. */ |
3167 |
ccs_free(head->read_buf); |
3168 |
head->read_buf = NULL; |
3169 |
ccs_free(head->write_buf); |
3170 |
head->write_buf = NULL; |
3171 |
ccs_free(head); |
3172 |
head = NULL; |
3173 |
file->private_data = NULL; |
3174 |
return 0; |
3175 |
} |
3176 |
|
3177 |
/** |
3178 |
* ccs_alloc_acl_element - Allocate permanent memory for ACL entry. |
3179 |
* |
3180 |
* @acl_type: Type of ACL entry. |
3181 |
* @condition: Pointer to condition part of the ACL entry. May be NULL. |
3182 |
* |
3183 |
* Returns pointer to the ACL entry on success, NULL otherwise. |
3184 |
*/ |
3185 |
void *ccs_alloc_acl_element(const u8 acl_type, |
3186 |
const struct condition_list *condition) |
3187 |
{ |
3188 |
int len; |
3189 |
struct acl_info *ptr; |
3190 |
switch (acl_type) { |
3191 |
case TYPE_SINGLE_PATH_ACL: |
3192 |
len = sizeof(struct single_path_acl_record); |
3193 |
break; |
3194 |
case TYPE_DOUBLE_PATH_ACL: |
3195 |
len = sizeof(struct double_path_acl_record); |
3196 |
break; |
3197 |
case TYPE_ARGV0_ACL: |
3198 |
len = sizeof(struct argv0_acl_record); |
3199 |
break; |
3200 |
case TYPE_ENV_ACL: |
3201 |
len = sizeof(struct env_acl_record); |
3202 |
break; |
3203 |
case TYPE_CAPABILITY_ACL: |
3204 |
len = sizeof(struct capability_acl_record); |
3205 |
break; |
3206 |
case TYPE_IP_NETWORK_ACL: |
3207 |
len = sizeof(struct ip_network_acl_record); |
3208 |
break; |
3209 |
case TYPE_SIGNAL_ACL: |
3210 |
len = sizeof(struct signal_acl_record); |
3211 |
break; |
3212 |
case TYPE_EXECUTE_HANDLER: |
3213 |
case TYPE_DENIED_EXECUTE_HANDLER: |
3214 |
len = sizeof(struct execute_handler_record); |
3215 |
break; |
3216 |
default: |
3217 |
return NULL; |
3218 |
} |
3219 |
/* |
3220 |
* If the ACL doesn't have condition part, reduce memory usage |
3221 |
* by eliminating sizeof(struct condition_list *). |
3222 |
*/ |
3223 |
if (!condition) |
3224 |
len -= sizeof(ptr->access_me_via_ccs_get_condition_part); |
3225 |
ptr = ccs_alloc_element(len); |
3226 |
if (!ptr) |
3227 |
return NULL; |
3228 |
if (condition) { |
3229 |
ptr->access_me_via_ccs_get_condition_part = condition; |
3230 |
ptr->type = acl_type | ACL_WITH_CONDITION; |
3231 |
return ptr; |
3232 |
} |
3233 |
/* |
3234 |
* Substract sizeof(struct condition_list *) because I eliminated |
3235 |
* sizeof(struct condition_list *) from "struct acl_info" |
3236 |
* but I must return the start address of "struct acl_info". |
3237 |
*/ |
3238 |
ptr = (void *) (((u8 *) ptr) |
3239 |
- sizeof(ptr->access_me_via_ccs_get_condition_part)); |
3240 |
ptr->type = acl_type; |
3241 |
return ptr; |
3242 |
} |
3243 |
|
3244 |
/** |
3245 |
* ccs_get_condition_part - Get condition part of the given ACL entry. |
3246 |
* |
3247 |
* @acl: Pointer to "struct acl_info". Pointer to an ACL entry. |
3248 |
* |
3249 |
* Returns pointer to the condition part if the ACL has it, NULL otherwise. |
3250 |
*/ |
3251 |
const struct condition_list *ccs_get_condition_part(const struct acl_info *acl) |
3252 |
{ |
3253 |
return (acl->type & ACL_WITH_CONDITION) ? |
3254 |
acl->access_me_via_ccs_get_condition_part : NULL; |
3255 |
} |