kentpeacock / rpms / openssh

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