All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] firewire: core: validate descriptor and sub-block lengths in fw_core_add_descriptor()
@ 2026-07-25 15:52 Sreeraj S Kurup
  2026-07-25 15:52 ` [PATCH V3 v3 1/2] firewire: core: validate overall descriptor length " Sreeraj S Kurup
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-07-25 15:52 UTC (permalink / raw)
  To: o-takashi; +Cc: linux1394-devel, linux-kernel, Sreeraj S Kurup

This two-patch series addresses potential out-of-bounds memory
accesses when parsing Config ROM descriptors in
fw_core_add_descriptor().

Patch 1 adds overall length validation using the in_range() macro
to ensure descriptors fit within standard IEEE 1394 Config ROM
limits (256 quadlets).

Patch 2 validates individual sub-block header lengths during
iteration to prevent reading past allocated buffer boundaries on
malformed inputs.

v2 -> v3:
 - Split original single patch into two distinct commits for cleaner
   review as requested by Takashi Sakamoto.
 - Simplified overall length check using the in_range() macro.

Sreeraj S Kurup (2):
  firewire: core: validate overall descriptor length in
    fw_core_add_descriptor()
  firewire: core: validate sub-block lengths in fw_core_add_descriptor()

 drivers/firewire/core-card.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

-- 
2.54.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH V3 v3 1/2] firewire: core: validate overall descriptor length in fw_core_add_descriptor()
  2026-07-25 15:52 [PATCH v3 0/2] firewire: core: validate descriptor and sub-block lengths in fw_core_add_descriptor() Sreeraj S Kurup
@ 2026-07-25 15:52 ` Sreeraj S Kurup
  2026-07-25 15:52 ` [PATCH V3 v3 2/2] firewire: core: validate sub-block lengths " Sreeraj S Kurup
  2026-07-27  0:47 ` [PATCH v3 0/2] firewire: core: validate descriptor and " Takashi Sakamoto
  2 siblings, 0 replies; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-07-25 15:52 UTC (permalink / raw)
  To: o-takashi; +Cc: linux1394-devel, linux-kernel, Sreeraj S Kurup

In fw_core_add_descriptor(), incoming descriptor structures are processed
without checking whether the descriptor's specified length falls within
valid boundaries. An empty descriptor (length 0) or an oversized descriptor
exceeding the IEEE 1394 Config ROM capacity can lead to invalid processing.

Add bounds checking at the start of fw_core_add_descriptor() using the
in_range() helper macro to reject descriptors with length 0 or length
exceeding 256 quadlets (the standard maximum Configuration ROM size).

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
 drivers/firewire/core-card.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/firewire/core-card.c b/drivers/firewire/core-card.c
index a754c6366b97..eaec54ea287a 100644
--- a/drivers/firewire/core-card.c
+++ b/drivers/firewire/core-card.c
@@ -16,6 +16,7 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/minmax.h>
 #include <linux/spinlock.h>
 #include <linux/workqueue.h>
 
@@ -167,11 +168,10 @@ int fw_core_add_descriptor(struct fw_descriptor *desc)
 {
 	size_t i;
 
-	/*
-	 * Check descriptor is valid; the length of all blocks in the
-	 * descriptor has to add up to exactly the length of the
-	 * block.
-	 */
+	/* Reject empty descriptors or those exceeding max Config ROM size (256 quadlets) */
+	if (!in_range(desc->length, 1, 256))
+		return -EINVAL;
+
 	i = 0;
 	while (i < desc->length)
 		i += (desc->data[i] >> 16) + 1;
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH V3 v3 2/2] firewire: core: validate sub-block lengths in fw_core_add_descriptor()
  2026-07-25 15:52 [PATCH v3 0/2] firewire: core: validate descriptor and sub-block lengths in fw_core_add_descriptor() Sreeraj S Kurup
  2026-07-25 15:52 ` [PATCH V3 v3 1/2] firewire: core: validate overall descriptor length " Sreeraj S Kurup
@ 2026-07-25 15:52 ` Sreeraj S Kurup
  2026-07-27  0:47 ` [PATCH v3 0/2] firewire: core: validate descriptor and " Takashi Sakamoto
  2 siblings, 0 replies; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-07-25 15:52 UTC (permalink / raw)
  To: o-takashi; +Cc: linux1394-devel, linux-kernel, Sreeraj S Kurup

When traversing internal block structures of a descriptor in
fw_core_add_descriptor(), each sub-block header specifies its own length
in the upper 16 bits of its header quadlet.

If a malformed or corrupted descriptor provides a sub-block length that
exceeds the remaining total length of the descriptor buffer, the parsing
loop advances past the allocated boundary of desc->data, leading to an
out-of-bounds read access.

Validate each sub-block's length against the remaining descriptor size
before advancing the offset pointer to ensure loop bounds safety.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
 drivers/firewire/core-card.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/drivers/firewire/core-card.c b/drivers/firewire/core-card.c
index eaec54ea287a..7b5d324aa7cc 100644
--- a/drivers/firewire/core-card.c
+++ b/drivers/firewire/core-card.c
@@ -173,9 +173,25 @@ int fw_core_add_descriptor(struct fw_descriptor *desc)
 		return -EINVAL;
 
 	i = 0;
-	while (i < desc->length)
-		i += (desc->data[i] >> 16) + 1;
+	/*
+	 * Validate internal block structures within the descriptor. Each sub-block
+	 * encodes its length in the top 16 bits of its header quadlet.
+	 */
+	while (i < desc->length) {
+		u16 block_len = desc->data[i] >> 16;
+
+		/*
+		 * Guard against corrupted descriptors where an individual block length
+		 * claims to extend past the allocated end of desc->data, avoiding
+		 * out-of-bounds reads.
+		 */
+		if (block_len >= desc->length - i)
+			return -EINVAL;
+
+		i += block_len + 1;
+	}
 
+	/* The sum of sub-block lengths must match total descriptor length */
 	if (i != desc->length)
 		return -EINVAL;
 
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 0/2] firewire: core: validate descriptor and sub-block lengths in fw_core_add_descriptor()
  2026-07-25 15:52 [PATCH v3 0/2] firewire: core: validate descriptor and sub-block lengths in fw_core_add_descriptor() Sreeraj S Kurup
  2026-07-25 15:52 ` [PATCH V3 v3 1/2] firewire: core: validate overall descriptor length " Sreeraj S Kurup
  2026-07-25 15:52 ` [PATCH V3 v3 2/2] firewire: core: validate sub-block lengths " Sreeraj S Kurup
@ 2026-07-27  0:47 ` Takashi Sakamoto
  2 siblings, 0 replies; 4+ messages in thread
From: Takashi Sakamoto @ 2026-07-27  0:47 UTC (permalink / raw)
  To: Sreeraj S Kurup; +Cc: linux1394-devel, linux-kernel

Hi,

On Sat, Jul 25, 2026 at 03:52:53PM +0000, Sreeraj S Kurup wrote:
> This two-patch series addresses potential out-of-bounds memory
> accesses when parsing Config ROM descriptors in
> fw_core_add_descriptor().
> 
> Patch 1 adds overall length validation using the in_range() macro
> to ensure descriptors fit within standard IEEE 1394 Config ROM
> limits (256 quadlets).
> 
> Patch 2 validates individual sub-block header lengths during
> iteration to prevent reading past allocated buffer boundaries on
> malformed inputs.
> 
> v2 -> v3:
>  - Split original single patch into two distinct commits for cleaner
>    review as requested by Takashi Sakamoto.
>  - Simplified overall length check using the in_range() macro.
> 
> Sreeraj S Kurup (2):
>   firewire: core: validate overall descriptor length in
>     fw_core_add_descriptor()
>   firewire: core: validate sub-block lengths in fw_core_add_descriptor()
> 
>  drivers/firewire/core-card.c | 28 ++++++++++++++++++++++------
>  1 file changed, 22 insertions(+), 6 deletions(-)

Applied to for-next branch.

By the way, how do you think about the following change in your previous
patches? It seems to be omitted from current patchset.

======== 8< --------
diff --git a/drivers/firewire/core-card.c b/drivers/firewire/core-card.c
index a754c6366b97..cb8ce491fe9d 100644
--- a/drivers/firewire/core-card.c
+++ b/drivers/firewire/core-card.c
@@ -143,7 +143,11 @@ static void generate_config_rom(struct fw_card *card, __be32 *config_rom)
 	for (i = 0; i < j; i += length + 1)
 		length = fw_compute_block_crc(config_rom + i);

-	WARN_ON(j != config_rom_length);
+     if (j != config_rom_length) {
+        pr_warn("FireWire ROM length mismatch: expected %zu, got %d\n",
+            config_rom_length, j);
+        config_rom_length = j;
+     }
 }
======== 8< --------


Thanks

Takashi Sakamoto

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-27  0:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-25 15:52 [PATCH v3 0/2] firewire: core: validate descriptor and sub-block lengths in fw_core_add_descriptor() Sreeraj S Kurup
2026-07-25 15:52 ` [PATCH V3 v3 1/2] firewire: core: validate overall descriptor length " Sreeraj S Kurup
2026-07-25 15:52 ` [PATCH V3 v3 2/2] firewire: core: validate sub-block lengths " Sreeraj S Kurup
2026-07-27  0:47 ` [PATCH v3 0/2] firewire: core: validate descriptor and " Takashi Sakamoto

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.