e47cb00
diff -up pam_ssh_agent_auth-0.9/iterate_ssh_agent_keys.c.psaa-build pam_ssh_agent_auth-0.9/iterate_ssh_agent_keys.c
e47cb00
--- pam_ssh_agent_auth-0.9/iterate_ssh_agent_keys.c.psaa-build	2009-08-08 11:51:04.000000000 +0200
e47cb00
+++ pam_ssh_agent_auth-0.9/iterate_ssh_agent_keys.c	2009-10-16 15:20:55.000000000 +0200
e47cb00
@@ -41,7 +41,16 @@
e47cb00
 #include "buffer.h"
e47cb00
 #include "key.h"
e47cb00
 #include "authfd.h"
e47cb00
+#include "ssh.h"
e47cb00
 #include <stdio.h>
e47cb00
+#include <sys/types.h>
e47cb00
+#include <sys/stat.h>
e47cb00
+#include <sys/socket.h>
e47cb00
+#include <sys/un.h>
e47cb00
+#include <unistd.h>
e47cb00
+#include <stdlib.h>
e47cb00
+#include <errno.h>
e47cb00
+#include <fcntl.h>
e47cb00
 #include <openssl/evp.h>
e47cb00
 
e47cb00
 #include "userauth_pubkey_from_id.h"
e47cb00
@@ -73,6 +82,96 @@ session_id2_gen()
e47cb00
     return cookie;
e47cb00
 }
e47cb00
 
e47cb00
+/* 
e47cb00
+ * Added by Jamie Beverly, ensure socket fd points to a socket owned by the user 
e47cb00
+ * A cursory check is done, but to avoid race conditions, it is necessary 
e47cb00
+ * to drop effective UID when connecting to the socket. 
e47cb00
+ *
e47cb00
+ * If the cause of error is EACCES, because we verified we would not have that 
e47cb00
+ * problem initially, we can safely assume that somebody is attempting to find a 
e47cb00
+ * race condition; so a more "direct" log message is generated.
e47cb00
+ */
e47cb00
+
e47cb00
+int
e47cb00
+ssh_get_authentication_socket_for_uid(uid_t uid)
e47cb00
+{
e47cb00
+	const char *authsocket;
e47cb00
+	int sock;
e47cb00
+	struct sockaddr_un sunaddr;
e47cb00
+	struct stat sock_st;
e47cb00
+
e47cb00
+	authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
e47cb00
+	if (!authsocket)
e47cb00
+		return -1;
e47cb00
+
e47cb00
+	/* Advisory only; seteuid ensures no race condition; but will only log if we see EACCES */
e47cb00
+	if( stat(authsocket,&sock_st) == 0) {
e47cb00
+		if(uid != 0 && sock_st.st_uid != uid) {
e47cb00
+			fatal("uid %lu attempted to open an agent socket owned by uid %lu", (unsigned long) uid, (unsigned long) sock_st.st_uid);
e47cb00
+			return -1;
e47cb00
+		}
e47cb00
+	}
e47cb00
+
e47cb00
+	/* 
e47cb00
+	 * Ensures that the EACCES tested for below can _only_ happen if somebody 
e47cb00
+	 * is attempting to race the stat above to bypass authentication.
e47cb00
+	 */
e47cb00
+	if( (sock_st.st_mode & S_IWUSR) != S_IWUSR || (sock_st.st_mode & S_IRUSR) != S_IRUSR) {
e47cb00
+		error("ssh-agent socket has incorrect permissions for owner");
e47cb00
+		return -1;
e47cb00
+	}
e47cb00
+
e47cb00
+	sunaddr.sun_family = AF_UNIX;
e47cb00
+	strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
e47cb00
+
e47cb00
+	sock = socket(AF_UNIX, SOCK_STREAM, 0);
e47cb00
+	if (sock < 0)
e47cb00
+		return -1;
e47cb00
+
e47cb00
+	/* close on exec */
e47cb00
+	if (fcntl(sock, F_SETFD, 1) == -1) {
e47cb00
+		close(sock);
e47cb00
+		return -1;
e47cb00
+	}
e47cb00
+
e47cb00
+	errno = 0; 
e47cb00
+	seteuid(uid); /* To ensure a race condition is not used to circumvent the stat
e47cb00
+	             above, we will temporarily drop UID to the caller */
e47cb00
+	if (connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr) < 0) {
e47cb00
+		close(sock);
e47cb00
+        if(errno == EACCES)
e47cb00
+		fatal("MAJOR SECURITY WARNING: uid %lu made a deliberate and malicious attempt to open an agent socket owned by another user", (unsigned long) uid);
e47cb00
+		return -1;
e47cb00
+	}
e47cb00
+
e47cb00
+	seteuid(0); /* we now continue the regularly scheduled programming */
e47cb00
+
e47cb00
+	return sock;
e47cb00
+}
e47cb00
+
e47cb00
+AuthenticationConnection *
e47cb00
+ssh_get_authentication_connection_for_uid(uid_t uid)
e47cb00
+{
e47cb00
+	AuthenticationConnection *auth;
e47cb00
+	int sock;
e47cb00
+
e47cb00
+	sock = ssh_get_authentication_socket_for_uid(uid);
e47cb00
+
e47cb00
+	/*
e47cb00
+	 * Fail if we couldn't obtain a connection.  This happens if we
e47cb00
+	 * exited due to a timeout.
e47cb00
+	 */
e47cb00
+	if (sock < 0)
e47cb00
+		return NULL;
e47cb00
+
e47cb00
+	auth = xmalloc(sizeof(*auth));
e47cb00
+	auth->fd = sock;
e47cb00
+	buffer_init(&auth->identities);
e47cb00
+	auth->howmany = 0;
e47cb00
+
e47cb00
+	return auth;
e47cb00
+}
e47cb00
+
e47cb00
 int
