Skip to contentSkip to conversation

generic: test record locks across execve

PATCH Superseded Xiong Zhou 3 participants 6 messages Apr 3, 2018 – May 2, 2018

lore
Share & tools
lore

Xiong Zhou

author
#0 lore raw

PATCH 1/1 generic: test record locks across execve

According to fcntl(2) man page, record locks are preserved across an execve(2). This is a regression case for this.

---

Thanks Daniel for the report and reproducer!
Jeff Layton is fixing this in kernel upstream.

 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/t_locks_execve.c  | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/generic/482     | 70 +++++++++++++++++++++++++++++++++++++++++
 tests/generic/482.out |  2 ++
 tests/generic/group   |  1 +
 6 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 src/t_locks_execve.c
 create mode 100755 tests/generic/482
 create mode 100644 tests/generic/482.out
.gitignore +1 -0
index 368d11c..192ca35 100644
@@ -121,6 +121,7 @@
 /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
src/Makefile +1 -1
index 0d3feae..6ca5636 100644
@@ -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 \
src/t_locks_execve.c new file +86 -0

86 lines not on this page · raw message

tests/generic/482 new file +70 -0

70 lines not on this page · raw message

tests/generic/482.out new file +2 -0
new file mode 100644
index 0000000..f5e17c8
@@ -0,0 +1,2 @@
+QA output created by 482
+Silence is golden
tests/generic/group +1 -0
index e867606..0bf3c1f 100644
@@ -484,3 +484,4 @@
 479 auto quick metadata
 480 auto quick metadata
 481 auto quick log metadata
+482 auto lock quick
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 4 lines · Xiong Zhou

According to fcntl(2) man page, record locks are preserved across an execve(2). This is a regression case for this.

Sorry for the late review..

Quoted 4 lines

---

Thanks Daniel for the report and reproducer!
Jeff Layton is fixing this in kernel upstream.

If it's already fixed, can you please reference the commit id in the log?

Quoted 70 lines
 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/t_locks_execve.c  | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/generic/482     | 70 +++++++++++++++++++++++++++++++++++++++++
 tests/generic/482.out |  2 ++
 tests/generic/group   |  1 +
 6 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 src/t_locks_execve.c
 create mode 100755 tests/generic/482
 create mode 100644 tests/generic/482.out
.gitignore +1 -0
index 368d11c..192ca35 100644
@@ -121,6 +121,7 @@
 /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
