From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 8D09E3BCD1E for ; Fri, 10 Jul 2026 07:01:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783666925; cv=none; b=SOxX0JRivc+xxZ8KDp28IQT5bdpgM5s0e/d5osTQm/qm9ETm1Ea2wb5UkU1VGp0+RoLkE/Qld1P6JbRnY9/BKdJQOioglIFUNlAsZ8w0CpSdtobCgeJKgMWGHY8J5jLmaP7k3K8D2XSUL0qu4G7X2XSCUDSX45Ip1w5xZh+DAMc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783666925; c=relaxed/simple; bh=Sa+ZMM5dTNjCDBOF2MATzUebRnFH0dCZpZVATJCDrms=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YcxYeyokr5e6w8XFDMq6EhwDAk8jffH8pVOsXGN8Tc75gdTdYJj0HZUUWiiSckcMuA4cq45QQgTjTInSHw+ylT+//St9DNV6UMCHE5jiz/gwaK1SYw6FAdZm1dhNRHR2A4mTZqCWGKOgM3S8KeXEAOlHSN1YsEJs8MLrK79dYyc= 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=c52/M0At; arc=none smtp.client-ip=209.85.128.49 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="c52/M0At" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-493bf73ec2aso3176455e9.2 for ; Fri, 10 Jul 2026 00:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783666914; x=1784271714; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=KiDkE10v4AFJNJVFDsx15gyYeOOnHnd5nJ5DdCHOga8=; b=c52/M0AtYHCt4A4mpXCzRxUMWSrQTiBMZvwthstSEIB0CgkZWvQzVdqLLEpMxjYeFB RdVou8ykYChLgBI8lBeP47hPme5JjwijCumbldghHwpJ8imGxCLiNaXC1o8EJGmBXSMy C4JTIpGQM7USho9FMz0LRec3Dj8WvibME8IvmPBJVCUFwInb39oH1U+BtqgZ1VFs6ifn 0NgLQKcnpMwDCjJzUzwDs6FRfgwDlRLayIkUt6VSgGNz0U4DGq8GJm6E+ah03IdmstWP cl1oyT+9s0vWAKBnHjcCzVGLKUjRp37e96jLNv1Vc7Ap2sm9ph+DoB2kMAtu7UKzjYWu MHxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783666914; x=1784271714; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KiDkE10v4AFJNJVFDsx15gyYeOOnHnd5nJ5DdCHOga8=; b=Y1jcFSOsxSMJ74XyFzmsXCzD5E/qUX3zCK3+r7DU+Ypg6tNmjgqsStv7jGVz12zNtz lJGdEqPrAGd8EbysoGT7PgDVPYz0EkzrqxRgreBVVzkQ62B6H/808vAGcCR0e+BHolMH EoGzYmr4BPhoOPJ/5oCt8g4GSoByTsNSPflRDrTBnCLCaPoZ7QrApYeHn6RHGh71o3C+ be8Y22BYxMrq2fOHFiN+c7AjwjTK/GfHPw/ky7H5f2NdeuTukq93PQi4D6PQlG/p6Y2j A4jzzxNg2sLK9DXxnRPgEXwhRg7cJEhj9SeuH+BbwECGBVaExZMdLaypaQ6yt0+rEiFq +6Gg== X-Forwarded-Encrypted: i=1; AHgh+RrBbWUGztufZvSNKkO8zlyS573RVR8cFmyIJ5WxkdIpP1vy44oo9xP3GfgeB32cKKYbIIn093BMmg6qHgw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7hRxn8donDdf+KTdtS8/ROkwmTUamYQSWhRXVo5Z3SrS9jeow yWfDyV5CR6wPfvLdO/Xoky5TwUI7EQ1MlGuEFP/1w2kmnqBlfdaUwexA X-Gm-Gg: AfdE7cl6d907S+FhUZpxBQo4+JzBQFzkC9m80bsX++E1MBDlKI+ZaFRaw+4wwawZpMg wgefS3TgEx/pDb8tCugQFO08FDQM5vwt+FQx4MP+QTKTY4OD6K1opdWQW9QrNqDNU2LrwtB6GHW ephYekl1TobTsGYJvyzSPnCxN/v4go701vOeWzHKw/k48+0bAtLXU0DZIR5y48yOUBwLgkmaNBp J7YNN+5+7eovb+R4FMRZj5DPvrB66wv5MhZ2ysi2cx3KCfGeuK/RggOm2RpTlvyIaB/HIRjBs+z 2HLjG/RCX+g3p1q20GqHeG9SNnqAHdVCuiZxNH2x6yOkrmbG7Q2YK3UdRqABYOB2vKVWoSwWflD R+BR2Rt8DQ2nYQSIvSxR0HNDYdFbaO/OXIMB4No9TQbnZ0y+ptbQZcf+MFfHe6usewjES/yxu2c urgy2VwyTz/E2DC4sJZXMTN714OtMe84JJLzTQ3svr1dZ2DRDz5Y7EXH52oG9X7RTiZcaYXgfhZ ilAYbAQ/NIPTLoCgFE= X-Received: by 2002:a05:600c:3e11:b0:493:df1d:7488 with SMTP id 5b1f17b1804b1-493e6904b5dmr108220295e9.16.1783666913698; Fri, 10 Jul 2026 00:01:53 -0700 (PDT) Received: from workstation.speedport.ip (p200300d5070afe7db3148fa84ed2ac78.dip0.t-ipconnect.de. [2003:d5:70a:fe7d:b314:8fa8:4ed2:ac78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493eb742d0esm114245665e9.13.2026.07.10.00.01.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jul 2026 00:01:53 -0700 (PDT) From: Mert Seftali To: Lee Jones , Pavel Machek Cc: Kees Cook , "Gustavo A . R . Silva" , linux-leds@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Mert Seftali Subject: [PATCH v3] leds: pwm: Annotate leds[] with __counted_by() Date: Fri, 10 Jul 2026 09:01:41 +0200 Message-ID: <20260710070141.37214-1-mertsftl@gmail.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add the __counted_by() attribute to the flexible array member leds[] in struct led_pwm_priv so the compiler and runtime (e.g. FORTIFY_SOURCE, UBSAN_BOUNDS) can bounds-check accesses against num_leds. For the annotation to be correct, num_leds must equal the number of allocated elements before leds[] is accessed. Even taking the address &priv->leds[i] is bounds-checked against num_leds under UBSAN_BOUNDS. The driver allocates device_get_child_node_count() elements up front, so set num_leds to that count right after allocation. While here, pass the led_pwm_data element into led_pwm_add() instead of the whole led_pwm_priv, so the helper no longer needs to index the array; the caller walks a led_pwm_data pointer over priv->leds. No functional change intended. Suggested-by: Lee Jones Signed-off-by: Mert Seftali --- Changes in v3: - v2 followed the review suggestion to pass the element and increment num_leds in the caller. That is exactly what Sashiko caught: with __counted_by(num_leds), &priv->leds[num_leds] is bounds-checked, so incrementing num_leds from 0 as the loop runs trips UBSAN_BOUNDS on probe. num_leds is now set to the full count up front and the caller walks a led_pwm_data pointer, which keeps the element-passing and the dropped index variable, just without the running counter. Changes in v2 (per Lee Jones review): - Pass the led_pwm_data element into led_pwm_add() so it drops the priv and index arguments. Build-tested only (no PWM-LED hardware). The __counted_by bounds behaviour that motivated v3 was verified with a standalone UBSAN reproducer on gcc and clang. drivers/leds/leds-pwm.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c index 6c1f2f50ff85..d1116a5cb2b2 100644 --- a/drivers/leds/leds-pwm.c +++ b/drivers/leds/leds-pwm.c @@ -36,7 +36,7 @@ struct led_pwm_data { struct led_pwm_priv { int num_leds; - struct led_pwm_data leds[]; + struct led_pwm_data leds[] __counted_by(num_leds); }; static int led_pwm_set(struct led_classdev *led_cdev, @@ -81,10 +81,9 @@ static int led_pwm_default_brightness_get(struct fwnode_handle *fwnode, } __attribute__((nonnull)) -static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, +static int led_pwm_add(struct device *dev, struct led_pwm_data *led_data, struct led_pwm *led, struct fwnode_handle *fwnode) { - struct led_pwm_data *led_data = &priv->leds[priv->num_leds]; struct led_init_data init_data = { .fwnode = fwnode }; int ret; @@ -167,12 +166,12 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, } } - priv->num_leds++; return 0; } static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv) { + struct led_pwm_data *led_data = priv->leds; struct led_pwm led; int ret; @@ -193,7 +192,7 @@ static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv) led.default_state = led_init_default_state_get(fwnode); - ret = led_pwm_add(dev, priv, &led, fwnode); + ret = led_pwm_add(dev, led_data++, &led, fwnode); if (ret) return ret; } @@ -217,6 +216,8 @@ static int led_pwm_probe(struct platform_device *pdev) if (!priv) return -ENOMEM; + priv->num_leds = count; + ret = led_pwm_create_fwnode(&pdev->dev, priv); if (ret) -- 2.55.0