Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1177000pxb; Fri, 20 Nov 2020 03:13:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJyPS2ipsVfDneCD64biBzjfjI07fY1aqyv1mt3tqQVAeGObeqv2RdaMBk5U7zZyerbzfdIL X-Received: by 2002:aa7:d48d:: with SMTP id b13mr34002683edr.264.1605870806584; Fri, 20 Nov 2020 03:13:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605870806; cv=none; d=google.com; s=arc-20160816; b=iP2k3pUw2EH7Ogx4wqzDm/3S44gx+b4EoTLmJwmRkDFnhCxkAnMZrc/llTBQiAd6CR eT92ZnbwkNExf8j1Tp3MV/0gvZWiPmiT/xkfSoYUjMJ3+inP5qGHHDdFbiOSfyDsDecu 85rnTDoTVot5fVY9U2P1+4roqdBHVvYQ3Ov+D3oKKGyceobBHS7Pm3U+BDCdLEwvz8WD Yqqo9cwKCLKZb+qhRT1wlXDzv7fH0BogwK90aY5OV/gptilI1Cyvvw3ldJgInSV1GCH6 21MbDCqqalwsgpp6KpPvhmUK1zztUpeLrszlIuqF6mdA9nNp4o6aeFx/mrZ0y9GsU3e0 bcag== 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=cb1W+ocCeVfPS1gJr1klh+Fx2HKM903l0VrLmyhtEDM=; b=vnFOMKUyXKUTXVciyP8/USP77R9jlkWaCGDqV46tfLfaGaIvVouLPDMT15if0kI7xs /I+KOog1/PN8ARWYYySXy+JNLz21Wjlr1tdtc42XF5plU3rAg5dWz+rwh2OHt+DtLsON ADbw37wxBYelWcwDkRuK8SKVqIR8MvlZ5onfpQfnqtZ/D03A6mbsyVEeGcKL/PRBO+wa Np4DJwZKdvMFNef+dlZLx6SSUB5YFH5mDSeSrw3C0xUVBwCTmNcLtisG6UcZnGdTyIDE LXumgcddYWEeL0XJ4PonCfqgOl8R8aXerwhHUjFIEwzZfl96lsvdzqh9eDPeJ3hHHhLf l4Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0ZElrxoa; 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 c19si1537589edw.66.2020.11.20.03.13.04; Fri, 20 Nov 2020 03:13: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=0ZElrxoa; 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 S1728653AbgKTLKJ (ORCPT + 99 others); Fri, 20 Nov 2020 06:10:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:54056 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728365AbgKTLGS (ORCPT ); Fri, 20 Nov 2020 06:06:18 -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 3B5F12236F; Fri, 20 Nov 2020 11:06:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1605870376; bh=5qeS4CZMo09H/Af9Z0VwPzriEpL2Vul070MAciqK2C8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0ZElrxoa5p6e7nIulyrpmRYsVJ6mGXXGWmlksno95B4juqcxioyVgF615MxuvWUjo g/J3SAVC3VEPudeRUj5qWBhYL77m7TdfM5mtVpHSmxmvTR2KeusRDs20io8HFyHfqO DyiDFyvIw+dkhowFTeFfJS2penRP1MMo7c8mQYmc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bodong Zhao , Dmitry Torokhov Subject: [PATCH 4.19 10/14] Input: sunkbd - avoid use-after-free in teardown paths Date: Fri, 20 Nov 2020 12:03:31 +0100 Message-Id: <20201120104540.318439982@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201120104539.806156260@linuxfoundation.org> References: <20201120104539.806156260@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 @@ -111,7 +111,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; @@ -212,16 +213,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) | @@ -234,11 +231,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); + } } /*