test: add helpers for user namespace mapping
authorDjalal Harouni <tixxdz@opendz.org>
Mon, 18 Aug 2014 14:38:03 +0000 (15:38 +0100)
committerDaniel Mack <zonque@gmail.com>
Tue, 19 Aug 2014 07:16:48 +0000 (09:16 +0200)
Will be used by other tests.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test/kdbus-util.c
test/kdbus-util.h

index a979c99dc08245511bd795483199d373631e440f..e59254d458886bc33ae7afe362290dd0c2bc3679 100644 (file)
@@ -795,3 +795,53 @@ int drop_privileges(uid_t uid, gid_t gid)
 
        return ret;
 }
+
+static int do_userns_map_id(pid_t pid,
+                           const char *map_file,
+                           const char *map_id)
+{
+       int ret;
+       int fd;
+
+       fd = open(map_file, O_RDWR);
+       if (fd < 0) {
+               ret = -errno;
+               fprintf(stderr, "error open %s: %d (%m)\n",
+                       map_file, ret);
+               return ret;
+       }
+
+       ret = write(fd, map_id, strlen(map_id));
+       if (ret < 0) {
+               ret = -errno;
+               fprintf(stderr, "error write to %s: %d (%m)\n",
+                       map_file, ret);
+               goto out;
+       }
+
+       ret = 0;
+
+out:
+       close(fd);
+       return ret;
+}
+
+int userns_map_uid_gid(pid_t pid,
+                      const char *map_uid,
+                      const char *map_gid)
+{
+       int ret;
+       char file_id[128] = {'\0'};
+
+       snprintf(file_id, sizeof(file_id), "/proc/%ld/uid_map",
+                (long) pid);
+
+       ret = do_userns_map_id(pid, file_id, map_uid);
+       if (ret < 0)
+               return ret;
+
+       snprintf(file_id, sizeof(file_id), "/proc/%ld/gid_map",
+                (long) pid);
+
+       return do_userns_map_id(pid, file_id, map_gid);
+}
index 5ecab2f5a5c92f556ac95fa5d940fa9bdfae89cb..765f6af683cea4d3e30ecdce3adb28472c3f56c9 100644 (file)
@@ -29,6 +29,9 @@
             (uint8_t *)(item) < (uint8_t *)(head) + (head)->size;      \
             item = KDBUS_ITEM_NEXT(item))
 
+/* Dump as user of process, useful for user namespace testing */
+#define SUID_DUMP_USER 1
+
 struct conn {
        int fd;
        uint64_t id;
@@ -67,3 +70,7 @@ int conn_update_policy(struct conn *conn, const char *name,
 void add_match_empty(int fd);
 
 int drop_privileges(uid_t uid, gid_t gid);
+
+int userns_map_uid_gid(pid_t pid,
+                      const char *map_uid,
+                      const char *map_gid);