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