From: Christian Brauner Date: Mon, 28 May 2018 13:10:19 +0000 (+0200) Subject: conf: ensure lxc_delete_tty() does not crash X-Git-Tag: lxc-3.1.0~283^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2358%2Fhead;p=thirdparty%2Flxc.git conf: ensure lxc_delete_tty() does not crash We need to make sure that the ttys are actually initialized otherwise deleting them is not safe. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index d7984bd0f..76cb255d6 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -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);