jonathancammack / rpms / openssh

Forked from rpms/openssh 9 months ago
Clone

Blame SOURCES/pam_ssh_agent_auth-0.10.3-build.patch

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