From patchwork Wed Sep 23 12:43:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuriy Kolerov X-Patchwork-Id: 521684 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by ozlabs.org (Postfix) with ESMTP id E060914018C for ; Wed, 23 Sep 2015 22:43:52 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id EEC6E335F8; Wed, 23 Sep 2015 12:43:51 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id BOsg0Zd0KoTL; Wed, 23 Sep 2015 12:43:49 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by silver.osuosl.org (Postfix) with ESMTP id 6B6E8335ED; Wed, 23 Sep 2015 12:43:48 +0000 (UTC) X-Original-To: uclibc@lists.busybox.net Delivered-To: uclibc@osuosl.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by ash.osuosl.org (Postfix) with ESMTP id 306101CE80D for ; Wed, 23 Sep 2015 12:43:47 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 2A4D991A2D for ; Wed, 23 Sep 2015 12:43:47 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CPl7HPP-sITi for ; Wed, 23 Sep 2015 12:43:46 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from smtprelay.synopsys.com (us01smtprelay-2.synopsys.com [198.182.47.9]) by whitealder.osuosl.org (Postfix) with ESMTPS id B0166912B7 for ; Wed, 23 Sep 2015 12:43:46 +0000 (UTC) Received: from us02secmta1.synopsys.com (us02secmta1.synopsys.com [10.12.235.96]) by smtprelay.synopsys.com (Postfix) with ESMTP id 7680324E140A for ; Wed, 23 Sep 2015 05:43:46 -0700 (PDT) Received: from us02secmta1.internal.synopsys.com (us02secmta1.internal.synopsys.com [127.0.0.1]) by us02secmta1.internal.synopsys.com (Service) with ESMTP id 6AF5E4E217 for ; Wed, 23 Sep 2015 05:43:46 -0700 (PDT) Received: from mailhost.synopsys.com (unknown [10.13.184.66]) by us02secmta1.internal.synopsys.com (Service) with ESMTP id 37D024E214 for ; Wed, 23 Sep 2015 05:43:46 -0700 (PDT) Received: from mailhost.synopsys.com (localhost [127.0.0.1]) by mailhost.synopsys.com (Postfix) with ESMTP id 27B12E60; Wed, 23 Sep 2015 05:43:46 -0700 (PDT) Received: from ykolerov-lab.internal.synopsys.com (ykolerov-lab.internal.synopsys.com [10.121.8.82]) by mailhost.synopsys.com (Postfix) with ESMTP id 76034E5D; Wed, 23 Sep 2015 05:43:44 -0700 (PDT) From: Yuriy Kolerov To: uclibc@uclibc.org, Vineet.Gupta1@synopsys.com, Alexey.Brodkin@synopsys.com, Anton.Kolesov@synopsys.com Subject: [PATCH v3 2/3] libc: fix sign extension in fallocate() Date: Wed, 23 Sep 2015 15:43:38 +0300 Message-Id: <1443012219-10992-2-git-send-email-yuriy.kolerov@synopsys.com> X-Mailer: git-send-email 2.2.0 In-Reply-To: <1443012219-10992-1-git-send-email-yuriy.kolerov@synopsys.com> References: <1443012219-10992-1-git-send-email-yuriy.kolerov@synopsys.com> Cc: Francois.Bedard@synopsys.com X-BeenThere: uclibc@uclibc.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "Discussion and development of uClibc \(the embedded C library\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: uclibc-bounces@uclibc.org Sender: "uClibc" 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(-) diff --git a/libc/sysdeps/linux/common/fallocate.c b/libc/sysdeps/linux/common/fallocate.c index b2309e9..0f80fb4 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