From c433319551329ff709173bb08541d0fe015d7265 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Tue, 2 Jan 2018 22:15:17 +0100 Subject: [PATCH] conf: adapt idmap helpers - mapped_hostid_entry() - idmap_add() Closes #2033. Signed-off-by: Christian Brauner --- src/lxc/conf.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 38f868afb..5617ff35f 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3722,8 +3722,8 @@ static int run_userns_fn(void *data) return d->fn(d->arg); } -static struct id_map *mapped_hostid_entry(struct lxc_conf *conf, unsigned id, - enum idtype idtype) +static struct id_map *find_mapped_hostid_entry(struct lxc_conf *conf, + unsigned id, enum idtype idtype) { struct lxc_list *it; struct id_map *map; @@ -3740,14 +3740,6 @@ static struct id_map *mapped_hostid_entry(struct lxc_conf *conf, unsigned id, } } - if (!retmap) - return NULL; - - retmap = malloc(sizeof(*retmap)); - if (!retmap) - return NULL; - - memcpy(retmap, map, sizeof(*retmap)); return retmap; } @@ -3755,27 +3747,28 @@ static struct id_map *mapped_hostid_entry(struct lxc_conf *conf, unsigned id, * Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already * existing one or establish a new one. */ -static struct id_map *idmap_add(struct lxc_conf *conf, uid_t id, enum idtype type) +static struct id_map *mapped_hostid_add(struct lxc_conf *conf, uid_t id, enum idtype type) { int hostid_mapped; - struct id_map *entry = NULL; + struct id_map *entry = NULL, *tmp = NULL; + + entry = malloc(sizeof(*entry)); + if (!entry) + return NULL; /* Reuse existing mapping. */ - entry = mapped_hostid_entry(conf, id, type); - if (entry) - return entry; + tmp = find_mapped_hostid_entry(conf, id, type); + if (tmp) + return memcpy(entry, tmp, sizeof(*entry)); /* Find new mapping. */ hostid_mapped = find_unmapped_nsid(conf, type); if (hostid_mapped < 0) { - DEBUG("failed to find free mapping for id %d", id); + DEBUG("Failed to find free mapping for id %d", id); + free(entry); return NULL; } - entry = malloc(sizeof(*entry)); - if (!entry) - return NULL; - entry->idtype = type; entry->nsid = hostid_mapped; entry->hostid = (unsigned long)id; @@ -3884,10 +3877,10 @@ int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data, /* Check whether the {g,u}id of the user has a mapping. */ if (!host_uid_map) - host_uid_map = idmap_add(conf, euid, ID_TYPE_UID); + host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); if (!host_gid_map) - host_gid_map = idmap_add(conf, egid, ID_TYPE_GID); + host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); if (!host_uid_map) { DEBUG("failed to find mapping for uid %d", euid); @@ -4095,12 +4088,12 @@ int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data, /* Check whether the {g,u}id of the user has a mapping. */ if (!host_uid_map) - host_uid_map = idmap_add(conf, euid, ID_TYPE_UID); + host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); else host_uid_map = container_root_uid; if (!host_gid_map) - host_gid_map = idmap_add(conf, egid, ID_TYPE_GID); + host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); else host_gid_map = container_root_gid; -- 2.47.2