blob: 917f564cdab16bbc6daa73215ee8a39cdd559bb6 [file] [log] [blame]
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09001/*
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 *
Tetsuo Handa39826a12009-04-08 22:31:28 +09008 * Version: 2.2.0 2009/04/01
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09009 *
10 */
11
12#include <linux/types.h>
13#include <linux/mount.h>
14#include <linux/mnt_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040015#include <linux/fs_struct.h>
Stephen Hemminger024e1a42009-10-27 19:24:46 -070016#include <linux/hash.h>
17
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090018#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 */
30int 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 */
77int 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(&current->fs->lock);
98 root = current->fs->root;
99 path_get(&root);
100 read_unlock(&current->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);
113 }
114 if (IS_ERR(sp))
115 error = PTR_ERR(sp);
116 else
117 error = tomoyo_encode(newname, sp - newname, sp);
118 /* Append trailing '/' if dentry is a directory. */
119 if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
120 && *newname) {
121 sp = newname + strlen(newname);
122 if (*(sp - 1) != '/') {
123 if (sp < newname + newname_len - 4) {
124 *sp++ = '/';
125 *sp = '\0';
126 } else {
127 error = -ENOMEM;
128 }
129 }
130 }
131 if (error)
132 printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
133 return error;
134}
135
136/**
137 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
138 *
139 * @path: Pointer to "struct path".
140 *
141 * Returns the realpath of the given @path on success, NULL otherwise.
142 *
143 * These functions use tomoyo_alloc(), so the caller must call tomoyo_free()
144 * if these functions didn't return NULL.
145 */
146char *tomoyo_realpath_from_path(struct path *path)
147{
148 char *buf = tomoyo_alloc(sizeof(struct tomoyo_page_buffer));
149
150 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
151 <= TOMOYO_MAX_PATHNAME_LEN - 1);
152 if (!buf)
153 return NULL;
154 if (tomoyo_realpath_from_path2(path, buf,
155 TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
156 return buf;
157 tomoyo_free(buf);
158 return NULL;
159}
160
161/**
162 * tomoyo_realpath - Get realpath of a pathname.
163 *
164 * @pathname: The pathname to solve.
165 *
166 * Returns the realpath of @pathname on success, NULL otherwise.
167 */
168char *tomoyo_realpath(const char *pathname)
169{
Al Viroe24977d2009-04-02 21:17:03 -0400170 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900171
Al Viroe24977d2009-04-02 21:17:03 -0400172 if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
173 char *buf = tomoyo_realpath_from_path(&path);
174 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900175 return buf;
176 }
177 return NULL;
178}
179
180/**
181 * tomoyo_realpath_nofollow - Get realpath of a pathname.
182 *
183 * @pathname: The pathname to solve.
184 *
185 * Returns the realpath of @pathname on success, NULL otherwise.
186 */
187char *tomoyo_realpath_nofollow(const char *pathname)
188{
Al Viroe24977d2009-04-02 21:17:03 -0400189 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900190
Al Viroe24977d2009-04-02 21:17:03 -0400191 if (pathname && kern_path(pathname, 0, &path) == 0) {
192 char *buf = tomoyo_realpath_from_path(&path);
193 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900194 return buf;
195 }
196 return NULL;
197}
198
199/* Memory allocated for non-string data. */
200static unsigned int tomoyo_allocated_memory_for_elements;
201/* Quota for holding non-string data. */
202static unsigned int tomoyo_quota_for_elements;
203
204/**
205 * tomoyo_alloc_element - Allocate permanent memory for structures.
206 *
207 * @size: Size in bytes.
208 *
209 * Returns pointer to allocated memory on success, NULL otherwise.
210 *
211 * Memory has to be zeroed.
212 * The RAM is chunked, so NEVER try to kfree() the returned pointer.
213 */
214void *tomoyo_alloc_element(const unsigned int size)
215{
216 static char *buf;
217 static DEFINE_MUTEX(lock);
218 static unsigned int buf_used_len = PATH_MAX;
219 char *ptr = NULL;
220 /*Assumes sizeof(void *) >= sizeof(long) is true. */
221 const unsigned int word_aligned_size
222 = roundup(size, max(sizeof(void *), sizeof(long)));
223 if (word_aligned_size > PATH_MAX)
224 return NULL;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900225 mutex_lock(&lock);
226 if (buf_used_len + word_aligned_size > PATH_MAX) {
227 if (!tomoyo_quota_for_elements ||
228 tomoyo_allocated_memory_for_elements
229 + PATH_MAX <= tomoyo_quota_for_elements)
230 ptr = kzalloc(PATH_MAX, GFP_KERNEL);
231 if (!ptr) {
232 printk(KERN_WARNING "ERROR: Out of memory "
233 "for tomoyo_alloc_element().\n");
234 if (!tomoyo_policy_loaded)
235 panic("MAC Initialization failed.\n");
236 } else {
237 buf = ptr;
238 tomoyo_allocated_memory_for_elements += PATH_MAX;
239 buf_used_len = word_aligned_size;
240 ptr = buf;
241 }
242 } else if (word_aligned_size) {
243 int i;
244 ptr = buf + buf_used_len;
245 buf_used_len += word_aligned_size;
246 for (i = 0; i < word_aligned_size; i++) {
247 if (!ptr[i])
248 continue;
249 printk(KERN_ERR "WARNING: Reserved memory was tainted! "
250 "The system might go wrong.\n");
251 ptr[i] = '\0';
252 }
253 }
254 mutex_unlock(&lock);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900255 return ptr;
256}
257
258/* Memory allocated for string data in bytes. */
259static unsigned int tomoyo_allocated_memory_for_savename;
260/* Quota for holding string data in bytes. */
261static unsigned int tomoyo_quota_for_savename;
262
263/*
264 * TOMOYO uses this hash only when appending a string into the string
265 * table. Frequency of appending strings is very low. So we don't need
266 * large (e.g. 64k) hash size. 256 will be sufficient.
267 */
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700268#define TOMOYO_HASH_BITS 8
269#define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900270
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900271/*
272 * tomoyo_name_entry is a structure which is used for linking
273 * "struct tomoyo_path_info" into tomoyo_name_list .
274 *
275 * Since tomoyo_name_list manages a list of strings which are shared by
276 * multiple processes (whereas "struct tomoyo_path_info" inside
277 * "struct tomoyo_path_info_with_data" is not shared), a reference counter will
278 * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
279 * when TOMOYO starts supporting garbage collector.
280 */
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900281struct tomoyo_name_entry {
282 struct list_head list;
283 struct tomoyo_path_info entry;
284};
285
286/* Structure for available memory region. */
287struct tomoyo_free_memory_block_list {
288 struct list_head list;
289 char *ptr; /* Pointer to a free area. */
290 int len; /* Length of the area. */
291};
292
293/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900294 * tomoyo_name_list is used for holding string data used by TOMOYO.
295 * Since same string data is likely used for multiple times (e.g.
296 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
297 * "const struct tomoyo_path_info *".
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900298 */
299static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
300
301/**
302 * tomoyo_save_name - Allocate permanent memory for string data.
303 *
304 * @name: The string to store into the permernent memory.
305 *
306 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
307 *
308 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
309 */
310const struct tomoyo_path_info *tomoyo_save_name(const char *name)
311{
312 static LIST_HEAD(fmb_list);
313 static DEFINE_MUTEX(lock);
314 struct tomoyo_name_entry *ptr;
315 unsigned int hash;
316 /* fmb contains available size in bytes.
317 fmb is removed from the fmb_list when fmb->len becomes 0. */
318 struct tomoyo_free_memory_block_list *fmb;
319 int len;
320 char *cp;
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700321 struct list_head *head;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900322
323 if (!name)
324 return NULL;
325 len = strlen(name) + 1;
326 if (len > TOMOYO_MAX_PATHNAME_LEN) {
327 printk(KERN_WARNING "ERROR: Name too long "
328 "for tomoyo_save_name().\n");
329 return NULL;
330 }
331 hash = full_name_hash((const unsigned char *) name, len - 1);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700332 head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
333
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900334 mutex_lock(&lock);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700335 list_for_each_entry(ptr, head, list) {
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900336 if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
337 goto out;
338 }
339 list_for_each_entry(fmb, &fmb_list, list) {
340 if (len <= fmb->len)
341 goto ready;
342 }
343 if (!tomoyo_quota_for_savename ||
344 tomoyo_allocated_memory_for_savename + PATH_MAX
345 <= tomoyo_quota_for_savename)
346 cp = kzalloc(PATH_MAX, GFP_KERNEL);
347 else
348 cp = NULL;
349 fmb = kzalloc(sizeof(*fmb), GFP_KERNEL);
350 if (!cp || !fmb) {
351 kfree(cp);
352 kfree(fmb);
353 printk(KERN_WARNING "ERROR: Out of memory "
354 "for tomoyo_save_name().\n");
355 if (!tomoyo_policy_loaded)
356 panic("MAC Initialization failed.\n");
357 ptr = NULL;
358 goto out;
359 }
360 tomoyo_allocated_memory_for_savename += PATH_MAX;
361 list_add(&fmb->list, &fmb_list);
362 fmb->ptr = cp;
363 fmb->len = PATH_MAX;
364 ready:
365 ptr = tomoyo_alloc_element(sizeof(*ptr));
366 if (!ptr)
367 goto out;
368 ptr->entry.name = fmb->ptr;
369 memmove(fmb->ptr, name, len);
370 tomoyo_fill_path_info(&ptr->entry);
371 fmb->ptr += len;
372 fmb->len -= len;
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700373 list_add_tail(&ptr->list, head);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900374 if (fmb->len == 0) {
375 list_del(&fmb->list);
376 kfree(fmb);
377 }
378 out:
379 mutex_unlock(&lock);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900380 return ptr ? &ptr->entry : NULL;
381}
382
383/**
384 * tomoyo_realpath_init - Initialize realpath related code.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900385 */
Tetsuo Handa1581e7d2009-02-21 20:40:50 +0900386void __init tomoyo_realpath_init(void)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900387{
388 int i;
389
390 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
391 for (i = 0; i < TOMOYO_MAX_HASH; i++)
392 INIT_LIST_HEAD(&tomoyo_name_list[i]);
393 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
394 tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME);
395 list_add_tail(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
396 down_read(&tomoyo_domain_list_lock);
397 if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
398 panic("Can't register tomoyo_kernel_domain");
399 up_read(&tomoyo_domain_list_lock);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900400}
401
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900402/* Memory allocated for temporary purpose. */
403static atomic_t tomoyo_dynamic_memory_size;
404
405/**
406 * tomoyo_alloc - Allocate memory for temporary purpose.
407 *
408 * @size: Size in bytes.
409 *
410 * Returns pointer to allocated memory on success, NULL otherwise.
411 */
412void *tomoyo_alloc(const size_t size)
413{
414 void *p = kzalloc(size, GFP_KERNEL);
415 if (p)
416 atomic_add(ksize(p), &tomoyo_dynamic_memory_size);
417 return p;
418}
419
420/**
421 * tomoyo_free - Release memory allocated by tomoyo_alloc().
422 *
423 * @p: Pointer returned by tomoyo_alloc(). May be NULL.
424 *
425 * Returns nothing.
426 */
427void tomoyo_free(const void *p)
428{
429 if (p) {
430 atomic_sub(ksize(p), &tomoyo_dynamic_memory_size);
431 kfree(p);
432 }
433}
434
435/**
436 * tomoyo_read_memory_counter - Check for memory usage in bytes.
437 *
438 * @head: Pointer to "struct tomoyo_io_buffer".
439 *
440 * Returns memory usage.
441 */
442int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
443{
444 if (!head->read_eof) {
445 const unsigned int shared
446 = tomoyo_allocated_memory_for_savename;
447 const unsigned int private
448 = tomoyo_allocated_memory_for_elements;
449 const unsigned int dynamic
450 = atomic_read(&tomoyo_dynamic_memory_size);
451 char buffer[64];
452
453 memset(buffer, 0, sizeof(buffer));
454 if (tomoyo_quota_for_savename)
455 snprintf(buffer, sizeof(buffer) - 1,
456 " (Quota: %10u)",
457 tomoyo_quota_for_savename);
458 else
459 buffer[0] = '\0';
460 tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
461 if (tomoyo_quota_for_elements)
462 snprintf(buffer, sizeof(buffer) - 1,
463 " (Quota: %10u)",
464 tomoyo_quota_for_elements);
465 else
466 buffer[0] = '\0';
467 tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
468 tomoyo_io_printf(head, "Dynamic: %10u\n", dynamic);
469 tomoyo_io_printf(head, "Total: %10u\n",
470 shared + private + dynamic);
471 head->read_eof = true;
472 }
473 return 0;
474}
475
476/**
477 * tomoyo_write_memory_quota - Set memory quota.
478 *
479 * @head: Pointer to "struct tomoyo_io_buffer".
480 *
481 * Returns 0.
482 */
483int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
484{
485 char *data = head->write_buf;
486 unsigned int size;
487
488 if (sscanf(data, "Shared: %u", &size) == 1)
489 tomoyo_quota_for_savename = size;
490 else if (sscanf(data, "Private: %u", &size) == 1)
491 tomoyo_quota_for_elements = size;
492 return 0;
493}