e47cb00
 find_authorized_keys(uid_t uid)
e47cb00
 {
e47cb00
@@ -85,7 +184,7 @@ find_authorized_keys(uid_t uid)
e47cb00
     OpenSSL_add_all_digests();
e47cb00
     session_id2 = session_id2_gen();
e47cb00
 
e47cb00
-    if ((ac = ssh_get_authentication_connection(uid))) {
e47cb00
+    if ((ac = ssh_get_authentication_connection_for_uid(uid))) {
e47cb00
         verbose("Contacted ssh-agent of user %s (%u)", getpwuid(uid)->pw_name, uid);
e47cb00
         for (key = ssh_get_first_identity(ac, &comment, 2); key != NULL; key = ssh_get_next_identity(ac, &comment, 2)) 
e47cb00
         {
e47cb00
@@ -113,3 +212,4 @@ find_authorized_keys(uid_t uid)
e47cb00
     EVP_cleanup();
e47cb00
     return retval;
e47cb00
 }
e47cb00
+
e47cb00
diff -up pam_ssh_agent_auth-0.9/Makefile.in.psaa-build pam_ssh_agent_auth-0.9/Makefile.in
e47cb00
--- pam_ssh_agent_auth-0.9/Makefile.in.psaa-build	2009-08-06 07:40:16.000000000 +0200
e47cb00
+++ pam_ssh_agent_auth-0.9/Makefile.in	2009-10-16 15:20:55.000000000 +0200
e47cb00
@@ -28,7 +28,7 @@ PATHS=
e47cb00
 CC=@CC@
e47cb00
 LD=@LD@
e47cb00
 CFLAGS=@CFLAGS@
e47cb00
-CPPFLAGS=-I. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@
e47cb00
+CPPFLAGS=-I.. -I$(srcdir) -I/usr/include/nss3 -I/usr/include/nspr4 @CPPFLAGS@ $(PATHS) @DEFS@
e47cb00
 LIBS=@LIBS@
e47cb00
 AR=@AR@
e47cb00
 AWK=@AWK@
e47cb00
@@ -37,7 +37,7 @@ INSTALL=@INSTALL@
e47cb00
 PERL=@PERL@
e47cb00
 SED=@SED@
e47cb00
 ENT=@ENT@
e47cb00
-LDFLAGS=-L. -Lopenbsd-compat/ @LDFLAGS@
e47cb00
+LDFLAGS=-L.. -L../openbsd-compat/ @LDFLAGS@
e47cb00
 LDFLAGS_SHARED = @LDFLAGS_SHARED@
e47cb00
 EXEEXT=@EXEEXT@
e47cb00
 
e47cb00
@@ -48,7 +48,7 @@ PAM_MODULES=pam_ssh_agent_auth.so
e47cb00
 
e47cb00
 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
e47cb00
 
e47cb00
-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
e47cb00
+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
e47cb00
 
e47cb00
 
e47cb00
 MANPAGES_IN	= pam_ssh_agent_auth.pod
e47cb00
@@ -67,13 +67,13 @@ $(PAM_MODULES): Makefile.in config.h
e47cb00
 .c.o:
e47cb00
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
e47cb00
 
e47cb00
-LIBCOMPAT=openbsd-compat/libopenbsd-compat.a
e47cb00
+LIBCOMPAT=../openbsd-compat/libopenbsd-compat.a
e47cb00
 $(LIBCOMPAT): always
e47cb00
 	(cd openbsd-compat && $(MAKE))
e47cb00
 always:
e47cb00
 
e47cb00
-pam_ssh_agent_auth.so: $(LIBCOMPAT) $(SSHOBJS) $(PAM_SSH_AGENT_AUTH_OBJS)  pam_ssh_agent_auth.o
e47cb00
-	$(LD) $(LDFLAGS_SHARED) -o $@ $(SSHOBJS) $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lopenbsd-compat $(LIBS) -lpam pam_ssh_agent_auth.o
e47cb00
+pam_ssh_agent_auth.so: $(PAM_SSH_AGENT_AUTH_OBJS)  pam_ssh_agent_auth.o
e47cb00
+	$(LD) $(LDFLAGS_SHARED) -o $@ $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) -lpam -lnss3 pam_ssh_agent_auth.o
e47cb00
 
e47cb00
 $(MANPAGES): $(MANPAGES_IN)
e47cb00
 	pod2man --section=8 --release=v0.8 --name=pam_ssh_agent_auth --official --center "PAM" pam_ssh_agent_auth.pod > pam_ssh_agent_auth.8
e47cb00
diff -up pam_ssh_agent_auth-0.9/pam_user_authorized_keys.c.psaa-build pam_ssh_agent_auth-0.9/pam_user_authorized_keys.c
e47cb00
--- pam_ssh_agent_auth-0.9/pam_user_authorized_keys.c.psaa-build	2009-07-29 02:46:38.000000000 +0200
e47cb00
+++ pam_ssh_agent_auth-0.9/pam_user_authorized_keys.c	2009-10-16 15:50:36.000000000 +0200
e47cb00
@@ -94,7 +94,7 @@ parse_authorized_key_file(const char *us
e47cb00
     /*
e47cb00
      * temporary copy, so that both tilde expansion and percent expansion both get to apply to the path
e47cb00
      */
e47cb00
-    strncat(auth_keys_file_buf, authorized_keys_file_input, 4096);
e47cb00
+    strncat(auth_keys_file_buf, authorized_keys_file_input, sizeof(auth_keys_file_buf)-1);
e47cb00
 
e47cb00
     if(allow_user_owned_authorized_keys_file)
e47cb00
         authorized_keys_file_allowed_owner_uid = getpwnam(user)->pw_uid;