From: Christian Brauner Date: Tue, 2 Jan 2018 21:15:17 +0000 (+0100) Subject: conf: adapt idmap helpers X-Git-Tag: lxc-2.0.10~438 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ef659aaf61b57aeda16cf1244a0dbf4cd5cc7740;p=thirdparty%2Flxc.git conf: adapt idmap helpers - mapped_hostid_entry() - idmap_add() Closes #2033. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 3aeed5c2d..126faee12 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3474,8 +3474,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; @@ -3492,14 +3492,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; } @@ -3507,27 +3499,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; @@ -3636,10 +3629,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); @@ -3847,12 +3840,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;