bdb932c
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c.psaa-build openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c
bdb932c
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c.psaa-build	2016-11-13 04:24:32.000000000 +0100
bdb932c
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c	2017-02-07 14:29:41.626116675 +0100
87ab5fc
@@ -43,12 +43,31 @@
87ab5fc
 #include <openssl/evp.h>
87ab5fc
 #include "ssh2.h"
87ab5fc
 #include "misc.h"
e47cb00
+#include "ssh.h"
e47cb00
+#include <sys/types.h>
e47cb00
+#include <sys/stat.h>
e47cb00
+#include <sys/socket.h>
e47cb00
+#include <sys/un.h>
e47cb00
+#include <unistd.h>
e47cb00
+#include <stdlib.h>
e47cb00
+#include <errno.h>
e47cb00
+#include <fcntl.h>
e47cb00
 
e47cb00
 #include "userauth_pubkey_from_id.h"
87ab5fc
 #include "identity.h"
87ab5fc
 #include "get_command_line.h"
87ab5fc
 extern char **environ;
e47cb00
 
e47cb00
+/* 
e47cb00
+ * Added by Jamie Beverly, ensure socket fd points to a socket owned by the user 
e47cb00
+ * A cursory check is done, but to avoid race conditions, it is necessary 
e47cb00
+ * to drop effective UID when connecting to the socket. 
e47cb00
+ *
e47cb00
+ * If the cause of error is EACCES, because we verified we would not have that 
e47cb00
+ * problem initially, we can safely assume that somebody is attempting to find a 
e47cb00
+ * race condition; so a more "direct" log message is generated.
e47cb00
+ */
e47cb00
+
87ab5fc
 static char *
87ab5fc
 log_action(char ** action, size_t count)
