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