Skip to content

Commit b3cbf98

Browse files
committed
SUNRPC: Support TLS handshake in the server-side TCP socket code
This patch adds opportunitistic RPC-with-TLS to the Linux in-kernel NFS server. If the client requests RPC-with-TLS and the user space handshake agent is running, the server will set up a TLS session. There are no policy settings yet. For example, the server cannot yet require the use of RPC-with-TLS to access its data. Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent 22b620e commit b3cbf98

6 files changed

Lines changed: 132 additions & 8 deletions

File tree

include/linux/sunrpc/svc_xprt.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct svc_xprt_ops {
2727
void (*xpo_detach)(struct svc_xprt *);
2828
void (*xpo_free)(struct svc_xprt *);
2929
void (*xpo_kill_temp_xprt)(struct svc_xprt *);
30-
void (*xpo_start_tls)(struct svc_xprt *);
30+
void (*xpo_handshake)(struct svc_xprt *xprt);
3131
};
3232

3333
struct svc_xprt_class {
@@ -70,6 +70,9 @@ struct svc_xprt {
7070
#define XPT_LOCAL 12 /* connection from loopback interface */
7171
#define XPT_KILL_TEMP 13 /* call xpo_kill_temp_xprt before closing */
7272
#define XPT_CONG_CTRL 14 /* has congestion control */
73+
#define XPT_HANDSHAKE 15 /* xprt requests a handshake */
74+
#define XPT_TLS_SESSION 16 /* transport-layer security established */
75+
#define XPT_PEER_AUTH 17 /* peer has been authenticated */
7376

7477
struct svc_serv *xpt_server; /* service for transport */
7578
atomic_t xpt_reserved; /* space on outq that is rsvd */

include/linux/sunrpc/svcsock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct svc_sock {
3838
/* Number of queued send requests */
3939
atomic_t sk_sendqlen;
4040

41+
struct completion sk_handshake_done;
42+
4143
struct page * sk_pages[RPCSVC_MAXPAGES]; /* received data */
4244
};
4345

include/trace/events/sunrpc.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,10 @@ TRACE_EVENT(svc_stats_latency,
18571857
{ BIT(XPT_CACHE_AUTH), "CACHE_AUTH" }, \
18581858
{ BIT(XPT_LOCAL), "LOCAL" }, \
18591859
{ BIT(XPT_KILL_TEMP), "KILL_TEMP" }, \
1860-
{ BIT(XPT_CONG_CTRL), "CONG_CTRL" })
1860+
{ BIT(XPT_CONG_CTRL), "CONG_CTRL" }, \
1861+
{ BIT(XPT_HANDSHAKE), "HANDSHAKE" }, \
1862+
{ BIT(XPT_TLS_SESSION), "TLS_SESSION" }, \
1863+
{ BIT(XPT_PEER_AUTH), "PEER_AUTH" })
18611864

18621865
TRACE_EVENT(svc_xprt_create_err,
18631866
TP_PROTO(
@@ -1990,6 +1993,17 @@ DEFINE_SVC_XPRT_EVENT(close);
19901993
DEFINE_SVC_XPRT_EVENT(detach);
19911994
DEFINE_SVC_XPRT_EVENT(free);
19921995

1996+
#define DEFINE_SVC_TLS_EVENT(name) \
1997+
DEFINE_EVENT(svc_xprt_event, svc_tls_##name, \
1998+
TP_PROTO(const struct svc_xprt *xprt), \
1999+
TP_ARGS(xprt))
2000+
2001+
DEFINE_SVC_TLS_EVENT(start);
2002+
DEFINE_SVC_TLS_EVENT(upcall);
2003+
DEFINE_SVC_TLS_EVENT(unavailable);
2004+
DEFINE_SVC_TLS_EVENT(not_started);
2005+
DEFINE_SVC_TLS_EVENT(timed_out);
2006+
19932007
TRACE_EVENT(svc_xprt_accept,
19942008
TP_PROTO(
19952009
const struct svc_xprt *xprt,

net/sunrpc/svc_xprt.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ static bool svc_xprt_ready(struct svc_xprt *xprt)
427427

428428
if (xpt_flags & BIT(XPT_BUSY))
429429
return false;
430-
if (xpt_flags & (BIT(XPT_CONN) | BIT(XPT_CLOSE)))
430+
if (xpt_flags & (BIT(XPT_CONN) | BIT(XPT_CLOSE) | BIT(XPT_HANDSHAKE)))
431431
return true;
432432
if (xpt_flags & (BIT(XPT_DATA) | BIT(XPT_DEFERRED))) {
433433
if (xprt->xpt_ops->xpo_has_wspace(xprt) &&
@@ -828,6 +828,9 @@ static int svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt)
828828
module_put(xprt->xpt_class->xcl_owner);
829829
}
830830
svc_xprt_received(xprt);
831+
} else if (test_bit(XPT_HANDSHAKE, &xprt->xpt_flags)) {
832+
xprt->xpt_ops->xpo_handshake(xprt);
833+
svc_xprt_received(xprt);
831834
} else if (svc_xprt_reserve_slot(rqstp, xprt)) {
832835
/* XPT_DATA|XPT_DEFERRED case: */
833836
dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",

net/sunrpc/svcauth_unix.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#include <net/ipv6.h>
1818
#include <linux/kernel.h>
1919
#include <linux/user_namespace.h>
20-
#define RPCDBG_FACILITY RPCDBG_AUTH
20+
#include <trace/events/sunrpc.h>
2121

22+
#define RPCDBG_FACILITY RPCDBG_AUTH
2223

2324
#include "netns.h"
2425

@@ -832,6 +833,7 @@ svcauth_tls_accept(struct svc_rqst *rqstp)
832833
{
833834
struct xdr_stream *xdr = &rqstp->rq_arg_stream;
834835
struct svc_cred *cred = &rqstp->rq_cred;
836+
struct svc_xprt *xprt = rqstp->rq_xprt;
835837
u32 flavor, len;
836838
void *body;
837839
__be32 *p;
@@ -865,14 +867,19 @@ svcauth_tls_accept(struct svc_rqst *rqstp)
865867
if (cred->cr_group_info == NULL)
866868
return SVC_CLOSE;
867869

868-
if (rqstp->rq_xprt->xpt_ops->xpo_start_tls) {
870+
if (xprt->xpt_ops->xpo_handshake) {
869871
p = xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2 + 8);
870872
if (!p)
871873
return SVC_CLOSE;
874+
trace_svc_tls_start(xprt);
872875
*p++ = rpc_auth_null;
873876
*p++ = cpu_to_be32(8);
874877
memcpy(p, "STARTTLS", 8);
878+
879+
set_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
880+
svc_xprt_enqueue(xprt);
875881
} else {
882+
trace_svc_tls_unavailable(xprt);
876883
if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream,
877884
RPC_AUTH_NULL, NULL, 0) < 0)
878885
return SVC_CLOSE;

net/sunrpc/svcsock.c

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@
4444
#include <net/tcp.h>
4545
#include <net/tcp_states.h>
4646
#include <net/tls.h>
47+
#include <net/handshake.h>
4748
#include <linux/uaccess.h>
4849
#include <linux/highmem.h>
4950
#include <asm/ioctls.h>
51+
#include <linux/key.h>
5052

5153
#include <linux/sunrpc/types.h>
5254
#include <linux/sunrpc/clnt.h>
@@ -64,6 +66,12 @@
6466

6567
#define RPCDBG_FACILITY RPCDBG_SVCXPRT
6668

69+
/* To-do: to avoid tying up an nfsd thread while waiting for a
70+
* handshake request, the request could instead be deferred.
71+
*/
72+
enum {
73+
SVC_HANDSHAKE_TO = 5U * HZ
74+
};
6775

6876
static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
6977
int flags);
@@ -359,6 +367,8 @@ static void svc_data_ready(struct sock *sk)
359367
rmb();
360368
svsk->sk_odata(sk);
361369
trace_svcsock_data_ready(&svsk->sk_xprt, 0);
370+
if (test_bit(XPT_HANDSHAKE, &svsk->sk_xprt.xpt_flags))
371+
return;
362372
if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
363373
svc_xprt_enqueue(&svsk->sk_xprt);
364374
}
@@ -396,6 +406,88 @@ static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
396406
sock_no_linger(svsk->sk_sock->sk);
397407
}
398408

409+
/**
410+
* svc_tcp_handshake_done - Handshake completion handler
411+
* @data: address of xprt to wake
412+
* @status: status of handshake
413+
* @peerid: serial number of key containing the remote peer's identity
414+
*
415+
* If a security policy is specified as an export option, we don't
416+
* have a specific export here to check. So we set a "TLS session
417+
* is present" flag on the xprt and let an upper layer enforce local
418+
* security policy.
419+
*/
420+
static void svc_tcp_handshake_done(void *data, int status, key_serial_t peerid)
421+
{
422+
struct svc_xprt *xprt = data;
423+
struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
424+
425+
if (!status) {
426+
if (peerid != TLS_NO_PEERID)
427+
set_bit(XPT_PEER_AUTH, &xprt->xpt_flags);
428+
set_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
429+
}
430+
clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
431+
complete_all(&svsk->sk_handshake_done);
432+
}
433+
434+
/**
435+
* svc_tcp_handshake - Perform a transport-layer security handshake
436+
* @xprt: connected transport endpoint
437+
*
438+
*/
439+
static void svc_tcp_handshake(struct svc_xprt *xprt)
440+
{
441+
struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
442+
struct sock *sk = svsk->sk_sock->sk;
443+
struct tls_handshake_args args = {
444+
.ta_sock = svsk->sk_sock,
445+
.ta_done = svc_tcp_handshake_done,
446+
.ta_data = xprt,
447+
};
448+
int ret;
449+
450+
trace_svc_tls_upcall(xprt);
451+
452+
clear_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
453+
init_completion(&svsk->sk_handshake_done);
454+
455+
ret = tls_server_hello_x509(&args, GFP_KERNEL);
456+
if (ret) {
457+
trace_svc_tls_not_started(xprt);
458+
goto out_failed;
459+
}
460+
461+
ret = wait_for_completion_interruptible_timeout(&svsk->sk_handshake_done,
462+
SVC_HANDSHAKE_TO);
463+
if (ret <= 0) {
464+
if (tls_handshake_cancel(sk)) {
465+
trace_svc_tls_timed_out(xprt);
466+
goto out_close;
467+
}
468+
}
469+
470+
if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags)) {
471+
trace_svc_tls_unavailable(xprt);
472+
goto out_close;
473+
}
474+
475+
/* Mark the transport ready in case the remote sent RPC
476+
* traffic before the kernel received the handshake
477+
* completion downcall.
478+
*/
479+
set_bit(XPT_DATA, &xprt->xpt_flags);
480+
svc_xprt_enqueue(xprt);
481+
return;
482+
483+
out_close:
484+
set_bit(XPT_CLOSE, &xprt->xpt_flags);
485+
out_failed:
486+
clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
487+
set_bit(XPT_DATA, &xprt->xpt_flags);
488+
svc_xprt_enqueue(xprt);
489+
}
490+
399491
/*
400492
* See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
401493
*/
@@ -1257,6 +1349,7 @@ static const struct svc_xprt_ops svc_tcp_ops = {
12571349
.xpo_has_wspace = svc_tcp_has_wspace,
12581350
.xpo_accept = svc_tcp_accept,
12591351
.xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
1352+
.xpo_handshake = svc_tcp_handshake,
12601353
};
12611354

12621355
static struct svc_xprt_class svc_tcp_class = {
@@ -1580,10 +1673,12 @@ static void svc_tcp_sock_detach(struct svc_xprt *xprt)
15801673
static void svc_sock_free(struct svc_xprt *xprt)
15811674
{
15821675
struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1676+
struct socket *sock = svsk->sk_sock;
15831677

1584-
if (svsk->sk_sock->file)
1585-
sockfd_put(svsk->sk_sock);
1678+
tls_handshake_cancel(sock->sk);
1679+
if (sock->file)
1680+
sockfd_put(sock);
15861681
else
1587-
sock_release(svsk->sk_sock);
1682+
sock_release(sock);
15881683
kfree(svsk);
15891684
}

0 commit comments

Comments
 (0)