Patchwork [v3] linux-user: fix emulation of getdents

login
register
mail settings
Submitter Dmitry V. Levin
Date Aug. 20, 2012, 9:36 p.m.
Message ID <20120820213617.GA22459@altlinux.org>
Download mbox | patch
Permalink /patch/178931/
State New
Headers show

Comments

Dmitry V. Levin - Aug. 20, 2012, 9:36 p.m.
In case when TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64, the last
byte of the target dirent structure (aka d_type byte) was never copied
from the host dirent structure, thus breaking everything that relies
on valid d_type value, e.g. glob(3).

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
---
 linux-user/syscall.c      | 11 +++++------
 linux-user/syscall_defs.h |  2 +-
 2 files changed, 6 insertions(+), 7 deletions(-)
Peter Maydell - Aug. 20, 2012, 10:05 p.m.
On 20 August 2012 22:36, Dmitry V. Levin <ldv@altlinux.org> wrote:
> In case when TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64, the last
> byte of the target dirent structure (aka d_type byte) was never copied
> from the host dirent structure, thus breaking everything that relies
> on valid d_type value, e.g. glob(3).

Looks pretty good, just one nit...

> diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
> index 2cfda5a..f7271fa 100644
> --- a/linux-user/syscall_defs.h
> +++ b/linux-user/syscall_defs.h
> @@ -258,7 +258,7 @@ struct target_dirent {
>         abi_long        d_ino;
>         abi_long        d_off;
>         unsigned short  d_reclen;
> -       char            d_name[256]; /* We must not include limits.h! */
> +       char            d_name[];
>  };

Can you convert this struct to match coding style while you're touching
it, please? (ie no hardcoded tabs). scripts/checkpatch.pl will let you
know about this kind of thing.

thanks
-- PMM

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 41c869b..8a8aaca 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7025,15 +7025,14 @@  abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 		tde = target_dirp;
                 while (len > 0) {
                     reclen = de->d_reclen;
-		    treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
+                    tnamelen = reclen - offsetof(struct linux_dirent, d_name);
+                    assert(tnamelen >= 0);
+                    treclen = tnamelen + offsetof(struct target_dirent, d_name);
+                    assert(count1 + treclen <= count);
                     tde->d_reclen = tswap16(treclen);
                     tde->d_ino = tswapal(de->d_ino);
                     tde->d_off = tswapal(de->d_off);
-		    tnamelen = treclen - (2 * sizeof(abi_long) + 2);
-		    if (tnamelen > 256)
-                        tnamelen = 256;
-                    /* XXX: may not be correct */
-                    pstrcpy(tde->d_name, tnamelen, de->d_name);
+                    memcpy(tde->d_name, de->d_name, tnamelen);
                     de = (struct linux_dirent *)((char *)de + reclen);
                     len -= reclen;
                     tde = (struct target_dirent *)((char *)tde + treclen);
diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
index 2cfda5a..f7271fa 100644
--- a/linux-user/syscall_defs.h
+++ b/linux-user/syscall_defs.h
@@ -258,7 +258,7 @@  struct target_dirent {
 	abi_long	d_ino;
 	abi_long	d_off;
 	unsigned short	d_reclen;
-	char		d_name[256]; /* We must not include limits.h! */
+	char		d_name[];
 };
 
 struct target_dirent64 {