iscsi-target: Don't use stack buffer for scatterlist
PATCH v3 1/1 iscsi-target: Don't use stack buffer for scatterlist
Fedora got a bug report of a crash with iSCSI:
kernel BUG at include/linux/scatterlist.h:143!
...
RIP: 0010:iscsit_do_crypto_hash_buf+0x154/0x180 [iscsi_target_mod] ... Call Trace: ? iscsi_target_tx_thread+0x200/0x200 [iscsi_target_mod] iscsit_get_rx_pdu+0x4cd/0xa90 [iscsi_target_mod] ? native_sched_clock+0x3e/0xa0 ? iscsi_target_tx_thread+0x200/0x200 [iscsi_target_mod] iscsi_target_rx_thread+0x81/0xf0 [iscsi_target_mod] kthread+0x120/0x140 ? kthread_create_worker_on_cpu+0x70/0x70 ret_from_fork+0x3a/0x50
This is a BUG_ON for using a stack buffer with a scatterlist. There are two cases that trigger this bug. Switch to using a dynamically allocated buffer for one case and do not assign a NULL buffer in another case.
---
v3: Switched to kcalloc to drop an extra memset call. I was asked offline about kmalloc vs. kmalloc_array and ended up asking about this at the security summit since I was there last week. The preferred style is kmalloc_array for all arrays, even those of one byte characters. I will also switch it if people feel really strongly about this or I've misunderstood the array guidelines.
--- drivers/target/iscsi/iscsi_target.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
drivers/target/iscsi/iscsi_target.c +14 -8
@@ -1416,7 +1416,8 @@ static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,sg_init_table (sg,ARRAY_SIZE (sg));sg_set_buf (sg, buf, payload_length); -sg_set_buf (sg +1 , pad_bytes, padding); +if (padding) +sg_set_buf (sg +1 , pad_bytes, padding);ahash_request_set_crypt (hash, sg, data_crc, payload_length + padding);
@@ -3910,10 +3911,14 @@ static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)static void iscsit_get_rx_pdu (struct iscsi_conn *conn) {int ret; -u8 buffer[ISCSI_HDR_LEN ], opcode; +u8 *buffer, opcode;u32 checksum =0 , digest =0 ;struct kvec iov; + buffer =kcalloc (ISCSI_HDR_LEN ,sizeof (*buffer),GFP_KERNEL ); +if (!buffer) +return ; +while (!kthread_should_stop ()) {/* * Ensure that both TX and RX per connection kthreads
@@ -3921,7 +3926,6 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn)*/ iscsit_thread_check_cpumask (conn, current,0 ); -memset (buffer,0 ,ISCSI_HDR_LEN );memset (&iov,0 ,sizeof (struct kvec )); iov.iov_base = buffer;
@@ -3930,7 +3934,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) ret =rx_data (conn, &iov,1 ,ISCSI_HDR_LEN );if (ret !=ISCSI_HDR_LEN ) {iscsit_rx_thread_wait_for_tcp (conn); -return ; +break ; }if (conn->conn_ops->HeaderDigest) {
@@ -3940,7 +3944,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) ret =rx_data (conn, &iov,1 ,ISCSI_CRC_LEN );if (ret !=ISCSI_CRC_LEN ) {iscsit_rx_thread_wait_for_tcp (conn); -return ; +break ; } iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
@@ -3964,7 +3968,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) }if (conn->conn_state ==TARG_CONN_STATE_IN_LOGOUT ) -return ; +break ; opcode = buffer[0 ] &ISCSI_OPCODE_MASK ;
@@ -3975,13 +3979,15 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) " while in Discovery Session, rejecting.\n", opcode);iscsit_add_reject (conn,ISCSI_REASON_PROTOCOL_ERROR , buffer); -return ; +break ; } ret =iscsi_target_rx_opcode (conn, buffer);if (ret <0 ) -return ; +break ; } + +kfree (buffer); }int iscsi_target_rx_thread (void *arg)
Signature · 1 line
2.17.1
Quoted 107 lines · Laura Abbott
Fedora got a bug report of a crash with iSCSI:
kernel BUG at
include/linux/scatterlist.h:143!
...RIP: 0010:iscsit_do_crypto_hash_buf+0x154/0x180 [iscsi_target_mod] ... Call Trace: ? iscsi_target_tx_thread+0x200/0x200 [iscsi_target_mod] iscsit_get_rx_pdu+0x4cd/0xa90 [iscsi_target_mod] ? native_sched_clock+0x3e/0xa0 ? iscsi_target_tx_thread+0x200/0x200 [iscsi_target_mod] iscsi_target_rx_thread+0x81/0xf0 [iscsi_target_mod] kthread+0x120/0x140 ? kthread_create_worker_on_cpu+0x70/0x70 ret_from_fork+0x3a/0x50This is a
BUG_ONfor using a stack buffer with a scatterlist. There are two cases that trigger this bug. Switch to using a dynamically allocated buffer for one case and do not assign a NULL buffer in another case.Signed-off-by Laura Abbott <[email protected]>---
v3: Switched to kcalloc to drop an extra memset call. I was asked offline about kmalloc vs.kmalloc_arrayand ended up asking about this at the security summit since I was there last week. The preferred style iskmalloc_arrayfor all arrays, even those of one byte characters. I will also switch it if people feel really strongly about this or I've misunderstood the array guidelines.--- drivers/target/iscsi/iscsi_target.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)drivers/target/iscsi/iscsi_target.c +11
-7@@ -1416,7 +1416,8 @@ static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,sg_init_table (sg,ARRAY_SIZE (sg));sg_set_buf (sg, buf, payload_length); -sg_set_buf (sg +1 , pad_bytes, padding); +if (padding) +sg_set_buf (sg +1 , pad_bytes, padding);ahash_request_set_crypt (hash, sg, data_crc, payload_length + padding);@@ -3910,10 +3911,14 @@ static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)static void iscsit_get_rx_pdu (struct iscsi_conn *conn) {int ret; -u8 buffer[ISCSI_HDR_LEN ], opcode; +u8 *buffer, opcode;u32 checksum =0 , digest =0 ;struct kvec iov; + buffer =kcalloc (ISCSI_HDR_LEN ,sizeof (*buffer),GFP_KERNEL ); +if (!buffer) +return ; +while (!kthread_should_stop ()) {/* * Ensure that both TX and RX per connection kthreads @@ -3921,7 +3926,6 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn)*/ iscsit_thread_check_cpumask (conn, current,0 ); -memset (buffer,0 ,ISCSI_HDR_LEN );memset (&iov,0 ,sizeof (struct kvec )); iov.iov_base = buffer;@@ -3930,7 +3934,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) ret =rx_data (conn, &iov,1 ,ISCSI_HDR_LEN );if (ret !=ISCSI_HDR_LEN ) {iscsit_rx_thread_wait_for_tcp (conn); -return ; +break ; }if (conn->conn_ops->HeaderDigest) {@@ -3940,7 +3944,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) ret =rx_data (conn, &iov,1 ,ISCSI_CRC_LEN );if (ret !=ISCSI_CRC_LEN ) {iscsit_rx_thread_wait_for_tcp (conn); -return ; +break ; } iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,@@ -3964,7 +3968,7 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) }if (conn->conn_state ==TARG_CONN_STATE_IN_LOGOUT ) -return ; +break ; opcode = buffer[0 ] &ISCSI_OPCODE_MASK ;@@ -3975,13 +3979,15 @@ static void iscsit_get_rx_pdu(struct iscsi_conn *conn) " while in Discovery Session, rejecting.\n", opcode);iscsit_add_reject (conn,ISCSI_REASON_PROTOCOL_ERROR , buffer); -return ; +break ; }
drivers/target/iscsi/iscsi_target.c static void iscsit_get_rx_pdu(struct iscsi_conn *conn) · line 3993
Quoted 10 lines
drivers/target/iscsi/iscsi_target.c +3
-1ret =iscsi_target_rx_opcode (conn, buffer);if (ret <0 ) -return ; +break ; } + +kfree (buffer); }int iscsi_target_rx_thread (void *arg)
Ok.
Martin K. Petersen
AppliedLaura,
Quoted 3 lines
There are two cases that trigger this bug. Switch to using a dynamically allocated buffer for one case and do not assign a NULL buffer in another case.
Applied to 4.20/scsi-queue, thank you!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering
Quoted 2 lines
Applied to 4.20/scsi-queue, thank you!
4.19/scsi-fixes, that is...
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering