Skip to contentSkip to conversation

9pfs: Fix segfault in do_readdir_many caused by struct dirent overread

PATCH Superseded Vitaly Chikunov 5 participants 18 messages Feb 4, 2022 – Feb 5, 2022

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 9pfs: Fix segfault in do_readdir_many caused by struct dirent overread

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

 #0  0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 + 0x497eed)
 #1  0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9)
 #2  0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983)
 #3  0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

 hw/9pfs/codir.c      |  3 +--
 include/qemu/osdep.h | 13 +++++++++++++
 util/osdep.c         | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/9pfs/codir.c +1 -2
index 032cce04c4..c0873bde16 100644
@@ -143,8 +143,7 @@ static int do_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp,
         } else {
             e = e->next = g_malloc0(sizeof(V9fsDirEnt));
         }
-        e->dent = g_malloc0(sizeof(struct dirent));
-        memcpy(e->dent, dent, sizeof(struct dirent));
+        e->dent = qemu_dirent_dup(dent);
 
         /* perform a full stat() for directory entry if requested by caller */
         if (dostat) {
include/qemu/osdep.h +13 -0
index d1660d67fa..ce12f64853 100644
@@ -805,6 +805,19 @@ static inline int platform_does_not_support_system(const char *command)
 }
 #endif /* !HAVE_SYSTEM_FUNCTION */
 
+/**
+ * Duplicate directory entry @dent.
+ *
+ * It is highly recommended to use this function instead of open coding
+ * duplication of @c dirent objects, because the actual @c struct @c dirent
+ * size may be bigger or shorter than @c sizeof(struct dirent) and correct
+ * handling is platform specific (see gitlab issue #841).
+ *
+ * @dent - original directory entry to be duplicated
+ * @returns duplicated directory entry which should be freed with g_free()
+ */
+struct dirent *qemu_dirent_dup(struct dirent *dent);
+
 #ifdef __cplusplus
 }
 #endif
util/osdep.c +18 -0
index 42a0a4986a..2c80528a61 100644
@@ -33,6 +33,7 @@
 extern int madvise(char *, size_t, int);
 #endif
 
+#include <dirent.h>
 #include "qemu-common.h"
 #include "qemu/cutils.h"
 #include "qemu/sockets.h"
@@ -615,3 +616,20 @@ writev(int fd, const struct iovec *iov, int iov_cnt)
     return readv_writev(fd, iov, iov_cnt, true);
 }
 #endif
+
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}
Signature · 1 line
2.33.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · On Freitag, 4. Februar 2022 06:06:09 CET Vitaly Chikunov wrote:

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

#0 0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 +
0x497eed) #1 0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64
+ 0x4982e9) #2 0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983) #3 0x00007ffff73e0be0 n/a (n/a + 0x0)

Quoted 8 lines

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Tests are passing now, no other issues encountered during some quick tests on my side. So looks good to me, thanks!

Best regards,
Christian Schoenebeck

Quoted 81 lines

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

 hw/9pfs/codir.c      |  3 +--
 include/qemu/osdep.h | 13 +++++++++++++
 util/osdep.c         | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/9pfs/codir.c +1 -2
index 032cce04c4..c0873bde16 100644
@@ -143,8 +143,7 @@ static int do_readdir_many(V9fsPDU *pdu, V9fsFidState
 *fidp, } else {
             e = e->next = g_malloc0(sizeof(V9fsDirEnt));
         }
-        e->dent = g_malloc0(sizeof(struct dirent));
-        memcpy(e->dent, dent, sizeof(struct dirent));
+        e->dent = qemu_dirent_dup(dent);
 
         /* perform a full stat() for directory entry if requested by caller
 */ if (dostat) {
include/qemu/osdep.h +13 -0
index d1660d67fa..ce12f64853 100644
@@ -805,6 +805,19 @@ static inline int
 platform_does_not_support_system(const char *command) }
 #endif /* !HAVE_SYSTEM_FUNCTION */
 
+/**
+ * Duplicate directory entry @dent.
+ *
+ * It is highly recommended to use this function instead of open coding
+ * duplication of @c dirent objects, because the actual @c struct @c dirent
+ * size may be bigger or shorter than @c sizeof(struct dirent) and correct
+ * handling is platform specific (see gitlab issue #841).
+ *
+ * @dent - original directory entry to be duplicated
+ * @returns duplicated directory entry which should be freed with g_free()
+ */
+struct dirent *qemu_dirent_dup(struct dirent *dent);
+
 #ifdef __cplusplus
 }
 #endif
