Skip to contentSkip to conversation

sunrpc: refactor TLS transport to remove rpc_clnt dependency

RFC PATCH In review Benjamin Coddington 2 participants 3 messages Mar 11 – Mar 12

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 sunrpc: refactor TLS transport to remove rpc_clnt dependency

The TLS connection worker (xs_tcp_tls_setup_socket) currently requires a reference to the upper-layer rpc_clnt to populate arguments for rpc_create() when setting up the lower-layer transport for the RPC_AUTH_TLS probe. This dependency led to lifetime issues and a use-after-free (UAF) of the client's credentials if the task finished before the worker ran addressed previously in linux-nfs upstream posting:
https://lore.kernel.org/linux-nfs/[email protected]/

However, it is architecturally cleaner to decouple the transport connection logic from the RPC client entirely. The TLS probe requires the upper-layer's RPC program and version to facilitate the probe.

Refactor the TLS transport setup to:
- Remove the clnt member from struct sock_xprt.
- Save the RPC program number and version from the task during xs_connect() into the sock_xprt structure.
- Update xs_tcp_tls_setup_socket to use these saved parameters for lower-layer client creation in dummy program definitiions.
- Update TLS-related tracepoints to remove the rpc_clnt dependency.
- Remove the rpc_clnt refcounting and assignment in xs_connect.

This simplifies state management and eliminates the risk of UAF without requiring the transport to pin the upper-layer client.

---

I'm not sure I like what came out here. It adds a lot of boilerplate, and it reduces the tracepoint data (pretty necessary due to the race, however).

Most of the fix is from just not setting cl_cred - it stays NULL. Probably the other values we get from the upper rpc_clnt are still hanging around.

We could reduce the dummy definition boilerplate by passing the rpc_program pointer, but then there's a potential null-dref if the calling module gets unloaded.

Can RCU the upper rpc_clnt? Any other ideas?

This is lightly tested by hand..

Ben

---

 include/linux/sunrpc/xprtsock.h |  3 +-
 include/trace/events/sunrpc.h   | 16 +++------
 net/sunrpc/xprtsock.c           | 59 +++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 23 deletions(-)
