rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

Blame SOURCES/pam_ssh_agent_auth-0.9.3-build.patch

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