rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

Blame SOURCES/pam_ssh_agent_auth-0.9.3-build.patch

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