Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp1604831lqg; Sun, 3 Mar 2024 19:22:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVfvKtM2HEDhBA7N4ihURKXKhwEQYX65yIlfr5RXg5WggDQ0UZBGh7fgMw1pAQKVcKa5FGb4Ob4J4YnsTjVrMmtVnc/Z4CXJ5nF9rXflA== X-Google-Smtp-Source: AGHT+IGtE9gONLoPDeQYw/fF/vunhJtBbBXcXy/Jq/8fDAvZvnGdYhGkZkalW5WaHgMd5Vc0B8K4 X-Received: by 2002:a0c:e48c:0:b0:690:667b:f4f3 with SMTP id n12-20020a0ce48c000000b00690667bf4f3mr5218023qvl.17.1709522569821; Sun, 03 Mar 2024 19:22:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709522569; cv=pass; d=google.com; s=arc-20160816; b=v/t3ljh5z9hGI7CYArb+zUORHXgJQOUF76vAGs+HCdgb1TZWcSTMpJxgSQMzDTZTo9 7QZ4QTQQWkGt56dMvlWFERarVjnjLzVE+pN/cR/cZ9plkeHBbCFUFhWZ7AR0ZMXdvJxw abRpqitB0zCd/BkMqMujB79EVVukOC8QS1T3+pg+iqokt1GwWw81hBKEtgrHj7GjiJS/ UrKh9wCe4TNTuug/Imac4fA0u0KRZWlg9xn0ivSehrO9TutEsCyNGpqL2xOattXOC4R8 zJwi0EemRjFUA85yNUB0MLQslPhQeBI7DMB75OcHh+dBcNZSTwXBRqbq/Wc9kDd2GY4R +CMg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=jxmt+Kc2v6qV/L6QxjdTZYCYf9lVHm+1NQ1vS7oFQKw=; fh=q9t7aa7z/wDgd9X0fMDtFVCiZWFUxMKnsUqB2uZsFt8=; b=QMczBWPTDBGYiWXVlqvUovFauwv9BPFRmYHbyCtk0K/dI9LKll9KHJYQtJINi4Ri+9 HGMJxeVqCnNpWyFUWytQxUJFqs15x8QU+KaWNK6XzLSv75bmvt3PQTU0QkaZg6bIV4Tr JNzMoXG1kgLvw3seZ+ZiKBj8TDt5bya50CWC50PcYDRFHsFZpxJuqV0YkD0IKiyK44ql UbZsGe+8pS7+32Ke8W4bY7VAULj3S7DwxYqjfZwWZOfaumRIw/me1vlgaiBHUeqtdoSm vOvnfMnn3AS1pX9qfyd1YIyOawq/IQNL57EbpRCquD8jgrmkPOPmR+DM7Npt8gMWkgfN Czbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-90008-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-90008-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id i3-20020ad45c63000000b006903e2831aasi8802023qvh.370.2024.03.03.19.22.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 19:22:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-90008-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-90008-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-90008-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 860BD1C20ACC for ; Mon, 4 Mar 2024 03:22:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2307D46A2; Mon, 4 Mar 2024 03:22:44 +0000 (UTC) Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F583211C; Mon, 4 Mar 2024 03:22:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709522563; cv=none; b=f2hna1515vWoGVh0XBoNN43VsliJ0rEhPPW/biBEaVMvpQ7uavCZbOA4mf7/97hGy1kVzXP8Q+Ru4SLf8oTmq8VHndfQNysTvt5ZifYz8FwfLP2OUKQwkPcPgvt7srXzjjDInFF4ip/AaCrExticNpfLaW+AgPQYfy/t9nno9n4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709522563; c=relaxed/simple; bh=veLqZ5GaMtlPnNbR+KmO08huNmMlZ5vta4bUNMiDihc=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=I4hfPovIpD792U5GIo8GYGfMVhOiIT5vmWTMDTTUSL8Df69gHCnMETIBPS2RXT86yZoGEnWmA1uTh07he6UkD1LBL4w/89MIl3wGXGlLpatD6oJT9+CxaYtYwnOoayaMVt5chOQtnzgEK8nmmdQXQa1Ky+RNbiXv0hh0baeQ7Ps= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Tp3sZ4GfTz1FLcr; Mon, 4 Mar 2024 11:22:26 +0800 (CST) Received: from kwepemd500004.china.huawei.com (unknown [7.221.188.173]) by mail.maildlp.com (Postfix) with ESMTPS id 34A571A016B; Mon, 4 Mar 2024 11:22:32 +0800 (CST) Received: from kwepemd500012.china.huawei.com (7.221.188.25) by kwepemd500004.china.huawei.com (7.221.188.173) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Mon, 4 Mar 2024 11:22:31 +0800 Received: from huawei.com (10.90.53.73) by kwepemd500012.china.huawei.com (7.221.188.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Mon, 4 Mar 2024 11:22:31 +0800 From: Li Zetao To: , CC: , , Subject: [PATCH] bcachefs: Fix null-ptr-deref in bch2_fs_alloc() Date: Mon, 4 Mar 2024 11:22:03 +0800 Message-ID: <20240304032203.3480001-1-lizetao1@huawei.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemd500012.china.huawei.com (7.221.188.25) There is a null-ptr-deref issue reported by kasan: KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] Call Trace: bch2_fs_alloc+0x1092/0x2170 [bcachefs] bch2_fs_open+0x683/0xe10 [bcachefs] ... When initializing the name of bch_fs, it needs to dynamically alloc memory to meet the length of the name. However, when name allocation failed, it will cause a null-ptr-deref access exception in subsequent string copy. Fix this issue by checking if name allocation is successful. Fixes: 401ec4db6308 ("bcachefs: Printbuf rework") Signed-off-by: Li Zetao --- fs/bcachefs/super.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/bcachefs/super.c b/fs/bcachefs/super.c index 6b23e11825e6..24fa41bbe7e3 100644 --- a/fs/bcachefs/super.c +++ b/fs/bcachefs/super.c @@ -818,13 +818,13 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) goto err; pr_uuid(&name, c->sb.user_uuid.b); - strscpy(c->name, name.buf, sizeof(c->name)); - printbuf_exit(&name); - ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; if (ret) goto err; + strscpy(c->name, name.buf, sizeof(c->name)); + printbuf_exit(&name); + /* Compat: */ if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) -- 2.34.1