vfs: vfs_tmpfile: ensure O_EXCL flag is enforced

If O_EXCL is *not* specified, then linkat() can be
used to link the temporary file into the filesystem.
If O_EXCL is specified then linkat() should fail (-1).

After commit 863f144f12ad ("vfs: open inside ->tmpfile()")
the O_EXCL flag is no longer honored by the vfs layer for
tmpfile, which means the file can be linked even if O_EXCL
flag is specified, which is a change in behaviour for
userspace!

The open flags was previously passed as a parameter, so it
was uneffected by the changes to file->f_flags caused by
finish_open(). This patch fixes the issue by storing
file->f_flags in a local variable so the O_EXCL test
logic is restored.

This regression was detected by Android CTS Bionic fcntl()
tests running on android-mainline [1].

[1] https://android.googlesource.com/platform/bionic/+/
refs/heads/master/tests/fcntl_test.cpp#352

Fixes: 863f144f12ad ("vfs: open inside ->tmpfile()")
Acked-by: Miklos Szeredi <mszeredi@redhat.com>
Tested-by: Will McVicker <willmcvicker@google.com>
Signed-off-by: Peter Griffin <peter.griffin@linaro.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

authored by Peter Griffin and committed by Al Viro 406c706c 7ee47dcf

Changed files
+2 -1
fs
+2 -1
fs/namei.c
··· 3591 3591 struct inode *dir = d_inode(parentpath->dentry); 3592 3592 struct inode *inode; 3593 3593 int error; 3594 + int open_flag = file->f_flags; 3594 3595 3595 3596 /* we want directory to be writable */ 3596 3597 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); ··· 3614 3613 if (error) 3615 3614 return error; 3616 3615 inode = file_inode(file); 3617 - if (!(file->f_flags & O_EXCL)) { 3616 + if (!(open_flag & O_EXCL)) { 3618 3617 spin_lock(&inode->i_lock); 3619 3618 inode->i_state |= I_LINKABLE; 3620 3619 spin_unlock(&inode->i_lock);