Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1077503ybl; Wed, 14 Aug 2019 10:19:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqy/QyUFAdwic9wDo1Ifb62fbLEHhQUW8ZneV+51P87+ni9Ur/qccJE6ne1RQ6sU+fxMYvrM X-Received: by 2002:a17:90a:26ac:: with SMTP id m41mr752070pje.59.1565803178488; Wed, 14 Aug 2019 10:19:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565803178; cv=none; d=google.com; s=arc-20160816; b=C3WeaJdGglhNaK/aAo2gBErN2/mj11zocAYDR3jbChyR32DFDEnsYeGvtntR785Fvv ZxBWXe6NjXPFapzhehoMZBFH5IhVxohBehXIl8Ed2uooNzvoVRsoR/5lT03WSXp3geK9 Y9ut0r6bjZyCp8+BS7oW9UszOS/Xv6pgdWPcxjtfu0i+LGoW78yAs8TYE9H8Aju/9x1q YFm6rmaqn9636uDY9X1SDapkFDJ91EJGVbfqNCUw1hCpCJPqB+uajfxVzv4jGElN9DNU l5sxjKAOT7K2mB1VHkOfqMlhyqoiC76qXUipcDU03tCmniD2jrfWTEN30toRbP9ju3UY VCmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SX+OqPy3BUVBf/erSvHnwXywIKJd0okej8YRG9oUYMo=; b=X7t0ezivZmXbMqrjI1uANGLCev2LYUq2TuzxOjtj0dYfTnK4WHrjI+fEFkZpwtI4LT Sa8c6tRofKyGllcHIQrVZ1saXSbyt1U3OxhDPlR5TPn+KQZ9w97+P/yMmIjgxigF7dYy zTtaScyia1xP6kEPwcIJzNqrxfPuYJvQnaOiOwQLbeJ43sqLZOfuqPHGMoOD5Q5t3hrB x2ikgTj/791qjkEgTPTsk2Hby54BPb5CepAFu/Y5CgFyjbfEFBX2bkCLDVo2YIakJ8Es NxgZEw26zv3t1AiDlx02+g/W3SZ6kJ/aQhvYk1hux7x4sH85Ej/jqF2v8qFGRGnVaOXF YGig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="qzC/FULY"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y11si197310pjp.54.2019.08.14.10.19.22; Wed, 14 Aug 2019 10:19:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="qzC/FULY"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730088AbfHNRSv (ORCPT + 99 others); Wed, 14 Aug 2019 13:18:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:35972 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730690AbfHNRMO (ORCPT ); Wed, 14 Aug 2019 13:12:14 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 F0B7A2133F; Wed, 14 Aug 2019 17:12:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565802733; bh=L+qeEKzA9GLPmmim2nacHjtP7EpGVaAj+yDAOQ+yr4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qzC/FULYhK5s6sjcMHT4A01kILnHzXOeTIDyDVRnby0puYp8dgO9tWYzHBvbz/VZ9 YiwruT9CcGIjgWTdKsodH/0CdWsXpne1rkQS50dOpkxmzUzMlTmTg92eQ3cvvz2taE ckcBlVVOcHx7lIl7UqJRzFAWb990ndL9bbHpRRiw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Roderick Colenbrander , Jiri Kosina Subject: [PATCH 4.19 70/91] HID: sony: Fix race condition between rumble and device remove. Date: Wed, 14 Aug 2019 19:01:33 +0200 Message-Id: <20190814165752.654373877@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190814165748.991235624@linuxfoundation.org> References: <20190814165748.991235624@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Roderick Colenbrander commit e0f6974a54d3f7f1b5fdf5a593bd43ce9206ec04 upstream. Valve reported a kernel crash on Ubuntu 18.04 when disconnecting a DS4 gamepad while rumble is enabled. This issue is reproducible with a frequency of 1 in 3 times in the game Borderlands 2 when using an automatic weapon, which triggers many rumble operations. We found the issue to be a race condition between sony_remove and the final device destruction by the HID / input system. The problem was that sony_remove didn't clean some of its work_item state in "struct sony_sc". After sony_remove work, the corresponding evdev node was around for sufficient time for applications to still queue rumble work after "sony_remove". On pre-4.19 kernels the race condition caused a kernel crash due to a NULL-pointer dereference as "sc->output_report_dmabuf" got freed during sony_remove. On newer kernels this crash doesn't happen due the buffer now being allocated using devm_kzalloc. However we can still queue work, while the driver is an undefined state. This patch fixes the described problem, by guarding the work_item "state_worker" with an initialized variable, which we are setting back to 0 on cleanup. Signed-off-by: Roderick Colenbrander CC: stable@vger.kernel.org Signed-off-by: Jiri Kosina Signed-off-by: Greg Kroah-Hartman --- drivers/hid/hid-sony.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -587,10 +587,14 @@ static void sony_set_leds(struct sony_sc static inline void sony_schedule_work(struct sony_sc *sc, enum sony_worker which) { + unsigned long flags; + switch (which) { case SONY_WORKER_STATE: - if (!sc->defer_initialization) + spin_lock_irqsave(&sc->lock, flags); + if (!sc->defer_initialization && sc->state_worker_initialized) schedule_work(&sc->state_worker); + spin_unlock_irqrestore(&sc->lock, flags); break; case SONY_WORKER_HOTPLUG: if (sc->hotplug_worker_initialized) @@ -2553,13 +2557,18 @@ static inline void sony_init_output_repo static inline void sony_cancel_work_sync(struct sony_sc *sc) { + unsigned long flags; + if (sc->hotplug_worker_initialized) cancel_work_sync(&sc->hotplug_worker); - if (sc->state_worker_initialized) + if (sc->state_worker_initialized) { + spin_lock_irqsave(&sc->lock, flags); + sc->state_worker_initialized = 0; + spin_unlock_irqrestore(&sc->lock, flags); cancel_work_sync(&sc->state_worker); + } } - static int sony_input_configured(struct hid_device *hdev, struct hid_input *hidinput) {