From 4480f9b5558906fce2c35f1819d4e1fe5922a9fa Mon Sep 17 00:00:00 2001 From: Yuriy Kolerov Date: Wed, 23 Sep 2015 15:43:38 +0300 Subject: 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 --- libc/sysdeps/linux/common/fallocate.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'libc/sysdeps/linux/common') diff --git a/libc/sysdeps/linux/common/fallocate.c b/libc/sysdeps/linux/common/fallocate.c index b2309e978..0f80fb4c2 100644 --- a/libc/sysdeps/linux/common/fallocate.c +++ b/libc/sysdeps/linux/common/fallocate.c @@ -18,28 +18,20 @@ extern __typeof(fallocate) __libc_fallocate attribute_hidden; int attribute_hidden __libc_fallocate(int fd, int mode, __off_t offset, __off_t len) { - int ret; - # if __WORDSIZE == 32 - uint32_t off_low = offset; - uint32_t len_low = len; - /* may assert that these >>31 are 0 */ - uint32_t zero = 0; - INTERNAL_SYSCALL_DECL(err); - ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, mode, - __LONG_LONG_PAIR (zero, off_low), - __LONG_LONG_PAIR (zero, len_low))); + return fallocate64(fd, mode, offset, len); # elif __WORDSIZE == 64 + int ret; INTERNAL_SYSCALL_DECL(err); ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, mode, offset, len)); -# else -# error your machine is neither 32 bit or 64 bit ... it must be magical -# endif if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err))) { __set_errno(INTERNAL_SYSCALL_ERRNO (ret, err)); ret = -1; } return ret; +# else +# error your machine is neither 32 bit or 64 bit ... it must be magical +# endif } # if defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU -- cgit v1.2.3