From nobody Sun Sep 27 03:13:28 2026 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E2892248BE for ; Sun, 6 Sep 2026 11:40:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788694825; cv=none; b=rMFKGnonKjvQgPARG6uvIVURXwUQi2U9NPRZgYGIj9mbOiRU4CUPYC5MkF/YAvJ+FhAD911IEl5PZs9uogMpFzzSWJm08U7JruTx41kyXR7BHHAacGv2CwfQOfg3vlL2AMuM4nJ0W58VonM81vnUEvPLlZoEAdoxrpm0j0DMFXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788694825; c=relaxed/simple; bh=30Yz9Elei6VI66bk5i4Y7ZNktd4r3jSz3yxXPEN1eDM=; h=Message-ID:MIME-Version:From:To:Cc:Date:Subject:Content-Type; b=GF5I3N/H6XZjoHRBl0Lpw8q0hyNC/vLrCDEQ4v6a0lwPupSMMLoDBmw8uQ3DcLQiiZsi8aHMzkKU6DhPfQga4iWjrPuZ0ooSjL3h4j3elDvSbucczkZeOuwhOQQsFVAoyDrsXpS4sEW/J9rJU3Ikm6qM7mXUtsNFsmu9vsyKWl8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=pCFUyyPH; arc=none smtp.client-ip=209.85.215.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pCFUyyPH" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-cc439aa0078so2086711a12.0 for ; Sun, 06 Sep 2026 04:40:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788694809; x=1789299609; darn=vger.kernel.org; h=content-transfer-encoding:content-type:subject:date:cc:to:from :mime-version:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=zq5M3OTpr2g6eiAg252Kka23Tri2IujEhHRTMO6gelw=; b=pCFUyyPHQuZXN3nTdBKWgoNeZFE6TbnSxY2t6xxT2euZ17F5yP+5GZG4DsU0Kio7bV 1lk8Fq+SHhaButOWAybMbq8m8te9gkFHIFDwd3vPbJtbnlomdBU9mFGAbBaTSJa7ZTvf TnU5qnvSedR6dv7O37omvCVLUxdL4BUkdxX1vFRB8zPhITnG8B0xe45b5NjvhmmHP+Cc Kx4XVxKo6265bevfpLWzdA/zhdyZkUveWp7y8FOsXGoMF78Pig/uyWWiLudvQSOFD/7v BZYI+37/Myd+LPQLT/MZOt2EgSC7E0yrWNbMXka32W4ibIhGXuA06v3uip7J0w70By2Y xXMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788694809; x=1789299609; h=content-transfer-encoding:content-type:subject:date:cc:to:from :mime-version:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=zq5M3OTpr2g6eiAg252Kka23Tri2IujEhHRTMO6gelw=; b=RLgvqkCg/kYSuFaDic98azux8LuYddWztynQ77vOhWieEoVJMIBcRu3Ez0nBGEMM6M yBJx/dTUXASRT9vo2j95XIf6J4JhVdmJ7KHh5GLp562ncyMRWgOU3qJFRWWYFNE2VkMf 6L+0PpC8J5eizHoo/e465nAKRchI/ZxAizFvPdE8J4ZZHAB/DH2Iqu2tkFJSutEYI+Xf Ix3ddXw78OgZw/Gi2UgXRDmKGaWfcR8o3A0cmV+Uxab3Ok192OTOLm0HV6Btie//AYih Y//5f9k7ytLmWjNfZMHQMbTCKBz+qMVv9AJYtmtyV2G6BeTknA/yAJMFM2dq625G2T3G HqNw== X-Forwarded-Encrypted: i=1; AKwUvBysFFI41l1f6InHsvogMO3pM1SQ413FqUEIb3dvbJQwlRYEyQFm4pZmquBAxdXPDWQ8A2vqhRmtIkuNaTM=@vger.kernel.org X-Gm-Message-State: AFuF++kVVHBt3fqszkbEgd1E9wKWL8Y97HqKdFvhnp1oULKKupEtLJ/X wEv3jq+30VcZPVXKi4CPj0N4zRaUwksgv/u452OyL523QVv0zW/WrXUX X-Gm-Gg: AYBFou1wz7lAVzrtLAI0A0AVtKYI2abM0469iueuQezorvvO3/NPIdvxclTqDBH3hdI mINBRfXgkKxyWERsVWm5O/zpnK9PKkl7nEdWAsuyMY9T3n5XfAf1CatGVFW8WS+ketYVALHmtak aUPgNRQ0+hDcRgcuqMw0Vh7u1+gXYm70RKO7kYk9JhFOHBXZcYheYJLX+etynGhm/WkYoi3MtbH YU7S27V17N2T2mRSUJIfzsyRgr0P821uSoGpuGj++29c4hXnYpVILRnMdQwV7rIwJEZgFELI1CO pd0UWIGTB98UuMm61wxlxk6L4AwBb8HDQ2wmYV6Acy2gBHAWJonaKgx+9ewC8uPS61X9sNhiOtt R3pzLsP9RaWqWC3d8Mn+E74YjaKyR9J5uy2DJY2zumiVtZklJ4bSdvpa9mLxW+M+msX5j1AP8l2 s7fNkf24Q+72Nkq2rKfpybsyVMydJmMHS2hIe3UBhPo123bJ0PY/+cbw8j2O0moBoOIJC9larFK 6/31Gfp3US055tcx/F8gbG4ZITKSZxJOgjI53oX+oVUiEOuEG8d6DViZg== X-Received: by 2002:a17:90b:5807:b0:398:9be9:ab8c with SMTP id 98e67ed59e1d1-39b26229bd7mr27188589a91.17.1788694809187; Sun, 06 Sep 2026 04:40:09 -0700 (PDT) Received: from manush ([2406:7400:94:7a79:f149:be84:942d:9300]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3339885d29esm22094892eec.2.2026.09.06.04.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2026 04:40:08 -0700 (PDT) Message-ID: <6a9d5118.a865c849.172a3c.4f1f@mx.google.com> X-Mailer: git-send-email 2.46.2.windows.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: "Manush Prajwal" To: ukleinek@kernel.org Cc: lee@kernel.org, linux-pwm@vger.kernel.org, mfd@lists.linux.dev, linux-kernel@vger.kernel.org Date: 6 Sep 2026 17:10:07 +0530 Subject: [PATCH v2] pwm: lp3943: validate DT output indices in lp3943_pwm_parse_dt() Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ti,pwm0/ti,pwm1 devicetree properties are raw u32 arrays naming which of the 16 physical LP3943 outputs (0-15, per the documented binding and the 16-entry enum lp3943_pwm_output) each PWM channel drives. lp3943_pwm_parse_dt() reads these values straight from the devicetree with of_property_read_u32_array() and stores them in pwm_map->output[] without checking they are actually in range. Both consumers of pwm_map->output[] then use an out-of-range value directly as an index/bit-offset with no bounds check of their own: - lp3943_pwm_request_map() does test_and_set_bit(offset, &lp3943->pin_used) with offset taken straight from output[i], corrupting memory beyond the single 'unsigned long pin_used' field for offset >=3D BITS_PER_LONG. - lp3943_pwm_set_mode() does mux[index].reg / .mask / .shift with index taken straight from output[i], reading past the 16-entry lp3943_mux_cfg[] array and then issuing a regmap write built from whatever garbage register/mask/shift values were read out of bounds. A devicetree (malformed board file or a loaded overlay) with an out-of-range ti,pwm0/ti,pwm1 value is enough to reach either path; nothing upstream of these two functions validates the value. Add a range check in lp3943_pwm_parse_dt() right after the values are read, rejecting the devicetree with -EINVAL before any out-of-range value can reach pwm_map->output[]. Check explicitly for negative values in addition to the upper bound: enum lp3943_pwm_output's underlying integer representation is implementation-defined per the C standard, so do not rely on the compiler having chosen an unsigned type for it even though it always does in practice for GCC (verified empirically, with and without -fshort-enums, since none of the enumerators are negative). Add the LP3943_NUM_OUTPUTS(16) constant next to the existing enum it bounds, alongside the existing LP3943_NUM_PWMS(2) constant. Signed-off-by: Manush Prajwal --- Changes since v1: - Sashiko AI review flagged that the bounds check could theoretically be bypassed by a devicetree value with the MSB set, if the compiler chose a signed underlying type for enum lp3943_pwm_output. I checked this empirically with gcc 13.2.0, both with and without -fshort-enums: since none of the enum's values are negative, gcc always picks an unsigned underlying type, so output[j] >=3D 16 already rejects such values today. Added the explicit "< 0" check anyway, since the C standard leaves the enum's underlying type implementation-defined and the check costs nothing. - The same review also flagged a missing NULL check on pdata->pwms[hwpwm] in lp3943_pwm_request_map() as a pre-existing, separate bug (not introduced by this patch, confirmed by inspection) - sending that as its own follow-up patch rather than folding an unrelated fix in here. drivers/pwm/pwm-lp3943.c | 7 ++++++- include/linux/mfd/lp3943.h | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-lp3943.c b/drivers/pwm/pwm-lp3943.c index 10537e74b..e8bb8dbb9 100644 --- a/drivers/pwm/pwm-lp3943.c +++ b/drivers/pwm/pwm-lp3943.c @@ -218,7 +218,7 @@ static int lp3943_pwm_parse_dt(struct device *dev, struct lp3943_platform_data *pdata; struct lp3943_pwm_map *pwm_map; enum lp3943_pwm_output *output; - int i, err, num_outputs, count =3D 0; + int i, j, err, num_outputs, count =3D 0; =20 if (!node) return -EINVAL; @@ -247,6 +247,11 @@ static int lp3943_pwm_parse_dt(struct device *dev, if (err) return err; =20 + for (j =3D 0; j < num_outputs; j++) { + if (output[j] < 0 || output[j] >=3D LP3943_NUM_OUTPUTS) + return -EINVAL; + } + pwm_map =3D devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL); if (!pwm_map) return -ENOMEM; diff --git a/include/linux/mfd/lp3943.h b/include/linux/mfd/lp3943.h index 5d2d172d3..844938ca7 100644 --- a/include/linux/mfd/lp3943.h +++ b/include/linux/mfd/lp3943.h @@ -53,6 +53,8 @@ enum lp3943_pwm_output { LP3943_PWM_OUT15, }; =20 +#define LP3943_NUM_OUTPUTS 16 + /* * struct lp3943_pwm_map * @output: Output pins which are mapped to each PWM channel --=20 2.46.2.windows.1