From patchwork Wed Apr 10 08:26:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valente X-Patchwork-Id: 162007 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp5925085jan; Wed, 10 Apr 2019 01:27:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqxu6LTlR6RZbzUJ05V4x6VDuUBiRQtoswppxxDnJL0OPw0sewxwhz+++LX3PpCyHmdWQktY X-Received: by 2002:a62:6f47:: with SMTP id k68mr25793766pfc.196.1554884838603; Wed, 10 Apr 2019 01:27:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554884838; cv=none; d=google.com; s=arc-20160816; b=uPBv7Qs0Stmuf8yFWE8+2OVvtvrLwECazs+nupjjhQxmu2qgdErJH6QLqvxyFmTZJH XgOFxhfjvLCZ91KUdFZtbCgZG7YEa7O89lTAPKbuYmz1Z6kjPDcgnMH51qM9nPf/xjma +G5bA61VoQYaAm9vnFcTWRnHFz+dIofFBpo0A+vJta2ISZsCMzcCF9Sm/n9ji92vbRio +7VxaMA/usCXNVkwe546LCosfhsUTA0/+0QvfqArdmKJAx4d1SAKMvnxWeB10qQOpmmQ GCKrTaorKITAUpply8HWEySjNm85Up/Eb7rxo25ciIY8XNshxwWRu55XPr5rqzIjT+DZ uN7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=XX9dSz/AtUZ52Tp2kC2tWYDeGDmBEkI/+RsqbAt06sQ=; b=g+zsDCk3lTpj7AjURKQVX9FWyM/jfoo5crrSHHiRS0scqVBT/j8qZez/zhRAUgk64G fo3FRphhIhQtAXuC+pQwgAjI5bTT1TjrbdrbOua/Lnc+PhL7AdEu+vT2OkBwcjNUv5d0 1HdBLqX1iB+uoLvrZg5LYp3s1JLmUiP4jz8kukMZQ4053iXIPC/jgngokoOKugpV78rx nM9ylWVumWKuVGxbgo7ObHfY7MF9BsveffLKpVs4ijJ0TGFBQl9GY6MXvQrHWedFEAeo kJgzQEQ49X8SwXBK4g8v2nTCOCgpUO1yRsC97I8FQg41/tlGOajkaF2/fmA9c+JO+z8D 2Zrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G0kIOolM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g20si30936582pfh.226.2019.04.10.01.27.18; Wed, 10 Apr 2019 01:27:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G0kIOolM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729619AbfDJI1Q (ORCPT + 31 others); Wed, 10 Apr 2019 04:27:16 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:52564 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729544AbfDJI1Q (ORCPT ); Wed, 10 Apr 2019 04:27:16 -0400 Received: by mail-wm1-f66.google.com with SMTP id a184so1601571wma.2 for ; Wed, 10 Apr 2019 01:27:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=XX9dSz/AtUZ52Tp2kC2tWYDeGDmBEkI/+RsqbAt06sQ=; b=G0kIOolMKYhh02MFYzgheBFwKwDXDSs9OqwbN4hvVkZQDMZgzfWji2bCQtJLh/Z/+u A5Wk6eJ7Kf7LTeQ36bNYKHtjQN3BAMsGpnFSOYwKmtdPUrxfaa1d9u70tyY750KrLmo0 JTqa9XcElwSrBQYmLKzTfuvv3XVO+mRvWMU9nUH2tmJ5SiP15b8qUc86kFa73JXoerLF 2o//ls8QofjNHUDqmtTBjHS4bz46nL2zia2GPw7paMOOzKb32onW5/jEVjQ0zRm4HP5U 6yyicKdMElz8onp+I1tj4h1PdO6kx/mnq2ZA3g0z9F2TsVu88VZehxYxCA2ARB7AEc8v 12QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=XX9dSz/AtUZ52Tp2kC2tWYDeGDmBEkI/+RsqbAt06sQ=; b=TpmSn+P3ysPlFMdhCDvNdam4OrwoZ6BDYxW3D15TBUjtCMg8w/2+cpmpt1+tBlGixM 1iVi4IUFPaRFXPBDPoBMqj4rzLnBu5qkZ4c9nDG9PCtgip6FVQTNGHfFfokvdlVr7q+4 81AtBTi73GnZCzemKho4iAB/AWpCSE2qo7G3zg7MF1Wk//2Sm53NorJC9gCIoYASKRqc kpThPzwDNgc9tkXJgKtLCWUyocM75PxW8Gv2oisu/+eeDvo6YJzITnkMrRG9WDQMHt1O k4AmnMzSINBLMDH28erbt7vZstXeauN3re34T65FJ/14Z30t4n0lpmEutp1haUBoli61 By1w== X-Gm-Message-State: APjAAAXonxlWIsgQSaPNBylIXNptuf2B77hhkUJYW0DAW+vuSpKJF9Lg E4MaElujsQKSj7soHEqL1yXTfg== X-Received: by 2002:a1c:7211:: with SMTP id n17mr1780519wmc.32.1554884833651; Wed, 10 Apr 2019 01:27:13 -0700 (PDT) Received: from wifi-122_dhcprange-158.wifi.unimo.it ([155.185.122.158]) by smtp.gmail.com with ESMTPSA id y125sm2293921wmc.39.2019.04.10.01.27.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Apr 2019 01:27:12 -0700 (PDT) From: Paolo Valente To: Jens Axboe Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, ulf.hansson@linaro.org, linus.walleij@linaro.org, broonie@kernel.org, bfq-iosched@googlegroups.com, oleksandr@natalenko.name, Paolo Valente , Dmitrii Tcvetkov , Douglas Anderson Subject: [PATCH] block, bfq: fix use after free in bfq_bfqq_expire Date: Wed, 10 Apr 2019 10:26:46 +0200 Message-Id: <20190410082646.14221-1-paolo.valente@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The function bfq_bfqq_expire() invokes the function __bfq_bfqq_expire(), and the latter may free the in-service bfq-queue. If this happens, then no other instruction of bfq_bfqq_expire() must be executed, or a use-after-free will occur. Basing on the assumption that __bfq_bfqq_expire() invokes bfq_put_queue() on the in-service bfq-queue exactly once, the queue is assumed to be freed if its refcounter is equal to one right before invoking __bfq_bfqq_expire(). But, since commit 9dee8b3b057e1 ("block, bfq: fix queue removal from weights tree") this assumption is false. __bfq_bfqq_expire() may also invoke bfq_weights_tree_remove() and, since commit 9dee8b3b057e1, also the latter function may invoke bfq_put_queue(). So __bfq_bfqq_expire() may invoke bfq_put_queue() twice, and this is the actual case where the in-service queue may happen to be freed. To address this issue, this commit moves the check on the refcounter of the queue right around the last bfq_put_queue() that may be invoked on the queue. Reported-by: Dmitrii Tcvetkov Reported-by: Douglas Anderson Tested-by: Dmitrii Tcvetkov Tested-by: Douglas Anderson Signed-off-by: Paolo Valente --- block/bfq-iosched.c | 15 +++++++-------- block/bfq-iosched.h | 2 +- block/bfq-wf2q.c | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 11 deletions(-) -- 2.20.1 diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index fac188dd78fa..30b88ec7ad26 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -2822,7 +2822,7 @@ static void bfq_dispatch_remove(struct request_queue *q, struct request *rq) bfq_remove_request(q, rq); } -static void __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq) +static bool __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq) { /* * If this bfqq is shared between multiple processes, check @@ -2855,9 +2855,11 @@ static void __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq) /* * All in-service entities must have been properly deactivated * or requeued before executing the next function, which - * resets all in-service entites as no more in service. + * resets all in-service entites as no more in service. This + * may cause bfqq to be freed. If this happens, the next + * function returns true. */ - __bfq_bfqd_reset_in_service(bfqd); + return __bfq_bfqd_reset_in_service(bfqd); } /** @@ -3262,7 +3264,6 @@ void bfq_bfqq_expire(struct bfq_data *bfqd, bool slow; unsigned long delta = 0; struct bfq_entity *entity = &bfqq->entity; - int ref; /* * Check whether the process is slow (see bfq_bfqq_is_slow). @@ -3347,10 +3348,8 @@ void bfq_bfqq_expire(struct bfq_data *bfqd, * reason. */ __bfq_bfqq_recalc_budget(bfqd, bfqq, reason); - ref = bfqq->ref; - __bfq_bfqq_expire(bfqd, bfqq); - - if (ref == 1) /* bfqq is gone, no more actions on it */ + if (__bfq_bfqq_expire(bfqd, bfqq)) + /* bfqq is gone, no more actions on it */ return; bfqq->injected_service = 0; diff --git a/block/bfq-iosched.h b/block/bfq-iosched.h index 062e1c4787f4..86394e503ca9 100644 --- a/block/bfq-iosched.h +++ b/block/bfq-iosched.h @@ -995,7 +995,7 @@ bool __bfq_deactivate_entity(struct bfq_entity *entity, bool ins_into_idle_tree); bool next_queue_may_preempt(struct bfq_data *bfqd); struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd); -void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd); +bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd); void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq, bool ins_into_idle_tree, bool expiration); void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq); diff --git a/block/bfq-wf2q.c b/block/bfq-wf2q.c index a11bef75483d..ae4d000ac0af 100644 --- a/block/bfq-wf2q.c +++ b/block/bfq-wf2q.c @@ -1605,7 +1605,8 @@ struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd) return bfqq; } -void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd) +/* returns true if the in-service queue gets freed */ +bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd) { struct bfq_queue *in_serv_bfqq = bfqd->in_service_queue; struct bfq_entity *in_serv_entity = &in_serv_bfqq->entity; @@ -1629,8 +1630,20 @@ void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd) * service tree either, then release the service reference to * the queue it represents (taken with bfq_get_entity). */ - if (!in_serv_entity->on_st) + if (!in_serv_entity->on_st) { + /* + * If no process is referencing in_serv_bfqq any + * longer, then the service reference may be the only + * reference to the queue. If this is the case, then + * bfqq gets freed here. + */ + int ref = in_serv_bfqq->ref; bfq_put_queue(in_serv_bfqq); + if (ref == 1) + return true; + } + + return false; } void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,