All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: Paolo Valente <paolo.valente@linaro.org>
Cc: Kees Cook <keescook@chromium.org>,
	Oleksandr Natalenko <oleksandr@natalenko.name>,
	Bart Van Assche <bart.vanassche@wdc.com>,
	David Windsor <dave@nullcore.net>,
	"James E.J. Bottomley" <jejb@linux.vnet.ibm.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>,
	linux-scsi@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
	Christoph Hellwig <hch@lst.de>, Hannes Reinecke <hare@suse.com>,
	Johannes Thumshirn <jthumshirn@suse.de>,
	linux-block@vger.kernel.org
Subject: Re: usercopy whitelist woe in scsi_sense_cache
Date: Wed, 18 Apr 2018 08:30:35 -0600	[thread overview]
Message-ID: <c30400f9-50e6-b5cf-d06b-a5e971e98546@kernel.dk> (raw)
In-Reply-To: <011EF7D1-B095-4B8D-AD2A-993048932C49@linaro.org>

On 4/18/18 3:08 AM, Paolo Valente wrote:
> 
> 
>> Il giorno 18 apr 2018, alle ore 00:57, Jens Axboe <axboe@kernel.dk> ha scritto:
>>
>> On 4/17/18 3:48 PM, Jens Axboe wrote:
>>> On 4/17/18 3:47 PM, Kees Cook wrote:
>>>> On Tue, Apr 17, 2018 at 2:39 PM, Jens Axboe <axboe@kernel.dk> wrote:
>>>>> On 4/17/18 3:25 PM, Kees Cook wrote:
>>>>>> On Tue, Apr 17, 2018 at 1:46 PM, Kees Cook <keescook@chromium.org> wrote:
>>>>>>> I see elv.priv[1] assignments made in a few places -- is it possible
>>>>>>> there is some kind of uninitialized-but-not-NULL state that can leak
>>>>>>> in there?
>>>>>>
>>>>>> Got it. This fixes it for me:
>>>>>>
>>>>>> diff --git a/block/blk-mq.c b/block/blk-mq.c
>>>>>> index 0dc9e341c2a7..859df3160303 100644
>>>>>> --- a/block/blk-mq.c
>>>>>> +++ b/block/blk-mq.c
>>>>>> @@ -363,7 +363,7 @@ static struct request *blk_mq_get_request(struct
>>>>>> request_queue *q,
>>>>>>
>>>>>>        rq = blk_mq_rq_ctx_init(data, tag, op);
>>>>>>        if (!op_is_flush(op)) {
>>>>>> -               rq->elv.icq = NULL;
>>>>>> +               memset(&rq->elv, 0, sizeof(rq->elv));
>>>>>>                if (e && e->type->ops.mq.prepare_request) {
>>>>>>                        if (e->type->icq_cache && rq_ioc(bio))
>>>>>>                                blk_mq_sched_assign_ioc(rq, bio);
>>>>>> @@ -461,7 +461,7 @@ void blk_mq_free_request(struct request *rq)
>>>>>>                        e->type->ops.mq.finish_request(rq);
>>>>>>                if (rq->elv.icq) {
>>>>>>                        put_io_context(rq->elv.icq->ioc);
>>>>>> -                       rq->elv.icq = NULL;
>>>>>> +                       memset(&rq->elv, 0, sizeof(rq->elv));
>>>>>>                }
>>>>>>        }
>>>>>
>>>>> This looks like a BFQ problem, this should not be necessary. Paolo,
>>>>> you're calling your own prepare request handler from the insert
>>>>> as well, and your prepare request does nothing if rq->elv.icq == NULL.
>>>>
>>>> I sent the patch anyway, since it's kind of a robustness improvement,
>>>> I'd hope. If you fix BFQ also, please add:
>>>
>>> It's also a memset() in the hot path, would prefer to avoid that...
>>> The issue here is really the convoluted bfq usage of insert/prepare,
>>> I'm sure Paolo can take it from here.
>>
> 
> Hi,
> I'm very sorry for tuning in very late, but, at the same time, very
> glad to find the problem probably already solved ;) (in this respect, I swear,
> my delay was not intentional)
> 
>> Does this fix it?
>>
>> diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c
>> index f0ecd98509d8..d883469a1582 100644
>> --- a/block/bfq-iosched.c
>> +++ b/block/bfq-iosched.c
>> @@ -4934,8 +4934,11 @@ static void bfq_prepare_request(struct request *rq, struct bio *bio)
>> 	bool new_queue = false;
>> 	bool bfqq_already_existing = false, split = false;
>>
>> -	if (!rq->elv.icq)
>> +	if (!rq->elv.icq) {
>> +		rq->elv.priv[0] = rq->elv.priv[1] = NULL;
>> 		return;
>> +	}
>> +
> 
> This does solve the problem at hand.  But it also arouses a question,
> related to a possible subtle bug.
> 
> For BFQ, !rq->elv.icq basically means "this request is not for me, as
> I am an icq-based scheduler".  But, IIUC the main points in this
> thread, then this assumption is false.  If it is actually false, then
> I hope that all requests with !rq->elv.icq that are sent to BFQ do
> verify the condition (at_head || blk_rq_is_passthrough(rq)).  In fact,
> requests that do not verify that condition are those that BFQ must put
> in a bfq_queue.  So, even if this patch makes the crash disappear, we
> drive BFQ completely crazy (and we may expect other strange failures)
> if we send BFQ a request with !((at_head || blk_rq_is_passthrough(rq))
> and !rq->elv.icq.  BFQ has to put that rq into a bfq_queue, but simply
> cannot.
> 
> Jens, or any other, could you please shed a light on this, and explain
> how things are exactly?

Your assumption is correct, however you set ->priv[0] and ->priv[1] for
requests, but only for ->elv.icq != NULL. So let's assume you get a
request and assign those two, request completes. Later on, you get
the same request, bypass insert it. BFQ doesn't clear the bic/bfqq
pointers in the request, since ->elv.icq == NULL. It gets inserted
into the dispatch list.

Then when __bfq_dispatch_request() is called, you do:

bfqq = RQ_BFQQ(rq);
if (bfqq)
	bfqq->dispatched++;
[...]

which is wrong, since you don't know if you assigned a bfqq for this
request. The memory that bfqq points to could be long gone, if that
queue is freed. So you could either guard any bfqq/bic retrieval
with ->elv.icq != NULL, or you could just clear the pointers for
the case where the values aren't valid.

-- 
Jens Axboe

  reply	other threads:[~2018-04-18 14:30 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-04 19:07 usercopy whitelist woe in scsi_sense_cache Oleksandr Natalenko
2018-04-04 20:21 ` Kees Cook
2018-04-04 20:44   ` Douglas Gilbert
2018-04-04 20:49   ` Oleksandr Natalenko
2018-04-04 21:25     ` Kees Cook
2018-04-04 21:34       ` Oleksandr Natalenko
2018-04-05  9:56       ` Oleksandr Natalenko
2018-04-05 14:21         ` Kees Cook
2018-04-05 14:32           ` Oleksandr Natalenko
2018-04-05 14:33             ` Oleksandr Natalenko
     [not found]               ` <CAGXu5jL8oLV2xvjBVYv_SNXr74LdgpXEmU7K+cLYpD7jh2chgw@mail.gmail.com>
2018-04-05 18:52                 ` Kees Cook
2018-04-06  6:21                   ` Oleksandr Natalenko
2018-04-08 19:07                   ` Oleksandr Natalenko
2018-04-09  9:35                     ` Christoph Hellwig
2018-04-09 15:54                       ` Oleksandr Natalenko
2018-04-09 18:32                     ` Kees Cook
2018-04-09 19:02                       ` Oleksandr Natalenko
2018-04-09 20:30                         ` Kees Cook
2018-04-09 23:03                           ` Kees Cook
2018-04-10  6:35                           ` Oleksandr Natalenko
2018-04-10  6:53                             ` Kees Cook
2018-04-10 17:16                               ` Oleksandr Natalenko
2018-04-11  3:13                                 ` Kees Cook
2018-04-11 22:47                                   ` Kees Cook
2018-04-12  0:03                                     ` Kees Cook
2018-04-12 18:44                                       ` Kees Cook
2018-04-12 19:04                                         ` Oleksandr Natalenko
2018-04-12 19:04                                           ` Oleksandr Natalenko
2018-04-12 22:01                                           ` Kees Cook
2018-04-12 22:01                                             ` Kees Cook
2018-04-12 22:47                                             ` Kees Cook
2018-04-12 22:47                                               ` Kees Cook
2018-04-13  3:02                                               ` Kees Cook
2018-04-16 20:44                                                 ` Kees Cook
2018-04-17  3:12                                                   ` Kees Cook
2018-04-17  9:19                                                     ` Oleksandr Natalenko
2018-04-17 16:25                                                       ` Kees Cook
2018-04-17 10:02                                                     ` James Bottomley
2018-04-17 16:30                                                       ` Kees Cook
2018-04-17 16:42                                                     ` Kees Cook
2018-04-17 16:46                                                       ` Jens Axboe
2018-04-17 20:03                                                     ` Kees Cook
2018-04-17 20:20                                                       ` Kees Cook
2018-04-17 20:25                                                         ` Kees Cook
2018-04-17 20:28                                                           ` Jens Axboe
2018-04-17 20:28                                                             ` Jens Axboe
2018-04-17 20:46                                                             ` Kees Cook
2018-04-17 21:25                                                               ` Kees Cook
2018-04-17 21:39                                                                 ` Jens Axboe
2018-04-17 21:47                                                                   ` Kees Cook
2018-04-17 21:48                                                                     ` Jens Axboe
2018-04-17 22:57                                                                       ` Jens Axboe
2018-04-17 23:06                                                                         ` Kees Cook
2018-04-17 23:12                                                                           ` Jens Axboe
2018-04-18  9:08                                                                         ` Paolo Valente
2018-04-18  9:08                                                                           ` Paolo Valente
2018-04-18 14:30                                                                           ` Jens Axboe [this message]
2018-04-19  9:32                                                                             ` Paolo Valente
2018-04-19  9:32                                                                               ` Paolo Valente
2018-04-20 20:23                                                                               ` Kees Cook
2018-04-20 20:41                                                                                 ` Oleksandr Natalenko
2018-04-21  8:43                                                                                 ` Paolo Valente
2018-04-21  8:43                                                                                   ` Paolo Valente
2018-04-17 21:55                                                                     ` Oleksandr Natalenko
2018-04-10 13:47                             ` Oleksandr Natalenko
2018-04-04 20:32 ` Kees Cook
2018-04-04 20:47   ` Douglas Gilbert

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=c30400f9-50e6-b5cf-d06b-a5e971e98546@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=bart.vanassche@wdc.com \
    --cc=dave@nullcore.net \
    --cc=hare@suse.com \
    --cc=hch@lst.de \
    --cc=jejb@linux.vnet.ibm.com \
    --cc=jthumshirn@suse.de \
    --cc=keescook@chromium.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=oleksandr@natalenko.name \
    --cc=paolo.valente@linaro.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.