src/Makefile +1 -1
index 0d3feae..6ca5636 100644
@@ -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 \
src/t_locks_execve.c new file +27 -0
new file mode 100644
index 0000000..a93c6ca
@@ -0,0 +1,86 @@
+#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;
+}
+
+static void checklock(int fd)
+{

src/t_locks_execve.c line 37

Quoted 10 lines
src/t_locks_execve.c new file +10 -0
+	pid_t child = fork();
+	if (child < 0)
+		err_exit("fork", errno);
+	if (child == 0) {
+		struct flock fl = {
+			.l_type = F_WRLCK,
+			.l_whence = SEEK_SET,
+			.l_start = 0,
+			.l_len = 1,
+		};

The struct flock used here is the same as in main(), make it a global struct?

src/t_locks_execve.c line 43

Quoted 6 lines
src/t_locks_execve.c new file +6 -0
+
+		if (fcntl(fd, F_GETLK, &fl) < 0)
+			err_exit("getlk", errno);
+
+		if (fl.l_type == F_UNLCK)
+			printf("Lock released\n");

I'd like to see more descriptive error message here.

src/t_locks_execve.c line 44

Quoted 1 line
src/t_locks_execve.c new file +1 -0
+		exit(0);

exit with non-zero on failure?

src/t_locks_execve.c line 49

Quoted 5 lines
src/t_locks_execve.c new file +5 -0
+	}
+	waitpid(child, NULL, 0);
+}
+
+int main(int argc, char **argv) {

int main(...)
{
...
}

Quoted 13 lines
src/t_locks_execve.c new file +13 -0
+
+	int fd;
+	/* 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);
+	struct flock fl = {

src/t_locks_execve.c line 72

Quoted 10 lines
src/t_locks_execve.c new file +10 -0
+		.l_type = F_WRLCK,
+		.l_whence = SEEK_SET,
+		.l_start = 0,
+		.l_len = 1,
+	};
+	if (fcntl(fd, F_SETLK, &fl) < 0)
+		err_exit("setlk", errno);
+
+	pthread_t th;
+	pthread_create(&th, NULL, thread_fn, &fd);

I'm not sure why we need a thread here. Add more comments?

src/t_locks_execve.c line 83

Quoted 11 lines
src/t_locks_execve.c new file +11 -0
+
+	int flags;
+	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);
+
+	char fdstr[10];
+	snprintf(fdstr, sizeof(fdstr), "%d", fd);
+	char *newargv[] = { argv[0], argv[1], fdstr, NULL };

Better to declare all the variables at the beginning of main.

Quoted 13 lines
src/t_locks_execve.c new file +3 -0
+	execve(argv[0], newargv, NULL);
+	return 0;
+}
tests/generic/482 new file +4 -0
new file mode 100755
index 0000000..f7cb251
@@ -0,0 +1,70 @@
+#! /bin/bash
+# FS QA Test 482
+#
+# This is testing

tests/generic/482 line 14

Quoted 10 lines
tests/generic/482 new file +10 -0
+#
+#  "Record locks are not inherited by a child created via fork(2),
+#   but are preserved across an execve(2)."
+#
+# from fcntl(2) man page.
+#
+# Author: "Daniel P. Berrangé" <[email protected]>
+#
+#-----------------------------------------------------------------------
+# Copyright (c) 2018 RedHat Inc.  All Rights Reserved.

"Red Hat"?

Quoted 69 lines
tests/generic/482 new file +56 -0
+#
+# 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
+
+# Modify as appropriate.
+_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
+
+# run test programe
+$here/src/t_locks_execve $TEST_DIR/t_lock_execve_file
+
+# success, all done
+echo "Silence is golden"
+status=0
+exit
tests/generic/482.out new file +2 -0
new file mode 100644
index 0000000..f5e17c8
@@ -0,0 +1,2 @@
+QA output created by 482
+Silence is golden
tests/generic/group +0 -0
index e867606..0bf3c1f 100644

tests/generic/group line 487

Quoted 4 lines
tests/generic/group +1 -0
@@ -484,3 +484,4 @@
 479 auto quick metadata
 480 auto quick metadata
 481 auto quick log metadata
+482 auto lock quick

Please re-number it when resending :)

Thanks,
Eryu

Quoted 7 lines
Signature · 6 lines
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe fstests" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#2 lore raw

PATCH v2 1/1 generic: test record locks across execve

According to fcntl(2) man page, record locks are preserved across an execve(2). This is a regression case for this.

---
v2:
Do not fork when checking lock after execve;
More comments and some minor fixes.

Thanks Eryu for the review!
Jeff Layton's patch for this issue has not landed Linus tree.

 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/t_locks_execve.c  | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484     | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484.out |  2 ++
 tests/generic/group   |  1 +
 6 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 src/t_locks_execve.c
 create mode 100755 tests/generic/484
 create mode 100644 tests/generic/484.out
.gitignore +1 -0
index 368d11c8..192ca35e 100644
@@ -121,6 +121,7 @@
 /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
src/Makefile +1 -1
index 0d3feae1..6ca56366 100644
@@ -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 \
src/t_locks_execve.c new file +73 -0

73 lines not on this page · raw message

tests/generic/484 new file +73 -0

73 lines not on this page · raw message

tests/generic/484.out new file +2 -0
new file mode 100644
index 00000000..94f2f0bd
@@ -0,0 +1,2 @@
+QA output created by 484
+Silence is golden
tests/generic/group +1 -0
index ea8e51b3..798321f9 100644
@@ -486,3 +486,4 @@
 481 auto quick log metadata
 482 auto metadata replay
 483 auto quick log metadata
+484 auto lock quick
Signature · 1 line
2.17.0.252.gfe0a9ea
#3 lore raw
Quoted 2 lines · Xiong Zhou

According to fcntl(2) man page, record locks are preserved across an execve(2). This is a regression case for this.

After looking at the kernel patch, this commit log doesn't look accurate to me. The bug only happens if the process that calls execve(2) is a multithread process. And this case only excrises this particular case.

Please put more background information in commit log and the test description, this helps reviewers to understand the problem you're going to test. I find that the commit log from Jeff Layton's patch seems good enough:

"
POSIX mandates that open fds and their associated file locks should be preserved across an execve. This works, unless the process is multithreaded at the time that execve is called.

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.
"

This also answers my previous question: why do we need an idle thread, IMHO, "spawning thread is necessary to reproduce the issue" doesn't really answer the question. But with above description it's fine to have such comments in the code.

Quoted 90 lines

---
v2:
Do not fork when checking lock after execve;
More comments and some minor fixes.

Thanks Eryu for the review!
Jeff Layton's patch for this issue has not landed Linus tree.

 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/t_locks_execve.c  | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484     | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484.out |  2 ++
 tests/generic/group   |  1 +
 6 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 src/t_locks_execve.c
 create mode 100755 tests/generic/484
 create mode 100644 tests/generic/484.out
.gitignore +1 -0
index 368d11c8..192ca35e 100644
@@ -121,6 +121,7 @@
 /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
src/Makefile +1 -1
index 0d3feae1..6ca56366 100644
@@ -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 \
src/t_locks_execve.c new file +38 -0
new file mode 100644
index 00000000..dae4506d
@@ -0,0 +1,73 @@
+#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("The write lock got released during execve\n");

src/t_locks_execve.c line 48

Quoted 10 lines
src/t_locks_execve.c new file +10 -0
+		exit(1);
+	}
+	exit(0);
+}
+
+int main(int argc, char **argv)
+{
+	int fd, flags;
+	char fdstr[10];
+	char *newargv[] = { argv[0], argv[1], fdstr, NULL };

We could add new line to make the code more readable, e.g. right here.

src/t_locks_execve.c line 54

Quoted 6 lines
src/t_locks_execve.c new file +6 -0
+	/* passing fd in argv[2] in execve */
+	if (argc == 3) {
+		fd = atoi(argv[2]);
+		checklock(fd);
+		exit(0);
+	}

And here :)

