From patchwork Mon Sep 30 03:06:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 1990721 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XH5Zh17w7z1xsq for ; Mon, 30 Sep 2024 13:06:51 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sv6kE-0000Gv-Jo; Mon, 30 Sep 2024 03:06:42 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sv6kC-0000Gk-Kv for kernel-team@lists.ubuntu.com; Mon, 30 Sep 2024 03:06:40 +0000 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 86F4B3F078 for ; Mon, 30 Sep 2024 03:06:40 +0000 (UTC) Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2e0d1a1de58so3422082a91.2 for ; Sun, 29 Sep 2024 20:06:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727665598; x=1728270398; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uceypaBmAx/b0QthMYeTYfl4Ch9hQbxDE/lpy47Ou54=; b=HWksvdWEijh8VyN3sWVRpSIABl2V4umhXiFOHruFAy8FcutRkA9IQkQBKM4RxgnsMu v+6cT51ROXCOjwPEO4cRG02wWjhKruk2Xh6aIEM2oQODh4WTBUF9eHMixvpNbwMCQ3sr 0fI70HWpZ47+5XOYemG6nQtWUnZjSyMXbkYQZxYTIdjy6XM+XGV1bDyDGEbteZbHiPFX Uz44NtvG0dcr5tzxpZcgVl3wYVsEPxig4QCtDztCPSGQC6/vpo4+WV4U4DvZpRr1dRol 1xn5A5GGFFqAyKjLDgh6W0HBTY2abpHUGokzAo4T6X3pUME9xR+D3InXx8gt7hokelgz m/lQ== X-Gm-Message-State: AOJu0Yz1W/RJCkNED/3CqIHA3rC+Tj4ycb7X2ntuGW9ZTCF2DC1s++nk JOjSrqJphbr+yO9VmcCszJPn/aEJafXSaMEhN2insYf2iJ8LzSdoZHDnbQEK0BS9P8dQV7yEtj9 S1KIe/OxKlfrAkptIMusUXc9lRtfyT238g5nPVRXAg6dAS05+h4BHTV0OAV4VbJdAOwm53sn5IW PSkAzxgODHUQ== X-Received: by 2002:a17:90a:db56:b0:2d8:a344:900d with SMTP id 98e67ed59e1d1-2e0b89dffbemr12213427a91.12.1727665598412; Sun, 29 Sep 2024 20:06:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcn0RwBYXD04vYNxSUTwXPP0FINEAm/J6wvWtw97ox9Tqp2mK0ufxGleAy/T3EDY8RIZpTRg== X-Received: by 2002:a17:90a:db56:b0:2d8:a344:900d with SMTP id 98e67ed59e1d1-2e0b89dffbemr12213409a91.12.1727665598032; Sun, 29 Sep 2024 20:06:38 -0700 (PDT) Received: from localhost.localdomain ([240f:74:7be:1:c835:dd5c:238f:3a73]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e0daaf91e9sm5153092a91.52.2024.09.29.20.06.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 20:06:37 -0700 (PDT) From: Koichiro Den To: kernel-team@lists.ubuntu.com Subject: [SRU][F][PATCH 0/1] CVE-2022-48733 Date: Mon, 30 Sep 2024 12:06:05 +0900 Message-ID: <20240930030610.591772-1-koichiro.den@canonical.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" [Impact] btrfs: fix use-after-free after failure to create a snapshot At ioctl.c:create_snapshot(), we allocate a pending snapshot structure and then attach it to the transaction's list of pending snapshots. After that we call btrfs_commit_transaction(), and if that returns an error we jump to 'fail' label, where we kfree() the pending snapshot structure. This can result in a later use-after-free of the pending snapshot: 1) We allocated the pending snapshot and added it to the transaction's list of pending snapshots; 2) We call btrfs_commit_transaction(), and it fails either at the first call to btrfs_run_delayed_refs() or btrfs_start_dirty_block_groups(). In both cases, we don't abort the transaction and we release our transaction handle. We jump to the 'fail' label and free the pending snapshot structure. We return with the pending snapshot still in the transaction's list; 3) Another task commits the transaction. This time there's no error at all, and then during the transaction commit it accesses a pointer to the pending snapshot structure that the snapshot creation task has already freed, resulting in a user-after-free. This issue could actually be detected by smatch, which produced the following warning: fs/btrfs/ioctl.c:843 create_snapshot() warn: '&pending_snapshot->list' not removed from list So fix this by not having the snapshot creation ioctl directly add the pending snapshot to the transaction's list. Instead add the pending snapshot to the transaction handle, and then at btrfs_commit_transaction() we add the snapshot to the list only when we can guarantee that any error returned after that point will result in a transaction abort, in which case the ioctl code can safely free the pending snapshot and no one can access it anymore. [Backport] Adjusted context due to missing commits: - 9babda9f33fd ("btrfs: Remove async_transid from btrfs_mksubvol/create_subvol/create_snapshot") - d0c2f4fa555e ("btrfs: make concurrent fsyncs wait less when waiting for a transaction commit") [Fix] Noble: not affected Jammy: fixed via stable Focal: Backport - adjusted context, see [Backport] Bionic: fix sent to esm ML Xenial: fix sent to esm ML Trusty: won't fix [Test Case] Compile and boot tested (amd64 only). Ran Smatch and verified that with this backport, the following warn message disappeared: fs/btrfs/ioctl.c:908 create_snapshot() warn: '&pending_snapshot->list' not removed from list Also, did snapshot testing and verified that btrfs_commit_transaction() triggered by it succeeded without any issues (amd64 only). [Where problems could occur] This fix affects those who use btrfs snapshot feature, an issue with this fix would be visible to the user via unpredicted system behaviour or a system crash induced by use-after-free. Filipe Manana (1): btrfs: fix use-after-free after failure to create a snapshot fs/btrfs/ioctl.c | 5 +---- fs/btrfs/transaction.c | 24 ++++++++++++++++++++++++ fs/btrfs/transaction.h | 2 ++ 3 files changed, 27 insertions(+), 4 deletions(-) Acked-by: Ivan Hu