vishalmishra434 / rpms / openssh

Forked from rpms/openssh 4 months ago
Clone
f5835d
diff -up openssh-7.6p1/audit-bsm.c.audit openssh-7.6p1/audit-bsm.c
f5835d
--- openssh-7.6p1/audit-bsm.c.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/audit-bsm.c	2017-10-04 17:18:32.834505048 +0200
f5835d
@@ -373,10 +373,23 @@ audit_connection_from(const char *host,
f5835d
 #endif
f5835d
 }
f5835d
 
f5835d
-void
f5835d
+int
f5835d
 audit_run_command(const char *command)
f5835d
 {
f5835d
 	/* not implemented */
f5835d
+	return 0;
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_end_command(int handle, const char *command)
f5835d
+{
f5835d
+	/* not implemented */
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_count_session_open(void)
f5835d
+{
f5835d
+	/* not necessary */
f5835d
 }
f5835d
 
f5835d
 void
f5835d
@@ -391,6 +404,12 @@ audit_session_close(struct logininfo *li
f5835d
 	/* not implemented */
f5835d
 }
f5835d
 
f5835d
+int
f5835d
+audit_keyusage(int host_user, char *fp, int rv)
f5835d
+{
f5835d
+	/* not implemented */
f5835d
+}
f5835d
+
f5835d
 void
f5835d
 audit_event(ssh_audit_event_t event)
f5835d
 {
f5835d
@@ -452,4 +471,34 @@ audit_event(ssh_audit_event_t event)
f5835d
 		debug("%s: unhandled event %d", __func__, event);
f5835d
 	}
f5835d
 }
f5835d
+
f5835d
+void
f5835d
+audit_unsupported_body(int what)
f5835d
+{
f5835d
+	/* not implemented */
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	/* not implemented */
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	/* not implemented */
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_destroy_sensitive_data(const char *fp)
f5835d
+{
f5835d
+	/* not implemented */
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	/* not implemented */
f5835d
+}
f5835d
 #endif /* BSM */
f5835d
diff -up openssh-7.6p1/audit.c.audit openssh-7.6p1/audit.c
f5835d
--- openssh-7.6p1/audit.c.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/audit.c	2017-10-04 17:18:32.834505048 +0200
f5835d
@@ -34,6 +35,12 @@
f5835d
 #include "log.h"
f5835d
 #include "hostfile.h"
f5835d
 #include "auth.h"
f5835d
+#include "ssh-gss.h"
f5835d
+#include "monitor_wrap.h"
f5835d
+#include "xmalloc.h"
f5835d
+#include "misc.h"
f5835d
+#include "servconf.h"
f5835d
+#include "ssherr.h"
f5835d
 
f5835d
 /*
f5835d
  * Care must be taken when using this since it WILL NOT be initialized when
f5835d
@@ -41,6 +47,7 @@
f5835d
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
f5835d
  */
f5835d
 extern Authctxt *the_authctxt;
f5835d
+extern ServerOptions options;
f5835d
 
f5835d
 /* Maybe add the audit class to struct Authmethod? */
f5835d
 ssh_audit_event_t
f5835d
@@ -69,13 +76,10 @@ audit_classify_auth(const char *method)
f5835d
 const char *
f5835d
 audit_username(void)
f5835d
 {
f5835d
-	static const char unknownuser[] = "(unknown user)";
f5835d
-	static const char invaliduser[] = "(invalid user)";
f5835d
+	static const char unknownuser[] = "(unknown)";
f5835d
 
f5835d
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
f5835d
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
f5835d
 		return (unknownuser);
f5835d
-	if (!the_authctxt->valid)
f5835d
-		return (invaliduser);
f5835d
 	return (the_authctxt->user);
f5835d
 }
f5835d
 
f5835d
@@ -109,6 +113,35 @@ audit_event_lookup(ssh_audit_event_t ev)
f5835d
 	return(event_lookup[i].name);
f5835d
 }
f5835d
 
f5835d
+void
f5835d
+audit_key(int host_user, int *rv, const struct sshkey *key)
f5835d
+{
f5835d
+	char *fp;
f5835d
+
f5835d
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
f5835d
+	if (audit_keyusage(host_user, fp, (*rv == 0)) == 0)
f5835d
+		*rv = -SSH_ERR_INTERNAL_ERROR;
f5835d
+	free(fp);
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_unsupported(int what)
f5835d
+{
f5835d
+	PRIVSEP(audit_unsupported_body(what));
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
f5835d
+{
f5835d
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, pfs, getpid(), getuid()));
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_session_key_free(int ctos)
f5835d
+{
f5835d
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
f5835d
+}
f5835d
+
f5835d
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
f5835d
 /*
f5835d
  * Null implementations of audit functions.
f5835d
@@ -138,6 +173,17 @@ audit_event(ssh_audit_event_t event)
f5835d
 }
f5835d
 
f5835d
 /*
f5835d
+ * Called when a child process has called, or will soon call,
f5835d
+ * audit_session_open.
f5835d
+ */
f5835d
+void
f5835d
+audit_count_session_open(void)
f5835d
+{
f5835d
+	debug("audit count session open euid %d user %s", geteuid(),
f5835d
+	      audit_username());
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
  * Called when a user session is started.  Argument is the tty allocated to
f5835d
  * the session, or NULL if no tty was allocated.
f5835d
  *
f5835d
@@ -172,13 +218,82 @@ audit_session_close(struct logininfo *li
f5835d
 /*
f5835d
  * This will be called when a user runs a non-interactive command.  Note that
f5835d
  * it may be called multiple times for a single connection since SSH2 allows
f5835d
- * multiple sessions within a single connection.
f5835d
+ * multiple sessions within a single connection.  Returns a "handle" for
f5835d
+ * audit_end_command.
f5835d
  */
f5835d
-void
f5835d
+int
f5835d
 audit_run_command(const char *command)
f5835d
 {
f5835d
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
f5835d
 	    audit_username(), command);
f5835d
+	return 0;
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * This will be called when the non-interactive command finishes.  Note that
f5835d
+ * it may be called multiple times for a single connection since SSH2 allows
f5835d
+ * multiple sessions within a single connection.  "handle" should come from
f5835d
+ * the corresponding audit_run_command.
f5835d
+ */
f5835d
+void
f5835d
+audit_end_command(int handle, const char *command)
f5835d
+{
f5835d
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
f5835d
+	    audit_username(), command);
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
f5835d
+ *
f5835d
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
f5835d
+ */
f5835d
+int
f5835d
+audit_keyusage(int host_user, char *fp, int rv)
f5835d
+{
f5835d
+	debug("audit %s key usage euid %d user %s fingerprint %s, result %d",
f5835d
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(),
f5835d
+		fp, rv);
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * This will be called when the protocol negotiation fails.
f5835d
+ */
f5835d
+void
f5835d
+audit_unsupported_body(int what)
f5835d
+{
f5835d
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * This will be called on succesfull protocol negotiation.
f5835d
+ */
f5835d
+void
f5835d
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
f5835d
+	       uid_t uid)
f5835d
+{
f5835d
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
f5835d
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
f5835d
+	        (unsigned)uid);
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * This will be called on succesfull session key discard
f5835d
+ */
f5835d
+void
f5835d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
f5835d
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * This will be called on destroy private part of the server key
f5835d
+ */
f5835d
+void
f5835d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
f5835d
+		geteuid(), fp, (long)pid, (unsigned)uid);
f5835d
 }
f5835d
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
f5835d
 #endif /* SSH_AUDIT_EVENTS */
f5835d
diff -up openssh-7.6p1/audit.h.audit openssh-7.6p1/audit.h
f5835d
--- openssh-7.6p1/audit.h.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/audit.h	2017-10-04 17:18:32.834505048 +0200
f5835d
@@ -26,6 +26,7 @@
f5835d
 # define _SSH_AUDIT_H
f5835d
 
f5835d
 #include "loginrec.h"
f5835d
+#include "sshkey.h"
f5835d
 
f5835d
 enum ssh_audit_event_type {
f5835d
 	SSH_LOGIN_EXCEED_MAXTRIES,
f5835d
@@ -43,13 +44,32 @@ enum ssh_audit_event_type {
f5835d
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
f5835d
 	SSH_AUDIT_UNKNOWN
f5835d
 };
f5835d
+
f5835d
+enum ssh_audit_kex {
f5835d
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
f5835d
+	SSH_AUDIT_UNSUPPORTED_MAC,
f5835d
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
f5835d
+};
f5835d
 typedef enum ssh_audit_event_type ssh_audit_event_t;
f5835d
 
f5835d
+int	listening_for_clients(void);
f5835d
+
f5835d
 void	audit_connection_from(const char *, int);
f5835d
 void	audit_event(ssh_audit_event_t);
f5835d
+void	audit_count_session_open(void);
f5835d
 void	audit_session_open(struct logininfo *);
f5835d
 void	audit_session_close(struct logininfo *);
f5835d
-void	audit_run_command(const char *);
f5835d
+int	audit_run_command(const char *);
f5835d
+void 	audit_end_command(int, const char *);
f5835d
 ssh_audit_event_t audit_classify_auth(const char *);
f5835d
+int	audit_keyusage(int, char *, int);
f5835d
+void	audit_key(int, int *, const struct sshkey *);
f5835d
+void	audit_unsupported(int);
f5835d
+void	audit_kex(int, char *, char *, char *, char *);
f5835d
+void	audit_unsupported_body(int);
f5835d
+void	audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
f5835d
+void	audit_session_key_free(int ctos);
f5835d
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
f5835d
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
f5835d
 
f5835d
 #endif /* _SSH_AUDIT_H */
f5835d
diff -up openssh-7.6p1/audit-linux.c.audit openssh-7.6p1/audit-linux.c
f5835d
--- openssh-7.6p1/audit-linux.c.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/audit-linux.c	2017-10-04 17:18:32.835505053 +0200
f5835d
@@ -33,27 +33,40 @@
f5835d
 
f5835d
 #include "log.h"
f5835d
 #include "audit.h"
f5835d
+#include "sshkey.h"
f5835d
+#include "hostfile.h"
f5835d
+#include "auth.h"
f5835d
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
f5835d
+#include "servconf.h"
f5835d
 #include "canohost.h"
f5835d
 #include "packet.h"
f5835d
-
f5835d
+#include "cipher.h"
f5835d
+#include "channels.h"
f5835d
+#include "session.h"
f5835d
+
f5835d
+#define AUDIT_LOG_SIZE 256
f5835d
+
f5835d
+extern ServerOptions options;
f5835d
+extern Authctxt *the_authctxt;
f5835d
+extern u_int utmp_len;
f5835d
 const char *audit_username(void);
f5835d
 
f5835d
-int
f5835d
-linux_audit_record_event(int uid, const char *username, const char *hostname,
f5835d
-    const char *ip, const char *ttyn, int success)
f5835d
+static void
f5835d
+linux_audit_user_logxxx(int uid, const char *username,
f5835d
+    const char *ip, const char *ttyn, int success, int event)
f5835d
 {
f5835d
 	int audit_fd, rc, saved_errno;
f5835d
 
f5835d
 	if ((audit_fd = audit_open()) < 0) {
f5835d
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f5835d
 		    errno == EAFNOSUPPORT)
f5835d
-			return 1; /* No audit support in kernel */
f5835d
+			return; /* No audit support in kernel */
f5835d
 		else
f5835d
-			return 0; /* Must prevent login */
f5835d
+			goto fatal_report; /* Must prevent login */
f5835d
 	}
f5835d
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
f5835d
+	rc = audit_log_acct_message(audit_fd, event,
f5835d
 	    NULL, "login", username ? username : "(unknown)",
f5835d
-	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
f5835d
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
f5835d
 	saved_errno = errno;
f5835d
 	close(audit_fd);
f5835d
 
f5835d
@@ -65,9 +78,96 @@ linux_audit_record_event(int uid, const
f5835d
 		rc = 0;
f5835d
 	errno = saved_errno;
f5835d
 
f5835d
-	return rc >= 0;
f5835d
+	if (rc < 0) {
f5835d
+fatal_report:
f5835d
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f5835d
+	}
f5835d
+}
f5835d
+
f5835d
+static void
f5835d
+linux_audit_user_auth(int uid, const char *username,
f5835d
+    const char *ip, const char *ttyn, int success, int event)
f5835d
+{
f5835d
+	int audit_fd, rc, saved_errno;
f5835d
+	static const char *event_name[] = {
f5835d
+		"maxtries exceeded",
f5835d
+		"root denied",
f5835d
+		"success",
f5835d
+		"none",
f5835d
+		"password",
f5835d
+		"challenge-response",
f5835d
+		"pubkey",
f5835d
+		"hostbased",
f5835d
+		"gssapi",
f5835d
+		"invalid user",
f5835d
+		"nologin",
f5835d
+		"connection closed",
f5835d
+		"connection abandoned",
f5835d
+		"unknown"
f5835d
+	};
f5835d
+
f5835d
+	audit_fd = audit_open();
f5835d
+	if (audit_fd < 0) {
f5835d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f5835d
+		    errno == EAFNOSUPPORT)
f5835d
+			return; /* No audit support in kernel */
f5835d
+		else
f5835d
+			goto fatal_report; /* Must prevent login */
f5835d
+	}
f5835d
+
f5835d
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
f5835d
+		event = SSH_AUDIT_UNKNOWN;
f5835d
+
f5835d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
f5835d
+	    NULL, event_name[event], username ? username : "(unknown)",
f5835d
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
f5835d
+	saved_errno = errno;
f5835d
+	close(audit_fd);
f5835d
+	/*
f5835d
+	 * Do not report error if the error is EPERM and sshd is run as non
f5835d
+	 * root user.
f5835d
+	 */
f5835d
+	if ((rc == -EPERM) && (geteuid() != 0))
f5835d
+		rc = 0;
f5835d
+	errno = saved_errno;
f5835d
+	if (rc < 0) {
f5835d
+fatal_report:
f5835d
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f5835d
+	}
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+audit_keyusage(int host_user, char *fp, int rv)
f5835d
+{
f5835d
+	char buf[AUDIT_LOG_SIZE];
f5835d
+	int audit_fd, rc, saved_errno;
f5835d
+
f5835d
+	audit_fd = audit_open();
f5835d
+	if (audit_fd < 0) {
f5835d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f5835d
+					 errno == EAFNOSUPPORT)
f5835d
+			return 1; /* No audit support in kernel */
f5835d
+		else
f5835d
+			return 0; /* Must prevent login */
f5835d
+	}
f5835d
+	snprintf(buf, sizeof(buf), "%s_auth grantors=auth-key", host_user ? "pubkey" : "hostbased");
f5835d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
f5835d
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
f5835d
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
f5835d
+		goto out;
f5835d
+	snprintf(buf, sizeof(buf), "op=negotiate kind=auth-key fp=%s", fp);
f5835d
+	rc = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, buf, NULL,
f5835d
+		ssh_remote_ipaddr(active_state), NULL, rv);
f5835d
+out:
f5835d
+	saved_errno = errno;
f5835d
+	audit_close(audit_fd);
f5835d
+	errno = saved_errno;
f5835d
+	/* do not report error if the error is EPERM and sshd is run as non root user */
f5835d
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
f5835d
 }
f5835d
 
f5835d
+static int user_login_count = 0;
f5835d
+
f5835d
 /* Below is the sshd audit API code */
f5835d
 
f5835d
 void
f5835d
@@ -76,24 +177,55 @@ audit_connection_from(const char *host,
f5835d
 	/* not implemented */
f5835d
 }
f5835d
 
f5835d
-void
f5835d
+int
f5835d
 audit_run_command(const char *command)
f5835d
 {
f5835d
-	/* not implemented */
f5835d
+	if (!user_login_count++)
f5835d
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f5835d
+		    ssh_remote_ipaddr(active_state),
f5835d
+		    "ssh", 1, AUDIT_USER_LOGIN);
f5835d
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f5835d
+	    ssh_remote_ipaddr(active_state),
f5835d
+	    "ssh", 1, AUDIT_USER_START);
f5835d
+	return 0;
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_end_command(int handle, const char *command)
f5835d
+{
f5835d
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f5835d
+	    ssh_remote_ipaddr(active_state),
f5835d
+	    "ssh", 1, AUDIT_USER_END);
f5835d
+	if (user_login_count && !--user_login_count)
f5835d
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f5835d
+		    ssh_remote_ipaddr(active_state),
f5835d
+		    "ssh", 1, AUDIT_USER_LOGOUT);
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_count_session_open(void)
f5835d
+{
f5835d
+	user_login_count++;
f5835d
 }
f5835d
 
f5835d
 void
f5835d
 audit_session_open(struct logininfo *li)
f5835d
 {
f5835d
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
f5835d
-	    li->line, 1) == 0)
f5835d
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f5835d
+	if (!user_login_count++)
f5835d
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f5835d
+		    li->line, 1, AUDIT_USER_LOGIN);
f5835d
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f5835d
+	    li->line, 1, AUDIT_USER_START);
f5835d
 }
