]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc.id_map bug when writing directly to /proc/pid/[ug]id_map [PATCH]
authorMiquel van Smoorenburg <mikevs@xs4all.net>
Wed, 5 Feb 2014 22:38:11 +0000 (23:38 +0100)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Thu, 6 Feb 2014 05:34:51 +0000 (23:34 -0600)
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@xs4all.net>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lxc/conf.c

index fed532737a13e780c33198d00b2fe4bed8f7946c..3e16cc8226e6eb41985af76f86e01fedc99e8fea 100644 (file)
@@ -3115,7 +3115,7 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
                }
                pos = buf;
                if (!am_root)
-                       pos += sprintf(buf, "new%cidmap %d ",
+                       pos += sprintf(buf, "new%cidmap %d",
                                type == ID_TYPE_UID ? 'u' : 'g',
                                pid);
 
@@ -3127,24 +3127,27 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
 
                        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;