Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp182343rdg; Thu, 12 Oct 2023 02:31:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLeDWJXpgGk+n2iJ/iV0hq0fz40OxFE3371IBx7avofRGwDCf/EeIR6g6sKDEjpEokUeIK X-Received: by 2002:a05:6a21:998c:b0:161:76a4:4f79 with SMTP id ve12-20020a056a21998c00b0016176a44f79mr25712175pzb.23.1697103099626; Thu, 12 Oct 2023 02:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697103099; cv=none; d=google.com; s=arc-20160816; b=YrtlQebSG7tbNZEnp2DOfyjQeXN3lFUAbcrlEEmLKGg29pvVVwiP/lKoL0PfmTHnD1 8yn3TLejv75tvrDrdzppmTlGByTUoqzP7xe5ECnZY6QifqvuhAFXnXhD54u/cp5ZNV4l o4+dckaNj72peHyUWUmxuV/+AizQaWKaRoOiZs6U1V97hWBZouir/mytreIAzovSlbpa GQ88nxm7t2ecSfPCcGjRcm03Xzj39DWqswlqtJVGyF6+qloqLdfw70+etuQ7git/Z3pu Qrp22apiwuPbU77KQfo9QrJ1zLX/lJNrtlx9n396QzcXhPPBIM1opsKzuhhtdr7kJtBt Efcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=DlLscyKbOzRwDMEgD0kiAoS/C0NdN0X5BaIH3Rthk8Y=; fh=RCoRp+2ZNIiPYQrvjha1OwkzO63gNR4bbn6K2OqHrYg=; b=i2mVNuBL45S9tYR7xnDbL/VKOTEJGownTNpK5dHALD8opU4jY6vf+OSKBm7fmd2goX PEBaxPxnL6879hAxj1DgRze+Ces+RXHrnEBIKD8+gfiEHiBS5CRYRERknIo/fdRCpEuw 6s1qH4haslGyYuHFZoX02oYHqEb4XZiqACmMVNRcHIBxf0mLneskpK2q1I2tvlcU7D3N S79MjMwfR4+Y9k5BC/9T11+V3xcyR2Y/CuNswrxIyQhub8/hf0m4vQQ7qaLoN7M/dwkD PCVIZdd3b9Rn3rfPKLJzdmqKgc2WaDmeRmqYusM9FbcbHY3fSKdwlujPm6aJA3N9FmxY jCrw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id ha12-20020a17090af3cc00b00263f3c1bb86si1796971pjb.158.2023.10.12.02.31.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 02:31:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C2D268116B26; Thu, 12 Oct 2023 02:31:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235329AbjJLJbR (ORCPT + 99 others); Thu, 12 Oct 2023 05:31:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229981AbjJLJbQ (ORCPT ); Thu, 12 Oct 2023 05:31:16 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 373E091 for ; Thu, 12 Oct 2023 02:31:14 -0700 (PDT) Received: from dggpemm500009.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4S5kpY2whvz1M9NF; Thu, 12 Oct 2023 17:28:37 +0800 (CST) Received: from [10.174.178.209] (10.174.178.209) by dggpemm500009.china.huawei.com (7.185.36.225) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Thu, 12 Oct 2023 17:31:11 +0800 Message-ID: <2d04fa9e-e594-705c-339b-3090cb7d6fbd@huawei.com> Date: Thu, 12 Oct 2023 17:31:11 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.11.2 Subject: Re: [PATCH RFC] ubi: gluebi: Fix NULL pointer dereference caused by ftl notifier To: Zhihao Cheng , , , CC: , , , References: <20231010142925.545238-1-wangzhaolong1@huawei.com> <9f96baf1-962e-d595-0e4f-797315cd0348@huawei.com> From: ZhaoLong Wang In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.209] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500009.china.huawei.com (7.185.36.225) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 12 Oct 2023 02:31:37 -0700 (PDT) >> >>> 3.         P1                    P2 >>>     gluebi_create -> mtd_device_register -> add_mtd_device: >>>     device_register   // dev/mtd1 is visible >>> >>>                       fd = open(/dev/mtd1, O_WRONLY) >>>                        gluebi_get_device >>>                         gluebi->desc = ubi_open_volume >>> >>>     ftl_add_mtd >>>      mtd_read >>>       gluebi_read >>>        gluebi->desc is not ERR_PTR/NULL >>> >>>                      close(fd) >>>                       gluebi_put_device >>>                        ubi_close_volume >>>                         kfree(desc) >>>        ubi_read(gluebi->desc)   // UAF  (×) >>> >> >> Yes, it's also a problem. Perhaps it should be set to NULL after >> destroying gluebi->desc. > > The key point is that 'gluebi->desc' check & usage is not atomic in > gluebi_read. So following patch still can't handle situation 3. > Setting the desc to NULL works because mutex_lock "mtd_table_mutex" is held on all paths where ftl_add_mtd() is called. ubi_gluebi_init ubi_register_volume_notifier ubi_enumerate_volumes ubi_notify_all gluebi_notify nb->notifier_call() gluebi_create mtd_device_register mtd_device_parse_register add_mtd_device mutex_lock(&mtd_table_mutex); blktrans_notify_add not->add() ftl_add_mtd tr->add_mtd() scan_header mtd_read mtd_read mtd_read_oob gluebi_read mtd->read() gluebi->desc - NULL mutex_unlock(&mtd_table_mutex); ubi_cdev_ioctl ubi_create_volume ubi_volume_notify blocking_notifier_call_chain [kernel/notifier.c] notifier_call_chain gluebi_notify nb->notifier_call() gluebi_create mtd_device_register mtd_device_parse_register add_mtd_device mutex_lock(&mtd_table_mutex); blktrans_notify_add not->add() ftl_add_mtd tr->add_mtd() scan_header mtd_read(part->mbd.mtd, mtd_read_oob gluebi_read mtd->read() ubi_read(gluebi->desc mutex_unlock(&mtd_table_mutex); load_module ftl register_mtd_blktrans mutex_lock(&mtd_table_mutex); ftl_add_mtd not->add() scan_header mtd_read(part->mbd.mtd, mtd_read_oob gluebi_read mtd->read() ubi_read(gluebi->desc mutex_unlock(&mtd_table_mutex); This lock is also held during the process of closing the file. close(fd) mtdchar_close put_mtd_device mutex_lock(&mtd_table_mutex); __put_mtd_device gluebi_put_device ubi_close_volume kfree(desc) // desc == NULL mutex_unlock(&mtd_table_mutex);