keys: Replace uid/gid/perm permissions checking with an ACL
[sfrench/cifs-2.6.git] / security / keys / persistent.c
1 /* General persistent per-UID keyrings register
2  *
3  * Copyright (C) 2013 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 Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #include <linux/user_namespace.h>
13 #include <linux/cred.h>
14
15 #include "internal.h"
16
17 unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
18
19 static struct key_acl persistent_register_keyring_acl = {
20         .usage  = REFCOUNT_INIT(1),
21         .nr_ace = 2,
22         .aces = {
23                 KEY_POSSESSOR_ACE(KEY_ACE_SEARCH | KEY_ACE_WRITE),
24                 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
25         }
26 };
27
28 static struct key_acl persistent_keyring_acl = {
29         .usage  = REFCOUNT_INIT(1),
30         .nr_ace = 2,
31         .possessor_viewable = true,
32         .aces = {
33                 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_WRITE |
34                                   KEY_ACE_SEARCH | KEY_ACE_LINK |
35                                   KEY_ACE_CLEAR | KEY_ACE_INVAL),
36                 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
37         }
38 };
39
40 /*
41  * Create the persistent keyring register for the current user namespace.
42  *
43  * Called with the namespace's sem locked for writing.
44  */
45 static int key_create_persistent_register(struct user_namespace *ns)
46 {
47         struct key *reg = keyring_alloc(".persistent_register",
48                                         KUIDT_INIT(0), KGIDT_INIT(0),
49                                         current_cred(),
50                                         &persistent_register_keyring_acl,
51                                         KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
52         if (IS_ERR(reg))
53                 return PTR_ERR(reg);
54
55         ns->persistent_keyring_register = reg;
56         return 0;
57 }
58
59 /*
60  * Create the persistent keyring for the specified user.
61  *
62  * Called with the namespace's sem locked for writing.
63  */
64 static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
65                                        struct keyring_index_key *index_key)
66 {
67         struct key *persistent;
68         key_ref_t reg_ref, persistent_ref;
69
70         if (!ns->persistent_keyring_register) {
71                 long err = key_create_persistent_register(ns);
72                 if (err < 0)
73                         return ERR_PTR(err);
74         } else {
75                 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
76                 persistent_ref = find_key_to_update(reg_ref, index_key);
77                 if (persistent_ref)
78                         return persistent_ref;
79         }
80
81         persistent = keyring_alloc(index_key->description,
82                                    uid, INVALID_GID, current_cred(),
83                                    &persistent_keyring_acl,
84                                    KEY_ALLOC_NOT_IN_QUOTA, NULL,
85                                    ns->persistent_keyring_register);
86         if (IS_ERR(persistent))
87                 return ERR_CAST(persistent);
88
89         return make_key_ref(persistent, true);
90 }
91
92 /*
93  * Get the persistent keyring for a specific UID and link it to the nominated
94  * keyring.
95  */
96 static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
97                                key_ref_t dest_ref)
98 {
99         struct keyring_index_key index_key;
100         struct key *persistent;
101         key_ref_t reg_ref, persistent_ref;
102         char buf[32];
103         long ret;
104
105         /* Look in the register if it exists */
106         memset(&index_key, 0, sizeof(index_key));
107         index_key.type = &key_type_keyring;
108         index_key.description = buf;
109         index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
110         key_set_index_key(&index_key);
111
112         if (ns->persistent_keyring_register) {
113                 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
114                 down_read(&ns->keyring_sem);
115                 persistent_ref = find_key_to_update(reg_ref, &index_key);
116                 up_read(&ns->keyring_sem);
117
118                 if (persistent_ref)
119                         goto found;
120         }
121
122         /* It wasn't in the register, so we'll need to create it.  We might
123          * also need to create the register.
124          */
125         down_write(&ns->keyring_sem);
126         persistent_ref = key_create_persistent(ns, uid, &index_key);
127         up_write(&ns->keyring_sem);
128         if (!IS_ERR(persistent_ref))
129                 goto found;
130
131         return PTR_ERR(persistent_ref);
132
133 found:
134         ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
135         if (ret == 0) {
136                 persistent = key_ref_to_ptr(persistent_ref);
137                 ret = key_link(key_ref_to_ptr(dest_ref), persistent);
138                 if (ret == 0) {
139                         key_set_timeout(persistent, persistent_keyring_expiry);
140                         ret = persistent->serial;
141                 }
142         }
143
144         key_ref_put(persistent_ref);
145         return ret;
146 }
147
148 /*
149  * Get the persistent keyring for a specific UID and link it to the nominated
150  * keyring.
151  */
152 long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
153 {
154         struct user_namespace *ns = current_user_ns();
155         key_ref_t dest_ref;
156         kuid_t uid;
157         long ret;
158
159         /* -1 indicates the current user */
160         if (_uid == (uid_t)-1) {
161                 uid = current_uid();
162         } else {
163                 uid = make_kuid(ns, _uid);
164                 if (!uid_valid(uid))
165                         return -EINVAL;
166
167                 /* You can only see your own persistent cache if you're not
168                  * sufficiently privileged.
169                  */
170                 if (!uid_eq(uid, current_uid()) &&
171                     !uid_eq(uid, current_euid()) &&
172                     !ns_capable(ns, CAP_SETUID))
173                         return -EPERM;
174         }
175
176         /* There must be a destination keyring */
177         dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
178         if (IS_ERR(dest_ref))
179                 return PTR_ERR(dest_ref);
180         if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
181                 ret = -ENOTDIR;
182                 goto out_put_dest;
183         }
184
185         ret = key_get_persistent(ns, uid, dest_ref);
186
187 out_put_dest:
188         key_ref_put(dest_ref);
189         return ret;
190 }