xref: /freebsd/sys/kgssapi/gss_impl.c (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5  * Authors: Doug Rabson <dfr@rabson.org>
6  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/jail.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/syscall.h>
44 #include <sys/sysent.h>
45 #include <sys/sysproto.h>
46 
47 #include <kgssapi/gssapi.h>
48 #include <kgssapi/gssapi_impl.h>
49 #include <rpc/rpc.h>
50 #include <rpc/rpc_com.h>
51 #include <rpc/rpcsec_gss.h>
52 
53 #include "gssd.h"
54 #include "kgss_if.h"
55 
56 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
57 
58 /*
59  * Syscall hooks
60  */
61 static struct syscall_helper_data gssd_syscalls[] = {
62 	SYSCALL_INIT_HELPER(gssd_syscall),
63 	SYSCALL_INIT_LAST
64 };
65 
66 struct kgss_mech_list kgss_mechs;
67 struct mtx kgss_gssd_lock;
68 
69 KGSS_VNET_DEFINE(CLIENT *, kgss_gssd_handle) = NULL;
70 
71 static int
72 kgss_load(void)
73 {
74 	int error;
75 
76 	LIST_INIT(&kgss_mechs);
77 	error = syscall_helper_register(gssd_syscalls, SY_THR_STATIC_KLD);
78 	if (error != 0)
79 		return (error);
80 	return (0);
81 }
82 
83 static void
84 kgss_unload(void)
85 {
86 
87 	syscall_helper_unregister(gssd_syscalls);
88 }
89 
90 int
91 sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap)
92 {
93         struct sockaddr_un sun;
94         struct netconfig *nconf;
95 	char path[MAXPATHLEN];
96 	int error;
97 	CLIENT *cl, *oldcl;
98 
99 	error = priv_check(td, PRIV_NFS_DAEMON);
100 	if (error)
101 		return (error);
102 
103 	error = copyinstr(uap->path, path, sizeof(path), NULL);
104 	if (error)
105 		return (error);
106 	if (strlen(path) + 1 > sizeof(sun.sun_path))
107 		return (EINVAL);
108 
109 	if (path[0] != '\0') {
110 		sun.sun_family = AF_LOCAL;
111 		strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
112 		sun.sun_len = SUN_LEN(&sun);
113 
114 		nconf = getnetconfigent("local");
115 		cl = clnt_reconnect_create(nconf,
116 		    (struct sockaddr *) &sun, GSSD, GSSDVERS,
117 		    RPC_MAXDATASIZE, RPC_MAXDATASIZE);
118 		/*
119 		 * The number of retries defaults to INT_MAX, which effectively
120 		 * means an infinite, uninterruptable loop.  Limiting it to
121 		 * five retries keeps it from running forever.
122 		 */
123 		if (cl != NULL) {
124 			int retry_count = 5;
125 			struct timeval timo;
126 			CLNT_CONTROL(cl, CLSET_RETRIES, &retry_count);
127 
128 			/*
129 			 * Set the timeout for an upcall to 5 minutes.  The
130 			 * default of 25 seconds is not long enough for some
131 			 * gss_XXX() calls done by the gssd(8) daemon.
132 			 */
133 			timo.tv_sec = 5 * 60;
134 			timo.tv_usec = 0;
135 			CLNT_CONTROL(cl, CLSET_TIMEOUT, &timo);
136 		}
137 	} else
138 		cl = NULL;
139 
140 	KGSS_CURVNET_SET_QUIET(KGSS_TD_TO_VNET(curthread));
141 	mtx_lock(&kgss_gssd_lock);
142 	oldcl = KGSS_VNET(kgss_gssd_handle);
143 	KGSS_VNET(kgss_gssd_handle) = cl;
144 	mtx_unlock(&kgss_gssd_lock);
145 	KGSS_CURVNET_RESTORE();
146 
147 	if (oldcl != NULL) {
148 		CLNT_CLOSE(oldcl);
149 		CLNT_RELEASE(oldcl);
150 	}
151 
152 	return (0);
153 }
154 
155 int
156 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
157 {
158 
159 	if (oid1 == oid2)
160 		return (1);
161 	if (!oid1 || !oid2)
162 		return (0);
163 	if (oid1->length != oid2->length)
164 		return (0);
165 	if (memcmp(oid1->elements, oid2->elements, oid1->length))
166 		return (0);
167 	return (1);
168 }
169 
170 void
171 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
172 {
173 	struct kgss_mech *km;
174 
175 	km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
176 	km->km_mech_type = mech_type;
177 	km->km_mech_name = name;
178 	km->km_class = cls;
179 	LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
180 }
181 
182 void
183 kgss_uninstall_mech(gss_OID mech_type)
184 {
185 	struct kgss_mech *km;
186 
187 	LIST_FOREACH(km, &kgss_mechs, km_link) {
188 		if (kgss_oid_equal(km->km_mech_type, mech_type)) {
189 			LIST_REMOVE(km, km_link);
190 			free(km, M_GSSAPI);
191 			return;
192 		}
193 	}
194 }
195 
196 gss_OID
197 kgss_find_mech_by_name(const char *name)
198 {
199 	struct kgss_mech *km;
200 
201 	LIST_FOREACH(km, &kgss_mechs, km_link) {
202 		if (!strcmp(km->km_mech_name, name)) {
203 			return (km->km_mech_type);
204 		}
205 	}
206 	return (GSS_C_NO_OID);
207 }
208 
209 const char *
210 kgss_find_mech_by_oid(const gss_OID oid)
211 {
212 	struct kgss_mech *km;
213 
214 	LIST_FOREACH(km, &kgss_mechs, km_link) {
215 		if (kgss_oid_equal(km->km_mech_type, oid)) {
216 			return (km->km_mech_name);
217 		}
218 	}
219 	return (NULL);
220 }
221 
222 gss_ctx_id_t
223 kgss_create_context(gss_OID mech_type)
224 {
225 	struct kgss_mech *km;
226 	gss_ctx_id_t ctx;
227 
228 	LIST_FOREACH(km, &kgss_mechs, km_link) {
229 		if (kgss_oid_equal(km->km_mech_type, mech_type))
230 			break;
231 	}
232 	if (!km)
233 		return (NULL);
234 
235 	ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
236 	KGSS_INIT(ctx);
237 
238 	return (ctx);
239 }
240 
241 void
242 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
243 {
244 
245 	KGSS_DELETE(ctx, output_token);
246 	kobj_delete((kobj_t) ctx, M_GSSAPI);
247 }
248 
249 OM_uint32
250 kgss_transfer_context(gss_ctx_id_t ctx)
251 {
252 	struct export_sec_context_res res;
253 	struct export_sec_context_args args;
254 	enum clnt_stat stat;
255 	OM_uint32 maj_stat;
256 
257 	KGSS_CURVNET_SET_QUIET(KGSS_TD_TO_VNET(curthread));
258 	if (!KGSS_VNET(kgss_gssd_handle)) {
259 		KGSS_CURVNET_RESTORE();
260 		return (GSS_S_FAILURE);
261 	}
262 
263 	args.ctx = ctx->handle;
264 	bzero(&res, sizeof(res));
265 	stat = gssd_export_sec_context_1(&args, &res, KGSS_VNET(kgss_gssd_handle));
266 	KGSS_CURVNET_RESTORE();
267 	if (stat != RPC_SUCCESS) {
268 		return (GSS_S_FAILURE);
269 	}
270 
271 	maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
272 	ctx->handle = 0;
273 
274 	xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
275 
276 	return (maj_stat);
277 }
278 
279 void
280 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
281 {
282 	to->length = from->length;
283 	if (from->length) {
284 		to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
285 		bcopy(from->value, to->value, from->length);
286 	} else {
287 		to->value = NULL;
288 	}
289 }
290 
291 /*
292  * Acquire the kgss_gssd_handle and return it with a reference count,
293  * if it is available.
294  */
295 CLIENT *
296 kgss_gssd_client(void)
297 {
298 	CLIENT *cl;
299 
300 	KGSS_CURVNET_SET_QUIET(KGSS_TD_TO_VNET(curthread));
301 	mtx_lock(&kgss_gssd_lock);
302 	cl = KGSS_VNET(kgss_gssd_handle);
303 	if (cl != NULL)
304 		CLNT_ACQUIRE(cl);
305 	mtx_unlock(&kgss_gssd_lock);
306 	KGSS_CURVNET_RESTORE();
307 	return (cl);
308 }
309 
310 /*
311  * Kernel module glue
312  */
313 static int
314 kgssapi_modevent(module_t mod, int type, void *data)
315 {
316 	int error = 0;
317 
318 	switch (type) {
319 	case MOD_LOAD:
320 		rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
321 		rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
322 		rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
323 		rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
324 		rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
325 		rpc_gss_entries.rpc_gss_max_data_length =
326 		    rpc_gss_max_data_length;
327 		rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
328 		rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
329 		rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
330 		rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
331 		rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
332 		rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
333 		rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
334 		rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
335 		rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
336 		rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
337 		rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
338 		rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
339 		rpc_gss_entries.rpc_gss_get_principal_name =
340 		    rpc_gss_get_principal_name;
341 		rpc_gss_entries.rpc_gss_svc_max_data_length =
342 		    rpc_gss_svc_max_data_length;
343 		mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
344 		error = kgss_load();
345 		break;
346 	case MOD_UNLOAD:
347 		kgss_unload();
348 		mtx_destroy(&kgss_gssd_lock);
349 		/*
350 		 * Unloading of the kgssapi module is not currently supported.
351 		 * If somebody wants this, we would need to keep track of
352 		 * currently executing threads and make sure the count is 0.
353 		 */
354 		/* FALLTHROUGH */
355 	default:
356 		error = EOPNOTSUPP;
357 	}
358 	return (error);
359 }
360 static moduledata_t kgssapi_mod = {
361 	"kgssapi",
362 	kgssapi_modevent,
363 	NULL,
364 };
365 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
366 MODULE_DEPEND(kgssapi, xdr, 1, 1, 1);
367 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
368 MODULE_VERSION(kgssapi, 1);
369