vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/audit-bsm.c.audit4 openssh-5.9p1/audit-bsm.c
JFCH c2ea13
--- openssh-5.9p1/audit-bsm.c.audit4	2011-09-13 07:36:58.921674464 +0200
JFCH c2ea13
+++ openssh-5.9p1/audit-bsm.c	2011-09-13 07:36:59.171674206 +0200
Jan F. Chadima 69dd72
@@ -408,4 +408,10 @@ audit_kex_body(int ctos, char *enc, char
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	/* not implemented */
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	/* not implemented */
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
 #endif /* BSM */
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/audit-linux.c.audit4 openssh-5.9p1/audit-linux.c
JFCH c2ea13
--- openssh-5.9p1/audit-linux.c.audit4	2011-09-13 07:36:58.938720835 +0200
JFCH c2ea13
+++ openssh-5.9p1/audit-linux.c	2011-09-13 07:36:59.187673990 +0200
Jan F. Chadima 69dd72
@@ -292,6 +292,8 @@ audit_unsupported_body(int what)
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+const static char *direction[] = { "from-server", "from-client", "both" };
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
 audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid,
Jan F. Chadima 69dd72
 	       uid_t uid)
Jan F. Chadima 69dd72
@@ -299,7 +301,6 @@ audit_kex_body(int ctos, char *enc, char
Jan F. Chadima 69dd72
 #ifdef AUDIT_CRYPTO_SESSION
Jan F. Chadima 69dd72
 	char buf[AUDIT_LOG_SIZE];
Jan F. Chadima 69dd72
 	int audit_fd, audit_ok;
Jan F. Chadima 69dd72
-	const static char *direction[] = { "from-server", "from-client", "both" };
Jan F. Chadima 69dd72
 	Cipher *cipher = cipher_by_name(enc);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
Jan F. Chadima 69dd72
@@ -323,4 +324,30 @@ audit_kex_body(int ctos, char *enc, char
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	char buf[AUDIT_LOG_SIZE];
Jan F. Chadima 69dd72
+	int audit_fd, audit_ok;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
Jan F. Chadima 69dd72
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
Jan F. Chadima 69dd72
+		 get_remote_port(),
Jan F. Chadima 69dd72
+		 get_local_ipaddr(packet_get_connection_in()),
Jan F. Chadima 69dd72
+		 get_local_port());
Jan F. Chadima 69dd72
+	audit_fd = audit_open();
Jan F. Chadima 69dd72
+	if (audit_fd < 0) {
Jan F. Chadima 69dd72
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jan F. Chadima 69dd72
+					 errno != EAFNOSUPPORT)
Jan F. Chadima 69dd72
+			error("cannot open audit");
Jan F. Chadima 69dd72
+		return;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jan F. Chadima 69dd72
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
Jan F. Chadima 69dd72
+	audit_close(audit_fd);
Jan F. Chadima 69dd72
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jan F. Chadima 69dd72
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jan F. Chadima 69dd72
+		error("cannot write into audit");
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 #endif /* USE_LINUX_AUDIT */
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/audit.c.audit4 openssh-5.9p1/audit.c
JFCH c2ea13
--- openssh-5.9p1/audit.c.audit4	2011-09-13 07:36:58.954674484 +0200
JFCH c2ea13
+++ openssh-5.9p1/audit.c	2011-09-13 07:36:59.202799426 +0200
Jan F. Chadima 69dd72
@@ -143,6 +143,12 @@ audit_kex(int ctos, char *enc, char *mac
Jan F. Chadima 69dd72
 	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, getpid(), getuid()));
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_session_key_free(int ctos)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
Jan F. Chadima 69dd72
 /*
Jan F. Chadima 69dd72
  * Null implementations of audit functions.
Jan F. Chadima 69dd72
@@ -274,5 +280,15 @@ audit_kex_body(int ctos, char *enc, char
Jan F. Chadima 69dd72
 		(unsigned)geteuid(), ctos, enc, mac, compress, (long)pid,
Jan F. Chadima 69dd72
 	        (unsigned)uid);
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+/*
Jan F. Chadima 69dd72
+ * This will be called on succesfull session key discard
Jan F. Chadima 69dd72
+ */
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
Jan F. Chadima 69dd72
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
Jan F. Chadima 69dd72
 #endif /* SSH_AUDIT_EVENTS */
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/audit.h.audit4 openssh-5.9p1/audit.h
JFCH c2ea13
--- openssh-5.9p1/audit.h.audit4	2011-09-13 07:36:58.971799421 +0200
JFCH c2ea13
+++ openssh-5.9p1/audit.h	2011-09-13 07:36:59.216674281 +0200
Jan F. Chadima 69dd72
@@ -62,5 +62,7 @@ void	audit_unsupported(int);
Jan F. Chadima 69dd72
 void	audit_kex(int, char *, char *, char *);