util/osdep.c +18 -0
index 42a0a4986a..2c80528a61 100644
@@ -33,6 +33,7 @@
 extern int madvise(char *, size_t, int);
 #endif
 
+#include <dirent.h>
 #include "qemu-common.h"
 #include "qemu/cutils.h"
 #include "qemu/sockets.h"
@@ -615,3 +616,20 @@ writev(int fd, const struct iovec *iov, int iov_cnt)
     return readv_writev(fd, iov, iov_cnt, true);
 }
 #endif
+
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}
Reviewed-by
#3 lore raw
In reply to Dmitry V. Levin
Quoted 93 lines · Dmitry V. Levin
Quoted 92 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

 #0  0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 + 0x497eed)
 #1  0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9)
 #2  0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983)
 #3  0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

 hw/9pfs/codir.c      |  3 +--
 include/qemu/osdep.h | 13 +++++++++++++
 util/osdep.c         | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/9pfs/codir.c +1 -2
index 032cce04c4..c0873bde16 100644
@@ -143,8 +143,7 @@ static int do_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp,
         } else {
             e = e->next = g_malloc0(sizeof(V9fsDirEnt));
         }
-        e->dent = g_malloc0(sizeof(struct dirent));
-        memcpy(e->dent, dent, sizeof(struct dirent));
+        e->dent = qemu_dirent_dup(dent);
 
         /* perform a full stat() for directory entry if requested by caller */
         if (dostat) {
include/qemu/osdep.h +13 -0
index d1660d67fa..ce12f64853 100644
@@ -805,6 +805,19 @@ static inline int platform_does_not_support_system(const char *command)
 }
 #endif /* !HAVE_SYSTEM_FUNCTION */
 
+/**
+ * Duplicate directory entry @dent.
+ *
+ * It is highly recommended to use this function instead of open coding
+ * duplication of @c dirent objects, because the actual @c struct @c dirent
+ * size may be bigger or shorter than @c sizeof(struct dirent) and correct
+ * handling is platform specific (see gitlab issue #841).
+ *
+ * @dent - original directory entry to be duplicated
+ * @returns duplicated directory entry which should be freed with g_free()
+ */
+struct dirent *qemu_dirent_dup(struct dirent *dent);
+
 #ifdef __cplusplus
 }
 #endif
util/osdep.c +12 -0
index 42a0a4986a..2c80528a61 100644
@@ -33,6 +33,7 @@
 extern int madvise(char *, size_t, int);
 #endif
 
+#include <dirent.h>
 #include "qemu-common.h"
 #include "qemu/cutils.h"
 #include "qemu/sockets.h"
@@ -615,3 +616,20 @@ writev(int fd, const struct iovec *iov, int iov_cnt)
     return readv_writev(fd, iov, iov_cnt, true);
 }
 #endif
+
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {
+        /* Fallback to the most portable way. */
Quoted 8 lines

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 635

Quoted 6 lines
util/osdep.c +6 -0
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}

There should be no '"', sorry about that. :)

Signature · 1 line
ldv
Reviewed-by on v3 1/1
#2 lore raw
In reply to Vitaly Chikunov
Quoted 88 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

 #0  0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 + 0x497eed)
 #1  0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9)
 #2  0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983)
 #3  0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

 hw/9pfs/codir.c      |  3 +--
 include/qemu/osdep.h | 13 +++++++++++++
 util/osdep.c         | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/9pfs/codir.c +1 -2
index 032cce04c4..c0873bde16 100644
@@ -143,8 +143,7 @@ static int do_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp,
         } else {
             e = e->next = g_malloc0(sizeof(V9fsDirEnt));
         }
-        e->dent = g_malloc0(sizeof(struct dirent));
-        memcpy(e->dent, dent, sizeof(struct dirent));
+        e->dent = qemu_dirent_dup(dent);
 
         /* perform a full stat() for directory entry if requested by caller */
         if (dostat) {
include/qemu/osdep.h +13 -0
index d1660d67fa..ce12f64853 100644
@@ -805,6 +805,19 @@ static inline int platform_does_not_support_system(const char *command)
 }
 #endif /* !HAVE_SYSTEM_FUNCTION */
 
