From 533deafbdf189f5fbb280c28562dd43ace2f4b0f Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Wed, 19 Apr 2023 19:02:03 +0300 Subject: Use O_CLOEXEC in more places (BZ #15722) When opening a temporary file without O_CLOEXEC we risk leaking the file descriptor if another thread calls (fork and then) exec while we have the fd open. Fix this by consistently passing O_CLOEXEC everywhere where we open a file for internal use (and not to return it to the user, in which case the API defines whether or not the close-on-exec flag shall be set on the returned fd). Reviewed-by: Adhemerval Zanella Signed-off-by: Sergey Bugaev Message-Id: <20230419160207.65988-4-bugaevc@gmail.com> --- gmon/gmon.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'gmon') diff --git a/gmon/gmon.c b/gmon/gmon.c index bc0e29438a..6439ed1caa 100644 --- a/gmon/gmon.c +++ b/gmon/gmon.c @@ -384,13 +384,14 @@ write_gmon (void) size_t len = strlen (env); char buf[len + 20]; __snprintf (buf, sizeof (buf), "%s.%u", env, __getpid ()); - fd = __open_nocancel (buf, O_CREAT|O_TRUNC|O_WRONLY|O_NOFOLLOW, 0666); + fd = __open_nocancel (buf, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW + | O_CLOEXEC, 0666); } if (fd == -1) { - fd = __open_nocancel ("gmon.out", O_CREAT|O_TRUNC|O_WRONLY|O_NOFOLLOW, - 0666); + fd = __open_nocancel ("gmon.out", O_CREAT | O_TRUNC | O_WRONLY + | O_NOFOLLOW | O_CLOEXEC, 0666); if (fd < 0) { char buf[300]; -- cgit 1.4.1