From 944b19f493c92c12abb50d6cd03ffeff7e98a8b8 Mon Sep 17 00:00:00 2001 From: Tomohiro Kusumi Date: Sun, 7 Jul 2019 15:38:39 +0900 Subject: [PATCH] Don't fail to apply umask for O_TMPFILE files Apply umask to `mode` which will eventually be applied to inode. This is needed since VFS doesn't apply umask for O_TMPFILE files. (Note that zpl_init_acl() applies `ip->i_mode &= ~current_umask();` only when POSIX ACL is used.) Signed-off-by: Tomohiro Kusumi --- module/zfs/zpl_inode.c | 5 + tests/runfiles/linux.run | 3 +- .../tests/functional/tmpfile/.gitignore | 1 + .../tests/functional/tmpfile/Makefile.am | 4 +- .../functional/tmpfile/tmpfile_stat_mode.c | 97 +++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c diff --git a/module/zfs/zpl_inode.c b/module/zfs/zpl_inode.c index 3f3b2e2dc53c..446e5120f407 100644 --- a/module/zfs/zpl_inode.c +++ b/module/zfs/zpl_inode.c @@ -226,6 +226,11 @@ zpl_tmpfile(struct inode *dir, struct dentry *dentry, zpl_umode_t mode) crhold(cr); vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); + /* + * The VFS does not apply the umask to the provided mode as of 5.2. + * Therefore, it must be applied at the filesystem layer. + */ + mode &= ~current_umask(); zpl_vap_init(vap, dir, mode, cr); cookie = spl_fstrans_mark(); diff --git a/tests/runfiles/linux.run b/tests/runfiles/linux.run index d8b2a0b42b31..5e8beef99c17 100644 --- a/tests/runfiles/linux.run +++ b/tests/runfiles/linux.run @@ -854,7 +854,8 @@ tests = ['threadsappend_001_pos'] tags = ['functional', 'threadsappend'] [tests/functional/tmpfile] -tests = ['tmpfile_001_pos', 'tmpfile_002_pos', 'tmpfile_003_pos'] +tests = ['tmpfile_001_pos', 'tmpfile_002_pos', 'tmpfile_003_pos', + 'tmpfile_stat_mode'] tags = ['functional', 'tmpfile'] [tests/functional/trim] diff --git a/tests/zfs-tests/tests/functional/tmpfile/.gitignore b/tests/zfs-tests/tests/functional/tmpfile/.gitignore index b7a19481ad29..de014c5256ce 100644 --- a/tests/zfs-tests/tests/functional/tmpfile/.gitignore +++ b/tests/zfs-tests/tests/functional/tmpfile/.gitignore @@ -2,3 +2,4 @@ /tmpfile_001_pos /tmpfile_002_pos /tmpfile_003_pos +/tmpfile_stat_mode diff --git a/tests/zfs-tests/tests/functional/tmpfile/Makefile.am b/tests/zfs-tests/tests/functional/tmpfile/Makefile.am index 411445217a6d..cfec10c89af4 100644 --- a/tests/zfs-tests/tests/functional/tmpfile/Makefile.am +++ b/tests/zfs-tests/tests/functional/tmpfile/Makefile.am @@ -8,8 +8,10 @@ dist_pkgdata_SCRIPTS = \ pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/tests/functional/tmpfile -pkgexec_PROGRAMS = tmpfile_test tmpfile_001_pos tmpfile_002_pos tmpfile_003_pos +pkgexec_PROGRAMS = tmpfile_test tmpfile_001_pos tmpfile_002_pos \ + tmpfile_003_pos tmpfile_stat_mode tmpfile_test_SOURCES= tmpfile_test.c tmpfile_001_pos_SOURCES = tmpfile_001_pos.c tmpfile_002_pos_SOURCES = tmpfile_002_pos.c tmpfile_003_pos_SOURCES = tmpfile_003_pos.c +tmpfile_stat_mode_SOURCES = tmpfile_stat_mode.c diff --git a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c new file mode 100644 index 000000000000..e901c1326758 --- /dev/null +++ b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +/* backward compat in case it's not defined */ +#ifndef O_TMPFILE +#define O_TMPFILE (020000000|O_DIRECTORY) +#endif + +/* + * DESCRIPTION: + * Verify stat(2) for O_TMPFILE file considers umask. + * + * STRATEGY: + * 1. open(2) with O_TMPFILE. + * 2. linkat(2). + * 3. fstat(2)/stat(2) and verify .st_mode value. + */ + +static void +test_stat(mode_t mask) +{ + struct stat st, fst; + int i, fd; + char spath[1024], dpath[1024]; + char *penv[] = {"TESTDIR", "TESTFILE0"}; + mode_t masked = 0777 & ~mask; + mode_t mode; + + /* + * Get the environment variable values. + */ + for (i = 0; i < sizeof (penv) / sizeof (char *); i++) { + if ((penv[i] = getenv(penv[i])) == NULL) { + fprintf(stderr, "getenv(penv[%d])\n", i); + exit(1); + } + } + + umask(mask); + fd = open(penv[0], O_RDWR|O_TMPFILE, 0666); + if (fd == -1) { + perror("open"); + exit(2); + } + + if (fstat(fd, &fst) == -1) { + perror("fstat"); + close(fd); + exit(3); + } + + snprintf(spath, sizeof (spath), "/proc/self/fd/%d", fd); + snprintf(dpath, sizeof (dpath), "%s/%s", penv[0], penv[1]); + + unlink(dpath); + if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) == -1) { + perror("linkat"); + close(fd); + exit(4); + } + close(fd); + + if (stat(dpath, &st) == -1) { + perror("stat"); + exit(5); + } + unlink(dpath); + + /* Verify fstat(2) result */ + mode = fst.st_mode & 0777; + if (mode != masked) { + fprintf(stderr, "fstat(2) %o != %o\n", mode, masked); + exit(6); + } + + /* Verify stat(2) result */ + mode = st.st_mode & 0777; + if (mode != masked) { + fprintf(stderr, "stat(2) %o != %o\n", mode, masked); + exit(7); + } +} + +int +main(int argc, char *argv[]) +{ + fprintf(stdout, "Verify stat(2) for O_TMPFILE file considers umask.\n"); + + test_stat(0022); + test_stat(0077); + + return (0); +}