f32b842
diff -up openssh-8.6p1/audit-bsm.c.audit openssh-8.6p1/audit-bsm.c
f32b842
--- openssh-8.6p1/audit-bsm.c.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/audit-bsm.c	2021-04-19 16:47:35.753062106 +0200
f32b842
@@ -373,13 +373,26 @@ audit_connection_from(const char *host,
d3d59da
 #endif
d3d59da
 }
d3d59da
 
d3d59da
+int
def1deb
+audit_run_command(struct ssh *ssh, const char *command)
d3d59da
+{
d3d59da
+	/* not implemented */
def1deb
+	return 0;
d3d59da
+}
d3d59da
+
def1deb
 void
def1deb
-audit_run_command(const char *command)
def1deb
+audit_end_command(struct ssh *ssh, int handle, const char *command)
def1deb
 {
def1deb
 	/* not implemented */
d3d59da
 }
d3d59da
 
d3d59da
 void
def1deb
+audit_count_session_open(void)
def1deb
+{
def1deb
+	/* not necessary */
def1deb
+}
def1deb
+
def1deb
+void
def1deb
 audit_session_open(struct logininfo *li)
def1deb
 {
def1deb
 	/* not implemented */
f32b842
@@ -391,6 +404,12 @@ audit_session_close(struct logininfo *li
d3d59da
 	/* not implemented */
d3d59da
 }
d3d59da
 
d3d59da
+int
def1deb
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
d3d59da
+{
d3d59da
+	/* not implemented */
d3d59da
+}
d3d59da
+
d3d59da
 void
def1deb
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
d3d59da
 {
f32b842
@@ -452,4 +471,28 @@ audit_event(struct ssh *ssh, ssh_audit_e
d3d59da
 		debug("%s: unhandled event %d", __func__, event);
d3d59da
 	}
d3d59da
 }
d3d59da
+
d3d59da
+void
def1deb
+audit_unsupported_body(struct ssh *ssh, int what)
d3d59da
+{
d3d59da
+	/* not implemented */
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
d3d59da
+{
d3d59da
+	/* not implemented */
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_session_key_free_body(struct ssh * ssh, int ctos, pid_t pid, uid_t uid)
d3d59da
+{
d3d59da
+	/* not implemented */
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
d3d59da
+{
d3d59da
+	/* not implemented */
d3d59da
+}
d3d59da
 #endif /* BSM */
f32b842
diff -up openssh-8.6p1/audit.c.audit openssh-8.6p1/audit.c
f32b842
--- openssh-8.6p1/audit.c.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/audit.c	2021-04-19 16:47:35.753062106 +0200
def1deb
@@ -34,6 +34,12 @@
bbf61da
 #include "log.h"
535d341
 #include "hostfile.h"
535d341
 #include "auth.h"
535d341
+#include "ssh-gss.h"
535d341
+#include "monitor_wrap.h"
535d341
+#include "xmalloc.h"
535d341
+#include "misc.h"
535d341
+#include "servconf.h"
b1ec43e
+#include "ssherr.h"
535d341
 
535d341
 /*
535d341
  * Care must be taken when using this since it WILL NOT be initialized when
6cf9b8e
@@ -41,6 +47,7 @@
535d341
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
535d341
  */
535d341
 extern Authctxt *the_authctxt;
535d341
+extern ServerOptions options;
535d341
 
535d341
 /* Maybe add the audit class to struct Authmethod? */
535d341
 ssh_audit_event_t
6cf9b8e
@@ -69,13 +76,10 @@ audit_classify_auth(const char *method)
535d341
 const char *
535d341
 audit_username(void)
535d341
 {
535d341
-	static const char unknownuser[] = "(unknown user)";
535d341
-	static const char invaliduser[] = "(invalid user)";
535d341
+	static const char unknownuser[] = "(unknown)";
535d341
 
535d341
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
535d341
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
535d341
 		return (unknownuser);
535d341
-	if (!the_authctxt->valid)
535d341
-		return (invaliduser);
535d341
 	return (the_authctxt->user);
535d341
 }
535d341
 
57349a8
@@ -109,6 +113,35 @@ audit_event_lookup(ssh_audit_event_t ev)
535d341
 	return(event_lookup[i].name);
535d341
 }
535d341
 
535d341
+void
def1deb
+audit_key(struct ssh *ssh, int host_user, int *rv, const struct sshkey *key)
535d341
+{
535d341
+	char *fp;
535d341
+
535d341
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
def1deb
+	if (audit_keyusage(ssh, host_user, fp, (*rv == 0)) == 0)
0f4b4cc
+		*rv = -SSH_ERR_INTERNAL_ERROR;
535d341
+	free(fp);
535d341
+}
535d341
+
535d341
+void
def1deb
+audit_unsupported(struct ssh *ssh, int what)
535d341
+{
def1deb
+	PRIVSEP(audit_unsupported_body(ssh, what));
535d341
+}
535d341
+
535d341
+void
def1deb
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
535d341
+{
def1deb
+	PRIVSEP(audit_kex_body(ssh, ctos, enc, mac, comp, pfs, getpid(), getuid()));
535d341
+}
535d341
+
535d341
+void
def1deb
+audit_session_key_free(struct ssh *ssh, int ctos)
535d341
+{
def1deb
+	PRIVSEP(audit_session_key_free_body(ssh, ctos, getpid(), getuid()));
535d341
+}
535d341
+
535d341
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
535d341
 /*
535d341
  * Null implementations of audit functions.
def1deb
@@ -138,6 +171,17 @@ audit_event(struct ssh *ssh, ssh_audit_e
535d341
 }
535d341
 
535d341
 /*
535d341
+ * Called when a child process has called, or will soon call,
535d341
+ * audit_session_open.
535d341
+ */
535d341
+void
535d341
+audit_count_session_open(void)
535d341
+{
535d341
+	debug("audit count session open euid %d user %s", geteuid(),
535d341
+	      audit_username());
535d341
+}
535d341
+
535d341
+/*
535d341
  * Called when a user session is started.  Argument is the tty allocated to
535d341
  * the session, or NULL if no tty was allocated.
535d341
  *
def1deb
@@ -172,13 +216,82 @@ audit_session_close(struct logininfo *li
535d341
 /*
535d341
  * This will be called when a user runs a non-interactive command.  Note that
535d341
  * it may be called multiple times for a single connection since SSH2 allows
535d341
- * multiple sessions within a single connection.
535d341
+ * multiple sessions within a single connection.  Returns a "handle" for
535d341
+ * audit_end_command.
535d341
  */
535d341
-void
def1deb
-audit_run_command(const char *command)
535d341
+int
def1deb
+audit_run_command(struct ssh *ssh, const char *command)
535d341
 {
535d341
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
535d341
 	    audit_username(), command);
535d341
+	return 0;
535d341
+}
535d341
+
535d341
+/*
535d341
+ * This will be called when the non-interactive command finishes.  Note that
535d341
+ * it may be called multiple times for a single connection since SSH2 allows
535d341
+ * multiple sessions within a single connection.  "handle" should come from
535d341
+ * the corresponding audit_run_command.
535d341
+ */
535d341
+void
def1deb
+audit_end_command(struct ssh *ssh, int handle, const char *command)
535d341
+{
535d341
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
535d341
+	    audit_username(), command);
535d341
+}
535d341
+
535d341
+/*
535d341
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
535d341
+ *
535d341
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
535d341
+ */
535d341
+int
def1deb
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
535d341
+{
57349a8
+	debug("audit %s key usage euid %d user %s fingerprint %s, result %d",
57349a8
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(),
28ce052
+		fp, rv);
535d341
+}
535d341
+
535d341
+/*
535d341
+ * This will be called when the protocol negotiation fails.
535d341
+ */
535d341
+void
def1deb
+audit_unsupported_body(struct ssh *ssh, int what)
535d341
+{
535d341
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
535d341
+}
535d341
+
535d341
+/*
535d341
+ * This will be called on succesfull protocol negotiation.
535d341
+ */
535d341
+void
def1deb
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
535d341
+	       uid_t uid)
535d341
+{
535d341
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
535d341
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
535d341
+	        (unsigned)uid);
535d341
+}
535d341
+
535d341
+/*
535d341
+ * This will be called on succesfull session key discard
535d341
+ */
535d341
+void
def1deb
+audit_session_key_free_body(struct ssh *, int ctos, pid_t pid, uid_t uid)
535d341
+{
535d341
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
535d341
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
535d341
+}
535d341
+
535d341
+/*
535d341
+ * This will be called on destroy private part of the server key
535d341
+ */
535d341
+void
def1deb
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
535d341
+{
535d341
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
535d341
+		geteuid(), fp, (long)pid, (unsigned)uid);
535d341
 }
535d341
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
535d341
 #endif /* SSH_AUDIT_EVENTS */
f32b842
diff -up openssh-8.6p1/audit.h.audit openssh-8.6p1/audit.h
f32b842
--- openssh-8.6p1/audit.h.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/audit.h	2021-04-19 16:47:35.753062106 +0200
6cf9b8e
@@ -26,6 +26,7 @@
535d341
 # define _SSH_AUDIT_H
535d341
 
535d341
 #include "loginrec.h"
bbf61da
+#include "sshkey.h"
535d341
 
def1deb
 struct ssh;
def1deb
 
def1deb
@@ -45,13 +46,32 @@ enum ssh_audit_event_type {
28ce052
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
28ce052
 	SSH_AUDIT_UNKNOWN
535d341
 };
28ce052
+
28ce052
+enum ssh_audit_kex {
28ce052
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
28ce052
+	SSH_AUDIT_UNSUPPORTED_MAC,
28ce052
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
28ce052
+};
535d341
 typedef enum ssh_audit_event_type ssh_audit_event_t;
535d341
 
535d341
+int	listening_for_clients(void);
535d341
+
535d341
 void	audit_connection_from(const char *, int);
def1deb
 void	audit_event(struct ssh *, ssh_audit_event_t);
535d341
+void	audit_count_session_open(void);
535d341
 void	audit_session_open(struct logininfo *);
535d341
 void	audit_session_close(struct logininfo *);
535d341
-void	audit_run_command(const char *);
def1deb
+int	audit_run_command(struct ssh *, const char *);
def1deb
+void 	audit_end_command(struct ssh *, int, const char *);
535d341
 ssh_audit_event_t audit_classify_auth(const char *);
def1deb
+int	audit_keyusage(struct ssh *, int, char *, int);
def1deb
+void	audit_key(struct ssh *, int, int *, const struct sshkey *);
def1deb
+void	audit_unsupported(struct ssh *, int);
def1deb
+void	audit_kex(struct ssh *, int, char *, char *, char *, char *);
def1deb
+void	audit_unsupported_body(struct ssh *, int);
def1deb
+void	audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
def1deb
+void	audit_session_key_free(struct ssh *, int ctos);
def1deb
+void	audit_session_key_free_body(struct ssh *, int ctos, pid_t, uid_t);
def1deb
+void	audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
535d341
 
535d341
 #endif /* _SSH_AUDIT_H */
f32b842
diff -up openssh-8.6p1/audit-linux.c.audit openssh-8.6p1/audit-linux.c
f32b842
--- openssh-8.6p1/audit-linux.c.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/audit-linux.c	2021-04-19 16:47:35.753062106 +0200
f284c5e
@@ -33,27 +33,40 @@
d3d59da
 
d3d59da
 #include "log.h"
d3d59da
 #include "audit.h"
bbf61da
+#include "sshkey.h"
d3d59da
+#include "hostfile.h"
d3d59da
+#include "auth.h"
1900351
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
d3d59da
+#include "servconf.h"
d3d59da
 #include "canohost.h"
b487a6d
 #include "packet.h"
b487a6d
-
d3d59da
+#include "cipher.h"
b487a6d
+#include "channels.h"
b487a6d
+#include "session.h"
b487a6d
+
cf5c114
+#define AUDIT_LOG_SIZE 256
d3d59da
+
d3d59da
+extern ServerOptions options;
d3d59da
+extern Authctxt *the_authctxt;
d3d59da
+extern u_int utmp_len;
b487a6d
 const char *audit_username(void);
d3d59da
 
d3d59da
-int
b487a6d
-linux_audit_record_event(int uid, const char *username, const char *hostname,
b487a6d
-    const char *ip, const char *ttyn, int success)
d3d59da
+static void
f284c5e
+linux_audit_user_logxxx(int uid, const char *username,
b487a6d
+    const char *ip, const char *ttyn, int success, int event)
d3d59da
 {
d3d59da
 	int audit_fd, rc, saved_errno;
d3d59da
 
b487a6d
 	if ((audit_fd = audit_open()) < 0) {
d3d59da
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
d3d59da
 		    errno == EAFNOSUPPORT)
d3d59da
-			return 1; /* No audit support in kernel */
d3d59da
+			return; /* No audit support in kernel */
d3d59da
 		else
d3d59da
-			return 0; /* Must prevent login */
d3d59da
+			goto fatal_report; /* Must prevent login */
d3d59da
 	}
d3d59da
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
d3d59da
+	rc = audit_log_acct_message(audit_fd, event,
d3d59da
 	    NULL, "login", username ? username : "(unknown)",
f284c5e
-	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
f284c5e
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
d3d59da
 	saved_errno = errno;
f284c5e
 	close(audit_fd);
f284c5e
 
57349a8
@@ -65,9 +78,96 @@ linux_audit_record_event(int uid, const
d3d59da
 		rc = 0;
d3d59da
 	errno = saved_errno;
b487a6d
 
b487a6d
-	return rc >= 0;
d3d59da
+	if (rc < 0) {
d3d59da
+fatal_report:
d3d59da
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
d3d59da
+	}
b487a6d
+}
b487a6d
+
d3d59da
+static void
d3d59da
+linux_audit_user_auth(int uid, const char *username,
f284c5e
+    const char *ip, const char *ttyn, int success, int event)
d3d59da
+{
d3d59da
+	int audit_fd, rc, saved_errno;
d3d59da
+	static const char *event_name[] = {
d3d59da
+		"maxtries exceeded",
d3d59da
+		"root denied",
d3d59da
+		"success",
d3d59da
+		"none",
d3d59da
+		"password",
d3d59da
+		"challenge-response",
d3d59da
+		"pubkey",
d3d59da
+		"hostbased",
d3d59da
+		"gssapi",
d3d59da
+		"invalid user",
d3d59da
+		"nologin",
d3d59da
+		"connection closed",
d3d59da
+		"connection abandoned",
d3d59da
+		"unknown"
d3d59da
+	};
d3d59da
+
d3d59da
+	audit_fd = audit_open();
d3d59da
+	if (audit_fd < 0) {
d3d59da
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
d3d59da
+		    errno == EAFNOSUPPORT)
d3d59da
+			return; /* No audit support in kernel */
d3d59da
+		else
d3d59da
+			goto fatal_report; /* Must prevent login */
d3d59da
+	}
28ce052
+
d3d59da
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
d3d59da
+		event = SSH_AUDIT_UNKNOWN;
d3d59da
+
d3d59da
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
d3d59da
+	    NULL, event_name[event], username ? username : "(unknown)",
f284c5e
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
d3d59da
+	saved_errno = errno;
d3d59da
+	close(audit_fd);
d3d59da
+	/*
d3d59da
+	 * Do not report error if the error is EPERM and sshd is run as non
d3d59da
+	 * root user.
d3d59da
+	 */
d3d59da
+	if ((rc == -EPERM) && (geteuid() != 0))
d3d59da
+		rc = 0;
d3d59da
+	errno = saved_errno;
d3d59da
+	if (rc < 0) {
d3d59da
+fatal_report:
d3d59da
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
d3d59da
+	}
d3d59da
+}
d3d59da
+
d3d59da
+int
def1deb
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
d3d59da
+{
d3d59da
+	char buf[AUDIT_LOG_SIZE];
d3d59da
+	int audit_fd, rc, saved_errno;
d3d59da
+
d3d59da
+	audit_fd = audit_open();
d3d59da
+	if (audit_fd < 0) {
d3d59da
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
d3d59da
+					 errno == EAFNOSUPPORT)
d3d59da
+			return 1; /* No audit support in kernel */
28ce052
+		else
d3d59da
+			return 0; /* Must prevent login */
d3d59da
+	}
9b05c6d
+	snprintf(buf, sizeof(buf), "%s_auth grantors=auth-key", host_user ? "pubkey" : "hostbased");
d3d59da
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
def1deb
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(ssh), NULL, rv);
d3d59da
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
d3d59da
+		goto out;
57349a8
+	snprintf(buf, sizeof(buf), "op=negotiate kind=auth-key fp=%s", fp);
57349a8
+	rc = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, buf, NULL,
def1deb
+		ssh_remote_ipaddr(ssh), NULL, rv);
d3d59da
+out:
d3d59da
+	saved_errno = errno;
d3d59da
+	audit_close(audit_fd);
d3d59da
+	errno = saved_errno;
d3d59da
+	/* do not report error if the error is EPERM and sshd is run as non root user */
d3d59da
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
b487a6d
 }
b487a6d
 
d3d59da
+static int user_login_count = 0;
d3d59da
+
d3d59da
 /* Below is the sshd audit API code */
d3d59da
 
d3d59da
 void
def1deb
@@ -76,49 +176,210 @@ audit_connection_from(const char *host,
d3d59da
 	/* not implemented */
b487a6d
 }
d3d59da
 
d3d59da
+int
def1deb
+audit_run_command(struct ssh *ssh, const char *command)
def1deb
+{
28ce052
+	if (!user_login_count++)
f284c5e
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1deb
+		    ssh_remote_ipaddr(ssh),
f284c5e
+		    "ssh", 1, AUDIT_USER_LOGIN);
f284c5e
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1deb
+	    ssh_remote_ipaddr(ssh),
f284c5e
+	    "ssh", 1, AUDIT_USER_START);
d3d59da
+	return 0;
d3d59da
+}
d3d59da
+
def1deb
 void
def1deb
-audit_run_command(const char *command)
def1deb
+audit_end_command(struct ssh *ssh, int handle, const char *command)
def1deb
 {
def1deb
-	/* not implemented */
f284c5e
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1deb
+	    ssh_remote_ipaddr(ssh),
f284c5e
+	    "ssh", 1, AUDIT_USER_END);
28ce052
+	if (user_login_count && !--user_login_count)
f284c5e
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1deb
+		    ssh_remote_ipaddr(ssh),
f284c5e
+		    "ssh", 1, AUDIT_USER_LOGOUT);
d3d59da
+}
d3d59da
+
d3d59da
+void
d3d59da
+audit_count_session_open(void)
d3d59da
+{
d3d59da
+	user_login_count++;
d3d59da
 }
d3d59da
 
d3d59da
 void
d3d59da
 audit_session_open(struct logininfo *li)
d3d59da
 {
b487a6d
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
b487a6d
-	    li->line, 1) == 0)
d3d59da
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
28ce052
+	if (!user_login_count++)
d3d59da
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f284c5e
+		    li->line, 1, AUDIT_USER_LOGIN);
d3d59da
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f284c5e
+	    li->line, 1, AUDIT_USER_START);
d3d59da
 }
d3d59da
 
d3d59da
 void
d3d59da
 audit_session_close(struct logininfo *li)
d3d59da
 {
d3d59da
-	/* not implemented */
d3d59da
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f284c5e
+	    li->line, 1, AUDIT_USER_END);
28ce052
+	if (user_login_count && !--user_login_count)
d3d59da
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f284c5e
+		    li->line, 1, AUDIT_USER_LOGOUT);
d3d59da
 }
d3d59da
 
d3d59da
 void