87ab5fc
 {
87ab5fc
@@ -85,7 +104,7 @@ void
87ab5fc
 pamsshagentauth_session_id2_gen(Buffer * session_id2, const char * user,
87ab5fc
                                 const char * ruser, const char * servicename)
87ab5fc
 {
87ab5fc
-    char *cookie = NULL;
87ab5fc
+    u_char *cookie = NULL;
87ab5fc
     uint8_t i = 0;
87ab5fc
     uint32_t rnd = 0;
87ab5fc
     uint8_t cookie_len;
bdb932c
@@ -112,7 +131,7 @@ pamsshagentauth_session_id2_gen(Buffer *
87ab5fc
         if (i % 4 == 0) {
87ab5fc
             rnd = pamsshagentauth_arc4random();
87ab5fc
         }
87ab5fc
-        cookie[i] = (char) rnd;
87ab5fc
+        cookie[i] = (u_char) rnd;
87ab5fc
         rnd >>= 8;
87ab5fc
     }
87ab5fc
 
bdb932c
@@ -177,6 +196,86 @@ pamsshagentauth_session_id2_gen(Buffer *
87ab5fc
 }
87ab5fc
 
87ab5fc
 int
e47cb00
+ssh_get_authentication_socket_for_uid(uid_t uid)
e47cb00
+{
e47cb00
+	const char *authsocket;
e47cb00
+	int sock;
e47cb00
+	struct sockaddr_un sunaddr;
e47cb00
+	struct stat sock_st;
e47cb00
+
e47cb00
+	authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
e47cb00
+	if (!authsocket)
e47cb00
+		return -1;
e47cb00
+
e47cb00
+	/* Advisory only; seteuid ensures no race condition; but will only log if we see EACCES */
e47cb00
+	if( stat(authsocket,&sock_st) == 0) {
e47cb00
+		if(uid != 0 && sock_st.st_uid != uid) {
e47cb00
+			fatal("uid %lu attempted to open an agent socket owned by uid %lu", (unsigned long) uid, (unsigned long) sock_st.st_uid);
e47cb00
+			return -1;
e47cb00
+		}
e47cb00
+	}
e47cb00
+
e47cb00
+	/* 
e47cb00
+	 * Ensures that the EACCES tested for below can _only_ happen if somebody 
e47cb00
+	 * is attempting to race the stat above to bypass authentication.
e47cb00
+	 */
e47cb00
+	if( (sock_st.st_mode & S_IWUSR) != S_IWUSR || (sock_st.st_mode & S_IRUSR) != S_IRUSR) {
e47cb00
+		error("ssh-agent socket has incorrect permissions for owner");
e47cb00
+		return -1;
e47cb00
+	}
e47cb00
+
e47cb00
+	sunaddr.sun_family = AF_UNIX;
e47cb00
+	strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
e47cb00
+
e47cb00
+	sock = socket(AF_UNIX, SOCK_STREAM, 0);
e47cb00
+	if (sock < 0)
e47cb00
+		return -1;
e47cb00
+
e47cb00
+	/* close on exec */
e47cb00
+	if (fcntl(sock, F_SETFD, 1) == -1) {
e47cb00
+		close(sock);
e47cb00
+		return -1;
e47cb00
+	}
e47cb00
+
e47cb00
+	errno = 0; 
e47cb00
+	seteuid(uid); /* To ensure a race condition is not used to circumvent the stat
e47cb00
+	             above, we will temporarily drop UID to the caller */
e47cb00
+	if (connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr) < 0) {
e47cb00
+		close(sock);
e47cb00
+        if(errno == EACCES)
e47cb00
+		fatal("MAJOR SECURITY WARNING: uid %lu made a deliberate and malicious attempt to open an agent socket owned by another user", (unsigned long) uid);
e47cb00
+		return -1;
e47cb00
+	}
e47cb00
+
e47cb00
+	seteuid(0); /* we now continue the regularly scheduled programming */
e47cb00
+
e47cb00
+	return sock;
e47cb00
+}
e47cb00
+
e47cb00
+AuthenticationConnection *
e47cb00
+ssh_get_authentication_connection_for_uid(uid_t uid)
e47cb00
+{
e47cb00
+	AuthenticationConnection *auth;
e47cb00
+	int sock;
e47cb00
+
e47cb00
+	sock = ssh_get_authentication_socket_for_uid(uid);
e47cb00
+
e47cb00
+	/*
e47cb00
+	 * Fail if we couldn't obtain a connection.  This happens if we
e47cb00
+	 * exited due to a timeout.
e47cb00
+	 */
e47cb00
+	if (sock < 0)
e47cb00
+		return NULL;
e47cb00
+
e47cb00
+	auth = xmalloc(sizeof(*auth));
e47cb00
+	auth->fd = sock;
e47cb00
+	buffer_init(&auth->identities);
e47cb00
+	auth->howmany = 0;
e47cb00
+
e47cb00
+	return auth;
e47cb00
+}
e47cb00
+
87ab5fc
+int
87ab5fc
 pamsshagentauth_find_authorized_keys(const char * user, const char * ruser, const char * servicename)
e47cb00
 {
87ab5fc
     Buffer session_id2 = { 0 };
87ab5fc
@@ -190,7 +289,7 @@ pamsshagentauth_find_authorized_keys(con
e47cb00
     OpenSSL_add_all_digests();
87ab5fc
     pamsshagentauth_session_id2_gen(&session_id2, user, ruser, servicename);
e47cb00
 
e47cb00
-    if ((ac = ssh_get_authentication_connection(uid))) {
e47cb00
+    if ((ac = ssh_get_authentication_connection_for_uid(uid))) {
87ab5fc
         pamsshagentauth_verbose("Contacted ssh-agent of user %s (%u)", ruser, uid);
e47cb00
         for (key = ssh_get_first_identity(ac, &comment, 2); key != NULL; key = ssh_get_next_identity(ac, &comment, 2)) 
e47cb00
         {
bdb932c
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/Makefile.in.psaa-build openssh-7.4p1/pam_ssh_agent_auth-0.10.3/Makefile.in
bdb932c
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/Makefile.in.psaa-build	2016-11-13 04:24:32.000000000 +0100
bdb932c
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/Makefile.in	2017-02-07 14:40:14.407566921 +0100
87ab5fc
@@ -52,7 +52,7 @@ PATHS=
e47cb00
 CC=@CC@
e47cb00
 LD=@LD@
e47cb00
 CFLAGS=@CFLAGS@
e47cb00
-CPPFLAGS=-I. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@
c8f1381
+CPPFLAGS=-I.. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@
e47cb00
 LIBS=@LIBS@
e47cb00
 AR=@AR@
e47cb00
 AWK=@AWK@
87ab5fc
@@ -61,7 +61,7 @@ INSTALL=@INSTALL@
e47cb00
 PERL=@PERL@
e47cb00
 SED=@SED@
e47cb00
 ENT=@ENT@
e47cb00
-LDFLAGS=-L. -Lopenbsd-compat/ @LDFLAGS@
e47cb00
+LDFLAGS=-L.. -L../openbsd-compat/ @LDFLAGS@
e47cb00
 LDFLAGS_SHARED = @LDFLAGS_SHARED@
e47cb00
 EXEEXT=@EXEEXT@
e47cb00
 
bdb932c
@@ -74,7 +74,7 @@ SSHOBJS=xmalloc.o atomicio.o authfd.o bu
e47cb00
 
bdb932c
 ED25519OBJS=ed25519-donna/ed25519.o
e47cb00
 
87ab5fc
-PAM_SSH_AGENT_AUTH_OBJS=pam_user_key_allowed2.o iterate_ssh_agent_keys.o userauth_pubkey_from_id.o pam_user_authorized_keys.o get_command_line.o
87ab5fc
+PAM_SSH_AGENT_AUTH_OBJS=pam_user_key_allowed2.o iterate_ssh_agent_keys.o userauth_pubkey_from_id.o pam_user_authorized_keys.o get_command_line.o secure_filename.o
e47cb00
 
e47cb00
 
e47cb00
 MANPAGES_IN	= pam_ssh_agent_auth.pod
bdb932c
@@ -94,13 +94,13 @@ $(PAM_MODULES): Makefile.in config.h
e47cb00
 .c.o:
bdb932c
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
e47cb00
 
e47cb00
-LIBCOMPAT=openbsd-compat/libopenbsd-compat.a
e47cb00
+LIBCOMPAT=../openbsd-compat/libopenbsd-compat.a
e47cb00
 $(LIBCOMPAT): always
e47cb00
 	(cd openbsd-compat && $(MAKE))
e47cb00
 always:
e47cb00
 
bdb932c
-pam_ssh_agent_auth.so: $(LIBCOMPAT) $(SSHOBJS) $(ED25519OBJS) $(PAM_SSH_AGENT_AUTH_OBJS)  pam_ssh_agent_auth.o
bdb932c
-	$(LD) $(LDFLAGS_SHARED) -o $@ $(SSHOBJS) $(ED25519OBJS) $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lopenbsd-compat pam_ssh_agent_auth.o $(LIBS) -lpam
bbf61da
+pam_ssh_agent_auth.so: $(PAM_SSH_AGENT_AUTH_OBJS)  pam_ssh_agent_auth.o ../uidswap.o
bbf61da
+	$(LD) $(LDFLAGS_SHARED) -o $@ $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat pam_ssh_agent_auth.o ../uidswap.o $(LIBS) -lpam
e47cb00
 
e47cb00
 $(MANPAGES): $(MANPAGES_IN)
bdb932c
 	pod2man --section=8 --release=v0.10.3 --name=pam_ssh_agent_auth --official --center "PAM" pam_ssh_agent_auth.pod > pam_ssh_agent_auth.8