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>
17 #include <linux/magic.h>
18 #include <linux/slab.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 struct path ns_root
= {.mnt
= NULL
, .dentry
= NULL
};
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 (path
->mnt
->mnt_sb
->s_magic
== PROC_SUPER_MAGIC
)) {
103 memcpy(sp
, "/proc", 5);
105 sp
= ERR_PTR(-ENOMEM
);
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
)
115 sp
= newname
+ strlen(newname
);
116 if (*(sp
- 1) != '/') {
117 if (sp
< newname
+ newname_len
- 4) {
126 printk(KERN_WARNING
"tomoyo_realpath: Pathname too long.\n");
131 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
133 * @path: Pointer to "struct path".
135 * Returns the realpath of the given @path on success, NULL otherwise.
137 * These functions use kzalloc(), so the caller must call kfree()
138 * if these functions didn't return NULL.
140 char *tomoyo_realpath_from_path(struct path
*path
)
142 char *buf
= kzalloc(sizeof(struct tomoyo_page_buffer
), GFP_NOFS
);
144 BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer
)
145 <= TOMOYO_MAX_PATHNAME_LEN
- 1);
148 if (tomoyo_realpath_from_path2(path
, buf
,
149 TOMOYO_MAX_PATHNAME_LEN
- 1) == 0)
156 * tomoyo_realpath - Get realpath of a pathname.
158 * @pathname: The pathname to solve.
160 * Returns the realpath of @pathname on success, NULL otherwise.
162 char *tomoyo_realpath(const char *pathname
)
166 if (pathname
&& kern_path(pathname
, LOOKUP_FOLLOW
, &path
) == 0) {
167 char *buf
= tomoyo_realpath_from_path(&path
);
175 * tomoyo_realpath_nofollow - Get realpath of a pathname.
177 * @pathname: The pathname to solve.
179 * Returns the realpath of @pathname on success, NULL otherwise.
181 char *tomoyo_realpath_nofollow(const char *pathname
)
185 if (pathname
&& kern_path(pathname
, 0, &path
) == 0) {
186 char *buf
= tomoyo_realpath_from_path(&path
);
193 /* Memory allocated for non-string data. */
194 static atomic_t tomoyo_policy_memory_size
;
195 /* Quota for holding policy. */
196 static unsigned int tomoyo_quota_for_policy
;
199 * tomoyo_memory_ok - Check memory quota.
201 * @ptr: Pointer to allocated memory.
203 * Returns true on success, false otherwise.
205 * Caller holds tomoyo_policy_lock.
206 * Memory pointed by @ptr will be zeroed on success.
208 bool tomoyo_memory_ok(void *ptr
)
210 int allocated_len
= ptr
? ksize(ptr
) : 0;
211 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
)) {
215 memset(ptr
, 0, allocated_len
);
218 printk(KERN_WARNING
"ERROR: Out of memory "
219 "for tomoyo_alloc_element().\n");
220 if (!tomoyo_policy_loaded
)
221 panic("MAC Initialization failed.\n");
226 * tomoyo_commit_ok - Check memory quota.
228 * @data: Data to copy from.
229 * @size: Size in byte.
231 * Returns pointer to allocated memory on success, NULL otherwise.
233 void *tomoyo_commit_ok(void *data
, const unsigned int size
)
235 void *ptr
= kzalloc(size
, GFP_NOFS
);
236 if (tomoyo_memory_ok(ptr
)) {
237 memmove(ptr
, data
, size
);
238 memset(data
, 0, size
);
245 * tomoyo_memory_free - Free memory for elements.
247 * @ptr: Pointer to allocated memory.
249 void tomoyo_memory_free(void *ptr
)
251 atomic_sub(ksize(ptr
), &tomoyo_policy_memory_size
);
256 * 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 *".
261 struct list_head tomoyo_name_list
[TOMOYO_MAX_HASH
];
264 * tomoyo_get_name - Allocate permanent memory for string data.
266 * @name: The string to store into the permernent memory.
268 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
270 const struct tomoyo_path_info
*tomoyo_get_name(const char *name
)
272 struct tomoyo_name_entry
*ptr
;
276 struct list_head
*head
;
280 len
= strlen(name
) + 1;
281 hash
= full_name_hash((const unsigned char *) name
, len
- 1);
282 head
= &tomoyo_name_list
[hash_long(hash
, TOMOYO_HASH_BITS
)];
283 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
285 list_for_each_entry(ptr
, head
, list
) {
286 if (hash
!= ptr
->entry
.hash
|| strcmp(name
, ptr
->entry
.name
))
288 atomic_inc(&ptr
->users
);
291 ptr
= kzalloc(sizeof(*ptr
) + len
, GFP_NOFS
);
292 allocated_len
= ptr
? ksize(ptr
) : 0;
293 if (!ptr
|| (tomoyo_quota_for_policy
&&
294 atomic_read(&tomoyo_policy_memory_size
) + allocated_len
295 > tomoyo_quota_for_policy
)) {
297 printk(KERN_WARNING
"ERROR: Out of memory "
298 "for tomoyo_get_name().\n");
299 if (!tomoyo_policy_loaded
)
300 panic("MAC Initialization failed.\n");
304 atomic_add(allocated_len
, &tomoyo_policy_memory_size
);
305 ptr
->entry
.name
= ((char *) ptr
) + sizeof(*ptr
);
306 memmove((char *) ptr
->entry
.name
, name
, len
);
307 atomic_set(&ptr
->users
, 1);
308 tomoyo_fill_path_info(&ptr
->entry
);
309 list_add_tail(&ptr
->list
, head
);
311 mutex_unlock(&tomoyo_policy_lock
);
312 return ptr
? &ptr
->entry
: NULL
;
316 * tomoyo_realpath_init - Initialize realpath related code.
318 void __init
tomoyo_realpath_init(void)
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
);
326 tomoyo_kernel_domain
.domainname
= tomoyo_get_name(TOMOYO_ROOT_NAME
);
328 * tomoyo_read_lock() is not needed because this function is
329 * called before the first "delete" request.
331 list_add_tail_rcu(&tomoyo_kernel_domain
.list
, &tomoyo_domain_list
);
332 if (tomoyo_find_domain(TOMOYO_ROOT_NAME
) != &tomoyo_kernel_domain
)
333 panic("Can't register tomoyo_kernel_domain");
337 * tomoyo_read_memory_counter - Check for memory usage in bytes.
339 * @head: Pointer to "struct tomoyo_io_buffer".
341 * Returns memory usage.
343 int tomoyo_read_memory_counter(struct tomoyo_io_buffer
*head
)
345 if (!head
->read_eof
) {
346 const unsigned int policy
347 = atomic_read(&tomoyo_policy_memory_size
);
350 memset(buffer
, 0, sizeof(buffer
));
351 if (tomoyo_quota_for_policy
)
352 snprintf(buffer
, sizeof(buffer
) - 1,
354 tomoyo_quota_for_policy
);
357 tomoyo_io_printf(head
, "Policy: %10u%s\n", policy
, buffer
);
358 tomoyo_io_printf(head
, "Total: %10u\n", policy
);
359 head
->read_eof
= true;
365 * tomoyo_write_memory_quota - Set memory quota.
367 * @head: Pointer to "struct tomoyo_io_buffer".
371 int tomoyo_write_memory_quota(struct tomoyo_io_buffer
*head
)
373 char *data
= head
->write_buf
;
376 if (sscanf(data
, "Policy: %u", &size
) == 1)
377 tomoyo_quota_for_policy
= size
;