dcookies.c 7.1 KB

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