Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1193757pxb; Fri, 20 Nov 2020 03:41:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJxFCIBQW29ACBHYpdqZxjsgiKGPtu0id4D/fWXQL/0cWqZTEWHWel8bE0V1eRFqTWGMou41 X-Received: by 2002:a17:906:2454:: with SMTP id a20mr14171121ejb.208.1605872504639; Fri, 20 Nov 2020 03:41:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605872504; cv=none; d=google.com; s=arc-20160816; b=qh4AMfIrFUzmFNN8VLHHbdhN6jkeLNpq4a/f01snxV4D9UMSPmDSLPPjRxck69/DZa HupOX66wbYuUBJd13CmbDp5wHK4fJOu8zQ9slskC5eOKaH6+gq7CJpELXOxdc8hz4RCk NCP8lHYt0LY7NXhufm3cZ5AdMANvXy/xoozaxDgCtVR7HkvphJxSIq2PTuW+FtxIx9S3 xrZD2tsFrdziXDl/dgBlwTIVNqRK/HczYRLZPiiqgC0g2yOCRqg/oqxdNZUP53qpUIXW jxfhVSpU+MRtsnpHrXE/LeFz89HdOy37GqyLoWPmbSAdUaQJKZpTiO/j9n9JpxIxMXd/ xdCQ== 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=WvUhqTjPabPKV5p8+gUzZBR4+RYOFCI8kzaJ3vZ54BTAcypJqPFlDl2Wdwsj9YvZos WJBHMvB+RZITBvQP5SRqQcM+tPt/fNMNBTBlmuu6GZB9WK8xdyJdu2qAwm/N3GyMdx4e k5z6RakrLwcEgZNCR8G0O4XhdOcpzoWM81kpNuUk7bzo+qme0XOkCG/zOAIKXedtloRe tSNJcTUkHvZUCb3mCOKKInvFIr2hHSlgZePtf3XcLSmOIzVxT6pMCYFqYmr8+YQ7DKEx AcG0AKkIF+3v4m5k/O7U1ovWgUjWohUF2hKV3W25ckL9/dUPQsqEhICQeO2kjZcL8bUS djug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jvfYdvYw; 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 wn12si1410714ejb.43.2020.11.20.03.41.21; Fri, 20 Nov 2020 03:41:44 -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=jvfYdvYw; 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 S1728621AbgKTLI0 (ORCPT + 99 others); Fri, 20 Nov 2020 06:08:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:55794 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727160AbgKTLH6 (ORCPT ); Fri, 20 Nov 2020 06:07:58 -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 BE1E62222F; Fri, 20 Nov 2020 11:07:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1605870476; bh=DJS4WfM+dre9YyUbSNAmJkskNFk84dzyRK0Eh8YC6hE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jvfYdvYw8awQRdGGnDyo+DONFY/dlfkCiVFcF+IbuwBu2dpKE7L2rIdAPRk3A9cRR YRSndFq6hghLIDzwaM58+Px3ucM9gnvkK8A6M040KzJX3t+VpJ3f0yZxVAxkYsAic0 9KjUC0FDSp0rF6yDU3Pab6gyPOKZWrGIKxu94KO4= 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.9 07/14] Input: sunkbd - avoid use-after-free in teardown paths Date: Fri, 20 Nov 2020 12:03:45 +0100 Message-Id: <20201120104541.532175373@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201120104541.168007611@linuxfoundation.org> References: <20201120104541.168007611@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); + } } /*