vishalmishra434 / rpms / openssh

Forked from rpms/openssh 3 months ago
Clone
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/audit-bsm.c.audit5 openssh-5.9p0/audit-bsm.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/audit-bsm.c.audit5	2011-09-03 19:36:19.596148796 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/audit-bsm.c	2011-09-03 19:36:22.335036037 +0200
Jan F. Chadima 69dd72
@@ -414,4 +414,22 @@ audit_session_key_free_body(int ctos, pi
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_destroy_sensitive_data(const char *fp)
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_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
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_generate_ephemeral_server_key(const char *fp)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	/* not implemented */
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
 #endif /* BSM */
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/audit-linux.c.audit5 openssh-5.9p0/audit-linux.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/audit-linux.c.audit5	2011-09-03 19:36:19.693075842 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/audit-linux.c	2011-09-03 19:36:22.783167781 +0200
Jan F. Chadima 69dd72
@@ -350,4 +350,50 @@ audit_session_key_free_body(int ctos, pi
Jan F. Chadima 69dd72
 		error("cannot write into audit");
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_destroy_sensitive_data(const char *fp, 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=server fp=%s direction=? spid=%jd suid=%jd ",
Jan F. Chadima 69dd72
+		fp, (intmax_t)pid, (intmax_t)uid);
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,
Jan F. Chadima 69dd72
+			listening_for_clients() ? NULL : get_remote_ipaddr(),
Jan F. Chadima 69dd72
+			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
+void
Jan F. Chadima 69dd72
+audit_generate_ephemeral_server_key(const char *fp)
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=create kind=server fp=%s direction=? ", fp);
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, 0, 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
 #endif /* USE_LINUX_AUDIT */
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/audit.c.audit5 openssh-5.9p0/audit.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/audit.c.audit5	2011-09-03 19:36:19.798026930 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/audit.c	2011-09-03 19:36:22.894025519 +0200
Jan F. Chadima 69dd72
@@ -290,5 +290,24 @@ audit_session_key_free_body(int ctos, pi
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
+
Jan F. Chadima 69dd72
+/*
Jan F. Chadima 69dd72
+ * This will be called on destroy private part of the server key
Jan F. Chadima 69dd72
+ */
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
Jan F. Chadima 69dd72
+		geteuid(), fp, (long)pid, (unsigned)uid);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+/*
Jan F. Chadima 69dd72
+ * This will be called on generation of the ephemeral server key
Jan F. Chadima 69dd72
+ */
Jan F. Chadima 69dd72
+void
Jan F. Chadima 69dd72
+audit_generate_ephemeral_server_key(const char *)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
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 69dd72
diff -up openssh-5.9p0/audit.h.audit5 openssh-5.9p0/audit.h
Jan F. Chadima 69dd72
--- openssh-5.9p0/audit.h.audit5	2011-09-03 19:36:20.080151641 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/audit.h	2011-09-03 19:36:23.002024714 +0200
Jan F. Chadima 69dd72
@@ -48,6 +48,8 @@ enum ssh_audit_event_type {
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
 typedef enum ssh_audit_event_type ssh_audit_event_t;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+int	listening_for_clients(void);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 void	audit_connection_from(const char *, int);
Jan F. Chadima 69dd72
 void	audit_event(ssh_audit_event_t);
Jan F. Chadima 69dd72
 void	audit_count_session_open(void);
Jan F. Chadima 69dd72
@@ -64,5 +66,7 @@ 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
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jan F. Chadima 69dd72
+void	audit_generate_ephemeral_server_key(const char *);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #endif /* _SSH_AUDIT_H */
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/key.c.audit5 openssh-5.9p0/key.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/key.c.audit5	2011-09-03 19:36:11.788093495 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/key.c	2011-09-03 19:36:23.114023593 +0200
Jan F. Chadima 69dd72
@@ -1797,6 +1797,30 @@ key_demote(const Key *k)
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 int
Jan F. Chadima 69dd72
+key_is_private(const Key *k)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	switch (k->type) {
Jan F. Chadima 69dd72
+	case KEY_RSA_CERT_V00:
Jan F. Chadima 69dd72
+	case KEY_RSA_CERT:
Jan F. Chadima 69dd72
+	case KEY_RSA1:
Jan F. Chadima 69dd72
+	case KEY_RSA:
Jan F. Chadima 69dd72
+		return k->rsa->d != NULL;
Jan F. Chadima 69dd72
+	case KEY_DSA_CERT_V00:
Jan F. Chadima 69dd72
+	case KEY_DSA_CERT:
Jan F. Chadima 69dd72
+	case KEY_DSA:
Jan F. Chadima 69dd72
+		return k->dsa->priv_key != NULL;
Jan F. Chadima 69dd72
+#ifdef OPENSSL_HAS_ECC
Jan F. Chadima 69dd72
+	case KEY_ECDSA_CERT:
Jan F. Chadima 69dd72
+	case KEY_ECDSA:
Jan F. Chadima 69dd72
+		return EC_KEY_get0_private_key(k->ecdsa) != NULL;
Jan F. Chadima 69dd72
+#endif
Jan F. Chadima 69dd72
+	default:
Jan F. Chadima 69dd72
+		fatal("key_is_private: bad key type %d", k->type);
Jan F. Chadima 69dd72
+		return 1;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+int
Jan F. Chadima 69dd72
 key_is_cert(const Key *k)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	if (k == NULL)
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/key.h.audit5 openssh-5.9p0/key.h
Jan F. Chadima 69dd72
--- openssh-5.9p0/key.h.audit5	2011-09-03 19:36:11.873026290 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/key.h	2011-09-03 19:36:23.209025893 +0200
Jan F. Chadima 69dd72
@@ -109,6 +109,7 @@ Key	*key_generate(int, u_int);
Jan F. Chadima 69dd72
 Key	*key_from_private(const Key *);
Jan F. Chadima 69dd72
 int	 key_type_from_name(char *);
Jan F. Chadima 69dd72
 int	 key_is_cert(const Key *);
Jan F. Chadima 69dd72
+int	 key_is_private(const Key *k);
Jan F. Chadima 69dd72
 int	 key_type_plain(int);
Jan F. Chadima 69dd72
 int	 key_to_certified(Key *, int);
Jan F. Chadima 69dd72
 int	 key_drop_cert(Key *);
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/monitor.c.audit5 openssh-5.9p0/monitor.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/monitor.c.audit5	2011-09-03 19:36:20.953025745 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/monitor.c	2011-09-03 19:36:23.356034851 +0200
Jan F. Chadima 69dd72
@@ -114,6 +114,8 @@ extern Buffer auth_debug;
Jan F. Chadima 69dd72
 extern int auth_debug_init;
Jan F. Chadima 69dd72
 extern Buffer loginmsg;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+extern void destroy_sensitive_data(int);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 /* State exported from the child */
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 struct {
Jan F. Chadima 69dd72
@@ -191,6 +193,7 @@ int mm_answer_audit_end_command(int, Buf
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
+int mm_answer_audit_server_key_free(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
@@ -244,6 +247,7 @@ struct mon_table mon_dispatch_proto20[]
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
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
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
@@ -285,6 +289,7 @@ struct mon_table mon_dispatch_postauth20
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
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
     {0, 0, NULL}
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
@@ -319,6 +324,7 @@ struct mon_table mon_dispatch_proto15[]
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
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
     {0, 0, NULL}
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
@@ -334,6 +340,7 @@ struct mon_table mon_dispatch_postauth15
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
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
     {0, 0, NULL}
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
@@ -1722,6 +1729,8 @@ mm_answer_term(int sock, Buffer *req)
Jan F. Chadima 69dd72
 		sshpam_cleanup();
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+	destroy_sensitive_data(0);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Jan F. Chadima 69dd72
 		if (errno != EINTR)
Jan F. Chadima 69dd72
 			exit(1);
Jan F. Chadima 69dd72
@@ -2466,4 +2475,24 @@ mm_answer_audit_session_key_free_body(in
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
+
Jan F. Chadima 69dd72
+int
Jan F. Chadima 69dd72
+mm_answer_audit_server_key_free(int sock, Buffer *m)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	int len;
Jan F. Chadima 69dd72
+	char *fp;
Jan F. Chadima 69dd72
+	pid_t pid;
Jan F. Chadima 69dd72
+	uid_t uid;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	fp = buffer_get_string(m, &len;;
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_destroy_sensitive_data(fp, 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_SERVER_KEY_FREE, m);
Jan F. Chadima 69dd72
+	return 0;
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
 #endif /* SSH_AUDIT_EVENTS */
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/monitor.h.audit5 openssh-5.9p0/monitor.h
Jan F. Chadima 69dd72
--- openssh-5.9p0/monitor.h.audit5	2011-09-03 19:36:21.000000000 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/monitor.h	2011-09-03 19:36:54.529052350 +0200
Jan F. Chadima 69dd72
@@ -64,6 +64,7 @@ enum monitor_reqtype {
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_AUDIT_SERVER_KEY_FREE, MONITOR_ANS_AUDIT_SERVER_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 69dd72
diff -up openssh-5.9p0/monitor_wrap.c.audit5 openssh-5.9p0/monitor_wrap.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/monitor_wrap.c.audit5	2011-09-03 19:36:21.212025456 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/monitor_wrap.c	2011-09-03 19:36:23.714087175 +0200
Jan F. Chadima 69dd72
@@ -1559,4 +1559,20 @@ mm_audit_session_key_free_body(int ctos,
Jan F. Chadima 69dd72
 				  &m);
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_destroy_sensitive_data(const char *fp, 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_cstring(&m, fp);
Jan F. Chadima 69dd72
+	buffer_put_int64(&m, pid);
Jan F. Chadima 69dd72
+	buffer_put_int64(&m, uid);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
Jan F. Chadima 69dd72
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SERVER_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 69dd72
diff -up openssh-5.9p0/monitor_wrap.h.audit5 openssh-5.9p0/monitor_wrap.h
Jan F. Chadima 69dd72
--- openssh-5.9p0/monitor_wrap.h.audit5	2011-09-03 19:36:21.316023957 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/monitor_wrap.h	2011-09-03 19:36:23.814151174 +0200
Jan F. Chadima 69dd72
@@ -81,6 +81,7 @@ void mm_audit_end_command(int, const cha
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
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 struct Session;
Jan F. Chadima 69dd72
diff -up openssh-5.9p0/session.c.audit5 openssh-5.9p0/session.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/session.c.audit5	2011-09-03 19:36:22.072087382 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/session.c	2011-09-03 19:36:23.985151473 +0200
Jan F. Chadima 69dd72
@@ -136,7 +136,7 @@ extern int log_stderr;
Jan F. Chadima 69dd72
 extern int debug_flag;
Jan F. Chadima 69dd72
 extern u_int utmp_len;
Jan F. Chadima 69dd72
 extern int startup_pipe;
Jan F. Chadima 69dd72
-extern void destroy_sensitive_data(void);
Jan F. Chadima 69dd72
+extern void destroy_sensitive_data(int);
Jan F. Chadima 69dd72
 extern Buffer loginmsg;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 /* original command from peer. */
Jan F. Chadima 69dd72
@@ -1633,7 +1633,7 @@ do_child(Session *s, const char *command
Jan F. Chadima 69dd72
 	int r = 0;
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
+	destroy_sensitive_data(1);
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
diff -up openssh-5.9p0/sshd.c.audit5 openssh-5.9p0/sshd.c
Jan F. Chadima 69dd72
--- openssh-5.9p0/sshd.c.audit5	2011-09-03 19:36:22.196087195 +0200
Jan F. Chadima 69dd72
+++ openssh-5.9p0/sshd.c	2011-09-03 19:36:24.126148826 +0200
Jan F. Chadima 69dd72
@@ -254,7 +254,7 @@ Buffer loginmsg;
Jan F. Chadima 69dd72
 struct passwd *privsep_pw = NULL;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 /* Prototypes for various functions defined later in this file. */
Jan F. Chadima 69dd72
-void destroy_sensitive_data(void);
Jan F. Chadima 69dd72
+void destroy_sensitive_data(int);
Jan F. Chadima 69dd72
 void demote_sensitive_data(void);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 static void do_ssh1_kex(void);
Jan F. Chadima 69dd72
@@ -273,6 +273,15 @@ close_listen_socks(void)
Jan F. Chadima 69dd72
 	num_listen_socks = -1;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+/*
Jan F. Chadima 69dd72
+ * Is this process listening for clients (i.e. not specific to any specific
Jan F. Chadima 69dd72
+ * client connection?)
Jan F. Chadima 69dd72
+ */
Jan F. Chadima 69dd72
+int listening_for_clients(void)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	return num_listen_socks > 0;
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 static void
Jan F. Chadima 69dd72
 close_startup_pipes(void)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
@@ -533,22 +542,47 @@ sshd_exchange_identification(int sock_in
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-/* Destroy the host and server keys.  They will no longer be needed. */
Jan F. Chadima 69dd72
+/*
Jan F. Chadima 69dd72
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
Jan F. Chadima 69dd72
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
Jan F. Chadima 69dd72
+ */
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
-destroy_sensitive_data(void)
Jan F. Chadima 69dd72
+destroy_sensitive_data(int privsep)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	int i;
Jan F. Chadima 69dd72
+	pid_t pid;
Jan F. Chadima 69dd72
+	uid_t uid;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	if (sensitive_data.server_key) {
Jan F. Chadima 69dd72
 		key_free(sensitive_data.server_key);
Jan F. Chadima 69dd72
 		sensitive_data.server_key = NULL;
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
+	pid = getpid();
Jan F. Chadima 69dd72
+	uid = getuid();
Jan F. Chadima 69dd72
 	for (i = 0; i < options.num_host_key_files; i++) {
Jan F. Chadima 69dd72
 		if (sensitive_data.host_keys[i]) {
Jan F. Chadima 69dd72
+			char *fp;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+			if (key_is_private(sensitive_data.host_keys[i]))
Jan F. Chadima 69dd72
+				fp = key_fingerprint(sensitive_data.host_keys[i],
Jan F. Chadima 69dd72
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
Jan F. Chadima 69dd72
+					SSH_FP_HEX);
Jan F. Chadima 69dd72
+			else
Jan F. Chadima 69dd72
+				fp = NULL;
Jan F. Chadima 69dd72
 			key_free(sensitive_data.host_keys[i]);
Jan F. Chadima 69dd72
 			sensitive_data.host_keys[i] = NULL;
Jan F. Chadima 69dd72
+			if (fp != NULL) {
Jan F. Chadima 69dd72
+				if (privsep)
Jan F. Chadima 69dd72
+					PRIVSEP(audit_destroy_sensitive_data(fp,
Jan F. Chadima 69dd72
+						pid, uid));
Jan F. Chadima 69dd72
+				else
Jan F. Chadima 69dd72
+					audit_destroy_sensitive_data(fp,
Jan F. Chadima 69dd72
+						pid, uid);
Jan F. Chadima 69dd72
+				xfree(fp);
Jan F. Chadima 69dd72
+			}
Jan F. Chadima 69dd72
 		}
Jan F. Chadima 69dd72
-		if (sensitive_data.host_certificates[i]) {
Jan F. Chadima 69dd72
+		if (sensitive_data.host_certificates
Jan F. Chadima 69dd72
+		    && sensitive_data.host_certificates[i]) {
Jan F. Chadima 69dd72
 			key_free(sensitive_data.host_certificates[i]);
Jan F. Chadima 69dd72
 			sensitive_data.host_certificates[i] = NULL;
Jan F. Chadima 69dd72
 		}
Jan F. Chadima 69dd72
@@ -562,6 +596,8 @@ void
Jan F. Chadima 69dd72
 demote_sensitive_data(void)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	Key *tmp;
Jan F. Chadima 69dd72
+	pid_t pid;
Jan F. Chadima 69dd72
+	uid_t uid;
Jan F. Chadima 69dd72
 	int i;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	if (sensitive_data.server_key) {
Jan F. Chadima 69dd72
@@ -570,13 +606,27 @@ demote_sensitive_data(void)
Jan F. Chadima 69dd72
 		sensitive_data.server_key = tmp;
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+	pid = getpid();
Jan F. Chadima 69dd72
+	uid = getuid();
Jan F. Chadima 69dd72
 	for (i = 0; i < options.num_host_key_files; i++) {
Jan F. Chadima 69dd72
 		if (sensitive_data.host_keys[i]) {
Jan F. Chadima 69dd72
+			char *fp;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+			if (key_is_private(sensitive_data.host_keys[i]))
Jan F. Chadima 69dd72
+				fp = key_fingerprint(sensitive_data.host_keys[i],
Jan F. Chadima 69dd72
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
Jan F. Chadima 69dd72
+					SSH_FP_HEX);
Jan F. Chadima 69dd72
+			else
Jan F. Chadima 69dd72
+				fp = NULL;
Jan F. Chadima 69dd72
 			tmp = key_demote(sensitive_data.host_keys[i]);
Jan F. Chadima 69dd72
 			key_free(sensitive_data.host_keys[i]);
Jan F. Chadima 69dd72
 			sensitive_data.host_keys[i] = tmp;
Jan F. Chadima 69dd72
 			if (tmp->type == KEY_RSA1)
Jan F. Chadima 69dd72
 				sensitive_data.ssh1_host_key = tmp;
Jan F. Chadima 69dd72
+			if (fp != NULL) {
Jan F. Chadima 69dd72
+				audit_destroy_sensitive_data(fp, pid, uid);
Jan F. Chadima 69dd72
+				xfree(fp);
Jan F. Chadima 69dd72
+			}
Jan F. Chadima 69dd72
 		}
Jan F. Chadima 69dd72
 		/* Certs do not need demotion */
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
@@ -1143,6 +1193,7 @@ server_accept_loop(int *sock_in, int *so
Jan F. Chadima 69dd72
 		if (received_sigterm) {
Jan F. Chadima 69dd72
 			logit("Received signal %d; terminating.",
Jan F. Chadima 69dd72
 			    (int) received_sigterm);
Jan F. Chadima 69dd72
+			destroy_sensitive_data(0);
Jan F. Chadima 69dd72
 			close_listen_socks();
Jan F. Chadima 69dd72
 			unlink(options.pid_file);
Jan F. Chadima 69dd72
 			exit(received_sigterm == SIGTERM ? 0 : 255);
Jan F. Chadima 69dd72
@@ -2045,7 +2096,7 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 		privsep_postauth(authctxt);
Jan F. Chadima 69dd72
 		/* the monitor process [priv] will not return */
Jan F. Chadima 69dd72
 		if (!compat20)
Jan F. Chadima 69dd72
-			destroy_sensitive_data();
Jan F. Chadima 69dd72
+			destroy_sensitive_data(0);
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	packet_set_timeout(options.client_alive_interval,
Jan F. Chadima 69dd72
@@ -2056,6 +2107,7 @@ main(int ac, char **av)
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
+	destroy_sensitive_data(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
@@ -2284,7 +2336,7 @@ do_ssh1_kex(void)
Jan F. Chadima 69dd72
 			session_id[i] = session_key[i] ^ session_key[i + 16];
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 	/* Destroy the private and public keys. No longer. */
Jan F. Chadima 69dd72
-	destroy_sensitive_data();
Jan F. Chadima 69dd72
+	destroy_sensitive_data(0);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	if (use_privsep)
Jan F. Chadima 69dd72
 		mm_ssh1_session_id(session_id);
Jan F. Chadima 69dd72
@@ -2374,8 +2426,22 @@ do_ssh2_kex(void)
Jan F. Chadima 69dd72
 void
Jan F. Chadima 69dd72
 cleanup_exit(int i)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
+	static int in_cleanup;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	int is_privsep_child;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	/* cleanup_exit can be called at the very least from the privsep
Jan F. Chadima 69dd72
+	   wrappers used for auditing.  Make sure we don't recurse
Jan F. Chadima 69dd72
+	   indefinitely. */
Jan F. Chadima 69dd72
+	if (in_cleanup)
Jan F. Chadima 69dd72
+		_exit(i);
Jan F. Chadima 69dd72
+	in_cleanup = 1;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	if (the_authctxt)
Jan F. Chadima 69dd72
 		do_cleanup(the_authctxt);
Jan F. Chadima 69dd72
+	is_privsep_child = use_privsep && pmonitor != NULL && !mm_is_monitor();
Jan F. Chadima 69dd72
+	if (sensitive_data.host_keys != NULL)
Jan F. Chadima 69dd72
+		destroy_sensitive_data(is_privsep_child);
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' */