Skip to contentSkip to conversation

softmmu/physmem.c: Check return value from realpath()

PATCH Applied Peter Maydell 3 participants 3 messages Aug 12, 2021 – Aug 13, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 softmmu/physmem.c: Check return value from realpath()

The realpath() function can return NULL on error, so we need to check for it to avoid crashing when we try to strstr() into it. This can happen if we run out of memory, or if /sys/ is not mounted, among other situations.

Fixes Coverity 1459913, 1460474
Fixes ce317be98db0 ("exec: fetch the alignment of Linux devdax pmem character device nodes")
---
 softmmu/physmem.c | 3 +++
 1 file changed, 3 insertions(+)
softmmu/physmem.c +3 -0
index c47cb6da2e4..eb0595d57c4 100644
@@ -1451,6 +1451,9 @@ static int64_t get_file_align(int fd)
         path = g_strdup_printf("/sys/dev/char/%d:%d",
                     major(st.st_rdev), minor(st.st_rdev));
         rpath = realpath(path, NULL);
+        if (!rpath) {
+            return -errno;
+        }
 
         rc = daxctl_new(&ctx);
         if (rc) {
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Peter Maydell

The realpath() function can return NULL on error, so we need to check for it to avoid crashing when we try to strstr() into it. This can happen if we run out of memory, or if /sys/ is not mounted, among other situations.

Fixes Coverity 1459913, 1460474
Fixes ce317be98db0 ("exec: fetch the alignment of Linux devdax pmem character device nodes")
---
  softmmu/physmem.c | 3 +++
  1 file changed, 3 insertions(+)
softmmu/physmem.c +0 -0
index c47cb6da2e4..eb0595d57c4 100644

softmmu/physmem.c static int64_t get_file_align(int fd) · line 1456

Quoted 6 lines
softmmu/physmem.c +3 -0
@@ -1451,6 +1451,9 @@ static int64_t get_file_align(int fd)
          path = g_strdup_printf("/sys/dev/char/%d:%d",
                      major(st.st_rdev), minor(st.st_rdev));
          rpath = realpath(path, NULL);
+        if (!rpath) {
+            return -errno;
+        }

Good find!

Thanks,
Jingqi

#2 lore raw
In reply to Peter Maydell
Quoted 18 lines · Peter Maydell

The realpath() function can return NULL on error, so we need to check for it to avoid crashing when we try to strstr() into it. This can happen if we run out of memory, or if /sys/ is not mounted, among other situations.

Fixes Coverity 1459913, 1460474
Fixes ce317be98db0 ("exec: fetch the alignment of Linux devdax pmem character device nodes")
---
  softmmu/physmem.c | 3 +++
  1 file changed, 3 insertions(+)
softmmu/physmem.c +0 -0
index c47cb6da2e4..eb0595d57c4 100644

softmmu/physmem.c static int64_t get_file_align(int fd) · line 1459

Quoted 9 lines
softmmu/physmem.c +3 -0
@@ -1451,6 +1451,9 @@ static int64_t get_file_align(int fd)
          path = g_strdup_printf("/sys/dev/char/%d:%d",
                      major(st.st_rdev), minor(st.st_rdev));
          rpath = realpath(path, NULL);
+        if (!rpath) {
+            return -errno;
+        }
 
          rc = daxctl_new(&ctx);
          if (rc) {

Queued, thanks.

Paolo