Skip to content

Commit d4cdc46

Browse files
bwhacksKalle Valo
authored andcommitted
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 Reported-by: Martin-Éric Racine <[email protected]> References: https://bugs.debian.org/1062421 References: https://bugzilla.kernel.org/show_bug.cgi?id=219124 Signed-off-by: Ben Hutchings <[email protected]> Fixes: 54d9469 ("fortify: Add run-time WARN for cross-field memcpy()") Tested-by: Martin-Éric Racine <[email protected]> Tested-by: Brandon Nielsen <[email protected]> Acked-by: Stanislaw Gruszka <[email protected]> Signed-off-by: Kalle Valo <[email protected]> Link: https://patch.msgid.link/ZuIhQRi/[email protected]
1 parent 34b6954 commit d4cdc46

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

drivers/net/wireless/intel/iwlegacy/common.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3122,6 +3122,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
31223122
struct il_cmd_meta *out_meta;
31233123
dma_addr_t phys_addr;
31243124
unsigned long flags;
3125+
u8 *out_payload;
31253126
u32 idx;
31263127
u16 fix_size;
31273128

@@ -3157,6 +3158,16 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
31573158
out_cmd = txq->cmd[idx];
31583159
out_meta = &txq->meta[idx];
31593160

3161+
/* The payload is in the same place in regular and huge
3162+
* command buffers, but we need to let the compiler know when
3163+
* we're using a larger payload buffer to avoid "field-
3164+
* spanning write" warnings at run-time for huge commands.
3165+
*/
3166+
if (cmd->flags & CMD_SIZE_HUGE)
3167+
out_payload = ((struct il_device_cmd_huge *)out_cmd)->cmd.payload;
3168+
else
3169+
out_payload = out_cmd->cmd.payload;
3170+
31603171
if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
31613172
spin_unlock_irqrestore(&il->hcmd_lock, flags);
31623173
return -ENOSPC;
@@ -3170,7 +3181,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
31703181
out_meta->callback = cmd->callback;
31713182

31723183
out_cmd->hdr.cmd = cmd->id;
3173-
memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
3184+
memcpy(out_payload, cmd->data, cmd->len);
31743185

31753186
/* At this point, the out_cmd now has all of the incoming cmd
31763187
* information */

drivers/net/wireless/intel/iwlegacy/common.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,18 @@ struct il_device_cmd {
560560

561561
#define TFD_MAX_PAYLOAD_SIZE (sizeof(struct il_device_cmd))
562562

563+
/**
564+
* struct il_device_cmd_huge
565+
*
566+
* For use when sending huge commands.
567+
*/
568+
struct il_device_cmd_huge {
569+
struct il_cmd_header hdr; /* uCode API */
570+
union {
571+
u8 payload[IL_MAX_CMD_SIZE - sizeof(struct il_cmd_header)];
572+
} __packed cmd;
573+
} __packed;
574+
563575
struct il_host_cmd {
564576
const void *data;
565577
unsigned long reply_page;

0 commit comments

Comments
 (0)