Skip to contentSkip to conversation

inotify11: Fix race condition between fork() and inotify init

PATCH In review Martin Doucha 3 participants 3 messages Sep 8, 2022 – Sep 9, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 inotify11: Fix race condition between fork() and inotify init

When the child process runs a few churn cycles before the parent completes inotify initialization, the parent will get stuck reading the inotify file descriptor until it eventually times out. Create inotify watch befory fork()ing to prevent this race condition.

---
 testcases/kernel/syscalls/inotify/inotify11.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/inotify/inotify11.c +4 -3
index e426f9e1c..dd32ea7fd 100644
@@ -61,15 +61,16 @@ static void verify_inotify(void)
 	struct inotify_event *event;
 	int inotify_fd;
 
+	inotify_fd = SAFE_MYINOTIFY_INIT();
+	SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
+
 	pid = SAFE_FORK();
 	if (pid == 0) {
+		SAFE_CLOSE(inotify_fd);
 		churn();
 		return;
 	}
 
-	inotify_fd = SAFE_MYINOTIFY_INIT();
-	SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
-
 	while (!opened && nevents < CHURN_FILES) {
 		int i, fd, len;
 
Signature · 5 lines
2.37.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Martin Doucha

When the child process runs a few churn cycles before the parent completes inotify initialization, the parent will get stuck reading the inotify file descriptor until it eventually times out. Create inotify watch befory fork()ing to prevent this race condition.

LGTM

Quoted 34 lines
---
 testcases/kernel/syscalls/inotify/inotify11.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/inotify/inotify11.c +4 -4
index e426f9e1c..dd32ea7fd 100644
@@ -61,15 +61,16 @@ static void verify_inotify(void)
        struct inotify_event *event;
        int inotify_fd;
 
+       inotify_fd = SAFE_MYINOTIFY_INIT();
+       SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
+
        pid = SAFE_FORK();
        if (pid == 0) {
+               SAFE_CLOSE(inotify_fd);
                churn();
                return;
        }
 
-       inotify_fd = SAFE_MYINOTIFY_INIT();
-       SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
-
        while (!opened && nevents < CHURN_FILES) {
                int i, fd, len;
 
--

2.37.2

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi all,

thanks, merged!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp