aboutgitcodebugslistschat
path: root/isolation.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2022-10-14 15:25:32 +1100
committerStefano Brivio <sbrivio@redhat.com>2022-10-15 02:10:36 +0200
commitea5936dd3f6293fb761e3b670a0f40233e5396fd (patch)
treeff4744bab430784cd416bc063902022018e905bf /isolation.c
parent096e48669b56273d44a3242d7af4840b38335961 (diff)
downloadpasst-ea5936dd3f6293fb761e3b670a0f40233e5396fd.tar
passt-ea5936dd3f6293fb761e3b670a0f40233e5396fd.tar.gz
passt-ea5936dd3f6293fb761e3b670a0f40233e5396fd.tar.bz2
passt-ea5936dd3f6293fb761e3b670a0f40233e5396fd.tar.lz
passt-ea5936dd3f6293fb761e3b670a0f40233e5396fd.tar.xz
passt-ea5936dd3f6293fb761e3b670a0f40233e5396fd.tar.zst
passt-ea5936dd3f6293fb761e3b670a0f40233e5396fd.zip
Replace FWRITE with a function
In a few places we use the FWRITE() macro to open a file, replace it's contents with a given string and close it again. There's no real reason this needs to be a macro rather than just a function though. Turn it into a function 'write_file()' and make some ancillary cleanups while we're there: - Add a return code so the caller can handle giving a useful error message - Handle the case of short write()s (unlikely, but possible) - Add O_TRUNC, to make sure we replace the existing contents entirely Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'isolation.c')
-rw-r--r--isolation.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/isolation.c b/isolation.c
index a3ea842..af0d33a 100644
--- a/isolation.c
+++ b/isolation.c
@@ -130,7 +130,8 @@ void isolate_initial(void)
*/
void isolate_user(uid_t uid, gid_t gid, bool use_userns, const char *userns)
{
- char nsmap[BUFSIZ];
+ char uidmap[BUFSIZ];
+ char gidmap[BUFSIZ];
/* First set our UID & GID in the original namespace */
if (setgroups(0, NULL)) {
@@ -185,14 +186,14 @@ void isolate_user(uid_t uid, gid_t gid, bool use_userns, const char *userns)
}
/* Configure user and group mappings */
- snprintf(nsmap, BUFSIZ, "0 %u 1", uid);
- FWRITE("/proc/self/uid_map", nsmap, "Cannot set uid_map in namespace");
+ snprintf(uidmap, BUFSIZ, "0 %u 1", uid);
+ snprintf(gidmap, BUFSIZ, "0 %u 1", gid);
- FWRITE("/proc/self/setgroups", "deny",
- "Cannot write to setgroups in namespace");
-
- snprintf(nsmap, BUFSIZ, "0 %u 1", gid);
- FWRITE("/proc/self/gid_map", nsmap, "Cannot set gid_map in namespace");
+ if (write_file("/proc/self/uid_map", uidmap) ||
+ write_file("/proc/self/setgroups", "deny") ||
+ write_file("/proc/self/gid_map", gidmap)) {
+ warn("Couldn't configure user namespace");
+ }
}
/**