d890745
From f98a09cacff7baad8748c9aa217afd155a4d493f Mon Sep 17 00:00:00 2001
d890745
From: "mmcc@openbsd.org" <mmcc@openbsd.org>
d890745
Date: Tue, 20 Oct 2015 03:36:35 +0000
d890745
Subject: [PATCH] upstream commit
d890745
d890745
Replace a function-local allocation with stack memory.
d890745
d890745
ok djm@
d890745
d890745
Upstream-ID: c09fbbab637053a2ab9f33ca142b4e20a4c5a17e
d890745
---
d890745
 clientloop.c | 9 ++-------
d890745
 1 file changed, 2 insertions(+), 7 deletions(-)
d890745
d890745
diff --git a/clientloop.c b/clientloop.c
d890745
index 87ceb3d..1e05cba 100644
d890745
--- a/clientloop.c
d890745
+++ b/clientloop.c
d890745
@@ -311,11 +311,10 @@ client_x11_get_proto(const char *display, const char *xauth_path,
d890745
 	static char proto[512], data[512];
d890745
 	FILE *f;
d890745
 	int got_data = 0, generated = 0, do_unlink = 0, i;
d890745
-	char *xauthdir, *xauthfile;
d890745
+	char xauthdir[PATH_MAX] = "", xauthfile[PATH_MAX] = "";
d890745
 	struct stat st;
d890745
 	u_int now, x11_timeout_real;
d890745
 
d890745
-	xauthdir = xauthfile = NULL;
d890745
 	*_proto = proto;
d890745
 	*_data = data;
d890745
 	proto[0] = data[0] = '\0';
d890745
@@ -343,8 +342,6 @@ client_x11_get_proto(const char *display, const char *xauth_path,
d890745
 			display = xdisplay;
d890745
 		}
d890745
 		if (trusted == 0) {
d890745
-			xauthdir = xmalloc(PATH_MAX);
d890745
-			xauthfile = xmalloc(PATH_MAX);
d890745
 			mktemp_proto(xauthdir, PATH_MAX);
d890745
 			/*
d890745
 			 * The authentication cookie should briefly outlive
d890745
@@ -407,8 +404,6 @@ client_x11_get_proto(const char *display, const char *xauth_path,
d890745
 		unlink(xauthfile);
d890745
 		rmdir(xauthdir);
d890745
 	}
d890745
-	free(xauthdir);
d890745
-	free(xauthfile);
d890745
 
d890745
 	/*
d890745
 	 * If we didn't get authentication data, just make up some
d890745
-- 
d890745
2.5.0
d890745
d890745
From ed4ce82dbfa8a3a3c8ea6fa0db113c71e234416c Mon Sep 17 00:00:00 2001
d890745
From: "djm@openbsd.org" <djm@openbsd.org>
d890745
Date: Wed, 13 Jan 2016 23:04:47 +0000
d890745
Subject: [PATCH] upstream commit
d890745
d890745
eliminate fallback from untrusted X11 forwarding to trusted
d890745
 forwarding when the X server disables the SECURITY extension; Reported by
d890745
 Thomas Hoger; ok deraadt@
d890745
d890745
Upstream-ID: f76195bd2064615a63ef9674a0e4096b0713f938
d890745
---
d890745
 clientloop.c | 114 ++++++++++++++++++++++++++++++++++++-----------------------
d890745
 clientloop.h |   4 +--
d890745
 mux.c        |  22 ++++++------
d890745
 ssh.c        |  23 +++++-------
d890745
 4 files changed, 93 insertions(+), 70 deletions(-)
d890745
d890745
diff --git a/clientloop.c b/clientloop.c
d890745
index f555451..c0386d5 100644
d890745
--- a/clientloop.c
d890745
+++ b/clientloop.c
d890745
@@ -288,6 +288,9 @@ client_x11_display_valid(const char *display)
d890745
 {
d890745
 	size_t i, dlen;
d890745
 
d890745
+	if (display == NULL)
d890745
+		return 0;
d890745
+
d890745
 	dlen = strlen(display);
d890745
 	for (i = 0; i < dlen; i++) {
d890745
 		if (!isalnum((u_char)display[i]) &&
d890745
@@ -301,34 +304,33 @@ client_x11_display_valid(const char *display)
d890745
 
d890745
 #define SSH_X11_PROTO		"MIT-MAGIC-COOKIE-1"
d890745
 #define X11_TIMEOUT_SLACK	60
d890745
-void
d890745
+int
d890745
 client_x11_get_proto(const char *display, const char *xauth_path,
d890745
     u_int trusted, u_int timeout, char **_proto, char **_data)
d890745
 {
d890745
-	char cmd[1024];
d890745
-	char line[512];
d890745
-	char xdisplay[512];
d890745
+	char cmd[1024], line[512], xdisplay[512];
d890745
+	char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
d890745
 	static char proto[512], data[512];
d890745
 	FILE *f;
d890745
-	int got_data = 0, generated = 0, do_unlink = 0, i;
d890745
-	char xauthdir[PATH_MAX] = "", xauthfile[PATH_MAX] = "";
d890745
+	int got_data = 0, generated = 0, do_unlink = 0, i, r;
d890745
 	struct stat st;
d890745
 	u_int now, x11_timeout_real;
d890745
 
d890745
 	*_proto = proto;
d890745
 	*_data = data;
d890745
-	proto[0] = data[0] = '\0';
d890745
+	proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
d890745
 
d890745
-	if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
d890745
-		debug("No xauth program.");
d890745
-	} else if (!client_x11_display_valid(display)) {
d890745
-		logit("DISPLAY '%s' invalid, falling back to fake xauth data",
d890745
+	if (!client_x11_display_valid(display)) {
1d944e9
+		logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
d890745
 		    display);
d890745
-	} else {
d890745
-		if (display == NULL) {
d890745
-			debug("x11_get_proto: DISPLAY not set");
d890745
-			return;
d890745
-		}
d890745
+		return -1;
d890745
+	}
d890745
+	if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
d890745
+		debug("No xauth program.");
d890745
+		xauth_path = NULL;
d890745
+	}
d890745
+
d890745
+	if (xauth_path != NULL) {
d890745
 		/*
d890745
 		 * Handle FamilyLocal case where $DISPLAY does
d890745
 		 * not match an authorization entry.  For this we
d890745
@@ -337,43 +339,60 @@ client_x11_get_proto(const char *display, const char *xauth_path,
d890745
 		 *      is not perfect.
d890745
 		 */
d890745
 		if (strncmp(display, "localhost:", 10) == 0) {
d890745
-			snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
d890745
-			    display + 10);
d890745
+			if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
d890745
+			    display + 10)) < 0 ||
d890745
+			    (size_t)r >= sizeof(xdisplay)) {
d890745
+				error("%s: display name too long", __func__);
d890745
+				return -1;
d890745
+			}
d890745
 			display = xdisplay;
d890745
 		}
d890745
 		if (trusted == 0) {
d890745
-			mktemp_proto(xauthdir, PATH_MAX);
d890745
 			/*
d890745
+			 * Generate an untrusted X11 auth cookie.
d890745
+			 *
d890745
 			 * The authentication cookie should briefly outlive
d890745
 			 * ssh's willingness to forward X11 connections to
d890745
 			 * avoid nasty fail-open behaviour in the X server.
d890745
 			 */
d890745
+			mktemp_proto(xauthdir, sizeof(xauthdir));
d890745
+			if (mkdtemp(xauthdir) == NULL) {
d890745
+				error("%s: mkdtemp: %s",
d890745
+				    __func__, strerror(errno));
d890745
+				return -1;
d890745
+			}
d890745
+			do_unlink = 1;
d890745
+			if ((r = snprintf(xauthfile, sizeof(xauthfile),
d890745
+			    "%s/xauthfile", xauthdir)) < 0 ||
d890745
+			    (size_t)r >= sizeof(xauthfile)) {
d890745
+				error("%s: xauthfile path too long", __func__);
d890745
+				unlink(xauthfile);
d890745
+				rmdir(xauthdir);
d890745
+				return -1;
d890745
+			}
d890745
+
d890745
 			if (timeout >= UINT_MAX - X11_TIMEOUT_SLACK)
d890745
 				x11_timeout_real = UINT_MAX;
d890745
 			else
d890745
 				x11_timeout_real = timeout + X11_TIMEOUT_SLACK;
d890745
-			if (mkdtemp(xauthdir) != NULL) {
d890745
-				do_unlink = 1;
d890745
-				snprintf(xauthfile, PATH_MAX, "%s/xauthfile",
d890745
-				    xauthdir);
d890745
-				snprintf(cmd, sizeof(cmd),
d890745
-				    "%s -f %s generate %s " SSH_X11_PROTO
d890745
-				    " untrusted timeout %u 2>" _PATH_DEVNULL,
d890745
-				    xauth_path, xauthfile, display,
d890745
-				    x11_timeout_real);
d890745
-				debug2("x11_get_proto: %s", cmd);
d890745
-				if (x11_refuse_time == 0) {
d890745
-					now = monotime() + 1;
d890745
-					if (UINT_MAX - timeout < now)
d890745
-						x11_refuse_time = UINT_MAX;
d890745
-					else
d890745
-						x11_refuse_time = now + timeout;
d890745
-					channel_set_x11_refuse_time(
d890745
-					    x11_refuse_time);
d890745
-				}
d890745
-				if (system(cmd) == 0)
d890745
-					generated = 1;
d890745
+			if ((r = snprintf(cmd, sizeof(cmd),
d890745
+			    "%s -f %s generate %s " SSH_X11_PROTO
d890745
+			    " untrusted timeout %u 2>" _PATH_DEVNULL,
d890745
+			    xauth_path, xauthfile, display,
d890745
+			    x11_timeout_real)) < 0 ||
d890745
+			    (size_t)r >= sizeof(cmd))
d890745
+				fatal("%s: cmd too long", __func__);
d890745
+			debug2("%s: %s", __func__, cmd);
d890745
+			if (x11_refuse_time == 0) {
d890745
+				now = monotime() + 1;
d890745
+				if (UINT_MAX - timeout < now)
d890745
+					x11_refuse_time = UINT_MAX;
d890745
+				else
d890745
+					x11_refuse_time = now + timeout;
d890745
+				channel_set_x11_refuse_time(x11_refuse_time);
d890745
 			}
d890745
+			if (system(cmd) == 0)
d890745
+				generated = 1;
d890745
 		}
d890745
 
d890745
 		/*
d890745
@@ -395,9 +414,7 @@ client_x11_get_proto(const char *display, const char *xauth_path,
d890745
 				got_data = 1;
d890745
 			if (f)
d890745
 				pclose(f);
d890745
-		} else
d890745
-			error("Warning: untrusted X11 forwarding setup failed: "
d890745
-			    "xauth key data not generated");
d890745
+		}
d890745
 	}
d890745
 
d890745
 	if (do_unlink) {
d890745
@@ -405,6 +422,13 @@ client_x11_get_proto(const char *display, const char *xauth_path,
d890745
 		rmdir(xauthdir);
d890745
 	}
d890745
 
d890745
+	/* Don't fall back to fake X11 data for untrusted forwarding */
d890745
+	if (!trusted && !got_data) {
d890745
+		error("Warning: untrusted X11 forwarding setup failed: "
d890745
+		    "xauth key data not generated");
d890745
+		return -1;
d890745
+	}
d890745
+
d890745
 	/*
d890745
 	 * If we didn't get authentication data, just make up some
d890745
 	 * data.  The forwarding code will check the validity of the
d890745
@@ -427,6 +451,8 @@ client_x11_get_proto(const char *display, const char *xauth_path,
d890745
 			rnd >>= 8;
d890745
 		}
d890745
 	}
d890745
+
d890745
+	return 0;
d890745
 }
d890745
 
d890745
 /*
d890745
diff --git a/clientloop.h b/clientloop.h
d890745
index 338d451..f4d4c69 100644
d890745
--- a/clientloop.h
d890745
+++ b/clientloop.h
d890745
@@ -39,7 +39,7 @@
d890745
 
d890745
 /* Client side main loop for the interactive session. */
d890745
 int	 client_loop(int, int, int);
d890745
-void	 client_x11_get_proto(const char *, const char *, u_int, u_int,
d890745
+int	 client_x11_get_proto(const char *, const char *, u_int, u_int,
d890745
 	    char **, char **);
d890745
 void	 client_global_request_reply_fwd(int, u_int32_t, void *);
d890745
 void	 client_session2_setup(int, int, int, const char *, struct termios *,
d890745
diff --git a/mux.c b/mux.c
d890745
index f9c3af6..6bf53eb 100644
d890745
--- a/mux.c
d890745
+++ b/mux.c
d890745
@@ -1354,16 +1354,18 @@ mux_session_confirm(int id, int success, void *arg)
d890745
 		char *proto, *data;
d890745
 
d890745
 		/* Get reasonable local authentication information. */
d890745
-		client_x11_get_proto(display, options.xauth_location,
d890745
+		if (client_x11_get_proto(display, options.xauth_location,
d890745
 		    options.forward_x11_trusted, options.forward_x11_timeout,
d890745
-		    &proto, &data);
d890745
-		/* Request forwarding with authentication spoofing. */
d890745
-		debug("Requesting X11 forwarding with authentication "
d890745
-		    "spoofing.");
d890745
-		x11_request_forwarding_with_spoofing(id, display, proto,
d890745
-		    data, 1);
d890745
-		client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
d890745
-		/* XXX exit_on_forward_failure */
d890745
+		    &proto, &data) == 0) {
d890745
+			/* Request forwarding with authentication spoofing. */
d890745
+			debug("Requesting X11 forwarding with authentication "
d890745
+			    "spoofing.");
d890745
+			x11_request_forwarding_with_spoofing(id, display, proto,
d890745
+			    data, 1);
d890745
+			/* XXX exit_on_forward_failure */
d890745
+			client_expect_confirm(id, "X11 forwarding",
d890745
+			    CONFIRM_WARN);
d890745
+		}
d890745
 	}
d890745
 
d890745
 	if (cctx->want_agent_fwd && options.forward_agent) {
d890745
diff --git a/ssh.c b/ssh.c
d890745
index 81704ab..096c5b5 100644
d890745
--- a/ssh.c
d890745
+++ b/ssh.c
d890745
@@ -1626,6 +1626,7 @@ ssh_session(void)
d890745
 	struct winsize ws;
d890745
 	char *cp;
d890745
 	const char *display;
d890745
+	char *proto = NULL, *data = NULL;
d890745
 
d890745
 	/* Enable compression if requested. */
d890745
 	if (options.compression) {
d890745
@@ -1696,13 +1697,9 @@ ssh_session(void)
d890745
 	display = getenv("DISPLAY");
d890745
 	if (display == NULL && options.forward_x11)
d890745
 		debug("X11 forwarding requested but DISPLAY not set");
d890745
-	if (options.forward_x11 && display != NULL) {
d890745
-		char *proto, *data;
d890745
-		/* Get reasonable local authentication information. */
d890745
-		client_x11_get_proto(display, options.xauth_location,
d890745
-		    options.forward_x11_trusted,
d890745
-		    options.forward_x11_timeout,
d890745
-		    &proto, &data);
d890745
+	if (options.forward_x11 && client_x11_get_proto(display,
d890745
+	    options.xauth_location, options.forward_x11_trusted,
d890745
+	    options.forward_x11_timeout, &proto, &data) == 0) {
d890745
 		/* Request forwarding with authentication spoofing. */
d890745
 		debug("Requesting X11 forwarding with authentication "
d890745
 		    "spoofing.");
d890745
@@ -1792,6 +1789,7 @@ ssh_session2_setup(int id, int success, void *arg)
d890745
 	extern char **environ;
d890745
 	const char *display;
d890745
 	int interactive = tty_flag;
d890745
+	char *proto = NULL, *data = NULL;
d890745
 
d890745
 	if (!success)
d890745
 		return; /* No need for error message, channels code sens one */
d890745
@@ -1799,12 +1797,9 @@ ssh_session2_setup(int id, int success, void *arg)
d890745
 	display = getenv("DISPLAY");
d890745
 	if (display == NULL && options.forward_x11)
d890745
 		debug("X11 forwarding requested but DISPLAY not set");
d890745
-	if (options.forward_x11 && display != NULL) {
d890745
-		char *proto, *data;
d890745
-		/* Get reasonable local authentication information. */
d890745
-		client_x11_get_proto(display, options.xauth_location,
d890745
-		    options.forward_x11_trusted,
d890745
-		    options.forward_x11_timeout, &proto, &data);
d890745
+	if (options.forward_x11 && client_x11_get_proto(display,
d890745
+	    options.xauth_location, options.forward_x11_trusted,
d890745
+	    options.forward_x11_timeout, &proto, &data) == 0) {
d890745
 		/* Request forwarding with authentication spoofing. */
d890745
 		debug("Requesting X11 forwarding with authentication "
d890745
 		    "spoofing.");
d890745
-- 
d890745
2.5.0
1d944e9
1d944e9
From 5658ef2501e785fbbdf5de2dc33b1ff7a4dca73a Mon Sep 17 00:00:00 2001
1d944e9
From: "millert@openbsd.org" <millert@openbsd.org>
1d944e9
Date: Mon, 1 Feb 2016 21:18:17 +0000
1d944e9
Subject: upstream commit
1d944e9
1d944e9
Avoid ugly "DISPLAY "(null)" invalid; disabling X11
1d944e9
 forwarding" message when DISPLAY is not set.  This could also result in a
1d944e9
 crash on systems with a printf that doesn't handle NULL.  OK djm@
1d944e9
1d944e9
Upstream-ID: 20ee0cfbda678a247264c20ed75362042b90b412
1d944e9
---
1d944e9
 clientloop.c | 7 ++++---
1d944e9
 1 file changed, 4 insertions(+), 3 deletions(-)
1d944e9
1d944e9
diff --git a/clientloop.c b/clientloop.c
1d944e9
index f8f9a3f..f0a08f2 100644
1d944e9
--- a/clientloop.c
1d944e9
+++ b/clientloop.c
1d944e9
@@ -318,8 +318,9 @@ client_x11_get_proto(const char *display, const char *xauth_path,
1d944e9
 	proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
1d944e9
 
1d944e9
 	if (!client_x11_display_valid(display)) {
1d944e9
-		logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
1d944e9
-		    display);
1d944e9
+		if (display != NULL)
1d944e9
+			logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
1d944e9
+			    display);
1d944e9
 		return -1;
1d944e9
 	}
1d944e9
 	if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
1d944e9
-- 
1d944e9
cgit v0.11.2
1d944e9
1d944e9