+/**
+ * Duplicate directory entry @dent.
+ *
+ * It is highly recommended to use this function instead of open coding
+ * duplication of @c dirent objects, because the actual @c struct @c dirent
+ * size may be bigger or shorter than @c sizeof(struct dirent) and correct
+ * handling is platform specific (see gitlab issue #841).
+ *
+ * @dent - original directory entry to be duplicated
+ * @returns duplicated directory entry which should be freed with g_free()
+ */
+struct dirent *qemu_dirent_dup(struct dirent *dent);
+
 #ifdef __cplusplus
 }
 #endif
util/osdep.c +8 -0
index 42a0a4986a..2c80528a61 100644
@@ -33,6 +33,7 @@
 extern int madvise(char *, size_t, int);
 #endif
 
+#include <dirent.h>
 #include "qemu-common.h"
 #include "qemu/cutils.h"
 #include "qemu/sockets.h"
@@ -615,3 +616,20 @@ writev(int fd, const struct iovec *iov, int iov_cnt)
     return readv_writev(fd, iov, iov_cnt, true);
 }
 #endif
+
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 635

Quoted 10 lines
util/osdep.c +10 -0
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}
Signature · 1 line
ldv
#4 lore raw
In reply to Vitaly Chikunov
Quoted 27 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

  #0  0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 + 0x497eed)
  #1  0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9)
  #2  0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983)
  #3  0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 635

Quoted 7 lines
util/osdep.c +7 -0
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}

What about this?

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
    size_t sz;
#if defined _DIRENT_HAVE_D_RECLEN
    /* Avoid use of strlen() if there's d_reclen. */
    sz = dent->d_reclen;
#else
    /* Fallback to the most portable way. */
    sz = offsetof(struct dirent, d_name) +
                  strlen(dent->d_name) + 1;

#endif

    return g_memdup(dent, sz);
}
#5 lore raw
Quoted 64 lines · On Freitag, 4. Februar 2022 14:55:45 CET Philippe Mathieu-Daudé via wrote:
Quoted 29 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread

into unallocated page causing SIGSEGV. Example stack trace:
#0 0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64
+ 0x497eed) #1 0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9) #2 0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983) #3 0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was

caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 635

Quoted 7 lines
util/osdep.c +7 -0
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}

What about this?

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
     size_t sz;
#if defined _DIRENT_HAVE_D_RECLEN
     /* Avoid use of strlen() if there's d_reclen. */
     sz = dent->d_reclen;
#else
     /* Fallback to the most portable way. */
     sz = offsetof(struct dirent, d_name) +
                   strlen(dent->d_name) + 1;
Quoted 4 lines

#endif

     return g_memdup(dent, sz);
}

That was the intentional change v2 -> v3 Philippe. Previous v2 crashed the 9p 'synth' tests:

https://lore.kernel.org/qemu-devel/2627488.0S70g7mNYN@silver/T/#ma09bedde59a91e6435443e151f7e49fef8616e4c

You might argue that the 9p 'synth' driver should instead populate d_reclen instead, but this v3 is a simpler solution and guarantees to always work. So I'd prefer to go with Vitaly's v3 for now, especially as this patch would go to the stable branches as well.

Best regards,
Christian Schoenebeck

#6 lore raw
Quoted 73 lines · On Fri, 04 Feb 2022 15:12:18 +0100 Christian Schoenebeck…
Quoted 64 lines · On Freitag, 4. Februar 2022 14:55:45 CET Philippe Mathieu-Daudé via wrote:
Quoted 29 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread

into unallocated page causing SIGSEGV. Example stack trace:
#0 0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64
+ 0x497eed) #1 0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9) #2 0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983) #3 0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was

caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 635

Quoted 7 lines
util/osdep.c +7 -0
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}

What about this?

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
     size_t sz;
#if defined _DIRENT_HAVE_D_RECLEN
     /* Avoid use of strlen() if there's d_reclen. */
     sz = dent->d_reclen;
#else
     /* Fallback to the most portable way. */
     sz = offsetof(struct dirent, d_name) +
                   strlen(dent->d_name) + 1;
