Blame SOURCES/pam_ssh_agent_auth-0.9.3-build.patch

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