Skip to contentSkip to conversation

NFS: Fix potential buffer overflowin nfs_sysfs_link_rpc_client()

PATCH In review Gax-c 3 participants 4 messages Dec 17, 2024 – Dec 18, 2024

Subsystems (3)
lore
Share & tools
lore

Gax-c

author
#0 lore raw

PATCH 1/1 NFS: Fix potential buffer overflowin nfs_sysfs_link_rpc_client()

From: Zichen Xie <[email protected]>

name is char[64] where the size of clnt->cl_program->name remains unknown. Invoking strcat() directly will also lead to potential buffer overflow. Change them to strscpy() and strncat() to fix potential issues.

Fixes e13b549319a6 ("NFS: Add sysfs links to sunrpc clients for nfs_clients")
---
 fs/nfs/sysfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfs/sysfs.c +3 -3
index bf378ecd5d9f..7b59a40d40c0 100644
@@ -280,9 +280,9 @@ void nfs_sysfs_link_rpc_client(struct nfs_server *server,
 	char name[RPC_CLIENT_NAME_SIZE];
 	int ret;
 
-	strcpy(name, clnt->cl_program->name);
-	strcat(name, uniq ? uniq : "");
-	strcat(name, "_client");
+	strscpy(name, clnt->cl_program->name, sizeof(name));
+	strncat(name, uniq ? uniq : "", sizeof(name) - strlen(name) - 1);
+	strncat(name, "_client", sizeof(name) - strlen(name) - 1);
 
 	ret = sysfs_create_link_nowarn(&server->kobj,
 						&clnt->cl_sysfs->kobject, name);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · Gax-c

From: Zichen Xie <[email protected]>

name is char[64] where the size of clnt->cl_program->name remains unknown. Invoking strcat() directly will also lead to potential buffer
overflow. Change them to strscpy() and strncat() to fix potential issues.

What makes you think that clnt->cl_program->name is unknown?

All calls to nfs_sysfs_link_rpc_client() use well known RPC clients for which the cl_program is pointing to one of nlm_program, nfs_program or nfsacl_program. So we know very well the sizes of clnt->cl_program-

Quoted 31 lines

name.

Fixes e13b549319a6 ("NFS: Add sysfs links to sunrpc clients for

nfs_clients")

---
 fs/nfs/sysfs.c | 6 +++---

 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfs/sysfs.c +0 -0
index bf378ecd5d9f..7b59a40d40c0 100644
@@ -280,9 +280,9 @@ void nfs_sysfs_link_rpc_client(struct nfs_server
 *server,
  	char name[RPC_CLIENT_NAME_SIZE];
  	int ret;

fs/nfs/sysfs.c +3 -4
-	strcpy(name, clnt->cl_program->name);
-	strcat(name, uniq ? uniq : "");
-	strcat(name, "_client");
+	strscpy(name, clnt->cl_program->name, sizeof(name));
+	strncat(name, uniq ? uniq : "", sizeof(name) - strlen(name)
- 1);
+	strncat(name, "_client", sizeof(name) - strlen(name) - 1);


  ret = sysfs_create_link_nowarn(&server->kobj, &clnt->cl_sysfs-

Quoted 1 line

kobject, name);

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#2 lore raw
Quoted 9 lines · Trond Myklebust
Quoted 7 lines · Gax-c

From: Zichen Xie <[email protected]>

name is char[64] where the size of clnt->cl_program->name remains unknown. Invoking strcat() directly will also lead to potential buffer
overflow. Change them to strscpy() and strncat() to fix potential issues.

Quoted 8 lines

What makes you think that clnt->cl_program->name is unknown?

All calls to nfs_sysfs_link_rpc_client() use well known RPC clients for
which the cl_program is pointing to one of nlm_program, nfs_program or
nfsacl_program. So we know very well the sizes of clnt->cl_program-

Quoted 1 line

name.

Just to clarify: I'm not strongly against the patch itself. However it would seem premature to consider this a bug, let alone a stable fix candidate.

Has anyone ever seen a buffer overflow here? If so, under which circumstances?

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#3 lore raw
Quoted 17 lines · Trond Myklebust
Quoted 16 lines · Trond Myklebust
Quoted 7 lines · Gax-c

From: Zichen Xie <[email protected]>

name is char[64] where the size of clnt->cl_program->name remains unknown. Invoking strcat() directly will also lead to potential buffer
overflow. Change them to strscpy() and strncat() to fix potential issues.

What makes you think that clnt->cl_program->name is unknown?

All calls to nfs_sysfs_link_rpc_client() use well known RPC clients for
which the cl_program is pointing to one of nlm_program, nfs_program or
nfsacl_program. So we know very well the sizes of clnt->cl_program-

Quoted 8 lines
Quoted 1 line
Quoted 1 line

name.

Just to clarify: I'm not strongly against the patch itself. However it would seem premature to consider this a bug, let alone a stable fix candidate.

Has anyone ever seen a buffer overflow here? If so, under which circumstances?

No, there's no way in the current kernel, but I suppose both nfs_sysfs_link_rpc_client() as well as rpc_create() are exported, so we could end up having some other part of the kernel send a long "uniq" or create a client with a long cl_program->name. That change might escape our review.

Probably not a bad idea to send it back to stable IMO, since a change like that could get back there too.

Ben