blob: fae632da1058a1ecd574c8ff9d2f292f908fac8f [file] [log] [blame]
Chuck Lever2573a462019-02-11 11:25:15 -05001// SPDX-License-Identifier: BSD-3-Clause
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/gss_mech_switch.c
4 *
5 * Copyright (c) 2001 The Regents of the University of Michigan.
6 * All rights reserved.
7 *
8 * J. Bruce Fields <[email protected]>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/types.h>
12#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
Chuck Leverf7832882013-03-16 15:54:52 -040014#include <linux/oid_registry.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/msg_prot.h>
16#include <linux/sunrpc/gss_asn1.h>
17#include <linux/sunrpc/auth_gss.h>
18#include <linux/sunrpc/svcauth_gss.h>
19#include <linux/sunrpc/gss_err.h>
20#include <linux/sunrpc/sched.h>
21#include <linux/sunrpc/gss_api.h>
22#include <linux/sunrpc/clnt.h>
Chuck Leverff27e9f2019-10-24 09:34:10 -040023#include <trace/events/rpcgss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jeff Laytonf895b252014-11-17 16:58:04 -050025#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026# define RPCDBG_FACILITY RPCDBG_AUTH
27#endif
28
29static LIST_HEAD(registered_mechs);
30static DEFINE_SPINLOCK(registered_mechs_lock);
31
32static void
33gss_mech_free(struct gss_api_mech *gm)
34{
35 struct pf_desc *pf;
36 int i;
37
38 for (i = 0; i < gm->gm_pf_num; i++) {
39 pf = &gm->gm_pfs[i];
NeilBrown24c5efe2020-05-22 12:01:33 +100040 if (pf->domain)
41 auth_domain_put(pf->domain);
Jesper Juhla51482b2005-11-08 09:41:34 -080042 kfree(pf->auth_domain_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 pf->auth_domain_name = NULL;
44 }
45}
46
47static inline char *
48make_auth_domain_name(char *name)
49{
50 static char *prefix = "gss/";
51 char *new;
52
53 new = kmalloc(strlen(name) + strlen(prefix) + 1, GFP_KERNEL);
54 if (new) {
55 strcpy(new, prefix);
56 strcat(new, name);
57 }
58 return new;
59}
60
61static int
62gss_mech_svc_setup(struct gss_api_mech *gm)
63{
NeilBrown24c5efe2020-05-22 12:01:33 +100064 struct auth_domain *dom;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 struct pf_desc *pf;
66 int i, status;
67
68 for (i = 0; i < gm->gm_pf_num; i++) {
69 pf = &gm->gm_pfs[i];
70 pf->auth_domain_name = make_auth_domain_name(pf->name);
71 status = -ENOMEM;
72 if (pf->auth_domain_name == NULL)
73 goto out;
NeilBrown24c5efe2020-05-22 12:01:33 +100074 dom = svcauth_gss_register_pseudoflavor(
75 pf->pseudoflavor, pf->auth_domain_name);
76 if (IS_ERR(dom)) {
77 status = PTR_ERR(dom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 goto out;
NeilBrown24c5efe2020-05-22 12:01:33 +100079 }
80 pf->domain = dom;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 return 0;
83out:
84 gss_mech_free(gm);
85 return status;
86}
87
Chuck Lever50072202013-03-16 15:55:27 -040088/**
89 * gss_mech_register - register a GSS mechanism
90 * @gm: GSS mechanism handle
91 *
92 * Returns zero if successful, or a negative errno.
93 */
94int gss_mech_register(struct gss_api_mech *gm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int status;
97
98 status = gss_mech_svc_setup(gm);
99 if (status)
100 return status;
101 spin_lock(&registered_mechs_lock);
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400102 list_add_rcu(&gm->gm_list, &registered_mechs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 spin_unlock(&registered_mechs_lock);
Chuck Lever8885cb32007-01-31 12:14:05 -0500104 dprintk("RPC: registered gss mechanism %s\n", gm->gm_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
Trond Myklebust7bd88262008-12-23 15:21:32 -0500107EXPORT_SYMBOL_GPL(gss_mech_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Chuck Lever50072202013-03-16 15:55:27 -0400109/**
110 * gss_mech_unregister - release a GSS mechanism
111 * @gm: GSS mechanism handle
112 *
113 */
114void gss_mech_unregister(struct gss_api_mech *gm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 spin_lock(&registered_mechs_lock);
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400117 list_del_rcu(&gm->gm_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 spin_unlock(&registered_mechs_lock);
Chuck Lever8885cb32007-01-31 12:14:05 -0500119 dprintk("RPC: unregistered gss mechanism %s\n", gm->gm_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 gss_mech_free(gm);
121}
Trond Myklebust7bd88262008-12-23 15:21:32 -0500122EXPORT_SYMBOL_GPL(gss_mech_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
J. Bruce Fields0dc15312013-05-14 16:07:13 -0400124struct gss_api_mech *gss_mech_get(struct gss_api_mech *gm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 __module_get(gm->gm_owner);
127 return gm;
128}
J. Bruce Fields0dc15312013-05-14 16:07:13 -0400129EXPORT_SYMBOL(gss_mech_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Trond Myklebustc5f5e9c2013-01-12 11:17:14 -0500131static struct gss_api_mech *
J. Bruce Fields058c5c92011-06-22 10:50:08 -0400132_gss_mech_get_by_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct gss_api_mech *pos, *gm = NULL;
135
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400136 rcu_read_lock();
137 list_for_each_entry_rcu(pos, &registered_mechs, gm_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (0 == strcmp(name, pos->gm_name)) {
139 if (try_module_get(pos->gm_owner))
140 gm = pos;
141 break;
142 }
143 }
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400144 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return gm;
146
147}
148
J. Bruce Fields058c5c92011-06-22 10:50:08 -0400149struct gss_api_mech * gss_mech_get_by_name(const char *name)
150{
151 struct gss_api_mech *gm = NULL;
152
153 gm = _gss_mech_get_by_name(name);
154 if (!gm) {
155 request_module("rpc-auth-gss-%s", name);
156 gm = _gss_mech_get_by_name(name);
157 }
158 return gm;
159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
J. Bruce Fieldsb1df7632013-04-29 14:03:30 -0400161struct gss_api_mech *gss_mech_get_by_OID(struct rpcsec_gss_oid *obj)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162{
163 struct gss_api_mech *pos, *gm = NULL;
Chuck Leverf7832882013-03-16 15:54:52 -0400164 char buf[32];
165
166 if (sprint_oid(obj->data, obj->len, buf, sizeof(buf)) < 0)
167 return NULL;
Chuck Leverf7832882013-03-16 15:54:52 -0400168 request_module("rpc-auth-gss-%s", buf);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400170 rcu_read_lock();
171 list_for_each_entry_rcu(pos, &registered_mechs, gm_list) {
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000172 if (obj->len == pos->gm_oid.len) {
173 if (0 == memcmp(obj->data, pos->gm_oid.data, obj->len)) {
174 if (try_module_get(pos->gm_owner))
175 gm = pos;
176 break;
177 }
178 }
179 }
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400180 rcu_read_unlock();
Chuck Leverff27e9f2019-10-24 09:34:10 -0400181 if (!gm)
182 trace_rpcgss_oid_to_mech(buf);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000183 return gm;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static inline int
187mech_supports_pseudoflavor(struct gss_api_mech *gm, u32 pseudoflavor)
188{
189 int i;
190
191 for (i = 0; i < gm->gm_pf_num; i++) {
192 if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
193 return 1;
194 }
195 return 0;
196}
197
Trond Myklebustc5f5e9c2013-01-12 11:17:14 -0500198static struct gss_api_mech *_gss_mech_get_by_pseudoflavor(u32 pseudoflavor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
J. Bruce Fields058c5c92011-06-22 10:50:08 -0400200 struct gss_api_mech *gm = NULL, *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400202 rcu_read_lock();
203 list_for_each_entry_rcu(pos, &registered_mechs, gm_list) {
Trond Myklebust7a9a7b72014-05-18 13:47:14 -0400204 if (!mech_supports_pseudoflavor(pos, pseudoflavor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (try_module_get(pos->gm_owner))
207 gm = pos;
208 break;
209 }
Trond Myklebust0c1c19f2018-09-29 15:14:47 -0400210 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return gm;
212}
213
J. Bruce Fields058c5c92011-06-22 10:50:08 -0400214struct gss_api_mech *
215gss_mech_get_by_pseudoflavor(u32 pseudoflavor)
216{
217 struct gss_api_mech *gm;
218
219 gm = _gss_mech_get_by_pseudoflavor(pseudoflavor);
220
221 if (!gm) {
222 request_module("rpc-auth-gss-%u", pseudoflavor);
223 gm = _gss_mech_get_by_pseudoflavor(pseudoflavor);
224 }
225 return gm;
226}
227
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400228/**
Chuck Lever83523d02013-03-16 15:55:01 -0400229 * gss_svc_to_pseudoflavor - map a GSS service number to a pseudoflavor
230 * @gm: GSS mechanism handle
231 * @qop: GSS quality-of-protection value
232 * @service: GSS service value
233 *
234 * Returns a matching security flavor, or RPC_AUTH_MAXFLAVOR if none is found.
235 */
236rpc_authflavor_t gss_svc_to_pseudoflavor(struct gss_api_mech *gm, u32 qop,
237 u32 service)
Andy Adamsonc4170583f2007-07-17 04:04:42 -0700238{
239 int i;
240
241 for (i = 0; i < gm->gm_pf_num; i++) {
Chuck Lever83523d02013-03-16 15:55:01 -0400242 if (gm->gm_pfs[i].qop == qop &&
243 gm->gm_pfs[i].service == service) {
Andy Adamsonc4170583f2007-07-17 04:04:42 -0700244 return gm->gm_pfs[i].pseudoflavor;
245 }
246 }
Chuck Lever83523d02013-03-16 15:55:01 -0400247 return RPC_AUTH_MAXFLAVOR;
Andy Adamsonc4170583f2007-07-17 04:04:42 -0700248}
Andy Adamsonc4170583f2007-07-17 04:04:42 -0700249
Chuck Lever9568c5e2013-03-16 15:54:43 -0400250/**
251 * gss_mech_info2flavor - look up a pseudoflavor given a GSS tuple
252 * @info: a GSS mech OID, quality of protection, and service value
253 *
254 * Returns a matching pseudoflavor, or RPC_AUTH_MAXFLAVOR if the tuple is
255 * not supported.
256 */
257rpc_authflavor_t gss_mech_info2flavor(struct rpcsec_gss_info *info)
258{
259 rpc_authflavor_t pseudoflavor;
260 struct gss_api_mech *gm;
261
262 gm = gss_mech_get_by_OID(&info->oid);
263 if (gm == NULL)
264 return RPC_AUTH_MAXFLAVOR;
265
Chuck Lever83523d02013-03-16 15:55:01 -0400266 pseudoflavor = gss_svc_to_pseudoflavor(gm, info->qop, info->service);
Chuck Lever9568c5e2013-03-16 15:54:43 -0400267
268 gss_mech_put(gm);
269 return pseudoflavor;
270}
271
Chuck Levera77c8062013-03-16 15:55:10 -0400272/**
273 * gss_mech_flavor2info - look up a GSS tuple for a given pseudoflavor
274 * @pseudoflavor: GSS pseudoflavor to match
275 * @info: rpcsec_gss_info structure to fill in
276 *
277 * Returns zero and fills in "info" if pseudoflavor matches a
278 * supported mechanism. Otherwise a negative errno is returned.
279 */
280int gss_mech_flavor2info(rpc_authflavor_t pseudoflavor,
281 struct rpcsec_gss_info *info)
282{
283 struct gss_api_mech *gm;
284 int i;
285
286 gm = gss_mech_get_by_pseudoflavor(pseudoflavor);
287 if (gm == NULL)
288 return -ENOENT;
289
290 for (i = 0; i < gm->gm_pf_num; i++) {
291 if (gm->gm_pfs[i].pseudoflavor == pseudoflavor) {
292 memcpy(info->oid.data, gm->gm_oid.data, gm->gm_oid.len);
293 info->oid.len = gm->gm_oid.len;
294 info->qop = gm->gm_pfs[i].qop;
295 info->service = gm->gm_pfs[i].service;
296 gss_mech_put(gm);
297 return 0;
298 }
299 }
300
301 gss_mech_put(gm);
302 return -ENOENT;
303}
Andy Adamsonc4170583f2007-07-17 04:04:42 -0700304
305u32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306gss_pseudoflavor_to_service(struct gss_api_mech *gm, u32 pseudoflavor)
307{
308 int i;
309
310 for (i = 0; i < gm->gm_pf_num; i++) {
311 if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
312 return gm->gm_pfs[i].service;
313 }
314 return 0;
315}
J. Bruce Fields0dc15312013-05-14 16:07:13 -0400316EXPORT_SYMBOL(gss_pseudoflavor_to_service);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Chuck Lever65b80172016-06-29 13:55:06 -0400318bool
319gss_pseudoflavor_to_datatouch(struct gss_api_mech *gm, u32 pseudoflavor)
320{
321 int i;
322
323 for (i = 0; i < gm->gm_pf_num; i++) {
324 if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
325 return gm->gm_pfs[i].datatouch;
326 }
327 return false;
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330char *
331gss_service_to_auth_domain_name(struct gss_api_mech *gm, u32 service)
332{
333 int i;
334
335 for (i = 0; i < gm->gm_pf_num; i++) {
336 if (gm->gm_pfs[i].service == service)
337 return gm->gm_pfs[i].auth_domain_name;
338 }
339 return NULL;
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342void
343gss_mech_put(struct gss_api_mech * gm)
344{
J. Bruce Fields1df0cad2006-06-30 01:56:16 -0700345 if (gm)
346 module_put(gm->gm_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
J. Bruce Fields0dc15312013-05-14 16:07:13 -0400348EXPORT_SYMBOL(gss_mech_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/* The mech could probably be determined from the token instead, but it's just
351 * as easy for now to pass it in. */
352int
353gss_import_sec_context(const void *input_token, size_t bufsize,
354 struct gss_api_mech *mech,
Trond Myklebust1f4c86c2010-05-13 12:51:02 -0400355 struct gss_ctx **ctx_id,
Arnd Bergmann52879b42019-11-11 21:16:21 +0100356 time64_t *endtime,
Trond Myklebust1f4c86c2010-05-13 12:51:02 -0400357 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Trond Myklebust1f4c86c2010-05-13 12:51:02 -0400359 if (!(*ctx_id = kzalloc(sizeof(**ctx_id), gfp_mask)))
Trond Myklebustb891e4a2009-12-18 16:28:12 -0500360 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 (*ctx_id)->mech_type = gss_mech_get(mech);
362
Simo Sorce400f26b2012-05-25 18:09:53 -0400363 return mech->gm_ops->gss_import_sec_context(input_token, bufsize,
364 *ctx_id, endtime, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367/* gss_get_mic: compute a mic over message and return mic_token. */
368
369u32
370gss_get_mic(struct gss_ctx *context_handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct xdr_buf *message,
372 struct xdr_netobj *mic_token)
373{
374 return context_handle->mech_type->gm_ops
375 ->gss_get_mic(context_handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 message,
377 mic_token);
378}
379
380/* gss_verify_mic: check whether the provided mic_token verifies message. */
381
382u32
383gss_verify_mic(struct gss_ctx *context_handle,
384 struct xdr_buf *message,
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400385 struct xdr_netobj *mic_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 return context_handle->mech_type->gm_ops
388 ->gss_verify_mic(context_handle,
389 message,
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400390 mic_token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Kevin Coffman75610422010-03-17 13:02:47 -0400393/*
394 * This function is called from both the client and server code.
395 * Each makes guarantees about how much "slack" space is available
396 * for the underlying function in "buf"'s head and tail while
397 * performing the wrap.
398 *
399 * The client and server code allocate RPC_MAX_AUTH_SIZE extra
400 * space in both the head and tail which is available for use by
401 * the wrap function.
402 *
403 * Underlying functions should verify they do not use more than
404 * RPC_MAX_AUTH_SIZE of extra space in either the head or tail
405 * when performing the wrap.
406 */
J. Bruce Fields293f1eb2005-10-13 16:54:37 -0400407u32
408gss_wrap(struct gss_ctx *ctx_id,
J. Bruce Fields293f1eb2005-10-13 16:54:37 -0400409 int offset,
410 struct xdr_buf *buf,
411 struct page **inpages)
412{
413 return ctx_id->mech_type->gm_ops
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400414 ->gss_wrap(ctx_id, offset, buf, inpages);
J. Bruce Fields293f1eb2005-10-13 16:54:37 -0400415}
416
417u32
418gss_unwrap(struct gss_ctx *ctx_id,
J. Bruce Fields293f1eb2005-10-13 16:54:37 -0400419 int offset,
Chuck Lever31c95902020-04-18 21:06:23 -0400420 int len,
J. Bruce Fields293f1eb2005-10-13 16:54:37 -0400421 struct xdr_buf *buf)
422{
423 return ctx_id->mech_type->gm_ops
Chuck Lever31c95902020-04-18 21:06:23 -0400424 ->gss_unwrap(ctx_id, offset, len, buf);
J. Bruce Fields293f1eb2005-10-13 16:54:37 -0400425}
426
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/* gss_delete_sec_context: free all resources associated with context_handle.
429 * Note this differs from the RFC 2744-specified prototype in that we don't
430 * bother returning an output token, since it would never be used anyway. */
431
432u32
433gss_delete_sec_context(struct gss_ctx **context_handle)
434{
Chuck Lever8885cb32007-01-31 12:14:05 -0500435 dprintk("RPC: gss_delete_sec_context deleting %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *context_handle);
437
438 if (!*context_handle)
Eric Dumazeta02cec22010-09-22 20:43:57 +0000439 return GSS_S_NO_CONTEXT;
Al Viro27724422008-03-17 22:48:03 -0700440 if ((*context_handle)->internal_ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 (*context_handle)->mech_type->gm_ops
442 ->gss_delete_sec_context((*context_handle)
443 ->internal_ctx_id);
J. Bruce Fields1df0cad2006-06-30 01:56:16 -0700444 gss_mech_put((*context_handle)->mech_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(*context_handle);
446 *context_handle=NULL;
447 return GSS_S_COMPLETE;
448}