Skip to contentSkip to conversation

pseudo_util.c: Open file with O_CLOEXEC to avoid fd leak

PATCH In review Yu, Mingli 3 participants 5 messages Apr 9, 2020

lore
Share & tools
lore

Yu, Mingli

author
#0 lore raw

PATCH 1/1 pseudo_util.c: Open file with O_CLOEXEC to avoid fd leak

From: Pavel Modilaynen <[email protected]>

Use close-on-exec (O_CLOEXEC) flag when open log file to make sure its file descriptor is not leaked to parent process on fork/exec.

---
 pseudo_util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pseudo_util.c +1 -1
index c867ed6..0ec527b 100644
@@ -1594,7 +1594,7 @@ pseudo_logfile(char *filename, char *defname, int prefer_fd) {
 		}
 		free(filename);
 	}	
-	fd = open(pseudo_path, O_WRONLY | O_APPEND | O_CREAT, 0644);
+	fd = open(pseudo_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
 	if (fd == -1) {
 		pseudo_diag("help: can't open log file %s: %s\n", pseudo_path, strerror(errno));
 	} else {
Signature · 1 line
2.7.4
#1 lore raw
Quoted 3 lines · Mingli
(quoted diff) +1 -1
-	fd = open(pseudo_path, O_WRONLY | O_APPEND | O_CREAT, 0644);
+	fd = open(pseudo_path, O_WRONLY | O_APPEND | O_CREAT |

O_CLOEXEC, 0644);

I'm not confident in this.

This code is shared between the pseudo client and the pseudo server. In the pseudo server, it's possible that we coincidentally end up with fd being 2 already. In that case, we'd end up with this fd on 2, but then on exec (such as when execing the server) we'd possibly end up with it closed?

Looking more closely, dup2 will clear the CLOEXEC flag, so in the server path, if prefer_fd is 2 and fd doesn't start out as 2, we end up with it cleared...

But also so far as I can tell the code around the dup2 call there is probably generally wrong; it's explicitly closing the previous holder of that descriptor, which is unnecessary, and checking whether the file descriptor is already the desired one, which is also unnecessary. So this might need to be revisited.

Looking at it more, the only time this seems like it should be relevant would be in the case where we're in the pseudo client, but PSEUDO_DEBUG_FILE is set in the environment. Otherwise, the client should end up just using stderr, and the server shouldn't care because it's expecting this to be fd 2 and we probably want the child process to end up with fd 2 open.

What's the observed failure mode that we're fixing?

-s

author
#2 lore raw

Thanks Seebs's update!

We are trying to fix the issue as http://bugzilla.yoctoproject.org/show_bug.cgi?id=13311 stated.

Thanks,
Mingli
________________________________
From: Seebs <[email protected]>
Sent: Thursday, April 9, 2020 13:25
To: Yu, Mingli <[email protected]>
Cc: [email protected] <[email protected]>; MacLeod, Randy <[email protected]>; [email protected] <[email protected]>
Subject: Re: [OE-core] [pseudo][PATCH] pseudo_util.c: Open file with O_CLOEXEC to avoid fd leak

Quoted 3 lines · Mingli
(quoted diff) +1 -1
-     fd = open(pseudo_path, O_WRONLY | O_APPEND | O_CREAT, 0644);
+     fd = open(pseudo_path, O_WRONLY | O_APPEND | O_CREAT |

O_CLOEXEC, 0644);

I'm not confident in this.

This code is shared between the pseudo client and the pseudo server. In the pseudo server, it's possible that we coincidentally end up with fd being 2 already. In that case, we'd end up with this fd on 2, but then on exec (such as when execing the server) we'd possibly end up with it closed?

Looking more closely, dup2 will clear the CLOEXEC flag, so in the server path, if prefer_fd is 2 and fd doesn't start out as 2, we end up with it cleared...

But also so far as I can tell the code around the dup2 call there is probably generally wrong; it's explicitly closing the previous holder of that descriptor, which is unnecessary, and checking whether the file descriptor is already the desired one, which is also unnecessary. So this might need to be revisited.

Looking at it more, the only time this seems like it should be relevant would be in the case where we're in the pseudo client, but PSEUDO_DEBUG_FILE is set in the environment. Otherwise, the client should end up just using stderr, and the server shouldn't care because it's expecting this to be fd 2 and we probably want the child process to end up with fd 2 open.

What's the observed failure mode that we're fixing?

-s

#3 lore raw
Quoted 2 lines · Mingli

We are trying to fix the issue as
http://bugzilla.yoctoproject.org/show_bug.cgi?id=13311 stated.

Hmm. I think there's a more subtle logic bug here, and also one of the diagnostics there is a debug message I didn't notice when I made the commit, but it never comes up unless you run with PSEUDO_DEBUG_FILE.

I'm gonna think about it more. I'm now about half convinced that the patch is actually fine as-is for clients, it's just on the server side that I think we'd see problems.

-s

#4 lore raw

✗ patchtest: failure for pseudo_util.c: Open file with O_CLOEXEC to avoid fd leak

In reply to Yu, Mingli

== Series Details ==

Series: pseudo_util.c: Open file with O_CLOEXEC to avoid fd leak
Revision: 1
URL : https://patchwork.openembedded.org/series/23664/
State : failure

== Summary ==

Thank you for submitting this patch series to OpenEmbedded Core. This is an automated response. Several tests have been executed on the proposed series by patchtest resulting in the following failures:

* Issue Series does not apply on top of target branch [test_series_merge_on_head]
Suggested fix Rebase your series on top of targeted branch
Targeted branch master (currently at 1795f30d8a)

If you believe any of these test results are incorrect, please reply to the mailing list ([email protected]) raising your concerns. Otherwise we would appreciate you correcting the issues and submitting a new version of the patchset if applicable. Please ensure you add/increment the version number when sending the new version (i.e. [PATCH] -> [PATCH v2] -> [PATCH v3] -> ...).

---

Guidelines:     https://www.openembedded.org/wiki/Commit_Patch_Message_Guidelines
Test framework: http://git.yoctoproject.org/cgit/cgit.cgi/patchtest
Test suite:     http://git.yoctoproject.org/cgit/cgit.cgi/patchtest-oe