Skip to contentSkip to conversation

libndctl: fix potential buffer overflow in write_cache APIs

PATCH In review Vishal Verma 2 participants 7 messages May 3, 2018 – May 4, 2018 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

PATCH 1/4 libndctl: fix potential buffer overflow in write_cache APIs

We used a local stack variable to hold the sysfs path, which had a potential to overflow. Instead, switch to the 'scratch space' bdbs->buf to store the sysfs path as it is correctly sized for it.

---
 ndctl/lib/libndctl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ndctl/lib/libndctl.c +2 -2
index 59ea82a..2a3ef0c 100644
@@ -3991,10 +3991,10 @@ static int __ndctl_namespace_set_write_cache(struct ndctl_namespace *ndns,
 {
 	struct ndctl_ctx *ctx = ndctl_namespace_get_ctx(ndns);
 	struct ndctl_pfn *pfn = ndctl_namespace_get_pfn(ndns);
+	char *path = ndns->ndns_buf;
 	char buf[SYSFS_ATTR_SIZE];
 	int len = ndns->buf_len;
 	const char *bdev;
-	char path[50];
 
 	if (state != 1 && state != 0)
 		return -ENXIO;
@@ -4034,9 +4034,9 @@ NDCTL_EXPORT int ndctl_namespace_write_cache_is_enabled(
 	struct ndctl_ctx *ctx = ndctl_namespace_get_ctx(ndns);
 	struct ndctl_pfn *pfn = ndctl_namespace_get_pfn(ndns);
 	int len = ndns->buf_len, wc;
+	char *path = ndns->ndns_buf;
 	char buf[SYSFS_ATTR_SIZE];
 	const char *bdev;
-	char path[50];
 
 	if (pfn)
 		bdev = ndctl_pfn_get_block_device(pfn);
Signature · 6 lines
2.14.3

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
author maintainer
#1 lore raw

PATCH 2/4 libndctl: improve debug prints in wait_for_scrub_completion

ndctl_bus_wait_for_scrub_completion() printed the same debug message irrespective of whether we encountered a failure or not. Add a newe debug message that is printed for failing cases.

---
 ndctl/lib/libndctl.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
ndctl/lib/libndctl.c +5 -1
index 2a3ef0c..6733b85 100644
@@ -1248,7 +1248,11 @@ NDCTL_EXPORT int ndctl_bus_wait_for_scrub_completion(struct ndctl_bus *bus)
 		}
 	}
 
-	dbg(ctx, "bus%d: scrub complete\n", ndctl_bus_get_id(bus));
+	if (rc == 0)
+		dbg(ctx, "bus%d: scrub complete\n", ndctl_bus_get_id(bus));
+	else
+		dbg(ctx, "bus%d: error waiting for scrub completion: %s\n",
+			ndctl_bus_get_id(bus), strerror(-rc));
 	if (fd)
 		close (fd);
 	return rc;
Signature · 6 lines
2.14.3

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v1 2/4
#2 lore raw

Re: [ndctl PATCH 2/4] libndctl: improve debug prints in wait_for_scrub_completion

Quoted 2 lines · Vishal Verma

ndctl_bus_wait_for_scrub_completion() printed the same debug message irrespective of whether we encountered a failure or not. Add a newe

*new

Quoted 26 lines

debug message that is printed for failing cases.

---
 ndctl/lib/libndctl.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
ndctl/lib/libndctl.c +5 -2
index 2a3ef0c..6733b85 100644
@@ -1248,7 +1248,11 @@ NDCTL_EXPORT int ndctl_bus_wait_for_scrub_completion(struct ndctl_bus *bus)
                }
        }
 
-       dbg(ctx, "bus%d: scrub complete\n", ndctl_bus_get_id(bus));
+       if (rc == 0)
+               dbg(ctx, "bus%d: scrub complete\n", ndctl_bus_get_id(bus));
+       else
+               dbg(ctx, "bus%d: error waiting for scrub completion: %s\n",
+                       ndctl_bus_get_id(bus), strerror(-rc));
        if (fd)
                close (fd);
        return rc;
--
Quoted 2 lines

2.14.3

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author maintainer
#3 lore raw

PATCH 3/4 libndctl, test: fix a couple of unchecked returns

In reply to Vishal Verma

-Wunused-result with -D_FORTIFY_SOURCE=2 reported a couple of unchecked return values, fix them by appropriately failing on errors.

---
 ndctl/lib/libndctl.c | 5 ++++-
 test/dax-pmd.c       | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
ndctl/lib/libndctl.c +4 -1
index 6733b85..47e005e 100644
@@ -1243,7 +1243,10 @@ NDCTL_EXPORT int ndctl_bus_wait_for_scrub_completion(struct ndctl_bus *bus)
 				break;
 			}
 			dbg(ctx, "poll wake: revents: %d\n", fds.revents);
