]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: ensure lxc_delete_tty() does not crash 2358/head
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 28 May 2018 13:10:19 +0000 (15:10 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 28 May 2018 13:33:28 +0000 (15:33 +0200)
We need to make sure that the ttys are actually initialized otherwise deleting
them is not safe.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c

index d7984bd0f60f1738b1b5492f72af6394ebd4ec56..76cb255d6434f8eee9a740540616c43ba8511499 100644 (file)
@@ -968,6 +968,8 @@ int lxc_allocate_ttys(const char *name, struct lxc_conf *conf)
        for (i = 0; i < ttys->max; i++) {
                struct lxc_terminal_info *tty = &ttys->tty[i];
 
+               tty->master = -EBADF;
+               tty->slave = -EBADF;
                ret = openpty(&tty->master, &tty->slave,
                              tty->name, NULL, NULL);
                if (ret) {
@@ -1004,11 +1006,21 @@ void lxc_delete_tty(struct lxc_tty_info *ttys)
 {
        int i;
 
+       if (!ttys->tty)
+               return;
+
        for (i = 0; i < ttys->max; i++) {
                struct lxc_terminal_info *tty = &ttys->tty[i];
 
-               close(tty->master);
-               close(tty->slave);
+               if (tty->master >= 0) {
+                       close(tty->master);
+                       tty->master = -EBADF;
+               }
+
+               if (tty->slave >= 0) {
+                       close(tty->slave);
+                       tty->slave = -EBADF;
+               }
        }
 
        free(ttys->tty);