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>
22 * tomoyo_encode: Convert binary string to ascii string.
24 * @buffer: Buffer for ASCII string.
25 * @buflen: Size of @buffer.
26 * @str: Binary string.
28 * Returns 0 on success, -ENOMEM otherwise.
30 int tomoyo_encode(char *buffer
, int buflen
, const char *str
)
33 const unsigned char c
= *(unsigned char *) str
++;
35 if (tomoyo_is_valid(c
)) {
56 *buffer
++ = (c
>> 6) + '0';
57 *buffer
++ = ((c
>> 3) & 7) + '0';
58 *buffer
++ = (c
& 7) + '0';
64 * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root.
66 * @path: Pointer to "struct path".
67 * @newname: Pointer to buffer to return value in.
68 * @newname_len: Size of @newname.
70 * Returns 0 on success, negative value otherwise.
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.
77 int tomoyo_realpath_from_path2(struct path
*path
, char *newname
,
81 struct dentry
*dentry
= path
->dentry
;
84 if (!dentry
|| !path
->mnt
|| !newname
|| newname_len
<= 2048)
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
);
92 /* Taken from d_namespace_path(). */
94 struct path ns_root
= { };
97 read_lock(¤t
->fs
->lock
);
98 root
= current
->fs
->root
;
100 read_unlock(¤t
->fs
->lock
);
101 spin_lock(&vfsmount_lock
);
102 if (root
.mnt
&& root
.mnt
->mnt_ns
)
103 ns_root
.mnt
= mntget(root
.mnt
->mnt_ns
->root
);
105 ns_root
.dentry
= dget(ns_root
.mnt
->mnt_root
);
106 spin_unlock(&vfsmount_lock
);
107 spin_lock(&dcache_lock
);
109 sp
= __d_path(path
, &tmp
, newname
, newname_len
);
110 spin_unlock(&dcache_lock
);
113 /* Prepend "/proc" prefix if using internal proc vfs mount. */
114 if (!IS_ERR(sp
) && (path
->mnt
->mnt_parent
== path
->mnt
) &&
115 (strcmp(path
->mnt
->mnt_sb
->s_type
->name
, "proc") == 0)) {
118 memcpy(sp
, "/proc", 5);
120 sp
= ERR_PTR(-ENOMEM
);
126 error
= tomoyo_encode(newname
, sp
- newname
, sp
);
127 /* Append trailing '/' if dentry is a directory. */
128 if (!error
&& dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)
130 sp
= newname
+ strlen(newname
);
131 if (*(sp
- 1) != '/') {
132 if (sp
< newname
+ newname_len
- 4) {
141 printk(KERN_WARNING
"tomoyo_realpath: Pathname too long.\n");
146 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
148 * @path: Pointer to "struct path".
150 * Returns the realpath of the given @path on success, NULL otherwise.
152 * These functions use tomoyo_alloc(), so the caller must call tomoyo_free()
153 * if these functions didn't return NULL.
155 char *tomoyo_realpath_from_path(struct path
*path
)
157 char *buf
= tomoyo_alloc(sizeof(struct tomoyo_page_buffer
));
159 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer
)
160 <= TOMOYO_MAX_PATHNAME_LEN
- 1);
163 if (tomoyo_realpath_from_path2(path
, buf
,
164 TOMOYO_MAX_PATHNAME_LEN
- 1) == 0)
171 * tomoyo_realpath - Get realpath of a pathname.
173 * @pathname: The pathname to solve.
175 * Returns the realpath of @pathname on success, NULL otherwise.
177 char *tomoyo_realpath(const char *pathname
)
181 if (pathname
&& kern_path(pathname
, LOOKUP_FOLLOW
, &path
) == 0) {
182 char *buf
= tomoyo_realpath_from_path(&path
);
190 * tomoyo_realpath_nofollow - Get realpath of a pathname.
192 * @pathname: The pathname to solve.
194 * Returns the realpath of @pathname on success, NULL otherwise.
196 char *tomoyo_realpath_nofollow(const char *pathname
)
200 if (pathname
&& kern_path(pathname
, 0, &path
) == 0) {
201 char *buf
= tomoyo_realpath_from_path(&path
);
208 /* Memory allocated for non-string data. */
209 static unsigned int tomoyo_allocated_memory_for_elements
;
210 /* Quota for holding non-string data. */
211 static unsigned int tomoyo_quota_for_elements
;
214 * tomoyo_alloc_element - Allocate permanent memory for structures.
216 * @size: Size in bytes.
218 * Returns pointer to allocated memory on success, NULL otherwise.
220 * Memory has to be zeroed.
221 * The RAM is chunked, so NEVER try to kfree() the returned pointer.
223 void *tomoyo_alloc_element(const unsigned int size
)
226 static DEFINE_MUTEX(lock
);
227 static unsigned int buf_used_len
= PATH_MAX
;
229 /*Assumes sizeof(void *) >= sizeof(long) is true. */
230 const unsigned int word_aligned_size
231 = roundup(size
, max(sizeof(void *), sizeof(long)));
232 if (word_aligned_size
> PATH_MAX
)
235 if (buf_used_len
+ word_aligned_size
> PATH_MAX
) {
236 if (!tomoyo_quota_for_elements
||
237 tomoyo_allocated_memory_for_elements
238 + PATH_MAX
<= tomoyo_quota_for_elements
)
239 ptr
= kzalloc(PATH_MAX
, GFP_KERNEL
);
241 printk(KERN_WARNING
"ERROR: Out of memory "
242 "for tomoyo_alloc_element().\n");
243 if (!tomoyo_policy_loaded
)
244 panic("MAC Initialization failed.\n");
247 tomoyo_allocated_memory_for_elements
+= PATH_MAX
;
248 buf_used_len
= word_aligned_size
;
251 } else if (word_aligned_size
) {
253 ptr
= buf
+ buf_used_len
;
254 buf_used_len
+= word_aligned_size
;
255 for (i
= 0; i
< word_aligned_size
; i
++) {
258 printk(KERN_ERR
"WARNING: Reserved memory was tainted! "
259 "The system might go wrong.\n");
267 /* Memory allocated for string data in bytes. */
268 static unsigned int tomoyo_allocated_memory_for_savename
;
269 /* Quota for holding string data in bytes. */
270 static unsigned int tomoyo_quota_for_savename
;
273 * TOMOYO uses this hash only when appending a string into the string
274 * table. Frequency of appending strings is very low. So we don't need
275 * large (e.g. 64k) hash size. 256 will be sufficient.
277 #define TOMOYO_HASH_BITS 8
278 #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
281 * tomoyo_name_entry is a structure which is used for linking
282 * "struct tomoyo_path_info" into tomoyo_name_list .
284 * Since tomoyo_name_list manages a list of strings which are shared by
285 * multiple processes (whereas "struct tomoyo_path_info" inside
286 * "struct tomoyo_path_info_with_data" is not shared), a reference counter will
287 * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
288 * when TOMOYO starts supporting garbage collector.
290 struct tomoyo_name_entry
{
291 struct list_head list
;
292 struct tomoyo_path_info entry
;
295 /* Structure for available memory region. */
296 struct tomoyo_free_memory_block_list
{
297 struct list_head list
;
298 char *ptr
; /* Pointer to a free area. */
299 int len
; /* Length of the area. */
303 * tomoyo_name_list is used for holding string data used by TOMOYO.
304 * Since same string data is likely used for multiple times (e.g.
305 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
306 * "const struct tomoyo_path_info *".
308 static struct list_head tomoyo_name_list
[TOMOYO_MAX_HASH
];
311 * tomoyo_save_name - Allocate permanent memory for string data.
313 * @name: The string to store into the permernent memory.
315 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
317 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
319 const struct tomoyo_path_info
*tomoyo_save_name(const char *name
)
321 static LIST_HEAD(fmb_list
);
322 static DEFINE_MUTEX(lock
);
323 struct tomoyo_name_entry
*ptr
;
325 /* fmb contains available size in bytes.
326 fmb is removed from the fmb_list when fmb->len becomes 0. */
327 struct tomoyo_free_memory_block_list
*fmb
;
330 struct list_head
*head
;
334 len
= strlen(name
) + 1;
335 if (len
> TOMOYO_MAX_PATHNAME_LEN
) {
336 printk(KERN_WARNING
"ERROR: Name too long "
337 "for tomoyo_save_name().\n");
340 hash
= full_name_hash((const unsigned char *) name
, len
- 1);
341 head
= &tomoyo_name_list
[hash_long(hash
, TOMOYO_HASH_BITS
)];
344 list_for_each_entry(ptr
, head
, list
) {
345 if (hash
== ptr
->entry
.hash
&& !strcmp(name
, ptr
->entry
.name
))
348 list_for_each_entry(fmb
, &fmb_list
, list
) {
352 if (!tomoyo_quota_for_savename
||
353 tomoyo_allocated_memory_for_savename
+ PATH_MAX
354 <= tomoyo_quota_for_savename
)
355 cp
= kzalloc(PATH_MAX
, GFP_KERNEL
);
358 fmb
= kzalloc(sizeof(*fmb
), GFP_KERNEL
);
362 printk(KERN_WARNING
"ERROR: Out of memory "
363 "for tomoyo_save_name().\n");
364 if (!tomoyo_policy_loaded
)
365 panic("MAC Initialization failed.\n");
369 tomoyo_allocated_memory_for_savename
+= PATH_MAX
;
370 list_add(&fmb
->list
, &fmb_list
);
374 ptr
= tomoyo_alloc_element(sizeof(*ptr
));
377 ptr
->entry
.name
= fmb
->ptr
;
378 memmove(fmb
->ptr
, name
, len
);
379 tomoyo_fill_path_info(&ptr
->entry
);
382 list_add_tail(&ptr
->list
, head
);
384 list_del(&fmb
->list
);
389 return ptr
? &ptr
->entry
: NULL
;
393 * tomoyo_realpath_init - Initialize realpath related code.
395 void __init
tomoyo_realpath_init(void)
399 BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN
> PATH_MAX
);
400 for (i
= 0; i
< TOMOYO_MAX_HASH
; i
++)
401 INIT_LIST_HEAD(&tomoyo_name_list
[i
]);
402 INIT_LIST_HEAD(&tomoyo_kernel_domain
.acl_info_list
);
403 tomoyo_kernel_domain
.domainname
= tomoyo_save_name(TOMOYO_ROOT_NAME
);
404 list_add_tail(&tomoyo_kernel_domain
.list
, &tomoyo_domain_list
);
405 down_read(&tomoyo_domain_list_lock
);
406 if (tomoyo_find_domain(TOMOYO_ROOT_NAME
) != &tomoyo_kernel_domain
)
407 panic("Can't register tomoyo_kernel_domain");
408 up_read(&tomoyo_domain_list_lock
);
411 /* Memory allocated for temporary purpose. */
412 static atomic_t tomoyo_dynamic_memory_size
;
415 * tomoyo_alloc - Allocate memory for temporary purpose.
417 * @size: Size in bytes.
419 * Returns pointer to allocated memory on success, NULL otherwise.
421 void *tomoyo_alloc(const size_t size
)
423 void *p
= kzalloc(size
, GFP_KERNEL
);
425 atomic_add(ksize(p
), &tomoyo_dynamic_memory_size
);
430 * tomoyo_free - Release memory allocated by tomoyo_alloc().
432 * @p: Pointer returned by tomoyo_alloc(). May be NULL.
436 void tomoyo_free(const void *p
)
439 atomic_sub(ksize(p
), &tomoyo_dynamic_memory_size
);
445 * tomoyo_read_memory_counter - Check for memory usage in bytes.
447 * @head: Pointer to "struct tomoyo_io_buffer".
449 * Returns memory usage.
451 int tomoyo_read_memory_counter(struct tomoyo_io_buffer
*head
)
453 if (!head
->read_eof
) {
454 const unsigned int shared
455 = tomoyo_allocated_memory_for_savename
;
456 const unsigned int private
457 = tomoyo_allocated_memory_for_elements
;
458 const unsigned int dynamic
459 = atomic_read(&tomoyo_dynamic_memory_size
);
462 memset(buffer
, 0, sizeof(buffer
));
463 if (tomoyo_quota_for_savename
)
464 snprintf(buffer
, sizeof(buffer
) - 1,
466 tomoyo_quota_for_savename
);
469 tomoyo_io_printf(head
, "Shared: %10u%s\n", shared
, buffer
);
470 if (tomoyo_quota_for_elements
)
471 snprintf(buffer
, sizeof(buffer
) - 1,
473 tomoyo_quota_for_elements
);
476 tomoyo_io_printf(head
, "Private: %10u%s\n", private, buffer
);
477 tomoyo_io_printf(head
, "Dynamic: %10u\n", dynamic
);
478 tomoyo_io_printf(head
, "Total: %10u\n",
479 shared
+ private + dynamic
);
480 head
->read_eof
= true;
486 * tomoyo_write_memory_quota - Set memory quota.
488 * @head: Pointer to "struct tomoyo_io_buffer".
492 int tomoyo_write_memory_quota(struct tomoyo_io_buffer
*head
)
494 char *data
= head
->write_buf
;
497 if (sscanf(data
, "Shared: %u", &size
) == 1)
498 tomoyo_quota_for_savename
= size
;
499 else if (sscanf(data
, "Private: %u", &size
) == 1)
500 tomoyo_quota_for_elements
= size
;