From patchwork Sat Mar 4 07:42:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhihao Cheng X-Patchwork-Id: 64170 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp815333wrd; Fri, 3 Mar 2023 23:31:52 -0800 (PST) X-Google-Smtp-Source: AK7set9vz3o7db/o1lGS73NGa4FIYiNOZT4mw0mFf8qrCoMtTPfu6gB4dQDkFa6tX6y4ZroJL8dW X-Received: by 2002:a17:902:7891:b0:19a:a267:f16c with SMTP id q17-20020a170902789100b0019aa267f16cmr4347967pll.31.1677915112199; Fri, 03 Mar 2023 23:31:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677915112; cv=none; d=google.com; s=arc-20160816; b=Rc831/XbdpFxIq84VnrvQnYAwC3Z7RN9i9UAUnGUY0ByzQ8e+NDKyGYw/YWBFlP4FV R5qviJRtXpzpdS92jHUaEmyhKH2RS9ecfZJY6Ez+SEeZuDt8h4aiIsDWYjd5Cvhnm3EV LwKEGvMq7rHQhx2yyoRLLKZxYsyuNnSW4ZyuoyFpy/TjthR1eJ9kqukTW/m/gZdIU2aK RahBxFIuWihGZ7xV+WqBeEonm/L5FMu+Uem9oXoWzgkU0j9z+SDw2Mn+FVFX7BuFMqUP TkQS1xqz0jNcQ5+ZGw8rATQkBc9tQCRgHau8DhLNbiUAgBv1Ch8WTcDQgSb4RQuAo/J3 cyRA== 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 :message-id:date:subject:cc:to:from; bh=6W4mIqkAOZhB+sY5t+/eb68Eo3sLFCB4oiHWj0R2+64=; b=WYv6SD5aTf1lKUJAZG/OyqF+gmdX5sFxmK2N9BtXB/YhwgzcAwBZES32nZE1hUxFS2 6mawptVIN/HftvL323BjxWXFOHOQy3lAI/h+IzD496M/tJ6eSj5md1NiEgvN9e+foTRr 9YRtXHj+2kJdOgUh99mv5VHm4kHaQ6Y05BY1wX4vpqsU9st6FatVANA/2iRWfKqPYway 7n82fxnBDOYcdTiCOU6WTYzCbfrKfC1mccwJIhS2tQuhe2vUbgDdM+AWCVWllbQUVWQC kz+DY1w6gSJNbil/q5lyFveFzIn1jMBFUzX12SC1GUk6QLvzjUPEbk86zk61BscLsTU6 gXWw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e16-20020a170902cf5000b0019cbce63a6esi3941145plg.553.2023.03.03.23.31.39; Fri, 03 Mar 2023 23:31:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229591AbjCDHTg (ORCPT + 99 others); Sat, 4 Mar 2023 02:19:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbjCDHTe (ORCPT ); Sat, 4 Mar 2023 02:19:34 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1712717CDD for ; Fri, 3 Mar 2023 23:19:33 -0800 (PST) Received: from kwepemm600013.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4PTGNw4j48z16NpV; Sat, 4 Mar 2023 15:16:48 +0800 (CST) Received: from huawei.com (10.175.127.227) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Sat, 4 Mar 2023 15:19:30 +0800 From: Zhihao Cheng To: , , , CC: , , , Subject: [PATCH v2] ubi: Fix failure attaching when vid_hdr offset equals to (sub)page size Date: Sat, 4 Mar 2023 15:42:43 +0800 Message-ID: <20230304074243.1348767-1-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600013.china.huawei.com (7.193.23.68) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759421516930125463?= X-GMAIL-MSGID: =?utf-8?q?1759421516930125463?= Following process will make ubi attaching failed since commit 1b42b1a36fc946 ("ubi: ensure that VID header offset ... size"): ID="0xec,0xa1,0x00,0x15" # 128M 128KB 2KB modprobe nandsim id_bytes=$ID flash_eraseall /dev/mtd0 modprobe ubi mtd="0,2048" # set vid_hdr offset as 2048 (one page) (dmesg): ubi0 error: ubi_attach_mtd_dev [ubi]: VID header offset 2048 too large. UBI error: cannot attach mtd0 UBI error: cannot initialize UBI, error -22 Rework original solution, the key point is making sure 'vid_hdr_shift + UBI_VID_HDR_SIZE < ubi->vid_hdr_alsize', so we should check vid_hdr_shift rather not vid_hdr_offset. Then, ubi still support (sub)page aligined VID header offset. Fixes: 1b42b1a36fc946 ("ubi: ensure that VID header offset ... size") Signed-off-by: Zhihao Cheng --- v1->v2: Convert UBI_VID_HDR_SIZE as int type. drivers/mtd/ubi/build.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 0904eb40c95f..80803ada0528 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -666,12 +666,6 @@ static int io_init(struct ubi_device *ubi, int max_beb_per1024) ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size); ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size); - if (ubi->vid_hdr_offset && ((ubi->vid_hdr_offset + UBI_VID_HDR_SIZE) > - ubi->vid_hdr_alsize)) { - ubi_err(ubi, "VID header offset %d too large.", ubi->vid_hdr_offset); - return -EINVAL; - } - dbg_gen("min_io_size %d", ubi->min_io_size); dbg_gen("max_write_size %d", ubi->max_write_size); dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size); @@ -689,6 +683,21 @@ static int io_init(struct ubi_device *ubi, int max_beb_per1024) ubi->vid_hdr_aloffset; } + /* + * Memory allocation for VID header is ubi->vid_hdr_alsize + * which is described in comments in io.c. + * Make sure VID header shift + UBI_VID_HDR_SIZE not exceeds + * ubi->vid_hdr_alsize, so that all vid header operations + * won't access memory out of bounds. + */ + if ((ubi->vid_hdr_shift + UBI_VID_HDR_SIZE) > ubi->vid_hdr_alsize) { + ubi_err(ubi, "Invalid VID header offset %d, VID header shift(%d)" + " + VID header size(%d) > VID header aligned size(%d).", + ubi->vid_hdr_offset, ubi->vid_hdr_shift, + (int)UBI_VID_HDR_SIZE, ubi->vid_hdr_alsize); + return -EINVAL; + } + /* Similar for the data offset */ ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE; ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);