Quoted 82 lines
src/t_locks_execve.c new file +19 -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);
+
+	/* spawning thread is necessary to reproduce the issue */
+	pthread_t th;
+	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;
+}
tests/generic/484 new file +57 -0
new file mode 100755
index 00000000..76ebf1fd
@@ -0,0 +1,73 @@
+#! /bin/bash
+# FS QA Test 484
+#
+# This is testing
+#
+#  "Record locks are not inherited by a child created via fork(2),
+#   but are preserved across an execve(2)."
+#
+# from fcntl(2) man page.
+#
+# Fixed by patch from Jeff Layton:
+# locks: change POSIX lock ownership on execve when files_struct is displaced
+#
+# Author: "Daniel P. Berrangé" <[email protected]>
+#
+#-----------------------------------------------------------------------
+# 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
+

tests/generic/484 line 67

Quoted 10 lines
tests/generic/484 new file +10 -0
+# Modify as appropriate.
+_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
+
+# run test programe

Unnecessary comment.

tests/generic/484 line 68

Quoted 1 line
tests/generic/484 new file +1 -0
+$here/src/t_locks_execve $TEST_DIR/t_lock_execve_file

I can fix the commit log and test description and the code style issue on commit, no need to resend.

Thanks,
Eryu

Quoted 25 lines
tests/generic/484 new file +5 -0
+
+# success, all done
+echo "Silence is golden"
+status=0
+exit
tests/generic/484.out new file +2 -0
new file mode 100644
index 00000000..94f2f0bd
@@ -0,0 +1,2 @@
+QA output created by 484
+Silence is golden
tests/generic/group +1 -0
index ea8e51b3..798321f9 100644
@@ -486,3 +486,4 @@
 481 auto quick log metadata
 482 auto metadata replay
 483 auto quick log metadata
+484 auto lock quick
Signature · 1 line
2.17.0.252.gfe0a9ea
#4 lore raw
Quoted 24 lines · Eryu Guan
Quoted 2 lines · Xiong Zhou

According to fcntl(2) man page, record locks are preserved across an execve(2). This is a regression case for this.

After looking at the kernel patch, this commit log doesn't look accurate to me. The bug only happens if the process that calls execve(2) is a multithread process. And this case only excrises this particular case.

Please put more background information in commit log and the test description, this helps reviewers to understand the problem you're going to test. I find that the commit log from Jeff Layton's patch seems good enough:

"
POSIX mandates that open fds and their associated file locks should be preserved across an execve. This works, unless the process is multithreaded at the time that execve is called.

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.

