blob: d1b96f0196216cb23f9da450ac75413b6ffd69c6 [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>
Tetsuo Handa67fa4882009-12-09 15:36:04 +090017#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090019#include "common.h"
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090020
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 {
Al Viro37afdc72010-02-05 01:41:33 -050092 struct path ns_root = {.mnt = NULL, .dentry = NULL};
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090093
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090094 spin_lock(&dcache_lock);
Al Viro37afdc72010-02-05 01:41:33 -050095 /* go to whatever namespace root we are under */
96 sp = __d_path(path, &ns_root, newname, newname_len);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090097 spin_unlock(&dcache_lock);
Eric W. Biedermana4054b62009-11-20 09:12:22 -080098 /* Prepend "/proc" prefix if using internal proc vfs mount. */
Al Viro440b3c62010-02-05 09:37:21 -050099 if (!IS_ERR(sp) && (path->mnt->mnt_flags & MNT_INTERNAL) &&
Tetsuo Handa67fa4882009-12-09 15:36:04 +0900100 (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) {
Eric W. Biedermana4054b62009-11-20 09:12:22 -0800101 sp -= 5;
102 if (sp >= newname)
103 memcpy(sp, "/proc", 5);
104 else
105 sp = ERR_PTR(-ENOMEM);
106 }
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900107 }
108 if (IS_ERR(sp))
109 error = PTR_ERR(sp);
110 else
111 error = tomoyo_encode(newname, sp - newname, sp);
112 /* Append trailing '/' if dentry is a directory. */
113 if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
114 && *newname) {
115 sp = newname + strlen(newname);
116 if (*(sp - 1) != '/') {
117 if (sp < newname + newname_len - 4) {
118 *sp++ = '/';
119 *sp = '\0';
120 } else {
121 error = -ENOMEM;
122 }
123 }
124 }
125 if (error)
126 printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
127 return error;
128}
129
130/**
131 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
132 *
133 * @path: Pointer to "struct path".
134 *
135 * Returns the realpath of the given @path on success, NULL otherwise.
136 *
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900137 * These functions use kzalloc(), so the caller must call kfree()
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900138 * if these functions didn't return NULL.
139 */
140char *tomoyo_realpath_from_path(struct path *path)
141{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900142 char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_NOFS);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900143
144 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
145 <= TOMOYO_MAX_PATHNAME_LEN - 1);
146 if (!buf)
147 return NULL;
148 if (tomoyo_realpath_from_path2(path, buf,
149 TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
150 return buf;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900151 kfree(buf);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900152 return NULL;
153}
154
155/**
156 * tomoyo_realpath - Get realpath of a pathname.
157 *
158 * @pathname: The pathname to solve.
159 *
160 * Returns the realpath of @pathname on success, NULL otherwise.
161 */
162char *tomoyo_realpath(const char *pathname)
163{
Al Viroe24977d2009-04-02 21:17:03 -0400164 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900165
Al Viroe24977d2009-04-02 21:17:03 -0400166 if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
167 char *buf = tomoyo_realpath_from_path(&path);
168 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900169 return buf;
170 }
171 return NULL;
172}
173
174/**
175 * tomoyo_realpath_nofollow - Get realpath of a pathname.
176 *
177 * @pathname: The pathname to solve.
178 *
179 * Returns the realpath of @pathname on success, NULL otherwise.
180 */
181char *tomoyo_realpath_nofollow(const char *pathname)
182{
Al Viroe24977d2009-04-02 21:17:03 -0400183 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900184
Al Viroe24977d2009-04-02 21:17:03 -0400185 if (pathname && kern_path(pathname, 0, &path) == 0) {
186 char *buf = tomoyo_realpath_from_path(&path);
187 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900188 return buf;
189 }
190 return NULL;
191}
192
193/* Memory allocated for non-string data. */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900194static atomic_t tomoyo_policy_memory_size;
195/* Quota for holding policy. */
196static unsigned int tomoyo_quota_for_policy;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900197
198/**
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900199 * tomoyo_memory_ok - Check memory quota.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900200 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900201 * @ptr: Pointer to allocated memory.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900202 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900203 * Returns true on success, false otherwise.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900204 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900205 * Caller holds tomoyo_policy_lock.
206 * Memory pointed by @ptr will be zeroed on success.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900207 */
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900208bool tomoyo_memory_ok(void *ptr)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900209{
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900210 int allocated_len = ptr ? ksize(ptr) : 0;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900211 atomic_add(allocated_len, &tomoyo_policy_memory_size);
212 if (ptr && (!tomoyo_quota_for_policy ||
213 atomic_read(&tomoyo_policy_memory_size)
214 <= tomoyo_quota_for_policy)) {
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900215 memset(ptr, 0, allocated_len);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900216 return true;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900217 }
Tetsuo Handa847b1732010-02-11 09:43:54 +0900218 printk(KERN_WARNING "ERROR: Out of memory "
219 "for tomoyo_alloc_element().\n");
220 if (!tomoyo_policy_loaded)
221 panic("MAC Initialization failed.\n");
222 return false;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900223}
224
Tetsuo Handa847b1732010-02-11 09:43:54 +0900225/**
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900226 * tomoyo_commit_ok - Check memory quota.
227 *
228 * @data: Data to copy from.
229 * @size: Size in byte.
230 *
231 * Returns pointer to allocated memory on success, NULL otherwise.
232 */
233void *tomoyo_commit_ok(void *data, const unsigned int size)
234{
235 void *ptr = kzalloc(size, GFP_NOFS);
236 if (tomoyo_memory_ok(ptr)) {
237 memmove(ptr, data, size);
238 memset(data, 0, size);
239 return ptr;
240 }
241 return NULL;
242}
243
244/**
Tetsuo Handa847b1732010-02-11 09:43:54 +0900245 * tomoyo_memory_free - Free memory for elements.
246 *
247 * @ptr: Pointer to allocated memory.
248 */
249void tomoyo_memory_free(void *ptr)
250{
251 atomic_sub(ksize(ptr), &tomoyo_policy_memory_size);
252 kfree(ptr);
253}
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900254
255/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900256 * tomoyo_name_list is used for holding string data used by TOMOYO.
257 * Since same string data is likely used for multiple times (e.g.
258 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
259 * "const struct tomoyo_path_info *".
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900260 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900261struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900262
263/**
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900264 * tomoyo_get_name - Allocate permanent memory for string data.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900265 *
266 * @name: The string to store into the permernent memory.
267 *
268 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900269 */
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900270const struct tomoyo_path_info *tomoyo_get_name(const char *name)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900271{
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900272 struct tomoyo_name_entry *ptr;
273 unsigned int hash;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900274 int len;
Tetsuo Handae41035a2010-01-05 06:39:00 +0900275 int allocated_len;
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700276 struct list_head *head;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900277
278 if (!name)
279 return NULL;
280 len = strlen(name) + 1;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900281 hash = full_name_hash((const unsigned char *) name, len - 1);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700282 head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
Tetsuo Handa29282382010-05-06 00:18:15 +0900283 if (mutex_lock_interruptible(&tomoyo_policy_lock))
284 return NULL;
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700285 list_for_each_entry(ptr, head, list) {
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900286 if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
287 continue;
288 atomic_inc(&ptr->users);
289 goto out;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900290 }
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900291 ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS);
Tetsuo Handae41035a2010-01-05 06:39:00 +0900292 allocated_len = ptr ? ksize(ptr) : 0;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900293 if (!ptr || (tomoyo_quota_for_policy &&
294 atomic_read(&tomoyo_policy_memory_size) + allocated_len
295 > tomoyo_quota_for_policy)) {
Tetsuo Handae41035a2010-01-05 06:39:00 +0900296 kfree(ptr);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900297 printk(KERN_WARNING "ERROR: Out of memory "
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900298 "for tomoyo_get_name().\n");
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900299 if (!tomoyo_policy_loaded)
300 panic("MAC Initialization failed.\n");
301 ptr = NULL;
302 goto out;
303 }
Tetsuo Handa847b1732010-02-11 09:43:54 +0900304 atomic_add(allocated_len, &tomoyo_policy_memory_size);
Tetsuo Handae41035a2010-01-05 06:39:00 +0900305 ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
306 memmove((char *) ptr->entry.name, name, len);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900307 atomic_set(&ptr->users, 1);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900308 tomoyo_fill_path_info(&ptr->entry);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700309 list_add_tail(&ptr->list, head);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900310 out:
Tetsuo Handa29282382010-05-06 00:18:15 +0900311 mutex_unlock(&tomoyo_policy_lock);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900312 return ptr ? &ptr->entry : NULL;
313}
314
315/**
316 * tomoyo_realpath_init - Initialize realpath related code.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900317 */
Tetsuo Handa1581e7d2009-02-21 20:40:50 +0900318void __init tomoyo_realpath_init(void)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900319{
320 int i;
321
322 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
323 for (i = 0; i < TOMOYO_MAX_HASH; i++)
324 INIT_LIST_HEAD(&tomoyo_name_list[i]);
325 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900326 tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900327 /*
328 * tomoyo_read_lock() is not needed because this function is
329 * called before the first "delete" request.
330 */
331 list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900332 if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
333 panic("Can't register tomoyo_kernel_domain");
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900334}
335
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900336/**
337 * tomoyo_read_memory_counter - Check for memory usage in bytes.
338 *
339 * @head: Pointer to "struct tomoyo_io_buffer".
340 *
341 * Returns memory usage.
342 */
343int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
344{
345 if (!head->read_eof) {
Tetsuo Handa847b1732010-02-11 09:43:54 +0900346 const unsigned int policy
347 = atomic_read(&tomoyo_policy_memory_size);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900348 char buffer[64];
349
350 memset(buffer, 0, sizeof(buffer));
Tetsuo Handa847b1732010-02-11 09:43:54 +0900351 if (tomoyo_quota_for_policy)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900352 snprintf(buffer, sizeof(buffer) - 1,
353 " (Quota: %10u)",
Tetsuo Handa847b1732010-02-11 09:43:54 +0900354 tomoyo_quota_for_policy);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900355 else
356 buffer[0] = '\0';
Tetsuo Handa847b1732010-02-11 09:43:54 +0900357 tomoyo_io_printf(head, "Policy: %10u%s\n", policy, buffer);
358 tomoyo_io_printf(head, "Total: %10u\n", policy);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900359 head->read_eof = true;
360 }
361 return 0;
362}
363
364/**
365 * tomoyo_write_memory_quota - Set memory quota.
366 *
367 * @head: Pointer to "struct tomoyo_io_buffer".
368 *
369 * Returns 0.
370 */
371int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
372{
373 char *data = head->write_buf;
374 unsigned int size;
375
Tetsuo Handa847b1732010-02-11 09:43:54 +0900376 if (sscanf(data, "Policy: %u", &size) == 1)
377 tomoyo_quota_for_policy = size;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900378 return 0;
379}