-			pread(fd, buf, 1, 0);
+			if (pread(fd, buf, 1, 0) == -1) {
+				rc = -errno;
+				break;
+			}
 			fds.revents = 0;
 		}
 	}
test/dax-pmd.c +4 -1
index 06fe522..65bee6f 100644
@@ -125,7 +125,10 @@ int test_dax_directio(int dax_fd, unsigned long align, void *dax_addr, off_t off
 				rc = -ENXIO;
 			}
 			((char *) buf)[0] = 0;
-			pread(fd2, buf, 4096, 0);
+			if (pread(fd2, buf, 4096, 0) != 4096) {
+				faili(i);
+				rc = -ENXIO;
+			}
 			if (strcmp(buf, "odirect data") != 0) {
 				faili(i);
 				rc = -ENXIO;
Signature · 6 lines
2.14.3

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v1 3/4
#4 lore raw

Re: [ndctl PATCH 3/4] libndctl, test: fix a couple of unchecked returns

Quoted 2 lines · Vishal Verma

-Wunused-result with -D_FORTIFY_SOURCE=2 reported a couple of unchecked return values, fix them by appropriately failing on errors.

*Building with '-Wunused-result -D_FORTIFY_SOURCE=2' reports...

Quoted 30 lines
---
 ndctl/lib/libndctl.c | 5 ++++-
 test/dax-pmd.c       | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
ndctl/lib/libndctl.c +4 -1
index 6733b85..47e005e 100644
@@ -1243,7 +1243,10 @@ NDCTL_EXPORT int ndctl_bus_wait_for_scrub_completion(struct ndctl_bus *bus)
                                break;
                        }
                        dbg(ctx, "poll wake: revents: %d\n", fds.revents);
-                       pread(fd, buf, 1, 0);
+                       if (pread(fd, buf, 1, 0) == -1) {
+                               rc = -errno;
+                               break;
+                       }
                        fds.revents = 0;
                }
        }
test/dax-pmd.c +0 -0
index 06fe522..65bee6f 100644
@@ -125,7 +125,10 @@ int test_dax_directio(int dax_fd, unsigned long align, void *dax_addr, off_t off
                                rc = -ENXIO;

test/dax-pmd.c int test_dax_directio(int dax_fd, unsigned long align, void *dax_addr, off_t off · line 134

Quoted 10 lines
test/dax-pmd.c +4 -1
                        }
                        ((char *) buf)[0] = 0;
-                       pread(fd2, buf, 4096, 0);
+                       if (pread(fd2, buf, 4096, 0) != 4096) {
+                               faili(i);
+                               rc = -ENXIO;
+                       }
                        if (strcmp(buf, "odirect data") != 0) {
                                faili(i);
                                rc = -ENXIO;

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author maintainer
#5 lore raw

PATCH 4/4 configure: add -Wunused-result and -D_FORTIFY_SOURCE=2 to cflags

In reply to Vishal Verma

rpmbuild had these flags enabled, and reported a couple of errors that local builds otherwise didn't. The errors hav now been fixed, and we can enable these flags to catch future warnings and errors through these flags.

---
 configure.ac | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
configure.ac +3 -1
index cddad16..6dbfcee 100644
@@ -147,7 +147,9 @@ my_CFLAGS="\
 -Wsign-compare \
 -Wstrict-prototypes \
 -Wtype-limits \
--Wmaybe-uninitialized
+-Wmaybe-uninitialized \
+-Wunused-result \
+-D_FORTIFY_SOURCE=2
 "
 AC_SUBST([my_CFLAGS])
 
Signature · 6 lines
2.14.3

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v1 4/4
#6 lore raw

Re: [ndctl PATCH 4/4] configure: add -Wunused-result and -D_FORTIFY_SOURCE=2 to cflags

Quoted 2 lines · Vishal Verma

rpmbuild had these flags enabled, and reported a couple of errors that local builds otherwise didn't. The errors hav now been fixed, and we

*have

Quoted 14 lines

can enable these flags to catch future warnings and errors through these flags.

---
 configure.ac | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
configure.ac +0 -0
index cddad16..6dbfcee 100644

configure.ac my_CFLAGS="\ · line 154

Quoted 9 lines
configure.ac +3 -1
@@ -147,7 +147,9 @@ my_CFLAGS="\
 -Wsign-compare \
 -Wstrict-prototypes \
 -Wtype-limits \
--Wmaybe-uninitialized
+-Wmaybe-uninitialized \
+-Wunused-result \
+-D_FORTIFY_SOURCE=2
 "
 AC_SUBST([my_CFLAGS])

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm