dcookies.c 6.8 KB

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