Commit | Line | Data |
---|---|---|
9590837b KT |
1 | /* |
2 | * security/tomoyo/common.c | |
3 | * | |
4 | * Common functions for TOMOYO. | |
5 | * | |
c3ef1500 | 6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION |
9590837b KT |
7 | */ |
8 | ||
9 | #include <linux/uaccess.h> | |
5a0e3ad6 | 10 | #include <linux/slab.h> |
9590837b | 11 | #include <linux/security.h> |
9590837b | 12 | #include "common.h" |
9590837b | 13 | |
eadd99cc TH |
14 | /* String table for operation mode. */ |
15 | const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = { | |
16 | [TOMOYO_CONFIG_DISABLED] = "disabled", | |
17 | [TOMOYO_CONFIG_LEARNING] = "learning", | |
18 | [TOMOYO_CONFIG_PERMISSIVE] = "permissive", | |
19 | [TOMOYO_CONFIG_ENFORCING] = "enforcing" | |
9590837b | 20 | }; |
9590837b | 21 | |
57c2590f | 22 | /* String table for /sys/kernel/security/tomoyo/profile */ |
2c47ab93 | 23 | const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX |
57c2590f | 24 | + TOMOYO_MAX_MAC_CATEGORY_INDEX] = { |
2c47ab93 TH |
25 | [TOMOYO_MAC_FILE_EXECUTE] = "execute", |
26 | [TOMOYO_MAC_FILE_OPEN] = "open", | |
27 | [TOMOYO_MAC_FILE_CREATE] = "create", | |
28 | [TOMOYO_MAC_FILE_UNLINK] = "unlink", | |
29 | [TOMOYO_MAC_FILE_GETATTR] = "getattr", | |
30 | [TOMOYO_MAC_FILE_MKDIR] = "mkdir", | |
31 | [TOMOYO_MAC_FILE_RMDIR] = "rmdir", | |
32 | [TOMOYO_MAC_FILE_MKFIFO] = "mkfifo", | |
33 | [TOMOYO_MAC_FILE_MKSOCK] = "mksock", | |
34 | [TOMOYO_MAC_FILE_TRUNCATE] = "truncate", | |
35 | [TOMOYO_MAC_FILE_SYMLINK] = "symlink", | |
36 | [TOMOYO_MAC_FILE_MKBLOCK] = "mkblock", | |
37 | [TOMOYO_MAC_FILE_MKCHAR] = "mkchar", | |
38 | [TOMOYO_MAC_FILE_LINK] = "link", | |
39 | [TOMOYO_MAC_FILE_RENAME] = "rename", | |
40 | [TOMOYO_MAC_FILE_CHMOD] = "chmod", | |
41 | [TOMOYO_MAC_FILE_CHOWN] = "chown", | |
42 | [TOMOYO_MAC_FILE_CHGRP] = "chgrp", | |
43 | [TOMOYO_MAC_FILE_IOCTL] = "ioctl", | |
44 | [TOMOYO_MAC_FILE_CHROOT] = "chroot", | |
45 | [TOMOYO_MAC_FILE_MOUNT] = "mount", | |
46 | [TOMOYO_MAC_FILE_UMOUNT] = "unmount", | |
47 | [TOMOYO_MAC_FILE_PIVOT_ROOT] = "pivot_root", | |
57c2590f | 48 | [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file", |
9590837b KT |
49 | }; |
50 | ||
2066a361 TH |
51 | /* String table for conditions. */ |
52 | const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = { | |
53 | [TOMOYO_TASK_UID] = "task.uid", | |
54 | [TOMOYO_TASK_EUID] = "task.euid", | |
55 | [TOMOYO_TASK_SUID] = "task.suid", | |
56 | [TOMOYO_TASK_FSUID] = "task.fsuid", | |
57 | [TOMOYO_TASK_GID] = "task.gid", | |
58 | [TOMOYO_TASK_EGID] = "task.egid", | |
59 | [TOMOYO_TASK_SGID] = "task.sgid", | |
60 | [TOMOYO_TASK_FSGID] = "task.fsgid", | |
61 | [TOMOYO_TASK_PID] = "task.pid", | |
62 | [TOMOYO_TASK_PPID] = "task.ppid", | |
5b636857 TH |
63 | [TOMOYO_EXEC_ARGC] = "exec.argc", |
64 | [TOMOYO_EXEC_ENVC] = "exec.envc", | |
8761afd4 TH |
65 | [TOMOYO_TYPE_IS_SOCKET] = "socket", |
66 | [TOMOYO_TYPE_IS_SYMLINK] = "symlink", | |
67 | [TOMOYO_TYPE_IS_FILE] = "file", | |
68 | [TOMOYO_TYPE_IS_BLOCK_DEV] = "block", | |
69 | [TOMOYO_TYPE_IS_DIRECTORY] = "directory", | |
70 | [TOMOYO_TYPE_IS_CHAR_DEV] = "char", | |
71 | [TOMOYO_TYPE_IS_FIFO] = "fifo", | |
72 | [TOMOYO_MODE_SETUID] = "setuid", | |
73 | [TOMOYO_MODE_SETGID] = "setgid", | |
74 | [TOMOYO_MODE_STICKY] = "sticky", | |
75 | [TOMOYO_MODE_OWNER_READ] = "owner_read", | |
76 | [TOMOYO_MODE_OWNER_WRITE] = "owner_write", | |
77 | [TOMOYO_MODE_OWNER_EXECUTE] = "owner_execute", | |
78 | [TOMOYO_MODE_GROUP_READ] = "group_read", | |
79 | [TOMOYO_MODE_GROUP_WRITE] = "group_write", | |
80 | [TOMOYO_MODE_GROUP_EXECUTE] = "group_execute", | |
81 | [TOMOYO_MODE_OTHERS_READ] = "others_read", | |
82 | [TOMOYO_MODE_OTHERS_WRITE] = "others_write", | |
83 | [TOMOYO_MODE_OTHERS_EXECUTE] = "others_execute", | |
2ca9bf45 TH |
84 | [TOMOYO_EXEC_REALPATH] = "exec.realpath", |
85 | [TOMOYO_SYMLINK_TARGET] = "symlink.target", | |
8761afd4 TH |
86 | [TOMOYO_PATH1_UID] = "path1.uid", |
87 | [TOMOYO_PATH1_GID] = "path1.gid", | |
88 | [TOMOYO_PATH1_INO] = "path1.ino", | |
89 | [TOMOYO_PATH1_MAJOR] = "path1.major", | |
90 | [TOMOYO_PATH1_MINOR] = "path1.minor", | |
91 | [TOMOYO_PATH1_PERM] = "path1.perm", | |
92 | [TOMOYO_PATH1_TYPE] = "path1.type", | |
93 | [TOMOYO_PATH1_DEV_MAJOR] = "path1.dev_major", | |
94 | [TOMOYO_PATH1_DEV_MINOR] = "path1.dev_minor", | |
95 | [TOMOYO_PATH2_UID] = "path2.uid", | |
96 | [TOMOYO_PATH2_GID] = "path2.gid", | |
97 | [TOMOYO_PATH2_INO] = "path2.ino", | |
98 | [TOMOYO_PATH2_MAJOR] = "path2.major", | |
99 | [TOMOYO_PATH2_MINOR] = "path2.minor", | |
100 | [TOMOYO_PATH2_PERM] = "path2.perm", | |
101 | [TOMOYO_PATH2_TYPE] = "path2.type", | |
102 | [TOMOYO_PATH2_DEV_MAJOR] = "path2.dev_major", | |
103 | [TOMOYO_PATH2_DEV_MINOR] = "path2.dev_minor", | |
104 | [TOMOYO_PATH1_PARENT_UID] = "path1.parent.uid", | |
105 | [TOMOYO_PATH1_PARENT_GID] = "path1.parent.gid", | |
106 | [TOMOYO_PATH1_PARENT_INO] = "path1.parent.ino", | |
107 | [TOMOYO_PATH1_PARENT_PERM] = "path1.parent.perm", | |
108 | [TOMOYO_PATH2_PARENT_UID] = "path2.parent.uid", | |
109 | [TOMOYO_PATH2_PARENT_GID] = "path2.parent.gid", | |
110 | [TOMOYO_PATH2_PARENT_INO] = "path2.parent.ino", | |
111 | [TOMOYO_PATH2_PARENT_PERM] = "path2.parent.perm", | |
2066a361 TH |
112 | }; |
113 | ||
d5ca1725 TH |
114 | /* String table for PREFERENCE keyword. */ |
115 | static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = { | |
eadd99cc | 116 | [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log", |
d5ca1725 TH |
117 | [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry", |
118 | }; | |
119 | ||
2c47ab93 TH |
120 | /* String table for path operation. */ |
121 | const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = { | |
122 | [TOMOYO_TYPE_EXECUTE] = "execute", | |
123 | [TOMOYO_TYPE_READ] = "read", | |
124 | [TOMOYO_TYPE_WRITE] = "write", | |
125 | [TOMOYO_TYPE_APPEND] = "append", | |
126 | [TOMOYO_TYPE_UNLINK] = "unlink", | |
127 | [TOMOYO_TYPE_GETATTR] = "getattr", | |
128 | [TOMOYO_TYPE_RMDIR] = "rmdir", | |
129 | [TOMOYO_TYPE_TRUNCATE] = "truncate", | |
130 | [TOMOYO_TYPE_SYMLINK] = "symlink", | |
131 | [TOMOYO_TYPE_CHROOT] = "chroot", | |
132 | [TOMOYO_TYPE_UMOUNT] = "unmount", | |
133 | }; | |
134 | ||
135 | /* String table for categories. */ | |
136 | static const char * const tomoyo_category_keywords | |
137 | [TOMOYO_MAX_MAC_CATEGORY_INDEX] = { | |
138 | [TOMOYO_MAC_CATEGORY_FILE] = "file", | |
139 | }; | |
140 | ||
9590837b KT |
141 | /* Permit policy management by non-root user? */ |
142 | static bool tomoyo_manage_by_non_root; | |
143 | ||
144 | /* Utility functions. */ | |
145 | ||
57c2590f TH |
146 | /** |
147 | * tomoyo_yesno - Return "yes" or "no". | |
148 | * | |
149 | * @value: Bool value. | |
150 | */ | |
eadd99cc | 151 | const char *tomoyo_yesno(const unsigned int value) |
57c2590f TH |
152 | { |
153 | return value ? "yes" : "no"; | |
154 | } | |
155 | ||
d5ca1725 TH |
156 | /** |
157 | * tomoyo_addprintf - strncat()-like-snprintf(). | |
158 | * | |
159 | * @buffer: Buffer to write to. Must be '\0'-terminated. | |
160 | * @len: Size of @buffer. | |
161 | * @fmt: The printf()'s format string, followed by parameters. | |
162 | * | |
163 | * Returns nothing. | |
164 | */ | |
f23571e8 TH |
165 | static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...) |
166 | { | |
167 | va_list args; | |
168 | const int pos = strlen(buffer); | |
169 | va_start(args, fmt); | |
170 | vsnprintf(buffer + pos, len - pos - 1, fmt, args); | |
171 | va_end(args); | |
172 | } | |
173 | ||
7762fbff | 174 | /** |
f23571e8 | 175 | * tomoyo_flush - Flush queued string to userspace's buffer. |
7762fbff | 176 | * |
f23571e8 | 177 | * @head: Pointer to "struct tomoyo_io_buffer". |
7762fbff | 178 | * |
f23571e8 | 179 | * Returns true if all data was flushed, false otherwise. |
7762fbff | 180 | */ |
f23571e8 | 181 | static bool tomoyo_flush(struct tomoyo_io_buffer *head) |
7762fbff | 182 | { |
f23571e8 TH |
183 | while (head->r.w_pos) { |
184 | const char *w = head->r.w[0]; | |
2c47ab93 | 185 | size_t len = strlen(w); |
f23571e8 TH |
186 | if (len) { |
187 | if (len > head->read_user_buf_avail) | |
188 | len = head->read_user_buf_avail; | |
189 | if (!len) | |
190 | return false; | |
191 | if (copy_to_user(head->read_user_buf, w, len)) | |
192 | return false; | |
193 | head->read_user_buf_avail -= len; | |
194 | head->read_user_buf += len; | |
195 | w += len; | |
196 | } | |
c0fa797a TH |
197 | head->r.w[0] = w; |
198 | if (*w) | |
f23571e8 | 199 | return false; |
eadd99cc | 200 | /* Add '\0' for audit logs and query. */ |
f23571e8 TH |
201 | if (head->poll) { |
202 | if (!head->read_user_buf_avail || | |
203 | copy_to_user(head->read_user_buf, "", 1)) | |
204 | return false; | |
205 | head->read_user_buf_avail--; | |
206 | head->read_user_buf++; | |
207 | } | |
208 | head->r.w_pos--; | |
209 | for (len = 0; len < head->r.w_pos; len++) | |
210 | head->r.w[len] = head->r.w[len + 1]; | |
211 | } | |
212 | head->r.avail = 0; | |
213 | return true; | |
7762fbff TH |
214 | } |
215 | ||
4c3e9e2d | 216 | /** |
f23571e8 | 217 | * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure. |
4c3e9e2d | 218 | * |
f23571e8 TH |
219 | * @head: Pointer to "struct tomoyo_io_buffer". |
220 | * @string: String to print. | |
4c3e9e2d | 221 | * |
f23571e8 TH |
222 | * Note that @string has to be kept valid until @head is kfree()d. |
223 | * This means that char[] allocated on stack memory cannot be passed to | |
224 | * this function. Use tomoyo_io_printf() for char[] allocated on stack memory. | |
4c3e9e2d | 225 | */ |
f23571e8 | 226 | static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string) |
4c3e9e2d | 227 | { |
f23571e8 TH |
228 | if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) { |
229 | head->r.w[head->r.w_pos++] = string; | |
230 | tomoyo_flush(head); | |
231 | } else | |
232 | WARN_ON(1); | |
4c3e9e2d TH |
233 | } |
234 | ||
9590837b | 235 | /** |
f23571e8 | 236 | * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure. |
9590837b KT |
237 | * |
238 | * @head: Pointer to "struct tomoyo_io_buffer". | |
239 | * @fmt: The printf()'s format string, followed by parameters. | |
9590837b | 240 | */ |
f23571e8 | 241 | void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...) |
9590837b KT |
242 | { |
243 | va_list args; | |
2c47ab93 TH |
244 | size_t len; |
245 | size_t pos = head->r.avail; | |
9590837b | 246 | int size = head->readbuf_size - pos; |
9590837b | 247 | if (size <= 0) |
f23571e8 | 248 | return; |
9590837b | 249 | va_start(args, fmt); |
f23571e8 | 250 | len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1; |
9590837b | 251 | va_end(args); |
f23571e8 TH |
252 | if (pos + len >= head->readbuf_size) { |
253 | WARN_ON(1); | |
254 | return; | |
255 | } | |
256 | head->r.avail += len; | |
257 | tomoyo_set_string(head, head->read_buf + pos); | |
258 | } | |
259 | ||
0d2171d7 TH |
260 | /** |
261 | * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure. | |
262 | * | |
263 | * @head: Pointer to "struct tomoyo_io_buffer". | |
264 | * | |
265 | * Returns nothing. | |
266 | */ | |
f23571e8 TH |
267 | static void tomoyo_set_space(struct tomoyo_io_buffer *head) |
268 | { | |
269 | tomoyo_set_string(head, " "); | |
270 | } | |
271 | ||
0d2171d7 TH |
272 | /** |
273 | * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure. | |
274 | * | |
275 | * @head: Pointer to "struct tomoyo_io_buffer". | |
276 | * | |
277 | * Returns nothing. | |
278 | */ | |
f23571e8 TH |
279 | static bool tomoyo_set_lf(struct tomoyo_io_buffer *head) |
280 | { | |
281 | tomoyo_set_string(head, "\n"); | |
282 | return !head->r.w_pos; | |
283 | } | |
284 | ||
0d2171d7 TH |
285 | /** |
286 | * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure. | |
287 | * | |
288 | * @head: Pointer to "struct tomoyo_io_buffer". | |
289 | * | |
290 | * Returns nothing. | |
291 | */ | |
292 | static void tomoyo_set_slash(struct tomoyo_io_buffer *head) | |
293 | { | |
294 | tomoyo_set_string(head, "/"); | |
295 | } | |
296 | ||
bd03a3e4 TH |
297 | /* List of namespaces. */ |
298 | LIST_HEAD(tomoyo_namespace_list); | |
299 | /* True if namespace other than tomoyo_kernel_namespace is defined. */ | |
300 | static bool tomoyo_namespace_enabled; | |
301 | ||
302 | /** | |
303 | * tomoyo_init_policy_namespace - Initialize namespace. | |
304 | * | |
305 | * @ns: Pointer to "struct tomoyo_policy_namespace". | |
306 | * | |
307 | * Returns nothing. | |
308 | */ | |
309 | void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns) | |
310 | { | |
311 | unsigned int idx; | |
312 | for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++) | |
313 | INIT_LIST_HEAD(&ns->acl_group[idx]); | |
314 | for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++) | |
315 | INIT_LIST_HEAD(&ns->group_list[idx]); | |
316 | for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++) | |
317 | INIT_LIST_HEAD(&ns->policy_list[idx]); | |
318 | ns->profile_version = 20100903; | |
319 | tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list); | |
320 | list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list); | |
321 | } | |
322 | ||
323 | /** | |
324 | * tomoyo_print_namespace - Print namespace header. | |
325 | * | |
326 | * @head: Pointer to "struct tomoyo_io_buffer". | |
327 | * | |
328 | * Returns nothing. | |
329 | */ | |
330 | static void tomoyo_print_namespace(struct tomoyo_io_buffer *head) | |
331 | { | |
332 | if (!tomoyo_namespace_enabled) | |
333 | return; | |
334 | tomoyo_set_string(head, | |
335 | container_of(head->r.ns, | |
336 | struct tomoyo_policy_namespace, | |
337 | namespace_list)->name); | |
338 | tomoyo_set_space(head); | |
339 | } | |
340 | ||
f23571e8 TH |
341 | /** |
342 | * tomoyo_print_name_union - Print a tomoyo_name_union. | |
343 | * | |
344 | * @head: Pointer to "struct tomoyo_io_buffer". | |
345 | * @ptr: Pointer to "struct tomoyo_name_union". | |
346 | */ | |
347 | static void tomoyo_print_name_union(struct tomoyo_io_buffer *head, | |
348 | const struct tomoyo_name_union *ptr) | |
349 | { | |
350 | tomoyo_set_space(head); | |
0df7e8b8 | 351 | if (ptr->group) { |
f23571e8 TH |
352 | tomoyo_set_string(head, "@"); |
353 | tomoyo_set_string(head, ptr->group->group_name->name); | |
354 | } else { | |
355 | tomoyo_set_string(head, ptr->filename->name); | |
356 | } | |
357 | } | |
358 | ||
2ca9bf45 TH |
359 | /** |
360 | * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote. | |
361 | * | |
362 | * @head: Pointer to "struct tomoyo_io_buffer". | |
363 | * @ptr: Pointer to "struct tomoyo_name_union". | |
364 | * | |
365 | * Returns nothing. | |
366 | */ | |
367 | static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head, | |
368 | const struct tomoyo_name_union *ptr) | |
369 | { | |
370 | if (ptr->group) { | |
371 | tomoyo_set_string(head, "@"); | |
372 | tomoyo_set_string(head, ptr->group->group_name->name); | |
373 | } else { | |
374 | tomoyo_set_string(head, "\""); | |
375 | tomoyo_set_string(head, ptr->filename->name); | |
376 | tomoyo_set_string(head, "\""); | |
377 | } | |
378 | } | |
379 | ||
f23571e8 | 380 | /** |
2066a361 | 381 | * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space. |
f23571e8 | 382 | * |
2066a361 TH |
383 | * @head: Pointer to "struct tomoyo_io_buffer". |
384 | * @ptr: Pointer to "struct tomoyo_number_union". | |
385 | * | |
386 | * Returns nothing. | |
f23571e8 | 387 | */ |
2066a361 TH |
388 | static void tomoyo_print_number_union_nospace |
389 | (struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr) | |
f23571e8 | 390 | { |
0df7e8b8 | 391 | if (ptr->group) { |
f23571e8 TH |
392 | tomoyo_set_string(head, "@"); |
393 | tomoyo_set_string(head, ptr->group->group_name->name); | |
394 | } else { | |
395 | int i; | |
396 | unsigned long min = ptr->values[0]; | |
397 | const unsigned long max = ptr->values[1]; | |
0df7e8b8 TH |
398 | u8 min_type = ptr->value_type[0]; |
399 | const u8 max_type = ptr->value_type[1]; | |
f23571e8 TH |
400 | char buffer[128]; |
401 | buffer[0] = '\0'; | |
402 | for (i = 0; i < 2; i++) { | |
403 | switch (min_type) { | |
404 | case TOMOYO_VALUE_TYPE_HEXADECIMAL: | |
405 | tomoyo_addprintf(buffer, sizeof(buffer), | |
406 | "0x%lX", min); | |
407 | break; | |
408 | case TOMOYO_VALUE_TYPE_OCTAL: | |
409 | tomoyo_addprintf(buffer, sizeof(buffer), | |
410 | "0%lo", min); | |
411 | break; | |
412 | default: | |
2066a361 TH |
413 | tomoyo_addprintf(buffer, sizeof(buffer), "%lu", |
414 | min); | |
f23571e8 TH |
415 | break; |
416 | } | |
417 | if (min == max && min_type == max_type) | |
418 | break; | |
419 | tomoyo_addprintf(buffer, sizeof(buffer), "-"); | |
420 | min_type = max_type; | |
421 | min = max; | |
422 | } | |
423 | tomoyo_io_printf(head, "%s", buffer); | |
424 | } | |
9590837b KT |
425 | } |
426 | ||
2066a361 TH |
427 | /** |
428 | * tomoyo_print_number_union - Print a tomoyo_number_union. | |
429 | * | |
430 | * @head: Pointer to "struct tomoyo_io_buffer". | |
431 | * @ptr: Pointer to "struct tomoyo_number_union". | |
432 | * | |
433 | * Returns nothing. | |
434 | */ | |
435 | static void tomoyo_print_number_union(struct tomoyo_io_buffer *head, | |
436 | const struct tomoyo_number_union *ptr) | |
437 | { | |
438 | tomoyo_set_space(head); | |
439 | tomoyo_print_number_union_nospace(head, ptr); | |
440 | } | |
441 | ||
9590837b | 442 | /** |
e2bf6907 | 443 | * tomoyo_assign_profile - Create a new profile. |
9590837b | 444 | * |
bd03a3e4 | 445 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
9590837b KT |
446 | * @profile: Profile number to create. |
447 | * | |
448 | * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise. | |
449 | */ | |
bd03a3e4 TH |
450 | static struct tomoyo_profile *tomoyo_assign_profile |
451 | (struct tomoyo_policy_namespace *ns, const unsigned int profile) | |
9590837b | 452 | { |
57c2590f TH |
453 | struct tomoyo_profile *ptr; |
454 | struct tomoyo_profile *entry; | |
9590837b KT |
455 | if (profile >= TOMOYO_MAX_PROFILES) |
456 | return NULL; | |
bd03a3e4 | 457 | ptr = ns->profile_ptr[profile]; |
9590837b | 458 | if (ptr) |
57c2590f TH |
459 | return ptr; |
460 | entry = kzalloc(sizeof(*entry), GFP_NOFS); | |
461 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) | |
462 | goto out; | |
bd03a3e4 | 463 | ptr = ns->profile_ptr[profile]; |
57c2590f TH |
464 | if (!ptr && tomoyo_memory_ok(entry)) { |
465 | ptr = entry; | |
eadd99cc TH |
466 | ptr->default_config = TOMOYO_CONFIG_DISABLED | |
467 | TOMOYO_CONFIG_WANT_GRANT_LOG | | |
468 | TOMOYO_CONFIG_WANT_REJECT_LOG; | |
57c2590f TH |
469 | memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT, |
470 | sizeof(ptr->config)); | |
eadd99cc | 471 | ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024; |
d5ca1725 | 472 | ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048; |
57c2590f | 473 | mb(); /* Avoid out-of-order execution. */ |
bd03a3e4 | 474 | ns->profile_ptr[profile] = ptr; |
57c2590f | 475 | entry = NULL; |
cd7bec6a | 476 | } |
29282381 | 477 | mutex_unlock(&tomoyo_policy_lock); |
57c2590f TH |
478 | out: |
479 | kfree(entry); | |
9590837b KT |
480 | return ptr; |
481 | } | |
482 | ||
483 | /** | |
57c2590f TH |
484 | * tomoyo_profile - Find a profile. |
485 | * | |
bd03a3e4 | 486 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
57c2590f TH |
487 | * @profile: Profile number to find. |
488 | * | |
489 | * Returns pointer to "struct tomoyo_profile". | |
490 | */ | |
bd03a3e4 TH |
491 | struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns, |
492 | const u8 profile) | |
57c2590f | 493 | { |
d5ca1725 | 494 | static struct tomoyo_profile tomoyo_null_profile; |
bd03a3e4 | 495 | struct tomoyo_profile *ptr = ns->profile_ptr[profile]; |
d5ca1725 TH |
496 | if (!ptr) |
497 | ptr = &tomoyo_null_profile; | |
57c2590f TH |
498 | return ptr; |
499 | } | |
500 | ||
d5ca1725 TH |
501 | /** |
502 | * tomoyo_find_yesno - Find values for specified keyword. | |
503 | * | |
504 | * @string: String to check. | |
505 | * @find: Name of keyword. | |
506 | * | |
507 | * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise. | |
508 | */ | |
8e568687 TH |
509 | static s8 tomoyo_find_yesno(const char *string, const char *find) |
510 | { | |
511 | const char *cp = strstr(string, find); | |
512 | if (cp) { | |
513 | cp += strlen(find); | |
514 | if (!strncmp(cp, "=yes", 4)) | |
515 | return 1; | |
516 | else if (!strncmp(cp, "=no", 3)) | |
517 | return 0; | |
518 | } | |
519 | return -1; | |
520 | } | |
521 | ||
d5ca1725 TH |
522 | /** |
523 | * tomoyo_set_uint - Set value for specified preference. | |
524 | * | |
525 | * @i: Pointer to "unsigned int". | |
526 | * @string: String to check. | |
527 | * @find: Name of keyword. | |
528 | * | |
529 | * Returns nothing. | |
530 | */ | |
8e568687 TH |
531 | static void tomoyo_set_uint(unsigned int *i, const char *string, |
532 | const char *find) | |
533 | { | |
534 | const char *cp = strstr(string, find); | |
535 | if (cp) | |
536 | sscanf(cp + strlen(find), "=%u", i); | |
537 | } | |
538 | ||
d5ca1725 TH |
539 | /** |
540 | * tomoyo_set_mode - Set mode for specified profile. | |
541 | * | |
542 | * @name: Name of functionality. | |
543 | * @value: Mode for @name. | |
544 | * @profile: Pointer to "struct tomoyo_profile". | |
545 | * | |
546 | * Returns 0 on success, negative value otherwise. | |
547 | */ | |
8e568687 | 548 | static int tomoyo_set_mode(char *name, const char *value, |
8e568687 TH |
549 | struct tomoyo_profile *profile) |
550 | { | |
551 | u8 i; | |
552 | u8 config; | |
553 | if (!strcmp(name, "CONFIG")) { | |
554 | i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX; | |
555 | config = profile->default_config; | |
556 | } else if (tomoyo_str_starts(&name, "CONFIG::")) { | |
557 | config = 0; | |
558 | for (i = 0; i < TOMOYO_MAX_MAC_INDEX | |
559 | + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) { | |
2c47ab93 TH |
560 | int len = 0; |
561 | if (i < TOMOYO_MAX_MAC_INDEX) { | |
562 | const u8 c = tomoyo_index2category[i]; | |
563 | const char *category = | |
564 | tomoyo_category_keywords[c]; | |
565 | len = strlen(category); | |
566 | if (strncmp(name, category, len) || | |
567 | name[len++] != ':' || name[len++] != ':') | |
568 | continue; | |
569 | } | |
570 | if (strcmp(name + len, tomoyo_mac_keywords[i])) | |
8e568687 TH |
571 | continue; |
572 | config = profile->config[i]; | |
573 | break; | |
574 | } | |
575 | if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX) | |
576 | return -EINVAL; | |
577 | } else { | |
578 | return -EINVAL; | |
579 | } | |
d5ca1725 | 580 | if (strstr(value, "use_default")) { |
8e568687 TH |
581 | config = TOMOYO_CONFIG_USE_DEFAULT; |
582 | } else { | |
583 | u8 mode; | |
584 | for (mode = 0; mode < 4; mode++) | |
585 | if (strstr(value, tomoyo_mode[mode])) | |
586 | /* | |
587 | * Update lower 3 bits in order to distinguish | |
588 | * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'. | |
589 | */ | |
590 | config = (config & ~7) | mode; | |
eadd99cc TH |
591 | if (config != TOMOYO_CONFIG_USE_DEFAULT) { |
592 | switch (tomoyo_find_yesno(value, "grant_log")) { | |
593 | case 1: | |
594 | config |= TOMOYO_CONFIG_WANT_GRANT_LOG; | |
595 | break; | |
596 | case 0: | |
597 | config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG; | |
598 | break; | |
599 | } | |
600 | switch (tomoyo_find_yesno(value, "reject_log")) { | |
601 | case 1: | |
602 | config |= TOMOYO_CONFIG_WANT_REJECT_LOG; | |
603 | break; | |
604 | case 0: | |
605 | config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG; | |
606 | break; | |
607 | } | |
608 | } | |
8e568687 TH |
609 | } |
610 | if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX) | |
611 | profile->config[i] = config; | |
612 | else if (config != TOMOYO_CONFIG_USE_DEFAULT) | |
613 | profile->default_config = config; | |
614 | return 0; | |
615 | } | |
616 | ||
57c2590f TH |
617 | /** |
618 | * tomoyo_write_profile - Write profile table. | |
9590837b KT |
619 | * |
620 | * @head: Pointer to "struct tomoyo_io_buffer". | |
621 | * | |
622 | * Returns 0 on success, negative value otherwise. | |
623 | */ | |
624 | static int tomoyo_write_profile(struct tomoyo_io_buffer *head) | |
625 | { | |
626 | char *data = head->write_buf; | |
627 | unsigned int i; | |
9590837b KT |
628 | char *cp; |
629 | struct tomoyo_profile *profile; | |
bd03a3e4 TH |
630 | if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version) |
631 | == 1) | |
57c2590f TH |
632 | return 0; |
633 | i = simple_strtoul(data, &cp, 10); | |
d5ca1725 TH |
634 | if (*cp != '-') |
635 | return -EINVAL; | |
636 | data = cp + 1; | |
bd03a3e4 | 637 | profile = tomoyo_assign_profile(head->w.ns, i); |
d5ca1725 TH |
638 | if (!profile) |
639 | return -EINVAL; | |
9590837b KT |
640 | cp = strchr(data, '='); |
641 | if (!cp) | |
642 | return -EINVAL; | |
57c2590f | 643 | *cp++ = '\0'; |
9590837b | 644 | if (!strcmp(data, "COMMENT")) { |
2a086e5d TH |
645 | static DEFINE_SPINLOCK(lock); |
646 | const struct tomoyo_path_info *new_comment | |
647 | = tomoyo_get_name(cp); | |
648 | const struct tomoyo_path_info *old_comment; | |
649 | if (!new_comment) | |
650 | return -ENOMEM; | |
651 | spin_lock(&lock); | |
652 | old_comment = profile->comment; | |
653 | profile->comment = new_comment; | |
654 | spin_unlock(&lock); | |
bf24fb01 | 655 | tomoyo_put_name(old_comment); |
9590837b KT |
656 | return 0; |
657 | } | |
d5ca1725 TH |
658 | if (!strcmp(data, "PREFERENCE")) { |
659 | for (i = 0; i < TOMOYO_MAX_PREF; i++) | |
660 | tomoyo_set_uint(&profile->pref[i], cp, | |
661 | tomoyo_pref_keywords[i]); | |
662 | return 0; | |
f23571e8 | 663 | } |
d5ca1725 | 664 | return tomoyo_set_mode(data, cp, profile); |
f23571e8 TH |
665 | } |
666 | ||
eadd99cc TH |
667 | /** |
668 | * tomoyo_print_config - Print mode for specified functionality. | |
669 | * | |
670 | * @head: Pointer to "struct tomoyo_io_buffer". | |
671 | * @config: Mode for that functionality. | |
672 | * | |
673 | * Returns nothing. | |
674 | * | |
675 | * Caller prints functionality's name. | |
676 | */ | |
f23571e8 TH |
677 | static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config) |
678 | { | |
eadd99cc TH |
679 | tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n", |
680 | tomoyo_mode[config & 3], | |
681 | tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG), | |
682 | tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG)); | |
f23571e8 TH |
683 | } |
684 | ||
9590837b | 685 | /** |
57c2590f | 686 | * tomoyo_read_profile - Read profile table. |
9590837b KT |
687 | * |
688 | * @head: Pointer to "struct tomoyo_io_buffer". | |
eadd99cc TH |
689 | * |
690 | * Returns nothing. | |
9590837b | 691 | */ |
8fbe71f0 | 692 | static void tomoyo_read_profile(struct tomoyo_io_buffer *head) |
9590837b | 693 | { |
f23571e8 | 694 | u8 index; |
bd03a3e4 TH |
695 | struct tomoyo_policy_namespace *ns = |
696 | container_of(head->r.ns, typeof(*ns), namespace_list); | |
f23571e8 | 697 | const struct tomoyo_profile *profile; |
bd03a3e4 TH |
698 | if (head->r.eof) |
699 | return; | |
f23571e8 TH |
700 | next: |
701 | index = head->r.index; | |
bd03a3e4 | 702 | profile = ns->profile_ptr[index]; |
f23571e8 TH |
703 | switch (head->r.step) { |
704 | case 0: | |
bd03a3e4 TH |
705 | tomoyo_print_namespace(head); |
706 | tomoyo_io_printf(head, "PROFILE_VERSION=%u\n", | |
707 | ns->profile_version); | |
f23571e8 TH |
708 | head->r.step++; |
709 | break; | |
710 | case 1: | |
711 | for ( ; head->r.index < TOMOYO_MAX_PROFILES; | |
712 | head->r.index++) | |
bd03a3e4 | 713 | if (ns->profile_ptr[head->r.index]) |
f23571e8 TH |
714 | break; |
715 | if (head->r.index == TOMOYO_MAX_PROFILES) | |
716 | return; | |
717 | head->r.step++; | |
718 | break; | |
719 | case 2: | |
720 | { | |
d5ca1725 | 721 | u8 i; |
f23571e8 TH |
722 | const struct tomoyo_path_info *comment = |
723 | profile->comment; | |
bd03a3e4 | 724 | tomoyo_print_namespace(head); |
f23571e8 TH |
725 | tomoyo_io_printf(head, "%u-COMMENT=", index); |
726 | tomoyo_set_string(head, comment ? comment->name : ""); | |
727 | tomoyo_set_lf(head); | |
d5ca1725 TH |
728 | tomoyo_io_printf(head, "%u-PREFERENCE={ ", index); |
729 | for (i = 0; i < TOMOYO_MAX_PREF; i++) | |
730 | tomoyo_io_printf(head, "%s=%u ", | |
731 | tomoyo_pref_keywords[i], | |
732 | profile->pref[i]); | |
733 | tomoyo_set_string(head, "}\n"); | |
f23571e8 TH |
734 | head->r.step++; |
735 | } | |
736 | break; | |
737 | case 3: | |
738 | { | |
bd03a3e4 | 739 | tomoyo_print_namespace(head); |
f23571e8 TH |
740 | tomoyo_io_printf(head, "%u-%s", index, "CONFIG"); |
741 | tomoyo_print_config(head, profile->default_config); | |
742 | head->r.bit = 0; | |
743 | head->r.step++; | |
744 | } | |
745 | break; | |
746 | case 4: | |
747 | for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX | |
748 | + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) { | |
749 | const u8 i = head->r.bit; | |
750 | const u8 config = profile->config[i]; | |
57c2590f TH |
751 | if (config == TOMOYO_CONFIG_USE_DEFAULT) |
752 | continue; | |
bd03a3e4 | 753 | tomoyo_print_namespace(head); |
2c47ab93 TH |
754 | if (i < TOMOYO_MAX_MAC_INDEX) |
755 | tomoyo_io_printf(head, "%u-CONFIG::%s::%s", | |
756 | index, | |
757 | tomoyo_category_keywords | |
758 | [tomoyo_index2category[i]], | |
759 | tomoyo_mac_keywords[i]); | |
760 | else | |
761 | tomoyo_io_printf(head, "%u-CONFIG::%s", index, | |
762 | tomoyo_mac_keywords[i]); | |
f23571e8 TH |
763 | tomoyo_print_config(head, config); |
764 | head->r.bit++; | |
765 | break; | |
766 | } | |
767 | if (head->r.bit == TOMOYO_MAX_MAC_INDEX | |
768 | + TOMOYO_MAX_MAC_CATEGORY_INDEX) { | |
f23571e8 TH |
769 | head->r.index++; |
770 | head->r.step = 1; | |
9590837b | 771 | } |
57c2590f | 772 | break; |
9590837b | 773 | } |
f23571e8 TH |
774 | if (tomoyo_flush(head)) |
775 | goto next; | |
9590837b KT |
776 | } |
777 | ||
e2bf6907 TH |
778 | static bool tomoyo_same_manager(const struct tomoyo_acl_head *a, |
779 | const struct tomoyo_acl_head *b) | |
36f5e1ff | 780 | { |
e2bf6907 TH |
781 | return container_of(a, struct tomoyo_manager, head)->manager == |
782 | container_of(b, struct tomoyo_manager, head)->manager; | |
36f5e1ff TH |
783 | } |
784 | ||
9590837b KT |
785 | /** |
786 | * tomoyo_update_manager_entry - Add a manager entry. | |
787 | * | |
788 | * @manager: The path to manager or the domainnamme. | |
789 | * @is_delete: True if it is a delete request. | |
790 | * | |
791 | * Returns 0 on success, negative value otherwise. | |
fdb8ebb7 TH |
792 | * |
793 | * Caller holds tomoyo_read_lock(). | |
9590837b KT |
794 | */ |
795 | static int tomoyo_update_manager_entry(const char *manager, | |
796 | const bool is_delete) | |
797 | { | |
e2bf6907 | 798 | struct tomoyo_manager e = { }; |
a238cf5b | 799 | struct tomoyo_acl_param param = { |
bd03a3e4 | 800 | /* .ns = &tomoyo_kernel_namespace, */ |
a238cf5b | 801 | .is_delete = is_delete, |
bd03a3e4 TH |
802 | .list = &tomoyo_kernel_namespace. |
803 | policy_list[TOMOYO_ID_MANAGER], | |
a238cf5b TH |
804 | }; |
805 | int error = is_delete ? -ENOENT : -ENOMEM; | |
75093152 TH |
806 | if (tomoyo_domain_def(manager)) { |
807 | if (!tomoyo_correct_domain(manager)) | |
9590837b | 808 | return -EINVAL; |
9e4b50e9 | 809 | e.is_domain = true; |
9590837b | 810 | } else { |
75093152 | 811 | if (!tomoyo_correct_path(manager)) |
9590837b KT |
812 | return -EINVAL; |
813 | } | |
9e4b50e9 | 814 | e.manager = tomoyo_get_name(manager); |
a238cf5b TH |
815 | if (e.manager) { |
816 | error = tomoyo_update_policy(&e.head, sizeof(e), ¶m, | |
817 | tomoyo_same_manager); | |
818 | tomoyo_put_name(e.manager); | |
819 | } | |
9590837b KT |
820 | return error; |
821 | } | |
822 | ||
823 | /** | |
e2bf6907 | 824 | * tomoyo_write_manager - Write manager policy. |
9590837b KT |
825 | * |
826 | * @head: Pointer to "struct tomoyo_io_buffer". | |
827 | * | |
828 | * Returns 0 on success, negative value otherwise. | |
fdb8ebb7 TH |
829 | * |
830 | * Caller holds tomoyo_read_lock(). | |
9590837b | 831 | */ |
e2bf6907 | 832 | static int tomoyo_write_manager(struct tomoyo_io_buffer *head) |
9590837b KT |
833 | { |
834 | char *data = head->write_buf; | |
9590837b KT |
835 | |
836 | if (!strcmp(data, "manage_by_non_root")) { | |
bd03a3e4 | 837 | tomoyo_manage_by_non_root = !head->w.is_delete; |
9590837b KT |
838 | return 0; |
839 | } | |
bd03a3e4 | 840 | return tomoyo_update_manager_entry(data, head->w.is_delete); |
9590837b KT |
841 | } |
842 | ||
843 | /** | |
e2bf6907 | 844 | * tomoyo_read_manager - Read manager policy. |
9590837b KT |
845 | * |
846 | * @head: Pointer to "struct tomoyo_io_buffer". | |
847 | * | |
fdb8ebb7 | 848 | * Caller holds tomoyo_read_lock(). |
9590837b | 849 | */ |
e2bf6907 | 850 | static void tomoyo_read_manager(struct tomoyo_io_buffer *head) |
9590837b | 851 | { |
f23571e8 | 852 | if (head->r.eof) |
8fbe71f0 | 853 | return; |
bd03a3e4 TH |
854 | list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace. |
855 | policy_list[TOMOYO_ID_MANAGER]) { | |
e2bf6907 | 856 | struct tomoyo_manager *ptr = |
f23571e8 | 857 | list_entry(head->r.acl, typeof(*ptr), head.list); |
82e0f001 | 858 | if (ptr->head.is_deleted) |
9590837b | 859 | continue; |
f23571e8 TH |
860 | if (!tomoyo_flush(head)) |
861 | return; | |
862 | tomoyo_set_string(head, ptr->manager->name); | |
863 | tomoyo_set_lf(head); | |
9590837b | 864 | } |
f23571e8 | 865 | head->r.eof = true; |
9590837b KT |
866 | } |
867 | ||
868 | /** | |
e2bf6907 | 869 | * tomoyo_manager - Check whether the current process is a policy manager. |
9590837b KT |
870 | * |
871 | * Returns true if the current process is permitted to modify policy | |
872 | * via /sys/kernel/security/tomoyo/ interface. | |
fdb8ebb7 TH |
873 | * |
874 | * Caller holds tomoyo_read_lock(). | |
9590837b | 875 | */ |
e2bf6907 | 876 | static bool tomoyo_manager(void) |
9590837b | 877 | { |
e2bf6907 | 878 | struct tomoyo_manager *ptr; |
9590837b KT |
879 | const char *exe; |
880 | const struct task_struct *task = current; | |
881 | const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname; | |
882 | bool found = false; | |
883 | ||
884 | if (!tomoyo_policy_loaded) | |
885 | return true; | |
886 | if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid)) | |
887 | return false; | |
bd03a3e4 TH |
888 | list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace. |
889 | policy_list[TOMOYO_ID_MANAGER], head.list) { | |
82e0f001 | 890 | if (!ptr->head.is_deleted && ptr->is_domain |
9590837b KT |
891 | && !tomoyo_pathcmp(domainname, ptr->manager)) { |
892 | found = true; | |
893 | break; | |
894 | } | |
895 | } | |
9590837b KT |
896 | if (found) |
897 | return true; | |
898 | exe = tomoyo_get_exe(); | |
899 | if (!exe) | |
900 | return false; | |
bd03a3e4 TH |
901 | list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace. |
902 | policy_list[TOMOYO_ID_MANAGER], head.list) { | |
82e0f001 | 903 | if (!ptr->head.is_deleted && !ptr->is_domain |
9590837b KT |
904 | && !strcmp(exe, ptr->manager->name)) { |
905 | found = true; | |
906 | break; | |
907 | } | |
908 | } | |
9590837b KT |
909 | if (!found) { /* Reduce error messages. */ |
910 | static pid_t last_pid; | |
911 | const pid_t pid = current->pid; | |
912 | if (last_pid != pid) { | |
913 | printk(KERN_WARNING "%s ( %s ) is not permitted to " | |
914 | "update policies.\n", domainname->name, exe); | |
915 | last_pid = pid; | |
916 | } | |
917 | } | |
8e2d39a1 | 918 | kfree(exe); |
9590837b KT |
919 | return found; |
920 | } | |
921 | ||
922 | /** | |
bd03a3e4 | 923 | * tomoyo_select_domain - Parse select command. |
9590837b KT |
924 | * |
925 | * @head: Pointer to "struct tomoyo_io_buffer". | |
926 | * @data: String to parse. | |
927 | * | |
928 | * Returns true on success, false otherwise. | |
fdb8ebb7 TH |
929 | * |
930 | * Caller holds tomoyo_read_lock(). | |
9590837b | 931 | */ |
bd03a3e4 TH |
932 | static bool tomoyo_select_domain(struct tomoyo_io_buffer *head, |
933 | const char *data) | |
9590837b KT |
934 | { |
935 | unsigned int pid; | |
936 | struct tomoyo_domain_info *domain = NULL; | |
9b244373 | 937 | bool global_pid = false; |
bd03a3e4 TH |
938 | if (strncmp(data, "select ", 7)) |
939 | return false; | |
940 | data += 7; | |
9b244373 TH |
941 | if (sscanf(data, "pid=%u", &pid) == 1 || |
942 | (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { | |
9590837b | 943 | struct task_struct *p; |
1fcdc7c5 | 944 | rcu_read_lock(); |
9590837b | 945 | read_lock(&tasklist_lock); |
9b244373 TH |
946 | if (global_pid) |
947 | p = find_task_by_pid_ns(pid, &init_pid_ns); | |
948 | else | |
949 | p = find_task_by_vpid(pid); | |
9590837b KT |
950 | if (p) |
951 | domain = tomoyo_real_domain(p); | |
952 | read_unlock(&tasklist_lock); | |
1fcdc7c5 | 953 | rcu_read_unlock(); |
9590837b | 954 | } else if (!strncmp(data, "domain=", 7)) { |
75093152 | 955 | if (tomoyo_domain_def(data + 7)) |
9590837b | 956 | domain = tomoyo_find_domain(data + 7); |
9590837b KT |
957 | } else |
958 | return false; | |
0df7e8b8 | 959 | head->w.domain = domain; |
9590837b KT |
960 | /* Accessing read_buf is safe because head->io_sem is held. */ |
961 | if (!head->read_buf) | |
962 | return true; /* Do nothing if open(O_WRONLY). */ | |
f23571e8 TH |
963 | memset(&head->r, 0, sizeof(head->r)); |
964 | head->r.print_this_domain_only = true; | |
68eda8f5 DC |
965 | if (domain) |
966 | head->r.domain = &domain->list; | |
967 | else | |
968 | head->r.eof = 1; | |
9590837b | 969 | tomoyo_io_printf(head, "# select %s\n", data); |
475e6fa3 TH |
970 | if (domain && domain->is_deleted) |
971 | tomoyo_io_printf(head, "# This is a deleted domain.\n"); | |
9590837b KT |
972 | return true; |
973 | } | |
974 | ||
ccf135f5 TH |
975 | /** |
976 | * tomoyo_delete_domain - Delete a domain. | |
977 | * | |
978 | * @domainname: The name of domain. | |
979 | * | |
980 | * Returns 0. | |
fdb8ebb7 TH |
981 | * |
982 | * Caller holds tomoyo_read_lock(). | |
ccf135f5 TH |
983 | */ |
984 | static int tomoyo_delete_domain(char *domainname) | |
985 | { | |
986 | struct tomoyo_domain_info *domain; | |
987 | struct tomoyo_path_info name; | |
988 | ||
989 | name.name = domainname; | |
990 | tomoyo_fill_path_info(&name); | |
29282381 TH |
991 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
992 | return 0; | |
ccf135f5 | 993 | /* Is there an active domain? */ |
fdb8ebb7 | 994 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
ccf135f5 TH |
995 | /* Never delete tomoyo_kernel_domain */ |
996 | if (domain == &tomoyo_kernel_domain) | |
997 | continue; | |
998 | if (domain->is_deleted || | |
999 | tomoyo_pathcmp(domain->domainname, &name)) | |
1000 | continue; | |
1001 | domain->is_deleted = true; | |
1002 | break; | |
1003 | } | |
f737d95d | 1004 | mutex_unlock(&tomoyo_policy_lock); |
ccf135f5 TH |
1005 | return 0; |
1006 | } | |
1007 | ||
17fcfbd9 | 1008 | /** |
e2bf6907 | 1009 | * tomoyo_write_domain2 - Write domain policy. |
17fcfbd9 | 1010 | * |
bd03a3e4 | 1011 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
a238cf5b TH |
1012 | * @list: Pointer to "struct list_head". |
1013 | * @data: Policy to be interpreted. | |
1014 | * @is_delete: True if it is a delete request. | |
17fcfbd9 TH |
1015 | * |
1016 | * Returns 0 on success, negative value otherwise. | |
1017 | * | |
1018 | * Caller holds tomoyo_read_lock(). | |
1019 | */ | |
bd03a3e4 TH |
1020 | static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns, |
1021 | struct list_head *list, char *data, | |
e2bf6907 | 1022 | const bool is_delete) |
17fcfbd9 | 1023 | { |
a238cf5b | 1024 | struct tomoyo_acl_param param = { |
bd03a3e4 | 1025 | .ns = ns, |
a238cf5b TH |
1026 | .list = list, |
1027 | .data = data, | |
1028 | .is_delete = is_delete, | |
1029 | }; | |
1030 | static const struct { | |
1031 | const char *keyword; | |
1032 | int (*write) (struct tomoyo_acl_param *); | |
1033 | } tomoyo_callback[1] = { | |
1034 | { "file ", tomoyo_write_file }, | |
1035 | }; | |
1036 | u8 i; | |
1037 | for (i = 0; i < 1; i++) { | |
1038 | if (!tomoyo_str_starts(¶m.data, | |
1039 | tomoyo_callback[i].keyword)) | |
1040 | continue; | |
1041 | return tomoyo_callback[i].write(¶m); | |
1042 | } | |
1043 | return -EINVAL; | |
17fcfbd9 TH |
1044 | } |
1045 | ||
2c47ab93 TH |
1046 | /* String table for domain flags. */ |
1047 | const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = { | |
1048 | [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n", | |
1049 | [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n", | |
1050 | }; | |
1051 | ||
9590837b | 1052 | /** |
e2bf6907 | 1053 | * tomoyo_write_domain - Write domain policy. |
9590837b KT |
1054 | * |
1055 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1056 | * | |
1057 | * Returns 0 on success, negative value otherwise. | |
fdb8ebb7 TH |
1058 | * |
1059 | * Caller holds tomoyo_read_lock(). | |
9590837b | 1060 | */ |
e2bf6907 | 1061 | static int tomoyo_write_domain(struct tomoyo_io_buffer *head) |
9590837b KT |
1062 | { |
1063 | char *data = head->write_buf; | |
bd03a3e4 | 1064 | struct tomoyo_policy_namespace *ns; |
0df7e8b8 | 1065 | struct tomoyo_domain_info *domain = head->w.domain; |
bd03a3e4 TH |
1066 | const bool is_delete = head->w.is_delete; |
1067 | bool is_select = !is_delete && tomoyo_str_starts(&data, "select "); | |
9590837b | 1068 | unsigned int profile; |
bd03a3e4 | 1069 | if (*data == '<') { |
9590837b KT |
1070 | domain = NULL; |
1071 | if (is_delete) | |
1072 | tomoyo_delete_domain(data); | |
fdb8ebb7 | 1073 | else if (is_select) |
9590837b | 1074 | domain = tomoyo_find_domain(data); |
fdb8ebb7 | 1075 | else |
bd03a3e4 | 1076 | domain = tomoyo_assign_domain(data, false); |
0df7e8b8 | 1077 | head->w.domain = domain; |
9590837b KT |
1078 | return 0; |
1079 | } | |
1080 | if (!domain) | |
1081 | return -EINVAL; | |
bd03a3e4 | 1082 | ns = domain->ns; |
b5bc60b4 | 1083 | if (sscanf(data, "use_profile %u", &profile) == 1 |
9590837b | 1084 | && profile < TOMOYO_MAX_PROFILES) { |
bd03a3e4 | 1085 | if (!tomoyo_policy_loaded || ns->profile_ptr[profile]) |
9590837b KT |
1086 | domain->profile = (u8) profile; |
1087 | return 0; | |
1088 | } | |
32997144 TH |
1089 | if (sscanf(data, "use_group %u\n", &profile) == 1 |
1090 | && profile < TOMOYO_MAX_ACL_GROUPS) { | |
1091 | if (!is_delete) | |
1092 | domain->group = (u8) profile; | |
1093 | return 0; | |
1094 | } | |
2c47ab93 TH |
1095 | for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) { |
1096 | const char *cp = tomoyo_dif[profile]; | |
1097 | if (strncmp(data, cp, strlen(cp) - 1)) | |
1098 | continue; | |
1099 | domain->flags[profile] = !is_delete; | |
9b244373 TH |
1100 | return 0; |
1101 | } | |
bd03a3e4 TH |
1102 | return tomoyo_write_domain2(ns, &domain->acl_info_list, data, |
1103 | is_delete); | |
9590837b KT |
1104 | } |
1105 | ||
2066a361 TH |
1106 | /** |
1107 | * tomoyo_print_condition - Print condition part. | |
1108 | * | |
1109 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1110 | * @cond: Pointer to "struct tomoyo_condition". | |
1111 | * | |
1112 | * Returns true on success, false otherwise. | |
1113 | */ | |
1114 | static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, | |
1115 | const struct tomoyo_condition *cond) | |
1116 | { | |
1117 | switch (head->r.cond_step) { | |
1118 | case 0: | |
1119 | head->r.cond_index = 0; | |
1120 | head->r.cond_step++; | |
1121 | /* fall through */ | |
1122 | case 1: | |
1123 | { | |
1124 | const u16 condc = cond->condc; | |
1125 | const struct tomoyo_condition_element *condp = | |
1126 | (typeof(condp)) (cond + 1); | |
1127 | const struct tomoyo_number_union *numbers_p = | |
1128 | (typeof(numbers_p)) (condp + condc); | |
2ca9bf45 TH |
1129 | const struct tomoyo_name_union *names_p = |
1130 | (typeof(names_p)) | |
1131 | (numbers_p + cond->numbers_count); | |
5b636857 TH |
1132 | const struct tomoyo_argv *argv = |
1133 | (typeof(argv)) (names_p + cond->names_count); | |
1134 | const struct tomoyo_envp *envp = | |
1135 | (typeof(envp)) (argv + cond->argc); | |
2066a361 TH |
1136 | u16 skip; |
1137 | for (skip = 0; skip < head->r.cond_index; skip++) { | |
1138 | const u8 left = condp->left; | |
1139 | const u8 right = condp->right; | |
1140 | condp++; | |
1141 | switch (left) { | |
5b636857 TH |
1142 | case TOMOYO_ARGV_ENTRY: |
1143 | argv++; | |
1144 | continue; | |
1145 | case TOMOYO_ENVP_ENTRY: | |
1146 | envp++; | |
1147 | continue; | |
2066a361 TH |
1148 | case TOMOYO_NUMBER_UNION: |
1149 | numbers_p++; | |
1150 | break; | |
1151 | } | |
1152 | switch (right) { | |
2ca9bf45 TH |
1153 | case TOMOYO_NAME_UNION: |
1154 | names_p++; | |
1155 | break; | |
2066a361 TH |
1156 | case TOMOYO_NUMBER_UNION: |
1157 | numbers_p++; | |
1158 | break; | |
1159 | } | |
1160 | } | |
1161 | while (head->r.cond_index < condc) { | |
1162 | const u8 match = condp->equals; | |
1163 | const u8 left = condp->left; | |
1164 | const u8 right = condp->right; | |
1165 | if (!tomoyo_flush(head)) | |
1166 | return false; | |
1167 | condp++; | |
1168 | head->r.cond_index++; | |
1169 | tomoyo_set_space(head); | |
1170 | switch (left) { | |
5b636857 TH |
1171 | case TOMOYO_ARGV_ENTRY: |
1172 | tomoyo_io_printf(head, | |
1173 | "exec.argv[%lu]%s=\"", | |
1174 | argv->index, argv-> | |
1175 | is_not ? "!" : ""); | |
1176 | tomoyo_set_string(head, | |
1177 | argv->value->name); | |
1178 | tomoyo_set_string(head, "\""); | |
1179 | argv++; | |
1180 | continue; | |
1181 | case TOMOYO_ENVP_ENTRY: | |
1182 | tomoyo_set_string(head, | |
1183 | "exec.envp[\""); | |
1184 | tomoyo_set_string(head, | |
1185 | envp->name->name); | |
1186 | tomoyo_io_printf(head, "\"]%s=", envp-> | |
1187 | is_not ? "!" : ""); | |
1188 | if (envp->value) { | |
1189 | tomoyo_set_string(head, "\""); | |
1190 | tomoyo_set_string(head, envp-> | |
1191 | value->name); | |
1192 | tomoyo_set_string(head, "\""); | |
1193 | } else { | |
1194 | tomoyo_set_string(head, | |
1195 | "NULL"); | |
1196 | } | |
1197 | envp++; | |
1198 | continue; | |
2066a361 TH |
1199 | case TOMOYO_NUMBER_UNION: |
1200 | tomoyo_print_number_union_nospace | |
1201 | (head, numbers_p++); | |
1202 | break; | |
1203 | default: | |
1204 | tomoyo_set_string(head, | |
1205 | tomoyo_condition_keyword[left]); | |
1206 | break; | |
1207 | } | |
1208 | tomoyo_set_string(head, match ? "=" : "!="); | |
1209 | switch (right) { | |
2ca9bf45 TH |
1210 | case TOMOYO_NAME_UNION: |
1211 | tomoyo_print_name_union_quoted | |
1212 | (head, names_p++); | |
1213 | break; | |
2066a361 TH |
1214 | case TOMOYO_NUMBER_UNION: |
1215 | tomoyo_print_number_union_nospace | |
1216 | (head, numbers_p++); | |
1217 | break; | |
1218 | default: | |
1219 | tomoyo_set_string(head, | |
1220 | tomoyo_condition_keyword[right]); | |
1221 | break; | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | head->r.cond_step++; | |
1226 | /* fall through */ | |
1227 | case 2: | |
1228 | if (!tomoyo_flush(head)) | |
1229 | break; | |
1230 | head->r.cond_step++; | |
1231 | /* fall through */ | |
1232 | case 3: | |
1233 | tomoyo_set_lf(head); | |
1234 | return true; | |
1235 | } | |
1236 | return false; | |
1237 | } | |
1238 | ||
9590837b | 1239 | /** |
32997144 | 1240 | * tomoyo_set_group - Print "acl_group " header keyword and category name. |
9590837b | 1241 | * |
0d2171d7 TH |
1242 | * @head: Pointer to "struct tomoyo_io_buffer". |
1243 | * @category: Category name. | |
9590837b | 1244 | * |
0d2171d7 | 1245 | * Returns nothing. |
9590837b | 1246 | */ |
0d2171d7 TH |
1247 | static void tomoyo_set_group(struct tomoyo_io_buffer *head, |
1248 | const char *category) | |
9590837b | 1249 | { |
bd03a3e4 TH |
1250 | if (head->type == TOMOYO_EXCEPTIONPOLICY) { |
1251 | tomoyo_print_namespace(head); | |
32997144 TH |
1252 | tomoyo_io_printf(head, "acl_group %u ", |
1253 | head->r.acl_group_index); | |
bd03a3e4 | 1254 | } |
0d2171d7 | 1255 | tomoyo_set_string(head, category); |
9590837b KT |
1256 | } |
1257 | ||
1258 | /** | |
5db5a39b | 1259 | * tomoyo_print_entry - Print an ACL entry. |
9590837b KT |
1260 | * |
1261 | * @head: Pointer to "struct tomoyo_io_buffer". | |
5db5a39b | 1262 | * @acl: Pointer to an ACL entry. |
9590837b KT |
1263 | * |
1264 | * Returns true on success, false otherwise. | |
1265 | */ | |
5db5a39b TH |
1266 | static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, |
1267 | struct tomoyo_acl_info *acl) | |
9590837b | 1268 | { |
5db5a39b | 1269 | const u8 acl_type = acl->type; |
0d2171d7 | 1270 | bool first = true; |
f23571e8 | 1271 | u8 bit; |
9590837b | 1272 | |
2066a361 TH |
1273 | if (head->r.print_cond_part) |
1274 | goto print_cond_part; | |
5db5a39b TH |
1275 | if (acl->is_deleted) |
1276 | return true; | |
f23571e8 TH |
1277 | if (!tomoyo_flush(head)) |
1278 | return false; | |
1279 | else if (acl_type == TOMOYO_TYPE_PATH_ACL) { | |
5db5a39b TH |
1280 | struct tomoyo_path_acl *ptr = |
1281 | container_of(acl, typeof(*ptr), head); | |
1282 | const u16 perm = ptr->perm; | |
0d2171d7 | 1283 | for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) { |
5db5a39b TH |
1284 | if (!(perm & (1 << bit))) |
1285 | continue; | |
bd03a3e4 | 1286 | if (head->r.print_transition_related_only && |
5db5a39b TH |
1287 | bit != TOMOYO_TYPE_EXECUTE) |
1288 | continue; | |
0d2171d7 TH |
1289 | if (first) { |
1290 | tomoyo_set_group(head, "file "); | |
1291 | first = false; | |
1292 | } else { | |
1293 | tomoyo_set_slash(head); | |
1294 | } | |
1295 | tomoyo_set_string(head, tomoyo_path_keyword[bit]); | |
5db5a39b | 1296 | } |
0d2171d7 TH |
1297 | if (first) |
1298 | return true; | |
f23571e8 | 1299 | tomoyo_print_name_union(head, &ptr->name); |
bd03a3e4 | 1300 | } else if (head->r.print_transition_related_only) { |
5db5a39b TH |
1301 | return true; |
1302 | } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) { | |
1303 | struct tomoyo_path2_acl *ptr = | |
1304 | container_of(acl, typeof(*ptr), head); | |
0d2171d7 TH |
1305 | const u8 perm = ptr->perm; |
1306 | for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) { | |
1307 | if (!(perm & (1 << bit))) | |
1308 | continue; | |
1309 | if (first) { | |
1310 | tomoyo_set_group(head, "file "); | |
1311 | first = false; | |
1312 | } else { | |
1313 | tomoyo_set_slash(head); | |
1314 | } | |
1315 | tomoyo_set_string(head, tomoyo_mac_keywords | |
1316 | [tomoyo_pp2mac[bit]]); | |
1317 | } | |
1318 | if (first) | |
1319 | return true; | |
f23571e8 TH |
1320 | tomoyo_print_name_union(head, &ptr->name1); |
1321 | tomoyo_print_name_union(head, &ptr->name2); | |
5db5a39b TH |
1322 | } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) { |
1323 | struct tomoyo_path_number_acl *ptr = | |
1324 | container_of(acl, typeof(*ptr), head); | |
0d2171d7 TH |
1325 | const u8 perm = ptr->perm; |
1326 | for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) { | |
1327 | if (!(perm & (1 << bit))) | |
1328 | continue; | |
1329 | if (first) { | |
1330 | tomoyo_set_group(head, "file "); | |
1331 | first = false; | |
1332 | } else { | |
1333 | tomoyo_set_slash(head); | |
1334 | } | |
1335 | tomoyo_set_string(head, tomoyo_mac_keywords | |
1336 | [tomoyo_pn2mac[bit]]); | |
1337 | } | |
1338 | if (first) | |
1339 | return true; | |
f23571e8 TH |
1340 | tomoyo_print_name_union(head, &ptr->name); |
1341 | tomoyo_print_number_union(head, &ptr->number); | |
5db5a39b TH |
1342 | } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) { |
1343 | struct tomoyo_mkdev_acl *ptr = | |
1344 | container_of(acl, typeof(*ptr), head); | |
0d2171d7 TH |
1345 | const u8 perm = ptr->perm; |
1346 | for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) { | |
1347 | if (!(perm & (1 << bit))) | |
1348 | continue; | |
1349 | if (first) { | |
1350 | tomoyo_set_group(head, "file "); | |
1351 | first = false; | |
1352 | } else { | |
1353 | tomoyo_set_slash(head); | |
1354 | } | |
1355 | tomoyo_set_string(head, tomoyo_mac_keywords | |
1356 | [tomoyo_pnnn2mac[bit]]); | |
1357 | } | |
1358 | if (first) | |
1359 | return true; | |
f23571e8 TH |
1360 | tomoyo_print_name_union(head, &ptr->name); |
1361 | tomoyo_print_number_union(head, &ptr->mode); | |
1362 | tomoyo_print_number_union(head, &ptr->major); | |
1363 | tomoyo_print_number_union(head, &ptr->minor); | |
5db5a39b TH |
1364 | } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) { |
1365 | struct tomoyo_mount_acl *ptr = | |
1366 | container_of(acl, typeof(*ptr), head); | |
0d2171d7 | 1367 | tomoyo_set_group(head, "file mount"); |
f23571e8 TH |
1368 | tomoyo_print_name_union(head, &ptr->dev_name); |
1369 | tomoyo_print_name_union(head, &ptr->dir_name); | |
1370 | tomoyo_print_name_union(head, &ptr->fs_type); | |
1371 | tomoyo_print_number_union(head, &ptr->flags); | |
a1f9bb6a | 1372 | } |
2066a361 TH |
1373 | if (acl->cond) { |
1374 | head->r.print_cond_part = true; | |
1375 | head->r.cond_step = 0; | |
1376 | if (!tomoyo_flush(head)) | |
1377 | return false; | |
1378 | print_cond_part: | |
1379 | if (!tomoyo_print_condition(head, acl->cond)) | |
1380 | return false; | |
1381 | head->r.print_cond_part = false; | |
1382 | } else { | |
1383 | tomoyo_set_lf(head); | |
1384 | } | |
f23571e8 TH |
1385 | return true; |
1386 | } | |
1387 | ||
1388 | /** | |
1389 | * tomoyo_read_domain2 - Read domain policy. | |
1390 | * | |
32997144 TH |
1391 | * @head: Pointer to "struct tomoyo_io_buffer". |
1392 | * @list: Pointer to "struct list_head". | |
f23571e8 TH |
1393 | * |
1394 | * Caller holds tomoyo_read_lock(). | |
1395 | * | |
1396 | * Returns true on success, false otherwise. | |
1397 | */ | |
1398 | static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head, | |
32997144 | 1399 | struct list_head *list) |
f23571e8 | 1400 | { |
32997144 | 1401 | list_for_each_cookie(head->r.acl, list) { |
f23571e8 TH |
1402 | struct tomoyo_acl_info *ptr = |
1403 | list_entry(head->r.acl, typeof(*ptr), list); | |
1404 | if (!tomoyo_print_entry(head, ptr)) | |
1405 | return false; | |
1406 | } | |
1407 | head->r.acl = NULL; | |
a1f9bb6a | 1408 | return true; |
a1f9bb6a TH |
1409 | } |
1410 | ||
9590837b | 1411 | /** |
e2bf6907 | 1412 | * tomoyo_read_domain - Read domain policy. |
9590837b KT |
1413 | * |
1414 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1415 | * | |
fdb8ebb7 | 1416 | * Caller holds tomoyo_read_lock(). |
9590837b | 1417 | */ |
e2bf6907 | 1418 | static void tomoyo_read_domain(struct tomoyo_io_buffer *head) |
9590837b | 1419 | { |
f23571e8 | 1420 | if (head->r.eof) |
8fbe71f0 | 1421 | return; |
f23571e8 | 1422 | list_for_each_cookie(head->r.domain, &tomoyo_domain_list) { |
475e6fa3 | 1423 | struct tomoyo_domain_info *domain = |
f23571e8 TH |
1424 | list_entry(head->r.domain, typeof(*domain), list); |
1425 | switch (head->r.step) { | |
2c47ab93 | 1426 | u8 i; |
f23571e8 TH |
1427 | case 0: |
1428 | if (domain->is_deleted && | |
1429 | !head->r.print_this_domain_only) | |
1430 | continue; | |
1431 | /* Print domainname and flags. */ | |
1432 | tomoyo_set_string(head, domain->domainname->name); | |
1433 | tomoyo_set_lf(head); | |
b5bc60b4 | 1434 | tomoyo_io_printf(head, "use_profile %u\n", |
f23571e8 | 1435 | domain->profile); |
32997144 TH |
1436 | tomoyo_io_printf(head, "use_group %u\n", |
1437 | domain->group); | |
2c47ab93 TH |
1438 | for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++) |
1439 | if (domain->flags[i]) | |
1440 | tomoyo_set_string(head, tomoyo_dif[i]); | |
f23571e8 TH |
1441 | head->r.step++; |
1442 | tomoyo_set_lf(head); | |
1443 | /* fall through */ | |
1444 | case 1: | |
32997144 | 1445 | if (!tomoyo_read_domain2(head, &domain->acl_info_list)) |
f23571e8 TH |
1446 | return; |
1447 | head->r.step++; | |
1448 | if (!tomoyo_set_lf(head)) | |
1449 | return; | |
1450 | /* fall through */ | |
1451 | case 2: | |
1452 | head->r.step = 0; | |
1453 | if (head->r.print_this_domain_only) | |
1454 | goto done; | |
9590837b | 1455 | } |
9590837b | 1456 | } |
f23571e8 TH |
1457 | done: |
1458 | head->r.eof = true; | |
9590837b KT |
1459 | } |
1460 | ||
9590837b KT |
1461 | /** |
1462 | * tomoyo_write_pid: Specify PID to obtain domainname. | |
1463 | * | |
1464 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1465 | * | |
1466 | * Returns 0. | |
1467 | */ | |
1468 | static int tomoyo_write_pid(struct tomoyo_io_buffer *head) | |
1469 | { | |
f23571e8 | 1470 | head->r.eof = false; |
9590837b KT |
1471 | return 0; |
1472 | } | |
1473 | ||
1474 | /** | |
1475 | * tomoyo_read_pid - Get domainname of the specified PID. | |
1476 | * | |
1477 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1478 | * | |
1479 | * Returns the domainname which the specified PID is in on success, | |
1480 | * empty string otherwise. | |
1481 | * The PID is specified by tomoyo_write_pid() so that the user can obtain | |
1482 | * using read()/write() interface rather than sysctl() interface. | |
1483 | */ | |
8fbe71f0 | 1484 | static void tomoyo_read_pid(struct tomoyo_io_buffer *head) |
9590837b | 1485 | { |
f23571e8 TH |
1486 | char *buf = head->write_buf; |
1487 | bool global_pid = false; | |
1488 | unsigned int pid; | |
1489 | struct task_struct *p; | |
1490 | struct tomoyo_domain_info *domain = NULL; | |
1491 | ||
1492 | /* Accessing write_buf is safe because head->io_sem is held. */ | |
1493 | if (!buf) { | |
1494 | head->r.eof = true; | |
1495 | return; /* Do nothing if open(O_RDONLY). */ | |
9590837b | 1496 | } |
f23571e8 TH |
1497 | if (head->r.w_pos || head->r.eof) |
1498 | return; | |
1499 | head->r.eof = true; | |
1500 | if (tomoyo_str_starts(&buf, "global-pid ")) | |
1501 | global_pid = true; | |
1502 | pid = (unsigned int) simple_strtoul(buf, NULL, 10); | |
1503 | rcu_read_lock(); | |
1504 | read_lock(&tasklist_lock); | |
1505 | if (global_pid) | |
1506 | p = find_task_by_pid_ns(pid, &init_pid_ns); | |
1507 | else | |
1508 | p = find_task_by_vpid(pid); | |
1509 | if (p) | |
1510 | domain = tomoyo_real_domain(p); | |
1511 | read_unlock(&tasklist_lock); | |
1512 | rcu_read_unlock(); | |
1513 | if (!domain) | |
1514 | return; | |
1515 | tomoyo_io_printf(head, "%u %u ", pid, domain->profile); | |
1516 | tomoyo_set_string(head, domain->domainname->name); | |
9590837b KT |
1517 | } |
1518 | ||
5448ec4f | 1519 | static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = { |
bd03a3e4 TH |
1520 | [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ", |
1521 | [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ", | |
1522 | [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ", | |
1523 | [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ", | |
1524 | [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ", | |
1525 | [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ", | |
5448ec4f TH |
1526 | }; |
1527 | ||
e2bf6907 | 1528 | static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = { |
b5bc60b4 TH |
1529 | [TOMOYO_PATH_GROUP] = "path_group ", |
1530 | [TOMOYO_NUMBER_GROUP] = "number_group ", | |
e2bf6907 TH |
1531 | }; |
1532 | ||
9590837b | 1533 | /** |
e2bf6907 | 1534 | * tomoyo_write_exception - Write exception policy. |
9590837b KT |
1535 | * |
1536 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1537 | * | |
1538 | * Returns 0 on success, negative value otherwise. | |
fdb8ebb7 TH |
1539 | * |
1540 | * Caller holds tomoyo_read_lock(). | |
9590837b | 1541 | */ |
e2bf6907 | 1542 | static int tomoyo_write_exception(struct tomoyo_io_buffer *head) |
9590837b | 1543 | { |
bd03a3e4 | 1544 | const bool is_delete = head->w.is_delete; |
a238cf5b | 1545 | struct tomoyo_acl_param param = { |
bd03a3e4 TH |
1546 | .ns = head->w.ns, |
1547 | .is_delete = is_delete, | |
a238cf5b | 1548 | .data = head->write_buf, |
e2bf6907 | 1549 | }; |
a238cf5b | 1550 | u8 i; |
a238cf5b TH |
1551 | if (tomoyo_str_starts(¶m.data, "aggregator ")) |
1552 | return tomoyo_write_aggregator(¶m); | |
e2bf6907 | 1553 | for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) |
a238cf5b TH |
1554 | if (tomoyo_str_starts(¶m.data, tomoyo_transition_type[i])) |
1555 | return tomoyo_write_transition_control(¶m, i); | |
e2bf6907 | 1556 | for (i = 0; i < TOMOYO_MAX_GROUP; i++) |
a238cf5b TH |
1557 | if (tomoyo_str_starts(¶m.data, tomoyo_group_name[i])) |
1558 | return tomoyo_write_group(¶m, i); | |
32997144 TH |
1559 | if (tomoyo_str_starts(¶m.data, "acl_group ")) { |
1560 | unsigned int group; | |
1561 | char *data; | |
1562 | group = simple_strtoul(param.data, &data, 10); | |
1563 | if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ') | |
bd03a3e4 TH |
1564 | return tomoyo_write_domain2 |
1565 | (head->w.ns, &head->w.ns->acl_group[group], | |
1566 | data, is_delete); | |
32997144 | 1567 | } |
9590837b KT |
1568 | return -EINVAL; |
1569 | } | |
1570 | ||
1571 | /** | |
31845e8c | 1572 | * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list. |
9590837b KT |
1573 | * |
1574 | * @head: Pointer to "struct tomoyo_io_buffer". | |
31845e8c TH |
1575 | * @idx: Index number. |
1576 | * | |
1577 | * Returns true on success, false otherwise. | |
9590837b | 1578 | * |
fdb8ebb7 | 1579 | * Caller holds tomoyo_read_lock(). |
9590837b | 1580 | */ |
31845e8c | 1581 | static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) |
9590837b | 1582 | { |
bd03a3e4 TH |
1583 | struct tomoyo_policy_namespace *ns = |
1584 | container_of(head->r.ns, typeof(*ns), namespace_list); | |
1585 | struct list_head *list = &ns->group_list[idx]; | |
1586 | list_for_each_cookie(head->r.group, list) { | |
31845e8c | 1587 | struct tomoyo_group *group = |
0df7e8b8 | 1588 | list_entry(head->r.group, typeof(*group), head.list); |
f23571e8 | 1589 | list_for_each_cookie(head->r.acl, &group->member_list) { |
31845e8c | 1590 | struct tomoyo_acl_head *ptr = |
f23571e8 | 1591 | list_entry(head->r.acl, typeof(*ptr), list); |
31845e8c TH |
1592 | if (ptr->is_deleted) |
1593 | continue; | |
f23571e8 TH |
1594 | if (!tomoyo_flush(head)) |
1595 | return false; | |
bd03a3e4 | 1596 | tomoyo_print_namespace(head); |
f23571e8 TH |
1597 | tomoyo_set_string(head, tomoyo_group_name[idx]); |
1598 | tomoyo_set_string(head, group->group_name->name); | |
31845e8c | 1599 | if (idx == TOMOYO_PATH_GROUP) { |
f23571e8 TH |
1600 | tomoyo_set_space(head); |
1601 | tomoyo_set_string(head, container_of | |
1602 | (ptr, struct tomoyo_path_group, | |
1603 | head)->member_name->name); | |
31845e8c | 1604 | } else if (idx == TOMOYO_NUMBER_GROUP) { |
f23571e8 TH |
1605 | tomoyo_print_number_union(head, &container_of |
1606 | (ptr, | |
1607 | struct tomoyo_number_group, | |
1608 | head)->number); | |
31845e8c | 1609 | } |
f23571e8 | 1610 | tomoyo_set_lf(head); |
31845e8c | 1611 | } |
f23571e8 | 1612 | head->r.acl = NULL; |
31845e8c | 1613 | } |
f23571e8 | 1614 | head->r.group = NULL; |
31845e8c TH |
1615 | return true; |
1616 | } | |
1617 | ||
1618 | /** | |
1619 | * tomoyo_read_policy - Read "struct tomoyo_..._entry" list. | |
1620 | * | |
1621 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1622 | * @idx: Index number. | |
1623 | * | |
1624 | * Returns true on success, false otherwise. | |
1625 | * | |
1626 | * Caller holds tomoyo_read_lock(). | |
1627 | */ | |
1628 | static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) | |
1629 | { | |
bd03a3e4 TH |
1630 | struct tomoyo_policy_namespace *ns = |
1631 | container_of(head->r.ns, typeof(*ns), namespace_list); | |
1632 | struct list_head *list = &ns->policy_list[idx]; | |
1633 | list_for_each_cookie(head->r.acl, list) { | |
475e6fa3 | 1634 | struct tomoyo_acl_head *acl = |
f23571e8 | 1635 | container_of(head->r.acl, typeof(*acl), list); |
31845e8c TH |
1636 | if (acl->is_deleted) |
1637 | continue; | |
f23571e8 TH |
1638 | if (!tomoyo_flush(head)) |
1639 | return false; | |
31845e8c | 1640 | switch (idx) { |
5448ec4f | 1641 | case TOMOYO_ID_TRANSITION_CONTROL: |
31845e8c | 1642 | { |
5448ec4f | 1643 | struct tomoyo_transition_control *ptr = |
31845e8c | 1644 | container_of(acl, typeof(*ptr), head); |
bd03a3e4 | 1645 | tomoyo_print_namespace(head); |
0d2171d7 | 1646 | tomoyo_set_string(head, tomoyo_transition_type |
f23571e8 | 1647 | [ptr->type]); |
0d2171d7 TH |
1648 | tomoyo_set_string(head, ptr->program ? |
1649 | ptr->program->name : "any"); | |
1650 | tomoyo_set_string(head, " from "); | |
1651 | tomoyo_set_string(head, ptr->domainname ? | |
1652 | ptr->domainname->name : | |
1653 | "any"); | |
31845e8c TH |
1654 | } |
1655 | break; | |
31845e8c TH |
1656 | case TOMOYO_ID_AGGREGATOR: |
1657 | { | |
e2bf6907 | 1658 | struct tomoyo_aggregator *ptr = |
31845e8c | 1659 | container_of(acl, typeof(*ptr), head); |
bd03a3e4 | 1660 | tomoyo_print_namespace(head); |
b5bc60b4 | 1661 | tomoyo_set_string(head, "aggregator "); |
f23571e8 TH |
1662 | tomoyo_set_string(head, |
1663 | ptr->original_name->name); | |
1664 | tomoyo_set_space(head); | |
1665 | tomoyo_set_string(head, | |
1666 | ptr->aggregated_name->name); | |
31845e8c TH |
1667 | } |
1668 | break; | |
31845e8c TH |
1669 | default: |
1670 | continue; | |
9590837b | 1671 | } |
f23571e8 | 1672 | tomoyo_set_lf(head); |
9590837b | 1673 | } |
f23571e8 | 1674 | head->r.acl = NULL; |
31845e8c TH |
1675 | return true; |
1676 | } | |
1677 | ||
1678 | /** | |
e2bf6907 | 1679 | * tomoyo_read_exception - Read exception policy. |
31845e8c TH |
1680 | * |
1681 | * @head: Pointer to "struct tomoyo_io_buffer". | |
1682 | * | |
1683 | * Caller holds tomoyo_read_lock(). | |
1684 | */ | |
e2bf6907 | 1685 | static void tomoyo_read_exception(struct tomoyo_io_buffer *head) |
31845e8c | 1686 | { |
bd03a3e4 TH |
1687 | struct tomoyo_policy_namespace *ns = |
1688 | container_of(head->r.ns, typeof(*ns), namespace_list); | |
f23571e8 | 1689 | if (head->r.eof) |
31845e8c | 1690 | return; |
f23571e8 TH |
1691 | while (head->r.step < TOMOYO_MAX_POLICY && |
1692 | tomoyo_read_policy(head, head->r.step)) | |
1693 | head->r.step++; | |
1694 | if (head->r.step < TOMOYO_MAX_POLICY) | |
31845e8c | 1695 | return; |
f23571e8 TH |
1696 | while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP && |
1697 | tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY)) | |
1698 | head->r.step++; | |
1699 | if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP) | |
31845e8c | 1700 | return; |
32997144 TH |
1701 | while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP |
1702 | + TOMOYO_MAX_ACL_GROUPS) { | |
1703 | head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY | |
1704 | - TOMOYO_MAX_GROUP; | |
bd03a3e4 | 1705 | if (!tomoyo_read_domain2(head, &ns->acl_group |
32997144 TH |
1706 | [head->r.acl_group_index])) |
1707 | return; | |
1708 | head->r.step++; | |
1709 | } | |
f23571e8 | 1710 | head->r.eof = true; |
9590837b KT |
1711 | } |
1712 | ||
eadd99cc | 1713 | /* Wait queue for kernel -> userspace notification. */ |
17fcfbd9 | 1714 | static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait); |
eadd99cc TH |
1715 | /* Wait queue for userspace -> kernel notification. */ |
1716 | static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait); | |
17fcfbd9 TH |
1717 | |
1718 | /* Structure for query. */ | |
e2bf6907 | 1719 | struct tomoyo_query { |
17fcfbd9 TH |
1720 | struct list_head list; |
1721 | char *query; | |
eadd99cc | 1722 | size_t query_len; |
17fcfbd9 | 1723 | unsigned int serial; |
eadd99cc TH |
1724 | u8 timer; |
1725 | u8 answer; | |
1726 | u8 retry; | |
17fcfbd9 TH |
1727 | }; |
1728 | ||
e2bf6907 | 1729 | /* The list for "struct tomoyo_query". */ |
17fcfbd9 TH |
1730 | static LIST_HEAD(tomoyo_query_list); |
1731 | ||
eadd99cc TH |
1732 | /* Lock for manipulating tomoyo_query_list. */ |
1733 | static DEFINE_SPINLOCK(tomoyo_query_list_lock); | |
1734 | ||
17fcfbd9 TH |
1735 | /* |
1736 | * Number of "struct file" referring /sys/kernel/security/tomoyo/query | |
1737 | * interface. | |
1738 | */ | |
1739 | static atomic_t tomoyo_query_observers = ATOMIC_INIT(0); | |
1740 | ||
2ca9bf45 TH |
1741 | /** |
1742 | * tomoyo_truncate - Truncate a line. | |
1743 | * | |
1744 | * @str: String to truncate. | |
1745 | * | |
1746 | * Returns length of truncated @str. | |
1747 | */ | |
1748 | static int tomoyo_truncate(char *str) | |
1749 | { | |
1750 | char *start = str; | |
1751 | while (*(unsigned char *) str > (unsigned char) ' ') | |
1752 | str++; | |
1753 | *str = '\0'; | |
1754 | return strlen(start) + 1; | |
1755 | } | |
1756 | ||
eadd99cc TH |
1757 | /** |
1758 | * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode. | |
1759 | * | |
1760 | * @domain: Pointer to "struct tomoyo_domain_info". | |
1761 | * @header: Lines containing ACL. | |
1762 | * | |
1763 | * Returns nothing. | |
1764 | */ | |
1765 | static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header) | |
1766 | { | |
1767 | char *buffer; | |
2ca9bf45 | 1768 | char *realpath = NULL; |
5b636857 | 1769 | char *argv0 = NULL; |
2ca9bf45 | 1770 | char *symlink = NULL; |
eadd99cc TH |
1771 | char *cp = strchr(header, '\n'); |
1772 | int len; | |
1773 | if (!cp) | |
1774 | return; | |
1775 | cp = strchr(cp + 1, '\n'); | |
1776 | if (!cp) | |
1777 | return; | |
1778 | *cp++ = '\0'; | |
1779 | len = strlen(cp) + 1; | |
2ca9bf45 TH |
1780 | /* strstr() will return NULL if ordering is wrong. */ |
1781 | if (*cp == 'f') { | |
5b636857 TH |
1782 | argv0 = strstr(header, " argv[]={ \""); |
1783 | if (argv0) { | |
1784 | argv0 += 10; | |
1785 | len += tomoyo_truncate(argv0) + 14; | |
1786 | } | |
2ca9bf45 TH |
1787 | realpath = strstr(header, " exec={ realpath=\""); |
1788 | if (realpath) { | |
1789 | realpath += 8; | |
1790 | len += tomoyo_truncate(realpath) + 6; | |
1791 | } | |
1792 | symlink = strstr(header, " symlink.target=\""); | |
1793 | if (symlink) | |
1794 | len += tomoyo_truncate(symlink + 1) + 1; | |
1795 | } | |
eadd99cc TH |
1796 | buffer = kmalloc(len, GFP_NOFS); |
1797 | if (!buffer) | |
1798 | return; | |
1799 | snprintf(buffer, len - 1, "%s", cp); | |
2ca9bf45 TH |
1800 | if (realpath) |
1801 | tomoyo_addprintf(buffer, len, " exec.%s", realpath); | |
5b636857 TH |
1802 | if (argv0) |
1803 | tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0); | |
2ca9bf45 TH |
1804 | if (symlink) |
1805 | tomoyo_addprintf(buffer, len, "%s", symlink); | |
eadd99cc | 1806 | tomoyo_normalize_line(buffer); |
b22b8b9f TH |
1807 | if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer, |
1808 | false)) | |
1809 | tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES); | |
eadd99cc TH |
1810 | kfree(buffer); |
1811 | } | |
1812 | ||
17fcfbd9 TH |
1813 | /** |
1814 | * tomoyo_supervisor - Ask for the supervisor's decision. | |
1815 | * | |
eadd99cc TH |
1816 | * @r: Pointer to "struct tomoyo_request_info". |
1817 | * @fmt: The printf()'s format string, followed by parameters. | |
17fcfbd9 TH |
1818 | * |
1819 | * Returns 0 if the supervisor decided to permit the access request which | |
1820 | * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the | |
1821 | * supervisor decided to retry the access request which violated the policy in | |
1822 | * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise. | |
1823 | */ | |
1824 | int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) | |
1825 | { | |
1826 | va_list args; | |
eadd99cc | 1827 | int error; |
17fcfbd9 TH |
1828 | int len; |
1829 | static unsigned int tomoyo_serial; | |
eadd99cc | 1830 | struct tomoyo_query entry = { }; |
17fcfbd9 | 1831 | bool quota_exceeded = false; |
eadd99cc TH |
1832 | va_start(args, fmt); |
1833 | len = vsnprintf((char *) &len, 1, fmt, args) + 1; | |
1834 | va_end(args); | |
1835 | /* Write /sys/kernel/security/tomoyo/audit. */ | |
1836 | va_start(args, fmt); | |
1837 | tomoyo_write_log2(r, len, fmt, args); | |
1838 | va_end(args); | |
1839 | /* Nothing more to do if granted. */ | |
1840 | if (r->granted) | |
1841 | return 0; | |
b22b8b9f TH |
1842 | if (r->mode) |
1843 | tomoyo_update_stat(r->mode); | |
17fcfbd9 | 1844 | switch (r->mode) { |
eadd99cc TH |
1845 | case TOMOYO_CONFIG_ENFORCING: |
1846 | error = -EPERM; | |
1847 | if (atomic_read(&tomoyo_query_observers)) | |
1848 | break; | |
1849 | goto out; | |
17fcfbd9 | 1850 | case TOMOYO_CONFIG_LEARNING: |
eadd99cc TH |
1851 | error = 0; |
1852 | /* Check max_learning_entry parameter. */ | |
1853 | if (tomoyo_domain_quota_is_ok(r)) | |
1854 | break; | |
17fcfbd9 | 1855 | /* fall through */ |
eadd99cc | 1856 | default: |
17fcfbd9 TH |
1857 | return 0; |
1858 | } | |
eadd99cc | 1859 | /* Get message. */ |
17fcfbd9 | 1860 | va_start(args, fmt); |
eadd99cc | 1861 | entry.query = tomoyo_init_log(r, len, fmt, args); |
17fcfbd9 | 1862 | va_end(args); |
eadd99cc | 1863 | if (!entry.query) |
17fcfbd9 | 1864 | goto out; |
eadd99cc TH |
1865 | entry.query_len = strlen(entry.query) + 1; |
1866 | if (!error) { | |
1867 | tomoyo_add_entry(r->domain, entry.query); | |
17fcfbd9 | 1868 | goto out; |
eadd99cc TH |
1869 | } |
1870 | len = tomoyo_round2(entry.query_len); | |
17fcfbd9 | 1871 | spin_lock(&tomoyo_query_list_lock); |
eadd99cc TH |
1872 | if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] && |
1873 | tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len | |
1874 | >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) { | |
17fcfbd9 TH |
1875 | quota_exceeded = true; |
1876 | } else { | |
eadd99cc TH |
1877 | entry.serial = tomoyo_serial++; |
1878 | entry.retry = r->retry; | |
1879 | tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len; | |
1880 | list_add_tail(&entry.list, &tomoyo_query_list); | |
17fcfbd9 TH |
1881 | } |
1882 | spin_unlock(&tomoyo_query_list_lock); | |
1883 | if (quota_exceeded) | |
1884 | goto out; | |
17fcfbd9 | 1885 | /* Give 10 seconds for supervisor's opinion. */ |
eadd99cc TH |
1886 | while (entry.timer < 10) { |
1887 | wake_up_all(&tomoyo_query_wait); | |
1888 | if (wait_event_interruptible_timeout | |
1889 | (tomoyo_answer_wait, entry.answer || | |
1890 | !atomic_read(&tomoyo_query_observers), HZ)) | |
17fcfbd9 | 1891 | break; |
eadd99cc TH |
1892 | else |
1893 | entry.timer++; | |
17fcfbd9 TH |
1894 | } |
1895 | spin_lock(&tomoyo_query_list_lock); | |
eadd99cc TH |
1896 | list_del(&entry.list); |
1897 | tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len; | |
17fcfbd9 | 1898 | spin_unlock(&tomoyo_query_list_lock); |
eadd99cc | 1899 | switch (entry.answer) { |
17fcfbd9 TH |
1900 | case 3: /* Asked to retry by administrator. */ |
1901 | error = TOMOYO_RETRY_REQUEST; | |
1902 | r->retry++; | |
1903 | break; | |
1904 | case 1: | |
1905 | /* Granted by administrator. */ | |
1906 | error = 0; | |
1907 | break; | |
17fcfbd9 | 1908 | default: |
eadd99cc | 1909 | /* Timed out or rejected by administrator. */ |
17fcfbd9 TH |
1910 | break; |
1911 | } | |
eadd99cc TH |
1912 | out: |
1913 | kfree(entry.query); | |
17fcfbd9 TH |
1914 | return error; |
1915 | } | |
1916 | ||
1917 | /** | |
1918 | * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query. | |
1919 | * | |
1920 | * @file: Pointer to "struct file". | |
1921 | * @wait: Pointer to "poll_table". | |
1922 | * | |
1923 | * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise. | |
1924 | * | |
1925 | * Waits for access requests which violated policy in enforcing mode. | |
1926 | */ | |
1927 | static int tomoyo_poll_query(struct file *file, poll_table *wait) | |
1928 | { | |
1929 | struct list_head *tmp; | |
1930 | bool found = false; | |
1931 | u8 i; | |
1932 | for (i = 0; i < 2; i++) { | |
1933 | spin_lock(&tomoyo_query_list_lock); | |
1934 | list_for_each(tmp, &tomoyo_query_list) { | |
e2bf6907 TH |
1935 | struct tomoyo_query *ptr = |
1936 | list_entry(tmp, typeof(*ptr), list); | |
17fcfbd9 TH |
1937 | if (ptr->answer) |
1938 | continue; | |
1939 | found = true; | |
1940 | break; | |
1941 | } | |
1942 | spin_unlock(&tomoyo_query_list_lock); | |
1943 | if (found) | |
1944 | return POLLIN | POLLRDNORM; | |
1945 | if (i) | |
1946 | break; | |
1947 | poll_wait(file, &tomoyo_query_wait, wait); | |
1948 | } | |
1949 | return 0; | |
1950 | } | |
1951 | ||
1952 | /** | |
1953 | * tomoyo_read_query - Read access requests which violated policy in enforcing mode. | |
1954 | * | |
1955 | * @head: Pointer to "struct tomoyo_io_buffer". | |
17fcfbd9 | 1956 | */ |
8fbe71f0 | 1957 | static void tomoyo_read_query(struct tomoyo_io_buffer *head) |
17fcfbd9 TH |
1958 | { |
1959 | struct list_head *tmp; | |
2c47ab93 TH |
1960 | unsigned int pos = 0; |
1961 | size_t len = 0; | |
17fcfbd9 | 1962 | char *buf; |
f23571e8 | 1963 | if (head->r.w_pos) |
8fbe71f0 | 1964 | return; |
17fcfbd9 TH |
1965 | if (head->read_buf) { |
1966 | kfree(head->read_buf); | |
1967 | head->read_buf = NULL; | |
17fcfbd9 TH |
1968 | } |
1969 | spin_lock(&tomoyo_query_list_lock); | |
1970 | list_for_each(tmp, &tomoyo_query_list) { | |
e2bf6907 | 1971 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
17fcfbd9 TH |
1972 | if (ptr->answer) |
1973 | continue; | |
f23571e8 | 1974 | if (pos++ != head->r.query_index) |
17fcfbd9 TH |
1975 | continue; |
1976 | len = ptr->query_len; | |
1977 | break; | |
1978 | } | |
1979 | spin_unlock(&tomoyo_query_list_lock); | |
1980 | if (!len) { | |
f23571e8 | 1981 | head->r.query_index = 0; |
8fbe71f0 | 1982 | return; |
17fcfbd9 | 1983 | } |
eadd99cc | 1984 | buf = kzalloc(len + 32, GFP_NOFS); |
17fcfbd9 | 1985 | if (!buf) |
8fbe71f0 | 1986 | return; |
17fcfbd9 TH |
1987 | pos = 0; |
1988 | spin_lock(&tomoyo_query_list_lock); | |
1989 | list_for_each(tmp, &tomoyo_query_list) { | |
e2bf6907 | 1990 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
17fcfbd9 TH |
1991 | if (ptr->answer) |
1992 | continue; | |
f23571e8 | 1993 | if (pos++ != head->r.query_index) |
17fcfbd9 TH |
1994 | continue; |
1995 | /* | |
1996 | * Some query can be skipped because tomoyo_query_list | |
1997 | * can change, but I don't care. | |
1998 | */ | |
1999 | if (len == ptr->query_len) | |
eadd99cc TH |
2000 | snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial, |
2001 | ptr->retry, ptr->query); | |
17fcfbd9 TH |
2002 | break; |
2003 | } | |
2004 | spin_unlock(&tomoyo_query_list_lock); | |
2005 | if (buf[0]) { | |
17fcfbd9 | 2006 | head->read_buf = buf; |
f23571e8 TH |
2007 | head->r.w[head->r.w_pos++] = buf; |
2008 | head->r.query_index++; | |
17fcfbd9 TH |
2009 | } else { |
2010 | kfree(buf); | |
2011 | } | |
17fcfbd9 TH |
2012 | } |
2013 | ||
2014 | /** | |
2015 | * tomoyo_write_answer - Write the supervisor's decision. | |
2016 | * | |
2017 | * @head: Pointer to "struct tomoyo_io_buffer". | |
2018 | * | |
2019 | * Returns 0 on success, -EINVAL otherwise. | |
2020 | */ | |
2021 | static int tomoyo_write_answer(struct tomoyo_io_buffer *head) | |
2022 | { | |
2023 | char *data = head->write_buf; | |
2024 | struct list_head *tmp; | |
2025 | unsigned int serial; | |
2026 | unsigned int answer; | |
2027 | spin_lock(&tomoyo_query_list_lock); | |
2028 | list_for_each(tmp, &tomoyo_query_list) { | |
e2bf6907 | 2029 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
17fcfbd9 TH |
2030 | ptr->timer = 0; |
2031 | } | |
2032 | spin_unlock(&tomoyo_query_list_lock); | |
2033 | if (sscanf(data, "A%u=%u", &serial, &answer) != 2) | |
2034 | return -EINVAL; | |
2035 | spin_lock(&tomoyo_query_list_lock); | |
2036 | list_for_each(tmp, &tomoyo_query_list) { | |
e2bf6907 | 2037 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
17fcfbd9 TH |
2038 | if (ptr->serial != serial) |
2039 | continue; | |
2040 | if (!ptr->answer) | |
2041 | ptr->answer = answer; | |
2042 | break; | |
2043 | } | |
2044 | spin_unlock(&tomoyo_query_list_lock); | |
2045 | return 0; | |
2046 | } | |
2047 | ||
9590837b KT |
2048 | /** |
2049 | * tomoyo_read_version: Get version. | |
2050 | * | |
2051 | * @head: Pointer to "struct tomoyo_io_buffer". | |
2052 | * | |
2053 | * Returns version information. | |
2054 | */ | |
8fbe71f0 | 2055 | static void tomoyo_read_version(struct tomoyo_io_buffer *head) |
9590837b | 2056 | { |
f23571e8 | 2057 | if (!head->r.eof) { |
d5ca1725 | 2058 | tomoyo_io_printf(head, "2.4.0"); |
f23571e8 | 2059 | head->r.eof = true; |
9590837b | 2060 | } |
9590837b KT |
2061 | } |
2062 | ||
2063 | /** | |
2064 | * tomoyo_read_self_domain - Get the current process's domainname. | |
2065 | * | |
2066 | * @head: Pointer to "struct tomoyo_io_buffer". | |
2067 | * | |
2068 | * Returns the current process's domainname. | |
2069 | */ | |
8fbe71f0 | 2070 | static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head) |
9590837b | 2071 | { |
f23571e8 | 2072 | if (!head->r.eof) { |
9590837b KT |
2073 | /* |
2074 | * tomoyo_domain()->domainname != NULL | |
2075 | * because every process belongs to a domain and | |
2076 | * the domain's name cannot be NULL. | |
2077 | */ | |
2078 | tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name); | |
f23571e8 | 2079 | head->r.eof = true; |
9590837b | 2080 | } |
9590837b KT |
2081 | } |
2082 | ||
b22b8b9f TH |
2083 | /* String table for /sys/kernel/security/tomoyo/stat interface. */ |
2084 | static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = { | |
2085 | [TOMOYO_STAT_POLICY_UPDATES] = "update:", | |
2086 | [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:", | |
2087 | [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:", | |
2088 | [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:", | |
2089 | }; | |
2090 | ||
2091 | /* String table for /sys/kernel/security/tomoyo/stat interface. */ | |
2092 | static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = { | |
2093 | [TOMOYO_MEMORY_POLICY] = "policy:", | |
2094 | [TOMOYO_MEMORY_AUDIT] = "audit log:", | |
2095 | [TOMOYO_MEMORY_QUERY] = "query message:", | |
2096 | }; | |
2097 | ||
2098 | /* Timestamp counter for last updated. */ | |
2099 | static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT]; | |
2100 | /* Counter for number of updates. */ | |
2101 | static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT]; | |
2102 | ||
2103 | /** | |
2104 | * tomoyo_update_stat - Update statistic counters. | |
2105 | * | |
2106 | * @index: Index for policy type. | |
2107 | * | |
2108 | * Returns nothing. | |
2109 | */ | |
2110 | void tomoyo_update_stat(const u8 index) | |
2111 | { | |
2112 | struct timeval tv; | |
2113 | do_gettimeofday(&tv); | |
2114 | /* | |
2115 | * I don't use atomic operations because race condition is not fatal. | |
2116 | */ | |
2117 | tomoyo_stat_updated[index]++; | |
2118 | tomoyo_stat_modified[index] = tv.tv_sec; | |
2119 | } | |
2120 | ||
2121 | /** | |
2122 | * tomoyo_read_stat - Read statistic data. | |
2123 | * | |
2124 | * @head: Pointer to "struct tomoyo_io_buffer". | |
2125 | * | |
2126 | * Returns nothing. | |
2127 | */ | |
2128 | static void tomoyo_read_stat(struct tomoyo_io_buffer *head) | |
2129 | { | |
2130 | u8 i; | |
2131 | unsigned int total = 0; | |
2132 | if (head->r.eof) | |
2133 | return; | |
2134 | for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) { | |
2135 | tomoyo_io_printf(head, "Policy %-30s %10u", | |
2136 | tomoyo_policy_headers[i], | |
2137 | tomoyo_stat_updated[i]); | |
2138 | if (tomoyo_stat_modified[i]) { | |
2139 | struct tomoyo_time stamp; | |
2140 | tomoyo_convert_time(tomoyo_stat_modified[i], &stamp); | |
2141 | tomoyo_io_printf(head, " (Last: %04u/%02u/%02u " | |
2142 | "%02u:%02u:%02u)", | |
2143 | stamp.year, stamp.month, stamp.day, | |
2144 | stamp.hour, stamp.min, stamp.sec); | |
2145 | } | |
2146 | tomoyo_set_lf(head); | |
2147 | } | |
2148 | for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) { | |
2149 | unsigned int used = tomoyo_memory_used[i]; | |
2150 | total += used; | |
2151 | tomoyo_io_printf(head, "Memory used by %-22s %10u", | |
2152 | tomoyo_memory_headers[i], used); | |
2153 | used = tomoyo_memory_quota[i]; | |
2154 | if (used) | |
2155 | tomoyo_io_printf(head, " (Quota: %10u)", used); | |
2156 | tomoyo_set_lf(head); | |
2157 | } | |
2158 | tomoyo_io_printf(head, "Total memory used: %10u\n", | |
2159 | total); | |
2160 | head->r.eof = true; | |
2161 | } | |
2162 | ||
2163 | /** | |
2164 | * tomoyo_write_stat - Set memory quota. | |
2165 | * | |
2166 | * @head: Pointer to "struct tomoyo_io_buffer". | |
2167 | * | |
2168 | * Returns 0. | |
2169 | */ | |
2170 | static int tomoyo_write_stat(struct tomoyo_io_buffer *head) | |
2171 | { | |
2172 | char *data = head->write_buf; | |
2173 | u8 i; | |
2174 | if (tomoyo_str_starts(&data, "Memory used by ")) | |
2175 | for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) | |
2176 | if (tomoyo_str_starts(&data, tomoyo_memory_headers[i])) | |
2177 | sscanf(data, "%u", &tomoyo_memory_quota[i]); | |
2178 | return 0; | |
2179 | } | |
2180 | ||
9590837b KT |
2181 | /** |
2182 | * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface. | |
2183 | * | |
2184 | * @type: Type of interface. | |
2185 | * @file: Pointer to "struct file". | |
2186 | * | |
2e503bbb | 2187 | * Returns 0 on success, negative value otherwise. |
9590837b | 2188 | */ |
c3ef1500 | 2189 | int tomoyo_open_control(const u8 type, struct file *file) |
9590837b | 2190 | { |
4e5d6f7e | 2191 | struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS); |
9590837b KT |
2192 | |
2193 | if (!head) | |
2194 | return -ENOMEM; | |
2195 | mutex_init(&head->io_sem); | |
17fcfbd9 | 2196 | head->type = type; |
9590837b KT |
2197 | switch (type) { |
2198 | case TOMOYO_DOMAINPOLICY: | |
2199 | /* /sys/kernel/security/tomoyo/domain_policy */ | |
e2bf6907 TH |
2200 | head->write = tomoyo_write_domain; |
2201 | head->read = tomoyo_read_domain; | |
9590837b KT |
2202 | break; |
2203 | case TOMOYO_EXCEPTIONPOLICY: | |
2204 | /* /sys/kernel/security/tomoyo/exception_policy */ | |
e2bf6907 TH |
2205 | head->write = tomoyo_write_exception; |
2206 | head->read = tomoyo_read_exception; | |
9590837b | 2207 | break; |
eadd99cc TH |
2208 | case TOMOYO_AUDIT: |
2209 | /* /sys/kernel/security/tomoyo/audit */ | |
2210 | head->poll = tomoyo_poll_log; | |
2211 | head->read = tomoyo_read_log; | |
2212 | break; | |
9590837b KT |
2213 | case TOMOYO_SELFDOMAIN: |
2214 | /* /sys/kernel/security/tomoyo/self_domain */ | |
2215 | head->read = tomoyo_read_self_domain; | |
2216 | break; | |
9590837b KT |
2217 | case TOMOYO_PROCESS_STATUS: |
2218 | /* /sys/kernel/security/tomoyo/.process_status */ | |
2219 | head->write = tomoyo_write_pid; | |
2220 | head->read = tomoyo_read_pid; | |
2221 | break; | |
2222 | case TOMOYO_VERSION: | |
2223 | /* /sys/kernel/security/tomoyo/version */ | |
2224 | head->read = tomoyo_read_version; | |
2225 | head->readbuf_size = 128; | |
2226 | break; | |
b22b8b9f TH |
2227 | case TOMOYO_STAT: |
2228 | /* /sys/kernel/security/tomoyo/stat */ | |
2229 | head->write = tomoyo_write_stat; | |
2230 | head->read = tomoyo_read_stat; | |
2231 | head->readbuf_size = 1024; | |
9590837b KT |
2232 | break; |
2233 | case TOMOYO_PROFILE: | |
2234 | /* /sys/kernel/security/tomoyo/profile */ | |
2235 | head->write = tomoyo_write_profile; | |
2236 | head->read = tomoyo_read_profile; | |
2237 | break; | |
17fcfbd9 TH |
2238 | case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */ |
2239 | head->poll = tomoyo_poll_query; | |
2240 | head->write = tomoyo_write_answer; | |
2241 | head->read = tomoyo_read_query; | |
2242 | break; | |
9590837b KT |
2243 | case TOMOYO_MANAGER: |
2244 | /* /sys/kernel/security/tomoyo/manager */ | |
e2bf6907 TH |
2245 | head->write = tomoyo_write_manager; |
2246 | head->read = tomoyo_read_manager; | |
9590837b KT |
2247 | break; |
2248 | } | |
2249 | if (!(file->f_mode & FMODE_READ)) { | |
2250 | /* | |
2251 | * No need to allocate read_buf since it is not opened | |
2252 | * for reading. | |
2253 | */ | |
2254 | head->read = NULL; | |
17fcfbd9 TH |
2255 | head->poll = NULL; |
2256 | } else if (!head->poll) { | |
2257 | /* Don't allocate read_buf for poll() access. */ | |
9590837b KT |
2258 | if (!head->readbuf_size) |
2259 | head->readbuf_size = 4096 * 2; | |
4e5d6f7e | 2260 | head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS); |
9590837b | 2261 | if (!head->read_buf) { |
8e2d39a1 | 2262 | kfree(head); |
9590837b KT |
2263 | return -ENOMEM; |
2264 | } | |
2265 | } | |
2266 | if (!(file->f_mode & FMODE_WRITE)) { | |
2267 | /* | |
2268 | * No need to allocate write_buf since it is not opened | |
2269 | * for writing. | |
2270 | */ | |
2271 | head->write = NULL; | |
2272 | } else if (head->write) { | |
2273 | head->writebuf_size = 4096 * 2; | |
4e5d6f7e | 2274 | head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS); |
9590837b | 2275 | if (!head->write_buf) { |
8e2d39a1 TH |
2276 | kfree(head->read_buf); |
2277 | kfree(head); | |
9590837b KT |
2278 | return -ENOMEM; |
2279 | } | |
2280 | } | |
17fcfbd9 TH |
2281 | /* |
2282 | * If the file is /sys/kernel/security/tomoyo/query , increment the | |
2283 | * observer counter. | |
2284 | * The obserber counter is used by tomoyo_supervisor() to see if | |
2285 | * there is some process monitoring /sys/kernel/security/tomoyo/query. | |
2286 | */ | |
7c75964f | 2287 | if (type == TOMOYO_QUERY) |
17fcfbd9 | 2288 | atomic_inc(&tomoyo_query_observers); |
2e503bbb TH |
2289 | file->private_data = head; |
2290 | tomoyo_notify_gc(head, true); | |
9590837b KT |
2291 | return 0; |
2292 | } | |
2293 | ||
0849e3ba TH |
2294 | /** |
2295 | * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface. | |
2296 | * | |
2297 | * @file: Pointer to "struct file". | |
2298 | * @wait: Pointer to "poll_table". | |
2299 | * | |
2300 | * Waits for read readiness. | |
eadd99cc TH |
2301 | * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and |
2302 | * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd. | |
0849e3ba TH |
2303 | */ |
2304 | int tomoyo_poll_control(struct file *file, poll_table *wait) | |
2305 | { | |
2306 | struct tomoyo_io_buffer *head = file->private_data; | |
2307 | if (!head->poll) | |
2308 | return -ENOSYS; | |
2309 | return head->poll(file, wait); | |
2310 | } | |
2311 | ||
bd03a3e4 TH |
2312 | /** |
2313 | * tomoyo_set_namespace_cursor - Set namespace to read. | |
2314 | * | |
2315 | * @head: Pointer to "struct tomoyo_io_buffer". | |
2316 | * | |
2317 | * Returns nothing. | |
2318 | */ | |
2319 | static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head) | |
2320 | { | |
2321 | struct list_head *ns; | |
2322 | if (head->type != TOMOYO_EXCEPTIONPOLICY && | |
2323 | head->type != TOMOYO_PROFILE) | |
2324 | return; | |
2325 | /* | |
2326 | * If this is the first read, or reading previous namespace finished | |
2327 | * and has more namespaces to read, update the namespace cursor. | |
2328 | */ | |
2329 | ns = head->r.ns; | |
2330 | if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) { | |
2331 | /* Clearing is OK because tomoyo_flush() returned true. */ | |
2332 | memset(&head->r, 0, sizeof(head->r)); | |
2333 | head->r.ns = ns ? ns->next : tomoyo_namespace_list.next; | |
2334 | } | |
2335 | } | |
2336 | ||
2337 | /** | |
2338 | * tomoyo_has_more_namespace - Check for unread namespaces. | |
2339 | * | |
2340 | * @head: Pointer to "struct tomoyo_io_buffer". | |
2341 | * | |
2342 | * Returns true if we have more entries to print, false otherwise. | |
2343 | */ | |
2344 | static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head) | |
2345 | { | |
2346 | return (head->type == TOMOYO_EXCEPTIONPOLICY || | |
2347 | head->type == TOMOYO_PROFILE) && head->r.eof && | |
2348 | head->r.ns->next != &tomoyo_namespace_list; | |
2349 | } | |
2350 | ||
9590837b KT |
2351 | /** |
2352 | * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface. | |
2353 | * | |
0df7e8b8 | 2354 | * @head: Pointer to "struct tomoyo_io_buffer". |
9590837b KT |
2355 | * @buffer: Poiner to buffer to write to. |
2356 | * @buffer_len: Size of @buffer. | |
2357 | * | |
2358 | * Returns bytes read on success, negative value otherwise. | |
2359 | */ | |
2c47ab93 TH |
2360 | ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer, |
2361 | const int buffer_len) | |
9590837b | 2362 | { |
f23571e8 | 2363 | int len; |
2e503bbb | 2364 | int idx; |
9590837b KT |
2365 | |
2366 | if (!head->read) | |
2367 | return -ENOSYS; | |
2368 | if (mutex_lock_interruptible(&head->io_sem)) | |
2369 | return -EINTR; | |
f23571e8 TH |
2370 | head->read_user_buf = buffer; |
2371 | head->read_user_buf_avail = buffer_len; | |
2e503bbb | 2372 | idx = tomoyo_read_lock(); |
f23571e8 TH |
2373 | if (tomoyo_flush(head)) |
2374 | /* Call the policy handler. */ | |
bd03a3e4 TH |
2375 | do { |
2376 | tomoyo_set_namespace_cursor(head); | |
2377 | head->read(head); | |
2378 | } while (tomoyo_flush(head) && | |
2379 | tomoyo_has_more_namespace(head)); | |
2e503bbb | 2380 | tomoyo_read_unlock(idx); |
f23571e8 | 2381 | len = head->read_user_buf - buffer; |
9590837b KT |
2382 | mutex_unlock(&head->io_sem); |
2383 | return len; | |
2384 | } | |
2385 | ||
bd03a3e4 TH |
2386 | /** |
2387 | * tomoyo_parse_policy - Parse a policy line. | |
2388 | * | |
2389 | * @head: Poiter to "struct tomoyo_io_buffer". | |
2390 | * @line: Line to parse. | |
2391 | * | |
2392 | * Returns 0 on success, negative value otherwise. | |
2393 | * | |
2394 | * Caller holds tomoyo_read_lock(). | |
2395 | */ | |
2396 | static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line) | |
2397 | { | |
2398 | /* Delete request? */ | |
2399 | head->w.is_delete = !strncmp(line, "delete ", 7); | |
2400 | if (head->w.is_delete) | |
2401 | memmove(line, line + 7, strlen(line + 7) + 1); | |
2402 | /* Selecting namespace to update. */ | |
2403 | if (head->type == TOMOYO_EXCEPTIONPOLICY || | |
2404 | head->type == TOMOYO_PROFILE) { | |
2405 | if (*line == '<') { | |
2406 | char *cp = strchr(line, ' '); | |
2407 | if (cp) { | |
2408 | *cp++ = '\0'; | |
2409 | head->w.ns = tomoyo_assign_namespace(line); | |
2410 | memmove(line, cp, strlen(cp) + 1); | |
2411 | } else | |
2412 | head->w.ns = NULL; | |
2413 | } else | |
2414 | head->w.ns = &tomoyo_kernel_namespace; | |
2415 | /* Don't allow updating if namespace is invalid. */ | |
2416 | if (!head->w.ns) | |
2417 | return -ENOENT; | |
2418 | } | |
2419 | /* Do the update. */ | |
2420 | return head->write(head); | |
2421 | } | |
2422 | ||
9590837b KT |
2423 | /** |
2424 | * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface. | |
2425 | * | |
0df7e8b8 | 2426 | * @head: Pointer to "struct tomoyo_io_buffer". |
9590837b KT |
2427 | * @buffer: Pointer to buffer to read from. |
2428 | * @buffer_len: Size of @buffer. | |
2429 | * | |
2430 | * Returns @buffer_len on success, negative value otherwise. | |
2431 | */ | |
2c47ab93 TH |
2432 | ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head, |
2433 | const char __user *buffer, const int buffer_len) | |
9590837b | 2434 | { |
9590837b | 2435 | int error = buffer_len; |
bd03a3e4 | 2436 | size_t avail_len = buffer_len; |
9590837b | 2437 | char *cp0 = head->write_buf; |
2e503bbb | 2438 | int idx; |
9590837b KT |
2439 | if (!head->write) |
2440 | return -ENOSYS; | |
2441 | if (!access_ok(VERIFY_READ, buffer, buffer_len)) | |
2442 | return -EFAULT; | |
9590837b KT |
2443 | if (mutex_lock_interruptible(&head->io_sem)) |
2444 | return -EINTR; | |
2e503bbb | 2445 | idx = tomoyo_read_lock(); |
9590837b KT |
2446 | /* Read a line and dispatch it to the policy handler. */ |
2447 | while (avail_len > 0) { | |
2448 | char c; | |
0df7e8b8 | 2449 | if (head->w.avail >= head->writebuf_size - 1) { |
bd03a3e4 TH |
2450 | const int len = head->writebuf_size * 2; |
2451 | char *cp = kzalloc(len, GFP_NOFS); | |
2452 | if (!cp) { | |
2453 | error = -ENOMEM; | |
2454 | break; | |
2455 | } | |
2456 | memmove(cp, cp0, head->w.avail); | |
2457 | kfree(cp0); | |
2458 | head->write_buf = cp; | |
2459 | cp0 = cp; | |
2460 | head->writebuf_size = len; | |
2461 | } | |
2462 | if (get_user(c, buffer)) { | |
9590837b KT |
2463 | error = -EFAULT; |
2464 | break; | |
2465 | } | |
2466 | buffer++; | |
2467 | avail_len--; | |
0df7e8b8 | 2468 | cp0[head->w.avail++] = c; |
9590837b KT |
2469 | if (c != '\n') |
2470 | continue; | |
0df7e8b8 TH |
2471 | cp0[head->w.avail - 1] = '\0'; |
2472 | head->w.avail = 0; | |
9590837b | 2473 | tomoyo_normalize_line(cp0); |
bd03a3e4 TH |
2474 | if (!strcmp(cp0, "reset")) { |
2475 | head->w.ns = &tomoyo_kernel_namespace; | |
2476 | head->w.domain = NULL; | |
2477 | memset(&head->r, 0, sizeof(head->r)); | |
2478 | continue; | |
2479 | } | |
2480 | /* Don't allow updating policies by non manager programs. */ | |
2481 | switch (head->type) { | |
2482 | case TOMOYO_PROCESS_STATUS: | |
2483 | /* This does not write anything. */ | |
2484 | break; | |
2485 | case TOMOYO_DOMAINPOLICY: | |
2486 | if (tomoyo_select_domain(head, cp0)) | |
2487 | continue; | |
2488 | /* fall through */ | |
2489 | case TOMOYO_EXCEPTIONPOLICY: | |
2490 | if (!strcmp(cp0, "select transition_only")) { | |
2491 | head->r.print_transition_related_only = true; | |
2492 | continue; | |
2493 | } | |
2494 | /* fall through */ | |
2495 | default: | |
2496 | if (!tomoyo_manager()) { | |
2497 | error = -EPERM; | |
2498 | goto out; | |
2499 | } | |
2500 | } | |
2501 | switch (tomoyo_parse_policy(head, cp0)) { | |
2502 | case -EPERM: | |
2503 | error = -EPERM; | |
2504 | goto out; | |
b22b8b9f TH |
2505 | case 0: |
2506 | switch (head->type) { | |
2507 | case TOMOYO_DOMAINPOLICY: | |
2508 | case TOMOYO_EXCEPTIONPOLICY: | |
b22b8b9f TH |
2509 | case TOMOYO_STAT: |
2510 | case TOMOYO_PROFILE: | |
2511 | case TOMOYO_MANAGER: | |
2512 | tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES); | |
2513 | break; | |
2514 | default: | |
2515 | break; | |
2516 | } | |
2517 | break; | |
bd03a3e4 | 2518 | } |
9590837b | 2519 | } |
bd03a3e4 | 2520 | out: |
2e503bbb | 2521 | tomoyo_read_unlock(idx); |
9590837b KT |
2522 | mutex_unlock(&head->io_sem); |
2523 | return error; | |
2524 | } | |
2525 | ||
2526 | /** | |
2527 | * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface. | |
2528 | * | |
0df7e8b8 | 2529 | * @head: Pointer to "struct tomoyo_io_buffer". |
9590837b | 2530 | * |
2e503bbb | 2531 | * Returns 0. |
9590837b | 2532 | */ |
0df7e8b8 | 2533 | int tomoyo_close_control(struct tomoyo_io_buffer *head) |
9590837b | 2534 | { |
17fcfbd9 TH |
2535 | /* |
2536 | * If the file is /sys/kernel/security/tomoyo/query , decrement the | |
2537 | * observer counter. | |
2538 | */ | |
2e503bbb TH |
2539 | if (head->type == TOMOYO_QUERY && |
2540 | atomic_dec_and_test(&tomoyo_query_observers)) | |
2541 | wake_up_all(&tomoyo_answer_wait); | |
2542 | tomoyo_notify_gc(head, false); | |
9590837b KT |
2543 | return 0; |
2544 | } | |
2545 | ||
9590837b | 2546 | /** |
c3ef1500 | 2547 | * tomoyo_check_profile - Check all profiles currently assigned to domains are defined. |
9590837b | 2548 | */ |
c3ef1500 | 2549 | void tomoyo_check_profile(void) |
9590837b | 2550 | { |
c3ef1500 TH |
2551 | struct tomoyo_domain_info *domain; |
2552 | const int idx = tomoyo_read_lock(); | |
2553 | tomoyo_policy_loaded = true; | |
bd03a3e4 | 2554 | printk(KERN_INFO "TOMOYO: 2.4.0\n"); |
c3ef1500 TH |
2555 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
2556 | const u8 profile = domain->profile; | |
bd03a3e4 TH |
2557 | const struct tomoyo_policy_namespace *ns = domain->ns; |
2558 | if (ns->profile_version != 20100903) | |
2559 | printk(KERN_ERR | |
2560 | "Profile version %u is not supported.\n", | |
2561 | ns->profile_version); | |
2562 | else if (!ns->profile_ptr[profile]) | |
2563 | printk(KERN_ERR | |
2564 | "Profile %u (used by '%s') is not defined.\n", | |
2565 | profile, domain->domainname->name); | |
2566 | else | |
c3ef1500 | 2567 | continue; |
bd03a3e4 TH |
2568 | printk(KERN_ERR |
2569 | "Userland tools for TOMOYO 2.4 must be installed and " | |
2570 | "policy must be initialized.\n"); | |
2571 | printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.4/ " | |
9f1c1d42 | 2572 | "for more information.\n"); |
bd03a3e4 | 2573 | panic("STOP!"); |
c3ef1500 TH |
2574 | } |
2575 | tomoyo_read_unlock(idx); | |
c3ef1500 | 2576 | printk(KERN_INFO "Mandatory Access Control activated.\n"); |
9590837b | 2577 | } |
efe836ab TH |
2578 | |
2579 | /** | |
2580 | * tomoyo_load_builtin_policy - Load built-in policy. | |
2581 | * | |
2582 | * Returns nothing. | |
2583 | */ | |
2584 | void __init tomoyo_load_builtin_policy(void) | |
2585 | { | |
2586 | /* | |
2587 | * This include file is manually created and contains built-in policy | |
2588 | * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy", | |
2589 | * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager", | |
2590 | * "tomoyo_builtin_stat" in the form of "static char [] __initdata". | |
2591 | */ | |
2592 | #include "builtin-policy.h" | |
2593 | u8 i; | |
2594 | const int idx = tomoyo_read_lock(); | |
2595 | for (i = 0; i < 5; i++) { | |
2596 | struct tomoyo_io_buffer head = { }; | |
2597 | char *start = ""; | |
2598 | switch (i) { | |
2599 | case 0: | |
2600 | start = tomoyo_builtin_profile; | |
2601 | head.type = TOMOYO_PROFILE; | |
2602 | head.write = tomoyo_write_profile; | |
2603 | break; | |
2604 | case 1: | |
2605 | start = tomoyo_builtin_exception_policy; | |
2606 | head.type = TOMOYO_EXCEPTIONPOLICY; | |
2607 | head.write = tomoyo_write_exception; | |
2608 | break; | |
2609 | case 2: | |
2610 | start = tomoyo_builtin_domain_policy; | |
2611 | head.type = TOMOYO_DOMAINPOLICY; | |
2612 | head.write = tomoyo_write_domain; | |
2613 | break; | |
2614 | case 3: | |
2615 | start = tomoyo_builtin_manager; | |
2616 | head.type = TOMOYO_MANAGER; | |
2617 | head.write = tomoyo_write_manager; | |
2618 | break; | |
2619 | case 4: | |
2620 | start = tomoyo_builtin_stat; | |
2621 | head.type = TOMOYO_STAT; | |
2622 | head.write = tomoyo_write_stat; | |
2623 | break; | |
2624 | } | |
2625 | while (1) { | |
2626 | char *end = strchr(start, '\n'); | |
2627 | if (!end) | |
2628 | break; | |
2629 | *end = '\0'; | |
2630 | tomoyo_normalize_line(start); | |
2631 | head.write_buf = start; | |
2632 | tomoyo_parse_policy(&head, start); | |
2633 | start = end + 1; | |
2634 | } | |
2635 | } | |
2636 | tomoyo_read_unlock(idx); | |
0e4ae0e0 TH |
2637 | #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER |
2638 | tomoyo_check_profile(); | |
2639 | #endif | |
efe836ab | 2640 | } |