f5835d
 
f5835d
 void
f5835d
 audit_session_close(struct logininfo *li)
f5835d
 {
f5835d
-	/* not implemented */
f5835d
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f5835d
+	    li->line, 1, AUDIT_USER_END);
f5835d
+	if (user_login_count && !--user_login_count)
f5835d
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f5835d
+		    li->line, 1, AUDIT_USER_LOGOUT);
f5835d
 }
f5835d
 
f5835d
 void
f5835d
@@ -102,25 +231,155 @@ audit_event(ssh_audit_event_t event)
f5835d
 	struct ssh *ssh = active_state; /* XXX */
f5835d
 
f5835d
 	switch(event) {
f5835d
-	case SSH_AUTH_SUCCESS:
f5835d
-	case SSH_CONNECTION_CLOSE:
f5835d
 	case SSH_NOLOGIN:
f5835d
-	case SSH_LOGIN_EXCEED_MAXTRIES:
f5835d
 	case SSH_LOGIN_ROOT_DENIED:
f5835d
+		linux_audit_user_auth(-1, audit_username(),
f5835d
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
f5835d
+		linux_audit_user_logxxx(-1, audit_username(),
f5835d
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
f5835d
 		break;
f5835d
-	case SSH_AUTH_FAIL_NONE:
f5835d
 	case SSH_AUTH_FAIL_PASSWD:
f5835d
+		if (options.use_pam)
f5835d
+			break;
f5835d
+	case SSH_LOGIN_EXCEED_MAXTRIES:
f5835d
 	case SSH_AUTH_FAIL_KBDINT:
f5835d
 	case SSH_AUTH_FAIL_PUBKEY:
f5835d
 	case SSH_AUTH_FAIL_HOSTBASED:
f5835d
 	case SSH_AUTH_FAIL_GSSAPI:
f5835d
+		linux_audit_user_auth(-1, audit_username(),
f5835d
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
f5835d
+		break;
f5835d
+
f5835d
+	case SSH_CONNECTION_CLOSE:
f5835d
+		if (user_login_count) {
f5835d
+			while (user_login_count--)
f5835d
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f5835d
+				    ssh_remote_ipaddr(ssh),
f5835d
+				    "ssh", 1, AUDIT_USER_END);
f5835d
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f5835d
+			    ssh_remote_ipaddr(ssh),
f5835d
+			    "ssh", 1, AUDIT_USER_LOGOUT);
f5835d
+		}
f5835d
+		break;
f5835d
+
f5835d
+	case SSH_CONNECTION_ABANDON:
f5835d
 	case SSH_INVALID_USER:
f5835d
-		linux_audit_record_event(-1, audit_username(), NULL,
f5835d
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
f5835d
+		linux_audit_user_logxxx(-1, audit_username(),
f5835d
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
f5835d
 		break;
f5835d
 	default:
f5835d
 		debug("%s: unhandled event %d", __func__, event);
f5835d
 		break;
f5835d
 	}
f5835d
 }
f5835d
+
f5835d
+void
f5835d
+audit_unsupported_body(int what)
f5835d
+{
f5835d
+#ifdef AUDIT_CRYPTO_SESSION
f5835d
+	char buf[AUDIT_LOG_SIZE];
f5835d
+	const static char *name[] = { "cipher", "mac", "comp" };
f5835d
+	char *s;
f5835d
+	int audit_fd;
f5835d
+
f5835d
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
f5835d
+		name[what], ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())),
f5835d
+		ssh_local_port(active_state));
f5835d
+	free(s);
f5835d
+	audit_fd = audit_open();
f5835d
+	if (audit_fd < 0)
f5835d
+		/* no problem, the next instruction will be fatal() */
f5835d
+		return;
f5835d
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
f5835d
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 0);
f5835d
+	audit_close(audit_fd);
f5835d
+#endif
f5835d
+}
f5835d
+
f5835d
+const static char *direction[] = { "from-server", "from-client", "both" };
f5835d
+
f5835d
+void
f5835d
+audit_kex_body(int ctos, char *enc, char *mac, char *compress,
f5835d
+    char *pfs, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+#ifdef AUDIT_CRYPTO_SESSION
f5835d
+	char buf[AUDIT_LOG_SIZE];
f5835d
+	int audit_fd, audit_ok;
f5835d
+	const struct sshcipher *cipher = cipher_by_name(enc);
f5835d
+	char *s;
f5835d
+
f5835d
+	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s pfs=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
f5835d
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
f5835d
+		(intmax_t)pid, (intmax_t)uid,
f5835d
+		ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())), ssh_local_port(active_state));
f5835d
+	free(s);
f5835d
+	audit_fd = audit_open();
f5835d
+	if (audit_fd < 0) {
f5835d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f5835d
+					 errno == EAFNOSUPPORT)
f5835d
+			return; /* No audit support in kernel */
f5835d
+		else
f5835d
+			fatal("cannot open audit"); /* Must prevent login */
f5835d
+	}
f5835d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
f5835d
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
f5835d
+	audit_close(audit_fd);
f5835d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f5835d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f5835d
+		fatal("cannot write into audit"); /* Must prevent login */
f5835d
+#endif
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	char buf[AUDIT_LOG_SIZE];
f5835d
+	int audit_fd, audit_ok;
f5835d
+	char *s;
f5835d
+
f5835d
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
f5835d
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
f5835d
+		 ssh_remote_port(active_state),
f5835d
+		 (s = get_local_ipaddr(packet_get_connection_in())),
f5835d
+		 ssh_local_port(active_state));
f5835d
+	free(s);
f5835d
+	audit_fd = audit_open();
f5835d
+	if (audit_fd < 0) {
f5835d
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f5835d
+					 errno != EAFNOSUPPORT)
f5835d
+			error("cannot open audit");
f5835d
+		return;
f5835d
+	}
f5835d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f5835d
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
f5835d
+	audit_close(audit_fd);
f5835d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f5835d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f5835d
+		error("cannot write into audit");
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	char buf[AUDIT_LOG_SIZE];
f5835d
+	int audit_fd, audit_ok;
f5835d
+
f5835d
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
f5835d
+		fp, (intmax_t)pid, (intmax_t)uid);
f5835d
+	audit_fd = audit_open();
f5835d
+	if (audit_fd < 0) {
f5835d
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f5835d
+					 errno != EAFNOSUPPORT)
f5835d
+			error("cannot open audit");
f5835d
+		return;
f5835d
+	}
f5835d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f5835d
+			buf, NULL,
f5835d
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(active_state),
f5835d
+			NULL, 1);
f5835d
+	audit_close(audit_fd);
f5835d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f5835d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f5835d
+		error("cannot write into audit");
f5835d
+}
f5835d
 #endif /* USE_LINUX_AUDIT */
