vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jakub Jelen b487a6
diff -up openssh-7.3p1/audit-bsm.c.audit openssh-7.3p1/audit-bsm.c
Jakub Jelen b487a6
--- openssh-7.3p1/audit-bsm.c.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/audit-bsm.c	2016-08-02 09:28:22.692299001 +0200
Jakub Jelen 132f8f
@@ -375,10 +375,23 @@ audit_connection_from(const char *host,
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-void
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
 audit_run_command(const char *command)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	/* not implemented */
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_end_command(int handle, const char *command)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_count_session_open(void)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not necessary */
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Jakub Jelen 535d34
@@ -393,6 +406,12 @@ audit_session_close(struct logininfo *li
Petr Lautrbach d3d59d
 	/* not implemented */
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
@@ -454,4 +473,40 @@ audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 		debug("%s: unhandled event %d", __func__, event);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported_body(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_destroy_sensitive_data(const char *fp)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_generate_ephemeral_server_key(const char *fp)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
 #endif /* BSM */
Jakub Jelen b487a6
diff -up openssh-7.3p1/audit.c.audit openssh-7.3p1/audit.c
Jakub Jelen b487a6
--- openssh-7.3p1/audit.c.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/audit.c	2016-08-02 09:28:22.692299001 +0200
Jakub Jelen 535d34
@@ -28,6 +28,7 @@
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 #include <stdarg.h>
Jakub Jelen 535d34
 #include <string.h>
Jakub Jelen 535d34
+#include <unistd.h>
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 #ifdef SSH_AUDIT_EVENTS
Jakub Jelen 535d34
 
Jakub Jelen 535d34
@@ -36,6 +37,11 @@
Jakub Jelen 535d34
 #include "key.h"
Jakub Jelen 535d34
 #include "hostfile.h"
Jakub Jelen 535d34
 #include "auth.h"
Jakub Jelen 535d34
+#include "ssh-gss.h"
Jakub Jelen 535d34
+#include "monitor_wrap.h"
Jakub Jelen 535d34
+#include "xmalloc.h"
Jakub Jelen 535d34
+#include "misc.h"
Jakub Jelen 535d34
+#include "servconf.h"
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 /*
Jakub Jelen 535d34
  * Care must be taken when using this since it WILL NOT be initialized when
Jakub Jelen 535d34
@@ -43,6 +49,7 @@
Jakub Jelen 535d34
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
Jakub Jelen 535d34
  */
Jakub Jelen 535d34
 extern Authctxt *the_authctxt;
Jakub Jelen 535d34
+extern ServerOptions options;
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 /* Maybe add the audit class to struct Authmethod? */
Jakub Jelen 535d34
 ssh_audit_event_t
Jakub Jelen 535d34
@@ -71,13 +78,10 @@ audit_classify_auth(const char *method)
Jakub Jelen 535d34
 const char *
Jakub Jelen 535d34
 audit_username(void)
Jakub Jelen 535d34
 {
Jakub Jelen 535d34
-	static const char unknownuser[] = "(unknown user)";
Jakub Jelen 535d34
-	static const char invaliduser[] = "(invalid user)";
Jakub Jelen 535d34
+	static const char unknownuser[] = "(unknown)";
Jakub Jelen 535d34
 
Jakub Jelen 535d34
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
Jakub Jelen 535d34
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
Jakub Jelen 535d34
 		return (unknownuser);
Jakub Jelen 535d34
-	if (!the_authctxt->valid)
Jakub Jelen 535d34
-		return (invaliduser);
Jakub Jelen 535d34
 	return (the_authctxt->user);
Jakub Jelen 535d34
 }
Jakub Jelen 535d34
 
Jakub Jelen 535d34
@@ -111,6 +115,40 @@ audit_event_lookup(ssh_audit_event_t ev)
Jakub Jelen 535d34
 	return(event_lookup[i].name);
Jakub Jelen 535d34
 }
Jakub Jelen 535d34
 
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_key(int host_user, int *rv, const Key *key)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	char *fp;
Jakub Jelen 535d34
+	const char *crypto_name;
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
Jakub Jelen 535d34
+	if (key->type == KEY_RSA1)
Jakub Jelen 535d34
+		crypto_name = "ssh-rsa1";
Jakub Jelen 535d34
+	else
Jakub Jelen 535d34
+		crypto_name = key_ssh_name(key);
Jakub Jelen 535d34
+	if (audit_keyusage(host_user, crypto_name, key_size(key), fp, *rv) == 0)
Jakub Jelen 535d34
+		*rv = 0;
Jakub Jelen 535d34
+	free(fp);
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_unsupported(int what)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	PRIVSEP(audit_unsupported_body(what));
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, pfs, getpid(), getuid()));
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_session_key_free(int ctos)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
Jakub Jelen 535d34
 /*
Jakub Jelen 535d34
  * Null implementations of audit functions.
Jakub Jelen 535d34
@@ -140,6 +178,17 @@ audit_event(ssh_audit_event_t event)
Jakub Jelen 535d34
 }
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 /*
Jakub Jelen 535d34
+ * Called when a child process has called, or will soon call,
Jakub Jelen 535d34
+ * audit_session_open.
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_count_session_open(void)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	debug("audit count session open euid %d user %s", geteuid(),
Jakub Jelen 535d34
+	      audit_username());
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
  * Called when a user session is started.  Argument is the tty allocated to
Jakub Jelen 535d34
  * the session, or NULL if no tty was allocated.
Jakub Jelen 535d34
  *
Jakub Jelen 535d34
@@ -174,13 +223,91 @@ audit_session_close(struct logininfo *li
Jakub Jelen 535d34
 /*
Jakub Jelen 535d34
  * This will be called when a user runs a non-interactive command.  Note that
Jakub Jelen 535d34
  * it may be called multiple times for a single connection since SSH2 allows
Jakub Jelen 535d34
- * multiple sessions within a single connection.
Jakub Jelen 535d34
+ * multiple sessions within a single connection.  Returns a "handle" for
Jakub Jelen 535d34
+ * audit_end_command.
Jakub Jelen 535d34
  */
Jakub Jelen 535d34
-void
Jakub Jelen 535d34
+int
Jakub Jelen 535d34
 audit_run_command(const char *command)
Jakub Jelen 535d34
 {
Jakub Jelen 535d34
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
Jakub Jelen 535d34
 	    audit_username(), command);
Jakub Jelen 535d34
+	return 0;
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
+ * This will be called when the non-interactive command finishes.  Note that
Jakub Jelen 535d34
+ * it may be called multiple times for a single connection since SSH2 allows
Jakub Jelen 535d34
+ * multiple sessions within a single connection.  "handle" should come from
Jakub Jelen 535d34
+ * the corresponding audit_run_command.
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_end_command(int handle, const char *command)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
Jakub Jelen 535d34
+	    audit_username(), command);
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
Jakub Jelen 535d34
+ *
Jakub Jelen 535d34
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+int
Jakub Jelen 535d34
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
Jakub Jelen 535d34
+{
Jakub Jelen 28ce05
+	debug("audit %s key usage euid %d user %s key type %s key length %d fingerprint %s, result %d",
Jakub Jelen 535d34
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(), type, bits,
Jakub Jelen 28ce05
+		fp, rv);
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
+ * This will be called when the protocol negotiation fails.
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_unsupported_body(int what)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
+ * This will be called on succesfull protocol negotiation.
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
Jakub Jelen 535d34
+	       uid_t uid)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
Jakub Jelen 535d34
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
Jakub Jelen 535d34
+	        (unsigned)uid);
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
+ * This will be called on succesfull session key discard
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
Jakub Jelen 535d34
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
+ * This will be called on destroy private part of the server key
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
Jakub Jelen 535d34
+		geteuid(), fp, (long)pid, (unsigned)uid);
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+/*
Jakub Jelen 535d34
+ * This will be called on generation of the ephemeral server key
Jakub Jelen 535d34
+ */
Jakub Jelen 535d34
+void
Jakub Jelen 535d34
+audit_generate_ephemeral_server_key(const char *)
Jakub Jelen 535d34
+{
Jakub Jelen 535d34
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
Jakub Jelen 535d34
 }
Jakub Jelen 535d34
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
Jakub Jelen 535d34
 #endif /* SSH_AUDIT_EVENTS */
