jail: devices: create parent folder when creating devices
authorDaniel Golle <daniel@makrotopia.org>
Mon, 23 Aug 2021 14:22:31 +0000 (15:22 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Tue, 24 Aug 2021 17:31:24 +0000 (18:31 +0100)
Some device nodes live in subdirectories like /dev/dri.
Create those folders when populating /dev.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
jail/jail.c

index fc8d8247c5d521e255368e00768255f0b55fb3b5..d61bbe07138b91107e0ccd84a5a9a570b50d94ea 100644 (file)
@@ -579,6 +579,7 @@ static struct mknod_args default_devices[] = {
 static int create_devices(void)
 {
        struct mknod_args **cur, *curdef;
+       char *path, *tmp;
 
        if (!opts.devices)
                goto only_default_devices;
@@ -586,12 +587,33 @@ static int create_devices(void)
        cur = opts.devices;
 
        while (*cur) {
-               DEBUG("creating %s (mode=%08o)\n", (*cur)->path, (*cur)->mode);
-               if (mknod((*cur)->path, (*cur)->mode, (*cur)->dev))
+               path = (*cur)->path;
+               /* don't allow devices outside of /dev */
+               if (strncmp(path, "/dev", 4))
+                       return EPERM;
+
+               /* make sure parent folder exists */
+               tmp = strrchr(path, '/');
+               if (!tmp)
+                       return EINVAL;
+
+               *tmp = '\0';
+               if (strcmp(path, "/dev")) {
+                       DEBUG("creating directory %s\n", path);
+
+                       mkdir_p(path, 0755);
+               }
+               *tmp = '/';
+
+               DEBUG("creating %s (mode=%08o)\n", path, (*cur)->mode);
+
+               /* create device */
+               if (mknod(path, (*cur)->mode, (*cur)->dev))
                        return errno;
 
+               /* change owner, if needed */
                if (((*cur)->uid || (*cur)->gid) &&
-                   chown((*cur)->path, (*cur)->uid, (*cur)->gid))
+                   chown(path, (*cur)->uid, (*cur)->gid))
                        return errno;
 
                ++cur;