My program does the following in chronological order
- The program is started with root permissions.
- Among other tasks, A file only readable with root permissions is
open()
ed. - Root privileges are dropped.
- Child processes are spawned with
clone()
and theCLONE_FILES | CLONE_FS | CLONE_IO
flags set, which means that while they use separate regions of virtual memory, they share the same file descriptor table (and other IO stuff). - All child processes
execve()
their own programs (theFD_CLOEXEC
flag is not used). - The original program terminates.
Now I want every spawned program to read the contents of the aforementioned file, but after they all have read the file, I want it to be closed (for security reasons).
One possible solution I'm considering now is having a step 3a where the fd of the file is dup()
licated once for every child process, and each child gets its own fd (as an argv
). Then every child program would simply close()
their fd, so that after all fds pointing to the file are close()
d the "actual file" is closed.
But does it work that way? And is it safe to do this (i.e. is the file really closed)? If not, is there another/better method?