Jakub Jelen b487a6
diff -up openssh-7.3p1/audit.h.audit openssh-7.3p1/audit.h
Jakub Jelen b487a6
--- openssh-7.3p1/audit.h.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/audit.h	2016-08-02 09:28:22.692299001 +0200
Jakub Jelen 535d34
@@ -28,6 +28,7 @@
Jakub Jelen 535d34
 # define _SSH_AUDIT_H
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 #include "loginrec.h"
Jakub Jelen 535d34
+#include "key.h"
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 enum ssh_audit_event_type {
Jakub Jelen 535d34
 	SSH_LOGIN_EXCEED_MAXTRIES,
Jakub Jelen 28ce05
@@ -45,13 +46,33 @@ enum ssh_audit_event_type {
Jakub Jelen 28ce05
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
Jakub Jelen 28ce05
 	SSH_AUDIT_UNKNOWN
Jakub Jelen 535d34
 };
Jakub Jelen 28ce05
+
Jakub Jelen 28ce05
+enum ssh_audit_kex {
Jakub Jelen 28ce05
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
Jakub Jelen 28ce05
+	SSH_AUDIT_UNSUPPORTED_MAC,
Jakub Jelen 28ce05
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
Jakub Jelen 28ce05
+};
Jakub Jelen 535d34
 typedef enum ssh_audit_event_type ssh_audit_event_t;
Jakub Jelen 535d34
 
Jakub Jelen 535d34
+int	listening_for_clients(void);
Jakub Jelen 535d34
+
Jakub Jelen 535d34
 void	audit_connection_from(const char *, int);
Jakub Jelen 535d34
 void	audit_event(ssh_audit_event_t);
Jakub Jelen 535d34
+void	audit_count_session_open(void);
Jakub Jelen 535d34
 void	audit_session_open(struct logininfo *);
Jakub Jelen 535d34
 void	audit_session_close(struct logininfo *);
Jakub Jelen 535d34
-void	audit_run_command(const char *);
Jakub Jelen 535d34
+int	audit_run_command(const char *);
Jakub Jelen 535d34
+void 	audit_end_command(int, const char *);
Jakub Jelen 535d34
 ssh_audit_event_t audit_classify_auth(const char *);
Jakub Jelen 535d34
+int	audit_keyusage(int, const char *, unsigned, char *, int);
Jakub Jelen 535d34
+void	audit_key(int, int *, const Key *);
Jakub Jelen 535d34
+void	audit_unsupported(int);
Jakub Jelen 535d34
+void	audit_kex(int, char *, char *, char *, char *);
Jakub Jelen 535d34
+void	audit_unsupported_body(int);
Jakub Jelen 535d34
+void	audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
Jakub Jelen 535d34
+void	audit_session_key_free(int ctos);
Jakub Jelen 535d34
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
Jakub Jelen 535d34
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jakub Jelen 535d34
+void	audit_generate_ephemeral_server_key(const char *);
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 #endif /* _SSH_AUDIT_H */
Jakub Jelen b487a6
diff -up openssh-7.3p1/audit-linux.c.audit openssh-7.3p1/audit-linux.c
Jakub Jelen b487a6
--- openssh-7.3p1/audit-linux.c.audit	2016-08-02 09:28:22.693299000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/audit-linux.c	2016-08-02 09:37:19.652010336 +0200
Jakub Jelen b487a6
@@ -35,25 +35,38 @@
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #include "log.h"
Petr Lautrbach d3d59d
 #include "audit.h"
Petr Lautrbach d3d59d
+#include "key.h"
Petr Lautrbach d3d59d
+#include "hostfile.h"
Petr Lautrbach d3d59d
+#include "auth.h"
Petr Lautrbach 190035
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
Petr Lautrbach d3d59d
+#include "servconf.h"
Petr Lautrbach d3d59d
 #include "canohost.h"
Jakub Jelen b487a6
 #include "packet.h"
Jakub Jelen b487a6
-
Petr Lautrbach d3d59d
+#include "cipher.h"
Jakub Jelen b487a6
+#include "channels.h"
Jakub Jelen b487a6
+#include "session.h"
Jakub Jelen b487a6
+
Petr Lautrbach cf5c11
+#define AUDIT_LOG_SIZE 256
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+extern ServerOptions options;
Petr Lautrbach d3d59d
+extern Authctxt *the_authctxt;
Petr Lautrbach d3d59d
+extern u_int utmp_len;
Jakub Jelen b487a6
 const char *audit_username(void);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-int
Jakub Jelen b487a6
-linux_audit_record_event(int uid, const char *username, const char *hostname,
Jakub Jelen b487a6
-    const char *ip, const char *ttyn, int success)
Petr Lautrbach d3d59d
+static void
Jakub Jelen b487a6
+linux_audit_user_logxxx(int uid, const char *username, const char *hostname,
Jakub Jelen b487a6
+    const char *ip, const char *ttyn, int success, int event)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	int audit_fd, rc, saved_errno;
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
 	if ((audit_fd = audit_open()) < 0) {
Petr Lautrbach d3d59d
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
 		    errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
-			return 1; /* No audit support in kernel */
Petr Lautrbach d3d59d
+			return; /* No audit support in kernel */
Petr Lautrbach d3d59d
 		else
Petr Lautrbach d3d59d
-			return 0; /* Must prevent login */
Petr Lautrbach d3d59d
+			goto fatal_report; /* Must prevent login */
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, event,
Petr Lautrbach d3d59d
 	    NULL, "login", username ? username : "(unknown)",
Petr Lautrbach d3d59d
 	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
Petr Lautrbach d3d59d
 	saved_errno = errno;
Jakub Jelen b487a6
@@ -67,9 +80,97 @@ linux_audit_record_event(int uid, const
Petr Lautrbach d3d59d
 		rc = 0;
Petr Lautrbach d3d59d
 	errno = saved_errno;
Jakub Jelen b487a6
 
Jakub Jelen b487a6
-	return rc >= 0;
Petr Lautrbach d3d59d
+	if (rc < 0) {
Petr Lautrbach d3d59d
+fatal_report:
Petr Lautrbach d3d59d
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
Petr Lautrbach d3d59d
+	}
Jakub Jelen b487a6
+}
Jakub Jelen b487a6
+
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+linux_audit_user_auth(int uid, const char *username,
Petr Lautrbach d3d59d
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int audit_fd, rc, saved_errno;
Petr Lautrbach d3d59d
+	static const char *event_name[] = {
Petr Lautrbach d3d59d
+		"maxtries exceeded",
Petr Lautrbach d3d59d
+		"root denied",
Petr Lautrbach d3d59d
+		"success",
Petr Lautrbach d3d59d
+		"none",
Petr Lautrbach d3d59d
+		"password",
Petr Lautrbach d3d59d
+		"challenge-response",
Petr Lautrbach d3d59d
+		"pubkey",
Petr Lautrbach d3d59d
+		"hostbased",
Petr Lautrbach d3d59d
+		"gssapi",
Petr Lautrbach d3d59d
+		"invalid user",
Petr Lautrbach d3d59d
+		"nologin",
Petr Lautrbach d3d59d
+		"connection closed",
Petr Lautrbach d3d59d
+		"connection abandoned",
Petr Lautrbach d3d59d
+		"unknown"
Petr Lautrbach d3d59d
+	};
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
+		    errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			return; /* No audit support in kernel */
Petr Lautrbach d3d59d
+		else
Petr Lautrbach d3d59d
+			goto fatal_report; /* Must prevent login */
Petr Lautrbach d3d59d
+	}
Jakub Jelen 28ce05
+
Petr Lautrbach d3d59d
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
Petr Lautrbach d3d59d
+		event = SSH_AUDIT_UNKNOWN;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
Petr Lautrbach d3d59d
+	    NULL, event_name[event], username ? username : "(unknown)",
Petr Lautrbach d3d59d
+	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
Petr Lautrbach d3d59d
+	saved_errno = errno;
Petr Lautrbach d3d59d
+	close(audit_fd);
Petr Lautrbach d3d59d
+	/*
Petr Lautrbach d3d59d
+	 * Do not report error if the error is EPERM and sshd is run as non
Petr Lautrbach d3d59d
+	 * root user.
Petr Lautrbach d3d59d
+	 */
Petr Lautrbach d3d59d
+	if ((rc == -EPERM) && (geteuid() != 0))
Petr Lautrbach d3d59d
+		rc = 0;
Petr Lautrbach d3d59d
+	errno = saved_errno;
Petr Lautrbach d3d59d
+	if (rc < 0) {
Petr Lautrbach d3d59d
+fatal_report:
Petr Lautrbach d3d59d
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, rc, saved_errno;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
+					 errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			return 1; /* No audit support in kernel */
Jakub Jelen 28ce05
+		else
Petr Lautrbach d3d59d
+			return 0; /* Must prevent login */
Petr Lautrbach d3d59d
+	}
Jakub Jelen b487a6
+	snprintf(buf, sizeof(buf), "%s_auth rport=%d", host_user ? "pubkey" : "hostbased", ssh_remote_port(active_state));
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
Jakub Jelen b487a6
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
Petr Lautrbach d3d59d
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
Petr Lautrbach d3d59d
+		goto out;
Petr Lautrbach 190035
+	snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s rport=%d",
Jakub Jelen b487a6
+			type, bits, fp, ssh_remote_port(active_state));
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
Jakub Jelen b487a6
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
Petr Lautrbach d3d59d
+out:
Petr Lautrbach d3d59d
+	saved_errno = errno;
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+	errno = saved_errno;
Petr Lautrbach d3d59d
+	/* do not report error if the error is EPERM and sshd is run as non root user */
Petr Lautrbach d3d59d
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
Jakub Jelen b487a6
 }
Jakub Jelen b487a6
 
Petr Lautrbach d3d59d
+static int user_login_count = 0;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* Below is the sshd audit API code */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Jakub Jelen b487a6
@@ -78,24 +179,51 @@ audit_connection_from(const char *host,
Petr Lautrbach d3d59d
 	/* not implemented */
Jakub Jelen b487a6
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-void
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
 audit_run_command(const char *command)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
-	/* not implemented */
Jakub Jelen 28ce05
+	if (!user_login_count++)
Jakub Jelen b487a6
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+		    NULL, "ssh", 1, AUDIT_USER_LOGIN);
Jakub Jelen b487a6
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+	    NULL, "ssh", 1, AUDIT_USER_START);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_end_command(int handle, const char *command)
Petr Lautrbach d3d59d
+{
Jakub Jelen b487a6
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+	    NULL, "ssh", 1, AUDIT_USER_END);
Jakub Jelen 28ce05
+	if (user_login_count && !--user_login_count)
Jakub Jelen b487a6
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+		    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_count_session_open(void)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	user_login_count++;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 audit_session_open(struct logininfo *li)
Petr Lautrbach d3d59d
 {
Jakub Jelen b487a6
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
Jakub Jelen b487a6
-	    li->line, 1) == 0)
Petr Lautrbach d3d59d
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
Jakub Jelen 28ce05
+	if (!user_login_count++)
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+		    NULL, li->line, 1, AUDIT_USER_LOGIN);
Petr Lautrbach d3d59d
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+	    NULL, li->line, 1, AUDIT_USER_START);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 audit_session_close(struct logininfo *li)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
-	/* not implemented */
Petr Lautrbach d3d59d
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+	    NULL, li->line, 1, AUDIT_USER_END);
Jakub Jelen 28ce05
+	if (user_login_count && !--user_login_count)
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+		    NULL, li->line, 1, AUDIT_USER_LOGOUT);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Jakub Jelen b487a6
@@ -105,24 +233,180 @@ audit_event(ssh_audit_event_t event)
Jakub Jelen b487a6
 
Petr Lautrbach d3d59d
 	switch(event) {
Petr Lautrbach d3d59d
 	case SSH_AUTH_SUCCESS:
Petr Lautrbach d3d59d
-	case SSH_CONNECTION_CLOSE:
Petr Lautrbach d3d59d
+		linux_audit_user_auth(-1, audit_username(), NULL,
Jakub Jelen b487a6
+			ssh_remote_ipaddr(ssh), "ssh", 1, event);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	case SSH_NOLOGIN:
Petr Lautrbach d3d59d
-	case SSH_LOGIN_EXCEED_MAXTRIES:
Petr Lautrbach d3d59d
 	case SSH_LOGIN_ROOT_DENIED:
Petr Lautrbach d3d59d
+		linux_audit_user_auth(-1, audit_username(), NULL,
Jakub Jelen b487a6
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
Jakub Jelen b487a6
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
Petr Lautrbach d3d59d
 		break;
Petr Lautrbach d3d59d
+	case SSH_LOGIN_EXCEED_MAXTRIES:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_NONE:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_PASSWD:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_KBDINT:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_PUBKEY:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_HOSTBASED:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_GSSAPI:
Petr Lautrbach d3d59d
+		linux_audit_user_auth(-1, audit_username(), NULL,
Jakub Jelen b487a6
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	case SSH_CONNECTION_CLOSE:
Petr Lautrbach d3d59d
+		if (user_login_count) {
Petr Lautrbach d3d59d
+			while (user_login_count--)
Jakub Jelen b487a6
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
Jakub Jelen b487a6
+				    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+				    NULL, "ssh", 1, AUDIT_USER_END);
Jakub Jelen b487a6
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
Jakub Jelen b487a6
+			    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+			    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
Petr Lautrbach d3d59d
+		}
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	case SSH_CONNECTION_ABANDON:
Petr Lautrbach d3d59d
 	case SSH_INVALID_USER:
Petr Lautrbach d3d59d
-		linux_audit_record_event(-1, audit_username(), NULL,
Jakub Jelen b487a6
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
Jakub Jelen b487a6
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
Petr Lautrbach d3d59d
 		break;
Petr Lautrbach d3d59d
 	default:
Jakub Jelen b487a6
 		debug("%s: unhandled event %d", __func__, event);
Jakub Jelen b487a6
 		break;
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 }
Jakub Jelen b487a6
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported_body(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+#ifdef AUDIT_CRYPTO_SESSION
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	const static char *name[] = { "cipher", "mac", "comp" };
Petr Lautrbach d3d59d
+	char *s;
Petr Lautrbach d3d59d
+	int audit_fd;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
Jakub Jelen b487a6
+		name[what], ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())),
Jakub Jelen b487a6
+		ssh_local_port(active_state));
Petr Lautrbach 84822b
+	free(s);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0)
Petr Lautrbach d3d59d
+		/* no problem, the next instruction will be fatal() */
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
Jakub Jelen b487a6
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 0);
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+const static char *direction[] = { "from-server", "from-client", "both" };
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
Petr Lautrbach d3d59d
+	       uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+#ifdef AUDIT_CRYPTO_SESSION
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, audit_ok;
Jakub Jelen 3f5513
+	const struct sshcipher *cipher = cipher_by_name(enc);
Petr Lautrbach d3d59d
+	char *s;
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
+	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s pfs=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
Petr Lautrbach 190035
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
Petr Lautrbach d3d59d
+		(intmax_t)pid, (intmax_t)uid,
Jakub Jelen b487a6
+		ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())), ssh_local_port(active_state));
Petr Lautrbach 84822b
+	free(s);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
+					 errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			return; /* No audit support in kernel */
Jakub Jelen 28ce05
+		else
Petr Lautrbach d3d59d
+			fatal("cannot open audit"); /* Must prevent login */
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
Jakub Jelen b487a6
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Petr Lautrbach d3d59d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Petr Lautrbach d3d59d
+		fatal("cannot write into audit"); /* Must prevent login */
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, audit_ok;
Petr Lautrbach d3d59d
+	char *s;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
Petr Lautrbach d3d59d
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
Jakub Jelen b487a6
+		 ssh_remote_port(active_state),
Petr Lautrbach d3d59d
+		 (s = get_local_ipaddr(packet_get_connection_in())),
Jakub Jelen b487a6
+		 ssh_local_port(active_state));
Petr Lautrbach 84822b
+	free(s);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jakub Jelen 535d34
+					 errno != EAFNOSUPPORT)
Jakub Jelen 535d34
+			error("cannot open audit");
Jakub Jelen 535d34
+		return;
Jakub Jelen 535d34
+	}
Jakub Jelen 535d34
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jakub Jelen b487a6
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
Jakub Jelen 535d34
+	audit_close(audit_fd);
Jakub Jelen 535d34
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jakub Jelen 535d34
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jakub Jelen 535d34
+		error("cannot write into audit");
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Jakub Jelen 535d34
+	char buf[AUDIT_LOG_SIZE];
Jakub Jelen 535d34
+	int audit_fd, audit_ok;
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
Jakub Jelen 535d34
+		fp, (intmax_t)pid, (intmax_t)uid);
Jakub Jelen 535d34
+	audit_fd = audit_open();
Jakub Jelen 535d34
+	if (audit_fd < 0) {
Jakub Jelen 535d34
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jakub Jelen 535d34
+					 errno != EAFNOSUPPORT)
Jakub Jelen 535d34
+			error("cannot open audit");
Jakub Jelen 535d34
+		return;
Jakub Jelen 535d34
+	}
Jakub Jelen 535d34
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jakub Jelen 535d34
+			buf, NULL,
Jakub Jelen b487a6
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(active_state),
Jakub Jelen 535d34
+			NULL, 1);
Jakub Jelen 535d34
+	audit_close(audit_fd);
Jakub Jelen 535d34
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jakub Jelen 535d34
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jakub Jelen 535d34
+		error("cannot write into audit");
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Jakub Jelen 535d34
+audit_generate_ephemeral_server_key(const char *fp)
Petr Lautrbach d3d59d
+{
Jakub Jelen 535d34
+	char buf[AUDIT_LOG_SIZE];
Jakub Jelen 535d34
+	int audit_fd, audit_ok;
Petr Lautrbach d3d59d
+
Jakub Jelen 535d34
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=? ", fp);
Jakub Jelen 535d34
+	audit_fd = audit_open();
Jakub Jelen 535d34
+	if (audit_fd < 0) {
Jakub Jelen 535d34
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jakub Jelen 535d34
+					 errno != EAFNOSUPPORT)
Jakub Jelen 535d34
+			error("cannot open audit");
Jakub Jelen 535d34
+		return;
Jakub Jelen 535d34
+	}
Jakub Jelen 535d34
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jakub Jelen 535d34
+			buf, NULL, 0, NULL, 1);
Jakub Jelen 535d34
+	audit_close(audit_fd);
Jakub Jelen 535d34
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jakub Jelen 535d34
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jakub Jelen 535d34
+		error("cannot write into audit");
Jakub Jelen 535d34
+}
Jakub Jelen 535d34
 #endif /* USE_LINUX_AUDIT */
