vishalmishra434 / rpms / openssh

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