jail: don't assume positive return value of creat
authorDaniel Golle <daniel@makrotopia.org>
Fri, 13 Jan 2023 18:53:07 +0000 (18:53 +0000)
committerDaniel Golle <daniel@makrotopia.org>
Mon, 16 Jan 2023 21:07:48 +0000 (21:07 +0000)
Handle a (theoretical) error case when calling creat in
create_dev_console function.

Coverity CID: 1490100 (Argument cannot be negative)
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
jail/jail.c

index 31b64e5ddb565e83378dd01cc7f46e8e899505bd..09780ac7e2b732cd6456c835f93c29816ad3dc89 100644 (file)
@@ -374,7 +374,7 @@ static int create_dev_console(const char *jail_root)
 {
        char *console_fname;
        char dev_console_path[PATH_MAX];
-       int slave_console_fd;
+       int slave_console_fd, dev_console_dummy;
 
        /* Open UNIX/98 virtual console */
        console_fd = posix_openpt(O_RDWR | O_NOCTTY);
@@ -394,7 +394,11 @@ static int create_dev_console(const char *jail_root)
 
        /* mount-bind PTY slave to /dev/console in jail */
        snprintf(dev_console_path, sizeof(dev_console_path), "%s/dev/console", jail_root);
-       close(creat(dev_console_path, 0620));
+       dev_console_dummy = creat(dev_console_path, 0620);
+       if (dev_console_dummy < 0)
+               goto no_console;
+
+       close(dev_console_dummy);
 
        if (mount(console_fname, dev_console_path, "bind", MS_BIND, NULL))
                goto no_console;