Blame view

fs/dcookies.c 6.82 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * dcookies.c
   *
   * Copyright 2002 John Levon <levon@movementarian.org>
   *
   * Persistent cookie-path mappings. These are used by
   * profilers to convert a per-task EIP value into something
   * non-transitory that can be processed at a later date.
   * This is done by locking the dentry/vfsmnt pair in the
   * kernel until released by the tasks needing the persistent
   * objects. The tag is simply an unsigned long that refers
   * to the pair and can be looked up from userspace.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
17
18
  #include <linux/syscalls.h>
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/list.h>
  #include <linux/mount.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
19
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include <linux/dcache.h>
  #include <linux/mm.h>
4e950f6f0   Alexey Dobriyan   Remove fs.h from ...
22
  #include <linux/err.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
  #include <linux/errno.h>
  #include <linux/dcookies.h>
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
25
  #include <linux/mutex.h>
448678a0f   Jan Blunck   d_path: Make get_...
26
  #include <linux/path.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
32
33
  #include <asm/uaccess.h>
  
  /* The dcookies are allocated from a kmem_cache and
   * hashed onto a small number of lists. None of the
   * code here is particularly performance critical
   */
  struct dcookie_struct {
448678a0f   Jan Blunck   d_path: Make get_...
34
  	struct path path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
38
  	struct list_head hash_list;
  };
  
  static LIST_HEAD(dcookie_users);
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
39
  static DEFINE_MUTEX(dcookie_mutex);
e18b890bb   Christoph Lameter   [PATCH] slab: rem...
40
  static struct kmem_cache *dcookie_cache __read_mostly;
fa3536cc1   Eric Dumazet   [PATCH] Use __rea...
41
42
  static struct list_head *dcookie_hashtable __read_mostly;
  static size_t hash_size __read_mostly;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
47
48
49
50
51
52
  
  static inline int is_live(void)
  {
  	return !(list_empty(&dcookie_users));
  }
  
  
  /* The dentry is locked, its address will do for the cookie */
  static inline unsigned long dcookie_value(struct dcookie_struct * dcs)
  {
448678a0f   Jan Blunck   d_path: Make get_...
53
  	return (unsigned long)dcs->path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  }
  
  
  static size_t dcookie_hash(unsigned long dcookie)
  {
  	return (dcookie >> L1_CACHE_SHIFT) & (hash_size - 1);
  }
  
  
  static struct dcookie_struct * find_dcookie(unsigned long dcookie)
  {
  	struct dcookie_struct *found = NULL;
  	struct dcookie_struct * dcs;
  	struct list_head * pos;
  	struct list_head * list;
  
  	list = dcookie_hashtable + dcookie_hash(dcookie);
  
  	list_for_each(pos, list) {
  		dcs = list_entry(pos, struct dcookie_struct, hash_list);
  		if (dcookie_value(dcs) == dcookie) {
  			found = dcs;
  			break;
  		}
  	}
  
  	return found;
  }
  
  
  static void hash_dcookie(struct dcookie_struct * dcs)
  {
  	struct list_head * list = dcookie_hashtable + dcookie_hash(dcookie_value(dcs));
  	list_add(&dcs->hash_list, list);
  }
