1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/sched.h>
4 #include <linux/module.h>
5 #include <linux/sunrpc/types.h>
6 #include <linux/sunrpc/xdr.h>
7 #include <linux/sunrpc/svcsock.h>
8 #include <linux/sunrpc/svcauth.h>
9 #include <linux/sunrpc/gss_api.h>
10 #include <linux/sunrpc/addr.h>
11 #include <linux/err.h>
12 #include <linux/seq_file.h>
13 #include <linux/hash.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/user_namespace.h>
20 #define RPCDBG_FACILITY RPCDBG_AUTH
26 * AUTHUNIX and AUTHNULL credentials are both handled here.
27 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
28 * are always nobody (-2). i.e. we do the same IP address checks for
29 * AUTHNULL as for AUTHUNIX, and that is done here.
35 /* other stuff later */
38 extern struct auth_ops svcauth_null;
39 extern struct auth_ops svcauth_unix;
41 static void svcauth_unix_domain_release_rcu(struct rcu_head *head)
43 struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
44 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
50 static void svcauth_unix_domain_release(struct auth_domain *dom)
52 call_rcu(&dom->rcu_head, svcauth_unix_domain_release_rcu);
55 struct auth_domain *unix_domain_find(char *name)
57 struct auth_domain *rv;
58 struct unix_domain *new = NULL;
60 rv = auth_domain_find(name);
63 if (new && rv != &new->h)
64 svcauth_unix_domain_release(&new->h);
66 if (rv->flavour != &svcauth_unix) {
73 new = kmalloc(sizeof(*new), GFP_KERNEL);
76 kref_init(&new->h.ref);
77 new->h.name = kstrdup(name, GFP_KERNEL);
78 if (new->h.name == NULL) {
82 new->h.flavour = &svcauth_unix;
83 rv = auth_domain_lookup(name, &new->h);
86 EXPORT_SYMBOL_GPL(unix_domain_find);
89 /**************************************************
90 * cache for IP address to unix_domain
91 * as needed by AUTH_UNIX
94 #define IP_HASHMAX (1<<IP_HASHBITS)
98 char m_class[8]; /* e.g. "nfsd" */
99 struct in6_addr m_addr;
100 struct unix_domain *m_client;
101 struct rcu_head m_rcu;
104 static void ip_map_put(struct kref *kref)
106 struct cache_head *item = container_of(kref, struct cache_head, ref);
107 struct ip_map *im = container_of(item, struct ip_map,h);
109 if (test_bit(CACHE_VALID, &item->flags) &&
110 !test_bit(CACHE_NEGATIVE, &item->flags))
111 auth_domain_put(&im->m_client->h);
112 kfree_rcu(im, m_rcu);
115 static inline int hash_ip6(const struct in6_addr *ip)
117 return hash_32(ipv6_addr_hash(ip), IP_HASHBITS);
119 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
121 struct ip_map *orig = container_of(corig, struct ip_map, h);
122 struct ip_map *new = container_of(cnew, struct ip_map, h);
123 return strcmp(orig->m_class, new->m_class) == 0 &&
124 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
126 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
128 struct ip_map *new = container_of(cnew, struct ip_map, h);
129 struct ip_map *item = container_of(citem, struct ip_map, h);
131 strcpy(new->m_class, item->m_class);
132 new->m_addr = item->m_addr;
134 static void update(struct cache_head *cnew, struct cache_head *citem)
136 struct ip_map *new = container_of(cnew, struct ip_map, h);
137 struct ip_map *item = container_of(citem, struct ip_map, h);
139 kref_get(&item->m_client->h.ref);
140 new->m_client = item->m_client;
142 static struct cache_head *ip_map_alloc(void)
144 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
151 static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
153 return sunrpc_cache_pipe_upcall(cd, h);
156 static void ip_map_request(struct cache_detail *cd,
157 struct cache_head *h,
158 char **bpp, int *blen)
161 struct ip_map *im = container_of(h, struct ip_map, h);
163 if (ipv6_addr_v4mapped(&(im->m_addr))) {
164 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
166 snprintf(text_addr, 40, "%pI6", &im->m_addr);
168 qword_add(bpp, blen, im->m_class);
169 qword_add(bpp, blen, text_addr);
173 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
174 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time64_t expiry);
176 static int ip_map_parse(struct cache_detail *cd,
177 char *mesg, int mlen)
179 /* class ipaddress [domainname] */
180 /* should be safe just to use the start of the input buffer
187 struct sockaddr_in s4;
188 struct sockaddr_in6 s6;
190 struct sockaddr_in6 sin6;
194 struct auth_domain *dom;
197 if (mesg[mlen-1] != '\n')
202 len = qword_get(&mesg, class, sizeof(class));
203 if (len <= 0) return -EINVAL;
206 len = qword_get(&mesg, buf, mlen);
207 if (len <= 0) return -EINVAL;
209 if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0)
211 switch (address.sa.sa_family) {
213 /* Form a mapped IPv4 address in sin6 */
214 sin6.sin6_family = AF_INET6;
215 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
218 #if IS_ENABLED(CONFIG_IPV6)
220 memcpy(&sin6, &address.s6, sizeof(sin6));
227 expiry = get_expiry(&mesg);
231 /* domainname, or empty for NEGATIVE */
232 len = qword_get(&mesg, buf, mlen);
233 if (len < 0) return -EINVAL;
236 dom = unix_domain_find(buf);
242 /* IPv6 scope IDs are ignored for now */
243 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
245 err = __ip_map_update(cd, ipmp,
246 container_of(dom, struct unix_domain, h),
252 auth_domain_put(dom);
258 static int ip_map_show(struct seq_file *m,
259 struct cache_detail *cd,
260 struct cache_head *h)
263 struct in6_addr addr;
264 char *dom = "-no-domain-";
267 seq_puts(m, "#class IP domain\n");
270 im = container_of(h, struct ip_map, h);
271 /* class addr domain */
274 if (test_bit(CACHE_VALID, &h->flags) &&
275 !test_bit(CACHE_NEGATIVE, &h->flags))
276 dom = im->m_client->h.name;
278 if (ipv6_addr_v4mapped(&addr)) {
279 seq_printf(m, "%s %pI4 %s\n",
280 im->m_class, &addr.s6_addr32[3], dom);
282 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
288 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
289 struct in6_addr *addr)
292 struct cache_head *ch;
294 strcpy(ip.m_class, class);
296 ch = sunrpc_cache_lookup_rcu(cd, &ip.h,
297 hash_str(class, IP_HASHBITS) ^
301 return container_of(ch, struct ip_map, h);
306 static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
307 struct in6_addr *addr)
309 struct sunrpc_net *sn;
311 sn = net_generic(net, sunrpc_net_id);
312 return __ip_map_lookup(sn->ip_map_cache, class, addr);
315 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
316 struct unix_domain *udom, time64_t expiry)
319 struct cache_head *ch;
324 set_bit(CACHE_NEGATIVE, &ip.h.flags);
325 ip.h.expiry_time = expiry;
326 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
327 hash_str(ipm->m_class, IP_HASHBITS) ^
328 hash_ip6(&ipm->m_addr));
335 void svcauth_unix_purge(struct net *net)
337 struct sunrpc_net *sn;
339 sn = net_generic(net, sunrpc_net_id);
340 cache_purge(sn->ip_map_cache);
342 EXPORT_SYMBOL_GPL(svcauth_unix_purge);
344 static inline struct ip_map *
345 ip_map_cached_get(struct svc_xprt *xprt)
347 struct ip_map *ipm = NULL;
348 struct sunrpc_net *sn;
350 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
351 spin_lock(&xprt->xpt_lock);
352 ipm = xprt->xpt_auth_cache;
354 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
355 if (cache_is_expired(sn->ip_map_cache, &ipm->h)) {
357 * The entry has been invalidated since it was
358 * remembered, e.g. by a second mount from the
361 xprt->xpt_auth_cache = NULL;
362 spin_unlock(&xprt->xpt_lock);
363 cache_put(&ipm->h, sn->ip_map_cache);
368 spin_unlock(&xprt->xpt_lock);
374 ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
376 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
377 spin_lock(&xprt->xpt_lock);
378 if (xprt->xpt_auth_cache == NULL) {
379 /* newly cached, keep the reference */
380 xprt->xpt_auth_cache = ipm;
383 spin_unlock(&xprt->xpt_lock);
386 struct sunrpc_net *sn;
388 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
389 cache_put(&ipm->h, sn->ip_map_cache);
394 svcauth_unix_info_release(struct svc_xprt *xpt)
398 ipm = xpt->xpt_auth_cache;
400 struct sunrpc_net *sn;
402 sn = net_generic(xpt->xpt_net, sunrpc_net_id);
403 cache_put(&ipm->h, sn->ip_map_cache);
407 /****************************************************************************
408 * auth.unix.gid cache
409 * simple cache to map a UID to a list of GIDs
410 * because AUTH_UNIX aka AUTH_SYS has a max of UNX_NGROUPS
412 #define GID_HASHBITS 8
413 #define GID_HASHMAX (1<<GID_HASHBITS)
418 struct group_info *gi;
422 static int unix_gid_hash(kuid_t uid)
424 return hash_long(from_kuid(&init_user_ns, uid), GID_HASHBITS);
427 static void unix_gid_put(struct kref *kref)
429 struct cache_head *item = container_of(kref, struct cache_head, ref);
430 struct unix_gid *ug = container_of(item, struct unix_gid, h);
431 if (test_bit(CACHE_VALID, &item->flags) &&
432 !test_bit(CACHE_NEGATIVE, &item->flags))
433 put_group_info(ug->gi);
437 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
439 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
440 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
441 return uid_eq(orig->uid, new->uid);
443 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
445 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
446 struct unix_gid *item = container_of(citem, struct unix_gid, h);
447 new->uid = item->uid;
449 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
451 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
452 struct unix_gid *item = container_of(citem, struct unix_gid, h);
454 get_group_info(item->gi);
457 static struct cache_head *unix_gid_alloc(void)
459 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
466 static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
468 return sunrpc_cache_pipe_upcall_timeout(cd, h);
471 static void unix_gid_request(struct cache_detail *cd,
472 struct cache_head *h,
473 char **bpp, int *blen)
476 struct unix_gid *ug = container_of(h, struct unix_gid, h);
478 snprintf(tuid, 20, "%u", from_kuid(&init_user_ns, ug->uid));
479 qword_add(bpp, blen, tuid);
483 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid);
485 static int unix_gid_parse(struct cache_detail *cd,
486 char *mesg, int mlen)
488 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
496 struct unix_gid ug, *ugp;
498 if (mesg[mlen - 1] != '\n')
502 rv = get_int(&mesg, &id);
505 uid = make_kuid(current_user_ns(), id);
508 expiry = get_expiry(&mesg);
512 rv = get_int(&mesg, &gids);
513 if (rv || gids < 0 || gids > 8192)
516 ug.gi = groups_alloc(gids);
520 for (i = 0 ; i < gids ; i++) {
523 rv = get_int(&mesg, &gid);
527 kgid = make_kgid(current_user_ns(), gid);
528 if (!gid_valid(kgid))
530 ug.gi->gid[i] = kgid;
534 ugp = unix_gid_lookup(cd, uid);
536 struct cache_head *ch;
538 ug.h.expiry_time = expiry;
539 ch = sunrpc_cache_update(cd,
552 put_group_info(ug.gi);
556 static int unix_gid_show(struct seq_file *m,
557 struct cache_detail *cd,
558 struct cache_head *h)
560 struct user_namespace *user_ns = m->file->f_cred->user_ns;
566 seq_puts(m, "#uid cnt: gids...\n");
569 ug = container_of(h, struct unix_gid, h);
570 if (test_bit(CACHE_VALID, &h->flags) &&
571 !test_bit(CACHE_NEGATIVE, &h->flags))
572 glen = ug->gi->ngroups;
576 seq_printf(m, "%u %d:", from_kuid_munged(user_ns, ug->uid), glen);
577 for (i = 0; i < glen; i++)
578 seq_printf(m, " %d", from_kgid_munged(user_ns, ug->gi->gid[i]));
583 static const struct cache_detail unix_gid_cache_template = {
584 .owner = THIS_MODULE,
585 .hash_size = GID_HASHMAX,
586 .name = "auth.unix.gid",
587 .cache_put = unix_gid_put,
588 .cache_upcall = unix_gid_upcall,
589 .cache_request = unix_gid_request,
590 .cache_parse = unix_gid_parse,
591 .cache_show = unix_gid_show,
592 .match = unix_gid_match,
593 .init = unix_gid_init,
594 .update = unix_gid_update,
595 .alloc = unix_gid_alloc,
598 int unix_gid_cache_create(struct net *net)
600 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
601 struct cache_detail *cd;
604 cd = cache_create_net(&unix_gid_cache_template, net);
607 err = cache_register_net(cd, net);
609 cache_destroy_net(cd, net);
612 sn->unix_gid_cache = cd;
616 void unix_gid_cache_destroy(struct net *net)
618 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
619 struct cache_detail *cd = sn->unix_gid_cache;
621 sn->unix_gid_cache = NULL;
623 cache_unregister_net(cd, net);
624 cache_destroy_net(cd, net);
627 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid)
630 struct cache_head *ch;
633 ch = sunrpc_cache_lookup_rcu(cd, &ug.h, unix_gid_hash(uid));
635 return container_of(ch, struct unix_gid, h);
640 static struct group_info *unix_gid_find(kuid_t uid, struct svc_rqst *rqstp)
643 struct group_info *gi;
645 struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
648 ug = unix_gid_lookup(sn->unix_gid_cache, uid);
650 return ERR_PTR(-EAGAIN);
651 ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
654 return ERR_PTR(-ENOENT);
656 return ERR_PTR(-ESHUTDOWN);
658 gi = get_group_info(ug->gi);
659 cache_put(&ug->h, sn->unix_gid_cache);
662 return ERR_PTR(-EAGAIN);
667 svcauth_unix_set_client(struct svc_rqst *rqstp)
669 struct sockaddr_in *sin;
670 struct sockaddr_in6 *sin6, sin6_storage;
672 struct group_info *gi;
673 struct svc_cred *cred = &rqstp->rq_cred;
674 struct svc_xprt *xprt = rqstp->rq_xprt;
675 struct net *net = xprt->xpt_net;
676 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
678 switch (rqstp->rq_addr.ss_family) {
680 sin = svc_addr_in(rqstp);
681 sin6 = &sin6_storage;
682 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
685 sin6 = svc_addr_in6(rqstp);
691 rqstp->rq_client = NULL;
692 if (rqstp->rq_proc == 0)
695 ipm = ip_map_cached_get(xprt);
697 ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
703 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
713 rqstp->rq_client = &ipm->m_client->h;
714 kref_get(&rqstp->rq_client->ref);
715 ip_map_cached_put(xprt, ipm);
719 gi = unix_gid_find(cred->cr_uid, rqstp);
720 switch (PTR_ERR(gi)) {
728 put_group_info(cred->cr_group_info);
729 cred->cr_group_info = gi;
734 EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
737 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
739 struct kvec *argv = &rqstp->rq_arg.head[0];
740 struct kvec *resv = &rqstp->rq_res.head[0];
741 struct svc_cred *cred = &rqstp->rq_cred;
743 if (argv->iov_len < 3*4)
746 if (svc_getu32(argv) != 0) {
747 dprintk("svc: bad null cred\n");
748 *authp = rpc_autherr_badcred;
751 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
752 dprintk("svc: bad null verf\n");
753 *authp = rpc_autherr_badverf;
757 /* Signal that mapping to nobody uid/gid is required */
758 cred->cr_uid = INVALID_UID;
759 cred->cr_gid = INVALID_GID;
760 cred->cr_group_info = groups_alloc(0);
761 if (cred->cr_group_info == NULL)
762 return SVC_CLOSE; /* kmalloc failure - client must retry */
764 /* Put NULL verifier */
765 svc_putnl(resv, RPC_AUTH_NULL);
768 rqstp->rq_cred.cr_flavor = RPC_AUTH_NULL;
773 svcauth_null_release(struct svc_rqst *rqstp)
775 if (rqstp->rq_client)
776 auth_domain_put(rqstp->rq_client);
777 rqstp->rq_client = NULL;
778 if (rqstp->rq_cred.cr_group_info)
779 put_group_info(rqstp->rq_cred.cr_group_info);
780 rqstp->rq_cred.cr_group_info = NULL;
782 return 0; /* don't drop */
786 struct auth_ops svcauth_null = {
788 .owner = THIS_MODULE,
789 .flavour = RPC_AUTH_NULL,
790 .accept = svcauth_null_accept,
791 .release = svcauth_null_release,
792 .set_client = svcauth_unix_set_client,
797 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
799 struct kvec *argv = &rqstp->rq_arg.head[0];
800 struct kvec *resv = &rqstp->rq_res.head[0];
801 struct svc_cred *cred = &rqstp->rq_cred;
802 struct user_namespace *userns;
804 int len = argv->iov_len;
806 if ((len -= 3*4) < 0)
809 svc_getu32(argv); /* length */
810 svc_getu32(argv); /* time stamp */
811 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
812 if (slen > 64 || (len -= (slen + 3)*4) < 0)
814 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
815 argv->iov_len -= slen*4;
817 * Note: we skip uid_valid()/gid_valid() checks here for
818 * backwards compatibility with clients that use -1 id's.
819 * Instead, -1 uid or gid is later mapped to the
820 * (export-specific) anonymous id by nfsd_setuser.
821 * Supplementary gid's will be left alone.
823 userns = (rqstp->rq_xprt && rqstp->rq_xprt->xpt_cred) ?
824 rqstp->rq_xprt->xpt_cred->user_ns : &init_user_ns;
825 cred->cr_uid = make_kuid(userns, svc_getnl(argv)); /* uid */
826 cred->cr_gid = make_kgid(userns, svc_getnl(argv)); /* gid */
827 slen = svc_getnl(argv); /* gids length */
828 if (slen > UNX_NGROUPS || (len -= (slen + 2)*4) < 0)
830 cred->cr_group_info = groups_alloc(slen);
831 if (cred->cr_group_info == NULL)
833 for (i = 0; i < slen; i++) {
834 kgid_t kgid = make_kgid(userns, svc_getnl(argv));
835 cred->cr_group_info->gid[i] = kgid;
837 groups_sort(cred->cr_group_info);
838 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
839 *authp = rpc_autherr_badverf;
843 /* Put NULL verifier */
844 svc_putnl(resv, RPC_AUTH_NULL);
847 rqstp->rq_cred.cr_flavor = RPC_AUTH_UNIX;
851 *authp = rpc_autherr_badcred;
856 svcauth_unix_release(struct svc_rqst *rqstp)
858 /* Verifier (such as it is) is already in place.
860 if (rqstp->rq_client)
861 auth_domain_put(rqstp->rq_client);
862 rqstp->rq_client = NULL;
863 if (rqstp->rq_cred.cr_group_info)
864 put_group_info(rqstp->rq_cred.cr_group_info);
865 rqstp->rq_cred.cr_group_info = NULL;
871 struct auth_ops svcauth_unix = {
873 .owner = THIS_MODULE,
874 .flavour = RPC_AUTH_UNIX,
875 .accept = svcauth_unix_accept,
876 .release = svcauth_unix_release,
877 .domain_release = svcauth_unix_domain_release,
878 .set_client = svcauth_unix_set_client,
881 static const struct cache_detail ip_map_cache_template = {
882 .owner = THIS_MODULE,
883 .hash_size = IP_HASHMAX,
884 .name = "auth.unix.ip",
885 .cache_put = ip_map_put,
886 .cache_upcall = ip_map_upcall,
887 .cache_request = ip_map_request,
888 .cache_parse = ip_map_parse,
889 .cache_show = ip_map_show,
890 .match = ip_map_match,
893 .alloc = ip_map_alloc,
896 int ip_map_cache_create(struct net *net)
898 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
899 struct cache_detail *cd;
902 cd = cache_create_net(&ip_map_cache_template, net);
905 err = cache_register_net(cd, net);
907 cache_destroy_net(cd, net);
910 sn->ip_map_cache = cd;
914 void ip_map_cache_destroy(struct net *net)
916 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
917 struct cache_detail *cd = sn->ip_map_cache;
919 sn->ip_map_cache = NULL;
921 cache_unregister_net(cd, net);
922 cache_destroy_net(cd, net);