include/linux/sunrpc/xprtsock.h +2 -1
index 700a1e6c047c..7cafc3057bfa 100644
@@ -61,7 +61,8 @@ struct sock_xprt {
 	struct sockaddr_storage	srcaddr;
 	unsigned short		srcport;
 	int			xprt_err;
-	struct rpc_clnt		*clnt;
+	u32			connect_prog;
+	u32			connect_vers;
 
 	/*
 	 * UDP socket buffer size parameters
include/trace/events/sunrpc.h +5 -11
index 750ecce56930..8088a8ad83d4 100644
@@ -1528,28 +1528,23 @@ TRACE_EVENT(rpcb_unregister,
 
 DECLARE_EVENT_CLASS(rpc_tls_class,
 	TP_PROTO(
-		const struct rpc_clnt *clnt,
 		const struct rpc_xprt *xprt
 	),
 
-	TP_ARGS(clnt, xprt),
+	TP_ARGS(xprt),
 
 	TP_STRUCT__entry(
 		__field(unsigned long, requested_policy)
-		__field(u32, version)
 		__string(servername, xprt->servername)
-		__string(progname, clnt->cl_program->name)
 	),
 
 	TP_fast_assign(
-		__entry->requested_policy = clnt->cl_xprtsec.policy;
-		__entry->version = clnt->cl_vers;
+		__entry->requested_policy = xprt->xprtsec.policy;
 		__assign_str(servername);
-		__assign_str(progname);
 	),
 
-	TP_printk("server=%s %sv%u requested_policy=%s",
-		__get_str(servername), __get_str(progname), __entry->version,
+	TP_printk("server=%s requested_policy=%s",
+		__get_str(servername),
 		rpc_show_xprtsec_policy(__entry->requested_policy)
 	)
 );
@@ -1557,10 +1552,9 @@ DECLARE_EVENT_CLASS(rpc_tls_class,
 #define DEFINE_RPC_TLS_EVENT(name) \
 	DEFINE_EVENT(rpc_tls_class, rpc_tls_##name, \
 			TP_PROTO( \
-				const struct rpc_clnt *clnt, \
 				const struct rpc_xprt *xprt \
 			), \
-			TP_ARGS(clnt, xprt))
+			TP_ARGS(xprt))
 
 DEFINE_RPC_TLS_EVENT(unavailable);
 DEFINE_RPC_TLS_EVENT(not_started);
net/sunrpc/xprtsock.c +48 -11

106 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 11 lines · Benjamin Coddington

The TLS connection worker (xs_tcp_tls_setup_socket) currently requires a reference to the upper-layer rpc_clnt to populate arguments for rpc_create() when setting up the lower-layer transport for the RPC_AUTH_TLS probe. This dependency led to lifetime issues and a use-after-free (UAF) of the client's credentials if the task finished before the worker ran addressed previously in linux-nfs upstream posting:
https://lore.kernel.org/linux-nfs/[email protected]/

However, it is architecturally cleaner to decouple the transport connection logic from the RPC client entirely.

It's probably worth expanding (briefly) on the architectural cleanliness argument a bit. Why not simply bump the reference count on the rpc_clnt to prevent the UAF? There is definitely a bit of abuse in the current code that is removed by your refactor.

Quoted 9 lines

The TLS probe requires
the upper-layer's RPC program and version to facilitate the probe.

Refactor the TLS transport setup to:
- Remove the clnt member from struct sock_xprt.
- Save the RPC program number and version from the task during xs_connect() into the sock_xprt structure.
- Update xs_tcp_tls_setup_socket to use these saved parameters for lower-layer client creation in dummy program definitiions.

^definitiions^definitions

Quoted 7 lines

- Update TLS-related tracepoints to remove the rpc_clnt dependency.
- Remove the rpc_clnt refcounting and assignment in xs_connect.

This simplifies state management and eliminates the risk of UAF without requiring the transport to pin the upper-layer client.

More below.

Quoted 7 lines

---

I'm not sure I like what came out here. It adds a lot of boilerplate, and it reduces the tracepoint data (pretty necessary due to the race, however).

Most of the fix is from just not setting cl_cred - it stays NULL. Probably the other values we get from the upper rpc_clnt are still hanging around.

Can the patch description provide a rationale for why using a NULL cred is safe?

Quoted 70 lines

We could reduce the dummy definition boilerplate by passing the rpc_program pointer, but then there's a potential null-dref if the calling module gets unloaded.

Can RCU the upper rpc_clnt? Any other ideas?

This is lightly tested by hand..

Ben

---

 include/linux/sunrpc/xprtsock.h |  3 +-
 include/trace/events/sunrpc.h   | 16 +++------
 net/sunrpc/xprtsock.c           | 59 +++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 23 deletions(-)
include/linux/sunrpc/xprtsock.h +2 -1
index 700a1e6c047c..7cafc3057bfa 100644
@@ -61,7 +61,8 @@ struct sock_xprt {
 	struct sockaddr_storage	srcaddr;
 	unsigned short		srcport;
 	int			xprt_err;
-	struct rpc_clnt		*clnt;
+	u32			connect_prog;
+	u32			connect_vers;
 
 	/*
 	 * UDP socket buffer size parameters
include/trace/events/sunrpc.h +4 -9
index 750ecce56930..8088a8ad83d4 100644
@@ -1528,28 +1528,23 @@ TRACE_EVENT(rpcb_unregister,
 
 DECLARE_EVENT_CLASS(rpc_tls_class,
 	TP_PROTO(
-		const struct rpc_clnt *clnt,
 		const struct rpc_xprt *xprt
 	),
 
-	TP_ARGS(clnt, xprt),
+	TP_ARGS(xprt),
 
 	TP_STRUCT__entry(
 		__field(unsigned long, requested_policy)
-		__field(u32, version)
 		__string(servername, xprt->servername)
-		__string(progname, clnt->cl_program->name)
 	),
 
 	TP_fast_assign(
-		__entry->requested_policy = clnt->cl_xprtsec.policy;
-		__entry->version = clnt->cl_vers;
+		__entry->requested_policy = xprt->xprtsec.policy;
 		__assign_str(servername);
-		__assign_str(progname);
 	),
 
-	TP_printk("server=%s %sv%u requested_policy=%s",
-		__get_str(servername), __get_str(progname), __entry->version,
+	TP_printk("server=%s requested_policy=%s",
+		__get_str(servername),
 		rpc_show_xprtsec_policy(__entry->requested_policy)
 	)
 );
@@ -1557,10 +1552,9 @@ DECLARE_EVENT_CLASS(rpc_tls_class,
 #define DEFINE_RPC_TLS_EVENT(name) \

include/trace/events/sunrpc.h DECLARE_EVENT_CLASS(rpc_tls_class, · line 1560

Quoted 10 lines
include/trace/events/sunrpc.h +1 -2
 	DEFINE_EVENT(rpc_tls_class, rpc_tls_##name, \
 			TP_PROTO( \
-				const struct rpc_clnt *clnt, \
 				const struct rpc_xprt *xprt \
 			), \
-			TP_ARGS(clnt, xprt))
+			TP_ARGS(xprt))
 
 DEFINE_RPC_TLS_EVENT(unavailable);
 DEFINE_RPC_TLS_EVENT(not_started);

The requested_policy field that is retained is irrelevant to the RPC_AUTH_TLS probe (though it matters later during xs_tls_handshake_sync). But the program number and version that /are/ dropped are what actually go on the wire in the AUTH_TLS NULL call, so if the probe fails, the administrator wants to know which program/version pair was rejected, not whether the handshake that never happened would have been tls or mtls.

Note that this information is recoverable via container_of(xprt, struct sock_xprt, xprt)->connect_prog/connect_vers without needing rpc_clnt.

Quoted 29 lines
net/sunrpc/xprtsock.c +20 -0
index 2e1fe6013361..5f3103e9a8f2 100644
@@ -61,6 +61,45 @@
 #include "socklib.h"
 #include "sunrpc.h"
 
+/* Helper macro to define repetitive rpc_version structures */
+#define RPC_VERSION_DEFINE(prog, v_num)                 \
+static const struct rpc_version rpc_##prog##_version##v_num = { \
+    .number     = v_num,                                \
+    .nrprocs    = ARRAY_SIZE(rpc_##prog##_procs),       \
+    .procs      = rpc_##prog##_procs,                   \
+    .counts     = rpc_##prog##_counts,                  \
+}
+
+static struct rpc_stat rpc_tls_dummy_stats;
+
+static const struct rpc_procinfo rpc_tls_dummy_procs[] = {
+    [0] = {
+        .p_encode   = NULL,
+        .p_decode   = NULL,
+    },
+};
+
+static unsigned int 
 rpc_tls_dummy_counts[ARRAY_SIZE(rpc_tls_dummy_procs)];
+

net/sunrpc/xprtsock.c line 94

Quoted 10 lines
net/sunrpc/xprtsock.c +10 -0
+/* Generate the structures for versions 2, 3, and 4 */
+RPC_VERSION_DEFINE(tls_dummy, 2);
+RPC_VERSION_DEFINE(tls_dummy, 3);
+RPC_VERSION_DEFINE(tls_dummy, 4);
+
+static const struct rpc_version *rpc_tls_dummy_versions[5] = {
+    [2] = &rpc_tls_dummy_version2,
+    [3] = &rpc_tls_dummy_version3,
+    [4] = &rpc_tls_dummy_version4,
+};

This is a layering violation.

Today's Linux NFS client stack implements TLS only for the NFS protocol, which has support for only versions 2, 3, and 4. But what if we want to implement TLS for NLM (or any other RPC protocol) -- NLM has v1, v3, and v4.

A simpler design: use a single dummy version at index 0 with args->version = 0 always, relying solely on args->prognumber for the wire program number. The TLS probe never consults cl_vers -- it uses a fixed proc via msg.rpc_proc.

(Ultimately that is still a hack, but you get the idea).

net/sunrpc/xprtsock.c line 102

Quoted 9 lines
net/sunrpc/xprtsock.c +9 -0
+
+static const struct rpc_program rpc_tls_dummy_program = {
+    .name       = "tls_probe",
+    .number     = 0,
+    .nrvers     = ARRAY_SIZE(rpc_tls_dummy_versions),
+    .version    = rpc_tls_dummy_versions,
+    .stats      = &rpc_tls_dummy_stats,
+};
+

Nit: All of the new static structures and the macro use 4-space indentation rather than tabs. Other rpc_program definitions in the tree (rpcb_program in rpcb_clnt.c, gssp_program in gss_rpc_upcall.c) use tabs per the kernel coding style.

Quoted 8 lines
net/sunrpc/xprtsock.c +0 -0
 static void xs_close(struct rpc_xprt *xprt);
 static void xs_reset_srcport(struct sock_xprt *transport);

static void xs_set_srcport(struct sock_xprt *transport, struct socket
*sock);

net/sunrpc/xprtsock.c +0 -0
@@ -2687,24 +2726,21 @@ static void xs_tcp_tls_setup_socket(struct 
 work_struct *work)
 {
 	struct sock_xprt *upper_transport =

net/sunrpc/xprtsock.c static void xs_tcp_tls_setup_socket(struct · line 2736

Quoted 10 lines
net/sunrpc/xprtsock.c +1 -2
 		container_of(work, struct sock_xprt, connect_worker.work);
-	struct rpc_clnt *upper_clnt = upper_transport->clnt;
 	struct rpc_xprt *upper_xprt = &upper_transport->xprt;
 	struct rpc_create_args args = {
 		.net		= upper_xprt->xprt_net,
 		.protocol	= upper_xprt->prot,
 		.address	= (struct sockaddr *)&upper_xprt->addr,
 		.addrsize	= upper_xprt->addrlen,
-		.timeout	= upper_clnt->cl_timeout,
+		.timeout	= upper_xprt->timeout,

The timeout source changed here from the upper-layer client's configured timeout to the transport's default timeout. These can differ: upper_clnt->cl_timeout points to a copy of the NFS-configured timeout (set via nfs_init_timeout_values()), while upper_xprt->timeout points to xs_tcp_default_timeout.

For default NFS TCP mounts, nfs_init_timeout_values() produces to_maxval = 180 * HZ and to_increment = 60 * HZ, whereas xs_tcp_default_timeout has to_maxval = 60 * HZ and to_increment = 0.

Was this change intentional? The commit message does not mention it, and it alters the retry behavior for the TLS probe on mounts with custom timeout settings.

net/sunrpc/xprtsock.c static void xs_tcp_tls_setup_socket(struct work_struct *work) · line 2737

Quoted 5 lines
net/sunrpc/xprtsock.c +1 -3
 		.servername	= upper_xprt->servername,
-		.program	= upper_clnt->cl_program,
-		.prognumber	= upper_clnt->cl_prog,
-		.version	= upper_clnt->cl_vers,
+		.prognumber = upper_transport->connect_prog,

Whitespace damage.

Quoted 51 lines
net/sunrpc/xprtsock.c +2 -2
+		.version	= upper_transport->connect_vers,
+		.program	= &rpc_tls_dummy_program,
 		.authflavor	= RPC_AUTH_TLS,
-		.cred		= upper_clnt->cl_cred,
 		.xprtsec	= {
 			.policy		= RPC_XPRTSEC_NONE,
 		},
-		.stats		= upper_clnt->cl_stats,
 	};
 	unsigned int pflags = current->flags;
net/sunrpc/xprtsock.c +1 -1
 	struct rpc_clnt *lower_clnt;
@@ -2719,7 +2755,7 @@ static void xs_tcp_tls_setup_socket(struct 
 work_struct *work)
 	/* This implicitly sends an RPC_AUTH_TLS probe */
 	lower_clnt = rpc_create(&args);
 	if (IS_ERR(lower_clnt)) {
-		trace_rpc_tls_unavailable(upper_clnt, upper_xprt);
+		trace_rpc_tls_unavailable(upper_xprt);
 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
 		xprt_clear_connecting(upper_xprt);
net/sunrpc/xprtsock.c +4 -3
 		xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt));
@@ -2739,7 +2775,7 @@ static void xs_tcp_tls_setup_socket(struct 
 work_struct *work)
 
 	status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec);
 	if (status) {
-		trace_rpc_tls_not_started(upper_clnt, upper_xprt);
+		trace_rpc_tls_not_started(upper_xprt);
 		goto out_close;
 	}
@@ -2757,7 +2793,6 @@ static void xs_tcp_tls_setup_socket(struct 
 work_struct *work)
 
 out_unlock:
 	current_restore_flags(pflags, PF_MEMALLOC);
-	upper_transport->clnt = NULL;
 	xprt_unlock_connect(upper_xprt, upper_transport);
 	return;
@@ -2805,7 +2840,9 @@ static void xs_connect(struct rpc_xprt *xprt, 
 struct rpc_task *task)
 	} else
 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
 
