rutwa189 / rpms / openssh

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