Merge branch 'for-fsnotify' into for-linus
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / security / tomoyo / realpath.c
1 /*
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 *
8 * Version: 2.2.0 2009/04/01
9 *
10 */
11
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
18 #include "common.h"
19 #include "realpath.h"
20
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 */
30 int 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 */
77 int 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 {
92 struct path ns_root = {.mnt = NULL, .dentry = NULL};
93
94 spin_lock(&dcache_lock);
95 /* go to whatever namespace root we are under */
96 sp = __d_path(path, &ns_root, newname, newname_len);
97 spin_unlock(&dcache_lock);
98 /* Prepend "/proc" prefix if using internal proc vfs mount. */
99 if (!IS_ERR(sp) && (path->mnt->mnt_flags & MNT_INTERNAL) &&
100 (strcmp(path->mnt->mnt_sb->s_type->name, "proc") == 0)) {
101 sp -= 5;
102 if (sp >= newname)
103 memcpy(sp, "/proc", 5);
104 else
105 sp = ERR_PTR(-ENOMEM);
106 }
107 }
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 *
137 * These functions use tomoyo_alloc(), so the caller must call tomoyo_free()
138 * if these functions didn't return NULL.
139 */
140 char *tomoyo_realpath_from_path(struct path *path)
141 {
142 char *buf = tomoyo_alloc(sizeof(struct tomoyo_page_buffer));
143
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;
151 tomoyo_free(buf);
152 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 */
162 char *tomoyo_realpath(const char *pathname)
163 {
164 struct path path;
165
166 if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
167 char *buf = tomoyo_realpath_from_path(&path);
168 path_put(&path);
169 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 */
181 char *tomoyo_realpath_nofollow(const char *pathname)
182 {
183 struct path path;
184
185 if (pathname && kern_path(pathname, 0, &path) == 0) {
186 char *buf = tomoyo_realpath_from_path(&path);
187 path_put(&path);
188 return buf;
189 }
190 return NULL;
191 }
192
193 /* Memory allocated for non-string data. */
194 static unsigned int tomoyo_allocated_memory_for_elements;
195 /* Quota for holding non-string data. */
196 static unsigned int tomoyo_quota_for_elements;
197
198 /**
199 * tomoyo_alloc_element - Allocate permanent memory for structures.
200 *
201 * @size: Size in bytes.
202 *
203 * Returns pointer to allocated memory on success, NULL otherwise.
204 *
205 * Memory has to be zeroed.
206 * The RAM is chunked, so NEVER try to kfree() the returned pointer.
207 */
208 void *tomoyo_alloc_element(const unsigned int size)
209 {
210 static char *buf;
211 static DEFINE_MUTEX(lock);
212 static unsigned int buf_used_len = PATH_MAX;
213 char *ptr = NULL;
214 /*Assumes sizeof(void *) >= sizeof(long) is true. */
215 const unsigned int word_aligned_size
216 = roundup(size, max(sizeof(void *), sizeof(long)));
217 if (word_aligned_size > PATH_MAX)
218 return NULL;
219 mutex_lock(&lock);
220 if (buf_used_len + word_aligned_size > PATH_MAX) {
221 if (!tomoyo_quota_for_elements ||
222 tomoyo_allocated_memory_for_elements
223 + PATH_MAX <= tomoyo_quota_for_elements)
224 ptr = kzalloc(PATH_MAX, GFP_KERNEL);
225 if (!ptr) {
226 printk(KERN_WARNING "ERROR: Out of memory "
227 "for tomoyo_alloc_element().\n");
228 if (!tomoyo_policy_loaded)
229 panic("MAC Initialization failed.\n");
230 } else {
231 buf = ptr;
232 tomoyo_allocated_memory_for_elements += PATH_MAX;
233 buf_used_len = word_aligned_size;
234 ptr = buf;
235 }
236 } else if (word_aligned_size) {
237 int i;
238 ptr = buf + buf_used_len;
239 buf_used_len += word_aligned_size;
240 for (i = 0; i < word_aligned_size; i++) {
241 if (!ptr[i])
242 continue;
243 printk(KERN_ERR "WARNING: Reserved memory was tainted! "
244 "The system might go wrong.\n");
245 ptr[i] = '\0';
246 }
247 }
248 mutex_unlock(&lock);
249 return ptr;
250 }
251
252 /* Memory allocated for string data in bytes. */
253 static unsigned int tomoyo_allocated_memory_for_savename;
254 /* Quota for holding string data in bytes. */
255 static unsigned int tomoyo_quota_for_savename;
256
257 /*
258 * TOMOYO uses this hash only when appending a string into the string
259 * table. Frequency of appending strings is very low. So we don't need
260 * large (e.g. 64k) hash size. 256 will be sufficient.
261 */
262 #define TOMOYO_HASH_BITS 8
263 #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
264
265 /*
266 * tomoyo_name_entry is a structure which is used for linking
267 * "struct tomoyo_path_info" into tomoyo_name_list .
268 *
269 * Since tomoyo_name_list manages a list of strings which are shared by
270 * multiple processes (whereas "struct tomoyo_path_info" inside
271 * "struct tomoyo_path_info_with_data" is not shared), a reference counter will
272 * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
273 * when TOMOYO starts supporting garbage collector.
274 */
275 struct tomoyo_name_entry {
276 struct list_head list;
277 struct tomoyo_path_info entry;
278 };
279
280 /* Structure for available memory region. */
281 struct tomoyo_free_memory_block_list {
282 struct list_head list;
283 char *ptr; /* Pointer to a free area. */
284 int len; /* Length of the area. */
285 };
286
287 /*
288 * tomoyo_name_list is used for holding string data used by TOMOYO.
289 * Since same string data is likely used for multiple times (e.g.
290 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
291 * "const struct tomoyo_path_info *".
292 */
293 static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
294
295 /**
296 * tomoyo_save_name - Allocate permanent memory for string data.
297 *
298 * @name: The string to store into the permernent memory.
299 *
300 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
301 *
302 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
303 */
304 const struct tomoyo_path_info *tomoyo_save_name(const char *name)
305 {
306 static LIST_HEAD(fmb_list);
307 static DEFINE_MUTEX(lock);
308 struct tomoyo_name_entry *ptr;
309 unsigned int hash;
310 /* fmb contains available size in bytes.
311 fmb is removed from the fmb_list when fmb->len becomes 0. */
312 struct tomoyo_free_memory_block_list *fmb;
313 int len;
314 char *cp;
315 struct list_head *head;
316
317 if (!name)
318 return NULL;
319 len = strlen(name) + 1;
320 if (len > TOMOYO_MAX_PATHNAME_LEN) {
321 printk(KERN_WARNING "ERROR: Name too long "
322 "for tomoyo_save_name().\n");
323 return NULL;
324 }
325 hash = full_name_hash((const unsigned char *) name, len - 1);
326 head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
327
328 mutex_lock(&lock);
329 list_for_each_entry(ptr, head, list) {
330 if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
331 goto out;
332 }
333 list_for_each_entry(fmb, &fmb_list, list) {
334 if (len <= fmb->len)
335 goto ready;
336 }
337 if (!tomoyo_quota_for_savename ||
338 tomoyo_allocated_memory_for_savename + PATH_MAX
339 <= tomoyo_quota_for_savename)
340 cp = kzalloc(PATH_MAX, GFP_KERNEL);
341 else
342 cp = NULL;
343 fmb = kzalloc(sizeof(*fmb), GFP_KERNEL);
344 if (!cp || !fmb) {
345 kfree(cp);
346 kfree(fmb);
347 printk(KERN_WARNING "ERROR: Out of memory "
348 "for tomoyo_save_name().\n");
349 if (!tomoyo_policy_loaded)
350 panic("MAC Initialization failed.\n");
351 ptr = NULL;
352 goto out;
353 }
354 tomoyo_allocated_memory_for_savename += PATH_MAX;
355 list_add(&fmb->list, &fmb_list);
356 fmb->ptr = cp;
357 fmb->len = PATH_MAX;
358 ready:
359 ptr = tomoyo_alloc_element(sizeof(*ptr));
360 if (!ptr)
361 goto out;
362 ptr->entry.name = fmb->ptr;
363 memmove(fmb->ptr, name, len);
364 tomoyo_fill_path_info(&ptr->entry);
365 fmb->ptr += len;
366 fmb->len -= len;
367 list_add_tail(&ptr->list, head);
368 if (fmb->len == 0) {
369 list_del(&fmb->list);
370 kfree(fmb);
371 }
372 out:
373 mutex_unlock(&lock);
374 return ptr ? &ptr->entry : NULL;
375 }
376
377 /**
378 * tomoyo_realpath_init - Initialize realpath related code.
379 */
380 void __init tomoyo_realpath_init(void)
381 {
382 int i;
383
384 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
385 for (i = 0; i < TOMOYO_MAX_HASH; i++)
386 INIT_LIST_HEAD(&tomoyo_name_list[i]);
387 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
388 tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME);
389 list_add_tail(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
390 down_read(&tomoyo_domain_list_lock);
391 if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
392 panic("Can't register tomoyo_kernel_domain");
393 up_read(&tomoyo_domain_list_lock);
394 }
395
396 /* Memory allocated for temporary purpose. */
397 static atomic_t tomoyo_dynamic_memory_size;
398
399 /**
400 * tomoyo_alloc - Allocate memory for temporary purpose.
401 *
402 * @size: Size in bytes.
403 *
404 * Returns pointer to allocated memory on success, NULL otherwise.
405 */
406 void *tomoyo_alloc(const size_t size)
407 {
408 void *p = kzalloc(size, GFP_KERNEL);
409 if (p)
410 atomic_add(ksize(p), &tomoyo_dynamic_memory_size);
411 return p;
412 }
413
414 /**
415 * tomoyo_free - Release memory allocated by tomoyo_alloc().
416 *
417 * @p: Pointer returned by tomoyo_alloc(). May be NULL.
418 *
419 * Returns nothing.
420 */
421 void tomoyo_free(const void *p)
422 {
423 if (p) {
424 atomic_sub(ksize(p), &tomoyo_dynamic_memory_size);
425 kfree(p);
426 }
427 }
428
429 /**
430 * tomoyo_read_memory_counter - Check for memory usage in bytes.
431 *
432 * @head: Pointer to "struct tomoyo_io_buffer".
433 *
434 * Returns memory usage.
435 */
436 int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
437 {
438 if (!head->read_eof) {
439 const unsigned int shared
440 = tomoyo_allocated_memory_for_savename;
441 const unsigned int private
442 = tomoyo_allocated_memory_for_elements;
443 const unsigned int dynamic
444 = atomic_read(&tomoyo_dynamic_memory_size);
445 char buffer[64];
446
447 memset(buffer, 0, sizeof(buffer));
448 if (tomoyo_quota_for_savename)
449 snprintf(buffer, sizeof(buffer) - 1,
450 " (Quota: %10u)",
451 tomoyo_quota_for_savename);
452 else
453 buffer[0] = '\0';
454 tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
455 if (tomoyo_quota_for_elements)
456 snprintf(buffer, sizeof(buffer) - 1,
457 " (Quota: %10u)",
458 tomoyo_quota_for_elements);
459 else
460 buffer[0] = '\0';
461 tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
462 tomoyo_io_printf(head, "Dynamic: %10u\n", dynamic);
463 tomoyo_io_printf(head, "Total: %10u\n",
464 shared + private + dynamic);
465 head->read_eof = true;
466 }
467 return 0;
468 }
469
470 /**
471 * tomoyo_write_memory_quota - Set memory quota.
472 *
473 * @head: Pointer to "struct tomoyo_io_buffer".
474 *
475 * Returns 0.
476 */
477 int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
478 {
479 char *data = head->write_buf;
480 unsigned int size;
481
482 if (sscanf(data, "Shared: %u", &size) == 1)
483 tomoyo_quota_for_savename = size;
484 else if (sscanf(data, "Private: %u", &size) == 1)
485 tomoyo_quota_for_elements = size;
486 return 0;
487 }