1 #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>
8 #include <linux/sunrpc/gss_api.h>
9 #include <linux/sunrpc/addr.h>
10 #include <linux/err.h>
11 #include <linux/seq_file.h>
12 #include <linux/hash.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
17 #include <linux/kernel.h>
18 #include <linux/user_namespace.h>
19 #define RPCDBG_FACILITY RPCDBG_AUTH
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.
34 /* other stuff later */
37 extern struct auth_ops svcauth_null;
38 extern struct auth_ops svcauth_unix;
40 static void svcauth_unix_domain_release_rcu(struct rcu_head *head)
42 struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
43 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
49 static void svcauth_unix_domain_release(struct auth_domain *dom)
51 call_rcu(&dom->rcu_head, svcauth_unix_domain_release_rcu);
54 struct auth_domain *unix_domain_find(char *name)
56 struct auth_domain *rv;
57 struct unix_domain *new = NULL;
59 rv = auth_domain_find(name);
62 if (new && rv != &new->h)
63 svcauth_unix_domain_release(&new->h);
65 if (rv->flavour != &svcauth_unix) {
72 new = kmalloc(sizeof(*new), GFP_KERNEL);
75 kref_init(&new->h.ref);
76 new->h.name = kstrdup(name, GFP_KERNEL);
77 if (new->h.name == NULL) {
81 new->h.flavour = &svcauth_unix;
82 rv = auth_domain_lookup(name, &new->h);
85 EXPORT_SYMBOL_GPL(unix_domain_find);
88 /**************************************************
89 * cache for IP address to unix_domain
90 * as needed by AUTH_UNIX
93 #define IP_HASHMAX (1<<IP_HASHBITS)
97 char m_class[8]; /* e.g. "nfsd" */
98 struct in6_addr m_addr;
99 struct unix_domain *m_client;
100 struct rcu_head m_rcu;
103 static void ip_map_put(struct kref *kref)
105 struct cache_head *item = container_of(kref, struct cache_head, ref);
106 struct ip_map *im = container_of(item, struct ip_map,h);
108 if (test_bit(CACHE_VALID, &item->flags) &&
109 !test_bit(CACHE_NEGATIVE, &item->flags))
110 auth_domain_put(&im->m_client->h);
111 kfree_rcu(im, m_rcu);
114 static inline int hash_ip6(const struct in6_addr *ip)
116 return hash_32(ipv6_addr_hash(ip), IP_HASHBITS);
118 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
120 struct ip_map *orig = container_of(corig, struct ip_map, h);
121 struct ip_map *new = container_of(cnew, struct ip_map, h);
122 return strcmp(orig->m_class, new->m_class) == 0 &&
123 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
125 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
127 struct ip_map *new = container_of(cnew, struct ip_map, h);
128 struct ip_map *item = container_of(citem, struct ip_map, h);
130 strcpy(new->m_class, item->m_class);
131 new->m_addr = item->m_addr;
133 static void update(struct cache_head *cnew, struct cache_head *citem)
135 struct ip_map *new = container_of(cnew, struct ip_map, h);
136 struct ip_map *item = container_of(citem, struct ip_map, h);
138 kref_get(&item->m_client->h.ref);
139 new->m_client = item->m_client;
141 static struct cache_head *ip_map_alloc(void)
143 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
150 static void ip_map_request(struct cache_detail *cd,
151 struct cache_head *h,
152 char **bpp, int *blen)
155 struct ip_map *im = container_of(h, struct ip_map, h);
157 if (ipv6_addr_v4mapped(&(im->m_addr))) {
158 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
160 snprintf(text_addr, 40, "%pI6", &im->m_addr);
162 qword_add(bpp, blen, im->m_class);
163 qword_add(bpp, blen, text_addr);
167 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
168 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
170 static int ip_map_parse(struct cache_detail *cd,
171 char *mesg, int mlen)
173 /* class ipaddress [domainname] */
174 /* should be safe just to use the start of the input buffer
181 struct sockaddr_in s4;
182 struct sockaddr_in6 s6;
184 struct sockaddr_in6 sin6;
188 struct auth_domain *dom;
191 if (mesg[mlen-1] != '\n')
196 len = qword_get(&mesg, class, sizeof(class));
197 if (len <= 0) return -EINVAL;
200 len = qword_get(&mesg, buf, mlen);
201 if (len <= 0) return -EINVAL;
203 if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0)
205 switch (address.sa.sa_family) {
207 /* Form a mapped IPv4 address in sin6 */
208 sin6.sin6_family = AF_INET6;
209 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
212 #if IS_ENABLED(CONFIG_IPV6)
214 memcpy(&sin6, &address.s6, sizeof(sin6));
221 expiry = get_expiry(&mesg);
225 /* domainname, or empty for NEGATIVE */
226 len = qword_get(&mesg, buf, mlen);
227 if (len < 0) return -EINVAL;
230 dom = unix_domain_find(buf);
236 /* IPv6 scope IDs are ignored for now */
237 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
239 err = __ip_map_update(cd, ipmp,
240 container_of(dom, struct unix_domain, h),
246 auth_domain_put(dom);
252 static int ip_map_show(struct seq_file *m,
253 struct cache_detail *cd,
254 struct cache_head *h)
257 struct in6_addr addr;
258 char *dom = "-no-domain-";
261 seq_puts(m, "#class IP domain\n");
264 im = container_of(h, struct ip_map, h);
265 /* class addr domain */
268 if (test_bit(CACHE_VALID, &h->flags) &&
269 !test_bit(CACHE_NEGATIVE, &h->flags))
270 dom = im->m_client->h.name;
272 if (ipv6_addr_v4mapped(&addr)) {
273 seq_printf(m, "%s %pI4 %s\n",
274 im->m_class, &addr.s6_addr32[3], dom);
276 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
282 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
283 struct in6_addr *addr)
286 struct cache_head *ch;
288 strcpy(ip.m_class, class);
290 ch = sunrpc_cache_lookup_rcu(cd, &ip.h,
291 hash_str(class, IP_HASHBITS) ^
295 return container_of(ch, struct ip_map, h);
300 static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
301 struct in6_addr *addr)
303 struct sunrpc_net *sn;
305 sn = net_generic(net, sunrpc_net_id);
306 return __ip_map_lookup(sn->ip_map_cache, class, addr);
309 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
310 struct unix_domain *udom, time_t expiry)
313 struct cache_head *ch;
318 set_bit(CACHE_NEGATIVE, &ip.h.flags);
319 ip.h.expiry_time = expiry;
320 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
321 hash_str(ipm->m_class, IP_HASHBITS) ^
322 hash_ip6(&ipm->m_addr));
329 static inline int ip_map_update(struct net *net, struct ip_map *ipm,
330 struct unix_domain *udom, time_t expiry)
332 struct sunrpc_net *sn;
334 sn = net_generic(net, sunrpc_net_id);
335 return __ip_map_update(sn->ip_map_cache, ipm, udom, expiry);
338 void svcauth_unix_purge(struct net *net)
340 struct sunrpc_net *sn;
342 sn = net_generic(net, sunrpc_net_id);
343 cache_purge(sn->ip_map_cache);
345 EXPORT_SYMBOL_GPL(svcauth_unix_purge);
347 static inline struct ip_map *
348 ip_map_cached_get(struct svc_xprt *xprt)
350 struct ip_map *ipm = NULL;
351 struct sunrpc_net *sn;
353 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
354 spin_lock(&xprt->xpt_lock);
355 ipm = xprt->xpt_auth_cache;
357 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
358 if (cache_is_expired(sn->ip_map_cache, &ipm->h)) {
360 * The entry has been invalidated since it was
361 * remembered, e.g. by a second mount from the
364 xprt->xpt_auth_cache = NULL;
365 spin_unlock(&xprt->xpt_lock);
366 cache_put(&ipm->h, sn->ip_map_cache);
371 spin_unlock(&xprt->xpt_lock);
377 ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
379 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;
386 spin_unlock(&xprt->xpt_lock);
389 struct sunrpc_net *sn;
391 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
392 cache_put(&ipm->h, sn->ip_map_cache);
397 svcauth_unix_info_release(struct svc_xprt *xpt)
401 ipm = xpt->xpt_auth_cache;
403 struct sunrpc_net *sn;
405 sn = net_generic(xpt->xpt_net, sunrpc_net_id);
406 cache_put(&ipm->h, sn->ip_map_cache);
410 /****************************************************************************
411 * auth.unix.gid cache
412 * simple cache to map a UID to a list of GIDs
413 * because AUTH_UNIX aka AUTH_SYS has a max of UNX_NGROUPS
415 #define GID_HASHBITS 8
416 #define GID_HASHMAX (1<<GID_HASHBITS)
421 struct group_info *gi;
425 static int unix_gid_hash(kuid_t uid)
427 return hash_long(from_kuid(&init_user_ns, uid), GID_HASHBITS);
430 static void unix_gid_put(struct kref *kref)
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);
440 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
442 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
443 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
444 return uid_eq(orig->uid, new->uid);
446 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
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;
452 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
454 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
455 struct unix_gid *item = container_of(citem, struct unix_gid, h);
457 get_group_info(item->gi);
460 static struct cache_head *unix_gid_alloc(void)
462 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
469 static void unix_gid_request(struct cache_detail *cd,
470 struct cache_head *h,
471 char **bpp, int *blen)
474 struct unix_gid *ug = container_of(h, struct unix_gid, h);
476 snprintf(tuid, 20, "%u", from_kuid(&init_user_ns, ug->uid));
477 qword_add(bpp, blen, tuid);
481 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid);
483 static int unix_gid_parse(struct cache_detail *cd,
484 char *mesg, int mlen)
486 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
494 struct unix_gid ug, *ugp;
496 if (mesg[mlen - 1] != '\n')
500 rv = get_int(&mesg, &id);
503 uid = make_kuid(&init_user_ns, id);
506 expiry = get_expiry(&mesg);
510 rv = get_int(&mesg, &gids);
511 if (rv || gids < 0 || gids > 8192)
514 ug.gi = groups_alloc(gids);
518 for (i = 0 ; i < gids ; i++) {
521 rv = get_int(&mesg, &gid);
525 kgid = make_kgid(&init_user_ns, gid);
526 if (!gid_valid(kgid))
528 ug.gi->gid[i] = kgid;
532 ugp = unix_gid_lookup(cd, uid);
534 struct cache_head *ch;
536 ug.h.expiry_time = expiry;
537 ch = sunrpc_cache_update(cd,
550 put_group_info(ug.gi);
554 static int unix_gid_show(struct seq_file *m,
555 struct cache_detail *cd,
556 struct cache_head *h)
558 struct user_namespace *user_ns = &init_user_ns;
564 seq_puts(m, "#uid cnt: gids...\n");
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;
574 seq_printf(m, "%u %d:", from_kuid_munged(user_ns, ug->uid), glen);
575 for (i = 0; i < glen; i++)
576 seq_printf(m, " %d", from_kgid_munged(user_ns, ug->gi->gid[i]));
581 static const struct cache_detail unix_gid_cache_template = {
582 .owner = THIS_MODULE,
583 .hash_size = GID_HASHMAX,
584 .name = "auth.unix.gid",
585 .cache_put = unix_gid_put,
586 .cache_request = unix_gid_request,
587 .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,
595 int unix_gid_cache_create(struct net *net)
597 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
598 struct cache_detail *cd;
601 cd = cache_create_net(&unix_gid_cache_template, net);
604 err = cache_register_net(cd, net);
606 cache_destroy_net(cd, net);
609 sn->unix_gid_cache = cd;
613 void unix_gid_cache_destroy(struct net *net)
615 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
616 struct cache_detail *cd = sn->unix_gid_cache;
618 sn->unix_gid_cache = NULL;
620 cache_unregister_net(cd, net);
621 cache_destroy_net(cd, net);
624 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid)
627 struct cache_head *ch;
630 ch = sunrpc_cache_lookup_rcu(cd, &ug.h, unix_gid_hash(uid));
632 return container_of(ch, struct unix_gid, h);
637 static struct group_info *unix_gid_find(kuid_t uid, struct svc_rqst *rqstp)
640 struct group_info *gi;
642 struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
645 ug = unix_gid_lookup(sn->unix_gid_cache, uid);
647 return ERR_PTR(-EAGAIN);
648 ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
651 return ERR_PTR(-ENOENT);
653 return ERR_PTR(-ESHUTDOWN);
655 gi = get_group_info(ug->gi);
656 cache_put(&ug->h, sn->unix_gid_cache);
659 return ERR_PTR(-EAGAIN);
664 svcauth_unix_set_client(struct svc_rqst *rqstp)
666 struct sockaddr_in *sin;
667 struct sockaddr_in6 *sin6, sin6_storage;
669 struct group_info *gi;
670 struct svc_cred *cred = &rqstp->rq_cred;
671 struct svc_xprt *xprt = rqstp->rq_xprt;
672 struct net *net = xprt->xpt_net;
673 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
675 switch (rqstp->rq_addr.ss_family) {
677 sin = svc_addr_in(rqstp);
678 sin6 = &sin6_storage;
679 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
682 sin6 = svc_addr_in6(rqstp);
688 rqstp->rq_client = NULL;
689 if (rqstp->rq_proc == 0)
692 ipm = ip_map_cached_get(xprt);
694 ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
700 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
710 rqstp->rq_client = &ipm->m_client->h;
711 kref_get(&rqstp->rq_client->ref);
712 ip_map_cached_put(xprt, ipm);
716 gi = unix_gid_find(cred->cr_uid, rqstp);
717 switch (PTR_ERR(gi)) {
725 put_group_info(cred->cr_group_info);
726 cred->cr_group_info = gi;
731 EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
734 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
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;
740 if (argv->iov_len < 3*4)
743 if (svc_getu32(argv) != 0) {
744 dprintk("svc: bad null cred\n");
745 *authp = rpc_autherr_badcred;
748 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
749 dprintk("svc: bad null verf\n");
750 *authp = rpc_autherr_badverf;
754 /* Signal that mapping to nobody uid/gid is required */
755 cred->cr_uid = INVALID_UID;
756 cred->cr_gid = INVALID_GID;
757 cred->cr_group_info = groups_alloc(0);
758 if (cred->cr_group_info == NULL)
759 return SVC_CLOSE; /* kmalloc failure - client must retry */
761 /* Put NULL verifier */
762 svc_putnl(resv, RPC_AUTH_NULL);
765 rqstp->rq_cred.cr_flavor = RPC_AUTH_NULL;
770 svcauth_null_release(struct svc_rqst *rqstp)
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;
779 return 0; /* don't drop */
783 struct auth_ops svcauth_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,
794 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
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;
800 int len = argv->iov_len;
802 if ((len -= 3*4) < 0)
805 svc_getu32(argv); /* length */
806 svc_getu32(argv); /* time stamp */
807 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
808 if (slen > 64 || (len -= (slen + 3)*4) < 0)
810 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
811 argv->iov_len -= slen*4;
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.
819 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 */
821 slen = svc_getnl(argv); /* gids length */
822 if (slen > UNX_NGROUPS || (len -= (slen + 2)*4) < 0)
824 cred->cr_group_info = groups_alloc(slen);
825 if (cred->cr_group_info == NULL)
827 for (i = 0; i < slen; i++) {
828 kgid_t kgid = make_kgid(&init_user_ns, svc_getnl(argv));
829 cred->cr_group_info->gid[i] = kgid;
831 groups_sort(cred->cr_group_info);
832 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
833 *authp = rpc_autherr_badverf;
837 /* Put NULL verifier */
838 svc_putnl(resv, RPC_AUTH_NULL);
841 rqstp->rq_cred.cr_flavor = RPC_AUTH_UNIX;
845 *authp = rpc_autherr_badcred;
850 svcauth_unix_release(struct svc_rqst *rqstp)
852 /* Verifier (such as it is) is already in place.
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;
865 struct auth_ops svcauth_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,
875 static const struct cache_detail ip_map_cache_template = {
876 .owner = THIS_MODULE,
877 .hash_size = IP_HASHMAX,
878 .name = "auth.unix.ip",
879 .cache_put = ip_map_put,
880 .cache_request = ip_map_request,
881 .cache_parse = ip_map_parse,
882 .cache_show = ip_map_show,
883 .match = ip_map_match,
886 .alloc = ip_map_alloc,
889 int ip_map_cache_create(struct net *net)
891 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
892 struct cache_detail *cd;
895 cd = cache_create_net(&ip_map_cache_template, net);
898 err = cache_register_net(cd, net);
900 cache_destroy_net(cd, net);
903 sn->ip_map_cache = cd;
907 void ip_map_cache_destroy(struct net *net)
909 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
910 struct cache_detail *cd = sn->ip_map_cache;
912 sn->ip_map_cache = NULL;
914 cache_unregister_net(cd, net);
915 cache_destroy_net(cd, net);