vishalmishra434 / rpms / openssh

Forked from rpms/openssh 3 months ago
Clone
Jan F 003cb0
diff -up openssh-5.8p1/audit-bsm.c.audit4 openssh-5.8p1/audit-bsm.c
Jan F f9ff10
--- openssh-5.8p1/audit-bsm.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/audit-bsm.c	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -401,4 +401,10 @@ audit_kex_body(int ctos, char *enc, char
Jan F 9828ff
 {
Jan F 9828ff
 	/* not implemented */
Jan F 9828ff
 }
Jan F 9828ff
+
Jan F 9828ff
+void
Jan F c2b009
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F 9828ff
+{
Jan F 9828ff
+	/* not implemented */
Jan F 9828ff
+}
Jan F 9828ff
 #endif /* BSM */
Jan F 003cb0
diff -up openssh-5.8p1/audit.c.audit4 openssh-5.8p1/audit.c
Jan F f9ff10
--- openssh-5.8p1/audit.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/audit.c	2011-02-24 13:54:02.000000000 +0100
Jan F 9cefae
@@ -143,6 +143,12 @@ audit_kex(int ctos, char *enc, char *mac
Jan F 9cefae
 	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, getpid(), getuid()));
Jan F 003cb0
 }
Jan F 003cb0
 
Jan F 003cb0
+void
Jan F 003cb0
+audit_session_key_free(int ctos)
Jan F 003cb0
+{
Jan F c2b009
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
Jan F 003cb0
+}
Jan F 003cb0
+
Jan F 003cb0
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
Jan F 003cb0
 /*
Jan F 003cb0
  * Null implementations of audit functions.
Jan F f9ff10
@@ -260,5 +266,15 @@ audit_kex_body(int ctos, char *enc, char
Jan F 9cefae
 		(unsigned)geteuid(), ctos, enc, mac, compress, (long)pid,
Jan F 9cefae
 	        (unsigned)uid);
Jan F 9828ff
 }
Jan F 9828ff
+
Jan F 9828ff
+/*
Jan F 003cb0
+ * This will be called on succesfull session key discard
Jan F 9828ff
+ */
Jan F 9cefae
+void
Jan F c2b009
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F 9828ff
+{
Jan F c2b009
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
Jan F c2b009
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
Jan F 9828ff
+}
Jan F 9828ff
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
Jan F 9828ff
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/audit.h.audit4 openssh-5.8p1/audit.h
Jan F f9ff10
--- openssh-5.8p1/audit.h.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/audit.h	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -61,5 +61,7 @@ void	audit_unsupported(int);
Jan F 003cb0
 void	audit_kex(int, char *, char *, char *);
Jan F 003cb0
 void	audit_unsupported_body(int);
Jan F 9cefae
 void	audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
Jan F 003cb0
+void	audit_session_key_free(int ctos);
Jan F c2b009
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
Jan F 92eab1
 
Jan F 92eab1
 #endif /* _SSH_AUDIT_H */
Jan F 003cb0
diff -up openssh-5.8p1/audit-linux.c.audit4 openssh-5.8p1/audit-linux.c
Jan F f9ff10
--- openssh-5.8p1/audit-linux.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/audit-linux.c	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
@@ -285,6 +285,8 @@ audit_unsupported_body(int what)
Jan F 003cb0
 #endif
Jan F 003cb0
 }
Jan F 003cb0
 
Jan F 003cb0
+const static char *direction[] = { "from-server", "from-client", "both" };
Jan F 003cb0
+
Jan F 003cb0
 void
Jan F 9cefae
 audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid,
Jan F 9cefae
 	       uid_t uid)
Jan F f9ff10
@@ -292,7 +294,6 @@ audit_kex_body(int ctos, char *enc, char
Jan F 003cb0
 #ifdef AUDIT_CRYPTO_SESSION
Jan F 003cb0
 	char buf[AUDIT_LOG_SIZE];
Jan F 003cb0
 	int audit_fd, audit_ok;
Jan F 003cb0
-	const static char *direction[] = { "from-server", "from-client", "both" };
Jan F 003cb0
 	Cipher *cipher = cipher_by_name(enc);
Jan F 003cb0
 
Jan F 9cefae
 	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d spid=%jd suid=%jd rport=%d laddr=%s lport=%d",
Jan F f9ff10
@@ -316,4 +317,30 @@ audit_kex_body(int ctos, char *enc, char
Jan F 003cb0
 #endif
Jan F 92eab1
 }
Jan F 92eab1
 
Jan F 92eab1
+void
Jan F c2b009
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F 92eab1
+{
Jan F 92eab1
+	char buf[AUDIT_LOG_SIZE];
Jan F 92eab1
+	int audit_fd, audit_ok;
Jan F 92eab1
+
Jan F c2b009
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d",
Jan F c2b009
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
Jan F c2b009
+		 get_remote_port(),
Jan F 2c1a4a
+		 get_local_ipaddr(packet_get_connection_in()),
Jan F 2c1a4a
+		 get_local_port());
Jan F 92eab1
+	audit_fd = audit_open();
Jan F 92eab1
+	if (audit_fd < 0) {
Jan F 92eab1
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jan F 92eab1
+					 errno != EAFNOSUPPORT)
Jan F 92eab1
+			error("cannot open audit");
Jan F 92eab1
+		return;
Jan F 92eab1
+	}
Jan F 2c1a4a
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jan F 2c1a4a
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
Jan F 92eab1
+	audit_close(audit_fd);
Jan F 92eab1
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jan F 92eab1
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jan F 92eab1
+		error("cannot write into audit");
Jan F 92eab1
+}
Jan F 92eab1
+
Jan F 92eab1
 #endif /* USE_LINUX_AUDIT */
Jan F 003cb0
diff -up openssh-5.8p1/auditstub.c.audit4 openssh-5.8p1/auditstub.c
Jan F f9ff10
--- openssh-5.8p1/auditstub.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/auditstub.c	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -27,6 +27,8 @@
Jan F 1732b0
  * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
Jan F 1732b0
  */
Jan F 1732b0
 
Jan F 1732b0
+#include <sys/types.h>
Jan F 1732b0
+
Jan F 1732b0
 void
Jan F 1732b0
 audit_unsupported(int n)
