オープンソース・ソフトウェアの開発とダウンロード

Subversion リポジトリの参照

Annotation of /trunk/1.6.x/ccs-patch/fs/realpath.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 324 - (hide annotations) (download) (as text)
Mon Aug 6 12:39:49 2007 UTC (16 years, 9 months ago) by kumaneko
Original Path: trunk/1.5.x/ccs-patch/fs/realpath.c
File MIME type: text/x-csrc
File size: 12025 byte(s)
mkdir 1.5.x in trunk
1 kumaneko 111 /*
2     * fs/realpath.c
3     *
4     * Get the canonicalized absolute pathnames. The basis for SAKURA and TOMOYO.
5     *
6     * Copyright (C) 2005-2007 NTT DATA CORPORATION
7     *
8 kumaneko 290 * Version: 1.4.2 2007/07/13
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     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
25     #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     extern int sbin_init_started;
36    
37     /***** realpath handler *****/
38    
39     /*
40     * GetAbsolutePath - return the path of a dentry but ignores chroot'ed root.
41     * @dentry: dentry to report
42     * @vfsmnt: vfsmnt to which the dentry belongs
43     * @buffer: buffer to return value in
44     * @buflen: buffer length
45     *
46     * Caller holds the dcache_lock.
47     * Based on __d_path() in fs/dcache.c
48     *
49     * If dentry is a directory, trailing '/' is appended.
50     * Characters other than ' ' < c < 127 are converted to \ooo style octal string.
51     * Character \ is converted to \\ string.
52     */
53     static int GetAbsolutePath(struct dentry *dentry, struct vfsmount *vfsmnt, char *buffer, int buflen)
54     {
55     char *start = buffer;
56     char *end = buffer + buflen;
57     int is_dir = (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode));
58    
59     if (buflen < 256) goto out;
60    
61     *--end = '\0';
62     buflen--;
63    
64     for (;;) {
65     struct dentry *parent;
66    
67     if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
68     /* Global root? */
69     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
70     spin_lock(&vfsmount_lock);
71     #endif
72     if (vfsmnt->mnt_parent == vfsmnt) {
73     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
74     spin_unlock(&vfsmount_lock);
75     #endif
76     break;
77     }
78     dentry = vfsmnt->mnt_mountpoint;
79     vfsmnt = vfsmnt->mnt_parent;
80     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
81     spin_unlock(&vfsmount_lock);
82     #endif
83     continue;
84     }
85     if (is_dir) {
86     is_dir = 0; *--end = '/'; buflen--;
87     }
88     parent = dentry->d_parent;
89     {
90     const char *sp = dentry->d_name.name;
91     const char *cp = sp + dentry->d_name.len - 1;
92     unsigned char c;
93    
94     /* Exception: Use /proc/self/ rather than /proc/\$/ for current process. */
95     if (IS_ROOT(parent) && *sp > '0' && *sp <= '9' && parent->d_sb && parent->d_sb->s_magic == PROC_SUPER_MAGIC) {
96     char *ep;
97     const pid_t pid = (pid_t) simple_strtoul(sp, &ep, 10);
98     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
99     if (!*ep && pid == current->tgid) { sp = "self"; cp = sp + 3; }
100     #else
101     if (!*ep && pid == current->pid) { sp = "self"; cp = sp + 3; }
102     #endif
103     }
104    
105     while (sp <= cp) {
106     c = * (unsigned char *) cp;
107     if (c == '\\') {
108     buflen -= 2;
109     if (buflen < 0) goto out;
110     *--end = '\\';
111     *--end = '\\';
112     } else if (c > ' ' && c < 127) {
113     if (--buflen < 0) goto out;
114     *--end = (char) c;
115     } else {
116     buflen -= 4;
117     if (buflen < 0) goto out;
118     *--end = (c & 7) + '0';
119     *--end = ((c >> 3) & 7) + '0';
120     *--end = (c >> 6) + '0';
121     *--end = '\\';
122     }
123     cp--;
124     }
125     if (--buflen < 0) goto out;
126     *--end = '/';
127     }
128     dentry = parent;
129     }
130     if (*end == '/') { buflen++; end++; }
131     {
132     const char *sp = dentry->d_name.name;
133     const char *cp = sp + dentry->d_name.len - 1;
134     unsigned char c;
135     while (sp <= cp) {
136     c = * (unsigned char *) cp;
137     if (c == '\\') {
138     buflen -= 2;
139     if (buflen < 0) goto out;
140     *--end = '\\';
141     *--end = '\\';
142     } else if (c > ' ' && c < 127) {
143     if (--buflen < 0) goto out;
144     *--end = (char) c;
145     } else {
146     buflen -= 4;
147     if (buflen < 0) goto out;
148     *--end = (c & 7) + '0';
149     *--end = ((c >> 3) & 7) + '0';
150     *--end = (c >> 6) + '0';
151     *--end = '\\';
152     }
153     cp--;
154     }
155     }
156     /* Move the pathname to the top of the buffer. */
157     memmove(start, end, strlen(end) + 1);
158     return 0;
159     out:
160     return -ENOMEM;
161     }
162    
163     /* Returns realpath(3) of the given dentry but ignores chroot'ed root. */
164     int realpath_from_dentry2(struct dentry *dentry, struct vfsmount *mnt, char *newname, int newname_len)
165     {
166     int error;
167     struct dentry *d_dentry;
168     struct vfsmount *d_mnt;
169     if (!dentry || !mnt || !newname || newname_len <= 0) return -EINVAL;
170     if (!current->fs) {
171     printk("%s: current->fs == NULL for pid=%d\n", __FUNCTION__, current->pid);
172     return -ENOENT;
173     }
174     d_dentry = dget(dentry);
175     d_mnt = mntget(mnt);
176     /***** CRITICAL SECTION START *****/
177     spin_lock(&dcache_lock);
178     error = GetAbsolutePath(d_dentry, d_mnt, newname, newname_len);
179     spin_unlock(&dcache_lock);
180     /***** CRITICAL SECTION END *****/
181     dput(d_dentry);
182     mntput(d_mnt);
183     return error;
184     }
185    
186     /* Returns realpath(3) of the given pathname but ignores chroot'ed root. */
187     /* These functions use ccs_alloc(), so caller must ccs_free() if these functions didn't return NULL. */
188     char *realpath_from_dentry(struct dentry *dentry, struct vfsmount *mnt)
189     {
190     char *buf = ccs_alloc(CCS_MAX_PATHNAME_LEN);
191     if (buf && realpath_from_dentry2(dentry, mnt, buf, CCS_MAX_PATHNAME_LEN - 1) == 0) return buf;
192     ccs_free(buf);
193     return NULL;
194     }
195    
196     char *realpath(const char *pathname)
197     {
198     struct nameidata nd;
199     if (pathname && path_lookup(pathname, lookup_flags, &nd) == 0) {
200     char *buf = realpath_from_dentry(nd.dentry, nd.mnt);
201     path_release(&nd);
202     return buf;
203     }
204     return NULL;
205     }
206    
207     char *realpath_nofollow(const char *pathname)
208     {
209     struct nameidata nd;
210     if (pathname && path_lookup(pathname, lookup_flags ^ LOOKUP_FOLLOW, &nd) == 0) {
211     char *buf = realpath_from_dentry(nd.dentry, nd.mnt);
212     path_release(&nd);
213     return buf;
214     }
215     return NULL;
216     }
217    
218     /***** Private memory allocator. *****/
219    
220     /*
221     * Round up an integer so that the returned pointers are appropriately aligned.
222     * FIXME: Are there more requirements that is needed for assigning value atomically?
223     */
224     static inline unsigned int ROUNDUP(const unsigned int size) {
225     if (sizeof(void *) >= sizeof(long)) {
226     return ((size + sizeof(void *) - 1) / sizeof(void *)) * sizeof(void *);
227     } else {
228     return ((size + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
229     }
230     }
231    
232     static unsigned int allocated_memory_for_elements = 0;
233    
234     unsigned int GetMemoryUsedForElements(void)
235     {
236     return allocated_memory_for_elements;
237     }
238    
239     /* Allocate memory for structures. The RAM is chunked, so NEVER try to kfree() the returned pointer. */
240 kumaneko 214 void *alloc_element(const unsigned int size)
241 kumaneko 111 {
242     static DECLARE_MUTEX(lock);
243     static char *buf = NULL;
244     static unsigned int buf_used_len = PAGE_SIZE;
245     char *ptr = NULL;
246     const unsigned int word_aligned_size = ROUNDUP(size);
247     if (word_aligned_size > PAGE_SIZE) return NULL;
248     down(&lock);
249     if (buf_used_len + word_aligned_size > PAGE_SIZE) {
250     if ((ptr = kmalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) {
251     printk("ERROR: Out of memory for alloc_element().\n");
252     if (!sbin_init_started) panic("MAC Initialization failed.\n");
253     } else {
254     memset(ptr, 0, PAGE_SIZE);
255     buf = ptr;
256     allocated_memory_for_elements += PAGE_SIZE;
257     buf_used_len = word_aligned_size;
258     ptr = buf;
259     }
260     } else if (word_aligned_size) {
261     int i;
262     ptr = buf + buf_used_len;
263     buf_used_len += word_aligned_size;
264     for (i = 0; i < word_aligned_size; i++) {
265     if (ptr[i]) {
266     printk(KERN_ERR "WARNING: Reserved memory was tainted! The system might go wrong.\n");
267     ptr[i] = '\0';
268     }
269     }
270     }
271     up(&lock);
272     return ptr;
273     }
274    
275     /***** Shared memory allocator. *****/
276    
277     static unsigned int allocated_memory_for_savename = 0;
278    
279     unsigned int GetMemoryUsedForSaveName(void)
280     {
281     return allocated_memory_for_savename;
282     }
283    
284     #define MAX_HASH 256
285    
286 kumaneko 214 struct name_entry {
287 kumaneko 111 struct name_entry *next; /* Pointer to next record. NULL if none. */
288     struct path_info entry;
289 kumaneko 214 };
290 kumaneko 111
291 kumaneko 214 struct free_memory_block_list {
292 kumaneko 111 struct free_memory_block_list *next; /* Pointer to next record. NULL if none. */
293     char *ptr; /* Pointer to a free area. */
294     int len; /* Length of the area. */
295 kumaneko 214 };
296 kumaneko 111
297     /* Keep the given name on the RAM. The RAM is shared, so NEVER try to modify or kfree() the returned name. */
298     const struct path_info *SaveName(const char *name)
299     {
300 kumaneko 214 static struct free_memory_block_list fmb_list = { NULL, NULL, 0 };
301     static struct name_entry name_list[MAX_HASH]; /* The list of names. */
302 kumaneko 111 static DECLARE_MUTEX(lock);
303 kumaneko 214 struct name_entry *ptr, *prev = NULL;
304 kumaneko 111 unsigned int hash;
305 kumaneko 214 struct free_memory_block_list *fmb = &fmb_list;
306 kumaneko 111 int len;
307     static int first_call = 1;
308     if (!name) return NULL;
309     len = strlen(name) + 1;
310     if (len > CCS_MAX_PATHNAME_LEN) {
311     printk("ERROR: Name too long for SaveName().\n");
312     return NULL;
313     }
314     hash = full_name_hash((const unsigned char *) name, len - 1);
315     down(&lock);
316     if (first_call) {
317     int i;
318     first_call = 0;
319     memset(&name_list, 0, sizeof(name_list));
320     for (i = 0; i < MAX_HASH; i++) {
321     name_list[i].entry.name = "/";
322     fill_path_info(&name_list[i].entry);
323     }
324     if (CCS_MAX_PATHNAME_LEN > PAGE_SIZE) panic("Bad size.");
325     }
326     ptr = &name_list[hash % MAX_HASH];
327     while (ptr) {
328     if (hash == ptr->entry.hash && strcmp(name, ptr->entry.name) == 0) goto out;
329     prev = ptr; ptr = ptr->next;
330     }
331     while (len > fmb->len) {
332     if (fmb->next) {
333     fmb = fmb->next;
334     } else {
335     char *cp;
336 kumaneko 214 if ((cp = kmalloc(PAGE_SIZE, GFP_KERNEL)) == NULL || (fmb->next = alloc_element(sizeof(*fmb))) == NULL) {
337 kumaneko 111 kfree(cp);
338     printk("ERROR: Out of memory for SaveName().\n");
339     if (!sbin_init_started) panic("MAC Initialization failed.\n");
340     goto out; /* ptr == NULL */
341     }
342     memset(cp, 0, PAGE_SIZE);
343     allocated_memory_for_savename += PAGE_SIZE;
344     fmb = fmb->next;
345     fmb->ptr = cp;
346     fmb->len = PAGE_SIZE;
347     }
348     }
349 kumaneko 214 if ((ptr = alloc_element(sizeof(*ptr))) == NULL) goto out;
350 kumaneko 111 ptr->entry.name = fmb->ptr;
351     memmove(fmb->ptr, name, len);
352     fill_path_info(&ptr->entry);
353     fmb->ptr += len;
354     fmb->len -= len;
355     prev->next = ptr; /* prev != NULL because name_list is not empty. */
356     if (fmb->len == 0) {
357 kumaneko 214 struct free_memory_block_list *ptr = &fmb_list;
358 kumaneko 111 while (ptr->next != fmb) ptr = ptr->next; ptr->next = fmb->next;
359     }
360     out:
361     up(&lock);
362     return ptr ? &ptr->entry : NULL;
363     }
364    
365     /***** Dynamic memory allocator. *****/
366    
367 kumaneko 214 struct cache_entry {
368 kumaneko 111 struct list_head list;
369     void *ptr;
370     int size;
371 kumaneko 214 };
372 kumaneko 111
373     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
374     static struct kmem_cache *ccs_cachep = NULL;
375     #else
376     static kmem_cache_t *ccs_cachep = NULL;
377     #endif
378    
379     void __init realpath_Init(void)
380     {
381 kumaneko 316 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
382     ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), 0, 0, NULL);
383     #else
384 kumaneko 214 ccs_cachep = kmem_cache_create("ccs_cache", sizeof(struct cache_entry), 0, 0, NULL, NULL);
385 kumaneko 316 #endif
386 kumaneko 111 if (!ccs_cachep) panic("Can't create cache.\n");
387     }
388    
389     static LIST_HEAD(cache_list);
390     static spinlock_t cache_list_lock = SPIN_LOCK_UNLOCKED;
391     static unsigned int dynamic_memory_size = 0;
392    
393     unsigned int GetMemoryUsedForDynamic(void)
394     {
395     return dynamic_memory_size;
396     }
397    
398 kumaneko 214 void *ccs_alloc(const size_t size)
399 kumaneko 111 {
400     void *ret = kmalloc(size, GFP_KERNEL);
401     if (ret) {
402 kumaneko 214 struct cache_entry *new_entry = kmem_cache_alloc(ccs_cachep, GFP_KERNEL);
403 kumaneko 111 if (!new_entry) {
404     kfree(ret); ret = NULL;
405     } else {
406     INIT_LIST_HEAD(&new_entry->list);
407     new_entry->ptr = ret;
408     new_entry->size = size;
409     spin_lock(&cache_list_lock);
410     list_add_tail(&new_entry->list, &cache_list);
411     dynamic_memory_size += size;
412     spin_unlock(&cache_list_lock);
413     memset(ret, 0, size);
414     }
415     }
416 kumaneko 214 return ret;
417 kumaneko 111 }
418    
419     void ccs_free(const void *p)
420     {
421     struct list_head *v;
422 kumaneko 214 struct cache_entry *entry = NULL;
423 kumaneko 111 if (!p) return;
424     spin_lock(&cache_list_lock);
425     list_for_each(v, &cache_list) {
426 kumaneko 214 entry = list_entry(v, struct cache_entry, list);
427 kumaneko 111 if (entry->ptr != p) {
428     entry = NULL; continue;
429     }
430     list_del(&entry->list);
431     dynamic_memory_size -= entry->size;
432     break;
433     }
434     spin_unlock(&cache_list_lock);
435     if (entry) {
436     kfree(p);
437     kmem_cache_free(ccs_cachep, entry);
438     } else {
439     printk("BUG: ccs_free() with invalid pointer.\n");
440     }
441     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26