def1deb
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
def1deb
 {
d3d59da
 	switch(event) {
667e6f0
-	case SSH_AUTH_SUCCESS:
d3d59da
-	case SSH_CONNECTION_CLOSE:
d3d59da
 	case SSH_NOLOGIN:
d3d59da
-	case SSH_LOGIN_EXCEED_MAXTRIES:
d3d59da
 	case SSH_LOGIN_ROOT_DENIED:
f284c5e
+		linux_audit_user_auth(-1, audit_username(),
b487a6d
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
f284c5e
+		linux_audit_user_logxxx(-1, audit_username(),
b487a6d
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
d3d59da
 		break;
6996c6f
-	case SSH_AUTH_FAIL_NONE:
d3d59da
 	case SSH_AUTH_FAIL_PASSWD:
6996c6f
+		if (options.use_pam)
6996c6f
+			break;
6996c6f
+	case SSH_LOGIN_EXCEED_MAXTRIES:
d3d59da
 	case SSH_AUTH_FAIL_KBDINT:
d3d59da
 	case SSH_AUTH_FAIL_PUBKEY:
d3d59da
 	case SSH_AUTH_FAIL_HOSTBASED:
d3d59da
 	case SSH_AUTH_FAIL_GSSAPI:
f284c5e
+		linux_audit_user_auth(-1, audit_username(),
b487a6d
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
d3d59da
+		break;
d3d59da
+
d3d59da
+	case SSH_CONNECTION_CLOSE:
d3d59da
+		if (user_login_count) {
d3d59da
+			while (user_login_count--)
b487a6d
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f284c5e
+				    ssh_remote_ipaddr(ssh),
f284c5e
+				    "ssh", 1, AUDIT_USER_END);
b487a6d
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f284c5e
+			    ssh_remote_ipaddr(ssh),
f284c5e
+			    "ssh", 1, AUDIT_USER_LOGOUT);
d3d59da
+		}
d3d59da
+		break;
d3d59da
+
d3d59da
+	case SSH_CONNECTION_ABANDON:
d3d59da
 	case SSH_INVALID_USER:
d3d59da
-		linux_audit_record_event(-1, audit_username(), NULL,
b487a6d
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
f284c5e
+		linux_audit_user_logxxx(-1, audit_username(),
b487a6d
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
d3d59da
 		break;
d3d59da
 	default:
b487a6d
 		debug("%s: unhandled event %d", __func__, event);
b487a6d
 		break;
d3d59da
 	}
d3d59da
 }
b487a6d
+
d3d59da
+void
def1deb
+audit_unsupported_body(struct ssh *ssh, int what)
d3d59da
+{
d3d59da
+#ifdef AUDIT_CRYPTO_SESSION
d3d59da
+	char buf[AUDIT_LOG_SIZE];
d3d59da
+	const static char *name[] = { "cipher", "mac", "comp" };
d3d59da
+	char *s;
d3d59da
+	int audit_fd;
d3d59da
+
d3d59da
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
def1deb
+		name[what], ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
def1deb
+		ssh_local_port(ssh));
84822b5
+	free(s);
d3d59da
+	audit_fd = audit_open();
d3d59da
+	if (audit_fd < 0)
d3d59da
+		/* no problem, the next instruction will be fatal() */
d3d59da
+		return;
d3d59da
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
def1deb
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 0);
d3d59da
+	audit_close(audit_fd);
d3d59da
+#endif
d3d59da
+}
d3d59da
+
d3d59da
+const static char *direction[] = { "from-server", "from-client", "both" };
d3d59da
+
d3d59da
+void
def1deb
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress,
bbf61da
+    char *pfs, pid_t pid, uid_t uid)
d3d59da
+{
d3d59da
+#ifdef AUDIT_CRYPTO_SESSION
d3d59da
+	char buf[AUDIT_LOG_SIZE];
d3d59da
+	int audit_fd, audit_ok;
3f55133
+	const struct sshcipher *cipher = cipher_by_name(enc);
d3d59da
+	char *s;
d3d59da
+
1900351
+	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 ",
1900351
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
d3d59da
+		(intmax_t)pid, (intmax_t)uid,
def1deb
+		ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))), ssh_local_port(ssh));
84822b5
+	free(s);
d3d59da
+	audit_fd = audit_open();
d3d59da
+	if (audit_fd < 0) {
d3d59da
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
d3d59da
+					 errno == EAFNOSUPPORT)
d3d59da
+			return; /* No audit support in kernel */
28ce052
+		else
d3d59da
+			fatal("cannot open audit"); /* Must prevent login */
d3d59da
+	}
d3d59da
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
def1deb
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 1);
d3d59da
+	audit_close(audit_fd);
d3d59da
+	/* do not abort if the error is EPERM and sshd is run as non root user */
d3d59da
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
d3d59da
+		fatal("cannot write into audit"); /* Must prevent login */
d3d59da
+#endif
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
d3d59da
+{
d3d59da
+	char buf[AUDIT_LOG_SIZE];
d3d59da
+	int audit_fd, audit_ok;
d3d59da
+	char *s;
d3d59da
+
d3d59da
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
d3d59da
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
def1deb
+		 ssh_remote_port(ssh),
def1deb
+		 (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
def1deb
+		 ssh_local_port(ssh));
84822b5
+	free(s);
d3d59da
+	audit_fd = audit_open();
d3d59da
+	if (audit_fd < 0) {
d3d59da
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
535d341
+					 errno != EAFNOSUPPORT)
535d341
+			error("cannot open audit");
535d341
+		return;
535d341
+	}
535d341
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
def1deb
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 1);
535d341
+	audit_close(audit_fd);
535d341
+	/* do not abort if the error is EPERM and sshd is run as non root user */
535d341
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
535d341
+		error("cannot write into audit");
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
d3d59da
+{
535d341
+	char buf[AUDIT_LOG_SIZE];
535d341
+	int audit_fd, audit_ok;
535d341
+
535d341
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
535d341
+		fp, (intmax_t)pid, (intmax_t)uid);
535d341
+	audit_fd = audit_open();
535d341
+	if (audit_fd < 0) {
535d341
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
535d341
+					 errno != EAFNOSUPPORT)
535d341
+			error("cannot open audit");
535d341
+		return;
535d341
+	}
535d341
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
535d341
+			buf, NULL,
def1deb
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(ssh),
535d341
+			NULL, 1);
535d341
+	audit_close(audit_fd);
535d341
+	/* do not abort if the error is EPERM and sshd is run as non root user */
535d341
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
535d341
+		error("cannot write into audit");
d3d59da
+}
535d341
 #endif /* USE_LINUX_AUDIT */
