Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp469489ybl; Tue, 28 Jan 2020 06:27:38 -0800 (PST) X-Google-Smtp-Source: APXvYqzr+7yMgTmbveZsprq7V9QoYdjQffbBWvOCNJbPST8t16stuQGs/X2VrjgwyX39x6nYWnrx X-Received: by 2002:a9d:5885:: with SMTP id x5mr16157762otg.132.1580221658203; Tue, 28 Jan 2020 06:27:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580221658; cv=none; d=google.com; s=arc-20160816; b=vVdPbudQrCuHtX3UooY/2WdkzKEu4C6wYSR/W5BBcty+RHCHPvXJXMcW4TdImCNYnA o2M1ArHDPmFsPh4k81p0GPt/7I8ZXZucOAtwAy00sOZOUQ9j1SXSlBhrfu9FrTzFAugc MXSJ0ZBKa6G76HQ7iyXcog7DG+gZB8BkmfScx0wWckZkTfKlI4XiUfVJ6z5vudUX4VTY otDRbU9+/Rsw542XDs5gzohcISNnv49FgzeeC1r/H7b7IYcv9zeAqJsvGA+KHpaU3tKL g4ViB2tQ5wq830tjeOur296UvLtK32B68bHD0igEd1F8T9o3Zw5N2F2ko1w3c3jKhUSN MI5A== 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=PElJuzA9URJSz3Nkb+FqV6sZt+oRT5VR06ElKwyF7hY=; b=d6ZHEQMylPjR5iUxRTnAipeS9vTOO//hMExIbnBQPq0idLGE62IZF6R5IlVoaq4XCz Y2fMTVjqnf45mSrUdV1L/AzdcBrvvOJQDRKAncxEGEA9aMweHXwIiZ1bmIU1U+DXjQHF o5WxW7AH7/91Rbl8h7qzsXpdaHCAZXnVqMO5Irt/I4DQRSXSGDNJ8YNOizPzwcxoTe/D TxiwZO1P2QcIqKhQ8oO+spZZGRztuxrJw9fxu4APo84DnRmQ7KBTf1Svr8P0jPSondU2 MLtcfvPZ6fRYr1HvQqExe9Y0/PwBn7wZMqwkS6cW5ZwTUA28DsMr2IEcR4TrtV1xzjv7 zqNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=QkMrmzV2; 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 s6si3767467otq.115.2020.01.28.06.27.25; Tue, 28 Jan 2020 06:27:38 -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=QkMrmzV2; 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 S1732968AbgA1OZg (ORCPT + 99 others); Tue, 28 Jan 2020 09:25:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:52484 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732925AbgA1OZa (ORCPT ); Tue, 28 Jan 2020 09:25:30 -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 C27A721739; Tue, 28 Jan 2020 14:25:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580221530; bh=47W4q7AmdlxRLf2E1aFmx2eD2JLU1vA6j0yeFfY5Y1o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QkMrmzV2RkSfpHQl4hGHWxbvSgu4lI2E0UeNUTs9LjPgbfGeFolt7GJNIMILOJCC2 X2rmpw9RXcvv0ZRtoPtcm/NkwjskOcb2ucK4c5s+jiifYN+EiCQWA15G7Uon5TTkoW qp/nbR3CvIxhMUrrzmuCtkhhu9Yk1kT0n1yBBUj0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro Subject: [PATCH 4.9 260/271] do_last(): fetch directory ->i_mode and ->i_uid before its too late Date: Tue, 28 Jan 2020 15:06:49 +0100 Message-Id: <20200128135911.951103077@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200128135852.449088278@linuxfoundation.org> References: <20200128135852.449088278@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 d0cb50185ae942b03c4327be322055d622dc79f6 upstream. may_create_in_sticky() call is done when we already have dropped the reference to dir. Fixes: 30aba6656f61e (namei: allow restricted O_CREAT of FIFOs and regular files) Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- fs/namei.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) --- a/fs/namei.c +++ b/fs/namei.c @@ -1011,7 +1011,8 @@ static int may_linkat(struct path *link) * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory * should be allowed, or not, on files that already * exist. - * @dir: the sticky parent directory + * @dir_mode: mode bits of directory + * @dir_uid: owner of directory * @inode: the inode of the file to open * * Block an O_CREAT open of a FIFO (or a regular file) when: @@ -1027,18 +1028,18 @@ static int may_linkat(struct path *link) * * Returns 0 if the open is allowed, -ve on error. */ -static int may_create_in_sticky(struct dentry * const dir, +static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid, struct inode * const inode) { if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || - likely(!(dir->d_inode->i_mode & S_ISVTX)) || - uid_eq(inode->i_uid, dir->d_inode->i_uid) || + likely(!(dir_mode & S_ISVTX)) || + uid_eq(inode->i_uid, dir_uid) || uid_eq(current_fsuid(), inode->i_uid)) return 0; - if (likely(dir->d_inode->i_mode & 0002) || - (dir->d_inode->i_mode & 0020 && + if (likely(dir_mode & 0002) || + (dir_mode & 0020 && ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { return -EACCES; @@ -3259,6 +3260,8 @@ static int do_last(struct nameidata *nd, int *opened) { struct dentry *dir = nd->path.dentry; + kuid_t dir_uid = dir->d_inode->i_uid; + umode_t dir_mode = dir->d_inode->i_mode; int open_flag = op->open_flag; bool will_truncate = (open_flag & O_TRUNC) != 0; bool got_write = false; @@ -3401,7 +3404,7 @@ finish_open: error = -EISDIR; if (d_is_dir(nd->path.dentry)) goto out; - error = may_create_in_sticky(dir, + error = may_create_in_sticky(dir_mode, dir_uid, d_backing_inode(nd->path.dentry)); if (unlikely(error)) goto out;