Skip to contentSkip to conversation

SUNRPC: Do not dereference non-socket transports in sysfs

PATCH Superseded [email protected] 4 participants 6 messages Mar 24, 2022 – Mar 25, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 1/2 SUNRPC: Do not dereference non-socket transports in sysfs

From: Trond Myklebust <[email protected]>

Do not cast the struct xprt to a sock_xprt unless we know it is a UDP or TCP transport. Otherwise the call to lock the mutex will scribble over whatever structure is actually there. This has been seen to cause hard system lockups when the underlying transport was RDMA.

Fixes e44773daf851 ("SUNRPC: Add srcaddr as a file in sysfs")
---
 net/sunrpc/sysfs.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)
net/sunrpc/sysfs.c +22 -10
index 05c758da6a92..8ce053f84421 100644
@@ -107,22 +107,34 @@ static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj,
 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
 	struct sockaddr_storage saddr;
 	struct sock_xprt *sock;
+	const char *fmt = "<closed>\n";
 	ssize_t ret = -1;
 
-	if (!xprt || !xprt_connected(xprt)) {
-		xprt_put(xprt);
-		return -ENOTCONN;
-	}
+	if (!xprt || !xprt_connected(xprt))
+		goto out;
 
-	sock = container_of(xprt, struct sock_xprt, xprt);
-	mutex_lock(&sock->recv_mutex);
-	if (sock->sock == NULL ||
-	    kernel_getsockname(sock->sock, (struct sockaddr *)&saddr) < 0)
+	switch (xprt->xprt_class->ident) {
+	case XPRT_TRANSPORT_UDP:
+	case XPRT_TRANSPORT_TCP:
+		break;
+	default:
+		fmt = "<not a socket>\n";
 		goto out;
+	};
 
-	ret = sprintf(buf, "%pISc\n", &saddr);
-out:
+	sock = container_of(xprt, struct sock_xprt, xprt);
+	mutex_lock(&sock->recv_mutex);
+	if (sock->sock != NULL) {
+		ret = kernel_getsockname(sock->sock, (struct sockaddr *)&saddr);
+		if (ret >= 0) {
+			ret = sprintf(buf, "%pISc\n", &saddr);
+			fmt = NULL;
+		}
+	}
 	mutex_unlock(&sock->recv_mutex);
+out:
+	if (fmt)
+		ret = sprintf(buf, fmt);
 	xprt_put(xprt);
 	return ret + 1;
 }
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 2/2 SUNRPC: Don't return error values in sysfs read of closed files

From: Trond Myklebust <[email protected]>

Instead of returning an error value, which ends up being the return value for the read() system call, it is more elegant to simply return the error as a string value.

---
 net/sunrpc/sysfs.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
net/sunrpc/sysfs.c +9 -8
index 8ce053f84421..796e0f141282 100644
@@ -93,10 +93,13 @@ static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj,
 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
 	ssize_t ret;
 
-	if (!xprt)
-		return 0;
+	if (!xprt) {
+		ret = sprintf(buf, "<closed>\n");
+		goto out;
+	}
 	ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
 	xprt_put(xprt);
+out:
 	return ret + 1;
 }
 
@@ -147,8 +150,8 @@ static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
 	ssize_t ret;
 
 	if (!xprt || !xprt_connected(xprt)) {
-		xprt_put(xprt);
-		return -ENOTCONN;
+		ret = sprintf(buf, "<closed>\n");
+		goto out;
 	}
 
 	ret = sprintf(buf, "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n"
@@ -165,6 +168,7 @@ static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
 		       atomic_long_read(&xprt->queuelen),
 		       (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
 				xprt->address_strings[RPC_DISPLAY_PORT] : "0");
+out:
 	xprt_put(xprt);
 	return ret + 1;
 }
@@ -178,10 +182,7 @@ static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj,
 	int locked, connected, connecting, close_wait, bound, binding,
 	    closing, congested, cwnd_wait, write_space, offline, remove;
 
