[lxc-devel] lxc.id_map bug when writing directly to /proc/pid/[ug]id_map [PATCH]

Miquel van Smoorenburg mikevs at xs4all.net
Wed Feb 5 22:38:11 UTC 2014


lxc.id_map bug when writing directly to /proc/pid/[ug]id_map

There's some code in src/lxc/conf.c that sets up the UID/GID mapping. It
can use the external newuidmap/newgidmap tools, or it can write to
/proc/pid/[ug]id_map directly. The latter case is broken: lines are written
without a newline (\n) at the end. This patch fixes that. Note that
I did not check if the newuidmap/newgidmap case still works. It should,
but I wasn't able to test it.

Signed-off-by: Miquel van Smoorenburg <mikevs at xs4all.net>

--- lxc-1.0.0~beta1.orig/src/lxc/conf.c
+++ lxc-1.0.0~beta1/src/lxc/conf.c
@@ -3147,7 +3147,7 @@ int lxc_map_ids(struct lxc_list *idmap,
 		}
 		pos = buf;
 		if (!am_root)
-			pos += sprintf(buf, "new%cidmap %d ",
+			pos += sprintf(buf, "new%cidmap %d",
 				type == ID_TYPE_UID ? 'u' : 'g',
 				pid);
 
@@ -3159,24 +3159,27 @@ int lxc_map_ids(struct lxc_list *idmap,
 
 			had_entry = 1;
 			left = 4096 - (pos - buf);
-			fill = snprintf(pos, left, " %lu %lu %lu", map->nsid,
-					map->hostid, map->range);
+			fill = snprintf(pos, left, "%s%lu %lu %lu%s",
+					am_root ? "" : " ",
+					map->nsid, map->hostid, map->range,
+					am_root ? "\n" : "");
 			if (fill <= 0 || fill >= left)
 				SYSERROR("snprintf failed, too many mappings");
 			pos += fill;
 		}
 		if (!had_entry)
 			continue;
-		left = 4096 - (pos - buf);
-		fill = snprintf(pos, left, "\n");
-		if (fill <= 0 || fill >= left)
-			SYSERROR("snprintf failed, too many mappings");
-		pos += fill;
 
-		if (am_root)
+		if (am_root) {
 			ret = write_id_mapping(type, pid, buf, pos-buf);
-		else
+		} else {
+			left = 4096 - (pos - buf);
+			fill = snprintf(pos, left, "\n");
+			if (fill <= 0 || fill >= left)
+				SYSERROR("snprintf failed, too many mappings");
+			pos += fill;
 			ret = system(buf);
+		}
 
 		if (ret)
 			break;


More information about the lxc-devel mailing list