rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

Blame SOURCES/pam_ssh_agent_auth-0.10.3-build.patch

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