linux-user: fix readlink handling with magic exe symlink
commitf17f4989fa193fa8279474c5462289a3cfe69aea
authorMike Frysinger <vapier@chromium.org>
Fri, 8 Aug 2014 00:40:25 +0000 (8 09:40 +0900)
committerRiku Voipio <riku.voipio@linaro.org>
Fri, 22 Aug 2014 12:06:33 +0000 (22 15:06 +0300)
tree70bf13634f0b10c3a3c2d9ed85fc3861aede7ac1
parentc065976f2bca9b87bc699c5fdeb4d3ff1299b8c4
linux-user: fix readlink handling with magic exe symlink

The current code always returns the length of the path when it should
be returning the number of bytes it wrote to the output string.

Further, readlink is not supposed to append a NUL byte, but the current
snprintf logic will always do just that.

Even further, if you pass in a length of 0, you're suppoesd to get back
an error (EINVAL), but the current logic just returns 0.

Further still, if there was an error reading the symlink, we should not
go ahead and try to read the target buffer as it is garbage.

Simple test for the first two issues:
$ cat test.c
int main() {
    char buf[50];
    size_t len;
    for (len = 0; len < 10; ++len) {
        memset(buf, '!', sizeof(buf));
        ssize_t ret = readlink("/proc/self/exe", buf, len);
        buf[20] = '\0';
        printf("readlink(/proc/self/exe, {%s}, %zu) = %zi\n", buf, len, ret);
    }
    return 0;
}

Now compare the output of the native:
$ gcc test.c -o /tmp/x
$ /tmp/x
$ strace /tmp/x

With what qemu does:
$ armv7a-cros-linux-gnueabi-gcc test.c -o /tmp/x -static
$ qemu-arm /tmp/x
$ qemu-arm -strace /tmp/x

Signed-off-by: Mike Frysinger <vapier@chromium.org>
Signed-off-by: Riku Voipio <riku.voipio@linaro.org>
linux-user/syscall.c