From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796242882; b=A6QnZiaCZdqNoyNpywlhNRnanG95fUtJjR5/y4PIJh7s9s/pVvde6BuZuZQ5Z7ltmlzr Uu4Sj5xgpEj9G+gYsBkQEbzrV+6Q4fGhG8h79cBTBTTW03+yrkfAKhG9emdGUm07unK+k kJQL95Z4YRJppOc14hxwVzHtDP8WxK7BMHdmew38EOLEYFssoKQJnldP+G91GcooE56vn zFbiV2kGHjpZga2pc2iKi+cXUqkl9TNNJ6qRTd68Yw5COIabg+dXpW+llsAh4eF//siKB 1BmjQIOl5qO2kYrw7tmYoQEz+cxo/4QwYfi6oYN1tQDYjy0yT5EJR0qcxso2vfoMn7A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796242882; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=oW3arUE31FmDvbzo2HMCCavgd5Emp92lYR4HS0k+U58=; b=1FGEoEVZB4MScCyy4tUuKidlgYkiyAxILYXDr5XzDJX2TQOxs7mGxZYSyEXMKuufu06e +LSVmCe/d8/NK6kQjEDojT4f1LJxdY1bEN2Pl3z6j99rzLaAEbmdF9qKbKFfXqMOeNLpG in9yo1af0Mgqq8nbhxGAe7CXosf59uLrDf8+zKwWD8hn9+kY39xIbmHaw+9iAo4DuOc/3 YhHI4JRGD0L0jOEvQD/0tiexqKuYYLkT/jsGVx+c2WR81ONq2b/PDhnN+BycIwqt8qG8p 2ixE63sNyxQHsYKdLFxlh0x+MNkN2dwp0Dh/U8AhFBD4n9wv1581sJIpQB+utJz3I0w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RLa50VWR; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46579606A33F; Wed, 2 Dec 2026 20:21:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RLa50VWR Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABA471F00927; Wed, 2 Dec 2026 20:21:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796242882; bh=oW3arUE31FmDvbzo2HMCCavgd5Emp92lYR4HS0k+U58=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RLa50VWRTD2/7WRkShWmea6SF2I1hL9fPE4Cl/gelYTF34+l1fqCVSat851fo5RdU mXFXM3hZJs1f7iaM7YosqwkZTeiaAFFXaXdq6OxWJZ2YbiApnsmNKb+Hpu9SSSvSEf 4sR5vIagKd7nChOpbookFFTnBSqN/qIWoEhJ/+tvEE99b9xjXSIg+eiSmYRg0tjVYF IiiBCrz6ZuHRNpx3/g5AoBAt8PnWe1Ik4vLRy99TvR8vgjHI6wxg5yNwHtXXdRlxUv MrK2dzQTxdrm7CKWNJ3AIWEGxtrYMH7SWmB+oP1PVK7EXuKSCPEfKvNM34Enmoa9b4 EiL9u2kWWMdgA== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org, =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , David Lechner , Jonathan Cameron , linux-iio@vger.kernel.org, Mathieu Othacehe Subject: [PATCH v3 3/3] iio: proximity: isl29501: Fix division by zero and overflow in isl29501_set_freq() Date: Wed, 2 Dec 2026 20:21:12 +0000 Message-ID: X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit isl29501_set_freq() computes freq = val * 1000000 + val2 % 1000000 in a signed 32-bit int and passes it directly to do_div() without checking for zero or negative inputs. Writing 0 to the sampling_frequency attribute triggers a division by zero in the kernel, while valid frequencies above 2147 Hz up to the sensor's 2222 Hz limit overflow signed int. Validate val and val2 up front so that the requested frequency lies within (0, 2222] Hz with val2 in [0, MICROHZ_PER_HZ), compute freq as an unsigned int using MICROHZ_PER_HZ, and return -ERANGE otherwise. Fixes: 1c28799257bc ("iio: light: isl29501: Add support for the ISL29501 ToF sensor.") Closes: https://sashiko.dev/#/bug/linux-e4bc1c30-bbf1-458a-99f9-a36e949af67c Cc: stable@vger.kernel.org Signed-off-by: Lee Jones --- v2 => v3: - Fold the zero and upper-bound frequency checks into the initial validation guard, rejecting val2 >= MICROHZ_PER_HZ, zero frequency, and values above 2222 Hz (val == 2222 && val2 > 0) before computing freq without modulo (Andy) - Use MICROHZ_PER_HZ rather than HZ_PER_MHZ for unit accuracy v1 => v2: - Order local variables in reverse Christmas tree style (Andy) - Group val range checks together (Andy) - Use units.h multiplier macros (Andy) Cc: "Nuno Sá" Cc: Andy Shevchenko Cc: David Lechner Cc: Jonathan Cameron Cc: linux-iio@vger.kernel.org Cc: Mathieu Othacehe drivers/iio/proximity/isl29501.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/iio/proximity/isl29501.c b/drivers/iio/proximity/isl29501.c index a98a9975323c..a61d70db28c4 100644 --- a/drivers/iio/proximity/isl29501.c +++ b/drivers/iio/proximity/isl29501.c @@ -18,6 +18,7 @@ #include #include #include +#include /* Control, setting and status registers */ #define ISL29501_DEVICE_ID 0x00 @@ -863,12 +864,16 @@ static int isl29501_set_calibbias(struct isl29501_private *isl29501, static int isl29501_set_freq(struct isl29501_private *isl29501, int val, int val2) { - int freq; unsigned long long sample_time; + unsigned int freq; + + if (val < 0 || val > 2222 || val2 < 0 || val2 >= MICROHZ_PER_HZ || + (val == 0 && val2 == 0) || (val == 2222 && val2 > 0)) + return -ERANGE; /* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */ - freq = val * 1000000 + val2 % 1000000; - sample_time = 2222ULL * 1000000ULL; + freq = val * MICROHZ_PER_HZ + val2; + sample_time = 2222ULL * MICROHZ_PER_HZ; do_div(sample_time, freq); sample_time -= 1; -- 2.56.1.911.g883d8bcb20-goog