Skip to contentSkip to conversation

xfsprogs: minor 4.20 fixups

PATCH In review Eric Sandeen 5 participants 13 messages Feb 19, 2019 – Feb 22, 2019

lore
Share & tools
lore
#0 lore raw

cover xfsprogs: minor 4.20 fixups

Address a few nitpicks picked up by code scanners, 2 of which are "new" in 4.20.

#1 lore raw

PATCH 1/3 xfs_io: don't pass negative len to copy_file_range_cmd

If copy_src_filesize returns an error (-1) we should return that error, and not pass it to copy_file_range_cmd().

Addresses-Coverity-ID: 1431684 ("Improper use of negative value")

---

io/copy_file_range.c +4 -0
index 4e2969c..bc891c9 100644
@@ -121,6 +121,10 @@ copy_range_f(int argc, char **argv)
 
 	if (src == 0 && dst == 0 && len == 0) {
 		len = copy_src_filesize(fd);
+		if (len < 0) {
+			close(fd);
+			return 0;
+		}
 		copy_dst_truncate();
 	}
#2 lore raw

Re: [PATCH 1/3] xfs_io: don't pass negative len to copy_file_range_cmd

Quoted 12 lines · Eric Sandeen

If copy_src_filesize returns an error (-1) we should return that error, and not pass it to copy_file_range_cmd().

Addresses-Coverity-ID: 1431684 ("Improper use of negative value")

---

io/copy_file_range.c +0 -0
index 4e2969c..bc891c9 100644

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 123

Quoted 3 lines
io/copy_file_range.c +0 -0
@@ -121,6 +121,10 @@ copy_range_f(int argc, char **argv)
 
 	if (src == 0 && dst == 0 && len == 0) {
 		len = copy_src_filesize(fd);

len is size_t but this function returns off64_t, so if src happened to be larger than 4GB on a 32-bit system we'll just rip the upper bits off the number and use that as the file size.

Granted I don't know that we care about 32-bit systems.

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 124

Quoted 1 line
io/copy_file_range.c +1 -0
+		if (len < 0) {

But that doesn't fix the problem, because len is size_t, which is unsigned, so this test is never true.

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 128

Quoted 4 lines
io/copy_file_range.c +3 -0
+			close(fd);
+			return 0;
+		}
 		copy_dst_truncate();

Ugh, nobody checked the return value of copy_dst_truncate, so if we can't truncate the destination file we just ignore that and keep going...

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 129

Quoted 1 line
io/copy_file_range.c +0 -0
 	}

...totally untested patch fixing all that nonsense below.

--D

From: Darrick J. Wong <[email protected]>
Subject: [PATCH] xfs_io: actually check copy file range helper return values

We need to check the return value of copy_src_filesize and copy_dst_truncate because either could return -1 due to fstat/ftruncate failure.

Fixes 628e112afdd98c5 ("xfs_io: implement 'copy_range' command")
---
 io/copy_file_range.c |   19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
io/copy_file_range.c +17 -2
index 4e2969c9..ed6fafb1 100644
@@ -120,11 +120,26 @@ copy_range_f(int argc, char **argv)
 		return 0;
 
 	if (src == 0 && dst == 0 && len == 0) {
-		len = copy_src_filesize(fd);
-		copy_dst_truncate();
+		off64_t	sz;
+
+		sz = copy_src_filesize(fd);
+		if (sz < 0) {
+			ret = sz;
+			goto out;
+		}
+		if ((unsigned long)sz > SIZE_MAX) {
+			ret = -EOVERFLOW;
+			goto out;
+		}
+		len = sz;
+
+		ret = copy_dst_truncate();
+		if (ret < 0)
+			goto out;
 	}
 
 	ret = copy_file_range_cmd(fd, &src, &dst, len);
+out:
 	close(fd);
 	return ret;
 }
#3 lore raw

Re: [PATCH 1/3] xfs_io: don't pass negative len to copy_file_range_cmd

Quoted 62 lines · Darrick J. Wong
Quoted 12 lines · Eric Sandeen

If copy_src_filesize returns an error (-1) we should return that error, and not pass it to copy_file_range_cmd().

Addresses-Coverity-ID: 1431684 ("Improper use of negative value")

---

io/copy_file_range.c +0 -0
index 4e2969c..bc891c9 100644

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 123

Quoted 3 lines
io/copy_file_range.c +0 -0
@@ -121,6 +121,10 @@ copy_range_f(int argc, char **argv)
 
 	if (src == 0 && dst == 0 && len == 0) {
 		len = copy_src_filesize(fd);

len is size_t but this function returns off64_t, so if src happened to be larger than 4GB on a 32-bit system we'll just rip the upper bits off the number and use that as the file size.

Granted I don't know that we care about 32-bit systems.

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 124

Quoted 1 line
io/copy_file_range.c +1 -0
+		if (len < 0) {

But that doesn't fix the problem, because len is size_t, which is unsigned, so this test is never true.

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 128

Quoted 4 lines
io/copy_file_range.c +3 -0
+			close(fd);
+			return 0;
+		}
 		copy_dst_truncate();

Ugh, nobody checked the return value of copy_dst_truncate, so if we can't truncate the destination file we just ignore that and keep going...

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 129

Quoted 1 line
io/copy_file_range.c +0 -0
 	}

...totally untested patch fixing all that nonsense below.

--D

From: Darrick J. Wong <[email protected]>
Subject: [PATCH] xfs_io: actually check copy file range helper return values

We need to check the return value of copy_src_filesize and copy_dst_truncate because either could return -1 due to fstat/ftruncate failure.

Fixes 628e112afdd98c5 ("xfs_io: implement 'copy_range' command")
---
 io/copy_file_range.c |   19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
io/copy_file_range.c +0 -0
index 4e2969c9..ed6fafb1 100644

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 127

Quoted 10 lines
io/copy_file_range.c +5 -2
@@ -120,11 +120,26 @@ copy_range_f(int argc, char **argv)
 		return 0;
 
 	if (src == 0 && dst == 0 && len == 0) {
-		len = copy_src_filesize(fd);
-		copy_dst_truncate();
+		off64_t	sz;
+
+		sz = copy_src_filesize(fd);
+		if (sz < 0) {
+			ret = sz;

urgh this is the "what do we return in xfs_io functions?" dilemma, but every other failure in this function returns 0 to the caller...

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 130

Quoted 3 lines
io/copy_file_range.c +3 -0
+			goto out;
+		}
+		if ((unsigned long)sz > SIZE_MAX) {

as mentioned on IRC this still has the same truncation problem on a 32-bit box.

-Eric

io/copy_file_range.c copy_range_f(int argc, char **argv) · line 137

Quoted 7 lines
io/copy_file_range.c +7 -0
+			ret = -EOVERFLOW;
+			goto out;
+		}
+		len = sz;
+
+		ret = copy_dst_truncate();
+		if (ret < 0)

ditto here, ret -1 or 0?

Quoted 9 lines
io/copy_file_range.c +2 -0
+			goto out;
 	}
 
 	ret = copy_file_range_cmd(fd, &src, &dst, len);
+out:
 	close(fd);
 	return ret;
 }
#4 lore raw

PATCH v2 1/1 xfs_io: actually check copy file range helper return values

In reply to Eric Sandeen

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

We need to check the return value of copy_src_filesize and copy_dst_truncate because either could return -1 due to fstat/ftruncate failure.

Fixes 628e112afdd98c5 ("xfs_io: implement 'copy_range' command")

---
v2: fix return values and overflow problems

---
 io/copy_file_range.c |   17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
io/copy_file_range.c +15 -2
index 4e2969c9..d069e5bb 100644
@@ -120,11 +120,24 @@ copy_range_f(int argc, char **argv)
 		return 0;
 
 	if (src == 0 && dst == 0 && len == 0) {
-		len = copy_src_filesize(fd);
-		copy_dst_truncate();
+		off64_t	sz;
+
+		sz = copy_src_filesize(fd);
+		if (sz < 0 || (unsigned long long)sz > SIZE_MAX) {
+			ret = 1;
+			goto out;
+		}
+		len = sz;
+
+		ret = copy_dst_truncate();
+		if (ret < 0) {
+			ret = 1;
+			goto out;
+		}
 	}
 
 	ret = copy_file_range_cmd(fd, &src, &dst, len);