-	transport->clnt = task->tk_client;
+	transport->connect_prog = task->tk_client->cl_prog;
+	transport->connect_vers = task->tk_client->cl_vers;
+
 	queue_delayed_work(xprtiod_workqueue,
 			&transport->connect_worker,
Quoted 3 lines

delay);

Signature · 1 line
2.53.0

The proposed refactoring seems like a sensible direction.

The UAF potential is eliminated structurally, and some of the transport-to-client coupling (and layering violation) is removed.

Signature · 1 line
Chuck Lever
#2 lore raw
Quoted 197 lines · Chuck Lever
Quoted 11 lines · Benjamin Coddington

The TLS connection worker (xs_tcp_tls_setup_socket) currently requires a reference to the upper-layer rpc_clnt to populate arguments for rpc_create() when setting up the lower-layer transport for the RPC_AUTH_TLS probe. This dependency led to lifetime issues and a use-after-free (UAF) of the client's credentials if the task finished before the worker ran addressed previously in linux-nfs upstream posting:
https://lore.kernel.org/linux-nfs/[email protected]/

However, it is architecturally cleaner to decouple the transport connection logic from the RPC client entirely.

It's probably worth expanding (briefly) on the architectural cleanliness argument a bit. Why not simply bump the reference count on the rpc_clnt to prevent the UAF? There is definitely a bit of abuse in the current code that is removed by your refactor.

