Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

target: Fix race for SCF_COMPARE_AND_WRITE_POST checking

This patch addresses a race + use after free where the first
stage of COMPARE_AND_WRITE in compare_and_write_callback()
is rescheduled after the backend sends the secondary WRITE,
resulting in second stage compare_and_write_post() callback
completing in target_complete_ok_work() before the first
can return.

Because current code depends on checking se_cmd->se_cmd_flags
after return from se_cmd->transport_complete_callback(),
this results in first stage having SCF_COMPARE_AND_WRITE_POST
set, which incorrectly falls through into second stage CAW
processing code, eventually triggering a NULL pointer
dereference due to use after free.

To address this bug, pass in a new *post_ret parameter into
se_cmd->transport_complete_callback(), and depend upon this
value instead of ->se_cmd_flags to determine when to return
or fall through into ->queue_status() code for CAW.

Cc: Sagi Grimberg <sagig@mellanox.com>
Cc: <stable@vger.kernel.org> # v3.12+
Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>

+18 -11
+9 -4
drivers/target/target_core_sbc.c
··· 371 371 return 0; 372 372 } 373 373 374 - static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success) 374 + static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success, 375 + int *post_ret) 375 376 { 376 377 unsigned char *buf, *addr; 377 378 struct scatterlist *sg; ··· 438 437 cmd->data_direction); 439 438 } 440 439 441 - static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success) 440 + static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success, 441 + int *post_ret) 442 442 { 443 443 struct se_device *dev = cmd->se_dev; 444 444 ··· 449 447 * sent to the backend driver. 450 448 */ 451 449 spin_lock_irq(&cmd->t_state_lock); 452 - if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status) 450 + if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status) { 453 451 cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST; 452 + *post_ret = 1; 453 + } 454 454 spin_unlock_irq(&cmd->t_state_lock); 455 455 456 456 /* ··· 464 460 return TCM_NO_SENSE; 465 461 } 466 462 467 - static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success) 463 + static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success, 464 + int *post_ret) 468 465 { 469 466 struct se_device *dev = cmd->se_dev; 470 467 struct scatterlist *write_sg = NULL, *sg;
+8 -6
drivers/target/target_core_transport.c
··· 1658 1658 void transport_generic_request_failure(struct se_cmd *cmd, 1659 1659 sense_reason_t sense_reason) 1660 1660 { 1661 - int ret = 0; 1661 + int ret = 0, post_ret = 0; 1662 1662 1663 1663 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08llx" 1664 1664 " CDB: 0x%02x\n", cmd, cmd->tag, cmd->t_task_cdb[0]); ··· 1680 1680 */ 1681 1681 if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) && 1682 1682 cmd->transport_complete_callback) 1683 - cmd->transport_complete_callback(cmd, false); 1683 + cmd->transport_complete_callback(cmd, false, &post_ret); 1684 1684 1685 1685 switch (sense_reason) { 1686 1686 case TCM_NON_EXISTENT_LUN: ··· 2068 2068 */ 2069 2069 if (cmd->transport_complete_callback) { 2070 2070 sense_reason_t rc; 2071 + bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE); 2072 + bool zero_dl = !(cmd->data_length); 2073 + int post_ret = 0; 2071 2074 2072 - rc = cmd->transport_complete_callback(cmd, true); 2073 - if (!rc && !(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE_POST)) { 2074 - if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) && 2075 - !cmd->data_length) 2075 + rc = cmd->transport_complete_callback(cmd, true, &post_ret); 2076 + if (!rc && !post_ret) { 2077 + if (caw && zero_dl) 2076 2078 goto queue_rsp; 2077 2079 2078 2080 return;
+1 -1
include/target/target_core_base.h
··· 474 474 struct completion cmd_wait_comp; 475 475 const struct target_core_fabric_ops *se_tfo; 476 476 sense_reason_t (*execute_cmd)(struct se_cmd *); 477 - sense_reason_t (*transport_complete_callback)(struct se_cmd *, bool); 477 + sense_reason_t (*transport_complete_callback)(struct se_cmd *, bool, int *); 478 478 void *protocol_data; 479 479 480 480 unsigned char *t_task_cdb;