proc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* proc.c: proc 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/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <asm/errno.h>
  19. #include "internal.h"
  20. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  21. static int proc_keys_open(struct inode *inode, struct file *file);
  22. static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
  23. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
  24. static void proc_keys_stop(struct seq_file *p, void *v);
  25. static int proc_keys_show(struct seq_file *m, void *v);
  26. static const struct seq_operations proc_keys_ops = {
  27. .start = proc_keys_start,
  28. .next = proc_keys_next,
  29. .stop = proc_keys_stop,
  30. .show = proc_keys_show,
  31. };
  32. static const struct file_operations proc_keys_fops = {
  33. .open = proc_keys_open,
  34. .read = seq_read,
  35. .llseek = seq_lseek,
  36. .release = seq_release,
  37. };
  38. #endif
  39. static int proc_key_users_open(struct inode *inode, struct file *file);
  40. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
  41. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
  42. static void proc_key_users_stop(struct seq_file *p, void *v);
  43. static int proc_key_users_show(struct seq_file *m, void *v);
  44. static const struct seq_operations proc_key_users_ops = {
  45. .start = proc_key_users_start,
  46. .next = proc_key_users_next,
  47. .stop = proc_key_users_stop,
  48. .show = proc_key_users_show,
  49. };
  50. static const struct file_operations proc_key_users_fops = {
  51. .open = proc_key_users_open,
  52. .read = seq_read,
  53. .llseek = seq_lseek,
  54. .release = seq_release,
  55. };
  56. /*****************************************************************************/
  57. /*
  58. * declare the /proc files
  59. */
  60. static int __init key_proc_init(void)
  61. {
  62. struct proc_dir_entry *p;
  63. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  64. p = proc_create("keys", 0, NULL, &proc_keys_fops);
  65. if (!p)
  66. panic("Cannot create /proc/keys\n");
  67. #endif
  68. p = proc_create("key-users", 0, NULL, &proc_key_users_fops);
  69. if (!p)
  70. panic("Cannot create /proc/key-users\n");
  71. return 0;
  72. } /* end key_proc_init() */
  73. __initcall(key_proc_init);
  74. /*****************************************************************************/
  75. /*
  76. * implement "/proc/keys" to provides a list of the keys on the system
  77. */
  78. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  79. static int proc_keys_open(struct inode *inode, struct file *file)
  80. {
  81. return seq_open(file, &proc_keys_ops);
  82. }
  83. static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
  84. {
  85. struct rb_node *_p;
  86. loff_t pos = *_pos;
  87. spin_lock(&key_serial_lock);
  88. _p = rb_first(&key_serial_tree);
  89. while (pos > 0 && _p) {
  90. pos--;
  91. _p = rb_next(_p);
  92. }
  93. return _p;
  94. }
  95. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
  96. {
  97. (*_pos)++;
  98. return rb_next((struct rb_node *) v);
  99. }
  100. static void proc_keys_stop(struct seq_file *p, void *v)
  101. {
  102. spin_unlock(&key_serial_lock);
  103. }
  104. static int proc_keys_show(struct seq_file *m, void *v)
  105. {
  106. struct rb_node *_p = v;
  107. struct key *key = rb_entry(_p, struct key, serial_node);
  108. struct timespec now;
  109. unsigned long timo;
  110. char xbuf[12];
  111. int rc;
  112. /* check whether the current task is allowed to view the key (assuming
  113. * non-possession) */
  114. rc = key_task_permission(make_key_ref(key, 0), current, KEY_VIEW);
  115. if (rc < 0)
  116. return 0;
  117. now = current_kernel_time();
  118. rcu_read_lock();
  119. /* come up with a suitable timeout value */
  120. if (key->expiry == 0) {
  121. memcpy(xbuf, "perm", 5);
  122. }
  123. else if (now.tv_sec >= key->expiry) {
  124. memcpy(xbuf, "expd", 5);
  125. }
  126. else {
  127. timo = key->expiry - now.tv_sec;
  128. if (timo < 60)
  129. sprintf(xbuf, "%lus", timo);
  130. else if (timo < 60*60)
  131. sprintf(xbuf, "%lum", timo / 60);
  132. else if (timo < 60*60*24)
  133. sprintf(xbuf, "%luh", timo / (60*60));
  134. else if (timo < 60*60*24*7)
  135. sprintf(xbuf, "%lud", timo / (60*60*24));
  136. else
  137. sprintf(xbuf, "%luw", timo / (60*60*24*7));
  138. }
  139. #define showflag(KEY, LETTER, FLAG) \
  140. (test_bit(FLAG, &(KEY)->flags) ? LETTER : '-')
  141. seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  142. key->serial,
  143. showflag(key, 'I', KEY_FLAG_INSTANTIATED),
  144. showflag(key, 'R', KEY_FLAG_REVOKED),
  145. showflag(key, 'D', KEY_FLAG_DEAD),
  146. showflag(key, 'Q', KEY_FLAG_IN_QUOTA),
  147. showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT),
  148. showflag(key, 'N', KEY_FLAG_NEGATIVE),
  149. atomic_read(&key->usage),
  150. xbuf,
  151. key->perm,
  152. key->uid,
  153. key->gid,
  154. key->type->name);
  155. #undef showflag
  156. if (key->type->describe)
  157. key->type->describe(key, m);
  158. seq_putc(m, '\n');
  159. rcu_read_unlock();
  160. return 0;
  161. }
  162. #endif /* CONFIG_KEYS_DEBUG_PROC_KEYS */
  163. /*****************************************************************************/
  164. /*
  165. * implement "/proc/key-users" to provides a list of the key users
  166. */
  167. static int proc_key_users_open(struct inode *inode, struct file *file)
  168. {
  169. return seq_open(file, &proc_key_users_ops);
  170. }
  171. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  172. {
  173. struct rb_node *_p;
  174. loff_t pos = *_pos;
  175. spin_lock(&key_user_lock);
  176. _p = rb_first(&key_user_tree);
  177. while (pos > 0 && _p) {
  178. pos--;
  179. _p = rb_next(_p);
  180. }
  181. return _p;
  182. }
  183. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  184. {
  185. (*_pos)++;
  186. return rb_next((struct rb_node *) v);
  187. }
  188. static void proc_key_users_stop(struct seq_file *p, void *v)
  189. {
  190. spin_unlock(&key_user_lock);
  191. }
  192. static int proc_key_users_show(struct seq_file *m, void *v)
  193. {
  194. struct rb_node *_p = v;
  195. struct key_user *user = rb_entry(_p, struct key_user, node);
  196. unsigned maxkeys = (user->uid == 0) ?
  197. key_quota_root_maxkeys : key_quota_maxkeys;
  198. unsigned maxbytes = (user->uid == 0) ?
  199. key_quota_root_maxbytes : key_quota_maxbytes;
  200. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  201. user->uid,
  202. atomic_read(&user->usage),
  203. atomic_read(&user->nkeys),
  204. atomic_read(&user->nikeys),
  205. user->qnkeys,
  206. maxkeys,
  207. user->qnbytes,
  208. maxbytes);
  209. return 0;
  210. }