Jan F 1732b0
 {
Jan F 1732b0
@@ -37,3 +39,12 @@ audit_kex(int ctos, char *enc, char *mac
Jan F 003cb0
 {
Jan F 003cb0
 }
Jan F 003cb0
 
Jan F 003cb0
+void
Jan F 003cb0
+audit_session_key_free(int ctos)
Jan F 003cb0
+{
Jan F 003cb0
+}
Jan F 1732b0
+
Jan F 1732b0
+void
Jan F 1732b0
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F 1732b0
+{
Jan F 1732b0
+}
Jan F 003cb0
diff -up openssh-5.8p1/kex.c.audit4 openssh-5.8p1/kex.c
Jan F f9ff10
--- openssh-5.8p1/kex.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/kex.c	2011-02-24 13:54:02.000000000 +0100
Jan F 003cb0
@@ -624,3 +624,34 @@ dump_digest(char *msg, u_char *digest, i
Jan F f00e4a
 	fprintf(stderr, "\n");
Jan F f00e4a
 }
Jan F f00e4a
 #endif
Jan F f00e4a
+
Jan F f00e4a
+static void
Jan F f00e4a
+enc_destroy(Enc *enc)
Jan F f00e4a
+{
Jan F f00e4a
+	if (enc == NULL)
Jan F f00e4a
+		return;
Jan F f00e4a
+
Jan F f00e4a
+	if (enc->key) {
Jan F f00e4a
+		memset(enc->key, 0, enc->key_len);
Jan F f00e4a
+		xfree(enc->key);
Jan F f00e4a
+	}
Jan F f00e4a
+
Jan F f00e4a
+	if (enc->iv) {
Jan F f00e4a
+		memset(enc->iv,  0, enc->block_size);
Jan F f00e4a
+		xfree(enc->iv);
Jan F f00e4a
+	}
Jan F f00e4a
+
Jan F f00e4a
+	memset(enc, 0, sizeof(*enc));
Jan F f00e4a
+}
Jan F f00e4a
+
Jan F f00e4a
+void
Jan F f00e4a
+newkeys_destroy(Newkeys *newkeys)
Jan F f00e4a
+{
Jan F f00e4a
+	if (newkeys == NULL)
Jan F f00e4a
+		return;
Jan F f00e4a
+
Jan F f00e4a
+	enc_destroy(&newkeys->enc);
Jan F f00e4a
+	mac_destroy(&newkeys->mac);
Jan F f00e4a
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
Jan F f00e4a
+}
Jan F f00e4a
+
Jan F 003cb0
diff -up openssh-5.8p1/kex.h.audit4 openssh-5.8p1/kex.h
Jan F 003cb0
--- openssh-5.8p1/kex.h.audit4	2010-09-24 14:11:14.000000000 +0200
Jan F f9ff10
+++ openssh-5.8p1/kex.h	2011-02-24 13:54:02.000000000 +0100
Jan F 003cb0
@@ -156,6 +156,8 @@ void	 kexgex_server(Kex *);
Jan F 003cb0
 void	 kexecdh_client(Kex *);
Jan F 003cb0
 void	 kexecdh_server(Kex *);
Jan F f00e4a
 
Jan F f00e4a
+void	newkeys_destroy(Newkeys *newkeys);
Jan F f00e4a
+
Jan F f00e4a
 void
Jan F f00e4a
 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
Jan F f00e4a
     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
Jan F 003cb0
diff -up openssh-5.8p1/mac.c.audit4 openssh-5.8p1/mac.c
Jan F 003cb0
--- openssh-5.8p1/mac.c.audit4	2008-06-13 02:58:50.000000000 +0200
Jan F f9ff10
+++ openssh-5.8p1/mac.c	2011-02-24 13:54:02.000000000 +0100
Jan F f32d86
@@ -162,6 +162,20 @@ mac_clear(Mac *mac)
Jan F f00e4a
 	mac->umac_ctx = NULL;
Jan F f00e4a
 }
Jan F f00e4a
 
Jan F f00e4a
+void
Jan F f00e4a
+mac_destroy(Mac *mac)
Jan F f00e4a
+{
Jan F f00e4a
+	if (mac == NULL)
Jan F f00e4a
+		return;
Jan F f00e4a
+
Jan F f00e4a
+	if (mac->key) {
Jan F f00e4a
+		memset(mac->key, 0, mac->key_len);
Jan F f00e4a
+		xfree(mac->key);
Jan F f00e4a
+	}
Jan F f00e4a
+
Jan F f00e4a
+	memset(mac, 0, sizeof(*mac));
Jan F f00e4a
+}
Jan F f00e4a
+
Jan F f00e4a
 /* XXX copied from ciphers_valid */
Jan F f00e4a
 #define	MAC_SEP	","
Jan F f00e4a
 int
Jan F 003cb0
diff -up openssh-5.8p1/mac.h.audit4 openssh-5.8p1/mac.h
Jan F 003cb0
--- openssh-5.8p1/mac.h.audit4	2007-06-11 06:01:42.000000000 +0200
Jan F f9ff10
+++ openssh-5.8p1/mac.h	2011-02-24 13:54:02.000000000 +0100
Jan F f00e4a
@@ -28,3 +28,4 @@ int	 mac_setup(Mac *, char *);
Jan F f00e4a
 int	 mac_init(Mac *);
Jan F f00e4a
 u_char	*mac_compute(Mac *, u_int32_t, u_char *, int);
Jan F f00e4a
 void	 mac_clear(Mac *);
Jan F f00e4a
+void	 mac_destroy(Mac *);
Jan F 003cb0
diff -up openssh-5.8p1/monitor.c.audit4 openssh-5.8p1/monitor.c
Jan F f9ff10
--- openssh-5.8p1/monitor.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/monitor.c	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -181,6 +181,7 @@ int mm_answer_audit_command(int, Buffer 
Jan F 1732b0
 int mm_answer_audit_end_command(int, Buffer *);
Jan F 92eab1
 int mm_answer_audit_unsupported_body(int, Buffer *);
Jan F 92eab1
 int mm_answer_audit_kex_body(int, Buffer *);
Jan F 003cb0
+int mm_answer_audit_session_key_free_body(int, Buffer *);
Jan F 92eab1
 #endif
Jan F 92eab1
 
Jan F 92eab1
 static Authctxt *authctxt;
Jan F 1732b0
@@ -231,6 +232,7 @@ struct mon_table mon_dispatch_proto20[] 
Jan F 003cb0
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 92eab1
 #endif
Jan F 92eab1
 #ifdef BSD_AUTH
Jan F 92eab1
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Jan F 1732b0
@@ -270,6 +272,7 @@ struct mon_table mon_dispatch_postauth20
Jan F 1732b0
     {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 92eab1
 #endif
Jan F 92eab1
     {0, 0, NULL}
Jan F 92eab1
 };
Jan F 1732b0
@@ -303,6 +306,7 @@ struct mon_table mon_dispatch_proto15[] 
Jan F 003cb0
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 92eab1
 #endif
Jan F 92eab1
     {0, 0, NULL}
Jan F 92eab1
 };
Jan F 1732b0
@@ -317,6 +321,7 @@ struct mon_table mon_dispatch_postauth15
Jan F 1732b0
     {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 92eab1
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 92eab1
 #endif
Jan F 92eab1
     {0, 0, NULL}
Jan F 92eab1
 };
Jan F 1732b0
@@ -1783,11 +1788,13 @@ mm_get_keystate(struct monitor *pmonitor
Jan F 1732b0
 
Jan F 1732b0
 	blob = buffer_get_string(&m, &bloblen);
Jan F 1732b0
 	current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Jan F 1732b0
+	memset(blob, 0, bloblen);
Jan F 1732b0
 	xfree(blob);
Jan F 1732b0
 
Jan F 1732b0
 	debug3("%s: Waiting for second key", __func__);
Jan F 1732b0
 	blob = buffer_get_string(&m, &bloblen);
Jan F 1732b0
 	current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Jan F 1732b0
+	memset(blob, 0, bloblen);
Jan F 1732b0
 	xfree(blob);
Jan F 1732b0
 
Jan F 1732b0
 	/* Now get sequence numbers for the packets */
Jan F 1732b0
@@ -1833,6 +1840,16 @@ mm_get_keystate(struct monitor *pmonitor
Jan F 1732b0
 	}
Jan F 1732b0
 
Jan F 1732b0
 	buffer_free(&m);
Jan F 1732b0
+
Jan F 1732b0
+#ifdef SSH_AUDIT_EVENTS
Jan F 1732b0
+	if (compat20) {
Jan F 1732b0
+		buffer_init(&m);
Jan F 1732b0
+		mm_request_receive_expect(pmonitor->m_sendfd,
Jan F 1732b0
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Jan F 1732b0
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
Jan F 1732b0
+		buffer_free(&m);
Jan F 1732b0
+	}
Jan F 1732b0
+#endif
Jan F 1732b0
 }
Jan F 1732b0
 
Jan F 1732b0
 
Jan F 1732b0
@@ -2274,4 +2291,22 @@ mm_answer_audit_kex_body(int sock, Buffe
Jan F 92eab1
 	return 0;
Jan F 92eab1
 }
Jan F 003cb0
 
Jan F 92eab1
+int
Jan F 003cb0
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
Jan F 92eab1
+{
Jan F 003cb0
+	int ctos;
Jan F c2b009
+	pid_t pid;
Jan F c2b009
+	uid_t uid;
Jan F 003cb0
+
Jan F 003cb0
+	ctos = buffer_get_int(m);
Jan F c2b009
+	pid = buffer_get_int64(m);
Jan F c2b009
+	uid = buffer_get_int64(m);
Jan F 003cb0
+
Jan F c2b009
+	audit_session_key_free_body(ctos, pid, uid);
Jan F 92eab1
+
Jan F 92eab1
+	buffer_clear(m);
Jan F 92eab1
+
Jan F 003cb0
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
Jan F 92eab1
+	return 0;
Jan F 92eab1
+}
Jan F 92eab1
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/monitor.h.audit4 openssh-5.8p1/monitor.h
Jan F f9ff10
--- openssh-5.8p1/monitor.h.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/monitor.h	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -69,6 +69,7 @@ enum monitor_reqtype {
Jan F 003cb0
 	MONITOR_REQ_JPAKE_CHECK_CONFIRM, MONITOR_ANS_JPAKE_CHECK_CONFIRM,
Jan F 92eab1
 	MONITOR_REQ_AUDIT_UNSUPPORTED, MONITOR_ANS_AUDIT_UNSUPPORTED,
Jan F 92eab1
 	MONITOR_REQ_AUDIT_KEX, MONITOR_ANS_AUDIT_KEX,
Jan F 003cb0
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
Jan F 92eab1
 };
Jan F 92eab1
 
Jan F 92eab1
 struct mm_master;
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.c.audit4 openssh-5.8p1/monitor_wrap.c
Jan F f9ff10
--- openssh-5.8p1/monitor_wrap.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/monitor_wrap.c	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -601,12 +601,14 @@ mm_send_keystate(struct monitor *monitor
Jan F 1732b0
 		fatal("%s: conversion of newkeys failed", __func__);
Jan F 1732b0
 
Jan F 1732b0
 	buffer_put_string(&m, blob, bloblen);
Jan F 1732b0
+	memset(blob, 0, bloblen);
Jan F 1732b0
 	xfree(blob);
Jan F 1732b0
 
Jan F 1732b0
 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Jan F 1732b0
 		fatal("%s: conversion of newkeys failed", __func__);
Jan F 1732b0
 
Jan F 1732b0
 	buffer_put_string(&m, blob, bloblen);
Jan F 1732b0
+	memset(blob, 0, bloblen);
Jan F 1732b0
 	xfree(blob);
Jan F 1732b0
 
Jan F 1732b0
 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Jan F 1732b0
@@ -1463,4 +1465,19 @@ mm_audit_kex_body(int ctos, char *cipher
Jan F 003cb0
 
Jan F 92eab1
 	buffer_free(&m);
Jan F 92eab1
 }
Jan F 92eab1
+
Jan F 92eab1
+void
Jan F c2b009
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jan F 92eab1
+{
Jan F 92eab1
+	Buffer m;
Jan F 92eab1
+
Jan F 92eab1
+	buffer_init(&m);
Jan F 003cb0
+	buffer_put_int(&m, ctos);
Jan F c2b009
+	buffer_put_int64(&m, pid);
Jan F c2b009
+	buffer_put_int64(&m, uid);
Jan F 003cb0
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Jan F 003cb0
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
Jan F 92eab1
+				  &m);
Jan F 92eab1
+	buffer_free(&m);
Jan F 92eab1
+}
Jan F 92eab1
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.h.audit4 openssh-5.8p1/monitor_wrap.h
Jan F f9ff10
--- openssh-5.8p1/monitor_wrap.h.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/monitor_wrap.h	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -77,6 +77,7 @@ void mm_audit_run_command(const char *);
Jan F 1732b0
 void mm_audit_end_command(const char *);
Jan F 92eab1
 void mm_audit_unsupported_body(int);
Jan F 9cefae
 void mm_audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
Jan F c2b009
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
Jan F 92eab1
 #endif
Jan F 92eab1
 
Jan F 92eab1
 struct Session;
Jan F 003cb0
diff -up openssh-5.8p1/packet.c.audit4 openssh-5.8p1/packet.c
Jan F 003cb0
--- openssh-5.8p1/packet.c.audit4	2010-11-24 00:46:37.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/packet.c	2011-02-24 13:54:02.000000000 +0100
Jan F 1732b0
@@ -60,6 +60,7 @@
Jan F 1732b0
 #include <signal.h>
Jan F 1732b0
 
Jan F 1732b0
 #include "xmalloc.h"
Jan F 1732b0
+#include "audit.h"
Jan F 1732b0
 #include "buffer.h"
Jan F 1732b0
 #include "packet.h"
Jan F 1732b0
 #include "crc32.h"
Jan F 1732b0
@@ -497,6 +498,7 @@ packet_close(void)
Jan F f00e4a
 	}
Jan F f00e4a
 	cipher_cleanup(&active_state->send_context);
Jan F f00e4a
 	cipher_cleanup(&active_state->receive_context);
Jan F f00e4a
+	audit_session_key_free(2);
Jan F f00e4a
 }