+out:
 	close(fd);
 	return ret;
 }
Reviewed-by on v2 1/1
#5 lore raw

Re: [PATCH v2] xfs_io: actually check copy file range helper return values

Quoted 5 lines · Darrick J. Wong

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

We need to check the return value of copy_src_filesize and copy_dst_truncate because either could return -1 due to fstat/ftruncate failure.

Makes sense.

Quoted 41 lines
Fixes 628e112afdd98c5 ("xfs_io: implement 'copy_range' command")

---
v2: fix return values and overflow problems

---
 io/copy_file_range.c |   17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
io/copy_file_range.c +15 -2
index 4e2969c9..d069e5bb 100644
@@ -120,11 +120,24 @@ copy_range_f(int argc, char **argv)
 		return 0;
 
 	if (src == 0 && dst == 0 && len == 0) {
-		len = copy_src_filesize(fd);
-		copy_dst_truncate();
+		off64_t	sz;
+
+		sz = copy_src_filesize(fd);
+		if (sz < 0 || (unsigned long long)sz > SIZE_MAX) {
+			ret = 1;
+			goto out;
+		}
+		len = sz;
+
+		ret = copy_dst_truncate();
+		if (ret < 0) {
+			ret = 1;
+			goto out;
+		}
 	}
 
 	ret = copy_file_range_cmd(fd, &src, &dst, len);
+out:
 	close(fd);
 	return ret;
 }
#6 lore raw

PATCH 2/3 xfs_io: fix TOCTOU in openfile()

In reply to Eric Sandeen

openfile() stats a path to determine whether it is a pipe, and then opens it with flags based on the type it saw during the stat. It's possible that the file at that path changes in between, and Coverity points this out.

Instead, always open O_NONBLOCK, stat the fd we got, and turn the flag back off via fcntl() if it's not a pipe.

Addresses-Coverity-ID: 1442788 ("Time of check time of use")

---

