diff options
Diffstat (limited to 'security/tomoyo/realpath.c')
-rw-r--r-- | security/tomoyo/realpath.c | 426 |
1 files changed, 114 insertions, 312 deletions
diff --git a/security/tomoyo/realpath.c b/security/tomoyo/realpath.c index d1b96f019621..ed8ccd680102 100644 --- a/security/tomoyo/realpath.c +++ b/security/tomoyo/realpath.c | |||
@@ -1,174 +1,164 @@ | |||
1 | /* | 1 | /* |
2 | * security/tomoyo/realpath.c | 2 | * security/tomoyo/realpath.c |
3 | * | 3 | * |
4 | * Get the canonicalized absolute pathnames. The basis for TOMOYO. | 4 | * Pathname calculation functions for TOMOYO. |
5 | * | ||
6 | * Copyright (C) 2005-2009 NTT DATA CORPORATION | ||
7 | * | ||
8 | * Version: 2.2.0 2009/04/01 | ||
9 | * | 5 | * |
6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION | ||
10 | */ | 7 | */ |
11 | 8 | ||
12 | #include <linux/types.h> | 9 | #include <linux/types.h> |
13 | #include <linux/mount.h> | 10 | #include <linux/mount.h> |
14 | #include <linux/mnt_namespace.h> | 11 | #include <linux/mnt_namespace.h> |
15 | #include <linux/fs_struct.h> | 12 | #include <linux/fs_struct.h> |
16 | #include <linux/hash.h> | ||
17 | #include <linux/magic.h> | 13 | #include <linux/magic.h> |
18 | #include <linux/slab.h> | 14 | #include <linux/slab.h> |
15 | #include <net/sock.h> | ||
19 | #include "common.h" | 16 | #include "common.h" |
20 | 17 | ||
21 | /** | 18 | /** |
22 | * tomoyo_encode: Convert binary string to ascii string. | 19 | * tomoyo_encode: Convert binary string to ascii string. |
23 | * | 20 | * |
24 | * @buffer: Buffer for ASCII string. | 21 | * @str: String in binary format. |
25 | * @buflen: Size of @buffer. | 22 | * |
26 | * @str: Binary string. | 23 | * Returns pointer to @str in ascii format on success, NULL otherwise. |
27 | * | 24 | * |
28 | * Returns 0 on success, -ENOMEM otherwise. | 25 | * This function uses kzalloc(), so caller must kfree() if this function |
26 | * didn't return NULL. | ||
29 | */ | 27 | */ |
30 | int tomoyo_encode(char *buffer, int buflen, const char *str) | 28 | char *tomoyo_encode(const char *str) |
31 | { | 29 | { |
32 | while (1) { | 30 | int len = 0; |
33 | const unsigned char c = *(unsigned char *) str++; | 31 | const char *p = str; |
32 | char *cp; | ||
33 | char *cp0; | ||
34 | 34 | ||
35 | if (tomoyo_is_valid(c)) { | 35 | if (!p) |
36 | if (--buflen <= 0) | 36 | return NULL; |
37 | break; | 37 | while (*p) { |
38 | *buffer++ = (char) c; | 38 | const unsigned char c = *p++; |
39 | if (c != '\\') | 39 | if (c == '\\') |
40 | continue; | 40 | len += 2; |
41 | if (--buflen <= 0) | 41 | else if (c > ' ' && c < 127) |
42 | break; | 42 | len++; |
43 | *buffer++ = (char) c; | 43 | else |
44 | continue; | 44 | len += 4; |
45 | } | 45 | } |
46 | if (!c) { | 46 | len++; |
47 | if (--buflen <= 0) | 47 | /* Reserve space for appending "/". */ |
48 | break; | 48 | cp = kzalloc(len + 10, GFP_NOFS); |
49 | *buffer = '\0'; | 49 | if (!cp) |
50 | return 0; | 50 | return NULL; |
51 | cp0 = cp; | ||
52 | p = str; | ||
53 | while (*p) { | ||
54 | const unsigned char c = *p++; | ||
55 | |||
56 | if (c == '\\') { | ||
57 | *cp++ = '\\'; | ||
58 | *cp++ = '\\'; | ||
59 | } else if (c > ' ' && c < 127) { | ||
60 | *cp++ = c; | ||
61 | } else { | ||
62 | *cp++ = '\\'; | ||
63 | *cp++ = (c >> 6) + '0'; | ||
64 | *cp++ = ((c >> 3) & 7) + '0'; | ||
65 | *cp++ = (c & 7) + '0'; | ||
51 | } | 66 | } |
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 | } | 67 | } |
60 | return -ENOMEM; | 68 | return cp0; |
61 | } | 69 | } |
62 | 70 | ||
63 | /** | 71 | /** |
64 | * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root. | 72 | * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root. |
65 | * | 73 | * |
66 | * @path: Pointer to "struct path". | 74 | * @path: Pointer to "struct path". |
67 | * @newname: Pointer to buffer to return value in. | ||
68 | * @newname_len: Size of @newname. | ||
69 | * | 75 | * |
70 | * Returns 0 on success, negative value otherwise. | 76 | * Returns the realpath of the given @path on success, NULL otherwise. |
71 | * | 77 | * |
72 | * If dentry is a directory, trailing '/' is appended. | 78 | * If dentry is a directory, trailing '/' is appended. |
73 | * Characters out of 0x20 < c < 0x7F range are converted to | 79 | * Characters out of 0x20 < c < 0x7F range are converted to |
74 | * \ooo style octal string. | 80 | * \ooo style octal string. |
75 | * Character \ is converted to \\ string. | 81 | * Character \ is converted to \\ string. |
82 | * | ||
83 | * These functions use kzalloc(), so the caller must call kfree() | ||
84 | * if these functions didn't return NULL. | ||
76 | */ | 85 | */ |
77 | int tomoyo_realpath_from_path2(struct path *path, char *newname, | 86 | char *tomoyo_realpath_from_path(struct path *path) |
78 | int newname_len) | ||
79 | { | 87 | { |
80 | int error = -ENOMEM; | 88 | char *buf = NULL; |
89 | char *name = NULL; | ||
90 | unsigned int buf_len = PAGE_SIZE / 2; | ||
81 | struct dentry *dentry = path->dentry; | 91 | struct dentry *dentry = path->dentry; |
82 | char *sp; | 92 | bool is_dir; |
83 | 93 | if (!dentry) | |
84 | if (!dentry || !path->mnt || !newname || newname_len <= 2048) | 94 | return NULL; |
85 | return -EINVAL; | 95 | is_dir = dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode); |
86 | if (dentry->d_op && dentry->d_op->d_dname) { | 96 | while (1) { |
97 | struct path ns_root = { .mnt = NULL, .dentry = NULL }; | ||
98 | char *pos; | ||
99 | buf_len <<= 1; | ||
100 | kfree(buf); | ||
101 | buf = kmalloc(buf_len, GFP_NOFS); | ||
102 | if (!buf) | ||
103 | break; | ||
104 | /* Get better name for socket. */ | ||
105 | if (dentry->d_sb && dentry->d_sb->s_magic == SOCKFS_MAGIC) { | ||
106 | struct inode *inode = dentry->d_inode; | ||
107 | struct socket *sock = inode ? SOCKET_I(inode) : NULL; | ||
108 | struct sock *sk = sock ? sock->sk : NULL; | ||
109 | if (sk) { | ||
110 | snprintf(buf, buf_len - 1, "socket:[family=%u:" | ||
111 | "type=%u:protocol=%u]", sk->sk_family, | ||
112 | sk->sk_type, sk->sk_protocol); | ||
113 | } else { | ||
114 | snprintf(buf, buf_len - 1, "socket:[unknown]"); | ||
115 | } | ||
116 | name = tomoyo_encode(buf); | ||
117 | break; | ||
118 | } | ||
87 | /* For "socket:[\$]" and "pipe:[\$]". */ | 119 | /* For "socket:[\$]" and "pipe:[\$]". */ |
88 | static const int offset = 1536; | 120 | if (dentry->d_op && dentry->d_op->d_dname) { |
89 | sp = dentry->d_op->d_dname(dentry, newname + offset, | 121 | pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1); |
90 | newname_len - offset); | 122 | if (IS_ERR(pos)) |
91 | } else { | 123 | continue; |
92 | struct path ns_root = {.mnt = NULL, .dentry = NULL}; | 124 | name = tomoyo_encode(pos); |
93 | 125 | break; | |
126 | } | ||
127 | /* If we don't have a vfsmount, we can't calculate. */ | ||
128 | if (!path->mnt) | ||
129 | break; | ||
94 | spin_lock(&dcache_lock); | 130 | spin_lock(&dcache_lock); |
95 | /* go to whatever namespace root we are under */ | 131 | /* go to whatever namespace root we are under */ |
96 | sp = __d_path(path, &ns_root, newname, newname_len); | 132 | pos = __d_path(path, &ns_root, buf, buf_len); |
97 | spin_unlock(&dcache_lock); | 133 | spin_unlock(&dcache_lock); |
98 | /* Prepend "/proc" prefix if using internal proc vfs mount. */ | 134 | /* Prepend "/proc" prefix if using internal proc vfs mount. */ |
99 | if (!IS_ERR(sp) && (path->mnt->mnt_flags & MNT_INTERNAL) && | 135 | if (!IS_ERR(pos) && (path->mnt->mnt_flags & MNT_INTERNAL) && |
100 | (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) { | 136 | (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) { |
101 | sp -= 5; | 137 | pos -= 5; |
102 | if (sp >= newname) | 138 | if (pos >= buf) |
103 | memcpy(sp, "/proc", 5); | 139 | memcpy(pos, "/proc", 5); |
104 | else | 140 | else |
105 | sp = ERR_PTR(-ENOMEM); | 141 | pos = 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 | } | 142 | } |
143 | if (IS_ERR(pos)) | ||
144 | continue; | ||
145 | name = tomoyo_encode(pos); | ||
146 | break; | ||
124 | } | 147 | } |
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 kzalloc(), so the caller must call kfree() | ||
138 | * if these functions didn't return NULL. | ||
139 | */ | ||
140 | char *tomoyo_realpath_from_path(struct path *path) | ||
141 | { | ||
142 | char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_NOFS); | ||
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 | kfree(buf); | 148 | kfree(buf); |
152 | return NULL; | 149 | if (!name) |
153 | } | 150 | tomoyo_warn_oom(__func__); |
154 | 151 | else if (is_dir && *name) { | |
155 | /** | 152 | /* Append trailing '/' if dentry is a directory. */ |
156 | * tomoyo_realpath - Get realpath of a pathname. | 153 | char *pos = name + strlen(name) - 1; |
157 | * | 154 | if (*pos != '/') |
158 | * @pathname: The pathname to solve. | 155 | /* |
159 | * | 156 | * This is OK because tomoyo_encode() reserves space |
160 | * Returns the realpath of @pathname on success, NULL otherwise. | 157 | * for appending "/". |
161 | */ | 158 | */ |
162 | char *tomoyo_realpath(const char *pathname) | 159 | *++pos = '/'; |
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 | } | 160 | } |
171 | return NULL; | 161 | return name; |
172 | } | 162 | } |
173 | 163 | ||
174 | /** | 164 | /** |
@@ -189,191 +179,3 @@ char *tomoyo_realpath_nofollow(const char *pathname) | |||
189 | } | 179 | } |
190 | return NULL; | 180 | return NULL; |
191 | } | 181 | } |
192 | |||
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; | ||
197 | |||
198 | /** | ||
199 | * tomoyo_memory_ok - Check memory quota. | ||
200 | * | ||
201 | * @ptr: Pointer to allocated memory. | ||
202 | * | ||
203 | * Returns true on success, false otherwise. | ||
204 | * | ||
205 | * Caller holds tomoyo_policy_lock. | ||
206 | * Memory pointed by @ptr will be zeroed on success. | ||
207 | */ | ||
208 | bool tomoyo_memory_ok(void *ptr) | ||
209 | { | ||
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); | ||
216 | return true; | ||
217 | } | ||
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"); | ||
222 | return false; | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * tomoyo_commit_ok - Check memory quota. | ||
227 | * | ||
228 | * @data: Data to copy from. | ||
229 | * @size: Size in byte. | ||
230 | * | ||
231 | * Returns pointer to allocated memory on success, NULL otherwise. | ||
232 | */ | ||
233 | void *tomoyo_commit_ok(void *data, const unsigned int size) | ||
234 | { | ||
235 | void *ptr = kzalloc(size, GFP_NOFS); | ||
236 | if (tomoyo_memory_ok(ptr)) { | ||
237 | memmove(ptr, data, size); | ||
238 | memset(data, 0, size); | ||
239 | return ptr; | ||
240 | } | ||
241 | return NULL; | ||
242 | } | ||
243 | |||
244 | /** | ||
245 | * tomoyo_memory_free - Free memory for elements. | ||
246 | * | ||
247 | * @ptr: Pointer to allocated memory. | ||
248 | */ | ||
249 | void tomoyo_memory_free(void *ptr) | ||
250 | { | ||
251 | atomic_sub(ksize(ptr), &tomoyo_policy_memory_size); | ||
252 | kfree(ptr); | ||
253 | } | ||
254 | |||
255 | /* | ||
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 *". | ||
260 | */ | ||
261 | struct list_head tomoyo_name_list[TOMOYO_MAX_HASH]; | ||
262 | |||
263 | /** | ||
264 | * tomoyo_get_name - Allocate permanent memory for string data. | ||
265 | * | ||
266 | * @name: The string to store into the permernent memory. | ||
267 | * | ||
268 | * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise. | ||
269 | */ | ||
270 | const struct tomoyo_path_info *tomoyo_get_name(const char *name) | ||
271 | { | ||
272 | struct tomoyo_name_entry *ptr; | ||
273 | unsigned int hash; | ||
274 | int len; | ||
275 | int allocated_len; | ||
276 | struct list_head *head; | ||
277 | |||
278 | if (!name) | ||
279 | return NULL; | ||
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)) | ||
284 | return NULL; | ||
285 | list_for_each_entry(ptr, head, list) { | ||
286 | if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name)) | ||
287 | continue; | ||
288 | atomic_inc(&ptr->users); | ||
289 | goto out; | ||
290 | } | ||
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)) { | ||
296 | kfree(ptr); | ||
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"); | ||
301 | ptr = NULL; | ||
302 | goto out; | ||
303 | } | ||
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); | ||
310 | out: | ||
311 | mutex_unlock(&tomoyo_policy_lock); | ||
312 | return ptr ? &ptr->entry : NULL; | ||
313 | } | ||
314 | |||
315 | /** | ||
316 | * tomoyo_realpath_init - Initialize realpath related code. | ||
317 | */ | ||
318 | void __init tomoyo_realpath_init(void) | ||
319 | { | ||
320 | int i; | ||
321 | |||
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); | ||
327 | /* | ||
328 | * tomoyo_read_lock() is not needed because this function is | ||
329 | * called before the first "delete" request. | ||
330 | */ | ||
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"); | ||
334 | } | ||
335 | |||
336 | /** | ||
337 | * tomoyo_read_memory_counter - Check for memory usage in bytes. | ||
338 | * | ||
339 | * @head: Pointer to "struct tomoyo_io_buffer". | ||
340 | * | ||
341 | * Returns memory usage. | ||
342 | */ | ||
343 | int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head) | ||
344 | { | ||
345 | if (!head->read_eof) { | ||
346 | const unsigned int policy | ||
347 | = atomic_read(&tomoyo_policy_memory_size); | ||
348 | char buffer[64]; | ||
349 | |||
350 | memset(buffer, 0, sizeof(buffer)); | ||
351 | if (tomoyo_quota_for_policy) | ||
352 | snprintf(buffer, sizeof(buffer) - 1, | ||
353 | " (Quota: %10u)", | ||
354 | tomoyo_quota_for_policy); | ||
355 | else | ||
356 | buffer[0] = '\0'; | ||
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; | ||
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 | */ | ||
371 | int 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, "Policy: %u", &size) == 1) | ||
377 | tomoyo_quota_for_policy = size; | ||
378 | return 0; | ||
379 | } | ||