Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1123399ybl; Sat, 18 Jan 2020 19:20:59 -0800 (PST) X-Google-Smtp-Source: APXvYqzTSaffOBQv1o42r42E6zkeOUwLbCJ5ns4m4gaHN7LGsf8zq9d0S1rxsKs1yA9yXgEe/gPR X-Received: by 2002:a9d:32f:: with SMTP id 44mr11260213otv.234.1579404059195; Sat, 18 Jan 2020 19:20:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579404059; cv=none; d=google.com; s=arc-20160816; b=UIkq0Ti3Busr4thcUkn45bOyC/mEiABz7atmYP4p4qihozKL7EtiUxkRfDtOGos1NK OLbPvOvOmGxBtQOsphcJpsDzYWmcL+xYIm/QsbanX6C7JYdoNRgS9I+j3+KLuE+kCMUn F1HJcB3jw5S1lSB5IVOhHzv1551z4AVz/4BAyQ1vY9FInMDAA3ix7B4wDle7rKE9arkV 4uPEDlbkNqVVgHiY1YK/k95nrhda/yKRR1hJyOyUcMzkuwrQfJkejNHaczlMGNM7mc00 o9Hbou72NnbrXXfKadiWCy9mCnW0kMmR4X1iU4PPiy4bipPsfw1/6d2AEA+oKLzu4GdI /FOA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2Lk37qY3UgzYRTeeFCROddEXTwycNdS/k4k1YgZ63AQ=; b=vposopwnQXV53KZWRJ1Pd4Xif9P8ZCLBh9qtjgrNFpdFQI0YvmiY3twd+fau1G2enm xLZ6aM80xzjGK0pxKpUpS5I6bl1c3y+KLEqlFZb0i9/0SUp9ZCGZG/PCNEoHLUBiRa5Z F65IVH7za43626WvcI8mtHV4Bqzxegtyj4cZ2XUrms1su8rx9zzA7dvBtEwGoZ5R/53M V5K1XSKqd6Ona8S2B5AtA7daV2r1tF6lMHcAwUsZ7HFRxA0grpBnIIiLQwvtwfzS21ts jUHxp4za2Xx3ZJccDCR5pca+pzqw5hvdOQcGae+pWoYPG0aks23fNDHbhiGjeSj/1i5L 57mQ== ARC-Authentication-Results: i=1; mx.google.com; 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 16si11187640otu.77.2020.01.18.19.20.46; Sat, 18 Jan 2020 19:20:59 -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; 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 S1727032AbgASDTc (ORCPT + 99 others); Sat, 18 Jan 2020 22:19:32 -0500 Received: from zeniv.linux.org.uk ([195.92.253.2]:56710 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725906AbgASDTc (ORCPT ); Sat, 18 Jan 2020 22:19:32 -0500 Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1it175-00BFWK-1Z; Sun, 19 Jan 2020 03:19:00 +0000 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , linux-kernel@vger.kernel.org, Aleksa Sarai , David Howells , Eric Biederman , Christian Brauner , Al Viro Subject: [PATCH 05/17] make build_open_flags() treat O_CREAT | O_EXCL as implying O_NOFOLLOW Date: Sun, 19 Jan 2020 03:17:17 +0000 Message-Id: <20200119031738.2681033-5-viro@ZenIV.linux.org.uk> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200119031738.2681033-1-viro@ZenIV.linux.org.uk> References: <20200119031423.GV8904@ZenIV.linux.org.uk> <20200119031738.2681033-1-viro@ZenIV.linux.org.uk> MIME-Version: 1.0 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 O_CREAT | O_EXCL means "-EEXIST if we run into a trailing symlink". As it is, we might or might not have LOOKUP_FOLLOW in op->intent in that case - that depends upon having O_NOFOLLOW in open flags. It doesn't matter, since we won't be checking it in that case - do_last() bails out earlier. However, making sure it's not set (i.e. acting as if we had an explicit O_NOFOLLOW) makes the behaviour more explicit and allows to reorder the check for O_CREAT | O_EXCL in do_last() with the call of step_into() immediately following it. Signed-off-by: Al Viro --- fs/namei.c | 15 +++++---------- fs/open.c | 4 +++- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 3b6f60c02f8a..c19b458f66da 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3262,22 +3262,17 @@ static int do_last(struct nameidata *nd, if (unlikely(error < 0)) return error; - /* - * create/update audit record if it already exists. - */ - audit_inode(nd->name, path.dentry, 0); - - if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) { - path_to_nameidata(&path, nd); - return -EEXIST; - } - seq = 0; /* out of RCU mode, so the value doesn't matter */ inode = d_backing_inode(path.dentry); finish_lookup: error = step_into(nd, &path, 0, inode, seq); if (unlikely(error)) return error; + + if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) { + audit_inode(nd->name, nd->path.dentry, 0); + return -EEXIST; + } finish_open: /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ error = complete_walk(nd); diff --git a/fs/open.c b/fs/open.c index b62f5c0923a8..ba7009a5dd1a 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1014,8 +1014,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o if (flags & O_CREAT) { op->intent |= LOOKUP_CREATE; - if (flags & O_EXCL) + if (flags & O_EXCL) { op->intent |= LOOKUP_EXCL; + flags |= O_NOFOLLOW; + } } if (flags & O_DIRECTORY) -- 2.20.1