Jan F f00e4a
 
Jan F f00e4a
 /* Sets remote side protocol flags. */
Jan F 1732b0
@@ -731,6 +733,23 @@ packet_send1(void)
Jan F 1732b0
 	 */
Jan F 1732b0
 }
Jan F 1732b0
 
Jan F 1732b0
+static void
Jan F 1732b0
+newkeys_destroy_and_free(Newkeys *newkeys)
Jan F 1732b0
+{
Jan F 1732b0
+	if (newkeys == NULL)
Jan F 1732b0
+		return;
Jan F 1732b0
+
Jan F 1732b0
+	xfree(newkeys->enc.name);
Jan F 1732b0
+
Jan F 1732b0
+	mac_clear(&newkeys->mac);
Jan F 1732b0
+	xfree(newkeys->mac.name);
Jan F 1732b0
+
Jan F 1732b0
+	xfree(newkeys->comp.name);
Jan F 1732b0
+
Jan F 1732b0
+	newkeys_destroy(newkeys);
Jan F 1732b0
+	xfree(newkeys);
Jan F 1732b0
+}
Jan F 1732b0
+
Jan F 1732b0
 void
Jan F 1732b0
 set_newkeys(int mode)
Jan F 1732b0
 {
Jan F 1732b0
@@ -756,18 +775,9 @@ set_newkeys(int mode)
Jan F 003cb0
 	}
Jan F 003cb0
 	if (active_state->newkeys[mode] != NULL) {
Jan F 003cb0
 		debug("set_newkeys: rekeying");
Jan F 003cb0
+		audit_session_key_free(mode);
Jan F 003cb0
 		cipher_cleanup(cc);
Jan F 1732b0
-		enc  = &active_state->newkeys[mode]->enc;
Jan F 1732b0
-		mac  = &active_state->newkeys[mode]->mac;
Jan F 1732b0
-		comp = &active_state->newkeys[mode]->comp;
Jan F 1732b0
-		mac_clear(mac);
Jan F 1732b0
-		xfree(enc->name);
Jan F 1732b0
-		xfree(enc->iv);
Jan F 1732b0
-		xfree(enc->key);
Jan F 1732b0
-		xfree(mac->name);
Jan F 1732b0
-		xfree(mac->key);
Jan F 1732b0
-		xfree(comp->name);
Jan F 1732b0
-		xfree(active_state->newkeys[mode]);
Jan F 1732b0
+		newkeys_destroy_and_free(active_state->newkeys[mode]);
Jan F 1732b0
 	}
Jan F 1732b0
 	active_state->newkeys[mode] = kex_get_newkeys(mode);
Jan F 1732b0
 	if (active_state->newkeys[mode] == NULL)
