1 |
kumaneko |
111 |
/* |
2 |
|
|
* fs/realpath.c |
3 |
|
|
* |
4 |
|
|
* Get the canonicalized absolute pathnames. The basis for SAKURA and TOMOYO. |
5 |
|
|
* |
6 |
kumaneko |
856 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
kumaneko |
111 |
* |
8 |
kumaneko |
1785 |
* Version: 1.6.5-rc 2008/11/05 |
9 |
kumaneko |
111 |
* |
10 |
|
|
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
|
|
* See README.ccs for ChangeLog. |
12 |
|
|
* |
13 |
|
|
*/ |
14 |
|
|
#include <linux/string.h> |
15 |
|
|
#include <linux/mm.h> |
16 |
|
|
#include <linux/utime.h> |
17 |
|
|
#include <linux/file.h> |
18 |
|
|
#include <linux/smp_lock.h> |
19 |
|
|
#include <linux/module.h> |
20 |
|
|
#include <linux/slab.h> |
21 |
|
|
#include <asm/uaccess.h> |
22 |
|
|
#include <asm/atomic.h> |
23 |
|
|
#include <linux/version.h> |
24 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
25 |
kumaneko |
111 |
#include <linux/namei.h> |
26 |
|
|
#include <linux/mount.h> |
27 |
|
|
static const int lookup_flags = LOOKUP_FOLLOW; |
28 |
|
|
#else |
29 |
|
|
static const int lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE; |
30 |
|
|
#endif |
31 |
|
|
#include <linux/realpath.h> |
32 |
|
|
#include <linux/proc_fs.h> |
33 |
|
|
#include <linux/ccs_common.h> |
34 |
|
|
|
35 |
kumaneko |
1052 |
/** |
36 |
|
|
* get_absolute_path - Get the path of a dentry but ignores chroot'ed root. |
37 |
kumaneko |
111 |
* |
38 |
kumaneko |
1052 |
* @dentry: Pointer to "struct dentry". |
39 |
|
|
* @vfsmnt: Pointer to "struct vfsmount". |
40 |
|
|
* @buffer: Pointer to buffer to return value in. |
41 |
|
|
* @buflen: Sizeof @buffer. |
42 |
|
|
* |
43 |
|
|
* Returns 0 on success, -ENOMEM otherwise. |
44 |
|
|
* |
45 |
kumaneko |
1064 |
* Caller holds the dcache_lock and vfsmount_lock. |
46 |
kumaneko |
111 |
* Based on __d_path() in fs/dcache.c |
47 |
|
|
* |
48 |
|
|
* If dentry is a directory, trailing '/' is appended. |
49 |
kumaneko |
1052 |
* Characters out of 0x20 < c < 0x7F range are converted to |
50 |
|
|
* \ooo style octal string. |
51 |
kumaneko |
111 |
* Character \ is converted to \\ string. |
52 |
|
|
*/ |
53 |
kumaneko |
1052 |
static int get_absolute_path(struct dentry *dentry, struct vfsmount *vfsmnt, |
54 |
|
|
char *buffer, int buflen) |
55 |
kumaneko |
111 |
{ |
56 |
kumaneko |
1052 |
/***** CRITICAL SECTION START *****/ |
57 |
kumaneko |
111 |
char *start = buffer; |
58 |
|
|
char *end = buffer + buflen; |
59 |
kumaneko |
621 |
bool is_dir = (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)); |
60 |
kumaneko |
111 |
|
61 |
kumaneko |
1052 |
if (buflen < 256) |
62 |
|
|
goto out; |
63 |
kumaneko |
111 |
|
64 |
|
|
*--end = '\0'; |
65 |
|
|
buflen--; |
66 |
|
|
|
67 |
|
|
for (;;) { |
68 |
|
|
struct dentry *parent; |
69 |
|
|
|
70 |
|
|
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
71 |
|
|
/* Global root? */ |
72 |
kumaneko |
1052 |
if (vfsmnt->mnt_parent == vfsmnt) |
73 |
kumaneko |
111 |
break; |
74 |
|
|
dentry = vfsmnt->mnt_mountpoint; |
75 |
|
|
vfsmnt = vfsmnt->mnt_parent; |
76 |
|
|
continue; |
77 |
|
|
} |
78 |
|
|
if (is_dir) { |
79 |
kumaneko |
1052 |
is_dir = false; |
80 |
|
|
*--end = '/'; |
81 |
|
|
buflen--; |
82 |
kumaneko |
111 |
} |
83 |
|
|
parent = dentry->d_parent; |
84 |
|
|
{ |
85 |
|
|
const char *sp = dentry->d_name.name; |
86 |
|
|
const char *cp = sp + dentry->d_name.len - 1; |
87 |
|
|
unsigned char c; |
88 |
|
|
|
89 |
kumaneko |
1052 |
/* |
90 |
|
|
* Exception: Use /proc/self/ rather than |
91 |
|
|
* /proc/\$/ for current process. |
92 |
|
|
*/ |
93 |
|
|
if (IS_ROOT(parent) && *sp > '0' && *sp <= '9' && |
94 |
|
|
parent->d_sb && |
95 |
|
|
parent->d_sb->s_magic == PROC_SUPER_MAGIC) { |
96 |
kumaneko |
111 |
char *ep; |
97 |
kumaneko |
1052 |
const pid_t pid |
98 |
|
|
= (pid_t) simple_strtoul(sp, &ep, 10); |
99 |
kumaneko |
1778 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
100 |
|
|
const pid_t tgid |
101 |
|
|
= task_tgid_nr_ns(current, |
102 |
|
|
dentry->d_sb-> |
103 |
|
|
s_fs_info); |
104 |
|
|
if (!*ep && pid == tgid && tgid) { |
105 |
kumaneko |
1052 |
sp = "self"; |
106 |
|
|
cp = sp + 3; |
107 |
|
|
} |
108 |
kumaneko |
111 |
#else |
109 |
kumaneko |
1778 |
if (!*ep && pid == sys_getpid()) { |
110 |
kumaneko |
1052 |
sp = "self"; |
111 |
|
|
cp = sp + 3; |
112 |
|
|
} |
113 |
kumaneko |
111 |
#endif |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
while (sp <= cp) { |
117 |
kumaneko |
1052 |
c = *(unsigned char *) cp; |
118 |
kumaneko |
111 |
if (c == '\\') { |
119 |
|
|
buflen -= 2; |
120 |
kumaneko |
1052 |
if (buflen < 0) |
121 |
|
|
goto out; |
122 |
kumaneko |
111 |
*--end = '\\'; |
123 |
|
|
*--end = '\\'; |
124 |
|
|
} else if (c > ' ' && c < 127) { |
125 |
kumaneko |
1052 |
if (--buflen < 0) |
126 |
|
|
goto out; |
127 |
kumaneko |
111 |
*--end = (char) c; |
128 |
|
|
} else { |
129 |
|
|
buflen -= 4; |
130 |
kumaneko |
1052 |
if (buflen < 0) |
131 |
|
|
goto out; |
132 |
kumaneko |
111 |
*--end = (c & 7) + '0'; |
133 |
|
|
*--end = ((c >> 3) & 7) + '0'; |
134 |
|
|
*--end = (c >> 6) + '0'; |
135 |
|
|
*--end = '\\'; |
136 |
|
|
} |
137 |
|
|
cp--; |
138 |
|
|
} |
139 |
kumaneko |
1052 |
if (--buflen < 0) |
140 |
|
|
goto out; |
141 |
kumaneko |
111 |
*--end = '/'; |
142 |
|
|
} |
143 |
|
|
dentry = parent; |
144 |
|
|
} |
145 |
kumaneko |
1052 |
if (*end == '/') { |
146 |
|
|
buflen++; |
147 |
|
|
end++; |
148 |
|
|
} |
149 |
kumaneko |
111 |
{ |
150 |
|
|
const char *sp = dentry->d_name.name; |
151 |
|
|
const char *cp = sp + dentry->d_name.len - 1; |
152 |
|
|
unsigned char c; |
153 |
|
|
while (sp <= cp) { |
154 |
kumaneko |
1052 |
c = *(unsigned char *) cp; |
155 |
kumaneko |
111 |
if (c == '\\') { |
156 |
|
|
buflen -= 2; |
157 |
kumaneko |
1052 |
if (buflen < 0) |
158 |
|
|
goto out; |
159 |
kumaneko |
111 |
*--end = '\\'; |
160 |
|
|
*--end = '\\'; |
161 |
|
|
} else if (c > ' ' && c < 127) { |
162 |
kumaneko |
1052 |
if (--buflen < 0) |
163 |
|
|
goto out; |
164 |
kumaneko |
111 |
*--end = (char) c; |
165 |
|
|
} else { |
166 |
|
|
buflen -= 4; |
167 |
kumaneko |
1052 |
if (buflen < 0) |
168 |
|
|
goto out; |
169 |
kumaneko |
111 |
*--end = (c & 7) + '0'; |
170 |
|
|
*--end = ((c >> 3) & 7) + '0'; |
171 |
|
|
*--end = (c >> 6) + '0'; |
172 |
|
|
*--end = '\\'; |
173 |
|
|
} |
174 |
|
|
cp--; |
175 |
|
|
} |
176 |
|
|
} |
177 |
|
|
/* Move the pathname to the top of the buffer. */ |
178 |
|
|
memmove(start, end, strlen(end) + 1); |
179 |
|
|
return 0; |
180 |
|
|
out: |
181 |
|
|
return -ENOMEM; |
182 |
kumaneko |
1052 |
/***** CRITICAL SECTION END *****/ |
183 |
kumaneko |
111 |
} |
184 |
|
|
|
185 |
kumaneko |
1052 |
/** |
186 |
kumaneko |
1054 |
* ccs_realpath_from_dentry2 - Returns realpath(3) of the given dentry but ignores chroot'ed root. |
187 |
kumaneko |
1052 |
* |
188 |
|
|
* @dentry: Pointer to "struct dentry". |
189 |
|
|
* @mnt: Pointer to "struct vfsmount". |
190 |
|
|
* @newname: Pointer to buffer to return value in. |
191 |
kumaneko |
1064 |
* @newname_len: Size of @newname. |
192 |
kumaneko |
1052 |
* |
193 |
|
|
* Returns 0 on success, negative value otherwise. |
194 |
|
|
*/ |
195 |
|
|
int ccs_realpath_from_dentry2(struct dentry *dentry, struct vfsmount *mnt, |
196 |
|
|
char *newname, int newname_len) |
197 |
kumaneko |
111 |
{ |
198 |
kumaneko |
1379 |
int error = -EINVAL; |
199 |
kumaneko |
111 |
struct dentry *d_dentry; |
200 |
|
|
struct vfsmount *d_mnt; |
201 |
kumaneko |
1379 |
if (!dentry || !newname || newname_len <= 2048) |
202 |
|
|
goto out; |
203 |
kumaneko |
1259 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) |
204 |
|
|
if (dentry->d_op && dentry->d_op->d_dname) { |
205 |
|
|
/* For "socket:[\$]" and "pipe:[\$]". */ |
206 |
|
|
static const int offset = 1536; |
207 |
|
|
char *dp = newname; |
208 |
|
|
char *sp = dentry->d_op->d_dname(dentry, newname + offset, |
209 |
|
|
newname_len - offset); |
210 |
|
|
if (IS_ERR(sp)) { |
211 |
|
|
error = PTR_ERR(sp); |
212 |
|
|
goto out; |
213 |
|
|
} |
214 |
|
|
error = -ENOMEM; |
215 |
|
|
newname += offset; |
216 |
|
|
while (1) { |
217 |
|
|
const unsigned char c = *(unsigned char *) sp++; |
218 |
|
|
if (c == '\\') { |
219 |
|
|
if (dp + 2 >= newname) |
220 |
|
|
break; |
221 |
|
|
*dp++ = '\\'; |
222 |
|
|
*dp++ = '\\'; |
223 |
|
|
} else if (c > ' ' && c < 127) { |
224 |
|
|
if (dp + 1 >= newname) |
225 |
|
|
break; |
226 |
|
|
*dp++ = (char) c; |
227 |
|
|
} else if (c) { |
228 |
|
|
if (dp + 4 >= newname) |
229 |
|
|
break; |
230 |
|
|
*dp++ = '\\'; |
231 |
|
|
*dp++ = (c >> 6) + '0'; |
232 |
|
|
*dp++ = ((c >> 3) & 7) + '0'; |
233 |
|
|
*dp++ = (c & 7) + '0'; |
234 |
|
|
} else { |
235 |
|
|
*dp = '\0'; |
236 |
|
|
return 0; |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
goto out; |
240 |
|
|
} |
241 |
|
|
#endif |
242 |
kumaneko |
1379 |
if (!mnt) |
243 |
|
|
goto out; |
244 |
kumaneko |
111 |
d_dentry = dget(dentry); |
245 |
|
|
d_mnt = mntget(mnt); |
246 |
|
|
/***** CRITICAL SECTION START *****/ |
247 |
kumaneko |
1474 |
ccs_realpath_lock(); |
248 |
kumaneko |
1052 |
error = get_absolute_path(d_dentry, d_mnt, newname, newname_len); |
249 |
kumaneko |
1474 |
ccs_realpath_unlock(); |
250 |
kumaneko |
111 |
/***** CRITICAL SECTION END *****/ |
251 |
|
|
dput(d_dentry); |
252 |
|
|
mntput(d_mnt); |
253 |
kumaneko |
1259 |
out: |
254 |
kumaneko |
1056 |
if (error) |
255 |
kumaneko |
1379 |
printk(KERN_WARNING "ccs_realpath: Pathname too long. (%d)\n", |
256 |
|
|
error); |
257 |
kumaneko |
111 |
return error; |
258 |
|
|
} |
259 |
|
|
|
260 |
kumaneko |
1052 |
/** |
261 |
kumaneko |
1054 |
* ccs_realpath_from_dentry - Returns realpath(3) of the given pathname but ignores chroot'ed root. |
262 |
kumaneko |
1052 |
* |
263 |
|
|
* @dentry: Pointer to "struct dentry". |
264 |
|
|
* @mnt: Pointer to "struct vfsmount". |
265 |
|
|
* |
266 |
|
|
* Returns the realpath of the given @dentry and @mnt on success, |
267 |
|
|
* NULL otherwise. |
268 |
|
|
* |
269 |
|
|
* These functions use ccs_alloc(), so caller must ccs_free() |
270 |
|
|
* if these functions didn't return NULL. |
271 |
|
|
*/ |
272 |
|
|
char *ccs_realpath_from_dentry(struct dentry *dentry, struct vfsmount *mnt) |
273 |
kumaneko |
111 |
{ |
274 |
kumaneko |
1031 |
char *buf = ccs_alloc(sizeof(struct ccs_page_buffer)); |
275 |
kumaneko |
1052 |
if (buf && ccs_realpath_from_dentry2(dentry, mnt, buf, |
276 |
|
|
CCS_MAX_PATHNAME_LEN - 1) == 0) |
277 |
|
|
return buf; |
278 |
kumaneko |
111 |
ccs_free(buf); |
279 |
|
|
return NULL; |
280 |
|
|
} |
281 |
|
|
|
282 |
kumaneko |
1052 |
/** |
283 |
|
|
* ccs_realpath - Get realpath of a pathname. |
284 |
|
|
* |
285 |
|
|
* @pathname: The pathname to solve. |
286 |
|
|
* |
287 |
|
|
* Returns the realpath of @pathname on success, NULL otherwise. |
288 |
|
|
*/ |
289 |
|
|
char *ccs_realpath(const char *pathname) |
290 |
kumaneko |
111 |
{ |
291 |
|
|
struct nameidata nd; |
292 |
|
|
if (pathname && path_lookup(pathname, lookup_flags, &nd) == 0) { |
293 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
294 |
|
|
char *buf = ccs_realpath_from_dentry(nd.path.dentry, |
295 |
|
|
nd.path.mnt); |
296 |
kumaneko |
990 |
path_put(&nd.path); |
297 |
|
|
#else |
298 |
kumaneko |
1052 |
char *buf = ccs_realpath_from_dentry(nd.dentry, nd.mnt); |
299 |
kumaneko |
111 |
path_release(&nd); |
300 |
kumaneko |
990 |
#endif |
301 |
kumaneko |
111 |
return buf; |
302 |
|
|
} |
303 |
|
|
return NULL; |
304 |
|
|
} |
305 |
|
|
|
306 |
kumaneko |
1052 |
/** |
307 |
|
|
* ccs_realpath_nofollow - Get realpath of a pathname. |
308 |
|
|
* |
309 |
|
|
* @pathname: The pathname to solve. |
310 |
|
|
* |
311 |
|
|
* Returns the realpath of @pathname on success, NULL otherwise. |
312 |
|
|
*/ |
313 |
|
|
char *ccs_realpath_nofollow(const char *pathname) |
314 |
kumaneko |
111 |
{ |
315 |
|
|
struct nameidata nd; |
316 |
kumaneko |
1052 |
if (pathname && path_lookup(pathname, lookup_flags ^ LOOKUP_FOLLOW, |
317 |
|
|
&nd) == 0) { |
318 |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
319 |
|
|
char *buf = ccs_realpath_from_dentry(nd.path.dentry, |
320 |
|
|
nd.path.mnt); |
321 |
kumaneko |
990 |
path_put(&nd.path); |
322 |
|
|
#else |
323 |
kumaneko |
1052 |
char *buf = ccs_realpath_from_dentry(nd.dentry, nd.mnt); |
324 |
kumaneko |
111 |
path_release(&nd); |
325 |
kumaneko |
990 |
#endif |
326 |
kumaneko |
111 |
return buf; |
327 |
|
|
} |
328 |
|
|
return NULL; |
329 |
|
|
} |
330 |
|
|
|
331 |
kumaneko |
1052 |
/** |
332 |
kumaneko |
1054 |
* round_up - Round up an integer so that the returned pointers are appropriately aligned. |
333 |
kumaneko |
1052 |
* |
334 |
|
|
* @size: Size in bytes. |
335 |
|
|
* |
336 |
|
|
* Returns rounded value of @size. |
337 |
|
|
* |
338 |
|
|
* FIXME: Are there more requirements that is needed for assigning value |
339 |
|
|
* atomically? |
340 |
kumaneko |
111 |
*/ |
341 |
kumaneko |
1052 |
static inline unsigned int round_up(const unsigned int size) |
342 |
|
|
{ |
343 |
|
|
if (sizeof(void *) >= sizeof(long)) |
344 |
|
|
return ((size + sizeof(void *) - 1) |
345 |
|
|
/ sizeof(void *)) * sizeof(void *); |
346 |
|
|
else |
347 |
|
|
return ((size + sizeof(long) - 1) |
348 |
|
|
/ sizeof(long)) * sizeof(long); |
349 |
kumaneko |
111 |
} |
350 |
|
|
|
351 |
kumaneko |
1052 |
static unsigned int allocated_memory_for_elements; |
352 |
kumaneko |
1180 |
static unsigned int quota_for_elements; |
353 |
kumaneko |
111 |
|
354 |
kumaneko |
1052 |
/** |
355 |
|
|
* ccs_alloc_element - Allocate permanent memory for structures. |
356 |
|
|
* |
357 |
|
|
* @size: Size in bytes. |
358 |
|
|
* |
359 |
|
|
* Returns pointer to allocated memory on success, NULL otherwise. |
360 |
|
|
* |
361 |
|
|
* The RAM is chunked, so NEVER try to kfree() the returned pointer. |
362 |
|
|
*/ |
363 |
|
|
void *ccs_alloc_element(const unsigned int size) |
364 |
kumaneko |
111 |
{ |
365 |
kumaneko |
652 |
static DEFINE_MUTEX(lock); |
366 |
kumaneko |
1052 |
static char *buf; |
367 |
kumaneko |
111 |
static unsigned int buf_used_len = PAGE_SIZE; |
368 |
|
|
char *ptr = NULL; |
369 |
kumaneko |
1052 |
const unsigned int word_aligned_size = round_up(size); |
370 |
|
|
if (word_aligned_size > PAGE_SIZE) |
371 |
|
|
return NULL; |
372 |
kumaneko |
652 |
mutex_lock(&lock); |
373 |
kumaneko |
111 |
if (buf_used_len + word_aligned_size > PAGE_SIZE) { |
374 |
kumaneko |
1180 |
if (!quota_for_elements || allocated_memory_for_elements |
375 |
|
|
+ PAGE_SIZE <= quota_for_elements) |
376 |
|
|
ptr = kzalloc(PAGE_SIZE, GFP_KERNEL); |
377 |
kumaneko |
1052 |
if (!ptr) { |
378 |
|
|
printk(KERN_WARNING "ERROR: Out of memory " |
379 |
|
|
"for ccs_alloc_element().\n"); |
380 |
|
|
if (!sbin_init_started) |
381 |
|
|
panic("MAC Initialization failed.\n"); |
382 |
kumaneko |
111 |
} else { |
383 |
|
|
buf = ptr; |
384 |
|
|
allocated_memory_for_elements += PAGE_SIZE; |
385 |
|
|
buf_used_len = word_aligned_size; |
386 |
|
|
ptr = buf; |
387 |
|
|
} |
388 |
|
|
} else if (word_aligned_size) { |
389 |
|
|
int i; |
390 |
|
|
ptr = buf + buf_used_len; |
391 |
|
|
buf_used_len += word_aligned_size; |
392 |
|
|
for (i = 0; i < word_aligned_size; i++) { |
393 |
kumaneko |
1052 |
if (!ptr[i]) |
394 |
|
|
continue; |
395 |
|
|
printk(KERN_ERR "WARNING: Reserved memory was tainted! " |
396 |
|
|
"The system might go wrong.\n"); |
397 |
|
|
ptr[i] = '\0'; |
398 |
kumaneko |
111 |
} |
399 |
|
|
} |
400 |
kumaneko |
652 |
mutex_unlock(&lock); |
401 |
kumaneko |
111 |
return ptr; |
402 |
|
|
} |
403 |
|
|
|
404 |
kumaneko |
1052 |
static unsigned int allocated_memory_for_savename; |
405 |
kumaneko |
1180 |
static unsigned int quota_for_savename; |
406 |
kumaneko |
111 |
|
407 |
|
|
#define MAX_HASH 256 |
408 |
|
|
|
409 |
kumaneko |
1052 |
/* Structure for string data. */ |
410 |
kumaneko |
214 |
struct name_entry { |
411 |
kumaneko |
731 |
struct list1_head list; |
412 |
kumaneko |
111 |
struct path_info entry; |
413 |
kumaneko |
214 |
}; |
414 |
kumaneko |
111 |
|
415 |
kumaneko |
1052 |
/* Structure for available memory region. */ |
416 |
kumaneko |
214 |
struct free_memory_block_list { |
417 |
kumaneko |
731 |
struct list_head list; |
418 |
kumaneko |
1052 |
char *ptr; /* Pointer to a free area. */ |
419 |
|
|
int len; /* Length of the area. */ |
420 |
kumaneko |
214 |
}; |
421 |
kumaneko |
111 |
|
422 |
kumaneko |
1052 |
/* The list for "struct name_entry". */ |
423 |
|
|
static struct list1_head name_list[MAX_HASH]; |
424 |
kumaneko |
731 |
|
425 |
kumaneko |
1052 |
/** |
426 |
|
|
* ccs_save_name - Allocate permanent memory for string data. |
427 |
|
|
* |
428 |
|
|
* @name: The string to store into the permernent memory. |
429 |
|
|
* |
430 |
|
|
* Returns pointer to "struct path_info" on success, NULL otherwise. |
431 |
|
|
* |
432 |
|
|
* The RAM is shared, so NEVER try to modify or kfree() the returned name. |
433 |
|
|
*/ |
434 |
|
|
const struct path_info *ccs_save_name(const char *name) |
435 |
kumaneko |
111 |
{ |
436 |
kumaneko |
731 |
static LIST_HEAD(fmb_list); |
437 |
kumaneko |
652 |
static DEFINE_MUTEX(lock); |
438 |
kumaneko |
731 |
struct name_entry *ptr; |
439 |
kumaneko |
111 |
unsigned int hash; |
440 |
kumaneko |
731 |
struct free_memory_block_list *fmb; |
441 |
kumaneko |
111 |
int len; |
442 |
kumaneko |
731 |
char *cp; |
443 |
kumaneko |
1052 |
if (!name) |
444 |
|
|
return NULL; |
445 |
kumaneko |
111 |
len = strlen(name) + 1; |
446 |
|
|
if (len > CCS_MAX_PATHNAME_LEN) { |
447 |
kumaneko |
1052 |
printk(KERN_WARNING "ERROR: Name too long " |
448 |
|
|
"for ccs_save_name().\n"); |
449 |
kumaneko |
111 |
return NULL; |
450 |
|
|
} |
451 |
|
|
hash = full_name_hash((const unsigned char *) name, len - 1); |
452 |
kumaneko |
652 |
mutex_lock(&lock); |
453 |
kumaneko |
731 |
list1_for_each_entry(ptr, &name_list[hash % MAX_HASH], list) { |
454 |
kumaneko |
1052 |
if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name)) |
455 |
|
|
goto out; |
456 |
kumaneko |
111 |
} |
457 |
kumaneko |
731 |
list_for_each_entry(fmb, &fmb_list, list) { |
458 |
kumaneko |
1052 |
if (len <= fmb->len) |
459 |
|
|
goto ready; |
460 |
kumaneko |
111 |
} |
461 |
kumaneko |
1180 |
if (!quota_for_savename || allocated_memory_for_savename + PAGE_SIZE |
462 |
|
|
<= quota_for_savename) |
463 |
|
|
cp = kzalloc(PAGE_SIZE, GFP_KERNEL); |
464 |
|
|
else |
465 |
|
|
cp = NULL; |
466 |
kumaneko |
1016 |
fmb = kzalloc(sizeof(*fmb), GFP_KERNEL); |
467 |
kumaneko |
731 |
if (!cp || !fmb) { |
468 |
|
|
kfree(cp); |
469 |
|
|
kfree(fmb); |
470 |
kumaneko |
1052 |
printk(KERN_WARNING "ERROR: Out of memory " |
471 |
|
|
"for ccs_save_name().\n"); |
472 |
|
|
if (!sbin_init_started) |
473 |
|
|
panic("MAC Initialization failed.\n"); |
474 |
kumaneko |
731 |
ptr = NULL; |
475 |
|
|
goto out; |
476 |
|
|
} |
477 |
|
|
allocated_memory_for_savename += PAGE_SIZE; |
478 |
|
|
list_add(&fmb->list, &fmb_list); |
479 |
|
|
fmb->ptr = cp; |
480 |
|
|
fmb->len = PAGE_SIZE; |
481 |
|
|
ready: |
482 |
kumaneko |
1052 |
ptr = ccs_alloc_element(sizeof(*ptr)); |
483 |
|
|
if (!ptr) |
484 |
|
|
goto out; |
485 |
kumaneko |
111 |
ptr->entry.name = fmb->ptr; |
486 |
|
|
memmove(fmb->ptr, name, len); |
487 |
kumaneko |
1052 |
ccs_fill_path_info(&ptr->entry); |
488 |
kumaneko |
111 |
fmb->ptr += len; |
489 |
|
|
fmb->len -= len; |
490 |
kumaneko |
731 |
list1_add_tail_mb(&ptr->list, &name_list[hash % MAX_HASH]); |
491 |
kumaneko |
111 |
if (fmb->len == 0) { |
492 |
kumaneko |
731 |
list_del(&fmb->list); |
493 |
|
|
kfree(fmb); |
494 |
kumaneko |
111 |
} |
495 |
|
|
out: |
496 |
kumaneko |
652 |
mutex_unlock(&lock); |
497 |
kumaneko |
111 |
return ptr ? &ptr->entry : NULL; |
498 |
|
|
} |
499 |
|
|
|
500 |
kumaneko |
1052 |
/* Structure for temporarily allocated memory. */ |
501 |
kumaneko |
214 |
struct cache_entry { |
502 |
kumaneko |
111 |
struct list_head list; |
503 |
|
|
void *ptr; |
504 |
|
|
int size; |
505 |
kumaneko |
214 |
}; |
506 |
kumaneko |
111 |
|
507 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20) |
508 |
|
|
static struct kmem_cache *ccs_cachep; |
509 |
kumaneko |
111 |
#else |
510 |
kumaneko |
1052 |
static kmem_cache_t *ccs_cachep; |
511 |
kumaneko |
111 |
#endif |
512 |
|
|
|
513 |
kumaneko |
1052 |
/** |
514 |
|
|
* ccs_realpath_init - Initialize realpath related code. |
515 |
|
|
* |
516 |
|
|
* Returns 0. |
517 |
|
|
*/ |
518 |
|
|
static int __init ccs_realpath_init(void) |
519 |
kumaneko |
111 |
{ |
520 |
kumaneko |
731 |
int i; |
521 |
kumaneko |
1052 |
if (CCS_MAX_PATHNAME_LEN > PAGE_SIZE) |
522 |
|
|
panic("Bad size."); |
523 |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) |
524 |
|
|
ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), |
525 |
|
|
0, 0, NULL); |
526 |
kumaneko |
316 |
#else |
527 |
kumaneko |
1052 |
ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), |
528 |
|
|
0, 0, NULL, NULL); |
529 |
kumaneko |
316 |
#endif |
530 |
kumaneko |
1052 |
if (!ccs_cachep) |
531 |
|
|
panic("Can't create cache.\n"); |
532 |
|
|
for (i = 0; i < MAX_HASH; i++) |
533 |
kumaneko |
731 |
INIT_LIST1_HEAD(&name_list[i]); |
534 |
kumaneko |
722 |
INIT_LIST1_HEAD(&KERNEL_DOMAIN.acl_info_list); |
535 |
kumaneko |
1052 |
KERNEL_DOMAIN.domainname = ccs_save_name(ROOT_NAME); |
536 |
kumaneko |
722 |
list1_add_tail_mb(&KERNEL_DOMAIN.list, &domain_list); |
537 |
kumaneko |
1052 |
if (ccs_find_domain(ROOT_NAME) != &KERNEL_DOMAIN) |
538 |
|
|
panic("Can't register KERNEL_DOMAIN"); |
539 |
|
|
return 0; |
540 |
kumaneko |
111 |
} |
541 |
|
|
|
542 |
kumaneko |
1084 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
543 |
kumaneko |
1052 |
__initcall(ccs_realpath_init); |
544 |
kumaneko |
1084 |
#else |
545 |
kumaneko |
1106 |
core_initcall(ccs_realpath_init); |
546 |
kumaneko |
1084 |
#endif |
547 |
kumaneko |
1052 |
|
548 |
|
|
/* The list for "struct cache_entry". */ |
549 |
kumaneko |
111 |
static LIST_HEAD(cache_list); |
550 |
|
|
|
551 |
kumaneko |
1052 |
static DEFINE_SPINLOCK(cache_list_lock); |
552 |
|
|
|
553 |
|
|
static unsigned int dynamic_memory_size; |
554 |
|
|
|
555 |
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
556 |
|
|
/** |
557 |
|
|
* round2 - Rounded up to power-of-two value. |
558 |
|
|
* |
559 |
|
|
* @size: Size in bytes. |
560 |
|
|
* |
561 |
|
|
* Returns power-of-two of @size. |
562 |
|
|
*/ |
563 |
kumaneko |
581 |
static int round2(size_t size) |
564 |
|
|
{ |
565 |
|
|
#if PAGE_SIZE == 4096 |
566 |
|
|
size_t bsize = 32; |
567 |
|
|
#else |
568 |
|
|
size_t bsize = 64; |
569 |
|
|
#endif |
570 |
kumaneko |
1064 |
while (size > bsize) |
571 |
|
|
bsize <<= 1; |
572 |
kumaneko |
581 |
return bsize; |
573 |
|
|
} |
574 |
|
|
#endif |
575 |
|
|
|
576 |
kumaneko |
1052 |
/** |
577 |
|
|
* ccs_alloc - Allocate memory for temporal purpose. |
578 |
|
|
* |
579 |
|
|
* @size: Size in bytes. |
580 |
|
|
* |
581 |
|
|
* Returns pointer to allocated memory on success, NULL otherwise. |
582 |
|
|
*/ |
583 |
kumaneko |
214 |
void *ccs_alloc(const size_t size) |
584 |
kumaneko |
111 |
{ |
585 |
kumaneko |
1031 |
struct cache_entry *new_entry; |
586 |
kumaneko |
1055 |
void *ret = kzalloc(size, GFP_KERNEL); |
587 |
kumaneko |
1052 |
if (!ret) |
588 |
|
|
goto out; |
589 |
kumaneko |
1031 |
new_entry = kmem_cache_alloc(ccs_cachep, GFP_KERNEL); |
590 |
|
|
if (!new_entry) { |
591 |
kumaneko |
1064 |
kfree(ret); |
592 |
|
|
ret = NULL; |
593 |
kumaneko |
1031 |
goto out; |
594 |
|
|
} |
595 |
|
|
INIT_LIST_HEAD(&new_entry->list); |
596 |
|
|
new_entry->ptr = ret; |
597 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
598 |
kumaneko |
1031 |
new_entry->size = ksize(ret); |
599 |
|
|
#else |
600 |
|
|
new_entry->size = round2(size); |
601 |
|
|
#endif |
602 |
kumaneko |
1052 |
/***** CRITICAL SECTION START *****/ |
603 |
kumaneko |
1031 |
spin_lock(&cache_list_lock); |
604 |
|
|
list_add_tail(&new_entry->list, &cache_list); |
605 |
|
|
dynamic_memory_size += new_entry->size; |
606 |
|
|
spin_unlock(&cache_list_lock); |
607 |
kumaneko |
1052 |
/***** CRITICAL SECTION END *****/ |
608 |
kumaneko |
1031 |
out: |
609 |
kumaneko |
214 |
return ret; |
610 |
kumaneko |
111 |
} |
611 |
|
|
|
612 |
kumaneko |
1052 |
/** |
613 |
|
|
* ccs_free - Release memory allocated by ccs_alloc(). |
614 |
|
|
* |
615 |
|
|
* @p: Pointer returned by ccs_alloc(). May be NULL. |
616 |
|
|
* |
617 |
|
|
* Returns nothing. |
618 |
|
|
*/ |
619 |
kumaneko |
111 |
void ccs_free(const void *p) |
620 |
|
|
{ |
621 |
|
|
struct list_head *v; |
622 |
kumaneko |
214 |
struct cache_entry *entry = NULL; |
623 |
kumaneko |
1052 |
if (!p) |
624 |
|
|
return; |
625 |
|
|
/***** CRITICAL SECTION START *****/ |
626 |
kumaneko |
111 |
spin_lock(&cache_list_lock); |
627 |
|
|
list_for_each(v, &cache_list) { |
628 |
kumaneko |
214 |
entry = list_entry(v, struct cache_entry, list); |
629 |
kumaneko |
111 |
if (entry->ptr != p) { |
630 |
kumaneko |
1064 |
entry = NULL; |
631 |
|
|
continue; |
632 |
kumaneko |
111 |
} |
633 |
|
|
list_del(&entry->list); |
634 |
|
|
dynamic_memory_size -= entry->size; |
635 |
|
|
break; |
636 |
|
|
} |
637 |
|
|
spin_unlock(&cache_list_lock); |
638 |
kumaneko |
1052 |
/***** CRITICAL SECTION END *****/ |
639 |
kumaneko |
111 |
if (entry) { |
640 |
|
|
kfree(p); |
641 |
|
|
kmem_cache_free(ccs_cachep, entry); |
642 |
|
|
} else { |
643 |
kumaneko |
1052 |
printk(KERN_WARNING "BUG: ccs_free() with invalid pointer.\n"); |
644 |
kumaneko |
111 |
} |
645 |
|
|
} |
646 |
kumaneko |
1180 |
|
647 |
|
|
/** |
648 |
|
|
* ccs_read_memory_counter - Check for memory usage. |
649 |
|
|
* |
650 |
|
|
* @head: Pointer to "struct ccs_io_buffer". |
651 |
|
|
* |
652 |
|
|
* Returns memory usage. |
653 |
|
|
*/ |
654 |
|
|
int ccs_read_memory_counter(struct ccs_io_buffer *head) |
655 |
|
|
{ |
656 |
|
|
if (!head->read_eof) { |
657 |
|
|
const unsigned int shared = allocated_memory_for_savename; |
658 |
|
|
const unsigned int private = allocated_memory_for_elements; |
659 |
|
|
const unsigned int dynamic = dynamic_memory_size; |
660 |
|
|
char buffer[64]; |
661 |
|
|
memset(buffer, 0, sizeof(buffer)); |
662 |
|
|
if (quota_for_savename) |
663 |
|
|
snprintf(buffer, sizeof(buffer) - 1, |
664 |
|
|
" (Quota: %10u)", quota_for_savename); |
665 |
|
|
else |
666 |
|
|
buffer[0] = '\0'; |
667 |
|
|
ccs_io_printf(head, "Shared: %10u%s\n", shared, buffer); |
668 |
|
|
if (quota_for_elements) |
669 |
|
|
snprintf(buffer, sizeof(buffer) - 1, |
670 |
|
|
" (Quota: %10u)", quota_for_elements); |
671 |
|
|
else |
672 |
|
|
buffer[0] = '\0'; |
673 |
|
|
ccs_io_printf(head, "Private: %10u%s\n", private, buffer); |
674 |
|
|
ccs_io_printf(head, "Dynamic: %10u\n", dynamic); |
675 |
|
|
ccs_io_printf(head, "Total: %10u\n", |
676 |
|
|
shared + private + dynamic); |
677 |
|
|
head->read_eof = true; |
678 |
|
|
} |
679 |
|
|
return 0; |
680 |
|
|
} |
681 |
|
|
|
682 |
|
|
/** |
683 |
|
|
* ccs_write_memory_quota - Set memory quota. |
684 |
|
|
* |
685 |
|
|
* @head: Pointer to "struct ccs_io_buffer". |
686 |
|
|
* |
687 |
|
|
* Returns 0. |
688 |
|
|
*/ |
689 |
|
|
int ccs_write_memory_quota(struct ccs_io_buffer *head) |
690 |
|
|
{ |
691 |
|
|
char *data = head->write_buf; |
692 |
|
|
unsigned int size; |
693 |
|
|
if (sscanf(data, "Shared: %u", &size) == 1) |
694 |
|
|
quota_for_savename = size; |
695 |
|
|
else if (sscanf(data, "Private: %u", &size) == 1) |
696 |
|
|
quota_for_elements = size; |
697 |
|
|
return 0; |
698 |
|
|
} |