Quoted 9 lines

The TLS probe requires
the upper-layer's RPC program and version to facilitate the probe.

Refactor the TLS transport setup to:
- Remove the clnt member from struct sock_xprt.
- Save the RPC program number and version from the task during xs_connect() into the sock_xprt structure.
- Update xs_tcp_tls_setup_socket to use these saved parameters for lower-layer client creation in dummy program definitiions.

^definitiions^definitions

Quoted 7 lines

- Update TLS-related tracepoints to remove the rpc_clnt dependency.
- Remove the rpc_clnt refcounting and assignment in xs_connect.

This simplifies state management and eliminates the risk of UAF without requiring the transport to pin the upper-layer client.

More below.

Quoted 7 lines

---

I'm not sure I like what came out here. It adds a lot of boilerplate, and it reduces the tracepoint data (pretty necessary due to the race, however).

Most of the fix is from just not setting cl_cred - it stays NULL. Probably the other values we get from the upper rpc_clnt are still hanging around.

Can the patch description provide a rationale for why using a NULL cred is safe?

Quoted 70 lines

We could reduce the dummy definition boilerplate by passing the rpc_program pointer, but then there's a potential null-dref if the calling module gets unloaded.

Can RCU the upper rpc_clnt? Any other ideas?

This is lightly tested by hand..