Quoted 8 lines

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

This also answers my previous question: why do we need an idle thread, IMHO, "spawning thread is necessary to reproduce the issue" doesn't really answer the question. But with above description it's fine to have such comments in the code.

Thank you! Lesson learned :)

I was too lazy to get into details. Thumb up for you!

Thanks,
Xiong

Quoted 241 lines
Quoted 91 lines

---
v2:
Do not fork when checking lock after execve;
More comments and some minor fixes.

Thanks Eryu for the review!
Jeff Layton's patch for this issue has not landed Linus tree.

 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/t_locks_execve.c  | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484     | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484.out |  2 ++
 tests/generic/group   |  1 +
 6 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 src/t_locks_execve.c
 create mode 100755 tests/generic/484
 create mode 100644 tests/generic/484.out
.gitignore +1 -0
index 368d11c8..192ca35e 100644
@@ -121,6 +121,7 @@
 /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
src/Makefile +1 -1
index 0d3feae1..6ca56366 100644
@@ -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 \
src/t_locks_execve.c new file +38 -0
new file mode 100644
index 00000000..dae4506d
@@ -0,0 +1,73 @@
+#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("The write lock got released during execve\n");

src/t_locks_execve.c line 48

Quoted 10 lines
src/t_locks_execve.c new file +10 -0
+		exit(1);
+	}
+	exit(0);
+}
+
+int main(int argc, char **argv)
+{
+	int fd, flags;
+	char fdstr[10];
+	char *newargv[] = { argv[0], argv[1], fdstr, NULL };

We could add new line to make the code more readable, e.g. right here.

src/t_locks_execve.c line 54

Quoted 6 lines
src/t_locks_execve.c new file +6 -0
+	/* passing fd in argv[2] in execve */
+	if (argc == 3) {
+		fd = atoi(argv[2]);
+		checklock(fd);
+		exit(0);
+	}

And here :)

Quoted 82 lines
src/t_locks_execve.c new file +19 -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);
+
+	/* spawning thread is necessary to reproduce the issue */
+	pthread_t th;
+	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;
+}
tests/generic/484 new file +57 -0
new file mode 100755
index 00000000..76ebf1fd
@@ -0,0 +1,73 @@
+#! /bin/bash
+# FS QA Test 484
+#
+# This is testing
+#
+#  "Record locks are not inherited by a child created via fork(2),
+#   but are preserved across an execve(2)."
+#
+# from fcntl(2) man page.
+#
+# Fixed by patch from Jeff Layton:
+# locks: change POSIX lock ownership on execve when files_struct is displaced
+#
+# Author: "Daniel P. Berrangé" <[email protected]>
+#
+#-----------------------------------------------------------------------
+# 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
+

tests/generic/484 line 67

Quoted 10 lines
tests/generic/484 new file +10 -0
+# Modify as appropriate.
+_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
+
+# run test programe

Unnecessary comment.

tests/generic/484 line 68

Quoted 1 line
tests/generic/484 new file +1 -0
+$here/src/t_locks_execve $TEST_DIR/t_lock_execve_file

I can fix the commit log and test description and the code style issue on commit, no need to resend.

Thanks,
Eryu

Quoted 25 lines
tests/generic/484 new file +5 -0
+
+# success, all done
+echo "Silence is golden"
+status=0
+exit
tests/generic/484.out new file +2 -0
new file mode 100644
index 00000000..94f2f0bd
@@ -0,0 +1,2 @@
+QA output created by 484
+Silence is golden
tests/generic/group +1 -0
index ea8e51b3..798321f9 100644
@@ -486,3 +486,4 @@
 481 auto quick log metadata
 482 auto metadata replay
 483 auto quick log metadata
+484 auto lock quick
Signature · 1 line
2.17.0.252.gfe0a9ea
#5 lore raw
Quoted 34 lines · Xiong Murphy Zhou
Quoted 24 lines · Eryu Guan
Quoted 2 lines · Xiong Zhou

According to fcntl(2) man page, record locks are preserved across an execve(2). This is a regression case for this.

After looking at the kernel patch, this commit log doesn't look accurate to me. The bug only happens if the process that calls execve(2) is a multithread process. And this case only excrises this particular case.

Please put more background information in commit log and the test description, this helps reviewers to understand the problem you're going to test. I find that the commit log from Jeff Layton's patch seems good enough:

