xref: /openbmc/linux/net/sunrpc/svcauth.c (revision 0e17c50f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/net/sunrpc/svcauth.c
4  *
5  * The generic interface for RPC authentication on the server side.
6  *
7  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8  *
9  * CHANGES
10  * 19-Apr-2000 Chris Evans      - Security fix
11  */
12 
13 #include <linux/types.h>
14 #include <linux/module.h>
15 #include <linux/sunrpc/types.h>
16 #include <linux/sunrpc/xdr.h>
17 #include <linux/sunrpc/svcsock.h>
18 #include <linux/sunrpc/svcauth.h>
19 #include <linux/err.h>
20 #include <linux/hash.h>
21 
22 #include <trace/events/sunrpc.h>
23 
24 #define RPCDBG_FACILITY	RPCDBG_AUTH
25 
26 
27 /*
28  * Table of authenticators
29  */
30 extern struct auth_ops svcauth_null;
31 extern struct auth_ops svcauth_unix;
32 
33 static struct auth_ops __rcu *authtab[RPC_AUTH_MAXFLAVOR] = {
34 	[RPC_AUTH_NULL] = (struct auth_ops __force __rcu *)&svcauth_null,
35 	[RPC_AUTH_UNIX] = (struct auth_ops __force __rcu *)&svcauth_unix,
36 };
37 
38 static struct auth_ops *
39 svc_get_auth_ops(rpc_authflavor_t flavor)
40 {
41 	struct auth_ops		*aops;
42 
43 	if (flavor >= RPC_AUTH_MAXFLAVOR)
44 		return NULL;
45 	rcu_read_lock();
46 	aops = rcu_dereference(authtab[flavor]);
47 	if (aops != NULL && !try_module_get(aops->owner))
48 		aops = NULL;
49 	rcu_read_unlock();
50 	return aops;
51 }
52 
53 static void
54 svc_put_auth_ops(struct auth_ops *aops)
55 {
56 	module_put(aops->owner);
57 }
58 
59 int
60 svc_authenticate(struct svc_rqst *rqstp, __be32 *authp)
61 {
62 	rpc_authflavor_t	flavor;
63 	struct auth_ops		*aops;
64 
65 	*authp = rpc_auth_ok;
66 
67 	flavor = svc_getnl(&rqstp->rq_arg.head[0]);
68 
69 	dprintk("svc: svc_authenticate (%d)\n", flavor);
70 
71 	aops = svc_get_auth_ops(flavor);
72 	if (aops == NULL) {
73 		*authp = rpc_autherr_badcred;
74 		return SVC_DENIED;
75 	}
76 
77 	rqstp->rq_auth_slack = 0;
78 	init_svc_cred(&rqstp->rq_cred);
79 
80 	rqstp->rq_authop = aops;
81 	return aops->accept(rqstp, authp);
82 }
83 EXPORT_SYMBOL_GPL(svc_authenticate);
84 
85 int svc_set_client(struct svc_rqst *rqstp)
86 {
87 	rqstp->rq_client = NULL;
88 	return rqstp->rq_authop->set_client(rqstp);
89 }
90 EXPORT_SYMBOL_GPL(svc_set_client);
91 
92 /* A request, which was authenticated, has now executed.
93  * Time to finalise the credentials and verifier
94  * and release and resources
95  */
96 int svc_authorise(struct svc_rqst *rqstp)
97 {
98 	struct auth_ops *aops = rqstp->rq_authop;
99 	int rv = 0;
100 
101 	rqstp->rq_authop = NULL;
102 
103 	if (aops) {
104 		rv = aops->release(rqstp);
105 		svc_put_auth_ops(aops);
106 	}
107 	return rv;
108 }
109 
110 int
111 svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
112 {
113 	struct auth_ops *old;
114 	int rv = -EINVAL;
115 
116 	if (flavor < RPC_AUTH_MAXFLAVOR) {
117 		old = cmpxchg((struct auth_ops ** __force)&authtab[flavor], NULL, aops);
118 		if (old == NULL || old == aops)
119 			rv = 0;
120 	}
121 	return rv;
122 }
123 EXPORT_SYMBOL_GPL(svc_auth_register);
124 
125 void
126 svc_auth_unregister(rpc_authflavor_t flavor)
127 {
128 	if (flavor < RPC_AUTH_MAXFLAVOR)
129 		rcu_assign_pointer(authtab[flavor], NULL);
130 }
131 EXPORT_SYMBOL_GPL(svc_auth_unregister);
132 
133 /**************************************************
134  * 'auth_domains' are stored in a hash table indexed by name.
135  * When the last reference to an 'auth_domain' is dropped,
136  * the object is unhashed and freed.
137  * If auth_domain_lookup fails to find an entry, it will return
138  * it's second argument 'new'.  If this is non-null, it will
139  * have been atomically linked into the table.
140  */
141 
142 #define	DN_HASHBITS	6
143 #define	DN_HASHMAX	(1<<DN_HASHBITS)
144 
145 static struct hlist_head	auth_domain_table[DN_HASHMAX];
146 static DEFINE_SPINLOCK(auth_domain_lock);
147 
148 static void auth_domain_release(struct kref *kref)
149 	__releases(&auth_domain_lock)
150 {
151 	struct auth_domain *dom = container_of(kref, struct auth_domain, ref);
152 
153 	hlist_del_rcu(&dom->hash);
154 	dom->flavour->domain_release(dom);
155 	spin_unlock(&auth_domain_lock);
156 }
157 
158 void auth_domain_put(struct auth_domain *dom)
159 {
160 	kref_put_lock(&dom->ref, auth_domain_release, &auth_domain_lock);
161 }
162 EXPORT_SYMBOL_GPL(auth_domain_put);
163 
164 struct auth_domain *
165 auth_domain_lookup(char *name, struct auth_domain *new)
166 {
167 	struct auth_domain *hp;
168 	struct hlist_head *head;
169 
170 	head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
171 
172 	spin_lock(&auth_domain_lock);
173 
174 	hlist_for_each_entry(hp, head, hash) {
175 		if (strcmp(hp->name, name)==0) {
176 			kref_get(&hp->ref);
177 			spin_unlock(&auth_domain_lock);
178 			return hp;
179 		}
180 	}
181 	if (new)
182 		hlist_add_head_rcu(&new->hash, head);
183 	spin_unlock(&auth_domain_lock);
184 	return new;
185 }
186 EXPORT_SYMBOL_GPL(auth_domain_lookup);
187 
188 struct auth_domain *auth_domain_find(char *name)
189 {
190 	struct auth_domain *hp;
191 	struct hlist_head *head;
192 
193 	head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
194 
195 	rcu_read_lock();
196 	hlist_for_each_entry_rcu(hp, head, hash) {
197 		if (strcmp(hp->name, name)==0) {
198 			if (!kref_get_unless_zero(&hp->ref))
199 				hp = NULL;
200 			rcu_read_unlock();
201 			return hp;
202 		}
203 	}
204 	rcu_read_unlock();
205 	return NULL;
206 }
207 EXPORT_SYMBOL_GPL(auth_domain_find);
208