f5835d
diff -up openssh-7.6p1/auditstub.c.audit openssh-7.6p1/auditstub.c
f5835d
--- openssh-7.6p1/auditstub.c.audit	2017-10-04 17:18:32.835505053 +0200
f5835d
+++ openssh-7.6p1/auditstub.c	2017-10-04 17:18:32.835505053 +0200
f5835d
@@ -0,0 +1,50 @@
f5835d
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
f5835d
+
f5835d
+/*
f5835d
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
f5835d
+ * Use is subject to license terms.
f5835d
+ *
f5835d
+ * Redistribution and use in source and binary forms, with or without
f5835d
+ * modification, are permitted provided that the following conditions
f5835d
+ * are met:
f5835d
+ * 1. Redistributions of source code must retain the above copyright
f5835d
+ *    notice, this list of conditions and the following disclaimer.
f5835d
+ * 2. Redistributions in binary form must reproduce the above copyright
f5835d
+ *    notice, this list of conditions and the following disclaimer in the
f5835d
+ *    documentation and/or other materials provided with the distribution.
f5835d
+ *
f5835d
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f5835d
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f5835d
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f5835d
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f5835d
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f5835d
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f5835d
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f5835d
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f5835d
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f5835d
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f5835d
+ *
f5835d
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
f5835d
+ */
f5835d
+
f5835d
+#include <sys/types.h>
f5835d
+
f5835d
+void
f5835d
+audit_unsupported(int n)
f5835d
+{
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
f5835d
+{
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_session_key_free(int ctos)
f5835d
+{
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+}
f5835d
diff -up openssh-7.6p1/auth2.c.audit openssh-7.6p1/auth2.c
f5835d
--- openssh-7.6p1/auth2.c.audit	2017-10-04 17:18:32.746504598 +0200
f5835d
+++ openssh-7.6p1/auth2.c	2017-10-04 17:18:32.835505053 +0200
f5835d
@@ -255,9 +255,6 @@ input_userauth_request(int type, u_int32
f5835d
 		} else {
f5835d
 			/* Invalid user, fake password information */
f5835d
 			authctxt->pw = fakepw();
f5835d
-#ifdef SSH_AUDIT_EVENTS
f5835d
-			PRIVSEP(audit_event(SSH_INVALID_USER));
f5835d
-#endif
f5835d
 		}
f5835d
 #ifdef USE_PAM
f5835d
 		if (options.use_pam)
f5835d
diff -up openssh-7.6p1/auth2-hostbased.c.audit openssh-7.6p1/auth2-hostbased.c
f5835d
--- openssh-7.6p1/auth2-hostbased.c.audit	2017-10-04 17:18:32.683504276 +0200
f5835d
+++ openssh-7.6p1/auth2-hostbased.c	2017-10-04 17:18:32.835505053 +0200
f5835d
@@ -152,7 +152,7 @@ userauth_hostbased(struct ssh *ssh)
f5835d
 	/* test for allowed key and correct signature */
f5835d
 	authenticated = 0;
f5835d
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
f5835d
-	    PRIVSEP(sshkey_verify(key, sig, slen,
f5835d
+	    PRIVSEP(hostbased_key_verify(key, sig, slen,
f5835d
 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)
f5835d
 		authenticated = 1;
f5835d
 
f5835d
@@ -169,6 +169,19 @@ done:
f5835d
 	return authenticated;
f5835d
 }
f5835d
 
f5835d
+int
f5835d
+hostbased_key_verify(const struct sshkey *key, const u_char *sig, size_t slen,
f5835d
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
f5835d
+{
f5835d
+	int rv;
f5835d
+
f5835d
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat);
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	audit_key(0, &rv, key);
f5835d
+#endif
f5835d
+	return rv;
f5835d
+}
f5835d
+
f5835d
 /* return 1 if given hostkey is allowed */
f5835d
 int
f5835d
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
f5835d
diff -up openssh-7.6p1/auth2-pubkey.c.audit openssh-7.6p1/auth2-pubkey.c
f5835d
--- openssh-7.6p1/auth2-pubkey.c.audit	2017-10-04 17:18:32.828505018 +0200
f5835d
+++ openssh-7.6p1/auth2-pubkey.c	2017-10-04 17:18:32.835505053 +0200
f5835d
@@ -206,7 +206,7 @@ userauth_pubkey(struct ssh *ssh)
f5835d
 		/* test for correct signature */
f5835d
 		authenticated = 0;
f5835d
 		if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
f5835d
-		    PRIVSEP(sshkey_verify(key, sig, slen,
f5835d
+		    PRIVSEP(user_key_verify(key, sig, slen,
f5835d
 		    sshbuf_ptr(b), sshbuf_len(b),
f5835d
 		    (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
f5835d
 		    ssh->compat)) == 0) {
f5835d
@@ -250,6 +250,19 @@ done:
f5835d
 	return authenticated;
f5835d
 }
f5835d
 
f5835d
+int
f5835d
+user_key_verify(const struct sshkey *key, const u_char *sig, size_t slen,
f5835d
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
f5835d
+{
f5835d
+	int rv;
f5835d
+
f5835d
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat);
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	audit_key(1, &rv, key);
f5835d
+#endif
f5835d
+	return rv;
f5835d
+}
f5835d
+
f5835d
 static int
f5835d
 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
f5835d
 {
f5835d
diff -up openssh-7.6p1/auth.c.audit openssh-7.6p1/auth.c
f5835d
--- openssh-7.6p1/auth.c.audit	2017-10-04 17:18:32.746504598 +0200
f5835d
+++ openssh-7.6p1/auth.c	2017-10-04 17:18:32.835505053 +0200
f5835d
@@ -360,7 +360,7 @@ auth_log(Authctxt *authctxt, int authent
f5835d
 # endif
f5835d
 #endif
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
-	if (authenticated == 0 && !authctxt->postponed)
f5835d
+	if (authenticated == 0 && !authctxt->postponed && !partial)
f5835d
 		audit_event(audit_classify_auth(method));
f5835d
 #endif
f5835d
 }
f5835d
@@ -599,9 +599,6 @@ getpwnamallow(const char *user)
f5835d
 		record_failed_login(user,
f5835d
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
f5835d
 #endif
f5835d
-#ifdef SSH_AUDIT_EVENTS
f5835d
-		audit_event(SSH_INVALID_USER);
f5835d
-#endif /* SSH_AUDIT_EVENTS */
f5835d
 		return (NULL);
f5835d
 	}
f5835d
 	if (!allowed_user(pw))
f5835d
diff -up openssh-7.6p1/auth.h.audit openssh-7.6p1/auth.h
f5835d
--- openssh-7.6p1/auth.h.audit	2017-10-04 17:18:32.768504711 +0200
f5835d
+++ openssh-7.6p1/auth.h	2017-10-04 17:18:32.836505059 +0200
f5835d
@@ -198,6 +198,8 @@ struct passwd * getpwnamallow(const char
f5835d
 
f5835d
 char	*expand_authorized_keys(const char *, struct passwd *pw);
f5835d
 char	*authorized_principals_file(struct passwd *);
f5835d
+int	 user_key_verify(const struct sshkey *, const u_char *, size_t,
f5835d
+    const u_char *, size_t, const char *, u_int);
f5835d
 
f5835d
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
f5835d
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
f5835d
@@ -217,6 +218,8 @@ struct sshkey	*get_hostkey_private_by_ty
f5835d
 int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
f5835d
 int	 sshd_hostkey_sign(struct sshkey *, struct sshkey *, u_char **,
f5835d
 	     size_t *, const u_char *, size_t, const char *, u_int);
f5835d
+int	 hostbased_key_verify(const struct sshkey *, const u_char *, size_t,
f5835d
+    const u_char *, size_t, const char *, u_int);
f5835d
 
f5835d
 /* Key / cert options linkage to auth layer */
f5835d
 const struct sshauthopt *auth_options(struct ssh *);
f5835d
diff -up openssh-7.6p1/cipher.c.audit openssh-7.6p1/cipher.c
f5835d
--- openssh-7.6p1/cipher.c.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/cipher.c	2017-10-04 17:18:32.836505059 +0200
f5835d
@@ -61,25 +61,6 @@ struct sshcipher_ctx {
f5835d
 	const struct sshcipher *cipher;
f5835d
 };
f5835d
 
f5835d
-struct sshcipher {
f5835d
-	char	*name;
f5835d
-	u_int	block_size;
f5835d
-	u_int	key_len;
f5835d
-	u_int	iv_len;		/* defaults to block_size */
f5835d
-	u_int	auth_len;
f5835d
-	u_int	flags;
f5835d
-#define CFLAG_CBC		(1<<0)
f5835d
-#define CFLAG_CHACHAPOLY	(1<<1)
f5835d
-#define CFLAG_AESCTR		(1<<2)
f5835d
-#define CFLAG_NONE		(1<<3)
f5835d
-#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
f5835d
-#ifdef WITH_OPENSSL
f5835d
-	const EVP_CIPHER	*(*evptype)(void);
f5835d
-#else
f5835d
-	void	*ignored;
f5835d
-#endif
f5835d
-};
f5835d
-
f5835d
 static const struct sshcipher ciphers[] = {
f5835d
 #ifdef WITH_OPENSSL
f5835d
 #ifndef OPENSSL_NO_DES
f5835d
@@ -409,7 +409,7 @@ cipher_get_length(struct sshcipher_ctx *
f5835d
 void
f5835d
 cipher_free(struct sshcipher_ctx *cc)
f5835d
 {
f5835d
-	if (cc == NULL)
f5835d
+	if (cc == NULL || cc->cipher == NULL)
f5835d
 		return;
f5835d
 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
f5835d
 		explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
f5835d
diff -up openssh-7.6p1/cipher.h.audit openssh-7.6p1/cipher.h
f5835d
--- openssh-7.6p1/cipher.h.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/cipher.h	2017-10-04 17:18:32.836505059 +0200
f5835d
@@ -45,7 +45,25 @@
f5835d
 #define CIPHER_ENCRYPT		1
f5835d
 #define CIPHER_DECRYPT		0
f5835d
 
f5835d
-struct sshcipher;
f5835d
+struct sshcipher {
f5835d
+	char	*name;
f5835d
+	u_int	block_size;
f5835d
+	u_int	key_len;
f5835d
+	u_int	iv_len;		/* defaults to block_size */
f5835d
+	u_int	auth_len;
f5835d
+	u_int	flags;
f5835d
+#define CFLAG_CBC		(1<<0)
f5835d
+#define CFLAG_CHACHAPOLY	(1<<1)
f5835d
+#define CFLAG_AESCTR		(1<<2)
f5835d
+#define CFLAG_NONE		(1<<3)
f5835d
+#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
f5835d
+#ifdef WITH_OPENSSL
f5835d
+	const EVP_CIPHER	*(*evptype)(void);
f5835d
+#else
f5835d
+	void	*ignored;
f5835d
+#endif
f5835d
+};
f5835d
+
f5835d
 struct sshcipher_ctx;
f5835d
 
f5835d
 const struct sshcipher *cipher_by_name(const char *);
f5835d
diff -up openssh-7.6p1/kex.c.audit openssh-7.6p1/kex.c
f5835d
--- openssh-7.6p1/kex.c.audit	2017-10-04 17:18:32.822504987 +0200
f5835d
+++ openssh-7.6p1/kex.c	2017-10-04 17:18:32.836505059 +0200
f5835d
@@ -54,6 +54,7 @@
f5835d
 #include "ssherr.h"
f5835d
 #include "sshbuf.h"
f5835d
 #include "digest.h"
f5835d
+#include "audit.h"
f5835d
 
f5835d
 #ifdef GSSAPI
f5835d
 #include "ssh-gss.h"
f5835d
@@ -692,8 +693,12 @@ choose_enc(struct sshenc *enc, char *cli
f5835d
 {
f5835d
 	char *name = match_list(client, server, NULL);
f5835d
 
f5835d
-	if (name == NULL)
f5835d
+	if (name == NULL) {
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_CIPHER);
f5835d
+#endif
f5835d
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
f5835d
+	}
f5835d
 	if ((enc->cipher = cipher_by_name(name)) == NULL) {
f5835d
 		free(name);
f5835d
 		return SSH_ERR_INTERNAL_ERROR;
f5835d
@@ -713,8 +718,12 @@ choose_mac(struct ssh *ssh, struct sshma
f5835d
 {
f5835d
 	char *name = match_list(client, server, NULL);
f5835d
 
f5835d
-	if (name == NULL)
f5835d
+	if (name == NULL) {
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_MAC);
f5835d
+#endif
f5835d
 		return SSH_ERR_NO_MAC_ALG_MATCH;
f5835d
+	}
f5835d
 	if (mac_setup(mac, name) < 0) {
f5835d
 		free(name);
f5835d
 		return SSH_ERR_INTERNAL_ERROR;
f5835d
@@ -733,8 +742,12 @@ choose_comp(struct sshcomp *comp, char *
f5835d
 {
f5835d
 	char *name = match_list(client, server, NULL);
f5835d
 
f5835d
-	if (name == NULL)
f5835d
+	if (name == NULL) {
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_COMPRESSION);
f5835d
+#endif
f5835d
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
f5835d
+	}
f5835d
 	if (strcmp(name, "zlib@openssh.com") == 0) {
f5835d
 		comp->type = COMP_DELAYED;
f5835d
 	} else if (strcmp(name, "zlib") == 0) {
f5835d
@@ -904,6 +917,10 @@ kex_choose_conf(struct ssh *ssh)
f5835d
 		dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
f5835d
 		dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
f5835d
 		dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
f5835d
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+		audit_kex(mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
f5835d
+#endif
f5835d
 	}
f5835d
 	/* XXX need runden? */
f5835d
 	kex->we_need = need;
f5835d
@@ -1037,3 +1054,33 @@ dump_digest(char *msg, u_char *digest, i
f5835d
 	sshbuf_dump_data(digest, len, stderr);
f5835d
 }
f5835d
 #endif
f5835d
+
f5835d
+static void
f5835d
+enc_destroy(struct sshenc *enc)
f5835d
+{
f5835d
+	if (enc == NULL)
f5835d
+		return;
f5835d
+
f5835d
+	if (enc->key) {
f5835d
+		memset(enc->key, 0, enc->key_len);
f5835d
+		free(enc->key);
f5835d
+	}
f5835d
+
f5835d
+	if (enc->iv) {
f5835d
+		memset(enc->iv,  0, enc->iv_len);
f5835d
+		free(enc->iv);
f5835d
+	}
f5835d
+
f5835d
+	memset(enc, 0, sizeof(*enc));
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+newkeys_destroy(struct newkeys *newkeys)
f5835d
+{
f5835d
+	if (newkeys == NULL)
f5835d
+		return;
f5835d
+
f5835d
+	enc_destroy(&newkeys->enc);
f5835d
+	mac_destroy(&newkeys->mac);
f5835d
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
f5835d
+}
f5835d
diff -up openssh-7.6p1/kex.h.audit openssh-7.6p1/kex.h
f5835d
--- openssh-7.6p1/kex.h.audit	2017-10-04 17:18:32.822504987 +0200
f5835d
+++ openssh-7.6p1/kex.h	2017-10-04 17:18:32.836505059 +0200
f5835d
@@ -219,6 +219,8 @@ int	 kexgss_client(struct ssh *);
f5835d
 int	 kexgss_server(struct ssh *);
f5835d
 #endif
f5835d
 
f5835d
+void	newkeys_destroy(struct newkeys *newkeys);
f5835d
+
f5835d
 int	 kex_dh_hash(int, const char *, const char *,
f5835d
     const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
f5835d
     const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
f5835d
diff -up openssh-7.6p1/mac.c.audit openssh-7.6p1/mac.c
f5835d
--- openssh-7.6p1/mac.c.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/mac.c	2017-10-04 17:18:32.836505059 +0200
f5835d
@@ -242,6 +242,20 @@ mac_clear(struct sshmac *mac)
f5835d
 	mac->umac_ctx = NULL;
f5835d
 }
f5835d
 
f5835d
+void
f5835d
+mac_destroy(struct sshmac *mac)
f5835d
+{
f5835d
+	if (mac == NULL)
f5835d
+		return;
f5835d
+
f5835d
+	if (mac->key) {
f5835d
+		memset(mac->key, 0, mac->key_len);
f5835d
+		free(mac->key);
f5835d
+	}
f5835d
+
f5835d
+	memset(mac, 0, sizeof(*mac));
f5835d
+}
f5835d
+
f5835d
 /* XXX copied from ciphers_valid */
f5835d
 #define	MAC_SEP	","
f5835d
 int
f5835d
diff -up openssh-7.6p1/mac.h.audit openssh-7.6p1/mac.h
f5835d
--- openssh-7.6p1/mac.h.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/mac.h	2017-10-04 17:18:32.837505064 +0200
f5835d
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
f5835d
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
f5835d
     const u_char *, size_t);
f5835d
 void	 mac_clear(struct sshmac *);
f5835d
+void	 mac_destroy(struct sshmac *);
f5835d
 
f5835d
 #endif /* SSHMAC_H */
f5835d
diff -up openssh-7.6p1/Makefile.in.audit openssh-7.6p1/Makefile.in
f5835d
--- openssh-7.6p1/Makefile.in.audit	2017-10-04 17:18:32.749504614 +0200
f5835d
+++ openssh-7.6p1/Makefile.in	2017-10-04 17:18:32.837505064 +0200
f5835d
@@ -100,7 +100,8 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
f5835d
 	kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
f5835d
 	kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
f5835d
 	kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
f5835d
-	platform-pledge.o platform-tracing.o platform-misc.o
f5835d
+	platform-pledge.o platform-tracing.o platform-misc.o \
f5835d
+	auditstub.o
f5835d
 
f5835d
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
f5835d
 	sshconnect.o sshconnect2.o mux.o
f5835d
diff -up openssh-7.6p1/monitor.c.audit openssh-7.6p1/monitor.c
f5835d
--- openssh-7.6p1/monitor.c.audit	2017-10-04 17:18:32.824504997 +0200
f5835d
+++ openssh-7.6p1/monitor.c	2017-10-04 17:18:32.837505064 +0200
f5835d
@@ -102,6 +102,7 @@
f5835d
 #include "compat.h"
f5835d
 #include "ssh2.h"
f5835d
 #include "authfd.h"
f5835d
+#include "audit.h"
f5835d
 #include "match.h"
f5835d
 #include "ssherr.h"
f5835d
 
f5835d
@@ -117,6 +118,8 @@ extern Buffer auth_debug;
f5835d
 extern struct sshbuf *loginmsg;
f5835d
 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
f5835d
 
f5835d
+extern void destroy_sensitive_data(int);
f5835d
+
f5835d
 /* State exported from the child */
f5835d
 static struct sshbuf *child_state;
f5835d
 
f5835d
@@ -167,6 +170,11 @@ int mm_answer_gss_updatecreds(int, Buffe
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
 int mm_answer_audit_event(int, struct sshbuf *);
f5835d
 int mm_answer_audit_command(int, struct sshbuf *);
f5835d
+int mm_answer_audit_end_command(int, struct sshbuf *);
f5835d
+int mm_answer_audit_unsupported_body(int, struct sshbuf *);
f5835d
+int mm_answer_audit_kex_body(int, struct sshbuf *);
f5835d
+int mm_answer_audit_session_key_free_body(int, struct sshbuf *);
f5835d
+int mm_answer_audit_server_key_free(int, struct sshbuf *);
f5835d
 #endif
f5835d
 
f5835d
 static int monitor_read_log(struct monitor *);
f5835d
@@ -222,6 +230,10 @@ struct mon_table mon_dispatch_proto20[]
f5835d
 #endif
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f5835d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f5835d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f5835d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f5835d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f5835d
 #endif
f5835d
 #ifdef BSD_AUTH
f5835d
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
f5835d
@@ -260,6 +272,11 @@ struct mon_table mon_dispatch_postauth20
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f5835d
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
f5835d
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
f5835d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f5835d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f5835d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f5835d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f5835d
 #endif
f5835d
     {0, 0, NULL}
f5835d
 };
f5835d
@@ -1396,8 +1413,10 @@ mm_answer_keyverify(int sock, struct ssh
f5835d
 	char *sigalg;
f5835d
 	size_t signaturelen, datalen, bloblen;
f5835d
 	int r, ret, valid_data = 0, encoded_ret;
f5835d
+	int type = 0;
f5835d
 
f5835d
-	if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
f5835d
+	if ((r = sshbuf_get_u32(m, &type)) != 0 ||
f5835d
+	    (r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
f5835d
 	    (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
f5835d
 	    (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
f5835d
 	    (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
f5835d
@@ -1405,6 +1424,8 @@ mm_answer_keyverify(int sock, struct ssh
f5835d
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
f5835d
 	  !monitor_allowed_key(blob, bloblen))
f5835d
 		fatal("%s: bad key, not previously allowed", __func__);
f5835d
+	if (type != key_blobtype)
f5835d
+		fatal("%s: bad key type", __func__);
f5835d
 
f5835d
 	/* Empty signature algorithm means NULL. */
f5835d
 	if (*sigalg == '\0') {
f5835d
@@ -1414,21 +1435,24 @@ mm_answer_keyverify(int sock, struct ssh
f5835d
 	case MM_USERKEY:
f5835d
 		valid_data = monitor_valid_userblob(data, datalen);
f5835d
 		auth_method = "publickey";
f5835d
+		ret = user_key_verify(key, signature, signaturelen, data,
f5835d
+		    datalen, sigalg, active_state->compat);
f5835d
 		break;
f5835d
 	case MM_HOSTKEY:
f5835d
 		valid_data = monitor_valid_hostbasedblob(data, datalen,
f5835d
 		    hostbased_cuser, hostbased_chost);
f5835d
+		ret = hostbased_key_verify(key, signature, signaturelen, data,
f5835d
+		    datalen, sigalg, active_state->compat);
f5835d
 		auth_method = "hostbased";
f5835d
 		break;
f5835d
 	default:
f5835d
 		valid_data = 0;
f5835d
+		ret = 0;
f5835d
 		break;
f5835d
 	}
f5835d
 	if (!valid_data)
f5835d
 		fatal("%s: bad signature data blob", __func__);
f5835d
 
f5835d
-	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
f5835d
-	    sigalg, active_state->compat);
f5835d
 	debug3("%s: %s %p signature %s", __func__, auth_method, key,
f5835d
 	    (ret == 0) ? "verified" : "unverified");
f5835d
 	auth2_record_key(authctxt, ret == 0, key);
f5835d
@@ -1485,6 +1509,12 @@ mm_session_close(Session *s)
f5835d
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
f5835d
 		session_pty_cleanup2(s);
f5835d
 	}
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	if (s->command != NULL) {
f5835d
+		debug3("%s: command %d", __func__, s->command_handle);
f5835d
+		session_end_command2(s);
f5835d
+	}
f5835d
+#endif
f5835d
 	session_unused(s->self);
f5835d
 }
f5835d
 
f5835d
@@ -1588,6 +1618,8 @@ mm_answer_term(int sock, Buffer *req)
f5835d
 		sshpam_cleanup();
f5835d
 #endif
f5835d
 
f5835d
+	destroy_sensitive_data(0);
f5835d
+
f5835d
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
f5835d
 		if (errno != EINTR)
f5835d
 			exit(1);
f5835d
@@ -1630,12 +1662,47 @@ mm_answer_audit_command(int socket, Buff
f5835d
 {
f5835d
 	char *cmd;
f5835d
 	int r;
f5835d
+	Session *s;
f5835d
 
f5835d
 	debug3("%s entering", __func__);
f5835d
 	if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
f5835d
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
 	/* sanity check command, if so how? */
f5835d
-	audit_run_command(cmd);
f5835d
+	s = session_new();
f5835d
+	if (s == NULL)
f5835d
+		fatal("%s: error allocating a session", __func__);
f5835d
+	s->command = cmd;
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	s->command_handle = audit_run_command(cmd);
f5835d
+#endif
f5835d
+
f5835d
+	sshbuf_reset(m);
f5835d
+	sshbuf_put_u32(m, s->self);
f5835d
+
f5835d
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
f5835d
+
f5835d
+	return (0);
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+mm_answer_audit_end_command(int socket, struct sshbuf *m)
f5835d
+{
f5835d
+	int handle, r;
f5835d
+	size_t len;
f5835d
+	u_char *cmd = NULL;
f5835d
+	Session *s;
f5835d
+
f5835d
+	debug3("%s entering", __func__);
f5835d
+	if ((r = sshbuf_get_u32(m, &handle)) != 0 ||
f5835d
+	    (r = sshbuf_get_string(m, &cmd, &len)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	s = session_by_id(handle);
f5835d
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
f5835d
+	    strcmp(s->command, cmd) != 0)
f5835d
+		fatal("%s: invalid handle", __func__);
f5835d
+	mm_session_close(s);
f5835d
 	free(cmd);
f5835d
 	return (0);
f5835d
 }
f5835d
@@ -1702,6 +1768,7 @@ monitor_apply_keystate(struct monitor *p
f5835d
 void
f5835d
 mm_get_keystate(struct monitor *pmonitor)
f5835d
 {
f5835d
+	struct sshbuf *m;
f5835d
 	debug3("%s: Waiting for new keys", __func__);
f5835d
 
f5835d
 	if ((child_state = sshbuf_new()) == NULL)
f5835d
@@ -1709,6 +1776,19 @@ mm_get_keystate(struct monitor *pmonitor
f5835d
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
f5835d
 	    child_state);
f5835d
 	debug3("%s: GOT new keys", __func__);
f5835d
+
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	m = sshbuf_new();
f5835d
+	mm_request_receive_expect(pmonitor->m_sendfd,
f5835d
+				  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
f5835d
+	mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, m);
f5835d
+	sshbuf_free(m);
f5835d
+#endif
f5835d
+
f5835d
+	/* Drain any buffered messages from the child */
f5835d
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
f5835d
+		;
f5835d
+
f5835d
 }
f5835d
 
f5835d
 
f5835d
@@ -1976,3 +2056,102 @@ mm_answer_gss_updatecreds(int socket, Bu
f5835d
 
f5835d
 #endif /* GSSAPI */
f5835d
 
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+int
f5835d
+mm_answer_audit_unsupported_body(int sock, struct sshbuf *m)
f5835d
+{
f5835d
+	int what, r;
f5835d
+
f5835d
+	if ((r = sshbuf_get_u32(m, &what)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	audit_unsupported_body(what);
f5835d
+
f5835d
+	sshbuf_reset(m);
f5835d
+
f5835d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
f5835d
+	return 0;
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+mm_answer_audit_kex_body(int sock, struct sshbuf *m)
f5835d
+{
f5835d
+	int ctos, r;
f5835d
+	char *cipher, *mac, *compress, *pfs;
f5835d
+	u_int64_t tmp;
f5835d
+	pid_t pid;
f5835d
+	uid_t uid;
f5835d
+
f5835d
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
f5835d
+	    (r = sshbuf_get_cstring(m, &cipher, NULL)) != 0 ||
f5835d
+	    (r = sshbuf_get_cstring(m, &mac, NULL)) != 0 ||
f5835d
+	    (r = sshbuf_get_cstring(m, &compress, NULL)) != 0 ||
f5835d
+	    (r = sshbuf_get_cstring(m, &pfs, NULL)) != 0 ||
f5835d
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	pid = (pid_t) tmp;
f5835d
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	uid = (pid_t) tmp;
f5835d
+
f5835d
+	audit_kex_body(ctos, cipher, mac, compress, pfs, pid, uid);
f5835d
+
f5835d
+	free(cipher);
f5835d
+	free(mac);
f5835d
+	free(compress);
f5835d
+	free(pfs);
f5835d
+	sshbuf_reset(m);
f5835d
+
f5835d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
f5835d
+	return 0;
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+mm_answer_audit_session_key_free_body(int sock, struct sshbuf *m)
f5835d
+{
f5835d
+	int ctos, r;
f5835d
+	u_int64_t tmp;
f5835d
+	pid_t pid;
f5835d
+	uid_t uid;
f5835d
+
f5835d
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
f5835d
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	pid = (pid_t) tmp;
f5835d
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	uid = (uid_t) tmp;
f5835d
+
f5835d
+	audit_session_key_free_body(ctos, pid, uid);
f5835d
+
f5835d
+	sshbuf_reset(m);
f5835d
+
f5835d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
f5835d
+	return 0;
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+mm_answer_audit_server_key_free(int sock, struct sshbuf *m)
f5835d
+{
f5835d
+	size_t len, r;
f5835d
+	char *fp;
f5835d
+	u_int64_t tmp;
f5835d
+	pid_t pid;
f5835d
+	uid_t uid;
f5835d
+
f5835d
+	if ((r = sshbuf_get_cstring(m, &fp, &len)) != 0 ||
f5835d
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	pid = (pid_t) tmp;
f5835d
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	uid = (uid_t) tmp;
f5835d
+
f5835d
+	audit_destroy_sensitive_data(fp, pid, uid);
f5835d
+
f5835d
+	free(fp);
f5835d
+	sshbuf_reset(m);
f5835d
+
f5835d
+	return 0;
f5835d
+}
f5835d
+#endif /* SSH_AUDIT_EVENTS */
f5835d
diff -up openssh-7.6p1/monitor.h.audit openssh-7.6p1/monitor.h
f5835d
--- openssh-7.6p1/monitor.h.audit	2017-10-04 17:18:32.781504777 +0200
f5835d
+++ openssh-7.6p1/monitor.h	2017-10-04 17:18:32.837505064 +0200
f5835d
@@ -69,7 +69,13 @@ enum monitor_reqtype {
f5835d
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
f5835d
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
f5835d
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
f5835d
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
f5835d
+	MONITOR_REQ_AUDIT_EVENT = 112,
f5835d
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
f5835d
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
f5835d
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
f5835d
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
f5835d
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
f5835d
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124
f5835d
 
f5835d
 };
f5835d
 
f5835d
diff -up openssh-7.6p1/monitor_wrap.c.audit openssh-7.6p1/monitor_wrap.c
f5835d
--- openssh-7.6p1/monitor_wrap.c.audit	2017-10-04 17:18:32.750504619 +0200
f5835d
+++ openssh-7.6p1/monitor_wrap.c	2017-10-04 17:18:32.838505069 +0200
f5835d
@@ -463,7 +463,7 @@ mm_key_allowed(enum mm_keytype type, con
f5835d
  */
f5835d
 
f5835d
 int
f5835d
-mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
f5835d
+mm_sshkey_verify(enum mm_keytype type, const struct sshkey *key, const u_char *sig, size_t siglen,
f5835d
     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
f5835d
 {
f5835d
 	struct sshbuf *m;
f5835d
@@ -478,7 +478,8 @@ mm_sshkey_verify(const struct sshkey *ke
f5835d
 
f5835d
 	if ((m = sshbuf_new()) == NULL)
f5835d
 		fatal("%s: sshbuf_new failed", __func__);
f5835d
-	if ((r = sshkey_puts(key, m)) != 0 ||
f5835d
+	if ((r = sshbuf_put_u32(m, type)) != 0 ||
f5835d
+	    (r = sshkey_puts(key, m)) != 0 ||
f5835d
 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
f5835d
 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
f5835d
 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
f5835d
@@ -497,6 +498,20 @@ mm_sshkey_verify(const struct sshkey *ke
f5835d
 	return 0;
f5835d
 }
f5835d
 
f5835d
+int
f5835d
+mm_hostbased_key_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
f5835d
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
f5835d
+{
f5835d
+	return mm_sshkey_verify(MM_HOSTKEY, key, sig, siglen, data, datalen, pkalg, compat);
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+mm_user_key_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
f5835d
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
f5835d
+{
f5835d
+	return mm_sshkey_verify(MM_USERKEY, key, sig, siglen, data, datalen, pkalg, compat);
f5835d
+}
f5835d
+
f5835d
 void
f5835d
 mm_send_keystate(struct monitor *monitor)
f5835d
 {
f5835d
@@ -874,11 +889,12 @@ mm_audit_event(ssh_audit_event_t event)
f5835d
 	sshbuf_free(m);
f5835d
 }
f5835d
 
f5835d
-void
f5835d
+int
f5835d
 mm_audit_run_command(const char *command)
f5835d
 {
f5835d
 	struct sshbuf *m;
f5835d
 	int r;
f5835d
+	int handle;
f5835d
 
f5835d
 	debug3("%s entering command %s", __func__, command);
f5835d
 
f5835d
@@ -885,6 +901,30 @@ mm_audit_run_command(const char *command
f5835d
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
 
f5835d
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
f5835d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, m);
f5835d
+
f5835d
+	if ((r = sshbuf_get_u32(m, &handle)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	sshbuf_free(m);
f5835d
+
f5835d
+	return (handle);
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+mm_audit_end_command(int handle, const char *command)
f5835d
+{
f5835d
+	int r;
f5835d
+	struct sshbuf *m;
f5835d
+
f5835d
+	debug3("%s entering command %s", __func__, command);
f5835d
+
f5835d
+ 	if ((m = sshbuf_new()) == NULL)
f5835d
+ 		fatal("%s: sshbuf_new failed", __func__);
f5835d
+	if ((r = sshbuf_put_u32(m, handle)) != 0 ||
f5835d
+	    (r = sshbuf_put_cstring(m, command)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, m);
f5835d
 	sshbuf_free(m);
f5835d
 }
f5835d
 #endif /* SSH_AUDIT_EVENTS */
f5835d
@@ -1020,3 +1056,83 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
f5835d
 	return (ok);
f5835d
 }
f5835d
 #endif /* GSSAPI */
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+void
f5835d
+mm_audit_unsupported_body(int what)
f5835d
+{
f5835d
+	int r;
f5835d
+	struct sshbuf *m;
f5835d
+
f5835d
+ 	if ((m = sshbuf_new()) == NULL)
f5835d
+ 		fatal("%s: sshbuf_new failed", __func__);
f5835d
+	if ((r = sshbuf_put_u32(m, what)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, m);
f5835d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
f5835d
+				  m);
f5835d
+
f5835d
+	sshbuf_free(m);
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
f5835d
+		  uid_t uid)
f5835d
+{
f5835d
+	int r;
f5835d
+	struct sshbuf *m;
f5835d
+
f5835d
+ 	if ((m = sshbuf_new()) == NULL)
f5835d
+ 		fatal("%s: sshbuf_new failed", __func__);
f5835d
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
f5835d
+	    (r = sshbuf_put_cstring(m, cipher)) != 0 ||
f5835d
+	    (r = sshbuf_put_cstring(m, (mac ? mac : "<implicit>"))) != 0 ||
f5835d
+	    (r = sshbuf_put_cstring(m, compress)) != 0 ||
f5835d
+	    (r = sshbuf_put_cstring(m, fps)) != 0 ||
f5835d
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
f5835d
+	    (r = sshbuf_put_u64(m, uid)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, m);
f5835d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
f5835d
+				  m);
f5835d
+
f5835d
+	sshbuf_free(m);
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	int r;
f5835d
+	struct sshbuf *m;
f5835d
+
f5835d
+ 	if ((m = sshbuf_new()) == NULL)
f5835d
+ 		fatal("%s: sshbuf_new failed", __func__);
f5835d
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
f5835d
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
f5835d
+	    (r = sshbuf_put_u64(m, uid)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
f5835d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
f5835d
+				  m);
f5835d
+	sshbuf_free(m);
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f5835d
+{
f5835d
+	int r;
f5835d
+	struct sshbuf *m;
f5835d
+
f5835d
+ 	if ((m = sshbuf_new()) == NULL)
f5835d
+ 		fatal("%s: sshbuf_new failed", __func__);
f5835d
+	if ((r = sshbuf_put_cstring(m, fp)) != 0 ||
f5835d
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
f5835d
+	    (r = sshbuf_put_u64(m, uid)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, m);
f5835d
+	sshbuf_free(m);
f5835d
+}
f5835d
+#endif /* SSH_AUDIT_EVENTS */
f5835d
diff -up openssh-7.6p1/monitor_wrap.h.audit openssh-7.6p1/monitor_wrap.h
f5835d
--- openssh-7.6p1/monitor_wrap.h.audit	2017-10-04 17:18:32.750504619 +0200
f5835d
+++ openssh-7.6p1/monitor_wrap.h	2017-10-04 17:18:32.838505069 +0200
f5835d
@@ -53,7 +53,9 @@ int mm_key_allowed(enum mm_keytype, cons
f5835d
     struct sshauthopt **);
f5835d
 int mm_hostbased_key_allowed(struct passwd *, const char *,
f5835d
     const char *, struct sshkey *);
f5835d
-int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
f5835d
+int mm_hostbased_key_verify(const struct sshkey *, const u_char *, size_t,
f5835d
+    const u_char *, size_t, const char *, u_int);
f5835d
+int mm_user_key_verify(const struct sshkey *, const u_char *, size_t,
f5835d
     const u_char *, size_t, const char *, u_int);
f5835d
 
f5835d
 #ifdef GSSAPI
f5835d
@@ -78,7 +80,12 @@ void mm_sshpam_free_ctx(void *);
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
 #include "audit.h"
f5835d
 void mm_audit_event(ssh_audit_event_t);
f5835d
-void mm_audit_run_command(const char *);
f5835d
+int mm_audit_run_command(const char *);
f5835d
+void mm_audit_end_command(int, const char *);
f5835d
+void mm_audit_unsupported_body(int);
f5835d
+void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
f5835d
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
f5835d
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
f5835d
 #endif
f5835d
 
f5835d
 struct Session;
f5835d
diff -up openssh-7.6p1/packet.c.audit openssh-7.6p1/packet.c
f5835d
--- openssh-7.6p1/packet.c.audit	2017-10-04 17:18:32.672504220 +0200
f5835d
+++ openssh-7.6p1/packet.c	2017-10-04 17:25:48.141741390 +0200
f5835d
@@ -67,6 +67,7 @@
f5835d
 #include <zlib.h>
f5835d
 
f5835d
 #include "xmalloc.h"
f5835d
+#include "audit.h"
f5835d
 #include "crc32.h"
f5835d
 #include "compat.h"
f5835d
 #include "ssh2.h"
f5835d
@@ -502,6 +503,13 @@ ssh_packet_get_connection_out(struct ssh
f5835d
 	return ssh->state->connection_out;
f5835d
 }
f5835d
 
f5835d
+static int
f5835d
+packet_state_has_keys (const struct session_state *state)
f5835d
+{
f5835d
+	return state != NULL &&
f5835d
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
f5835d
+}
f5835d
+
f5835d
 /*
f5835d
  * Returns the IP-address of the remote host as a string.  The returned
f5835d
  * string must not be freed.
f5835d
@@ -566,22 +574,19 @@ ssh_packet_close_internal(struct ssh *ss
f5835d
 {
f5835d
 	struct session_state *state = ssh->state;
f5835d
 	u_int mode;
f5835d
+	u_int had_keys = packet_state_has_keys(state);
f5835d
 
f5835d
 	if (!state->initialized)
f5835d
 		return;
f5835d
 	state->initialized = 0;
f5835d
-	if (do_close) {
f5835d
-		if (state->connection_in == state->connection_out) {
f5835d
-			close(state->connection_out);
f5835d
-		} else {
f5835d
-			close(state->connection_in);
f5835d
-			close(state->connection_out);
f5835d
-		}
f5835d
-	}
f5835d
 	sshbuf_free(state->input);
f5835d
+	state->input = NULL;
f5835d
 	sshbuf_free(state->output);
f5835d
+	state->output = NULL;
f5835d
 	sshbuf_free(state->outgoing_packet);
f5835d
+	state->outgoing_packet = NULL;
f5835d
	sshbuf_free(state->incoming_packet);
f5835d
+	state->incoming_packet = NULL;
f5835d
 	for (mode = 0; mode < MODE_MAX; mode++) {
f5835d
 		kex_free_newkeys(state->newkeys[mode]);	/* current keys */
f5835d
 		state->newkeys[mode] = NULL;
f5835d
@@ -615,8 +616,18 @@ ssh_packet_close_internal(struct ssh *ss
f5835d
 	}
f5835d
 	cipher_free(state->send_context);
f5835d
 	cipher_free(state->receive_context);
f5835d
+	if (had_keys && state->server_side) {
f5835d
+		/* Assuming this is called only from privsep child */
f5835d
+		audit_session_key_free(MODE_MAX);
f5835d
+	}
f5835d
 	state->send_context = state->receive_context = NULL;
f5835d
 	if (do_close) {
f5835d
+		if (state->connection_in == state->connection_out) {
f5835d
+			close(state->connection_out);
f5835d
+		} else {
f5835d
+			close(state->connection_in);
f5835d
+			close(state->connection_out);
f5835d
+		}
f5835d
 		free(ssh->local_ipaddr);
f5835d
 		ssh->local_ipaddr = NULL;
f5835d
 		free(ssh->remote_ipaddr);
f5835d
@@ -854,6 +863,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
f5835d
 		   (unsigned long long)state->p_read.blocks,
f5835d
 		   (unsigned long long)state->p_send.bytes,
f5835d
 		   (unsigned long long)state->p_send.blocks);
f5835d
+		audit_session_key_free(mode);
f5835d
 		cipher_free(*ccp);
f5835d
 		*ccp = NULL;
f5835d
 		kex_free_newkeys(state->newkeys[mode]);
f5835d
@@ -2135,6 +2145,72 @@ ssh_packet_get_output(struct ssh *ssh)
f5835d
 	return (void *)ssh->state->output;
f5835d
 }
f5835d
 
f5835d
+static void
f5835d
+newkeys_destroy_and_free(struct newkeys *newkeys)
f5835d
+{
f5835d
+	if (newkeys == NULL)
f5835d
+		return;
f5835d
+
f5835d
+	free(newkeys->enc.name);
f5835d
+
f5835d
+	if (newkeys->mac.enabled) {
f5835d
+		mac_clear(&newkeys->mac);
f5835d
+		free(newkeys->mac.name);
f5835d
+	}
f5835d
+
f5835d
+	free(newkeys->comp.name);
f5835d
+
f5835d
+	newkeys_destroy(newkeys);
f5835d
+	free(newkeys);
f5835d
+}
f5835d
+
f5835d
+static void
f5835d
+packet_destroy_state(struct session_state *state)
f5835d
+{
f5835d
+	if (state == NULL)
f5835d
+		return;
f5835d
+
f5835d
+	cipher_free(state->receive_context);
f5835d
+	cipher_free(state->send_context);
f5835d
+
f5835d
+	sshbuf_free(state->input);
f5835d
+	state->input = NULL;
f5835d
+	sshbuf_free(state->output);
f5835d
+	state->output = NULL;
f5835d
+	sshbuf_free(state->outgoing_packet);
f5835d
+	state->outgoing_packet = NULL;
f5835d
+	sshbuf_free(state->incoming_packet);
f5835d
+	state->incoming_packet = NULL;
f5835d
+	if (state->compression_buffer) {
f5835d
+		sshbuf_free(state->compression_buffer);
f5835d
+		state->compression_buffer = NULL;
f5835d
+	}
f5835d
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
f5835d
+	state->newkeys[MODE_IN] = NULL;
f5835d
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
f5835d
+	state->newkeys[MODE_OUT] = NULL;
f5835d
+	mac_destroy(state->packet_discard_mac);
f5835d
+//	TAILQ_HEAD(, packet) outgoing;
f5835d
+//	memset(state, 0, sizeof(state));
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+packet_destroy_all(int audit_it, int privsep)
f5835d
+{
f5835d
+	if (audit_it)
f5835d
+		audit_it = (active_state != NULL && packet_state_has_keys(active_state->state));
f5835d
+	if (active_state != NULL)
f5835d
+		packet_destroy_state(active_state->state);
f5835d
+	if (audit_it) {
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+		if (privsep)
f5835d
+			audit_session_key_free(MODE_MAX);
f5835d
+		else
f5835d
+			audit_session_key_free_body(MODE_MAX, getpid(), getuid());
f5835d
+#endif
f5835d
+	}
f5835d
+}
f5835d
+
f5835d
 /* Reset after_authentication and reset compression in post-auth privsep */
f5835d
 static int
f5835d
 ssh_packet_set_postauth(struct ssh *ssh)
f5835d
diff -up openssh-7.6p1/packet.h.audit openssh-7.6p1/packet.h
f5835d
--- openssh-7.6p1/packet.h.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/packet.h	2017-10-04 17:18:32.838505069 +0200
f5835d
@@ -217,4 +217,5 @@ extern struct ssh *active_state;
f5835d
 # undef EC_POINT
f5835d
 #endif
f5835d
 
f5835d
+void	 packet_destroy_all(int, int);
f5835d
 #endif				/* PACKET_H */
f5835d
diff -up openssh-7.6p1/session.c.audit openssh-7.6p1/session.c
f5835d
--- openssh-7.6p1/session.c.audit	2017-10-04 17:18:32.812504936 +0200
f5835d
+++ openssh-7.6p1/session.c	2017-10-04 17:18:32.839505074 +0200
f5835d
@@ -138,7 +138,7 @@ extern char *__progname;
f5835d
 extern int debug_flag;
f5835d
 extern u_int utmp_len;
f5835d
 extern int startup_pipe;
f5835d
-extern void destroy_sensitive_data(void);
f5835d
+extern void destroy_sensitive_data(int);
f5835d
 extern struct sshbuf *loginmsg;
f5835d
 extern struct sshauthopt *auth_opts;
f5835d
 char *tun_fwd_ifnames; /* serverloop.c */
f5835d
@@ -605,6 +605,14 @@ do_exec_pty(struct ssh *ssh, Session *s,
f5835d
 	/* Parent.  Close the slave side of the pseudo tty. */
f5835d
 	close(ttyfd);
f5835d
 
f5835d
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
f5835d
+	/* do_login in the child did not affect state in this process,
f5835d
+	   compensate.  From an architectural standpoint, this is extremely
f5835d
+	   ugly. */
f5835d
+	if (command != NULL)
f5835d
+		audit_count_session_open();
f5835d
+#endif
f5835d
+
f5835d
 	/* Enter interactive session. */
f5835d
 	s->ptymaster = ptymaster;
f5835d
 	packet_set_interactive(1, 
f5835d
@@ -724,15 +732,19 @@ do_exec(struct ssh *ssh, Session *s, con
f5835d
 	    s->self);
f5835d
 
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
+	if (s->command != NULL || s->command_handle != -1)
f5835d
+		fatal("do_exec: command already set");
f5835d
 	if (command != NULL)
f5835d
-		PRIVSEP(audit_run_command(command));
f5835d
+		s->command = xstrdup(command);
f5835d
 	else if (s->ttyfd == -1) {
f5835d
 		char *shell = s->pw->pw_shell;
f5835d
 
f5835d
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
f5835d
 			shell =_PATH_BSHELL;
f5835d
-		PRIVSEP(audit_run_command(shell));
f5835d
+		s->command = xstrdup(shell);
f5835d
 	}
f5835d
+	if (s->command != NULL && s->ptyfd == -1)
f5835d
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
f5835d
 #endif
f5835d
 	if (s->ttyfd != -1)
f5835d
 		ret = do_exec_pty(ssh, s, command);
f5835d
@@ -1499,8 +1511,11 @@ do_child(struct ssh *ssh, Session *s, co
f5835d
 	int r = 0;
f5835d
 
f5835d
 	/* remove hostkey from the child's memory */
f5835d
-	destroy_sensitive_data();
f5835d
+	destroy_sensitive_data(1);
f5835d
 	packet_clear_keys();
f5835d
+	/* Don't audit this - both us and the parent would be talking to the
f5835d
+	   monitor over a single socket, with no synchronization. */
f5835d
+	packet_destroy_all(0, 1);
f5835d
 
f5835d
 	/* Force a password change */
f5835d
 	if (s->authctxt->force_pwchange) {
f5835d
@@ -1714,6 +1729,9 @@ session_unused(int id)
f5835d
 	sessions[id].ttyfd = -1;
f5835d
 	sessions[id].ptymaster = -1;
f5835d
 	sessions[id].x11_chanids = NULL;
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	sessions[id].command_handle = -1;
f5835d
+#endif
f5835d
 	sessions[id].next_unused = sessions_first_unused;
f5835d
 	sessions_first_unused = id;
f5835d
 }
f5835d
@@ -1796,6 +1814,19 @@ session_open(Authctxt *authctxt, int cha
f5835d
 }
f5835d
 
f5835d
 Session *
f5835d
+session_by_id(int id)
f5835d
+{
f5835d
+	if (id >= 0 && id < sessions_nalloc) {
f5835d
+		Session *s = &sessions[id];
f5835d
+		if (s->used)
f5835d
+			return s;
f5835d
+	}
f5835d
+	debug("%s: unknown id %d", __func__, id);
f5835d
+	session_dump();
f5835d
+	return NULL;
f5835d
+}
f5835d
+
f5835d
+Session *
f5835d
 session_by_tty(char *tty)
f5835d
 {
f5835d
 	int i;
f5835d
@@ -2307,6 +2338,32 @@ session_exit_message(struct ssh *ssh, Se
f5835d
 		chan_write_failed(ssh, c);
f5835d
 }
f5835d
 
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+void
f5835d
+session_end_command2(Session *s)
f5835d
+{
f5835d
+	if (s->command != NULL) {
f5835d
+		if (s->command_handle != -1)
f5835d
+			audit_end_command(s->command_handle, s->command);
f5835d
+		free(s->command);
f5835d
+		s->command = NULL;
f5835d
+		s->command_handle = -1;
f5835d
+	}
f5835d
+}
f5835d
+
f5835d
+static void
f5835d
+session_end_command(Session *s)
f5835d
+{
f5835d
+	if (s->command != NULL) {
f5835d
+		if (s->command_handle != -1)
f5835d
+			PRIVSEP(audit_end_command(s->command_handle, s->command));
f5835d
+		free(s->command);
f5835d
+		s->command = NULL;
f5835d
+		s->command_handle = -1;
f5835d
+	}
f5835d
+}
f5835d
+#endif
f5835d
+
f5835d
 void
f5835d
 session_close(struct ssh *ssh, Session *s)
f5835d
 {
f5835d
@@ -2320,6 +2377,10 @@ session_close(struct ssh *ssh, Session *
f5835d
 
f5835d
 	if (s->ttyfd != -1)
f5835d
 		session_pty_cleanup(s);
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	if (s->command)
f5835d
+		session_end_command(s);
f5835d
+#endif
f5835d
 	free(s->term);
f5835d
 	free(s->display);
f5835d
 	free(s->x11_chanids);
f5835d
@@ -2528,6 +2589,15 @@ do_authenticated2(struct ssh *ssh, Authc
f5835d
 	server_loop2(ssh, authctxt);
f5835d
 }
f5835d
 
f5835d
+static void
f5835d
+do_cleanup_one_session(Session *s)
f5835d
+{
f5835d
+	session_pty_cleanup2(s);
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	session_end_command2(s);
f5835d
+#endif
f5835d
+}
f5835d
+
f5835d
 void
f5835d
 do_cleanup(struct ssh *ssh, Authctxt *authctxt)
f5835d
 {
f5835d
@@ -2585,7 +2655,7 @@ do_cleanup(struct ssh *ssh, Authctxt *au
f5835d
 	 * or if running in monitor.
f5835d
 	 */
f5835d
 	if (!use_privsep || mm_is_monitor())
f5835d
-		session_destroy_all(ssh, session_pty_cleanup2);
f5835d
+		session_destroy_all(ssh, do_cleanup_one_session);
f5835d
 }
f5835d
 
f5835d
 /* Return a name for the remote host that fits inside utmp_size */
f5835d
diff -up openssh-7.6p1/session.h.audit openssh-7.6p1/session.h
f5835d
--- openssh-7.6p1/session.h.audit	2017-10-02 21:34:26.000000000 +0200
f5835d
+++ openssh-7.6p1/session.h	2017-10-04 17:18:32.839505074 +0200
f5835d
@@ -60,6 +60,12 @@ struct Session {
f5835d
 		char	*name;
f5835d
 		char	*val;
f5835d
 	} *env;
f5835d
+
f5835d
+	/* exec */
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	int	command_handle;
f5835d
+	char	*command;
f5835d
+#endif
f5835d
 };
f5835d
 
f5835d
 void	 do_authenticated(struct ssh *, Authctxt *);
f5835d
@@ -72,8 +78,10 @@ void	 session_close_by_pid(struct ssh *s
f5835d
 void	 session_close_by_channel(struct ssh *, int, void *);
f5835d
 void	 session_destroy_all(struct ssh *, void (*)(Session *));
f5835d
 void	 session_pty_cleanup2(Session *);
f5835d
+void	 session_end_command2(Session *);
f5835d
 
f5835d
 Session	*session_new(void);
f5835d
+Session *session_by_id(int);
f5835d
 Session	*session_by_tty(char *);
f5835d
 void	 session_close(struct ssh *, Session *);
f5835d
 void	 do_setusercontext(struct passwd *);
f5835d
diff -up openssh-7.6p1/sshd.c.audit openssh-7.6p1/sshd.c
f5835d
--- openssh-7.6p1/sshd.c.audit	2017-10-04 17:18:32.830505028 +0200
f5835d
+++ openssh-7.6p1/sshd.c	2017-10-04 17:18:32.839505074 +0200
f5835d
@@ -122,6 +122,7 @@
f5835d
 #include "ssh-gss.h"
f5835d
 #endif
f5835d
 #include "monitor_wrap.h"
f5835d
+#include "audit.h"
f5835d
 #include "ssh-sandbox.h"
f5835d
 #include "auth-options.h"
f5835d
 #include "version.h"
f5835d
@@ -248,7 +249,7 @@ Buffer loginmsg;
f5835d
 struct passwd *privsep_pw = NULL;
f5835d
 
f5835d
 /* Prototypes for various functions defined later in this file. */
f5835d
-void destroy_sensitive_data(void);
f5835d
+void destroy_sensitive_data(int);
f5835d
 void demote_sensitive_data(void);
f5835d
 static void do_ssh2_kex(void);
f5835d
 
f5835d
@@ -265,6 +266,15 @@ close_listen_socks(void)
f5835d
 	num_listen_socks = -1;
f5835d
 }
f5835d
 
f5835d
+/*
f5835d
+ * Is this process listening for clients (i.e. not specific to any specific
f5835d
+ * client connection?)
f5835d
+ */
f5835d
+int listening_for_clients(void)
f5835d
+{
f5835d
+	return num_listen_socks >= 0;
f5835d
+}
f5835d
+
f5835d
 static void
f5835d
 close_startup_pipes(void)
f5835d
 {
f5835d
@@ -475,18 +485,45 @@ sshd_exchange_identification(struct ssh
f5835d
 	}
f5835d
 }
f5835d
 
f5835d
-/* Destroy the host and server keys.  They will no longer be needed. */
f5835d
+/*
f5835d
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
f5835d
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
f5835d
+ */
f5835d
 void
f5835d
-destroy_sensitive_data(void)
f5835d
+destroy_sensitive_data(int privsep)
f5835d
 {
f5835d
 	u_int i;
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	pid_t pid;
f5835d
+	uid_t uid;
f5835d
 
f5835d
+	pid = getpid();
f5835d
+	uid = getuid();
f5835d
+#endif
f5835d
 	for (i = 0; i < options.num_host_key_files; i++) {
f5835d
 		if (sensitive_data.host_keys[i]) {
f5835d
+			char *fp;
f5835d
+
f5835d
+			if (sshkey_is_private(sensitive_data.host_keys[i]))
f5835d
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
f5835d
+			else
f5835d
+				fp = NULL;
f5835d
 			sshkey_free(sensitive_data.host_keys[i]);
f5835d
 			sensitive_data.host_keys[i] = NULL;
f5835d
+			if (fp != NULL) {
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+				if (privsep)
f5835d
+					PRIVSEP(audit_destroy_sensitive_data(fp,
f5835d
+						pid, uid));
f5835d
+				else
f5835d
+					audit_destroy_sensitive_data(fp,
f5835d
+						pid, uid);
f5835d
+#endif
f5835d
+				free(fp);
f5835d
+			}
f5835d
 		}
f5835d
-		if (sensitive_data.host_certificates[i]) {
f5835d
+		if (sensitive_data.host_certificates
f5835d
+		    && sensitive_data.host_certificates[i]) {
f5835d
 			sshkey_free(sensitive_data.host_certificates[i]);
f5835d
 			sensitive_data.host_certificates[i] = NULL;
f5835d
 		}
f5835d
@@ -499,16 +536,34 @@ demote_sensitive_data(void)
f5835d
 	struct sshkey *tmp;
f5835d
 	u_int i;
f5835d
 	int r;
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+	pid_t pid;
f5835d
+	uid_t uid;
f5835d
 
f5835d
+	pid = getpid();
f5835d
+	uid = getuid();
f5835d
+#endif
f5835d
 	for (i = 0; i < options.num_host_key_files; i++) {
f5835d
 		if (sensitive_data.host_keys[i]) {
f5835d
+			char *fp;
f5835d
+
f5835d
+			if (sshkey_is_private(sensitive_data.host_keys[i]))
f5835d
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
f5835d
+			else
f5835d
+				fp = NULL;
f5835d
			if ((r = sshkey_demote(sensitive_data.host_keys[i],
f5835d
			    &tmp)) != 0)
f5835d
				fatal("could not demote host %s key: %s",
f5835d
				    sshkey_type(sensitive_data.host_keys[i]),
f5835d
				    ssh_err(r));
f5835d
			sshkey_free(sensitive_data.host_keys[i]);
f5835d
 			sensitive_data.host_keys[i] = tmp;
f5835d
+			if (fp != NULL) {
f5835d
+#ifdef SSH_AUDIT_EVENTS
f5835d
+				audit_destroy_sensitive_data(fp, pid, uid);
f5835d
+#endif
f5835d
+				free(fp);
f5835d
+			}
f5835d
 		}
f5835d
 		/* Certs do not need demotion */
f5835d
 	}
f5835d
@@ -587,7 +642,7 @@ privsep_preauth(Authctxt *authctxt)
f5835d
 
f5835d
 	if (use_privsep == PRIVSEP_ON)
f5835d
 		box = ssh_sandbox_init(pmonitor);
f5835d
-	pid = fork();
f5835d
+	pmonitor->m_pid = pid = fork();
f5835d
 	if (pid == -1) {
f5835d
 		fatal("fork of unprivileged child failed");
f5835d
 	} else if (pid != 0) {
f5835d
@@ -1162,6 +1217,7 @@ server_accept_loop(int *sock_in, int *so
f5835d
 		if (received_sigterm) {
f5835d
 			logit("Received signal %d; terminating.",
f5835d
 			    (int) received_sigterm);
f5835d
+			destroy_sensitive_data(0);
f5835d
 			close_listen_socks();
f5835d
 			if (options.pid_file != NULL)
f5835d
 				unlink(options.pid_file);
f5835d
@@ -2165,6 +2221,9 @@ main(int ac, char **av)
f5835d
 	do_authenticated(ssh, authctxt);
f5835d
 
f5835d
 	/* The connection has been terminated. */
f5835d
+	packet_destroy_all(1, 1);
f5835d
+	destroy_sensitive_data(1);
f5835d
+
f5835d
 	packet_get_bytes(&ibytes, &obytes);
f5835d
 	verbose("Transferred: sent %llu, received %llu bytes",
f5835d
 	    (unsigned long long)obytes, (unsigned long long)ibytes);
f5835d
@@ -2344,6 +2403,15 @@ void
f5835d
 cleanup_exit(int i)
f5835d
 {
f5835d
 	struct ssh *ssh = active_state; /* XXX */
f5835d
+	static int in_cleanup = 0;
f5835d
+	int is_privsep_child;
f5835d
+
f5835d
+	/* cleanup_exit can be called at the very least from the privsep
f5835d
+	   wrappers used for auditing.  Make sure we don't recurse
f5835d
+	   indefinitely. */
f5835d
+	if (in_cleanup)
f5835d
+		_exit(i);
f5835d
+	in_cleanup = 1;
f5835d
 
f5835d
 	if (the_authctxt) {
f5835d
 		do_cleanup(ssh, the_authctxt);
f5835d
@@ -2356,9 +2424,14 @@ cleanup_exit(int i)
f5835d
 				    pmonitor->m_pid, strerror(errno));
f5835d
 		}
f5835d
 	}
f5835d
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
f5835d
+	if (sensitive_data.host_keys != NULL)
f5835d
+		destroy_sensitive_data(is_privsep_child);
f5835d
+	packet_destroy_all(1, is_privsep_child);
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
f5835d
-	if (!use_privsep || mm_is_monitor())
f5835d
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
f5835d
+	    (!use_privsep || mm_is_monitor()))
f5835d
 		audit_event(SSH_CONNECTION_ABANDON);
f5835d
 #endif
f5835d
 	_exit(i);
f5835d
diff -up openssh-7.6p1/sshkey.c.audit openssh-7.6p1/sshkey.c
f5835d
--- openssh-7.6p1/sshkey.c.audit	2017-10-04 17:18:32.758504660 +0200
f5835d
+++ openssh-7.6p1/sshkey.c	2017-10-04 17:18:32.839505074 +0200
f5835d
@@ -295,6 +295,32 @@ sshkey_type_is_valid_ca(int type)
f5835d
 }
f5835d
 
f5835d
 int
f5835d
+sshkey_is_private(const struct sshkey *k)
f5835d
+{
f5835d
+      switch (k->type) {
f5835d
+#ifdef WITH_OPENSSL
f5835d
+      case KEY_RSA_CERT:
f5835d
+      case KEY_RSA:
f5835d
+              return k->rsa->d != NULL;
f5835d
+      case KEY_DSA_CERT:
f5835d
+      case KEY_DSA:
f5835d
+              return k->dsa->priv_key != NULL;
f5835d
+#ifdef OPENSSL_HAS_ECC
f5835d
+      case KEY_ECDSA_CERT:
f5835d
+      case KEY_ECDSA:
f5835d
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
f5835d
+#endif /* OPENSSL_HAS_ECC */
f5835d
+#endif /* WITH_OPENSSL */
f5835d
+      case KEY_ED25519_CERT:
f5835d
+      case KEY_ED25519:
f5835d
+              return (k->ed25519_pk != NULL);
f5835d
+      default:
f5835d
+              /* fatal("key_is_private: bad key type %d", k->type); */
f5835d
+              return 0;
f5835d
+      }
f5835d
+}
f5835d
+
f5835d
+int
f5835d
 sshkey_is_cert(const struct sshkey *k)
f5835d
 {
f5835d
 	if (k == NULL)
f5835d
diff -up openssh-7.6p1/sshkey.h.audit openssh-7.6p1/sshkey.h
f5835d
--- openssh-7.6p1/sshkey.h.audit	2017-10-04 17:18:32.758504660 +0200
f5835d
+++ openssh-7.6p1/sshkey.h	2017-10-04 17:18:32.840505079 +0200
f5835d
@@ -133,6 +133,7 @@ u_int		 sshkey_size(const struct sshkey
f5835d
 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
f5835d
 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
f5835d
 int	 sshkey_type_from_name(const char *);
f5835d
+int	 sshkey_is_private(const struct sshkey *);
f5835d
 int	 sshkey_is_cert(const struct sshkey *);
f5835d
 int	 sshkey_type_is_cert(int);
f5835d
 int	 sshkey_type_plain(int);