public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: "Guilherme G. Piccoli" <gpiccoli@igalia.com>,
	Sam Lantinga <slouken@libsdl.org>, Jiri Kosina <jkosina@suse.com>,
	Sasha Levin <sashal@kernel.org>,
	djogorchock@gmail.com, jikos@kernel.org,
	benjamin.tissoires@redhat.com, linux-input@vger.kernel.org
Subject: [PATCH AUTOSEL 6.1 17/24] HID: nintendo: Prevent divide-by-zero on code
Date: Mon, 25 Dec 2023 19:22:10 -0500	[thread overview]
Message-ID: <20231226002255.5730-17-sashal@kernel.org> (raw)
In-Reply-To: <20231226002255.5730-1-sashal@kernel.org>

From: "Guilherme G. Piccoli" <gpiccoli@igalia.com>

[ Upstream commit 6eb04ca8c52e3f8c8ea7102ade81d642eee87f4a ]

It was reported [0] that adding a generic joycon to the system caused
a kernel crash on Steam Deck, with the below panic spew:

divide error: 0000 [#1] PREEMPT SMP NOPTI
[...]
Hardware name: Valve Jupiter/Jupiter, BIOS F7A0119 10/24/2023
RIP: 0010:nintendo_hid_event+0x340/0xcc1 [hid_nintendo]
[...]
Call Trace:
 [...]
 ? exc_divide_error+0x38/0x50
 ? nintendo_hid_event+0x340/0xcc1 [hid_nintendo]
 ? asm_exc_divide_error+0x1a/0x20
 ? nintendo_hid_event+0x307/0xcc1 [hid_nintendo]
 hid_input_report+0x143/0x160
 hidp_session_run+0x1ce/0x700 [hidp]

Since it's a divide-by-0 error, by tracking the code for potential
denominator issues, we've spotted 2 places in which this could happen;
so let's guard against the possibility and log in the kernel if the
condition happens. This is specially useful since some data that
fills some denominators are read from the joycon HW in some cases,
increasing the potential for flaws.

[0] https://github.com/ValveSoftware/SteamOS/issues/1070

Signed-off-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
Tested-by: Sam Lantinga <slouken@libsdl.org>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/hid/hid-nintendo.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
index 907c9b574e3b3..df07e3ae0ffb4 100644
--- a/drivers/hid/hid-nintendo.c
+++ b/drivers/hid/hid-nintendo.c
@@ -859,14 +859,27 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
  */
 static void joycon_calc_imu_cal_divisors(struct joycon_ctlr *ctlr)
 {
-	int i;
+	int i, divz = 0;
 
 	for (i = 0; i < 3; i++) {
 		ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] -
 						ctlr->accel_cal.offset[i];
 		ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] -
 						ctlr->gyro_cal.offset[i];
+
+		if (ctlr->imu_cal_accel_divisor[i] == 0) {
+			ctlr->imu_cal_accel_divisor[i] = 1;
+			divz++;
+		}
+
+		if (ctlr->imu_cal_gyro_divisor[i] == 0) {
+			ctlr->imu_cal_gyro_divisor[i] = 1;
+			divz++;
+		}
 	}
+
+	if (divz)
+		hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz);
 }
 
 static const s16 DFLT_ACCEL_OFFSET /*= 0*/;
@@ -1095,16 +1108,16 @@ static void joycon_parse_imu_report(struct joycon_ctlr *ctlr,
 		    JC_IMU_SAMPLES_PER_DELTA_AVG) {
 			ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum /
 						 ctlr->imu_delta_samples_count;
-			/* don't ever want divide by zero shenanigans */
-			if (ctlr->imu_avg_delta_ms == 0) {
-				ctlr->imu_avg_delta_ms = 1;
-				hid_warn(ctlr->hdev,
-					 "calculated avg imu delta of 0\n");
-			}
 			ctlr->imu_delta_samples_count = 0;
 			ctlr->imu_delta_samples_sum = 0;
 		}
 
+		/* don't ever want divide by zero shenanigans */
+		if (ctlr->imu_avg_delta_ms == 0) {
+			ctlr->imu_avg_delta_ms = 1;
+			hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n");
+		}
+
 		/* useful for debugging IMU sample rate */
 		hid_dbg(ctlr->hdev,
 			"imu_report: ms=%u last_ms=%u delta=%u avg_delta=%u\n",
-- 
2.43.0


  parent reply	other threads:[~2023-12-26  0:23 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-26  0:21 [PATCH AUTOSEL 6.1 01/24] Input: psmouse - enable Synaptics InterTouch for ThinkPad L14 G1 Sasha Levin
2023-12-26  0:21 ` [PATCH AUTOSEL 6.1 02/24] reset: hisilicon: hi6220: fix Wvoid-pointer-to-enum-cast warning Sasha Levin
2023-12-26  0:21 ` [PATCH AUTOSEL 6.1 03/24] Input: atkbd - skip ATKBD_CMD_GETID in translated mode Sasha Levin
2023-12-26  0:21 ` [PATCH AUTOSEL 6.1 04/24] Input: i8042 - add nomux quirk for Acer P459-G2-M Sasha Levin
2023-12-26  0:21 ` [PATCH AUTOSEL 6.1 05/24] Input: amimouse - convert to platform remove callback returning void Sasha Levin
2023-12-26  0:21 ` [PATCH AUTOSEL 6.1 06/24] s390/scm: fix virtual vs physical address confusion Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 07/24] ARC: fix spare error Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 08/24] wifi: iwlwifi: pcie: avoid a NULL pointer dereference Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 09/24] Input: xpad - add Razer Wolverine V2 support Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 10/24] kselftest: alsa: fixed a print formatting warning Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 11/24] HID: nintendo: fix initializer element is not constant error Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 12/24] platform/x86: thinkpad_acpi: fix for incorrect fan reporting on some ThinkPad systems Sasha Levin
2024-01-09 11:46   ` Pavel Machek
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 13/24] ASoC: Intel: bytcr_rt5640: Add quirk for the Medion Lifetab S10346 Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 14/24] ASoC: Intel: bytcr_rt5640: Add new swapped-speakers quirk Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 15/24] ALSA: hda/realtek: Add quirks for ASUS Zenbook 2022 Models Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 16/24] dm audit: fix Kconfig so DM_AUDIT depends on BLK_DEV_DM Sasha Levin
2023-12-26  0:22 ` Sasha Levin [this message]
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 18/24] smb: client: fix potential OOB in smb2_dump_detail() Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 19/24] i2c: rk3x: fix potential spinlock recursion on poll Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 20/24] drm/amd/display: get dprefclk ss info from integration info table Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 21/24] pinctrl: cy8c95x0: Fix typo Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 22/24] pinctrl: cy8c95x0: Fix regression Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 23/24] pinctrl: cy8c95x0: Fix get_pincfg Sasha Levin
2023-12-26  0:22 ` [PATCH AUTOSEL 6.1 24/24] ida: Fix crash in ida_free when the bitmap is empty Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231226002255.5730-17-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=benjamin.tissoires@redhat.com \
    --cc=djogorchock@gmail.com \
    --cc=gpiccoli@igalia.com \
    --cc=jikos@kernel.org \
    --cc=jkosina@suse.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=slouken@libsdl.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox