aboutgitcodebugslistschat
path: root/isolation.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2024-08-06 20:32:11 +0200
committerStefano Brivio <sbrivio@redhat.com>2024-08-08 21:31:25 +0200
commit09603cab28f9883baf1d7b48bdc102d6641dc300 (patch)
tree3ff30ff534d70179735e8f39354db6e418cb4cf0 /isolation.c
parent755f9fd91125c65361c81d8aa9e8af2cfd7adc6d (diff)
downloadpasst-09603cab28f9883baf1d7b48bdc102d6641dc300.tar
passt-09603cab28f9883baf1d7b48bdc102d6641dc300.tar.gz
passt-09603cab28f9883baf1d7b48bdc102d6641dc300.tar.bz2
passt-09603cab28f9883baf1d7b48bdc102d6641dc300.tar.lz
passt-09603cab28f9883baf1d7b48bdc102d6641dc300.tar.xz
passt-09603cab28f9883baf1d7b48bdc102d6641dc300.tar.zst
passt-09603cab28f9883baf1d7b48bdc102d6641dc300.zip
passt, util: Close any open file that the parent might have leaked
If a parent accidentally or due to implementation reasons leaks any open file, we don't want to have access to them, except for the file passed via --fd, if any. This is the case for Podman when Podman's parent leaks files into Podman: it's not practical for Podman to close unrelated files before starting pasta, as reported by Paul. Use close_range(2) to close all open files except for standard streams and the one from --fd. Given that parts of conf() depend on other files to be already opened, such as the epoll file descriptor, we can't easily defer this to a more convenient point, where --fd was already parsed. Introduce a minimal, duplicate version of --fd parsing to keep this simple. As we need to check that the passed --fd option doesn't exceed INT_MAX, because we'll parse it with strtol() but file descriptor indices are signed ints (regardless of the arguments close_range() take), extend the existing check in the actual --fd parsing in conf(), also rejecting file descriptors numbers that match standard streams, while at it. Suggested-by: Paul Holzinger <pholzing@redhat.com> Signed-off-by: Stefano Brivio <sbrivio@redhat.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au> Reviewed-by: Paul Holzinger <pholzing@redhat.com>
Diffstat (limited to 'isolation.c')
-rw-r--r--isolation.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/isolation.c b/isolation.c
index 4956d7e..45fba1e 100644
--- a/isolation.c
+++ b/isolation.c
@@ -29,7 +29,8 @@
*
* Executed immediately after startup, drops capabilities we don't
* need at any point during execution (or which we gain back when we
- * need by joining other namespaces).
+ * need by joining other namespaces), and closes any leaked file we
+ * might have inherited from the parent process.
*
* 2. isolate_user()
* =================
@@ -166,14 +167,17 @@ static void clamp_caps(void)
}
/**
- * isolate_initial() - Early, config independent self isolation
+ * isolate_initial() - Early, mostly config independent self isolation
+ * @argc: Argument count
+ * @argv: Command line options: only --fd (if present) is relevant here
*
* Should:
* - drop unneeded capabilities
+ * - close all open files except for standard streams and the one from --fd
* Musn't:
* - remove filesytem access (we need to access files during setup)
*/
-void isolate_initial(void)
+void isolate_initial(int argc, char **argv)
{
uint64_t keep;
@@ -207,6 +211,8 @@ void isolate_initial(void)
keep |= BIT(CAP_SETFCAP) | BIT(CAP_SYS_PTRACE);
drop_caps_ep_except(keep);
+
+ close_open_files(argc, argv);
}
/**