Skip to contentSkip to conversation

wifi: iwlegacy: Fix "field-spanning write" warning in il_enqueue_hcmd()

PATCH In review Ben Hutchings 5 participants 9 messages Sep 11, 2024 – Sep 19, 2024 Intel wireless 3945ABG/bg, 4965AGN (iwlegacy)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: iwlegacy: Fix "field-spanning write" warning in il_enqueue_hcmd()

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

    memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")
---
 drivers/net/wireless/intel/iwlegacy/common.c | 13 ++++++++++++-
 drivers/net/wireless/intel/iwlegacy/common.h | 12 ++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
drivers/net/wireless/intel/iwlegacy/common.c +12 -1
index 9d33a66a49b5..4616293ec0cf 100644
@@ -3122,6 +3122,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
 	struct il_cmd_meta *out_meta;
 	dma_addr_t phys_addr;
 	unsigned long flags;
+	u8 *out_payload;
 	u32 idx;
 	u16 fix_size;
 
@@ -3157,6 +3158,16 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
 	out_cmd = txq->cmd[idx];
 	out_meta = &txq->meta[idx];
 
+	/* The payload is in the same place in regular and huge
+	 * command buffers, but we need to let the compiler know when
+	 * we're using a larger payload buffer to avoid "field-
+	 * spanning write" warnings at run-time for huge commands.
+	 */
+	if (cmd->flags & CMD_SIZE_HUGE)
+		out_payload = ((struct il_device_cmd_huge *)out_cmd)->cmd.payload;
+	else
+		out_payload = out_cmd->cmd.payload;
+
 	if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
 		spin_unlock_irqrestore(&il->hcmd_lock, flags);
 		return -ENOSPC;
@@ -3170,7 +3181,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
 		out_meta->callback = cmd->callback;
 
 	out_cmd->hdr.cmd = cmd->id;
-	memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
+	memcpy(out_payload, cmd->data, cmd->len);
 
 	/* At this point, the out_cmd now has all of the incoming cmd
 	 * information */
