From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: Dominik Csapak <d.csapak@proxmox.com>,
Proxmox Backup Server development discussion
<pbs-devel@lists.proxmox.com>
Subject: Re: [pbs-devel] [PATCH proxmox] sys: fs: set FD_CLOEXEC when creating temp files
Date: Fri, 29 Nov 2024 09:14:18 +0100 (CET) [thread overview]
Message-ID: <708352934.1879.1732868058806@webmail.proxmox.com> (raw)
In-Reply-To: <ae0dba6b-c079-41ed-8c17-313d7a05de06@proxmox.com>
> Dominik Csapak <d.csapak@proxmox.com> hat am 29.11.2024 09:02 CET geschrieben:
> On 11/29/24 08:57, Fabian Grünbichler wrote:
> >> Dominik Csapak <d.csapak@proxmox.com> hat am 28.11.2024 15:54 CET geschrieben:
> >> @@ -128,7 +128,10 @@ pub fn make_tmp_file<P: AsRef<Path>>(
> >> let mut template = path.to_owned();
> >> template.set_extension("tmp_XXXXXX");
> >> let (mut file, tmp_path) = match unistd::mkstemp(&template) {
> >> - Ok((fd, path)) => (unsafe { File::from_raw_fd(fd) }, path),
> >> + Ok((fd, path)) => {
> >> + nix::fcntl::fcntl(fd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))?;
> >> + (unsafe { File::from_raw_fd(fd) }, path)
> >> + }
> >
> > unfortunately, this is still racy since the FD is open with O_CLOEXEC between the unistd::mkstemp and the fcntl - see the man page of fcntl which explicitly calls this out:
> >
> > "In multithreaded programs, using fcntl() F_SETFD to set the close-on-exec flag at the same time as another thread performs a fork(2) plus execve(2) is vulnerable to a race condition that may unintentionally leak the file descriptor to the program executed in the child process."
> >
> > we could use libc::mkostemp (unsafe, path/template+flags -> raw fd or error as c_int) instead? and/or we could write a wrapper around it and propose it upstream for nix inclusion? ;) but since this seems to be the only place where we call mkstemp..
> >
>
> yeah had the same though, and have a version here where i basically copied nix's mkstemp but with
> oflags + mkostemp call to libc, so i can send that if wanted
>
> the question for me is if it's ok to use since mkostemp is only a glibc extension (since 2.7) and we
> may use that in proxmox-backup-client which we want to statically build ?
> (not sure how that static compilation works with such a thing though...)
that's a fair argument, if we want that it looks like we'd have to reimplement mkostemp ourselves I guess.. or we abandon this approach and evaluate allow-listing FDs we want to keep, and closing all others on fork.. but then we have to ensure no forking ever happens outside of our immediate control, which seems kinda annoying as well..
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
next prev parent reply other threads:[~2024-11-29 8:14 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-28 14:54 Dominik Csapak
2024-11-29 7:57 ` Fabian Grünbichler
2024-11-29 8:02 ` Dominik Csapak
2024-11-29 8:14 ` Fabian Grünbichler [this message]
2024-11-29 10:21 ` Thomas Lamprecht
2024-11-29 12:31 ` Thomas Lamprecht
2024-11-29 13:12 ` Dominik Csapak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=708352934.1879.1732868058806@webmail.proxmox.com \
--to=f.gruenbichler@proxmox.com \
--cc=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox