2016-01-22 16:22:39 +00:00
|
|
|
diff -up openssh-7.1p2/pam_ssh_agent_auth-0.10.2/iterate_ssh_agent_keys.c.psaa-build openssh-7.1p2/pam_ssh_agent_auth-0.10.2/iterate_ssh_agent_keys.c
|
|
|
|
--- openssh-7.1p2/pam_ssh_agent_auth-0.10.2/iterate_ssh_agent_keys.c.psaa-build 2016-01-22 14:59:18.943919791 +0100
|
|
|
|
+++ openssh-7.1p2/pam_ssh_agent_auth-0.10.2/iterate_ssh_agent_keys.c 2016-01-22 15:16:12.534599318 +0100
|
|
|
|
@@ -43,12 +43,31 @@
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include "ssh2.h"
|
|
|
|
#include "misc.h"
|
2009-10-19 07:32:33 +00:00
|
|
|
+#include "ssh.h"
|
|
|
|
+#include <sys/types.h>
|
|
|
|
+#include <sys/stat.h>
|
|
|
|
+#include <sys/socket.h>
|
|
|
|
+#include <sys/un.h>
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <errno.h>
|
|
|
|
+#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "userauth_pubkey_from_id.h"
|
2016-01-22 16:22:39 +00:00
|
|
|
#include "identity.h"
|
|
|
|
#include "get_command_line.h"
|
|
|
|
extern char **environ;
|
2009-10-19 07:32:33 +00:00
|
|
|
|
|
|
|
+/*
|
|
|
|
+ * Added by Jamie Beverly, ensure socket fd points to a socket owned by the user
|
|
|
|
+ * A cursory check is done, but to avoid race conditions, it is necessary
|
|
|
|
+ * to drop effective UID when connecting to the socket.
|
|
|
|
+ *
|
|
|
|
+ * If the cause of error is EACCES, because we verified we would not have that
|
|
|
|
+ * problem initially, we can safely assume that somebody is attempting to find a
|
|
|
|
+ * race condition; so a more "direct" log message is generated.
|
|
|
|
+ */
|
|
|
|
+
|
2016-01-22 16:22:39 +00:00
|
|
|
static char *
|
|
|
|
log_action(char ** action, size_t count)
|
|
|
|
{
|
|
|
|
@@ -85,7 +104,7 @@ void
|
|
|
|
pamsshagentauth_session_id2_gen(Buffer * session_id2, const char * user,
|
|
|
|
const char * ruser, const char * servicename)
|
|
|
|
{
|
|
|
|
- char *cookie = NULL;
|
|
|
|
+ u_char *cookie = NULL;
|
|
|
|
uint8_t i = 0;
|
|
|
|
uint32_t rnd = 0;
|
|
|
|
uint8_t cookie_len;
|
|
|
|
@@ -110,7 +129,7 @@ pamsshagentauth_session_id2_gen(Buffer *
|
|
|
|
if (i % 4 == 0) {
|
|
|
|
rnd = pamsshagentauth_arc4random();
|
|
|
|
}
|
|
|
|
- cookie[i] = (char) rnd;
|
|
|
|
+ cookie[i] = (u_char) rnd;
|
|
|
|
rnd >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
@@ -142,6 +161,86 @@ pamsshagentauth_session_id2_gen(Buffer *
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-10-19 07:32:33 +00:00
|
|
|
+ssh_get_authentication_socket_for_uid(uid_t uid)
|
|
|
|
+{
|
|
|
|
+ const char *authsocket;
|
|
|
|
+ int sock;
|
|
|
|
+ struct sockaddr_un sunaddr;
|
|
|
|
+ struct stat sock_st;
|
|
|
|
+
|
|
|
|
+ authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
|
|
|
|
+ if (!authsocket)
|
|
|
|
+ return -1;
|
|
|
|
+
|
|
|
|
+ /* Advisory only; seteuid ensures no race condition; but will only log if we see EACCES */
|
|
|
|
+ if( stat(authsocket,&sock_st) == 0) {
|
|
|
|
+ if(uid != 0 && sock_st.st_uid != uid) {
|
|
|
|
+ fatal("uid %lu attempted to open an agent socket owned by uid %lu", (unsigned long) uid, (unsigned long) sock_st.st_uid);
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Ensures that the EACCES tested for below can _only_ happen if somebody
|
|
|
|
+ * is attempting to race the stat above to bypass authentication.
|
|
|
|
+ */
|
|
|
|
+ if( (sock_st.st_mode & S_IWUSR) != S_IWUSR || (sock_st.st_mode & S_IRUSR) != S_IRUSR) {
|
|
|
|
+ error("ssh-agent socket has incorrect permissions for owner");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sunaddr.sun_family = AF_UNIX;
|
|
|
|
+ strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
|
|
|
|
+
|
|
|
|
+ sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
+ if (sock < 0)
|
|
|
|
+ return -1;
|
|
|
|
+
|
|
|
|
+ /* close on exec */
|
|
|
|
+ if (fcntl(sock, F_SETFD, 1) == -1) {
|
|
|
|
+ close(sock);
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ errno = 0;
|
|
|
|
+ seteuid(uid); /* To ensure a race condition is not used to circumvent the stat
|
|
|
|
+ above, we will temporarily drop UID to the caller */
|
|
|
|
+ if (connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr) < 0) {
|
|
|
|
+ close(sock);
|
|
|
|
+ if(errno == EACCES)
|
|
|
|
+ fatal("MAJOR SECURITY WARNING: uid %lu made a deliberate and malicious attempt to open an agent socket owned by another user", (unsigned long) uid);
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ seteuid(0); /* we now continue the regularly scheduled programming */
|
|
|
|
+
|
|
|
|
+ return sock;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+AuthenticationConnection *
|
|
|
|
+ssh_get_authentication_connection_for_uid(uid_t uid)
|
|
|
|
+{
|
|
|
|
+ AuthenticationConnection *auth;
|
|
|
|
+ int sock;
|
|
|
|
+
|
|
|
|
+ sock = ssh_get_authentication_socket_for_uid(uid);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Fail if we couldn't obtain a connection. This happens if we
|
|
|
|
+ * exited due to a timeout.
|
|
|
|
+ */
|
|
|
|
+ if (sock < 0)
|
|
|
|
+ return NULL;
|
|
|
|
+
|
|
|
|
+ auth = xmalloc(sizeof(*auth));
|
|
|
|
+ auth->fd = sock;
|
|
|
|
+ buffer_init(&auth->identities);
|
|
|
|
+ auth->howmany = 0;
|
|
|
|
+
|
|
|
|
+ return auth;
|
|
|
|
+}
|
|
|
|
+
|
2016-01-22 16:22:39 +00:00
|
|
|
+int
|
|
|
|
pamsshagentauth_find_authorized_keys(const char * user, const char * ruser, const char * servicename)
|
2009-10-19 07:32:33 +00:00
|
|
|
{
|
2016-01-22 16:22:39 +00:00
|
|
|
Buffer session_id2 = { 0 };
|
|
|
|
@@ -190,7 +289,7 @@ pamsshagentauth_find_authorized_keys(con
|
2009-10-19 07:32:33 +00:00
|
|
|
OpenSSL_add_all_digests();
|
2016-01-22 16:22:39 +00:00
|
|
|
pamsshagentauth_session_id2_gen(&session_id2, user, ruser, servicename);
|
2009-10-19 07:32:33 +00:00
|
|
|
|
|
|
|
- if ((ac = ssh_get_authentication_connection(uid))) {
|
|
|
|
+ if ((ac = ssh_get_authentication_connection_for_uid(uid))) {
|
2016-01-22 16:22:39 +00:00
|
|
|
pamsshagentauth_verbose("Contacted ssh-agent of user %s (%u)", ruser, uid);
|
2009-10-19 07:32:33 +00:00
|
|
|
for (key = ssh_get_first_identity(ac, &comment, 2); key != NULL; key = ssh_get_next_identity(ac, &comment, 2))
|
|
|
|
{
|
2016-01-22 16:22:39 +00:00
|
|
|
diff -up openssh-7.1p2/pam_ssh_agent_auth-0.10.2/Makefile.in.psaa-build openssh-7.1p2/pam_ssh_agent_auth-0.10.2/Makefile.in
|
|
|
|
--- openssh-7.1p2/pam_ssh_agent_auth-0.10.2/Makefile.in.psaa-build 2014-03-31 19:35:17.000000000 +0200
|
|
|
|
+++ openssh-7.1p2/pam_ssh_agent_auth-0.10.2/Makefile.in 2016-01-22 15:20:16.479521651 +0100
|
|
|
|
@@ -52,7 +52,7 @@ PATHS=
|
2009-10-19 07:32:33 +00:00
|
|
|
CC=@CC@
|
|
|
|
LD=@LD@
|
|
|
|
CFLAGS=@CFLAGS@
|
|
|
|
-CPPFLAGS=-I. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@
|
|
|
|
+CPPFLAGS=-I.. -I$(srcdir) -I/usr/include/nss3 -I/usr/include/nspr4 @CPPFLAGS@ $(PATHS) @DEFS@
|
|
|
|
LIBS=@LIBS@
|
|
|
|
AR=@AR@
|
|
|
|
AWK=@AWK@
|
2016-01-22 16:22:39 +00:00
|
|
|
@@ -61,7 +61,7 @@ INSTALL=@INSTALL@
|
2009-10-19 07:32:33 +00:00
|
|
|
PERL=@PERL@
|
|
|
|
SED=@SED@
|
|
|
|
ENT=@ENT@
|
|
|
|
-LDFLAGS=-L. -Lopenbsd-compat/ @LDFLAGS@
|
|
|
|
+LDFLAGS=-L.. -L../openbsd-compat/ @LDFLAGS@
|
|
|
|
LDFLAGS_SHARED = @LDFLAGS_SHARED@
|
|
|
|
EXEEXT=@EXEEXT@
|
|
|
|
|
2016-01-22 16:22:39 +00:00
|
|
|
@@ -72,7 +72,7 @@ PAM_MODULES=pam_ssh_agent_auth.so
|
2009-10-19 07:32:33 +00:00
|
|
|
|
2016-01-22 16:22:39 +00:00
|
|
|
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 uidswap.o
|
2009-10-19 07:32:33 +00:00
|
|
|
|
2016-01-22 16:22:39 +00:00
|
|
|
-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
|
|
|
|
+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
|
2009-10-19 07:32:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
MANPAGES_IN = pam_ssh_agent_auth.pod
|
2016-01-22 16:22:39 +00:00
|
|
|
@@ -91,13 +91,13 @@ $(PAM_MODULES): Makefile.in config.h
|
2009-10-19 07:32:33 +00:00
|
|
|
.c.o:
|
|
|
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
|
|
|
|
|
|
|
|
-LIBCOMPAT=openbsd-compat/libopenbsd-compat.a
|
|
|
|
+LIBCOMPAT=../openbsd-compat/libopenbsd-compat.a
|
|
|
|
$(LIBCOMPAT): always
|
|
|
|
(cd openbsd-compat && $(MAKE))
|
|
|
|
always:
|
|
|
|
|
|
|
|
-pam_ssh_agent_auth.so: $(LIBCOMPAT) $(SSHOBJS) $(PAM_SSH_AGENT_AUTH_OBJS) pam_ssh_agent_auth.o
|
2016-01-22 16:22:39 +00:00
|
|
|
- $(LD) $(LDFLAGS_SHARED) -o $@ $(SSHOBJS) $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lopenbsd-compat pam_ssh_agent_auth.o $(LIBS) -lpam
|
2009-10-19 07:32:33 +00:00
|
|
|
+pam_ssh_agent_auth.so: $(PAM_SSH_AGENT_AUTH_OBJS) pam_ssh_agent_auth.o
|
2016-01-22 16:22:39 +00:00
|
|
|
+ $(LD) $(LDFLAGS_SHARED) -o $@ $(PAM_SSH_AGENT_AUTH_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat pam_ssh_agent_auth.o $(LIBS) -lpam -lnss3
|
2009-10-19 07:32:33 +00:00
|
|
|
|
|
|
|
$(MANPAGES): $(MANPAGES_IN)
|
2016-01-22 16:22:39 +00:00
|
|
|
pod2man --section=8 --release=v0.10.2 --name=pam_ssh_agent_auth --official --center "PAM" pam_ssh_agent_auth.pod > pam_ssh_agent_auth.8
|