Quoted 4 lines

#endif

     return g_memdup(dent, sz);
}

That was the intentional change v2 -> v3 Philippe. Previous v2 crashed the 9p 'synth' tests:

Quoted 7 lines

https://lore.kernel.org/qemu-devel/2627488.0S70g7mNYN@silver/T/#ma09bedde59a91e6435443e151f7e49fef8616e4c

You might argue that the 9p 'synth' driver should instead populate d_reclen instead, but this v3 is a simpler solution and guarantees to always work. So I'd prefer to go with Vitaly's v3 for now, especially as this patch would go to the stable branches as well.

This really is a band-aid. Anyone reading this will react as Philippe, and this is common code, not just 9pfs. With correctly populated dirents, the helper could be as simple as:

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
    size_t sz = offsetof(struct dirent, d_name) + _D_EXACT_NAMLEN(dent) + 1;

    return g_memdup(dent, sz);
}

If this is a cycles problem, I can help reviewing the fixes on the synth driver, or alternatively you can keep this code somewhere under 9pfs but please don't put that in common utils.

Cheers,

--
Greg

Quoted 4 lines

Best regards,
Christian Schoenebeck

#7 lore raw
Quoted 98 lines · On Freitag, 4. Februar 2022 16:16:06 CET Greg Kurz wrote:

On Fri, 04 Feb 2022 15:12:18 +0100

Quoted 82 lines · Christian Schoenebeck
Quoted 72 lines · On Freitag, 4. Februar 2022 14:55:45 CET Philippe Mathieu-Daudé via wrote:
Quoted 31 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread

into unallocated page causing SIGSEGV. Example stack trace:
#0 0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64
+ 0x497eed) #1 0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9) #2 0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983) #3 0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was

caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 635

Quoted 7 lines
util/osdep.c +7 -0
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}

What about this?

struct dirent *
qemu_dirent_dup(struct dirent *dent)
{

     size_t sz;

#if defined _DIRENT_HAVE_D_RECLEN

     /* Avoid use of strlen() if there's d_reclen. */
     sz = dent->d_reclen;

#else

     /* Fallback to the most portable way. */
     sz = offsetof(struct dirent, d_name) +
     
                   strlen(dent->d_name) + 1;
Quoted 5 lines

#endif

     return g_memdup(dent, sz);

}

That was the intentional change v2 -> v3 Philippe. Previous v2 crashed the 9p 'synth' tests:

Quoted 8 lines

https://lore.kernel.org/qemu-devel/2627488.0S70g7mNYN@silver/T/#ma09bedde5
9a91e6435443e151f7e49fef8616e4c

You might argue that the 9p 'synth' driver should instead populate d_reclen
instead, but this v3 is a simpler solution and guarantees to always work. So I'd prefer to go with Vitaly's v3 for now, especially as this patch would go to the stable branches as well.

This really is a band-aid. Anyone reading this will react as Philippe, and this is common code, not just 9pfs. With correctly populated dirents, the helper could be as simple as:

Quoted 7 lines

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
    size_t sz = offsetof(struct dirent, d_name) + _D_EXACT_NAMLEN(dent) + 1;

    return g_memdup(dent, sz);
}

Which requires _D_EXACT_NAMLEN being defined, whereas Vitaly's solution ensures to work on all systems.

Quoted 3 lines

If this is a cycles problem, I can help reviewing the fixes on the synth driver, or alternatively you can keep this code somewhere under 9pfs but please don't put that in common utils.

Who cares, sends a patch, and makes sure that it works at least as reliably as the currently suggested solution (tested with guest and test cases).

Thanks!

Best regards,
Christian Schoenebeck

#8 lore raw
In reply to Greg Kurz

Greg,

Quoted 84 lines · Greg Kurz
Quoted 73 lines · On Fri, 04 Feb 2022 15:12:18 +0100 Christian Schoenebeck…
Quoted 64 lines · On Freitag, 4. Februar 2022 14:55:45 CET Philippe Mathieu-Daudé via wrote:
Quoted 29 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread

into unallocated page causing SIGSEGV. Example stack trace:
#0 0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64
+ 0x497eed) #1 0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9) #2 0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983) #3 0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was

caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 635

Quoted 7 lines
util/osdep.c +7 -0
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}

What about this?

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
     size_t sz;
#if defined _DIRENT_HAVE_D_RECLEN
     /* Avoid use of strlen() if there's d_reclen. */
     sz = dent->d_reclen;
#else
     /* Fallback to the most portable way. */
     sz = offsetof(struct dirent, d_name) +
                   strlen(dent->d_name) + 1;
Quoted 4 lines

#endif

     return g_memdup(dent, sz);
}

That was the intentional change v2 -> v3 Philippe. Previous v2 crashed the 9p 'synth' tests:

Quoted 7 lines

https://lore.kernel.org/qemu-devel/2627488.0S70g7mNYN@silver/T/#ma09bedde59a91e6435443e151f7e49fef8616e4c

You might argue that the 9p 'synth' driver should instead populate d_reclen instead, but this v3 is a simpler solution and guarantees to always work. So I'd prefer to go with Vitaly's v3 for now, especially as this patch would go to the stable branches as well.

Quoted 8 lines

This really is a band-aid. Anyone reading this will react as Philippe, and this is common code, not just 9pfs. With correctly populated dirents, the helper could be as simple as:

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
    size_t sz = offsetof(struct dirent, d_name) + _D_EXACT_NAMLEN(dent) + 1;

But d_namlen is not populated by synth_direntry, so this will lead to a bug too. Idea is that qemu_dirent_dup handles real dirents and simulated (underpopulated) dirents.

Also Linux does not have d_namlen AFAIK, thus this code will not provide any speed up in most cases (and always fallback to strlen), unlike if we use d_reclen.

Also, I m not sure if _D_EXACT_NAMLEN is defined on all systems, so this needs ifdefs too.

Quoted 5 lines
    return g_memdup(dent, sz);
}

If this is a cycles problem,

If you don't like d_reclen speed ups, we can always just use most portable strlen method.

Vitaly,

Quoted 13 lines

I can help reviewing the fixes on
the synth driver, or alternatively you can keep this code somewhere under 9pfs but please don't put that in common utils.

Cheers,

--
Greg

Quoted 4 lines

Best regards,
Christian Schoenebeck

#9 lore raw

On Fri, Feb 04, 2022 at 06:32:07PM +0300, Vitaly Chikunov wrote:
[...]

Quoted 9 lines
Quoted 4 lines

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
    size_t sz = offsetof(struct dirent, d_name) + _D_EXACT_NAMLEN(dent) + 1;

But d_namlen is not populated by synth_direntry, so this will lead to a bug too. Idea is that qemu_dirent_dup handles real dirents and simulated (underpopulated) dirents.

Quoted 6 lines

Also Linux does not have d_namlen AFAIK, thus this code will not provide any speed up in most cases (and always fallback to strlen), unlike if we use d_reclen.

Also, I m not sure if _D_EXACT_NAMLEN is defined on all systems, so this needs ifdefs too.

Yes, _D_EXACT_NAMLEN() is a GNU extension, it was introduced in glibc back in 1996 but some popular libcs available for Linux do not provide this macro.

Signature · 1 line
ldv
#10 lore raw
Quoted 15 lines · Dmitry V. Levin

On Fri, Feb 04, 2022 at 06:32:07PM +0300, Vitaly Chikunov wrote:
[...]

Quoted 13 lines
Quoted 4 lines

struct dirent *
qemu_dirent_dup(struct dirent *dent)

