target: Fix race for SCF_COMPARE_AND_WRITE_POST checking
authorNicholas Bellinger <nab@linux-iscsi.org>
Fri, 6 Nov 2015 07:37:59 +0000 (23:37 -0800)
committerNicholas Bellinger <nab@linux-iscsi.org>
Sun, 29 Nov 2015 03:33:15 +0000 (19:33 -0800)
commit057085e522f8bf94c2e691a5b76880f68060f8ba
treed813e53ee7853a19eb2697e71bd2851b8acc5cc6
parentca82c2bded29b38d36140bfa1e76a7bbfcade390
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>
drivers/target/target_core_sbc.c
drivers/target/target_core_transport.c
include/target/target_core_base.h