-	if (!xprt)
-		return 0;
-
-	if (!xprt->state) {
+	if (!(xprt && xprt->state)) {
 		ret = sprintf(buf, "state=CLOSED\n");
 	} else {
 		locked = test_bit(XPRT_LOCKED, &xprt->state);
Signature · 1 line
2.35.1
#2 lore raw
In reply to [email protected]
Quoted 30 lines · On Mar 24, 2022, at 5:33 PM, [email protected] wrote:

From: Trond Myklebust <[email protected]>

Do not cast the struct xprt to a sock_xprt unless we know it is a UDP or TCP transport. Otherwise the call to lock the mutex will scribble over whatever structure is actually there. This has been seen to cause hard system lockups when the underlying transport was RDMA.

Fixes e44773daf851 ("SUNRPC: Add srcaddr as a file in sysfs")

---
net/sunrpc/sysfs.c | 32 ++++++++++++++++++++++----------

1 file changed, 22 insertions(+), 10 deletions(-)
net/sunrpc/sysfs.c +1 -1
index 05c758da6a92..8ce053f84421 100644
@@ -107,22 +107,34 @@ static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj,
 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
 	struct sockaddr_storage saddr;
 	struct sock_xprt *sock;
+	const char *fmt = "<closed>\n";
 	ssize_t ret = -1;
 
-	if (!xprt || !xprt_connected(xprt)) {

net/sunrpc/sysfs.c static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj, · line 120

Quoted 10 lines
net/sunrpc/sysfs.c +2 -7
-		xprt_put(xprt);
-		return -ENOTCONN;
-	}
+	if (!xprt || !xprt_connected(xprt))
+		goto out;
 
-	sock = container_of(xprt, struct sock_xprt, xprt);
-	mutex_lock(&sock->recv_mutex);
-	if (sock->sock == NULL ||
-	    kernel_getsockname(sock->sock, (struct sockaddr *)&saddr) < 0)

This would be a little nicer if it went through the transport switch instead. But isn't the socket address available in the rpc_xprt?

Quoted 30 lines
net/sunrpc/sysfs.c +19 -2
+	switch (xprt->xprt_class->ident) {
+	case XPRT_TRANSPORT_UDP:
+	case XPRT_TRANSPORT_TCP:
+		break;
+	default:
+		fmt = "<not a socket>\n";
 		goto out;
+	};
 
-	ret = sprintf(buf, "%pISc\n", &saddr);
-out:
+	sock = container_of(xprt, struct sock_xprt, xprt);
+	mutex_lock(&sock->recv_mutex);
+	if (sock->sock != NULL) {
+		ret = kernel_getsockname(sock->sock, (struct sockaddr *)&saddr);
+		if (ret >= 0) {
+			ret = sprintf(buf, "%pISc\n", &saddr);
+			fmt = NULL;
+		}
+	}
 	mutex_unlock(&sock->recv_mutex);
+out:
+	if (fmt)
+		ret = sprintf(buf, fmt);
 	xprt_put(xprt);
 	return ret + 1;
 }
Signature · 1 line
2.35.1

--
Chuck Lever

#3 lore raw
Quoted 40 lines · Chuck Lever III
Quoted 40 lines · On Mar 24, 2022, at 5:33 PM, [email protected] wrote:

From: Trond Myklebust <[email protected]>

Do not cast the struct xprt to a sock_xprt unless we know it is a UDP or
TCP transport. Otherwise the call to lock the mutex will scribble over
whatever structure is actually there. This has been seen to cause hard
system lockups when the underlying transport was RDMA.

Fixes e44773daf851 ("SUNRPC: Add srcaddr as a file in sysfs")

---
net/sunrpc/sysfs.c | 32 ++++++++++++++++++++++----------

1 file changed, 22 insertions(+), 10 deletions(-)
net/sunrpc/sysfs.c +3 -4
index 05c758da6a92..8ce053f84421 100644
@@ -107,22 +107,34 @@ static ssize_t
 rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj,
         struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
         struct sockaddr_storage saddr;
         struct sock_xprt *sock;
+       const char *fmt = "<closed>\n";
         ssize_t ret = -1;
 
-       if (!xprt || !xprt_connected(xprt)) {
-               xprt_put(xprt);
-               return -ENOTCONN;
-       }
+       if (!xprt || !xprt_connected(xprt))
+               goto out;
 
Quoted 10 lines

net/sunrpc/sysfs.c static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj,

Quoted 5 lines
net/sunrpc/sysfs.c +0 -4
-       sock = container_of(xprt, struct sock_xprt, xprt);
-       mutex_lock(&sock->recv_mutex);
-       if (sock->sock == NULL ||
-           kernel_getsockname(sock->sock, (struct sockaddr
 *)&saddr) < 0)

This would be a little nicer if it went through the transport switch instead. But isn't the socket address available in the rpc_xprt?

I agree that a follow up patch could move this and the other socket- specific code to the switch. However the point of this patch is to provide a stable fix for the memory scribble.

...and no, the source address is not available in the rpc_xprt. Only the destination address is stored there.

Quoted 37 lines
Quoted 25 lines
net/sunrpc/sysfs.c +19 -2
+       switch (xprt->xprt_class->ident) {
+       case XPRT_TRANSPORT_UDP:
+       case XPRT_TRANSPORT_TCP:
+               break;
+       default:
+               fmt = "<not a socket>\n";
                 goto out;
+       };
 
-       ret = sprintf(buf, "%pISc\n", &saddr);
-out:
+       sock = container_of(xprt, struct sock_xprt, xprt);
+       mutex_lock(&sock->recv_mutex);
+       if (sock->sock != NULL) {
+               ret = kernel_getsockname(sock->sock, (struct
 sockaddr *)&saddr);
+               if (ret >= 0) {
+                       ret = sprintf(buf, "%pISc\n", &saddr);
+                       fmt = NULL;
+               }
+       }
         mutex_unlock(&sock->recv_mutex);
+out:
+       if (fmt)
+               ret = sprintf(buf, fmt);
Quoted 6 lines

        xprt_put(xprt);
        return ret + 1;
}

Signature · 1 line
2.35.1

--
Chuck Lever

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#4 lore raw

[PATCH] SUNRPC: fix semicolon.cocci warnings

In reply to [email protected]
net/sunrpc/sysfs.c:123:2-3: Unneeded semicolon

From: kernel test robot <[email protected]>

net/sunrpc/sysfs.c:123:2-3: Unneeded semicolon

Remove unneeded semicolon.

Generated by: scripts/coccinelle/misc/semicolon.cocci

---

url:    https://github.com/0day-ci/linux/commits/trondmy-kernel-org/SUNRPC-Do-not-dereference-non-socket-transports-in-sysfs/20220325-054144
base:   git://git.linux-nfs.org/projects/trondmy/linux-nfs.git linux-next

:::::: branch date: 4 hours ago
:::::: commit date: 4 hours ago

 net/sunrpc/sysfs.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sunrpc/sysfs.c +1 -1
@@ -120,7 +120,7 @@ static ssize_t rpc_sysfs_xprt_srcaddr_sh
 	default:
 		fmt = "<not a socket>\n";
 		goto out;
-	};
+	}
 
 	sock = container_of(xprt, struct sock_xprt, xprt);
 	mutex_lock(&sock->recv_mutex);
#5 lore raw
In reply to [email protected]
I love your patch! Perhaps something to improve:

Hi,

I love your patch! Perhaps something to improve:

[auto build test WARNING on trondmy-nfs/linux-next]
[also build test WARNING on v5.17 next-20220324]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/trondmy-kernel-org/SUNRPC-Do-not-dereference-non-socket-transports-in-sysfs/20220325-054144
base:   git://git.linux-nfs.org/projects/trondmy/linux-nfs.git linux-next
config: x86_64-randconfig-c002 (https://download.01.org/0day-ci/archive/20220325/[email protected]/config)
compiler: gcc-9 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

If you fix the issue, kindly add following tag as appropriate

cocci warnings: (new ones prefixed by >>)

>> net/sunrpc/sysfs.c:123:2-3: Unneeded semicolon

Please review and possibly fold the followup patch.

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp