public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Douglas Gilbert <dgilbert@interlog.com>
To: Calvin Owens <calvinowens@fb.com>,
	"James E.J. Bottomley" <JBottomley@odin.com>
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel-team@fb.com
Subject: Re: [PATCH] sg: Fix double-free when drives detach during SG_IO
Date: Mon, 2 Nov 2015 21:57:35 +0100	[thread overview]
Message-ID: <5637CE3F.6020706@interlog.com> (raw)
In-Reply-To: <944215ec334c3c5b3af98210acf8f6479f5539c8.1445634103.git.calvinowens@fb.com>

On 15-10-31 12:57 AM, Calvin Owens wrote:
> In sg_common_write(), we free the block request and return -ENODEV if
> the device is detached in the middle of the SG_IO ioctl().
>
> Unfortunately, sg_finish_rem_req() also tries to free srp->rq, so we
> end up freeing rq->cmd in the already free rq object, and then free
> the object itself out from under the current user.
>
> This ends up corrupting random memory via the list_head on the rq
> object. The most common crash trace I saw is this:
>
>    ------------[ cut here ]------------
>    kernel BUG at block/blk-core.c:1420!
>    Call Trace:
>    [<ffffffff81281eab>] blk_put_request+0x5b/0x80
>    [<ffffffffa0069e5b>] sg_finish_rem_req+0x6b/0x120 [sg]
>    [<ffffffffa006bcb9>] sg_common_write.isra.14+0x459/0x5a0 [sg]
>    [<ffffffff8125b328>] ? selinux_file_alloc_security+0x48/0x70
>    [<ffffffffa006bf95>] sg_new_write.isra.17+0x195/0x2d0 [sg]
>    [<ffffffffa006cef4>] sg_ioctl+0x644/0xdb0 [sg]
>    [<ffffffff81170f80>] do_vfs_ioctl+0x90/0x520
>    [<ffffffff81258967>] ? file_has_perm+0x97/0xb0
>    [<ffffffff811714a1>] SyS_ioctl+0x91/0xb0
>    [<ffffffff81602afb>] tracesys+0xdd/0xe2
>      RIP [<ffffffff81281e04>] __blk_put_request+0x154/0x1a0
>
> The solution is straightforward: just set srp->rq to NULL in the
> failure branch so that sg_finish_rem_req() doesn't attempt to re-free
> it.
>
> Additionally, since sg_rq_end_io() will never be called on the object
> when this happens, we need to free memory backing ->cmd if it isn't
> embedded in the object itself.
>
> KASAN was extremely helpful in finding the root cause of this bug.
>
> Signed-off-by: Calvin Owens <calvinowens@fb.com>

Acked-by: Douglas Gilbert <dgilbert@interlog.com>

Thanks.

> ---
>   drivers/scsi/sg.c | 8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
> index 9d7b7db..503ab8b 100644
> --- a/drivers/scsi/sg.c
> +++ b/drivers/scsi/sg.c
> @@ -787,8 +787,14 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
>   		return k;	/* probably out of space --> ENOMEM */
>   	}
>   	if (atomic_read(&sdp->detaching)) {
> -		if (srp->bio)
> +		if (srp->bio) {
> +			if (srp->rq->cmd != srp->rq->__cmd)
> +				kfree(srp->rq->cmd);
> +
>   			blk_end_request_all(srp->rq, -EIO);
> +			srp->rq = NULL;
> +		}
> +
>   		sg_finish_rem_req(srp);
>   		return -ENODEV;
>   	}
>


  reply	other threads:[~2015-11-02 21:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-30 23:57 [PATCH] sg: Fix double-free when drives detach during SG_IO Calvin Owens
2015-11-02 20:57 ` Douglas Gilbert [this message]
2015-11-03  4:52   ` Martin K. Petersen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5637CE3F.6020706@interlog.com \
    --to=dgilbert@interlog.com \
    --cc=JBottomley@odin.com \
    --cc=calvinowens@fb.com \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox