blob: 1ac08dcbf85db80185f8068c70b81c2f101a7327 [file] [log] [blame]
Trond Myklebust9a559ef2008-03-12 12:24:49 -04001/*
2 * Generic RPC credential
3 *
4 * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
5 */
6
7#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Trond Myklebust9a559ef2008-03-12 12:24:49 -04009#include <linux/types.h>
10#include <linux/module.h>
11#include <linux/sched.h>
12#include <linux/sunrpc/auth.h>
13#include <linux/sunrpc/clnt.h>
14#include <linux/sunrpc/debug.h>
15#include <linux/sunrpc/sched.h>
16
Jeff Laytonf895b252014-11-17 16:58:04 -050017#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Trond Myklebust9a559ef2008-03-12 12:24:49 -040018# define RPCDBG_FACILITY RPCDBG_AUTH
19#endif
20
Eric W. Biedermanbf37f792013-02-01 15:55:38 -080021#define RPC_MACHINE_CRED_USERID GLOBAL_ROOT_UID
22#define RPC_MACHINE_CRED_GROUPID GLOBAL_ROOT_GID
Trond Myklebust7c67db32008-04-07 20:50:11 -040023
Trond Myklebust9a559ef2008-03-12 12:24:49 -040024struct generic_cred {
25 struct rpc_cred gc_base;
26 struct auth_cred acred;
27};
28
29static struct rpc_auth generic_auth;
Trond Myklebust9a559ef2008-03-12 12:24:49 -040030static const struct rpc_credops generic_credops;
31
32/*
33 * Public call interface
34 */
35struct rpc_cred *rpc_lookup_cred(void)
36{
37 return rpcauth_lookupcred(&generic_auth, 0);
38}
39EXPORT_SYMBOL_GPL(rpc_lookup_cred);
40
Weston Andros Adamsonc065d222016-04-21 20:51:55 -040041struct rpc_cred *
42rpc_lookup_generic_cred(struct auth_cred *acred, int flags, gfp_t gfp)
43{
44 return rpcauth_lookup_credcache(&generic_auth, acred, flags, gfp);
45}
46EXPORT_SYMBOL_GPL(rpc_lookup_generic_cred);
47
NeilBrownbd956082014-07-14 11:28:20 +100048struct rpc_cred *rpc_lookup_cred_nonblock(void)
49{
50 return rpcauth_lookupcred(&generic_auth, RPCAUTH_LOOKUP_RCU);
51}
52EXPORT_SYMBOL_GPL(rpc_lookup_cred_nonblock);
53
Trond Myklebust7c67db32008-04-07 20:50:11 -040054/*
55 * Public call interface for looking up machine creds.
56 */
Trond Myklebust68c97152012-01-03 13:22:46 -050057struct rpc_cred *rpc_lookup_machine_cred(const char *service_name)
Trond Myklebust7c67db32008-04-07 20:50:11 -040058{
59 struct auth_cred acred = {
Trond Myklebustb4528762008-05-11 12:18:51 -070060 .uid = RPC_MACHINE_CRED_USERID,
61 .gid = RPC_MACHINE_CRED_GROUPID,
Trond Myklebust68c97152012-01-03 13:22:46 -050062 .principal = service_name,
Trond Myklebust7c67db32008-04-07 20:50:11 -040063 .machine_cred = 1,
64 };
65
Trond Myklebust68c97152012-01-03 13:22:46 -050066 dprintk("RPC: looking up machine cred for service %s\n",
67 service_name);
Trond Myklebust7c67db32008-04-07 20:50:11 -040068 return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
69}
70EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
71
Trond Myklebust8572b8e2010-07-31 14:29:08 -040072static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
73 struct rpc_cred *cred, int lookupflags)
Trond Myklebust5c691042008-03-12 16:21:07 -040074{
75 struct rpc_auth *auth = task->tk_client->cl_auth;
76 struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
Trond Myklebust5c691042008-03-12 16:21:07 -040077
Trond Myklebust8572b8e2010-07-31 14:29:08 -040078 return auth->au_ops->lookup_cred(auth, acred, lookupflags);
Trond Myklebust5c691042008-03-12 16:21:07 -040079}
80
Frank Sorenson18028c92016-09-29 10:44:38 -050081static int
82generic_hash_cred(struct auth_cred *acred, unsigned int hashbits)
83{
84 return hash_64(from_kgid(&init_user_ns, acred->gid) |
85 ((u64)from_kuid(&init_user_ns, acred->uid) <<
86 (sizeof(gid_t) * 8)), hashbits);
87}
88
Trond Myklebust9a559ef2008-03-12 12:24:49 -040089/*
90 * Lookup generic creds for current process
91 */
92static struct rpc_cred *
93generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
94{
Jeff Layton3c6e0bc2016-04-21 20:51:54 -040095 return rpcauth_lookup_credcache(&generic_auth, acred, flags, GFP_KERNEL);
Trond Myklebust9a559ef2008-03-12 12:24:49 -040096}
97
98static struct rpc_cred *
Jeff Layton3c6e0bc2016-04-21 20:51:54 -040099generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags, gfp_t gfp)
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400100{
101 struct generic_cred *gcred;
102
Jeff Layton3c6e0bc2016-04-21 20:51:54 -0400103 gcred = kmalloc(sizeof(*gcred), gfp);
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400104 if (gcred == NULL)
105 return ERR_PTR(-ENOMEM);
106
107 rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
108 gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
109
110 gcred->acred.uid = acred->uid;
111 gcred->acred.gid = acred->gid;
112 gcred->acred.group_info = acred->group_info;
Andy Adamson4de6caa2013-08-14 11:59:15 -0400113 gcred->acred.ac_flags = 0;
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400114 if (gcred->acred.group_info != NULL)
115 get_group_info(gcred->acred.group_info);
Trond Myklebust7c67db32008-04-07 20:50:11 -0400116 gcred->acred.machine_cred = acred->machine_cred;
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500117 gcred->acred.principal = acred->principal;
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400118
Trond Myklebust7c67db32008-04-07 20:50:11 -0400119 dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
120 gcred->acred.machine_cred ? "machine" : "generic",
Eric W. Biedermancdba3212013-02-01 17:10:52 -0800121 gcred,
122 from_kuid(&init_user_ns, acred->uid),
123 from_kgid(&init_user_ns, acred->gid));
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400124 return &gcred->gc_base;
125}
126
127static void
128generic_free_cred(struct rpc_cred *cred)
129{
130 struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
131
132 dprintk("RPC: generic_free_cred %p\n", gcred);
133 if (gcred->acred.group_info != NULL)
134 put_group_info(gcred->acred.group_info);
135 kfree(gcred);
136}
137
138static void
139generic_free_cred_callback(struct rcu_head *head)
140{
141 struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
142 generic_free_cred(cred);
143}
144
145static void
146generic_destroy_cred(struct rpc_cred *cred)
147{
148 call_rcu(&cred->cr_rcu, generic_free_cred_callback);
149}
150
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500151static int
152machine_cred_match(struct auth_cred *acred, struct generic_cred *gcred, int flags)
153{
154 if (!gcred->acred.machine_cred ||
155 gcred->acred.principal != acred->principal ||
Eric W. Biederman0b4d51b2013-02-01 16:39:32 -0800156 !uid_eq(gcred->acred.uid, acred->uid) ||
157 !gid_eq(gcred->acred.gid, acred->gid))
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500158 return 0;
159 return 1;
160}
161
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400162/*
163 * Match credentials against current process creds.
164 */
165static int
166generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
167{
168 struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
Trond Myklebust23918b02008-11-20 16:06:21 -0500169 int i;
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400170
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500171 if (acred->machine_cred)
172 return machine_cred_match(acred, gcred, flags);
173
Eric W. Biederman0b4d51b2013-02-01 16:39:32 -0800174 if (!uid_eq(gcred->acred.uid, acred->uid) ||
175 !gid_eq(gcred->acred.gid, acred->gid) ||
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500176 gcred->acred.machine_cred != 0)
Trond Myklebust23918b02008-11-20 16:06:21 -0500177 goto out_nomatch;
178
179 /* Optimisation in the case where pointers are identical... */
180 if (gcred->acred.group_info == acred->group_info)
181 goto out_match;
182
183 /* Slow path... */
184 if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
185 goto out_nomatch;
186 for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700187 if (!gid_eq(gcred->acred.group_info->gid[i],
188 acred->group_info->gid[i]))
Trond Myklebust23918b02008-11-20 16:06:21 -0500189 goto out_nomatch;
190 }
191out_match:
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400192 return 1;
Trond Myklebust23918b02008-11-20 16:06:21 -0500193out_nomatch:
194 return 0;
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400195}
196
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400197int __init rpc_init_generic_auth(void)
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400198{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400199 return rpcauth_init_credcache(&generic_auth);
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400200}
201
Stephen Rothwellc135e842010-09-29 14:16:57 +1000202void rpc_destroy_generic_auth(void)
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400203{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400204 rpcauth_destroy_credcache(&generic_auth);
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400205}
206
Andy Adamson4de6caa2013-08-14 11:59:15 -0400207/*
208 * Test the the current time (now) against the underlying credential key expiry
209 * minus a timeout and setup notification.
210 *
211 * The normal case:
212 * If 'now' is before the key expiry minus RPC_KEY_EXPIRE_TIMEO, set
213 * the RPC_CRED_NOTIFY_TIMEOUT flag to setup the underlying credential
214 * rpc_credops crmatch routine to notify this generic cred when it's key
215 * expiration is within RPC_KEY_EXPIRE_TIMEO, and return 0.
216 *
217 * The error case:
218 * If the underlying cred lookup fails, return -EACCES.
219 *
220 * The 'almost' error case:
221 * If 'now' is within key expiry minus RPC_KEY_EXPIRE_TIMEO, but not within
222 * key expiry minus RPC_KEY_EXPIRE_FAIL, set the RPC_CRED_EXPIRE_SOON bit
223 * on the acred ac_flags and return 0.
224 */
225static int
226generic_key_timeout(struct rpc_auth *auth, struct rpc_cred *cred)
227{
228 struct auth_cred *acred = &container_of(cred, struct generic_cred,
229 gc_base)->acred;
230 struct rpc_cred *tcred;
231 int ret = 0;
232
233
234 /* Fast track for non crkey_timeout (no key) underlying credentials */
Scott Mayhewce52914e2016-06-07 15:14:48 -0400235 if (auth->au_flags & RPCAUTH_AUTH_NO_CRKEY_TIMEOUT)
Andy Adamson4de6caa2013-08-14 11:59:15 -0400236 return 0;
237
238 /* Fast track for the normal case */
239 if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags))
240 return 0;
241
242 /* lookup_cred either returns a valid referenced rpc_cred, or PTR_ERR */
243 tcred = auth->au_ops->lookup_cred(auth, acred, 0);
244 if (IS_ERR(tcred))
245 return -EACCES;
246
Andy Adamson4de6caa2013-08-14 11:59:15 -0400247 /* Test for the almost error case */
248 ret = tcred->cr_ops->crkey_timeout(tcred);
249 if (ret != 0) {
250 set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
251 ret = 0;
252 } else {
253 /* In case underlying cred key has been reset */
254 if (test_and_clear_bit(RPC_CRED_KEY_EXPIRE_SOON,
255 &acred->ac_flags))
256 dprintk("RPC: UID %d Credential key reset\n",
Geert Uytterhoeven13429302013-09-12 15:09:39 +0200257 from_kuid(&init_user_ns, tcred->cr_uid));
Andy Adamson4de6caa2013-08-14 11:59:15 -0400258 /* set up fasttrack for the normal case */
259 set_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
260 }
261
Andy Adamson4de6caa2013-08-14 11:59:15 -0400262 put_rpccred(tcred);
263 return ret;
264}
265
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400266static const struct rpc_authops generic_auth_ops = {
267 .owner = THIS_MODULE,
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400268 .au_name = "Generic",
Frank Sorenson18028c92016-09-29 10:44:38 -0500269 .hash_cred = generic_hash_cred,
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400270 .lookup_cred = generic_lookup_cred,
271 .crcreate = generic_create_cred,
Andy Adamson4de6caa2013-08-14 11:59:15 -0400272 .key_timeout = generic_key_timeout,
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400273};
274
275static struct rpc_auth generic_auth = {
276 .au_ops = &generic_auth_ops,
277 .au_count = ATOMIC_INIT(0),
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400278};
279
Andy Adamson4de6caa2013-08-14 11:59:15 -0400280static bool generic_key_to_expire(struct rpc_cred *cred)
281{
282 struct auth_cred *acred = &container_of(cred, struct generic_cred,
283 gc_base)->acred;
Trond Myklebust35a06202018-11-12 16:06:51 -0500284 return test_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
Andy Adamson4de6caa2013-08-14 11:59:15 -0400285}
286
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400287static const struct rpc_credops generic_credops = {
288 .cr_name = "Generic cred",
289 .crdestroy = generic_destroy_cred,
Trond Myklebust5c691042008-03-12 16:21:07 -0400290 .crbind = generic_bind_cred,
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400291 .crmatch = generic_match,
Andy Adamson4de6caa2013-08-14 11:59:15 -0400292 .crkey_to_expire = generic_key_to_expire,
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400293};