OSDN Git Service

libc: fix sign extension in fallocate()
authorYuriy Kolerov <yuriy.kolerov@synopsys.com>
Wed, 23 Sep 2015 12:43:38 +0000 (15:43 +0300)
committerWaldemar Brodkorb <wbx@openadk.org>
Fri, 9 Oct 2015 03:27:25 +0000 (05:27 +0200)
commitc8ebcd79a70e6b526a3d593a22e486ff9eb5b9ef
tree4ad23c43e5708df7eb1eba16aa4a169455d08b8e
parent0034d10e136348316d1887a48eb642401e8a26c4
libc: fix sign extension in fallocate()

For common generic syscall ABI fallocate syscall handler in kernel
expects a 64-bit signed arguments for offset and len. However uClibc
has 2 wrappers for this syscall: fallocate and fallocate64.

On 32-bit machines fallocate (not fallocate64) expects 32-bit values of
offset and len. Thus in this case uClibc's fallocate must pass to the
syscall those values with sign extension. High word of 64-bit value must
be 0 or 0xFFFFFFFF depending on sign of the original 32-bit value (offset
or len). It is how sign extansion works - all high bits of the negative
value must be 1.

So on 32-bit machines uClibc's fallocate does sign extension incorrectly
when 32-bit values are passed (offset or len). It just fills the second
word of 64-bit value by zeros. E.g. fallocate works incorrectly when offset
or length is negative value - in this case kernel thinks that positive
values are passed.

Solution is to call fallocate64 from fallocate and pass 32-bit values of
offset and len to fallocate64. off_t type is automatically converted to
off64_t with an appropriate sign extension. Then fallocate64 invokes
kernel's system call properly.

This error is detected in LTP's test kernel/syscalls/fallocate02:

    ----------->8----------
    fallocate(..., 1, -1024, 1024) failed, expected errno:22: TEST_ERRNO=0
    fallocate(..., 1, 1024, -1024) failed, expected errno:22: TEST_ERRNO=0
    fallocate(..., 1, 12288, -1024) failed, expected errno:22: TEST_ERRNO=0
    fallocate(..., 1, -24576, 1024) failed, expected errno:22: TEST_ERRNO=0
    ----------->8----------

fallocate does not emit an error because negative values are passed to the
kernel without sign extension and kernel thinks that it got valid positive
values.

Signed-off-by: Yuriy Kolerov <yuriy.kolerov@synopsys.com>
libc/sysdeps/linux/common/fallocate.c