From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FF48C388F7 for ; Thu, 22 Oct 2020 20:59:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CB16A20BED for ; Thu, 22 Oct 2020 20:59:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="zdUyA37A" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CB16A20BED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DE7D46B005D; Thu, 22 Oct 2020 16:59:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D71086B0062; Thu, 22 Oct 2020 16:59:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BEB3F6B0068; Thu, 22 Oct 2020 16:59:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id 8FEBC6B005D for ; Thu, 22 Oct 2020 16:59:38 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 1FA6D181AEF07 for ; Thu, 22 Oct 2020 20:59:38 +0000 (UTC) X-FDA: 77400777636.01.lift18_1d17ef527254 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id E332B100473CF; Thu, 22 Oct 2020 20:59:37 +0000 (UTC) X-HE-Tag: lift18_1d17ef527254 X-Filterd-Recvd-Size: 6179 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf22.hostedemail.com (Postfix) with ESMTP; Thu, 22 Oct 2020 20:59:36 +0000 (UTC) Received: from gmail.com (unknown [104.132.1.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8D47120874; Thu, 22 Oct 2020 20:59:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603400375; bh=Xexx6LzGU5CEQ5pUb/Rnl3sbc1IKJSCv776XJ3IiUZY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=zdUyA37A1L47KHgoyE+FT2EuXbQ76eLgYxDhD0pRsQ/vZP9VSxvex63p/S7m2wh2S LPo1qkQ3neXSHmpGEcAArmow8IRVH9ABGEBSoWdWkIGldhGddb2MyLlCZgza1+BxJr XSVfJ1lZvRBFbHtiVn91WdB+7mhw78zhWJSY/86E= Date: Thu, 22 Oct 2020 13:59:32 -0700 From: Eric Biggers To: Nick Desaulniers Cc: Matthew Wilcox , David Laight , Christoph Hellwig , David Hildenbrand , Greg KH , Al Viro , "kernel-team@android.com" , Andrew Morton , Jens Axboe , Arnd Bergmann , David Howells , "linux-arm-kernel@lists.infradead.org" , "linux-kernel@vger.kernel.org" , "linux-mips@vger.kernel.org" , "linux-parisc@vger.kernel.org" , "linuxppc-dev@lists.ozlabs.org" , "linux-s390@vger.kernel.org" , "sparclinux@vger.kernel.org" , "linux-block@vger.kernel.org" , "linux-scsi@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "linux-aio@kvack.org" , "io-uring@vger.kernel.org" , "linux-arch@vger.kernel.org" , "linux-mm@kvack.org" , "netdev@vger.kernel.org" , "keyrings@vger.kernel.org" , "linux-security-module@vger.kernel.org" Subject: Re: Buggy commit tracked to: "Re: [PATCH 2/9] iov_iter: move rw_copy_check_uvector() into lib/iov_iter.c" Message-ID: <20201022205932.GB3613750@gmail.com> References: <20201022090155.GA1483166@kroah.com> <5fd6003b-55a6-2c3c-9a28-8fd3a575ca78@redhat.com> <20201022132342.GB8781@lst.de> <8f1fff0c358b4b669d51cc80098dbba1@AcuMS.aculab.com> <20201022164040.GV20115@casper.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Oct 22, 2020 at 10:00:44AM -0700, Nick Desaulniers wrote: > On Thu, Oct 22, 2020 at 9:40 AM Matthew Wilcox wrote: > > > > On Thu, Oct 22, 2020 at 04:35:17PM +0000, David Laight wrote: > > > Wait... > > > readv(2) defines: > > > ssize_t readv(int fd, const struct iovec *iov, int iovcnt); > > > > It doesn't really matter what the manpage says. What does the AOSP > > libc header say? > > Same: https://android.googlesource.com/platform/bionic/+/refs/heads/master/libc/include/sys/uio.h#38 > > Theoretically someone could bypass libc to make a system call, right? > > > > > > But the syscall is defined as: > > > > > > SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, > > > unsigned long, vlen) > > > { > > > return do_readv(fd, vec, vlen, 0); > > > } > > > FWIW, glibc makes the readv() syscall assuming that fd and vlen are 'int' as well. So this problem isn't specific to Android's libc. >From objdump -d /lib/x86_64-linux-gnu/libc.so.6: 00000000000f4db0 : f4db0: 64 8b 04 25 18 00 00 mov %fs:0x18,%eax f4db7: 00 f4db8: 85 c0 test %eax,%eax f4dba: 75 14 jne f4dd0 f4dbc: b8 13 00 00 00 mov $0x13,%eax f4dc1: 0f 05 syscall ... There's some code for pthread cancellation, but no zeroing of the upper half of the fd and vlen arguments, which are in %edi and %edx respectively. But the glibc function prototype uses 'int' for them, not 'unsigned long' 'ssize_t readv(int fd, const struct iovec *iov, int iovcnt);'. So the high halves of the fd and iovcnt registers can contain garbage. Or at least that's what gcc (9.3.0) and clang (9.0.1) assume; they both compile the following void g(unsigned int x); void f(unsigned long x) { g(x); } into f() making a tail call to g(), without zeroing the top half of %rdi. Also note the following program succeeds on Linux 5.9 on x86_64. On kernels that have this bug, it should fail. (I couldn't get it to actually fail, so it must depend on the compiler and/or the kernel config...) #include #include #include #include #include int main() { int fd = open("/dev/zero", O_RDONLY); char buf[1000]; struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) }; long ret; ret = syscall(__NR_readv, fd, &iov, 0x100000001); if (ret < 0) perror("readv failed"); else printf("read %ld bytes\n", ret); } I think the right fix is to change the readv() (and writev(), etc.) syscalls to take 'unsigned int' rather than 'unsigned long', as that is what the users are assuming... - Eric