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 |
1052 |
* Version: 1.6.0-pre 2008/03/24 |
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 |
|
|
* 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 |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
100 |
|
|
if (!*ep && pid == current->tgid) { |
101 |
|
|
sp = "self"; |
102 |
|
|
cp = sp + 3; |
103 |
|
|
} |
104 |
kumaneko |
111 |
#else |
105 |
kumaneko |
1052 |
if (!*ep && pid == current->pid) { |
106 |
|
|
sp = "self"; |
107 |
|
|
cp = sp + 3; |
108 |
|
|
} |
109 |
kumaneko |
111 |
#endif |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
while (sp <= cp) { |
113 |
kumaneko |
1052 |
c = *(unsigned char *) cp; |
114 |
kumaneko |
111 |
if (c == '\\') { |
115 |
|
|
buflen -= 2; |
116 |
kumaneko |
1052 |
if (buflen < 0) |
117 |
|
|
goto out; |
118 |
kumaneko |
111 |
*--end = '\\'; |
119 |
|
|
*--end = '\\'; |
120 |
|
|
} else if (c > ' ' && c < 127) { |
121 |
kumaneko |
1052 |
if (--buflen < 0) |
122 |
|
|
goto out; |
123 |
kumaneko |
111 |
*--end = (char) c; |
124 |
|
|
} else { |
125 |
|
|
buflen -= 4; |
126 |
kumaneko |
1052 |
if (buflen < 0) |
127 |
|
|
goto out; |
128 |
kumaneko |
111 |
*--end = (c & 7) + '0'; |
129 |
|
|
*--end = ((c >> 3) & 7) + '0'; |
130 |
|
|
*--end = (c >> 6) + '0'; |
131 |
|
|
*--end = '\\'; |
132 |
|
|
} |
133 |
|
|
cp--; |
134 |
|
|
} |
135 |
kumaneko |
1052 |
if (--buflen < 0) |
136 |
|
|
goto out; |
137 |
kumaneko |
111 |
*--end = '/'; |
138 |
|
|
} |
139 |
|
|
dentry = parent; |
140 |
|
|
} |
141 |
kumaneko |
1052 |
if (*end == '/') { |
142 |
|
|
buflen++; |
143 |
|
|
end++; |
144 |
|
|
} |
145 |
kumaneko |
111 |
{ |
146 |
|
|
const char *sp = dentry->d_name.name; |
147 |
|
|
const char *cp = sp + dentry->d_name.len - 1; |
148 |
|
|
unsigned char c; |
149 |
|
|
while (sp <= cp) { |
150 |
kumaneko |
1052 |
c = *(unsigned char *) cp; |
151 |
kumaneko |
111 |
if (c == '\\') { |
152 |
|
|
buflen -= 2; |
153 |
kumaneko |
1052 |
if (buflen < 0) |
154 |
|
|
goto out; |
155 |
kumaneko |
111 |
*--end = '\\'; |
156 |
|
|
*--end = '\\'; |
157 |
|
|
} else if (c > ' ' && c < 127) { |
158 |
kumaneko |
1052 |
if (--buflen < 0) |
159 |
|
|
goto out; |
160 |
kumaneko |
111 |
*--end = (char) c; |
161 |
|
|
} else { |
162 |
|
|
buflen -= 4; |
163 |
kumaneko |
1052 |
if (buflen < 0) |
164 |
|
|
goto out; |
165 |
kumaneko |
111 |
*--end = (c & 7) + '0'; |
166 |
|
|
*--end = ((c >> 3) & 7) + '0'; |
167 |
|
|
*--end = (c >> 6) + '0'; |
168 |
|
|
*--end = '\\'; |
169 |
|
|
} |
170 |
|
|
cp--; |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
/* Move the pathname to the top of the buffer. */ |
174 |
|
|
memmove(start, end, strlen(end) + 1); |
175 |
|
|
return 0; |
176 |
|
|
out: |
177 |
|
|
return -ENOMEM; |
178 |
kumaneko |
1052 |
/***** CRITICAL SECTION END *****/ |
179 |
kumaneko |
111 |
} |
180 |
|
|
|
181 |
kumaneko |
1052 |
/** |
182 |
kumaneko |
1054 |
* ccs_realpath_from_dentry2 - Returns realpath(3) of the given dentry but ignores chroot'ed root. |
183 |
kumaneko |
1052 |
* |
184 |
|
|
* @dentry: Pointer to "struct dentry". |
185 |
|
|
* @mnt: Pointer to "struct vfsmount". |
186 |
|
|
* @newname: Pointer to buffer to return value in. |
187 |
|
|
* @newname_len: Size of @newname . |
188 |
|
|
* |
189 |
|
|
* Returns 0 on success, negative value otherwise. |
190 |
|
|
*/ |
191 |
|
|
int ccs_realpath_from_dentry2(struct dentry *dentry, struct vfsmount *mnt, |
192 |
|
|
char *newname, int newname_len) |
193 |
kumaneko |
111 |
{ |
194 |
|
|
int error; |
195 |
|
|
struct dentry *d_dentry; |
196 |
|
|
struct vfsmount *d_mnt; |
197 |
kumaneko |
1052 |
if (!dentry || !mnt || !newname || newname_len <= 0) |
198 |
|
|
return -EINVAL; |
199 |
kumaneko |
111 |
d_dentry = dget(dentry); |
200 |
|
|
d_mnt = mntget(mnt); |
201 |
|
|
/***** CRITICAL SECTION START *****/ |
202 |
|
|
spin_lock(&dcache_lock); |
203 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
204 |
|
|
spin_lock(&vfsmount_lock); |
205 |
|
|
#endif |
206 |
|
|
error = get_absolute_path(d_dentry, d_mnt, newname, newname_len); |
207 |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
208 |
|
|
spin_unlock(&vfsmount_lock); |
209 |
|
|
#endif |
210 |
kumaneko |
111 |
spin_unlock(&dcache_lock); |
211 |
|
|
/***** CRITICAL SECTION END *****/ |
212 |
|
|
dput(d_dentry); |
213 |
|
|
mntput(d_mnt); |
214 |
|
|
return error; |
215 |
|
|
} |
216 |
|
|
|
217 |
kumaneko |
1052 |
/** |
218 |
kumaneko |
1054 |
* ccs_realpath_from_dentry - Returns realpath(3) of the given pathname but ignores chroot'ed root. |
219 |
kumaneko |
1052 |
* |
220 |
|
|
* @dentry: Pointer to "struct dentry". |
221 |
|
|
* @mnt: Pointer to "struct vfsmount". |
222 |
|
|
* |
223 |
|
|
* Returns the realpath of the given @dentry and @mnt on success, |
224 |
|
|
* NULL otherwise. |
225 |
|
|
* |
226 |
|
|
* These functions use ccs_alloc(), so caller must ccs_free() |
227 |
|
|
* if these functions didn't return NULL. |
228 |
|
|
*/ |
229 |
|
|
char *ccs_realpath_from_dentry(struct dentry *dentry, struct vfsmount *mnt) |
230 |
kumaneko |
111 |
{ |
231 |
kumaneko |
1031 |
char *buf = ccs_alloc(sizeof(struct ccs_page_buffer)); |
232 |
kumaneko |
1052 |
if (buf && ccs_realpath_from_dentry2(dentry, mnt, buf, |
233 |
|
|
CCS_MAX_PATHNAME_LEN - 1) == 0) |
234 |
|
|
return buf; |
235 |
kumaneko |
111 |
ccs_free(buf); |
236 |
|
|
return NULL; |
237 |
|
|
} |
238 |
|
|
|
239 |
kumaneko |
1052 |
/** |
240 |
|
|
* ccs_realpath - Get realpath of a pathname. |
241 |
|
|
* |
242 |
|
|
* @pathname: The pathname to solve. |
243 |
|
|
* |
244 |
|
|
* Returns the realpath of @pathname on success, NULL otherwise. |
245 |
|
|
*/ |
246 |
|
|
char *ccs_realpath(const char *pathname) |
247 |
kumaneko |
111 |
{ |
248 |
|
|
struct nameidata nd; |
249 |
|
|
if (pathname && path_lookup(pathname, lookup_flags, &nd) == 0) { |
250 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
251 |
|
|
char *buf = ccs_realpath_from_dentry(nd.path.dentry, |
252 |
|
|
nd.path.mnt); |
253 |
kumaneko |
990 |
path_put(&nd.path); |
254 |
|
|
#else |
255 |
kumaneko |
1052 |
char *buf = ccs_realpath_from_dentry(nd.dentry, nd.mnt); |
256 |
kumaneko |
111 |
path_release(&nd); |
257 |
kumaneko |
990 |
#endif |
258 |
kumaneko |
111 |
return buf; |
259 |
|
|
} |
260 |
|
|
return NULL; |
261 |
|
|
} |
262 |
|
|
|
263 |
kumaneko |
1052 |
/** |
264 |
|
|
* ccs_realpath_nofollow - Get realpath of a pathname. |
265 |
|
|
* |
266 |
|
|
* @pathname: The pathname to solve. |
267 |
|
|
* |
268 |
|
|
* Returns the realpath of @pathname on success, NULL otherwise. |
269 |
|
|
*/ |
270 |
|
|
char *ccs_realpath_nofollow(const char *pathname) |
271 |
kumaneko |
111 |
{ |
272 |
|
|
struct nameidata nd; |
273 |
kumaneko |
1052 |
if (pathname && path_lookup(pathname, lookup_flags ^ LOOKUP_FOLLOW, |
274 |
|
|
&nd) == 0) { |
275 |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
276 |
|
|
char *buf = ccs_realpath_from_dentry(nd.path.dentry, |
277 |
|
|
nd.path.mnt); |
278 |
kumaneko |
990 |
path_put(&nd.path); |
279 |
|
|
#else |
280 |
kumaneko |
1052 |
char *buf = ccs_realpath_from_dentry(nd.dentry, nd.mnt); |
281 |
kumaneko |
111 |
path_release(&nd); |
282 |
kumaneko |
990 |
#endif |
283 |
kumaneko |
111 |
return buf; |
284 |
|
|
} |
285 |
|
|
return NULL; |
286 |
|
|
} |
287 |
|
|
|
288 |
kumaneko |
1052 |
/** |
289 |
kumaneko |
1054 |
* round_up - Round up an integer so that the returned pointers are appropriately aligned. |
290 |
kumaneko |
1052 |
* |
291 |
|
|
* @size: Size in bytes. |
292 |
|
|
* |
293 |
|
|
* Returns rounded value of @size. |
294 |
|
|
* |
295 |
|
|
* FIXME: Are there more requirements that is needed for assigning value |
296 |
|
|
* atomically? |
297 |
kumaneko |
111 |
*/ |
298 |
kumaneko |
1052 |
static inline unsigned int round_up(const unsigned int size) |
299 |
|
|
{ |
300 |
|
|
if (sizeof(void *) >= sizeof(long)) |
301 |
|
|
return ((size + sizeof(void *) - 1) |
302 |
|
|
/ sizeof(void *)) * sizeof(void *); |
303 |
|
|
else |
304 |
|
|
return ((size + sizeof(long) - 1) |
305 |
|
|
/ sizeof(long)) * sizeof(long); |
306 |
kumaneko |
111 |
} |
307 |
|
|
|
308 |
kumaneko |
1052 |
static unsigned int allocated_memory_for_elements; |
309 |
kumaneko |
111 |
|
310 |
kumaneko |
1052 |
/** |
311 |
kumaneko |
1054 |
* ccs_get_memory_used_for_elements - Get memory used for keeping ACL structures. |
312 |
kumaneko |
1052 |
* |
313 |
|
|
* Returns memory used for keeping ACL structures. |
314 |
|
|
*/ |
315 |
|
|
unsigned int ccs_get_memory_used_for_elements(void) |
316 |
kumaneko |
111 |
{ |
317 |
|
|
return allocated_memory_for_elements; |
318 |
|
|
} |
319 |
|
|
|
320 |
kumaneko |
1052 |
/** |
321 |
|
|
* ccs_alloc_element - Allocate permanent memory for structures. |
322 |
|
|
* |
323 |
|
|
* @size: Size in bytes. |
324 |
|
|
* |
325 |
|
|
* Returns pointer to allocated memory on success, NULL otherwise. |
326 |
|
|
* |
327 |
|
|
* The RAM is chunked, so NEVER try to kfree() the returned pointer. |
328 |
|
|
*/ |
329 |
|
|
void *ccs_alloc_element(const unsigned int size) |
330 |
kumaneko |
111 |
{ |
331 |
kumaneko |
652 |
static DEFINE_MUTEX(lock); |
332 |
kumaneko |
1052 |
static char *buf; |
333 |
kumaneko |
111 |
static unsigned int buf_used_len = PAGE_SIZE; |
334 |
|
|
char *ptr = NULL; |
335 |
kumaneko |
1052 |
const unsigned int word_aligned_size = round_up(size); |
336 |
|
|
if (word_aligned_size > PAGE_SIZE) |
337 |
|
|
return NULL; |
338 |
kumaneko |
652 |
mutex_lock(&lock); |
339 |
kumaneko |
111 |
if (buf_used_len + word_aligned_size > PAGE_SIZE) { |
340 |
kumaneko |
1052 |
ptr = kzalloc(PAGE_SIZE, GFP_KERNEL); |
341 |
|
|
if (!ptr) { |
342 |
|
|
printk(KERN_WARNING "ERROR: Out of memory " |
343 |
|
|
"for ccs_alloc_element().\n"); |
344 |
|
|
if (!sbin_init_started) |
345 |
|
|
panic("MAC Initialization failed.\n"); |
346 |
kumaneko |
111 |
} else { |
347 |
|
|
buf = ptr; |
348 |
|
|
allocated_memory_for_elements += PAGE_SIZE; |
349 |
|
|
buf_used_len = word_aligned_size; |
350 |
|
|
ptr = buf; |
351 |
|
|
} |
352 |
|
|
} else if (word_aligned_size) { |
353 |
|
|
int i; |
354 |
|
|
ptr = buf + buf_used_len; |
355 |
|
|
buf_used_len += word_aligned_size; |
356 |
|
|
for (i = 0; i < word_aligned_size; i++) { |
357 |
kumaneko |
1052 |
if (!ptr[i]) |
358 |
|
|
continue; |
359 |
|
|
printk(KERN_ERR "WARNING: Reserved memory was tainted! " |
360 |
|
|
"The system might go wrong.\n"); |
361 |
|
|
ptr[i] = '\0'; |
362 |
kumaneko |
111 |
} |
363 |
|
|
} |
364 |
kumaneko |
652 |
mutex_unlock(&lock); |
365 |
kumaneko |
111 |
return ptr; |
366 |
|
|
} |
367 |
|
|
|
368 |
kumaneko |
1052 |
static unsigned int allocated_memory_for_savename; |
369 |
kumaneko |
111 |
|
370 |
kumaneko |
1052 |
/** |
371 |
kumaneko |
1054 |
* ccs_get_memory_used_for_save_name - Get memory used for keeping string data. |
372 |
kumaneko |
1052 |
* |
373 |
|
|
* Returns memory used for keeping string data. |
374 |
|
|
*/ |
375 |
|
|
unsigned int ccs_get_memory_used_for_save_name(void) |
376 |
kumaneko |
111 |
{ |
377 |
kumaneko |
1055 |
return allocated_memory_for_savename; |
378 |
kumaneko |
111 |
} |
379 |
|
|
|
380 |
|
|
#define MAX_HASH 256 |
381 |
|
|
|
382 |
kumaneko |
1052 |
/* Structure for string data. */ |
383 |
kumaneko |
214 |
struct name_entry { |
384 |
kumaneko |
731 |
struct list1_head list; |
385 |
kumaneko |
111 |
struct path_info entry; |
386 |
kumaneko |
214 |
}; |
387 |
kumaneko |
111 |
|
388 |
kumaneko |
1052 |
/* Structure for available memory region. */ |
389 |
kumaneko |
214 |
struct free_memory_block_list { |
390 |
kumaneko |
731 |
struct list_head list; |
391 |
kumaneko |
1052 |
char *ptr; /* Pointer to a free area. */ |
392 |
|
|
int len; /* Length of the area. */ |
393 |
kumaneko |
214 |
}; |
394 |
kumaneko |
111 |
|
395 |
kumaneko |
1052 |
/* The list for "struct name_entry". */ |
396 |
|
|
static struct list1_head name_list[MAX_HASH]; |
397 |
kumaneko |
731 |
|
398 |
kumaneko |
1052 |
/** |
399 |
|
|
* ccs_save_name - Allocate permanent memory for string data. |
400 |
|
|
* |
401 |
|
|
* @name: The string to store into the permernent memory. |
402 |
|
|
* |
403 |
|
|
* Returns pointer to "struct path_info" on success, NULL otherwise. |
404 |
|
|
* |
405 |
|
|
* The RAM is shared, so NEVER try to modify or kfree() the returned name. |
406 |
|
|
*/ |
407 |
|
|
const struct path_info *ccs_save_name(const char *name) |
408 |
kumaneko |
111 |
{ |
409 |
kumaneko |
731 |
static LIST_HEAD(fmb_list); |
410 |
kumaneko |
652 |
static DEFINE_MUTEX(lock); |
411 |
kumaneko |
731 |
struct name_entry *ptr; |
412 |
kumaneko |
111 |
unsigned int hash; |
413 |
kumaneko |
731 |
struct free_memory_block_list *fmb; |
414 |
kumaneko |
111 |
int len; |
415 |
kumaneko |
731 |
char *cp; |
416 |
kumaneko |
1052 |
if (!name) |
417 |
|
|
return NULL; |
418 |
kumaneko |
111 |
len = strlen(name) + 1; |
419 |
|
|
if (len > CCS_MAX_PATHNAME_LEN) { |
420 |
kumaneko |
1052 |
printk(KERN_WARNING "ERROR: Name too long " |
421 |
|
|
"for ccs_save_name().\n"); |
422 |
kumaneko |
111 |
return NULL; |
423 |
|
|
} |
424 |
|
|
hash = full_name_hash((const unsigned char *) name, len - 1); |
425 |
kumaneko |
652 |
mutex_lock(&lock); |
426 |
kumaneko |
731 |
list1_for_each_entry(ptr, &name_list[hash % MAX_HASH], list) { |
427 |
kumaneko |
1052 |
if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name)) |
428 |
|
|
goto out; |
429 |
kumaneko |
111 |
} |
430 |
kumaneko |
731 |
list_for_each_entry(fmb, &fmb_list, list) { |
431 |
kumaneko |
1052 |
if (len <= fmb->len) |
432 |
|
|
goto ready; |
433 |
kumaneko |
111 |
} |
434 |
kumaneko |
1016 |
cp = kzalloc(PAGE_SIZE, GFP_KERNEL); |
435 |
|
|
fmb = kzalloc(sizeof(*fmb), GFP_KERNEL); |
436 |
kumaneko |
731 |
if (!cp || !fmb) { |
437 |
|
|
kfree(cp); |
438 |
|
|
kfree(fmb); |
439 |
kumaneko |
1052 |
printk(KERN_WARNING "ERROR: Out of memory " |
440 |
|
|
"for ccs_save_name().\n"); |
441 |
|
|
if (!sbin_init_started) |
442 |
|
|
panic("MAC Initialization failed.\n"); |
443 |
kumaneko |
731 |
ptr = NULL; |
444 |
|
|
goto out; |
445 |
|
|
} |
446 |
|
|
allocated_memory_for_savename += PAGE_SIZE; |
447 |
|
|
list_add(&fmb->list, &fmb_list); |
448 |
|
|
fmb->ptr = cp; |
449 |
|
|
fmb->len = PAGE_SIZE; |
450 |
|
|
ready: |
451 |
kumaneko |
1052 |
ptr = ccs_alloc_element(sizeof(*ptr)); |
452 |
|
|
if (!ptr) |
453 |
|
|
goto out; |
454 |
kumaneko |
111 |
ptr->entry.name = fmb->ptr; |
455 |
|
|
memmove(fmb->ptr, name, len); |
456 |
kumaneko |
1052 |
ccs_fill_path_info(&ptr->entry); |
457 |
kumaneko |
111 |
fmb->ptr += len; |
458 |
|
|
fmb->len -= len; |
459 |
kumaneko |
731 |
list1_add_tail_mb(&ptr->list, &name_list[hash % MAX_HASH]); |
460 |
kumaneko |
111 |
if (fmb->len == 0) { |
461 |
kumaneko |
731 |
list_del(&fmb->list); |
462 |
|
|
kfree(fmb); |
463 |
kumaneko |
111 |
} |
464 |
|
|
out: |
465 |
kumaneko |
652 |
mutex_unlock(&lock); |
466 |
kumaneko |
111 |
return ptr ? &ptr->entry : NULL; |
467 |
|
|
} |
468 |
|
|
|
469 |
kumaneko |
1052 |
/* Structure for temporarily allocated memory. */ |
470 |
kumaneko |
214 |
struct cache_entry { |
471 |
kumaneko |
111 |
struct list_head list; |
472 |
|
|
void *ptr; |
473 |
|
|
int size; |
474 |
kumaneko |
214 |
}; |
475 |
kumaneko |
111 |
|
476 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20) |
477 |
|
|
static struct kmem_cache *ccs_cachep; |
478 |
kumaneko |
111 |
#else |
479 |
kumaneko |
1052 |
static kmem_cache_t *ccs_cachep; |
480 |
kumaneko |
111 |
#endif |
481 |
|
|
|
482 |
kumaneko |
1052 |
/** |
483 |
|
|
* ccs_realpath_init - Initialize realpath related code. |
484 |
|
|
* |
485 |
|
|
* Returns 0. |
486 |
|
|
*/ |
487 |
|
|
static int __init ccs_realpath_init(void) |
488 |
kumaneko |
111 |
{ |
489 |
kumaneko |
731 |
int i; |
490 |
kumaneko |
1052 |
if (CCS_MAX_PATHNAME_LEN > PAGE_SIZE) |
491 |
|
|
panic("Bad size."); |
492 |
|
|
if (sizeof(struct path_info_with_data) > sizeof(struct ccs_page_buffer)) |
493 |
|
|
panic("Bad size."); |
494 |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) |
495 |
|
|
ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), |
496 |
|
|
0, 0, NULL); |
497 |
kumaneko |
316 |
#else |
498 |
kumaneko |
1052 |
ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), |
499 |
|
|
0, 0, NULL, NULL); |
500 |
kumaneko |
316 |
#endif |
501 |
kumaneko |
1052 |
if (!ccs_cachep) |
502 |
|
|
panic("Can't create cache.\n"); |
503 |
|
|
for (i = 0; i < MAX_HASH; i++) |
504 |
kumaneko |
731 |
INIT_LIST1_HEAD(&name_list[i]); |
505 |
kumaneko |
722 |
INIT_LIST1_HEAD(&KERNEL_DOMAIN.acl_info_list); |
506 |
kumaneko |
1052 |
KERNEL_DOMAIN.domainname = ccs_save_name(ROOT_NAME); |
507 |
kumaneko |
722 |
list1_add_tail_mb(&KERNEL_DOMAIN.list, &domain_list); |
508 |
kumaneko |
1052 |
if (ccs_find_domain(ROOT_NAME) != &KERNEL_DOMAIN) |
509 |
|
|
panic("Can't register KERNEL_DOMAIN"); |
510 |
|
|
return 0; |
511 |
kumaneko |
111 |
} |
512 |
|
|
|
513 |
kumaneko |
1052 |
__initcall(ccs_realpath_init); |
514 |
|
|
|
515 |
|
|
/* The list for "struct cache_entry". */ |
516 |
kumaneko |
111 |
static LIST_HEAD(cache_list); |
517 |
|
|
|
518 |
kumaneko |
1052 |
static DEFINE_SPINLOCK(cache_list_lock); |
519 |
|
|
|
520 |
|
|
static unsigned int dynamic_memory_size; |
521 |
|
|
|
522 |
|
|
/** |
523 |
|
|
* ccs_get_memory_used_for_dynamic - Get memory used for temporal purpose. |
524 |
|
|
* |
525 |
|
|
* Returns memory used for temporal purpose. |
526 |
|
|
*/ |
527 |
|
|
unsigned int ccs_get_memory_used_for_dynamic(void) |
528 |
kumaneko |
111 |
{ |
529 |
|
|
return dynamic_memory_size; |
530 |
|
|
} |
531 |
|
|
|
532 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
533 |
|
|
/** |
534 |
|
|
* round2 - Rounded up to power-of-two value. |
535 |
|
|
* |
536 |
|
|
* @size: Size in bytes. |
537 |
|
|
* |
538 |
|
|
* Returns power-of-two of @size. |
539 |
|
|
*/ |
540 |
kumaneko |
581 |
static int round2(size_t size) |
541 |
|
|
{ |
542 |
|
|
#if PAGE_SIZE == 4096 |
543 |
|
|
size_t bsize = 32; |
544 |
|
|
#else |
545 |
|
|
size_t bsize = 64; |
546 |
|
|
#endif |
547 |
|
|
while (size > bsize) bsize <<= 1; |
548 |
|
|
return bsize; |
549 |
|
|
} |
550 |
|
|
#endif |
551 |
|
|
|
552 |
kumaneko |
1052 |
/** |
553 |
|
|
* ccs_alloc - Allocate memory for temporal purpose. |
554 |
|
|
* |
555 |
|
|
* @size: Size in bytes. |
556 |
|
|
* |
557 |
|
|
* Returns pointer to allocated memory on success, NULL otherwise. |
558 |
|
|
*/ |
559 |
kumaneko |
214 |
void *ccs_alloc(const size_t size) |
560 |
kumaneko |
111 |
{ |
561 |
kumaneko |
1031 |
struct cache_entry *new_entry; |
562 |
kumaneko |
1055 |
void *ret = kzalloc(size, GFP_KERNEL); |
563 |
kumaneko |
1052 |
if (!ret) |
564 |
|
|
goto out; |
565 |
kumaneko |
1031 |
new_entry = kmem_cache_alloc(ccs_cachep, GFP_KERNEL); |
566 |
|
|
if (!new_entry) { |
567 |
|
|
kfree(ret); ret = NULL; |
568 |
|
|
goto out; |
569 |
|
|
} |
570 |
|
|
INIT_LIST_HEAD(&new_entry->list); |
571 |
|
|
new_entry->ptr = ret; |
572 |
kumaneko |
1052 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
573 |
kumaneko |
1031 |
new_entry->size = ksize(ret); |
574 |
|
|
#else |
575 |
|
|
new_entry->size = round2(size); |
576 |
|
|
#endif |
577 |
kumaneko |
1052 |
/***** CRITICAL SECTION START *****/ |
578 |
kumaneko |
1031 |
spin_lock(&cache_list_lock); |
579 |
|
|
list_add_tail(&new_entry->list, &cache_list); |
580 |
|
|
dynamic_memory_size += new_entry->size; |
581 |
|
|
spin_unlock(&cache_list_lock); |
582 |
kumaneko |
1052 |
/***** CRITICAL SECTION END *****/ |
583 |
kumaneko |
1031 |
out: |
584 |
kumaneko |
214 |
return ret; |
585 |
kumaneko |
111 |
} |
586 |
|
|
|
587 |
kumaneko |
1052 |
/** |
588 |
|
|
* ccs_free - Release memory allocated by ccs_alloc(). |
589 |
|
|
* |
590 |
|
|
* @p: Pointer returned by ccs_alloc(). May be NULL. |
591 |
|
|
* |
592 |
|
|
* Returns nothing. |
593 |
|
|
*/ |
594 |
kumaneko |
111 |
void ccs_free(const void *p) |
595 |
|
|
{ |
596 |
|
|
struct list_head *v; |
597 |
kumaneko |
214 |
struct cache_entry *entry = NULL; |
598 |
kumaneko |
1052 |
if (!p) |
599 |
|
|
return; |
600 |
|
|
/***** CRITICAL SECTION START *****/ |
601 |
kumaneko |
111 |
spin_lock(&cache_list_lock); |
602 |
|
|
list_for_each(v, &cache_list) { |
603 |
kumaneko |
214 |
entry = list_entry(v, struct cache_entry, list); |
604 |
kumaneko |
111 |
if (entry->ptr != p) { |
605 |
|
|
entry = NULL; continue; |
606 |
|
|
} |
607 |
|
|
list_del(&entry->list); |
608 |
|
|
dynamic_memory_size -= entry->size; |
609 |
|
|
break; |
610 |
|
|
} |
611 |
|
|
spin_unlock(&cache_list_lock); |
612 |
kumaneko |
1052 |
/***** CRITICAL SECTION END *****/ |
613 |
kumaneko |
111 |
if (entry) { |
614 |
|
|
kfree(p); |
615 |
|
|
kmem_cache_free(ccs_cachep, entry); |
616 |
|
|
} else { |
617 |
kumaneko |
1052 |
printk(KERN_WARNING "BUG: ccs_free() with invalid pointer.\n"); |
618 |
kumaneko |
111 |
} |
619 |
|
|
} |