Commit | Line | Data |
---|---|---|
c73bd6d4 KT |
1 | /* |
2 | * security/tomoyo/realpath.c | |
3 | * | |
4 | * Get the canonicalized absolute pathnames. The basis for TOMOYO. | |
5 | * | |
6 | * Copyright (C) 2005-2009 NTT DATA CORPORATION | |
7 | * | |
39826a1e | 8 | * Version: 2.2.0 2009/04/01 |
c73bd6d4 KT |
9 | * |
10 | */ | |
11 | ||
12 | #include <linux/types.h> | |
13 | #include <linux/mount.h> | |
14 | #include <linux/mnt_namespace.h> | |
5ad4e53b | 15 | #include <linux/fs_struct.h> |
024e1a49 SH |
16 | #include <linux/hash.h> |
17 | ||
c73bd6d4 KT |
18 | #include "common.h" |
19 | #include "realpath.h" | |
20 | ||
21 | /** | |
22 | * tomoyo_encode: Convert binary string to ascii string. | |
23 | * | |
24 | * @buffer: Buffer for ASCII string. | |
25 | * @buflen: Size of @buffer. | |
26 | * @str: Binary string. | |
27 | * | |
28 | * Returns 0 on success, -ENOMEM otherwise. | |
29 | */ | |
30 | int tomoyo_encode(char *buffer, int buflen, const char *str) | |
31 | { | |
32 | while (1) { | |
33 | const unsigned char c = *(unsigned char *) str++; | |
34 | ||
35 | if (tomoyo_is_valid(c)) { | |
36 | if (--buflen <= 0) | |
37 | break; | |
38 | *buffer++ = (char) c; | |
39 | if (c != '\\') | |
40 | continue; | |
41 | if (--buflen <= 0) | |
42 | break; | |
43 | *buffer++ = (char) c; | |
44 | continue; | |
45 | } | |
46 | if (!c) { | |
47 | if (--buflen <= 0) | |
48 | break; | |
49 | *buffer = '\0'; | |
50 | return 0; | |
51 | } | |
52 | buflen -= 4; | |
53 | if (buflen <= 0) | |
54 | break; | |
55 | *buffer++ = '\\'; | |
56 | *buffer++ = (c >> 6) + '0'; | |
57 | *buffer++ = ((c >> 3) & 7) + '0'; | |
58 | *buffer++ = (c & 7) + '0'; | |
59 | } | |
60 | return -ENOMEM; | |
61 | } | |
62 | ||
63 | /** | |
64 | * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root. | |
65 | * | |
66 | * @path: Pointer to "struct path". | |
67 | * @newname: Pointer to buffer to return value in. | |
68 | * @newname_len: Size of @newname. | |
69 | * | |
70 | * Returns 0 on success, negative value otherwise. | |
71 | * | |
72 | * If dentry is a directory, trailing '/' is appended. | |
73 | * Characters out of 0x20 < c < 0x7F range are converted to | |
74 | * \ooo style octal string. | |
75 | * Character \ is converted to \\ string. | |
76 | */ | |
77 | int tomoyo_realpath_from_path2(struct path *path, char *newname, | |
78 | int newname_len) | |
79 | { | |
80 | int error = -ENOMEM; | |
81 | struct dentry *dentry = path->dentry; | |
82 | char *sp; | |
83 | ||
84 | if (!dentry || !path->mnt || !newname || newname_len <= 2048) | |
85 | return -EINVAL; | |
86 | if (dentry->d_op && dentry->d_op->d_dname) { | |
87 | /* For "socket:[\$]" and "pipe:[\$]". */ | |
88 | static const int offset = 1536; | |
89 | sp = dentry->d_op->d_dname(dentry, newname + offset, | |
90 | newname_len - offset); | |
91 | } else { | |
92 | /* Taken from d_namespace_path(). */ | |
93 | struct path root; | |
94 | struct path ns_root = { }; | |
95 | struct path tmp; | |
96 | ||
97 | read_lock(¤t->fs->lock); | |
98 | root = current->fs->root; | |
99 | path_get(&root); | |
100 | read_unlock(¤t->fs->lock); | |
101 | spin_lock(&vfsmount_lock); | |
102 | if (root.mnt && root.mnt->mnt_ns) | |
103 | ns_root.mnt = mntget(root.mnt->mnt_ns->root); | |
104 | if (ns_root.mnt) | |
105 | ns_root.dentry = dget(ns_root.mnt->mnt_root); | |
106 | spin_unlock(&vfsmount_lock); | |
107 | spin_lock(&dcache_lock); | |
108 | tmp = ns_root; | |
109 | sp = __d_path(path, &tmp, newname, newname_len); | |
110 | spin_unlock(&dcache_lock); | |
111 | path_put(&root); | |
112 | path_put(&ns_root); | |
a4054b6b EB |
113 | /* Prepend "/proc" prefix if using internal proc vfs mount. */ |
114 | if (!IS_ERR(sp) && (path->mnt->mnt_parent == path->mnt) && | |
115 | (strcmp(path->mnt->mnt_sb->s_type->name, "proc") == 0)) { | |
116 | sp -= 5; | |
117 | if (sp >= newname) | |
118 | memcpy(sp, "/proc", 5); | |
119 | else | |
120 | sp = ERR_PTR(-ENOMEM); | |
121 | } | |
c73bd6d4 KT |
122 | } |
123 | if (IS_ERR(sp)) | |
124 | error = PTR_ERR(sp); | |
125 | else | |
126 | error = tomoyo_encode(newname, sp - newname, sp); | |
127 | /* Append trailing '/' if dentry is a directory. */ | |
128 | if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode) | |
129 | && *newname) { | |
130 | sp = newname + strlen(newname); | |
131 | if (*(sp - 1) != '/') { | |
132 | if (sp < newname + newname_len - 4) { | |
133 | *sp++ = '/'; | |
134 | *sp = '\0'; | |
135 | } else { | |
136 | error = -ENOMEM; | |
137 | } | |
138 | } | |
139 | } | |
140 | if (error) | |
141 | printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n"); | |
142 | return error; | |
143 | } | |
144 | ||
145 | /** | |
146 | * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root. | |
147 | * | |
148 | * @path: Pointer to "struct path". | |
149 | * | |
150 | * Returns the realpath of the given @path on success, NULL otherwise. | |
151 | * | |
152 | * These functions use tomoyo_alloc(), so the caller must call tomoyo_free() | |
153 | * if these functions didn't return NULL. | |
154 | */ | |
155 | char *tomoyo_realpath_from_path(struct path *path) | |
156 | { | |
157 | char *buf = tomoyo_alloc(sizeof(struct tomoyo_page_buffer)); | |
158 | ||
159 | BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer) | |
160 | <= TOMOYO_MAX_PATHNAME_LEN - 1); | |
161 | if (!buf) | |
162 | return NULL; | |
163 | if (tomoyo_realpath_from_path2(path, buf, | |
164 | TOMOYO_MAX_PATHNAME_LEN - 1) == 0) | |
165 | return buf; | |
166 | tomoyo_free(buf); | |
167 | return NULL; | |
168 | } | |
169 | ||
170 | /** | |
171 | * tomoyo_realpath - Get realpath of a pathname. | |
172 | * | |
173 | * @pathname: The pathname to solve. | |
174 | * | |
175 | * Returns the realpath of @pathname on success, NULL otherwise. | |
176 | */ | |
177 | char *tomoyo_realpath(const char *pathname) | |
178 | { | |
e24977d4 | 179 | struct path path; |
c73bd6d4 | 180 | |
e24977d4 AV |
181 | if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) { |
182 | char *buf = tomoyo_realpath_from_path(&path); | |
183 | path_put(&path); | |
c73bd6d4 KT |
184 | return buf; |
185 | } | |
186 | return NULL; | |
187 | } | |
188 | ||
189 | /** | |
190 | * tomoyo_realpath_nofollow - Get realpath of a pathname. | |
191 | * | |
192 | * @pathname: The pathname to solve. | |
193 | * | |
194 | * Returns the realpath of @pathname on success, NULL otherwise. | |
195 | */ | |
196 | char *tomoyo_realpath_nofollow(const char *pathname) | |
197 | { | |
e24977d4 | 198 | struct path path; |
c73bd6d4 | 199 | |
e24977d4 AV |
200 | if (pathname && kern_path(pathname, 0, &path) == 0) { |
201 | char *buf = tomoyo_realpath_from_path(&path); | |
202 | path_put(&path); | |
c73bd6d4 KT |
203 | return buf; |
204 | } | |
205 | return NULL; | |
206 | } | |
207 | ||
208 | /* Memory allocated for non-string data. */ | |
209 | static unsigned int tomoyo_allocated_memory_for_elements; | |
210 | /* Quota for holding non-string data. */ | |
211 | static unsigned int tomoyo_quota_for_elements; | |
212 | ||
213 | /** | |
214 | * tomoyo_alloc_element - Allocate permanent memory for structures. | |
215 | * | |
216 | * @size: Size in bytes. | |
217 | * | |
218 | * Returns pointer to allocated memory on success, NULL otherwise. | |
219 | * | |
220 | * Memory has to be zeroed. | |
221 | * The RAM is chunked, so NEVER try to kfree() the returned pointer. | |
222 | */ | |
223 | void *tomoyo_alloc_element(const unsigned int size) | |
224 | { | |
225 | static char *buf; | |
226 | static DEFINE_MUTEX(lock); | |
227 | static unsigned int buf_used_len = PATH_MAX; | |
228 | char *ptr = NULL; | |
229 | /*Assumes sizeof(void *) >= sizeof(long) is true. */ | |
230 | const unsigned int word_aligned_size | |
231 | = roundup(size, max(sizeof(void *), sizeof(long))); | |
232 | if (word_aligned_size > PATH_MAX) | |
233 | return NULL; | |
c73bd6d4 KT |
234 | mutex_lock(&lock); |
235 | if (buf_used_len + word_aligned_size > PATH_MAX) { | |
236 | if (!tomoyo_quota_for_elements || | |
237 | tomoyo_allocated_memory_for_elements | |
238 | + PATH_MAX <= tomoyo_quota_for_elements) | |
239 | ptr = kzalloc(PATH_MAX, GFP_KERNEL); | |
240 | if (!ptr) { | |
241 | printk(KERN_WARNING "ERROR: Out of memory " | |
242 | "for tomoyo_alloc_element().\n"); | |
243 | if (!tomoyo_policy_loaded) | |
244 | panic("MAC Initialization failed.\n"); | |
245 | } else { | |
246 | buf = ptr; | |
247 | tomoyo_allocated_memory_for_elements += PATH_MAX; | |
248 | buf_used_len = word_aligned_size; | |
249 | ptr = buf; | |
250 | } | |
251 | } else if (word_aligned_size) { | |
252 | int i; | |
253 | ptr = buf + buf_used_len; | |
254 | buf_used_len += word_aligned_size; | |
255 | for (i = 0; i < word_aligned_size; i++) { | |
256 | if (!ptr[i]) | |
257 | continue; | |
258 | printk(KERN_ERR "WARNING: Reserved memory was tainted! " | |
259 | "The system might go wrong.\n"); | |
260 | ptr[i] = '\0'; | |
261 | } | |
262 | } | |
263 | mutex_unlock(&lock); | |
c73bd6d4 KT |
264 | return ptr; |
265 | } | |
266 | ||
267 | /* Memory allocated for string data in bytes. */ | |
268 | static unsigned int tomoyo_allocated_memory_for_savename; | |
269 | /* Quota for holding string data in bytes. */ | |
270 | static unsigned int tomoyo_quota_for_savename; | |
271 | ||
272 | /* | |
273 | * TOMOYO uses this hash only when appending a string into the string | |
274 | * table. Frequency of appending strings is very low. So we don't need | |
275 | * large (e.g. 64k) hash size. 256 will be sufficient. | |
276 | */ | |
024e1a49 SH |
277 | #define TOMOYO_HASH_BITS 8 |
278 | #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS) | |
c73bd6d4 | 279 | |
c3fa109a TH |
280 | /* |
281 | * tomoyo_name_entry is a structure which is used for linking | |
282 | * "struct tomoyo_path_info" into tomoyo_name_list . | |
283 | * | |
284 | * Since tomoyo_name_list manages a list of strings which are shared by | |
285 | * multiple processes (whereas "struct tomoyo_path_info" inside | |
286 | * "struct tomoyo_path_info_with_data" is not shared), a reference counter will | |
287 | * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info" | |
288 | * when TOMOYO starts supporting garbage collector. | |
289 | */ | |
c73bd6d4 KT |
290 | struct tomoyo_name_entry { |
291 | struct list_head list; | |
292 | struct tomoyo_path_info entry; | |
293 | }; | |
294 | ||
295 | /* Structure for available memory region. */ | |
296 | struct tomoyo_free_memory_block_list { | |
297 | struct list_head list; | |
298 | char *ptr; /* Pointer to a free area. */ | |
299 | int len; /* Length of the area. */ | |
300 | }; | |
301 | ||
302 | /* | |
c3fa109a TH |
303 | * tomoyo_name_list is used for holding string data used by TOMOYO. |
304 | * Since same string data is likely used for multiple times (e.g. | |
305 | * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of | |
306 | * "const struct tomoyo_path_info *". | |
c73bd6d4 KT |
307 | */ |
308 | static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH]; | |
309 | ||
310 | /** | |
311 | * tomoyo_save_name - Allocate permanent memory for string data. | |
312 | * | |
313 | * @name: The string to store into the permernent memory. | |
314 | * | |
315 | * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise. | |
316 | * | |
317 | * The RAM is shared, so NEVER try to modify or kfree() the returned name. | |
318 | */ | |
319 | const struct tomoyo_path_info *tomoyo_save_name(const char *name) | |
320 | { | |
321 | static LIST_HEAD(fmb_list); | |
322 | static DEFINE_MUTEX(lock); | |
323 | struct tomoyo_name_entry *ptr; | |
324 | unsigned int hash; | |
325 | /* fmb contains available size in bytes. | |
326 | fmb is removed from the fmb_list when fmb->len becomes 0. */ | |
327 | struct tomoyo_free_memory_block_list *fmb; | |
328 | int len; | |
329 | char *cp; | |
024e1a49 | 330 | struct list_head *head; |
c73bd6d4 KT |
331 | |
332 | if (!name) | |
333 | return NULL; | |
334 | len = strlen(name) + 1; | |
335 | if (len > TOMOYO_MAX_PATHNAME_LEN) { | |
336 | printk(KERN_WARNING "ERROR: Name too long " | |
337 | "for tomoyo_save_name().\n"); | |
338 | return NULL; | |
339 | } | |
340 | hash = full_name_hash((const unsigned char *) name, len - 1); | |
024e1a49 SH |
341 | head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)]; |
342 | ||
c73bd6d4 | 343 | mutex_lock(&lock); |
024e1a49 | 344 | list_for_each_entry(ptr, head, list) { |
c73bd6d4 KT |
345 | if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name)) |
346 | goto out; | |
347 | } | |
348 | list_for_each_entry(fmb, &fmb_list, list) { | |
349 | if (len <= fmb->len) | |
350 | goto ready; | |
351 | } | |
352 | if (!tomoyo_quota_for_savename || | |
353 | tomoyo_allocated_memory_for_savename + PATH_MAX | |
354 | <= tomoyo_quota_for_savename) | |
355 | cp = kzalloc(PATH_MAX, GFP_KERNEL); | |
356 | else | |
357 | cp = NULL; | |
358 | fmb = kzalloc(sizeof(*fmb), GFP_KERNEL); | |
359 | if (!cp || !fmb) { | |
360 | kfree(cp); | |
361 | kfree(fmb); | |
362 | printk(KERN_WARNING "ERROR: Out of memory " | |
363 | "for tomoyo_save_name().\n"); | |
364 | if (!tomoyo_policy_loaded) | |
365 | panic("MAC Initialization failed.\n"); | |
366 | ptr = NULL; | |
367 | goto out; | |
368 | } | |
369 | tomoyo_allocated_memory_for_savename += PATH_MAX; | |
370 | list_add(&fmb->list, &fmb_list); | |
371 | fmb->ptr = cp; | |
372 | fmb->len = PATH_MAX; | |
373 | ready: | |
374 | ptr = tomoyo_alloc_element(sizeof(*ptr)); | |
375 | if (!ptr) | |
376 | goto out; | |
377 | ptr->entry.name = fmb->ptr; | |
378 | memmove(fmb->ptr, name, len); | |
379 | tomoyo_fill_path_info(&ptr->entry); | |
380 | fmb->ptr += len; | |
381 | fmb->len -= len; | |
024e1a49 | 382 | list_add_tail(&ptr->list, head); |
c73bd6d4 KT |
383 | if (fmb->len == 0) { |
384 | list_del(&fmb->list); | |
385 | kfree(fmb); | |
386 | } | |
387 | out: | |
388 | mutex_unlock(&lock); | |
c73bd6d4 KT |
389 | return ptr ? &ptr->entry : NULL; |
390 | } | |
391 | ||
392 | /** | |
393 | * tomoyo_realpath_init - Initialize realpath related code. | |
c73bd6d4 | 394 | */ |
1581e7dd | 395 | void __init tomoyo_realpath_init(void) |
c73bd6d4 KT |
396 | { |
397 | int i; | |
398 | ||
399 | BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX); | |
400 | for (i = 0; i < TOMOYO_MAX_HASH; i++) | |
401 | INIT_LIST_HEAD(&tomoyo_name_list[i]); | |
402 | INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list); | |
403 | tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME); | |
404 | list_add_tail(&tomoyo_kernel_domain.list, &tomoyo_domain_list); | |
405 | down_read(&tomoyo_domain_list_lock); | |
406 | if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain) | |
407 | panic("Can't register tomoyo_kernel_domain"); | |
408 | up_read(&tomoyo_domain_list_lock); | |
c73bd6d4 KT |
409 | } |
410 | ||
c73bd6d4 KT |
411 | /* Memory allocated for temporary purpose. */ |
412 | static atomic_t tomoyo_dynamic_memory_size; | |
413 | ||
414 | /** | |
415 | * tomoyo_alloc - Allocate memory for temporary purpose. | |
416 | * | |
417 | * @size: Size in bytes. | |
418 | * | |
419 | * Returns pointer to allocated memory on success, NULL otherwise. | |
420 | */ | |
421 | void *tomoyo_alloc(const size_t size) | |
422 | { | |
423 | void *p = kzalloc(size, GFP_KERNEL); | |
424 | if (p) | |
425 | atomic_add(ksize(p), &tomoyo_dynamic_memory_size); | |
426 | return p; | |
427 | } | |
428 | ||
429 | /** | |
430 | * tomoyo_free - Release memory allocated by tomoyo_alloc(). | |
431 | * | |
432 | * @p: Pointer returned by tomoyo_alloc(). May be NULL. | |
433 | * | |
434 | * Returns nothing. | |
435 | */ | |
436 | void tomoyo_free(const void *p) | |
437 | { | |
438 | if (p) { | |
439 | atomic_sub(ksize(p), &tomoyo_dynamic_memory_size); | |
440 | kfree(p); | |
441 | } | |
442 | } | |
443 | ||
444 | /** | |
445 | * tomoyo_read_memory_counter - Check for memory usage in bytes. | |
446 | * | |
447 | * @head: Pointer to "struct tomoyo_io_buffer". | |
448 | * | |
449 | * Returns memory usage. | |
450 | */ | |
451 | int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head) | |
452 | { | |
453 | if (!head->read_eof) { | |
454 | const unsigned int shared | |
455 | = tomoyo_allocated_memory_for_savename; | |
456 | const unsigned int private | |
457 | = tomoyo_allocated_memory_for_elements; | |
458 | const unsigned int dynamic | |
459 | = atomic_read(&tomoyo_dynamic_memory_size); | |
460 | char buffer[64]; | |
461 | ||
462 | memset(buffer, 0, sizeof(buffer)); | |
463 | if (tomoyo_quota_for_savename) | |
464 | snprintf(buffer, sizeof(buffer) - 1, | |
465 | " (Quota: %10u)", | |
466 | tomoyo_quota_for_savename); | |
467 | else | |
468 | buffer[0] = '\0'; | |
469 | tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer); | |
470 | if (tomoyo_quota_for_elements) | |
471 | snprintf(buffer, sizeof(buffer) - 1, | |
472 | " (Quota: %10u)", | |
473 | tomoyo_quota_for_elements); | |
474 | else | |
475 | buffer[0] = '\0'; | |
476 | tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer); | |
477 | tomoyo_io_printf(head, "Dynamic: %10u\n", dynamic); | |
478 | tomoyo_io_printf(head, "Total: %10u\n", | |
479 | shared + private + dynamic); | |
480 | head->read_eof = true; | |
481 | } | |
482 | return 0; | |
483 | } | |
484 | ||
485 | /** | |
486 | * tomoyo_write_memory_quota - Set memory quota. | |
487 | * | |
488 | * @head: Pointer to "struct tomoyo_io_buffer". | |
489 | * | |
490 | * Returns 0. | |
491 | */ | |
492 | int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head) | |
493 | { | |
494 | char *data = head->write_buf; | |
495 | unsigned int size; | |
496 | ||
497 | if (sscanf(data, "Shared: %u", &size) == 1) | |
498 | tomoyo_quota_for_savename = size; | |
499 | else if (sscanf(data, "Private: %u", &size) == 1) | |
500 | tomoyo_quota_for_elements = size; | |
501 | return 0; | |
502 | } |