vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Blob Blame History Raw
diff -up openssh-5.6p1/audit-bsm.c.audit5 openssh-5.6p1/audit-bsm.c
--- openssh-5.6p1/audit-bsm.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/audit-bsm.c	2011-02-07 18:53:53.000000000 +0100
@@ -401,4 +401,10 @@ audit_session_key_free_body(int ctos)
 {
 	/* not implemented */
 }
+
+void
+audit_destroy_sensitive_data(void)
+{
+	/* not implemented */
+}
 #endif /* BSM */
diff -up openssh-5.6p1/audit.c.audit5 openssh-5.6p1/audit.c
--- openssh-5.6p1/audit.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/audit.c	2011-02-07 18:53:53.000000000 +0100
@@ -268,5 +268,14 @@ audit_session_key_free_body(int ctos)
 {
 	debug("audit session key discard euid %d direction %d", geteuid(), ctos);
 }
+
+/*
+ * This will be called on destroy private part of the server key
+ */
+void
+audit_destroy_sensitive_data(void)
+{
+	debug("audit destroy sensitive data euid %d", geteuid());
+}
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
 #endif /* SSH_AUDIT_EVENTS */
diff -up openssh-5.6p1/audit.h.audit5 openssh-5.6p1/audit.h
--- openssh-5.6p1/audit.h.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/audit.h	2011-02-07 18:53:53.000000000 +0100
@@ -62,5 +62,6 @@ void	audit_unsupported_body(int);
 void	audit_kex_body(int, char *, char *, char *);
 void	audit_session_key_free(int ctos);
 void	audit_session_key_free_body(int ctos);
+void	audit_destroy_sensitive_data(void);
 
 #endif /* _SSH_AUDIT_H */
diff -up openssh-5.6p1/audit-linux.c.audit5 openssh-5.6p1/audit-linux.c
--- openssh-5.6p1/audit-linux.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/audit-linux.c	2011-02-07 18:53:53.000000000 +0100
@@ -226,4 +226,26 @@ audit_session_key_free_body(int ctos)
 		error("cannot write into audit");
 }
 
+void
+audit_destroy_sensitive_data(void)
+{
+	char buf[AUDIT_LOG_SIZE];
+	int audit_fd, audit_ok;
+
+	snprintf(buf, sizeof(buf), "destroy kind=server direction=?");
+	audit_fd = audit_open();
+	if (audit_fd < 0) {
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
+					 errno != EAFNOSUPPORT)
+			error("cannot open audit");
+		return;
+	}
+	audit_ok = audit_log_acct_message(audit_fd, AUDIT_CRYPTO_KEY_USER, NULL,
+			buf, NULL, -1, NULL, get_remote_ipaddr(), NULL, 1);
+	audit_close(audit_fd);
+	/* do not abort if the error is EPERM and sshd is run as non root user */
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
+		error("cannot write into audit");
+}
+
 #endif /* USE_LINUX_AUDIT */
diff -up openssh-5.6p1/kex.c.audit5 openssh-5.6p1/kex.c
--- openssh-5.6p1/kex.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/kex.c	2011-02-07 18:53:53.000000000 +0100
@@ -592,3 +592,34 @@ dump_digest(char *msg, u_char *digest, i
 	fprintf(stderr, "\n");
 }
 #endif
+
+static void
+enc_destroy(Enc *enc)
+{
+	if (enc == NULL)
+		return;
+
+	if (enc->key) {
+		memset(enc->key, 0, enc->key_len);
+		xfree(enc->key);
+	}
+
+	if (enc->iv) {
+		memset(enc->iv,  0, enc->block_size);
+		xfree(enc->iv);
+	}
+
+	memset(enc, 0, sizeof(*enc));
+}
+
+void
+newkeys_destroy(Newkeys *newkeys)
+{
+	if (newkeys == NULL)
+		return;
+
+	enc_destroy(&newkeys->enc);
+	mac_destroy(&newkeys->mac);
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
+}
+
diff -up openssh-5.6p1/kex.h.audit5 openssh-5.6p1/kex.h
--- openssh-5.6p1/kex.h.audit5	2010-02-26 21:55:05.000000000 +0100
+++ openssh-5.6p1/kex.h	2011-02-07 18:53:53.000000000 +0100
@@ -146,6 +146,8 @@ void	 kexdh_server(Kex *);
 void	 kexgex_client(Kex *);
 void	 kexgex_server(Kex *);
 
