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 2009/04/01
12 #include <linux/types.h>
13 #include <linux/mount.h>
14 #include <linux/mnt_namespace.h>
15 #include <linux/fs_struct.h>
16 #include <linux/hash.h>
17 #include <linux/magic.h>
23 * tomoyo_encode: Convert binary string to ascii string.
25 * @buffer: Buffer for ASCII string.
26 * @buflen: Size of @buffer.
27 * @str: Binary string.
29 * Returns 0 on success, -ENOMEM otherwise.
31 int tomoyo_encode(char *buffer, int buflen, const char *str)
34 const unsigned char c = *(unsigned char *) str++;
36 if (tomoyo_is_valid(c)) {
57 *buffer++ = (c >> 6) + '0';
58 *buffer++ = ((c >> 3) & 7) + '0';
59 *buffer++ = (c & 7) + '0';
65 * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root.
67 * @path: Pointer to "struct path".
68 * @newname: Pointer to buffer to return value in.
69 * @newname_len: Size of @newname.
71 * Returns 0 on success, negative value otherwise.
73 * If dentry is a directory, trailing '/' is appended.
74 * Characters out of 0x20 < c < 0x7F range are converted to
75 * \ooo style octal string.
76 * Character \ is converted to \\ string.
78 int tomoyo_realpath_from_path2(struct path *path, char *newname,
82 struct dentry *dentry = path->dentry;
85 if (!dentry || !path->mnt || !newname || newname_len <= 2048)
87 if (dentry->d_op && dentry->d_op->d_dname) {
88 /* For "socket:[\$]" and "pipe:[\$]". */
89 static const int offset = 1536;
90 sp = dentry->d_op->d_dname(dentry, newname + offset,
91 newname_len - offset);
93 /* Taken from d_namespace_path(). */
95 struct path ns_root = { };
98 read_lock(¤t->fs->lock);
99 root = current->fs->root;
101 read_unlock(¤t->fs->lock);
102 spin_lock(&vfsmount_lock);
103 if (root.mnt && root.mnt->mnt_ns)
104 ns_root.mnt = mntget(root.mnt->mnt_ns->root);
106 ns_root.dentry = dget(ns_root.mnt->mnt_root);
107 spin_unlock(&vfsmount_lock);
108 spin_lock(&dcache_lock);
110 sp = __d_path(path, &tmp, newname, newname_len);
111 spin_unlock(&dcache_lock);
114 /* Prepend "/proc" prefix if using internal proc vfs mount. */
115 if (!IS_ERR(sp) && (path->mnt->mnt_parent == path->mnt) &&
116 (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) {
119 memcpy(sp, "/proc", 5);
121 sp = ERR_PTR(-ENOMEM);
127 error = tomoyo_encode(newname, sp - newname, sp);
128 /* Append trailing '/' if dentry is a directory. */
129 if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
131 sp = newname + strlen(newname);
132 if (*(sp - 1) != '/') {
133 if (sp < newname + newname_len - 4) {
142 printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
147 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
149 * @path: Pointer to "struct path".
151 * Returns the realpath of the given @path on success, NULL otherwise.
153 * These functions use kzalloc(), so the caller must call kfree()
154 * if these functions didn't return NULL.
156 char *tomoyo_realpath_from_path(struct path *path)
158 char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_KERNEL);
160 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
161 <= TOMOYO_MAX_PATHNAME_LEN - 1);
164 if (tomoyo_realpath_from_path2(path, buf,
165 TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
172 * tomoyo_realpath - Get realpath of a pathname.
174 * @pathname: The pathname to solve.
176 * Returns the realpath of @pathname on success, NULL otherwise.
178 char *tomoyo_realpath(const char *pathname)
182 if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
183 char *buf = tomoyo_realpath_from_path(&path);
191 * tomoyo_realpath_nofollow - Get realpath of a pathname.
193 * @pathname: The pathname to solve.
195 * Returns the realpath of @pathname on success, NULL otherwise.
197 char *tomoyo_realpath_nofollow(const char *pathname)
201 if (pathname && kern_path(pathname, 0, &path) == 0) {
202 char *buf = tomoyo_realpath_from_path(&path);
209 /* Memory allocated for non-string data. */
210 static unsigned int tomoyo_allocated_memory_for_elements;
211 /* Quota for holding non-string data. */
212 static unsigned int tomoyo_quota_for_elements;
215 * tomoyo_memory_ok - Check memory quota.
217 * @ptr: Pointer to allocated memory.
219 * Returns true on success, false otherwise.
221 * Caller holds tomoyo_policy_lock.
222 * Memory pointed by @ptr will be zeroed on success.
224 bool tomoyo_memory_ok(void *ptr)
226 int allocated_len = ptr ? ksize(ptr) : 0;
228 if (!ptr || (tomoyo_quota_for_elements &&
229 tomoyo_allocated_memory_for_elements
230 + allocated_len > tomoyo_quota_for_elements)) {
231 printk(KERN_WARNING "ERROR: Out of memory "
232 "for tomoyo_alloc_element().\n");
233 if (!tomoyo_policy_loaded)
234 panic("MAC Initialization failed.\n");
237 tomoyo_allocated_memory_for_elements += allocated_len;
238 memset(ptr, 0, allocated_len);
243 /* Memory allocated for string data in bytes. */
244 static unsigned int tomoyo_allocated_memory_for_savename;
245 /* Quota for holding string data in bytes. */
246 static unsigned int tomoyo_quota_for_savename;
249 * TOMOYO uses this hash only when appending a string into the string
250 * table. Frequency of appending strings is very low. So we don't need
251 * large (e.g. 64k) hash size. 256 will be sufficient.
253 #define TOMOYO_HASH_BITS 8
254 #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
257 * tomoyo_name_list is used for holding string data used by TOMOYO.
258 * Since same string data is likely used for multiple times (e.g.
259 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
260 * "const struct tomoyo_path_info *".
262 static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
265 * tomoyo_get_name - Allocate permanent memory for string data.
267 * @name: The string to store into the permernent memory.
269 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
271 const struct tomoyo_path_info *tomoyo_get_name(const char *name)
273 static DEFINE_MUTEX(lock);
274 struct tomoyo_name_entry *ptr;
278 struct list_head *head;
282 len = strlen(name) + 1;
283 hash = full_name_hash((const unsigned char *) name, len - 1);
284 head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
286 list_for_each_entry(ptr, head, list) {
287 if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
289 atomic_inc(&ptr->users);
292 ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
293 allocated_len = ptr ? ksize(ptr) : 0;
294 if (!ptr || (tomoyo_quota_for_savename &&
295 tomoyo_allocated_memory_for_savename + allocated_len
296 > tomoyo_quota_for_savename)) {
298 printk(KERN_WARNING "ERROR: Out of memory "
299 "for tomoyo_get_name().\n");
300 if (!tomoyo_policy_loaded)
301 panic("MAC Initialization failed.\n");
305 tomoyo_allocated_memory_for_savename += allocated_len;
306 ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
307 memmove((char *) ptr->entry.name, name, len);
308 atomic_set(&ptr->users, 1);
309 tomoyo_fill_path_info(&ptr->entry);
310 list_add_tail(&ptr->list, head);
313 return ptr ? &ptr->entry : NULL;
317 * tomoyo_realpath_init - Initialize realpath related code.
319 void __init tomoyo_realpath_init(void)
323 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
324 for (i = 0; i < TOMOYO_MAX_HASH; i++)
325 INIT_LIST_HEAD(&tomoyo_name_list[i]);
326 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
327 tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
329 * tomoyo_read_lock() is not needed because this function is
330 * called before the first "delete" request.
332 list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
333 if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
334 panic("Can't register tomoyo_kernel_domain");
338 * tomoyo_read_memory_counter - Check for memory usage in bytes.
340 * @head: Pointer to "struct tomoyo_io_buffer".
342 * Returns memory usage.
344 int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
346 if (!head->read_eof) {
347 const unsigned int shared
348 = tomoyo_allocated_memory_for_savename;
349 const unsigned int private
350 = tomoyo_allocated_memory_for_elements;
353 memset(buffer, 0, sizeof(buffer));
354 if (tomoyo_quota_for_savename)
355 snprintf(buffer, sizeof(buffer) - 1,
357 tomoyo_quota_for_savename);
360 tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
361 if (tomoyo_quota_for_elements)
362 snprintf(buffer, sizeof(buffer) - 1,
364 tomoyo_quota_for_elements);
367 tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
368 tomoyo_io_printf(head, "Total: %10u\n", shared + private);
369 head->read_eof = true;
375 * tomoyo_write_memory_quota - Set memory quota.
377 * @head: Pointer to "struct tomoyo_io_buffer".
381 int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
383 char *data = head->write_buf;
386 if (sscanf(data, "Shared: %u", &size) == 1)
387 tomoyo_quota_for_savename = size;
388 else if (sscanf(data, "Private: %u", &size) == 1)
389 tomoyo_quota_for_elements = size;