Skip to contentSkip to conversation

firewire: core: add __counted_by_ptr attribute to struct fw_device

PATCH In review Bill Wendling 2 participants 2 messages Sep 25 – Sep 27 Firewire

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firewire: core: add __counted_by_ptr attribute to struct fw_device

The GCC and Clang attribute '__counted_by_ptr' associates a pointer field with an integer field holding its element count. This enables runtime bounds checking by KASAN and UBSAN to prevent out-of-bounds accesses to the pointer.

In 'struct fw_device' (defined in 'include/linux/firewire.h'), the field 'config_rom' is a pointer to the device's Configuration ROM data, and its associated element count is stored in 'config_rom_length'.

Additionally, update the existing KUnit test in 'drivers/firewire/device-attribute-test.c' where 'config_rom_length' was incorrectly initialized with the byte size ('sizeof(simple_avc_config_rom)') instead of the element count ('ARRAY_SIZE(simple_avc_config_rom)'). This ensures the '__counted_by_ptr' bounds-checking annotation does not trigger any false-positive panics or compile/runtime checks.

---
 drivers/firewire/device-attribute-test.c | 4 ++--
 include/linux/firewire.h                 | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/firewire/device-attribute-test.c +2 -2
index 97478a96d1c9..2c482afca8f1 100644
@@ -86,7 +86,7 @@ static void device_attr_simple_avc(struct kunit *test)
 			.type = &fw_device_type,
 		},
 		.config_rom = simple_avc_config_rom,
-		.config_rom_length = sizeof(simple_avc_config_rom),
+		.config_rom_length = ARRAY_SIZE(simple_avc_config_rom),
 	};
 	static const struct fw_unit unit0 = {
 		.device = {
@@ -168,7 +168,7 @@ static void device_attr_legacy_avc(struct kunit *test)
 			.type = &fw_device_type,
 		},
 		.config_rom = legacy_avc_config_rom,
-		.config_rom_length = sizeof(legacy_avc_config_rom),
+		.config_rom_length = ARRAY_SIZE(legacy_avc_config_rom),
 	};
 	static const struct fw_unit unit0 = {
 		.device = {
include/linux/firewire.h +1 -1
index fd35a6570cd8..2040b17d86fa 100644
@@ -224,7 +224,7 @@ struct fw_device {
 	struct mutex client_list_mutex;
 	struct list_head client_list;
 
-	const u32 *config_rom;
+	const u32 *config_rom __counted_by_ptr(config_rom_length);
 	size_t config_rom_length;
 	int config_rom_retries;
 	unsigned is_local:1;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
maintainer
#1 lore raw
Quoted 17 lines · Bill Wendling

The GCC and Clang attribute '__counted_by_ptr' associates a pointer field with an integer field holding its element count. This enables runtime bounds checking by KASAN and UBSAN to prevent out-of-bounds accesses to the pointer.

In 'struct fw_device' (defined in 'include/linux/firewire.h'), the field 'config_rom' is a pointer to the device's Configuration ROM data, and its associated element count is stored in 'config_rom_length'.

Additionally, update the existing KUnit test in 'drivers/firewire/device-attribute-test.c' where 'config_rom_length' was incorrectly initialized with the byte size ('sizeof(simple_avc_config_rom)') instead of the element count ('ARRAY_SIZE(simple_avc_config_rom)'). This ensures the '__counted_by_ptr' bounds-checking annotation does not trigger any false-positive panics or compile/runtime checks.

Quoted 7 lines
---
 drivers/firewire/device-attribute-test.c | 4 ++--
 include/linux/firewire.h                 | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

Thank you to correct the KUnit tests as well. Applied to for-next branch.

Thanks

Takashi Sakamoto