zfcp: Fix integer underflow in status read buffer payload length
Subsystems (2)
PATCH v5 1/1 zfcp: Fix integer underflow in status read buffer payload length
zfcp_dbf_hba_fsf_uss() only guards against a zero-length status read buffer. If srb->length is smaller than the fixed header size of struct fsf_status_read_buffer, subtracting the payload offset underflows and can lead to an out-of-bounds read.zfcp_dbf_san_in_els() has the same issue, where the underflowed value is used as the scatterlist payload length.
Fix both cases by validating payload access against the payload offset. If the reported buffer length does not reach the payload area, suppress payload reads and avoid constructing a scatterlist for non-existent payload data.
For HBA tracing, preserve serviceability by unconditionally storing the value-shifted SRB length in pl_len. This retains a reversible mapping to the original channel-reported length without changing the existing trace record format, while payload data is only read when the reported buffer length reaches the payload area.
For SAN tracing, preserve the original channel-reported SRB length in the trace record and separately track the number of payload bytes that may be safely read. This retains trace evidence of anomalous firmware-reported lengths while preventing payload reads beyond the reported buffer.
For valid payloads, preserve the existing tracing behavior.
---
Changes in v5:
- Address Steffen's review comments regarding HBA/SAN trace servicability.
- Preserve HBA pl_len semantics so trace readers can always assume the value-shifted subtraction.
- Retain the original channel-reported SRB length in SAN trace records.
- Use a separate capped length to control payload reads in the SAN path.
- Prevent payload reads when the reported buffer length does not reach the payload area.
Changes in v4:
- Preserve the original channel-reported SRB length in SAN trace records when no payload area exists.
- Distinguish between the recorded length and the capped payload length using length/cap_len semantics.
- Set cap_length to zero when no payload area exists to prevent payload reads beyond the reported buffer.
- Address both the underflow/OOB issue and the trace serviceability concern in the same patch.
Changes in v3:
- Rework payload length handling for srb->length < pay_offset and treat such cases as an empty payload.
- Prevent payload processing and scatterlist initialization when the reported buffer length does not reach the payload area.
- Keep the fix focused on the underflow/OOB issue and preserve the existing tracing behavior for valid payloads.
Changes in v2:
- Preserve anomalous short srb->length values by capping payload lengths to srb->length instead of forcing them to 0.
- Introduce a local pay_offs₹et variable to avoid repeated offsetof()
Changes on the git hub can be found at:
https://github.ibm.com/Ajay-Kumar-K-R/linux/tree/zfcp-fix-srb-length-underflow-v5
--- drivers/s390/scsi/zfcp_dbf.c | 36 ++++++++++++++++++++++++++---------- drivers/s390/scsi/zfcp_dbf.h | 2 +- 2 files changed, 27 insertions(+), 11 deletions(-)
drivers/s390/scsi/zfcp_dbf.c +26 -10
@@ -223,6 +223,7 @@ void zfcp_dbf_hba_fsf_uss(char *tag, struct zfcp_fsf_req *req)struct zfcp_dbf_hba *rec = &dbf->hba_buf;static int const level =2 ;unsigned long flags; +const u32 pay_offset = offsetof(struct fsf_status_read_buffer , payload);if (unlikely (!debug_level_enabled (dbf->hba, level)))return ;
@@ -253,11 +254,15 @@ void zfcp_dbf_hba_fsf_uss(char *tag, struct zfcp_fsf_req *req) rec->u.uss.s_id =ntoh24 (srb->s_id);memcpy (&rec->u.uss.res4, &srb->res4,sizeof (rec->u.uss.res4)); -/* status read buffer payload length */ - rec->pl_len = (!srb->length) ?0 : srb->length - - offsetof(struct fsf_status_read_buffer , payload); +/* Unconditionally store value-shifted SRB length: pl_len is a bijective +* map of srb->length (pl_len + pay_offset == srb->length). A bogus +* srb->length < pay_offset wraps to a large non-zero pl_len, which is +* unambiguously distinguishable from pl_len == 0(genuine empty payload) +*/ + rec->pl_len = (u16)(srb->length - pay_offset); -if (rec->pl_len) +/* Only access payload bytes when srb->length actually covers them. */ +if (srb->length > pay_offset)zfcp_dbf_pl_write (dbf, srb->payload.data, rec->pl_len,"fsf_uss" , req->req_id,ZFCP_DBF_PAY_LEVEL ); log:
@@ -714,17 +719,28 @@ void zfcp_dbf_san_in_els(char *tag, struct zfcp_fsf_req *fsf)struct zfcp_dbf *dbf = fsf->adapter->dbf;struct fsf_status_read_buffer *srb = (struct fsf_status_read_buffer *) fsf->data; -u16 length; +u16 length, cap_length;struct scatterlist sg; +const u32 pay_offset = offsetof(struct fsf_status_read_buffer , payload);if (unlikely (!debug_level_enabled (dbf->san,ZFCP_DBF_SAN_LEVEL )))return ; - length = (u16)(srb->length - - offsetof(struct fsf_status_read_buffer , payload)); -sg_init_one (&sg, srb->payload.data, length); -zfcp_dbf_san (tag, dbf,"san_els" , &sg,ZFCP_DBF_SAN_ELS , length, - fsf->req_id,ntoh24 (srb->d_id), length); +/* +* Store the original channel-reported SRB length in pl_len. +* cap_length specifies the number of payload bytes that may be read. +*/ + length = (u16 )srb->length; + cap_length =0 ; +if (srb->length > pay_offset) { +/* Payload present beyond the fixed header. */ + cap_length = (u16)(srb->length - pay_offset); +sg_init_one (&sg, srb->payload.data, cap_length); + } + +zfcp_dbf_san (tag, dbf,"san_els" , cap_length ? &sg :NULL , +ZFCP_DBF_SAN_ELS , length, fsf->req_id, +ntoh24 (srb->d_id), cap_length); }/**
drivers/s390/scsi/zfcp_dbf.h +1 -1
@@ -310,7 +310,7 @@ struct zfcp_dbf_pay {u8 counter;char area[ZFCP_DBF_TAG_LEN ];u64 fsf_req_id; -#define ZFCP_DBF_PAY_MAX_REC 0x100 +#define ZFCP_DBF_PAY_MAX_REC 4032char data[ZFCP_DBF_PAY_MAX_REC ]; } __packed;
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] scsi: zfcp: redundant trace records generated due to srb->length semantics change - [Low] scsi…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] scsi: zfcp: redundant trace records generated due to srb->length semantics change
- [Low] scsi: zfcp: 32-to-16 bit truncation breaks trace ambiguity goal for large anomalous lengths --
commit 461d7fe95c4b53bf3dfb0dc33b64ab47c7c0b0f4
Author: Ajaykumar Rajappa <[email protected]>
zfcp: Fix integer underflow in status read buffer payload length
This patch fixes an integer underflow when calculating the payload length of a status read buffer in zfcp_dbf_hba_fsf_uss() and zfcp_dbf_san_in_els(). It validates payload access against the payload offset before reading payload bytes, while keeping the serviceability trace records intact.
Quoted 4 lines
drivers/s390/scsi/zfcp_dbf.c +0
-0
[ ... ]
drivers/s390/scsi/zfcp_dbf.c void zfcp_dbf_hba_fsf_uss(char *tag, struct zfcp_fsf_req *req) · line 262
Quoted 13 lines
drivers/s390/scsi/zfcp_dbf.c +6
-3@@ -253,11 +254,15 @@ void zfcp_dbf_hba_fsf_uss(char *tag, struct zfcp_fsf_req *req) rec->u.uss.s_id =ntoh24 (srb->s_id);memcpy (&rec->u.uss.res4, &srb->res4,sizeof (rec->u.uss.res4)); -/* status read buffer payload length */ - rec->pl_len = (!srb->length) ?0 : srb->length - - offsetof(struct fsf_status_read_buffer , payload); +/* Unconditionally store value-shifted SRB length: pl_len is a bijective +* map of srb->length (pl_len + pay_offset == srb->length). A bogus +* srb->length < pay_offset wraps to a large non-zero pl_len, which is +* unambiguously distinguishable from pl_len == 0(genuine empty payload) +*/ + rec->pl_len = (u16)(srb->length - pay_offset);
[Severity: Low]
Could this 16-bit truncation break the trace ambiguity goal for large anomalous lengths?
The inline comment claims this creates a "bijective map" that makes anomalous lengths unambiguously distinguishable from a genuine empty payload. However, srb->length is a 32-bit field, whereas rec->pl_len is a 16-bit field.
If the hardware provides an anomalously large length (for example, srb->length == 65536 + pay_offset), the subtraction yields 65536, which truncates to exactly 0 when cast to u16.
Would this map the anomalous length to exactly 0, making it entirely indistinguishable from a genuine empty payload and mathematically violating the stated tracing design goal?
[ ... ]
Quoted 22 lines
drivers/s390/scsi/zfcp_dbf.c +7
-6@@ -714,17 +719,28 @@ void zfcp_dbf_san_in_els(char *tag, struct zfcp_fsf_req *fsf)struct zfcp_dbf *dbf = fsf->adapter->dbf;struct fsf_status_read_buffer *srb = (struct fsf_status_read_buffer *) fsf->data; -u16 length; +u16 length, cap_length;struct scatterlist sg; +const u32 pay_offset = offsetof(struct fsf_status_read_buffer , payload);if (unlikely (!debug_level_enabled (dbf->san,ZFCP_DBF_SAN_LEVEL )))return ; - length = (u16)(srb->length - - offsetof(struct fsf_status_read_buffer , payload)); -sg_init_one (&sg, srb->payload.data, length); -zfcp_dbf_san (tag, dbf,"san_els" , &sg,ZFCP_DBF_SAN_ELS , length, - fsf->req_id,ntoh24 (srb->d_id), length); +/* +* Store the original channel-reported SRB length in pl_len. +* cap_length specifies the number of payload bytes that may be read. +*/ + length = (u16 )srb->length;
drivers/s390/scsi/zfcp_dbf.c void zfcp_dbf_san_in_els(char *tag, struct zfcp_fsf_req *fsf) · line 743
Quoted 10 lines
drivers/s390/scsi/zfcp_dbf.c +10
-0+ cap_length =0 ; +if (srb->length > pay_offset) { +/* Payload present beyond the fixed header. */ + cap_length = (u16)(srb->length - pay_offset); +sg_init_one (&sg, srb->payload.data, cap_length); + } + +zfcp_dbf_san (tag, dbf,"san_els" , cap_length ? &sg :NULL , +ZFCP_DBF_SAN_ELS , length, fsf->req_id, +ntoh24 (srb->d_id), cap_length);
[Severity: Low]
Does passing the full srb->length to zfcp_dbf_san() break the length optimization check inside the trace function?
Inside zfcp_dbf_san(), this length argument is compared against a maximum inline payload size (ZFCP_DBF_SAN_MAX_PAYLOAD, 48 bytes) to optimize tracing by determining if a separate payload trace record can be skipped:
if (len <= rec_len)
goto out;Because the full srb->length includes the 60-byte fixed header (pay_offset), length will always be at least 60 for any valid incoming ELS request.
Consequently, the check length <= rec_len will always evaluate to false.
Can this lead to unconditional and redundant generation of payload trace records for all payloads, needlessly duplicating data and wasting trace buffer space?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1