From 386e676854f56268fae34cd84babae983c0d7f2e Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 28 May 2018 15:10:19 +0200 Subject: [PATCH] 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 --- src/lxc/conf.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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); -- 2.47.2