Skip to contentSkip to conversation

libhandle: fix potential unterminated string problem

PATCH In review Darrick J. Wong 3 participants 3 messages Oct 8, 2020 – Oct 15, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libhandle: fix potential unterminated string problem

From: Darrick J. Wong <[email protected]>

gcc 10.2 complains about the strncpy call here, since it's possible that the source string is so long that the fspath inside the fdhash structure will end up without a null terminator. Work around strncpy braindamage yet again by forcing the string to be terminated properly.

---
Unless this is supposed to be a memcpy? But it doesn't look like it.

---
 libhandle/handle.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libhandle/handle.c +2 -1
index eb099f43791e..5c1686b3968d 100644
@@ -107,7 +107,8 @@ path_to_fshandle(
 		}
 
 		fdhp->fsfd = fd;
-		strncpy(fdhp->fspath, fspath, sizeof(fdhp->fspath));
+		strncpy(fdhp->fspath, fspath, sizeof(fdhp->fspath) - 1);
+		fdhp->fspath[sizeof(fdhp->fspath) - 1] = 0;
 		memcpy(fdhp->fsh, *fshanp, FSIDSIZE);
 
 		fdhp->fnxt = fdhash_head;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

gcc 10.2 complains about the strncpy call here, since it's possible that the source string is so long that the fspath inside the fdhash structure will end up without a null terminator. Work around strncpy braindamage yet again by forcing the string to be terminated properly.

---
Unless this is supposed to be a memcpy? But it doesn't look like it.

so FWIW, coverity has complained about this one forever, CID 1297520

I think the thing that kept me from addressing it before was uncertainty around lopping one byte off of PATH_MAX / MAXPATHLEN, essentially.

In reality, I think (?) PATH_MAX is a bit of a fiction anyway, and there's probably nothing special about that one byte, and I think (?) this is always a path to a mountpoint (?) which is unlikely to be 4096 or 4095 chars long anyway (?) so ... um ... sure.

Quoted 19 lines
---
 libhandle/handle.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libhandle/handle.c +2 -1
index eb099f43791e..5c1686b3968d 100644
@@ -107,7 +107,8 @@ path_to_fshandle(
 		}
 
 		fdhp->fsfd = fd;
-		strncpy(fdhp->fspath, fspath, sizeof(fdhp->fspath));
+		strncpy(fdhp->fspath, fspath, sizeof(fdhp->fspath) - 1);
+		fdhp->fspath[sizeof(fdhp->fspath) - 1] = 0;
 		memcpy(fdhp->fsh, *fshanp, FSIDSIZE);
 
 		fdhp->fnxt = fdhash_head;
Reviewed-by on v1 1/1
#2 lore raw
In reply to Darrick J. Wong
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

gcc 10.2 complains about the strncpy call here, since it's possible that the source string is so long that the fspath inside the fdhash structure will end up without a null terminator. Work around strncpy braindamage yet again by forcing the string to be terminated properly.

---
Unless this is supposed to be a memcpy? But it doesn't look like it.

No, it should not be memcy, and the change looks good:

That being said path_to_fspath where fspath originates is horrible and not mutlithreading safe due to the static filename buffer.