Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2445977pxb; Fri, 17 Sep 2021 09:54:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7I9D8vrXhmLbOMOtGQ/amnkoBbC2fX3gcd8IRzt4zmGFuRdLKhM3UXKVySz6PeTbctt44 X-Received: by 2002:a92:cd8d:: with SMTP id r13mr8687472ilb.244.1631897688893; Fri, 17 Sep 2021 09:54:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631897688; cv=none; d=google.com; s=arc-20160816; b=jYWgB9qxirYT9m9x8bcYcuFxoNUT9iwu5KjXDANqBATR8TlGkFhXQjOaEVm9Hy4grR oQCCzE09QmejzFTxpW+vncwF77bkae05WKPf7oWklqNL27Or8vkPu3/7utU1rOOGUx+A ZJ1xSdNF52WNFNvfFyeOZswnQ/T4/vK3GjavH3s/vJov1av5gX9uQLtxhFci11nB4BhQ IO+QvWkbgvh4wVVMMqgjHk147gmtd3zXmI3nsVxI1Cap/CN2Y/Aj+4cPTmw5YlZAoi5X O72ITwdzYHG9l0wmAJH+TMrCUxsDMPbqXmWwiAQL7RcTw5zv5xvxCGOJUY+quZMzL4PS OAWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=NpjC2vhQxMNwcJykwWES5XKw9YQ1dBU9ER+zWax0kyA=; b=yZLqQltReyeThElnGpTmTWJnXbhrz2WskBxkqAGSlD5FJOhI4Yh5gEQEID9jvpeYYE fQ62mroZJkAe1dEV7k2jayblF0mjcgnOeKOUcR9+S7Mzoi3VVuE2MxnQleji6+iipQ7d 4B8h1yq5kxIoQJvAMi9sZsQkTu4mvTMm4x6cmYceGZx9TsRiNqF/n8AIiJzFcnsMkCE3 p0M6hALwgjB+Qw3iHMIGgnn+XkrC3VckDna/FigWWa3hpYjzJyb5Tn7cR3rFQhyCYIcA YorLyg6Y+adOYGTIxtJhN5gVjasJWPe+a2nMF50g7YYfUOeidMVlSJVLYR6UBRz1NArl V18w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="R6tlPCE/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g8si6205712ioq.76.2021.09.17.09.54.38; Fri, 17 Sep 2021 09:54:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="R6tlPCE/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232440AbhIQDfQ (ORCPT + 99 others); Thu, 16 Sep 2021 23:35:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:51921 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242276AbhIQDfP (ORCPT ); Thu, 16 Sep 2021 23:35:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631849634; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NpjC2vhQxMNwcJykwWES5XKw9YQ1dBU9ER+zWax0kyA=; b=R6tlPCE/s6UcVw7FHm8yYXeeW/iCI/c8Vf0XhJPbuHktqvXAb+2ghovVD1UiVD1SRKhR9m YNnV/TngGegdz8mUqmYCppKbys4VHeRh/qPsTnzCIo4mlnkFyMPh6I8vjxgjTqACL5B1G+ hKvBR2GmirVCbxgdOor4WPM9ZAeoMw8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-248-wkzXyDo5MjeoCvg_x25aVw-1; Thu, 16 Sep 2021 23:33:53 -0400 X-MC-Unique: wkzXyDo5MjeoCvg_x25aVw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7A9F71006AA3; Fri, 17 Sep 2021 03:33:51 +0000 (UTC) Received: from T590.Home (ovpn-12-120.pek2.redhat.com [10.72.12.120]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 964E960BF4; Fri, 17 Sep 2021 03:33:42 +0000 (UTC) Date: Fri, 17 Sep 2021 11:33:55 +0800 From: Ming Lei To: Yu Kuai Cc: josef@toxicpanda.com, axboe@kernel.dk, linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com Subject: Re: [PATCH v9] nbd: fix uaf in nbd_handle_reply() Message-ID: References: <20210916093350.1410403-8-yukuai3@huawei.com> <20210916141810.2325276-1-yukuai3@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210916141810.2325276-1-yukuai3@huawei.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 16, 2021 at 10:18:10PM +0800, Yu Kuai wrote: > There is a problem that nbd_handle_reply() might access freed request: > > 1) At first, a normal io is submitted and completed with scheduler: > > internel_tag = blk_mq_get_tag -> get tag from sched_tags > blk_mq_rq_ctx_init > sched_tags->rq[internel_tag] = sched_tag->static_rq[internel_tag] > ... > blk_mq_get_driver_tag > __blk_mq_get_driver_tag -> get tag from tags > tags->rq[tag] = sched_tag->static_rq[internel_tag] > > So, both tags->rq[tag] and sched_tags->rq[internel_tag] are pointing > to the request: sched_tags->static_rq[internal_tag]. Even if the > io is finished. > > 2) nbd server send a reply with random tag directly: > > recv_work > nbd_handle_reply > blk_mq_tag_to_rq(tags, tag) > rq = tags->rq[tag] > > 3) if the sched_tags->static_rq is freed: > > blk_mq_sched_free_requests > blk_mq_free_rqs(q->tag_set, hctx->sched_tags, i) > -> step 2) access rq before clearing rq mapping > blk_mq_clear_rq_mapping(set, tags, hctx_idx); > __free_pages() -> rq is freed here > > 4) Then, nbd continue to use the freed request in nbd_handle_reply > > Fix the problem by get 'q_usage_counter' before blk_mq_tag_to_rq(), > thus request is ensured not to be freed because 'q_usage_counter' is > not zero. > > Signed-off-by: Yu Kuai > --- > Changes in v9: > - move percpu_ref_put() behind. Looks fine: Reviewed-by: Ming Lei -- Ming