Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1064875pxb; Wed, 6 Apr 2022 07:56:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzoTm94r5kG5qoIQBqpj68ChcX9aU2VVOImJEhcYDbd5Qw8hsIWQOvNp4Onx0ynGG5myqzc X-Received: by 2002:a63:5759:0:b0:399:5816:6a80 with SMTP id h25-20020a635759000000b0039958166a80mr7543249pgm.253.1649256961961; Wed, 06 Apr 2022 07:56:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649256961; cv=none; d=google.com; s=arc-20160816; b=ge0h2/KPSAaAuQ7Gzl1W87z8FIzfk9qy/IZoxAoPKF3KyLeAQqPcZ7nI4uB99Vj68X d6Ki/qJUHbCxUKR7sB3IP0UEt6u/350KaqRavxEmoE9rFVQVQ/ky/ymFrJ6ftPoTsziK jF84OvmNDU/omYzQ/+aM5uT7VVIomuhu7zNrrqaY4H/Ow3Flly0P60FgS/abcLhdum7o eD/YodHL7SuTtvlsJ2XaNjb+12zeYA+Hu2aIliICCa8HPc8gTeg54kGoqG3rUBIJw/mx /0PglRqJ8PhLiS4HufsMM2aG2Z5C/STf0QaAe2yTRZOSJoIchvWx+oWrKxrZ0F/qZiTK Cx8A== 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=WXyRZv0dFS+FJsCsA8hrbNYSzBnSI5bk55m8kpMzfRcxDp3Ej/ldsv+uP4J3QU7kK2 PCUJuM69wtY/kGIXpNeibj1mQkxaLpb21kzADekI2GjV69p8d34SrRDr7XU508Yi7hgE zSo2niRjjIPGLQi7owORs1Th0CBXrSCHdYyTDq5oHvnS9iMuNWzzM6AD7ikI4xTPDHQu T19tCe+iy9Toafw7hFwpAoj7kDlA3RQNDnjRV8VyeanscUmEh3h/pUBXNhFxqZvndDIS 7lZ1azrF7wUMfUQxgqipneXsm+3wp2iooFAVQBdvkw5rMsHeVVS9pv9rnn8H/HJNW4wz g8Ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HcDPce86; 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 u191-20020a6379c8000000b003816043f0adsi16527431pgc.674.2022.04.06.07.56.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 07:56:01 -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=HcDPce86; 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 981B2DF9; Wed, 6 Apr 2022 05:40:52 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442966AbiDEWUB (ORCPT + 99 others); Tue, 5 Apr 2022 18:20:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354194AbiDEKMP (ORCPT ); Tue, 5 Apr 2022 06:12:15 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C76F452E71; Tue, 5 Apr 2022 02:58:21 -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 ams.source.kernel.org (Postfix) with ESMTPS id 7E0F9B81B18; Tue, 5 Apr 2022 09:58:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC77FC385A2; Tue, 5 Apr 2022 09:58:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152699; bh=SdJxWAh9cn5SWer6yPvtQ51y31r12iq2Iow3hSkJTFM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HcDPce86J1ynSyI3KI0ggXMVX8YkMqGEjBtKBhVed2+O2MuH48kfz2h5v2Zkjy75A aFMmdgrFYFTHufZM08B+2eO+O7g4eTxJML3eGO0+Ld7R4661PlT97yhxm5ZX4mAEkC 0jtLOcDN4cBvxLD8Ch9EDob2kHJ0yVn5KrP+pd+4= 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.15 823/913] ubifs: Fix read out-of-bounds in ubifs_wbuf_write_nolock() Date: Tue, 5 Apr 2022 09:31:25 +0200 Message-Id: <20220405070404.499878464@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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; }