From: Christian Brauner Date: Thu, 29 Jul 2021 13:46:17 +0000 (+0200) Subject: terminal: don't use ttyname_r() for native terminal allocation X-Git-Tag: lxc-5.0.0~130^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb01e311260cf50b48ecd98f1f9e0c63a2d450ac;p=thirdparty%2Flxc.git terminal: don't use ttyname_r() for native terminal allocation Since we can call that function from another mount namespace we need to do this manually. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/terminal.c b/src/lxc/terminal.c index 8850a9687..2f495bc62 100644 --- a/src/lxc/terminal.c +++ b/src/lxc/terminal.c @@ -909,10 +909,12 @@ err: return -ENODEV; } -static int lxc_terminal_create_native(const char *name, const char *lxcpath, struct lxc_conf *conf, +static int lxc_terminal_create_native(const char *name, const char *lxcpath, + struct lxc_conf *conf, struct lxc_terminal *terminal) { - __do_close int devpts_fd = -EBADF; + __do_close int devpts_fd = -EBADF, fd_pty = -EBADF; + int pty_nr = -1; int ret; devpts_fd = lxc_cmd_get_devpts_fd(name, lxcpath); @@ -949,12 +951,31 @@ static int lxc_terminal_create_native(const char *name, const char *lxcpath, str goto err; } - ret = ttyname_r(terminal->pty, terminal->name, sizeof(terminal->name)); + ret = ioctl(terminal->ptx, TIOCGPTN, &pty_nr); if (ret) { SYSWARN("Failed to retrieve name of terminal pty"); goto err; } + fd_pty = open_at(devpts_fd, fdstr(pty_nr), PROTECT_OPATH_FILE, + PROTECT_LOOKUP_ABSOLUTE_XDEV, 0); + if (fd_pty < 0) { + SYSWARN("Failed to open terminal pty fd by path %d/%d", devpts_fd, pty_nr); + goto err; + } + + if (!same_file_lax(terminal->pty, fd_pty)) { + SYSWARN("Terminal file descriptor changed"); + goto err; + } + + ret = strnprintf(terminal->name, sizeof(terminal->name), "dev/pts/%d", + pty_nr); + if (ret < 0) { + SYSWARN("Failed to create terminal pty name"); + goto err; + } + ret = lxc_terminal_peer_default(terminal); if (ret < 0) { SYSWARN("Failed to allocate proxy terminal");