Petr Šabata 81d24c
diff -up openssh/audit-bsm.c.audit openssh/audit-bsm.c
Petr Šabata 81d24c
--- openssh/audit-bsm.c.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/audit-bsm.c	2019-04-03 17:02:20.713886041 +0200
Petr Šabata 81d24c
@@ -372,13 +372,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 */
Petr Šabata 81d24c
@@ -390,6 +403,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
 {
Petr Šabata 81d24c
@@ -451,4 +470,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 */
Petr Šabata 81d24c
diff -up openssh/audit.c.audit openssh/audit.c
Petr Šabata 81d24c
--- openssh/audit.c.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/audit.c	2019-04-03 17:02:20.713886041 +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 */
Petr Šabata 81d24c
diff -up openssh/audit.h.audit openssh/audit.h
Petr Šabata 81d24c
--- openssh/audit.h.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/audit.h	2019-04-03 17:02:20.713886041 +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 */
Petr Šabata 81d24c
diff -up openssh/audit-linux.c.audit openssh/audit-linux.c
Petr Šabata 81d24c
--- openssh/audit-linux.c.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/audit-linux.c	2019-04-03 17:02:20.713886041 +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 */
Petr Šabata 81d24c
diff -up openssh/auditstub.c.audit openssh/auditstub.c
Petr Šabata 81d24c
--- openssh/auditstub.c.audit	2019-04-03 17:02:20.714886050 +0200
Petr Šabata 81d24c
+++ openssh/auditstub.c	2019-04-03 17:02:20.714886050 +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
+}
Petr Šabata 81d24c
diff -up openssh/auth2.c.audit openssh/auth2.c
Petr Šabata 81d24c
--- openssh/auth2.c.audit	2019-04-03 17:02:20.651885453 +0200
Petr Šabata 81d24c
+++ openssh/auth2.c	2019-04-03 17:02:20.714886050 +0200
Petr Šabata 81d24c
@@ -303,9 +303,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)
Petr Šabata 81d24c
diff -up openssh/auth2-hostbased.c.audit openssh/auth2-hostbased.c
Petr Šabata 81d24c
--- openssh/auth2-hostbased.c.audit	2019-04-03 17:02:20.612885083 +0200
Petr Šabata 81d24c
+++ openssh/auth2-hostbased.c	2019-04-03 17:02:20.714886050 +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,
Petr Šabata 81d24c
diff -up openssh/auth2-pubkey.c.audit openssh/auth2-pubkey.c
Petr Šabata 81d24c
--- openssh/auth2-pubkey.c.audit	2019-04-03 17:02:20.691885832 +0200
Petr Šabata 81d24c
+++ openssh/auth2-pubkey.c	2019-04-03 17:02:20.714886050 +0200
Petr Šabata 81d24c
@@ -219,7 +219,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) {
Petr Šabata 81d24c
@@ -278,6 +278,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
 {
Petr Šabata 81d24c
diff -up openssh/auth.c.audit openssh/auth.c
Petr Šabata 81d24c
--- openssh/auth.c.audit	2019-04-03 17:02:20.691885832 +0200
Petr Šabata 81d24c
+++ openssh/auth.c	2019-04-03 17:02:20.714886050 +0200
Petr Šabata 81d24c
@@ -366,7 +366,7 @@ auth_log(struct ssh *ssh, int authentica
Petr Šabata 81d24c
 # endif
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
 #ifdef SSH_AUDIT_EVENTS
Petr Šabata 81d24c
-	if (authenticated == 0 && !authctxt->postponed)
Petr Šabata 81d24c
+	if (authenticated == 0 && !authctxt->postponed && !partial)
Petr Šabata 81d24c
 		audit_event(ssh, audit_classify_auth(method));
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
 }
Petr Šabata 81d24c
@@ -592,9 +592,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))
Petr Šabata 81d24c
diff -up openssh/auth.h.audit openssh/auth.h
Petr Šabata 81d24c
--- openssh/auth.h.audit	2019-04-03 17:02:20.692885842 +0200
Petr Šabata 81d24c
+++ openssh/auth.h	2019-04-03 17:02:20.714886050 +0200
Petr Šabata 81d24c
@@ -195,6 +195,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);
Petr Šabata 81d24c
@@ -214,6 +216,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 *);
Petr Šabata 81d24c
diff -up openssh/cipher.c.audit openssh/cipher.c
Petr Šabata 81d24c
--- openssh/cipher.c.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/cipher.c	2019-04-03 17:02:20.714886050 +0200
Petr Šabata 81d24c
@@ -61,25 +61,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
Petr Šabata 81d24c
@@ -410,7 +391,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);
Petr Šabata 81d24c
diff -up openssh/cipher.h.audit openssh/cipher.h
Petr Šabata 81d24c
--- openssh/cipher.h.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/cipher.h	2019-04-03 17:02:20.714886050 +0200
Petr Šabata 81d24c
@@ -45,7 +45,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 *);
Petr Šabata 81d24c
diff -up openssh/kex.c.audit openssh/kex.c
Petr Šabata 81d24c
--- openssh/kex.c.audit	2019-04-03 17:02:20.652885462 +0200
Petr Šabata 81d24c
+++ openssh/kex.c	2019-04-03 17:02:20.715886060 +0200
Petr Šabata 81d24c
@@ -60,6 +60,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"
Petr Šabata 81d24c
@@ -758,12 +759,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) {
Petr Šabata 81d24c
 		error("%s: unsupported cipher %s", __func__, name);
Petr Šabata 81d24c
 		free(name);
Petr Šabata 81d24c
@@ -783,8 +788,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) {
Petr Šabata 81d24c
 		error("%s: unsupported MAC %s", __func__, name);
Petr Šabata 81d24c
 		free(name);
Petr Šabata 81d24c
@@ -796,12 +805,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;
Petr Šabata 81d24c
@@ -933,7 +946,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;
Petr Šabata 81d24c
@@ -948,7 +961,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;
Petr Šabata 81d24c
@@ -971,6 +984,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;
Petr Šabata 81d24c
@@ -1129,6 +1146,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.
Petr Šabata 81d24c
diff -up openssh/kex.h.audit openssh/kex.h
Petr Šabata 81d24c
--- openssh/kex.h.audit	2019-04-03 17:02:20.652885462 +0200
Petr Šabata 81d24c
+++ openssh/kex.h	2019-04-03 17:02:20.715886060 +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 **);
Petr Šabata 81d24c
diff -up openssh/mac.c.audit openssh/mac.c
Petr Šabata 81d24c
--- openssh/mac.c.audit	2019-04-03 17:02:20.652885462 +0200
Petr Šabata 81d24c
+++ openssh/mac.c	2019-04-03 17:02:20.715886060 +0200
Petr Šabata 81d24c
@@ -243,6 +243,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
Petr Šabata 81d24c
diff -up openssh/mac.h.audit openssh/mac.h
Petr Šabata 81d24c
--- openssh/mac.h.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/mac.h	2019-04-03 17:02:20.715886060 +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 */
Petr Šabata 81d24c
diff -up openssh/Makefile.in.audit openssh/Makefile.in
Petr Šabata 81d24c
--- openssh/Makefile.in.audit	2019-04-03 17:02:20.705885965 +0200
Petr Šabata 81d24c
+++ openssh/Makefile.in	2019-04-03 17:02:20.715886060 +0200
Petr Šabata 81d24c
@@ -109,7 +109,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
Petr Šabata 81d24c
 	sntrup4591761.o kexsntrup4591761x25519.o kexgen.o \
Petr Šabata 81d24c
 	kexgssc.o \
Petr Šabata 81d24c
	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
 
Petr Šabata 81d24c
diff -up openssh/monitor.c.audit openssh/monitor.c
Petr Šabata 81d24c
--- openssh/monitor.c.audit	2019-04-03 17:02:20.674885671 +0200
Petr Šabata 81d24c
+++ openssh/monitor.c	2019-04-03 17:03:17.201421405 +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"
Petr Šabata 81d24c
@@ -107,6 +108,8 @@ extern u_char session_id[];
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
 };
Petr Šabata 81d24c
@@ -1445,8 +1462,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)
Petr Šabata 81d24c
@@ -1455,6 +1474,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))
Petr Šabata 81d24c
 		fatal("%s: bad key, not previously allowed", __func__);
Petr Šabata 81d24c
+	if (type != key_blobtype)
Petr Šabata 81d24c
+		fatal("%s: bad key type", __func__);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 	/* Empty signature algorithm means NULL. */
Petr Šabata 81d24c
 	if (*sigalg == '\0') {
Petr Šabata 81d24c
@@ -1470,25 +1491,28 @@ mm_answer_keyverify(struct ssh *ssh, int
Petr Šabata 81d24c
 	case MM_USERKEY:
Petr Šabata 81d24c
 		valid_data = monitor_valid_userblob(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)
Petr Šabata 81d24c
 		fatal("%s: bad signature data blob", __func__);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 	if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
Petr Šabata 81d24c
 	    SSH_FP_DEFAULT)) == NULL)
Petr Šabata 81d24c
 		fatal("%s: sshkey_fingerprint failed", __func__);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
-	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
Petr Šabata 81d24c
-	    sigalg, ssh->compat, &sig_details);
Petr Šabata 81d24c
 	debug3("%s: %s %p signature %s%s%s", __func__, auth_method, key,
Petr Šabata 81d24c
 	    (ret == 0) ? "verified" : "unverified",
Petr Šabata 81d24c
 	    (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
Petr Šabata 81d24c
@@ -1536,13 +1560,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
 {
Petr Šabata 81d24c
 	debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Petr Šabata 81d24c
 	if (s->ttyfd != -1) {
Petr Šabata 81d24c
 		debug3("%s: tty %s ptyfd %d", __func__, 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) {
Petr Šabata 81d24c
+		debug3("%s: command %d", __func__, 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
 
Petr Šabata 81d24c
@@ -1609,7 +1639,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)
Petr Šabata 81d24c
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
Petr Šabata 81d24c
 	mm_request_send(sock, MONITOR_ANS_PTY, m);
Petr Šabata 81d24c
@@ -1628,7 +1658,7 @@ mm_answer_pty_cleanup(struct ssh *ssh, i
Petr Šabata 81d24c
 	if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
Petr Šabata 81d24c
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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);
Petr Šabata 81d24c
@@ -1650,6 +1680,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);
Petr Šabata 81d24c
@@ -1696,12 +1728,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)
Petr Šabata 81d24c
+		fatal("%s: error allocating a session", __func__);
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
+
Petr Šabata 81d24c
+	debug3("%s entering", __func__);
Petr Šabata 81d24c
+	if ((r = sshbuf_get_u32(m, &handle)) != 0 ||
Petr Šabata 81d24c
+	    (r = sshbuf_get_string(m, &cmd, &len)) != 0)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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)
Petr Šabata 81d24c
+		fatal("%s: invalid handle", __func__);
Petr Šabata 81d24c
+	mm_session_close(ssh, s);
Petr Šabata 81d24c
 	free(cmd);
Petr Šabata 81d24c
 	return (0);
Petr Šabata 81d24c
 }
Petr Šabata 81d24c
@@ -1767,6 +1834,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;
Petr Šabata 81d24c
 	debug3("%s: Waiting for new keys", __func__);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 	if ((child_state = sshbuf_new()) == NULL)
Petr Šabata 81d24c
@@ -1774,6 +1842,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);
Petr Šabata 81d24c
 	debug3("%s: GOT new keys", __func__);
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
 
Petr Šabata 81d24c
@@ -2066,3 +2147,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)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
Petr Šabata 81d24c
+	pid = (pid_t) tmp;
Petr Šabata 81d24c
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
Petr Šabata 81d24c
+	pid = (pid_t) tmp;
Petr Šabata 81d24c
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
Petr Šabata 81d24c
+	pid = (pid_t) tmp;
Petr Šabata 81d24c
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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 */
Petr Šabata 81d24c
diff -up openssh/monitor.h.audit openssh/monitor.h
Petr Šabata 81d24c
--- openssh/monitor.h.audit	2019-04-03 17:02:20.674885671 +0200
Petr Šabata 81d24c
+++ openssh/monitor.h	2019-04-03 17:02:20.715886060 +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,
Petr Šabata 81d24c
diff -up openssh/monitor_wrap.c.audit openssh/monitor_wrap.c
Petr Šabata 81d24c
--- openssh/monitor_wrap.c.audit	2019-04-03 17:02:20.653885472 +0200
Petr Šabata 81d24c
+++ openssh/monitor_wrap.c	2019-04-03 17:02:20.716886069 +0200
Petr Šabata 81d24c
@@ -513,7 +513,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
 {
Petr Šabata 81d24c
@@ -525,7 +525,8 @@ mm_sshkey_verify(const struct sshkey *ke
Petr Šabata 81d24c
 		*sig_detailsp = NULL;
Petr Šabata 81d24c
 	if ((m = sshbuf_new()) == NULL)
Petr Šabata 81d24c
 		fatal("%s: sshbuf_new failed", __func__);
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)
Petr Šabata 81d24c
@@ -547,6 +548,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
 {
Petr Šabata 81d24c
@@ -900,11 +915,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
 
Petr Šabata 81d24c
@@ -914,6 +930,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)
Petr Šabata 81d24c
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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
+
Petr Šabata 81d24c
+	debug3("%s entering command %s", __func__, command);
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+ 	if ((m = sshbuf_new()) == NULL)
Petr Šabata 81d24c
+ 		fatal("%s: sshbuf_new failed", __func__);
Petr Šabata 81d24c
+	if ((r = sshbuf_put_u32(m, handle)) != 0 ||
Petr Šabata 81d24c
+	    (r = sshbuf_put_cstring(m, command)) != 0)
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_END_COMMAND, m);
Petr Šabata 81d24c
 	sshbuf_free(m);
Petr Šabata 81d24c
 }
Petr Šabata 81d24c
 #endif /* SSH_AUDIT_EVENTS */
Petr Šabata 81d24c
@@ -1074,3 +1114,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)
Petr Šabata 81d24c
+ 		fatal("%s: sshbuf_new failed", __func__);
Petr Šabata 81d24c
+	if ((r = sshbuf_put_u32(m, what)) != 0)
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_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)
Petr Šabata 81d24c
+ 		fatal("%s: sshbuf_new failed", __func__);
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)
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_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)
Petr Šabata 81d24c
+ 		fatal("%s: sshbuf_new failed", __func__);
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)
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_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)
Petr Šabata 81d24c
+ 		fatal("%s: sshbuf_new failed", __func__);
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)
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_SERVER_KEY_FREE, m);
Petr Šabata 81d24c
+	sshbuf_free(m);
Petr Šabata 81d24c
+}
Petr Šabata 81d24c
+#endif /* SSH_AUDIT_EVENTS */
Petr Šabata 81d24c
diff -up openssh/monitor_wrap.h.audit openssh/monitor_wrap.h
Petr Šabata 81d24c
--- openssh/monitor_wrap.h.audit	2019-04-03 17:02:20.653885472 +0200
Petr Šabata 81d24c
+++ openssh/monitor_wrap.h	2019-04-03 17:02:20.716886069 +0200
Petr Šabata 81d24c
@@ -57,7 +57,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
Petr Šabata 81d24c
@@ -82,7 +84,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;
Petr Šabata 81d24c
diff -up openssh/packet.c.audit openssh/packet.c
Petr Šabata 81d24c
--- openssh/packet.c.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/packet.c	2019-04-03 17:02:20.716886069 +0200
Petr Šabata 81d24c
@@ -77,6 +77,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"
Petr Šabata 81d24c
@@ -510,6 +511,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.
Petr Šabata 81d24c
@@ -587,22 +595,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;
Petr Šabata 81d24c
@@ -636,8 +641,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);
Petr Šabata 81d24c
@@ -864,6 +879,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
Petr Šabata 81d24c
 		   (unsigned long long)state->p_send.bytes,
Petr Šabata 81d24c
 		   (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 */
Petr Šabata 81d24c
@@ -2167,6 +2183,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)
Petr Šabata 81d24c
diff -up openssh/packet.h.audit openssh/packet.h
Petr Šabata 81d24c
--- openssh/packet.h.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/packet.h	2019-04-03 17:02:20.716886069 +0200
Petr Šabata 81d24c
@@ -217,4 +217,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 */
Petr Šabata 81d24c
diff -up openssh/session.c.audit openssh/session.c
Petr Šabata 81d24c
--- openssh/session.c.audit	2019-04-03 17:02:20.712886031 +0200
Petr Šabata 81d24c
+++ openssh/session.c	2019-04-03 17:02:20.716886069 +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 */
Petr Šabata 81d24c
@@ -648,6 +648,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,
Petr Šabata 81d24c
@@ -740,15 +748,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);
Petr Šabata 81d24c
@@ -1556,8 +1568,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) {
Petr Šabata 81d24c
@@ -1769,6 +1784,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
 }