io/open.c +22 -12
index f5fbd2c..856018b 100644
@@ -62,6 +62,12 @@ openfile(
 	int		oflags;
 
 	oflags = flags & IO_READONLY ? O_RDONLY : O_RDWR;
+	/*
+	 * In case we've been passed a pipe to open, don't block waiting for a
+	 * reader or writer to appear. We want to either succeed or error out
+	 * immediately.  We'll clear O_NONBLOCK if it's not a pipe.
+	 */
+	oflags |= O_NONBLOCK;
 	if (flags & IO_APPEND)
 		oflags |= O_APPEND;
 	if (flags & IO_CREAT)
@@ -81,18 +87,6 @@ openfile(
 	if (flags & IO_NOFOLLOW)
 		oflags |= O_NOFOLLOW;
 
-	/*
-	 * if we've been passed a pipe to open, don't block waiting for a
-	 * reader or writer to appear. We want to either succeed or error out
-	 * immediately.
-	 */
-	if (stat(path, &st) < 0 && errno != ENOENT) {
-		perror("stat");
-		return -1;
-	}
-	if (S_ISFIFO(st.st_mode))
-		oflags |= O_NONBLOCK;
-
 	fd = open(path, oflags, mode);
 	if (fd < 0) {
 		if (errno == EISDIR &&
@@ -112,6 +106,22 @@ openfile(
 		}
 	}
 
+	if (fstat(fd, &st) < 0) {
+		perror("stat");
+		close(fd);
+		return -1;
+	}
+
+	/* We only want to keep nonblocking behavior for pipes */
+	if (!S_ISFIFO(st.st_mode)) {
+		oflags &= ~O_NONBLOCK;
+		if (fcntl(fd, F_SETFL, oflags) < 0) {
+			perror("fcntl");
+			close(fd);
+			return -1;
+		}
+	}
+
 	if (!geom || !platform_test_xfs_fd(fd))
 		return fd;
 
#7 lore raw

Re: [PATCH 2/3] xfs_io: fix TOCTOU in openfile()

Quoted 9 lines · Eric Sandeen

openfile() stats a path to determine whether it is a pipe, and then opens it with flags based on the type it saw during the stat. It's possible that the file at that path changes in between, and Coverity points this out.

Instead, always open O_NONBLOCK, stat the fd we got, and turn the flag back off via fcntl() if it's not a pipe.

Addresses-Coverity-ID: 1442788 ("Time of check time of use")

For O_NONBLOCK I think there is zero harm in the code as it stands, but I don't really care about it tht much.

I do wonder what happens if need to conditionally use O_NONBLOCK on open(), though....

Quoted 4 lines
io/open.c +0 -0
@@ -112,6 +106,22 @@ openfile(
 		}
 	}
 

io/open.c openfile( · line 118

Quoted 10 lines
io/open.c +10 -0
+	if (fstat(fd, &st) < 0) {
+		perror("stat");
+		close(fd);
+		return -1;
+	}
+
+	/* We only want to keep nonblocking behavior for pipes */
+	if (!S_ISFIFO(st.st_mode)) {
+		oflags &= ~O_NONBLOCK;
+		if (fcntl(fd, F_SETFL, oflags) < 0) {

Don't you need to do oflags = fcntl(fd, F_GETFL, NULL) first?

io/open.c openfile( · line 122

Quoted 4 lines
io/open.c +4 -0
+			perror("fcntl");
+			close(fd);
+			return -1;
+		}

can you add a "goto out_close;" error jump to this function now that this is the fourth and fifth places that have this same close/return on error...

CHeers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#8 lore raw

Re: [PATCH 2/3] xfs_io: fix TOCTOU in openfile()

Quoted 21 lines · Dave Chinner
Quoted 9 lines · Eric Sandeen

openfile() stats a path to determine whether it is a pipe, and then opens it with flags based on the type it saw during the stat. It's possible that the file at that path changes in between, and Coverity points this out.

Instead, always open O_NONBLOCK, stat the fd we got, and turn the flag back off via fcntl() if it's not a pipe.

Addresses-Coverity-ID: 1442788 ("Time of check time of use")

For O_NONBLOCK I think there is zero harm in the code as it stands, but I don't really care about it tht much.

I do wonder what happens if need to conditionally use O_NONBLOCK on open(), though....

Quoted 4 lines
(quoted diff) +0 -0
@@ -112,6 +106,22 @@ openfile(
 		}
 	}
 
Quoted 12 lines

io/open.c openfile( · line 118

Quoted 10 lines
io/open.c +10 -0
+	if (fstat(fd, &st) < 0) {
+		perror("stat");
+		close(fd);
+		return -1;
+	}
+
+	/* We only want to keep nonblocking behavior for pipes */
+	if (!S_ISFIFO(st.st_mode)) {
+		oflags &= ~O_NONBLOCK;
+		if (fcntl(fd, F_SETFL, oflags) < 0) {

Don't you need to do oflags = fcntl(fd, F_GETFL, NULL) first?

maybe? We just opened it with oflags, but *shrug* I guess it doesn't hurt?

Quoted 9 lines

io/open.c openfile( · line 122

Quoted 5 lines
io/open.c +4 -0
+			perror("fcntl");
+			close(fd);
+			return -1;
+		}

can you add a "goto out_close;" error jump to this function now that this is the fourth and fifth places that have this same close/return on error...

yeah, good point.

Quoted 4 lines

CHeers,

Dave.

#9 lore raw

Re: [PATCH 2/3] xfs_io: fix TOCTOU in openfile()

Quoted 37 lines · Eric Sandeen
Quoted 21 lines · Dave Chinner
Quoted 9 lines · Eric Sandeen

openfile() stats a path to determine whether it is a pipe, and then opens it with flags based on the type it saw during the stat. It's possible that the file at that path changes in between, and Coverity points this out.

Instead, always open O_NONBLOCK, stat the fd we got, and turn the flag back off via fcntl() if it's not a pipe.

Addresses-Coverity-ID: 1442788 ("Time of check time of use")

For O_NONBLOCK I think there is zero harm in the code as it stands, but I don't really care about it tht much.

I do wonder what happens if need to conditionally use O_NONBLOCK on open(), though....

Quoted 4 lines
(quoted diff) +0 -0
@@ -112,6 +106,22 @@ openfile(
 		}
 	}
 
Quoted 12 lines

io/open.c openfile( · line 118

Quoted 10 lines
io/open.c +10 -0
+	if (fstat(fd, &st) < 0) {
+		perror("stat");
+		close(fd);
+		return -1;
+	}
+
+	/* We only want to keep nonblocking behavior for pipes */
+	if (!S_ISFIFO(st.st_mode)) {
+		oflags &= ~O_NONBLOCK;
+		if (fcntl(fd, F_SETFL, oflags) < 0) {

Don't you need to do oflags = fcntl(fd, F_GETFL, NULL) first?

Quoted 2 lines

maybe? We just opened it with oflags, but *shrug* I guess it doesn't hurt?

The kernel can set O_ flags on the file (e.g. O_LARGEFILE) for you, which means that if you try to F_SETFL with the original oflags, the kernel will think you're trying to clear those flags and misunderstand.

--D

Quoted 16 lines
Quoted 9 lines

io/open.c openfile( · line 122

Quoted 5 lines
io/open.c +4 -0
+			perror("fcntl");
+			close(fd);
+			return -1;
+		}

can you add a "goto out_close;" error jump to this function now that this is the fourth and fifth places that have this same close/return on error...

yeah, good point.

Quoted 4 lines

CHeers,

Dave.

#10 lore raw

Re: [PATCH 2/3] xfs_io: fix TOCTOU in openfile()

Quoted 6 lines · Darrick J. Wong
Quoted 3 lines
Quoted 1 line

Don't you need to do oflags = fcntl(fd, F_GETFL, NULL) first?

maybe? We just opened it with oflags, but *shrug* I guess it doesn't hurt?

The kernel can set O_ flags on the file (e.g. O_LARGEFILE) for you, which means that if you try to F_SETFL with the original oflags, the kernel will think you're trying to clear those flags and misunderstand.

Ok, thanks.

-Eric

#11 lore raw

PATCH 3/3 libhandle: zero terminate fspath string

In reply to Eric Sandeen

I had a heck of a time chasing through whether we were null terminated by now or not, so just be safe and do it explicitly.

Addresses-Coverity-ID: 1297520 ("Buffer not null terminated")

---

libhandle/handle.c +1 -0
index eb099f4..d3927a3 100644
@@ -108,6 +108,7 @@ path_to_fshandle(
 
 		fdhp->fsfd = fd;
 		strncpy(fdhp->fspath, fspath, sizeof(fdhp->fspath));
+		fdhp->fspath[sizeof(fdhp->fspath) - 1] = '\0';
 		memcpy(fdhp->fsh, *fshanp, FSIDSIZE);
 
 		fdhp->fnxt = fdhash_head;
Reviewed-by on v1 3/3
#12 lore raw

Re: [PATCH 3/3] libhandle: zero terminate fspath string

Quoted 5 lines · Eric Sandeen

I had a heck of a time chasing through whether we were null terminated by now or not, so just be safe and do it explicitly.

Addresses-Coverity-ID: 1297520 ("Buffer not null terminated")

Looks ok,

--D

Quoted 15 lines

---

libhandle/handle.c +1 -0
index eb099f4..d3927a3 100644
@@ -108,6 +108,7 @@ path_to_fshandle(
 
 		fdhp->fsfd = fd;
 		strncpy(fdhp->fspath, fspath, sizeof(fdhp->fspath));
+		fdhp->fspath[sizeof(fdhp->fspath) - 1] = '\0';
 		memcpy(fdhp->fsh, *fshanp, FSIDSIZE);
 
 		fdhp->fnxt = fdhash_head;