1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.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
21#ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
22static int proc_keys_open(struct inode *inode, struct file *file);
23static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
24static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
25static void proc_keys_stop(struct seq_file *p, void *v);
26static int proc_keys_show(struct seq_file *m, void *v);
27
28static const struct seq_operations proc_keys_ops = {
29 .start = proc_keys_start,
30 .next = proc_keys_next,
31 .stop = proc_keys_stop,
32 .show = proc_keys_show,
33};
34
35static const struct file_operations proc_keys_fops = {
36 .open = proc_keys_open,
37 .read = seq_read,
38 .llseek = seq_lseek,
39 .release = seq_release,
40};
41#endif
42
43static int proc_key_users_open(struct inode *inode, struct file *file);
44static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
45static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
46static void proc_key_users_stop(struct seq_file *p, void *v);
47static int proc_key_users_show(struct seq_file *m, void *v);
48
49static const struct seq_operations proc_key_users_ops = {
50 .start = proc_key_users_start,
51 .next = proc_key_users_next,
52 .stop = proc_key_users_stop,
53 .show = proc_key_users_show,
54};
55
56static const struct file_operations proc_key_users_fops = {
57 .open = proc_key_users_open,
58 .read = seq_read,
59 .llseek = seq_lseek,
60 .release = seq_release,
61};
62
63
64
65
66static int __init key_proc_init(void)
67{
68 struct proc_dir_entry *p;
69
70#ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
71 p = proc_create("keys", 0, NULL, &proc_keys_fops);
72 if (!p)
73 panic("Cannot create /proc/keys\n");
74#endif
75
76 p = proc_create("key-users", 0, NULL, &proc_key_users_fops);
77 if (!p)
78 panic("Cannot create /proc/key-users\n");
79
80 return 0;
81}
82
83__initcall(key_proc_init);
84
85
86
87
88
89#ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
90
91static struct rb_node *key_serial_next(struct seq_file *p, struct rb_node *n)
92{
93 struct user_namespace *user_ns = seq_user_ns(p);
94
95 n = rb_next(n);
96 while (n) {
97 struct key *key = rb_entry(n, struct key, serial_node);
98 if (kuid_has_mapping(user_ns, key->user->uid))
99 break;
100 n = rb_next(n);
101 }
102 return n;
103}
104
105static int proc_keys_open(struct inode *inode, struct file *file)
106{
107 return seq_open(file, &proc_keys_ops);
108}
109
110static struct key *find_ge_key(struct seq_file *p, key_serial_t id)
111{
112 struct user_namespace *user_ns = seq_user_ns(p);
113 struct rb_node *n = key_serial_tree.rb_node;
114 struct key *minkey = NULL;
115
116 while (n) {
117 struct key *key = rb_entry(n, struct key, serial_node);
118 if (id < key->serial) {
119 if (!minkey || minkey->serial > key->serial)
120 minkey = key;
121 n = n->rb_left;
122 } else if (id > key->serial) {
123 n = n->rb_right;
124 } else {
125 minkey = key;
126 break;
127 }
128 key = NULL;
129 }
130
131 if (!minkey)
132 return NULL;
133
134 for (;;) {
135 if (kuid_has_mapping(user_ns, minkey->user->uid))
136 return minkey;
137 n = rb_next(&minkey->serial_node);
138 if (!n)
139 return NULL;
140 minkey = rb_entry(n, struct key, serial_node);
141 }
142}
143
144static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
145 __acquires(key_serial_lock)
146{
147 key_serial_t pos = *_pos;
148 struct key *key;
149
150 spin_lock(&key_serial_lock);
151
152 if (*_pos > INT_MAX)
153 return NULL;
154 key = find_ge_key(p, pos);
155 if (!key)
156 return NULL;
157 *_pos = key->serial;
158 return &key->serial_node;
159}
160
161static inline key_serial_t key_node_serial(struct rb_node *n)
162{
163 struct key *key = rb_entry(n, struct key, serial_node);
164 return key->serial;
165}
166
167static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
168{
169 struct rb_node *n;
170
171 n = key_serial_next(p, v);
172 if (n)
173 *_pos = key_node_serial(n);
174 return n;
175}
176
177static void proc_keys_stop(struct seq_file *p, void *v)
178 __releases(key_serial_lock)
179{
180 spin_unlock(&key_serial_lock);
181}
182
183static int proc_keys_show(struct seq_file *m, void *v)
184{
185 struct rb_node *_p = v;
186 struct key *key = rb_entry(_p, struct key, serial_node);
187 struct timespec now;
188 time_t expiry;
189 unsigned long timo;
190 unsigned long flags;
191 key_ref_t key_ref, skey_ref;
192 char xbuf[16];
193 short state;
194 int rc;
195
196 struct keyring_search_context ctx = {
197 .index_key.type = key->type,
198 .index_key.description = key->description,
199 .cred = current_cred(),
200 .match = lookup_user_key_possessed,
201 .match_data = key,
202 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
203 KEYRING_SEARCH_LOOKUP_DIRECT),
204 };
205
206 key_ref = make_key_ref(key, 0);
207
208
209
210
211 if (key->perm & KEY_POS_VIEW) {
212 skey_ref = search_my_process_keyrings(&ctx);
213 if (!IS_ERR(skey_ref)) {
214 key_ref_put(skey_ref);
215 key_ref = make_key_ref(key, 1);
216 }
217 }
218
219
220
221
222
223
224 rc = key_task_permission(key_ref, ctx.cred, KEY_VIEW);
225 if (rc < 0)
226 return 0;
227
228 now = current_kernel_time();
229
230 rcu_read_lock();
231
232
233 expiry = READ_ONCE(key->expiry);
234 if (expiry == 0) {
235 memcpy(xbuf, "perm", 5);
236 } else if (now.tv_sec >= expiry) {
237 memcpy(xbuf, "expd", 5);
238 } else {
239 timo = expiry - now.tv_sec;
240
241 if (timo < 60)
242 sprintf(xbuf, "%lus", timo);
243 else if (timo < 60*60)
244 sprintf(xbuf, "%lum", timo / 60);
245 else if (timo < 60*60*24)
246 sprintf(xbuf, "%luh", timo / (60*60));
247 else if (timo < 60*60*24*7)
248 sprintf(xbuf, "%lud", timo / (60*60*24));
249 else
250 sprintf(xbuf, "%luw", timo / (60*60*24*7));
251 }
252
253 state = key_read_state(key);
254
255#define showflag(FLAGS, LETTER, FLAG) \
256 ((FLAGS & (1 << FLAG)) ? LETTER : '-')
257
258 flags = READ_ONCE(key->flags);
259 seq_printf(m, "%08x %c%c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
260 key->serial,
261 state != KEY_IS_UNINSTANTIATED ? 'I' : '-',
262 showflag(flags, 'R', KEY_FLAG_REVOKED),
263 showflag(flags, 'D', KEY_FLAG_DEAD),
264 showflag(flags, 'Q', KEY_FLAG_IN_QUOTA),
265 showflag(flags, 'U', KEY_FLAG_USER_CONSTRUCT),
266 state < 0 ? 'N' : '-',
267 showflag(flags, 'i', KEY_FLAG_INVALIDATED),
268 atomic_read(&key->usage),
269 xbuf,
270 key->perm,
271 from_kuid_munged(seq_user_ns(m), key->uid),
272 from_kgid_munged(seq_user_ns(m), key->gid),
273 key->type->name);
274
275#undef showflag
276
277 if (key->type->describe)
278 key->type->describe(key, m);
279 seq_putc(m, '\n');
280
281 rcu_read_unlock();
282 return 0;
283}
284
285#endif
286
287static struct rb_node *__key_user_next(struct user_namespace *user_ns, struct rb_node *n)
288{
289 while (n) {
290 struct key_user *user = rb_entry(n, struct key_user, node);
291 if (kuid_has_mapping(user_ns, user->uid))
292 break;
293 n = rb_next(n);
294 }
295 return n;
296}
297
298static struct rb_node *key_user_next(struct user_namespace *user_ns, struct rb_node *n)
299{
300 return __key_user_next(user_ns, rb_next(n));
301}
302
303static struct rb_node *key_user_first(struct user_namespace *user_ns, struct rb_root *r)
304{
305 struct rb_node *n = rb_first(r);
306 return __key_user_next(user_ns, n);
307}
308
309
310
311
312
313static int proc_key_users_open(struct inode *inode, struct file *file)
314{
315 return seq_open(file, &proc_key_users_ops);
316}
317
318static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
319 __acquires(key_user_lock)
320{
321 struct rb_node *_p;
322 loff_t pos = *_pos;
323
324 spin_lock(&key_user_lock);
325
326 _p = key_user_first(seq_user_ns(p), &key_user_tree);
327 while (pos > 0 && _p) {
328 pos--;
329 _p = key_user_next(seq_user_ns(p), _p);
330 }
331
332 return _p;
333}
334
335static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
336{
337 (*_pos)++;
338 return key_user_next(seq_user_ns(p), (struct rb_node *)v);
339}
340
341static void proc_key_users_stop(struct seq_file *p, void *v)
342 __releases(key_user_lock)
343{
344 spin_unlock(&key_user_lock);
345}
346
347static int proc_key_users_show(struct seq_file *m, void *v)
348{
349 struct rb_node *_p = v;
350 struct key_user *user = rb_entry(_p, struct key_user, node);
351 unsigned maxkeys = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
352 key_quota_root_maxkeys : key_quota_maxkeys;
353 unsigned maxbytes = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
354 key_quota_root_maxbytes : key_quota_maxbytes;
355
356 seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
357 from_kuid_munged(seq_user_ns(m), user->uid),
358 atomic_read(&user->usage),
359 atomic_read(&user->nkeys),
360 atomic_read(&user->nikeys),
361 user->qnkeys,
362 maxkeys,
363 user->qnbytes,
364 maxbytes);
365
366 return 0;
367}
368