{
     size_t sz = offsetof(struct dirent, d_name) + _D_EXACT_NAMLEN(dent) + 1;

But d_namlen is not populated by synth_direntry, so this will lead to a bug too. Idea is that qemu_dirent_dup handles real dirents and simulated (underpopulated) dirents.

Also Linux does not have d_namlen AFAIK, thus this code will not provide any speed up in most cases (and always fallback to strlen), unlike if we use d_reclen.

Quoted 6 lines
Quoted 2 lines

Also, I m not sure if _D_EXACT_NAMLEN is defined on all systems, so this needs ifdefs too.

Yes, _D_EXACT_NAMLEN() is a GNU extension, it was introduced in glibc back in 1996 but some popular libcs available for Linux do not provide this macro.

Can't we define _D_EXACT_NAMLEN() if not available?

#11 lore raw
Quoted 19 lines · On Freitag, 4. Februar 2022 16:54:12 CET Philippe Mathieu-Daudé wrote:
Quoted 18 lines · Dmitry V. Levin

On Fri, Feb 04, 2022 at 06:32:07PM +0300, Vitaly Chikunov wrote:
[...]

Quoted 15 lines
Quoted 6 lines

struct dirent *
qemu_dirent_dup(struct dirent *dent)
{

size_t sz = offsetof(struct dirent, d_name) + _D_EXACT_NAMLEN(dent)
+ 1;

But d_namlen is not populated by synth_direntry, so this will lead to a bug too. Idea is that qemu_dirent_dup handles real dirents and simulated (underpopulated) dirents.

Also Linux does not have d_namlen AFAIK, thus this code will not provide any speed up in most cases (and always fallback to strlen), unlike if we use d_reclen.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Also, I m not sure if _D_EXACT_NAMLEN is defined on all systems, so this needs ifdefs too.

Yes, _D_EXACT_NAMLEN() is a GNU extension, it was introduced in glibc back in 1996 but some popular libcs available for Linux do not provide this macro.

Can't we define _D_EXACT_NAMLEN() if not available?

It is not that trivial.

With recent macOS patch set in mind: macOS does not have any of these macros either. It does have d_namlen and d_reclen though. Keep in mind though that macOS also has d_seekoff which is almost always zero though.

So please, don't blindly define something, test it! On doubt I stick with Vitaly's solution, because it just works^TM.

On the long term we can still adjust this to make all people happy, but this is about fixing a crash, so I am fine with what Greg called "band-aid".

Best regards,
Christian Schoenebeck

#13 lore raw
In reply to Philippe Mathieu-Daudé via
Quoted 28 lines · Philippe Mathieu-Daudé
Quoted 26 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

  #0  0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 + 0x497eed)
  #1  0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9)
  #2  0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983)
  #3  0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)
Quoted 11 lines

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

If it was so easy to be misled this way,
I'd suggest adding an explicit comment, e.g.

/*
* Test sz for non-zero even if d_reclen is available
* because some drivers may set d_reclen to zero. */

Signature · 1 line
ldv
#12 lore raw
In reply to Christian Schoenebeck
Quoted 29 lines · Christian Schoenebeck
Quoted 19 lines · On Freitag, 4. Februar 2022 16:54:12 CET Philippe Mathieu-Daudé wrote:
Quoted 18 lines · Dmitry V. Levin

On Fri, Feb 04, 2022 at 06:32:07PM +0300, Vitaly Chikunov wrote:
[...]

Quoted 15 lines
Quoted 6 lines

struct dirent *
qemu_dirent_dup(struct dirent *dent)
{

size_t sz = offsetof(struct dirent, d_name) + _D_EXACT_NAMLEN(dent)
+ 1;

But d_namlen is not populated by synth_direntry, so this will lead to a bug too. Idea is that qemu_dirent_dup handles real dirents and simulated (underpopulated) dirents.

Also Linux does not have d_namlen AFAIK, thus this code will not provide any speed up in most cases (and always fallback to strlen), unlike if we use d_reclen.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Also, I m not sure if _D_EXACT_NAMLEN is defined on all systems, so this needs ifdefs too.

Yes, _D_EXACT_NAMLEN() is a GNU extension, it was introduced in glibc back in 1996 but some popular libcs available for Linux do not provide this macro.

Can't we define _D_EXACT_NAMLEN() if not available?

Quoted 8 lines

It is not that trivial.

With recent macOS patch set in mind: macOS does not have any of these macros either. It does have d_namlen and d_reclen though. Keep in mind though that macOS also has d_seekoff which is almost always zero though.

So please, don't blindly define something, test it! On doubt I stick with Vitaly's solution, because it just works^TM.

Note I haven't NAck'ed this approach, I am simply looking at a better alternative if possible.

#14 lore raw
In reply to Dmitry V. Levin
Quoted 41 lines · Dmitry V. Levin
Quoted 28 lines · Philippe Mathieu-Daudé
Quoted 26 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

  #0  0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 + 0x497eed)
  #1  0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9)
  #2  0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983)
  #3  0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)
Quoted 11 lines

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

Quoted 7 lines

If it was so easy to be misled this way,
I'd suggest adding an explicit comment, e.g.

/*
* Test sz for non-zero even if d_reclen is available
* because some drivers may set d_reclen to zero. */

Sounds good, to avoid misunderstanding. I can send v4, with g_memdup (like Greg suggested).

Thanks,

Quoted 4 lines
Signature · 2 lines
-- 
ldv
#15 lore raw
In reply to Dmitry V. Levin
Quoted 41 lines · On Fri, 4 Feb 2022 19:19:39 +0300 "Dmitry V. Levin" <[email protected]> wrote:
Quoted 28 lines · Philippe Mathieu-Daudé
Quoted 26 lines · Vitaly Chikunov

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

  #0  0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 + 0x497eed)
  #1  0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64 + 0x4982e9)
  #2  0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983)
  #3  0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)
Quoted 11 lines

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 9 lines
util/osdep.c +9 -0
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

If _DIRENT_HAVE_D_RECLEN is defined, this case is unlikely...

Quoted 8 lines

If it was so easy to be misled this way,
I'd suggest adding an explicit comment, e.g.

/*
* Test sz for non-zero even if d_reclen is available
* because some drivers may set d_reclen to zero. */

+1

Quoted 1 line
#16 lore raw
In reply to Vitaly Chikunov
Quoted 82 lines · On Freitag, 4. Februar 2022 06:06:09 CET Vitaly Chikunov wrote:

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

#0 0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 +
0x497eed) #1 0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64
+ 0x4982e9) #2 0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983) #3 0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

 hw/9pfs/codir.c      |  3 +--
 include/qemu/osdep.h | 13 +++++++++++++
 util/osdep.c         | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
hw/9pfs/codir.c +1 -2
index 032cce04c4..c0873bde16 100644
@@ -143,8 +143,7 @@ static int do_readdir_many(V9fsPDU *pdu, V9fsFidState
 *fidp, } else {
             e = e->next = g_malloc0(sizeof(V9fsDirEnt));
         }
-        e->dent = g_malloc0(sizeof(struct dirent));
-        memcpy(e->dent, dent, sizeof(struct dirent));
+        e->dent = qemu_dirent_dup(dent);
 
         /* perform a full stat() for directory entry if requested by caller
 */ if (dostat) {
include/qemu/osdep.h +13 -0
index d1660d67fa..ce12f64853 100644
@@ -805,6 +805,19 @@ static inline int
 platform_does_not_support_system(const char *command) }
 #endif /* !HAVE_SYSTEM_FUNCTION */
 
+/**
+ * Duplicate directory entry @dent.
+ *
+ * It is highly recommended to use this function instead of open coding
+ * duplication of @c dirent objects, because the actual @c struct @c dirent
+ * size may be bigger or shorter than @c sizeof(struct dirent) and correct
+ * handling is platform specific (see gitlab issue #841).
+ *
+ * @dent - original directory entry to be duplicated
+ * @returns duplicated directory entry which should be freed with g_free()
+ */
+struct dirent *qemu_dirent_dup(struct dirent *dent);
+
 #ifdef __cplusplus
 }
 #endif
