Skip to content

Commit 6f4b102

Browse files
GustavoARSilvamartinkpetersen
authored andcommitted
scsi: qla2xxx: Fix memcpy() field-spanning write issue
purex_item.iocb is defined as a 64-element u8 array, but 64 is the minimum size and it can be allocated larger. This makes it a standard empty flex array. This was motivated by field-spanning write warnings during FPIN testing: https://lore.kernel.org/linux-nvme/20250709211919.49100-1-bgurney@redhat.com/ > kernel: memcpy: detected field-spanning write (size 60) of single field > "((uint8_t *)fpin_pkt + buffer_copy_offset)" > at drivers/scsi/qla2xxx/qla_isr.c:1221 (size 44) I removed the outer wrapper from the iocb flex array, so that it can be linked to 'purex_item.size' with '__counted_by'. These changes remove the default minimum 64-byte allocation, requiring further changes. In 'struct scsi_qla_host' the embedded 'default_item' is now followed by '__default_item_iocb[QLA_DEFAULT_PAYLOAD_SIZE]' to reserve space that will be used as 'default_item.iocb'. This is wrapped using the 'TRAILING_OVERLAP()' macro helper, which effectively creates a union between flexible-array member 'default_item.iocb' and '__default_item_iocb'. Since 'struct pure_item' now contains a flexible-array member, the helper must be placed at the end of 'struct scsi_qla_host' to prevent a '-Wflex-array-member-not-at-end' warning. 'qla24xx_alloc_purex_item()' is adjusted to no longer expect the default minimum size to be part of 'sizeof(struct purex_item)', the entire flexible array size is added to the structure size for allocation. This also slightly changes the layout of the purex_item struct, as 2-bytes of padding are added between 'size' and 'iocb'. The resulting size is the same, but iocb is shifted 2-bytes (the original 'purex_item' structure was padded at the end, after the 64-byte defined array size). I don't think this is a problem. Tested-by: Bryan Gurney <bgurney@redhat.com> Co-developed-by: Chris Leech <cleech@redhat.com> Signed-off-by: Chris Leech <cleech@redhat.com> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Link: https://lore.kernel.org/r/20250813200744.17975-10-bgurney@redhat.com Reviewed-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 6b5da52 commit 6f4b102

4 files changed

Lines changed: 18 additions & 16 deletions

File tree

drivers/scsi/qla2xxx/qla_def.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4890,9 +4890,7 @@ struct purex_item {
48904890
struct purex_item *pkt);
48914891
atomic_t in_use;
48924892
uint16_t size;
4893-
struct {
4894-
uint8_t iocb[64];
4895-
} iocb;
4893+
uint8_t iocb[] __counted_by(size);
48964894
};
48974895

48984896
#include "qla_edif.h"
@@ -5101,7 +5099,6 @@ typedef struct scsi_qla_host {
51015099
struct list_head head;
51025100
spinlock_t lock;
51035101
} purex_list;
5104-
struct purex_item default_item;
51055102

51065103
struct name_list_extended gnl;
51075104
/* Count of active session/fcport */
@@ -5130,6 +5127,11 @@ typedef struct scsi_qla_host {
51305127
#define DPORT_DIAG_IN_PROGRESS BIT_0
51315128
#define DPORT_DIAG_CHIP_RESET_IN_PROGRESS BIT_1
51325129
uint16_t dport_status;
5130+
5131+
/* Must be last --ends in a flexible-array member. */
5132+
TRAILING_OVERLAP(struct purex_item, default_item, iocb,
5133+
uint8_t __default_item_iocb[QLA_DEFAULT_PAYLOAD_SIZE];
5134+
);
51335135
} scsi_qla_host_t;
51345136

51355137
struct qla27xx_image_status {

drivers/scsi/qla2xxx/qla_isr.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,17 +1077,17 @@ static struct purex_item *
10771077
qla24xx_alloc_purex_item(scsi_qla_host_t *vha, uint16_t size)
10781078
{
10791079
struct purex_item *item = NULL;
1080-
uint8_t item_hdr_size = sizeof(*item);
10811080

10821081
if (size > QLA_DEFAULT_PAYLOAD_SIZE) {
1083-
item = kzalloc(item_hdr_size +
1084-
(size - QLA_DEFAULT_PAYLOAD_SIZE), GFP_ATOMIC);
1082+
item = kzalloc(struct_size(item, iocb, size), GFP_ATOMIC);
10851083
} else {
10861084
if (atomic_inc_return(&vha->default_item.in_use) == 1) {
10871085
item = &vha->default_item;
10881086
goto initialize_purex_header;
10891087
} else {
1090-
item = kzalloc(item_hdr_size, GFP_ATOMIC);
1088+
item = kzalloc(
1089+
struct_size(item, iocb, QLA_DEFAULT_PAYLOAD_SIZE),
1090+
GFP_ATOMIC);
10911091
}
10921092
}
10931093
if (!item) {
@@ -1127,17 +1127,16 @@ qla24xx_queue_purex_item(scsi_qla_host_t *vha, struct purex_item *pkt,
11271127
* @vha: SCSI driver HA context
11281128
* @pkt: ELS packet
11291129
*/
1130-
static struct purex_item
1131-
*qla24xx_copy_std_pkt(struct scsi_qla_host *vha, void *pkt)
1130+
static struct purex_item *
1131+
qla24xx_copy_std_pkt(struct scsi_qla_host *vha, void *pkt)
11321132
{
11331133
struct purex_item *item;
11341134

1135-
item = qla24xx_alloc_purex_item(vha,
1136-
QLA_DEFAULT_PAYLOAD_SIZE);
1135+
item = qla24xx_alloc_purex_item(vha, QLA_DEFAULT_PAYLOAD_SIZE);
11371136
if (!item)
11381137
return item;
11391138

1140-
memcpy(&item->iocb, pkt, sizeof(item->iocb));
1139+
memcpy(&item->iocb, pkt, QLA_DEFAULT_PAYLOAD_SIZE);
11411140
return item;
11421141
}
11431142

drivers/scsi/qla2xxx/qla_nvme.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ void qla2xxx_process_purls_iocb(void **pkt, struct rsp_que **rsp)
13081308

13091309
ql_dbg(ql_dbg_unsol, vha, 0x2121,
13101310
"PURLS OP[%01x] size %d xchg addr 0x%x portid %06x\n",
1311-
item->iocb.iocb[3], item->size, uctx->exchange_address,
1311+
item->iocb[3], item->size, uctx->exchange_address,
13121312
fcport->d_id.b24);
13131313
/* +48 0 1 2 3 4 5 6 7 8 9 A B C D E F
13141314
* ----- -----------------------------------------------

drivers/scsi/qla2xxx/qla_os.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6459,9 +6459,10 @@ void qla24xx_process_purex_rdp(struct scsi_qla_host *vha,
64596459
void
64606460
qla24xx_free_purex_item(struct purex_item *item)
64616461
{
6462-
if (item == &item->vha->default_item)
6462+
if (item == &item->vha->default_item) {
64636463
memset(&item->vha->default_item, 0, sizeof(struct purex_item));
6464-
else
6464+
memset(&item->vha->__default_item_iocb, 0, QLA_DEFAULT_PAYLOAD_SIZE);
6465+
} else
64656466
kfree(item);
64666467
}
64676468

0 commit comments

Comments
 (0)