generic: test record locks across execve in multithread process
authorXiong Zhou <xzhou@redhat.com>
Mon, 23 Apr 2018 02:42:48 +0000 (10:42 +0800)
committerEryu Guan <guaneryu@gmail.com>
Fri, 27 Apr 2018 11:06:48 +0000 (19:06 +0800)
POSIX requires that record locks are preserved across an execve(2).
But currently the locks are released if process is multithreaded at
the time that execve is called.

As Jeff Layton wrote in his patch:
"
In that case, we'll end up unsharing the files_struct but the locks
will still have their fl_owner set to the address of the old one.
Eventually, when the other threads die and the last reference to the
old files_struct is put, any POSIX locks get torn down since it
looks like a close occurred on them.

The result is that all of your open files will be intact with none
of the locks you held before execve.
"

Add a new regression test for this particular case.

[Eryu: rewrite commit log and test description]

Signed-off-by: Xiong Zhou <xzhou@redhat.com>
Reviewed-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
.gitignore
src/Makefile
src/t_locks_execve.c [new file with mode: 0644]
tests/generic/484 [new file with mode: 0755]
tests/generic/484.out [new file with mode: 0644]
tests/generic/group

index 368d11c84a663bba9d70dbc961b01a7d37c35ef0..192ca35e158c83bd7faee3e833de9521369e4366 100644 (file)
 /src/t_getcwd
 /src/t_holes
 /src/t_immutable
+/src/t_locks_execve
 /src/t_mmap_cow_race
 /src/t_mmap_dio
 /src/t_mmap_fallocate
index 0d3feae1eeb2d0c29d40a100d0257f78ac60d260..6ca5636639dc32e459de93107133e2c5e2a7b798 100644 (file)
@@ -15,7 +15,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
        holetest t_truncate_self t_mmap_dio af_unix t_mmap_stale_pmd \
        t_mmap_cow_race t_mmap_fallocate fsync-err t_mmap_write_ro \
        t_ext4_dax_journal_corruption t_ext4_dax_inline_corruption \
-       t_ofd_locks
+       t_ofd_locks t_locks_execve
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
        preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
diff --git a/src/t_locks_execve.c b/src/t_locks_execve.c
new file mode 100644 (file)
index 0000000..9ad2dc3
--- /dev/null
@@ -0,0 +1,77 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+static void err_exit(char *op, int errn)
+{
+       fprintf(stderr, "%s: %s\n", op, strerror(errn));
+       exit(errn);
+}
+
+void *thread_fn(void *arg)
+{
+       /* execve will release threads */
+       while(1) sleep(1);
+       return NULL;
+}
+
+struct flock fl = {
+       .l_type = F_WRLCK,
+       .l_whence = SEEK_SET,
+       .l_start = 0,
+       .l_len = 1,
+};
+
+static void checklock(int fd)
+{
+       if (fcntl(fd, F_GETLK, &fl) < 0)
+               err_exit("getlk", errno);
+       if (fl.l_type == F_UNLCK) {
+               printf("record lock is not preserved across execve(2)\n");
+               exit(1);
+       }
+       exit(0);
+}
+
+int main(int argc, char **argv)
+{
+       int fd, flags;
+       char fdstr[10];
+       char *newargv[] = { argv[0], argv[1], fdstr, NULL };
+       pthread_t th;
+
+       /* passing fd in argv[2] in execve */
+       if (argc == 3) {
+               fd = atoi(argv[2]);
+               checklock(fd);
+               exit(0);
+       }
+
+       fd = open(argv[1], O_WRONLY|O_CREAT, 0755);
+       if (fd < 0)
+               err_exit("open", errno);
+       if (fcntl(fd, F_SETLK, &fl) < 0)
+               err_exit("setlk", errno);
+
+       /* require multithread process to reproduce the issue */
+       pthread_create(&th, NULL, thread_fn, &fd);
+
+       if ((flags = fcntl(fd, F_GETFD)) < 0)
+               err_exit("getfd", errno);
+       flags &= ~FD_CLOEXEC;
+       if (fcntl(fd, F_SETFD, flags) < 0)
+               err_exit("setfd", errno);
+
+       snprintf(fdstr, sizeof(fdstr), "%d", fd);
+       execve(argv[0], newargv, NULL);
+
+       return 0;
+}
diff --git a/tests/generic/484 b/tests/generic/484
new file mode 100755 (executable)
index 0000000..1c2cd96
--- /dev/null
@@ -0,0 +1,65 @@
+#! /bin/bash
+# FS QA Test 484
+#
+# POSIX requires that record locks are preserved across an execve(2). But
+# there's bug that record locks are released if process is multithreaded at the
+# time that execve is called.
+#
+# Fixed by patch from Jeff Layton:
+# locks: change POSIX lock ownership on execve when files_struct is displaced
+#
+#-----------------------------------------------------------------------
+# Copyright (c) 2018 Red Hat Inc.  All Rights Reserved.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write the Free Software Foundation,
+# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#-----------------------------------------------------------------------
+#
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1       # failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+       cd /
+       rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+# real QA test starts here
+_supported_fs generic
+_supported_os Linux
+_require_test
+
+# prepare a 4k testfile in TEST_DIR
+$XFS_IO_PROG -f -c "pwrite -S 0xFF 0 4096" \
+       $TEST_DIR/t_lock_execve_file >> $seqres.full 2>&1
+
+$here/src/t_locks_execve $TEST_DIR/t_lock_execve_file
+
+# success, all done
+echo "Silence is golden"
+status=0
+exit
diff --git a/tests/generic/484.out b/tests/generic/484.out
new file mode 100644 (file)
index 0000000..94f2f0b
--- /dev/null
@@ -0,0 +1,2 @@
+QA output created by 484
+Silence is golden
index ea8e51b35e7937cdb9f3577b1eea5d0ef2413fdc..19be9267af3f272adbcf46c845892f40d4812a6d 100644 (file)
 481 auto quick log metadata
 482 auto metadata replay
 483 auto quick log metadata
+484 auto quick