Jan F. Chadima 69dd72
 void	audit_unsupported_body(int);
Jan F. Chadima 69dd72
 void	audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
Jan F. Chadima 69dd72
+void	audit_session_key_free(int ctos);
Jan F. Chadima 69dd72
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #endif /* _SSH_AUDIT_H */
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/auditstub.c.audit4 openssh-5.9p1/auditstub.c
JFCH c2ea13
--- openssh-5.9p1/auditstub.c.audit4	2011-09-13 07:36:58.986674407 +0200
JFCH c2ea13
+++ openssh-5.9p1/auditstub.c	2011-09-13 07:36:59.230674500 +0200
Jan F. Chadima 69dd72
@@ -27,6 +27,8 @@
Jan F. Chadima 69dd72
  * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
Jan F. Chadima 69dd72
  */
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+#include <sys/types.h>
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
 audit_unsupported(int n)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
@@ -37,3 +39,12 @@ audit_kex(int ctos, char *enc, char *mac
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_session_key_free(int ctos)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+}
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/kex.c.audit4 openssh-5.9p1/kex.c
JFCH c2ea13
--- openssh-5.9p1/kex.c.audit4	2011-09-13 07:36:59.032798982 +0200
JFCH c2ea13
+++ openssh-5.9p1/kex.c	2011-09-13 07:36:59.243799057 +0200
Jan F. Chadima 69dd72
@@ -624,3 +624,34 @@ dump_digest(char *msg, u_char *digest, i
Jan F. Chadima 69dd72
 	fprintf(stderr, "\n");
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+static void
Jan F. Chadima 69dd72
+enc_destroy(Enc *enc)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	if (enc == NULL)
Jan F. Chadima 69dd72
+		return;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (enc->key) {
Jan F. Chadima 69dd72
+		memset(enc->key, 0, enc->key_len);
Jan F. Chadima 69dd72
+		xfree(enc->key);
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (enc->iv) {
Jan F. Chadima 69dd72
+		memset(enc->iv,  0, enc->block_size);
Jan F. Chadima 69dd72
+		xfree(enc->iv);
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	memset(enc, 0, sizeof(*enc));
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+newkeys_destroy(Newkeys *newkeys)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	if (newkeys == NULL)
Jan F. Chadima 69dd72
+		return;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	enc_destroy(&newkeys->enc);
Jan F. Chadima 69dd72
+	mac_destroy(&newkeys->mac);
Jan F. Chadima 69dd72
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/kex.h.audit4 openssh-5.9p1/kex.h
Jan F. Chadima 39b26b
--- openssh-5.9p1/kex.h.audit4	2010-09-24 14:11:14.000000000 +0200
JFCH c2ea13
+++ openssh-5.9p1/kex.h	2011-09-13 07:36:59.259674391 +0200
Jan F. Chadima 69dd72
@@ -156,6 +156,8 @@ void	 kexgex_server(Kex *);
Jan F. Chadima 69dd72
 void	 kexecdh_client(Kex *);
Jan F. Chadima 69dd72
 void	 kexecdh_server(Kex *);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+void	newkeys_destroy(Newkeys *newkeys);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
Jan F. Chadima 69dd72
     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/mac.c.audit4 openssh-5.9p1/mac.c
Jan F. Chadima 39b26b
--- openssh-5.9p1/mac.c.audit4	2011-08-17 02:29:03.000000000 +0200
JFCH c2ea13
+++ openssh-5.9p1/mac.c	2011-09-13 07:36:59.273799275 +0200
Jan F. Chadima 69dd72
@@ -168,6 +168,20 @@ mac_clear(Mac *mac)
Jan F. Chadima 69dd72
 	mac->umac_ctx = NULL;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+mac_destroy(Mac *mac)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	if (mac == NULL)
Jan F. Chadima 69dd72
+		return;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (mac->key) {
Jan F. Chadima 69dd72
+		memset(mac->key, 0, mac->key_len);
Jan F. Chadima 69dd72
+		xfree(mac->key);
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	memset(mac, 0, sizeof(*mac));
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 /* XXX copied from ciphers_valid */
Jan F. Chadima 69dd72
 #define	MAC_SEP	","
Jan F. Chadima 69dd72
 int
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/mac.h.audit4 openssh-5.9p1/mac.h
Jan F. Chadima 39b26b
--- openssh-5.9p1/mac.h.audit4	2007-06-11 06:01:42.000000000 +0200
JFCH c2ea13
+++ openssh-5.9p1/mac.h	2011-09-13 07:36:59.286674543 +0200
Jan F. Chadima 69dd72
@@ -28,3 +28,4 @@ int	 mac_setup(Mac *, char *);
Jan F. Chadima 69dd72
 int	 mac_init(Mac *);
Jan F. Chadima 69dd72
 u_char	*mac_compute(Mac *, u_int32_t, u_char *, int);
Jan F. Chadima 69dd72
 void	 mac_clear(Mac *);
Jan F. Chadima 69dd72
+void	 mac_destroy(Mac *);
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/monitor.c.audit4 openssh-5.9p1/monitor.c
JFCH c2ea13
--- openssh-5.9p1/monitor.c.audit4	2011-09-13 07:36:59.058688802 +0200
JFCH c2ea13
+++ openssh-5.9p1/monitor.c	2011-09-13 07:38:37.825674060 +0200
Jan F. Chadima 69dd72
@@ -190,6 +190,7 @@ int mm_answer_audit_command(int, Buffer
Jan F. Chadima 69dd72
 int mm_answer_audit_end_command(int, Buffer *);
Jan F. Chadima 69dd72
 int mm_answer_audit_unsupported_body(int, Buffer *);
Jan F. Chadima 69dd72
 int mm_answer_audit_kex_body(int, Buffer *);
Jan F. Chadima 69dd72
+int mm_answer_audit_session_key_free_body(int, Buffer *);
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 static int monitor_read_log(struct monitor *);
Jan F. Chadima 69dd72
@@ -242,6 +243,7 @@ struct mon_table mon_dispatch_proto20[]
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F. Chadima 69dd72
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 #ifdef BSD_AUTH
Jan F. Chadima 69dd72
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Jan F. Chadima 69dd72
@@ -282,6 +284,7 @@ struct mon_table mon_dispatch_postauth20
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F. Chadima 69dd72
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
     {0, 0, NULL}
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
@@ -315,6 +318,7 @@ struct mon_table mon_dispatch_proto15[]
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F. Chadima 69dd72
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
     {0, 0, NULL}
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
@@ -329,6 +333,7 @@ struct mon_table mon_dispatch_postauth15
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F. Chadima 69dd72
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F. Chadima 69dd72
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
     {0, 0, NULL}
Jan F. Chadima 69dd72
 };
JFCH c2ea13
@@ -435,10 +440,6 @@ monitor_child_preauth(Authctxt *_authctx
JFCH c2ea13
 #endif
JFCH c2ea13
 	}
JFCH c2ea13
 
JFCH c2ea13
-	/* Drain any buffered messages from the child */
JFCH c2ea13
-	while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
JFCH c2ea13
-		;
JFCH c2ea13
-
JFCH c2ea13
 	if (!authctxt->valid)
JFCH c2ea13
 		fatal("%s: authenticated invalid user", __func__);
JFCH c2ea13
 	if (strcmp(auth_method, "unknown") == 0)
JFCH c2ea13
@@ -1925,11 +1926,13 @@ mm_get_keystate(struct monitor *pmonitor
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	blob = buffer_get_string(&m, &bloblen);
Jan F. Chadima 69dd72
 	current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Jan F. Chadima 69dd72
+	memset(blob, 0, bloblen);
Jan F. Chadima 69dd72
 	xfree(blob);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	debug3("%s: Waiting for second key", __func__);
Jan F. Chadima 69dd72
 	blob = buffer_get_string(&m, &bloblen);
Jan F. Chadima 69dd72
 	current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Jan F. Chadima 69dd72
+	memset(blob, 0, bloblen);
Jan F. Chadima 69dd72
 	xfree(blob);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	/* Now get sequence numbers for the packets */
JFCH c2ea13
@@ -1975,6 +1978,21 @@ mm_get_keystate(struct monitor *pmonitor
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	buffer_free(&m);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+#ifdef SSH_AUDIT_EVENTS
Jan F. Chadima 69dd72
+	if (compat20) {
Jan F. Chadima 69dd72
+		buffer_init(&m);
Jan F. Chadima 69dd72
+		mm_request_receive_expect(pmonitor->m_sendfd,
Jan F. Chadima 69dd72
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Jan F. Chadima 69dd72
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
Jan F. Chadima 69dd72
+		buffer_free(&m);
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+#endif
JFCH c2ea13
+
JFCH c2ea13
+	/* Drain any buffered messages from the child */
JFCH c2ea13
+	while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
JFCH c2ea13
+		;
JFCH c2ea13
+
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 
JFCH c2ea13
@@ -2429,4 +2447,22 @@ mm_answer_audit_kex_body(int sock, Buffe
Jan F. Chadima 69dd72
 	return 0;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+int
Jan F. Chadima 69dd72
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	int ctos;
Jan F. Chadima 69dd72
+	pid_t pid;
Jan F. Chadima 69dd72
+	uid_t uid;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	ctos = buffer_get_int(m);
Jan F. Chadima 69dd72
+	pid = buffer_get_int64(m);
Jan F. Chadima 69dd72
+	uid = buffer_get_int64(m);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	audit_session_key_free_body(ctos, pid, uid);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	buffer_clear(m);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
Jan F. Chadima 69dd72
+	return 0;
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
 #endif /* SSH_AUDIT_EVENTS */
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/monitor.h.audit4 openssh-5.9p1/monitor.h
JFCH c2ea13
--- openssh-5.9p1/monitor.h.audit4	2011-09-13 07:36:59.076799458 +0200
JFCH c2ea13
+++ openssh-5.9p1/monitor.h	2011-09-13 07:36:59.322799576 +0200
Jan F. Chadima 69dd72
@@ -63,6 +63,7 @@ enum monitor_reqtype {
Jan F. Chadima 69dd72
 	MONITOR_ANS_AUDIT_COMMAND, MONITOR_REQ_AUDIT_END_COMMAND,
Jan F. Chadima 69dd72
 	MONITOR_REQ_AUDIT_UNSUPPORTED, MONITOR_ANS_AUDIT_UNSUPPORTED,
Jan F. Chadima 69dd72
 	MONITOR_REQ_AUDIT_KEX, MONITOR_ANS_AUDIT_KEX,
Jan F. Chadima 69dd72
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
Jan F. Chadima 69dd72
 	MONITOR_REQ_TERM,
Jan F. Chadima 69dd72
 	MONITOR_REQ_JPAKE_STEP1, MONITOR_ANS_JPAKE_STEP1,
Jan F. Chadima 69dd72
 	MONITOR_REQ_JPAKE_GET_PWDATA, MONITOR_ANS_JPAKE_GET_PWDATA,
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/monitor_wrap.c.audit4 openssh-5.9p1/monitor_wrap.c
JFCH c2ea13
--- openssh-5.9p1/monitor_wrap.c.audit4	2011-09-13 07:36:59.100724984 +0200
JFCH c2ea13
+++ openssh-5.9p1/monitor_wrap.c	2011-09-13 07:36:59.339674340 +0200
Jan F. Chadima 69dd72
@@ -653,12 +653,14 @@ mm_send_keystate(struct monitor *monitor
Jan F. Chadima 69dd72
 		fatal("%s: conversion of newkeys failed", __func__);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	buffer_put_string(&m, blob, bloblen);
Jan F. Chadima 69dd72
+	memset(blob, 0, bloblen);
Jan F. Chadima 69dd72
 	xfree(blob);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Jan F. Chadima 69dd72
 		fatal("%s: conversion of newkeys failed", __func__);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	buffer_put_string(&m, blob, bloblen);
Jan F. Chadima 69dd72
+	memset(blob, 0, bloblen);
Jan F. Chadima 69dd72
 	xfree(blob);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Jan F. Chadima 69dd72
@@ -1542,4 +1544,19 @@ mm_audit_kex_body(int ctos, char *cipher
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	buffer_free(&m);
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	Buffer m;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	buffer_init(&m);
Jan F. Chadima 69dd72
+	buffer_put_int(&m, ctos);
Jan F. Chadima 69dd72
+	buffer_put_int64(&m, pid);
Jan F. Chadima 69dd72
+	buffer_put_int64(&m, uid);
Jan F. Chadima 69dd72
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Jan F. Chadima 69dd72
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
Jan F. Chadima 69dd72
+				  &m);
Jan F. Chadima 69dd72
+	buffer_free(&m);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
 #endif /* SSH_AUDIT_EVENTS */
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/monitor_wrap.h.audit4 openssh-5.9p1/monitor_wrap.h
JFCH c2ea13
--- openssh-5.9p1/monitor_wrap.h.audit4	2011-09-13 07:36:59.118674223 +0200
JFCH c2ea13
+++ openssh-5.9p1/monitor_wrap.h	2011-09-13 07:36:59.353674499 +0200
Jan F. Chadima 69dd72
@@ -80,6 +80,7 @@ int mm_audit_run_command(const char *);
Jan F. Chadima 69dd72
 void mm_audit_end_command(int, const char *);
Jan F. Chadima 69dd72
 void mm_audit_unsupported_body(int);
Jan F. Chadima 69dd72
 void mm_audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
Jan F. Chadima 69dd72
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 struct Session;
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/packet.c.audit4 openssh-5.9p1/packet.c
JFCH c2ea13
--- openssh-5.9p1/packet.c.audit4	2011-09-13 07:36:58.244674109 +0200
JFCH c2ea13
+++ openssh-5.9p1/packet.c	2011-09-13 07:36:59.373710318 +0200
Jan F. Chadima 69dd72
@@ -60,6 +60,7 @@
Jan F. Chadima 69dd72
 #include <signal.h>
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include "xmalloc.h"
Jan F. Chadima 69dd72
+#include "audit.h"
Jan F. Chadima 69dd72
 #include "buffer.h"
Jan F. Chadima 69dd72
 #include "packet.h"
Jan F. Chadima 69dd72
 #include "crc32.h"
Jan F. Chadima 69dd72
@@ -472,6 +473,13 @@ packet_get_connection_out(void)
Jan F. Chadima 69dd72
 	return active_state->connection_out;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+static int
Jan F. Chadima 69dd72
+packet_state_has_keys (const struct session_state *state)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	return state != NULL &&
Jan F. Chadima 69dd72
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 /* Closes the connection and clears and frees internal data structures. */
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
@@ -480,13 +488,6 @@ packet_close(void)
Jan F. Chadima 69dd72
 	if (!active_state->initialized)
Jan F. Chadima 69dd72
 		return;
Jan F. Chadima 69dd72
 	active_state->initialized = 0;
Jan F. Chadima 69dd72
-	if (active_state->connection_in == active_state->connection_out) {
Jan F. Chadima 69dd72
-		shutdown(active_state->connection_out, SHUT_RDWR);
Jan F. Chadima 69dd72
-		close(active_state->connection_out);
Jan F. Chadima 69dd72
-	} else {
Jan F. Chadima 69dd72
-		close(active_state->connection_in);
Jan F. Chadima 69dd72
-		close(active_state->connection_out);
Jan F. Chadima 69dd72
-	}
Jan F. Chadima 69dd72
 	buffer_free(&active_state->input);
Jan F. Chadima 69dd72
 	buffer_free(&active_state->output);
Jan F. Chadima 69dd72
 	buffer_free(&active_state->outgoing_packet);
Jan F. Chadima 69dd72
@@ -495,8 +496,18 @@ packet_close(void)
Jan F. Chadima 69dd72
 		buffer_free(&active_state->compression_buffer);
Jan F. Chadima 69dd72
 		buffer_compress_uninit();
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
-	cipher_cleanup(&active_state->send_context);
Jan F. Chadima 69dd72
-	cipher_cleanup(&active_state->receive_context);
Jan F. Chadima 69dd72
+	if (packet_state_has_keys(active_state)) {
Jan F. Chadima 69dd72
+		cipher_cleanup(&active_state->send_context);
Jan F. Chadima 69dd72
+		cipher_cleanup(&active_state->receive_context);
Jan F. Chadima 69dd72
+		audit_session_key_free(2);
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+	if (active_state->connection_in == active_state->connection_out) {
Jan F. Chadima 69dd72
+		shutdown(active_state->connection_out, SHUT_RDWR);
Jan F. Chadima 69dd72
+		close(active_state->connection_out);
Jan F. Chadima 69dd72
+	} else {
Jan F. Chadima 69dd72
+		close(active_state->connection_in);
Jan F. Chadima 69dd72
+		close(active_state->connection_out);
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 /* Sets remote side protocol flags. */
Jan F. Chadima 69dd72
@@ -731,6 +742,23 @@ packet_send1(void)
Jan F. Chadima 69dd72
 	 */
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+static void
Jan F. Chadima 69dd72
+newkeys_destroy_and_free(Newkeys *newkeys)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	if (newkeys == NULL)
Jan F. Chadima 69dd72
+		return;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	xfree(newkeys->enc.name);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	mac_clear(&newkeys->mac);
Jan F. Chadima 69dd72
+	xfree(newkeys->mac.name);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	xfree(newkeys->comp.name);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	newkeys_destroy(newkeys);
Jan F. Chadima 69dd72
+	xfree(newkeys);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
 set_newkeys(int mode)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
@@ -756,18 +784,9 @@ set_newkeys(int mode)
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 	if (active_state->newkeys[mode] != NULL) {
Jan F. Chadima 69dd72
 		debug("set_newkeys: rekeying");
Jan F. Chadima 69dd72
+		audit_session_key_free(mode);
Jan F. Chadima 69dd72
 		cipher_cleanup(cc);
Jan F. Chadima 69dd72
-		enc  = &active_state->newkeys[mode]->enc;
Jan F. Chadima 69dd72
-		mac  = &active_state->newkeys[mode]->mac;
Jan F. Chadima 69dd72
-		comp = &active_state->newkeys[mode]->comp;
Jan F. Chadima 69dd72
-		mac_clear(mac);
Jan F. Chadima 69dd72
-		xfree(enc->name);
Jan F. Chadima 69dd72
-		xfree(enc->iv);
Jan F. Chadima 69dd72
-		xfree(enc->key);
Jan F. Chadima 69dd72
-		xfree(mac->name);
Jan F. Chadima 69dd72
-		xfree(mac->key);
Jan F. Chadima 69dd72
-		xfree(comp->name);
Jan F. Chadima 69dd72
-		xfree(active_state->newkeys[mode]);
Jan F. Chadima 69dd72
+		newkeys_destroy_and_free(active_state->newkeys[mode]);
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 	active_state->newkeys[mode] = kex_get_newkeys(mode);
Jan F. Chadima 69dd72
 	if (active_state->newkeys[mode] == NULL)
Jan F. Chadima 39b26b
@@ -1927,6 +1946,47 @@ packet_get_newkeys(int mode)
Jan F. Chadima 69dd72
 	return (void *)active_state->newkeys[mode];
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+static void
Jan F. Chadima 69dd72
+packet_destroy_state(struct session_state *state)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	if (state == NULL)
Jan F. Chadima 69dd72
+		return;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	cipher_cleanup(&state->receive_context);
Jan F. Chadima 69dd72
+	cipher_cleanup(&state->send_context);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	buffer_free(&state->input);
Jan F. Chadima 69dd72
+	buffer_free(&state->output);
Jan F. Chadima 69dd72
+	buffer_free(&state->outgoing_packet);
Jan F. Chadima 69dd72
+	buffer_free(&state->incoming_packet);
Jan F. Chadima 69dd72
+	buffer_free(&state->compression_buffer);
Jan F. Chadima 69dd72
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
Jan F. Chadima 69dd72
+	state->newkeys[MODE_IN] = NULL;
Jan F. Chadima 69dd72
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
Jan F. Chadima 69dd72
+	state->newkeys[MODE_OUT] = NULL;
Jan F. Chadima 69dd72
+	mac_destroy(state->packet_discard_mac);
Jan F. Chadima 69dd72
+//	TAILQ_HEAD(, packet) outgoing;
Jan F. Chadima 69dd72
+//	memset(state, 0, sizeof(state));
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+packet_destroy_all(int audit_it, int privsep)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	if (audit_it)
Jan F. Chadima 69dd72
+		audit_it = packet_state_has_keys (active_state) ||
Jan F. Chadima 69dd72
+			packet_state_has_keys (backup_state);
Jan F. Chadima 69dd72
+	packet_destroy_state(active_state);
Jan F. Chadima 69dd72
+	packet_destroy_state(backup_state);
Jan F. Chadima 69dd72
+	if (audit_it) {
Jan F. Chadima 69dd72
+#ifdef SSH_AUDIT_EVENTS
Jan F. Chadima 69dd72
+		if (privsep)
Jan F. Chadima 69dd72
+			audit_session_key_free(2);
Jan F. Chadima 69dd72
+		else
Jan F. Chadima 69dd72
+			audit_session_key_free_body(2, getpid(), getuid());
Jan F. Chadima 69dd72
+#endif
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 /*
Jan F. Chadima 69dd72
  * Save the state for the real connection, and use a separate state when
Jan F. Chadima 69dd72
  * resuming a suspended connection.
Jan F. Chadima 39b26b
@@ -1934,18 +1994,12 @@ packet_get_newkeys(int mode)
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
 packet_backup_state(void)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
-	struct session_state *tmp;
Jan F. Chadima 69dd72
-
Jan F. Chadima 69dd72
 	close(active_state->connection_in);
Jan F. Chadima 69dd72
 	active_state->connection_in = -1;
Jan F. Chadima 69dd72
 	close(active_state->connection_out);
Jan F. Chadima 69dd72
 	active_state->connection_out = -1;
Jan F. Chadima 69dd72
-	if (backup_state)
Jan F. Chadima 69dd72
-		tmp = backup_state;
Jan F. Chadima 69dd72
-	else
Jan F. Chadima 69dd72
-		tmp = alloc_session_state();
Jan F. Chadima 69dd72
 	backup_state = active_state;
Jan F. Chadima 69dd72
-	active_state = tmp;
Jan F. Chadima 69dd72
+	active_state = alloc_session_state();
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 /*
Jan F. Chadima 39b26b
@@ -1962,9 +2016,7 @@ packet_restore_state(void)
Jan F. Chadima 69dd72
 	backup_state = active_state;
Jan F. Chadima 69dd72
 	active_state = tmp;
Jan F. Chadima 69dd72
 	active_state->connection_in = backup_state->connection_in;
Jan F. Chadima 69dd72
-	backup_state->connection_in = -1;
Jan F. Chadima 69dd72
 	active_state->connection_out = backup_state->connection_out;
Jan F. Chadima 69dd72
-	backup_state->connection_out = -1;
Jan F. Chadima 69dd72
 	len = buffer_len(&backup_state->input);
Jan F. Chadima 69dd72
 	if (len > 0) {
Jan F. Chadima 69dd72
 		buf = buffer_ptr(&backup_state->input);
Jan F. Chadima 39b26b
@@ -1972,4 +2024,10 @@ packet_restore_state(void)
Jan F. Chadima 69dd72
 		buffer_clear(&backup_state->input);
Jan F. Chadima 69dd72
 		add_recv_bytes(len);
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
+	backup_state->connection_in = -1;
Jan F. Chadima 69dd72
+	backup_state->connection_out = -1;
Jan F. Chadima 69dd72
+	packet_destroy_state(backup_state);
Jan F. Chadima 69dd72
+	xfree(backup_state);
Jan F. Chadima 69dd72
+	backup_state = NULL;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
+
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/packet.h.audit4 openssh-5.9p1/packet.h
Jan F. Chadima 39b26b
--- openssh-5.9p1/packet.h.audit4	2011-05-15 00:43:13.000000000 +0200
JFCH c2ea13
+++ openssh-5.9p1/packet.h	2011-09-13 07:36:59.390799281 +0200
Jan F. Chadima 69dd72
@@ -124,4 +124,5 @@ void	 packet_restore_state(void);
Jan F. Chadima 69dd72
 void	*packet_get_input(void);
Jan F. Chadima 69dd72
 void	*packet_get_output(void);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+void	 packet_destroy_all(int, int);
Jan F. Chadima 69dd72
 #endif				/* PACKET_H */
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/session.c.audit4 openssh-5.9p1/session.c
JFCH c2ea13
--- openssh-5.9p1/session.c.audit4	2011-09-13 07:36:58.637798995 +0200
JFCH c2ea13
+++ openssh-5.9p1/session.c	2011-09-13 07:36:59.411690264 +0200
Jan F. Chadima 69dd72
@@ -1634,6 +1634,9 @@ do_child(Session *s, const char *command
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	/* remove hostkey from the child's memory */
Jan F. Chadima 69dd72
 	destroy_sensitive_data();
Jan F. Chadima 69dd72
+	/* Don't audit this - both us and the parent would be talking to the
Jan F. Chadima 69dd72
+	   monitor over a single socket, with no synchronization. */
Jan F. Chadima 69dd72
+	packet_destroy_all(0, 1);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	/* Force a password change */
Jan F. Chadima 69dd72
 	if (s->authctxt->force_pwchange) {
Jan F. Chadima 39b26b
diff -up openssh-5.9p1/sshd.c.audit4 openssh-5.9p1/sshd.c
JFCH c2ea13
--- openssh-5.9p1/sshd.c.audit4	2011-09-13 07:36:59.143674103 +0200
JFCH c2ea13
+++ openssh-5.9p1/sshd.c	2011-09-13 07:39:06.125718627 +0200
Jan F. Chadima 69dd72
@@ -684,6 +684,8 @@ privsep_preauth(Authctxt *authctxt)
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+extern Newkeys *current_keys[];
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 static void
Jan F. Chadima 69dd72
 privsep_postauth(Authctxt *authctxt)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
@@ -708,6 +710,10 @@ privsep_postauth(Authctxt *authctxt)
Jan F. Chadima 69dd72
 	else if (pmonitor->m_pid != 0) {
Jan F. Chadima 69dd72
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
Jan F. Chadima 69dd72
 		buffer_clear(&loginmsg);
Jan F. Chadima 69dd72
+ 		newkeys_destroy(current_keys[MODE_OUT]);
Jan F. Chadima 69dd72
+		newkeys_destroy(current_keys[MODE_IN]);
Jan F. Chadima 69dd72
+		audit_session_key_free_body(2, getpid(), getuid());
Jan F. Chadima 69dd72
+		packet_destroy_all(0, 0);
Jan F. Chadima 69dd72
 		monitor_child_postauth(pmonitor);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 		/* NEVERREACHED */
JFCH c2ea13
@@ -1999,6 +2005,7 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 	 */
Jan F. Chadima 69dd72
 	if (use_privsep) {
Jan F. Chadima 69dd72
 		mm_send_keystate(pmonitor);
Jan F. Chadima 69dd72
+		packet_destroy_all(1, 1);
Jan F. Chadima 69dd72
 		exit(0);
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 
JFCH c2ea13
@@ -2051,6 +2058,8 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 	do_authenticated(authctxt);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	/* The connection has been terminated. */
Jan F. Chadima 69dd72
+	packet_destroy_all(1, 1);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
Jan F. Chadima 69dd72
 	packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
Jan F. Chadima 69dd72
 	verbose("Transferred: sent %llu, received %llu bytes",
JFCH c2ea13
@@ -2368,8 +2377,20 @@ do_ssh2_kex(void)
Jan F. Chadima 39b26b
 void
Jan F. Chadima 39b26b
 cleanup_exit(int i)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 39b26b
+	static int in_cleanup = 0;
Jan F. Chadima 39b26b
+	int is_privsep_child;
Jan F. Chadima 39b26b
+
Jan F. Chadima 39b26b
+	/* cleanup_exit can be called at the very least from the privsep
Jan F. Chadima 39b26b
+	   wrappers used for auditing.  Make sure we don't recurse
Jan F. Chadima 39b26b
+	   indefinitely. */
Jan F. Chadima 39b26b
+	if (in_cleanup)
Jan F. Chadima 39b26b
+		_exit(i);
Jan F. Chadima 39b26b
+	in_cleanup = 1;
Jan F. Chadima 39b26b
+
Jan F. Chadima 69dd72
 	if (the_authctxt)
Jan F. Chadima 69dd72
 		do_cleanup(the_authctxt);
Jan F. Chadima 39b26b
+	is_privsep_child = use_privsep && pmonitor != NULL && !mm_is_monitor();
Jan F. Chadima 69dd72
+	packet_destroy_all(1, is_privsep_child);
Jan F. Chadima 69dd72
 #ifdef SSH_AUDIT_EVENTS
Jan F. Chadima 69dd72
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
Jan F. Chadima 69dd72
 	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&