Jan F f9ff10
@@ -1912,6 +1922,54 @@ packet_get_newkeys(int mode)
Jan F 865391
 	return (void *)active_state->newkeys[mode];
Jan F f00e4a
 }
Jan F 865391
 
Jan F f00e4a
+static void
Jan F f00e4a
+packet_destroy_state(struct session_state *state)
Jan F f00e4a
+{
Jan F f00e4a
+	if (state == NULL)
Jan F f00e4a
+		return;
Jan F f00e4a
+
Jan F f00e4a
+	cipher_cleanup(&state->receive_context);
Jan F f00e4a
+	cipher_cleanup(&state->send_context);
Jan F f00e4a
+
Jan F f00e4a
+	buffer_free(&state->input);
Jan F f00e4a
+	buffer_free(&state->output);
Jan F f00e4a
+	buffer_free(&state->outgoing_packet);
Jan F f00e4a
+	buffer_free(&state->incoming_packet);
Jan F f00e4a
+	buffer_free(&state->compression_buffer);
Jan F 1732b0
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
Jan F 1732b0
+	state->newkeys[MODE_IN] = NULL;
Jan F 1732b0
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
Jan F 1732b0
+	state->newkeys[MODE_OUT] = NULL;
Jan F f00e4a
+	mac_destroy(state->packet_discard_mac);
Jan F f00e4a
+//	TAILQ_HEAD(, packet) outgoing;
Jan F ee23b0
+//	memset(state, 0, sizeof(state));
Jan F f00e4a
+}
Jan F f00e4a
+
Jan F 1732b0
+static int
Jan F 1732b0
+packet_state_has_keys (const struct session_state *state)
Jan F 1732b0
+{
Jan F 1732b0
+	return state != NULL &&
Jan F 1732b0
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
Jan F 1732b0
+}
Jan F 1732b0
+
Jan F f00e4a
+void
Jan F f9ff10
+packet_destroy_all(int audit_it, int privsep)
Jan F f00e4a
+{
Jan F f9ff10
+	if (audit_it)
Jan F f9ff10
+		audit_it = packet_state_has_keys (active_state) ||
Jan F f9ff10
+			packet_state_has_keys (backup_state);
Jan F f00e4a
+	packet_destroy_state(active_state);
Jan F f00e4a
+	packet_destroy_state(backup_state);
Jan F 1732b0
+	if (audit_it) {
Jan F 1732b0
+#ifdef SSH_AUDIT_EVENTS
Jan F 1732b0
+		if (privsep)
Jan F 1732b0
+			audit_session_key_free(2);
Jan F 1732b0
+		else
Jan F 1732b0
+			audit_session_key_free_body(2, getpid(), getuid());
Jan F 1732b0
+#endif
Jan F 1732b0
+	}
Jan F f00e4a
+}
Jan F 865391
+
Jan F 865391
 /*
Jan F 865391
  * Save the state for the real connection, and use a separate state when
Jan F 865391
  * resuming a suspended connection.
Jan F f9ff10
@@ -1919,18 +1977,12 @@ packet_get_newkeys(int mode)
Jan F 865391
 void
Jan F 865391
 packet_backup_state(void)
Jan F 865391
 {
Jan F 865391
-	struct session_state *tmp;
Jan F 865391
-
Jan F 865391
 	close(active_state->connection_in);
Jan F 865391
 	active_state->connection_in = -1;
Jan F 865391
 	close(active_state->connection_out);
Jan F 865391
 	active_state->connection_out = -1;
Jan F 865391
-	if (backup_state)
Jan F 865391
-		tmp = backup_state;
Jan F 865391
-	else
Jan F 865391
-		tmp = alloc_session_state();
Jan F 865391
 	backup_state = active_state;
Jan F 865391
-	active_state = tmp;
Jan F 865391
+	active_state = alloc_session_state();
Jan F 865391
 }
Jan F 865391
 
Jan F 865391
 /*
Jan F f9ff10
@@ -1947,9 +1999,7 @@ packet_restore_state(void)
Jan F 865391
 	backup_state = active_state;
Jan F 865391
 	active_state = tmp;
Jan F 865391
 	active_state->connection_in = backup_state->connection_in;
Jan F 865391
-	backup_state->connection_in = -1;
Jan F 865391
 	active_state->connection_out = backup_state->connection_out;
Jan F 865391
-	backup_state->connection_out = -1;
Jan F 865391
 	len = buffer_len(&backup_state->input);
Jan F 865391
 	if (len > 0) {
Jan F 865391
 		buf = buffer_ptr(&backup_state->input);
Jan F f9ff10
@@ -1957,4 +2007,10 @@ packet_restore_state(void)
Jan F 865391
 		buffer_clear(&backup_state->input);
Jan F 865391
 		add_recv_bytes(len);
Jan F 865391
 	}
Jan F 865391
+	backup_state->connection_in = -1;
Jan F 865391
+	backup_state->connection_out = -1;
Jan F 865391
+	packet_destroy_state(backup_state);
Jan F 865391
+	xfree(backup_state);
Jan F 865391
+	backup_state = NULL;
Jan F 865391
 }
Jan F 865391
+
Jan F 003cb0
diff -up openssh-5.8p1/packet.h.audit4 openssh-5.8p1/packet.h
Jan F 003cb0
--- openssh-5.8p1/packet.h.audit4	2010-11-20 05:19:38.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/packet.h	2011-02-24 13:54:02.000000000 +0100
Jan F 003cb0
@@ -125,4 +125,5 @@ void	 packet_restore_state(void);
Jan F f00e4a
 void	*packet_get_input(void);
Jan F f00e4a
 void	*packet_get_output(void);
Jan F f00e4a
 
Jan F f9ff10
+void	 packet_destroy_all(int, int);
Jan F f00e4a
 #endif				/* PACKET_H */
