kentpeacock / rpms / openssh

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