Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1004251pxb; Wed, 6 Apr 2022 06:30:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywm/L8AN5bPi+cg8w911Dmd3081Jv5bQfHOfa4eTWND5VFPDuzcBhpMYXKgAQ1bq1aR0HM X-Received: by 2002:a17:90b:2092:b0:1ca:b52d:1811 with SMTP id hb18-20020a17090b209200b001cab52d1811mr10214310pjb.222.1649251852772; Wed, 06 Apr 2022 06:30:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649251852; cv=none; d=google.com; s=arc-20160816; b=UmeJwuAEJ4A2weTg71ZPYPErL0458BXwOfkHDryf7oJ7YGeG6XWFPChU19uwFl+b1x dibObxx0dUgbgeB2BFcRfBiNjJ9tNV+AYa13cWMOisW47SOo+uJSJw6KN94dwYz9ru8R T30bKNqCim5LqP0i0yiiueYQo8k+fEEFNWLxm5eN37g8P0YV7lxIEU0412jtJU5jhZ+l UdjGqMZM83Sn4QPu5V1i8XOfz0yYcoUlQsdjjdEzGBXAOVGFVi9WG2jjwNqDziH4xGWD rY5MNrfkZRd4JJylMHK6iv/MIj/KBpJjSOjqOXP3U/VG7CZ/Doi5LU3aRyoQNH0mjHDV ZX4g== 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=qAJqFmOsa+8MLOpSI7YnO7xVOoMol8WzVAfK/aEUEgs=; b=oBLzDmj2ZmqQxw0AdxockhQ2Hiw799yiFRHG0XV/Lo/4ws3QjzJe4nLRZmqsnvQhJB dEX13zI1mTs8AbZNufJVxS3ocdV9biviczh3L3jJAgjGolEX/6DFi74xBSNN2SvA0nj5 b00Q73BRI3KVowNt0B2zotQqpXVTehoZUYBN2tXCNYAK2YG14woornHbulPu6lk+oR9l KXt4GPoev26qtohZvmqpHhCkKIu65eQGChR3HsCYaKbc3PEwbpjBhH9IMKIbDDm9z3Q6 ISQkfobc57dgyENJ2OLS9EMRPvo4HL3lGDxOVX4VCWhrg+ZfvbCfFhtk6U3nGp3UgS8E nkrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=p9uwMj9P; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id z7-20020a170903018700b0015408cd595asi17180304plg.27.2022.04.06.06.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 06:30:52 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=p9uwMj9P; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 E1F895C81BD; Wed, 6 Apr 2022 04:08:33 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1444515AbiDEWVK (ORCPT + 99 others); Tue, 5 Apr 2022 18:21:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344872AbiDEKkO (ORCPT ); Tue, 5 Apr 2022 06:40:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C21AD2CE08; Tue, 5 Apr 2022 03:25:31 -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 5D8176141D; Tue, 5 Apr 2022 10:25:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 641B9C385A0; Tue, 5 Apr 2022 10:25:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649154330; bh=NzYuvdptzYEU15AeBZX/QMowvTHlIzsvLfoKsENOWLY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p9uwMj9Ptj2hkFj99ond32D9ylDC6ik9mPEX85k57qae1owwh5Sg4ysfGwotgeifs Wgddr476mfYJhcBaiblvg6Q5VclTu8cj0A0mHJKJ/fFyYXdseA/ZFvIrYAF1FHt9za oOZf3TyMjtnx1m9onON5R9v7EU/SMQB+Mqn7KLy0= 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.10 542/599] ubifs: Fix read out-of-bounds in ubifs_wbuf_write_nolock() Date: Tue, 5 Apr 2022 09:33:56 +0200 Message-Id: <20220405070314.972283060@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@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 @@ -846,16 +846,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; }