2 * security/tomoyo/realpath.c
4 * Get the canonicalized absolute pathnames. The basis for TOMOYO.
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
8 * Version: 2.2.0-pre 2009/02/01
12 #include <linux/types.h>
13 #include <linux/mount.h>
14 #include <linux/mnt_namespace.h>
19 * tomoyo_encode: Convert binary string to ascii string.
21 * @buffer: Buffer for ASCII string.
22 * @buflen: Size of @buffer.
23 * @str: Binary string.
25 * Returns 0 on success, -ENOMEM otherwise.
27 int tomoyo_encode(char *buffer, int buflen, const char *str)
30 const unsigned char c = *(unsigned char *) str++;
32 if (tomoyo_is_valid(c)) {
53 *buffer++ = (c >> 6) + '0';
54 *buffer++ = ((c >> 3) & 7) + '0';
55 *buffer++ = (c & 7) + '0';
61 * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root.
63 * @path: Pointer to "struct path".
64 * @newname: Pointer to buffer to return value in.
65 * @newname_len: Size of @newname.
67 * Returns 0 on success, negative value otherwise.
69 * If dentry is a directory, trailing '/' is appended.
70 * Characters out of 0x20 < c < 0x7F range are converted to
71 * \ooo style octal string.
72 * Character \ is converted to \\ string.
74 int tomoyo_realpath_from_path2(struct path *path, char *newname,
78 struct dentry *dentry = path->dentry;
81 if (!dentry || !path->mnt || !newname || newname_len <= 2048)
83 if (dentry->d_op && dentry->d_op->d_dname) {
84 /* For "socket:[\$]" and "pipe:[\$]". */
85 static const int offset = 1536;
86 sp = dentry->d_op->d_dname(dentry, newname + offset,
87 newname_len - offset);
89 /* Taken from d_namespace_path(). */
91 struct path ns_root = { };
94 read_lock(¤t->fs->lock);
95 root = current->fs->root;
97 read_unlock(¤t->fs->lock);
98 spin_lock(&vfsmount_lock);
99 if (root.mnt && root.mnt->mnt_ns)
100 ns_root.mnt = mntget(root.mnt->mnt_ns->root);
102 ns_root.dentry = dget(ns_root.mnt->mnt_root);
103 spin_unlock(&vfsmount_lock);
104 spin_lock(&dcache_lock);
106 sp = __d_path(path, &tmp, newname, newname_len);
107 spin_unlock(&dcache_lock);
114 error = tomoyo_encode(newname, sp - newname, sp);
115 /* Append trailing '/' if dentry is a directory. */
116 if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
118 sp = newname + strlen(newname);
119 if (*(sp - 1) != '/') {
120 if (sp < newname + newname_len - 4) {
129 printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
134 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
136 * @path: Pointer to "struct path".
138 * Returns the realpath of the given @path on success, NULL otherwise.
140 * These functions use tomoyo_alloc(), so the caller must call tomoyo_free()
141 * if these functions didn't return NULL.
143 char *tomoyo_realpath_from_path(struct path *path)
145 char *buf = tomoyo_alloc(sizeof(struct tomoyo_page_buffer));
147 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
148 <= TOMOYO_MAX_PATHNAME_LEN - 1);
151 if (tomoyo_realpath_from_path2(path, buf,
152 TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
159 * tomoyo_realpath - Get realpath of a pathname.
161 * @pathname: The pathname to solve.
163 * Returns the realpath of @pathname on success, NULL otherwise.
165 char *tomoyo_realpath(const char *pathname)
169 if (pathname && path_lookup(pathname, LOOKUP_FOLLOW, &nd) == 0) {
170 char *buf = tomoyo_realpath_from_path(&nd.path);
178 * tomoyo_realpath_nofollow - Get realpath of a pathname.
180 * @pathname: The pathname to solve.
182 * Returns the realpath of @pathname on success, NULL otherwise.
184 char *tomoyo_realpath_nofollow(const char *pathname)
188 if (pathname && path_lookup(pathname, 0, &nd) == 0) {
189 char *buf = tomoyo_realpath_from_path(&nd.path);
196 /* Memory allocated for non-string data. */
197 static unsigned int tomoyo_allocated_memory_for_elements;
198 /* Quota for holding non-string data. */
199 static unsigned int tomoyo_quota_for_elements;
202 * tomoyo_alloc_element - Allocate permanent memory for structures.
204 * @size: Size in bytes.
206 * Returns pointer to allocated memory on success, NULL otherwise.
208 * Memory has to be zeroed.
209 * The RAM is chunked, so NEVER try to kfree() the returned pointer.
211 void *tomoyo_alloc_element(const unsigned int size)
214 static DEFINE_MUTEX(lock);
215 static unsigned int buf_used_len = PATH_MAX;
217 /*Assumes sizeof(void *) >= sizeof(long) is true. */
218 const unsigned int word_aligned_size
219 = roundup(size, max(sizeof(void *), sizeof(long)));
220 if (word_aligned_size > PATH_MAX)
222 /***** EXCLUSIVE SECTION START *****/
224 if (buf_used_len + word_aligned_size > PATH_MAX) {
225 if (!tomoyo_quota_for_elements ||
226 tomoyo_allocated_memory_for_elements
227 + PATH_MAX <= tomoyo_quota_for_elements)
228 ptr = kzalloc(PATH_MAX, GFP_KERNEL);
230 printk(KERN_WARNING "ERROR: Out of memory "
231 "for tomoyo_alloc_element().\n");
232 if (!tomoyo_policy_loaded)
233 panic("MAC Initialization failed.\n");
236 tomoyo_allocated_memory_for_elements += PATH_MAX;
237 buf_used_len = word_aligned_size;
240 } else if (word_aligned_size) {
242 ptr = buf + buf_used_len;
243 buf_used_len += word_aligned_size;
244 for (i = 0; i < word_aligned_size; i++) {
247 printk(KERN_ERR "WARNING: Reserved memory was tainted! "
248 "The system might go wrong.\n");
253 /***** EXCLUSIVE SECTION END *****/
257 /* Memory allocated for string data in bytes. */
258 static unsigned int tomoyo_allocated_memory_for_savename;
259 /* Quota for holding string data in bytes. */
260 static unsigned int tomoyo_quota_for_savename;
263 * TOMOYO uses this hash only when appending a string into the string
264 * table. Frequency of appending strings is very low. So we don't need
265 * large (e.g. 64k) hash size. 256 will be sufficient.
267 #define TOMOYO_MAX_HASH 256
269 /* Structure for string data. */
270 struct tomoyo_name_entry {
271 struct list_head list;
272 struct tomoyo_path_info entry;
275 /* Structure for available memory region. */
276 struct tomoyo_free_memory_block_list {
277 struct list_head list;
278 char *ptr; /* Pointer to a free area. */
279 int len; /* Length of the area. */
283 * The list for "struct tomoyo_name_entry".
285 * This list is updated only inside tomoyo_save_name(), thus
286 * no global mutex exists.
288 static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
291 * tomoyo_save_name - Allocate permanent memory for string data.
293 * @name: The string to store into the permernent memory.
295 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
297 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
299 const struct tomoyo_path_info *tomoyo_save_name(const char *name)
301 static LIST_HEAD(fmb_list);
302 static DEFINE_MUTEX(lock);
303 struct tomoyo_name_entry *ptr;
305 /* fmb contains available size in bytes.
306 fmb is removed from the fmb_list when fmb->len becomes 0. */
307 struct tomoyo_free_memory_block_list *fmb;
313 len = strlen(name) + 1;
314 if (len > TOMOYO_MAX_PATHNAME_LEN) {
315 printk(KERN_WARNING "ERROR: Name too long "
316 "for tomoyo_save_name().\n");
319 hash = full_name_hash((const unsigned char *) name, len - 1);
320 /***** EXCLUSIVE SECTION START *****/
322 list_for_each_entry(ptr, &tomoyo_name_list[hash % TOMOYO_MAX_HASH],
324 if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
327 list_for_each_entry(fmb, &fmb_list, list) {
331 if (!tomoyo_quota_for_savename ||
332 tomoyo_allocated_memory_for_savename + PATH_MAX
333 <= tomoyo_quota_for_savename)
334 cp = kzalloc(PATH_MAX, GFP_KERNEL);
337 fmb = kzalloc(sizeof(*fmb), GFP_KERNEL);
341 printk(KERN_WARNING "ERROR: Out of memory "
342 "for tomoyo_save_name().\n");
343 if (!tomoyo_policy_loaded)
344 panic("MAC Initialization failed.\n");
348 tomoyo_allocated_memory_for_savename += PATH_MAX;
349 list_add(&fmb->list, &fmb_list);
353 ptr = tomoyo_alloc_element(sizeof(*ptr));
356 ptr->entry.name = fmb->ptr;
357 memmove(fmb->ptr, name, len);
358 tomoyo_fill_path_info(&ptr->entry);
361 list_add_tail(&ptr->list, &tomoyo_name_list[hash % TOMOYO_MAX_HASH]);
363 list_del(&fmb->list);
368 /***** EXCLUSIVE SECTION END *****/
369 return ptr ? &ptr->entry : NULL;
373 * tomoyo_realpath_init - Initialize realpath related code.
375 void __init tomoyo_realpath_init(void)
379 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
380 for (i = 0; i < TOMOYO_MAX_HASH; i++)
381 INIT_LIST_HEAD(&tomoyo_name_list[i]);
382 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
383 tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME);
384 list_add_tail(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
385 down_read(&tomoyo_domain_list_lock);
386 if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
387 panic("Can't register tomoyo_kernel_domain");
388 up_read(&tomoyo_domain_list_lock);
391 /* Memory allocated for temporary purpose. */
392 static atomic_t tomoyo_dynamic_memory_size;
395 * tomoyo_alloc - Allocate memory for temporary purpose.
397 * @size: Size in bytes.
399 * Returns pointer to allocated memory on success, NULL otherwise.
401 void *tomoyo_alloc(const size_t size)
403 void *p = kzalloc(size, GFP_KERNEL);
405 atomic_add(ksize(p), &tomoyo_dynamic_memory_size);
410 * tomoyo_free - Release memory allocated by tomoyo_alloc().
412 * @p: Pointer returned by tomoyo_alloc(). May be NULL.
416 void tomoyo_free(const void *p)
419 atomic_sub(ksize(p), &tomoyo_dynamic_memory_size);
425 * tomoyo_read_memory_counter - Check for memory usage in bytes.
427 * @head: Pointer to "struct tomoyo_io_buffer".
429 * Returns memory usage.
431 int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
433 if (!head->read_eof) {
434 const unsigned int shared
435 = tomoyo_allocated_memory_for_savename;
436 const unsigned int private
437 = tomoyo_allocated_memory_for_elements;
438 const unsigned int dynamic
439 = atomic_read(&tomoyo_dynamic_memory_size);
442 memset(buffer, 0, sizeof(buffer));
443 if (tomoyo_quota_for_savename)
444 snprintf(buffer, sizeof(buffer) - 1,
446 tomoyo_quota_for_savename);
449 tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
450 if (tomoyo_quota_for_elements)
451 snprintf(buffer, sizeof(buffer) - 1,
453 tomoyo_quota_for_elements);
456 tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
457 tomoyo_io_printf(head, "Dynamic: %10u\n", dynamic);
458 tomoyo_io_printf(head, "Total: %10u\n",
459 shared + private + dynamic);
460 head->read_eof = true;
466 * tomoyo_write_memory_quota - Set memory quota.
468 * @head: Pointer to "struct tomoyo_io_buffer".
472 int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
474 char *data = head->write_buf;
477 if (sscanf(data, "Shared: %u", &size) == 1)
478 tomoyo_quota_for_savename = size;
479 else if (sscanf(data, "Private: %u", &size) == 1)
480 tomoyo_quota_for_elements = size;