blob: fb9041b92f72233841cf1d173aa1b1cafe91e623 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/sched.h>
3#include <linux/module.h>
4#include <linux/sunrpc/types.h>
5#include <linux/sunrpc/xdr.h>
6#include <linux/sunrpc/svcsock.h>
7#include <linux/sunrpc/svcauth.h>
Andy Adamsonc4170583f2007-07-17 04:04:42 -07008#include <linux/sunrpc/gss_api.h>
Jeff Layton59766872013-02-04 12:50:00 -05009#include <linux/sunrpc/addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/err.h>
11#include <linux/seq_file.h>
12#include <linux/hash.h>
Paulo Marques543537b2005-06-23 00:09:02 -070013#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Greg Banks7b2b1fe2006-10-04 02:15:50 -070015#include <net/sock.h>
Aurélien Charbonf15364b2008-01-18 15:50:56 +010016#include <net/ipv6.h>
17#include <linux/kernel.h>
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080018#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define RPCDBG_FACILITY RPCDBG_AUTH
20
21
Pavel Emelyanov90d51b02010-09-27 14:02:29 +040022#include "netns.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * AUTHUNIX and AUTHNULL credentials are both handled here.
26 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
27 * are always nobody (-2). i.e. we do the same IP address checks for
28 * AUTHNULL as for AUTHUNIX, and that is done here.
29 */
30
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct unix_domain {
33 struct auth_domain h;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 /* other stuff later */
35};
36
H Hartley Sweeten13e0e952011-06-20 18:19:12 -070037extern struct auth_ops svcauth_null;
NeilBrownefc36aa2006-03-27 01:14:59 -080038extern struct auth_ops svcauth_unix;
39
Trond Myklebust608a0ab2018-10-01 10:41:44 -040040static void svcauth_unix_domain_release_rcu(struct rcu_head *head)
J. Bruce Fields8b3e07a2011-03-07 22:50:47 -050041{
Trond Myklebust608a0ab2018-10-01 10:41:44 -040042 struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
J. Bruce Fields8b3e07a2011-03-07 22:50:47 -050043 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
44
45 kfree(dom->name);
46 kfree(ud);
47}
48
Trond Myklebust608a0ab2018-10-01 10:41:44 -040049static void svcauth_unix_domain_release(struct auth_domain *dom)
50{
51 call_rcu(&dom->rcu_head, svcauth_unix_domain_release_rcu);
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054struct auth_domain *unix_domain_find(char *name)
55{
NeilBrownefc36aa2006-03-27 01:14:59 -080056 struct auth_domain *rv;
57 struct unix_domain *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust608a0ab2018-10-01 10:41:44 -040059 rv = auth_domain_find(name);
NeilBrownefc36aa2006-03-27 01:14:59 -080060 while(1) {
NeilBrownad1b5222006-03-27 01:15:11 -080061 if (rv) {
62 if (new && rv != &new->h)
J. Bruce Fields352b5d12011-03-09 22:40:30 -050063 svcauth_unix_domain_release(&new->h);
NeilBrownad1b5222006-03-27 01:15:11 -080064
65 if (rv->flavour != &svcauth_unix) {
66 auth_domain_put(rv);
67 return NULL;
68 }
NeilBrownefc36aa2006-03-27 01:14:59 -080069 return rv;
70 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
NeilBrownefc36aa2006-03-27 01:14:59 -080072 new = kmalloc(sizeof(*new), GFP_KERNEL);
73 if (new == NULL)
74 return NULL;
75 kref_init(&new->h.ref);
76 new->h.name = kstrdup(name, GFP_KERNEL);
NeilBrowndd08d6e2006-12-13 00:35:44 -080077 if (new->h.name == NULL) {
78 kfree(new);
79 return NULL;
80 }
NeilBrownefc36aa2006-03-27 01:14:59 -080081 new->h.flavour = &svcauth_unix;
NeilBrownefc36aa2006-03-27 01:14:59 -080082 rv = auth_domain_lookup(name, &new->h);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Trond Myklebust24c37672008-12-23 16:30:12 -050085EXPORT_SYMBOL_GPL(unix_domain_find);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/**************************************************
89 * cache for IP address to unix_domain
90 * as needed by AUTH_UNIX
91 */
92#define IP_HASHBITS 8
93#define IP_HASHMAX (1<<IP_HASHBITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95struct ip_map {
96 struct cache_head h;
97 char m_class[8]; /* e.g. "nfsd" */
Aurélien Charbonf15364b2008-01-18 15:50:56 +010098 struct in6_addr m_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct unix_domain *m_client;
Trond Myklebustfd5d2f72018-10-01 10:41:46 -0400100 struct rcu_head m_rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
NeilBrownbaab9352006-03-27 01:15:09 -0800103static void ip_map_put(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
NeilBrownbaab9352006-03-27 01:15:09 -0800105 struct cache_head *item = container_of(kref, struct cache_head, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct ip_map *im = container_of(item, struct ip_map,h);
NeilBrownbaab9352006-03-27 01:15:09 -0800107
108 if (test_bit(CACHE_VALID, &item->flags) &&
109 !test_bit(CACHE_NEGATIVE, &item->flags))
110 auth_domain_put(&im->m_client->h);
Trond Myklebustfd5d2f72018-10-01 10:41:46 -0400111 kfree_rcu(im, m_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Eric Dumazetddbe5032012-07-18 08:11:12 +0000114static inline int hash_ip6(const struct in6_addr *ip)
NeilBrown1f1e0302006-01-06 00:09:49 -0800115{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000116 return hash_32(ipv6_addr_hash(ip), IP_HASHBITS);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100117}
NeilBrown1a9917c2006-03-27 01:15:02 -0800118static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
119{
120 struct ip_map *orig = container_of(corig, struct ip_map, h);
121 struct ip_map *new = container_of(cnew, struct ip_map, h);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800122 return strcmp(orig->m_class, new->m_class) == 0 &&
123 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800124}
125static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
126{
127 struct ip_map *new = container_of(cnew, struct ip_map, h);
128 struct ip_map *item = container_of(citem, struct ip_map, h);
NeilBrown1f1e0302006-01-06 00:09:49 -0800129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 strcpy(new->m_class, item->m_class);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000131 new->m_addr = item->m_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
NeilBrown1a9917c2006-03-27 01:15:02 -0800133static void update(struct cache_head *cnew, struct cache_head *citem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
NeilBrown1a9917c2006-03-27 01:15:02 -0800135 struct ip_map *new = container_of(cnew, struct ip_map, h);
136 struct ip_map *item = container_of(citem, struct ip_map, h);
137
NeilBrownefc36aa2006-03-27 01:14:59 -0800138 kref_get(&item->m_client->h.ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 new->m_client = item->m_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
NeilBrown1a9917c2006-03-27 01:15:02 -0800141static struct cache_head *ip_map_alloc(void)
142{
143 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
144 if (i)
145 return &i->h;
146 else
147 return NULL;
148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150static void ip_map_request(struct cache_detail *cd,
151 struct cache_head *h,
152 char **bpp, int *blen)
153{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100154 char text_addr[40];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct ip_map *im = container_of(h, struct ip_map, h);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800156
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100157 if (ipv6_addr_v4mapped(&(im->m_addr))) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700158 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100159 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700160 snprintf(text_addr, 40, "%pI6", &im->m_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 qword_add(bpp, blen, im->m_class);
163 qword_add(bpp, blen, text_addr);
164 (*bpp)[-1] = '\n';
165}
166
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400167static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
168static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170static int ip_map_parse(struct cache_detail *cd,
171 char *mesg, int mlen)
172{
173 /* class ipaddress [domainname] */
174 /* should be safe just to use the start of the input buffer
175 * for scratch: */
176 char *buf = mesg;
177 int len;
NeilBrown1a9917c2006-03-27 01:15:02 -0800178 char class[8];
Chuck Lever07396052010-01-26 14:03:47 -0500179 union {
180 struct sockaddr sa;
181 struct sockaddr_in s4;
182 struct sockaddr_in6 s6;
183 } address;
184 struct sockaddr_in6 sin6;
NeilBrown1a9917c2006-03-27 01:15:02 -0800185 int err;
186
187 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct auth_domain *dom;
189 time_t expiry;
190
191 if (mesg[mlen-1] != '\n')
192 return -EINVAL;
193 mesg[mlen-1] = 0;
194
195 /* class */
NeilBrown1a9917c2006-03-27 01:15:02 -0800196 len = qword_get(&mesg, class, sizeof(class));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (len <= 0) return -EINVAL;
198
199 /* ip address */
200 len = qword_get(&mesg, buf, mlen);
201 if (len <= 0) return -EINVAL;
202
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400203 if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
Chuck Lever07396052010-01-26 14:03:47 -0500205 switch (address.sa.sa_family) {
206 case AF_INET:
207 /* Form a mapped IPv4 address in sin6 */
Chuck Lever07396052010-01-26 14:03:47 -0500208 sin6.sin6_family = AF_INET6;
Pavel Emelyanov70dc78d2010-10-05 20:48:02 +0400209 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
210 &sin6.sin6_addr);
Chuck Lever07396052010-01-26 14:03:47 -0500211 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000212#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever07396052010-01-26 14:03:47 -0500213 case AF_INET6:
214 memcpy(&sin6, &address.s6, sizeof(sin6));
215 break;
216#endif
217 default:
218 return -EINVAL;
219 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 expiry = get_expiry(&mesg);
222 if (expiry ==0)
223 return -EINVAL;
224
225 /* domainname, or empty for NEGATIVE */
226 len = qword_get(&mesg, buf, mlen);
227 if (len < 0) return -EINVAL;
228
229 if (len) {
230 dom = unix_domain_find(buf);
231 if (dom == NULL)
232 return -ENOENT;
233 } else
234 dom = NULL;
235
Chuck Lever07396052010-01-26 14:03:47 -0500236 /* IPv6 scope IDs are ignored for now */
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400237 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800238 if (ipmp) {
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400239 err = __ip_map_update(cd, ipmp,
NeilBrown1a9917c2006-03-27 01:15:02 -0800240 container_of(dom, struct unix_domain, h),
241 expiry);
242 } else
243 err = -ENOMEM;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (dom)
246 auth_domain_put(dom);
NeilBrown1a9917c2006-03-27 01:15:02 -0800247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 cache_flush();
NeilBrown1a9917c2006-03-27 01:15:02 -0800249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static int ip_map_show(struct seq_file *m,
253 struct cache_detail *cd,
254 struct cache_head *h)
255{
256 struct ip_map *im;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100257 struct in6_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 char *dom = "-no-domain-";
259
260 if (h == NULL) {
261 seq_puts(m, "#class IP domain\n");
262 return 0;
263 }
264 im = container_of(h, struct ip_map, h);
265 /* class addr domain */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000266 addr = im->m_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800268 if (test_bit(CACHE_VALID, &h->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 !test_bit(CACHE_NEGATIVE, &h->flags))
270 dom = im->m_client->h.name;
271
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100272 if (ipv6_addr_v4mapped(&addr)) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700273 seq_printf(m, "%s %pI4 %s\n",
274 im->m_class, &addr.s6_addr32[3], dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100275 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700276 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400282static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
283 struct in6_addr *addr)
NeilBrown1a9917c2006-03-27 01:15:02 -0800284{
285 struct ip_map ip;
286 struct cache_head *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
NeilBrown1a9917c2006-03-27 01:15:02 -0800288 strcpy(ip.m_class, class);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000289 ip.m_addr = *addr;
Trond Myklebustfd5d2f72018-10-01 10:41:46 -0400290 ch = sunrpc_cache_lookup_rcu(cd, &ip.h,
291 hash_str(class, IP_HASHBITS) ^
292 hash_ip6(addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800293
294 if (ch)
295 return container_of(ch, struct ip_map, h);
296 else
297 return NULL;
298}
299
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400300static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
301 struct in6_addr *addr)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400302{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400303 struct sunrpc_net *sn;
304
305 sn = net_generic(net, sunrpc_net_id);
306 return __ip_map_lookup(sn->ip_map_cache, class, addr);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400307}
308
309static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
310 struct unix_domain *udom, time_t expiry)
NeilBrown1a9917c2006-03-27 01:15:02 -0800311{
312 struct ip_map ip;
313 struct cache_head *ch;
314
315 ip.m_client = udom;
316 ip.h.flags = 0;
317 if (!udom)
318 set_bit(CACHE_NEGATIVE, &ip.h.flags);
NeilBrown1a9917c2006-03-27 01:15:02 -0800319 ip.h.expiry_time = expiry;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400320 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
NeilBrown1a9917c2006-03-27 01:15:02 -0800321 hash_str(ipm->m_class, IP_HASHBITS) ^
Eric Dumazetddbe5032012-07-18 08:11:12 +0000322 hash_ip6(&ipm->m_addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800323 if (!ch)
324 return -ENOMEM;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400325 cache_put(ch, cd);
NeilBrown1a9917c2006-03-27 01:15:02 -0800326 return 0;
327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400329static inline int ip_map_update(struct net *net, struct ip_map *ipm,
330 struct unix_domain *udom, time_t expiry)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400331{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400332 struct sunrpc_net *sn;
333
334 sn = net_generic(net, sunrpc_net_id);
335 return __ip_map_update(sn->ip_map_cache, ipm, udom, expiry);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400336}
337
Stanislav Kinsburskye5f06f72012-04-11 15:13:28 +0400338void svcauth_unix_purge(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Stanislav Kinsburskye5f06f72012-04-11 15:13:28 +0400340 struct sunrpc_net *sn;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400341
Stanislav Kinsburskye5f06f72012-04-11 15:13:28 +0400342 sn = net_generic(net, sunrpc_net_id);
343 cache_purge(sn->ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Trond Myklebust24c37672008-12-23 16:30:12 -0500345EXPORT_SYMBOL_GPL(svcauth_unix_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700347static inline struct ip_map *
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400348ip_map_cached_get(struct svc_xprt *xprt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700349{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600350 struct ip_map *ipm = NULL;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400351 struct sunrpc_net *sn;
Tom Tuckerdef13d72007-12-30 21:08:08 -0600352
353 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
354 spin_lock(&xprt->xpt_lock);
355 ipm = xprt->xpt_auth_cache;
356 if (ipm != NULL) {
NeilBrown7715cde2013-06-13 12:53:42 +1000357 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
358 if (cache_is_expired(sn->ip_map_cache, &ipm->h)) {
Tom Tuckerdef13d72007-12-30 21:08:08 -0600359 /*
360 * The entry has been invalidated since it was
361 * remembered, e.g. by a second mount from the
362 * same IP address.
363 */
364 xprt->xpt_auth_cache = NULL;
365 spin_unlock(&xprt->xpt_lock);
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400366 cache_put(&ipm->h, sn->ip_map_cache);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600367 return NULL;
368 }
369 cache_get(&ipm->h);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700370 }
Tom Tuckerdef13d72007-12-30 21:08:08 -0600371 spin_unlock(&xprt->xpt_lock);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700372 }
373 return ipm;
374}
375
376static inline void
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400377ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700378{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600379 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
380 spin_lock(&xprt->xpt_lock);
381 if (xprt->xpt_auth_cache == NULL) {
382 /* newly cached, keep the reference */
383 xprt->xpt_auth_cache = ipm;
384 ipm = NULL;
385 }
386 spin_unlock(&xprt->xpt_lock);
NeilBrown30f3dee2007-04-16 22:53:25 -0700387 }
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400388 if (ipm) {
389 struct sunrpc_net *sn;
390
391 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
392 cache_put(&ipm->h, sn->ip_map_cache);
393 }
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700394}
395
396void
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400397svcauth_unix_info_release(struct svc_xprt *xpt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700398{
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400399 struct ip_map *ipm;
400
401 ipm = xpt->xpt_auth_cache;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400402 if (ipm != NULL) {
403 struct sunrpc_net *sn;
404
405 sn = net_generic(xpt->xpt_net, sunrpc_net_id);
406 cache_put(&ipm->h, sn->ip_map_cache);
407 }
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700408}
409
NeilBrown3fc605a2007-02-14 00:33:13 -0800410/****************************************************************************
411 * auth.unix.gid cache
412 * simple cache to map a UID to a list of GIDs
Kinglong Mee57864612017-02-07 21:48:11 +0800413 * because AUTH_UNIX aka AUTH_SYS has a max of UNX_NGROUPS
NeilBrown3fc605a2007-02-14 00:33:13 -0800414 */
415#define GID_HASHBITS 8
416#define GID_HASHMAX (1<<GID_HASHBITS)
NeilBrown3fc605a2007-02-14 00:33:13 -0800417
418struct unix_gid {
419 struct cache_head h;
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800420 kuid_t uid;
NeilBrown3fc605a2007-02-14 00:33:13 -0800421 struct group_info *gi;
Trond Myklebustfd5d2f72018-10-01 10:41:46 -0400422 struct rcu_head rcu;
NeilBrown3fc605a2007-02-14 00:33:13 -0800423};
NeilBrown3fc605a2007-02-14 00:33:13 -0800424
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800425static int unix_gid_hash(kuid_t uid)
426{
427 return hash_long(from_kuid(&init_user_ns, uid), GID_HASHBITS);
428}
429
NeilBrown3fc605a2007-02-14 00:33:13 -0800430static void unix_gid_put(struct kref *kref)
431{
432 struct cache_head *item = container_of(kref, struct cache_head, ref);
433 struct unix_gid *ug = container_of(item, struct unix_gid, h);
434 if (test_bit(CACHE_VALID, &item->flags) &&
435 !test_bit(CACHE_NEGATIVE, &item->flags))
436 put_group_info(ug->gi);
Trond Myklebustfd5d2f72018-10-01 10:41:46 -0400437 kfree_rcu(ug, rcu);
NeilBrown3fc605a2007-02-14 00:33:13 -0800438}
439
440static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
441{
442 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
443 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
Eric W. Biederman0b4d51b2013-02-01 16:39:32 -0800444 return uid_eq(orig->uid, new->uid);
NeilBrown3fc605a2007-02-14 00:33:13 -0800445}
446static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
447{
448 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
449 struct unix_gid *item = container_of(citem, struct unix_gid, h);
450 new->uid = item->uid;
451}
452static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
453{
454 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
455 struct unix_gid *item = container_of(citem, struct unix_gid, h);
456
457 get_group_info(item->gi);
458 new->gi = item->gi;
459}
460static struct cache_head *unix_gid_alloc(void)
461{
462 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
463 if (g)
464 return &g->h;
465 else
466 return NULL;
467}
468
469static void unix_gid_request(struct cache_detail *cd,
470 struct cache_head *h,
471 char **bpp, int *blen)
472{
473 char tuid[20];
474 struct unix_gid *ug = container_of(h, struct unix_gid, h);
475
Eric W. Biederman25da9262013-02-02 02:49:44 -0800476 snprintf(tuid, 20, "%u", from_kuid(&init_user_ns, ug->uid));
NeilBrown3fc605a2007-02-14 00:33:13 -0800477 qword_add(bpp, blen, tuid);
478 (*bpp)[-1] = '\n';
479}
480
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800481static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid);
NeilBrown3fc605a2007-02-14 00:33:13 -0800482
483static int unix_gid_parse(struct cache_detail *cd,
484 char *mesg, int mlen)
485{
486 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
Eric W. Biederman25da9262013-02-02 02:49:44 -0800487 int id;
488 kuid_t uid;
NeilBrown3fc605a2007-02-14 00:33:13 -0800489 int gids;
490 int rv;
491 int i;
492 int err;
493 time_t expiry;
494 struct unix_gid ug, *ugp;
495
Dan Carpenter34769642012-01-20 10:48:18 +0300496 if (mesg[mlen - 1] != '\n')
NeilBrown3fc605a2007-02-14 00:33:13 -0800497 return -EINVAL;
498 mesg[mlen-1] = 0;
499
Eric W. Biederman25da9262013-02-02 02:49:44 -0800500 rv = get_int(&mesg, &id);
NeilBrown3fc605a2007-02-14 00:33:13 -0800501 if (rv)
502 return -EINVAL;
Eric W. Biederman25da9262013-02-02 02:49:44 -0800503 uid = make_kuid(&init_user_ns, id);
NeilBrown3fc605a2007-02-14 00:33:13 -0800504 ug.uid = uid;
505
506 expiry = get_expiry(&mesg);
507 if (expiry == 0)
508 return -EINVAL;
509
510 rv = get_int(&mesg, &gids);
511 if (rv || gids < 0 || gids > 8192)
512 return -EINVAL;
513
514 ug.gi = groups_alloc(gids);
515 if (!ug.gi)
516 return -ENOMEM;
517
518 for (i = 0 ; i < gids ; i++) {
519 int gid;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800520 kgid_t kgid;
NeilBrown3fc605a2007-02-14 00:33:13 -0800521 rv = get_int(&mesg, &gid);
522 err = -EINVAL;
523 if (rv)
524 goto out;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800525 kgid = make_kgid(&init_user_ns, gid);
526 if (!gid_valid(kgid))
527 goto out;
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700528 ug.gi->gid[i] = kgid;
NeilBrown3fc605a2007-02-14 00:33:13 -0800529 }
530
Thiago Rafael Beckerbdcf0a42017-12-14 15:33:12 -0800531 groups_sort(ug.gi);
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400532 ugp = unix_gid_lookup(cd, uid);
NeilBrown3fc605a2007-02-14 00:33:13 -0800533 if (ugp) {
534 struct cache_head *ch;
535 ug.h.flags = 0;
536 ug.h.expiry_time = expiry;
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400537 ch = sunrpc_cache_update(cd,
NeilBrown3fc605a2007-02-14 00:33:13 -0800538 &ug.h, &ugp->h,
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800539 unix_gid_hash(uid));
NeilBrown3fc605a2007-02-14 00:33:13 -0800540 if (!ch)
541 err = -ENOMEM;
542 else {
543 err = 0;
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400544 cache_put(ch, cd);
NeilBrown3fc605a2007-02-14 00:33:13 -0800545 }
546 } else
547 err = -ENOMEM;
548 out:
549 if (ug.gi)
550 put_group_info(ug.gi);
551 return err;
552}
553
554static int unix_gid_show(struct seq_file *m,
555 struct cache_detail *cd,
556 struct cache_head *h)
557{
Eric W. Biederman25da9262013-02-02 02:49:44 -0800558 struct user_namespace *user_ns = &init_user_ns;
NeilBrown3fc605a2007-02-14 00:33:13 -0800559 struct unix_gid *ug;
560 int i;
561 int glen;
562
563 if (h == NULL) {
564 seq_puts(m, "#uid cnt: gids...\n");
565 return 0;
566 }
567 ug = container_of(h, struct unix_gid, h);
568 if (test_bit(CACHE_VALID, &h->flags) &&
569 !test_bit(CACHE_NEGATIVE, &h->flags))
570 glen = ug->gi->ngroups;
571 else
572 glen = 0;
573
Eric W. Biederman25da9262013-02-02 02:49:44 -0800574 seq_printf(m, "%u %d:", from_kuid_munged(user_ns, ug->uid), glen);
NeilBrown3fc605a2007-02-14 00:33:13 -0800575 for (i = 0; i < glen; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700576 seq_printf(m, " %d", from_kgid_munged(user_ns, ug->gi->gid[i]));
NeilBrown3fc605a2007-02-14 00:33:13 -0800577 seq_printf(m, "\n");
578 return 0;
579}
580
Bhumika Goyalee24eac2017-10-17 18:14:26 +0200581static const struct cache_detail unix_gid_cache_template = {
NeilBrown3fc605a2007-02-14 00:33:13 -0800582 .owner = THIS_MODULE,
583 .hash_size = GID_HASHMAX,
NeilBrown3fc605a2007-02-14 00:33:13 -0800584 .name = "auth.unix.gid",
585 .cache_put = unix_gid_put,
Stanislav Kinsbursky73fb8472013-02-04 14:02:45 +0300586 .cache_request = unix_gid_request,
NeilBrown3fc605a2007-02-14 00:33:13 -0800587 .cache_parse = unix_gid_parse,
588 .cache_show = unix_gid_show,
589 .match = unix_gid_match,
590 .init = unix_gid_init,
591 .update = unix_gid_update,
592 .alloc = unix_gid_alloc,
593};
594
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400595int unix_gid_cache_create(struct net *net)
596{
597 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
598 struct cache_detail *cd;
599 int err;
600
601 cd = cache_create_net(&unix_gid_cache_template, net);
602 if (IS_ERR(cd))
603 return PTR_ERR(cd);
604 err = cache_register_net(cd, net);
605 if (err) {
606 cache_destroy_net(cd, net);
607 return err;
608 }
609 sn->unix_gid_cache = cd;
610 return 0;
611}
612
613void unix_gid_cache_destroy(struct net *net)
614{
615 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
616 struct cache_detail *cd = sn->unix_gid_cache;
617
618 sn->unix_gid_cache = NULL;
619 cache_purge(cd);
620 cache_unregister_net(cd, net);
621 cache_destroy_net(cd, net);
622}
623
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800624static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid)
NeilBrown3fc605a2007-02-14 00:33:13 -0800625{
626 struct unix_gid ug;
627 struct cache_head *ch;
628
629 ug.uid = uid;
Trond Myklebustfd5d2f72018-10-01 10:41:46 -0400630 ch = sunrpc_cache_lookup_rcu(cd, &ug.h, unix_gid_hash(uid));
NeilBrown3fc605a2007-02-14 00:33:13 -0800631 if (ch)
632 return container_of(ch, struct unix_gid, h);
633 else
634 return NULL;
635}
636
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800637static struct group_info *unix_gid_find(kuid_t uid, struct svc_rqst *rqstp)
NeilBrown3fc605a2007-02-14 00:33:13 -0800638{
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400639 struct unix_gid *ug;
640 struct group_info *gi;
641 int ret;
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400642 struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
643 sunrpc_net_id);
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400644
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400645 ug = unix_gid_lookup(sn->unix_gid_cache, uid);
NeilBrown3fc605a2007-02-14 00:33:13 -0800646 if (!ug)
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400647 return ERR_PTR(-EAGAIN);
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400648 ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400649 switch (ret) {
NeilBrown3fc605a2007-02-14 00:33:13 -0800650 case -ENOENT:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400651 return ERR_PTR(-ENOENT);
NeilBrown1ebede82010-08-12 17:04:07 +1000652 case -ETIMEDOUT:
653 return ERR_PTR(-ESHUTDOWN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800654 case 0:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400655 gi = get_group_info(ug->gi);
Stanislav Kinsbursky73393232012-01-19 21:42:29 +0400656 cache_put(&ug->h, sn->unix_gid_cache);
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400657 return gi;
NeilBrown3fc605a2007-02-14 00:33:13 -0800658 default:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400659 return ERR_PTR(-EAGAIN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800660 }
661}
662
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700663int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664svcauth_unix_set_client(struct svc_rqst *rqstp)
665{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100666 struct sockaddr_in *sin;
667 struct sockaddr_in6 *sin6, sin6_storage;
NeilBrown1a9917c2006-03-27 01:15:02 -0800668 struct ip_map *ipm;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400669 struct group_info *gi;
670 struct svc_cred *cred = &rqstp->rq_cred;
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400671 struct svc_xprt *xprt = rqstp->rq_xprt;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400672 struct net *net = xprt->xpt_net;
673 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100675 switch (rqstp->rq_addr.ss_family) {
676 case AF_INET:
677 sin = svc_addr_in(rqstp);
678 sin6 = &sin6_storage;
Brian Haleyb301e822009-10-07 13:58:25 -0700679 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100680 break;
681 case AF_INET6:
682 sin6 = svc_addr_in6(rqstp);
683 break;
684 default:
685 BUG();
686 }
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 rqstp->rq_client = NULL;
689 if (rqstp->rq_proc == 0)
690 return SVC_OK;
691
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400692 ipm = ip_map_cached_get(xprt);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700693 if (ipm == NULL)
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400694 ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100695 &sin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (ipm == NULL)
698 return SVC_DENIED;
699
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400700 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 default:
702 BUG();
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800703 case -ETIMEDOUT:
NeilBrown1ebede82010-08-12 17:04:07 +1000704 return SVC_CLOSE;
705 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return SVC_DROP;
707 case -ENOENT:
708 return SVC_DENIED;
709 case 0:
710 rqstp->rq_client = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800711 kref_get(&rqstp->rq_client->ref);
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400712 ip_map_cached_put(xprt, ipm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 break;
714 }
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400715
716 gi = unix_gid_find(cred->cr_uid, rqstp);
717 switch (PTR_ERR(gi)) {
718 case -EAGAIN:
719 return SVC_DROP;
NeilBrown1ebede82010-08-12 17:04:07 +1000720 case -ESHUTDOWN:
721 return SVC_CLOSE;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400722 case -ENOENT:
723 break;
724 default:
725 put_group_info(cred->cr_group_info);
726 cred->cr_group_info = gi;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return SVC_OK;
729}
730
Trond Myklebust24c37672008-12-23 16:30:12 -0500731EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700734svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 struct kvec *argv = &rqstp->rq_arg.head[0];
737 struct kvec *resv = &rqstp->rq_res.head[0];
738 struct svc_cred *cred = &rqstp->rq_cred;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (argv->iov_len < 3*4)
741 return SVC_GARBAGE;
742
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800743 if (svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dprintk("svc: bad null cred\n");
745 *authp = rpc_autherr_badcred;
746 return SVC_DENIED;
747 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700748 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 dprintk("svc: bad null verf\n");
750 *authp = rpc_autherr_badverf;
751 return SVC_DENIED;
752 }
753
754 /* Signal that mapping to nobody uid/gid is required */
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800755 cred->cr_uid = INVALID_UID;
756 cred->cr_gid = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 cred->cr_group_info = groups_alloc(0);
758 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000759 return SVC_CLOSE; /* kmalloc failure - client must retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700762 svc_putnl(resv, RPC_AUTH_NULL);
763 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -0400765 rqstp->rq_cred.cr_flavor = RPC_AUTH_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return SVC_OK;
767}
768
769static int
770svcauth_null_release(struct svc_rqst *rqstp)
771{
772 if (rqstp->rq_client)
773 auth_domain_put(rqstp->rq_client);
774 rqstp->rq_client = NULL;
775 if (rqstp->rq_cred.cr_group_info)
776 put_group_info(rqstp->rq_cred.cr_group_info);
777 rqstp->rq_cred.cr_group_info = NULL;
778
779 return 0; /* don't drop */
780}
781
782
783struct auth_ops svcauth_null = {
784 .name = "null",
785 .owner = THIS_MODULE,
786 .flavour = RPC_AUTH_NULL,
787 .accept = svcauth_null_accept,
788 .release = svcauth_null_release,
789 .set_client = svcauth_unix_set_client,
790};
791
792
793static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700794svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct kvec *argv = &rqstp->rq_arg.head[0];
797 struct kvec *resv = &rqstp->rq_res.head[0];
798 struct svc_cred *cred = &rqstp->rq_cred;
799 u32 slen, i;
800 int len = argv->iov_len;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if ((len -= 3*4) < 0)
803 return SVC_GARBAGE;
804
805 svc_getu32(argv); /* length */
806 svc_getu32(argv); /* time stamp */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700807 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (slen > 64 || (len -= (slen + 3)*4) < 0)
809 goto badcred;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700810 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 argv->iov_len -= slen*4;
J. Bruce Fieldsafe3c3f2013-05-24 17:24:34 -0400812 /*
813 * Note: we skip uid_valid()/gid_valid() checks here for
814 * backwards compatibility with clients that use -1 id's.
815 * Instead, -1 uid or gid is later mapped to the
816 * (export-specific) anonymous id by nfsd_setuser.
817 * Supplementary gid's will be left alone.
818 */
Eric W. Biedermanf025adf2013-02-02 03:03:04 -0800819 cred->cr_uid = make_kuid(&init_user_ns, svc_getnl(argv)); /* uid */
820 cred->cr_gid = make_kgid(&init_user_ns, svc_getnl(argv)); /* gid */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700821 slen = svc_getnl(argv); /* gids length */
Kinglong Mee57864612017-02-07 21:48:11 +0800822 if (slen > UNX_NGROUPS || (len -= (slen + 2)*4) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto badcred;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400824 cred->cr_group_info = groups_alloc(slen);
825 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000826 return SVC_CLOSE;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800827 for (i = 0; i < slen; i++) {
828 kgid_t kgid = make_kgid(&init_user_ns, svc_getnl(argv));
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700829 cred->cr_group_info->gid[i] = kgid;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800830 }
Thiago Rafael Beckerbdcf0a42017-12-14 15:33:12 -0800831 groups_sort(cred->cr_group_info);
Alexey Dobriyan76994312006-09-26 22:28:46 -0700832 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 *authp = rpc_autherr_badverf;
834 return SVC_DENIED;
835 }
836
837 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700838 svc_putnl(resv, RPC_AUTH_NULL);
839 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -0400841 rqstp->rq_cred.cr_flavor = RPC_AUTH_UNIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return SVC_OK;
843
844badcred:
845 *authp = rpc_autherr_badcred;
846 return SVC_DENIED;
847}
848
849static int
850svcauth_unix_release(struct svc_rqst *rqstp)
851{
852 /* Verifier (such as it is) is already in place.
853 */
854 if (rqstp->rq_client)
855 auth_domain_put(rqstp->rq_client);
856 rqstp->rq_client = NULL;
857 if (rqstp->rq_cred.cr_group_info)
858 put_group_info(rqstp->rq_cred.cr_group_info);
859 rqstp->rq_cred.cr_group_info = NULL;
860
861 return 0;
862}
863
864
865struct auth_ops svcauth_unix = {
866 .name = "unix",
867 .owner = THIS_MODULE,
868 .flavour = RPC_AUTH_UNIX,
869 .accept = svcauth_unix_accept,
870 .release = svcauth_unix_release,
871 .domain_release = svcauth_unix_domain_release,
872 .set_client = svcauth_unix_set_client,
873};
874
Bhumika Goyalee24eac2017-10-17 18:14:26 +0200875static const struct cache_detail ip_map_cache_template = {
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400876 .owner = THIS_MODULE,
877 .hash_size = IP_HASHMAX,
878 .name = "auth.unix.ip",
879 .cache_put = ip_map_put,
Stanislav Kinsbursky73fb8472013-02-04 14:02:45 +0300880 .cache_request = ip_map_request,
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400881 .cache_parse = ip_map_parse,
882 .cache_show = ip_map_show,
883 .match = ip_map_match,
884 .init = ip_map_init,
885 .update = update,
886 .alloc = ip_map_alloc,
887};
888
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400889int ip_map_cache_create(struct net *net)
890{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400891 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400892 struct cache_detail *cd;
893 int err;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400894
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400895 cd = cache_create_net(&ip_map_cache_template, net);
896 if (IS_ERR(cd))
897 return PTR_ERR(cd);
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400898 err = cache_register_net(cd, net);
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400899 if (err) {
900 cache_destroy_net(cd, net);
901 return err;
902 }
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400903 sn->ip_map_cache = cd;
904 return 0;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400905}
906
907void ip_map_cache_destroy(struct net *net)
908{
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400909 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
910 struct cache_detail *cd = sn->ip_map_cache;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400911
Stanislav Kinsburskyd05cc10402012-01-19 21:42:45 +0400912 sn->ip_map_cache = NULL;
913 cache_purge(cd);
914 cache_unregister_net(cd, net);
915 cache_destroy_net(cd, net);
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400916}