proc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. else
  126. (*_pos)++;
  127. return n;
  128. }
  129. static void proc_keys_stop(struct seq_file *p, void *v)
  130. __releases(key_serial_lock)
  131. {
  132. spin_unlock(&key_serial_lock);
  133. }
  134. static int proc_keys_show(struct seq_file *m, void *v)
  135. {
  136. struct rb_node *_p = v;
  137. struct key *key = rb_entry(_p, struct key, serial_node);
  138. unsigned long flags;
  139. key_ref_t key_ref, skey_ref;
  140. time64_t now, expiry;
  141. char xbuf[16];
  142. short state;
  143. u64 timo;
  144. int rc;
  145. struct keyring_search_context ctx = {
  146. .index_key = key->index_key,
  147. .cred = m->file->f_cred,
  148. .match_data.cmp = lookup_user_key_possessed,
  149. .match_data.raw_data = key,
  150. .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  151. .flags = KEYRING_SEARCH_NO_STATE_CHECK,
  152. };
  153. key_ref = make_key_ref(key, 0);
  154. /* determine if the key is possessed by this process (a test we can
  155. * skip if the key does not indicate the possessor can view it
  156. */
  157. if (key->perm & KEY_POS_VIEW) {
  158. skey_ref = search_my_process_keyrings(&ctx);
  159. if (!IS_ERR(skey_ref)) {
  160. key_ref_put(skey_ref);
  161. key_ref = make_key_ref(key, 1);
  162. }
  163. }
  164. /* check whether the current task is allowed to view the key */
  165. rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
  166. if (rc < 0)
  167. return 0;
  168. now = ktime_get_real_seconds();
  169. rcu_read_lock();
  170. /* come up with a suitable timeout value */
  171. expiry = READ_ONCE(key->expiry);
  172. if (expiry == 0) {
  173. memcpy(xbuf, "perm", 5);
  174. } else if (now >= expiry) {
  175. memcpy(xbuf, "expd", 5);
  176. } else {
  177. timo = expiry - now;
  178. if (timo < 60)
  179. sprintf(xbuf, "%llus", timo);
  180. else if (timo < 60*60)
  181. sprintf(xbuf, "%llum", div_u64(timo, 60));
  182. else if (timo < 60*60*24)
  183. sprintf(xbuf, "%lluh", div_u64(timo, 60 * 60));
  184. else if (timo < 60*60*24*7)
  185. sprintf(xbuf, "%llud", div_u64(timo, 60 * 60 * 24));
  186. else
  187. sprintf(xbuf, "%lluw", div_u64(timo, 60 * 60 * 24 * 7));
  188. }
  189. state = key_read_state(key);
  190. #define showflag(FLAGS, LETTER, FLAG) \
  191. ((FLAGS & (1 << FLAG)) ? LETTER : '-')
  192. flags = READ_ONCE(key->flags);
  193. seq_printf(m, "%08x %c%c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  194. key->serial,
  195. state != KEY_IS_UNINSTANTIATED ? 'I' : '-',
  196. showflag(flags, 'R', KEY_FLAG_REVOKED),
  197. showflag(flags, 'D', KEY_FLAG_DEAD),
  198. showflag(flags, 'Q', KEY_FLAG_IN_QUOTA),
  199. showflag(flags, 'U', KEY_FLAG_USER_CONSTRUCT),
  200. state < 0 ? 'N' : '-',
  201. showflag(flags, 'i', KEY_FLAG_INVALIDATED),
  202. refcount_read(&key->usage),
  203. xbuf,
  204. key->perm,
  205. from_kuid_munged(seq_user_ns(m), key->uid),
  206. from_kgid_munged(seq_user_ns(m), key->gid),
  207. key->type->name);
  208. #undef showflag
  209. if (key->type->describe)
  210. key->type->describe(key, m);
  211. seq_putc(m, '\n');
  212. rcu_read_unlock();
  213. return 0;
  214. }
  215. static struct rb_node *__key_user_next(struct user_namespace *user_ns, struct rb_node *n)
  216. {
  217. while (n) {
  218. struct key_user *user = rb_entry(n, struct key_user, node);
  219. if (kuid_has_mapping(user_ns, user->uid))
  220. break;
  221. n = rb_next(n);
  222. }
  223. return n;
  224. }
  225. static struct rb_node *key_user_next(struct user_namespace *user_ns, struct rb_node *n)
  226. {
  227. return __key_user_next(user_ns, rb_next(n));
  228. }
  229. static struct rb_node *key_user_first(struct user_namespace *user_ns, struct rb_root *r)
  230. {
  231. struct rb_node *n = rb_first(r);
  232. return __key_user_next(user_ns, n);
  233. }
  234. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  235. __acquires(key_user_lock)
  236. {
  237. struct rb_node *_p;
  238. loff_t pos = *_pos;
  239. spin_lock(&key_user_lock);
  240. _p = key_user_first(seq_user_ns(p), &key_user_tree);
  241. while (pos > 0 && _p) {
  242. pos--;
  243. _p = key_user_next(seq_user_ns(p), _p);
  244. }
  245. return _p;
  246. }
  247. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  248. {
  249. (*_pos)++;
  250. return key_user_next(seq_user_ns(p), (struct rb_node *)v);
  251. }
  252. static void proc_key_users_stop(struct seq_file *p, void *v)
  253. __releases(key_user_lock)
  254. {
  255. spin_unlock(&key_user_lock);
  256. }
  257. static int proc_key_users_show(struct seq_file *m, void *v)
  258. {
  259. struct rb_node *_p = v;
  260. struct key_user *user = rb_entry(_p, struct key_user, node);
  261. unsigned maxkeys = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
  262. key_quota_root_maxkeys : key_quota_maxkeys;
  263. unsigned maxbytes = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
  264. key_quota_root_maxbytes : key_quota_maxbytes;
  265. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  266. from_kuid_munged(seq_user_ns(m), user->uid),
  267. refcount_read(&user->usage),
  268. atomic_read(&user->nkeys),
  269. atomic_read(&user->nikeys),
  270. user->qnkeys,
  271. maxkeys,
  272. user->qnbytes,
  273. maxbytes);
  274. return 0;
  275. }