Jan F f9ff10
diff -up openssh-5.8p1/session.c.audit4 openssh-5.8p1/session.c
Jan F f9ff10
--- openssh-5.8p1/session.c.audit4	2011-02-24 13:54:01.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/session.c	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
@@ -1617,6 +1617,9 @@ do_child(Session *s, const char *command
Jan F f9ff10
 
Jan F f9ff10
 	/* remove hostkey from the child's memory */
Jan F f9ff10
 	destroy_sensitive_data();
Jan F f9ff10
+	/* Don't audit this - both us and the parent would be talking to the
Jan F f9ff10
+	   monitor over a single socket, with no synchronization. */
Jan F f9ff10
+	packet_destroy_all(0, 1);
Jan F f9ff10
 
Jan F f9ff10
 	/* Force a password change */
Jan F f9ff10
 	if (s->authctxt->force_pwchange) {
Jan F 003cb0
diff -up openssh-5.8p1/sshd.c.audit4 openssh-5.8p1/sshd.c
Jan F f9ff10
--- openssh-5.8p1/sshd.c.audit4	2011-02-24 13:54:02.000000000 +0100
Jan F f9ff10
+++ openssh-5.8p1/sshd.c	2011-02-24 13:55:09.000000000 +0100
Jan F 003cb0
@@ -663,6 +663,8 @@ privsep_preauth(Authctxt *authctxt)
Jan F f32d86
 	return (0);
Jan F f32d86
 }
Jan F f32d86
 
Jan F f32d86
+extern Newkeys *current_keys[];
Jan F f32d86
+
Jan F f32d86
 static void
Jan F f32d86
 privsep_postauth(Authctxt *authctxt)
Jan F f32d86
 {
Jan F 1732b0
@@ -688,6 +690,9 @@ privsep_postauth(Authctxt *authctxt)
Jan F 6f9316
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
Jan F 6f9316
 		close(pmonitor->m_recvfd);
Jan F 6f9316
 		buffer_clear(&loginmsg);
Jan F f32d86
+ 		newkeys_destroy(current_keys[MODE_OUT]);
Jan F f32d86
+		newkeys_destroy(current_keys[MODE_IN]);
Jan F f9ff10
+		packet_destroy_all(1, 0);
Jan F 6f9316
 		monitor_child_postauth(pmonitor);
Jan F 6f9316
 
Jan F 6f9316
 		/* NEVERREACHED */
Jan F 1732b0
@@ -1974,6 +1979,7 @@ main(int ac, char **av)
Jan F f00e4a
 	 */
Jan F f00e4a
 	if (use_privsep) {
Jan F f00e4a
 		mm_send_keystate(pmonitor);
Jan F f9ff10
+		packet_destroy_all(1, 1);
Jan F f00e4a
 		exit(0);
Jan F f00e4a
 	}
Jan F f00e4a
 
Jan F 1732b0
@@ -2026,6 +2032,8 @@ main(int ac, char **av)
Jan F cfb0f3
 	do_authenticated(authctxt);
Jan F cfb0f3
 
Jan F cfb0f3
 	/* The connection has been terminated. */
Jan F f9ff10
+	packet_destroy_all(1, 0);
Jan F cfb0f3
+
Jan F cfb0f3
 	packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
Jan F cfb0f3
 	packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
Jan F 003cb0
 	verbose("Transferred: sent %llu, received %llu bytes",
Jan F f9ff10
@@ -2345,6 +2353,7 @@ cleanup_exit(int i)
Jan F f9ff10
 {
Jan F f9ff10
 	if (the_authctxt)
Jan F f9ff10
 		do_cleanup(the_authctxt);
Jan F f9ff10
+	packet_destroy_all(1, is_privsep_child);
Jan F f9ff10
 #ifdef SSH_AUDIT_EVENTS
Jan F f9ff10
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
Jan F f9ff10
 	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&