+void	newkeys_destroy(Newkeys *newkeys);
+
 void
 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
diff -up openssh-5.6p1/mac.c.audit5 openssh-5.6p1/mac.c
--- openssh-5.6p1/mac.c.audit5	2008-06-13 02:58:50.000000000 +0200
+++ openssh-5.6p1/mac.c	2011-02-07 18:53:53.000000000 +0100
@@ -162,6 +162,20 @@ mac_clear(Mac *mac)
 	mac->umac_ctx = NULL;
 }
 
+void
+mac_destroy(Mac *mac)
+{
+	if (mac == NULL)
+		return;
+
+	if (mac->key) {
+		memset(mac->key, 0, mac->key_len);
+		xfree(mac->key);
+	}
+
+	memset(mac, 0, sizeof(*mac));
+}
+
 /* XXX copied from ciphers_valid */
 #define	MAC_SEP	","
 int
diff -up openssh-5.6p1/mac.h.audit5 openssh-5.6p1/mac.h
--- openssh-5.6p1/mac.h.audit5	2007-06-11 06:01:42.000000000 +0200
+++ openssh-5.6p1/mac.h	2011-02-07 18:53:53.000000000 +0100
@@ -28,3 +28,4 @@ int	 mac_setup(Mac *, char *);
 int	 mac_init(Mac *);
 u_char	*mac_compute(Mac *, u_int32_t, u_char *, int);
 void	 mac_clear(Mac *);
+void	 mac_destroy(Mac *);
diff -up openssh-5.6p1/monitor.c.audit5 openssh-5.6p1/monitor.c
--- openssh-5.6p1/monitor.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/monitor.c	2011-02-07 18:53:53.000000000 +0100
@@ -181,6 +181,7 @@ int mm_answer_audit_command(int, Buffer 
 int mm_answer_audit_unsupported_body(int, Buffer *);
 int mm_answer_audit_kex_body(int, Buffer *);
 int mm_answer_audit_session_key_free_body(int, Buffer *);
+int mm_answer_audit_server_key_free(int, Buffer *);
 #endif
 
 static Authctxt *authctxt;
@@ -232,6 +233,7 @@ struct mon_table mon_dispatch_proto20[] 
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
 #endif
 #ifdef BSD_AUTH
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
@@ -271,6 +273,7 @@ struct mon_table mon_dispatch_postauth20
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
 #endif
     {0, 0, NULL}
 };
@@ -305,6 +308,7 @@ struct mon_table mon_dispatch_proto15[] 
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
 #endif
     {0, 0, NULL}
 };
@@ -319,6 +323,7 @@ struct mon_table mon_dispatch_postauth15
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
 #endif
     {0, 0, NULL}
 };
@@ -2271,4 +2276,15 @@ mm_answer_audit_session_key_free_body(in
 	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
 	return 0;
 }
+
+int
+mm_answer_audit_server_key_free(int sock, Buffer *m)
+{
+	audit_destroy_sensitive_data();
+
+	buffer_clear(m);
+
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SERVER_KEY_FREE, m);
+	return 0;
+}
 #endif /* SSH_AUDIT_EVENTS */
diff -up openssh-5.6p1/monitor.h.audit5 openssh-5.6p1/monitor.h
--- openssh-5.6p1/monitor.h.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/monitor.h	2011-02-07 18:53:53.000000000 +0100
@@ -69,6 +69,7 @@ enum monitor_reqtype {
 	MONITOR_REQ_AUDIT_UNSUPPORTED, MONITOR_ANS_AUDIT_UNSUPPORTED,
 	MONITOR_REQ_AUDIT_KEX, MONITOR_ANS_AUDIT_KEX,
 	MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
 };
 
 struct mm_master;
