blob: 9557168b3767796f444b77dd490fe9e0252d24e7 [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 {
91 /* Taken from d_namespace_path(). */
92 struct path root;
93 struct path ns_root = { };
94 struct path tmp;
95
96 read_lock(&current->fs->lock);
97 root = current->fs->root;
98 path_get(&root);
99 read_unlock(&current->fs->lock);
100 spin_lock(&vfsmount_lock);
101 if (root.mnt && root.mnt->mnt_ns)
102 ns_root.mnt = mntget(root.mnt->mnt_ns->root);
103 if (ns_root.mnt)
104 ns_root.dentry = dget(ns_root.mnt->mnt_root);
105 spin_unlock(&vfsmount_lock);
106 spin_lock(&dcache_lock);
107 tmp = ns_root;
108 sp = __d_path(path, &tmp, newname, newname_len);
109 spin_unlock(&dcache_lock);
110 path_put(&root);
111 path_put(&ns_root);
Eric W. Biedermana4054b62009-11-20 09:12:22 -0800112 /* Prepend "/proc" prefix if using internal proc vfs mount. */
113 if (!IS_ERR(sp) && (path->mnt->mnt_parent == path->mnt) &&
Tetsuo Handa67fa4882009-12-09 15:36:04 +0900114 (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) {
Eric W. Biedermana4054b62009-11-20 09:12:22 -0800115 sp -= 5;
116 if (sp >= newname)
117 memcpy(sp, "/proc", 5);
118 else
119 sp = ERR_PTR(-ENOMEM);
120 }
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900121 }
122 if (IS_ERR(sp))
123 error = PTR_ERR(sp);
124 else
125 error = tomoyo_encode(newname, sp - newname, sp);
126 /* Append trailing '/' if dentry is a directory. */
127 if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
128 && *newname) {
129 sp = newname + strlen(newname);
130 if (*(sp - 1) != '/') {
131 if (sp < newname + newname_len - 4) {
132 *sp++ = '/';
133 *sp = '\0';
134 } else {
135 error = -ENOMEM;
136 }
137 }
138 }
139 if (error)
140 printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
141 return error;
142}
143
144/**
145 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
146 *
147 * @path: Pointer to "struct path".
148 *
149 * Returns the realpath of the given @path on success, NULL otherwise.
150 *
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900151 * These functions use kzalloc(), so the caller must call kfree()
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900152 * if these functions didn't return NULL.
153 */
154char *tomoyo_realpath_from_path(struct path *path)
155{
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900156 char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_KERNEL);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900157
158 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
159 <= TOMOYO_MAX_PATHNAME_LEN - 1);
160 if (!buf)
161 return NULL;
162 if (tomoyo_realpath_from_path2(path, buf,
163 TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
164 return buf;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900165 kfree(buf);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900166 return NULL;
167}
168
169/**
170 * tomoyo_realpath - Get realpath of a pathname.
171 *
172 * @pathname: The pathname to solve.
173 *
174 * Returns the realpath of @pathname on success, NULL otherwise.
175 */
176char *tomoyo_realpath(const char *pathname)
177{
Al Viroe24977d2009-04-02 21:17:03 -0400178 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900179
Al Viroe24977d2009-04-02 21:17:03 -0400180 if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
181 char *buf = tomoyo_realpath_from_path(&path);
182 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900183 return buf;
184 }
185 return NULL;
186}
187
188/**
189 * tomoyo_realpath_nofollow - Get realpath of a pathname.
190 *
191 * @pathname: The pathname to solve.
192 *
193 * Returns the realpath of @pathname on success, NULL otherwise.
194 */
195char *tomoyo_realpath_nofollow(const char *pathname)
196{
Al Viroe24977d2009-04-02 21:17:03 -0400197 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900198
Al Viroe24977d2009-04-02 21:17:03 -0400199 if (pathname && kern_path(pathname, 0, &path) == 0) {
200 char *buf = tomoyo_realpath_from_path(&path);
201 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900202 return buf;
203 }
204 return NULL;
205}
206
207/* Memory allocated for non-string data. */
208static unsigned int tomoyo_allocated_memory_for_elements;
209/* Quota for holding non-string data. */
210static unsigned int tomoyo_quota_for_elements;
211
212/**
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900213 * tomoyo_memory_ok - Check memory quota.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900214 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900215 * @ptr: Pointer to allocated memory.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900216 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900217 * Returns true on success, false otherwise.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900218 *
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900219 * Caller holds tomoyo_policy_lock.
220 * Memory pointed by @ptr will be zeroed on success.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900221 */
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900222bool tomoyo_memory_ok(void *ptr)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900223{
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900224 int allocated_len = ptr ? ksize(ptr) : 0;
225 bool result = false;
226 if (!ptr || (tomoyo_quota_for_elements &&
227 tomoyo_allocated_memory_for_elements
228 + allocated_len > tomoyo_quota_for_elements)) {
229 printk(KERN_WARNING "ERROR: Out of memory "
230 "for tomoyo_alloc_element().\n");
231 if (!tomoyo_policy_loaded)
232 panic("MAC Initialization failed.\n");
233 } else {
234 result = true;
235 tomoyo_allocated_memory_for_elements += allocated_len;
236 memset(ptr, 0, allocated_len);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900237 }
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900238 return result;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900239}
240
241/* Memory allocated for string data in bytes. */
242static unsigned int tomoyo_allocated_memory_for_savename;
243/* Quota for holding string data in bytes. */
244static unsigned int tomoyo_quota_for_savename;
245
246/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900247 * tomoyo_name_list is used for holding string data used by TOMOYO.
248 * Since same string data is likely used for multiple times (e.g.
249 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
250 * "const struct tomoyo_path_info *".
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900251 */
252static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
253
254/**
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900255 * tomoyo_get_name - Allocate permanent memory for string data.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900256 *
257 * @name: The string to store into the permernent memory.
258 *
259 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900260 */
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900261const struct tomoyo_path_info *tomoyo_get_name(const char *name)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900262{
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900263 static DEFINE_MUTEX(lock);
264 struct tomoyo_name_entry *ptr;
265 unsigned int hash;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900266 int len;
Tetsuo Handae41035a2010-01-05 06:39:00 +0900267 int allocated_len;
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700268 struct list_head *head;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900269
270 if (!name)
271 return NULL;
272 len = strlen(name) + 1;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900273 hash = full_name_hash((const unsigned char *) name, len - 1);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700274 head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900275 mutex_lock(&lock);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700276 list_for_each_entry(ptr, head, list) {
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900277 if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
278 continue;
279 atomic_inc(&ptr->users);
280 goto out;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900281 }
Tetsuo Handae41035a2010-01-05 06:39:00 +0900282 ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
283 allocated_len = ptr ? ksize(ptr) : 0;
284 if (!ptr || (tomoyo_quota_for_savename &&
285 tomoyo_allocated_memory_for_savename + allocated_len
286 > tomoyo_quota_for_savename)) {
287 kfree(ptr);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900288 printk(KERN_WARNING "ERROR: Out of memory "
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900289 "for tomoyo_get_name().\n");
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900290 if (!tomoyo_policy_loaded)
291 panic("MAC Initialization failed.\n");
292 ptr = NULL;
293 goto out;
294 }
Tetsuo Handae41035a2010-01-05 06:39:00 +0900295 tomoyo_allocated_memory_for_savename += allocated_len;
296 ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
297 memmove((char *) ptr->entry.name, name, len);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900298 atomic_set(&ptr->users, 1);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900299 tomoyo_fill_path_info(&ptr->entry);
Stephen Hemminger024e1a42009-10-27 19:24:46 -0700300 list_add_tail(&ptr->list, head);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900301 out:
302 mutex_unlock(&lock);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900303 return ptr ? &ptr->entry : NULL;
304}
305
306/**
307 * tomoyo_realpath_init - Initialize realpath related code.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900308 */
Tetsuo Handa1581e7d2009-02-21 20:40:50 +0900309void __init tomoyo_realpath_init(void)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900310{
311 int i;
312
313 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
314 for (i = 0; i < TOMOYO_MAX_HASH; i++)
315 INIT_LIST_HEAD(&tomoyo_name_list[i]);
316 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900317 tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900318 /*
319 * tomoyo_read_lock() is not needed because this function is
320 * called before the first "delete" request.
321 */
322 list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900323 if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
324 panic("Can't register tomoyo_kernel_domain");
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900325}
326
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900327/**
328 * tomoyo_read_memory_counter - Check for memory usage in bytes.
329 *
330 * @head: Pointer to "struct tomoyo_io_buffer".
331 *
332 * Returns memory usage.
333 */
334int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
335{
336 if (!head->read_eof) {
337 const unsigned int shared
338 = tomoyo_allocated_memory_for_savename;
339 const unsigned int private
340 = tomoyo_allocated_memory_for_elements;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900341 char buffer[64];
342
343 memset(buffer, 0, sizeof(buffer));
344 if (tomoyo_quota_for_savename)
345 snprintf(buffer, sizeof(buffer) - 1,
346 " (Quota: %10u)",
347 tomoyo_quota_for_savename);
348 else
349 buffer[0] = '\0';
350 tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
351 if (tomoyo_quota_for_elements)
352 snprintf(buffer, sizeof(buffer) - 1,
353 " (Quota: %10u)",
354 tomoyo_quota_for_elements);
355 else
356 buffer[0] = '\0';
357 tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900358 tomoyo_io_printf(head, "Total: %10u\n", shared + private);
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
376 if (sscanf(data, "Shared: %u", &size) == 1)
377 tomoyo_quota_for_savename = size;
378 else if (sscanf(data, "Private: %u", &size) == 1)
379 tomoyo_quota_for_elements = size;
380 return 0;
381}