"
POSIX mandates that open fds and their associated file locks should be preserved across an execve. This works, unless the process is multithreaded at the time that execve is called.

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.

Quoted 8 lines

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

This also answers my previous question: why do we need an idle thread, IMHO, "spawning thread is necessary to reproduce the issue" doesn't really answer the question. But with above description it's fine to have such comments in the code.

Quoted 7 lines

Thank you! Lesson learned :)

I was too lazy to get into details. Thumb up for you!

Thanks,
Xiong

Thanks for adding the test.

Just to be clear, the patch I had originally proposed for this is not going to work across all filesystems. We'll probably have to fix this in a different way, but it's not clear yet what the fix should look like.

Quoted 246 lines
Quoted 233 lines
Quoted 91 lines

---
v2:
Do not fork when checking lock after execve;
More comments and some minor fixes.

Thanks Eryu for the review!
Jeff Layton's patch for this issue has not landed Linus tree.

 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/t_locks_execve.c  | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484     | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/484.out |  2 ++
 tests/generic/group   |  1 +
 6 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 src/t_locks_execve.c
 create mode 100755 tests/generic/484
 create mode 100644 tests/generic/484.out
.gitignore +1 -0
index 368d11c8..192ca35e 100644
@@ -121,6 +121,7 @@
 /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
src/Makefile +1 -1
index 0d3feae1..6ca56366 100644
@@ -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 \
src/t_locks_execve.c new file +38 -0
new file mode 100644
index 00000000..dae4506d
@@ -0,0 +1,73 @@
+#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("The write lock got released during execve\n");

src/t_locks_execve.c line 48

Quoted 10 lines
src/t_locks_execve.c new file +10 -0
+		exit(1);
+	}
+	exit(0);
+}
+
+int main(int argc, char **argv)
+{
+	int fd, flags;
+	char fdstr[10];
+	char *newargv[] = { argv[0], argv[1], fdstr, NULL };

We could add new line to make the code more readable, e.g. right here.

src/t_locks_execve.c line 54

Quoted 6 lines
src/t_locks_execve.c new file +6 -0
+	/* passing fd in argv[2] in execve */
+	if (argc == 3) {
+		fd = atoi(argv[2]);
+		checklock(fd);
+		exit(0);
+	}

And here :)

Quoted 82 lines
src/t_locks_execve.c new file +19 -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);
+
+	/* spawning thread is necessary to reproduce the issue */
+	pthread_t th;
+	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;
+}
tests/generic/484 new file +57 -0
new file mode 100755
index 00000000..76ebf1fd
@@ -0,0 +1,73 @@
+#! /bin/bash
+# FS QA Test 484
+#
+# This is testing
+#
+#  "Record locks are not inherited by a child created via fork(2),
+#   but are preserved across an execve(2)."
+#
+# from fcntl(2) man page.
+#
+# Fixed by patch from Jeff Layton:
+# locks: change POSIX lock ownership on execve when files_struct is displaced
+#
+# Author: "Daniel P. Berrangé" <[email protected]>
+#
+#-----------------------------------------------------------------------
+# 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
+

tests/generic/484 line 67

Quoted 10 lines
tests/generic/484 new file +10 -0
+# Modify as appropriate.
+_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
+
+# run test programe

Unnecessary comment.

tests/generic/484 line 68

Quoted 1 line
tests/generic/484 new file +1 -0
+$here/src/t_locks_execve $TEST_DIR/t_lock_execve_file

I can fix the commit log and test description and the code style issue on commit, no need to resend.

Thanks,
Eryu

Quoted 17 lines
tests/generic/484 new file +5 -0
+
+# success, all done
+echo "Silence is golden"
+status=0
+exit
tests/generic/484.out new file +2 -0
new file mode 100644
index 00000000..94f2f0bd
@@ -0,0 +1,2 @@
+QA output created by 484
+Silence is golden
tests/generic/group +0 -0
index ea8e51b3..798321f9 100644
Quoted 8 lines
Quoted 8 lines
tests/generic/group +1 -0
@@ -486,3 +486,4 @@
 481 auto quick log metadata
 482 auto metadata replay
 483 auto quick log metadata
+484 auto lock quick
Signature · 1 line
2.17.0.252.gfe0a9ea

--
To unsubscribe from this list: send the line "unsubscribe fstests" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
Jeff Layton <[email protected]>