summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2018-11-22 10:36:41 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2018-11-22 10:36:41 +0000
commit65ffacb450ff60dede5960f4185388d9c9b9a910 (patch)
treee8f853b6c57ed1a99377ae15a08fcb8f8b0c40ee
parent386e6a237d1378c8800e3e33a20b37e4cfd12204 (diff)
Do not use PWD unless it actually matches the real working directory.
-rw-r--r--usr.bin/tmux/client.c8
-rw-r--r--usr.bin/tmux/tmux.c31
-rw-r--r--usr.bin/tmux/tmux.h3
3 files changed, 32 insertions, 10 deletions
diff --git a/usr.bin/tmux/client.c b/usr.bin/tmux/client.c
index 29db814efce..e482ab28dd9 100644
--- a/usr.bin/tmux/client.c
+++ b/usr.bin/tmux/client.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: client.c,v 1.127 2018/04/26 12:42:51 guenther Exp $ */
+/* $OpenBSD: client.c,v 1.128 2018/11/22 10:36:40 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -222,7 +222,7 @@ client_main(struct event_base *base, int argc, char **argv, int flags)
const char *ttynam, *cwd;
pid_t ppid;
enum msgtype msg;
- char *cause, path[PATH_MAX];
+ char *cause;
struct termios tio, saved_tio;
size_t size;
@@ -277,9 +277,7 @@ client_main(struct event_base *base, int argc, char **argv, int flags)
client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
/* Save these before pledge(). */
- if ((cwd = getenv("PWD")) == NULL &&
- (cwd = getcwd(path, sizeof path)) == NULL &&
- (cwd = find_home()) == NULL)
+ if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
cwd = "/";
if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
ttynam = "";
diff --git a/usr.bin/tmux/tmux.c b/usr.bin/tmux/tmux.c
index 84d801c7951..b12fda88cd6 100644
--- a/usr.bin/tmux/tmux.c
+++ b/usr.bin/tmux/tmux.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.c,v 1.186 2018/01/12 10:22:02 nicm Exp $ */
+/* $OpenBSD: tmux.c,v 1.187 2018/11/22 10:36:40 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -167,6 +167,31 @@ setblocking(int fd, int state)
}
const char *
+find_cwd(void)
+{
+ char resolved1[PATH_MAX], resolved2[PATH_MAX];
+ static char cwd[PATH_MAX];
+ const char *pwd;
+
+ if (getcwd(cwd, sizeof cwd) == NULL)
+ return (NULL);
+ if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
+ return (cwd);
+
+ /*
+ * We want to use PWD so that symbolic links are maintained,
+ * but only if it matches the actual working directory.
+ */
+ if (realpath(pwd, resolved1) == NULL)
+ return (cwd);
+ if (realpath(cwd, resolved2) == NULL)
+ return (cwd);
+ if (strcmp(resolved1, resolved2) != 0)
+ return (cwd);
+ return (pwd);
+}
+
+const char *
find_home(void)
{
struct passwd *pw;
@@ -191,7 +216,6 @@ int
main(int argc, char **argv)
{
char *path, *label, *cause, **var;
- char tmp[PATH_MAX];
const char *s, *shell, *cwd;
int opt, flags, keys;
const struct options_table_entry *oe;
@@ -293,8 +317,7 @@ main(int argc, char **argv)
global_environ = environ_create();
for (var = environ; *var != NULL; var++)
environ_put(global_environ, *var);
- if ((cwd = getenv("PWD")) == NULL &&
- (cwd = getcwd(tmp, sizeof tmp)) != NULL)
+ if ((cwd = find_cwd()) != NULL)
environ_set(global_environ, "PWD", "%s", cwd);
global_options = options_create(NULL);
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index 49a0258b67e..cccb9aa1495 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.850 2018/10/25 15:13:38 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.851 2018/11/22 10:36:40 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -1506,6 +1506,7 @@ extern int ptm_fd;
extern const char *shell_command;
int areshell(const char *);
void setblocking(int, int);
+const char *find_cwd(void);
const char *find_home(void);
/* proc.c */