From patchwork Mon Oct 4 12:58:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Gardner X-Patchwork-Id: 1536095 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=boTGTI9Z; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HNLPC1K8pz9t54 for ; Mon, 4 Oct 2021 23:58:22 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1mXNXo-0007NN-5W; Mon, 04 Oct 2021 12:58:12 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1mXNXl-0007Mq-7Q for kernel-team@lists.ubuntu.com; Mon, 04 Oct 2021 12:58:09 +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-1.canonical.com (Postfix) with ESMTPS id 026CF3F338 for ; Mon, 4 Oct 2021 12:58:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1633352289; bh=FnNMhf0J8hIZznT/437QLE5kjjh7h1Unq4Z9O2CGQSE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=boTGTI9ZEbucyqxYxXWOPkH+Byotjb8fuehZR0NSYtGs+KDlB4rAO94+OXUDgOx+v wQkOZa2ALFXVw53a78ijmAEMkRWwF6yq8t2rFdKbLii7zW42fFnihMnrk8BiRbBp7k GpsrlGxrGZX/X4mtBpNbewBTlQFsOwTHWDCyWrT/Tfq6xZbPx60svq61QSqClV39RO NMszGduHMz3dv20gKiMX5XYel5yWnTQmiQjlCC2BX6C4WK7kMDLJtBa6tDB1s7UGBc +Bq6xyIGo9ztCanDYD1Q5iVnAJQdymntgUkpD956uE1E1iEUw5jxVMPoagwpjI+NNZ D7O0R8HE6zXRQ== Received: by mail-pj1-f70.google.com with SMTP id h10-20020a17090a470a00b00196ead30459so11656463pjg.0 for ; Mon, 04 Oct 2021 05:58:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=FnNMhf0J8hIZznT/437QLE5kjjh7h1Unq4Z9O2CGQSE=; b=CIfS/5yq04ORQVzrxBtHi3+hCYLoUfpuL1T+YZDggt4EdB7mxdxf9ldDFjNbpWOzV/ TuWIAyTKEfh1OrCvoKMgnJRNwimziHahaNaBDXQIDFOOyLe1cqPsMpCD9oLyzlFlqIcn Xzh2gbjOerzWRYagG85xmzBBfQ3dHJ/jRki8QN7pNK+Tf77AQ0l325HxOLHCcrizjN6f 6FP2qAN5F4m4rIfJQ0dqiq20oz/KzWjGWmAqhCVQw0z2ZKaUg9ehlHqQIEdK7dT6+fw9 0f1phVup/kO13wvpOxTBhtscXy+mMO/war05S/ut6PPJWw3JfYl0nV9i+zzAXfFka0gK LYtw== X-Gm-Message-State: AOAM53026DxFMzhRxmYcvykrYFaXledxDWQhtNr2eX5QxqBWD+5VzKT4 tHmLpxsCnLVd5kaPJYVKCYYVCymeZgq1LdzdfO5oVp4yTrZW0rOQmNQW1AoJ523tSERtRAMr/wb Q1vWAs7Q9+ONuKVIKFPMHcXtGDEqPr/oKwB0HnCy10w== X-Received: by 2002:a17:90a:de16:: with SMTP id m22mr30556403pjv.54.1633352287202; Mon, 04 Oct 2021 05:58:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKKBmJmafcX59FDuBdSIxZSIgoFDvr9nBSLFOWfn86hQF9Fb8MEDpwP9a5/eC+Yc9orXeYAQ== X-Received: by 2002:a17:90a:de16:: with SMTP id m22mr30556367pjv.54.1633352286867; Mon, 04 Oct 2021 05:58:06 -0700 (PDT) Received: from localhost.localdomain ([69.163.84.166]) by smtp.gmail.com with ESMTPSA id j7sm13686625pjf.18.2021.10.04.05.58.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Oct 2021 05:58:06 -0700 (PDT) From: Tim Gardner To: kernel-team@lists.ubuntu.com Subject: [PATCH][focal/linux-azure, focal/linux-hwe-5.8] btrfs: fix NULL pointer dereference when deleting device by invalid id Date: Mon, 4 Oct 2021 06:58:05 -0600 Message-Id: <20211004125805.12662-1-tim.gardner@canonical.com> X-Mailer: git-send-email 2.33.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" From: Qu Wenruo BugLink: https://bugs.launchpad.net/bugs/1945987 [BUG] It's easy to trigger NULL pointer dereference, just by removing a non-existing device id: # mkfs.btrfs -f -m single -d single /dev/test/scratch1 \ /dev/test/scratch2 # mount /dev/test/scratch1 /mnt/btrfs # btrfs device remove 3 /mnt/btrfs Then we have the following kernel NULL pointer dereference: BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP NOPTI CPU: 9 PID: 649 Comm: btrfs Not tainted 5.14.0-rc3-custom+ #35 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:btrfs_rm_device+0x4de/0x6b0 [btrfs] btrfs_ioctl+0x18bb/0x3190 [btrfs] ? lock_is_held_type+0xa5/0x120 ? find_held_lock.constprop.0+0x2b/0x80 ? do_user_addr_fault+0x201/0x6a0 ? lock_release+0xd2/0x2d0 ? __x64_sys_ioctl+0x83/0xb0 __x64_sys_ioctl+0x83/0xb0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae [CAUSE] Commit a27a94c2b0c7 ("btrfs: Make btrfs_find_device_by_devspec return btrfs_device directly") moves the "missing" device path check into btrfs_rm_device(). But btrfs_rm_device() itself can have case where it only receives @devid, with NULL as @device_path. In that case, calling strcmp() on NULL will trigger the NULL pointer dereference. Before that commit, we handle the "missing" case inside btrfs_find_device_by_devspec(), which will not check @device_path at all if @devid is provided, thus no way to trigger the bug. [FIX] Before calling strcmp(), also make sure @device_path is not NULL. Fixes: a27a94c2b0c7 ("btrfs: Make btrfs_find_device_by_devspec return btrfs_device directly") CC: stable@vger.kernel.org # 5.4+ Reported-by: butt3rflyh4ck Reviewed-by: Anand Jain Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba (cherry picked from commit e4571b8c5e9ffa1e85c0c671995bd4dcc5c75091) Signed-off-by: Tim Gardner Acked-by: Stefan Bader Acked-by: Colin Ian King --- fs/btrfs/volumes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 3e3529c600cb7..e882c790292f9 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -2168,7 +2168,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, if (IS_ERR(device)) { if (PTR_ERR(device) == -ENOENT && - strcmp(device_path, "missing") == 0) + device_path && strcmp(device_path, "missing") == 0) ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; else ret = PTR_ERR(device);