From patchwork Fri Aug 1 18:18:47 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Kolesov X-Patchwork-Id: 375846 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from silver.osuosl.org (silver.osuosl.org [140.211.166.136]) by ozlabs.org (Postfix) with ESMTP id 30B2E140107 for ; Sat, 2 Aug 2014 04:19:00 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 6791E20C2B; Fri, 1 Aug 2014 18:18:58 +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 Xc34N7GxzHL5; Fri, 1 Aug 2014 18:18:56 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by silver.osuosl.org (Postfix) with ESMTP id A066630342; Fri, 1 Aug 2014 18:18:56 +0000 (UTC) X-Original-To: uclibc@lists.busybox.net Delivered-To: uclibc@osuosl.org Received: from silver.osuosl.org (silver.osuosl.org [140.211.166.136]) by ash.osuosl.org (Postfix) with ESMTP id C84E01BFA2E for ; Fri, 1 Aug 2014 18:18:55 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id C3DDB20C2B for ; Fri, 1 Aug 2014 18:18:55 +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 Tana7KmjbhLM for ; Fri, 1 Aug 2014 18:18:54 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from smtprelay.synopsys.com (smtprelay.synopsys.com [198.182.44.111]) by silver.osuosl.org (Postfix) with ESMTPS id D7EAE26AC6 for ; Fri, 1 Aug 2014 18:18:54 +0000 (UTC) Received: from us02secmta2.synopsys.com (us02secmta2.synopsys.com [10.12.235.98]) by smtprelay.synopsys.com (Postfix) with ESMTP id 44B3C24E08EC for ; Fri, 1 Aug 2014 11:18:54 -0700 (PDT) Received: from us02secmta2.internal.synopsys.com (us02secmta2.internal.synopsys.com [127.0.0.1]) by us02secmta2.internal.synopsys.com (Service) with ESMTP id 38D1555F13 for ; Fri, 1 Aug 2014 11:18:54 -0700 (PDT) Received: from mailhost.synopsys.com (mailhost2.synopsys.com [10.9.202.240]) by us02secmta2.internal.synopsys.com (Service) with ESMTP id 1C0D455F02 for ; Fri, 1 Aug 2014 11:18:54 -0700 (PDT) Received: from mailhost.synopsys.com (localhost [127.0.0.1]) by mailhost.synopsys.com (Postfix) with ESMTP id 0EC97E80; Fri, 1 Aug 2014 11:18:54 -0700 (PDT) Received: from akolesov-lab.internal.synopsys.com (akolesov-lab.internal.synopsys.com [10.121.8.134]) by mailhost.synopsys.com (Postfix) with ESMTP id 3D509E7C; Fri, 1 Aug 2014 11:18:53 -0700 (PDT) From: Anton Kolesov To: uclibc@uclibc.org Subject: [PATCH v2] lseek: Correct order of offset arguments Date: Fri, 1 Aug 2014 22:18:47 +0400 Message-Id: <1406917127-8088-1-git-send-email-Anton.Kolesov@synopsys.com> X-Mailer: git-send-email 1.8.4.1 In-Reply-To: <1471f205b40.2763.0f39ed3bcad52ef2c88c90062b7714dc@gmail.com> References: <1471f205b40.2763.0f39ed3bcad52ef2c88c90062b7714dc@gmail.com> Cc: arc-linux-dev@synopsys.com X-BeenThere: uclibc@uclibc.org X-Mailman-Version: 2.1.14 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-bounces@uclibc.org There was a runtime error in systems without large file support. Call fseek(fd, 4096, SEEK_SET) has been failing with EINVAL, though it was succeeding for offset = 4092. This has been happening because llseek system call accepts 64-bit value as an offset argument and lseek function has been ordering 32-bits words that form this offset value, according to the endianness. However this ordering to match endianness is not required, because llseek doesn't accept one 64-bit offset argument, it accepts two 32-bit offset argument, then stitches them into one following its endianness. As a result on little endian system, order of words has been swapped two time: in libc and in kernel. Thus call to fseek with offset 4096 (0x1000) was doing a system call to llseek with offset 0x1000_0000_0000. I'm not entirely sure why then offset = 4092 hasn't been failing then. This patch removes malicious swap of words when calling llseek. Signed-off-by: Anton Kolesov --- libc/sysdeps/linux/common/lseek.c | 4 +--- test/stdio/lseek_no_lfs.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test/stdio/lseek_no_lfs.c diff --git a/libc/sysdeps/linux/common/lseek.c b/libc/sysdeps/linux/common/lseek.c index 500c6bf..11a1fbb 100644 --- a/libc/sysdeps/linux/common/lseek.c +++ b/libc/sysdeps/linux/common/lseek.c @@ -24,9 +24,7 @@ off_t __NC(lseek)(int fd, off_t offset, int whence) #elif __WORDSIZE == 32 __off64_t result; __off_t high = 0; - return INLINE_SYSCALL(llseek, 5, fd, - __LONG_LONG_PAIR(high, offset), - &result, whence) ?: result; + return INLINE_SYSCALL(llseek, 5, fd, high, offset, &result, whence) ?: result; #endif /* No need to handle __WORDSIZE == 64 as such a kernel won't define __NR_llseek */ } diff --git a/test/stdio/lseek_no_lfs.c b/test/stdio/lseek_no_lfs.c new file mode 100644 index 0000000..54daf6b --- /dev/null +++ b/test/stdio/lseek_no_lfs.c @@ -0,0 +1,22 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + FILE * f = fopen(argv[0], "rb"); + if (!f) + { + printf("Error: Can't open %s, reason: %s\n", argv[0], strerror(errno)); + return 1; + } + + if (fseek(f, (unsigned)4096, (int)SEEK_SET) == -1) + { + printf("Test failed, fseek return fail code. errno=%u (%s)\n", errno, strerror(errno)); + return 1; + } + + fclose(f); + return 0; +}