proc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* procfs files for key database enumeration
  3. *
  4. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/init.h>
  8. #include <linux/sched.h>
  9. #include <linux/fs.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/seq_file.h>
  12. #include <asm/errno.h>
  13. #include "internal.h"
  14. static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
  15. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
  16. static void proc_keys_stop(struct seq_file *p, void *v);
  17. static int proc_keys_show(struct seq_file *m, void *v);
  18. static const struct seq_operations proc_keys_ops = {
  19. .start = proc_keys_start,
  20. .next = proc_keys_next,
  21. .stop = proc_keys_stop,
  22. .show = proc_keys_show,
  23. };
  24. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
  25. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
  26. static void proc_key_users_stop(struct seq_file *p, void *v);
  27. static int proc_key_users_show(struct seq_file *m, void *v);
  28. static const struct seq_operations proc_key_users_ops = {
  29. .start = proc_key_users_start,
  30. .next = proc_key_users_next,
  31. .stop = proc_key_users_stop,
  32. .show = proc_key_users_show,
  33. };
  34. /*
  35. * Declare the /proc files.
  36. */
  37. static int __init key_proc_init(void)
  38. {
  39. struct proc_dir_entry *p;
  40. p = proc_create_seq("keys", 0, NULL, &proc_keys_ops);
  41. if (!p)
  42. panic("Cannot create /proc/keys\n");
  43. p = proc_create_seq("key-users", 0, NULL, &proc_key_users_ops);
  44. if (!p)
  45. panic("Cannot create /proc/key-users\n");
  46. return 0;
  47. }
  48. __initcall(key_proc_init);
  49. /*
  50. * Implement "/proc/keys" to provide a list of the keys on the system that
  51. * grant View permission to the caller.
  52. */
  53. static struct rb_node *key_serial_next(struct seq_file *p, struct rb_node *n)
  54. {
  55. struct user_namespace *user_ns = seq_user_ns(p);
  56. n = rb_next(n);
  57. while (n) {
  58. struct key *key = rb_entry(n, struct key, serial_node);
  59. if (kuid_has_mapping(user_ns, key->user->uid))
  60. break;
  61. n = rb_next(n);
  62. }
  63. return n;
  64. }
  65. static struct key *find_ge_key(struct seq_file *p, key_serial_t id)
  66. {
  67. struct user_namespace *user_ns = seq_user_ns(p);
  68. struct rb_node *n = key_serial_tree.rb_node;
  69. struct key *minkey = NULL;
  70. while (n) {
  71. struct key *key = rb_entry(n, struct key, serial_node);
  72. if (id < key->serial) {
  73. if (!minkey || minkey->serial > key->serial)
  74. minkey = key;
  75. n = n->rb_left;
  76. } else if (id > key->serial) {
  77. n = n->rb_right;
  78. } else {
  79. minkey = key;
  80. break;
  81. }
  82. key = NULL;
  83. }
  84. if (!minkey)
  85. return NULL;
  86. for (;;) {
  87. if (kuid_has_mapping(user_ns, minkey->user->uid))
  88. return minkey;
  89. n = rb_next(&minkey->serial_node);
  90. if (!n)
  91. return NULL;
  92. minkey = rb_entry(n, struct key, serial_node);
  93. }
  94. }
  95. static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
  96. __acquires(key_serial_lock)
  97. {
  98. key_serial_t pos = *_pos;
  99. struct key *key;
  100. spin_lock(&key_serial_lock);
  101. if (*_pos > INT_MAX)
  102. return NULL;
  103. key = find_ge_key(p, pos);
  104. if (!key)
  105. return NULL;
  106. *_pos = key->serial;
  107. return &key->serial_node;
  108. }
  109. static inline key_serial_t key_node_serial(struct rb_node *n)
  110. {
  111. struct key *key = rb_entry(n, struct key, serial_node);
  112. return key->serial;
  113. }
  114. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
  115. {
  116. struct rb_node *n;
  117. n = key_serial_next(p, v);
  118. if (n)
  119. *_pos = key_node_serial(n);
  120. else
  121. (*_pos)++;
  122. return n;
  123. }
  124. static void proc_keys_stop(struct seq_file *p, void *v)
  125. __releases(key_serial_lock)
  126. {
  127. spin_unlock(&key_serial_lock);
  128. }
  129. static int proc_keys_show(struct seq_file *m, void *v)
  130. {
  131. struct rb_node *_p = v;
  132. struct key *key = rb_entry(_p, struct key, serial_node);
  133. unsigned long flags;
  134. key_ref_t key_ref, skey_ref;
  135. time64_t now, expiry;
  136. char xbuf[16];
  137. short state;
  138. u64 timo;
  139. int rc;
  140. struct keyring_search_context ctx = {
  141. .index_key = key->index_key,
  142. .cred = m->file->f_cred,
  143. .match_data.cmp = lookup_user_key_possessed,
  144. .match_data.raw_data = key,
  145. .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  146. .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
  147. KEYRING_SEARCH_RECURSE),
  148. };
  149. key_ref = make_key_ref(key, 0);
  150. /* determine if the key is possessed by this process (a test we can
  151. * skip if the key does not indicate the possessor can view it
  152. */
  153. if (key->perm & KEY_POS_VIEW) {
  154. rcu_read_lock();
  155. skey_ref = search_cred_keyrings_rcu(&ctx);
  156. rcu_read_unlock();
  157. if (!IS_ERR(skey_ref)) {
  158. key_ref_put(skey_ref);
  159. key_ref = make_key_ref(key, 1);
  160. }
  161. }
  162. /* check whether the current task is allowed to view the key */
  163. rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
  164. if (rc < 0)
  165. return 0;
  166. now = ktime_get_real_seconds();
  167. rcu_read_lock();
  168. /* come up with a suitable timeout value */
  169. expiry = READ_ONCE(key->expiry);
  170. if (expiry == 0) {
  171. memcpy(xbuf, "perm", 5);
  172. } else if (now >= expiry) {
  173. memcpy(xbuf, "expd", 5);
  174. } else {
  175. timo = expiry - now;
  176. if (timo < 60)
  177. sprintf(xbuf, "%llus", timo);
  178. else if (timo < 60*60)
  179. sprintf(xbuf, "%llum", div_u64(timo, 60));
  180. else if (timo < 60*60*24)
  181. sprintf(xbuf, "%lluh", div_u64(timo, 60 * 60));
  182. else if (timo < 60*60*24*7)
  183. sprintf(xbuf, "%llud", div_u64(timo, 60 * 60 * 24));
  184. else
  185. sprintf(xbuf, "%lluw", div_u64(timo, 60 * 60 * 24 * 7));
  186. }
  187. state = key_read_state(key);
  188. #define showflag(FLAGS, LETTER, FLAG) \
  189. ((FLAGS & (1 << FLAG)) ? LETTER : '-')
  190. flags = READ_ONCE(key->flags);
  191. seq_printf(m, "%08x %c%c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  192. key->serial,
  193. state != KEY_IS_UNINSTANTIATED ? 'I' : '-',
  194. showflag(flags, 'R', KEY_FLAG_REVOKED),
  195. showflag(flags, 'D', KEY_FLAG_DEAD),
  196. showflag(flags, 'Q', KEY_FLAG_IN_QUOTA),
  197. showflag(flags, 'U', KEY_FLAG_USER_CONSTRUCT),
  198. state < 0 ? 'N' : '-',
  199. showflag(flags, 'i', KEY_FLAG_INVALIDATED),
  200. refcount_read(&key->usage),
  201. xbuf,
  202. key->perm,
  203. from_kuid_munged(seq_user_ns(m), key->uid),
  204. from_kgid_munged(seq_user_ns(m), key->gid),
  205. key->type->name);
  206. #undef showflag
  207. if (key->type->describe)
  208. key->type->describe(key, m);
  209. seq_putc(m, '\n');
  210. rcu_read_unlock();
  211. return 0;
  212. }
  213. static struct rb_node *__key_user_next(struct user_namespace *user_ns, struct rb_node *n)
  214. {
  215. while (n) {
  216. struct key_user *user = rb_entry(n, struct key_user, node);
  217. if (kuid_has_mapping(user_ns, user->uid))
  218. break;
  219. n = rb_next(n);
  220. }
  221. return n;
  222. }
  223. static struct rb_node *key_user_next(struct user_namespace *user_ns, struct rb_node *n)
  224. {
  225. return __key_user_next(user_ns, rb_next(n));
  226. }
  227. static struct rb_node *key_user_first(struct user_namespace *user_ns, struct rb_root *r)
  228. {
  229. struct rb_node *n = rb_first(r);
  230. return __key_user_next(user_ns, n);
  231. }
  232. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  233. __acquires(key_user_lock)
  234. {
  235. struct rb_node *_p;
  236. loff_t pos = *_pos;
  237. spin_lock(&key_user_lock);
  238. _p = key_user_first(seq_user_ns(p), &key_user_tree);
  239. while (pos > 0 && _p) {
  240. pos--;
  241. _p = key_user_next(seq_user_ns(p), _p);
  242. }
  243. return _p;
  244. }
  245. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  246. {
  247. (*_pos)++;
  248. return key_user_next(seq_user_ns(p), (struct rb_node *)v);
  249. }
  250. static void proc_key_users_stop(struct seq_file *p, void *v)
  251. __releases(key_user_lock)
  252. {
  253. spin_unlock(&key_user_lock);
  254. }
  255. static int proc_key_users_show(struct seq_file *m, void *v)
  256. {
  257. struct rb_node *_p = v;
  258. struct key_user *user = rb_entry(_p, struct key_user, node);
  259. unsigned maxkeys = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
  260. key_quota_root_maxkeys : key_quota_maxkeys;
  261. unsigned maxbytes = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
  262. key_quota_root_maxbytes : key_quota_maxbytes;
  263. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  264. from_kuid_munged(seq_user_ns(m), user->uid),
  265. refcount_read(&user->usage),
  266. atomic_read(&user->nkeys),
  267. atomic_read(&user->nikeys),
  268. user->qnkeys,
  269. maxkeys,
  270. user->qnbytes,
  271. maxbytes);
  272. return 0;
  273. }