f32b842
diff -up openssh-8.6p1/auditstub.c.audit openssh-8.6p1/auditstub.c
f32b842
--- openssh-8.6p1/auditstub.c.audit	2021-04-19 16:47:35.754062114 +0200
f32b842
+++ openssh-8.6p1/auditstub.c	2021-04-19 16:47:35.754062114 +0200
def1deb
@@ -0,0 +1,52 @@
d3d59da
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
d3d59da
+
d3d59da
+/*
d3d59da
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
d3d59da
+ * Use is subject to license terms.
d3d59da
+ *
d3d59da
+ * Redistribution and use in source and binary forms, with or without
d3d59da
+ * modification, are permitted provided that the following conditions
d3d59da
+ * are met:
d3d59da
+ * 1. Redistributions of source code must retain the above copyright
d3d59da
+ *    notice, this list of conditions and the following disclaimer.
d3d59da
+ * 2. Redistributions in binary form must reproduce the above copyright
d3d59da
+ *    notice, this list of conditions and the following disclaimer in the
d3d59da
+ *    documentation and/or other materials provided with the distribution.
d3d59da
+ *
d3d59da
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
d3d59da
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d3d59da
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
d3d59da
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
d3d59da
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
d3d59da
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
d3d59da
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
d3d59da
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
d3d59da
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
d3d59da
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d3d59da
+ *
d3d59da
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
d3d59da
+ */
d3d59da
+
d3d59da
+#include <sys/types.h>
d3d59da
+
def1deb
+struct ssh;
def1deb
+
d3d59da
+void
def1deb
+audit_unsupported(struct ssh *ssh, int n)
d3d59da
+{
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
d3d59da
+{
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_session_key_free(struct ssh *ssh, int ctos)
d3d59da
+{
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
d3d59da
+{
d3d59da
+}
f32b842
diff -up openssh-8.6p1/auth2.c.audit openssh-8.6p1/auth2.c
f32b842
--- openssh-8.6p1/auth2.c.audit	2021-04-19 16:47:35.682061561 +0200
f32b842
+++ openssh-8.6p1/auth2.c	2021-04-19 16:47:35.754062114 +0200
f32b842
@@ -298,9 +298,6 @@ input_userauth_request(int type, u_int32
535d341
 		} else {
17b491b
 			/* Invalid user, fake password information */
535d341
 			authctxt->pw = fakepw();
414bfae
-#ifdef SSH_AUDIT_EVENTS
def1deb
-			PRIVSEP(audit_event(ssh, SSH_INVALID_USER));
535d341
-#endif
535d341
 		}
535d341
 #ifdef USE_PAM
535d341
 		if (options.use_pam)
f32b842
diff -up openssh-8.6p1/auth2-hostbased.c.audit openssh-8.6p1/auth2-hostbased.c
f32b842
--- openssh-8.6p1/auth2-hostbased.c.audit	2021-04-19 16:47:35.656061361 +0200
f32b842
+++ openssh-8.6p1/auth2-hostbased.c	2021-04-19 16:47:35.754062114 +0200
def1deb
@@ -158,7 +158,7 @@ userauth_hostbased(struct ssh *ssh)
d3d59da
 	authenticated = 0;
def1deb
 	if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
def1deb
 	    chost, key)) &&
5b55d09
-	    PRIVSEP(sshkey_verify(key, sig, slen,
def1deb
+	    PRIVSEP(hostbased_key_verify(ssh, key, sig, slen,
51f5c1c
 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
d3d59da
 		authenticated = 1;
5b55d09
 
51f5c1c
@@ -175,6 +175,20 @@ done:
d3d59da
 	return authenticated;
d3d59da
 }
d3d59da
 
d3d59da
+int
def1deb
+hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
51f5c1c
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat,
51f5c1c
+    struct sshkey_sig_details **detailsp)
d3d59da
+{
d3d59da
+	int rv;
d3d59da
+
51f5c1c
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat, detailsp);
d3d59da
+#ifdef SSH_AUDIT_EVENTS
def1deb
+	audit_key(ssh, 0, &rv, key);
d3d59da
+#endif
d3d59da
+	return rv;
d3d59da
+}
d3d59da
+
d3d59da
 /* return 1 if given hostkey is allowed */
d3d59da
 int
def1deb
 hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
f32b842
diff -up openssh-8.6p1/auth2-pubkey.c.audit openssh-8.6p1/auth2-pubkey.c
f32b842
--- openssh-8.6p1/auth2-pubkey.c.audit	2021-04-19 16:47:35.726061899 +0200
f32b842
+++ openssh-8.6p1/auth2-pubkey.c	2021-04-19 16:47:35.754062114 +0200
f32b842
@@ -213,7 +213,7 @@ userauth_pubkey(struct ssh *ssh)
d3d59da
 		/* test for correct signature */
d3d59da
 		authenticated = 0;
3cd4899
 		if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
bbf61da
-		    PRIVSEP(sshkey_verify(key, sig, slen,
def1deb
+		    PRIVSEP(user_key_verify(ssh, key, sig, slen,
bbf61da
 		    sshbuf_ptr(b), sshbuf_len(b),
bbf61da
 		    (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
51f5c1c
 		    ssh->compat, &sig_details)) == 0) {
f32b842
@@ -305,6 +305,20 @@ done:
5b55d09
 	return authenticated;
d3d59da
 }
d3d59da
 
d3d59da
+int
def1deb
+user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
51f5c1c
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat,
51f5c1c
+    struct sshkey_sig_details **detailsp)
d3d59da
+{
d3d59da
+	int rv;
d3d59da
+
51f5c1c
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat, detailsp);
d3d59da
+#ifdef SSH_AUDIT_EVENTS
def1deb
+	audit_key(ssh, 1, &rv, key);
d3d59da
+#endif
d3d59da
+	return rv;
d3d59da
+}
d3d59da
+
5b55d09
 static int
5b55d09
 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
5b55d09
 {
f32b842
diff -up openssh-8.6p1/auth.c.audit openssh-8.6p1/auth.c
f32b842
--- openssh-8.6p1/auth.c.audit	2021-04-19 16:47:35.681061553 +0200
f32b842
+++ openssh-8.6p1/auth.c	2021-04-19 16:47:35.754062114 +0200
f32b842
@@ -367,7 +367,7 @@ auth_log(struct ssh *ssh, int authentica
c45ece5
 # endif
c45ece5
 #endif
c45ece5
 #ifdef SSH_AUDIT_EVENTS
c45ece5
-	if (authenticated == 0 && !authctxt->postponed)
c45ece5
+	if (authenticated == 0 && !authctxt->postponed && !partial)
def1deb
 		audit_event(ssh, audit_classify_auth(method));
c45ece5
 #endif
c45ece5
 }
f32b842
@@ -597,9 +597,6 @@ getpwnamallow(struct ssh *ssh, const cha
def1deb
 		record_failed_login(ssh, user,
5878ebb
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
535d341
 #endif
d3d59da
-#ifdef SSH_AUDIT_EVENTS
def1deb
-		audit_event(ssh, SSH_INVALID_USER);
535d341
-#endif /* SSH_AUDIT_EVENTS */
535d341
 		return (NULL);
535d341
 	}
def1deb
 	if (!allowed_user(ssh, pw))
f32b842
diff -up openssh-8.6p1/auth.h.audit openssh-8.6p1/auth.h
f32b842
--- openssh-8.6p1/auth.h.audit	2021-04-19 16:47:35.697061676 +0200
f32b842
+++ openssh-8.6p1/auth.h	2021-04-19 16:47:35.754062114 +0200
f32b842
@@ -193,6 +193,8 @@ struct passwd * getpwnamallow(struct ssh
535d341
 
535d341
 char	*expand_authorized_keys(const char *, struct passwd *pw);
535d341
 char	*authorized_principals_file(struct passwd *);
def1deb
+int	 user_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
51f5c1c
+    const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
535d341
 
535d341
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
535d341
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
f32b842
@@ -212,6 +214,8 @@ struct sshkey	*get_hostkey_private_by_ty
5b55d09
 int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
def1deb
 int	 sshd_hostkey_sign(struct ssh *, struct sshkey *, struct sshkey *,
def1deb
     u_char **, size_t *, const u_char *, size_t, const char *);
def1deb
+int	 hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
51f5c1c
+    const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
535d341
 
3cd4899
 /* Key / cert options linkage to auth layer */
3cd4899
 const struct sshauthopt *auth_options(struct ssh *);
f32b842
diff -up openssh-8.6p1/cipher.c.audit openssh-8.6p1/cipher.c
f32b842
--- openssh-8.6p1/cipher.c.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/cipher.c	2021-04-19 16:47:35.755062122 +0200
f32b842
@@ -64,25 +64,6 @@ struct sshcipher_ctx {
6cf9b8e
 	const struct sshcipher *cipher;
6cf9b8e
 };
d3d59da
 
1900351
-struct sshcipher {
9f526c6
-	char	*name;
9f526c6
-	u_int	block_size;
9f526c6
-	u_int	key_len;
9f526c6
-	u_int	iv_len;		/* defaults to block_size */
9f526c6
-	u_int	auth_len;
9f526c6
-	u_int	flags;
9f526c6
-#define CFLAG_CBC		(1<<0)
9f526c6
-#define CFLAG_CHACHAPOLY	(1<<1)
1900351
-#define CFLAG_AESCTR		(1<<2)
1900351
-#define CFLAG_NONE		(1<<3)
5b55d09
-#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
1900351
-#ifdef WITH_OPENSSL
9f526c6
-	const EVP_CIPHER	*(*evptype)(void);
1900351
-#else
1900351
-	void	*ignored;
1900351
-#endif
9f526c6
-};
9f526c6
-
1900351
 static const struct sshcipher ciphers[] = {
5b55d09
 #ifdef WITH_OPENSSL
bbf61da
 #ifndef OPENSSL_NO_DES
f32b842
@@ -422,7 +403,7 @@ cipher_get_length(struct sshcipher_ctx *
217da75
 void
217da75
 cipher_free(struct sshcipher_ctx *cc)
217da75
 {
217da75
-	if (cc == NULL)
217da75
+	if (cc == NULL || cc->cipher == NULL)
217da75
 		return;
5cd9552
 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
5cd9552
 		chachapoly_free(cc->cp_ctx);
f32b842
diff -up openssh-8.6p1/cipher.h.audit openssh-8.6p1/cipher.h
f32b842
--- openssh-8.6p1/cipher.h.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/cipher.h	2021-04-19 16:47:35.755062122 +0200
f32b842
@@ -47,7 +47,25 @@
1900351
 #define CIPHER_ENCRYPT		1
1900351
 #define CIPHER_DECRYPT		0
d3d59da
 
1900351
-struct sshcipher;
5b55d09
+struct sshcipher {
d3d59da
+	char	*name;
d3d59da
+	u_int	block_size;
d3d59da
+	u_int	key_len;
d3d59da
+	u_int	iv_len;		/* defaults to block_size */
d3d59da
+	u_int	auth_len;
5160c9c
+	u_int	flags;
5160c9c
+#define CFLAG_CBC		(1<<0)
5160c9c
+#define CFLAG_CHACHAPOLY	(1<<1)
1900351
+#define CFLAG_AESCTR		(1<<2)
1900351
+#define CFLAG_NONE		(1<<3)
5b55d09
+#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
1900351
+#ifdef WITH_OPENSSL
d3d59da
+	const EVP_CIPHER	*(*evptype)(void);
1900351
+#else
1900351
+	void	*ignored;
1900351
+#endif
d3d59da
+};
5b55d09
+
6cf9b8e
 struct sshcipher_ctx;
6cf9b8e
 
5b55d09
 const struct sshcipher *cipher_by_name(const char *);
f32b842
diff -up openssh-8.6p1/kex.c.audit openssh-8.6p1/kex.c
f32b842
--- openssh-8.6p1/kex.c.audit	2021-04-19 16:47:35.743062030 +0200
f32b842
+++ openssh-8.6p1/kex.c	2021-04-19 16:47:35.755062122 +0200
f32b842
@@ -65,6 +65,7 @@
132f8f8
 #include "ssherr.h"
132f8f8
 #include "sshbuf.h"
5160c9c
 #include "digest.h"
d3d59da
+#include "audit.h"
d3d59da
 
5160c9c
 #ifdef GSSAPI
5160c9c
 #include "ssh-gss.h"
f32b842
@@ -816,12 +817,16 @@ kex_start_rekex(struct ssh *ssh)
def1deb
 }
def1deb
 
def1deb
 static int
def1deb
-choose_enc(struct sshenc *enc, char *client, char *server)
def1deb
+choose_enc(struct ssh *ssh, struct sshenc *enc, char *client, char *server)
d3d59da
 {
d3d59da
 	char *name = match_list(client, server, NULL);
132f8f8
 
d3d59da
-	if (name == NULL)
d3d59da
+	if (name == NULL) {
d3d59da
+#ifdef SSH_AUDIT_EVENTS
def1deb
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_CIPHER);
d3d59da
+#endif
132f8f8
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
d3d59da
+	}
17b491b
 	if ((enc->cipher = cipher_by_name(name)) == NULL) {
25c16c6
 		error_f("unsupported cipher %s", name);
17b491b
 		free(name);
f32b842
@@ -842,8 +847,12 @@ choose_mac(struct ssh *ssh, struct sshma
d3d59da
 {
d3d59da
 	char *name = match_list(client, server, NULL);
132f8f8
 
d3d59da
-	if (name == NULL)
d3d59da
+	if (name == NULL) {
d3d59da
+#ifdef SSH_AUDIT_EVENTS
def1deb
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_MAC);
d3d59da
+#endif
132f8f8
 		return SSH_ERR_NO_MAC_ALG_MATCH;
d3d59da
+	}
17b491b
 	if (mac_setup(mac, name) < 0) {
25c16c6
 		error_f("unsupported MAC %s", name);
17b491b
 		free(name);
f32b842
@@ -856,12 +865,16 @@ choose_mac(struct ssh *ssh, struct sshma
def1deb
 }
def1deb
 
def1deb
 static int
def1deb
-choose_comp(struct sshcomp *comp, char *client, char *server)
def1deb
+choose_comp(struct ssh *ssh, struct sshcomp *comp, char *client, char *server)
d3d59da
 {
d3d59da
 	char *name = match_list(client, server, NULL);
132f8f8
 
d3d59da
-	if (name == NULL)
d3d59da
+	if (name == NULL) {
d3d59da
+#ifdef SSH_AUDIT_EVENTS
def1deb
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_COMPRESSION);
d3d59da
+#endif
132f8f8
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
d3d59da
+	}
51f5c1c
 #ifdef WITH_ZLIB
d3d59da
 	if (strcmp(name, "zlib@openssh.com") == 0) {
d3d59da
 		comp->type = COMP_DELAYED;
f32b842
@@ -1002,7 +1015,7 @@ kex_choose_conf(struct ssh *ssh)
def1deb
 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
def1deb
 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
def1deb
 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
def1deb
-		if ((r = choose_enc(&newkeys->enc, cprop[nenc],
def1deb
+		if ((r = choose_enc(ssh, &newkeys->enc, cprop[nenc],
def1deb
 		    sprop[nenc])) != 0) {
def1deb
 			kex->failed_choice = peer[nenc];
def1deb
 			peer[nenc] = NULL;
f32b842
@@ -1017,7 +1030,7 @@ kex_choose_conf(struct ssh *ssh)
def1deb
 			peer[nmac] = NULL;
def1deb
 			goto out;
def1deb
 		}
def1deb
-		if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
def1deb
+		if ((r = choose_comp(ssh, &newkeys->comp, cprop[ncomp],
def1deb
 		    sprop[ncomp])) != 0) {
def1deb
 			kex->failed_choice = peer[ncomp];
def1deb
 			peer[ncomp] = NULL;
f32b842
@@ -1040,6 +1053,10 @@ kex_choose_conf(struct ssh *ssh)
6cf9b8e
 		dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
6cf9b8e
 		dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
6cf9b8e
 		dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
1900351
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
1900351
+#ifdef SSH_AUDIT_EVENTS
def1deb
+		audit_kex(ssh, mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
1900351
+#endif
1900351
 	}
1900351
 	/* XXX need runden? */
1900351
 	kex->we_need = need;
f32b842
@@ -1297,6 +1314,36 @@ dump_digest(const char *msg, const u_cha
d3d59da
 }
d3d59da
 #endif
def1deb
 
d3d59da
+static void
132f8f8
+enc_destroy(struct sshenc *enc)
d3d59da
+{
d3d59da
+	if (enc == NULL)
d3d59da
+		return;
d3d59da
+
d3d59da
+	if (enc->key) {
d3d59da
+		memset(enc->key, 0, enc->key_len);
84822b5
+		free(enc->key);
d3d59da
+	}
d3d59da
+
d3d59da
+	if (enc->iv) {
13073f8
+		memset(enc->iv,  0, enc->iv_len);
84822b5
+		free(enc->iv);
d3d59da
+	}
d3d59da
+
d3d59da
+	memset(enc, 0, sizeof(*enc));
d3d59da
+}
d3d59da
+
d3d59da
+void
132f8f8
+newkeys_destroy(struct newkeys *newkeys)
d3d59da
+{
d3d59da
+	if (newkeys == NULL)
d3d59da
+		return;
d3d59da
+
d3d59da
+	enc_destroy(&newkeys->enc);
d3d59da
+	mac_destroy(&newkeys->mac);
d3d59da
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
d3d59da
+}
def1deb
+
def1deb
 /*
def1deb
  * Send a plaintext error message to the peer, suffixed by \r\n.
def1deb
  * Only used during banner exchange, and there only for the server.
f32b842
diff -up openssh-8.6p1/kex.h.audit openssh-8.6p1/kex.h
f32b842
--- openssh-8.6p1/kex.h.audit	2021-04-19 16:47:35.683061568 +0200
f32b842
+++ openssh-8.6p1/kex.h	2021-04-19 16:47:35.756062129 +0200
def1deb
@@ -226,6 +226,8 @@ int	 kexgss_client(struct ssh *);
132f8f8
 int	 kexgss_server(struct ssh *);
5160c9c
 #endif
d3d59da
 
132f8f8
+void	newkeys_destroy(struct newkeys *newkeys);
d3d59da
+
def1deb
 int	 kex_dh_keypair(struct kex *);
def1deb
 int	 kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
def1deb
     struct sshbuf **);
f32b842
diff -up openssh-8.6p1/mac.c.audit openssh-8.6p1/mac.c
f32b842
--- openssh-8.6p1/mac.c.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/mac.c	2021-04-19 16:47:35.756062129 +0200
f32b842
@@ -239,6 +239,20 @@ mac_clear(struct sshmac *mac)
d3d59da
 	mac->umac_ctx = NULL;
d3d59da
 }
d3d59da
 
d3d59da
+void
132f8f8
+mac_destroy(struct sshmac *mac)
d3d59da
+{
d3d59da
+	if (mac == NULL)
d3d59da
+		return;
d3d59da
+
d3d59da
+	if (mac->key) {
d3d59da
+		memset(mac->key, 0, mac->key_len);
84822b5
+		free(mac->key);
d3d59da
+	}
d3d59da
+
d3d59da
+	memset(mac, 0, sizeof(*mac));
d3d59da
+}
d3d59da
+
d3d59da
 /* XXX copied from ciphers_valid */
d3d59da
 #define	MAC_SEP	","
d3d59da
 int
f32b842
diff -up openssh-8.6p1/mac.h.audit openssh-8.6p1/mac.h
f32b842
--- openssh-8.6p1/mac.h.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/mac.h	2021-04-19 16:47:35.756062129 +0200
b487a6d
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
5878ebb
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
5878ebb
     const u_char *, size_t);
132f8f8
 void	 mac_clear(struct sshmac *);
132f8f8
+void	 mac_destroy(struct sshmac *);
132f8f8
 
132f8f8
 #endif /* SSHMAC_H */
f32b842
diff -up openssh-8.6p1/Makefile.in.audit openssh-8.6p1/Makefile.in
f32b842
--- openssh-8.6p1/Makefile.in.audit	2021-04-19 16:47:35.731061937 +0200
f32b842
+++ openssh-8.6p1/Makefile.in	2021-04-19 16:47:35.756062129 +0200
f32b842
@@ -112,7 +112,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
25c16c6
 	kexsntrup761x25519.o sntrup761.o kexgen.o \
def1deb
 	kexgssc.o \
f32b842
 	sftp-realpath.o platform-pledge.o platform-tracing.o platform-misc.o \
51f5c1c
-	sshbuf-io.o
51f5c1c
+	sshbuf-io.o auditstub.o
535d341
 
51f5c1c
 SKOBJS=	ssh-sk-client.o
def1deb
 
f32b842
diff -up openssh-8.6p1/monitor.c.audit openssh-8.6p1/monitor.c
f32b842
--- openssh-8.6p1/monitor.c.audit	2021-04-19 16:47:35.707061753 +0200
f32b842
+++ openssh-8.6p1/monitor.c	2021-04-19 16:47:35.756062129 +0200
def1deb
@@ -93,6 +93,7 @@
13073f8
 #include "compat.h"
5160c9c
 #include "ssh2.h"
84822b5
 #include "authfd.h"
d3d59da
+#include "audit.h"
132f8f8
 #include "match.h"
132f8f8
 #include "ssherr.h"
51f5c1c
 #include "sk-api.h"
f32b842
@@ -107,6 +108,8 @@ extern u_int utmp_len;
bbf61da
 extern struct sshbuf *loginmsg;
3cd4899
 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
d3d59da
 
def1deb
+extern void destroy_sensitive_data(struct ssh *, int);
d3d59da
+
d3d59da
 /* State exported from the child */
132f8f8
 static struct sshbuf *child_state;
d3d59da
 
def1deb
@@ -157,6 +160,11 @@ int mm_answer_gss_updatecreds(struct ssh
d3d59da
 #ifdef SSH_AUDIT_EVENTS
def1deb
 int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
def1deb
 int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
def1deb
+int mm_answer_audit_end_command(struct ssh *, int, struct sshbuf *);
def1deb
+int mm_answer_audit_unsupported_body(struct ssh *, int, struct sshbuf *);
def1deb
+int mm_answer_audit_kex_body(struct ssh *, int, struct sshbuf *);
def1deb
+int mm_answer_audit_session_key_free_body(struct ssh *, int, struct sshbuf *);
def1deb
+int mm_answer_audit_server_key_free(struct ssh *, int, struct sshbuf *);
d3d59da
 #endif
d3d59da
 
def1deb
 static Authctxt *authctxt;
def1deb
@@ -215,6 +223,10 @@ struct mon_table mon_dispatch_proto20[]
d3d59da
 #endif
d3d59da
 #ifdef SSH_AUDIT_EVENTS
d3d59da
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
d3d59da
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
d3d59da
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
d3d59da
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
d3d59da
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
d3d59da
 #endif
d3d59da
 #ifdef BSD_AUTH
d3d59da
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
def1deb
@@ -249,6 +261,11 @@ struct mon_table mon_dispatch_postauth20
d3d59da
 #ifdef SSH_AUDIT_EVENTS
d3d59da
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
d3d59da
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
d3d59da
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
d3d59da
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
d3d59da
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
d3d59da
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
d3d59da
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
d3d59da
 #endif
d3d59da
     {0, 0, NULL}
d3d59da
 };
f32b842
@@ -1444,8 +1461,10 @@ mm_answer_keyverify(struct ssh *ssh, int
bd35168
 	int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
bd35168
 	int encoded_ret;
51f5c1c
 	struct sshkey_sig_details *sig_details = NULL;
d3d59da
+	int type = 0;
d3d59da
 
51f5c1c
-	if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
bbf61da
+	if ((r = sshbuf_get_u32(m, &type)) != 0 ||
51f5c1c
+	    (r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
51f5c1c
 	    (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
51f5c1c
 	    (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
bbf61da
 	    (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
f32b842
@@ -1454,6 +1473,8 @@ mm_answer_keyverify(struct ssh *ssh, int
d3d59da
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
d3d59da
 	  !monitor_allowed_key(blob, bloblen))
25c16c6
 		fatal_f("bad key, not previously allowed");
d3d59da
+	if (type != key_blobtype)
25c16c6
+		fatal_f("bad key type");
d3d59da
 
3cd4899
 	/* Empty signature algorithm means NULL. */
3cd4899
 	if (*sigalg == '\0') {
f32b842
@@ -1469,14 +1490,19 @@ mm_answer_keyverify(struct ssh *ssh, int
5b55d09
 	case MM_USERKEY:
25c16c6
 		valid_data = monitor_valid_userblob(ssh, data, datalen);
5b55d09
 		auth_method = "publickey";
def1deb
+		ret = user_key_verify(ssh, key, signature, signaturelen, data,
51f5c1c
+		    datalen, sigalg, ssh->compat, &sig_details);
5b55d09
 		break;
5b55d09
 	case MM_HOSTKEY:
5b55d09
 		valid_data = monitor_valid_hostbasedblob(data, datalen,
5b55d09
 		    hostbased_cuser, hostbased_chost);
5b55d09
 		auth_method = "hostbased";
51f5c1c
+		ret = hostbased_key_verify(ssh, key, signature, signaturelen, data,
51f5c1c
+		    datalen, sigalg, ssh->compat, &sig_details);
5b55d09
 		break;
5b55d09
 	default:
5b55d09
 		valid_data = 0;
5b55d09
+		ret = 0;
5b55d09
 		break;
5b55d09
 	}
d3d59da
 	if (!valid_data)
f32b842
@@ -1488,8 +1514,6 @@ mm_answer_keyverify(struct ssh *ssh, int
51f5c1c
 	    SSH_FP_DEFAULT)) == NULL)
25c16c6
 		fatal_f("sshkey_fingerprint failed");
51f5c1c
 
5b55d09
-	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
51f5c1c
-	    sigalg, ssh->compat, &sig_details);
25c16c6
 	debug3_f("%s %p signature %s%s%s", auth_method, key,
51f5c1c
 	    (ret == 0) ? "verified" : "unverified",
51f5c1c
 	    (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
f32b842
@@ -1576,13 +1600,19 @@ mm_record_login(struct ssh *ssh, Session
def1deb
 }
def1deb
 
def1deb
 static void
def1deb
-mm_session_close(Session *s)
def1deb
+mm_session_close(struct ssh *ssh, Session *s)
def1deb
 {
25c16c6
 	debug3_f("session %d pid %ld", s->self, (long)s->pid);
def1deb
 	if (s->ttyfd != -1) {
25c16c6
 		debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
d3d59da
 		session_pty_cleanup2(s);
d3d59da
 	}
d3d59da
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+	if (s->command != NULL) {
25c16c6
+		debug3_f("command %d", s->command_handle);
def1deb
+		session_end_command2(ssh, s);
d3d59da
+	}
d3d59da
+#endif
d3d59da
 	session_unused(s->self);
d3d59da
 }
d3d59da
 
f32b842
@@ -1649,7 +1679,7 @@ mm_answer_pty(struct ssh *ssh, int sock,
def1deb
 
def1deb
  error:
def1deb
 	if (s != NULL)
def1deb
-		mm_session_close(s);
def1deb
+		mm_session_close(ssh, s);
def1deb
 	if ((r = sshbuf_put_u32(m, 0)) != 0)
25c16c6
 		fatal_fr(r, "assemble 0");
def1deb
 	mm_request_send(sock, MONITOR_ANS_PTY, m);
f32b842
@@ -1668,7 +1698,7 @@ mm_answer_pty_cleanup(struct ssh *ssh, i
def1deb
 	if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
25c16c6
 		fatal_fr(r, "parse tty");
def1deb
 	if ((s = session_by_tty(tty)) != NULL)
def1deb
-		mm_session_close(s);
def1deb
+		mm_session_close(ssh, s);
def1deb
 	sshbuf_reset(m);
def1deb
 	free(tty);
def1deb
 	return (0);
f32b842
@@ -1690,6 +1720,8 @@ mm_answer_term(struct ssh *ssh, int sock
d3d59da
 		sshpam_cleanup();
d3d59da
 #endif
d3d59da
 
def1deb
+	destroy_sensitive_data(ssh, 0);
d3d59da
+
d3d59da
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
d3d59da
 		if (errno != EINTR)
d3d59da
 			exit(1);
f32b842
@@ -1736,12 +1768,47 @@ mm_answer_audit_command(struct ssh *ssh,
d3d59da
 {
d3d59da
 	char *cmd;
bbf61da
 	int r;
d3d59da
+	Session *s;
d3d59da
 
d3d59da
 	debug3("%s entering", __func__);
bbf61da
 	if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
bbf61da
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d3d59da
+
d3d59da
 	/* sanity check command, if so how? */
d3d59da
-	audit_run_command(cmd);
d3d59da
+	s = session_new();
d3d59da
+	if (s == NULL)
25c16c6
+		fatal_f("error allocating a session");
d3d59da
+	s->command = cmd;
739842b
+#ifdef SSH_AUDIT_EVENTS
def1deb
+	s->command_handle = audit_run_command(ssh, cmd);
739842b
+#endif
d3d59da
+
bbf61da
+	sshbuf_reset(m);
bbf61da
+	sshbuf_put_u32(m, s->self);
d3d59da
+
d3d59da
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
d3d59da
+
d3d59da
+	return (0);
d3d59da
+}
d3d59da
+
d3d59da
+int
def1deb
+mm_answer_audit_end_command(struct ssh *ssh, int socket, struct sshbuf *m)
d3d59da
+{
bbf61da
+	int handle, r;
bbf61da
+	size_t len;
bbf61da
+	u_char *cmd = NULL;
d3d59da
+	Session *s;
d3d59da
+
25c16c6
+	debug3_f("entering");
bbf61da
+	if ((r = sshbuf_get_u32(m, &handle)) != 0 ||
bbf61da
+	    (r = sshbuf_get_string(m, &cmd, &len)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
d3d59da
+
d3d59da
+	s = session_by_id(handle);
d3d59da
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
d3d59da
+	    strcmp(s->command, cmd) != 0)
25c16c6
+		fatal_f("invalid handle");
def1deb
+	mm_session_close(ssh, s);
84822b5
 	free(cmd);
d3d59da
 	return (0);
d3d59da
 }
f32b842
@@ -1813,6 +1880,7 @@ monitor_apply_keystate(struct ssh *ssh,
132f8f8
 void
def1deb
 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
132f8f8
 {
bbf61da
+	struct sshbuf *m;
25c16c6
 	debug3_f("Waiting for new keys");
d3d59da
 
132f8f8
 	if ((child_state = sshbuf_new()) == NULL)
f32b842
@@ -1820,6 +1888,19 @@ mm_get_keystate(struct ssh *ssh, struct
132f8f8
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
132f8f8
 	    child_state);
25c16c6
 	debug3_f("GOT new keys");
d3d59da
+
d3d59da
+#ifdef SSH_AUDIT_EVENTS
bbf61da
+	m = sshbuf_new();
5b55d09
+	mm_request_receive_expect(pmonitor->m_sendfd,
bbf61da
+				  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
def1deb
+	mm_answer_audit_session_key_free_body(ssh, pmonitor->m_sendfd, m);
bbf61da
+	sshbuf_free(m);
d3d59da
+#endif
d3d59da
+
d3d59da
+	/* Drain any buffered messages from the child */
d3d59da
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
d3d59da
+		;
d3d59da
+
d3d59da
 }
d3d59da
 
d3d59da
 
f32b842
@@ -2111,3 +2192,102 @@ mm_answer_gss_updatecreds(struct ssh *ss
5160c9c
 
5160c9c
 #endif /* GSSAPI */
d3d59da
 
d3d59da
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+int
def1deb
+mm_answer_audit_unsupported_body(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59da
+{
bbf61da
+	int what, r;
d3d59da
+
bbf61da
+	if ((r = sshbuf_get_u32(m, &what)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
d3d59da
+
def1deb
+	audit_unsupported_body(ssh, what);
d3d59da
+
bbf61da
+	sshbuf_reset(m);
d3d59da
+
d3d59da
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
d3d59da
+	return 0;
d3d59da
+}
d3d59da
+
d3d59da
+int
def1deb
+mm_answer_audit_kex_body(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59da
+{
bbf61da
+	int ctos, r;
1900351
+	char *cipher, *mac, *compress, *pfs;
bbf61da
+	u_int64_t tmp;
d3d59da
+	pid_t pid;
d3d59da
+	uid_t uid;
d3d59da
+
bbf61da
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
bbf61da
+	    (r = sshbuf_get_cstring(m, &cipher, NULL)) != 0 ||
bbf61da
+	    (r = sshbuf_get_cstring(m, &mac, NULL)) != 0 ||
bbf61da
+	    (r = sshbuf_get_cstring(m, &compress, NULL)) != 0 ||
bbf61da
+	    (r = sshbuf_get_cstring(m, &pfs, NULL)) != 0 ||
bbf61da
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+	pid = (pid_t) tmp;
bbf61da
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+	uid = (pid_t) tmp;
d3d59da
+
def1deb
+	audit_kex_body(ssh, ctos, cipher, mac, compress, pfs, pid, uid);
d3d59da
+
84822b5
+	free(cipher);
84822b5
+	free(mac);
84822b5
+	free(compress);
1900351
+	free(pfs);
bbf61da
+	sshbuf_reset(m);
d3d59da
+
d3d59da
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
d3d59da
+	return 0;
d3d59da
+}
d3d59da
+
d3d59da
+int
def1deb
+mm_answer_audit_session_key_free_body(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59da
+{
bbf61da
+	int ctos, r;
bbf61da
+	u_int64_t tmp;
d3d59da
+	pid_t pid;
d3d59da
+	uid_t uid;
d3d59da
+
bbf61da
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
bbf61da
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+	pid = (pid_t) tmp;
bbf61da
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+	uid = (uid_t) tmp;
d3d59da
+
def1deb
+	audit_session_key_free_body(ssh, ctos, pid, uid);
d3d59da
+
bbf61da
+	sshbuf_reset(m);
d3d59da
+
d3d59da
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
d3d59da
+	return 0;
d3d59da
+}
d3d59da
+
d3d59da
+int
def1deb
+mm_answer_audit_server_key_free(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59da
+{
bbf61da
+	size_t len, r;
d3d59da
+	char *fp;
bbf61da
+	u_int64_t tmp;
d3d59da
+	pid_t pid;
d3d59da
+	uid_t uid;
d3d59da
+
bbf61da
+	if ((r = sshbuf_get_cstring(m, &fp, &len)) != 0 ||
bbf61da
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+	pid = (pid_t) tmp;
bbf61da
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+	uid = (uid_t) tmp;
d3d59da
+
def1deb
+	audit_destroy_sensitive_data(ssh, fp, pid, uid);
d3d59da
+
84822b5
+	free(fp);
bbf61da
+	sshbuf_reset(m);
d3d59da
+
d3d59da
+	return 0;
d3d59da
+}
d3d59da
+#endif /* SSH_AUDIT_EVENTS */
f32b842
diff -up openssh-8.6p1/monitor.h.audit openssh-8.6p1/monitor.h
f32b842
--- openssh-8.6p1/monitor.h.audit	2021-04-19 16:47:35.707061753 +0200
f32b842
+++ openssh-8.6p1/monitor.h	2021-04-19 16:47:35.757062137 +0200
def1deb
@@ -65,7 +65,13 @@ enum monitor_reqtype {
d3d59da
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
d3d59da
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
d3d59da
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
d3d59da
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
d3d59da
+	MONITOR_REQ_AUDIT_EVENT = 112,
d3d59da
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
d3d59da
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
d3d59da
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
d3d59da
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
d3d59da
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
def1deb
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124,
def1deb
 
def1deb
 	MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
def1deb
 	MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
f32b842
diff -up openssh-8.6p1/monitor_wrap.c.audit openssh-8.6p1/monitor_wrap.c
f32b842
--- openssh-8.6p1/monitor_wrap.c.audit	2021-04-19 16:47:35.685061584 +0200
f32b842
+++ openssh-8.6p1/monitor_wrap.c	2021-04-19 16:47:35.757062137 +0200
f32b842
@@ -520,7 +520,7 @@ mm_key_allowed(enum mm_keytype type, con
d3d59da
  */
d3d59da
 
d3d59da
 int
5b55d09
-mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
5b55d09
+mm_sshkey_verify(enum mm_keytype type, const struct sshkey *key, const u_char *sig, size_t siglen,
51f5c1c
     const u_char *data, size_t datalen, const char *sigalg, u_int compat,
51f5c1c
     struct sshkey_sig_details **sig_detailsp)
d3d59da
 {
f32b842
@@ -536,7 +536,8 @@ mm_sshkey_verify(const struct sshkey *ke
51f5c1c
 		*sig_detailsp = NULL;
bbf61da
 	if ((m = sshbuf_new()) == NULL)
25c16c6
 		fatal_f("sshbuf_new failed");
bbf61da
-	if ((r = sshkey_puts(key, m)) != 0 ||
bbf61da
+	if ((r = sshbuf_put_u32(m, type)) != 0 ||
bbf61da
+	    (r = sshkey_puts(key, m)) != 0 ||
bbf61da
 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
bbf61da
 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
bbf61da
 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
f32b842
@@ -569,6 +570,22 @@ mm_sshkey_verify(const struct sshkey *ke
5b55d09
 	return 0;
d3d59da
 }
d3d59da
 
d3d59da
+int
def1deb
+mm_hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
51f5c1c
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat,
51f5c1c
+    struct sshkey_sig_details **detailsp)
d3d59da
+{
51f5c1c
+	return mm_sshkey_verify(MM_HOSTKEY, key, sig, siglen, data, datalen, pkalg, compat, detailsp);
d3d59da
+}
d3d59da
+
d3d59da
+int
def1deb
+mm_user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
51f5c1c
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat,
51f5c1c
+    struct sshkey_sig_details **detailsp)
d3d59da
+{
51f5c1c
+	return mm_sshkey_verify(MM_USERKEY, key, sig, siglen, data, datalen, pkalg, compat, detailsp);
d3d59da
+}
d3d59da
+
132f8f8
 void
def1deb
 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
132f8f8
 {
f32b842
@@ -921,11 +938,12 @@ mm_audit_event(struct ssh *ssh, ssh_audi
bbf61da
 	sshbuf_free(m);
d3d59da
 }
d3d59da
 
d3d59da
-void
def1deb
-mm_audit_run_command(const char *command)
d3d59da
+int
def1deb
+mm_audit_run_command(struct ssh *ssh, const char *command)
d3d59da
 {
bbf61da
 	struct sshbuf *m;
bbf61da
 	int r;
d3d59da
+	int handle;
d3d59da
 
d3d59da
 	debug3("%s entering command %s", __func__, command);
d3d59da
 
f32b842
@@ -935,6 +953,30 @@ mm_audit_run_command(const char *command
bbf61da
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d3d59da
 
bbf61da
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
bbf61da
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, m);
d3d59da
+
bbf61da
+	if ((r = sshbuf_get_u32(m, &handle)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+	sshbuf_free(m);
d3d59da
+
d3d59da
+	return (handle);
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+mm_audit_end_command(struct ssh *ssh, int handle, const char *command)
d3d59da
+{
bbf61da
+	int r;
bbf61da
+	struct sshbuf *m;
d3d59da
+
25c16c6
+	debug3_f("entering command %s", command);
d3d59da
+
bbf61da
+ 	if ((m = sshbuf_new()) == NULL)
25c16c6
+ 		fatal_f("sshbuf_new failed");
bbf61da
+	if ((r = sshbuf_put_u32(m, handle)) != 0 ||
bbf61da
+	    (r = sshbuf_put_cstring(m, command)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
d3d59da
+
bbf61da
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, m);
bbf61da
 	sshbuf_free(m);
d3d59da
 }
d3d59da
 #endif /* SSH_AUDIT_EVENTS */
f32b842
@@ -1095,3 +1137,83 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
bbf61da
 }
def1deb
 
5160c9c
 #endif /* GSSAPI */
d3d59da
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+void
def1deb
+mm_audit_unsupported_body(struct ssh *ssh, int what)
d3d59da
+{
bbf61da
+	int r;
bbf61da
+	struct sshbuf *m;
d3d59da
+
bbf61da
+ 	if ((m = sshbuf_new()) == NULL)
25c16c6
+ 		fatal_f("sshbuf_new failed");
bbf61da
+	if ((r = sshbuf_put_u32(m, what)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
d3d59da
+
bbf61da
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, m);
d3d59da
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
bbf61da
+				  m);
d3d59da
+
bbf61da
+	sshbuf_free(m);
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+mm_audit_kex_body(struct ssh *ssh, int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
d3d59da
+		  uid_t uid)
d3d59da
+{
bbf61da
+	int r;
bbf61da
+	struct sshbuf *m;
bbf61da
+
bbf61da
+ 	if ((m = sshbuf_new()) == NULL)
25c16c6
+ 		fatal_f("sshbuf_new failed");
bbf61da
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
bbf61da
+	    (r = sshbuf_put_cstring(m, cipher)) != 0 ||
bbf61da
+	    (r = sshbuf_put_cstring(m, (mac ? mac : "<implicit>"))) != 0 ||
bbf61da
+	    (r = sshbuf_put_cstring(m, compress)) != 0 ||
bbf61da
+	    (r = sshbuf_put_cstring(m, fps)) != 0 ||
bbf61da
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
bbf61da
+	    (r = sshbuf_put_u64(m, uid)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+
bbf61da
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, m);
d3d59da
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
bbf61da
+				  m);
d3d59da
+
bbf61da
+	sshbuf_free(m);
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+mm_audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
d3d59da
+{
bbf61da
+	int r;
bbf61da
+	struct sshbuf *m;
d3d59da
+
bbf61da
+ 	if ((m = sshbuf_new()) == NULL)
25c16c6
+ 		fatal_f("sshbuf_new failed");
bbf61da
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
bbf61da
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
bbf61da
+	    (r = sshbuf_put_u64(m, uid)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+
bbf61da
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
d3d59da
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
bbf61da
+				  m);
bbf61da
+	sshbuf_free(m);
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+mm_audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
d3d59da
+{
bbf61da
+	int r;
bbf61da
+	struct sshbuf *m;
bbf61da
+
bbf61da
+ 	if ((m = sshbuf_new()) == NULL)
25c16c6
+ 		fatal_f("sshbuf_new failed");
bbf61da
+	if ((r = sshbuf_put_cstring(m, fp)) != 0 ||
bbf61da
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
bbf61da
+	    (r = sshbuf_put_u64(m, uid)) != 0)
25c16c6
+		fatal_fr(r, "buffer error");
bbf61da
+
bbf61da
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, m);
bbf61da
+	sshbuf_free(m);
d3d59da
+}
d3d59da
+#endif /* SSH_AUDIT_EVENTS */
f32b842
diff -up openssh-8.6p1/monitor_wrap.h.audit openssh-8.6p1/monitor_wrap.h
f32b842
--- openssh-8.6p1/monitor_wrap.h.audit	2021-04-19 16:47:35.685061584 +0200
f32b842
+++ openssh-8.6p1/monitor_wrap.h	2021-04-19 16:47:35.757062137 +0200
f32b842
@@ -61,7 +61,9 @@ int mm_user_key_allowed(struct ssh *, st
3cd4899
     struct sshauthopt **);
def1deb
 int mm_hostbased_key_allowed(struct ssh *, struct passwd *, const char *,
5b55d09
     const char *, struct sshkey *);
5b55d09
-int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
def1deb
+int mm_hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
51f5c1c
+    const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
def1deb
+int mm_user_key_verify(struct ssh*, const struct sshkey *, const u_char *, size_t,
51f5c1c
     const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
6cf9b8e
 
6cf9b8e
 #ifdef GSSAPI
f32b842
@@ -86,7 +88,12 @@ void mm_sshpam_free_ctx(void *);
d3d59da
 #ifdef SSH_AUDIT_EVENTS
d3d59da
 #include "audit.h"
def1deb
 void mm_audit_event(struct ssh *, ssh_audit_event_t);
d3d59da
-void mm_audit_run_command(const char *);
def1deb
+int mm_audit_run_command(struct ssh *ssh, const char *);
def1deb
+void mm_audit_end_command(struct ssh *ssh, int, const char *);
def1deb
+void mm_audit_unsupported_body(struct ssh *, int);
def1deb
+void mm_audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
def1deb
+void mm_audit_session_key_free_body(struct ssh *, int, pid_t, uid_t);
def1deb
+void mm_audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
d3d59da
 #endif
d3d59da
 
d3d59da
 struct Session;
f32b842
diff -up openssh-8.6p1/packet.c.audit openssh-8.6p1/packet.c
f32b842
--- openssh-8.6p1/packet.c.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/packet.c	2021-04-19 16:48:46.885608837 +0200
f32b842
@@ -81,6 +81,7 @@
51f5c1c
 #endif
d3d59da
 
d3d59da
 #include "xmalloc.h"
d3d59da
+#include "audit.h"
132f8f8
 #include "compat.h"
5b55d09
 #include "ssh2.h"
36fef56
 #include "cipher.h"
f32b842
@@ -506,6 +507,13 @@ ssh_packet_get_connection_out(struct ssh
132f8f8
 	return ssh->state->connection_out;
d3d59da
 }
d3d59da
 
d3d59da
+static int
d3d59da
+packet_state_has_keys (const struct session_state *state)
d3d59da
+{
d3d59da
+	return state != NULL &&
d3d59da
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
d3d59da
+}
d3d59da
+
132f8f8
 /*
132f8f8
  * Returns the IP-address of the remote host as a string.  The returned
132f8f8
  * string must not be freed.
f32b842
@@ -583,22 +591,19 @@ ssh_packet_close_internal(struct ssh *ss
5b55d09
 {
5b55d09
 	struct session_state *state = ssh->state;
5b55d09
 	u_int mode;
5b55d09
+	u_int had_keys = packet_state_has_keys(state);
5b55d09
 
132f8f8
 	if (!state->initialized)
d3d59da
 		return;
132f8f8
 	state->initialized = 0;
5b55d09
-	if (do_close) {
5b55d09
-		if (state->connection_in == state->connection_out) {
5b55d09
-			close(state->connection_out);
5b55d09
-		} else {
5b55d09
-			close(state->connection_in);
5b55d09
-			close(state->connection_out);
5b55d09
-		}
d3d59da
-	}
132f8f8
 	sshbuf_free(state->input);
cdc735a
+	state->input = NULL;
132f8f8
 	sshbuf_free(state->output);
cdc735a
+	state->output = NULL;
132f8f8
 	sshbuf_free(state->outgoing_packet);
cdc735a
+	state->outgoing_packet = NULL;
def1deb
 	sshbuf_free(state->incoming_packet);
cdc735a
+	state->incoming_packet = NULL;
cdc735a
 	for (mode = 0; mode < MODE_MAX; mode++) {
cdc735a
 		kex_free_newkeys(state->newkeys[mode]);	/* current keys */
cdc735a
 		state->newkeys[mode] = NULL;
f32b842
@@ -634,8 +639,18 @@ ssh_packet_close_internal(struct ssh *ss
51f5c1c
 #endif	/* WITH_ZLIB */
5b55d09
 	cipher_free(state->send_context);
5b55d09
 	cipher_free(state->receive_context);
cdc735a
+	if (had_keys && state->server_side) {
cdc735a
+		/* Assuming this is called only from privsep child */
def1deb
+		audit_session_key_free(ssh, MODE_MAX);
cdc735a
+	}
6cf9b8e
 	state->send_context = state->receive_context = NULL;
5b55d09
 	if (do_close) {
5b55d09
+		if (state->connection_in == state->connection_out) {
5b55d09
+			close(state->connection_out);
5b55d09
+		} else {
5b55d09
+			close(state->connection_in);
5b55d09
+			close(state->connection_out);
5b55d09
+		}
bbf61da
 		free(ssh->local_ipaddr);
bbf61da
 		ssh->local_ipaddr = NULL;
5b55d09
 		free(ssh->remote_ipaddr);
f32b842
@@ -892,6 +907,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
f32b842
 		    (unsigned long long)state->p_send.bytes,
f32b842
 		    (unsigned long long)state->p_send.blocks);
5b55d09
 		kex_free_newkeys(state->newkeys[mode]);
def1deb
+		audit_session_key_free(ssh, mode);
def1deb
 		state->newkeys[mode] = NULL;
def1deb
 	}
def1deb
 	/* note that both bytes and the seqnr are not reset */
f32b842
@@ -2173,6 +2189,72 @@ ssh_packet_get_output(struct ssh *ssh)
132f8f8
 	return (void *)ssh->state->output;
d3d59da
 }
d3d59da
 
d3d59da
+static void
132f8f8
+newkeys_destroy_and_free(struct newkeys *newkeys)
d3d59da
+{
d3d59da
+	if (newkeys == NULL)
d3d59da
+		return;
d3d59da
+
84822b5
+	free(newkeys->enc.name);
d3d59da
+
014fe81
+	if (newkeys->mac.enabled) {
014fe81
+		mac_clear(&newkeys->mac);
84822b5
+		free(newkeys->mac.name);
014fe81
+	}
d3d59da
+
84822b5
+	free(newkeys->comp.name);
d3d59da
+
d3d59da
+	newkeys_destroy(newkeys);
84822b5
+	free(newkeys);
d3d59da
+}
d3d59da
+
d3d59da
+static void
d3d59da
+packet_destroy_state(struct session_state *state)
d3d59da
+{
d3d59da
+	if (state == NULL)
d3d59da
+		return;
d3d59da
+
6cf9b8e
+	cipher_free(state->receive_context);
6cf9b8e
+	cipher_free(state->send_context);
7f87bd9
+ 	state->send_context = state->receive_context = NULL;
d3d59da
+
bbf61da
+	sshbuf_free(state->input);
132f8f8
+	state->input = NULL;
bbf61da
+	sshbuf_free(state->output);
132f8f8
+	state->output = NULL;
bbf61da
+	sshbuf_free(state->outgoing_packet);
132f8f8
+	state->outgoing_packet = NULL;
bbf61da
+	sshbuf_free(state->incoming_packet);
132f8f8
+	state->incoming_packet = NULL;
bbf61da
+	if (state->compression_buffer) {
bbf61da
+		sshbuf_free(state->compression_buffer);
132f8f8
+		state->compression_buffer = NULL;
132f8f8
+	}
d3d59da
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
d3d59da
+	state->newkeys[MODE_IN] = NULL;
d3d59da
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
d3d59da
+	state->newkeys[MODE_OUT] = NULL;
d3d59da
+	mac_destroy(state->packet_discard_mac);
d3d59da
+//	TAILQ_HEAD(, packet) outgoing;
d3d59da
+//	memset(state, 0, sizeof(state));
d3d59da
+}
d3d59da
+
d3d59da
+void
def1deb
+packet_destroy_all(struct ssh *ssh, int audit_it, int privsep)
d3d59da
+{
d3d59da
+	if (audit_it)
def1deb
+		audit_it = packet_state_has_keys(ssh->state);
def1deb
+	packet_destroy_state(ssh->state);
d3d59da
+	if (audit_it) {
d3d59da
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+		if (privsep)
def1deb
+			audit_session_key_free(ssh, MODE_MAX);
d3d59da
+		else
def1deb
+			audit_session_key_free_body(ssh, MODE_MAX, getpid(), getuid());
d3d59da
+#endif
d3d59da
+	}
d3d59da
+}
d3d59da
+
1900351
 /* Reset after_authentication and reset compression in post-auth privsep */
13073f8
 static int
13073f8
 ssh_packet_set_postauth(struct ssh *ssh)
f32b842
diff -up openssh-8.6p1/packet.h.audit openssh-8.6p1/packet.h
f32b842
--- openssh-8.6p1/packet.h.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/packet.h	2021-04-19 16:47:35.758062145 +0200
f32b842
@@ -218,4 +218,5 @@ const u_char	*sshpkt_ptr(struct ssh *, s
132f8f8
 # undef EC_POINT
132f8f8
 #endif
d3d59da
 
def1deb
+void	 packet_destroy_all(struct ssh *, int, int);
d3d59da
 #endif				/* PACKET_H */
f32b842
diff -up openssh-8.6p1/session.c.audit openssh-8.6p1/session.c
f32b842
--- openssh-8.6p1/session.c.audit	2021-04-19 16:47:35.722061868 +0200
f32b842
+++ openssh-8.6p1/session.c	2021-04-19 16:47:35.758062145 +0200
def1deb
@@ -136,7 +136,7 @@ extern char *__progname;
d3d59da
 extern int debug_flag;
d3d59da
 extern u_int utmp_len;
d3d59da
 extern int startup_pipe;
d3d59da
-extern void destroy_sensitive_data(void);
def1deb
+extern void destroy_sensitive_data(struct ssh *, int);
bbf61da
 extern struct sshbuf *loginmsg;
3cd4899
 extern struct sshauthopt *auth_opts;
def1deb
 extern char *tun_fwd_ifnames; /* serverloop.c */
f32b842
@@ -644,6 +644,14 @@ do_exec_pty(struct ssh *ssh, Session *s,
d3d59da
 	/* Parent.  Close the slave side of the pseudo tty. */
d3d59da
 	close(ttyfd);
d3d59da
 
28ce052
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
d3d59da
+	/* do_login in the child did not affect state in this process,
d3d59da
+	   compensate.  From an architectural standpoint, this is extremely
d3d59da
+	   ugly. */
6cf9b8e
+	if (command != NULL)
d3d59da
+		audit_count_session_open();
d3d59da
+#endif
d3d59da
+
d3d59da
 	/* Enter interactive session. */
d3d59da
 	s->ptymaster = ptymaster;
def1deb
 	ssh_packet_set_interactive(ssh, 1,
f32b842
@@ -736,15 +744,19 @@ do_exec(struct ssh *ssh, Session *s, con
13073f8
 	    s->self);
d3d59da
 
d3d59da
 #ifdef SSH_AUDIT_EVENTS
d3d59da
+	if (s->command != NULL || s->command_handle != -1)
d3d59da
+		fatal("do_exec: command already set");
d3d59da
 	if (command != NULL)
d3d59da
-		PRIVSEP(audit_run_command(command));
d3d59da
+		s->command = xstrdup(command);
d3d59da
 	else if (s->ttyfd == -1) {
d3d59da
 		char *shell = s->pw->pw_shell;
d3d59da
 
d3d59da
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
d3d59da
 			shell =_PATH_BSHELL;
d3d59da
-		PRIVSEP(audit_run_command(shell));
d3d59da
+		s->command = xstrdup(shell);
d3d59da
 	}
acf9885
+	if (s->command != NULL && s->ptyfd == -1)
def1deb
+		s->command_handle = PRIVSEP(audit_run_command(ssh, s->command));
d3d59da
 #endif
d3d59da
 	if (s->ttyfd != -1)
5b55d09
 		ret = do_exec_pty(ssh, s, command);
f32b842
@@ -1550,8 +1562,11 @@ do_child(struct ssh *ssh, Session *s, co
def1deb
 	sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
d3d59da
 
d3d59da
 	/* remove hostkey from the child's memory */
d3d59da
-	destroy_sensitive_data();
def1deb
+	destroy_sensitive_data(ssh, 1);
def1deb
 	ssh_packet_clear_keys(ssh);
d3d59da
+	/* Don't audit this - both us and the parent would be talking to the
d3d59da
+	   monitor over a single socket, with no synchronization. */
def1deb
+	packet_destroy_all(ssh, 0, 1);
d3d59da
 
d3d59da
 	/* Force a password change */
d3d59da
 	if (s->authctxt->force_pwchange) {
f32b842
@@ -1763,6 +1778,9 @@ session_unused(int id)
d3d59da
 	sessions[id].ttyfd = -1;
d3d59da
 	sessions[id].ptymaster = -1;
d3d59da
 	sessions[id].x11_chanids = NULL;
739842b
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+	sessions[id].command_handle = -1;
739842b
+#endif
d3d59da
 	sessions[id].next_unused = sessions_first_unused;
d3d59da
 	sessions_first_unused = id;
d3d59da
 }
f32b842
@@ -1843,6 +1861,19 @@ session_open(Authctxt *authctxt, int cha
d3d59da
 }
d3d59da
 
d3d59da
 Session *
d3d59da
+session_by_id(int id)
d3d59da
+{
d3d59da
+	if (id >= 0 && id < sessions_nalloc) {
d3d59da
+		Session *s = &sessions[id];
d3d59da
+		if (s->used)
d3d59da
+			return s;
d3d59da
+	}
25c16c6
+	debug_f("unknown id %d", id);
d3d59da
+	session_dump();
d3d59da
+	return NULL;
d3d59da
+}
d3d59da
+
d3d59da
+Session *
d3d59da
 session_by_tty(char *tty)
d3d59da
 {
d3d59da
 	int i;
f32b842
@@ -2450,6 +2481,32 @@ session_exit_message(struct ssh *ssh, Se
5b55d09
 		chan_write_failed(ssh, c);
d3d59da
 }
d3d59da
 
d3d59da
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+void
def1deb
+session_end_command2(struct ssh *ssh, Session *s)
d3d59da
+{
acf9885
+	if (s->command != NULL) {
f78ec07
+		if (s->command_handle != -1)
def1deb
+			audit_end_command(ssh, s->command_handle, s->command);
84822b5
+		free(s->command);
d3d59da
+		s->command = NULL;
d3d59da
+		s->command_handle = -1;
d3d59da
+	}
d3d59da
+}
d3d59da
+
d3d59da
+static void
def1deb
+session_end_command(struct ssh *ssh, Session *s)
d3d59da
+{
d3d59da
+	if (s->command != NULL) {
f78ec07
+		if (s->command_handle != -1)
def1deb
+			PRIVSEP(audit_end_command(ssh, s->command_handle, s->command));
84822b5
+		free(s->command);
d3d59da
+		s->command = NULL;
d3d59da
+		s->command_handle = -1;
d3d59da
+	}
d3d59da
+}
d3d59da
+#endif
d3d59da
+
d3d59da
 void
5b55d09
 session_close(struct ssh *ssh, Session *s)
d3d59da
 {
f32b842
@@ -2463,6 +2520,10 @@ session_close(struct ssh *ssh, Session *
13073f8
 
d3d59da
 	if (s->ttyfd != -1)
d3d59da
 		session_pty_cleanup(s);
d3d59da
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+	if (s->command)
def1deb
+		session_end_command(ssh, s);
d3d59da
+#endif
84822b5
 	free(s->term);
84822b5
 	free(s->display);
84822b5
 	free(s->x11_chanids);
f32b842
@@ -2537,14 +2598,14 @@ session_close_by_channel(struct ssh *ssh
def1deb
 }
def1deb
 
def1deb
 void
def1deb
-session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
def1deb
+session_destroy_all(struct ssh *ssh, void (*closefunc)(struct ssh *ssh, Session *))
def1deb
 {
def1deb
 	int i;
def1deb
 	for (i = 0; i < sessions_nalloc; i++) {
def1deb
 		Session *s = &sessions[i];
def1deb
 		if (s->used) {
def1deb
 			if (closefunc != NULL)
def1deb
-				closefunc(s);
def1deb
+				closefunc(ssh, s);
def1deb
 			else
def1deb
 				session_close(ssh, s);
def1deb
 		}
f32b842
@@ -2671,6 +2732,15 @@ do_authenticated2(struct ssh *ssh, Authc
5b55d09
 	server_loop2(ssh, authctxt);
d3d59da
 }
d3d59da
 
d3d59da
+static void
def1deb
+do_cleanup_one_session(struct ssh *ssh, Session *s)
d3d59da
+{
d3d59da
+	session_pty_cleanup2(s);
d3d59da
+#ifdef SSH_AUDIT_EVENTS
def1deb
+	session_end_command2(ssh, s);
d3d59da
+#endif
d3d59da
+}
d3d59da
+
d3d59da
 void
5b55d09
 do_cleanup(struct ssh *ssh, Authctxt *authctxt)
d3d59da
 {
f32b842
@@ -2734,7 +2804,7 @@ do_cleanup(struct ssh *ssh, Authctxt *au
d3d59da
 	 * or if running in monitor.
d3d59da
 	 */
d3d59da
 	if (!use_privsep || mm_is_monitor())
5b55d09
-		session_destroy_all(ssh, session_pty_cleanup2);
5b55d09
+		session_destroy_all(ssh, do_cleanup_one_session);
d3d59da
 }
5878ebb
 
5878ebb
 /* Return a name for the remote host that fits inside utmp_size */
f32b842
diff -up openssh-8.6p1/session.h.audit openssh-8.6p1/session.h
f32b842
--- openssh-8.6p1/session.h.audit	2021-04-16 05:55:25.000000000 +0200
f32b842
+++ openssh-8.6p1/session.h	2021-04-19 16:47:35.758062145 +0200
def1deb
@@ -61,6 +61,12 @@ struct Session {
d3d59da
 		char	*name;
d3d59da
 		char	*val;
d3d59da
 	} *env;
d3d59da
+
d3d59da
+	/* exec */
d3d59da
+#ifdef SSH_AUDIT_EVENTS
d3d59da
+	int	command_handle;
d3d59da
+	char	*command;
d3d59da
+#endif
d3d59da
 };
d3d59da
 
5b55d09
 void	 do_authenticated(struct ssh *, Authctxt *);
def1deb
@@ -71,10 +77,12 @@ void	 session_unused(int);
def1deb
 int	 session_input_channel_req(struct ssh *, Channel *, const char *);
def1deb
 void	 session_close_by_pid(struct ssh *ssh, pid_t, int);
5b55d09
 void	 session_close_by_channel(struct ssh *, int, void *);
def1deb
-void	 session_destroy_all(struct ssh *, void (*)(Session *));
def1deb
+void	 session_destroy_all(struct ssh *, void (*)(struct ssh*, Session *));
d3d59da
 void	 session_pty_cleanup2(Session *);
def1deb
+void	 session_end_command2(struct ssh *ssh, Session *);
d3d59da
 
d3d59da
 Session	*session_new(void);
d3d59da
+Session *session_by_id(int);
d3d59da
 Session	*session_by_tty(char *);
5b55d09
 void	 session_close(struct ssh *, Session *);
d3d59da
 void	 do_setusercontext(struct passwd *);
f32b842
diff -up openssh-8.6p1/sshd.c.audit openssh-8.6p1/sshd.c
f32b842
--- openssh-8.6p1/sshd.c.audit	2021-04-19 16:47:35.727061907 +0200
f32b842
+++ openssh-8.6p1/sshd.c	2021-04-19 16:47:35.759062152 +0200
5b55d09
@@ -122,6 +122,7 @@
13073f8
 #include "ssh-gss.h"