Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1193110pxb; Fri, 20 Nov 2020 03:40:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJydyjXiwx+x0WaN6FjDanqhfOpVaP9FsmwHxhT3yRQ4H9MGsdz9pDvj3CGzc+ZbqD7TAMyO X-Received: by 2002:a05:6402:154b:: with SMTP id p11mr35557303edx.217.1605872426998; Fri, 20 Nov 2020 03:40:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605872426; cv=none; d=google.com; s=arc-20160816; b=etz3yFQILYmp2uC70U99tz4FwPD4LlUH9k2DtBR5/+REYr0YFTXcNLltYQz6s/RZAU bBOIfmkH3EVM1yeIGFsMwTcukWJnixcnZP7MJZsGJ232FiKqDExK3Vbecz73/PRl3oY9 qDGklnzKrXMEM3XzXLTBSSVpg0i3Tywv7KNlA0znbT/k1dQVXGX44z2aTqHA124kdomG gI7LS2TxJyGDq3FYt/Y0uQLJIlqRWfWrrLqB1wSnFusrYoLkBGsyl2kRc2qTbdU95irB 6Eg9Sfy5ScFo0qumxcu/d3Ndgrj66TeQKqXIFYsS+nDfA/HYHO3wRQtA0jRbvgpO54YZ Cb4Q== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4qtDoHx5XITmmf4aZVuZAZaC+QoEWXOfTvRZs9Krg+Y=; b=U3v2GlYScZbvTQiFt9imNogzOdkHCZ1N4sPcYrsEk8aAb98cvQN9+w6T5sEsOyOLas QPgV9Hctkc1obzNE4K79YvMwQiBwHb48y6teYSCZRSF48VnutGkVBCALxIWyxn6Z+6Ra OMv06PIJ3Dupi5sn3s1dyGY4gbbdg6VosA243poEB5gS022om3dqFBHnB8Ir7I12yutf Hv6F0R2JxXQ4i3rtty2BfrVrQ1ledryvXKujVZ7WmKW6xm8t/EQD4aH2zaQacqd291pB yVGr6UPa2T/8tSRgrC6cuMJBYxmGdjSz1bIMQltyNH1aVKGnKlyekM96pNpRtMpJg6Vg SYBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=08ji30Kz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r9si1619691edy.536.2020.11.20.03.40.04; Fri, 20 Nov 2020 03:40:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=08ji30Kz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728142AbgKTLIz (ORCPT + 99 others); Fri, 20 Nov 2020 06:08:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:55336 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727696AbgKTLHV (ORCPT ); Fri, 20 Nov 2020 06:07:21 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3B985206E3; Fri, 20 Nov 2020 11:07:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1605870440; bh=DJS4WfM+dre9YyUbSNAmJkskNFk84dzyRK0Eh8YC6hE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=08ji30Kz+sMNwQmZvZi0JybHnPcaa9zNMSiTGg59/3VYNamtTpJkWlhh3Eo7Zx+JR a3zeM3cAgLNk8SvuuxYsobUljD8W1w+PQ/fZtu1NIlobEVpYTtQkQEHdcB4n02A7Cu N7myMaqHUUgMg1oQskhlaCkd1DPErYNPheinBa6I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bodong Zhao , Dmitry Torokhov Subject: [PATCH 5.4 13/17] Input: sunkbd - avoid use-after-free in teardown paths Date: Fri, 20 Nov 2020 12:03:40 +0100 Message-Id: <20201120104541.708035430@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201120104541.058449969@linuxfoundation.org> References: <20201120104541.058449969@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dmitry Torokhov commit 77e70d351db7de07a46ac49b87a6c3c7a60fca7e upstream. We need to make sure we cancel the reinit work before we tear down the driver structures. Reported-by: Bodong Zhao Tested-by: Bodong Zhao Cc: stable@vger.kernel.org Signed-off-by: Dmitry Torokhov Signed-off-by: Greg Kroah-Hartman --- drivers/input/keyboard/sunkbd.c | 41 ++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) --- a/drivers/input/keyboard/sunkbd.c +++ b/drivers/input/keyboard/sunkbd.c @@ -99,7 +99,8 @@ static irqreturn_t sunkbd_interrupt(stru switch (data) { case SUNKBD_RET_RESET: - schedule_work(&sunkbd->tq); + if (sunkbd->enabled) + schedule_work(&sunkbd->tq); sunkbd->reset = -1; break; @@ -200,16 +201,12 @@ static int sunkbd_initialize(struct sunk } /* - * sunkbd_reinit() sets leds and beeps to a state the computer remembers they - * were in. + * sunkbd_set_leds_beeps() sets leds and beeps to a state the computer remembers + * they were in. */ -static void sunkbd_reinit(struct work_struct *work) +static void sunkbd_set_leds_beeps(struct sunkbd *sunkbd) { - struct sunkbd *sunkbd = container_of(work, struct sunkbd, tq); - - wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ); - serio_write(sunkbd->serio, SUNKBD_CMD_SETLED); serio_write(sunkbd->serio, (!!test_bit(LED_CAPSL, sunkbd->dev->led) << 3) | @@ -222,11 +219,39 @@ static void sunkbd_reinit(struct work_st SUNKBD_CMD_BELLOFF - !!test_bit(SND_BELL, sunkbd->dev->snd)); } + +/* + * sunkbd_reinit() wait for the keyboard reset to complete and restores state + * of leds and beeps. + */ + +static void sunkbd_reinit(struct work_struct *work) +{ + struct sunkbd *sunkbd = container_of(work, struct sunkbd, tq); + + /* + * It is OK that we check sunkbd->enabled without pausing serio, + * as we only want to catch true->false transition that will + * happen once and we will be woken up for it. + */ + wait_event_interruptible_timeout(sunkbd->wait, + sunkbd->reset >= 0 || !sunkbd->enabled, + HZ); + + if (sunkbd->reset >= 0 && sunkbd->enabled) + sunkbd_set_leds_beeps(sunkbd); +} + static void sunkbd_enable(struct sunkbd *sunkbd, bool enable) { serio_pause_rx(sunkbd->serio); sunkbd->enabled = enable; serio_continue_rx(sunkbd->serio); + + if (!enable) { + wake_up_interruptible(&sunkbd->wait); + cancel_work_sync(&sunkbd->tq); + } } /*