bmh10 / rpms / openssh

Forked from rpms/openssh 11 days ago
Clone

Blame SOURCES/pam_ssh_agent_auth-0.10.3-build.patch

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