]> git.proxmox.com Git - mirror_lxc.git/commitdiff
terminal: safely retrieve path of slave device
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 30 Jun 2018 09:15:36 +0000 (11:15 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 2 Jul 2018 09:13:44 +0000 (11:13 +0200)
openpty() is a horrible function that uses strcpy() into the char *name
argument if name != NULL. We can't rely on the path being sane in all cases so
let's split out the name retrieval to ttyname_r().

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

index 614c07a135bfa7fbd5524e0adaba77d4cb910268..338d33cd4758a1e20a0760d149159cab3b5ac5ab 100644 (file)
@@ -570,13 +570,20 @@ static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal *terminal,
        /* This is the proxy terminal that will be given to the client, and
         * that the real terminal master will send to / recv from.
         */
-       ret = openpty(&terminal->proxy.master, &terminal->proxy.slave,
-                     terminal->proxy.name, NULL, NULL);
+       ret = openpty(&terminal->proxy.master, &terminal->proxy.slave, NULL,
+                     NULL, NULL);
        if (ret < 0) {
                SYSERROR("Failed to open proxy terminal");
                return -1;
        }
 
+       ret = ttyname_r(terminal->proxy.slave, terminal->proxy.name,
+                       sizeof(terminal->proxy.name));
+       if (ret < 0) {
+               SYSERROR("Failed to retrieve name of proxy terminal slave");
+               goto on_error;
+       }
+
        ret = lxc_setup_tios(terminal->proxy.slave, &oldtermio);
        if (ret < 0)
                goto on_error;
@@ -862,12 +869,18 @@ int lxc_terminal_create(struct lxc_terminal *terminal)
 {
        int ret;
 
-       ret = openpty(&terminal->master, &terminal->slave, terminal->name, NULL, NULL);
+       ret = openpty(&terminal->master, &terminal->slave, NULL, NULL, NULL);
        if (ret < 0) {
                SYSERROR("Failed to open terminal");
                return -1;
        }
 
+       ret = ttyname_r(terminal->slave, terminal->name, sizeof(terminal->name));
+       if (ret < 0) {
+               SYSERROR("Failed to retrieve name of terminal slave");
+               goto err;
+       }
+
        ret = fcntl(terminal->master, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
                SYSERROR("Failed to set FD_CLOEXEC flag on terminal master");