From patchwork Fri Jul 1 14:23:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12903401 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D585FCCA479 for ; Fri, 1 Jul 2022 14:25:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7803E6B007D; Fri, 1 Jul 2022 10:25:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 730886B007E; Fri, 1 Jul 2022 10:25:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5D1546B0081; Fri, 1 Jul 2022 10:25:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4C3306B007E for ; Fri, 1 Jul 2022 10:25:20 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2CB5420592 for ; Fri, 1 Jul 2022 14:25:20 +0000 (UTC) X-FDA: 79638753600.12.86E98B9 Received: from mail-lf1-f74.google.com (mail-lf1-f74.google.com [209.85.167.74]) by imf17.hostedemail.com (Postfix) with ESMTP id C39564003F for ; Fri, 1 Jul 2022 14:25:19 +0000 (UTC) Received: by mail-lf1-f74.google.com with SMTP id p36-20020a05651213a400b004779d806c13so1178712lfa.10 for ; Fri, 01 Jul 2022 07:25:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=iGvD4mfjvjSWX+nv6535woTSPAyHTTcU/sEMimyHpxI=; b=P7QKDp3hV6WoKSuU7RWtHYNnUzKsYPMuoVFYGC5CAEPmJ0nl1tQiRY+9OYfXLY42wB nFSOJB8BBflVDmdDuWSkOCm55g6bwX9dQkpXj4xCR6s6JCEls/jHSMNPkv+lVDZh5nKs hUARMca5KeTe+Ir0fpc6ZOi4Uq5KQZl/WLZTZOatCEOeYcao1NRgQlKKrm6iBVPu3X1Z JYhJ2HBgIVmRPYmPgcVuYdtvK/4rJXCJLuWe7FZ63qgHZC3DxwnGi+wOpBYOkbuzJh/f yrpShYElychLxpVWGpPbFszkIP0ovqs29G1ENpHftNUWpa2XkYBhGLzu4Zi+nYeVFrno cQCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=iGvD4mfjvjSWX+nv6535woTSPAyHTTcU/sEMimyHpxI=; b=S7K4L3i+42iqfBjfM2mOmhH2SB3hDlwBwhODcvikjfRD3xGQSCIkbuIRIi/onPyBy4 NrUUWx+InqAzbTLz7QGSDNVteuEK0CVYzkrjvEe0+NrOWHmGOwQ45rbgTT+JlS+jWx8b 31QPCLTVc2ruAGCTmgjkHGC+PD/QYGblKQU50qfnpkJXZYm6D8hJzk5S8HVbverTZ+OY w5ZFDYpI6oj6QJooGZGW7AdrYBfJAWRW75DGmnbefpaApdkr6gG7e3HxlQs9nCnKDE06 UPPr7zD5830bsR7up/PtfIRPiYxYgrf+26utGqKkijvJJsDO3oUrZ9sBcnh3i5fIeoH5 lnpw== X-Gm-Message-State: AJIora+lUe6vg6pVjR8s1RzgT7AQVgbtdz/tuq92xHuA9r064nK76xdR Qq/I4ro4IYCwikSdP1Wyx7lrxuKSXJI= X-Google-Smtp-Source: AGRyM1s7QtTcOviesKYVgedtf1csh4E3jTJYd2DFNUsU1Lb1euL9e2M1gyfvpy9SPS5aVSID3ksrqFaDH6I= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:a6f5:f713:759c:abb6]) (user=glider job=sendgmr) by 2002:a05:6512:4c3:b0:47f:6f6e:a7e7 with SMTP id w3-20020a05651204c300b0047f6f6ea7e7mr9859006lfq.674.1656685518270; Fri, 01 Jul 2022 07:25:18 -0700 (PDT) Date: Fri, 1 Jul 2022 16:23:09 +0200 In-Reply-To: <20220701142310.2188015-1-glider@google.com> Message-Id: <20220701142310.2188015-45-glider@google.com> Mime-Version: 1.0 References: <20220701142310.2188015-1-glider@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 44/45] mm: fs: initialize fsdata passed to write_begin/write_end interface From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656685519; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iGvD4mfjvjSWX+nv6535woTSPAyHTTcU/sEMimyHpxI=; b=GWh18qXUx1JMLNE8ZakDimyCJQ0ZuK3uJpR+oo2b4jdCZcjgS5zKt9FWetsGyeuWmZRIGX FY0jRjtOK/m1STYhoJo8yiJS1me4CFLEN2DqRVjfMmJjVLvNhYpQhUZQUOl22mhQ5MsDHX ejKcU+ZwD/qw7y7kyxFHXoitwtgk+Y0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656685519; a=rsa-sha256; cv=none; b=R8ILuMi57Zv3OlnK0muj3E+hBS3s865vDNXeWmBiWyM0Ww2SBMA8UeNAtDHrlORaJoGgTL qLwuVBr32anyH0iYIQbpv4gFpPXdrieaYnXjLNqXu6hkDV1tl8cZw1QRn3gOT2Wr0WZkxX y+xmlo8WzREB7wyNKKx7HKbbggofx14= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=P7QKDp3h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3zgO_YgYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.167.74 as permitted sender) smtp.mailfrom=3zgO_YgYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com X-Stat-Signature: fb68xdp74cdqes7kxpgj59jwysnoxg7e X-Rspam-User: Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=P7QKDp3h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3zgO_YgYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.167.74 as permitted sender) smtp.mailfrom=3zgO_YgYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C39564003F X-HE-Tag: 1656685519-964464 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: Functions implementing the a_ops->write_end() interface accept the `void *fsdata` parameter that is supposed to be initialized by the corresponding a_ops->write_begin() (which accepts `void **fsdata`). However not all a_ops->write_begin() implementations initialize `fsdata` unconditionally, so it may get passed uninitialized to a_ops->write_end(), resulting in undefined behavior. Fix this by initializing fsdata with NULL before the call to write_begin(), rather than doing so in all possible a_ops implementations. This patch covers only the following cases found by running x86 KMSAN under syzkaller: - generic_perform_write() - cont_expand_zero() and generic_cont_expand_simple() - page_symlink() Other cases of passing uninitialized fsdata may persist in the codebase. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I414f0ee3a164c9c335d91d82ce4558f6f2841471 --- fs/buffer.c | 4 ++-- fs/namei.c | 2 +- mm/filemap.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index 898c7f301b1b9..d014009cff941 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -2349,7 +2349,7 @@ int generic_cont_expand_simple(struct inode *inode, loff_t size) struct address_space *mapping = inode->i_mapping; const struct address_space_operations *aops = mapping->a_ops; struct page *page; - void *fsdata; + void *fsdata = NULL; int err; err = inode_newsize_ok(inode, size); @@ -2375,7 +2375,7 @@ static int cont_expand_zero(struct file *file, struct address_space *mapping, const struct address_space_operations *aops = mapping->a_ops; unsigned int blocksize = i_blocksize(inode); struct page *page; - void *fsdata; + void *fsdata = NULL; pgoff_t index, curidx; loff_t curpos; unsigned zerofrom, offset, len; diff --git a/fs/namei.c b/fs/namei.c index 6b39dfd3b41bc..5e3ff9d65f502 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -5051,7 +5051,7 @@ int page_symlink(struct inode *inode, const char *symname, int len) const struct address_space_operations *aops = mapping->a_ops; bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS); struct page *page; - void *fsdata; + void *fsdata = NULL; int err; unsigned int flags; diff --git a/mm/filemap.c b/mm/filemap.c index ffdfbc8b0e3ca..72467f00f1916 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3753,7 +3753,7 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i) unsigned long offset; /* Offset into pagecache page */ unsigned long bytes; /* Bytes to write to page */ size_t copied; /* Bytes copied from user */ - void *fsdata; + void *fsdata = NULL; offset = (pos & (PAGE_SIZE - 1)); bytes = min_t(unsigned long, PAGE_SIZE - offset,