From patchwork Thu Feb 9 23:58:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pietro Borrello X-Patchwork-Id: 13135247 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BF04C61DA4 for ; Thu, 9 Feb 2023 23:59:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229986AbjBIX7C (ORCPT ); Thu, 9 Feb 2023 18:59:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229940AbjBIX7B (ORCPT ); Thu, 9 Feb 2023 18:59:01 -0500 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C7FC56EFA for ; Thu, 9 Feb 2023 15:58:58 -0800 (PST) Received: by mail-ej1-x634.google.com with SMTP id sa10so11512670ejc.9 for ; Thu, 09 Feb 2023 15:58:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=diag.uniroma1.it; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=zdbMGzrRc8LDNDumfc9xBhICdUvlCcLYTrnDicQI/uI=; b=iZEvxitkI76QJtUhzk3xKX/4EPowsvl34KB9wqntEq4yuICsE2lG0hZDRWnrdY8kiu 9+FII9iaTX1nqznQnMDq9H2ZblSYgJs8M2hJDhr5oAx5b4xwpnlbp3159acgb3uZaLHz 9gMGregBPode/jZNDNY61M2JaxNfq0zQ0/cL4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zdbMGzrRc8LDNDumfc9xBhICdUvlCcLYTrnDicQI/uI=; b=pnO1MmyS29x7uFRrBwoDcYiQfaLPt0jbjJAnQbTnWCnBgogIOVkYTatZQixTp2TqTt VYbKDy5TL5wcCU3HRZxSTEBnAdUo4+Vg3slAXtjFXOAx2/ZixNw9VMl1QY87zYvwmXhD HELW+B6n73f7Uo6N6+aM6QiGmUN6Wq9frqQFo8qvT2Ubgnbv5cCEQmWDj8I0RAnW9/Wt CZd7aHZ53PTuppBRyfXsOm/03o2UqMFyXW6ECSG+MF/L8aeBkOxXRqtat5kq7tCRRedY uIWOrrku2+YoANdzcvqyDiw2NCAD4jqs9JSQkJ0ip5as5GRy3Frwcv+Y4B/S/vg+AcO2 gLng== X-Gm-Message-State: AO0yUKXrgx6Yw4BMy+GEVbPkbQM0QctSFT316af0DvdSneTmC9GorECT 9S355ApRW5ZfBWMeARaee+/pZA== X-Google-Smtp-Source: AK7set+GrLbFJZDTcwJI/wZkTwNLZg7fQ7UY4vfNSAVWU+vnDfawBhQ26ctzBcy3UkjAkshi059wzA== X-Received: by 2002:a17:906:d92:b0:88a:a09d:e673 with SMTP id m18-20020a1709060d9200b0088aa09de673mr12958032eji.31.1675987136696; Thu, 09 Feb 2023 15:58:56 -0800 (PST) Received: from [192.168.17.2] (wolkje-127.labs.vu.nl. [130.37.198.127]) by smtp.gmail.com with ESMTPSA id fi9-20020a170906da0900b0084c6ec69a9dsm1528177ejb.124.2023.02.09.15.58.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 15:58:56 -0800 (PST) From: Pietro Borrello Date: Thu, 09 Feb 2023 23:58:55 +0000 Subject: [PATCH v3 1/2] HID: bigben: use spinlock to safely schedule workers MIME-Version: 1.0 Message-Id: <20230125-hid-unregister-leds-v3-1-0a52ac225e00@diag.uniroma1.it> References: <20230125-hid-unregister-leds-v3-0-0a52ac225e00@diag.uniroma1.it> In-Reply-To: <20230125-hid-unregister-leds-v3-0-0a52ac225e00@diag.uniroma1.it> To: Jiri Kosina , Benjamin Tissoires , Hanno Zulla , Carlo Caione Cc: Cristiano Giuffrida , "Bos, H.J." , Jakob Koschel , Jiri Kosina , Roderick Colenbrander , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Pietro Borrello X-Mailer: b4 0.12.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1675987135; l=4759; i=borrello@diag.uniroma1.it; s=20221223; h=from:subject:message-id; bh=q65DuWimKWU3Wcj6tgEaabi1e6T/ANMNxvlhUu1UoEE=; b=RyPUT+ZOAScekoh6qrPJ9YICAO7xxeCffbjKBmnLlGmLHTuP9bs5Ddv4qLA3VvcpDiSev3TMxYGW bxy8fUcIBl3AEEsI0sPnpEOY3i/76etzKjW28OM6YEZQGCT+O28a X-Developer-Key: i=borrello@diag.uniroma1.it; a=ed25519; pk=4xRQbiJKehl7dFvrG33o2HpveMrwQiUPKtIlObzKmdY= Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Use spinlocks to deal with workers introducing a wrapper bigben_schedule_work(), and several spinlock checks. Otherwise, bigben_set_led() may schedule bigben->worker after the structure has been freed, causing a use-after-free. Fixes: 4eb1b01de5b9 ("HID: hid-bigbenff: fix race condition for scheduled work during removal") Signed-off-by: Pietro Borrello --- drivers/hid/hid-bigbenff.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/drivers/hid/hid-bigbenff.c b/drivers/hid/hid-bigbenff.c index e8b16665860d..28769aa7fed6 100644 --- a/drivers/hid/hid-bigbenff.c +++ b/drivers/hid/hid-bigbenff.c @@ -174,6 +174,7 @@ static __u8 pid0902_rdesc_fixed[] = { struct bigben_device { struct hid_device *hid; struct hid_report *report; + spinlock_t lock; bool removed; u8 led_state; /* LED1 = 1 .. LED4 = 8 */ u8 right_motor_on; /* right motor off/on 0/1 */ @@ -184,15 +185,24 @@ struct bigben_device { struct work_struct worker; }; +static inline void bigben_schedule_work(struct bigben_device *bigben) +{ + unsigned long flags; + + spin_lock_irqsave(&bigben->lock, flags); + if (!bigben->removed) + schedule_work(&bigben->worker); + spin_unlock_irqrestore(&bigben->lock, flags); +} static void bigben_worker(struct work_struct *work) { struct bigben_device *bigben = container_of(work, struct bigben_device, worker); struct hid_field *report_field = bigben->report->field[0]; + unsigned long flags; - if (bigben->removed || !report_field) - return; + spin_lock_irqsave(&bigben->lock, flags); if (bigben->work_led) { bigben->work_led = false; @@ -219,6 +229,8 @@ static void bigben_worker(struct work_struct *work) report_field->value[7] = 0x00; /* padding */ hid_hw_request(bigben->hid, bigben->report, HID_REQ_SET_REPORT); } + + spin_unlock_irqrestore(&bigben->lock, flags); } static int hid_bigben_play_effect(struct input_dev *dev, void *data, @@ -228,6 +240,7 @@ static int hid_bigben_play_effect(struct input_dev *dev, void *data, struct bigben_device *bigben = hid_get_drvdata(hid); u8 right_motor_on; u8 left_motor_force; + unsigned long flags; if (!bigben) { hid_err(hid, "no device data\n"); @@ -242,10 +255,13 @@ static int hid_bigben_play_effect(struct input_dev *dev, void *data, if (right_motor_on != bigben->right_motor_on || left_motor_force != bigben->left_motor_force) { + spin_lock_irqsave(&bigben->lock, flags); bigben->right_motor_on = right_motor_on; bigben->left_motor_force = left_motor_force; bigben->work_ff = true; - schedule_work(&bigben->worker); + spin_unlock_irqrestore(&bigben->lock, flags); + + bigben_schedule_work(bigben); } return 0; @@ -259,6 +275,7 @@ static void bigben_set_led(struct led_classdev *led, struct bigben_device *bigben = hid_get_drvdata(hid); int n; bool work; + unsigned long flags; if (!bigben) { hid_err(hid, "no device data\n"); @@ -267,6 +284,7 @@ static void bigben_set_led(struct led_classdev *led, for (n = 0; n < NUM_LEDS; n++) { if (led == bigben->leds[n]) { + spin_lock_irqsave(&bigben->lock, flags); if (value == LED_OFF) { work = (bigben->led_state & BIT(n)); bigben->led_state &= ~BIT(n); @@ -274,10 +292,11 @@ static void bigben_set_led(struct led_classdev *led, work = !(bigben->led_state & BIT(n)); bigben->led_state |= BIT(n); } + spin_unlock_irqrestore(&bigben->lock, flags); if (work) { bigben->work_led = true; - schedule_work(&bigben->worker); + bigben_schedule_work(bigben); } return; } @@ -307,8 +326,12 @@ static enum led_brightness bigben_get_led(struct led_classdev *led) static void bigben_remove(struct hid_device *hid) { struct bigben_device *bigben = hid_get_drvdata(hid); + unsigned long flags; + spin_lock_irqsave(&bigben->lock, flags); bigben->removed = true; + spin_unlock_irqrestore(&bigben->lock, flags); + cancel_work_sync(&bigben->worker); hid_hw_stop(hid); } @@ -362,6 +385,7 @@ static int bigben_probe(struct hid_device *hid, set_bit(FF_RUMBLE, hidinput->input->ffbit); INIT_WORK(&bigben->worker, bigben_worker); + spin_lock_init(&bigben->lock); error = input_ff_create_memless(hidinput->input, NULL, hid_bigben_play_effect); @@ -402,7 +426,7 @@ static int bigben_probe(struct hid_device *hid, bigben->left_motor_force = 0; bigben->work_led = true; bigben->work_ff = true; - schedule_work(&bigben->worker); + bigben_schedule_work(bigben); hid_info(hid, "LED and force feedback support for BigBen gamepad\n"); From patchwork Thu Feb 9 23:58:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pietro Borrello X-Patchwork-Id: 13135248 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B826C61DA4 for ; Thu, 9 Feb 2023 23:59:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230018AbjBIX7E (ORCPT ); Thu, 9 Feb 2023 18:59:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230006AbjBIX7C (ORCPT ); Thu, 9 Feb 2023 18:59:02 -0500 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFD7C5A9D8 for ; Thu, 9 Feb 2023 15:58:58 -0800 (PST) Received: by mail-ej1-x629.google.com with SMTP id c26so6761650ejz.10 for ; Thu, 09 Feb 2023 15:58:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=diag.uniroma1.it; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=XHeWZ65g4BMWv1wpl53BAYKvu7rKoumv2nA2YEeP6jg=; b=UzQWu9+vMmJKCczmOAEbeSrdKvhAmFa6KOuiECJoBeZQpc29/PL26NBo6Hd+DKZ2lC KuTqWz6XIaV5MzitHcn7zc9SumXQkWbyF9TBcSsX1Q0DAVTRJTFOpNy9Y7srdbuWj4vZ v2CarUm7tvVa0Flgt5frBD/OaF7LNkr8NDhww= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XHeWZ65g4BMWv1wpl53BAYKvu7rKoumv2nA2YEeP6jg=; b=TYyBkolRZLv2SAFjpeAxMZP4ku3f433fERlRjZNxrwt8l2fvLtpH5YF9vrgezuyBAY 1UhHIZLFGGd5HOHuJDkwVQ7y5TCKl7yla8HAQMY2/hKwI2u3C54E8CSCc2mj0hfv7r1a KmaVIJD1dWQ1/gSucw4pyr42xldB2DYeLFp9aNOF1EBtmW3zxlzCmwjI2Njj//aFmVq/ L03WWD/qNZRR9NI2UPhOboKbB1CYrbcCb4al700HhpQfJH/N43SHTS4aSyKaXgy60Yrp szsxB95ASsp10BCMDUioS7OLvlaQIq5ra51ww6GWRu8Qhu7pukczWzC19Lo5KOli2UWK 1jJQ== X-Gm-Message-State: AO0yUKWNNiIjozcjXByr4y1OWgWpw7OFzjzIBPEujN/uxGYroudbuYqv smwkBtt0gBDMvpj+ZR/EYzD4bQ== X-Google-Smtp-Source: AK7set8CpmMMo4bA7feRvqMUcm6S94eLmyUaBP/asuzG+0iTf9JFrrf8rh+xN7FpD2S+VXRzGbeSDA== X-Received: by 2002:a17:907:c23:b0:8aa:b526:36b3 with SMTP id ga35-20020a1709070c2300b008aab52636b3mr16925820ejc.14.1675987137291; Thu, 09 Feb 2023 15:58:57 -0800 (PST) Received: from [192.168.17.2] (wolkje-127.labs.vu.nl. [130.37.198.127]) by smtp.gmail.com with ESMTPSA id fi9-20020a170906da0900b0084c6ec69a9dsm1528177ejb.124.2023.02.09.15.58.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 15:58:56 -0800 (PST) From: Pietro Borrello Date: Thu, 09 Feb 2023 23:58:56 +0000 Subject: [PATCH v3 2/2] HID: asus: use spinlock to safely schedule workers MIME-Version: 1.0 Message-Id: <20230125-hid-unregister-leds-v3-2-0a52ac225e00@diag.uniroma1.it> References: <20230125-hid-unregister-leds-v3-0-0a52ac225e00@diag.uniroma1.it> In-Reply-To: <20230125-hid-unregister-leds-v3-0-0a52ac225e00@diag.uniroma1.it> To: Jiri Kosina , Benjamin Tissoires , Hanno Zulla , Carlo Caione Cc: Cristiano Giuffrida , "Bos, H.J." , Jakob Koschel , Jiri Kosina , Roderick Colenbrander , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Pietro Borrello X-Mailer: b4 0.12.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1675987135; l=3166; i=borrello@diag.uniroma1.it; s=20221223; h=from:subject:message-id; bh=IdfH/zx2eMB+IWRHkM7AohAvJEH/GhNAfk3hb0BMJ34=; b=yFzHFWPuS+supnmEZxKFZUrkALQEH4Ttl6+azLTN4F5JD2ZaTP/Ejm0jUKxR4NlKTeUXfe0ptmMU LyCKr1D6C5KIYPOdiTxjG+Xg4wtVm5hbQ34Y5mxE4kpLvNoJUDKS X-Developer-Key: i=borrello@diag.uniroma1.it; a=ed25519; pk=4xRQbiJKehl7dFvrG33o2HpveMrwQiUPKtIlObzKmdY= Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Use spinlocks to deal with workers introducing a wrapper asus_schedule_work(), and several spinlock checks. Otherwise, asus_kbd_backlight_set() may schedule led->work after the structure has been freed, causing a use-after-free. Fixes: af22a610bc38 ("HID: asus: support backlight on USB keyboards") Signed-off-by: Pietro Borrello --- drivers/hid/hid-asus.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-asus.c b/drivers/hid/hid-asus.c index f99752b998f3..30e194803bd7 100644 --- a/drivers/hid/hid-asus.c +++ b/drivers/hid/hid-asus.c @@ -98,6 +98,7 @@ struct asus_kbd_leds { struct hid_device *hdev; struct work_struct work; unsigned int brightness; + spinlock_t lock; bool removed; }; @@ -490,13 +491,23 @@ static int rog_nkey_led_init(struct hid_device *hdev) return ret; } +static void asus_schedule_work(struct asus_kbd_leds *led) +{ + unsigned long flags; + + spin_lock_irqsave(&led->lock, flags); + if (!led->removed) + schedule_work(&led->work); + spin_unlock_irqrestore(&led->lock, flags); +} + static void asus_kbd_backlight_set(struct led_classdev *led_cdev, enum led_brightness brightness) { struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds, cdev); led->brightness = brightness; - schedule_work(&led->work); + asus_schedule_work(led); } static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev) @@ -512,15 +523,17 @@ static void asus_kbd_backlight_work(struct work_struct *work) struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work); u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 }; int ret; + unsigned long flags; - if (led->removed) - return; + spin_lock_irqsave(&led->lock, flags); buf[4] = led->brightness; ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf)); if (ret < 0) hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret); + + spin_unlock_irqrestore(&led->lock, flags); } /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes @@ -584,6 +597,7 @@ static int asus_kbd_register_leds(struct hid_device *hdev) drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set; drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get; INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work); + spin_lock_init(&drvdata->kbd_backlight->lock); ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev); if (ret < 0) { @@ -1119,9 +1133,13 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) static void asus_remove(struct hid_device *hdev) { struct asus_drvdata *drvdata = hid_get_drvdata(hdev); + unsigned long flags; if (drvdata->kbd_backlight) { + spin_lock_irqsave(&drvdata->kbd_backlight->lock, flags); drvdata->kbd_backlight->removed = true; + spin_unlock_irqrestore(&drvdata->kbd_backlight->lock, flags); + cancel_work_sync(&drvdata->kbd_backlight->work); }