util/osdep.c +1 -0
index 42a0a4986a..2c80528a61 100644
@@ -33,6 +33,7 @@
 extern int madvise(char *, size_t, int);
 #endif
 
+#include <dirent.h>
 #include "qemu-common.h"
 #include "qemu/cutils.h"
 #include "qemu/sockets.h"
@@ -615,3 +616,20 @@ writev(int fd, const struct iovec *iov, int iov_cnt)
     return readv_writev(fd, iov, iov_cnt, true);
 }
 #endif

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 10 lines
util/osdep.c +10 -0
+
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

Philippe, Greg, apart from the additional comment, do you want to see this check to be changed in v4 to this?

	if (unlikely(sz == 0)) {

Best regards,
Christian Schoenebeck

Quoted 7 lines
util/osdep.c +7 -0
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);
+}
#17 lore raw
Quoted 97 lines · Christian Schoenebeck
Quoted 82 lines · On Freitag, 4. Februar 2022 06:06:09 CET Vitaly Chikunov wrote:

struct dirent' returned from readdir(3) could be shorter (or longer) than sizeof(struct dirent)', thus memcpy of sizeof length will overread into unallocated page causing SIGSEGV. Example stack trace:

#0 0x00005555559ebeed v9fs_co_readdir_many (/usr/bin/qemu-system-x86_64 +
0x497eed) #1 0x00005555559ec2e9 v9fs_readdir (/usr/bin/qemu-system-x86_64
+ 0x4982e9) #2 0x0000555555eb7983 coroutine_trampoline (/usr/bin/qemu-system-x86_64 + 0x963983) #3 0x00007ffff73e0be0 n/a (n/a + 0x0)

