Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6556907ybx; Mon, 11 Nov 2019 10:54:40 -0800 (PST) X-Google-Smtp-Source: APXvYqy+mZVxUBBjkXA/2tLF6c5CrLDaWpSM4gQL3zEYyvFXKZKy59iieEm7WKDdNZRcYuk5SK39 X-Received: by 2002:a17:906:a40e:: with SMTP id l14mr7427082ejz.168.1573498480401; Mon, 11 Nov 2019 10:54:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573498480; cv=none; d=google.com; s=arc-20160816; b=QlYcffkjToQBxvEMvbqvfNbHJIuBTTQihRpM3GscC+OC6FjJeCWOGhG/paidg2Jn0G OjsrmSu3+Sy6a6+n1PUB42Cqi/VNDkcRzkaBsO6SFnDuAju797VnH+pH/e5Vn2DNxGRg lptzRJ3wksSj4H8yj2pTuM/3t81CJ53+p9HQHc/m3FRN1b4AnveZOmaUDaW8E/gCZvVo UEY+95GfEFFRy7gssd9AIHsTe3voG7icAd3NCSZfaayzBvj5WUEzgLWRRcQm8T6wc1NB zNjJ3Avrf+TaM57sWPeSTPiGbb1X9d2FQpBhViXMr7STKK0E1JdSgTu3rf2uumaSU4t2 2eog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=386o3JbEFSG0m5yrAUpBNIIpo11okTTIpYQVIFGGN58=; b=NUTd3R/WRVkAx0KdxxP0tHJoRvP1oCxTQHawajIvjmzizoSNCUd/SYnc4+LyRTr2H4 Sy3bxUiZ/kKiNIQw591GigLDQ35kTG5/P+FNq2kMgjwis4ExOsyGlXE5ks3jXBVscqYH NOrbZ1wFOCtp+J5ElDdTc0rCD4PvhvC2kAUl59mOgmkkHaqF+nigfuyLHHKbKwzYuNpV WTSYpUWs7cxWX5KvTiqKkxDb98RsCRLINvLUWYuae4lAdbW9U7OHqWumLdwFA0BV0yMb naDzdkWcFFIQZvchaQFF1ltgRYpsk7vKV7dmlkkbwqEeP7zi43TNrZZ5xi+66i6s9uRd ik+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Cb6CxVbN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b20si10735442edx.344.2019.11.11.10.54.16; Mon, 11 Nov 2019 10:54:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Cb6CxVbN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730164AbfKKSvq (ORCPT + 99 others); Mon, 11 Nov 2019 13:51:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:45892 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729976AbfKKSvk (ORCPT ); Mon, 11 Nov 2019 13:51:40 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0EF14222BD; Mon, 11 Nov 2019 18:51:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573498298; bh=AC7j0hTvdLJC4KtmliyQnPkC2qsF+C31855uvSXbV0I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cb6CxVbNbRE1cie9Q7NWp6147GMEBZBdYodcIYOgfVqOcjMjQ1kaIT9xlkyD6H0VF mpuaHmNxgJakuRipkgdCbmnmWXuroKL6NP48DRaj5dxf+bWfcrRhO3z8eEuQPRJwaP HyDXmUhkjiz/fci/TOnNoqrCs5O0WXslVtzEavbE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro , Christoph Hellwig Subject: [PATCH 5.3 081/193] configfs: fix a deadlock in configfs_symlink() Date: Mon, 11 Nov 2019 19:27:43 +0100 Message-Id: <20191111181507.118324581@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191111181459.850623879@linuxfoundation.org> References: <20191111181459.850623879@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Al Viro commit 351e5d869e5ac10cb40c78b5f2d7dfc816ad4587 upstream. Configfs abuses symlink(2). Unlike the normal filesystems, it wants the target resolved at symlink(2) time, like link(2) would've done. The problem is that ->symlink() is called with the parent directory locked exclusive, so resolving the target inside the ->symlink() is easily deadlocked. Short of really ugly games in sys_symlink() itself, all we can do is to unlock the parent before resolving the target and relock it after. However, that invalidates the checks done by the caller of ->symlink(), so we have to * check that dentry is still where it used to be (it couldn't have been moved, but it could've been unhashed) * recheck that it's still negative (somebody else might've successfully created a symlink with the same name while we were looking the target up) * recheck the permissions on the parent directory. Cc: stable@vger.kernel.org Signed-off-by: Al Viro Signed-off-by: Christoph Hellwig Signed-off-by: Greg Kroah-Hartman --- fs/configfs/symlink.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) --- a/fs/configfs/symlink.c +++ b/fs/configfs/symlink.c @@ -143,11 +143,42 @@ int configfs_symlink(struct inode *dir, !type->ct_item_ops->allow_link) goto out_put; + /* + * This is really sick. What they wanted was a hybrid of + * link(2) and symlink(2) - they wanted the target resolved + * at syscall time (as link(2) would've done), be a directory + * (which link(2) would've refused to do) *AND* be a deep + * fucking magic, making the target busy from rmdir POV. + * symlink(2) is nothing of that sort, and the locking it + * gets matches the normal symlink(2) semantics. Without + * attempts to resolve the target (which might very well + * not even exist yet) done prior to locking the parent + * directory. This perversion, OTOH, needs to resolve + * the target, which would lead to obvious deadlocks if + * attempted with any directories locked. + * + * Unfortunately, that garbage is userland ABI and we should've + * said "no" back in 2005. Too late now, so we get to + * play very ugly games with locking. + * + * Try *ANYTHING* of that sort in new code, and you will + * really regret it. Just ask yourself - what could a BOFH + * do to me and do I want to find it out first-hand? + * + * AV, a thoroughly annoyed bastard. + */ + inode_unlock(dir); ret = get_target(symname, &path, &target_item, dentry->d_sb); + inode_lock(dir); if (ret) goto out_put; - ret = type->ct_item_ops->allow_link(parent_item, target_item); + if (dentry->d_inode || d_unhashed(dentry)) + ret = -EEXIST; + else + ret = inode_permission(dir, MAY_WRITE | MAY_EXEC); + if (!ret) + ret = type->ct_item_ops->allow_link(parent_item, target_item); if (!ret) { mutex_lock(&configfs_symlink_mutex); ret = create_link(parent_item, target_item, dentry);