From patchwork Fri Oct 14 01:52:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yingliang X-Patchwork-Id: 2459 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp572802wrs; Thu, 13 Oct 2022 19:03:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4WCEUa+c24Wq836hoSqtfBKkf9AP62c3QCViIz74m2zJRxTSdEEAEs9nYaXZe0uEPjpKf9 X-Received: by 2002:a17:906:9b83:b0:730:b3ae:343 with SMTP id dd3-20020a1709069b8300b00730b3ae0343mr1890779ejc.670.1665712982463; Thu, 13 Oct 2022 19:03:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665712982; cv=none; d=google.com; s=arc-20160816; b=FTnt6PrU6qZLcSpjzmfbyaAqgMb3E0Rdbz7gcSP9sGvymt+cWQ3bCF7IounG30vnyY SWoG5StdmBafellEsdFg8W+PwEW7xZc0OWsXX6use5HJIUiqY1g7jx+PZcAdiRwtEic0 tGiEalhIReR5kR5yWATDpkFxSxuwv+HhlSSYK/kSODn+bRkWEVMs0JjBt3EiTudFwx4p fyqTHqyebLE0pRzWMLoDuTlE2nhJr4Ud4Epi6/MuuXPtVmQZ2XlXVg8NKS3ZTdbxZPxG eRNhOxTOCbWxD5z2PlmijHT8G30c6eF9HwxKK2FMndXTTY/PC0yEjQMCgui93nKMZAAh kJbg== 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=tFzduVrc6xLhKT2esnm60GW/XDsMHeS3I6Kf4NJ2WmE=; b=rxFgR1dkI6aKPiT0+xm+tVrlynsVpwapRn4umbsx7pzlO2f7Y+aETSOGZJpsk4Xs7X Tp97c2ckfRHb5ehniZlo9oGBJrotDhYUbte9wSJqfALqCxY7HPPzBuLHiKEcPg+c1OM/ HoucM527qzjCHiiBVvpLqMLLRQjYg8SpIWmKiWyZh58k5LTckhhDf6s+fZs4dlB4UU9h STYVlIemYeEQ+OUiD/4W4bCzWde/zfj1glRGGJb+gWBGO96J5noAsXUwCNbbqiGEj0ne hyT7IUFC3gzQsxGqR3mbuHtmN4przFKLZUi5emYns2hWXTuPAOpUcZnNcc4FdDua3/1+ rTbA== 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 js16-20020a17090797d000b0077d8a28dc81si1104316ejc.594.2022.10.13.19.02.20; Thu, 13 Oct 2022 19:03:02 -0700 (PDT) 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 S229712AbiJNBwy (ORCPT + 99 others); Thu, 13 Oct 2022 21:52:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229540AbiJNBwu (ORCPT ); Thu, 13 Oct 2022 21:52:50 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEFF3189830 for ; Thu, 13 Oct 2022 18:52:47 -0700 (PDT) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MpTpP3gqPzpVtc; Fri, 14 Oct 2022 09:49:33 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 14 Oct 2022 09:52:46 +0800 Received: from huawei.com (10.175.103.91) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 14 Oct 2022 09:52:45 +0800 From: Yang Yingliang To: CC: , , Subject: [PATCH] platform/loongarch: laptop: fix possible UAF and simplify return in generic_acpi_laptop_init() Date: Fri, 14 Oct 2022 09:52:12 +0800 Message-ID: <20221014015212.1150629-1-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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?1746626655920857354?= X-GMAIL-MSGID: =?utf-8?q?1746626655920857354?= Current the return value of 'sub_driver->init' is not checked, if sparse_keymap_setup() called in the init function fails, 'generic_inputdev' is freed, then it willl lead a UAF when using it in generic_acpi_laptop_init(). Fix it by checking return value. Set generic_inputdev to NULL after free to avoid double free it. The error code in generic_subdriver_init() is always negative, the return of generic_subdriver_init() can be simplified. Fixes: 6246ed09111f ("LoongArch: Add ACPI-based generic laptop driver") Signed-off-by: Yang Yingliang --- drivers/platform/loongarch/loongson-laptop.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/platform/loongarch/loongson-laptop.c b/drivers/platform/loongarch/loongson-laptop.c index f0166ad5d2c2..572290abf151 100644 --- a/drivers/platform/loongarch/loongson-laptop.c +++ b/drivers/platform/loongarch/loongson-laptop.c @@ -448,6 +448,7 @@ static int __init event_init(struct generic_sub_driver *sub_driver) if (ret < 0) { pr_err("Failed to setup input device keymap\n"); input_free_device(generic_inputdev); + generic_inputdev = NULL; return ret; } @@ -502,8 +503,11 @@ static int __init generic_subdriver_init(struct generic_sub_driver *sub_driver) if (ret) return -EINVAL; - if (sub_driver->init) - sub_driver->init(sub_driver); + if (sub_driver->init) { + ret = sub_driver->init(sub_driver); + if (ret) + goto err_out; + } if (sub_driver->notify) { ret = setup_acpi_notify(sub_driver); @@ -519,7 +523,7 @@ static int __init generic_subdriver_init(struct generic_sub_driver *sub_driver) err_out: generic_subdriver_exit(sub_driver); - return (ret < 0) ? ret : 0; + return ret; } static void generic_subdriver_exit(struct generic_sub_driver *sub_driver)