Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6632340rwr; Tue, 2 May 2023 03:20:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4RORID3uleFRCvvpmZwRHUOTTSeNWaFZ6CGUjvV/aB94hB7TRD34cYpt2nzdy0RkD5ahRT X-Received: by 2002:a17:902:ecd1:b0:1a6:6b85:7b4a with SMTP id a17-20020a170902ecd100b001a66b857b4amr17414254plh.18.1683022825255; Tue, 02 May 2023 03:20:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683022825; cv=none; d=google.com; s=arc-20160816; b=mIxj8htY2c8foJLyoOW80RpP+O5IGpNDly5dHt3wMecVjDyZ2xQfbDFcfgZpNwdDbb K42+7Mwwqm3URSXlZyddYCU+OrXeifI6b6cbaVZqDn2lSpohonkoiHUOy7zZ5hOi1oYP ghqJ7RseF0DFr332XiwdmUUuJy9twDujwnhrNqWYVef04T3Ar/4qWzSS2fa0sWxBnY/6 lNqI8sBQr0qeivrmtMm2K0TrsSPiUQ/cpcuPLGYihvR/CZBsyw2rQJy5rGu42xyfVdA3 1xL+942alsSQKux3wZSW5aSk4DYYq59CYKaP5jB3KUKqQb4HxX+7vTepj3ytbb175SGf QX4A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=s/6sFSlGfMf3SMKOopDC98QIBHme0pnqfLuYiOrN5/I=; b=OH6KG1+3m4SxbxHMP9CeqrgXfOUsddX1HNh+4sEjn2d9DgtHVRfKW+HD2PXtRS+ooL Q/aN/lPfEBT/mqVwkPNJKsO+6QD6hehz3Y1H2dWoZIclkcULpSgLe0UMqwE40+L8AC9H JCcGqNKpxlpBao4fZOqYHHgQFZkxRbPFya4EP2Shs/IUNUYQ7r1GUxC4FT6UFEXaRtke OTZ6Su5B15PJifaxEyWSULPubdOheUV2yak6Yk5b/oHHxvdkzHm2kBG8hfFqjerccQTA 03rXBVoQ2uyWQRx5kGnGeFKQjGXq9hCzIiitFjRYBm+qJrT1R0VCGxCEBPSerEiYEIep uX9A== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j23-20020a170902759700b001aafe496e6dsi3466695pll.539.2023.05.02.03.20.10; Tue, 02 May 2023 03:20:25 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232197AbjEBKRU (ORCPT + 99 others); Tue, 2 May 2023 06:17:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233120AbjEBKRS (ORCPT ); Tue, 2 May 2023 06:17:18 -0400 Received: from baptiste.telenet-ops.be (baptiste.telenet-ops.be [IPv6:2a02:1800:120:4::f00:13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 473C630F9 for ; Tue, 2 May 2023 03:17:12 -0700 (PDT) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed30:f07a:92a2:297:162b]) by baptiste.telenet-ops.be with bizsmtp id rmHA290075FQxRj01mHA2t; Tue, 02 May 2023 12:17:10 +0200 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtp (Exim 4.95) (envelope-from ) id 1ptn4D-000ymV-Ut; Tue, 02 May 2023 12:17:10 +0200 Received: from geert by rox.of.borg with local (Exim 4.95) (envelope-from ) id 1ptn4H-00AtQ6-UW; Tue, 02 May 2023 12:17:09 +0200 From: Geert Uytterhoeven To: Dmitry Torokhov , Javier Martinez Canillas , Brendan Higgins , David Gow Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH 1/2] Input: tests - fix use-after-free and refcount underflow in input_test_exit() Date: Tue, 2 May 2023 12:17:02 +0200 Message-Id: <957b3b309a44d39fb6e38b2a526b250f69ea3d2c.1683022164.git.geert+renesas@glider.be> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 With CONFIG_DEBUG_SLAB=y: # Subtest: input_core 1..3 input: Test input device as /devices/virtual/input/input1 8<--- cut here --- Unable to handle kernel paging request at virtual address 6b6b6dd7 when read ... __lock_acquire from lock_acquire+0x26c/0x300 lock_acquire from _raw_spin_lock_irqsave+0x50/0x64 _raw_spin_lock_irqsave from devres_remove+0x20/0x7c devres_remove from devres_destroy+0x8/0x24 devres_destroy from input_free_device+0x2c/0x60 input_free_device from kunit_try_run_case+0x70/0x94 [kunit] Without CONFIG_DEBUG_SLAB=y: KTAP version 1 # Subtest: input_core 1..3 input: Test input device as /devices/virtual/input/input1 ------------[ cut here ]------------ WARNING: CPU: 0 PID: 694 at lib/refcount.c:28 refcount_warn_saturate+0x54/0x100 refcount_t: underflow; use-after-free. ... Call Trace: [<0037cad4>] dump_stack+0xc/0x10 [<00377614>] __warn+0x7e/0xb4 [<0037768c>] warn_slowpath_fmt+0x42/0x62 [<001eee1c>] refcount_warn_saturate+0x54/0x100 [<000b1d34>] kfree_const+0x0/0x20 [<0036290a>] __kobject_del+0x0/0x6e [<001eee1c>] refcount_warn_saturate+0x54/0x100 [<00362a1a>] kobject_put+0xa2/0xb6 [<11965770>] kunit_generic_run_threadfn_adapter+0x0/0x1c [kunit] As per the comments for input_allocate_device() and input_register_device(), input_free_device() must be called only to free devices that have not been registered. input_unregister_device() already calls input_put_device(), thus leading to a use-after-free. Moreover, the kunit_suite.exit() method is called after every test case, even on failures. As the test itself already does cleanups in its failure paths, this may lead to a second use-after-free. Fix the first issue by dropping the call to input_allocate_device() from input_test_exit(). Fix the second issue by making the cleanup code conditional on a successful test. Fixes: fdefcbdd6f361841 ("Input: Add KUnit tests for some of the input core helper functions") Signed-off-by: Geert Uytterhoeven --- drivers/input/tests/input_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/input/tests/input_test.c b/drivers/input/tests/input_test.c index e5a6c1ad2167c103..8b8ac3412a70d3b4 100644 --- a/drivers/input/tests/input_test.c +++ b/drivers/input/tests/input_test.c @@ -43,8 +43,8 @@ static void input_test_exit(struct kunit *test) { struct input_dev *input_dev = test->priv; - input_unregister_device(input_dev); - input_free_device(input_dev); + if (input_dev) + input_unregister_device(input_dev); } static void input_test_poll(struct input_dev *input) { } -- 2.34.1