Ben

---

 include/linux/sunrpc/xprtsock.h |  3 +-
 include/trace/events/sunrpc.h   | 16 +++------
 net/sunrpc/xprtsock.c           | 59 +++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 23 deletions(-)
include/linux/sunrpc/xprtsock.h +2 -1
index 700a1e6c047c..7cafc3057bfa 100644
@@ -61,7 +61,8 @@ struct sock_xprt {
 	struct sockaddr_storage	srcaddr;
 	unsigned short		srcport;
 	int			xprt_err;
-	struct rpc_clnt		*clnt;
+	u32			connect_prog;
+	u32			connect_vers;
 
 	/*
 	 * UDP socket buffer size parameters
include/trace/events/sunrpc.h +4 -9
index 750ecce56930..8088a8ad83d4 100644
@@ -1528,28 +1528,23 @@ TRACE_EVENT(rpcb_unregister,
 
 DECLARE_EVENT_CLASS(rpc_tls_class,
 	TP_PROTO(
-		const struct rpc_clnt *clnt,
 		const struct rpc_xprt *xprt
 	),
 
-	TP_ARGS(clnt, xprt),
+	TP_ARGS(xprt),
 
 	TP_STRUCT__entry(
 		__field(unsigned long, requested_policy)
-		__field(u32, version)
 		__string(servername, xprt->servername)
-		__string(progname, clnt->cl_program->name)
 	),
 
 	TP_fast_assign(
-		__entry->requested_policy = clnt->cl_xprtsec.policy;
-		__entry->version = clnt->cl_vers;
+		__entry->requested_policy = xprt->xprtsec.policy;
 		__assign_str(servername);
-		__assign_str(progname);
 	),
 
-	TP_printk("server=%s %sv%u requested_policy=%s",
-		__get_str(servername), __get_str(progname), __entry->version,
+	TP_printk("server=%s requested_policy=%s",
+		__get_str(servername),
 		rpc_show_xprtsec_policy(__entry->requested_policy)
 	)
 );
@@ -1557,10 +1552,9 @@ DECLARE_EVENT_CLASS(rpc_tls_class,
 #define DEFINE_RPC_TLS_EVENT(name) \

include/trace/events/sunrpc.h DECLARE_EVENT_CLASS(rpc_tls_class, · line 1560

Quoted 10 lines
include/trace/events/sunrpc.h +1 -2
 	DEFINE_EVENT(rpc_tls_class, rpc_tls_##name, \
 			TP_PROTO( \
-				const struct rpc_clnt *clnt, \
 				const struct rpc_xprt *xprt \
 			), \
-			TP_ARGS(clnt, xprt))
+			TP_ARGS(xprt))
 
 DEFINE_RPC_TLS_EVENT(unavailable);
 DEFINE_RPC_TLS_EVENT(not_started);

The requested_policy field that is retained is irrelevant to the RPC_AUTH_TLS probe (though it matters later during xs_tls_handshake_sync). But the program number and version that /are/ dropped are what actually go on the wire in the AUTH_TLS NULL call, so if the probe fails, the administrator wants to know which program/version pair was rejected, not whether the handshake that never happened would have been tls or mtls.

Note that this information is recoverable via container_of(xprt, struct sock_xprt, xprt)->connect_prog/connect_vers without needing rpc_clnt.

Quoted 29 lines
net/sunrpc/xprtsock.c +20 -0
index 2e1fe6013361..5f3103e9a8f2 100644
@@ -61,6 +61,45 @@
 #include "socklib.h"
 #include "sunrpc.h"
 
+/* Helper macro to define repetitive rpc_version structures */
+#define RPC_VERSION_DEFINE(prog, v_num)                 \
+static const struct rpc_version rpc_##prog##_version##v_num = { \
+    .number     = v_num,                                \
+    .nrprocs    = ARRAY_SIZE(rpc_##prog##_procs),       \
+    .procs      = rpc_##prog##_procs,                   \
+    .counts     = rpc_##prog##_counts,                  \
+}
+
+static struct rpc_stat rpc_tls_dummy_stats;
+
+static const struct rpc_procinfo rpc_tls_dummy_procs[] = {
+    [0] = {
+        .p_encode   = NULL,
+        .p_decode   = NULL,
+    },
+};
+
+static unsigned int
 rpc_tls_dummy_counts[ARRAY_SIZE(rpc_tls_dummy_procs)];
+

net/sunrpc/xprtsock.c line 94

Quoted 10 lines
net/sunrpc/xprtsock.c +10 -0
+/* Generate the structures for versions 2, 3, and 4 */
+RPC_VERSION_DEFINE(tls_dummy, 2);
+RPC_VERSION_DEFINE(tls_dummy, 3);
+RPC_VERSION_DEFINE(tls_dummy, 4);
+
+static const struct rpc_version *rpc_tls_dummy_versions[5] = {
+    [2] = &rpc_tls_dummy_version2,
+    [3] = &rpc_tls_dummy_version3,
+    [4] = &rpc_tls_dummy_version4,
+};

This is a layering violation.

Today's Linux NFS client stack implements TLS only for the NFS protocol, which has support for only versions 2, 3, and 4. But what if we want to implement TLS for NLM (or any other RPC protocol) -- NLM has v1, v3, and v4.

Quoted 6 lines

A simpler design: use a single dummy version at index 0 with args->version = 0 always, relying solely on args->prognumber for the wire program number. The TLS probe never consults cl_vers -- it uses a fixed proc via msg.rpc_proc.

(Ultimately that is still a hack, but you get the idea).

Yep - I get it - that was the originally idea until I discovered there's some sanity/cross-referencing code in rpc_new_client() that may need modification because it wants to make sure that version matches the defined program version as looked up in the program->version[] index.

I kinda which I could define a completely static rpc_clnt, make a mutable copy, set prognum and version and somehow get that plumbed throught the rpc_create() machinery.. then throw it away.. </waves hands wildly>

Quoted 117 lines

net/sunrpc/xprtsock.c line 102

Quoted 9 lines
net/sunrpc/xprtsock.c +9 -0
+
+static const struct rpc_program rpc_tls_dummy_program = {
+    .name       = "tls_probe",
+    .number     = 0,
+    .nrvers     = ARRAY_SIZE(rpc_tls_dummy_versions),
+    .version    = rpc_tls_dummy_versions,
+    .stats      = &rpc_tls_dummy_stats,
+};
+

Nit: All of the new static structures and the macro use 4-space indentation rather than tabs. Other rpc_program definitions in the tree (rpcb_program in rpcb_clnt.c, gssp_program in gss_rpc_upcall.c) use tabs per the kernel coding style.

Quoted 8 lines
net/sunrpc/xprtsock.c +0 -0
 static void xs_close(struct rpc_xprt *xprt);
 static void xs_reset_srcport(struct sock_xprt *transport);

static void xs_set_srcport(struct sock_xprt *transport, struct socket
*sock);

net/sunrpc/xprtsock.c +0 -0
@@ -2687,24 +2726,21 @@ static void xs_tcp_tls_setup_socket(struct
 work_struct *work)
 {
 	struct sock_xprt *upper_transport =

net/sunrpc/xprtsock.c static void xs_tcp_tls_setup_socket(struct · line 2736

Quoted 10 lines
net/sunrpc/xprtsock.c +1 -2
 		container_of(work, struct sock_xprt, connect_worker.work);
-	struct rpc_clnt *upper_clnt = upper_transport->clnt;
 	struct rpc_xprt *upper_xprt = &upper_transport->xprt;
 	struct rpc_create_args args = {
 		.net		= upper_xprt->xprt_net,
 		.protocol	= upper_xprt->prot,
 		.address	= (struct sockaddr *)&upper_xprt->addr,
 		.addrsize	= upper_xprt->addrlen,
-		.timeout	= upper_clnt->cl_timeout,
+		.timeout	= upper_xprt->timeout,

The timeout source changed here from the upper-layer client's configured timeout to the transport's default timeout. These can differ: upper_clnt->cl_timeout points to a copy of the NFS-configured timeout (set via nfs_init_timeout_values()), while upper_xprt->timeout points to xs_tcp_default_timeout.

For default NFS TCP mounts, nfs_init_timeout_values() produces to_maxval = 180 * HZ and to_increment = 60 * HZ, whereas xs_tcp_default_timeout has to_maxval = 60 * HZ and to_increment = 0.

Was this change intentional? The commit message does not mention it, and it alters the retry behavior for the TLS probe on mounts with custom timeout settings.

net/sunrpc/xprtsock.c static void xs_tcp_tls_setup_socket(struct work_struct *work) · line 2737

Quoted 5 lines
net/sunrpc/xprtsock.c +1 -3
 		.servername	= upper_xprt->servername,
-		.program	= upper_clnt->cl_program,
-		.prognumber	= upper_clnt->cl_prog,
-		.version	= upper_clnt->cl_vers,
+		.prognumber = upper_transport->connect_prog,

Whitespace damage.

Quoted 51 lines
net/sunrpc/xprtsock.c +2 -2
+		.version	= upper_transport->connect_vers,
+		.program	= &rpc_tls_dummy_program,
 		.authflavor	= RPC_AUTH_TLS,
-		.cred		= upper_clnt->cl_cred,
 		.xprtsec	= {
 			.policy		= RPC_XPRTSEC_NONE,
 		},
-		.stats		= upper_clnt->cl_stats,
 	};
 	unsigned int pflags = current->flags;
net/sunrpc/xprtsock.c +1 -1
 	struct rpc_clnt *lower_clnt;
@@ -2719,7 +2755,7 @@ static void xs_tcp_tls_setup_socket(struct
 work_struct *work)
 	/* This implicitly sends an RPC_AUTH_TLS probe */
 	lower_clnt = rpc_create(&args);
 	if (IS_ERR(lower_clnt)) {
-		trace_rpc_tls_unavailable(upper_clnt, upper_xprt);
+		trace_rpc_tls_unavailable(upper_xprt);
 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
 		xprt_clear_connecting(upper_xprt);
net/sunrpc/xprtsock.c +4 -3
 		xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt));
@@ -2739,7 +2775,7 @@ static void xs_tcp_tls_setup_socket(struct
 work_struct *work)
 
 	status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec);
 	if (status) {
-		trace_rpc_tls_not_started(upper_clnt, upper_xprt);
+		trace_rpc_tls_not_started(upper_xprt);
 		goto out_close;
 	}
@@ -2757,7 +2793,6 @@ static void xs_tcp_tls_setup_socket(struct
 work_struct *work)
 
 out_unlock:
 	current_restore_flags(pflags, PF_MEMALLOC);
-	upper_transport->clnt = NULL;
 	xprt_unlock_connect(upper_xprt, upper_transport);
 	return;
@@ -2805,7 +2840,9 @@ static void xs_connect(struct rpc_xprt *xprt,
 struct rpc_task *task)
 	} else
 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
 
-	transport->clnt = task->tk_client;
+	transport->connect_prog = task->tk_client->cl_prog;
+	transport->connect_vers = task->tk_client->cl_vers;
+
 	queue_delayed_work(xprtiod_workqueue,
 			&transport->connect_worker,
Quoted 3 lines

delay);

Signature · 1 line
2.53.0

The proposed refactoring seems like a sensible direction.

Quoted 2 lines

The UAF potential is eliminated structurally, and some of the transport-to-client coupling (and layering violation) is removed.

Ok - thanks for looking at this and showing interest. As you've noted there are quite a few rough spots here due to me just tacking this together quickly. Sorry for the messiness.

I'll plan on iterating on your review points for a v2 - may be a couple days to get back around to it.

Ben