Sync OpenBSD patchset 345:

Don't attempt to open() the tty path, rely on the client sending its stdin fd
with imsg and fatal if it doesn't, then set the FD_CLOEXEC flag in tty_init
instead of tty_open to prevent them leaking into child processes if any are
created between the two calls.

This bumps the protocol version, so the tmux server should be killed before
upgrading.
This commit is contained in:
Tiago Cunha 2009-09-23 14:39:30 +00:00
parent c40d8cbda4
commit acedc2dcf2
4 changed files with 43 additions and 47 deletions

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.71 2009-09-20 22:11:27 tcunha Exp $ */ /* $Id: client.c,v 1.72 2009-09-23 14:39:30 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -45,7 +45,7 @@ client_init(char *path, struct client_ctx *cctx, int cmdflags, int flags)
struct winsize ws; struct winsize ws;
size_t size; size_t size;
int fd, fd2, mode; int fd, fd2, mode;
char *name, *term; char *term;
#ifdef HAVE_SETPROCTITLE #ifdef HAVE_SETPROCTITLE
char rpathbuf[MAXPATHLEN]; char rpathbuf[MAXPATHLEN];
#endif #endif
@ -117,13 +117,8 @@ server_started:
*data.term = '\0'; *data.term = '\0';
} }
*data.tty = '\0'; if ((fd2 = dup(STDIN_FILENO)) == -1)
if ((name = ttyname(STDIN_FILENO)) == NULL) fatal("dup failed");
fatal("ttyname failed");
if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
fatalx("ttyname failed");
fd2 = dup(STDIN_FILENO);
imsg_compose(&cctx->ibuf, MSG_IDENTIFY, imsg_compose(&cctx->ibuf, MSG_IDENTIFY,
PROTOCOL_VERSION, -1, fd2, &data, sizeof data); PROTOCOL_VERSION, -1, fd2, &data, sizeof data);
} }

View File

@ -1,4 +1,4 @@
/* $Id: server-msg.c,v 1.84 2009-09-15 23:52:30 tcunha Exp $ */ /* $Id: server-msg.c,v 1.85 2009-09-23 14:39:30 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -74,6 +74,8 @@ server_msg_dispatch(struct client *c)
case MSG_IDENTIFY: case MSG_IDENTIFY:
if (datalen != sizeof identifydata) if (datalen != sizeof identifydata)
fatalx("bad MSG_IDENTIFY size"); fatalx("bad MSG_IDENTIFY size");
if (imsg.fd == -1)
fatalx("MSG_IDENTIFY missing fd");
memcpy(&identifydata, imsg.data, sizeof identifydata); memcpy(&identifydata, imsg.data, sizeof identifydata);
server_msg_identify(c, &identifydata, imsg.fd); server_msg_identify(c, &identifydata, imsg.fd);
@ -243,21 +245,13 @@ error:
void void
server_msg_identify(struct client *c, struct msg_identify_data *data, int fd) server_msg_identify(struct client *c, struct msg_identify_data *data, int fd)
{ {
c->tty.sx = data->sx;
if (c->tty.sx == 0)
c->tty.sx = 80;
c->tty.sy = data->sy;
if (c->tty.sy == 0)
c->tty.sy = 24;
c->cwd = NULL; c->cwd = NULL;
data->cwd[(sizeof data->cwd) - 1] = '\0'; data->cwd[(sizeof data->cwd) - 1] = '\0';
if (*data->cwd != '\0') if (*data->cwd != '\0')
c->cwd = xstrdup(data->cwd); c->cwd = xstrdup(data->cwd);
data->tty[(sizeof data->tty) - 1] = '\0';
data->term[(sizeof data->term) - 1] = '\0'; data->term[(sizeof data->term) - 1] = '\0';
tty_init(&c->tty, fd, data->tty, data->term); tty_init(&c->tty, fd, data->term);
if (data->flags & IDENTIFY_UTF8) if (data->flags & IDENTIFY_UTF8)
c->tty.flags |= TTY_UTF8; c->tty.flags |= TTY_UTF8;
if (data->flags & IDENTIFY_256COLOURS) if (data->flags & IDENTIFY_256COLOURS)
@ -267,6 +261,13 @@ server_msg_identify(struct client *c, struct msg_identify_data *data, int fd)
if (data->flags & IDENTIFY_HASDEFAULTS) if (data->flags & IDENTIFY_HASDEFAULTS)
c->tty.term_flags |= TERM_HASDEFAULTS; c->tty.term_flags |= TERM_HASDEFAULTS;
c->tty.sx = data->sx;
if (c->tty.sx == 0)
c->tty.sx = 80;
c->tty.sy = data->sy;
if (c->tty.sy == 0)
c->tty.sy = 24;
c->flags |= CLIENT_TERMINAL; c->flags |= CLIENT_TERMINAL;
} }

8
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.450 2009-09-22 14:22:21 tcunha Exp $ */ /* $Id: tmux.h,v 1.451 2009-09-23 14:39:30 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -21,7 +21,7 @@
#include "config.h" #include "config.h"
#define PROTOCOL_VERSION 1 #define PROTOCOL_VERSION 2
#include <sys/param.h> #include <sys/param.h>
#include <sys/time.h> #include <sys/time.h>
@ -326,8 +326,6 @@ struct msg_command_data {
}; };
struct msg_identify_data { struct msg_identify_data {
char tty[TTY_NAME_MAX];
char cwd[MAXPATHLEN]; char cwd[MAXPATHLEN];
char term[TERMINAL_LENGTH]; char term[TERMINAL_LENGTH];
@ -1198,7 +1196,7 @@ void tty_putcode2(struct tty *, enum tty_code_code, int, int);
void tty_puts(struct tty *, const char *); void tty_puts(struct tty *, const char *);
void tty_putc(struct tty *, u_char); void tty_putc(struct tty *, u_char);
void tty_pututf8(struct tty *, const struct grid_utf8 *); void tty_pututf8(struct tty *, const struct grid_utf8 *);
void tty_init(struct tty *, int, char *, char *); void tty_init(struct tty *, int, char *);
void tty_start_tty(struct tty *); void tty_start_tty(struct tty *);
void tty_stop_tty(struct tty *); void tty_stop_tty(struct tty *);
void tty_detect_utf8(struct tty *); void tty_detect_utf8(struct tty *);

48
tty.c
View File

@ -1,4 +1,4 @@
/* $Id: tty.c,v 1.133 2009-09-23 14:33:13 tcunha Exp $ */ /* $Id: tty.c,v 1.134 2009-09-23 14:39:30 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -44,16 +44,31 @@ void tty_cell(struct tty *,
const struct grid_cell *, const struct grid_utf8 *); const struct grid_cell *, const struct grid_utf8 *);
void void
tty_init(struct tty *tty, int fd, char *path, char *term) tty_init(struct tty *tty, int fd, char *term)
{ {
tty->path = xstrdup(path); int mode;
tty->fd = fd; char *path;
memset(tty, 0, sizeof *tty);
tty->log_fd = -1; tty->log_fd = -1;
if (term == NULL || *term == '\0') if (term == NULL || *term == '\0')
tty->termname = xstrdup("unknown"); tty->termname = xstrdup("unknown");
else else
tty->termname = xstrdup(term); tty->termname = xstrdup(term);
if ((mode = fcntl(fd, F_GETFL)) == -1)
fatal("fcntl failed");
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed");
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
tty->fd = fd;
if ((path = ttyname(fd)) == NULL)
fatalx("ttyname failed");
tty->path = xstrdup(path);
tty->flags = 0; tty->flags = 0;
tty->term_flags = 0; tty->term_flags = 0;
} }
@ -61,27 +76,14 @@ tty_init(struct tty *tty, int fd, char *path, char *term)
int int
tty_open(struct tty *tty, const char *overrides, char **cause) tty_open(struct tty *tty, const char *overrides, char **cause)
{ {
int mode; int fd;
if (tty->fd == -1) { if (debug_level > 3) {
tty->fd = open(tty->path, O_RDWR|O_NONBLOCK); fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (tty->fd == -1) { if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
xasprintf(cause, "%s: %s", tty->path, strerror(errno)); fatal("fcntl failed");
return (-1); tty->log_fd = fd;
} }
}
if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
fatal("fcntl failed");
if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed");
if (fcntl(tty->fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
if (debug_level > 3)
tty->log_fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
else
tty->log_fd = -1;
tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause); tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
if (tty->term == NULL) { if (tty->term == NULL) {