drivers/net/wireless/intel/iwlegacy/common.h +12 -0
index 69687fcf963f..027dae5619a3 100644
@@ -560,6 +560,18 @@ struct il_device_cmd {
 
 #define TFD_MAX_PAYLOAD_SIZE (sizeof(struct il_device_cmd))
 
+/**
+ * struct il_device_cmd_huge
+ *
+ * For use when sending huge commands.
+ */
+struct il_device_cmd_huge {
+	struct il_cmd_header hdr;	/* uCode API */
+	union {
+		u8 payload[IL_MAX_CMD_SIZE - sizeof(struct il_cmd_header)];
+	} __packed cmd;
+} __packed;
+
 struct il_host_cmd {
 	const void *data;
 	unsigned long reply_page;
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 19 lines · Ben Hutchings

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

    memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

Quoted 7 lines

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")

I proposed diffrent fix for this here:
https://lore.kernel.org/linux-wireless/[email protected]/
but never get feedback if it works on real HW.
So I prefer this one, sice it was tested.

Martin-Éric and Brandon, could you plase also test patch from
https://lore.kernel.org/linux-wireless/Zr2gxERA3RL3EwRe@elsanto/
if it does not break the driver?

Thanks
Stanislaw

Quoted 66 lines
---
 drivers/net/wireless/intel/iwlegacy/common.c | 13 ++++++++++++-
 drivers/net/wireless/intel/iwlegacy/common.h | 12 ++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
drivers/net/wireless/intel/iwlegacy/common.c +12 -1
index 9d33a66a49b5..4616293ec0cf 100644
@@ -3122,6 +3122,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
 	struct il_cmd_meta *out_meta;
 	dma_addr_t phys_addr;
 	unsigned long flags;
+	u8 *out_payload;
 	u32 idx;
 	u16 fix_size;
 
@@ -3157,6 +3158,16 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
 	out_cmd = txq->cmd[idx];
 	out_meta = &txq->meta[idx];
 
+	/* The payload is in the same place in regular and huge
+	 * command buffers, but we need to let the compiler know when
+	 * we're using a larger payload buffer to avoid "field-
+	 * spanning write" warnings at run-time for huge commands.
+	 */
+	if (cmd->flags & CMD_SIZE_HUGE)
+		out_payload = ((struct il_device_cmd_huge *)out_cmd)->cmd.payload;
+	else
+		out_payload = out_cmd->cmd.payload;
+
 	if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
 		spin_unlock_irqrestore(&il->hcmd_lock, flags);
 		return -ENOSPC;
@@ -3170,7 +3181,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
 		out_meta->callback = cmd->callback;
 
 	out_cmd->hdr.cmd = cmd->id;
-	memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
+	memcpy(out_payload, cmd->data, cmd->len);
 
 	/* At this point, the out_cmd now has all of the incoming cmd
 	 * information */
drivers/net/wireless/intel/iwlegacy/common.h +12 -0
index 69687fcf963f..027dae5619a3 100644
@@ -560,6 +560,18 @@ struct il_device_cmd {
 
 #define TFD_MAX_PAYLOAD_SIZE (sizeof(struct il_device_cmd))
 
+/**
+ * struct il_device_cmd_huge
+ *
+ * For use when sending huge commands.
+ */
+struct il_device_cmd_huge {
+	struct il_cmd_header hdr;	/* uCode API */
+	union {
+		u8 payload[IL_MAX_CMD_SIZE - sizeof(struct il_cmd_header)];
+	} __packed cmd;
+} __packed;
+
 struct il_host_cmd {
 	const void *data;
 	unsigned long reply_page;
#2 lore raw
Quoted 33 lines · Stanislaw Gruszka
Quoted 19 lines · Ben Hutchings

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

     memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

Quoted 7 lines

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")

I proposed diffrent fix for this here:
https://lore.kernel.org/linux-wireless/[email protected]/
but never get feedback if it works on real HW.
So I prefer this one, sice it was tested.

Quoted 6 lines

Martin-Éric and Brandon, could you plase also test patch from
https://lore.kernel.org/linux-wireless/Zr2gxERA3RL3EwRe@elsanto/
if it does not break the driver?

As far as I can tell nothing breaks with that additional patch applied.

Quoted 71 lines

Thanks
Stanislaw

Quoted 66 lines
---
  drivers/net/wireless/intel/iwlegacy/common.c | 13 ++++++++++++-
  drivers/net/wireless/intel/iwlegacy/common.h | 12 ++++++++++++
  2 files changed, 24 insertions(+), 1 deletion(-)
drivers/net/wireless/intel/iwlegacy/common.c +12 -1
index 9d33a66a49b5..4616293ec0cf 100644
@@ -3122,6 +3122,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
  	struct il_cmd_meta *out_meta;
  	dma_addr_t phys_addr;
  	unsigned long flags;
+	u8 *out_payload;
  	u32 idx;
  	u16 fix_size;
 
@@ -3157,6 +3158,16 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
  	out_cmd = txq->cmd[idx];
  	out_meta = &txq->meta[idx];
 
+	/* The payload is in the same place in regular and huge
+	 * command buffers, but we need to let the compiler know when
+	 * we're using a larger payload buffer to avoid "field-
+	 * spanning write" warnings at run-time for huge commands.
+	 */
+	if (cmd->flags & CMD_SIZE_HUGE)
+		out_payload = ((struct il_device_cmd_huge *)out_cmd)->cmd.payload;
+	else
+		out_payload = out_cmd->cmd.payload;
+
  	if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
  		spin_unlock_irqrestore(&il->hcmd_lock, flags);
  		return -ENOSPC;
@@ -3170,7 +3181,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
  		out_meta->callback = cmd->callback;
 
  	out_cmd->hdr.cmd = cmd->id;
-	memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
+	memcpy(out_payload, cmd->data, cmd->len);
 
  	/* At this point, the out_cmd now has all of the incoming cmd
  	 * information */
drivers/net/wireless/intel/iwlegacy/common.h +12 -0
index 69687fcf963f..027dae5619a3 100644
@@ -560,6 +560,18 @@ struct il_device_cmd {
 
  #define TFD_MAX_PAYLOAD_SIZE (sizeof(struct il_device_cmd))
 
+/**
+ * struct il_device_cmd_huge
+ *
+ * For use when sending huge commands.
+ */
+struct il_device_cmd_huge {
+	struct il_cmd_header hdr;	/* uCode API */
+	union {
+		u8 payload[IL_MAX_CMD_SIZE - sizeof(struct il_cmd_header)];
+	} __packed cmd;
+} __packed;
+
  struct il_host_cmd {
  	const void *data;
  	unsigned long reply_page;
#3 lore raw
Quoted 34 lines · Brandon Nielsen
Quoted 33 lines · Stanislaw Gruszka
Quoted 19 lines · Ben Hutchings

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

     memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

Quoted 7 lines

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")

I proposed diffrent fix for this here:
https://lore.kernel.org/linux-wireless/[email protected]/
but never get feedback if it works on real HW.
So I prefer this one, sice it was tested.

Quoted 8 lines
Quoted 6 lines

Martin-Éric and Brandon, could you plase also test patch from
https://lore.kernel.org/linux-wireless/Zr2gxERA3RL3EwRe@elsanto/
if it does not break the driver?

As far as I can tell nothing breaks with that additional patch applied.

Great, thanks for testing the patch.
Stanislaw

#4 lore raw
In reply to Stanislaw Gruszka

to 12. syysk. 2024 klo 11.40 Stanislaw Gruszka ([email protected]) kirjoitti:

Quoted 29 lines · Ben Hutchings
Quoted 19 lines

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

    memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

Quoted 7 lines

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")
Quoted 3 lines

I proposed diffrent fix for this here:
https://lore.kernel.org/linux-wireless/[email protected]/
but never get feedback if it works on real HW.

I submitted bug reports about this at Debian and on kernel.org's Bugzilla, but never saw this patch, so obviously could not provide feedback.

Quoted 3 lines

So I prefer this one, sice it was tested.

Thanks.

Quoted 3 lines

Martin-Éric and Brandon, could you plase also test patch from
https://lore.kernel.org/linux-wireless/Zr2gxERA3RL3EwRe@elsanto/
if it does not break the driver?

I am not currently in a position to test this. I will trust Brandon's results on this one.

Meanwhile, an entirely unrelated bug crept into the kernel. See attachments on the above bugzilla report.

Martin-Éric

#5 lore raw
In reply to Ben Hutchings
Quoted 19 lines · Ben Hutchings

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

    memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

Quoted 8 lines

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")

Should this patch go wireless tree for v6.12? As this is a regression I think it should.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/ZuIhQRi/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#6 lore raw
Quoted 30 lines · Kalle Valo
Quoted 19 lines · Ben Hutchings

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

    memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

Quoted 8 lines

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")
Quoted 2 lines

Should this patch go wireless tree for v6.12? As this is a regression I think it should.

It's not driver regression per se, just false positive warning when built with CONFIG_FORTIFY_SOURCE. But it should go to 6.12 IMHO as fix for the warning.

Regards
Stanislaw

#7 lore raw
Quoted 32 lines · Stanislaw Gruszka
Quoted 30 lines · Kalle Valo
Quoted 27 lines · Ben Hutchings

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

    memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")
Quoted 6 lines
Quoted 2 lines

Should this patch go wireless tree for v6.12? As this is a regression I think it should.

It's not driver regression per se, just false positive warning when built with CONFIG_FORTIFY_SOURCE. But it should go to 6.12 IMHO as fix for the warning.

Thanks, I'll then take this to wireless tree.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#8 lore raw
In reply to Ben Hutchings
Quoted 19 lines · Ben Hutchings

iwlegacy uses command buffers with a payload size of 320 bytes (default) or 4092 bytes (huge). The struct il_device_cmd type describes the default buffers and there is no separate type describing the huge buffers.

The il_enqueue_hcmd() function works with both default and huge buffers, and has a memcpy() to the buffer payload. The size of this copy may exceed 320 bytes when using a huge buffer, which now results in a run-time warning:

    memcpy: detected field-spanning write (size 1014) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3170 (size 320)

To fix this:

- Define a new struct type for huge buffers, with a correctly sized payload field
- When using a huge buffer in il_enqueue_hcmd(), cast the command buffer pointer to that type when looking up the payload field

Quoted 8 lines

References: https://bugs.debian.org/1062421
References: https://bugzilla.kernel.org/show_bug.cgi?id=219124

Fixes 54d9469bc515 ("fortify: Add run-time WARN for cross-field memcpy()")

Patch applied to wireless.git, thanks.

d4cdc46ca16a wifi: iwlegacy: Fix "field-spanning write" warning in il_enqueue_hcmd()

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/ZuIhQRi/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches