Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp56527lfe; Fri, 15 Apr 2022 19:17:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJww1R52Ep+PoHmcXDV1XFULczmCIcN8qRG2KMGg8NCzQ3+anafCgRtyxXq/bFbDZYmIGilZ X-Received: by 2002:a63:3841:0:b0:399:26da:29af with SMTP id h1-20020a633841000000b0039926da29afmr1347592pgn.489.1650075447360; Fri, 15 Apr 2022 19:17:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650075447; cv=none; d=google.com; s=arc-20160816; b=ZbwlQTEXvVpY3NzfpV9PKhn5EiqRK/LRA9DoYybV2zWzUtf622jwGs+sQiE2fQiD8a qxLmfb4Z5bG/nb761MRjkSBjxfyjhKmYoIcs2DYmoWrU3fZklJ4MEh6qqcuHsEgpFl0+ x/E/J+4gwE5eowcYyIk0I2rzWa7V0V+YoEhSU2NJE440RgPIbR7BnR6hMb3X/s9OGAes 9ZmF+V3IJHz3e6q+hN4RQee14XPC8pCgOiv2N25PTSrHiP9jdHzwOGHB2U3u3hgt6x/6 a+m/lf42+ptzetR0ReK3J4szprCfZcMvRE3Zpc1yQ9J3Tubiv8Mkh+0umAFABWqauSFf pD+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=QVBUvWl8kGtXJE+a1r+F3zgr1dPxqwVimN26EQvBjP4=; b=lBplFJscstAGkM6vTshZd0mM9680jf49MnmtxhzSmyu93s8S1veVrAYokdqhmf/o03 gKAroKdwAEmMWZ1aVkgzs36CC+6s9O3r9xb1WWH3kbdtoIVKQIITcJ/lUdt/kNeK/bS9 NZ8Zkx2D3l9iP7DVkx+NL1FdK0gIuS1RZGl8E/YVVNp7qt7BCkaCnnnK5y2gdE5/Bnz4 t0IrywNSXbEoRFXyULnJyt4LXXjWQZgdz78dV3neng9fb4x19WOQabqGRN5raPvHMuNE VEFiED67lwEBos/LJIT1jokTDCsWQFErX/vLauyQAqTYlFSx8ajIQUm65DIdxtwYxNtp 2n4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="s/Em8c2K"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id p22-20020a63f456000000b0039d998598e9si3108428pgk.83.2022.04.15.19.17.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 19:17:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="s/Em8c2K"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 39483E07; Fri, 15 Apr 2022 18:32:12 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343754AbiDNOJP (ORCPT + 99 others); Thu, 14 Apr 2022 10:09:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344069AbiDNNaR (ORCPT ); Thu, 14 Apr 2022 09:30:17 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABB62E87; Thu, 14 Apr 2022 06:27:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 495BD60C14; Thu, 14 Apr 2022 13:27:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DCBDC385A1; Thu, 14 Apr 2022 13:27:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649942858; bh=qUoHhxm8c6MY+CT2TztA03prbPlOAEVB7sisnsz2cM8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s/Em8c2KAyPiFMqyPRbnsSPnbLKdEXNlQgJ8v/FTD5wCIIUaOoJskX6wNnquNpwkW XELM2e3HGv3QQws3NMWP03V0gAMbKoGmmDDFm5OQfpw37zXs+0yUYFH27mAyEPlDJw kVMYqYfAbXMutgMRgSKklf2C8EZlo2C1HIzETqh0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chengsong Ke , Zhihao Cheng , Richard Weinberger Subject: [PATCH 4.19 237/338] ubifs: Fix read out-of-bounds in ubifs_wbuf_write_nolock() Date: Thu, 14 Apr 2022 15:12:20 +0200 Message-Id: <20220414110845.632867618@linuxfoundation.org> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20220414110838.883074566@linuxfoundation.org> References: <20220414110838.883074566@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhihao Cheng commit 4f2262a334641e05f645364d5ade1f565c85f20b upstream. Function ubifs_wbuf_write_nolock() may access buf out of bounds in following process: ubifs_wbuf_write_nolock(): aligned_len = ALIGN(len, 8); // Assume len = 4089, aligned_len = 4096 if (aligned_len <= wbuf->avail) ... // Not satisfy if (wbuf->used) { ubifs_leb_write() // Fill some data in avail wbuf len -= wbuf->avail; // len is still not 8-bytes aligned aligned_len -= wbuf->avail; } n = aligned_len >> c->max_write_shift; if (n) { n <<= c->max_write_shift; err = ubifs_leb_write(c, wbuf->lnum, buf + written, wbuf->offs, n); // n > len, read out of bounds less than 8(n-len) bytes } , which can be catched by KASAN: ========================================================= BUG: KASAN: slab-out-of-bounds in ecc_sw_hamming_calculate+0x1dc/0x7d0 Read of size 4 at addr ffff888105594ff8 by task kworker/u8:4/128 Workqueue: writeback wb_workfn (flush-ubifs_0_0) Call Trace: kasan_report.cold+0x81/0x165 nand_write_page_swecc+0xa9/0x160 ubifs_leb_write+0xf2/0x1b0 [ubifs] ubifs_wbuf_write_nolock+0x421/0x12c0 [ubifs] write_head+0xdc/0x1c0 [ubifs] ubifs_jnl_write_inode+0x627/0x960 [ubifs] wb_workfn+0x8af/0xb80 Function ubifs_wbuf_write_nolock() accepts that parameter 'len' is not 8 bytes aligned, the 'len' represents the true length of buf (which is allocated in 'ubifs_jnl_xxx', eg. ubifs_jnl_write_inode), so ubifs_wbuf_write_nolock() must handle the length read from 'buf' carefully to write leb safely. Fetch a reproducer in [Link]. Fixes: 1e51764a3c2ac0 ("UBIFS: add new flash file system") Link: https://bugzilla.kernel.org/show_bug.cgi?id=214785 Reported-by: Chengsong Ke Signed-off-by: Zhihao Cheng Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- fs/ubifs/io.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) --- a/fs/ubifs/io.c +++ b/fs/ubifs/io.c @@ -810,16 +810,42 @@ int ubifs_wbuf_write_nolock(struct ubifs */ n = aligned_len >> c->max_write_shift; if (n) { - n <<= c->max_write_shift; + int m = n - 1; + dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, wbuf->offs); - err = ubifs_leb_write(c, wbuf->lnum, buf + written, - wbuf->offs, n); + + if (m) { + /* '(n-1)<max_write_shift < len' is always true. */ + m <<= c->max_write_shift; + err = ubifs_leb_write(c, wbuf->lnum, buf + written, + wbuf->offs, m); + if (err) + goto out; + wbuf->offs += m; + aligned_len -= m; + len -= m; + written += m; + } + + /* + * The non-written len of buf may be less than 'n' because + * parameter 'len' is not 8 bytes aligned, so here we read + * min(len, n) bytes from buf. + */ + n = 1 << c->max_write_shift; + memcpy(wbuf->buf, buf + written, min(len, n)); + if (n > len) { + ubifs_assert(c, n - len < 8); + ubifs_pad(c, wbuf->buf + len, n - len); + } + + err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, n); if (err) goto out; wbuf->offs += n; aligned_len -= n; - len -= n; + len -= min(len, n); written += n; }