While fixing, provide a helper for any future `struct dirent' cloning.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/841

---
Tested on x86-64 Linux again.

Changes from v2:
- Make it work with a simulated dirent where d_reclen is 0, which was caused abort in readdir qos-test, by using fallback at runtime.

  hw/9pfs/codir.c      |  3 +--
  include/qemu/osdep.h | 13 +++++++++++++
  util/osdep.c         | 18 ++++++++++++++++++
  3 files changed, 32 insertions(+), 2 deletions(-)
hw/9pfs/codir.c +1 -2
index 032cce04c4..c0873bde16 100644
@@ -143,8 +143,7 @@ static int do_readdir_many(V9fsPDU *pdu, V9fsFidState
 *fidp, } else {
              e = e->next = g_malloc0(sizeof(V9fsDirEnt));
          }
-        e->dent = g_malloc0(sizeof(struct dirent));
-        memcpy(e->dent, dent, sizeof(struct dirent));
+        e->dent = qemu_dirent_dup(dent);
 
          /* perform a full stat() for directory entry if requested by caller
 */ if (dostat) {
include/qemu/osdep.h +13 -0
index d1660d67fa..ce12f64853 100644
@@ -805,6 +805,19 @@ static inline int
 platform_does_not_support_system(const char *command) }
  #endif /* !HAVE_SYSTEM_FUNCTION */
 
+/**
+ * Duplicate directory entry @dent.
+ *
+ * It is highly recommended to use this function instead of open coding
+ * duplication of @c dirent objects, because the actual @c struct @c dirent
+ * size may be bigger or shorter than @c sizeof(struct dirent) and correct
+ * handling is platform specific (see gitlab issue #841).
+ *
+ * @dent - original directory entry to be duplicated
+ * @returns duplicated directory entry which should be freed with g_free()
+ */
+struct dirent *qemu_dirent_dup(struct dirent *dent);
+
  #ifdef __cplusplus
  }
  #endif
util/osdep.c +1 -0
index 42a0a4986a..2c80528a61 100644
@@ -33,6 +33,7 @@
  extern int madvise(char *, size_t, int);
  #endif
 
+#include <dirent.h>
  #include "qemu-common.h"
  #include "qemu/cutils.h"
  #include "qemu/sockets.h"
@@ -615,3 +616,20 @@ writev(int fd, const struct iovec *iov, int iov_cnt)
      return readv_writev(fd, iov, iov_cnt, true);
  }
  #endif

util/osdep.c writev(int fd, const struct iovec *iov, int iov_cnt) · line 628

Quoted 10 lines
util/osdep.c +10 -0
+
+struct dirent *
+qemu_dirent_dup(struct dirent *dent)
+{
+    size_t sz = 0;
+#if defined _DIRENT_HAVE_D_RECLEN
+    /* Avoid use of strlen() if there's d_reclen. */
+    sz = dent->d_reclen;
+#endif
+    if (sz == 0) {

Philippe, Greg, apart from the additional comment, do you want to see this check to be changed in v4 to this?

Quoted 1 line
	if (unlikely(sz == 0)) {

This is not an hot path so probably not very useful.

Quoted 10 lines

Best regards,
Christian Schoenebeck

Quoted 6 lines
util/osdep.c +6 -0
+        /* Fallback to the most portable way. */
+        sz = offsetof(struct dirent, d_name) +
+                      strlen(dent->d_name) + 1;
+    }
+    struct dirent *dst = g_malloc(sz);
+    return memcpy(dst, dent, sz);

However 'return g_memdup(dent, sz)' is simpler to review.

Quoted 4 lines
Quoted 1 line
util/osdep.c +1 -0
+}