Jakub Jelen b487a6
diff -up openssh-7.3p1/auditstub.c.audit openssh-7.3p1/auditstub.c
Jakub Jelen b487a6
--- openssh-7.3p1/auditstub.c.audit	2016-08-02 09:28:22.693299000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/auditstub.c	2016-08-02 09:28:22.693299000 +0200
Petr Lautrbach d3d59d
@@ -0,0 +1,50 @@
Petr Lautrbach d3d59d
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
Petr Lautrbach d3d59d
+ * Use is subject to license terms.
Petr Lautrbach d3d59d
+ *
Petr Lautrbach d3d59d
+ * Redistribution and use in source and binary forms, with or without
Petr Lautrbach d3d59d
+ * modification, are permitted provided that the following conditions
Petr Lautrbach d3d59d
+ * are met:
Petr Lautrbach d3d59d
+ * 1. Redistributions of source code must retain the above copyright
Petr Lautrbach d3d59d
+ *    notice, this list of conditions and the following disclaimer.
Petr Lautrbach d3d59d
+ * 2. Redistributions in binary form must reproduce the above copyright
Petr Lautrbach d3d59d
+ *    notice, this list of conditions and the following disclaimer in the
Petr Lautrbach d3d59d
+ *    documentation and/or other materials provided with the distribution.
Petr Lautrbach d3d59d
+ *
Petr Lautrbach d3d59d
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Petr Lautrbach d3d59d
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Petr Lautrbach d3d59d
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Petr Lautrbach d3d59d
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Petr Lautrbach d3d59d
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Petr Lautrbach d3d59d
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Petr Lautrbach d3d59d
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Petr Lautrbach d3d59d
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Petr Lautrbach d3d59d
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Petr Lautrbach d3d59d
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Petr Lautrbach d3d59d
+ *
Petr Lautrbach d3d59d
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+#include <sys/types.h>
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported(int n)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free(int ctos)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Jakub Jelen b487a6
diff -up openssh-7.3p1/auth2.c.audit openssh-7.3p1/auth2.c
Jakub Jelen b487a6
--- openssh-7.3p1/auth2.c.audit	2016-08-02 09:28:22.685299005 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/auth2.c	2016-08-02 09:28:22.693299000 +0200
Jakub Jelen 535d34
@@ -249,9 +249,6 @@ input_userauth_request(int type, u_int32
Jakub Jelen 535d34
 		} else {
Jakub Jelen 535d34
 			logit("input_userauth_request: invalid user %s", user);
Jakub Jelen 535d34
 			authctxt->pw = fakepw();
Petr Lautrbach 414bfa
-#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 535d34
-			PRIVSEP(audit_event(SSH_INVALID_USER));
Jakub Jelen 535d34
-#endif
Jakub Jelen 535d34
 		}
Jakub Jelen 535d34
 #ifdef USE_PAM
Jakub Jelen 535d34
 		if (options.use_pam)
