Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp438515ybl; Tue, 28 Jan 2020 06:02:17 -0800 (PST) X-Google-Smtp-Source: APXvYqwwCPtshAdVntEDvRkG1e82SUTn86DvYx8pHKRWoNuzvrNr+Tlq0EeKT7Gl0YlpvyST6PRB X-Received: by 2002:a9d:53c2:: with SMTP id i2mr15264840oth.43.1580220136887; Tue, 28 Jan 2020 06:02:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580220136; cv=none; d=google.com; s=arc-20160816; b=TGPRdLbAK4drH4zxBKEpN+mzNefw1wLbTNvmOBbju5rUciCxEYrgyzGrmiviNT/M85 k3QEpc1Cnd7yqT9LxjvkCXE7qXJ8sBIf4MApLnEdlC+3koSD/fCR8Sa7GXgnlxUrahS2 u6uwHdyoRTukI9hkX3uVCCML/LaEXBgfKFGN5jkrQxHk3Av0E5gMm/5+Bg75Alm9hlQA NxZLEHCWOt1Ga3O3q8gDGLDyf9uToU9KvuvadGhgijVQeXQZrav2K+8uBUdRVNT2TpbB Y845drnlMaqqGHatgI5+5yKzjM+5H0+pgxUuMGtQ1fXTjUP8BL4gz9aPbl8O88p96S7c OBtQ== 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=SzV8Pf/NcI8W38ArkCZIrgUnnckwbjvEldkI17/Zt/0=; b=04dRauBXDXctljaa9P+5Nb7zttmRier2X4JgaTgxCAAJVVNAf4vOBHPzSoLbnxTXEZ esWffUcrpUJfLm2c0Z3xH7xkgdac7WAfwNLzW4DsN+dFwmjGGCCsBIXzHsBpUelny45v 3aabf8Yx/eTG2e/2S3fFGIWJpvxSvzENOyHevz8M1MAsjCIeIskQdXyRK3ENKOOX59aN MoHKryNfHuiVHKWeXkpm/eQhZZZYEot8bf9tcH4f+il53ung4B61m9nm1udr8QpjRQaZ eIxSbZ/dTI3rlHRGvTiE2hPH5h7rHVyrVUHjdFMXky/lEyrQo6o8ffOm9uKoBEiaCK8r ZoGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=p5zvJCui; 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 w26si8316367otl.213.2020.01.28.06.02.02; Tue, 28 Jan 2020 06:02:16 -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=p5zvJCui; 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 S1726650AbgA1OAV (ORCPT + 99 others); Tue, 28 Jan 2020 09:00:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:46056 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726692AbgA1OAU (ORCPT ); Tue, 28 Jan 2020 09:00:20 -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 0C00124694; Tue, 28 Jan 2020 14:00:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580220019; bh=8JpFQ3NofESOkuHFqaDPypBoczwv8Ujt9h1/c4Jearc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p5zvJCuil/3q0ZOHQk6q0Xxx4EMKwuJq2Ng0hNCR2rTuDdVBAZksHgZtm6xzV+j8w r27z1T/EP7tcN9Z8l05ny4ghFMyBGA27ImasQJdyCxwpsUII+nH6XgrhNwweYoT4fg E4FG1W7Z5dGdhTa70Zd5/mrBWYRChgZaOfbDejHk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro Subject: [PATCH 4.14 36/46] do_last(): fetch directory ->i_mode and ->i_uid before its too late Date: Tue, 28 Jan 2020 14:58:10 +0100 Message-Id: <20200128135754.563268564@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200128135749.822297911@linuxfoundation.org> References: <20200128135749.822297911@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 @@ -1023,7 +1023,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: @@ -1039,18 +1040,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; @@ -3265,6 +3266,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; @@ -3400,7 +3403,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;