kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
f09e2e
diff -up pam_ssh_agent_auth-0.9.3/iterate_ssh_agent_keys.c.psaa-build pam_ssh_agent_auth-0.9.3/iterate_ssh_agent_keys.c
f09e2e
--- pam_ssh_agent_auth-0.9.3/iterate_ssh_agent_keys.c.psaa-build	2010-01-13 03:17:01.000000000 +0100
f09e2e
+++ pam_ssh_agent_auth-0.9.3/iterate_ssh_agent_keys.c	2012-06-21 20:14:56.432527764 +0200
f09e2e
@@ -37,7 +37,16 @@
f09e2e
 #include "buffer.h"
f09e2e
 #include "key.h"
f09e2e
 #include "authfd.h"
f09e2e
+#include "ssh.h"
f09e2e
 #include <stdio.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>
f09e2e
 #include <openssl/evp.h>
f09e2e
 
f09e2e
 #include "userauth_pubkey_from_id.h"
f09e2e
@@ -69,6 +78,96 @@ session_id2_gen()
f09e2e
     return cookie;
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
f09e2e
 find_authorized_keys(uid_t uid)
f09e2e
 {
f09e2e
@@ -81,7 +180,7 @@ find_authorized_keys(uid_t uid)
f09e2e
     OpenSSL_add_all_digests();
f09e2e
     session_id2 = session_id2_gen();
f09e2e
 
f09e2e
-    if ((ac = ssh_get_authentication_connection(uid))) {
f09e2e
+    if ((ac = ssh_get_authentication_connection_for_uid(uid))) {
f09e2e
         verbose("Contacted ssh-agent of user %s (%u)", getpwuid(uid)->pw_name, uid);
f09e2e
         for (key = ssh_get_first_identity(ac, &comment, 2); key != NULL; key = ssh_get_next_identity(ac, &comment, 2)) 
f09e2e
         {
f09e2e
@@ -109,3 +208,4 @@ find_authorized_keys(uid_t uid)
f09e2e
     EVP_cleanup();
f09e2e
     return retval;
f09e2e
 }
f09e2e
+
f09e2e
diff -up pam_ssh_agent_auth-0.9.3/Makefile.in.psaa-build pam_ssh_agent_auth-0.9.3/Makefile.in
f09e2e
--- pam_ssh_agent_auth-0.9.3/Makefile.in.psaa-build	2009-10-27 21:19:41.000000000 +0100
f09e2e
+++ pam_ssh_agent_auth-0.9.3/Makefile.in	2012-06-21 20:14:56.432527764 +0200
f09e2e
@@ -28,7 +28,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@
f09e2e
@@ -37,7 +37,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
 
f09e2e
@@ -48,7 +48,7 @@ PAM_MODULES=pam_ssh_agent_auth.so
f09e2e
 
f09e2e
 SSHOBJS=xmalloc.o atomicio.o authfd.o bufaux.o bufbn.o buffer.o cleanup.o entropy.o fatal.o key.o log.o misc.o secure_filename.o ssh-dss.o ssh-rsa.o uuencode.o compat.o
f09e2e
 
f09e2e
-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
f09e2e
+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 secure_filename.o
f09e2e
 
f09e2e
 
f09e2e
 MANPAGES_IN	= pam_ssh_agent_auth.pod
f09e2e
@@ -67,13 +67,13 @@ $(PAM_MODULES): Makefile.in config.h
f09e2e
 .c.o:
f09e2e
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
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
 
f09e2e
-pam_ssh_agent_auth.so: $(LIBCOMPAT) $(SSHOBJS) $(PAM_SSH_AGENT_AUTH_OBJS)  pam_ssh_agent_auth.o
f09e2e
-	$(LD) $(LDFLAGS_SHARED) -o $@ $(SSHOBJS) $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lopenbsd-compat $(LIBS) -lpam pam_ssh_agent_auth.o
f09e2e
+pam_ssh_agent_auth.so: $(PAM_SSH_AGENT_AUTH_OBJS)  pam_ssh_agent_auth.o
f09e2e
+	$(LD) $(LDFLAGS_SHARED) -o $@ $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) -lpam -lnss3 pam_ssh_agent_auth.o
f09e2e
 
f09e2e
 $(MANPAGES): $(MANPAGES_IN)
f09e2e
 	pod2man --section=8 --release=v0.8 --name=pam_ssh_agent_auth --official --center "PAM" pam_ssh_agent_auth.pod > pam_ssh_agent_auth.8
f09e2e
diff -up pam_ssh_agent_auth-0.9.3/pam_user_authorized_keys.c.psaa-build pam_ssh_agent_auth-0.9.3/pam_user_authorized_keys.c