Jakub Jelen b487a6
diff -up openssh-7.3p1/auth2-hostbased.c.audit openssh-7.3p1/auth2-hostbased.c
Jakub Jelen b487a6
--- openssh-7.3p1/auth2-hostbased.c.audit	2016-08-02 09:28:22.686299004 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/auth2-hostbased.c	2016-08-02 09:28:22.693299000 +0200
Jakub Jelen b487a6
@@ -148,7 +148,7 @@ userauth_hostbased(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	/* test for allowed key and correct signature */
Petr Lautrbach d3d59d
 	authenticated = 0;
Petr Lautrbach d3d59d
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
Petr Lautrbach d3d59d
-	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Petr Lautrbach d3d59d
+	    PRIVSEP(hostbased_key_verify(key, sig, slen, buffer_ptr(&b),
Jakub Jelen 209c7a
 			buffer_len(&b))) == 1) {
Petr Lautrbach d3d59d
 		authenticated = 1;
Jakub Jelen 209c7a
 		authctxt->last_details = pubkey;
Jakub Jelen b487a6
@@ -169,6 +169,18 @@ done:
Petr Lautrbach d3d59d
 	return authenticated;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+hostbased_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int rv;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	rv = key_verify(key, sig, slen, data, datalen);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	audit_key(0, &rv, key);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+	return rv;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* return 1 if given hostkey is allowed */
Petr Lautrbach d3d59d
 int
Petr Lautrbach d3d59d
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Jakub Jelen b487a6
diff -up openssh-7.3p1/auth2-pubkey.c.audit openssh-7.3p1/auth2-pubkey.c
Jakub Jelen b487a6
--- openssh-7.3p1/auth2-pubkey.c.audit	2016-08-02 09:28:22.686299004 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/auth2-pubkey.c	2016-08-02 09:28:22.694299000 +0200
Jakub Jelen b487a6
@@ -179,7 +179,7 @@ userauth_pubkey(Authctxt *authctxt)
Petr Lautrbach d3d59d
 		/* test for correct signature */
Petr Lautrbach d3d59d
 		authenticated = 0;
Jakub Jelen 535d34
 		if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
Petr Lautrbach d3d59d
-		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Petr Lautrbach d3d59d
+		    PRIVSEP(user_key_verify(key, sig, slen, buffer_ptr(&b),
Jakub Jelen 132f8f
 		    buffer_len(&b))) == 1) {
Petr Lautrbach d3d59d
 			authenticated = 1;
Jakub Jelen 209c7a
 			authctxt->last_details = pubkey;
Jakub Jelen b487a6
@@ -248,6 +248,18 @@ pubkey_auth_info(Authctxt *authctxt, con
Petr Lautrbach 84822b
 	free(extra);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+user_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int rv;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	rv = key_verify(key, sig, slen, data, datalen);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	audit_key(1, &rv, key);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+	return rv;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Jakub Jelen 535d34
 /*
Jakub Jelen 535d34
  * Splits 's' into an argument vector. Handles quoted string and basic
Jakub Jelen 535d34
  * escape characters (\\, \", \'). Caller must free the argument vector
Jakub Jelen b487a6
diff -up openssh-7.3p1/auth.c.audit openssh-7.3p1/auth.c
Jakub Jelen b487a6
--- openssh-7.3p1/auth.c.audit	2016-08-02 09:28:22.635299033 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/auth.c	2016-08-02 09:28:22.694299000 +0200
Jakub Jelen b487a6
@@ -657,9 +657,6 @@ getpwnamallow(const char *user)
Jakub Jelen 535d34
 		record_failed_login(user,
Jakub Jelen 5878eb
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
Jakub Jelen 535d34
 #endif
Petr Lautrbach d3d59d
-#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 535d34
-		audit_event(SSH_INVALID_USER);
Jakub Jelen 535d34
-#endif /* SSH_AUDIT_EVENTS */
Jakub Jelen 535d34
 		return (NULL);
Jakub Jelen 535d34
 	}
Jakub Jelen 535d34
 	if (!allowed_user(pw))
Jakub Jelen b487a6
diff -up openssh-7.3p1/auth.h.audit openssh-7.3p1/auth.h
Jakub Jelen b487a6
--- openssh-7.3p1/auth.h.audit	2016-08-02 09:28:22.686299004 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/auth.h	2016-08-02 09:28:22.694299000 +0200
Jakub Jelen b487a6
@@ -199,6 +199,7 @@ void	abandon_challenge_response(Authctxt
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 char	*expand_authorized_keys(const char *, struct passwd *pw);
Jakub Jelen 535d34
 char	*authorized_principals_file(struct passwd *);
Jakub Jelen 535d34
+int	 user_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
Jakub Jelen 535d34
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
Jakub Jelen b487a6
@@ -219,6 +220,7 @@ int	 get_hostkey_index(Key *, int, struc
Jakub Jelen 535d34
 int	 ssh1_session_key(BIGNUM *);
Jakub Jelen 535d34
 int	 sshd_hostkey_sign(Key *, Key *, u_char **, size_t *,
Jakub Jelen 13073f
 	     const u_char *, size_t, const char *, u_int);
Jakub Jelen 535d34
+int	 hostbased_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 /* debug messages during authentication */
Jakub Jelen 535d34
 void	 auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
Jakub Jelen b487a6
diff -up openssh-7.3p1/auth-rsa.c.audit openssh-7.3p1/auth-rsa.c
Jakub Jelen b487a6
--- openssh-7.3p1/auth-rsa.c.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/auth-rsa.c	2016-08-02 09:28:22.694299000 +0200
Jakub Jelen 535d34
@@ -95,7 +95,10 @@ auth_rsa_verify_response(Key *key, BIGNU
Jakub Jelen 535d34
 {
Jakub Jelen 535d34
 	u_char buf[32], mdbuf[16];
Jakub Jelen 535d34
 	struct ssh_digest_ctx *md;
Jakub Jelen 535d34
-	int len;
Jakub Jelen 535d34
+	int len, rv;
Jakub Jelen 535d34
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 535d34
+	char *fp;
Jakub Jelen 535d34
+#endif
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 	/* don't allow short keys */
Jakub Jelen 535d34
 	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Jakub Jelen 535d34
@@ -119,12 +122,18 @@ auth_rsa_verify_response(Key *key, BIGNU
Jakub Jelen 535d34
 	ssh_digest_free(md);
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 	/* Verify that the response is the original challenge. */
Jakub Jelen 535d34
-	if (timingsafe_bcmp(response, mdbuf, 16) != 0) {
Jakub Jelen 535d34
-		/* Wrong answer. */
Jakub Jelen 535d34
-		return (0);
Jakub Jelen 535d34
+	rv = timingsafe_bcmp(response, mdbuf, 16) == 0;
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 535d34
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
Jakub Jelen 535d34
+	if (audit_keyusage(1, "ssh-rsa1", RSA_size(key->rsa) * 8, fp, rv) == 0) {
Jakub Jelen 535d34
+		debug("unsuccessful audit");
Jakub Jelen 535d34
+		rv = 0;
Jakub Jelen 535d34
 	}
Jakub Jelen 535d34
-	/* Correct answer. */
Jakub Jelen 535d34
-	return (1);
Jakub Jelen 535d34
+	free(fp);
Jakub Jelen 535d34
+#endif
Jakub Jelen 535d34
+
Jakub Jelen 535d34
+	return rv;
Jakub Jelen 535d34
 }
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 /*
Jakub Jelen b487a6
diff -up openssh-7.3p1/cipher.c.audit openssh-7.3p1/cipher.c
Jakub Jelen b487a6
--- openssh-7.3p1/cipher.c.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/cipher.c	2016-08-02 09:28:22.694299000 +0200
Jakub Jelen 535d34
@@ -57,26 +57,6 @@ extern const EVP_CIPHER *evp_ssh1_3des(v
Petr Lautrbach 190035
 extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
Petr Lautrbach 190035
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
-struct sshcipher {
Petr Lautrbach 9f526c
-	char	*name;
Petr Lautrbach 9f526c
-	int	number;		/* for ssh1 only */
Petr Lautrbach 9f526c
-	u_int	block_size;
Petr Lautrbach 9f526c
-	u_int	key_len;
Petr Lautrbach 9f526c
-	u_int	iv_len;		/* defaults to block_size */
Petr Lautrbach 9f526c
-	u_int	auth_len;
Petr Lautrbach 9f526c
-	u_int	discard_len;
Petr Lautrbach 9f526c
-	u_int	flags;
Petr Lautrbach 9f526c
-#define CFLAG_CBC		(1<<0)
Petr Lautrbach 9f526c
-#define CFLAG_CHACHAPOLY	(1<<1)
Petr Lautrbach 190035
-#define CFLAG_AESCTR		(1<<2)
Petr Lautrbach 190035
-#define CFLAG_NONE		(1<<3)
Petr Lautrbach 190035
-#ifdef WITH_OPENSSL
Petr Lautrbach 9f526c
-	const EVP_CIPHER	*(*evptype)(void);
Petr Lautrbach 190035
-#else
Petr Lautrbach 190035
-	void	*ignored;
Petr Lautrbach 190035
-#endif
Petr Lautrbach 9f526c
-};
Petr Lautrbach 9f526c
-
Petr Lautrbach 190035
 static const struct sshcipher ciphers[] = {
Petr Lautrbach 190035
 #ifdef WITH_SSH1
Petr Lautrbach 9f526c
 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
Jakub Jelen b487a6
diff -up openssh-7.3p1/cipher.h.audit openssh-7.3p1/cipher.h
Jakub Jelen b487a6
--- openssh-7.3p1/cipher.h.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/cipher.h	2016-08-02 09:28:22.694299000 +0200
Petr Lautrbach 190035
@@ -62,7 +62,26 @@
Petr Lautrbach 190035
 #define CIPHER_ENCRYPT		1
Petr Lautrbach 190035
 #define CIPHER_DECRYPT		0
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
-struct sshcipher;
Petr Lautrbach 190035
+struct sshcipher {
Petr Lautrbach d3d59d
+	char	*name;
Petr Lautrbach d3d59d
+	int	number;		/* for ssh1 only */
Petr Lautrbach d3d59d
+	u_int	block_size;
Petr Lautrbach d3d59d
+	u_int	key_len;
Petr Lautrbach d3d59d
+	u_int	iv_len;		/* defaults to block_size */
Petr Lautrbach d3d59d
+	u_int	auth_len;
Petr Lautrbach d3d59d
+	u_int	discard_len;
Petr Lautrbach 5160c9
+	u_int	flags;
Petr Lautrbach 5160c9
+#define CFLAG_CBC		(1<<0)
Petr Lautrbach 5160c9
+#define CFLAG_CHACHAPOLY	(1<<1)
Petr Lautrbach 190035
+#define CFLAG_AESCTR		(1<<2)
Petr Lautrbach 190035
+#define CFLAG_NONE		(1<<3)
Petr Lautrbach 190035
+#ifdef WITH_OPENSSL
Petr Lautrbach d3d59d
+	const EVP_CIPHER	*(*evptype)(void);
Petr Lautrbach 190035
+#else
Petr Lautrbach 190035
+	void	*ignored;
Petr Lautrbach 190035
+#endif
Petr Lautrbach d3d59d
+};
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
 struct sshcipher_ctx {
Petr Lautrbach d3d59d
 	int	plaintext;
Petr Lautrbach d3d59d
 	int	encrypt;
Jakub Jelen b487a6
diff -up openssh-7.3p1/kex.c.audit openssh-7.3p1/kex.c
Jakub Jelen b487a6
--- openssh-7.3p1/kex.c.audit	2016-08-02 09:28:22.676299010 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/kex.c	2016-08-02 09:28:22.695298999 +0200
Jakub Jelen b487a6
@@ -55,6 +55,7 @@
Jakub Jelen 132f8f
 #include "ssherr.h"
Jakub Jelen 132f8f
 #include "sshbuf.h"
Petr Lautrbach 5160c9
 #include "digest.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Petr Lautrbach d3d59d
 
Petr Lautrbach 5160c9
 #ifdef GSSAPI
Petr Lautrbach 5160c9
 #include "ssh-gss.h"
Jakub Jelen b487a6
@@ -673,8 +674,12 @@ choose_enc(struct sshenc *enc, char *cli
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	char *name = match_list(client, server, NULL);
Jakub Jelen 132f8f
 
Petr Lautrbach d3d59d
-	if (name == NULL)
Petr Lautrbach d3d59d
+	if (name == NULL) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 28ce05
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_CIPHER);
Petr Lautrbach d3d59d
+#endif
Jakub Jelen 132f8f
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	if ((enc->cipher = cipher_by_name(name)) == NULL)
Jakub Jelen 132f8f
 		return SSH_ERR_INTERNAL_ERROR;
Petr Lautrbach d3d59d
 	enc->name = name;
Jakub Jelen b487a6
@@ -692,8 +697,12 @@ choose_mac(struct ssh *ssh, struct sshma
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	char *name = match_list(client, server, NULL);
Jakub Jelen 132f8f
 
Petr Lautrbach d3d59d
-	if (name == NULL)
Petr Lautrbach d3d59d
+	if (name == NULL) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 28ce05
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_MAC);
Petr Lautrbach d3d59d
+#endif
Jakub Jelen 132f8f
 		return SSH_ERR_NO_MAC_ALG_MATCH;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	if (mac_setup(mac, name) < 0)
Jakub Jelen 132f8f
 		return SSH_ERR_INTERNAL_ERROR;
Petr Lautrbach d3d59d
 	/* truncate the key */
Jakub Jelen b487a6
@@ -710,8 +719,12 @@ choose_comp(struct sshcomp *comp, char *
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	char *name = match_list(client, server, NULL);
Jakub Jelen 132f8f
 
Petr Lautrbach d3d59d
-	if (name == NULL)
Petr Lautrbach d3d59d
+	if (name == NULL) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 28ce05
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_COMPRESSION);
Petr Lautrbach d3d59d
+#endif
Jakub Jelen 132f8f
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	if (strcmp(name, "zlib@openssh.com") == 0) {
Petr Lautrbach d3d59d
 		comp->type = COMP_DELAYED;
Petr Lautrbach d3d59d
 	} else if (strcmp(name, "zlib") == 0) {
Jakub Jelen b487a6
@@ -882,6 +895,10 @@ kex_choose_conf(struct ssh *ssh)
Petr Lautrbach 190035
 		dh_need = MAX(dh_need, newkeys->enc.block_size);
Petr Lautrbach 190035
 		dh_need = MAX(dh_need, newkeys->enc.iv_len);
Petr Lautrbach 190035
 		dh_need = MAX(dh_need, newkeys->mac.key_len);
Petr Lautrbach 190035
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
Petr Lautrbach 190035
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach 190035
+		audit_kex(mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
Petr Lautrbach 190035
+#endif
Petr Lautrbach 190035
 	}
Petr Lautrbach 190035
 	/* XXX need runden? */
Petr Lautrbach 190035
 	kex->we_need = need;
Jakub Jelen b487a6
@@ -1056,3 +1073,33 @@ dump_digest(char *msg, u_char *digest, i
Jakub Jelen 132f8f
 	sshbuf_dump_data(digest, len, stderr);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+static void
Jakub Jelen 132f8f
+enc_destroy(struct sshenc *enc)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (enc == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	if (enc->key) {
Petr Lautrbach d3d59d
+		memset(enc->key, 0, enc->key_len);
Petr Lautrbach 84822b
+		free(enc->key);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	if (enc->iv) {
Jakub Jelen 13073f
+		memset(enc->iv,  0, enc->iv_len);
Petr Lautrbach 84822b
+		free(enc->iv);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	memset(enc, 0, sizeof(*enc));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Jakub Jelen 132f8f
+newkeys_destroy(struct newkeys *newkeys)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (newkeys == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	enc_destroy(&newkeys->enc);
Petr Lautrbach d3d59d
+	mac_destroy(&newkeys->mac);
Petr Lautrbach d3d59d
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
Petr Lautrbach d3d59d
+}
Jakub Jelen b487a6
diff -up openssh-7.3p1/kex.h.audit openssh-7.3p1/kex.h
Jakub Jelen b487a6
--- openssh-7.3p1/kex.h.audit	2016-08-02 09:28:22.676299010 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/kex.h	2016-08-02 09:28:22.695298999 +0200
Jakub Jelen b487a6
@@ -212,6 +212,8 @@ int	 kexgss_client(struct ssh *);
Jakub Jelen 132f8f
 int	 kexgss_server(struct ssh *);
Petr Lautrbach 5160c9
 #endif
Petr Lautrbach d3d59d
 
Jakub Jelen 132f8f
+void	newkeys_destroy(struct newkeys *newkeys);
Petr Lautrbach d3d59d
+
Jakub Jelen 5878eb
 int	 kex_dh_hash(int, const char *, const char *,
Jakub Jelen 132f8f
     const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
Jakub Jelen 132f8f
     const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
Jakub Jelen b487a6
diff -up openssh-7.3p1/key.h.audit openssh-7.3p1/key.h
Jakub Jelen b487a6
--- openssh-7.3p1/key.h.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/key.h	2016-08-02 09:28:22.695298999 +0200
Jakub Jelen 132f8f
@@ -50,6 +50,7 @@ typedef struct sshkey Key;
Jakub Jelen 132f8f
 #define key_ecdsa_bits_to_nid	sshkey_ecdsa_bits_to_nid
Petr Lautrbach 190035
 #define key_ecdsa_key_to_nid	sshkey_ecdsa_key_to_nid
Petr Lautrbach 190035
 #define key_is_cert		sshkey_is_cert
Petr Lautrbach 190035
+#define key_is_private		sshkey_is_private
Petr Lautrbach 190035
 #define key_type_plain		sshkey_type_plain
Petr Lautrbach 190035
 #define key_curve_name_to_nid	sshkey_curve_name_to_nid
Jakub Jelen 3f5513
 #define key_curve_nid_to_bits	sshkey_curve_nid_to_bits
Jakub Jelen b487a6
diff -up openssh-7.3p1/mac.c.audit openssh-7.3p1/mac.c
Jakub Jelen b487a6
--- openssh-7.3p1/mac.c.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/mac.c	2016-08-02 09:28:22.695298999 +0200
Jakub Jelen b487a6
@@ -245,6 +245,20 @@ mac_clear(struct sshmac *mac)
Petr Lautrbach d3d59d
 	mac->umac_ctx = NULL;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+void
Jakub Jelen 132f8f
+mac_destroy(struct sshmac *mac)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (mac == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	if (mac->key) {
Petr Lautrbach d3d59d
+		memset(mac->key, 0, mac->key_len);
Petr Lautrbach 84822b
+		free(mac->key);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	memset(mac, 0, sizeof(*mac));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* XXX copied from ciphers_valid */
Petr Lautrbach d3d59d
 #define	MAC_SEP	","
Petr Lautrbach d3d59d
 int
Jakub Jelen b487a6
diff -up openssh-7.3p1/mac.h.audit openssh-7.3p1/mac.h
Jakub Jelen b487a6
--- openssh-7.3p1/mac.h.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/mac.h	2016-08-02 09:28:22.695298999 +0200
Jakub Jelen b487a6
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
Jakub Jelen 5878eb
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
Jakub Jelen 5878eb
     const u_char *, size_t);
Jakub Jelen 132f8f
 void	 mac_clear(struct sshmac *);
Jakub Jelen 132f8f
+void	 mac_destroy(struct sshmac *);
Jakub Jelen 132f8f
 
Jakub Jelen 132f8f
 #endif /* SSHMAC_H */
Jakub Jelen b487a6
diff -up openssh-7.3p1/Makefile.in.audit openssh-7.3p1/Makefile.in
Jakub Jelen b487a6
--- openssh-7.3p1/Makefile.in.audit	2016-08-02 09:28:22.638299031 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/Makefile.in	2016-08-02 09:28:22.695298999 +0200
Jakub Jelen 13073f
@@ -99,7 +99,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
Jakub Jelen 535d34
 	kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
Jakub Jelen 535d34
 	kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
Jakub Jelen 13073f
 	kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
Jakub Jelen 5878eb
-	platform-pledge.o platform-tracing.o
Jakub Jelen 5878eb
+	platform-pledge.o platform-tracing.o auditstub.o
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
Jakub Jelen 13073f
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o
Jakub Jelen b487a6
diff -up openssh-7.3p1/monitor.c.audit openssh-7.3p1/monitor.c
Jakub Jelen b487a6
--- openssh-7.3p1/monitor.c.audit	2016-08-02 09:28:22.687299004 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/monitor.c	2016-08-02 09:28:22.696298999 +0200
Jakub Jelen b487a6
@@ -103,6 +103,7 @@
Jakub Jelen 13073f
 #include "compat.h"
Petr Lautrbach 5160c9
 #include "ssh2.h"
Petr Lautrbach 84822b
 #include "authfd.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Jakub Jelen 132f8f
 #include "match.h"
Jakub Jelen 132f8f
 #include "ssherr.h"
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
@@ -118,6 +119,8 @@ extern Buffer auth_debug;
Petr Lautrbach d3d59d
 extern int auth_debug_init;
Petr Lautrbach d3d59d
 extern Buffer loginmsg;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+extern void destroy_sensitive_data(int);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* State exported from the child */
Jakub Jelen 132f8f
 static struct sshbuf *child_state;
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
@@ -168,6 +171,11 @@ int mm_answer_gss_updatecreds(int, Buffe
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 int mm_answer_audit_event(int, Buffer *);
Petr Lautrbach d3d59d
 int mm_answer_audit_command(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_end_command(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_unsupported_body(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_kex_body(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_session_key_free_body(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_server_key_free(int, Buffer *);
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 static int monitor_read_log(struct monitor *);
Jakub Jelen b487a6
@@ -227,6 +235,10 @@ struct mon_table mon_dispatch_proto20[]
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #ifdef BSD_AUTH
Petr Lautrbach d3d59d
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Jakub Jelen b487a6
@@ -265,6 +277,11 @@ struct mon_table mon_dispatch_postauth20
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
     {0, 0, NULL}
Petr Lautrbach d3d59d
 };
Jakub Jelen b487a6
@@ -297,6 +314,10 @@ struct mon_table mon_dispatch_proto15[]
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach 190035
 #endif /* WITH_SSH1 */
Petr Lautrbach d3d59d
     {0, 0, NULL}
Jakub Jelen b487a6
@@ -310,6 +331,11 @@ struct mon_table mon_dispatch_postauth15
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach 190035
 #endif /* WITH_SSH1 */
Petr Lautrbach d3d59d
     {0, 0, NULL}
Jakub Jelen b487a6
@@ -1488,9 +1514,11 @@ mm_answer_keyverify(int sock, Buffer *m)
Petr Lautrbach d3d59d
 	Key *key;
Petr Lautrbach d3d59d
 	u_char *signature, *data, *blob;
Petr Lautrbach d3d59d
 	u_int signaturelen, datalen, bloblen;
Petr Lautrbach d3d59d
+	int type = 0;
Petr Lautrbach d3d59d
 	int verified = 0;
Petr Lautrbach d3d59d
 	int valid_data = 0;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+	type = buffer_get_int(m);
Petr Lautrbach d3d59d
 	blob = buffer_get_string(m, &bloblen);
Petr Lautrbach d3d59d
 	signature = buffer_get_string(m, &signaturelen);
Petr Lautrbach d3d59d
 	data = buffer_get_string(m, &datalen);
Jakub Jelen b487a6
@@ -1498,6 +1526,8 @@ mm_answer_keyverify(int sock, Buffer *m)
Petr Lautrbach d3d59d
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Petr Lautrbach d3d59d
 	  !monitor_allowed_key(blob, bloblen))
Petr Lautrbach d3d59d
 		fatal("%s: bad key, not previously allowed", __func__);
Petr Lautrbach d3d59d
+	if (type != key_blobtype)
Petr Lautrbach d3d59d
+		fatal("%s: bad key type", __func__);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	key = key_from_blob(blob, bloblen);
Petr Lautrbach d3d59d
 	if (key == NULL)
Jakub Jelen b487a6
@@ -1518,7 +1548,17 @@ mm_answer_keyverify(int sock, Buffer *m)
Petr Lautrbach d3d59d
 	if (!valid_data)
Petr Lautrbach d3d59d
 		fatal("%s: bad signature data blob", __func__);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-	verified = key_verify(key, signature, signaturelen, data, datalen);
Petr Lautrbach d3d59d
+	switch (key_blobtype) {
Petr Lautrbach d3d59d
+	case MM_USERKEY:
Petr Lautrbach d3d59d
+		verified = user_key_verify(key, signature, signaturelen, data, datalen);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+	case MM_HOSTKEY:
Petr Lautrbach d3d59d
+		verified = hostbased_key_verify(key, signature, signaturelen, data, datalen);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+	default:
Petr Lautrbach d3d59d
+		verified = 0;
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	debug3("%s: key %p signature %s",
Petr Lautrbach d3d59d
 	    __func__, key, (verified == 1) ? "verified" : "unverified");
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
@@ -1584,6 +1624,12 @@ mm_session_close(Session *s)
Petr Lautrbach d3d59d
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Petr Lautrbach d3d59d
 		session_pty_cleanup2(s);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (s->command != NULL) {
Petr Lautrbach d3d59d
+		debug3("%s: command %d", __func__, s->command_handle);
Petr Lautrbach d3d59d
+		session_end_command2(s);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 	session_unused(s->self);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
@@ -1866,6 +1912,8 @@ mm_answer_term(int sock, Buffer *req)
Petr Lautrbach d3d59d
 		sshpam_cleanup();
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+	destroy_sensitive_data(0);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Petr Lautrbach d3d59d
 		if (errno != EINTR)
Petr Lautrbach d3d59d
 			exit(1);
Jakub Jelen b487a6
@@ -1908,11 +1956,43 @@ mm_answer_audit_command(int socket, Buff
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	u_int len;
Petr Lautrbach d3d59d
 	char *cmd;
Petr Lautrbach d3d59d
+	Session *s;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	debug3("%s entering", __func__);
Petr Lautrbach d3d59d
 	cmd = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	/* sanity check command, if so how? */
Petr Lautrbach d3d59d
-	audit_run_command(cmd);
Petr Lautrbach d3d59d
+	s = session_new();
Petr Lautrbach d3d59d
+	if (s == NULL)
Petr Lautrbach d3d59d
+		fatal("%s: error allocating a session", __func__);
Petr Lautrbach d3d59d
+	s->command = cmd;
Petr Lautrbach d3d59d
+	s->command_handle = audit_run_command(cmd);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+	buffer_put_int(m, s->self);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	return (0);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_end_command(int socket, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int handle;
Petr Lautrbach d3d59d
+	u_int len;
Petr Lautrbach d3d59d
+	char *cmd;
Petr Lautrbach d3d59d
+	Session *s;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	debug3("%s entering", __func__);
Petr Lautrbach d3d59d
+	handle = buffer_get_int(m);
Petr Lautrbach d3d59d
+	cmd = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	s = session_by_id(handle);
Petr Lautrbach d3d59d
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
Petr Lautrbach d3d59d
+	    strcmp(s->command, cmd) != 0)
Petr Lautrbach d3d59d
+		fatal("%s: invalid handle", __func__);
Petr Lautrbach d3d59d
+	mm_session_close(s);
Petr Lautrbach 84822b
 	free(cmd);
Petr Lautrbach d3d59d
 	return (0);
Petr Lautrbach d3d59d
 }
Jakub Jelen b487a6
@@ -1972,6 +2052,7 @@ monitor_apply_keystate(struct monitor *p
Jakub Jelen 132f8f
 void
Jakub Jelen 132f8f
 mm_get_keystate(struct monitor *pmonitor)
Jakub Jelen 132f8f
 {
Jakub Jelen 132f8f
+	Buffer m;
Jakub Jelen 132f8f
 	debug3("%s: Waiting for new keys", __func__);
Petr Lautrbach d3d59d
 
Jakub Jelen 132f8f
 	if ((child_state = sshbuf_new()) == NULL)
Jakub Jelen b487a6
@@ -1979,6 +2060,21 @@ mm_get_keystate(struct monitor *pmonitor
Jakub Jelen 132f8f
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
Jakub Jelen 132f8f
 	    child_state);
Jakub Jelen 132f8f
 	debug3("%s: GOT new keys", __func__);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (compat20) {
Petr Lautrbach d3d59d
+		buffer_init(&m);
Petr Lautrbach d3d59d
+		mm_request_receive_expect(pmonitor->m_sendfd,
Petr Lautrbach d3d59d
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Petr Lautrbach d3d59d
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
Petr Lautrbach d3d59d
+		buffer_free(&m);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	/* Drain any buffered messages from the child */
Petr Lautrbach d3d59d
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
Petr Lautrbach d3d59d
+		;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
@@ -2248,3 +2344,86 @@ mm_answer_gss_updatecreds(int socket, Bu
Petr Lautrbach 5160c9
 
Petr Lautrbach 5160c9
 #endif /* GSSAPI */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_unsupported_body(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int what;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	what = buffer_get_int(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_unsupported_body(what);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_kex_body(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int ctos, len;
Petr Lautrbach 190035
+	char *cipher, *mac, *compress, *pfs;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	ctos = buffer_get_int(m);
Petr Lautrbach d3d59d
+	cipher = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	mac = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	compress = buffer_get_string(m, &len;;
Petr Lautrbach 190035
+	pfs = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	pid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+	uid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
+	audit_kex_body(ctos, cipher, mac, compress, pfs, pid, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(cipher);
Petr Lautrbach 84822b
+	free(mac);
Petr Lautrbach 84822b
+	free(compress);
Petr Lautrbach 190035
+	free(pfs);
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int ctos;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	ctos = buffer_get_int(m);
Petr Lautrbach d3d59d
+	pid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+	uid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_session_key_free_body(ctos, pid, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_server_key_free(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int len;
Petr Lautrbach d3d59d
+	char *fp;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	fp = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	pid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+	uid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_destroy_sensitive_data(fp, pid, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(fp);
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+#endif /* SSH_AUDIT_EVENTS */
Jakub Jelen b487a6
diff -up openssh-7.3p1/monitor.h.audit openssh-7.3p1/monitor.h
Jakub Jelen b487a6
--- openssh-7.3p1/monitor.h.audit	2016-08-02 09:28:22.660299019 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/monitor.h	2016-08-02 09:28:22.696298999 +0200
Petr Lautrbach 5160c9
@@ -69,7 +69,13 @@ enum monitor_reqtype {
Petr Lautrbach d3d59d
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
Petr Lautrbach d3d59d
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
Petr Lautrbach d3d59d
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
Petr Lautrbach d3d59d
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_EVENT = 112,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
Jakub Jelen 6e9574
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 };
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
diff -up openssh-7.3p1/monitor_wrap.c.audit openssh-7.3p1/monitor_wrap.c
Jakub Jelen b487a6
--- openssh-7.3p1/monitor_wrap.c.audit	2016-08-02 09:28:22.639299030 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/monitor_wrap.c	2016-08-02 09:28:22.696298999 +0200
Jakub Jelen b487a6
@@ -466,7 +466,7 @@ mm_key_allowed(enum mm_keytype type, con
Petr Lautrbach d3d59d
  */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 int
Petr Lautrbach d3d59d
-mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+mm_key_verify(enum mm_keytype type, Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	Buffer m;
Petr Lautrbach d3d59d
 	u_char *blob;
Jakub Jelen b487a6
@@ -480,6 +480,7 @@ mm_key_verify(Key *key, u_char *sig, u_i
Petr Lautrbach d3d59d
 		return (0);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, type);
Petr Lautrbach d3d59d
 	buffer_put_string(&m, blob, len);
Petr Lautrbach d3d59d
 	buffer_put_string(&m, sig, siglen);
Petr Lautrbach d3d59d
 	buffer_put_string(&m, data, datalen);
Jakub Jelen b487a6
@@ -497,6 +498,18 @@ mm_key_verify(Key *key, u_char *sig, u_i
Petr Lautrbach d3d59d
 	return (verified);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_hostbased_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	return mm_key_verify(MM_HOSTKEY, key, sig, siglen, data, datalen);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_user_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	return mm_key_verify(MM_USERKEY, key, sig, siglen, data, datalen);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Jakub Jelen 132f8f
 void
Jakub Jelen 132f8f
 mm_send_keystate(struct monitor *monitor)
Jakub Jelen 132f8f
 {
Jakub Jelen b487a6
@@ -1010,10 +1023,11 @@ mm_audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 	buffer_free(&m);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-void
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
 mm_audit_run_command(const char *command)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	Buffer m;
Petr Lautrbach d3d59d
+	int handle;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	debug3("%s entering command %s", __func__, command);
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
@@ -1021,6 +1035,26 @@ mm_audit_run_command(const char *command
Petr Lautrbach d3d59d
 	buffer_put_cstring(&m, command);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, &m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	handle = buffer_get_int(&m);
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	return (handle);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_end_command(int handle, const char *command)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	debug3("%s entering command %s", __func__, command);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, handle);
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, command);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, &m);
Petr Lautrbach d3d59d
 	buffer_free(&m);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 #endif /* SSH_AUDIT_EVENTS */
Jakub Jelen b487a6
@@ -1156,3 +1190,70 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
Petr Lautrbach 5160c9
 
Petr Lautrbach 5160c9
 #endif /* GSSAPI */
Petr Lautrbach 5160c9
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_unsupported_body(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, what);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
Petr Lautrbach d3d59d
+				  &m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
Petr Lautrbach d3d59d
+		  uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, ctos);
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, cipher);
Jakub Jelen aa9a77
+	buffer_put_cstring(&m, (mac ? mac : "<implicit>"));
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, compress);
Petr Lautrbach 190035
+	buffer_put_cstring(&m, fps);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, pid);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
Petr Lautrbach d3d59d
+				  &m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, ctos);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, pid);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, uid);
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
Petr Lautrbach d3d59d
+				  &m);
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, fp);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, pid);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+#endif /* SSH_AUDIT_EVENTS */
Jakub Jelen b487a6
diff -up openssh-7.3p1/monitor_wrap.h.audit openssh-7.3p1/monitor_wrap.h
Jakub Jelen b487a6
--- openssh-7.3p1/monitor_wrap.h.audit	2016-08-02 09:28:22.639299030 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/monitor_wrap.h	2016-08-02 09:28:22.696298999 +0200
Jakub Jelen b487a6
@@ -54,7 +54,8 @@ int mm_hostbased_key_allowed(struct pass
Jakub Jelen 5878eb
     const char *, Key *);
Jakub Jelen 5878eb
 int mm_auth_rhosts_rsa_key_allowed(struct passwd *, const char *,
Jakub Jelen 5878eb
     const char *, Key *);
Petr Lautrbach d3d59d
-int mm_key_verify(Key *, u_char *, u_int, u_char *, u_int);
Petr Lautrbach d3d59d
+int mm_hostbased_key_verify(Key *, u_char *, u_int, u_char *, u_int);
Petr Lautrbach d3d59d
+int mm_user_key_verify(Key *, u_char *, u_int, u_char *, u_int);
Petr Lautrbach d3d59d
 int mm_auth_rsa_key_allowed(struct passwd *, BIGNUM *, Key **);
Petr Lautrbach d3d59d
 int mm_auth_rsa_verify_response(Key *, BIGNUM *, u_char *);
Petr Lautrbach d3d59d
 BIGNUM *mm_auth_rsa_generate_challenge(Key *);
Jakub Jelen b487a6
@@ -81,7 +82,12 @@ void mm_sshpam_free_ctx(void *);
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 #include "audit.h"
Petr Lautrbach d3d59d
 void mm_audit_event(ssh_audit_event_t);
Petr Lautrbach d3d59d
-void mm_audit_run_command(const char *);
Petr Lautrbach d3d59d
+int mm_audit_run_command(const char *);
Petr Lautrbach d3d59d
+void mm_audit_end_command(int, const char *);
Petr Lautrbach d3d59d
+void mm_audit_unsupported_body(int);
Petr Lautrbach 190035
+void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
Petr Lautrbach d3d59d
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
Petr Lautrbach d3d59d
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 struct Session;
Jakub Jelen b487a6
diff -up openssh-7.3p1/packet.c.audit openssh-7.3p1/packet.c
Jakub Jelen b487a6
--- openssh-7.3p1/packet.c.audit	2016-08-02 09:28:22.582299062 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/packet.c	2016-08-02 09:28:22.697298998 +0200
Jakub Jelen b487a6
@@ -68,6 +68,7 @@
Jakub Jelen 132f8f
 #include "key.h"	/* typedefs XXX */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #include "xmalloc.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Petr Lautrbach d3d59d
 #include "crc32.h"
Jakub Jelen 132f8f
 #include "deattack.h"
Jakub Jelen 132f8f
 #include "compat.h"
Jakub Jelen b487a6
@@ -468,6 +469,13 @@ ssh_packet_get_connection_out(struct ssh
Jakub Jelen 132f8f
 	return ssh->state->connection_out;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static int
Petr Lautrbach d3d59d
+packet_state_has_keys (const struct session_state *state)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	return state != NULL &&
Petr Lautrbach d3d59d
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Jakub Jelen 132f8f
 /*
Jakub Jelen 132f8f
  * Returns the IP-address of the remote host as a string.  The returned
Jakub Jelen 132f8f
  * string must not be freed.
Jakub Jelen b487a6
@@ -537,13 +545,6 @@ ssh_packet_close(struct ssh *ssh)
Jakub Jelen 132f8f
 	if (!state->initialized)
Petr Lautrbach d3d59d
 		return;
Jakub Jelen 132f8f
 	state->initialized = 0;
Jakub Jelen 132f8f
-	if (state->connection_in == state->connection_out) {
Jakub Jelen 132f8f
-		shutdown(state->connection_out, SHUT_RDWR);
Jakub Jelen 132f8f
-		close(state->connection_out);
Petr Lautrbach d3d59d
-	} else {
Jakub Jelen 132f8f
-		close(state->connection_in);
Jakub Jelen 132f8f
-		close(state->connection_out);
Petr Lautrbach d3d59d
-	}
Jakub Jelen 132f8f
 	sshbuf_free(state->input);
Jakub Jelen 132f8f
 	sshbuf_free(state->output);
Jakub Jelen 132f8f
 	sshbuf_free(state->outgoing_packet);
Jakub Jelen b487a6
@@ -575,12 +576,22 @@ ssh_packet_close(struct ssh *ssh)
Jakub Jelen 132f8f
 				inflateEnd(stream);
Jakub Jelen 132f8f
 		}
Petr Lautrbach d3d59d
 	}
Jakub Jelen 132f8f
-	if ((r = cipher_cleanup(&state->send_context)) != 0)
Jakub Jelen 132f8f
-		error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
Jakub Jelen 132f8f
-	if ((r = cipher_cleanup(&state->receive_context)) != 0)
Jakub Jelen 132f8f
-		error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
Jakub Jelen 132f8f
+	if (packet_state_has_keys(state)) {
Jakub Jelen 132f8f
+		if ((r = cipher_cleanup(&state->send_context)) != 0)
Jakub Jelen 132f8f
+			error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
Jakub Jelen 132f8f
+		if ((r = cipher_cleanup(&state->receive_context)) != 0)
Jakub Jelen 132f8f
+			error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
Jakub Jelen 28ce05
+		audit_session_key_free(MODE_MAX);
Petr Lautrbach d3d59d
+	}
Jakub Jelen 13073f
 	free(ssh->remote_ipaddr);
Jakub Jelen 13073f
 	ssh->remote_ipaddr = NULL;
Jakub Jelen 132f8f
+	if (state->connection_in == state->connection_out) {
Jakub Jelen 132f8f
+		shutdown(state->connection_out, SHUT_RDWR);
Jakub Jelen 132f8f
+		close(state->connection_out);
Petr Lautrbach d3d59d
+	} else {
Jakub Jelen 132f8f
+		close(state->connection_in);
Jakub Jelen 132f8f
+		close(state->connection_out);
Petr Lautrbach d3d59d
+	}
Jakub Jelen 132f8f
 	free(ssh->state);
Jakub Jelen 132f8f
 	ssh->state = NULL;
Petr Lautrbach d3d59d
 }
Jakub Jelen b487a6
@@ -1005,6 +1016,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
Jakub Jelen 13073f
 		   (unsigned long long)state->p_read.blocks,
Jakub Jelen 13073f
 		   (unsigned long long)state->p_send.bytes,
Jakub Jelen 13073f
 		   (unsigned long long)state->p_send.blocks);
Jakub Jelen 132f8f
+		audit_session_key_free(mode);
Jakub Jelen 132f8f
 		if ((r = cipher_cleanup(cc)) != 0)
Jakub Jelen 132f8f
 			return r;
Jakub Jelen 132f8f
 		enc  = &state->newkeys[mode]->enc;
Jakub Jelen b487a6
@@ -2450,6 +2462,72 @@ ssh_packet_get_output(struct ssh *ssh)
Jakub Jelen 132f8f
 	return (void *)ssh->state->output;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static void
Jakub Jelen 132f8f
+newkeys_destroy_and_free(struct newkeys *newkeys)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (newkeys == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(newkeys->enc.name);
Petr Lautrbach d3d59d
+
Petr Lautrbach 014fe8
+	if (newkeys->mac.enabled) {
Petr Lautrbach 014fe8
+		mac_clear(&newkeys->mac);
Petr Lautrbach 84822b
+		free(newkeys->mac.name);
Petr Lautrbach 014fe8
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(newkeys->comp.name);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	newkeys_destroy(newkeys);
Petr Lautrbach 84822b
+	free(newkeys);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+packet_destroy_state(struct session_state *state)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (state == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	cipher_cleanup(&state->receive_context);
Petr Lautrbach d3d59d
+	cipher_cleanup(&state->send_context);
Petr Lautrbach d3d59d
+
Jakub Jelen 132f8f
+	buffer_free(state->input);
Jakub Jelen 132f8f
+	state->input = NULL;
Jakub Jelen 132f8f
+	buffer_free(state->output);
Jakub Jelen 132f8f
+	state->output = NULL;
Jakub Jelen 132f8f
+	buffer_free(state->outgoing_packet);
Jakub Jelen 132f8f
+	state->outgoing_packet = NULL;
Jakub Jelen 132f8f
+	buffer_free(state->incoming_packet);
Jakub Jelen 132f8f
+	state->incoming_packet = NULL;
Jakub Jelen 132f8f
+	if( state->compression_buffer ) {
Jakub Jelen 132f8f
+		buffer_free(state->compression_buffer);
Jakub Jelen 132f8f
+		state->compression_buffer = NULL;
Jakub Jelen 132f8f
+	}
Petr Lautrbach d3d59d
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
Petr Lautrbach d3d59d
+	state->newkeys[MODE_IN] = NULL;
Petr Lautrbach d3d59d
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
Petr Lautrbach d3d59d
+	state->newkeys[MODE_OUT] = NULL;
Petr Lautrbach d3d59d
+	mac_destroy(state->packet_discard_mac);
Petr Lautrbach d3d59d
+//	TAILQ_HEAD(, packet) outgoing;
Petr Lautrbach d3d59d
+//	memset(state, 0, sizeof(state));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+packet_destroy_all(int audit_it, int privsep)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (audit_it)
Jakub Jelen 28ce05
+		audit_it = (active_state != NULL && packet_state_has_keys(active_state->state));
Jakub Jelen 12cf3e
+	if (active_state != NULL)
Jakub Jelen 12cf3e
+		packet_destroy_state(active_state->state);
Petr Lautrbach d3d59d
+	if (audit_it) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+		if (privsep)
Jakub Jelen 28ce05
+			audit_session_key_free(MODE_MAX);
Petr Lautrbach d3d59d
+		else
Jakub Jelen 28ce05
+			audit_session_key_free_body(MODE_MAX, getpid(), getuid());
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
 /* Reset after_authentication and reset compression in post-auth privsep */
Jakub Jelen 13073f
 static int
Jakub Jelen 13073f
 ssh_packet_set_postauth(struct ssh *ssh)
Jakub Jelen b487a6
diff -up openssh-7.3p1/packet.h.audit openssh-7.3p1/packet.h
Jakub Jelen b487a6
--- openssh-7.3p1/packet.h.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/packet.h	2016-08-02 09:28:22.697298998 +0200
Jakub Jelen b487a6
@@ -204,4 +204,5 @@ extern struct ssh *active_state;
Jakub Jelen 132f8f
 # undef EC_POINT
Jakub Jelen 132f8f
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+void	 packet_destroy_all(int, int);
Petr Lautrbach d3d59d
 #endif				/* PACKET_H */
Jakub Jelen b487a6
diff -up openssh-7.3p1/sandbox-seccomp-filter.c.audit openssh-7.3p1/sandbox-seccomp-filter.c
Jakub Jelen b487a6
--- openssh-7.3p1/sandbox-seccomp-filter.c.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/sandbox-seccomp-filter.c	2016-08-02 09:28:22.697298998 +0200
Jakub Jelen b487a6
@@ -159,6 +159,12 @@ static const struct sock_filter preauth_
Jakub Jelen 535d34
 #ifdef __NR_gettimeofday
Jakub Jelen 535d34
 	SC_ALLOW(gettimeofday),
Jakub Jelen 535d34
 #endif
Jakub Jelen 535d34
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 535d34
+	SC_ALLOW(getuid),
Jakub Jelen 535d34
+#ifdef __NR_getuid32 /* not defined on x86_64 */
Jakub Jelen 535d34
+	SC_ALLOW(getuid32),
Jakub Jelen 535d34
+#endif
Jakub Jelen 535d34
+#endif
Jakub Jelen 535d34
 #ifdef __NR_madvise
Jakub Jelen 535d34
 	SC_ALLOW(madvise),
Jakub Jelen 535d34
 #endif
Jakub Jelen b487a6
diff -up openssh-7.3p1/session.c.audit openssh-7.3p1/session.c
Jakub Jelen b487a6
--- openssh-7.3p1/session.c.audit	2016-08-02 09:28:22.691299001 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/session.c	2016-08-02 09:28:22.698298997 +0200
Jakub Jelen b487a6
@@ -144,7 +144,7 @@ extern int log_stderr;
Petr Lautrbach d3d59d
 extern int debug_flag;
Petr Lautrbach d3d59d
 extern u_int utmp_len;
Petr Lautrbach d3d59d
 extern int startup_pipe;
Petr Lautrbach d3d59d
-extern void destroy_sensitive_data(void);
Petr Lautrbach d3d59d
+extern void destroy_sensitive_data(int);
Petr Lautrbach d3d59d
 extern Buffer loginmsg;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /* original command from peer. */
Jakub Jelen b487a6
@@ -757,6 +757,14 @@ do_exec_pty(Session *s, const char *comm
Petr Lautrbach d3d59d
 	/* Parent.  Close the slave side of the pseudo tty. */
Petr Lautrbach d3d59d
 	close(ttyfd);
Petr Lautrbach d3d59d
 
Jakub Jelen 28ce05
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
Petr Lautrbach d3d59d
+	/* do_login in the child did not affect state in this process,
Petr Lautrbach d3d59d
+	   compensate.  From an architectural standpoint, this is extremely
Petr Lautrbach d3d59d
+	   ugly. */
Petr Lautrbach d3d59d
+	if (!(options.use_login && command == NULL))
Petr Lautrbach d3d59d
+		audit_count_session_open();
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	/* Enter interactive session. */
Petr Lautrbach d3d59d
 	s->ptymaster = ptymaster;
Petr Lautrbach d3d59d
 	packet_set_interactive(1, 
Jakub Jelen b487a6
@@ -882,15 +890,19 @@ do_exec(Session *s, const char *command)
Jakub Jelen 13073f
 	    s->self);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (s->command != NULL || s->command_handle != -1)
Petr Lautrbach d3d59d
+		fatal("do_exec: command already set");
Petr Lautrbach d3d59d
 	if (command != NULL)
Petr Lautrbach d3d59d
-		PRIVSEP(audit_run_command(command));
Petr Lautrbach d3d59d
+		s->command = xstrdup(command);
Petr Lautrbach d3d59d
 	else if (s->ttyfd == -1) {
Petr Lautrbach d3d59d
 		char *shell = s->pw->pw_shell;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
Petr Lautrbach d3d59d
 			shell =_PATH_BSHELL;
Petr Lautrbach d3d59d
-		PRIVSEP(audit_run_command(shell));
Petr Lautrbach d3d59d
+		s->command = xstrdup(shell);
Petr Lautrbach d3d59d
 	}
Jakub Jelen acf988
+	if (s->command != NULL && s->ptyfd == -1)
Petr Lautrbach d3d59d
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 	if (s->ttyfd != -1)
Petr Lautrbach d3d59d
 		ret = do_exec_pty(s, command);
Jakub Jelen b487a6
@@ -1756,7 +1768,10 @@ do_child(Session *s, const char *command
Petr Lautrbach d3d59d
 	int r = 0;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* remove hostkey from the child's memory */
Petr Lautrbach d3d59d
-	destroy_sensitive_data();
Petr Lautrbach d3d59d
+	destroy_sensitive_data(1);
Petr Lautrbach d3d59d
+	/* Don't audit this - both us and the parent would be talking to the
Petr Lautrbach d3d59d
+	   monitor over a single socket, with no synchronization. */
Petr Lautrbach d3d59d
+	packet_destroy_all(0, 1);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* Force a password change */
Petr Lautrbach d3d59d
 	if (s->authctxt->force_pwchange) {
Jakub Jelen b487a6
@@ -1986,6 +2001,7 @@ session_unused(int id)
Petr Lautrbach d3d59d
 	sessions[id].ttyfd = -1;
Petr Lautrbach d3d59d
 	sessions[id].ptymaster = -1;
Petr Lautrbach d3d59d
 	sessions[id].x11_chanids = NULL;
Petr Lautrbach d3d59d
+	sessions[id].command_handle = -1;
Petr Lautrbach d3d59d
 	sessions[id].next_unused = sessions_first_unused;
Petr Lautrbach d3d59d
 	sessions_first_unused = id;
Petr Lautrbach d3d59d
 }
Jakub Jelen b487a6
@@ -2068,6 +2084,19 @@ session_open(Authctxt *authctxt, int cha
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 Session *
Petr Lautrbach d3d59d
+session_by_id(int id)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (id >= 0 && id < sessions_nalloc) {
Petr Lautrbach d3d59d
+		Session *s = &sessions[id];
Petr Lautrbach d3d59d
+		if (s->used)
Petr Lautrbach d3d59d
+			return s;
Petr Lautrbach d3d59d
+	}
Jakub Jelen 28ce05
+	debug("%s: unknown id %d", __func__, id);
Petr Lautrbach d3d59d
+	session_dump();
Petr Lautrbach d3d59d
+	return NULL;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+Session *
Petr Lautrbach d3d59d
 session_by_tty(char *tty)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	int i;
Jakub Jelen b487a6
@@ -2590,6 +2619,32 @@ session_exit_message(Session *s, int sta
Petr Lautrbach d3d59d
 		chan_write_failed(c);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+session_end_command2(Session *s)
Petr Lautrbach d3d59d
+{
Jakub Jelen acf988
+	if (s->command != NULL) {
Jakub Jelen f78ec0
+		if (s->command_handle != -1)
Jakub Jelen acf988
+			audit_end_command(s->command_handle, s->command);
Petr Lautrbach 84822b
+		free(s->command);
Petr Lautrbach d3d59d
+		s->command = NULL;
Petr Lautrbach d3d59d
+		s->command_handle = -1;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+session_end_command(Session *s)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (s->command != NULL) {
Jakub Jelen f78ec0
+		if (s->command_handle != -1)
Jakub Jelen f78ec0
+			PRIVSEP(audit_end_command(s->command_handle, s->command));
Petr Lautrbach 84822b
+		free(s->command);
Petr Lautrbach d3d59d
+		s->command = NULL;
Petr Lautrbach d3d59d
+		s->command_handle = -1;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 session_close(Session *s)
Petr Lautrbach d3d59d
 {
Jakub Jelen b487a6
@@ -2604,6 +2659,10 @@ session_close(Session *s)
Jakub Jelen 13073f
 
Petr Lautrbach d3d59d
 	if (s->ttyfd != -1)
Petr Lautrbach d3d59d
 		session_pty_cleanup(s);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (s->command)
Petr Lautrbach d3d59d
+		session_end_command(s);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach 84822b
 	free(s->term);
Petr Lautrbach 84822b
 	free(s->display);
Petr Lautrbach 84822b
 	free(s->x11_chanids);
Jakub Jelen b487a6
@@ -2818,6 +2877,15 @@ do_authenticated2(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	server_loop2(authctxt);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+do_cleanup_one_session(Session *s)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	session_pty_cleanup2(s);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	session_end_command2(s);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 do_cleanup(Authctxt *authctxt)
Petr Lautrbach d3d59d
 {
Jakub Jelen b487a6
@@ -2869,7 +2937,7 @@ do_cleanup(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	 * or if running in monitor.
Petr Lautrbach d3d59d
 	 */
Petr Lautrbach d3d59d
 	if (!use_privsep || mm_is_monitor())
Petr Lautrbach d3d59d
-		session_destroy_all(session_pty_cleanup2);
Petr Lautrbach d3d59d
+		session_destroy_all(do_cleanup_one_session);
Petr Lautrbach d3d59d
 }
Jakub Jelen 5878eb
 
Jakub Jelen 5878eb
 /* Return a name for the remote host that fits inside utmp_size */
Jakub Jelen b487a6
diff -up openssh-7.3p1/session.h.audit openssh-7.3p1/session.h
Jakub Jelen b487a6
--- openssh-7.3p1/session.h.audit	2016-07-28 00:54:27.000000000 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/session.h	2016-08-02 09:28:22.698298997 +0200
Petr Lautrbach 5160c9
@@ -61,6 +61,12 @@ struct Session {
Petr Lautrbach d3d59d
 		char	*name;
Petr Lautrbach d3d59d
 		char	*val;
Petr Lautrbach d3d59d
 	} *env;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	/* exec */
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	int	command_handle;
Petr Lautrbach d3d59d
+	char	*command;
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 };
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void	 do_authenticated(Authctxt *);
Petr Lautrbach 5160c9
@@ -73,8 +79,10 @@ void	 session_close_by_pid(pid_t, int);
Petr Lautrbach d3d59d
 void	 session_close_by_channel(int, void *);
Petr Lautrbach d3d59d
 void	 session_destroy_all(void (*)(Session *));
Petr Lautrbach d3d59d
 void	 session_pty_cleanup2(Session *);
Petr Lautrbach d3d59d
+void	 session_end_command2(Session *);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 Session	*session_new(void);
Petr Lautrbach d3d59d
+Session *session_by_id(int);
Petr Lautrbach d3d59d
 Session	*session_by_tty(char *);
Petr Lautrbach d3d59d
 void	 session_close(Session *);
Petr Lautrbach d3d59d
 void	 do_setusercontext(struct passwd *);
Jakub Jelen b487a6
diff -up openssh-7.3p1/sshd.c.audit openssh-7.3p1/sshd.c
Jakub Jelen b487a6
--- openssh-7.3p1/sshd.c.audit	2016-08-02 09:28:22.667299015 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/sshd.c	2016-08-02 09:28:22.698298997 +0200
Jakub Jelen 13073f
@@ -121,6 +121,7 @@
Jakub Jelen 13073f
 #include "ssh-gss.h"
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #include "monitor_wrap.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Petr Lautrbach d3d59d
 #include "ssh-sandbox.h"
Petr Lautrbach d3d59d
 #include "version.h"
Jakub Jelen 132f8f
 #include "ssherr.h"
Jakub Jelen 13073f
@@ -260,7 +261,7 @@ Buffer loginmsg;
Petr Lautrbach d3d59d
 struct passwd *privsep_pw = NULL;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /* Prototypes for various functions defined later in this file. */
Petr Lautrbach d3d59d
-void destroy_sensitive_data(void);
Petr Lautrbach d3d59d
+void destroy_sensitive_data(int);
Petr Lautrbach d3d59d
 void demote_sensitive_data(void);
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
 #ifdef WITH_SSH1
Jakub Jelen 13073f
@@ -281,6 +282,15 @@ close_listen_socks(void)
Petr Lautrbach d3d59d
 	num_listen_socks = -1;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * Is this process listening for clients (i.e. not specific to any specific
Petr Lautrbach d3d59d
+ * client connection?)
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+int listening_for_clients(void)
Petr Lautrbach d3d59d
+{
Jakub Jelen b16516
+	return num_listen_socks >= 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 static void
Petr Lautrbach d3d59d
 close_startup_pipes(void)
Petr Lautrbach d3d59d
 {
Jakub Jelen b487a6
@@ -566,22 +576,49 @@ sshd_exchange_identification(struct ssh
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-/* Destroy the host and server keys.  They will no longer be needed. */
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
Petr Lautrbach d3d59d
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
-destroy_sensitive_data(void)
Petr Lautrbach d3d59d
+destroy_sensitive_data(int privsep)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	int i;
Jakub Jelen 28ce05
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
 
Jakub Jelen 28ce05
+	pid = getpid();
Jakub Jelen 28ce05
+	uid = getuid();
Jakub Jelen 28ce05
+#endif
Petr Lautrbach d3d59d
 	if (sensitive_data.server_key) {
Petr Lautrbach d3d59d
 		key_free(sensitive_data.server_key);
Petr Lautrbach d3d59d
 		sensitive_data.server_key = NULL;
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 	for (i = 0; i < options.num_host_key_files; i++) {
Petr Lautrbach d3d59d
 		if (sensitive_data.host_keys[i]) {
Petr Lautrbach d3d59d
+			char *fp;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+			if (key_is_private(sensitive_data.host_keys[i]))
Jakub Jelen 132f8f
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
Petr Lautrbach d3d59d
+			else
Petr Lautrbach d3d59d
+				fp = NULL;
Petr Lautrbach d3d59d
 			key_free(sensitive_data.host_keys[i]);
Petr Lautrbach d3d59d
 			sensitive_data.host_keys[i] = NULL;
Petr Lautrbach d3d59d
+			if (fp != NULL) {
Jakub Jelen 28ce05
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+				if (privsep)
Petr Lautrbach d3d59d
+					PRIVSEP(audit_destroy_sensitive_data(fp,
Petr Lautrbach d3d59d
+						pid, uid));
Petr Lautrbach d3d59d
+				else
Petr Lautrbach d3d59d
+					audit_destroy_sensitive_data(fp,
Petr Lautrbach d3d59d
+						pid, uid);
Jakub Jelen 28ce05
+#endif
Petr Lautrbach 84822b
+				free(fp);
Petr Lautrbach d3d59d
+			}
Petr Lautrbach d3d59d
 		}
Petr Lautrbach d3d59d
-		if (sensitive_data.host_certificates[i]) {
Petr Lautrbach d3d59d
+		if (sensitive_data.host_certificates
Petr Lautrbach d3d59d
+		    && sensitive_data.host_certificates[i]) {
Petr Lautrbach d3d59d
 			key_free(sensitive_data.host_certificates[i]);
Petr Lautrbach d3d59d
 			sensitive_data.host_certificates[i] = NULL;
Petr Lautrbach d3d59d
 		}
Jakub Jelen b487a6
@@ -596,7 +633,13 @@ demote_sensitive_data(void)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	Key *tmp;
Jakub Jelen 28ce05
 	int i;
Jakub Jelen 28ce05
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+	pid = getpid();
Petr Lautrbach d3d59d
+	uid = getuid();
Jakub Jelen 28ce05
+#endif
Jakub Jelen 28ce05
 	if (sensitive_data.server_key) {
Jakub Jelen 28ce05
 		tmp = key_demote(sensitive_data.server_key);
Jakub Jelen 28ce05
 		key_free(sensitive_data.server_key);
Jakub Jelen b487a6
@@ -605,11 +648,23 @@ demote_sensitive_data(void)
Jakub Jelen 28ce05
 
Petr Lautrbach d3d59d
 	for (i = 0; i < options.num_host_key_files; i++) {
Petr Lautrbach d3d59d
 		if (sensitive_data.host_keys[i]) {
Petr Lautrbach d3d59d
+			char *fp;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+			if (key_is_private(sensitive_data.host_keys[i]))
Jakub Jelen 132f8f
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
Petr Lautrbach d3d59d
+			else
Petr Lautrbach d3d59d
+				fp = NULL;
Petr Lautrbach d3d59d
 			tmp = key_demote(sensitive_data.host_keys[i]);
Petr Lautrbach d3d59d
 			key_free(sensitive_data.host_keys[i]);
Petr Lautrbach d3d59d
 			sensitive_data.host_keys[i] = tmp;
Petr Lautrbach d3d59d
 			if (tmp->type == KEY_RSA1)
Petr Lautrbach d3d59d
 				sensitive_data.ssh1_host_key = tmp;
Petr Lautrbach d3d59d
+			if (fp != NULL) {
Jakub Jelen 28ce05
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+				audit_destroy_sensitive_data(fp, pid, uid);
Jakub Jelen 28ce05
+#endif
Petr Lautrbach 84822b
+				free(fp);
Petr Lautrbach d3d59d
+			}
Petr Lautrbach d3d59d
 		}
Petr Lautrbach d3d59d
 		/* Certs do not need demotion */
Petr Lautrbach d3d59d
 	}
Jakub Jelen b487a6
@@ -681,7 +736,7 @@ privsep_preauth(Authctxt *authctxt)
Petr Lautrbach 82d2be
 
Petr Lautrbach 82d2be
 	if (use_privsep == PRIVSEP_ON)
Petr Lautrbach 5160c9
 		box = ssh_sandbox_init(pmonitor);
Petr Lautrbach 82d2be
-	pid = fork();
Petr Lautrbach 82d2be
+	pmonitor->m_pid = pid = fork();
Petr Lautrbach 82d2be
 	if (pid == -1) {
Petr Lautrbach 82d2be
 		fatal("fork of unprivileged child failed");
Petr Lautrbach 82d2be
 	} else if (pid != 0) {
Jakub Jelen b487a6
@@ -766,6 +821,12 @@ privsep_postauth(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	else if (pmonitor->m_pid != 0) {
Petr Lautrbach d3d59d
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
Petr Lautrbach d3d59d
 		buffer_clear(&loginmsg);
Jakub Jelen 12cf3e
+		if (*pmonitor->m_pkex != NULL ){
Jakub Jelen 12cf3e
+			newkeys_destroy((*pmonitor->m_pkex)->newkeys[MODE_OUT]);
Jakub Jelen 12cf3e
+			newkeys_destroy((*pmonitor->m_pkex)->newkeys[MODE_IN]);
Jakub Jelen 12cf3e
+			audit_session_key_free_body(2, getpid(), getuid());
Jakub Jelen 12cf3e
+			packet_destroy_all(0, 0);
Jakub Jelen 12cf3e
+		}
Petr Lautrbach d3d59d
 		monitor_child_postauth(pmonitor);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 		/* NEVERREACHED */
Jakub Jelen b487a6
@@ -1310,6 +1371,7 @@ server_accept_loop(int *sock_in, int *so
Petr Lautrbach d3d59d
 		if (received_sigterm) {
Petr Lautrbach d3d59d
 			logit("Received signal %d; terminating.",
Petr Lautrbach d3d59d
 			    (int) received_sigterm);
Petr Lautrbach d3d59d
+			destroy_sensitive_data(0);
Petr Lautrbach d3d59d
 			close_listen_socks();
Jakub Jelen 132f8f
 			if (options.pid_file != NULL)
Jakub Jelen 132f8f
 				unlink(options.pid_file);
Jakub Jelen b487a6
@@ -2335,6 +2397,7 @@ main(int ac, char **av)
Petr Lautrbach d3d59d
 	 */
Petr Lautrbach d3d59d
 	if (use_privsep) {
Petr Lautrbach d3d59d
 		mm_send_keystate(pmonitor);
Petr Lautrbach d3d59d
+		packet_destroy_all(1, 1);
Petr Lautrbach d3d59d
 		exit(0);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 
Jakub Jelen b487a6
@@ -2380,7 +2443,7 @@ main(int ac, char **av)
Petr Lautrbach d3d59d
 		privsep_postauth(authctxt);
Petr Lautrbach d3d59d
 		/* the monitor process [priv] will not return */
Petr Lautrbach d3d59d
 		if (!compat20)
Petr Lautrbach d3d59d
-			destroy_sensitive_data();
Petr Lautrbach d3d59d
+			destroy_sensitive_data(0);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	packet_set_timeout(options.client_alive_interval,
Jakub Jelen b487a6
@@ -2394,6 +2457,9 @@ main(int ac, char **av)
Petr Lautrbach d3d59d
 	do_authenticated(authctxt);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* The connection has been terminated. */
Petr Lautrbach d3d59d
+	packet_destroy_all(1, 1);
Petr Lautrbach d3d59d
+	destroy_sensitive_data(1);
Petr Lautrbach d3d59d
+
Jakub Jelen 132f8f
 	packet_get_bytes(&ibytes, &obytes);
Petr Lautrbach d3d59d
 	verbose("Transferred: sent %llu, received %llu bytes",
Jakub Jelen 132f8f
 	    (unsigned long long)obytes, (unsigned long long)ibytes);
Jakub Jelen b487a6
@@ -2556,6 +2622,10 @@ do_ssh1_kex(void)
Petr Lautrbach d3d59d
 		if (cookie[i] != packet_get_char())
Petr Lautrbach d3d59d
 			packet_disconnect("IP Spoofing check bytes do not match.");
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach 190035
+	audit_kex(2, cipher_name(cipher_type), "crc", "none", "none");
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	debug("Encryption type: %.200s", cipher_name(cipher_type));
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* Get the encrypted integer. */
Jakub Jelen b487a6
@@ -2616,7 +2686,7 @@ do_ssh1_kex(void)
Petr Lautrbach d3d59d
 	}
Jakub Jelen 132f8f
 
Petr Lautrbach d3d59d
 	/* Destroy the private and public keys. No longer. */
Petr Lautrbach d3d59d
-	destroy_sensitive_data();
Jakub Jelen 12cf3e
+	destroy_sensitive_data(1);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	if (use_privsep)
Petr Lautrbach d3d59d
 		mm_ssh1_session_id(session_id);
Jakub Jelen b487a6
@@ -2794,6 +2864,16 @@ do_ssh2_kex(void)
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 cleanup_exit(int i)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
+	static int in_cleanup = 0;
Petr Lautrbach d3d59d
+	int is_privsep_child;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	/* cleanup_exit can be called at the very least from the privsep
Petr Lautrbach d3d59d
+	   wrappers used for auditing.  Make sure we don't recurse
Petr Lautrbach d3d59d
+	   indefinitely. */
Petr Lautrbach d3d59d
+	if (in_cleanup)
Petr Lautrbach d3d59d
+		_exit(i);
Petr Lautrbach d3d59d
+	in_cleanup = 1;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	if (the_authctxt) {
Petr Lautrbach d3d59d
 		do_cleanup(the_authctxt);
Petr Lautrbach 190035
 		if (use_privsep && privsep_is_preauth &&
Jakub Jelen b487a6
@@ -2805,9 +2885,14 @@ cleanup_exit(int i)
Petr Lautrbach d3d59d
 				    pmonitor->m_pid, strerror(errno));
Petr Lautrbach d3d59d
 		}
Petr Lautrbach d3d59d
 	}
Petr Lautrbach 82d2be
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
Petr Lautrbach d3d59d
+	if (sensitive_data.host_keys != NULL)
Petr Lautrbach d3d59d
+		destroy_sensitive_data(is_privsep_child);
Petr Lautrbach d3d59d
+	packet_destroy_all(1, is_privsep_child);
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
Petr Lautrbach d3d59d
-	if (!use_privsep || mm_is_monitor())
Petr Lautrbach d3d59d
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
Petr Lautrbach d3d59d
+	    (!use_privsep || mm_is_monitor()))
Petr Lautrbach d3d59d
 		audit_event(SSH_CONNECTION_ABANDON);
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 	_exit(i);
Jakub Jelen b487a6
diff -up openssh-7.3p1/sshkey.c.audit openssh-7.3p1/sshkey.c
Jakub Jelen b487a6
--- openssh-7.3p1/sshkey.c.audit	2016-08-02 09:28:22.689299002 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/sshkey.c	2016-08-02 09:28:22.699298997 +0200
Jakub Jelen b487a6
@@ -304,6 +304,33 @@ sshkey_type_is_valid_ca(int type)
Petr Lautrbach 190035
 }
Petr Lautrbach 190035
 
Petr Lautrbach 190035
 int
Petr Lautrbach 190035
+sshkey_is_private(const struct sshkey *k)
Petr Lautrbach 190035
+{
Petr Lautrbach 190035
+      switch (k->type) {
Jakub Jelen da62b7
+#ifdef WITH_OPENSSL
Petr Lautrbach 190035
+      case KEY_RSA_CERT:
Petr Lautrbach 190035
+      case KEY_RSA1:
Petr Lautrbach 190035
+      case KEY_RSA:
Petr Lautrbach 190035
+              return k->rsa->d != NULL;
Petr Lautrbach 190035
+      case KEY_DSA_CERT:
Petr Lautrbach 190035
+      case KEY_DSA:
Petr Lautrbach 190035
+              return k->dsa->priv_key != NULL;
Petr Lautrbach 190035
+#ifdef OPENSSL_HAS_ECC
Petr Lautrbach 190035
+      case KEY_ECDSA_CERT:
Petr Lautrbach 190035
+      case KEY_ECDSA:
Petr Lautrbach 190035
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
Jakub Jelen da62b7
+#endif /* OPENSSL_HAS_ECC */
Jakub Jelen da62b7
+#endif /* WITH_OPENSSL */
Petr Lautrbach 190035
+      case KEY_ED25519_CERT:
Petr Lautrbach 190035
+      case KEY_ED25519:
Petr Lautrbach 190035
+              return (k->ed25519_pk != NULL);
Petr Lautrbach 190035
+      default:
Petr Lautrbach 190035
+              /* fatal("key_is_private: bad key type %d", k->type); */
Petr Lautrbach 190035
+              return 0;
Petr Lautrbach 190035
+      }
Petr Lautrbach 190035
+}
Petr Lautrbach 190035
+
Petr Lautrbach 190035
+int
Petr Lautrbach 190035
 sshkey_is_cert(const struct sshkey *k)
Petr Lautrbach 190035
 {
Petr Lautrbach 190035
 	if (k == NULL)
Jakub Jelen b487a6
diff -up openssh-7.3p1/sshkey.h.audit openssh-7.3p1/sshkey.h
Jakub Jelen b487a6
--- openssh-7.3p1/sshkey.h.audit	2016-08-02 09:28:22.689299002 +0200
Jakub Jelen b487a6
+++ openssh-7.3p1/sshkey.h	2016-08-02 09:28:22.699298997 +0200
Jakub Jelen b487a6
@@ -134,6 +134,7 @@ u_int		 sshkey_size(const struct sshkey
Petr Lautrbach 190035
 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
Petr Lautrbach 190035
 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
Petr Lautrbach 190035
 int	 sshkey_type_from_name(const char *);
Petr Lautrbach 190035
+int	 sshkey_is_private(const struct sshkey *);
Petr Lautrbach 190035
 int	 sshkey_is_cert(const struct sshkey *);
Petr Lautrbach 190035
 int	 sshkey_type_is_cert(int);
Petr Lautrbach 190035
 int	 sshkey_type_plain(int);