From patchwork Fri Mar 26 00:02:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Luck X-Patchwork-Id: 12165199 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=-11.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham 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 ACD85C433E1 for ; Fri, 26 Mar 2021 00:02:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3E5D561A0A for ; Fri, 26 Mar 2021 00:02:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3E5D561A0A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 41D7D6B0078; Thu, 25 Mar 2021 20:02:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F6686B0074; Thu, 25 Mar 2021 20:02:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 221056B007B; Thu, 25 Mar 2021 20:02:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0126.hostedemail.com [216.40.44.126]) by kanga.kvack.org (Postfix) with ESMTP id 0187C6B0074 for ; Thu, 25 Mar 2021 20:02:50 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id C0F656129 for ; Fri, 26 Mar 2021 00:02:50 +0000 (UTC) X-FDA: 77960074500.17.4410628 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf15.hostedemail.com (Postfix) with ESMTP id 2CC2BA000251 for ; Fri, 26 Mar 2021 00:02:47 +0000 (UTC) IronPort-SDR: krEatQuSqb6So0GktLBnlokCdXKP8aK7PPvE4mia7EXxOl4QYpNcgi4AaC8fUUzieGF8hro+Lb BwFzD+ZbCRQA== X-IronPort-AV: E=McAfee;i="6000,8403,9934"; a="276171512" X-IronPort-AV: E=Sophos;i="5.81,278,1610438400"; d="scan'208";a="276171512" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2021 17:02:44 -0700 IronPort-SDR: wN2owymOr8LuNbROqRY5grqOI0GqflWynmkq92DqloOFlO9DkdfPS4k+ss2TfBJymhpfMtG3Gn hBOppYjcZfwg== X-IronPort-AV: E=Sophos;i="5.81,278,1610438400"; d="scan'208";a="416265839" Received: from agluck-desk2.sc.intel.com ([10.3.52.146]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2021 17:02:43 -0700 From: Tony Luck To: Borislav Petkov Cc: Tony Luck , x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andy Lutomirski , Aili Yao , =?utf-8?b?SE9SSUdVQ0hJIE5BT1lBKCDloIDlj6PjgIDnm7TkuZ8p?= Subject: [PATCH 2/4] mce/iter: Check for copyin failure & return error up stack Date: Thu, 25 Mar 2021 17:02:33 -0700 Message-Id: <20210326000235.370514-3-tony.luck@intel.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210326000235.370514-1-tony.luck@intel.com> References: <20210326000235.370514-1-tony.luck@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 2CC2BA000251 X-Stat-Signature: x3oeotttp9m1eufwftdhrfy8xtjqq6wu Received-SPF: none (intel.com>: No applicable sender policy available) receiver=imf15; identity=mailfrom; envelope-from=""; helo=mga05.intel.com; client-ip=192.55.52.43 X-HE-DKIM-Result: none/none X-HE-Tag: 1616716967-443083 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: Check for failure from low level copy from user code. Doing this requires some type changes from the unsigned "size_t" so some signed type (so that "if (xxx < 0)" works!). I picked "loff_t" but there may be some other more appropriate type. Very likely more places need to be changed. These changes based on a machine check copying user data for a write(2) system call to an xfs file system where the stack trace looks like: [ 149.445163] ? copyin+0x2d/0x40 [ 149.448687] ? iov_iter_copy_from_user_atomic+0xd6/0x3a0 [ 149.454625] ? iomap_write_actor+0xc7/0x190 [ 149.459319] ? iomap_apply+0x12a/0x440 [ 149.463508] ? iomap_write_begin+0x530/0x530 [ 149.468276] ? iomap_write_begin+0x530/0x530 [ 149.473041] ? iomap_file_buffered_write+0x62/0x90 [ 149.478390] ? iomap_write_begin+0x530/0x530 [ 149.483157] ? xfs_file_buffered_write+0xe0/0x340 [xfs] [ 149.489393] ? new_sync_write+0x122/0x1b0 [ 149.493879] ? vfs_write+0x20a/0x350 [ 149.497888] ? ksys_write+0x5f/0xe0 [ 149.501784] ? do_syscall_64+0x33/0x40 --- Needs to be bundled with other patches for bisection safety --- fs/iomap/buffered-io.c | 8 +++++++- include/linux/uio.h | 2 +- lib/iov_iter.c | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 414769a6ad11..6cc28e3f32ff 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -757,7 +757,7 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, struct page *page; unsigned long offset; /* Offset into pagecache page */ unsigned long bytes; /* Bytes to write to page */ - size_t copied; /* Bytes copied from user */ + loff_t copied; /* Bytes copied from user */ offset = offset_in_page(pos); bytes = min_t(unsigned long, PAGE_SIZE - offset, @@ -791,6 +791,12 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); + if (copied < 0) { + unlock_page(page); + put_page(page); + return copied; + } + copied = iomap_write_end(inode, pos, bytes, copied, page, iomap, srcmap); diff --git a/include/linux/uio.h b/include/linux/uio.h index 27ff8eb786dc..c2d82e8e309c 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -111,7 +111,7 @@ static inline struct iovec iov_iter_iovec(const struct iov_iter *iter) }; } -size_t iov_iter_copy_from_user_atomic(struct page *page, +loff_t iov_iter_copy_from_user_atomic(struct page *page, struct iov_iter *i, unsigned long offset, size_t bytes); void iov_iter_advance(struct iov_iter *i, size_t bytes); void iov_iter_revert(struct iov_iter *i, size_t bytes); diff --git a/lib/iov_iter.c b/lib/iov_iter.c index f66c62aa7154..57e2f81c51ee 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -16,13 +16,18 @@ #define PIPE_PARANOIA /* for now */ #define iterate_iovec(i, n, __v, __p, skip, STEP) { \ - size_t left; \ + loff_t left; \ size_t wanted = n; \ __p = i->iov; \ __v.iov_len = min(n, __p->iov_len - skip); \ if (likely(__v.iov_len)) { \ __v.iov_base = __p->iov_base + skip; \ left = (STEP); \ + if (left < 0) { \ + wanted = left; \ + n = 0; \ + goto err; \ + } \ __v.iov_len -= left; \ skip += __v.iov_len; \ n -= __v.iov_len; \ @@ -36,10 +41,16 @@ continue; \ __v.iov_base = __p->iov_base; \ left = (STEP); \ + if (left < 0) { \ + wanted = left; \ + n = 0; \ + break; \ + } \ __v.iov_len -= left; \ skip = __v.iov_len; \ n -= __v.iov_len; \ } \ +err: \ n = wanted - n; \ } @@ -975,7 +986,7 @@ size_t iov_iter_zero(size_t bytes, struct iov_iter *i) } EXPORT_SYMBOL(iov_iter_zero); -size_t iov_iter_copy_from_user_atomic(struct page *page, +loff_t iov_iter_copy_from_user_atomic(struct page *page, struct iov_iter *i, unsigned long offset, size_t bytes) { char *kaddr = kmap_atomic(page), *p = kaddr + offset;