Petr Šabata 81d24c
@@ -1851,6 +1869,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
+	}
Petr Šabata 81d24c
+	debug("%s: unknown id %d", __func__, 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;
Petr Šabata 81d24c
@@ -2461,6 +2492,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
 {
Petr Šabata 81d24c
@@ -2474,6 +2531,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);
Petr Šabata 81d24c
@@ -2549,14 +2610,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
 		}
Petr Šabata 81d24c
@@ -2683,6 +2744,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
 {
Petr Šabata 81d24c
@@ -2746,7 +2816,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 */
Petr Šabata 81d24c
diff -up openssh/session.h.audit openssh/session.h
Petr Šabata 81d24c
--- openssh/session.h.audit	2019-03-27 23:26:14.000000000 +0100
Petr Šabata 81d24c
+++ openssh/session.h	2019-04-03 17:02:20.717886079 +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 *);
Petr Šabata 81d24c
diff -up openssh/sshd.c.audit openssh/sshd.c
Petr Šabata 81d24c
--- openssh/sshd.c.audit	2019-04-03 17:02:20.692885842 +0200
Petr Šabata 81d24c
+++ openssh/sshd.c	2019-04-03 17:02:20.717886079 +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"
Petr Šabata 81d24c
@@ -261,8 +262,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;
Petr Šabata 81d24c
@@ -278,6 +279,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
 {
Petr Šabata 81d24c
@@ -380,18 +390,45 @@ grace_alarm_handler(int sig)
Petr Šabata 81d24c
 	    ssh_remote_port(the_active_state));
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
 		}
Petr Šabata 81d24c
@@ -400,14 +437,26 @@ 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)
Petr Šabata 81d24c
 				fatal("could not demote host %s key: %s",
Petr Šabata 81d24c
@@ -415,6 +464,12 @@ demote_sensitive_data(void)
Petr Šabata 81d24c
 				    ssh_err(r));
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
 	}
Petr Šabata 81d24c
@@ -442,7 +497,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
 
Petr Šabata 81d24c
@@ -457,7 +512,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();
Petr Šabata 81d24c
@@ -496,7 +551,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) {
Petr Šabata 81d24c
@@ -542,7 +597,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);
Petr Šabata 81d24c
@@ -594,7 +649,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
 
Petr Šabata 81d24c
@@ -1057,7 +1112,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;
Petr Šabata 81d24c
@@ -1112,6 +1167,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);
Petr Šabata 81d24c
@@ -1978,7 +2034,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
 
Petr Šabata 81d24c
@@ -2222,6 +2278,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);
Petr Šabata 81d24c
@@ -2401,6 +2460,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 &&
Petr Šabata 81d24c
@@ -2414,9 +2482,16 @@ cleanup_exit(int i)
Petr Šabata 81d24c
 				    pmonitor->m_pid, strerror(errno));
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);
Petr Šabata 81d24c
diff -up openssh/sshkey.c.audit openssh/sshkey.c
Petr Šabata 81d24c
--- openssh/sshkey.c.audit	2019-04-03 17:02:20.657885510 +0200
Petr Šabata 81d24c
+++ openssh/sshkey.c	2019-04-03 17:02:20.718886088 +0200
Petr Šabata 81d24c
@@ -331,6 +331,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)
Petr Šabata 81d24c
diff -up openssh/sshkey.h.audit openssh/sshkey.h
Petr Šabata 81d24c
--- openssh/sshkey.h.audit	2019-04-03 17:02:20.657885510 +0200
Petr Šabata 81d24c
+++ openssh/sshkey.h	2019-04-03 17:02:20.718886088 +0200
Petr Šabata 81d24c
@@ -148,6 +148,7 @@ u_int		 sshkey_size(const struct sshkey
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);