dcookies.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/module.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/errno.h>
  23. #include <linux/dcookies.h>
  24. #include <linux/mutex.h>
  25. #include <asm/uaccess.h>
  26. /* The dcookies are allocated from a kmem_cache and
  27. * hashed onto a small number of lists. None of the
  28. * code here is particularly performance critical
  29. */
  30. struct dcookie_struct {
  31. struct dentry * dentry;
  32. struct vfsmount * vfsmnt;
  33. struct list_head hash_list;
  34. };
  35. static LIST_HEAD(dcookie_users);
  36. static DEFINE_MUTEX(dcookie_mutex);
  37. static kmem_cache_t *dcookie_cache __read_mostly;
  38. static struct list_head *dcookie_hashtable __read_mostly;
  39. static size_t hash_size __read_mostly;
  40. static inline int is_live(void)
  41. {
  42. return !(list_empty(&dcookie_users));
  43. }
  44. /* The dentry is locked, its address will do for the cookie */
  45. static inline unsigned long dcookie_value(struct dcookie_struct * dcs)
  46. {
  47. return (unsigned long)dcs->dentry;
  48. }
  49. static size_t dcookie_hash(unsigned long dcookie)
  50. {
  51. return (dcookie >> L1_CACHE_SHIFT) & (hash_size - 1);
  52. }
  53. static struct dcookie_struct * find_dcookie(unsigned long dcookie)
  54. {
  55. struct dcookie_struct *found = NULL;
  56. struct dcookie_struct * dcs;
  57. struct list_head * pos;
  58. struct list_head * list;
  59. list = dcookie_hashtable + dcookie_hash(dcookie);
  60. list_for_each(pos, list) {
  61. dcs = list_entry(pos, struct dcookie_struct, hash_list);
  62. if (dcookie_value(dcs) == dcookie) {
  63. found = dcs;
  64. break;
  65. }
  66. }
  67. return found;
  68. }
  69. static void hash_dcookie(struct dcookie_struct * dcs)
  70. {
  71. struct list_head * list = dcookie_hashtable + dcookie_hash(dcookie_value(dcs));
  72. list_add(&dcs->hash_list, list);
  73. }
  74. static struct dcookie_struct * alloc_dcookie(struct dentry * dentry,
  75. struct vfsmount * vfsmnt)
  76. {
  77. struct dcookie_struct * dcs = kmem_cache_alloc(dcookie_cache, GFP_KERNEL);
  78. if (!dcs)
  79. return NULL;
  80. dentry->d_cookie = dcs;
  81. dcs->dentry = dget(dentry);
  82. dcs->vfsmnt = mntget(vfsmnt);
  83. hash_dcookie(dcs);
  84. return dcs;
  85. }
  86. /* This is the main kernel-side routine that retrieves the cookie
  87. * value for a dentry/vfsmnt pair.
  88. */
  89. int get_dcookie(struct dentry * dentry, struct vfsmount * vfsmnt,
  90. unsigned long * cookie)
  91. {
  92. int err = 0;
  93. struct dcookie_struct * dcs;
  94. mutex_lock(&dcookie_mutex);
  95. if (!is_live()) {
  96. err = -EINVAL;
  97. goto out;
  98. }
  99. dcs = dentry->d_cookie;
  100. if (!dcs)
  101. dcs = alloc_dcookie(dentry, vfsmnt);
  102. if (!dcs) {
  103. err = -ENOMEM;
  104. goto out;
  105. }
  106. *cookie = dcookie_value(dcs);
  107. out:
  108. mutex_unlock(&dcookie_mutex);
  109. return err;
  110. }
  111. /* And here is where the userspace process can look up the cookie value
  112. * to retrieve the path.
  113. */
  114. asmlinkage long sys_lookup_dcookie(u64 cookie64, char __user * buf, size_t len)
  115. {
  116. unsigned long cookie = (unsigned long)cookie64;
  117. int err = -EINVAL;
  118. char * kbuf;
  119. char * path;
  120. size_t pathlen;
  121. struct dcookie_struct * dcs;
  122. /* we could leak path information to users
  123. * without dir read permission without this
  124. */
  125. if (!capable(CAP_SYS_ADMIN))
  126. return -EPERM;
  127. mutex_lock(&dcookie_mutex);
  128. if (!is_live()) {
  129. err = -EINVAL;
  130. goto out;
  131. }
  132. if (!(dcs = find_dcookie(cookie)))
  133. goto out;
  134. err = -ENOMEM;
  135. kbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  136. if (!kbuf)
  137. goto out;
  138. /* FIXME: (deleted) ? */
  139. path = d_path(dcs->dentry, dcs->vfsmnt, kbuf, PAGE_SIZE);
  140. if (IS_ERR(path)) {
  141. err = PTR_ERR(path);
  142. goto out_free;
  143. }
  144. err = -ERANGE;
  145. pathlen = kbuf + PAGE_SIZE - path;
  146. if (pathlen <= len) {
  147. err = pathlen;
  148. if (copy_to_user(buf, path, pathlen))
  149. err = -EFAULT;
  150. }
  151. out_free:
  152. kfree(kbuf);
  153. out:
  154. mutex_unlock(&dcookie_mutex);
  155. return err;
  156. }
  157. static int dcookie_init(void)
  158. {
  159. struct list_head * d;
  160. unsigned int i, hash_bits;
  161. int err = -ENOMEM;
  162. dcookie_cache = kmem_cache_create("dcookie_cache",
  163. sizeof(struct dcookie_struct),
  164. 0, 0, NULL, NULL);
  165. if (!dcookie_cache)
  166. goto out;
  167. dcookie_hashtable = kmalloc(PAGE_SIZE, GFP_KERNEL);
  168. if (!dcookie_hashtable)
  169. goto out_kmem;
  170. err = 0;
  171. /*
  172. * Find the power-of-two list-heads that can fit into the allocation..
  173. * We don't guarantee that "sizeof(struct list_head)" is necessarily
  174. * a power-of-two.
  175. */
  176. hash_size = PAGE_SIZE / sizeof(struct list_head);
  177. hash_bits = 0;
  178. do {
  179. hash_bits++;
  180. } while ((hash_size >> hash_bits) != 0);
  181. hash_bits--;
  182. /*
  183. * Re-calculate the actual number of entries and the mask
  184. * from the number of bits we can fit.
  185. */
  186. hash_size = 1UL << hash_bits;
  187. /* And initialize the newly allocated array */
  188. d = dcookie_hashtable;
  189. i = hash_size;
  190. do {
  191. INIT_LIST_HEAD(d);
  192. d++;
  193. i--;
  194. } while (i);
  195. out:
  196. return err;
  197. out_kmem:
  198. kmem_cache_destroy(dcookie_cache);
  199. goto out;
  200. }
  201. static void free_dcookie(struct dcookie_struct * dcs)
  202. {
  203. dcs->dentry->d_cookie = NULL;
  204. dput(dcs->dentry);
  205. mntput(dcs->vfsmnt);
  206. kmem_cache_free(dcookie_cache, dcs);
  207. }
  208. static void dcookie_exit(void)
  209. {
  210. struct list_head * list;
  211. struct list_head * pos;
  212. struct list_head * pos2;
  213. struct dcookie_struct * dcs;
  214. size_t i;
  215. for (i = 0; i < hash_size; ++i) {
  216. list = dcookie_hashtable + i;
  217. list_for_each_safe(pos, pos2, list) {
  218. dcs = list_entry(pos, struct dcookie_struct, hash_list);
  219. list_del(&dcs->hash_list);
  220. free_dcookie(dcs);
  221. }
  222. }
  223. kfree(dcookie_hashtable);
  224. kmem_cache_destroy(dcookie_cache);
  225. }
  226. struct dcookie_user {
  227. struct list_head next;
  228. };
  229. struct dcookie_user * dcookie_register(void)
  230. {
  231. struct dcookie_user * user;
  232. mutex_lock(&dcookie_mutex);
  233. user = kmalloc(sizeof(struct dcookie_user), GFP_KERNEL);
  234. if (!user)
  235. goto out;
  236. if (!is_live() && dcookie_init())
  237. goto out_free;
  238. list_add(&user->next, &dcookie_users);
  239. out:
  240. mutex_unlock(&dcookie_mutex);
  241. return user;
  242. out_free:
  243. kfree(user);
  244. user = NULL;
  245. goto out;
  246. }
  247. void dcookie_unregister(struct dcookie_user * user)
  248. {
  249. mutex_lock(&dcookie_mutex);
  250. list_del(&user->next);
  251. kfree(user);
  252. if (!is_live())
  253. dcookie_exit();
  254. mutex_unlock(&dcookie_mutex);
  255. }
  256. EXPORT_SYMBOL_GPL(dcookie_register);
  257. EXPORT_SYMBOL_GPL(dcookie_unregister);
  258. EXPORT_SYMBOL_GPL(get_dcookie);