Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6537919ybx; Mon, 11 Nov 2019 10:35:24 -0800 (PST) X-Google-Smtp-Source: APXvYqxZ50KZhgHZ58dA2ayQs5IHeTEjWmbHLmA4K8CpJakUoG9PGYUf/vZjHTeSE5Mmr87hjypr X-Received: by 2002:a05:6402:38e:: with SMTP id o14mr22189784edv.285.1573497324314; Mon, 11 Nov 2019 10:35:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573497324; cv=none; d=google.com; s=arc-20160816; b=F7Zp3Tqn3gGSo+XlPvH/guUV2LHoj7yaH7v+lZjBmliwYkdDo0nMSsqPHr/0bIWr4N 47jNcxlj1/h3zcSb9QGiAZ7NgJNjGRAvZW32TTTvXGKPy4MK0mNI/I0KVfMtrvuofAKj WYKwAW3tBWnNY1ef/AQxAIJVXQKIIwWw+6xA3Vs3zi2I5GU8+UTeVdAkutj1miVHCy1C D8O+67nqC/6VZ+0oaJY2Y6qNoM5KGtR7mMUJddmBOhBOSEJEJ1MSqpSGRzvskio81S/l p1yLYZYiQyakS3clRriPgX93IGL9Y+OD7kk/We+q9BZS0m5mIQzlsupAPE0CQaVSuVe6 k44w== 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=JlM/V4z/zcpsQ8z7m4trH1Q5ERFPh3FVQbrLBvhfEsw=; b=wqtolRiMA19wZmgKKWJ7ctZY33aTWrmvK7ncahoQT70FB+EChZYHZUCN39MG8aNCI0 iJvjXQw0JHufFdiBxovhtsjqFMbhbeCfly3EvpQc9Olu+zeBpDGl6zSa9nDrnYJGld03 KxGLfEKQAUZIUbwFVtIhMUjJnGOcAUrmDvyF52x0sOEpPtHP2A6dRy4LnzDLYk9iaDqp 2dEKLsiok+Z1jjpciHF5pJ/lY6WuepmPJb7o0Ye/t7ZoRtrBLlK9eVahj280lHtBN5jr MYZGhbkdDs1J2xnotm5FOwzY7i8WDDyXmg6OFpQsrmQvmb/t6nJrFndQlj4qMEr6tAwU 4rwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AcM7T8n2; 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 z16si13225615edi.241.2019.11.11.10.35.00; Mon, 11 Nov 2019 10:35:24 -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=AcM7T8n2; 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 S1728114AbfKKSdM (ORCPT + 99 others); Mon, 11 Nov 2019 13:33:12 -0500 Received: from mail.kernel.org ([198.145.29.99]:50450 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727295AbfKKSdK (ORCPT ); Mon, 11 Nov 2019 13:33:10 -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 B475120674; Mon, 11 Nov 2019 18:33:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573497187; bh=Umhrk4I3/8m4n1PENLs+C//lxigBRjVE7+2A8v6Wleo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AcM7T8n2PaSJbXm+z9dkrXOp/hwIvqP2rJwmo/OtcH/led+2+iy1RDOddn8UeA1W8 nuL+j9GyjyjEMF2Tfuw8B7MOy/x/4hOE/Hho7nOaDFYn9Deoz4+Bb/ZQvvqTcVFacb I+iEybMmxkTNuWCGH+pBxkMwywsYERK2cXZQZy18= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro , Christoph Hellwig Subject: [PATCH 4.9 32/65] configfs: fix a deadlock in configfs_symlink() Date: Mon, 11 Nov 2019 19:28:32 +0100 Message-Id: <20191111181346.721864795@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191111181331.917659011@linuxfoundation.org> References: <20191111181331.917659011@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 @@ -157,11 +157,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);