vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jan F 003cb0
diff -up openssh-5.8p1/audit-bsm.c.audit5 openssh-5.8p1/audit-bsm.c
Jan F 1732b0
--- openssh-5.8p1/audit-bsm.c.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/audit-bsm.c	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
@@ -407,4 +407,22 @@ audit_session_key_free_body(int ctos, pi
Jan F 003cb0
 {
Jan F 003cb0
 	/* not implemented */
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+void
Jan F 2c1a4a
+audit_destroy_sensitive_data(const char *fp)
Jan F 003cb0
+{
Jan F 003cb0
+	/* not implemented */
Jan F 003cb0
+}
Jan F c2b009
+
Jan F c2b009
+void
Jan F c2b009
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F c2b009
+{
Jan F c2b009
+	/* not implemented */
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
+void
Jan F c2b009
+audit_generate_ephemeral_server_key(const char *fp)
Jan F c2b009
+{
Jan F c2b009
+	/* not implemented */
Jan F c2b009
+}
Jan F 003cb0
 #endif /* BSM */
Jan F 003cb0
diff -up openssh-5.8p1/audit.c.audit5 openssh-5.8p1/audit.c
Jan F 1732b0
--- openssh-5.8p1/audit.c.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/audit.c	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
@@ -275,5 +275,24 @@ audit_session_key_free_body(int ctos, pi
Jan F 9cefae
 	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
Jan F 9cefae
 		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+/*
Jan F 003cb0
+ * This will be called on destroy private part of the server key
Jan F 003cb0
+ */
Jan F 003cb0
+void
Jan F c2b009
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F 003cb0
+{
Jan F c2b009
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
Jan F c2b009
+		geteuid(), fp, (long)pid, (unsigned)uid);
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
+/*
Jan F c2b009
+ * This will be called on generation of the ephemeral server key
Jan F c2b009
+ */
Jan F c2b009
+void
Jan F c2b009
+audit_generate_ephemeral_server_key(const char *)
Jan F c2b009
+{
Jan F c2b009
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
Jan F 003cb0
+}
Jan F 003cb0
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
Jan F 003cb0
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/audit.h.audit5 openssh-5.8p1/audit.h
Jan F 1732b0
--- openssh-5.8p1/audit.h.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/audit.h	2011-02-23 09:33:38.000000000 +0100
Jan F c2b009
@@ -48,6 +48,8 @@ enum ssh_audit_event_type {
Jan F c2b009
 };
Jan F c2b009
 typedef enum ssh_audit_event_type ssh_audit_event_t;
Jan F c2b009
 
Jan F c2b009
+int	listening_for_clients(void);
Jan F c2b009
+
Jan F c2b009
 void	audit_connection_from(const char *, int);
Jan F c2b009
 void	audit_event(ssh_audit_event_t);
Jan F c2b009
 void	audit_session_open(struct logininfo *);
Jan F 1732b0
@@ -63,5 +65,7 @@ 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 9cefae
 void	audit_session_key_free_body(int ctos, pid_t, uid_t);
Jan F c2b009
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jan F c2b009
+void	audit_generate_ephemeral_server_key(const char *);
Jan F 003cb0
 
Jan F 003cb0
 #endif /* _SSH_AUDIT_H */
Jan F 003cb0
diff -up openssh-5.8p1/audit-linux.c.audit5 openssh-5.8p1/audit-linux.c
Jan F 1732b0
--- openssh-5.8p1/audit-linux.c.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/audit-linux.c	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
@@ -339,4 +339,50 @@ audit_session_key_free_body(int ctos, pi
Jan F 1732b0
 		error("cannot write into audit");
Jan F 1732b0
 }
Jan F 1732b0
 
Jan F c2b009
+void
Jan F c2b009
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F c2b009
+{
Jan F c2b009
+	char buf[AUDIT_LOG_SIZE];
Jan F c2b009
+	int audit_fd, audit_ok;
Jan F c2b009
+
Jan F c2b009
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd",
Jan F c2b009
+		fp, (intmax_t)pid, (intmax_t)uid);
Jan F c2b009
+	audit_fd = audit_open();
Jan F c2b009
+	if (audit_fd < 0) {
Jan F c2b009
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jan F c2b009
+					 errno != EAFNOSUPPORT)
Jan F c2b009
+			error("cannot open audit");
Jan F c2b009
+		return;
Jan F c2b009
+	}
Jan F c2b009
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jan F 1732b0
+			buf, NULL,
Jan F 1732b0
+			listening_for_clients() ? NULL : get_remote_ipaddr(),
Jan F 1732b0
+			NULL, 1);
Jan F 1732b0
+	audit_close(audit_fd);
Jan F 1732b0
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jan F 1732b0
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jan F 1732b0
+		error("cannot write into audit");
Jan F 1732b0
+}
Jan F 1732b0
+
Jan F 003cb0
+void
Jan F c2b009
+audit_generate_ephemeral_server_key(const char *fp)
Jan F 003cb0
+{
Jan F 003cb0
+	char buf[AUDIT_LOG_SIZE];
Jan F 003cb0
+	int audit_fd, audit_ok;
Jan F 003cb0
+
Jan F c2b009
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=?", fp);
Jan F 003cb0
+	audit_fd = audit_open();
Jan F 003cb0
+	if (audit_fd < 0) {
Jan F 003cb0
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jan F 003cb0
+					 errno != EAFNOSUPPORT)
Jan F 003cb0
+			error("cannot open audit");
Jan F 003cb0
+		return;
Jan F 003cb0
+	}
Jan F 2c1a4a
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jan F c2b009
+			buf, NULL, 0, NULL, 1);
Jan F 003cb0
+	audit_close(audit_fd);
Jan F 003cb0
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jan F 003cb0
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jan F 003cb0
+		error("cannot write into audit");
Jan F 003cb0
+}
Jan F 003cb0
 #endif /* USE_LINUX_AUDIT */