448678a0f   Jan Blunck   d_path: Make get_...
89
  static struct dcookie_struct *alloc_dcookie(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
  {
448678a0f   Jan Blunck   d_path: Make get_...
91
92
  	struct dcookie_struct *dcs = kmem_cache_alloc(dcookie_cache,
  							GFP_KERNEL);
c2452f327   Nick Piggin   shrink struct dentry
93
  	struct dentry *d;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
  	if (!dcs)
  		return NULL;
c2452f327   Nick Piggin   shrink struct dentry
96
97
98
99
  	d = path->dentry;
  	spin_lock(&d->d_lock);
  	d->d_flags |= DCACHE_COOKIE;
  	spin_unlock(&d->d_lock);
448678a0f   Jan Blunck   d_path: Make get_...
100
101
  	dcs->path = *path;
  	path_get(path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  	hash_dcookie(dcs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
106
107
108
109
  	return dcs;
  }
  
  
  /* This is the main kernel-side routine that retrieves the cookie
   * value for a dentry/vfsmnt pair.
   */
448678a0f   Jan Blunck   d_path: Make get_...
110
  int get_dcookie(struct path *path, unsigned long *cookie)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
112
113
  {
  	int err = 0;
  	struct dcookie_struct * dcs;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
114
  	mutex_lock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
119
  
  	if (!is_live()) {
  		err = -EINVAL;
  		goto out;
  	}
c2452f327   Nick Piggin   shrink struct dentry
120
121
122
  	if (path->dentry->d_flags & DCACHE_COOKIE) {
  		dcs = find_dcookie((unsigned long)path->dentry);
  	} else {
448678a0f   Jan Blunck   d_path: Make get_...
123
  		dcs = alloc_dcookie(path);
c2452f327   Nick Piggin   shrink struct dentry
124
125
126
127
  		if (!dcs) {
  			err = -ENOMEM;
  			goto out;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
131
132
  	}
  
  	*cookie = dcookie_value(dcs);
  
  out:
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
133
  	mutex_unlock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
136
137
138
139
140
  	return err;
  }
  
  
  /* And here is where the userspace process can look up the cookie value
   * to retrieve the path.
   */
6673e0c3f   Heiko Carstens   [CVE-2009-0029] S...
141
  SYSCALL_DEFINE(lookup_dcookie)(u64 cookie64, char __user * buf, size_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
143
144
145
146
147
148
149
150
151
152
153
154
  {
  	unsigned long cookie = (unsigned long)cookie64;
  	int err = -EINVAL;
  	char * kbuf;
  	char * path;
  	size_t pathlen;
  	struct dcookie_struct * dcs;
  
  	/* we could leak path information to users
  	 * without dir read permission without this
  	 */
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
155
  	mutex_lock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  
  	if (!is_live()) {
  		err = -EINVAL;
  		goto out;
  	}
  
  	if (!(dcs = find_dcookie(cookie)))
  		goto out;
  
  	err = -ENOMEM;
  	kbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  	if (!kbuf)
  		goto out;
  
  	/* FIXME: (deleted) ? */
cf28b4863   Jan Blunck   d_path: Make d_pa...
171
  	path = d_path(&dcs->path, kbuf, PAGE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172

fe47ae7f5   Robert Richter   oprofile, dcookie...
173
  	mutex_unlock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  	if (IS_ERR(path)) {
  		err = PTR_ERR(path);
  		goto out_free;
  	}
  
  	err = -ERANGE;
   
  	pathlen = kbuf + PAGE_SIZE - path;
  	if (pathlen <= len) {
  		err = pathlen;
  		if (copy_to_user(buf, path, pathlen))
  			err = -EFAULT;
  	}
  
  out_free:
  	kfree(kbuf);
fe47ae7f5   Robert Richter   oprofile, dcookie...
190
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
  out:
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
192
  	mutex_unlock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
194
  	return err;
  }
6673e0c3f   Heiko Carstens   [CVE-2009-0029] S...
195
196
197
198
199
200
201
  #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  asmlinkage long SyS_lookup_dcookie(u64 cookie64, long buf, long len)
  {
  	return SYSC_lookup_dcookie(cookie64, (char __user *) buf, (size_t) len);
  }
  SYSCALL_ALIAS(sys_lookup_dcookie, SyS_lookup_dcookie);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
204
205
206
207
208
209
210
  
  static int dcookie_init(void)
  {
  	struct list_head * d;
  	unsigned int i, hash_bits;
  	int err = -ENOMEM;
  
  	dcookie_cache = kmem_cache_create("dcookie_cache",
  		sizeof(struct dcookie_struct),
20c2df83d   Paul Mundt   mm: Remove slab d...
211
  		0, 0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  
  	if (!dcookie_cache)
  		goto out;
  
  	dcookie_hashtable = kmalloc(PAGE_SIZE, GFP_KERNEL);
  	if (!dcookie_hashtable)
  		goto out_kmem;
  
  	err = 0;
  
  	/*
  	 * Find the power-of-two list-heads that can fit into the allocation..
  	 * We don't guarantee that "sizeof(struct list_head)" is necessarily
  	 * a power-of-two.
  	 */
  	hash_size = PAGE_SIZE / sizeof(struct list_head);
  	hash_bits = 0;
  	do {
  		hash_bits++;
  	} while ((hash_size >> hash_bits) != 0);
  	hash_bits--;
  
  	/*
  	 * Re-calculate the actual number of entries and the mask
  	 * from the number of bits we can fit.
  	 */
  	hash_size = 1UL << hash_bits;
  
  	/* And initialize the newly allocated array */
  	d = dcookie_hashtable;
  	i = hash_size;
  	do {
  		INIT_LIST_HEAD(d);
  		d++;
  		i--;
  	} while (i);
  
  out:
  	return err;
  out_kmem:
  	kmem_cache_destroy(dcookie_cache);
  	goto out;
  }
  
  
  static void free_dcookie(struct dcookie_struct * dcs)
  {
c2452f327   Nick Piggin   shrink struct dentry
259
260
261
262
263
  	struct dentry *d = dcs->path.dentry;
  
  	spin_lock(&d->d_lock);
  	d->d_flags &= ~DCACHE_COOKIE;
  	spin_unlock(&d->d_lock);
448678a0f   Jan Blunck   d_path: Make get_...
264
  	path_put(&dcs->path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  	kmem_cache_free(dcookie_cache, dcs);
  }
  
  
  static void dcookie_exit(void)
  {
  	struct list_head * list;
  	struct list_head * pos;
  	struct list_head * pos2;
  	struct dcookie_struct * dcs;
  	size_t i;
  
  	for (i = 0; i < hash_size; ++i) {
  		list = dcookie_hashtable + i;
  		list_for_each_safe(pos, pos2, list) {
  			dcs = list_entry(pos, struct dcookie_struct, hash_list);
  			list_del(&dcs->hash_list);
  			free_dcookie(dcs);
  		}
  	}
  
  	kfree(dcookie_hashtable);
  	kmem_cache_destroy(dcookie_cache);
  }
  
  
  struct dcookie_user {
  	struct list_head next;
  };
   
  struct dcookie_user * dcookie_register(void)
  {
  	struct dcookie_user * user;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
298
  	mutex_lock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
302
303
304
305
306
307
308
309
  
  	user = kmalloc(sizeof(struct dcookie_user), GFP_KERNEL);
  	if (!user)
  		goto out;
  
  	if (!is_live() && dcookie_init())
  		goto out_free;
  
  	list_add(&user->next, &dcookie_users);
  
  out:
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
310
  	mutex_unlock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
313
314
315
316
317
318
319
320
  	return user;
  out_free:
  	kfree(user);
  	user = NULL;
  	goto out;
  }
  
  
  void dcookie_unregister(struct dcookie_user * user)
  {
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
321
  	mutex_lock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
323
324
325
326
327
  
  	list_del(&user->next);
  	kfree(user);
  
  	if (!is_live())
  		dcookie_exit();
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
328
  	mutex_unlock(&dcookie_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
330
331
332
333
  }
  
  EXPORT_SYMBOL_GPL(dcookie_register);
  EXPORT_SYMBOL_GPL(dcookie_unregister);
  EXPORT_SYMBOL_GPL(get_dcookie);