Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1054335pxv; Thu, 22 Jul 2021 21:03:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDos1Gx8p0pnbbe9samndPJugWOQjMt+IZX547DP+Aw+Ug8B+BrErcSz5WuCjyjurOCO6e X-Received: by 2002:a17:906:4158:: with SMTP id l24mr2822425ejk.245.1627013005089; Thu, 22 Jul 2021 21:03:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627013005; cv=none; d=google.com; s=arc-20160816; b=oGu2nW2cSWGMNKLunN8ElUQE4u8WLdJv7PAFoZO/kOWMaa2O+EiAmWfJsLCnRHjQVq byd8d7PIDBrkTdddle0Uu00cz4P07UGxRD21YryCtfVWRtev4T91rmjaGa9nYtRHz6ha jP1fZdXYIbrJQCCjtW5VK8PMHSsqk3/53C0xlFBozKDD/fCl9Kfexxf+XXZbh9vp/N3t eeTDiixIwCgV36sAKmaxyC8g7emD6NZegyt+TouSRy7Y/yzED7qWhBKZJR6xgBIEmYKi 6lUB1LEyj+cCkcPWsQgD+5Zlt+UIbKlWgrzRRcfrTUhQWXGVpfD6lVxqAHYbatf3wGdn G1Yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1ZyBEKHZ5bZLjqAcWH0PNo+z6Q2+47HzspngdSFrx+A=; b=pfoR8wiaZE6F9LiFid6O5Nfg4LgYt94r9i/radP2t5bv1FE/H1+3JiIrddXJvKA8xW HR/hQEL34ABa9QACXkONEU6VN9gtJmGPz+UswMY/cWXRtxAMxdrhduA61AWCjgAuaCmS Radn/U91z/uTqq23/WT2UWuQ3I5iRNLExWlaK8rF3uIIDHhXGGM0s2eBAkzGKDvQBJh4 RNsYWTVL392OTR99DLHPjt/GJbl1q4uy3euTGsoWgE03icWhkm6tAY/ave3zhOFh1zHQ sJgRBu4mFZos2ExopWUC2BHS8l7vTrDYT3331hgRC7ZTWzjmO0cq9/Xqgu6uFhUOuTfx zzqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="A/3JTu4+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o19si28420790ejm.312.2021.07.22.21.03.02; Thu, 22 Jul 2021 21:03:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="A/3JTu4+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234113AbhGWDUI (ORCPT + 99 others); Thu, 22 Jul 2021 23:20:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:39664 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233609AbhGWDS3 (ORCPT ); Thu, 22 Jul 2021 23:18:29 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7873060F25; Fri, 23 Jul 2021 03:59:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627012743; bh=iwY2UIX29yvhgYYsGAvG5Ti4MqpDN7jWYSdzmf6P4uw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A/3JTu4+AeY1S3ANPX0uv7yie1pz8l8tIUiQHjtq28cFdvK4624wL6cGQg6u4+CWW YQfT9FzuUswPd5MZWxLqRbrZVZxBiDyz9CX5xkpcaie88qCA0xUIBQ7mf06yj/nmS1 yP/ApYvcdPwK1CJTc/hpuwygFtSGLjfHe2pGVqe1wvM8tpZjMoV+OKUPnc0r+mvf// pBwrrSJcbIImhgKfr4VIzB1b3R3F2nPFLLPn2c06QpQNlFTsOJuE/FQhGqXYB0ZEIh 2c4Hia8EUKDqSam5c0IQYTv3jxXjt6rQRowM1CSrqkAduGXZ6YEhxx9D3K8od+Ymln mIGrgMIV9C9tg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Desmond Cheong Zhi Xi , syzbot+b718ec84a87b7e73ade4@syzkaller.appspotmail.com, Viacheslav Dubeyko , Al Viro , Greg Kroah-Hartman , "Gustavo A . R . Silva" , Shuah Khan , Andrew Morton , Linus Torvalds , Sasha Levin , linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 7/8] hfs: add lock nesting notation to hfs_find_init Date: Thu, 22 Jul 2021 23:58:51 -0400 Message-Id: <20210723035852.532303-7-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210723035852.532303-1-sashal@kernel.org> References: <20210723035852.532303-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Desmond Cheong Zhi Xi [ Upstream commit b3b2177a2d795e35dc11597b2609eb1e7e57e570 ] Syzbot reports a possible recursive lock in [1]. This happens due to missing lock nesting information. From the logs, we see that a call to hfs_fill_super is made to mount the hfs filesystem. While searching for the root inode, the lock on the catalog btree is grabbed. Then, when the parent of the root isn't found, a call to __hfs_bnode_create is made to create the parent of the root. This eventually leads to a call to hfs_ext_read_extent which grabs a lock on the extents btree. Since the order of locking is catalog btree -> extents btree, this lock hierarchy does not lead to a deadlock. To tell lockdep that this locking is safe, we add nesting notation to distinguish between catalog btrees, extents btrees, and attributes btrees (for HFS+). This has already been done in hfsplus. Link: https://syzkaller.appspot.com/bug?id=f007ef1d7a31a469e3be7aeb0fde0769b18585db [1] Link: https://lkml.kernel.org/r/20210701030756.58760-4-desmondcheongzx@gmail.com Signed-off-by: Desmond Cheong Zhi Xi Reported-by: syzbot+b718ec84a87b7e73ade4@syzkaller.appspotmail.com Tested-by: syzbot+b718ec84a87b7e73ade4@syzkaller.appspotmail.com Reviewed-by: Viacheslav Dubeyko Cc: Al Viro Cc: Greg Kroah-Hartman Cc: Gustavo A. R. Silva Cc: Shuah Khan Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/hfs/bfind.c | 14 +++++++++++++- fs/hfs/btree.h | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c index 4af318fbda77..ef9498a6e88a 100644 --- a/fs/hfs/bfind.c +++ b/fs/hfs/bfind.c @@ -25,7 +25,19 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->key = ptr + tree->max_key_len + 2; hfs_dbg(BNODE_REFS, "find_init: %d (%p)\n", tree->cnid, __builtin_return_address(0)); - mutex_lock(&tree->tree_lock); + switch (tree->cnid) { + case HFS_CAT_CNID: + mutex_lock_nested(&tree->tree_lock, CATALOG_BTREE_MUTEX); + break; + case HFS_EXT_CNID: + mutex_lock_nested(&tree->tree_lock, EXTENTS_BTREE_MUTEX); + break; + case HFS_ATTR_CNID: + mutex_lock_nested(&tree->tree_lock, ATTR_BTREE_MUTEX); + break; + default: + return -EINVAL; + } return 0; } diff --git a/fs/hfs/btree.h b/fs/hfs/btree.h index dcc2aab1b2c4..25ac9a8bb57a 100644 --- a/fs/hfs/btree.h +++ b/fs/hfs/btree.h @@ -13,6 +13,13 @@ typedef int (*btree_keycmp)(const btree_key *, const btree_key *); #define NODE_HASH_SIZE 256 +/* B-tree mutex nested subclasses */ +enum hfs_btree_mutex_classes { + CATALOG_BTREE_MUTEX, + EXTENTS_BTREE_MUTEX, + ATTR_BTREE_MUTEX, +}; + /* A HFS BTree held in memory */ struct hfs_btree { struct super_block *sb; -- 2.30.2