From patchwork Mon Mar 13 22:27:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Khazhy Kumykov X-Patchwork-Id: 13173395 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A48D8C6FD1C for ; Mon, 13 Mar 2023 22:30:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229845AbjCMW37 (ORCPT ); Mon, 13 Mar 2023 18:29:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229742AbjCMW35 (ORCPT ); Mon, 13 Mar 2023 18:29:57 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E19E6A2D1 for ; Mon, 13 Mar 2023 15:29:54 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id v21so4488248ple.9 for ; Mon, 13 Mar 2023 15:29:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678746594; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0vVn1LLW9jGH3vizR2Vh0QqNw1Uoj7/4oaLgWURAUoE=; b=FDliuMCwyve+rDQBz3ikpE9QZV9Wgy+AmmYSmVsN4I6pOYIVhw2dOXfgLvo8WSjmGt 9pnm5xlpf3/+5fF1sEoLBpjRlCwoevAU0Py0Q3Fv3R4AEee8ctH5LXLpbkStvJSpF5xy DqQjjaJ0tq/iAj6Azmy4WScEwy8Jqc5j3bEc4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678746594; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0vVn1LLW9jGH3vizR2Vh0QqNw1Uoj7/4oaLgWURAUoE=; b=E9TDuwXI49QYdyv9IQAsxeP7YKIDd3o3/cNIaa2/32QxaxMhsi+ecGUHKo85At0oN8 7haJfCXLfvc1kZUm9S+l5R/eqsrhKYytmVKIgkRZE4CvvDE4TmkExt6tJZuiPgzCew5i 1vJpGwaHhBWobyxdv0CbANwmjWMhJ0dgVSSTBkEhmEChYrRcQSPPZzBS3f2eewc2IIP9 /gELSzNf4Tjw7oAnJYxMPPtu74QRh6VqyN4uWpfMUPPTYlqxO2S9vVE6nb/anIPpyvrI RL4Ou1sajKwb422LyUlChuhZlncG1Y/8rpODr20pw+YiS9jMALye8f52c2G4hYPHG1l5 FlGg== X-Gm-Message-State: AO0yUKVFL5lgOCwLsipJdyeFxogjDt/BUg0aROTRfkO8THE6CrQf+VkW xGgckoz2jXJnGrpuVabx0qUnt70dpzMhwoYOKPM= X-Google-Smtp-Source: AK7set8JxLnbKfrXgkJSXgb3F/hTJt/RLi+cknWDSWt0Dzy0C8up3d5635IExrlxC3q2K75iExEWkw== X-Received: by 2002:a17:90b:1b4a:b0:237:b5d4:c0df with SMTP id nv10-20020a17090b1b4a00b00237b5d4c0dfmr35428420pjb.6.1678746593809; Mon, 13 Mar 2023 15:29:53 -0700 (PDT) Received: from khazhy-linux.svl.corp.google.com ([2620:15c:2d4:203:157:b07d:930a:fb24]) by smtp.gmail.com with ESMTPSA id km8-20020a17090327c800b0019aa8149cb9sm352440plb.79.2023.03.13.15.29.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 15:29:53 -0700 (PDT) From: Khazhismel Kumykov X-Google-Original-From: Khazhismel Kumykov To: stable@vger.kernel.org Cc: linux-block@vger.kernel.org, Yu Kuai , Jan Kara , Jens Axboe , Sasha Levin , Khazhismel Kumykov Subject: [PATCH v5.10 1/5] block, bfq: fix possible uaf for 'bfqq->bic' Date: Mon, 13 Mar 2023 15:27:53 -0700 Message-Id: <20230313222757.1103179-2-khazhy@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog In-Reply-To: <20230313222757.1103179-1-khazhy@google.com> References: <20230313222757.1103179-1-khazhy@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai [ Upstream commit 64dc8c732f5c2b406cc752e6aaa1bd5471159cab ] Our test report a uaf for 'bfqq->bic' in 5.10: ================================================================== BUG: KASAN: use-after-free in bfq_select_queue+0x378/0xa30 CPU: 6 PID: 2318352 Comm: fsstress Kdump: loaded Not tainted 5.10.0-60.18.0.50.h602.kasan.eulerosv2r11.x86_64 #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.12.1-0-ga5cab58-20220320_160524-szxrtosci10000 04/01/2014 Call Trace: bfq_select_queue+0x378/0xa30 bfq_dispatch_request+0xe8/0x130 blk_mq_do_dispatch_sched+0x62/0xb0 __blk_mq_sched_dispatch_requests+0x215/0x2a0 blk_mq_sched_dispatch_requests+0x8f/0xd0 __blk_mq_run_hw_queue+0x98/0x180 __blk_mq_delay_run_hw_queue+0x22b/0x240 blk_mq_run_hw_queue+0xe3/0x190 blk_mq_sched_insert_requests+0x107/0x200 blk_mq_flush_plug_list+0x26e/0x3c0 blk_finish_plug+0x63/0x90 __iomap_dio_rw+0x7b5/0x910 iomap_dio_rw+0x36/0x80 ext4_dio_read_iter+0x146/0x190 [ext4] ext4_file_read_iter+0x1e2/0x230 [ext4] new_sync_read+0x29f/0x400 vfs_read+0x24e/0x2d0 ksys_read+0xd5/0x1b0 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x61/0xc6 Commit 3bc5e683c67d ("bfq: Split shared queues on move between cgroups") changes that move process to a new cgroup will allocate a new bfqq to use, however, the old bfqq and new bfqq can point to the same bic: 1) Initial state, two process with io in the same cgroup. Process 1 Process 2 (BIC1) (BIC2) | Λ | Λ | | | | V | V | bfqq1 bfqq2 2) bfqq1 is merged to bfqq2. Process 1 Process 2 (BIC1) (BIC2) | | \-------------\| V bfqq1 bfqq2(coop) 3) Process 1 exit, then issue new io(denoce IOA) from Process 2. (BIC2) | Λ | | V | bfqq2(coop) 4) Before IOA is completed, move Process 2 to another cgroup and issue io. Process 2 (BIC2) Λ |\--------------\ | V bfqq2 bfqq3 Now that BIC2 points to bfqq3, while bfqq2 and bfqq3 both point to BIC2. If all the requests are completed, and Process 2 exit, BIC2 will be freed while there is no guarantee that bfqq2 will be freed before BIC2. Fix the problem by clearing bfqq->bic while bfqq is detached from bic. Fixes: 3bc5e683c67d ("bfq: Split shared queues on move between cgroups") Suggested-by: Jan Kara Signed-off-by: Yu Kuai Reviewed-by: Jan Kara Link: https://lore.kernel.org/r/20221214030430.3304151-1-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin Signed-off-by: Khazhismel Kumykov --- block/bfq-iosched.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index 7c4b8d0635eb..afaededb3c49 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -373,6 +373,12 @@ struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync) void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync) { + struct bfq_queue *old_bfqq = bic->bfqq[is_sync]; + + /* Clear bic pointer if bfqq is detached from this bic */ + if (old_bfqq && old_bfqq->bic == bic) + old_bfqq->bic = NULL; + bic->bfqq[is_sync] = bfqq; } @@ -4977,7 +4983,6 @@ static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync) unsigned long flags; spin_lock_irqsave(&bfqd->lock, flags); - bfqq->bic = NULL; bfq_exit_bfqq(bfqd, bfqq); bic_set_bfqq(bic, NULL, is_sync); spin_unlock_irqrestore(&bfqd->lock, flags); From patchwork Mon Mar 13 22:27:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khazhy Kumykov X-Patchwork-Id: 13173396 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7AD5C74A4B for ; Mon, 13 Mar 2023 22:30:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229742AbjCMW37 (ORCPT ); Mon, 13 Mar 2023 18:29:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbjCMW36 (ORCPT ); Mon, 13 Mar 2023 18:29:58 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7965569CCB for ; Mon, 13 Mar 2023 15:29:55 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id u5so14601521plq.7 for ; Mon, 13 Mar 2023 15:29:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678746595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=46km6/xQDE/lr25tFcuq2eYU9Y42SqtxUbI2RZS41Fg=; b=T8Ne8ugWDZF9tpmQV+0C+EFGYNh2NwYqrv6iybU5bymFLC7HrIkw5Jf5Mxm32WhPFz GY+ZIOvtkJdE2smmo80YiChjxTyJVvDTynNFrPjHlmCf3QUj6JccoX73tuPVORzHcu9l 4gTVTSxI3Fyq6zQEr6i5RWl/WCC91KDf1Np3c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678746595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=46km6/xQDE/lr25tFcuq2eYU9Y42SqtxUbI2RZS41Fg=; b=c6pwj46JS+Ynjju2sLmb92zZEwLFffI//iV8k2fX98iNciFPSU+QFjyhpz0+c1ePaZ cOhSbWDNUi3L+5YJLucxVGfyNpW7n0IQ9P4JaeRnpWVdFfuh62xob/reS6jSOcpXYqyb jWfzqvjMFb4PhjzLOyp2ZLsY1WMWdPcNqmxFcMyidgl5YP2IKOAAKmdqNkCiyb1ZmOrI EclX5J8NgkfP3JypZuIXoWChJYR64ZSXVbVUodb/obhbLMRU8BolFjF/sOk+JRTIyMld aQ+LTXef1v8L1z8unedCEjPqtk30EEyfS7NEx648AH8Hf2JaGdst2/0hb2ZTbusuj+tT YAyw== X-Gm-Message-State: AO0yUKUGBIp6NA/hIyPNdaGA9WawEjTethCiwuyeARVgh7C0eOBQusD1 T1PHsG0Yuzt7QEvqtoAxf0zboA== X-Google-Smtp-Source: AK7set+JOvgWc7s+kU5s9wzz2uKUB9/K1uceOE3LlxdRZ0kl63wBGGkDy3C9BjcjxjcoX0RBAm7isg== X-Received: by 2002:a17:90a:c7c3:b0:23b:4bf6:bbee with SMTP id gf3-20020a17090ac7c300b0023b4bf6bbeemr8127165pjb.21.1678746595006; Mon, 13 Mar 2023 15:29:55 -0700 (PDT) Received: from khazhy-linux.svl.corp.google.com ([2620:15c:2d4:203:157:b07d:930a:fb24]) by smtp.gmail.com with ESMTPSA id km8-20020a17090327c800b0019aa8149cb9sm352440plb.79.2023.03.13.15.29.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 15:29:54 -0700 (PDT) From: Khazhismel Kumykov X-Google-Original-From: Khazhismel Kumykov To: stable@vger.kernel.org Cc: linux-block@vger.kernel.org, Yu Kuai , Yi Zhang , Jens Axboe , Sasha Levin , Khazhismel Kumykov Subject: [PATCH v5.10 2/5] block, bfq: fix uaf for bfqq in bfq_exit_icq_bfqq Date: Mon, 13 Mar 2023 15:27:54 -0700 Message-Id: <20230313222757.1103179-3-khazhy@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog In-Reply-To: <20230313222757.1103179-1-khazhy@google.com> References: <20230313222757.1103179-1-khazhy@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai [ Upstream commit 246cf66e300b76099b5dbd3fdd39e9a5dbc53f02 ] Commit 64dc8c732f5c ("block, bfq: fix possible uaf for 'bfqq->bic'") will access 'bic->bfqq' in bic_set_bfqq(), however, bfq_exit_icq_bfqq() can free bfqq first, and then call bic_set_bfqq(), which will cause uaf. Fix the problem by moving bfq_exit_bfqq() behind bic_set_bfqq(). Fixes: 64dc8c732f5c ("block, bfq: fix possible uaf for 'bfqq->bic'") Reported-by: Yi Zhang Signed-off-by: Yu Kuai Link: https://lore.kernel.org/r/20221226030605.1437081-1-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin Signed-off-by: Khazhismel Kumykov --- block/bfq-iosched.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index afaededb3c49..0a53b653a7e2 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -4983,8 +4983,8 @@ static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync) unsigned long flags; spin_lock_irqsave(&bfqd->lock, flags); - bfq_exit_bfqq(bfqd, bfqq); bic_set_bfqq(bic, NULL, is_sync); + bfq_exit_bfqq(bfqd, bfqq); spin_unlock_irqrestore(&bfqd->lock, flags); } } From patchwork Mon Mar 13 22:27:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khazhy Kumykov X-Patchwork-Id: 13173397 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AAA7C7618B for ; Mon, 13 Mar 2023 22:30:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229918AbjCMWaB (ORCPT ); Mon, 13 Mar 2023 18:30:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229863AbjCMW37 (ORCPT ); Mon, 13 Mar 2023 18:29:59 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 948A57B9B2 for ; Mon, 13 Mar 2023 15:29:57 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id p20so14576968plw.13 for ; Mon, 13 Mar 2023 15:29:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678746597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xa07PDDOnj/X6NHN+d/QgDm/+DQki57Tyzb3ho/cDuY=; b=bylvb4fxugyVDyToLt5KgTYX0EeU8moTM+EGxcxevMVmFAAwBd/b8qm1kmZvfBvdqg xrvhBGF1I/rw0V2kUxdFKH8/ATrtXkDfLxsHhbAcMfTUdZ8YL91kykyDgtDPC5rPCAtw G7yuXDGBulWFqWPG85pX3lZvecfT3ebaG7yRY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678746597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xa07PDDOnj/X6NHN+d/QgDm/+DQki57Tyzb3ho/cDuY=; b=N+Pk6OcXldRZ19LXphK8TI+pJPzVwH205DxHnZ8CU8vXkQEcCav3R7KhZW7BoGc8q4 +ZJ4sCr3PtlfEzukUfPix0oejpVSQTODxYinSjkDdjaxZt8mfDdtVBqkSVAdOCise6yr xirRqivWzd1nbYlvB7fmcn1c2OVP6nz0ghQTMlEVrEbljbPGctikycFmsy3AtpwGxJLo vbEg0pMA1gkewztus1Iik+0+6vtYGlXDEg8YqQLIyQzhLgZ3vkctvsQDeSMft0C3HrUs 07QkBorLrTTkuIOp1vPRMZpXgEb2dKJe91FvXI3V19WuurBOJnuyQYa1OMTyLFdnDiJn cFdg== X-Gm-Message-State: AO0yUKXleUcc8Pc4mtvDLMntsCc/EavkzGB/+17hLpn6rxosmsEDJrp6 XSN1tzyNGN14gUe0D9epIjT5xA== X-Google-Smtp-Source: AK7set8ijmeCDZTeQ5anQRrCdK+T4bnN0nNP0w7J4l9Ofm1NnmHTTqjHYMYqUtegpiLW2JeXvfPgEw== X-Received: by 2002:a17:902:d4c8:b0:19e:7bce:cc65 with SMTP id o8-20020a170902d4c800b0019e7bcecc65mr39459482plg.66.1678746596837; Mon, 13 Mar 2023 15:29:56 -0700 (PDT) Received: from khazhy-linux.svl.corp.google.com ([2620:15c:2d4:203:157:b07d:930a:fb24]) by smtp.gmail.com with ESMTPSA id km8-20020a17090327c800b0019aa8149cb9sm352440plb.79.2023.03.13.15.29.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 15:29:56 -0700 (PDT) From: Khazhismel Kumykov X-Google-Original-From: Khazhismel Kumykov To: stable@vger.kernel.org Cc: linux-block@vger.kernel.org, NeilBrown , Jens Axboe , Anna Schumaker , Chao Yu , "Darrick J . Wong" , Ilya Dryomov , Jaegeuk Kim , Jan Kara , Jeff Layton , Lars Ellenberg , Miklos Szeredi , Paolo Valente , Philipp Reisner , Ryusuke Konishi , Trond Myklebust , Wu Fengguang , Andrew Morton , Linus Torvalds , Sasha Levin , Khazhismel Kumykov Subject: [PATCH v5.10 3/5] block/bfq-iosched.c: use "false" rather than "BLK_RW_ASYNC" Date: Mon, 13 Mar 2023 15:27:55 -0700 Message-Id: <20230313222757.1103179-4-khazhy@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog In-Reply-To: <20230313222757.1103179-1-khazhy@google.com> References: <20230313222757.1103179-1-khazhy@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: NeilBrown [ Upstream commit f6bad159f5d5e5b33531aba3d9b860ad8618afe0 ] bfq_get_queue() expects a "bool" for the third arg, so pass "false" rather than "BLK_RW_ASYNC" which will soon be removed. Link: https://lkml.kernel.org/r/164549983746.9187.7949730109246767909.stgit@noble.brown Signed-off-by: NeilBrown Acked-by: Jens Axboe Cc: Anna Schumaker Cc: Chao Yu Cc: Darrick J. Wong Cc: Ilya Dryomov Cc: Jaegeuk Kim Cc: Jan Kara Cc: Jeff Layton Cc: Lars Ellenberg Cc: Miklos Szeredi Cc: Paolo Valente Cc: Philipp Reisner Cc: Ryusuke Konishi Cc: Trond Myklebust Cc: Wu Fengguang Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Stable-dep-of: b600de2d7d3a ("block, bfq: fix uaf for bfqq in bic_set_bfqq()") Signed-off-by: Sasha Levin Signed-off-by: Khazhismel Kumykov --- block/bfq-iosched.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index 0a53b653a7e2..35b240cba092 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -5071,7 +5071,7 @@ static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio) bfqq = bic_to_bfqq(bic, false); if (bfqq) { bfq_release_process_ref(bfqd, bfqq); - bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic); + bfqq = bfq_get_queue(bfqd, bio, false, bic); bic_set_bfqq(bic, bfqq, false); } From patchwork Mon Mar 13 22:27:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khazhy Kumykov X-Patchwork-Id: 13173399 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A925CC76196 for ; Mon, 13 Mar 2023 22:30:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229938AbjCMWaC (ORCPT ); Mon, 13 Mar 2023 18:30:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229985AbjCMW37 (ORCPT ); Mon, 13 Mar 2023 18:29:59 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D4D47EA33 for ; Mon, 13 Mar 2023 15:29:58 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id d13so3466901pjh.0 for ; Mon, 13 Mar 2023 15:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678746598; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FXqMwWwZuspLZLvoXRZdl9+91DL98FHBt3BrpUMzrfc=; b=KnNT2FCspPV/5hiSse0cmreKXdTEhVxglNy0zyCJL8zolYeTFHlnZSoR3lwlSdUaHp S980fGvhkM/Xk7Ifiu+tfpbC8ynqXoOkEgS3kscUz1NPY6V5hOC2cOXM7Zyq3dAMwl2G IVgJYY4MbvSKvXR8NNhMQ/WmbNM67wpOi8oF0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678746598; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FXqMwWwZuspLZLvoXRZdl9+91DL98FHBt3BrpUMzrfc=; b=cRPbqGbDeOQz9u5wUVM9IP7xcSHgwCqAeAN5jYOI93azmLNrASdqB8vb5M9joHNNn7 gIL6JcVTGaJiDXPpCqw4OpT9egWqP4sV4pKyPUCLfwS22sPATL+DAJmsxd7XzaCPHvDZ fGt6fvepk8jMyXV/9rAhM+91C91k9HawgZjBssbT7n8ocKl7UmHqZ1PqnB+EQdjrhoOH sBcQNdizthUBx8aShMkscYtCTxMqKlta0Kl9VUrrWFYayEHX2ZqKNLPA7d8hxTl3LUZ0 eiyjuDaJMUZ+QMfd1YghSKhnNGDUqayweqB64qd3XMujGN+j3xMNJIfFhvO7oJweezE0 SURQ== X-Gm-Message-State: AO0yUKWQgKuEgAID7wpKu5exCrDAogr+GKow7Q46eGnPZ2h0Dgq8+jpP ugYsdXT0egPedaL0rkN32RzOLw== X-Google-Smtp-Source: AK7set/znSADW30jufifevptgSTUlyVkYs+FwxwM/lLCmRtZrch2HNLXkgzES3kQLOqupqdTpAjLmA== X-Received: by 2002:a17:902:eccd:b0:19c:f232:21ca with SMTP id a13-20020a170902eccd00b0019cf23221camr45821624plh.3.1678746597972; Mon, 13 Mar 2023 15:29:57 -0700 (PDT) Received: from khazhy-linux.svl.corp.google.com ([2620:15c:2d4:203:157:b07d:930a:fb24]) by smtp.gmail.com with ESMTPSA id km8-20020a17090327c800b0019aa8149cb9sm352440plb.79.2023.03.13.15.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 15:29:57 -0700 (PDT) From: Khazhismel Kumykov X-Google-Original-From: Khazhismel Kumykov To: stable@vger.kernel.org Cc: linux-block@vger.kernel.org, Yu Kuai , Jan Kara , Jens Axboe , Sasha Levin , Khazhismel Kumykov Subject: [PATCH v5.10 4/5] block, bfq: replace 0/1 with false/true in bic apis Date: Mon, 13 Mar 2023 15:27:56 -0700 Message-Id: <20230313222757.1103179-5-khazhy@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog In-Reply-To: <20230313222757.1103179-1-khazhy@google.com> References: <20230313222757.1103179-1-khazhy@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai [ Upstream commit 337366e02b370d2800110fbc99940f6ddddcbdfa ] Just to make the code a litter cleaner, there are no functional changes. Signed-off-by: Yu Kuai Reviewed-by: Jan Kara Link: https://lore.kernel.org/r/20221214033155.3455754-3-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe Stable-dep-of: b600de2d7d3a ("block, bfq: fix uaf for bfqq in bic_set_bfqq()") Signed-off-by: Sasha Levin Signed-off-by: Khazhismel Kumykov --- block/bfq-cgroup.c | 8 ++++---- block/bfq-iosched.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/block/bfq-cgroup.c b/block/bfq-cgroup.c index badb90352bf3..2f440b79183d 100644 --- a/block/bfq-cgroup.c +++ b/block/bfq-cgroup.c @@ -705,15 +705,15 @@ static void *__bfq_bic_change_cgroup(struct bfq_data *bfqd, struct bfq_io_cq *bic, struct bfq_group *bfqg) { - struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0); - struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1); + struct bfq_queue *async_bfqq = bic_to_bfqq(bic, false); + struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, true); struct bfq_entity *entity; if (async_bfqq) { entity = &async_bfqq->entity; if (entity->sched_data != &bfqg->sched_data) { - bic_set_bfqq(bic, NULL, 0); + bic_set_bfqq(bic, NULL, false); bfq_release_process_ref(bfqd, async_bfqq); } } @@ -749,7 +749,7 @@ static void *__bfq_bic_change_cgroup(struct bfq_data *bfqd, */ bfq_put_cooperator(sync_bfqq); bfq_release_process_ref(bfqd, sync_bfqq); - bic_set_bfqq(bic, NULL, 1); + bic_set_bfqq(bic, NULL, true); } } } diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index 35b240cba092..016d7f32af9f 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -2816,7 +2816,7 @@ bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic, /* * Merge queues (that is, let bic redirect its requests to new_bfqq) */ - bic_set_bfqq(bic, new_bfqq, 1); + bic_set_bfqq(bic, new_bfqq, true); bfq_mark_bfqq_coop(new_bfqq); /* * new_bfqq now belongs to at least two bics (it is a shared queue): @@ -6014,7 +6014,7 @@ bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq) return bfqq; } - bic_set_bfqq(bic, NULL, 1); + bic_set_bfqq(bic, NULL, true); bfq_put_cooperator(bfqq); From patchwork Mon Mar 13 22:27:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khazhy Kumykov X-Patchwork-Id: 13173398 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57B8DC6FD1C for ; Mon, 13 Mar 2023 22:30:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229456AbjCMWaD (ORCPT ); Mon, 13 Mar 2023 18:30:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbjCMWaB (ORCPT ); Mon, 13 Mar 2023 18:30:01 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EE8E69CCB for ; Mon, 13 Mar 2023 15:29:59 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id a2so14618733plm.4 for ; Mon, 13 Mar 2023 15:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678746599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XPLn+orVfSK+z4tupCE3vvaGTo6pOxRCWsTN9XiJPXw=; b=jwVQFnAZvTbL1m3KsEL+nj7y4CkOYqkthOJJZum4ez+s9ehzdY/jhGB0JirvrTvSCM 7SR9W1NWfEuWwA6QbUBpgU0GPxwrjZmm2fE5jYhi23JHRYR5Q3ZCDyPrpG03hgw3sark ZA4S2l4uR/NYjZU1VXsKl+FSisRWndkN9o27U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678746599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XPLn+orVfSK+z4tupCE3vvaGTo6pOxRCWsTN9XiJPXw=; b=Jxg6Zs/7J05KC5Qb7qxERPAfaChWN+OaNwV8ygq9FDmIOhXE+efku2SWhxUK2b0NJs TMnzaGRKm0WBPzKQtc3/JOGwvUNekYadciu7Kal72xfEjEnrUshssbUcWqx1dUioZH6b GZ1va1sB5VOyeHD0euEj6xhhJhnPss3YuTCITsP1hDkaA6b+Fi/a9ggRExgkcItvMIuk pv6X+RYSJbOd8/8kzr8lRiqnffzab2pMkwqB6HOrVtCwIOjK+W8Xy5HKLNLfcTOKGMte MaWr88mk3Sw3PVGqNTX1THokCbRcyhRpSj5k3bw2gun9xPE1/6Jl0k5rZmRm6qXQt2Dg U6OA== X-Gm-Message-State: AO0yUKUwmNtpIoTXvQU2HwaS7cLJsUF8nKt1ZKwi5om742H6BA+ooPRX Rb175N1asNYD6m9VyQThofwigw== X-Google-Smtp-Source: AK7set+bwCXPeXXJYiHoIs21LWog9BcgfWy0JQjslrBQOlmossB4jqnklmjuEP81ZHtlr8WCPOmtjQ== X-Received: by 2002:a17:903:446:b0:1a0:48ff:53a0 with SMTP id iw6-20020a170903044600b001a048ff53a0mr3867362plb.35.1678746599155; Mon, 13 Mar 2023 15:29:59 -0700 (PDT) Received: from khazhy-linux.svl.corp.google.com ([2620:15c:2d4:203:157:b07d:930a:fb24]) by smtp.gmail.com with ESMTPSA id km8-20020a17090327c800b0019aa8149cb9sm352440plb.79.2023.03.13.15.29.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 15:29:58 -0700 (PDT) From: Khazhismel Kumykov X-Google-Original-From: Khazhismel Kumykov To: stable@vger.kernel.org Cc: linux-block@vger.kernel.org, Yu Kuai , Shinichiro Kawasaki , Jan Kara , Jens Axboe , Sasha Levin , Khazhismel Kumykov Subject: [PATCH v5.10 5/5] block, bfq: fix uaf for bfqq in bic_set_bfqq() Date: Mon, 13 Mar 2023 15:27:57 -0700 Message-Id: <20230313222757.1103179-6-khazhy@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog In-Reply-To: <20230313222757.1103179-1-khazhy@google.com> References: <20230313222757.1103179-1-khazhy@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai [ Upstream commit b600de2d7d3a16f9007fad1bdae82a3951a26af2 ] After commit 64dc8c732f5c ("block, bfq: fix possible uaf for 'bfqq->bic'"), bic->bfqq will be accessed in bic_set_bfqq(), however, in some context bic->bfqq will be freed, and bic_set_bfqq() is called with the freed bic->bfqq. Fix the problem by always freeing bfqq after bic_set_bfqq(). Fixes: 64dc8c732f5c ("block, bfq: fix possible uaf for 'bfqq->bic'") Reported-and-tested-by: Shinichiro Kawasaki Signed-off-by: Yu Kuai Reviewed-by: Jan Kara Link: https://lore.kernel.org/r/20230130014136.591038-1-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin Signed-off-by: Khazhismel Kumykov --- block/bfq-cgroup.c | 2 +- block/bfq-iosched.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/block/bfq-cgroup.c b/block/bfq-cgroup.c index 2f440b79183d..1f9ccc661d57 100644 --- a/block/bfq-cgroup.c +++ b/block/bfq-cgroup.c @@ -748,8 +748,8 @@ static void *__bfq_bic_change_cgroup(struct bfq_data *bfqd, * request from the old cgroup. */ bfq_put_cooperator(sync_bfqq); - bfq_release_process_ref(bfqd, sync_bfqq); bic_set_bfqq(bic, NULL, true); + bfq_release_process_ref(bfqd, sync_bfqq); } } } diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index 016d7f32af9f..6687b805bab3 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -5070,9 +5070,11 @@ static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio) bfqq = bic_to_bfqq(bic, false); if (bfqq) { - bfq_release_process_ref(bfqd, bfqq); + struct bfq_queue *old_bfqq = bfqq; + bfqq = bfq_get_queue(bfqd, bio, false, bic); bic_set_bfqq(bic, bfqq, false); + bfq_release_process_ref(bfqd, old_bfqq); } bfqq = bic_to_bfqq(bic, true);