diff -up openssh-5.6p1/monitor_wrap.c.audit5 openssh-5.6p1/monitor_wrap.c
--- openssh-5.6p1/monitor_wrap.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/monitor_wrap.c	2011-02-07 18:53:53.000000000 +0100
@@ -1458,4 +1458,16 @@ mm_audit_session_key_free_body(int ctos)
 				  &m);
 	buffer_free(&m);
 }
+
+void
+mm_audit_destroy_sensitive_data(void)
+{
+	Buffer m;
+
+	buffer_init(&m);
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
+				  &m);
+	buffer_free(&m);
+}
 #endif /* SSH_AUDIT_EVENTS */
diff -up openssh-5.6p1/monitor_wrap.h.audit5 openssh-5.6p1/monitor_wrap.h
--- openssh-5.6p1/monitor_wrap.h.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/monitor_wrap.h	2011-02-07 18:53:53.000000000 +0100
@@ -77,6 +77,7 @@ void mm_audit_run_command(const char *);
 void mm_audit_unsupported_body(int);
 void mm_audit_kex_body(int, char *, char *, char *);
 void mm_audit_session_key_free_body(int);
+void mm_audit_server_key_free_body(void);
 #endif
 
 struct Session;
diff -up openssh-5.6p1/packet.c.audit5 openssh-5.6p1/packet.c
--- openssh-5.6p1/packet.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/packet.c	2011-02-07 18:53:54.000000000 +0100
@@ -60,6 +60,7 @@
 #include <signal.h>
 
 #include "xmalloc.h"
+#include "audit.h"
 #include "buffer.h"
 #include "packet.h"
 #include "crc32.h"
@@ -495,9 +496,9 @@ packet_close(void)
 		buffer_free(&active_state->compression_buffer);
 		buffer_compress_uninit();
 	}
-	audit_session_key_free(2);
 	cipher_cleanup(&active_state->send_context);
 	cipher_cleanup(&active_state->receive_context);
+	audit_session_key_free(2);
 }
 
 /* Sets remote side protocol flags. */
@@ -1893,6 +1894,34 @@ packet_get_newkeys(int mode)
 	return (void *)active_state->newkeys[mode];
 }
 
+static void
+packet_destroy_state(struct session_state *state)
+{
+	if (state == NULL)
+		return;
+
+	cipher_cleanup(&state->receive_context);
+	cipher_cleanup(&state->send_context);
+
+	buffer_free(&state->input);
+	buffer_free(&state->output);
+	buffer_free(&state->outgoing_packet);
+	buffer_free(&state->incoming_packet);
+	buffer_free(&state->compression_buffer);
+	newkeys_destroy(state->newkeys[MODE_IN]);
+	newkeys_destroy(state->newkeys[MODE_OUT]);
+	mac_destroy(state->packet_discard_mac);
+//	TAILQ_HEAD(, packet) outgoing;
+//	memset(state, 0, sizeof(state));
+}
+
+void
+packet_destroy_all(void)
+{
+	packet_destroy_state(active_state);
+	packet_destroy_state(backup_state);
+}
+
 /*
  * Save the state for the real connection, and use a separate state when
  * resuming a suspended connection.
@@ -1900,18 +1929,12 @@ packet_get_newkeys(int mode)
 void
 packet_backup_state(void)
 {
-	struct session_state *tmp;
-
 	close(active_state->connection_in);
 	active_state->connection_in = -1;
 	close(active_state->connection_out);
 	active_state->connection_out = -1;
-	if (backup_state)
-		tmp = backup_state;
-	else
-		tmp = alloc_session_state();
 	backup_state = active_state;
-	active_state = tmp;
+	active_state = alloc_session_state();
 }
 
 /*
@@ -1928,9 +1951,7 @@ packet_restore_state(void)
 	backup_state = active_state;
 	active_state = tmp;
 	active_state->connection_in = backup_state->connection_in;
-	backup_state->connection_in = -1;
 	active_state->connection_out = backup_state->connection_out;
-	backup_state->connection_out = -1;
 	len = buffer_len(&backup_state->input);
 	if (len > 0) {
 		buf = buffer_ptr(&backup_state->input);
@@ -1938,4 +1959,10 @@ packet_restore_state(void)
 		buffer_clear(&backup_state->input);
 		add_recv_bytes(len);
 	}
+	backup_state->connection_in = -1;
+	backup_state->connection_out = -1;
+	packet_destroy_state(backup_state);
+	xfree(backup_state);
+	backup_state = NULL;
 }
+
diff -up openssh-5.6p1/packet.h.audit5 openssh-5.6p1/packet.h
--- openssh-5.6p1/packet.h.audit5	2009-07-05 23:11:13.000000000 +0200
+++ openssh-5.6p1/packet.h	2011-02-07 18:53:54.000000000 +0100
@@ -115,4 +115,5 @@ void	 packet_restore_state(void);
 void	*packet_get_input(void);
 void	*packet_get_output(void);
 
+void	 packet_destroy_all(void);
 #endif				/* PACKET_H */
