mirror-linux/drivers/target/iscsi
Alexandru Hossu 85db739131 scsi: target: iscsi: Validate CHAP_R length before base64 decode
chap_server_compute_hash() allocates client_digest as
kzalloc(chap->digest_size) and then, for BASE64-encoded responses,
passes chap_r directly to chap_base64_decode() without checking whether
the input length could produce more than digest_size bytes of output.

chap_base64_decode() writes to the destination unconditionally as long
as there is input to consume. With MAX_RESPONSE_LENGTH set to 128 and
the "0b" prefix stripped by extract_param(), up to 127 base64 characters
can reach the decoder. 127 characters decode to 95 bytes. For SHA-256
(digest_size=32) this overflows client_digest by 63 bytes; for MD5
(digest_size=16) the overflow is 79 bytes.

The length check at line 344 fires after the write has already happened.

The HEX branch in the same switch statement already validates the length
up front. Apply the same approach to the BASE64 branch: strip trailing
base64 padding characters, then reject any input whose data length
exceeds DIV_ROUND_UP(digest_size * 4, 3) before calling the decoder.

Stripping trailing '=' before the comparison handles both padded and
unpadded encodings. chap_base64_decode() already returns early on '=',
so the full original string is still passed to the decoder unchanged.

The mutual CHAP path decodes CHAP_C into initiatorchg_binhex, which is
kzalloc(CHAP_CHALLENGE_STR_LEN). extract_param() caps initiatorchg at
CHAP_CHALLENGE_STR_LEN characters, so at most CHAP_CHALLENGE_STR_LEN-1
base64 characters reach the decoder. The maximum decoded size,
DIV_ROUND_UP((CHAP_CHALLENGE_STR_LEN-1) * 3, 4), is less than
CHAP_CHALLENGE_STR_LEN, so no overflow is possible there. A comment is
added at the call site to document this.

Fixes: 1e57338834 ("scsi: target: iscsi: Support base64 in CHAP")
Cc: stable@vger.kernel.org
Signed-off-by: Alexandru Hossu <hossu.alexandru@gmail.com>
Reviewed-by: David Disseldorp <ddiss@suse.de>
Link: https://patch.msgid.link/20260521151121.808477-1-hossu.alexandru@gmail.com
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2026-05-22 23:06:00 -04:00
..
cxgbit Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
Kconfig scsi: target: iscsi: switch to using the crc32c library 2024-12-01 17:23:02 -08:00
Makefile
iscsi_target.c scsi: target: iscsi: Fix CRC overread and double-free in iscsit_handle_text_cmd() 2026-05-22 22:24:16 -04:00
iscsi_target.h scsi: target: Remove unused functions 2025-01-02 13:19:34 -05:00
iscsi_target_auth.c scsi: target: iscsi: Validate CHAP_R length before base64 decode 2026-05-22 23:06:00 -04:00
iscsi_target_auth.h
iscsi_target_configfs.c scsi: target: Use driver completion preference by default 2026-02-28 21:04:02 -05:00
iscsi_target_datain_values.c
iscsi_target_datain_values.h
iscsi_target_device.c
iscsi_target_device.h
iscsi_target_erl0.c treewide, timers: Rename from_timer() to timer_container_of() 2025-06-08 09:07:37 +02:00
iscsi_target_erl0.h
iscsi_target_erl1.c treewide, timers: Rename from_timer() to timer_container_of() 2025-06-08 09:07:37 +02:00
iscsi_target_erl1.h
iscsi_target_erl2.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
iscsi_target_erl2.h scsi: target: Remove unused functions 2025-01-02 13:19:34 -05:00
iscsi_target_login.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
iscsi_target_login.h scsi: target: iscsi: switch to using the crc32c library 2024-12-01 17:23:02 -08:00
iscsi_target_nego.c scsi: target: iscsi: Bound iscsi_encode_text_output() appends to rsp_buf 2026-05-22 23:04:36 -04:00
iscsi_target_nego.h scsi: target: Remove unused declarations 2024-08-22 21:15:21 -04:00
iscsi_target_nodeattrib.c
iscsi_target_nodeattrib.h
iscsi_target_parameters.c scsi: target: iscsi: Bound iscsi_encode_text_output() appends to rsp_buf 2026-05-22 23:04:36 -04:00
iscsi_target_parameters.h scsi: target: iscsi: Bound iscsi_encode_text_output() appends to rsp_buf 2026-05-22 23:04:36 -04:00
iscsi_target_seq_pdu_list.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
iscsi_target_seq_pdu_list.h
iscsi_target_stat.c
iscsi_target_tmr.c scsi: target: iscsi: Use int type to store negative value 2025-09-09 22:05:36 -04:00
iscsi_target_tmr.h
iscsi_target_tpg.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
iscsi_target_tpg.h scsi: target: Remove unused functions 2025-01-02 13:19:34 -05:00
iscsi_target_transport.c
iscsi_target_util.c scsi: target: iscsi: Fix use-after-free in iscsit_dec_session_usage_count() 2026-01-16 23:02:15 -05:00
iscsi_target_util.h scsi: target: Remove unused functions 2025-01-02 13:19:34 -05:00