Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1749484rwi; Thu, 13 Oct 2022 19:28:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4oFBybR0b4sTkIMVtgIKuVEwt4kLUSG286k9T7aVxS2qVvRKsz2a1np9hcnLM17k/7NpXc X-Received: by 2002:a17:906:9b8e:b0:78c:65a4:af84 with SMTP id dd14-20020a1709069b8e00b0078c65a4af84mr1893759ejc.127.1665714519396; Thu, 13 Oct 2022 19:28:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665714519; cv=none; d=google.com; s=arc-20160816; b=JjGVIGP2X3ujdKRAdfoV/e0ZkamLFnrmMrUOrIB0VNJtFtQcZTlx+4Vkv/Rj/Ede7d xmkBQdM9g8hhjvn/c66+K1xAde5AO4oJfBEznkHunhxQPiCLgxgaoki42ySSRM7oYPh/ f1oH3qkHXVjlxneZfMsqdLyUV4G6ivXMVnue2K58AQxeEluSxfUNZafDiHg+1XczDOi1 XzSd3Z2LLH6DlryPhgS6Ffl/jMMAmVQhmUmRwtliIR5oYwXKHnXVmvoFIi0dU15ljbJO CvBJJ//+x/iIFe82/HSj9R4q5szgNyYrWM9egv+mNRhj/tBWkQZiA3gguI9EsMauZ6kC PJQg== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o16-20020a170906975000b0078d78760c1asi1553588ejy.95.2022.10.13.19.27.57; Thu, 13 Oct 2022 19:28:39 -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 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII 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 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) -- 2.25.1