terminal: safely retrieve path of slave device

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: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent c93e2e0a
...@@ -570,13 +570,20 @@ static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal *terminal, ...@@ -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 /* This is the proxy terminal that will be given to the client, and
* that the real terminal master will send to / recv from. * that the real terminal master will send to / recv from.
*/ */
ret = openpty(&terminal->proxy.master, &terminal->proxy.slave, ret = openpty(&terminal->proxy.master, &terminal->proxy.slave, NULL,
terminal->proxy.name, NULL, NULL); NULL, NULL);
if (ret < 0) { if (ret < 0) {
SYSERROR("Failed to open proxy terminal"); SYSERROR("Failed to open proxy terminal");
return -1; 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); ret = lxc_setup_tios(terminal->proxy.slave, &oldtermio);
if (ret < 0) if (ret < 0)
goto on_error; goto on_error;
...@@ -862,12 +869,18 @@ int lxc_terminal_create(struct lxc_terminal *terminal) ...@@ -862,12 +869,18 @@ int lxc_terminal_create(struct lxc_terminal *terminal)
{ {
int ret; int ret;
ret = openpty(&terminal->master, &terminal->slave, terminal->name, NULL, NULL); ret = openpty(&terminal->master, &terminal->slave, NULL, NULL, NULL);
if (ret < 0) { if (ret < 0) {
SYSERROR("Failed to open terminal"); SYSERROR("Failed to open terminal");
return -1; 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); ret = fcntl(terminal->master, F_SETFD, FD_CLOEXEC);
if (ret < 0) { if (ret < 0) {
SYSERROR("Failed to set FD_CLOEXEC flag on terminal master"); SYSERROR("Failed to set FD_CLOEXEC flag on terminal master");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment