Skip to content

Commit 79d59d0

Browse files
rolanddNicholas Bellinger
authored andcommitted
iscsi-target: Fix wrong buffer / buffer overrun in iscsi_change_param_value()
In non-leading connection login, iscsi_login_non_zero_tsih_s1() calls iscsi_change_param_value() with the buffer it uses to hold the login PDU, not a temporary buffer. This leads to the login header getting corrupted and login failing for non-leading connections in MC/S. Fix this by adding a wrapper iscsi_change_param_sprintf() that handles the temporary buffer itself to avoid confusion. Also handle sending a reject in case of failure in the wrapper, which lets the calling code get quite a bit smaller and easier to read. Finally, bump the size of the temporary buffer from 32 to 64 bytes to be safe, since "MaxRecvDataSegmentLength=" by itself is 25 bytes; with a trailing NUL, a value >= 1M will lead to a buffer overrun. (This isn't the default but we don't need to run right at the ragged edge here) Reported-by: Santosh Kulkarni <santosh.kulkarni@calsoftinc.com> Signed-off-by: Roland Dreier <roland@purestorage.com> Cc: stable@vger.kernel.org # 3.10+ Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent 6cc44a6 commit 79d59d0

1 file changed

Lines changed: 31 additions & 39 deletions

File tree

‎drivers/target/iscsi/iscsi_target_login.c‎

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,28 @@ static void iscsi_login_set_conn_values(
249249
mutex_unlock(&auth_id_lock);
250250
}
251251

252+
static __printf(2, 3) int iscsi_change_param_sprintf(
253+
struct iscsi_conn *conn,
254+
const char *fmt, ...)
255+
{
256+
va_list args;
257+
unsigned char buf[64];
258+
259+
memset(buf, 0, sizeof buf);
260+
261+
va_start(args, fmt);
262+
vsnprintf(buf, sizeof buf, fmt, args);
263+
va_end(args);
264+
265+
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
266+
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
267+
ISCSI_LOGIN_STATUS_NO_RESOURCES);
268+
return -1;
269+
}
270+
271+
return 0;
272+
}
273+
252274
/*
253275
* This is the leading connection of a new session,
254276
* or session reinstatement.
@@ -339,7 +361,6 @@ static int iscsi_login_zero_tsih_s2(
339361
{
340362
struct iscsi_node_attrib *na;
341363
struct iscsi_session *sess = conn->sess;
342-
unsigned char buf[32];
343364
bool iser = false;
344365

345366
sess->tpg = conn->tpg;
@@ -380,26 +401,16 @@ static int iscsi_login_zero_tsih_s2(
380401
*
381402
* In our case, we have already located the struct iscsi_tiqn at this point.
382403
*/
383-
memset(buf, 0, 32);
384-
sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
385-
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
386-
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
387-
ISCSI_LOGIN_STATUS_NO_RESOURCES);
404+
if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
388405
return -1;
389-
}
390406

391407
/*
392408
* Workaround for Initiators that have broken connection recovery logic.
393409
*
394410
* "We would really like to get rid of this." Linux-iSCSI.org team
395411
*/
396-
memset(buf, 0, 32);
397-
sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
398-
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
399-
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
400-
ISCSI_LOGIN_STATUS_NO_RESOURCES);
412+
if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
401413
return -1;
402-
}
403414

404415
if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
405416
return -1;
@@ -411,12 +422,9 @@ static int iscsi_login_zero_tsih_s2(
411422
unsigned long mrdsl, off;
412423
int rc;
413424

414-
sprintf(buf, "RDMAExtensions=Yes");
415-
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
416-
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
417-
ISCSI_LOGIN_STATUS_NO_RESOURCES);
425+
if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
418426
return -1;
419-
}
427+
420428
/*
421429
* Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
422430
* Immediate Data + Unsolicitied Data-OUT if necessary..
@@ -446,12 +454,8 @@ static int iscsi_login_zero_tsih_s2(
446454
pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
447455
" to PAGE_SIZE\n", mrdsl);
448456

449-
sprintf(buf, "MaxRecvDataSegmentLength=%lu\n", mrdsl);
450-
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
451-
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
452-
ISCSI_LOGIN_STATUS_NO_RESOURCES);
457+
if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
453458
return -1;
454-
}
455459
/*
456460
* ISER currently requires that ImmediateData + Unsolicited
457461
* Data be disabled when protection / signature MRs are enabled.
@@ -461,19 +465,12 @@ static int iscsi_login_zero_tsih_s2(
461465
(TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
462466
TARGET_PROT_DOUT_INSERT)) {
463467

464-
sprintf(buf, "ImmediateData=No");
465-
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
466-
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
467-
ISCSI_LOGIN_STATUS_NO_RESOURCES);
468+
if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
468469
return -1;
469-
}
470470

471-
sprintf(buf, "InitialR2T=Yes");
472-
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
473-
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
474-
ISCSI_LOGIN_STATUS_NO_RESOURCES);
471+
if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
475472
return -1;
476-
}
473+
477474
pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
478475
" T10-PI enabled ISER session\n");
479476
}
@@ -618,13 +615,8 @@ static int iscsi_login_non_zero_tsih_s2(
618615
*
619616
* In our case, we have already located the struct iscsi_tiqn at this point.
620617
*/
621-
memset(buf, 0, 32);
622-
sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
623-
if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
624-
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
625-
ISCSI_LOGIN_STATUS_NO_RESOURCES);
618+
if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
626619
return -1;
627-
}
628620

629621
return iscsi_login_disable_FIM_keys(conn->param_list, conn);
630622
}

0 commit comments

Comments
 (0)