1d31ef
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c.psaa-seteuid openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c
1d31ef
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c.psaa-seteuid	2017-02-13 17:51:29.790005199 +0100
1d31ef
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/iterate_ssh_agent_keys.c	2017-02-13 17:52:53.640919947 +0100
1d31ef
@@ -238,17 +238,25 @@ ssh_get_authentication_socket_for_uid(ui
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 */
1d31ef
-	if (connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr) < 0) {
f09e2e
+	/* To ensure a race condition is not used to circumvent the stat
f09e2e
+	   above, we will temporarily drop UID to the caller */
f09e2e
+	if (seteuid(uid) == -1) {
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);
1d31ef
+		error("seteuid(%lu) failed with error: %s", (unsigned long) uid, strerror(errno));
1d31ef
 		return -1;
1d31ef
 	}
1d31ef
+	if (connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr) < 0) {
1d31ef
+		close(sock);
f09e2e
+		sock = -1;
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);
1d31ef
+	}
1d31ef
 
1d31ef
-	seteuid(0); /* we now continue the regularly scheduled programming */
1d31ef
-
1d31ef
+	/* we now continue the regularly scheduled programming */
1d31ef
+	if (0 != seteuid(0)) {
1d31ef
+	fatal("setuid(0) failed with error: %s", strerror(errno));
1d31ef
+	return -1;
1d31ef
+	}
1d31ef
 	return sock;
1d31ef
 }
f09e2e