diff -up openssh-5.6p1/session.c.audit5 openssh-5.6p1/session.c
--- openssh-5.6p1/session.c.audit5	2010-06-26 02:00:15.000000000 +0200
+++ openssh-5.6p1/session.c	2011-02-07 18:53:54.000000000 +0100
@@ -1677,6 +1677,7 @@ do_child(Session *s, const char *command
 
 	/* remove hostkey from the child's memory */
 	destroy_sensitive_data();
+	PRIVSEP(audit_destroy_sensitive_data());
 
 	/* Force a password change */
 	if (s->authctxt->force_pwchange) {
diff -up openssh-5.6p1/sshd.c.audit5 openssh-5.6p1/sshd.c
--- openssh-5.6p1/sshd.c.audit5	2011-02-07 18:53:53.000000000 +0100
+++ openssh-5.6p1/sshd.c	2011-02-07 19:08:56.000000000 +0100
@@ -579,6 +579,7 @@ demote_sensitive_data(void)
 		}
 		/* Certs do not need demotion */
 	}
+	audit_destroy_sensitive_data();
 
 	/* We do not clear ssh1_host key and cookie.  XXX - Okay Niels? */
 }
@@ -663,6 +664,8 @@ privsep_preauth(Authctxt *authctxt)
 	return (0);
 }
 
+extern Newkeys *current_keys[];
+
 static void
 privsep_postauth(Authctxt *authctxt)
 {
@@ -688,6 +691,10 @@ privsep_postauth(Authctxt *authctxt)
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
 		close(pmonitor->m_recvfd);
 		buffer_clear(&loginmsg);
+ 		newkeys_destroy(current_keys[MODE_OUT]);
+		newkeys_destroy(current_keys[MODE_IN]);
+		packet_destroy_all();
+		audit_session_key_free_body(2);
 		monitor_child_postauth(pmonitor);
 
 		/* NEVERREACHED */
@@ -1970,6 +1977,8 @@ main(int ac, char **av)
 	 */
 	if (use_privsep) {
 		mm_send_keystate(pmonitor);
+		packet_destroy_all();
+		audit_session_key_free(2);
 		exit(0);
 	}
 
@@ -2011,8 +2020,10 @@ main(int ac, char **av)
 	if (use_privsep) {
 		privsep_postauth(authctxt);
 		/* the monitor process [priv] will not return */
-		if (!compat20)
+		if (!compat20) {
 			destroy_sensitive_data();
+			audit_destroy_sensitive_data();
+		}
 	}
 
 	packet_set_timeout(options.client_alive_interval,
@@ -2022,6 +2033,9 @@ main(int ac, char **av)
 	do_authenticated(authctxt);
 
 	/* The connection has been terminated. */
+	packet_destroy_all();
+	audit_session_key_free(2);
+
 	packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
 	packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
 	verbose("Transferred: sent %llu, received %llu bytes", obytes, ibytes);
@@ -2249,6 +2263,7 @@ do_ssh1_kex(void)
 	}
 	/* Destroy the private and public keys. No longer. */
 	destroy_sensitive_data();
+	audit_destroy_sensitive_data();
 
 	if (use_privsep)
 		mm_ssh1_session_id(session_id);