Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp747705pxb; Tue, 5 Apr 2022 21:34:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfzXUKfM4f0++yw2bQC4ZMV/iIuyBzFiM+xQQOKJCr5nyjqVSv2Se4nZnGRD9DJ+hpOtBZ X-Received: by 2002:a05:6a00:4096:b0:4fa:8f23:e972 with SMTP id bw22-20020a056a00409600b004fa8f23e972mr7122554pfb.26.1649219666716; Tue, 05 Apr 2022 21:34:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649219666; cv=none; d=google.com; s=arc-20160816; b=DbFdKBM0K70Dj/PMB9YlvjX0V47KmBenjwKkWi/Ydrqqdx7mfJD6XzEUzJ8mjvq3gM BH8O6pJLm7R/G0s2Ky0QUSyTpvZTn55hMJ6UODBeCOlKT1F01pLshtfbA5/gntZjm3QC ZFI+75IaOdtBDodTMMbFwaZzTlQhYdpKWCj0tJA33wI9eEADz9Np+PxuulV9CczPJvni pmvULAXHNBv/77UmkaC1daCguR6L8fKVM3Sa4fG/HCyU3EW8WkEWHtceX23/eIcfyq66 iI8BVGU0VlQZTJCNu3B6iqBLxqqlk1idFMR8/m086+C1+ujm9tUUxKMbxjwxuVhCKHUX 1f2A== 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=HbBcsyvqluYUAuyG4J8jfUD4apU0DTukHB6hQTx2y+A=; b=r78KyS8edkccJRYi8/wogjoLymFTX4MnBNP43nYgjtKX32mTHP6UDSHh+9sLsNjVVQ /WPQ1Tom+MXpbeidmLbXVkRYX6AeQuka/K1wNLmIyQP8w3UonT4ZpmIF7yWYqymcQMl1 X5HIxFkK71u2RXLoEWKKdkSUh3mWIN6XMsOIdCnVzeCHflwochcPN1Nqarbp4CImvf7h alQ4p4Z00f1pSFSzlrDtZMFouvWlekzLdOtj4APEy04ny740tMtaTVm2yeHuTmYG56Cj vpHLym18Hu84XbKDPAzhZjfsthPGTaOOyf64rVO86f7JmJS/YdrB1i3vjllGUQI05ETg 4SGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=x4xoX4Mf; 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 pf11-20020a17090b1d8b00b001c7049d4fe7si4156209pjb.146.2022.04.05.21.34.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 21:34:26 -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=x4xoX4Mf; 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 5E37B28CF5E; Tue, 5 Apr 2022 20:03:18 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1390855AbiDENnp (ORCPT + 99 others); Tue, 5 Apr 2022 09:43:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346252AbiDEJXk (ORCPT ); Tue, 5 Apr 2022 05:23:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD8FBCA0E1; Tue, 5 Apr 2022 02:13:13 -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 15A5B61672; Tue, 5 Apr 2022 09:13:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2388FC385A0; Tue, 5 Apr 2022 09:13:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649149992; bh=SdJxWAh9cn5SWer6yPvtQ51y31r12iq2Iow3hSkJTFM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=x4xoX4MfNcGpFt4FsZR2+5P2GJUmvS3Fnp5K7R9qusg+3G+WZRWlc90b6pbdd1iwN 96z1h82w2t38gjTp4f7/ieoLpy5X7pQQ9vThRnFQvh8x3jwdw6cbgoSUUZO/DgIjeI wFJ26fHUVnOIwjwnEYKbqG+mKPiAGOAtCYV/224Y= 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 5.16 0914/1017] ubifs: Fix read out-of-bounds in ubifs_wbuf_write_nolock() Date: Tue, 5 Apr 2022 09:30:27 +0200 Message-Id: <20220405070421.351725476@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@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 @@ -833,16 +833,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; }