Jan F c2b009
diff -up openssh-5.8p1/key.c.audit5 openssh-5.8p1/key.c
Jan F c2b009
--- openssh-5.8p1/key.c.audit5	2011-02-04 01:48:34.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/key.c	2011-02-23 09:33:38.000000000 +0100
Jan F c2b009
@@ -1769,6 +1769,30 @@ key_demote(const Key *k)
Jan F c2b009
 }
Jan F c2b009
 
Jan F c2b009
 int
Jan F c2b009
+key_is_private(const Key *k)
Jan F c2b009
+{
Jan F c2b009
+	switch (k->type) {
Jan F c2b009
+	case KEY_RSA_CERT_V00:
Jan F c2b009
+	case KEY_RSA_CERT:
Jan F c2b009
+	case KEY_RSA1:
Jan F c2b009
+	case KEY_RSA:
Jan F c2b009
+		return k->rsa->d != NULL;
Jan F c2b009
+	case KEY_DSA_CERT_V00:
Jan F c2b009
+	case KEY_DSA_CERT:
Jan F c2b009
+	case KEY_DSA:
Jan F c2b009
+		return k->dsa->priv_key != NULL;
Jan F c2b009
+#ifdef OPENSSL_HAS_ECC
Jan F c2b009
+	case KEY_ECDSA_CERT:
Jan F c2b009
+	case KEY_ECDSA:
Jan F c2b009
+		return EC_KEY_get0_private_key(k->ecdsa) != NULL;
Jan F c2b009
+#endif
Jan F c2b009
+	default:
Jan F c2b009
+		fatal("key_is_private: bad key type %d", k->type);
Jan F c2b009
+		return 1;
Jan F c2b009
+	}
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
+int
Jan F c2b009
 key_is_cert(const Key *k)
Jan F c2b009
 {
Jan F c2b009
 	if (k == NULL)
Jan F c2b009
diff -up openssh-5.8p1/key.h.audit5 openssh-5.8p1/key.h
Jan F c2b009
--- openssh-5.8p1/key.h.audit5	2010-11-05 00:19:49.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/key.h	2011-02-23 09:33:38.000000000 +0100
Jan F c2b009
@@ -106,6 +106,7 @@ Key	*key_generate(int, u_int);
Jan F c2b009
 Key	*key_from_private(const Key *);
Jan F c2b009
 int	 key_type_from_name(char *);
Jan F c2b009
 int	 key_is_cert(const Key *);
Jan F c2b009
+int	 key_is_private(const Key *k);
Jan F c2b009
 int	 key_type_plain(int);
Jan F c2b009
 int	 key_to_certified(Key *, int);
Jan F c2b009
 int	 key_drop_cert(Key *);
Jan F 003cb0
diff -up openssh-5.8p1/monitor.c.audit5 openssh-5.8p1/monitor.c
Jan F 1732b0
--- openssh-5.8p1/monitor.c.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/monitor.c	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
@@ -182,6 +182,7 @@ int mm_answer_audit_end_command(int, Buf
Jan F 003cb0
 int mm_answer_audit_unsupported_body(int, Buffer *);
Jan F 003cb0
 int mm_answer_audit_kex_body(int, Buffer *);
Jan F 003cb0
 int mm_answer_audit_session_key_free_body(int, Buffer *);
Jan F 003cb0
+int mm_answer_audit_server_key_free(int, Buffer *);
Jan F 003cb0
 #endif
Jan F 003cb0
 
Jan F 003cb0
 static Authctxt *authctxt;
Jan F 1732b0
@@ -233,6 +234,7 @@ struct mon_table mon_dispatch_proto20[] 
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {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 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
 #ifdef BSD_AUTH
Jan F 003cb0
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Jan F 1732b0
@@ -273,6 +275,7 @@ struct mon_table mon_dispatch_postauth20
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {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 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
     {0, 0, NULL}
Jan F 003cb0
 };
Jan F 1732b0
@@ -307,6 +310,7 @@ struct mon_table mon_dispatch_proto15[] 
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {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 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
     {0, 0, NULL}
Jan F 003cb0
 };
Jan F 1732b0
@@ -322,6 +326,7 @@ struct mon_table mon_dispatch_postauth15
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {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 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
     {0, 0, NULL}
Jan F 003cb0
 };
Jan F 1732b0
@@ -2309,4 +2314,24 @@ mm_answer_audit_session_key_free_body(in
Jan F 003cb0
 	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
Jan F 003cb0
 	return 0;
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+int
Jan F 003cb0
+mm_answer_audit_server_key_free(int sock, Buffer *m)
Jan F 003cb0
+{
Jan F 2c1a4a
+	int len;
Jan F 2c1a4a
+	char *fp;
Jan F c2b009
+	pid_t pid;
Jan F c2b009
+	uid_t uid;
Jan F 2c1a4a
+
Jan F 2c1a4a
+	fp = buffer_get_string(m, &len;;
Jan F c2b009
+	pid = buffer_get_int64(m);
Jan F c2b009
+	uid = buffer_get_int64(m);
Jan F 2c1a4a
+
Jan F c2b009
+	audit_destroy_sensitive_data(fp, pid, uid);
Jan F 003cb0
+
Jan F 003cb0
+	buffer_clear(m);
Jan F 003cb0
+
Jan F 003cb0
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SERVER_KEY_FREE, m);
Jan F 003cb0
+	return 0;
Jan F 003cb0
+}
Jan F 003cb0
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/monitor.h.audit5 openssh-5.8p1/monitor.h
Jan F 1732b0
--- openssh-5.8p1/monitor.h.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/monitor.h	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
@@ -70,6 +70,7 @@ enum monitor_reqtype {
Jan F 003cb0
 	MONITOR_REQ_AUDIT_UNSUPPORTED, MONITOR_ANS_AUDIT_UNSUPPORTED,
Jan F 003cb0
 	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 003cb0
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
Jan F 003cb0
 };
Jan F 003cb0
 
Jan F 003cb0
 struct mm_master;
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.c.audit5 openssh-5.8p1/monitor_wrap.c
Jan F 1732b0
--- openssh-5.8p1/monitor_wrap.c.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/monitor_wrap.c	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
@@ -1480,4 +1480,20 @@ mm_audit_session_key_free_body(int ctos,
Jan F 003cb0
 				  &m);
Jan F 003cb0
 	buffer_free(&m);
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+void
Jan F c2b009
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F 003cb0
+{
Jan F 003cb0
+	Buffer m;
Jan F 003cb0
+
Jan F 003cb0
+	buffer_init(&m);
Jan F 2c1a4a
+	buffer_put_cstring(&m, fp);
Jan F c2b009
+	buffer_put_int64(&m, pid);
Jan F c2b009
+	buffer_put_int64(&m, uid);
Jan F 2c1a4a
+
Jan F 003cb0
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
Jan F 003cb0
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
Jan F 003cb0
+				  &m);
Jan F 003cb0
+	buffer_free(&m);
Jan F 003cb0
+}
Jan F 003cb0
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.h.audit5 openssh-5.8p1/monitor_wrap.h
Jan F 1732b0
--- openssh-5.8p1/monitor_wrap.h.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/monitor_wrap.h	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
@@ -78,6 +78,7 @@ void mm_audit_end_command(const char *);
Jan F 003cb0
 void mm_audit_unsupported_body(int);
Jan F 9cefae
 void mm_audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
Jan F 9cefae
 void mm_audit_session_key_free_body(int, pid_t, uid_t);
Jan F c2b009
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jan F 003cb0
 #endif
Jan F 003cb0
 
Jan F 003cb0
 struct Session;
Jan F 003cb0
diff -up openssh-5.8p1/session.c.audit5 openssh-5.8p1/session.c
Jan F 1732b0
--- openssh-5.8p1/session.c.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/session.c	2011-02-23 09:33:38.000000000 +0100
Jan F 2c1a4a
@@ -132,7 +132,7 @@ extern int log_stderr;
Jan F 2c1a4a
 extern int debug_flag;
Jan F 2c1a4a
 extern u_int utmp_len;
Jan F 2c1a4a
 extern int startup_pipe;
Jan F 2c1a4a
-extern void destroy_sensitive_data(void);
Jan F 2c1a4a
+extern void destroy_sensitive_data(int);
Jan F 2c1a4a
 extern Buffer loginmsg;
Jan F 2c1a4a
 
Jan F 2c1a4a
 /* original command from peer. */
Jan F 1732b0
@@ -1616,7 +1616,7 @@ do_child(Session *s, const char *command
Jan F 2c1a4a
 	int r = 0;
Jan F 003cb0
 
Jan F 003cb0
 	/* remove hostkey from the child's memory */
Jan F 2c1a4a
-	destroy_sensitive_data();
Jan F 2c1a4a
+	destroy_sensitive_data(1);
Jan F 003cb0
 
Jan F 003cb0
 	/* Force a password change */
Jan F 003cb0
 	if (s->authctxt->force_pwchange) {
Jan F 003cb0
diff -up openssh-5.8p1/sshd.c.audit5 openssh-5.8p1/sshd.c
Jan F 1732b0
--- openssh-5.8p1/sshd.c.audit5	2011-02-23 09:33:38.000000000 +0100
Jan F 1732b0
+++ openssh-5.8p1/sshd.c	2011-02-23 09:33:38.000000000 +0100
Jan F 2c1a4a
@@ -253,7 +253,7 @@ Buffer loginmsg;
Jan F 2c1a4a
 struct passwd *privsep_pw = NULL;
Jan F 2c1a4a
 
Jan F 2c1a4a
 /* Prototypes for various functions defined later in this file. */
Jan F 2c1a4a
-void destroy_sensitive_data(void);
Jan F 2c1a4a
+void destroy_sensitive_data(int);
Jan F 2c1a4a
 void demote_sensitive_data(void);
Jan F 2c1a4a
 
Jan F 2c1a4a
 static void do_ssh1_kex(void);
Jan F c2b009
@@ -272,6 +272,15 @@ close_listen_socks(void)
Jan F c2b009
 	num_listen_socks = -1;
Jan F c2b009
 }
Jan F c2b009
 
Jan F c2b009
+/*
Jan F c2b009
+ * Is this process listening for clients (i.e. not specific to any specific
Jan F c2b009
+ * client connection?)
Jan F c2b009
+ */
Jan F c2b009
+int listening_for_clients(void)
Jan F c2b009
+{
Jan F c2b009
+	return num_listen_socks > 0;
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
 static void
Jan F c2b009
 close_startup_pipes(void)
Jan F c2b009
 {
Jan F c2b009
@@ -532,22 +541,47 @@ sshd_exchange_identification(int sock_in
Jan F c2b009
 	}
Jan F c2b009
 }
Jan F 2c1a4a
 
Jan F c2b009
-/* Destroy the host and server keys.  They will no longer be needed. */
Jan F c2b009
+/*
Jan F c2b009
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
Jan F c2b009
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
Jan F c2b009
+ */
Jan F 2c1a4a
 void
Jan F 2c1a4a
-destroy_sensitive_data(void)
Jan F 2c1a4a
+destroy_sensitive_data(int privsep)
Jan F 2c1a4a
 {
Jan F 2c1a4a
 	int i;
Jan F c2b009
+	pid_t pid;
Jan F c2b009
+	uid_t uid;
Jan F 2c1a4a
 
Jan F c2b009
 	if (sensitive_data.server_key) {
Jan F c2b009
 		key_free(sensitive_data.server_key);
Jan F c2b009
 		sensitive_data.server_key = NULL;
Jan F 2c1a4a
 	}
Jan F c2b009
+	pid = getpid();
Jan F c2b009
+	uid = getuid();
Jan F 2c1a4a
 	for (i = 0; i < options.num_host_key_files; i++) {
Jan F 2c1a4a
 		if (sensitive_data.host_keys[i]) {
Jan F 2c1a4a
+			char *fp;
Jan F 2c1a4a
+
Jan F c2b009
+			if (key_is_private(sensitive_data.host_keys[i]))
Jan F c2b009
+				fp = key_fingerprint(sensitive_data.host_keys[i],
Jan F c2b009
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
Jan F c2b009
+					SSH_FP_HEX);
Jan F c2b009
+			else
Jan F c2b009
+				fp = NULL;
Jan F 2c1a4a
 			key_free(sensitive_data.host_keys[i]);
Jan F 2c1a4a
 			sensitive_data.host_keys[i] = NULL;
Jan F c2b009
+			if (fp != NULL) {
Jan F c2b009
+				if (privsep)
Jan F c2b009
+					PRIVSEP(audit_destroy_sensitive_data(fp,
Jan F c2b009
+						pid, uid));
Jan F c2b009
+				else
Jan F c2b009
+					audit_destroy_sensitive_data(fp,
Jan F c2b009
+						pid, uid);
Jan F c2b009
+				xfree(fp);
Jan F c2b009
+			}
Jan F 2c1a4a
 		}
Jan F c2b009
-		if (sensitive_data.host_certificates[i]) {
Jan F c2b009
+		if (sensitive_data.host_certificates
Jan F c2b009
+		    && sensitive_data.host_certificates[i]) {
Jan F 2c1a4a
 			key_free(sensitive_data.host_certificates[i]);
Jan F c2b009
 			sensitive_data.host_certificates[i] = NULL;
Jan F c2b009
 		}
Jan F c2b009
@@ -561,6 +595,8 @@ void
Jan F c2b009
 demote_sensitive_data(void)
Jan F c2b009
 {
Jan F c2b009
 	Key *tmp;
Jan F c2b009
+	pid_t pid;
Jan F c2b009
+	uid_t uid;
Jan F c2b009
 	int i;
Jan F 2c1a4a
 
Jan F c2b009
 	if (sensitive_data.server_key) {
Jan F c2b009
@@ -569,13 +605,27 @@ demote_sensitive_data(void)
Jan F c2b009
 		sensitive_data.server_key = tmp;
Jan F c2b009
 	}
Jan F c2b009
 
Jan F c2b009
+	pid = getpid();
Jan F c2b009
+	uid = getuid();
Jan F 2c1a4a
 	for (i = 0; i < options.num_host_key_files; i++) {
Jan F 2c1a4a
 		if (sensitive_data.host_keys[i]) {
Jan F 2c1a4a
+			char *fp;
Jan F 2c1a4a
+
Jan F c2b009
+			if (key_is_private(sensitive_data.host_keys[i]))
Jan F c2b009
+				fp = key_fingerprint(sensitive_data.host_keys[i],
Jan F c2b009
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
Jan F c2b009
+					SSH_FP_HEX);
Jan F c2b009
+			else
Jan F c2b009
+				fp = NULL;
Jan F 2c1a4a
 			tmp = key_demote(sensitive_data.host_keys[i]);
Jan F 2c1a4a
 			key_free(sensitive_data.host_keys[i]);
Jan F 2c1a4a
 			sensitive_data.host_keys[i] = tmp;
Jan F 2c1a4a
 			if (tmp->type == KEY_RSA1)
Jan F 2c1a4a
 				sensitive_data.ssh1_host_key = tmp;
Jan F c2b009
+			if (fp != NULL) {
Jan F c2b009
+				audit_destroy_sensitive_data(fp, pid, uid);
Jan F c2b009
+				xfree(fp);
Jan F c2b009
+			}
Jan F 003cb0
 		}
Jan F 003cb0
 		/* Certs do not need demotion */
Jan F 003cb0
 	}
Jan F 1732b0
@@ -1119,6 +1169,7 @@ server_accept_loop(int *sock_in, int *so
Jan F c2b009
 		if (received_sigterm) {
Jan F c2b009
 			logit("Received signal %d; terminating.",
Jan F c2b009
 			    (int) received_sigterm);
Jan F c2b009
+			destroy_sensitive_data(0);
Jan F c2b009
 			close_listen_socks();
Jan F c2b009
 			unlink(options.pid_file);
Jan F c2b009
 			exit(255);
Jan F 1732b0
@@ -2022,7 +2073,7 @@ main(int ac, char **av)
Jan F 003cb0
 		privsep_postauth(authctxt);
Jan F 003cb0
 		/* the monitor process [priv] will not return */
Jan F 2c1a4a
 		if (!compat20)
Jan F 2c1a4a
-			destroy_sensitive_data();
Jan F 2c1a4a
+			destroy_sensitive_data(0);
Jan F 003cb0
 	}
Jan F 003cb0
 
Jan F 003cb0
 	packet_set_timeout(options.client_alive_interval,
Jan F 1732b0
@@ -2261,7 +2312,7 @@ do_ssh1_kex(void)
Jan F 2c1a4a
 			session_id[i] = session_key[i] ^ session_key[i + 16];
Jan F 003cb0
 	}
Jan F 003cb0
 	/* Destroy the private and public keys. No longer. */
Jan F 2c1a4a
-	destroy_sensitive_data();
Jan F 2c1a4a
+	destroy_sensitive_data(0);
Jan F 003cb0
 
Jan F 003cb0
 	if (use_privsep)
Jan F 003cb0
 		mm_ssh1_session_id(session_id);
Jan F 1732b0
@@ -2351,8 +2402,23 @@ do_ssh2_kex(void)
Jan F 1732b0
 void
Jan F 1732b0
 cleanup_exit(int i)
Jan F c2b009
 {
Jan F 1732b0
+	static int in_cleanup;
Jan F 1732b0
+
Jan F 1732b0
+	int is_privsep_child;
Jan F 1732b0
+
Jan F 1732b0
+	/* cleanup_exit can be called at the very least from the privsep
Jan F 1732b0
+	   wrappers used for auditing.  Make sure we don't recurse
Jan F 1732b0
+	   indefinitely. */
Jan F 1732b0
+	if (in_cleanup)
Jan F 1732b0
+		_exit(i);
Jan F 1732b0
+	in_cleanup = 1;
Jan F 1732b0
+
Jan F c2b009
 	if (the_authctxt)
Jan F c2b009
 		do_cleanup(the_authctxt);
Jan F 1732b0
+	is_privsep_child = use_privsep && pmonitor != NULL && !mm_is_monitor();
Jan F c2b009
+	if (sensitive_data.host_keys != NULL)
Jan F 1732b0
+		destroy_sensitive_data(is_privsep_child);
Jan F 1732b0
+	packet_destroy_all(is_privsep_child);
Jan F c2b009
 #ifdef SSH_AUDIT_EVENTS
Jan F c2b009
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
Jan F c2b009
 	if (!use_privsep || mm_is_monitor())