new[ug]idmap: not require CAP_SYS_ADMIN in the parent userNS

if the euid!=owner of the userns, the kernel returns EPERM when trying
to write the uidmap and there is no CAP_SYS_ADMIN in the parent
namespace.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2018-10-08 18:18:18 +02:00
parent 6bf2d74dfc
commit 1ecca8439d
6 changed files with 50 additions and 7 deletions

View File

@@ -36,6 +36,10 @@
#include <stdio.h>
#include "prototypes.h"
#include "idmapping.h"
#include <sys/prctl.h>
#if HAVE_SYS_CAPABILITY_H
# include <sys/capability.h>
#endif
struct map_range *get_map_ranges(int ranges, int argc, char **argv)
{
@@ -121,17 +125,56 @@ struct map_range *get_map_ranges(int ranges, int argc, char **argv)
void write_mapping(int proc_dir_fd, int ranges, struct map_range *mappings,
const char *map_file)
const char *map_file, uid_t uid)
{
int idx;
struct map_range *mapping;
size_t bufsize;
char *buf, *pos;
int fd;
#if HAVE_SYS_CAPABILITY_H
struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3, 0 };
struct __user_cap_data_struct data[2] = { { 0 } };
#endif
bufsize = ranges * ((ULONG_DIGITS + 1) * 3);
pos = buf = xmalloc(bufsize);
#if HAVE_SYS_CAPABILITY_H
if (capget(&hdr, data) < 0) {
fprintf(stderr, _("%s: Could not get capabilities\n"), Prog);
exit(EXIT_FAILURE);
}
if (!(data[0].effective & CAP_TO_MASK(CAP_SYS_ADMIN)) &&
uid != geteuid()) {
bool uid_map;
if (strcmp(map_file, "uid_map") == 0) {
uid_map = true;
} else if (strcmp(map_file, "gid_map") == 0) {
uid_map = false;
} else {
fprintf(stderr, _("%s: Invalid map file %s specified\n"), Prog, map_file);
exit(EXIT_FAILURE);
}
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
fprintf(stderr, _("%s: Could not prctl(PR_SET_KEEPCAPS)\n"), Prog);
exit(EXIT_FAILURE);
}
if (seteuid(uid) < 0) {
fprintf(stderr, _("%s: Could not seteuid to %d\n"), Prog, uid);
exit(EXIT_FAILURE);
}
memset(data, 0, sizeof(data));
data[0].effective = data[0].permitted = CAP_TO_MASK(uid_map ? CAP_SETUID : CAP_SETGID);
if (capset(&hdr, data) < 0) {
fprintf(stderr, _("%s: Could not set caps\n"), Prog);
exit(EXIT_FAILURE);
}
}
#endif
/* Build the mapping command */
mapping = mappings;
for (idx = 0; idx < ranges; idx++, mapping++) {