dcookies.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * dcookies.c
  3. *
  4. * Copyright 2002 John Levon <levon@movementarian.org>
  5. *
  6. * Persistent cookie-path mappings. These are used by
  7. * profilers to convert a per-task EIP value into something
  8. * non-transitory that can be processed at a later date.
  9. * This is done by locking the dentry/vfsmnt pair in the
  10. * kernel until released by the tasks needing the persistent
  11. * objects. The tag is simply an unsigned long that refers
  12. * to the pair and can be looked up from userspace.
  13. */
  14. #include <linux/syscalls.h>
  15. #include <linux/export.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/mount.h>
  19. #include <linux/capability.h>
  20. #include <linux/dcache.h>
  21. #include <linux/mm.h>
  22. #include <linux/err.h>
  23. #include <linux/errno.h>
  24. #include <linux/dcookies.h>
  25. #include <linux/mutex.h>
  26. #include <linux/path.h>
  27. #include <linux/compat.h>
  28. #include <asm/uaccess.h>
  29. /* The dcookies are allocated from a kmem_cache and
  30. * hashed onto a small number of lists. None of the
  31. * code here is particularly performance critical
  32. */
  33. struct dcookie_struct {
  34. struct path path;
  35. struct list_head hash_list;
  36. };
  37. static LIST_HEAD(dcookie_users);
  38. static DEFINE_MUTEX(dcookie_mutex);
  39. static struct kmem_cache *dcookie_cache __read_mostly;
  40. static struct list_head *dcookie_hashtable __read_mostly;
  41. static size_t hash_size __read_mostly;
  42. static inline int is_live(void)
  43. {
  44. return !(list_empty(&dcookie_users));
  45. }
  46. /* The dentry is locked, its address will do for the cookie */
  47. static inline unsigned long dcookie_value(struct dcookie_struct * dcs)
  48. {
  49. return (unsigned long)dcs->path.dentry;
  50. }
  51. static size_t dcookie_hash(unsigned long dcookie)
  52. {
  53. return (dcookie >> L1_CACHE_SHIFT) & (hash_size - 1);
  54. }
  55. static struct dcookie_struct * find_dcookie(unsigned long dcookie)
  56. {
  57. struct dcookie_struct *found = NULL;
  58. struct dcookie_struct * dcs;
  59. struct list_head * pos;
  60. struct list_head * list;
  61. list = dcookie_hashtable + dcookie_hash(dcookie);
  62. list_for_each(pos, list) {
  63. dcs = list_entry(pos, struct dcookie_struct, hash_list);
  64. if (dcookie_value(dcs) == dcookie) {
  65. found = dcs;
  66. break;
  67. }
  68. }
  69. return found;
  70. }
  71. static void hash_dcookie(struct dcookie_struct * dcs)
  72. {
  73. struct list_head * list = dcookie_hashtable + dcookie_hash(dcookie_value(dcs));
  74. list_add(&dcs->hash_list, list);
  75. }
  76. static struct dcookie_struct *alloc_dcookie(struct path *path)
  77. {
  78. struct dcookie_struct *dcs = kmem_cache_alloc(dcookie_cache,
  79. GFP_KERNEL);
  80. struct dentry *d;
  81. if (!dcs)
  82. return NULL;
  83. d = path->dentry;
  84. spin_lock(&d->d_lock);
  85. d->d_flags |= DCACHE_COOKIE;
  86. spin_unlock(&d->d_lock);
  87. dcs->path = *path;
  88. path_get(path);
  89. hash_dcookie(dcs);
  90. return dcs;
  91. }
  92. /* This is the main kernel-side routine that retrieves the cookie
  93. * value for a dentry/vfsmnt pair.
  94. */
  95. int get_dcookie(struct path *path, unsigned long *cookie)
  96. {
  97. int err = 0;
  98. struct dcookie_struct * dcs;
  99. mutex_lock(&dcookie_mutex);
  100. if (!is_live()) {
  101. err = -EINVAL;
  102. goto out;
  103. }
  104. if (path->dentry->d_flags & DCACHE_COOKIE) {
  105. dcs = find_dcookie((unsigned long)path->dentry);
  106. } else {
  107. dcs = alloc_dcookie(path);
  108. if (!dcs) {
  109. err = -ENOMEM;
  110. goto out;
  111. }
  112. }
  113. *cookie = dcookie_value(dcs);
  114. out:
  115. mutex_unlock(&dcookie_mutex);
  116. return err;
  117. }
  118. /* And here is where the userspace process can look up the cookie value
  119. * to retrieve the path.
  120. */
  121. SYSCALL_DEFINE3(lookup_dcookie, u64, cookie64, char __user *, buf, size_t, len)
  122. {
  123. unsigned long cookie = (unsigned long)cookie64;
  124. int err = -EINVAL;
  125. char * kbuf;
  126. char * path;
  127. size_t pathlen;
  128. struct dcookie_struct * dcs;
  129. /* we could leak path information to users
  130. * without dir read permission without this
  131. */
  132. if (!capable(CAP_SYS_ADMIN))
  133. return -EPERM;
  134. mutex_lock(&dcookie_mutex);
  135. if (!is_live()) {
  136. err = -EINVAL;
  137. goto out;
  138. }
  139. if (!(dcs = find_dcookie(cookie)))
  140. goto out;
  141. err = -ENOMEM;
  142. kbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  143. if (!kbuf)
  144. goto out;
  145. /* FIXME: (deleted) ? */
  146. path = d_path(&dcs->path, kbuf, PAGE_SIZE);
  147. mutex_unlock(&dcookie_mutex);
  148. if (IS_ERR(path)) {
  149. err = PTR_ERR(path);
  150. goto out_free;
  151. }
  152. err = -ERANGE;
  153. pathlen = kbuf + PAGE_SIZE - path;
  154. if (pathlen <= len) {
  155. err = pathlen;
  156. if (copy_to_user(buf, path, pathlen))
  157. err = -EFAULT;
  158. }
  159. out_free:
  160. kfree(kbuf);
  161. return err;
  162. out:
  163. mutex_unlock(&dcookie_mutex);
  164. return err;
  165. }
  166. #ifdef CONFIG_COMPAT
  167. COMPAT_SYSCALL_DEFINE4(lookup_dcookie, u32, w0, u32, w1, char __user *, buf, compat_size_t, len)
  168. {
  169. #ifdef __BIG_ENDIAN
  170. return sys_lookup_dcookie(((u64)w0 << 32) | w1, buf, len);
  171. #else
  172. return sys_lookup_dcookie(((u64)w1 << 32) | w0, buf, len);
  173. #endif
  174. }
  175. #endif
  176. static int dcookie_init(void)
  177. {
  178. struct list_head * d;
  179. unsigned int i, hash_bits;
  180. int err = -ENOMEM;
  181. dcookie_cache = kmem_cache_create("dcookie_cache",
  182. sizeof(struct dcookie_struct),
  183. 0, 0, NULL);
  184. if (!dcookie_cache)
  185. goto out;
  186. dcookie_hashtable = kmalloc(PAGE_SIZE, GFP_KERNEL);
  187. if (!dcookie_hashtable)
  188. goto out_kmem;
  189. err = 0;
  190. /*
  191. * Find the power-of-two list-heads that can fit into the allocation..
  192. * We don't guarantee that "sizeof(struct list_head)" is necessarily
  193. * a power-of-two.
  194. */
  195. hash_size = PAGE_SIZE / sizeof(struct list_head);
  196. hash_bits = 0;
  197. do {
  198. hash_bits++;
  199. } while ((hash_size >> hash_bits) != 0);
  200. hash_bits--;
  201. /*
  202. * Re-calculate the actual number of entries and the mask
  203. * from the number of bits we can fit.
  204. */
  205. hash_size = 1UL << hash_bits;
  206. /* And initialize the newly allocated array */
  207. d = dcookie_hashtable;
  208. i = hash_size;
  209. do {
  210. INIT_LIST_HEAD(d);
  211. d++;
  212. i--;
  213. } while (i);
  214. out:
  215. return err;
  216. out_kmem:
  217. kmem_cache_destroy(dcookie_cache);
  218. goto out;
  219. }
  220. static void free_dcookie(struct dcookie_struct * dcs)
  221. {
  222. struct dentry *d = dcs->path.dentry;
  223. spin_lock(&d->d_lock);
  224. d->d_flags &= ~DCACHE_COOKIE;
  225. spin_unlock(&d->d_lock);
  226. path_put(&dcs->path);
  227. kmem_cache_free(dcookie_cache, dcs);
  228. }
  229. static void dcookie_exit(void)
  230. {
  231. struct list_head * list;
  232. struct list_head * pos;
  233. struct list_head * pos2;
  234. struct dcookie_struct * dcs;
  235. size_t i;
  236. for (i = 0; i < hash_size; ++i) {
  237. list = dcookie_hashtable + i;
  238. list_for_each_safe(pos, pos2, list) {
  239. dcs = list_entry(pos, struct dcookie_struct, hash_list);
  240. list_del(&dcs->hash_list);
  241. free_dcookie(dcs);
  242. }
  243. }
  244. kfree(dcookie_hashtable);
  245. kmem_cache_destroy(dcookie_cache);
  246. }
  247. struct dcookie_user {
  248. struct list_head next;
  249. };
  250. struct dcookie_user * dcookie_register(void)
  251. {
  252. struct dcookie_user * user;
  253. mutex_lock(&dcookie_mutex);
  254. user = kmalloc(sizeof(struct dcookie_user), GFP_KERNEL);
  255. if (!user)
  256. goto out;
  257. if (!is_live() && dcookie_init())
  258. goto out_free;
  259. list_add(&user->next, &dcookie_users);
  260. out:
  261. mutex_unlock(&dcookie_mutex);
  262. return user;
  263. out_free:
  264. kfree(user);
  265. user = NULL;
  266. goto out;
  267. }
  268. void dcookie_unregister(struct dcookie_user * user)
  269. {
  270. mutex_lock(&dcookie_mutex);
  271. list_del(&user->next);
  272. kfree(user);
  273. if (!is_live())
  274. dcookie_exit();
  275. mutex_unlock(&dcookie_mutex);
  276. }
  277. EXPORT_SYMBOL_GPL(dcookie_register);
  278. EXPORT_SYMBOL_GPL(dcookie_unregister);
  279. EXPORT_SYMBOL_GPL(get_dcookie);