blob: cf7d61f781b9768cc8691cd3b4fc4c234a313b55 [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>
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090018#include "common.h"
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090019
20/**
21 * tomoyo_encode: Convert binary string to ascii string.
22 *
23 * @buffer: Buffer for ASCII string.
24 * @buflen: Size of @buffer.
25 * @str: Binary string.
26 *
27 * Returns 0 on success, -ENOMEM otherwise.
28 */
29int tomoyo_encode(char *buffer, int buflen, const char *str)
30{
31 while (1) {
32 const unsigned char c = *(unsigned char *) str++;
33
34 if (tomoyo_is_valid(c)) {
35 if (--buflen <= 0)
36 break;
37 *buffer++ = (char) c;
38 if (c != '\\')
39 continue;
40 if (--buflen <= 0)
41 break;
42 *buffer++ = (char) c;
43 continue;
44 }
45 if (!c) {
46 if (--buflen <= 0)
47 break;
48 *buffer = '\0';
49 return 0;
50 }
51 buflen -= 4;
52 if (buflen <= 0)
53 break;
54 *buffer++ = '\\';
55 *buffer++ = (c >> 6) + '0';
56 *buffer++ = ((c >> 3) & 7) + '0';
57 *buffer++ = (c & 7) + '0';
58 }
59 return -ENOMEM;
60}
61
62/**
63 * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root.
64 *
65 * @path: Pointer to "struct path".
66 * @newname: Pointer to buffer to return value in.
67 * @newname_len: Size of @newname.
68 *
69 * Returns 0 on success, negative value otherwise.
70 *
71 * If dentry is a directory, trailing '/' is appended.
72 * Characters out of 0x20 < c < 0x7F range are converted to
73 * \ooo style octal string.
74 * Character \ is converted to \\ string.
75 */
76int tomoyo_realpath_from_path2(struct path *path, char *newname,
77 int newname_len)
78{
79 int error = -ENOMEM;
80 struct dentry *dentry = path->dentry;
81 char *sp;
82
83 if (!dentry || !path->mnt || !newname || newname_len <= 2048)
84 return -EINVAL;
85 if (dentry->d_op && dentry->d_op->d_dname) {
86 /* For "socket:[\$]" and "pipe:[\$]". */
87 static const int offset = 1536;
88 sp = dentry->d_op->d_dname(dentry, newname + offset,
89 newname_len - offset);
90 } else {
Al Viro37afdc72010-02-05 01:41:33 -050091 struct path ns_root = {.mnt = NULL, .dentry = NULL};
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090092
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090093 spin_lock(&dcache_lock);
Al Viro37afdc72010-02-05 01:41:33 -050094 /* go to whatever namespace root we are under */
95 sp = __d_path(path, &ns_root, newname, newname_len);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090096 spin_unlock(&dcache_lock);
Eric W. Biedermana4054b62009-11-20 09:12:22 -080097 /* Prepend "/proc" prefix if using internal proc vfs mount. */
Al Viro440b3c62010-02-05 09:37:21 -050098 if (!IS_ERR(sp) && (path->mnt->mnt_flags & MNT_INTERNAL) &&
Tetsuo Handa67fa4882009-12-09 15:36:04 +090099 (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) {
Eric W. Biedermana4054b62009-11-20 09:12:22 -0800100 sp -= 5;
101 if (sp >= newname)
102 memcpy(sp, "/proc", 5);
103 else
104 sp = ERR_PTR(-ENOMEM);
105 }
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900106 }
107 if (IS_ERR(sp))
108 error = PTR_ERR(sp);
109 else
110 error = tomoyo_encode(newname, sp - newname, sp);
111 /* Append trailing '/' if dentry is a directory. */
112 if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
113 && *newname) {
114 sp = newname + strlen(newname);
115 if (*(sp - 1) != '/') {
116 if (sp < newname + newname_len - 4) {
117 *sp++ = '/';
118 *sp = '\0';
119 } else {
120 error = -ENOMEM;
121 }
122 }
123 }
124 if (error)
125 printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
126 return error;
127}
128
129/**
130 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
131 *
132 * @path: Pointer to "struct path".
133 *
134 * Returns the realpath of the given @path on success, NULL otherwise.
135 *
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900136 * These functions use kzalloc(), so the caller must call kfree()
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900137 * if these functions didn't return NULL.
138 */
139char *tomoyo_realpath_from_path(struct path *path)
140{
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900141 char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_KERNEL);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900142
143 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
144 <= TOMOYO_MAX_PATHNAME_LEN - 1);
145 if (!buf)
146 return NULL;
147 if (tomoyo_realpath_from_path2(path, buf,
148 TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
149 return buf;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900150 kfree(buf);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900151 return NULL;
152}
153
154/**
155 * tomoyo_realpath - Get realpath of a pathname.
156 *
157 * @pathname: The pathname to solve.
158 *
159 * Returns the realpath of @pathname on success, NULL otherwise.
160 */
161char *tomoyo_realpath(const char *pathname)
162{
Al Viroe24977d2009-04-02 21:17:03 -0400163 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900164
Al Viroe24977d2009-04-02 21:17:03 -0400165 if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
166 char *buf = tomoyo_realpath_from_path(&path);
167 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900168 return buf;
169 }
170 return NULL;
171}
172
173/**
174 * tomoyo_realpath_nofollow - Get realpath of a pathname.
175 *
176 * @pathname: The pathname to solve.
177 *
178 * Returns the realpath of @pathname on success, NULL otherwise.
179 */
180char *tomoyo_realpath_nofollow(const char *pathname)
181{
Al Viroe24977d2009-04-02 21:17:03 -0400182 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900183
Al Viroe24977d2009-04-02 21:17:03 -0400184 if (pathname && kern_path(pathname, 0, &path) == 0) {
185 char *buf = tomoyo_realpath_from_path(&path);
186 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900187 return buf;
188 }
189 return NULL;
190}
191
192/* Memory allocated for non-string data. */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900193static atomic_t tomoyo_policy_memory_size;
194/* Quota for holding policy. */
195static unsigned int tomoyo_quota_for_policy;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900196
197/**
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900198 * tomoyo_memory_ok - Check memory quota.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900199 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900200 * @ptr: Pointer to allocated memory.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900201 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900202 * Returns true on success, false otherwise.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900203 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900204 * Caller holds tomoyo_policy_lock.
205 * Memory pointed by @ptr will be zeroed on success.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900206 */
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900207bool tomoyo_memory_ok(void *ptr)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900208{
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900209 int allocated_len = ptr ? ksize(ptr) : 0;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900210 atomic_add(allocated_len, &tomoyo_policy_memory_size);
211 if (ptr && (!tomoyo_quota_for_policy ||
212 atomic_read(&tomoyo_policy_memory_size)
213 <= tomoyo_quota_for_policy)) {
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900214 memset(ptr, 0, allocated_len);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900215 return true;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900216 }
Tetsuo Handa847b1732010-02-11 09:43:54 +0900217 printk(KERN_WARNING "ERROR: Out of memory "
218 "for tomoyo_alloc_element().\n");
219 if (!tomoyo_policy_loaded)
220 panic("MAC Initialization failed.\n");
221 return false;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900222}
223
Tetsuo Handa847b1732010-02-11 09:43:54 +0900224/**
225 * tomoyo_memory_free - Free memory for elements.
226 *
227 * @ptr: Pointer to allocated memory.
228 */
229void tomoyo_memory_free(void *ptr)
230{
231 atomic_sub(ksize(ptr), &tomoyo_policy_memory_size);
232 kfree(ptr);
233}
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900234
235/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900236 * tomoyo_name_list is used for holding string data used by TOMOYO.
237 * Since same string data is likely used for multiple times (e.g.
238 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
239 * "const struct tomoyo_path_info *".
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900240 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900241struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
242/* Lock for protecting tomoyo_name_list . */
243DEFINE_MUTEX(tomoyo_name_list_lock);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900244
245/**
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900246 * tomoyo_get_name - Allocate permanent memory for string data.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900247 *
248 * @name: The string to store into the permernent memory.
249 *
250 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900251 */
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900252const struct tomoyo_path_info *tomoyo_get_name(const char *name)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900253{
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900254 struct tomoyo_name_entry *ptr;
255 unsigned int hash;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900256 int len;
Tetsuo Handae41035a2010-01-05 06:39:00 +0900257 int allocated_len;
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700258 struct list_head *head;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900259
260 if (!name)
261 return NULL;
262 len = strlen(name) + 1;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900263 hash = full_name_hash((const unsigned char *) name, len - 1);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700264 head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
Tetsuo Handa847b1732010-02-11 09:43:54 +0900265 mutex_lock(&tomoyo_name_list_lock);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700266 list_for_each_entry(ptr, head, list) {
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900267 if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
268 continue;
269 atomic_inc(&ptr->users);
270 goto out;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900271 }
Tetsuo Handae41035a2010-01-05 06:39:00 +0900272 ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
273 allocated_len = ptr ? ksize(ptr) : 0;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900274 if (!ptr || (tomoyo_quota_for_policy &&
275 atomic_read(&tomoyo_policy_memory_size) + allocated_len
276 > tomoyo_quota_for_policy)) {
Tetsuo Handae41035a2010-01-05 06:39:00 +0900277 kfree(ptr);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900278 printk(KERN_WARNING "ERROR: Out of memory "
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900279 "for tomoyo_get_name().\n");
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900280 if (!tomoyo_policy_loaded)
281 panic("MAC Initialization failed.\n");
282 ptr = NULL;
283 goto out;
284 }
Tetsuo Handa847b1732010-02-11 09:43:54 +0900285 atomic_add(allocated_len, &tomoyo_policy_memory_size);
Tetsuo Handae41035a2010-01-05 06:39:00 +0900286 ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
287 memmove((char *) ptr->entry.name, name, len);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900288 atomic_set(&ptr->users, 1);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900289 tomoyo_fill_path_info(&ptr->entry);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700290 list_add_tail(&ptr->list, head);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900291 out:
Tetsuo Handa847b1732010-02-11 09:43:54 +0900292 mutex_unlock(&tomoyo_name_list_lock);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900293 return ptr ? &ptr->entry : NULL;
294}
295
296/**
297 * tomoyo_realpath_init - Initialize realpath related code.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900298 */
Tetsuo Handa1581e7d2009-02-21 20:40:50 +0900299void __init tomoyo_realpath_init(void)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900300{
301 int i;
302
303 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
304 for (i = 0; i < TOMOYO_MAX_HASH; i++)
305 INIT_LIST_HEAD(&tomoyo_name_list[i]);
306 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900307 tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900308 /*
309 * tomoyo_read_lock() is not needed because this function is
310 * called before the first "delete" request.
311 */
312 list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900313 if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
314 panic("Can't register tomoyo_kernel_domain");
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900315}
316
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900317/**
318 * tomoyo_read_memory_counter - Check for memory usage in bytes.
319 *
320 * @head: Pointer to "struct tomoyo_io_buffer".
321 *
322 * Returns memory usage.
323 */
324int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
325{
326 if (!head->read_eof) {
Tetsuo Handa847b1732010-02-11 09:43:54 +0900327 const unsigned int policy
328 = atomic_read(&tomoyo_policy_memory_size);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900329 char buffer[64];
330
331 memset(buffer, 0, sizeof(buffer));
Tetsuo Handa847b1732010-02-11 09:43:54 +0900332 if (tomoyo_quota_for_policy)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900333 snprintf(buffer, sizeof(buffer) - 1,
334 " (Quota: %10u)",
Tetsuo Handa847b1732010-02-11 09:43:54 +0900335 tomoyo_quota_for_policy);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900336 else
337 buffer[0] = '\0';
Tetsuo Handa847b1732010-02-11 09:43:54 +0900338 tomoyo_io_printf(head, "Policy: %10u%s\n", policy, buffer);
339 tomoyo_io_printf(head, "Total: %10u\n", policy);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900340 head->read_eof = true;
341 }
342 return 0;
343}
344
345/**
346 * tomoyo_write_memory_quota - Set memory quota.
347 *
348 * @head: Pointer to "struct tomoyo_io_buffer".
349 *
350 * Returns 0.
351 */
352int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
353{
354 char *data = head->write_buf;
355 unsigned int size;
356
Tetsuo Handa847b1732010-02-11 09:43:54 +0900357 if (sscanf(data, "Policy: %u", &size) == 1)
358 tomoyo_quota_for_policy = size;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900359 return 0;
360}