vishalmishra434 / rpms / openssh

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