proc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* procfs files for key database enumeration
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/fs.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include <asm/errno.h>
  18. #include "internal.h"
  19. static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
  20. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
  21. static void proc_keys_stop(struct seq_file *p, void *v);
  22. static int proc_keys_show(struct seq_file *m, void *v);
  23. static const struct seq_operations proc_keys_ops = {
  24. .start = proc_keys_start,
  25. .next = proc_keys_next,
  26. .stop = proc_keys_stop,
  27. .show = proc_keys_show,
  28. };
  29. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
  30. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
  31. static void proc_key_users_stop(struct seq_file *p, void *v);
  32. static int proc_key_users_show(struct seq_file *m, void *v);
  33. static const struct seq_operations proc_key_users_ops = {
  34. .start = proc_key_users_start,
  35. .next = proc_key_users_next,
  36. .stop = proc_key_users_stop,
  37. .show = proc_key_users_show,
  38. };
  39. /*
  40. * Declare the /proc files.
  41. */
  42. static int __init key_proc_init(void)
  43. {
  44. struct proc_dir_entry *p;
  45. p = proc_create_seq("keys", 0, NULL, &proc_keys_ops);
  46. if (!p)
  47. panic("Cannot create /proc/keys\n");
  48. p = proc_create_seq("key-users", 0, NULL, &proc_key_users_ops);
  49. if (!p)
  50. panic("Cannot create /proc/key-users\n");
  51. return 0;
  52. }
  53. __initcall(key_proc_init);
  54. /*
  55. * Implement "/proc/keys" to provide a list of the keys on the system that
  56. * grant View permission to the caller.
  57. */
  58. static struct rb_node *key_serial_next(struct seq_file *p, struct rb_node *n)
  59. {
  60. struct user_namespace *user_ns = seq_user_ns(p);
  61. n = rb_next(n);
  62. while (n) {
  63. struct key *key = rb_entry(n, struct key, serial_node);
  64. if (kuid_has_mapping(user_ns, key->user->uid))
  65. break;
  66. n = rb_next(n);
  67. }
  68. return n;
  69. }
  70. static struct key *find_ge_key(struct seq_file *p, key_serial_t id)
  71. {
  72. struct user_namespace *user_ns = seq_user_ns(p);
  73. struct rb_node *n = key_serial_tree.rb_node;
  74. struct key *minkey = NULL;
  75. while (n) {
  76. struct key *key = rb_entry(n, struct key, serial_node);
  77. if (id < key->serial) {
  78. if (!minkey || minkey->serial > key->serial)
  79. minkey = key;
  80. n = n->rb_left;
  81. } else if (id > key->serial) {
  82. n = n->rb_right;
  83. } else {
  84. minkey = key;
  85. break;
  86. }
  87. key = NULL;
  88. }
  89. if (!minkey)
  90. return NULL;
  91. for (;;) {
  92. if (kuid_has_mapping(user_ns, minkey->user->uid))
  93. return minkey;
  94. n = rb_next(&minkey->serial_node);
  95. if (!n)
  96. return NULL;
  97. minkey = rb_entry(n, struct key, serial_node);
  98. }
  99. }
  100. static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
  101. __acquires(key_serial_lock)
  102. {
  103. key_serial_t pos = *_pos;
  104. struct key *key;
  105. spin_lock(&key_serial_lock);
  106. if (*_pos > INT_MAX)
  107. return NULL;
  108. key = find_ge_key(p, pos);
  109. if (!key)
  110. return NULL;
  111. *_pos = key->serial;
  112. return &key->serial_node;
  113. }
  114. static inline key_serial_t key_node_serial(struct rb_node *n)
  115. {
  116. struct key *key = rb_entry(n, struct key, serial_node);
  117. return key->serial;
  118. }
  119. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
  120. {
  121. struct rb_node *n;
  122. n = key_serial_next(p, v);
  123. if (n)
  124. *_pos = key_node_serial(n);
  125. return n;
  126. }
  127. static void proc_keys_stop(struct seq_file *p, void *v)
  128. __releases(key_serial_lock)
  129. {
  130. spin_unlock(&key_serial_lock);
  131. }
  132. static int proc_keys_show(struct seq_file *m, void *v)
  133. {
  134. struct rb_node *_p = v;
  135. struct key *key = rb_entry(_p, struct key, serial_node);
  136. unsigned long flags;
  137. key_ref_t key_ref, skey_ref;
  138. time64_t now, expiry;
  139. char xbuf[16];
  140. short state;
  141. u64 timo;
  142. int rc;
  143. struct keyring_search_context ctx = {
  144. .index_key = key->index_key,
  145. .cred = m->file->f_cred,
  146. .match_data.cmp = lookup_user_key_possessed,
  147. .match_data.raw_data = key,
  148. .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  149. .flags = KEYRING_SEARCH_NO_STATE_CHECK,
  150. };
  151. key_ref = make_key_ref(key, 0);
  152. /* determine if the key is possessed by this process (a test we can
  153. * skip if the key does not indicate the possessor can view it
  154. */
  155. if (key->perm & KEY_POS_VIEW) {
  156. skey_ref = search_my_process_keyrings(&ctx);
  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. }