blob: 387f6b3ffbeafa79db312e0745af5f9ebd9c21ca [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/auth_unix.c
4 *
5 * UNIX-style authentication; no AUTH_SHORT support
6 *
7 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8 */
9
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
12#include <linux/sched.h>
13#include <linux/module.h>
NeilBrown2edd8d72018-12-03 11:30:31 +110014#include <linux/mempool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
16#include <linux/sunrpc/auth.h>
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080017#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jeff Laytonf895b252014-11-17 16:58:04 -050020#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021# define RPCDBG_FACILITY RPCDBG_AUTH
22#endif
23
24static struct rpc_auth unix_auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -040025static const struct rpc_credops unix_credops;
NeilBrown2edd8d72018-12-03 11:30:31 +110026static mempool_t *unix_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static struct rpc_auth *
Sargun Dhillon82b98ca2018-07-05 16:48:50 +000029unx_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Chuck Lever46121cf2007-01-31 12:14:08 -050031 dprintk("RPC: creating UNIX authenticator for client %p\n",
32 clnt);
Trond Myklebust331bc712018-10-14 10:40:29 -040033 refcount_inc(&unix_auth.au_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return &unix_auth;
35}
36
37static void
38unx_destroy(struct rpc_auth *auth)
39{
Chuck Lever46121cf2007-01-31 12:14:08 -050040 dprintk("RPC: destroying UNIX authenticator %p\n", auth);
Frank Sorenson1e035d02016-09-29 10:44:39 -050041}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Lookup AUTH_UNIX creds for current process
45 */
46static struct rpc_cred *
47unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
48{
NeilBrown2edd8d72018-12-03 11:30:31 +110049 struct rpc_cred *ret = mempool_alloc(unix_pool, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Chuck Lever46121cf2007-01-31 12:14:08 -050051 dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
NeilBrown8276c902018-12-03 11:30:30 +110052 from_kuid(&init_user_ns, acred->cred->fsuid),
53 from_kgid(&init_user_ns, acred->cred->fsgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
NeilBrown2edd8d72018-12-03 11:30:31 +110055 rpcauth_init_cred(ret, acred, auth, &unix_credops);
56 ret->cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
57 return ret;
Trond Myklebust31be5bf2007-06-24 15:55:26 -040058}
Trond Myklebust696e38d2007-06-25 09:48:25 -040059
Trond Myklebust31be5bf2007-06-24 15:55:26 -040060static void
61unx_free_cred_callback(struct rcu_head *head)
62{
NeilBrown2edd8d72018-12-03 11:30:31 +110063 struct rpc_cred *rpc_cred = container_of(head, struct rpc_cred, cr_rcu);
64 dprintk("RPC: unx_free_cred %p\n", rpc_cred);
65 put_cred(rpc_cred->cr_cred);
66 mempool_free(rpc_cred, unix_pool);
Trond Myklebust31be5bf2007-06-24 15:55:26 -040067}
68
69static void
70unx_destroy_cred(struct rpc_cred *cred)
71{
72 call_rcu(&cred->cr_rcu, unx_free_cred_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75/*
NeilBrown2edd8d72018-12-03 11:30:31 +110076 * Match credentials against current the auth_cred.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
78static int
NeilBrown2edd8d72018-12-03 11:30:31 +110079unx_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Trond Myklebustaf093832008-03-12 12:12:16 -040081 unsigned int groups = 0;
82 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
NeilBrown2edd8d72018-12-03 11:30:31 +110084 if (cred->cr_cred == acred->cred)
85 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
NeilBrown2edd8d72018-12-03 11:30:31 +110087 if (!uid_eq(cred->cr_cred->fsuid, acred->cred->fsuid) || !gid_eq(cred->cr_cred->fsgid, acred->cred->fsgid))
Trond Myklebustaf093832008-03-12 12:12:16 -040088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
NeilBrownfc0664f2018-12-03 11:30:30 +110090 if (acred->cred && acred->cred->group_info != NULL)
91 groups = acred->cred->group_info->ngroups;
Kinglong Mee57864612017-02-07 21:48:11 +080092 if (groups > UNX_NGROUPS)
93 groups = UNX_NGROUPS;
NeilBrown2edd8d72018-12-03 11:30:31 +110094 if (cred->cr_cred->group_info == NULL)
95 return groups == 0;
96 if (groups != cred->cr_cred->group_info->ngroups)
NeilBrowndc6f55e2011-10-25 10:25:49 +110097 return 0;
NeilBrown2edd8d72018-12-03 11:30:31 +110098
99 for (i = 0; i < groups ; i++)
100 if (!gid_eq(cred->cr_cred->group_info->gid[i], acred->cred->group_info->gid[i]))
101 return 0;
Trond Myklebustaf093832008-03-12 12:12:16 -0400102 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * Marshal credentials.
107 * Maybe we should keep a cached credential for performance reasons.
108 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700109static __be32 *
110unx_marshal(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct rpc_clnt *clnt = task->tk_client;
NeilBrown2edd8d72018-12-03 11:30:31 +1100113 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700114 __be32 *base, *hold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int i;
NeilBrown2edd8d72018-12-03 11:30:31 +1100116 struct group_info *gi = cred->cr_cred->group_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 *p++ = htonl(RPC_AUTH_UNIX);
119 base = p++;
120 *p++ = htonl(jiffies/HZ);
121
122 /*
123 * Copy the UTS nodename captured when the client was created.
124 */
125 p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen);
126
NeilBrown2edd8d72018-12-03 11:30:31 +1100127 *p++ = htonl((u32) from_kuid(&init_user_ns, cred->cr_cred->fsuid));
128 *p++ = htonl((u32) from_kgid(&init_user_ns, cred->cr_cred->fsgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 hold = p++;
NeilBrown2edd8d72018-12-03 11:30:31 +1100130 if (gi)
131 for (i = 0; i < UNX_NGROUPS && i < gi->ngroups; i++)
132 *p++ = htonl((u32) from_kgid(&init_user_ns, gi->gid[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *hold = htonl(p - hold - 1); /* gid array length */
134 *base = htonl((p - base - 1) << 2); /* cred length */
135
136 *p++ = htonl(RPC_AUTH_NULL);
137 *p++ = htonl(0);
138
139 return p;
140}
141
142/*
143 * Refresh credentials. This is a no-op for AUTH_UNIX
144 */
145static int
146unx_refresh(struct rpc_task *task)
147{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400148 set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_rqstp->rq_cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
150}
151
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700152static __be32 *
153unx_validate(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 rpc_authflavor_t flavor;
156 u32 size;
157
158 flavor = ntohl(*p++);
159 if (flavor != RPC_AUTH_NULL &&
160 flavor != RPC_AUTH_UNIX &&
161 flavor != RPC_AUTH_SHORT) {
162 printk("RPC: bad verf flavor: %u\n", flavor);
Andy Adamson35fa5f72013-08-14 11:59:17 -0400163 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
166 size = ntohl(*p++);
167 if (size > RPC_MAX_AUTH_SIZE) {
168 printk("RPC: giant verf size: %u\n", size);
Andy Adamson35fa5f72013-08-14 11:59:17 -0400169 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
Trond Myklebusta17c2152010-07-31 14:29:08 -0400171 task->tk_rqstp->rq_cred->cr_auth->au_rslack = (size >> 2) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 p += (size >> 2);
173
174 return p;
175}
176
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400177int __init rpc_init_authunix(void)
Trond Myklebust9499b432007-06-24 15:57:57 -0400178{
NeilBrown2edd8d72018-12-03 11:30:31 +1100179 unix_pool = mempool_create_kmalloc_pool(16, sizeof(struct rpc_cred));
180 return unix_pool ? 0 : -ENOMEM;
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400181}
182
183void rpc_destroy_authunix(void)
184{
NeilBrown2edd8d72018-12-03 11:30:31 +1100185 mempool_destroy(unix_pool);
Trond Myklebust9499b432007-06-24 15:57:57 -0400186}
187
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400188const struct rpc_authops authunix_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .owner = THIS_MODULE,
190 .au_flavor = RPC_AUTH_UNIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .au_name = "UNIX",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .create = unx_create,
193 .destroy = unx_destroy,
194 .lookup_cred = unx_lookup_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
197static
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198struct rpc_auth unix_auth = {
Chuck Lever45006322016-03-01 13:06:02 -0500199 .au_cslack = UNX_CALLSLACK,
200 .au_rslack = NUL_REPLYSLACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .au_ops = &authunix_ops,
Trond Myklebust81039f12006-06-09 09:34:34 -0400202 .au_flavor = RPC_AUTH_UNIX,
Trond Myklebust331bc712018-10-14 10:40:29 -0400203 .au_count = REFCOUNT_INIT(1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206static
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400207const struct rpc_credops unix_credops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .cr_name = "AUTH_UNIX",
209 .crdestroy = unx_destroy_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .crmatch = unx_match,
211 .crmarshal = unx_marshal,
212 .crrefresh = unx_refresh,
213 .crvalidate = unx_validate,
214};