xref: /freebsd/sys/rpc/rpcsec_tls/rpctls_impl.c (revision 96104caaf00ef48d43c93e95f04ec4de01ee1550)
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 /* Modified from the kernel GSSAPI code for RPC-over-TLS. */
31 
32 #include <sys/cdefs.h>
33 #include "opt_kern_tls.h"
34 
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/mutex.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/socketvar.h>
48 #include <sys/syscall.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysent.h>
51 #include <sys/sysproto.h>
52 #include <sys/tree.h>
53 
54 #include <net/vnet.h>
55 
56 #include <rpc/rpc.h>
57 #include <rpc/rpc_com.h>
58 #include <rpc/krpc.h>
59 #include <rpc/rpcsec_tls.h>
60 
61 #include <vm/vm.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_param.h>
64 
65 #include "rpctlscd.h"
66 #include "rpctlssd.h"
67 
68 /*
69  * Syscall hooks
70  */
71 static struct syscall_helper_data rpctls_syscalls[] = {
72 	SYSCALL_INIT_HELPER(rpctls_syscall),
73 	SYSCALL_INIT_LAST
74 };
75 
76 static struct opaque_auth rpctls_null_verf;
77 
78 KRPC_VNET_DECLARE(uint64_t, svc_vc_tls_handshake_success);
79 KRPC_VNET_DECLARE(uint64_t, svc_vc_tls_handshake_failed);
80 
81 static CLIENT *rpctls_connect_handle;
82 static CLIENT *rpctls_server_handle;
83 
84 struct upsock {
85 	RB_ENTRY(upsock) tree;
86 	struct socket *so;
87 	union {
88 		CLIENT *cl;
89 		SVCXPRT *xp;
90 	};
91 	bool server;
92 };
93 
94 static RB_HEAD(upsock_t, upsock) upcall_sockets;
95 static intptr_t
upsock_compare(const struct upsock * a,const struct upsock * b)96 upsock_compare(const struct upsock *a, const struct upsock *b)
97 {
98 	return ((intptr_t)((uintptr_t)a->so/2 - (uintptr_t)b->so/2));
99 }
100 RB_GENERATE_STATIC(upsock_t, upsock, tree, upsock_compare);
101 static struct mtx rpctls_lock;
102 
103 static enum clnt_stat	rpctls_server(SVCXPRT *xprt, uint32_t *flags,
104     uid_t *uid, int *ngrps, gid_t **gids);
105 
106 static CLIENT *
rpctls_client_nl_create(const char * group,const rpcprog_t program,const rpcvers_t version)107 rpctls_client_nl_create(const char *group, const rpcprog_t program,
108     const rpcvers_t version)
109 {
110 	CLIENT *cl;
111 
112 	cl = client_nl_create(group, program, version);
113 	KASSERT(cl, ("%s: netlink client already exist", __func__));
114 	/*
115 	 * Set the try_count to 1 so that no retries of the RPC occur.  Since
116 	 * it is an upcall to a local daemon, requests should not be lost and
117 	 * doing one of these RPCs multiple times is not correct.  If the
118 	 * server is not working correctly, the daemon can get stuck in
119 	 * SSL_connect() trying to read data from the socket during the upcall.
120 	 * Set a timeout (currently 15sec) and assume the daemon is hung when
121 	 *  the timeout occurs.
122 	 */
123 	clnt_control(cl, CLSET_RETRIES, &(int){1});
124 	clnt_control(cl, CLSET_TIMEOUT, &(struct timeval){.tv_sec = 15});
125 	clnt_control(cl, CLSET_WAITCHAN, __DECONST(char *, group));
126 
127 	return (cl);
128 }
129 
130 int
rpctls_init(void)131 rpctls_init(void)
132 {
133 	int error;
134 
135 	error = syscall_helper_register(rpctls_syscalls, SY_THR_STATIC_KLD);
136 	if (error != 0) {
137 		printf("rpctls_init: cannot register syscall\n");
138 		return (error);
139 	}
140 	mtx_init(&rpctls_lock, "rpctls lock", NULL, MTX_DEF);
141 	rpctls_null_verf.oa_flavor = AUTH_NULL;
142 	rpctls_null_verf.oa_base = RPCTLS_START_STRING;
143 	rpctls_null_verf.oa_length = strlen(RPCTLS_START_STRING);
144 	rpctls_connect_handle = rpctls_client_nl_create("tlsclnt",
145 	    RPCTLSCD, RPCTLSCDVERS);
146 	rpctls_server_handle = rpctls_client_nl_create("tlsserv",
147 	    RPCTLSSD, RPCTLSSDVERS);
148 	return (0);
149 }
150 
151 int
sys_rpctls_syscall(struct thread * td,struct rpctls_syscall_args * uap)152 sys_rpctls_syscall(struct thread *td, struct rpctls_syscall_args *uap)
153 {
154 	struct file *fp;
155 	struct upsock *upsp, ups;
156 	int fd = -1, error;
157 
158 	error = priv_check(td, PRIV_NFS_DAEMON);
159 	if (error != 0)
160 		return (error);
161 
162 	KRPC_CURVNET_SET(KRPC_TD_TO_VNET(td));
163 	mtx_lock(&rpctls_lock);
164 	upsp = RB_FIND(upsock_t, &upcall_sockets,
165 	    &(struct upsock){
166 	    .so = __DECONST(struct socket *, uap->socookie) });
167 	if (__predict_true(upsp != NULL)) {
168 		RB_REMOVE(upsock_t, &upcall_sockets, upsp);
169 		/*
170 		 * The upsp points to stack of NFS mounting thread.  Even
171 		 * though we removed it from the tree, we still don't own it.
172 		 * Make a copy before releasing the lock.  The mounting thread
173 		 * may timeout the RPC and unroll its stack.
174 		 */
175 		ups = *upsp;
176 	}
177 	mtx_unlock(&rpctls_lock);
178 	if (upsp == NULL) {
179 		KRPC_CURVNET_RESTORE();
180 		printf("%s: socket lookup failed\n", __func__);
181 		return (EPERM);
182 	}
183 	if ((error = falloc(td, &fp, &fd, 0)) != 0) {
184 		KRPC_CURVNET_RESTORE();
185 		return (error);
186 	}
187 	soref(ups.so);
188 	if (ups.server) {
189 		/*
190 		 * Once this file descriptor is associated
191 		 * with the socket, it cannot be closed by
192 		 * the server side krpc code (svc_vc.c).
193 		 */
194 		sx_xlock(&ups.xp->xp_lock);
195 		ups.xp->xp_tls = RPCTLS_FLAGS_HANDSHFAIL;
196 		sx_xunlock(&ups.xp->xp_lock);
197 	} else {
198 		/*
199 		 * Initialize TLS state so that clnt_vc_destroy() will
200 		 * not close the socket and will leave that for the
201 		 * daemon to do.
202 		 */
203 		CLNT_CONTROL(ups.cl, CLSET_TLS, &(int){RPCTLS_INHANDSHAKE});
204 	}
205 	finit(fp, FREAD | FWRITE, DTYPE_SOCKET, ups.so, &socketops);
206 	fdrop(fp, td);	/* Drop fp reference. */
207 	td->td_retval[0] = fd;
208 	KRPC_CURVNET_RESTORE();
209 
210 	return (error);
211 }
212 
213 /* Error handling for both client and server failed RPC upcalls. */
214 static void
rpctls_rpc_failed(struct upsock * ups,struct socket * so)215 rpctls_rpc_failed(struct upsock *ups, struct socket *so)
216 {
217 
218 	mtx_lock(&rpctls_lock);
219 	if (RB_FIND(upsock_t, &upcall_sockets, ups)) {
220 		struct upsock *removed __diagused;
221 
222 		removed = RB_REMOVE(upsock_t, &upcall_sockets, ups);
223 		mtx_unlock(&rpctls_lock);
224 		MPASS(removed == ups);
225 		/*
226 		 * Do a shutdown on the socket, since the daemon is
227 		 * probably stuck in SSL_accept() trying to read the
228 		 * socket.  Do not soclose() the socket, since the
229 		 * daemon will close() the socket after SSL_accept()
230 		 * returns an error.
231 		 */
232 		soshutdown(so, SHUT_RD);
233 	} else {
234 		/*
235 		 * The daemon has taken the socket from the tree, but
236 		 * failed to do the handshake.
237 		 */
238 		mtx_unlock(&rpctls_lock);
239 	}
240 }
241 
242 /* Do an upcall for a new socket connect using TLS. */
243 enum clnt_stat
rpctls_connect(CLIENT * newclient,char * certname,struct socket * so,uint32_t * reterr)244 rpctls_connect(CLIENT *newclient, char *certname, struct socket *so,
245     uint32_t *reterr)
246 {
247 	struct rpctlscd_connect_arg arg;
248 	struct rpctlscd_connect_res res;
249 	struct rpc_callextra ext;
250 	enum clnt_stat stat;
251 	struct upsock ups = {
252 		.so = so,
253 		.cl = newclient,
254 		.server = false,
255 	};
256 
257 	/* First, do the AUTH_TLS NULL RPC. */
258 	memset(&ext, 0, sizeof(ext));
259 	ext.rc_auth = authtls_create();
260 	stat = clnt_call_private(newclient, &ext, NULLPROC, (xdrproc_t)xdr_void,
261 	    NULL, (xdrproc_t)xdr_void, NULL, (struct timeval){ .tv_sec = 30 });
262 	AUTH_DESTROY(ext.rc_auth);
263 	if (stat == RPC_AUTHERROR)
264 		return (stat);
265 	if (stat != RPC_SUCCESS)
266 		return (RPC_SYSTEMERROR);
267 
268 	mtx_lock(&rpctls_lock);
269 	RB_INSERT(upsock_t, &upcall_sockets, &ups);
270 	mtx_unlock(&rpctls_lock);
271 
272 	/* Temporarily block reception during the handshake upcall. */
273 	CLNT_CONTROL(newclient, CLSET_BLOCKRCV, &(int){1});
274 
275 	/* Do the connect handshake upcall. */
276 	if (certname != NULL) {
277 		arg.certname.certname_len = strlen(certname);
278 		arg.certname.certname_val = certname;
279 	} else
280 		arg.certname.certname_len = 0;
281 	arg.socookie = (uint64_t)so;
282 	stat = rpctlscd_connect_2(&arg, &res, rpctls_connect_handle);
283 	if (stat == RPC_SUCCESS)
284 		*reterr = res.reterr;
285 	else
286 		rpctls_rpc_failed(&ups, so);
287 
288 	/* Unblock reception. */
289 	CLNT_CONTROL(newclient, CLSET_BLOCKRCV, &(int){0});
290 
291 #ifdef INVARIANTS
292 	mtx_lock(&rpctls_lock);
293 	MPASS((RB_FIND(upsock_t, &upcall_sockets, &ups) == NULL));
294 	mtx_unlock(&rpctls_lock);
295 #endif
296 
297 	return (stat);
298 }
299 
300 /* Do an upcall to handle an non-application data record using TLS. */
301 enum clnt_stat
rpctls_cl_handlerecord(void * socookie,uint32_t * reterr)302 rpctls_cl_handlerecord(void *socookie, uint32_t *reterr)
303 {
304 	struct rpctlscd_handlerecord_arg arg;
305 	struct rpctlscd_handlerecord_res res;
306 	enum clnt_stat stat;
307 
308 	/* Do the handlerecord upcall. */
309 	arg.socookie = (uint64_t)socookie;
310 	stat = rpctlscd_handlerecord_2(&arg, &res, rpctls_connect_handle);
311 	if (stat == RPC_SUCCESS)
312 		*reterr = res.reterr;
313 	return (stat);
314 }
315 
316 enum clnt_stat
rpctls_srv_handlerecord(void * socookie,uint32_t * reterr)317 rpctls_srv_handlerecord(void *socookie, uint32_t *reterr)
318 {
319 	struct rpctlssd_handlerecord_arg arg;
320 	struct rpctlssd_handlerecord_res res;
321 	enum clnt_stat stat;
322 
323 	/* Do the handlerecord upcall. */
324 	arg.socookie = (uint64_t)socookie;
325 	stat = rpctlssd_handlerecord_2(&arg, &res, rpctls_server_handle);
326 	if (stat == RPC_SUCCESS)
327 		*reterr = res.reterr;
328 	return (stat);
329 }
330 
331 /* Do an upcall to shut down a socket using TLS. */
332 enum clnt_stat
rpctls_cl_disconnect(void * socookie,uint32_t * reterr)333 rpctls_cl_disconnect(void *socookie, uint32_t *reterr)
334 {
335 	struct rpctlscd_disconnect_arg arg;
336 	struct rpctlscd_disconnect_res res;
337 	enum clnt_stat stat;
338 
339 	/* Do the disconnect upcall. */
340 	arg.socookie = (uint64_t)socookie;
341 	stat = rpctlscd_disconnect_2(&arg, &res, rpctls_connect_handle);
342 	if (stat == RPC_SUCCESS)
343 		*reterr = res.reterr;
344 	return (stat);
345 }
346 
347 enum clnt_stat
rpctls_srv_disconnect(void * socookie,uint32_t * reterr)348 rpctls_srv_disconnect(void *socookie, uint32_t *reterr)
349 {
350 	struct rpctlssd_disconnect_arg arg;
351 	struct rpctlssd_disconnect_res res;
352 	enum clnt_stat stat;
353 
354 	/* Do the disconnect upcall. */
355 	arg.socookie = (uint64_t)socookie;
356 	stat = rpctlssd_disconnect_2(&arg, &res, rpctls_server_handle);
357 	if (stat == RPC_SUCCESS)
358 		*reterr = res.reterr;
359 	return (stat);
360 }
361 
362 /* Do an upcall for a new server socket using TLS. */
363 static enum clnt_stat
rpctls_server(SVCXPRT * xprt,uint32_t * flags,uid_t * uid,int * ngrps,gid_t ** gids)364 rpctls_server(SVCXPRT *xprt, uint32_t *flags, uid_t *uid, int *ngrps,
365     gid_t **gids)
366 {
367 	enum clnt_stat stat;
368 	struct upsock ups = {
369 		.so = xprt->xp_socket,
370 		.xp = xprt,
371 		.server = true,
372 	};
373 	struct rpctlssd_connect_arg arg;
374 	struct rpctlssd_connect_res res;
375 	gid_t *gidp;
376 	uint32_t *gidv;
377 	int i;
378 
379 	mtx_lock(&rpctls_lock);
380 	RB_INSERT(upsock_t, &upcall_sockets, &ups);
381 	mtx_unlock(&rpctls_lock);
382 
383 	/* Do the server upcall. */
384 	res.gid.gid_val = NULL;
385 	arg.socookie = (uint64_t)xprt->xp_socket;
386 	stat = rpctlssd_connect_2(&arg, &res, rpctls_server_handle);
387 	if (stat == RPC_SUCCESS) {
388 		*flags = res.flags;
389 		if ((*flags & (RPCTLS_FLAGS_CERTUSER |
390 		    RPCTLS_FLAGS_DISABLED)) == RPCTLS_FLAGS_CERTUSER) {
391 			*ngrps = res.gid.gid_len;
392 			*uid = res.uid;
393 			*gids = gidp = mem_alloc(*ngrps * sizeof(gid_t));
394 			gidv = res.gid.gid_val;
395 			for (i = 0; i < *ngrps; i++)
396 				*gidp++ = *gidv++;
397 		}
398 	} else
399 		rpctls_rpc_failed(&ups, xprt->xp_socket);
400 
401 	mem_free(res.gid.gid_val, 0);
402 
403 #ifdef INVARIANTS
404 	mtx_lock(&rpctls_lock);
405 	MPASS((RB_FIND(upsock_t, &upcall_sockets, &ups) == NULL));
406 	mtx_unlock(&rpctls_lock);
407 #endif
408 
409 	return (stat);
410 }
411 
412 /*
413  * Handle the NULL RPC with authentication flavor of AUTH_TLS.
414  * This is a STARTTLS command, so do the upcall to the rpctlssd daemon,
415  * which will do the TLS handshake.
416  */
417 enum auth_stat
_svcauth_rpcsec_tls(struct svc_req * rqst,struct rpc_msg * msg)418 _svcauth_rpcsec_tls(struct svc_req *rqst, struct rpc_msg *msg)
419 
420 {
421 	bool_t call_stat;
422 	enum clnt_stat stat;
423 	SVCXPRT *xprt;
424 	uint32_t flags;
425 	int ngrps;
426 	uid_t uid;
427 	gid_t *gidp;
428 #ifdef KERN_TLS
429 	u_int maxlen;
430 #endif
431 
432 	KRPC_CURVNET_SET_QUIET(KRPC_TD_TO_VNET(curthread));
433 	KRPC_VNET(svc_vc_tls_handshake_failed)++;
434 	/* Initialize reply. */
435 	rqst->rq_verf = rpctls_null_verf;
436 
437 	/* Check client credentials. */
438 	if (rqst->rq_cred.oa_length != 0 ||
439 	    msg->rm_call.cb_verf.oa_length != 0 ||
440 	    msg->rm_call.cb_verf.oa_flavor != AUTH_NULL) {
441 		KRPC_CURVNET_RESTORE();
442 		return (AUTH_BADCRED);
443 	}
444 
445 	if (rqst->rq_proc != NULLPROC) {
446 		KRPC_CURVNET_RESTORE();
447 		return (AUTH_REJECTEDCRED);
448 	}
449 
450 	call_stat = FALSE;
451 #ifdef KERN_TLS
452 	if (rpctls_getinfo(&maxlen, false, true))
453 		call_stat = TRUE;
454 #endif
455 	if (!call_stat) {
456 		KRPC_CURVNET_RESTORE();
457 		return (AUTH_REJECTEDCRED);
458 	}
459 
460 	/*
461 	 * Disable reception for the krpc so that the TLS handshake can
462 	 * be done on the socket in the rpctlssd daemon.
463 	 */
464 	xprt = rqst->rq_xprt;
465 	sx_xlock(&xprt->xp_lock);
466 	xprt->xp_dontrcv = TRUE;
467 	sx_xunlock(&xprt->xp_lock);
468 
469 	/*
470 	 * Send the reply to the NULL RPC with AUTH_TLS, which is the
471 	 * STARTTLS command for Sun RPC.
472 	 */
473 	call_stat = svc_sendreply(rqst, (xdrproc_t)xdr_void, NULL);
474 	if (!call_stat) {
475 		sx_xlock(&xprt->xp_lock);
476 		xprt->xp_dontrcv = FALSE;
477 		sx_xunlock(&xprt->xp_lock);
478 		xprt_active(xprt);	/* Harmless if already active. */
479 		KRPC_CURVNET_RESTORE();
480 		return (AUTH_REJECTEDCRED);
481 	}
482 
483 	/* Do an upcall to do the TLS handshake. */
484 	stat = rpctls_server(xprt, &flags, &uid, &ngrps, &gidp);
485 
486 	/* Re-enable reception on the socket within the krpc. */
487 	sx_xlock(&xprt->xp_lock);
488 	xprt->xp_dontrcv = FALSE;
489 	if (stat == RPC_SUCCESS) {
490 		xprt->xp_tls = flags;
491 		if ((flags & (RPCTLS_FLAGS_CERTUSER |
492 		    RPCTLS_FLAGS_DISABLED)) == RPCTLS_FLAGS_CERTUSER) {
493 			xprt->xp_ngrps = ngrps;
494 			xprt->xp_uid = uid;
495 			xprt->xp_gidp = gidp;
496 		}
497 		KRPC_VNET(svc_vc_tls_handshake_failed)--;
498 		KRPC_VNET(svc_vc_tls_handshake_success)++;
499 	}
500 	sx_xunlock(&xprt->xp_lock);
501 	xprt_active(xprt);		/* Harmless if already active. */
502 	KRPC_CURVNET_RESTORE();
503 
504 	return (RPCSEC_GSS_NODISPATCH);
505 }
506 
507 /*
508  * Get kern.ipc.tls.enable and kern.ipc.tls.maxlen.
509  */
510 bool
rpctls_getinfo(u_int * maxlenp,bool rpctlscd_run,bool rpctlssd_run)511 rpctls_getinfo(u_int *maxlenp, bool rpctlscd_run, bool rpctlssd_run)
512 {
513 	u_int maxlen;
514 	bool enable;
515 	int error;
516 	size_t siz;
517 
518 	if (!mb_use_ext_pgs)
519 		return (false);
520 	siz = sizeof(enable);
521 	error = kernel_sysctlbyname(curthread, "kern.ipc.tls.enable",
522 	    &enable, &siz, NULL, 0, NULL, 0);
523 	if (error != 0)
524 		return (false);
525 	siz = sizeof(maxlen);
526 	error = kernel_sysctlbyname(curthread, "kern.ipc.tls.maxlen",
527 	    &maxlen, &siz, NULL, 0, NULL, 0);
528 	if (error != 0)
529 		return (false);
530 	*maxlenp = maxlen;
531 	return (enable);
532 }
533