Skip to contentSkip to conversation

Implement Trusted Security Event Modeling.

PATCH In review Greg Wettstein 3 participants 41 messages Aug 26, 2024 – Mar 3, 2025 Page 2 of 2

Subsystems (2)
lore
Share & tools
lore
#32 lore raw

PATCH v4 6/14 Implement TSEM control plane.

In reply to Greg Wettstein

The fs.c file contains the implementation of the TSEM control plane that is surfaced through the following directory heirarchy in the securityfs filesystem mount

/sys/kernel/security/tsem

The following file documents the interfaces provided by the control plane:

Documentation/ABI/testing/tsem

The directory heirarchy partitions into the following two branches

/sys/kernel/security/tsem/internal_tma

/sys/kernel/security/tsem/external_tma

Where the internal_tma directory surfaces the characteristics of the in kernel Trusted Modeling Agent implementation.

When an externally modeled namespace is created a file is created in the external_tma directory that is named for the security context identifier of the namespace that was created. An external trust orchestrator uses this file to read security events that occur in the context of the namespace.

This file also contains the functionality that generates the JSON encoded security event descriptions. These descriptions encode the information used as input to TSEM security models.

---
 security/tsem/fs.c | 2304 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 2304 insertions(+)
 create mode 100644 security/tsem/fs.c
security/tsem/fs.c new file +2304 -0
new file mode 100644
index 000000000000..04762dd5fd5c
@@ -0,0 +1,2304 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+ *
+ * This file implements the TSEM control plane that is surfaced
+ * through the following directory heirarchy:
+ *
+ * /sys/kernel/security/tsem
+ *
+ * The TSEM ABI documentation documents the directories and files that
+ * are implemented in this directory heirarchy.
+ *
+ * A significant portion of the functions in this file are used to
+ * generate the JSON encoding of a security event description.  This
+ * encoding is surfaced to the tsem_export_show() function through the
+ * tsem_fs_show_trajectory() function.  This latter function is also
+ * used by the trajectory and forensics pseudo files that output the
+ * characteristics of security events modeled by the internal trusted
+ * modeling agent implementation.
+ *
+ * The pseudo-files implemented are context sensitive in that they
+ * provide output describing the security modeling namespace that the
+ * process that accesses the file is running in.
+ */
+
+#include <linux/seq_file.h>
+#include <linux/poll.h>
+
+#include "tsem.h"
+#include "nsmgr.h"
+
+static struct dentry *tsem_dir;
+static struct dentry *control;
+static struct dentry *id;
+static struct dentry *aggregate;
+static struct dentry *internal_tma;
+static struct dentry *model;
+static struct dentry *forensics;
+static struct dentry *forensics_counts;
+static struct dentry *forensics_coeff;
+static struct dentry *trajectory;
+static struct dentry *trajectory_counts;
+static struct dentry *trajectory_coeff;
+static struct dentry *measurement;
+static struct dentry *state;
+static struct dentry *external_tma;
+
+struct control_commands {
+	char *cmd;
+	enum tsem_control_type type;
+};
+
+static const char * const control_commands[] = {
+	"internal",
+	"external",
+	"export",
+	"enforce",
+	"seal",
+	"trusted",
+	"untrusted",
+	"state",
+	"pseudonym",
+	"base",
+	"lock"
+};
+
+enum namespace_argument_type {
+	NS_MODEL = 0,
+	NS_REF,
+	NS_DIGEST,
+	NS_KEY,
+	NS_CACHE
+};
+
+static const char * const namespace_arguments[] = {
+	"model",
+	"nsref",
+	"digest",
+	"key",
+	"cache"
+};
+
+enum control_argument_type {
+	CONTROL_KEY = 0,
+	CONTROL_PID
+};
+
+static const char * const control_arguments[] = {
+	"key",
+	"pid"
+};
+
+static bool can_access_fs(void)
+{
+	struct tsem_context *ctx = tsem_context(current);
+
+	if (ctx->external)
+		return false;
+	if (capable(CAP_MAC_ADMIN))
+		return true;
+	if (ctx->sealed)
+		return false;
+	return true;
+}
+
+static int control_COE(unsigned long cmd, pid_t pid, char *keystr)
+{
+	bool wakeup = false;
+	int retn = -ESRCH;
+	u8 event_key[HASH_MAX_DIGESTSIZE];
+	struct task_struct *COE;
+	struct tsem_task *task;
+	struct tsem_task *tma = tsem_task(current);
+
+	rcu_read_lock();
+	COE = find_task_by_vpid(pid);
+	if (COE != NULL) {
+		task = tsem_task(COE);
+		if (tsem_context(COE)->id != tma->tma_for_ns) {
+			retn = -EINVAL;
+			goto done;
+		}
+
+		retn = tsem_ns_event_key(task->task_key, keystr, event_key);
+		if (retn)
+			goto done;
+
+		if (memcmp(tma->task_key, event_key, tsem_digestsize())) {
+			retn = -EINVAL;
+			goto done;
+		}
+
+		if (cmd == TSEM_CONTROL_UNTRUSTED)
+			task->trust_status = TSEM_TASK_UNTRUSTED;
+		if (cmd == TSEM_CONTROL_TRUSTED) {
+			task->trust_status &= ~TSEM_TASK_TRUST_PENDING;
+			if (tsem_task_trusted(COE))
+				task->trust_status = TSEM_TASK_TRUSTED;
+		}
+		retn = 0;
+		wakeup = true;
+	}
+
+ done:
+	rcu_read_unlock();
+
+	if (retn == -EINVAL)
+		pr_warn("tsem: Invalid process release request.\n");
+
+	if (wakeup)
+		wake_up_process(COE);
+
+	return retn;
+}
+
+static int config_COE(unsigned long cmd, char *arg)
+{
+	char **argv, *argp, *key = NULL;
+	int argc, retn = -EINVAL;
+	unsigned int lp;
+	long pid = 0;
+	enum control_argument_type control_arg;
+
+	if (!*arg)
+		return retn;
+
+	argv = argv_split(GFP_KERNEL, arg, &argc);
+	if (!argv)
+		return -ENOMEM;
+
+	for (lp = 0; lp < argc; ++lp) {
+		argp = strchr(argv[lp], '=');
+		if (!argp)
+			goto done;
+		*argp++ = '\0';
+
+		control_arg = match_string(control_arguments,
+					   ARRAY_SIZE(control_arguments),
+					   argv[lp]);
+		if (control_arg < 0)
+			goto done;
+
+		switch (control_arg) {
+		case CONTROL_KEY:
+			key = argp;
+			if (strlen(key) != tsem_digestsize()*2)
+				goto done;
+			break;
+		case CONTROL_PID:
+			if (kstrtol(argp, 0, &pid))
+				goto done;
+			break;
+		}
+	}
+
+	if (!key || !pid)
+		goto done;
+	retn = control_COE(cmd, pid, key);
+
+ done:
+	argv_free(argv);
+	return retn;
+}
+
+static int config_context(unsigned long cmd, char *bufr)
+{
+	int retn = -EINVAL;
+	unsigned int lp;
+	struct tsem_context *ctx = tsem_context(current);
+
+	if (ctx->sealed)
+		return -EPERM;
+
+	if (cmd == TSEM_CONTROL_SEAL) {
+		ctx->sealed = true;
+		retn = 0;
+	}
+
+	if (cmd == TSEM_CONTROL_ENFORCE) {
+		for (lp = 0; lp < ARRAY_SIZE(tsem_root_actions); ++lp)
+			ctx->actions[lp] = TSEM_ACTION_EPERM;
+		retn = 0;
+	}
+
+	return retn;
+}
+
+static int config_point(enum tsem_control_type type, char *arg)
+{
+	char *argp;
+	int retn = -EINVAL;
+	u8 mapping[HASH_MAX_DIGESTSIZE];
+
+	if (!arg)
+		goto done;
+
+	argp = strchr(arg, '=');
+	if (!argp)
+		goto done;
+	*argp++ = '\0';
+
+	if (strcmp(arg, "value"))
+		goto done;
+
+	if (strlen(argp) != tsem_digestsize()*2)
+		goto done;
+	if (hex2bin(mapping, argp, tsem_digestsize()))
+		goto done;
+
+	if (type == TSEM_CONTROL_MAP_STATE)
+		retn = tsem_model_load_point(mapping);
+	else if (type == TSEM_CONTROL_MAP_PSEUDONYM)
+		retn = tsem_model_load_pseudonym(mapping);
+	else {
+		tsem_model_load_base(mapping);
+		retn = 0;
+	}
+
+ done:
+	return retn;
+}
+
+static int config_namespace(enum tsem_control_type type, const char *arg)
+{
+	char **argv, *argp, *digest = "sha256", *key = NULL;
+	int argc, retn = -EINVAL;
+	unsigned int lp, cache_size = TSEM_MAGAZINE_SIZE_INTERNAL;
+	enum namespace_argument_type ns_arg;
+	enum tsem_ns_reference ns_ref = TSEM_NS_INITIAL;
+	const struct tsem_context_ops *ops = &tsem_model0_ops;
+
+	if (type == TSEM_CONTROL_EXTERNAL || type == TSEM_CONTROL_EXPORT)
+		cache_size = TSEM_MAGAZINE_SIZE_EXTERNAL;
+
+	if (!arg) {
+		if (type == TSEM_CONTROL_EXTERNAL)
+			return retn;
+		return tsem_ns_create(type, digest, ns_ref, key, cache_size,
+				      ops);
+	}
+
+	argv = argv_split(GFP_KERNEL, arg, &argc);
+	if (!argv)
+		return -ENOMEM;
+
+	for (lp = 0; lp < argc; ++lp) {
+		argp = strchr(argv[lp], '=');
+		if (!argp)
+			goto done;
+		*argp++ = '\0';
+
+		ns_arg = match_string(namespace_arguments,
+				    ARRAY_SIZE(namespace_arguments), argv[lp]);
+		if (ns_arg < 0)
+			goto done;
+
+		switch (ns_arg) {
+		case NS_MODEL:
+			ops = tsem_nsmgr_get(argp);
+			if (!ops)
+				goto done;
+			break;
+		case NS_REF:
+			if (!strcmp(argp, "current"))
+				ns_ref = TSEM_NS_CURRENT;
+			else if (!strcmp(argp, "initial"))
+				ns_ref = TSEM_NS_INITIAL;
+			else
+				goto done;
+			break;
+		case NS_DIGEST:
+			digest = argp;
+			if (!crypto_has_shash(digest, 0, 0))
+				goto done;
+			break;
+		case NS_KEY:
+			key = argp;
+			if (strlen(key) % 2)
+				goto done;
+			break;
+		case NS_CACHE:
+			if (kstrtouint(argp, 0, &cache_size))
+				goto done;
+			if (!cache_size)
+				goto done;
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (type == TSEM_CONTROL_EXTERNAL && !key)
+		goto done;
+
+	retn = tsem_ns_create(type, digest, ns_ref, key, cache_size, ops);
+
+ done:
+	argv_free(argv);
+	return retn;
+}
+
+static void show_creds(struct seq_file *c, char *key, char *term,
+		       struct tsem_COE *cp)
+{
+	tsem_fs_show_field(c, key);
+	tsem_fs_show_key(c, "uid", ",", "%u", cp->uid);
+	tsem_fs_show_key(c, "euid", ",", "%u", cp->euid);
+	tsem_fs_show_key(c, "suid", ",", "%u", cp->suid);
+	tsem_fs_show_key(c, "gid", ",", "%u", cp->gid);
+	tsem_fs_show_key(c, "egid", ",", "%u", cp->egid);
+	tsem_fs_show_key(c, "sgid", ",", "%u", cp->sgid);
+	tsem_fs_show_key(c, "fsuid", ",", "%u", cp->fsuid);
+	tsem_fs_show_key(c, "fsgid", ",", "%u", cp->fsgid);
+	tsem_fs_show_key(c, "capeff", ",", "0x%llx", cp->capeff.value);
+	tsem_fs_show_key(c, "securebits", "}", "%u", cp->securebits);
+
+	seq_puts(c, term);
+}
+
+static void show_event(struct seq_file *c, struct tsem_event *ep)
+{
+	tsem_fs_show_field(c, "event");
+	if (ep->pid)
+		tsem_fs_show_key(c, "pid", ",", "%u", ep->pid);
+	tsem_fs_show_key(c, "context", ",", "%llu", ep->context);
+	tsem_fs_show_key(c, "number", ",", "%llu", ep->event_number);
+	tsem_fs_show_key(c, "process", ",", "%s", ep->comm);
+	tsem_fs_show_key(c, "type", ",", "%s", tsem_names[ep->event]);
+	tsem_fs_show_key(c, "ttd", ",", "%llu", ep->instance);
+	tsem_fs_show_key(c, "p_ttd", ",", "%llu", ep->p_instance);
+	tsem_fs_show_key(c, "task_id", ",", "%*phN", tsem_digestsize(),
+			 ep->task_id);
+	tsem_fs_show_key(c, "p_task_id", ",", "%*phN", tsem_digestsize(),
+			 ep->p_task_id);
+	tsem_fs_show_key(c, "ts", "}, ", "%llu", ep->timestamp);
+
+	tsem_fs_show_field(c, "COE");
+	tsem_fs_show_key(c, "uid", ",", "%u", ep->COE.uid);
+	tsem_fs_show_key(c, "euid", ",", "%u", ep->COE.euid);
+	tsem_fs_show_key(c, "suid", ",", "%u", ep->COE.suid);
+	tsem_fs_show_key(c, "gid", ",", "%u", ep->COE.gid);
+	tsem_fs_show_key(c, "egid", ",", "%u", ep->COE.egid);
+	tsem_fs_show_key(c, "sgid", ",", "%u", ep->COE.sgid);
+	tsem_fs_show_key(c, "fsuid", ",", "%u", ep->COE.fsuid);
+	tsem_fs_show_key(c, "fsgid", ",", "%u", ep->COE.fsgid);
+	tsem_fs_show_key(c, "capeff", "}, ", "0x%llx", ep->COE.capeff.value);
+
+	tsem_fs_show_field(c, tsem_names[ep->event]);
+}
+
+static void show_path(struct seq_file *c, char *key, char *term,
+		      struct tsem_path *path)
+{
+	tsem_fs_show_field(c, key);
+
+	if (path->dev) {
+		tsem_fs_show_field(c, "dev");
+		tsem_fs_show_key(c, "major", ",", "%u", MAJOR(path->dev));
+		tsem_fs_show_key(c, "minor", "}, ", "%u", MINOR(path->dev));
+	}
+
+	if (path->created) {
+		tsem_fs_show_key(c, "owner", ",", "%*phN", tsem_digestsize(),
+				 path->owner);
+		tsem_fs_show_key(c, "instance", ",", "%llu", path->instance);
+	}
+
+	tsem_fs_show_key(c, "pathname", "}", "%s", path->pathname);
+	seq_puts(c, term);
+}
+
+static void show_inode(struct seq_file *c, char *key, char *term,
+		       struct tsem_inode_cell *inode)
+{
+	tsem_fs_show_field(c, key);
+	tsem_fs_show_key(c, "uid", ",", "%u", inode->uid);
+	tsem_fs_show_key(c, "gid", ",", "%u", inode->gid);
+	tsem_fs_show_key(c, "mode", ",", "0%o", inode->mode);
+	tsem_fs_show_key(c, "s_magic", ",", "0x%0x", inode->s_magic);
+	tsem_fs_show_key(c, "s_id", ",", "%s", inode->s_id);
+	tsem_fs_show_key(c, "s_uuid", "}", "%*phN", sizeof(inode->s_uuid),
+			 inode->s_uuid);
+
+	seq_puts(c, term);
+}
+
+static void show_dentry(struct seq_file *c, char *key, char *term,
+			struct tsem_dentry *dentry)
+{
+	tsem_fs_show_field(c, key);
+	if (dentry->have_inode)
+		show_inode(c, "inode", ", ", &dentry->inode);
+
+	show_path(c, "path", "}", &dentry->path);
+
+	seq_puts(c, term);
+}
+
+static void show_file(struct seq_file *c, char *term,
+		      struct tsem_file_args *args)
+{
+	tsem_fs_show_field(c, "file");
+	tsem_fs_show_key(c, "flags", ",", "%u", args->out.flags);
+
+	show_inode(c, "inode", ", ", &args->out.inode);
+	show_path(c, "path", ", ", &args->out.path);
+	tsem_fs_show_key(c, "digest", "}", "%*phN", tsem_digestsize(),
+			 args->out.digest);
+
+	seq_puts(c, term);
+}
+
+static void show_inode_create(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	show_event(c, ep);
+
+	show_inode(c, "dir", ", ", &args->out.dir);
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "mode", "}", "0%o", args->mode);
+}
+
+static void show_inode_remove(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	show_event(c, ep);
+
+	show_inode(c, "dir", ", ", &args->out.dir);
+	show_dentry(c, "dentry", "}", &args->out.dentry);
+}
+
+static void show_inode_link(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	show_event(c, ep);
+
+	show_dentry(c, "old_dentry", ", ", &args->out.dentry);
+	show_inode(c, "dir", ", ", &args->out.dir);
+	show_dentry(c, "new_dentry", "}", &args->out.new_dentry);
+}
+
+static void show_syslog(struct seq_file *c, struct tsem_event *ep)
+{
+	show_event(c, ep);
+	tsem_fs_show_key(c, "type", "}", "%d", ep->CELL.value);
+}
+
+static void show_settime(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_time_args *args = &ep->CELL.time;
+
+	show_event(c, ep);
+
+	if (args->have_ts) {
+		tsem_fs_show_field(c, "ts");
+		tsem_fs_show_key(c, "seconds", ",", "%d", args->seconds);
+		tsem_fs_show_key(c, "nsecs", args->have_tz ? "}, " : "}",
+				 "%d", args->nsecs);
+	}
+
+	if (args->have_tz) {
+		tsem_fs_show_field(c, "tz");
+		tsem_fs_show_key(c, "minuteswest", ",", "%d",
+				 args->minuteswest);
+		tsem_fs_show_key(c, "dsttime", "}", "%d", args->dsttime);
+	}
+
+	seq_putc(c, '}');
+}
+
+static void show_inode_symlink(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	show_event(c, ep);
+
+	show_inode(c, "dir", ", ", &args->out.dir);
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "old_name", "}", "%s", args->out.old_name);
+}
+
+static void show_inode_mknod(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	show_event(c, ep);
+
+	show_inode(c, "dir", ", ", &args->out.dir);
+
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "mode", ",", "0%o", args->mode);
+	tsem_fs_show_field(c, "dev");
+	tsem_fs_show_key(c, "major", ",", "%u", MAJOR(args->dev));
+	tsem_fs_show_key(c, "minor", "}}", "%u", MINOR(args->dev));
+}
+
+static void show_inode_rename(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	show_event(c, ep);
+
+	show_inode(c, "old_dir", ", ", &args->out.dir);
+	show_dentry(c, "old_dentry", ", ", &args->out.dentry);
+	show_inode(c, "new_dir", ", ", &args->out.new_dir);
+	show_dentry(c, "new_dentry", "}", &args->out.new_dentry);
+}
+
+static void show_inode_killpriv(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	show_event(c, ep);
+
+	show_dentry(c, "dentry", "}", &args->out.dentry);
+}
+
+static void show_file_open(struct seq_file *c, struct tsem_event *ep)
+{
+	show_event(c, ep);
+
+	show_file(c, "}", &ep->CELL.file);
+}
+
+static void show_mmap(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_mmap_file_args *args = &ep->CELL.mmap_file;
+
+	show_event(c, ep);
+
+	if (!args->anonymous)
+		show_file(c, ", ", &args->file);
+	tsem_fs_show_key(c, "prot", ",", "%u", args->prot);
+	tsem_fs_show_key(c, "flags", "}", "%u", args->flags);
+}
+
+static void show_file_ioctl(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_file_args *args = &ep->CELL.file;
+
+	show_event(c, ep);
+
+	show_file(c, ", ", args);
+	tsem_fs_show_key(c, "cmd", "}", "%u", args->cmd);
+}
+
+static void show_socket_info(struct seq_file *c, const char *key, char *term,
+			     struct tsem_socket *args)
+{
+	tsem_fs_show_field(c, key);
+	tsem_fs_show_key(c, "family", ",", "%d", args->family);
+	tsem_fs_show_key(c, "type", ",", "%d", args->type);
+	tsem_fs_show_key(c, "protocol", ",", "%d", args->protocol);
+	tsem_fs_show_key(c, "owner", "}", "%*phN", tsem_digestsize(),
+			 args->owner);
+
+	seq_puts(c, term);
+}
+
+static void show_netlink(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_netlink_args *args = &ep->CELL.netlink;
+
+	show_event(c, ep);
+
+	show_socket_info(c, "sock", ", ", &args->out.sock);
+	tsem_fs_show_key(c, "uid", ",", "%u", args->out.uid);
+	tsem_fs_show_key(c, "gid", ",", "%u", args->out.gid);
+	tsem_fs_show_key(c, "portid", ",", "%u", args->out.portid);
+	tsem_fs_show_key(c, "dst_group", ",", "%u", args->out.dst_group);
+	tsem_fs_show_key(c, "flags", ",", "%u", args->out.flags);
+	tsem_fs_show_key(c, "nsid_set", ",", "%u", args->out.nsid_set);
+	tsem_fs_show_key(c, "nsid", "}", "%d", args->out.nsid);
+}
+
+static void show_ipc_cred(struct seq_file *c, char *key, char *term,
+			  struct tsem_ipc_args *args)
+{
+	tsem_fs_show_field(c, key);
+	tsem_fs_show_key(c, "uid", ",", "%u", args->out.perm.uid);
+	tsem_fs_show_key(c, "gid", ",", "%u", args->out.perm.gid);
+	tsem_fs_show_key(c, "cuid", ",", "%u", args->out.perm.cuid);
+	tsem_fs_show_key(c, "cgid", ",", "%u", args->out.perm.cgid);
+	tsem_fs_show_key(c, "mode", ",", "0%o", args->out.perm.mode);
+	tsem_fs_show_key(c, "owner", "}", "%*phN", tsem_digestsize(),
+			 args->out.owner);
+
+	seq_puts(c, term);
+}
+
+static void show_ipc_permission(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_ipc_args *args = &ep->CELL.ipc;
+
+	show_event(c, ep);
+
+	show_ipc_cred(c, "ipcp", ", ", args);
+	tsem_fs_show_key(c, "flag", "}", "%u", args->perm_flag);
+}
+
+static void show_ipc_shm_value(struct seq_file *c, struct tsem_event *ep,
+			       char *name)
+{
+	struct tsem_ipc_args *args = &ep->CELL.ipc;
+
+	show_event(c, ep);
+
+	show_ipc_cred(c, "perm", ", ", args);
+	tsem_fs_show_key(c, name, "}", "%d", args->perm_flag);
+}
+
+static void show_msg_queue_msgrcv(struct seq_file *c, struct tsem_event *ep)
+
+{
+	struct tsem_ipc_args *args = &ep->CELL.ipc;
+
+	show_event(c, ep);
+
+	show_ipc_cred(c, "perm", ", ", args);
+	tsem_fs_show_key(c, "target", ",", "%*phN", tsem_digestsize(),
+			 args->out.target);
+	tsem_fs_show_key(c, "type", ",", "%ld", args->type);
+	tsem_fs_show_key(c, "mode", "}", "%d", args->value);
+}
+
+static void show_sem_semop(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_ipc_args *args = &ep->CELL.ipc;
+
+	show_event(c, ep);
+
+	show_ipc_cred(c, "perm", ", ", args);
+	tsem_fs_show_key(c, "nsops", ",", "%u", args->nsops);
+	tsem_fs_show_key(c, "alter", "}", "%d", args->value);
+}
+
+static void show_socket_pair(struct seq_file *c, struct tsem_event *ep,
+			     char *first, char *second)
+{
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	show_event(c, ep);
+
+	show_socket_info(c, first, ", ", &args->out.socka);
+	show_socket_info(c, second, "}", &args->out.sockb);
+}
+
+static void show_socket_create(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "family", ",", "%u", args->out.socka.family);
+	tsem_fs_show_key(c, "type", ",", "%u", args->out.socka.type);
+	tsem_fs_show_key(c, "protocol", ",", "%u", args->out.socka.protocol);
+	tsem_fs_show_key(c, "kern", "}", "%u", args->out.socka.kern);
+}
+
+static void show_socket_connect_bind(struct seq_file *c, struct tsem_event *ep)
+{
+	struct sockaddr_in *ipv4;
+	struct sockaddr_in6 *ipv6;
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	show_event(c, ep);
+
+	show_socket_info(c, "sock", ", ", &args->out.socka);
+
+	tsem_fs_show_field(c, "addr");
+	switch (args->out.socka.family) {
+	case AF_INET:
+		ipv4 = (struct sockaddr_in *) &args->out.ipv4;
+		tsem_fs_show_field(c, "af_inet");
+		tsem_fs_show_key(c, "port", ",", "%u", ipv4->sin_port);
+		tsem_fs_show_key(c, "address", "}}}", "%u",
+				 ipv4->sin_addr.s_addr);
+		break;
+	case AF_INET6:
+		ipv6 = (struct sockaddr_in6 *) &args->out.ipv6;
+		tsem_fs_show_field(c, "af_inet6");
+		tsem_fs_show_key(c, "port", ",", "%u", ipv6->sin6_port);
+		tsem_fs_show_key(c, "flow", ",", "%u", ipv6->sin6_flowinfo);
+		tsem_fs_show_key(c, "scope", ",", "%u", ipv6->sin6_scope_id);
+		tsem_fs_show_key(c, "address", "}}}", "%*phN",
+			 (int) sizeof(ipv6->sin6_addr.in6_u.u6_addr8),
+			 ipv6->sin6_addr.in6_u.u6_addr8);
+		break;
+	case AF_UNIX:
+		tsem_fs_show_field(c, "af_unix");
+		tsem_fs_show_key(c, "address", "}}}", "%s", args->out.path);
+		break;
+	default:
+		tsem_fs_show_field(c, "af_other");
+		tsem_fs_show_key(c, "address", "}}}", "%*phN",
+				 tsem_digestsize(), args->out.mapping);
+		break;
+	}
+}
+
+static void show_socket_accept(struct seq_file *c, struct tsem_event *ep)
+{
+	char *p;
+	int size;
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	show_event(c, ep);
+
+	show_socket_info(c, "sock", ", ", &args->out.socka);
+
+	tsem_fs_show_field(c, "addr");
+	switch (args->out.socka.family) {
+	case AF_INET:
+		tsem_fs_show_field(c, "af_inet");
+		tsem_fs_show_key(c, "port", ",", "%u",
+				 args->out.ipv4.sin_port);
+		tsem_fs_show_key(c, "address", "}}}", "%u",
+				 args->out.ipv4.sin_addr);
+		break;
+	case AF_INET6:
+		tsem_fs_show_field(c, "af_inet6");
+		tsem_fs_show_key(c, "port", ",", "%u",
+				 args->out.ipv6.sin6_port);
+		p = args->out.ipv6.sin6_addr.in6_u.u6_addr8;
+		size = sizeof(args->out.ipv6.sin6_addr.in6_u.u6_addr8);
+		tsem_fs_show_key(c, "address", "}}}", "%*phN", size, p);
+		break;
+	case AF_UNIX:
+		tsem_fs_show_field(c, "af_unix");
+		tsem_fs_show_key(c, "address", "}}}", "%s", args->out.path);
+		break;
+	default:
+		tsem_fs_show_field(c, "af_other");
+		tsem_fs_show_key(c, "address", "}}}", "%*phN",
+				 tsem_digestsize(), args->out.mapping);
+		break;
+	}
+}
+
+static void show_socket_value(struct seq_file *c, struct tsem_event *ep,
+			      char *key)
+{
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	show_event(c, ep);
+
+	show_socket_info(c, "sock", ", ", &args->out.socka);
+	tsem_fs_show_key(c, key, "}", "%d", args->value);
+}
+
+static void show_socket_msg(struct seq_file *c, struct tsem_event *ep)
+{
+	int size;
+	struct sockaddr_in *ipv4;
+	struct sockaddr_in6 *ipv6;
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	show_event(c, ep);
+
+	show_socket_info(c, "sock", args->out.have_addr ? ", " : "}",
+			 &args->out.socka);
+
+	if (args->out.have_addr) {
+		tsem_fs_show_field(c, "addr");
+		switch (args->out.socka.family) {
+		case AF_INET:
+			ipv4 = &args->out.ipv4;
+			tsem_fs_show_field(c, "af_inet");
+			tsem_fs_show_key(c, "port", ",", "%u", ipv4->sin_port);
+			tsem_fs_show_key(c, "address", "}}}", "%u",
+					 ipv4->sin_addr.s_addr);
+			break;
+		case AF_INET6:
+			ipv6 = &args->out.ipv6;
+			size = sizeof(ipv6->sin6_addr.in6_u.u6_addr8);
+			tsem_fs_show_field(c, "af_inet6");
+			tsem_fs_show_key(c, "port", ",", "%u",
+					 ipv6->sin6_port);
+			tsem_fs_show_key(c, "address", "}}}", "%*phN", size,
+					 ipv6->sin6_addr.in6_u.u6_addr8);
+			break;
+		}
+	}
+}
+
+static void show_socket_argument(struct seq_file *c, struct tsem_event *ep)
+{
+	show_event(c, ep);
+
+	show_socket_info(c, "sock", "}", &ep->CELL.socket.out.socka);
+}
+
+static void show_socket_setsockopt(struct seq_file *c, struct tsem_event *ep)
+{
+	show_event(c, ep);
+
+	show_socket_info(c, "sock", ", ", &ep->CELL.socket.out.socka);
+	tsem_fs_show_key(c, "level", ",", "%d", ep->CELL.socket.value);
+	tsem_fs_show_key(c, "optname", "}", "%d", ep->CELL.socket.optname);
+}
+
+static void show_kernel_module_request(struct seq_file *c,
+				       struct tsem_event *ep)
+{
+	struct tsem_kernel_args *args = &ep->CELL.kernel;
+
+	show_event(c, ep);
+	tsem_fs_show_key(c, "kmod_name", "}", "%s", args->out.kmod_name);
+}
+
+static void show_kernel_load_data(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_kernel_args *args = &ep->CELL.kernel;
+
+	show_event(c, ep);
+	tsem_fs_show_key(c, "id", ",", "%d", args->id);
+	tsem_fs_show_key(c, "contents", "}", "%d", args->contents);
+}
+
+static void show_kernel_read_file(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_kernel_args *args = &ep->CELL.kernel;
+
+	show_event(c, ep);
+
+	show_file(c, ", ", &args->out.file);
+	tsem_fs_show_key(c, "id", ",", "%d", args->id);
+	tsem_fs_show_key(c, "contents", "}", "%d", args->contents);
+}
+
+static void show_task_kill(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "target", ",", "%*phN", tsem_digestsize(),
+			 args->target);
+	tsem_fs_show_key(c, "sig", ",", "%u", args->signal);
+	tsem_fs_show_key(c, "cross_ns", "}", "%u", args->cross_model);
+}
+
+static void show_ptrace_access_check(struct seq_file *c,
+				     struct tsem_event *ep)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "child", ", ", "%*phN", tsem_digestsize(),
+			 args->target);
+	tsem_fs_show_key(c, "mode", "}", "%u", args->u.resource);
+}
+
+static void show_task_ptraceme(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "source", "}", "%*phN", tsem_digestsize(),
+			 args->source);
+}
+
+static void show_capget(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_capability_args *args = &ep->CELL.capability;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "target", ",", "%*phN", tsem_digestsize(),
+			 args->target);
+	tsem_fs_show_key(c, "effective", ",", "0x%llx", args->effective);
+	tsem_fs_show_key(c, "inheritable", ",", "0x%llx", args->inheritable);
+	tsem_fs_show_key(c, "permitted", "}", "0x%llx", args->permitted);
+}
+
+static void show_capset(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_capability_args *args = &ep->CELL.capability;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "effective", ",", "0x%llx", args->effective);
+	tsem_fs_show_key(c, "inheritable", ",", "0x%llx", args->inheritable);
+	tsem_fs_show_key(c, "permitted", "}", "0x%llx", args->permitted);
+}
+
+static void show_capable(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_capability_args *args = &ep->CELL.capability;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "cap", ",", "%d", args->cap);
+	tsem_fs_show_key(c, "opts", "}", "%u", args->opts);
+}
+
+static void show_task_setpgid(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "task", ",", "%*phN", tsem_digestsize(),
+			 args->target);
+	tsem_fs_show_key(c, "source", "}", "%*phN", tsem_digestsize(),
+			 args->source);
+}
+
+static void show_task_getpgid(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "task", "}", "%*phN", tsem_digestsize(),
+			 args->target);
+}
+
+static void show_task_nice(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "task", ",", "%*phN", tsem_digestsize(),
+			 args->target);
+	tsem_fs_show_key(c, "nice", "}", "%d", args->u.value);
+}
+
+static void show_task_prlimit(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_prlimit_args *args = &ep->CELL.task_prlimit;
+
+	show_event(c, ep);
+
+	show_creds(c, "cred", ", ", &args->out.cred);
+	show_creds(c, "tcred", ", ", &args->out.tcred);
+	tsem_fs_show_key(c, "flags", "}", "%d", args->flags);
+}
+
+static void show_task_setrlimit(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "task", ",", "%*phN", tsem_digestsize(),
+			 args->target);
+	tsem_fs_show_field(c, "new_rlim");
+	tsem_fs_show_key(c, "resource", ",", "%u", args->u.resource);
+	tsem_fs_show_key(c, "current", ",", "%llu", args->cur);
+	tsem_fs_show_key(c, "max", "}", "%llu", args->max);
+	seq_putc(c, '}');
+}
+
+static void show_task_prctl(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_task_prctl_args *args = &ep->CELL.task_prctl;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "option", ",", "%d", args->option);
+	tsem_fs_show_key(c, "arg2", ",", "%llu", args->arg2);
+	tsem_fs_show_key(c, "arg3", ",", "%llu", args->arg3);
+	tsem_fs_show_key(c, "arg4", ",", "%llu", args->arg4);
+	tsem_fs_show_key(c, "arg5", "}", "%llu", args->arg5);
+}
+
+static void show_task_value(struct seq_file *c, struct tsem_event *ep,
+			    char *key)
+{
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "task", ",", "%*phN", tsem_digestsize(),
+			 args->target);
+	tsem_fs_show_key(c, key, "}", "%d", args->u.value);
+}
+
+static void show_inode_getattr(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_attr_args *args = &ep->CELL.inode_attr;
+
+	show_event(c, ep);
+
+	show_dentry(c, "path", "}", &args->out.dentry);
+}
+
+static void show_inode_setattr(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_attr_args *args = &ep->CELL.inode_attr;
+
+	show_event(c, ep);
+
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_field(c, "attr");
+	tsem_fs_show_key(c, "valid", ",", "%u", args->out.valid);
+	tsem_fs_show_key(c, "mode", ",", "0%o", args->out.mode);
+	tsem_fs_show_key(c, "uid", ",", "%u", args->out.uid);
+	tsem_fs_show_key(c, "gid", ",", "%u", args->out.gid);
+	tsem_fs_show_key(c, "size", "}", "%lu", args->out.size);
+	seq_putc(c, '}');
+}
+
+static void show_inode_setxattr(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_xattr_args *args = &ep->CELL.inode_xattr;
+
+	show_event(c, ep);
+
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "name", ",", "%s", args->out.name);
+	tsem_fs_show_key(c, "value", ",", "%s", args->out.encoded_value);
+	tsem_fs_show_key(c, "flags", "}", "%d", args->out.flags);
+}
+
+static void show_inode_getxattr(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_xattr_args *args = &ep->CELL.inode_xattr;
+
+	show_event(c, ep);
+
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "name", "}", "%s", args->out.name);
+}
+
+static void show_inode_listxattr(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_inode_xattr_args *args = &ep->CELL.inode_xattr;
+
+	show_event(c, ep);
+
+	show_dentry(c, "dentry", "}", &args->out.dentry);
+}
+
+static void show_key_alloc(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_key_args *args = &ep->CELL.key;
+
+	show_event(c, ep);
+
+	show_creds(c, "cred", ", ", &args->out.cred);
+	tsem_fs_show_key(c, "flags", "}", "%llu", args->flags);
+}
+
+static void show_key_permission(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_key_args *args = &ep->CELL.key;
+
+	show_event(c, ep);
+
+	tsem_fs_show_field(c, "key_ref");
+	tsem_fs_show_key(c, "possessed", ",", "%u", args->out.possessed);
+	tsem_fs_show_key(c, "uid", ",", "%u", args->out.uid);
+	tsem_fs_show_key(c, "gid", ",", "%u", args->out.gid);
+	tsem_fs_show_key(c, "flags", "}, ", "%lu", args->out.flags);
+	show_creds(c, "cred", ", ", &args->out.cred);
+	tsem_fs_show_key(c, "perm", "}", "%u", args->out.perm);
+}
+
+static void show_sb_mount(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	show_event(c, ep);
+
+	if (args->out.dev_name)
+		tsem_fs_show_key(c, "dev_name", ",", "%s", args->out.dev_name);
+
+	show_path(c, "path", ", ", &args->out.path);
+
+	if (args->out.type)
+		tsem_fs_show_key(c, "type", ",", "%s", args->out.type);
+
+	tsem_fs_show_key(c, "flags", "}", "%lu", args->flags);
+}
+
+static void show_sb_umount(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	show_event(c, ep);
+
+	show_dentry(c, "mnt", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "flags", "}", "%d", args->flags);
+}
+
+static void show_sb_remount(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	show_event(c, ep);
+
+	tsem_fs_show_field(c, "sb");
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "fstype", ",", "%s", args->out.type);
+	tsem_fs_show_key(c, "s_flags", "}", "%lu", args->flags);
+	seq_putc(c, '}');
+}
+
+static void show_sb_statfs(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	show_event(c, ep);
+
+	show_dentry(c, "dentry", "}", &args->out.dentry);
+}
+
+static void show_move_path(struct seq_file *c, struct tsem_event *ep,
+			   char *path1, char *path2)
+{
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	show_event(c, ep);
+
+	show_path(c, path1, ", ", &args->out.path);
+	show_path(c, path2, "}", &args->out.path2);
+}
+
+static void show_quotactl(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_quota_args *args = &ep->CELL.quota;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "cmds", ",", "%d", args->cmds);
+	tsem_fs_show_key(c, "type", ",", "%d", args->type);
+	tsem_fs_show_key(c, "id", ",", "%d", args->id);
+
+	tsem_fs_show_field(c, "sb");
+	show_dentry(c, "dentry", ", ", &args->out.dentry);
+	tsem_fs_show_key(c, "fstype", ",", "%s", args->out.fstype);
+	tsem_fs_show_key(c, "s_flags", "}", "%d", args->out.s_flags);
+	seq_putc(c, '}');
+}
+
+static void show_quotaon(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_quota_args *args = &ep->CELL.quota;
+
+	show_event(c, ep);
+
+	show_dentry(c, "dentry", "}", &args->out.dentry);
+}
+
+static void show_bpf(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_bpf_args *args = &ep->CELL.bpf;
+
+	show_event(c, ep);
+
+	tsem_fs_show_key(c, "cmd", ",", "%d", args->bpf.cmd);
+	tsem_fs_show_field(c, "attr");
+	tsem_fs_show_key(c, "size", "}", "%u", args->bpf.size);
+	seq_putc(c, '}');
+}
+
+static void show_bpf_map(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_bpf_args *args = &ep->CELL.bpf;
+
+	show_event(c, ep);
+
+	tsem_fs_show_field(c, "map");
+	tsem_fs_show_key(c, "map_type", "}, ", "%d", args->map.map_type);
+	tsem_fs_show_key(c, "fmode", "}", "%u", args->map.fmode);
+}
+
+static void show_bpf_prog(struct seq_file *c, struct tsem_event *ep)
+{
+	struct tsem_bpf_args *args = &ep->CELL.bpf;
+
+	show_event(c, ep);
+
+	tsem_fs_show_field(c, "prog");
+	tsem_fs_show_key(c, "type", ",", "%d", args->prog.type);
+	tsem_fs_show_key(c, "attach_type", "}", "%d", args->prog.attach_type);
+	seq_putc(c, '}');
+}
+
+static void show_event_generic(struct seq_file *c, struct tsem_event *ep)
+{
+	show_event(c, ep);
+	seq_putc(c, '}');
+}
+
+static void *trajectory_start(struct seq_file *c, loff_t *pos)
+{
+	struct list_head *end;
+	struct tsem_model *model = tsem_model(current);
+
+	spin_lock(&model->trajectory_lock);
+	end = model->trajectory_list.prev;
+	spin_unlock(&model->trajectory_lock);
+
+	mutex_lock(&model->trajectory_end_mutex);
+	model->trajectory_end = end;
+
+	return seq_list_start(&model->trajectory_list, *pos);
+}
+
+static void *trajectory_next(struct seq_file *c, void *p, loff_t *pos)
+{
+	struct list_head *next = ((struct list_head *) p)->next;
+	struct tsem_model *model = tsem_model(current);
+
+	if (!model->trajectory_end) {
+		++*pos;
+		return NULL;
+	}
+
+	if (next == model->trajectory_end)
+		model->trajectory_end = NULL;
+
+	return seq_list_next(p, &model->trajectory_list, pos);
+}
+
+static void trajectory_stop(struct seq_file *c, void *pos)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	mutex_unlock(&model->trajectory_end_mutex);
+}
+
+static int trajectory_show(struct seq_file *c, void *trajectory)
+{
+	struct tsem_event *ep;
+
+	ep = list_entry(trajectory, struct tsem_event, list);
+
+	seq_putc(c, '{');
+	tsem_fs_show_trajectory(c, ep);
+	seq_puts(c, "}\n");
+
+	return 0;
+}
+
+static const struct seq_operations trajectory_seqops = {
+	.start = trajectory_start,
+	.next = trajectory_next,
+	.stop = trajectory_stop,
+	.show = trajectory_show
+};
+
+static int trajectory_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return seq_open(file, &trajectory_seqops);
+}
+
+static const struct file_operations trajectory_ops = {
+	.open = trajectory_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static void *trajectory_count_start(struct seq_file *c, loff_t *pos)
+{
+	struct list_head *end;
+	struct tsem_model *model = tsem_model(current);
+
+	spin_lock(&model->point_lock);
+	end = model->point_list.prev;
+	spin_unlock(&model->point_lock);
+
+	mutex_lock(&model->point_end_mutex);
+	model->point_end = end;
+
+	return seq_list_start(&model->point_list, *pos);
+}
+
+static void *trajectory_count_next(struct seq_file *c, void *p, loff_t *pos)
+{
+	struct list_head *next = ((struct list_head *) p)->next;
+	struct tsem_model *model = tsem_model(current);
+
+	if (!model->point_end) {
+		++*pos;
+		return NULL;
+	}
+
+	if (next == model->point_end)
+		model->point_end = NULL;
+
+	return seq_list_next(p, &model->point_list, pos);
+}
+
+static void trajectory_count_stop(struct seq_file *c, void *pos)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	mutex_unlock(&model->point_end_mutex);
+}
+
+static int trajectory_count_show(struct seq_file *c, void *point)
+{
+	struct tsem_event_point *pt;
+
+	pt = list_entry(point, struct tsem_event_point, list);
+	if (!pt->valid)
+		return 0;
+
+	seq_printf(c, "%llu\n", pt->count);
+	return 0;
+}
+
+static const struct seq_operations trajectory_count_seqops = {
+	.start = trajectory_count_start,
+	.next = trajectory_count_next,
+	.stop = trajectory_count_stop,
+	.show = trajectory_count_show
+};
+
+static int trajectory_count_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return seq_open(file, &trajectory_count_seqops);
+}
+
+static const struct file_operations trajectory_count_ops = {
+	.open = trajectory_count_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static void *trajectory_point_start(struct seq_file *c, loff_t *pos)
+{
+	struct list_head *end;
+	struct tsem_model *model = tsem_model(current);
+
+	spin_lock(&model->point_lock);
+	end = model->point_list.prev;
+	spin_unlock(&model->point_lock);
+
+	mutex_lock(&model->point_end_mutex);
+	model->point_end = end;
+
+	return seq_list_start(&model->point_list, *pos);
+}
+
+static void *trajectory_point_next(struct seq_file *c, void *p, loff_t *pos)
+{
+	struct list_head *next = ((struct list_head *) p)->next;
+	struct tsem_model *model = tsem_model(current);
+
+	if (!model->point_end) {
+		++*pos;
+		return NULL;
+	}
+
+	if (next == model->point_end)
+		model->point_end = NULL;
+
+	return seq_list_next(p, &model->point_list, pos);
+}
+
+static void trajectory_point_stop(struct seq_file *c, void *pos)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	mutex_unlock(&model->point_end_mutex);
+}
+
+static int trajectory_point_show(struct seq_file *c, void *point)
+{
+	struct tsem_event_point *pt;
+
+	pt = list_entry(point, struct tsem_event_point, list);
+	if (!pt->valid)
+		return 0;
+
+	seq_printf(c, "%*phN\n", tsem_digestsize(), pt->point);
+	return 0;
+}
+
+static const struct seq_operations trajectory_point_seqops = {
+	.start = trajectory_point_start,
+	.next = trajectory_point_next,
+	.stop = trajectory_point_stop,
+	.show = trajectory_point_show
+};
+
+static int trajectory_point_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return seq_open(file, &trajectory_point_seqops);
+}
+
+static const struct file_operations trajectory_point_ops = {
+	.open = trajectory_point_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static int open_control(struct inode *inode, struct file *filp)
+{
+	if (!capable(CAP_MAC_ADMIN))
+		return -EACCES;
+	if (!(filp->f_flags & O_WRONLY))
+		return -EACCES;
+	return 0;
+}
+
+static ssize_t write_control(struct file *file, const char __user *buf,
+			     size_t datalen, loff_t *ppos)
+{
+	char *p, *arg, cmdbufr[128];
+	ssize_t retn = -EINVAL;
+	enum tsem_control_type type;
+
+	if (*ppos != 0)
+		goto done;
+	if (datalen > sizeof(cmdbufr)-1)
+		goto done;
+
+	memset(cmdbufr, '\0', sizeof(cmdbufr));
+	if (copy_from_user(cmdbufr, buf, datalen)) {
+		retn = -EFAULT;
+		goto done;
+	}
+
+	p = strchr(cmdbufr, '\n');
+	if (!p)
+		goto done;
+	*p = '\0';
+
+	arg = strchr(cmdbufr, ' ');
+	if (arg != NULL) {
+		*arg = '\0';
+		++arg;
+	}
+
+	type = match_string(control_commands, ARRAY_SIZE(control_commands),
+			    cmdbufr);
+	if (type < 0)
+		goto done;
+
+	switch (type) {
+	case TSEM_CONTROL_EXTERNAL:
+	case TSEM_CONTROL_INTERNAL:
+	case TSEM_CONTROL_EXPORT:
+		retn = config_namespace(type, arg);
+		break;
+	case TSEM_CONTROL_ENFORCE:
+	case TSEM_CONTROL_SEAL:
+		retn = config_context(type, cmdbufr);
+		break;
+	case TSEM_CONTROL_TRUSTED:
+	case TSEM_CONTROL_UNTRUSTED:
+		retn = config_COE(type, arg);
+		break;
+	case TSEM_CONTROL_MAP_STATE:
+	case TSEM_CONTROL_MAP_PSEUDONYM:
+	case TSEM_CONTROL_MAP_BASE:
+		retn = config_point(type, arg);
+		break;
+	case TSEM_CONTROL_LOCK:
+		retn = tsem_nsmgr_lock(false);
+		break;
+	}
+
+done:
+	if (!retn)
+		retn = datalen;
+	return retn;
+}
+
+static int release_control(struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
+static const struct file_operations control_ops = {
+	.open = open_control,
+	.write = write_control,
+	.release = release_control,
+	.llseek = generic_file_llseek,
+};
+
+static void *forensics_start(struct seq_file *c, loff_t *pos)
+{
+	struct list_head *end;
+	struct tsem_model *model = tsem_model(current);
+
+	spin_lock(&model->forensics_lock);
+	end = model->forensics_list.prev;
+	spin_unlock(&model->forensics_lock);
+
+	mutex_lock(&model->forensics_end_mutex);
+	model->forensics_end = end;
+
+	return seq_list_start(&model->forensics_list, *pos);
+}
+
+static void *forensics_next(struct seq_file *c, void *p, loff_t *pos)
+{
+	struct list_head *next = ((struct list_head *) p)->next;
+	struct tsem_model *model = tsem_model(current);
+
+	if (!model->forensics_end) {
+		++*pos;
+		return NULL;
+	}
+
+	if (next == model->forensics_end)
+		model->forensics_end = NULL;
+
+	return seq_list_next(p, &model->forensics_list, pos);
+}
+
+static void forensics_stop(struct seq_file *c, void *pos)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	mutex_unlock(&model->forensics_end_mutex);
+}
+
+static int forensics_show(struct seq_file *c, void *event)
+{
+	struct tsem_event *ep;
+
+	ep = list_entry(event, struct tsem_event, list);
+
+	seq_putc(c, '{');
+	tsem_fs_show_trajectory(c, ep);
+	seq_puts(c, "}\n");
+
+	return 0;
+}
+
+static const struct seq_operations forensics_seqops = {
+	.start = forensics_start,
+	.next = forensics_next,
+	.stop = forensics_stop,
+	.show = forensics_show
+};
+
+static int forensics_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return seq_open(file, &forensics_seqops);
+}
+
+static const struct file_operations forensics_ops = {
+	.open = forensics_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static void *forensics_point_start(struct seq_file *c, loff_t *pos)
+{
+	struct list_head *end;
+	struct tsem_model *model = tsem_model(current);
+
+	spin_lock(&model->point_lock);
+	end = model->point_list.prev;
+	spin_unlock(&model->point_lock);
+
+	mutex_lock(&model->point_end_mutex);
+	model->point_end = end;
+
+	return seq_list_start(&model->point_list, *pos);
+}
+
+static void *forensics_point_next(struct seq_file *c, void *p, loff_t *pos)
+{
+	struct list_head *next = ((struct list_head *) p)->next;
+	struct tsem_model *model = tsem_model(current);
+
+	if (!model->point_end) {
+		++*pos;
+		return NULL;
+	}
+
+	if (next == model->point_end)
+		model->point_end = NULL;
+
+	return seq_list_next(p, &model->point_list, pos);
+}
+
+static void forensics_point_stop(struct seq_file *c, void *pos)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	mutex_unlock(&model->point_end_mutex);
+}
+
+static int forensics_point_show(struct seq_file *c, void *point)
+{
+	struct tsem_event_point *pt;
+
+	pt = list_entry(point, struct tsem_event_point, list);
+	if (pt->valid)
+		return 0;
+
+	seq_printf(c, "%*phN\n", tsem_digestsize(), pt->point);
+	return 0;
+}
+
+static const struct seq_operations forensics_point_seqops = {
+	.start = forensics_point_start,
+	.next = forensics_point_next,
+	.stop = forensics_point_stop,
+	.show = forensics_point_show
+};
+
+static int forensics_point_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return seq_open(file, &forensics_point_seqops);
+}
+
+static const struct file_operations forensics_point_ops = {
+	.open = forensics_point_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static void *forensics_count_start(struct seq_file *c, loff_t *pos)
+{
+	struct list_head *end;
+	struct tsem_model *model = tsem_model(current);
+
+	spin_lock(&model->point_lock);
+	end = model->point_list.prev;
+	spin_unlock(&model->point_lock);
+
+	mutex_lock(&model->point_end_mutex);
+	model->point_end = end;
+
+	return seq_list_start(&model->point_list, *pos);
+}
+
+static void *forensics_count_next(struct seq_file *c, void *p, loff_t *pos)
+{
+	struct list_head *next = ((struct list_head *) p)->next;
+	struct tsem_model *model = tsem_model(current);
+
+	if (!model->point_end) {
+		++*pos;
+		return NULL;
+	}
+
+	if (next == model->point_end)
+		model->point_end = NULL;
+
+	return seq_list_next(p, &model->point_list, pos);
+}
+
+static void forensics_count_stop(struct seq_file *c, void *pos)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	mutex_unlock(&model->point_end_mutex);
+}
+
+static int forensics_count_show(struct seq_file *c, void *point)
+{
+	struct tsem_event_point *pt;
+
+	pt = list_entry(point, struct tsem_event_point, list);
+	if (pt->valid)
+		return 0;
+
+	seq_printf(c, "%llu\n", pt->count);
+	return 0;
+}
+
+static const struct seq_operations forensics_count_seqops = {
+	.start = forensics_count_start,
+	.next = forensics_count_next,
+	.stop = forensics_count_stop,
+	.show = forensics_count_show
+};
+
+static int forensics_count_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return seq_open(file, &forensics_count_seqops);
+}
+
+static const struct file_operations forensics_count_ops = {
+	.open = forensics_count_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static int measurement_show(struct seq_file *c, void *event)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	seq_printf(c, "%*phN\n", tsem_digestsize(), model->measurement);
+	return 0;
+}
+
+static int measurement_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return single_open(file, &measurement_show, NULL);
+}
+
+static const struct file_operations measurement_ops = {
+	.open = measurement_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static int id_show(struct seq_file *c, void *event)
+{
+	seq_printf(c, "%llu\n", tsem_context(current)->id);
+	return 0;
+}
+
+static int id_open(struct inode *inode, struct file *file)
+{
+	struct tsem_context *ctx = tsem_context(current);
+
+	if (ctx->sealed)
+		return -EACCES;
+	return single_open(file, &id_show, NULL);
+}
+
+static const struct file_operations id_ops = {
+	.open = id_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static int state_show(struct seq_file *m, void *v)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	tsem_model_compute_state();
+	seq_printf(m, "%*phN\n", tsem_digestsize(), model->state);
+	return 0;
+}
+
+static int state_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return single_open(file, &state_show, NULL);
+}
+
+static const struct file_operations state_ops = {
+	.open = state_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static int aggregate_show(struct seq_file *m, void *v)
+{
+	seq_printf(m, "%*phN\n", tsem_digestsize(), tsem_trust_aggregate());
+	return 0;
+}
+
+static int aggregate_open(struct inode *inode, struct file *file)
+{
+	if (!can_access_fs())
+		return -EACCES;
+	return single_open(file, &aggregate_show, NULL);
+}
+
+static const struct file_operations aggregate_ops = {
+	.open = aggregate_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+static __poll_t export_poll(struct file *file, struct poll_table_struct *wait)
+{
+	struct tsem_context *ctx = tsem_context(current);
+
+	if (!ctx->external)
+		return -ENOENT;
+
+	poll_wait(file, &ctx->external->wq, wait);
+
+	if (ctx->external->have_event) {
+		ctx->external->have_event = false;
+		return EPOLLIN | EPOLLRDNORM;
+	}
+	return 0;
+}
+
+static int export_open(struct inode *inode, struct file *file)
+{
+	if (!capable(CAP_MAC_ADMIN))
+		return -EACCES;
+	return single_open(file, &tsem_export_show, NULL);
+}
+
+static const struct file_operations export_ops = {
+	.open = export_open,
+	.poll = export_poll,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = seq_release,
+};
+
+/**
+ * tsem_fs_create_external() - Create an external TMA update file.
+ * @id: A pointer to the ASCII representation of the modeling domain
+ *      that the export file is being created for.
+ *
+ * This function is used to create a pseudo-file that will output security
+ * event descriptions for a namespace.  This routine will create the
+ * following file:
+ *
+ * /sys/kernel/security/tsem/ExternalTMA/N
+ *
+ * Where N is replaced with the security model context identifier.
+ *
+ * Return: If creation of the update file is successful a pointer to the
+ *	   dentry of the file is returned.  If an error was encountered
+ *	   the pointer with an encoded code will be returned.
+ */
+struct dentry *tsem_fs_create_external(const char *name)
+{
+	struct dentry *dentry;
+
+	dentry = securityfs_create_file(name, 0400, external_tma, NULL,
+					&export_ops);
+	if (!IS_ERR(dentry))
+		tsem_inode(dentry->d_inode)->status = TSEM_INODE_CONTROL_PLANE;
+	return dentry;
+}
+
+/**
+ * tsem_fs_show_trajectory() - Generate the output of a security event.
+ * @sf: A pointer to the seq_file structure to which output will
+ *      be set.
+ * @ep: A pointer to the event description that is to be output.
+ *
+ * This function is used to generate a record that will be output to
+ * the pseudo-file that outputs the security events for the
+ * domain being modeled.
+ */
+void tsem_fs_show_trajectory(struct seq_file *c, struct tsem_event *ep)
+{
+	if (ep->no_params)
+		show_event_generic(c, ep);
+
+	switch (ep->event) {
+	case TSEM_SYSLOG:
+		show_syslog(c, ep);
+		break;
+	case TSEM_SETTIME:
+		show_settime(c, ep);
+		break;
+	case TSEM_NETLINK_SEND:
+		show_netlink(c, ep);
+		break;
+	case TSEM_IPC_PERMISSION:
+		show_ipc_permission(c, ep);
+		break;
+	case TSEM_SHM_ASSOCIATE:
+	case TSEM_SHM_SHMAT:
+		show_ipc_shm_value(c, ep, "shmflg");
+		break;
+	case TSEM_SHM_SHMCTL:
+	case TSEM_SEM_SEMCTL:
+	case TSEM_MSG_QUEUE_MSGCTL:
+		show_ipc_shm_value(c, ep, "cmd");
+		break;
+	case TSEM_SEM_SEMOP:
+		show_sem_semop(c, ep);
+		break;
+	case TSEM_SEM_ASSOCIATE:
+		show_ipc_shm_value(c, ep, "semflg");
+		break;
+	case TSEM_MSG_QUEUE_ASSOCIATE:
+	case TSEM_MSG_QUEUE_MSGSND:
+		show_ipc_shm_value(c, ep, "msqflg");
+		break;
+	case TSEM_MSG_QUEUE_MSGRCV:
+		show_msg_queue_msgrcv(c, ep);
+		break;
+	case TSEM_INODE_CREATE:
+	case TSEM_INODE_MKDIR:
+		show_inode_create(c, ep);
+		break;
+	case TSEM_INODE_RMDIR:
+	case TSEM_INODE_UNLINK:
+		show_inode_remove(c, ep);
+		break;
+	case TSEM_INODE_LINK:
+		show_inode_link(c, ep);
+		break;
+	case TSEM_INODE_SYMLINK:
+		show_inode_symlink(c, ep);
+		break;
+	case TSEM_INODE_MKNOD:
+		show_inode_mknod(c, ep);
+		break;
+	case TSEM_INODE_RENAME:
+		show_inode_rename(c, ep);
+		break;
+	case TSEM_INODE_KILLPRIV:
+		show_inode_killpriv(c, ep);
+		break;
+	case TSEM_FILE_OPEN:
+	case TSEM_FILE_RECEIVE:
+		show_file_open(c, ep);
+		break;
+	case TSEM_MMAP_FILE:
+		show_mmap(c, ep);
+		break;
+	case TSEM_FILE_IOCTL:
+	case TSEM_FILE_LOCK:
+	case TSEM_FILE_FCNTL:
+		show_file_ioctl(c, ep);
+		break;
+	case TSEM_UNIX_STREAM_CONNECT:
+	case TSEM_UNIX_MAY_SEND:
+		show_socket_pair(c, ep, "sock", "other");
+		break;
+	case TSEM_SOCKET_SOCKETPAIR:
+		show_socket_pair(c, ep, "socka", "sockb");
+		break;
+	case TSEM_SOCKET_CREATE:
+		show_socket_create(c, ep);
+		break;
+	case TSEM_SOCKET_CONNECT:
+	case TSEM_SOCKET_BIND:
+		show_socket_connect_bind(c, ep);
+		break;
+	case TSEM_SOCKET_SENDMSG:
+	case TSEM_SOCKET_RECVMSG:
+		show_socket_msg(c, ep);
+		break;
+	case TSEM_SOCKET_GETSOCKNAME:
+	case TSEM_SOCKET_GETPEERNAME:
+	case TSEM_TUN_DEV_ATTACH_QUEUE:
+		show_socket_argument(c, ep);
+		break;
+	case TSEM_SOCKET_SETSOCKOPT:
+		show_socket_setsockopt(c, ep);
+		break;
+	case TSEM_SOCKET_ACCEPT:
+		show_socket_accept(c, ep);
+		break;
+	case TSEM_SOCKET_LISTEN:
+		show_socket_value(c, ep, "backlog");
+		break;
+	case TSEM_SOCKET_SHUTDOWN:
+		show_socket_value(c, ep, "how");
+		break;
+	case TSEM_KERNEL_MODULE_REQUEST:
+		show_kernel_module_request(c, ep);
+		break;
+	case TSEM_KERNEL_LOAD_DATA:
+		show_kernel_load_data(c, ep);
+		break;
+	case TSEM_KERNEL_READ_FILE:
+		show_kernel_read_file(c, ep);
+		break;
+	case TSEM_TASK_KILL:
+		show_task_kill(c, ep);
+		break;
+	case TSEM_PTRACE_ACCESS_CHECK:
+		show_ptrace_access_check(c, ep);
+		break;
+	case TSEM_PTRACE_TRACEME:
+		show_task_ptraceme(c, ep);
+		break;
+	case TSEM_CAPGET:
+		show_capget(c, ep);
+		break;
+	case TSEM_CAPSET:
+		show_capset(c, ep);
+		break;
+	case TSEM_CAPABLE:
+		show_capable(c, ep);
+		break;
+	case TSEM_TASK_SETPGID:
+		show_task_setpgid(c, ep);
+		break;
+	case TSEM_TASK_GETPGID:
+	case TSEM_TASK_GETSID:
+	case TSEM_TASK_GETIOPRIO:
+	case TSEM_TASK_SETSCHEDULER:
+	case TSEM_TASK_GETSCHEDULER:
+		show_task_getpgid(c, ep);
+		break;
+	case TSEM_TASK_SETNICE:
+		show_task_nice(c, ep);
+		break;
+	case TSEM_TASK_SETIOPRIO:
+		show_task_value(c, ep, "ioprio");
+		break;
+	case TSEM_TASK_PRLIMIT:
+		show_task_prlimit(c, ep);
+		break;
+	case TSEM_TASK_SETRLIMIT:
+		show_task_setrlimit(c, ep);
+		break;
+	case TSEM_TASK_PRCTL:
+		show_task_prctl(c, ep);
+		break;
+	case TSEM_INODE_GETATTR:
+		show_inode_getattr(c, ep);
+		break;
+	case TSEM_INODE_SETATTR:
+		show_inode_setattr(c, ep);
+		break;
+	case TSEM_INODE_SETXATTR:
+		show_inode_setxattr(c, ep);
+		break;
+	case TSEM_INODE_GETXATTR:
+	case TSEM_INODE_REMOVEXATTR:
+		show_inode_getxattr(c, ep);
+		break;
+	case TSEM_INODE_LISTXATTR:
+		show_inode_listxattr(c, ep);
+		break;
+	case TSEM_KEY_ALLOC:
+		show_key_alloc(c, ep);
+		break;
+	case TSEM_KEY_PERMISSION:
+		show_key_permission(c, ep);
+		break;
+	case TSEM_SB_MOUNT:
+		show_sb_mount(c, ep);
+		break;
+	case TSEM_SB_UMOUNT:
+		show_sb_umount(c, ep);
+		break;
+	case TSEM_SB_REMOUNT:
+		show_sb_remount(c, ep);
+		break;
+	case TSEM_SB_STATFS:
+		show_sb_statfs(c, ep);
+		break;
+	case TSEM_SB_PIVOTROOT:
+		show_move_path(c, ep, "old_path", "new_path");
+		break;
+	case TSEM_MOVE_MOUNT:
+		show_move_path(c, ep, "from_path", "to_path");
+		break;
+	case TSEM_QUOTACTL:
+		show_quotactl(c, ep);
+		break;
+	case TSEM_QUOTA_ON:
+		show_quotaon(c, ep);
+		break;
+	case TSEM_BPF:
+		show_bpf(c, ep);
+		break;
+	case TSEM_BPF_MAP:
+		show_bpf_map(c, ep);
+		break;
+	case TSEM_BPF_PROG:
+		show_bpf_prog(c, ep);
+		break;
+	default:
+		break;
+	}
+}
+
+/**
+ * tesm_fs_show_field() - Output a JSON field description
+ * @sf: A pointer to the seq_file structure that the field description
+ *	is to be written to.
+ * @f:  A pointer to null terminated character buffer containing the
+ *      name of the field to encode
+ *
+ * This function is used to generate a JSON field description that
+ * is used to name a sequence of key/value pairs describing the
+ * characteristcis of the field.
+ */
+void tsem_fs_show_field(struct seq_file *c, const char *field)
+{
+	seq_printf(c, "\"%s\": {", field);
+}
+
+/**
+ * tesm_fs_tsem_fs_show_key() - Output a JSON key/value pair
+ * @sf: A pointer to the seq_file structure that the field description
+ *	is to be written to.
+ * @key: A pointer to the null-terminated character buffer containing
+ *	 the key description.
+ * @term: A pointer to a null-terminated character buffer containing
+ *	  the string that is to be used for terminating the key/value
+ *	  pair.
+ * @fmt: The printf format that is to be used for formatting the
+ *	 value of the key.
+ *
+ * This function is a variadic function that is used to encode a
+ * JSON key/value pair that provides one of characteristics of an
+ * event description field.
+ */
+void tsem_fs_show_key(struct seq_file *c, char *key, char *term, char *fmt,
+		      ...)
+{
+	va_list args;
+
+	seq_printf(c, "\"%s\": \"", key);
+
+	va_start(args, fmt);
+	seq_vprintf(c, fmt, args);
+	va_end(args);
+
+	if (term[0] == ',')
+		seq_printf(c, "\"%s ", term);
+	else
+		seq_printf(c, "\"%s", term);
+}
+
+static bool _init_inode(struct dentry *dp)
+{
+	if (IS_ERR(dp))
+		return false;
+
+	tsem_inode(dp->d_inode)->status = TSEM_INODE_CONTROL_PLANE;
+	return true;
+}
+
+/**
+ * tesm_fs_init() - Initialize the TSEM control filesystem heirarchy
+ *
+ * This function is called as part of the TSEM LSM initialization
+ * process.  The purpose of this function is to create the TSEM
+ * control plane, based on the securityfs filesystem, by creating the
+ * /sys/kernel/security/tsem directory and populating that directory
+ * with the control plane files and internal TMA model information
+ * files.  The /sys/kernel/security/tsem/ExternalTMA directory is
+ * also created.  This directory will be used to hold the modeling
+ * domain specific files that will emit the security event descriptions
+ * for the domain.
+ *
+ * Return: If filesystem initialization is successful a return code of 0
+ *	   is returned.  A negative return value is returned if an error
+ *	   is encountered.
+ */
+int __init tsem_fs_init(void)
+{
+	int retn = -1;
+
+	tsem_dir = securityfs_create_dir("tsem", NULL);
+	if (!_init_inode(tsem_dir))
+		goto done;
+
+	control = securityfs_create_file("control", 0200, tsem_dir, NULL,
+					 &control_ops);
+	if (!_init_inode(control))
+		goto err;
+
+	id = securityfs_create_file("id", 0400, tsem_dir, NULL, &id_ops);
+	if (!_init_inode(id))
+		goto err;
+
+	aggregate = securityfs_create_file("aggregate", 0400, tsem_dir, NULL,
+					   &aggregate_ops);
+	if (!_init_inode(aggregate))
+		goto err;
+
+	internal_tma = securityfs_create_dir("internal_tma", tsem_dir);
+	if (!_init_inode(internal_tma))
+		goto err;
+
+	model = securityfs_create_dir("model0", internal_tma);
+	if (!_init_inode(model))
+		goto err;
+
+	forensics = securityfs_create_file("forensics", 0400, model, NULL,
+					   &forensics_ops);
+	if (!_init_inode(forensics))
+		goto err;
+
+	forensics_counts = securityfs_create_file("forensics_counts", 0400,
+						 model, NULL,
+						 &forensics_count_ops);
+	if (!_init_inode(forensics_counts))
+		goto err;
+
+	forensics_coeff = securityfs_create_file("forensics_coefficients",
+						 0400, model, NULL,
+						 &forensics_point_ops);
+	if (!_init_inode(forensics_coeff))
+		goto err;
+
+	trajectory = securityfs_create_file("trajectory", 0400, model, NULL,
+					     &trajectory_ops);
+	if (!_init_inode(trajectory))
+		goto err;
+
+	trajectory_counts = securityfs_create_file("trajectory_counts", 0400,
+						   model, NULL,
+						   &trajectory_count_ops);
+	if (!_init_inode(trajectory_counts))
+		goto err;
+
+	trajectory_coeff = securityfs_create_file("trajectory_coefficients",
+						  0400, model, NULL,
+						  &trajectory_point_ops);
+	if (!_init_inode(trajectory_coeff))
+		goto err;
+
+	measurement = securityfs_create_file("measurement", 0400,
+						  model, NULL,
+						  &measurement_ops);
+	if (!_init_inode(measurement))
+		goto err;
+
+	state = securityfs_create_file("state", 0400, model, NULL,
+					&state_ops);
+	if (!_init_inode(state))
+		goto err;
+
+	external_tma = securityfs_create_dir("external_tma", tsem_dir);
+	if (!_init_inode(external_tma))
+		goto err;
+
+	retn = 0;
+
+ done:
+	return retn;
+
+ err:
+	securityfs_remove(tsem_dir);
+	securityfs_remove(control);
+	securityfs_remove(id);
+	securityfs_remove(aggregate);
+	securityfs_remove(internal_tma);
+	securityfs_remove(model);
+	securityfs_remove(forensics);
+	securityfs_remove(forensics_counts);
+	securityfs_remove(forensics_coeff);
+	securityfs_remove(trajectory);
+	securityfs_remove(trajectory_counts);
+	securityfs_remove(trajectory_coeff);
+	securityfs_remove(measurement);
+	securityfs_remove(state);
+	securityfs_remove(external_tma);
+
+	return retn;
+}
Signature · 1 line
2.39.1
#33 lore raw

PATCH v4 7/14 Add namespace implementation.

In reply to Greg Wettstein

The TSEM LSM has its own 'namespace' implementation for restricting the scope of a security model that is independent of other resource namespaces but which acts in much the same manner.

The TSEM control plane is used to signal that a process should leave the root security modeling namespace and institute a new subordinate modeling namespace. Process that derive from the lead process all contribute security event descriptions to the model that is being used to prove the security behavior of the namespace.

Each modeling domain has a unique numeric identifier that is implemented as an unsigned 64 bit value that is incremented each time a new security modeling namespace is created. This guarantees that every security modeling namespace will have a
unique identifier associated with it. The id value of 0 is
reserved for the root security modeling namespace.

Each modeling domain is designated as either internally or externally modeled. An internally modeled domain has its security model implemented by a Trusted Modeling Agent (TMA) implementation that is run in the context of the kernel.

Externally modeled domains have a description of the security event exported to a trust orchestrator running in userspace. That trust orchestrator has an associated Trusted Modeling Agent that implements the root of trust for the security namespace.

A process that exports a security event description is scheduled away into an interruptible sleep state, with the exception of event handlers that are running in atomic context. The trust orchestrator that created the external modeling domain is responsible for using the TSEM control plane to wake the process up and set the trust status of the process to be trusted or untrusted. The trust orchestrator also has the responsibility of addressing the status of a workload that has generated a model violating event that occurs in atomic context.

The namespace exists until the last task running in the context of the namespace exits. The resources associated with the namespace are released at that time.

---
 security/tsem/namespace.c | 530 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 530 insertions(+)
 create mode 100644 security/tsem/namespace.c
security/tsem/namespace.c new file +530 -0
new file mode 100644
index 000000000000..e265a1cc7d09
@@ -0,0 +1,530 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+ *
+ * This file is responsible for managing the TSEM namespace
+ * implementation that allows security models to be implemented for
+ * workloads that are isolated from the root security modeling
+ * namespace.  The namespaces that TSEM implements are
+ * non-heirarchical and are at only a single depth below the root
+ * security modeling namespace.
+ *
+ * Security modeling namespaces are identified by a 64-bit context
+ * identity with context identity 0 being reserved for the root
+ * security modeling namespace.
+ *
+ * The modeling of a namspace can be done by either an internal
+ * trusted modeling agent implementation or by a trusted modeling
+ * agent implementation associated with an external trust
+ * orchestrator.
+ *
+ * This file is responsible for creating and setting up a tsem_context
+ * structure that defines the modeling context being used by a
+ * security modeling namespace.  This structure encapsulates
+ * functionality that is generic to the namespace, such as the
+ * cryptographic digest function that is used for security coefficient
+ * mapping.
+ *
+ * Information for managing externally modeled namespaces are
+ * maintained in the tsem_context structure while information for
+ * internally modeled namespaces is maintained in the tsem_model
+ * structure.  These structures are populated in the tsem_context
+ * structure depending on whether or not an internal or externally
+ * modeled namespace is being created.
+ *
+ * A tsem_context structure has a kref structure that is used to track
+ * the number of uses of a context.  This reference count is
+ * incremented each time a task is allocated in the context of a
+ * security modeling namespace and decremented when a task exists.
+ *
+ * The release of the last reference to a context causes the structure
+ * and its embedded structures to be released, thus freeing the
+ * resources that have been allocated for modeling the namespace.
+ */
+
+#include "tsem.h"
+#include "nsmgr.h"
+
+static u64 context_id;
+
+struct context_key {
+	struct list_head list;
+	u64 context_id;
+	u8 key[HASH_MAX_DIGESTSIZE];
+};
+
+DEFINE_MUTEX(context_id_mutex);
+LIST_HEAD(context_id_list);
+
+static void remove_task_key(u64 context_id)
+{
+	struct context_key *entry, *tmp_entry;
+
+	list_for_each_entry_safe(entry, tmp_entry, &context_id_list, list) {
+		if (context_id == entry->context_id) {
+			list_del(&entry->list);
+			kfree(entry);
+			break;
+		}
+	}
+}
+
+static int generate_task_key(const char *keystr, u64 context_id,
+			     struct tsem_task *t_ttask,
+			     struct tsem_task *p_ttask)
+{
+	int retn;
+	bool found_key, valid_key = false;
+	unsigned int size = tsem_digestsize();
+	struct context_key *entry;
+
+	while (!valid_key) {
+		get_random_bytes(t_ttask->task_key, size);
+		retn = tsem_ns_event_key(t_ttask->task_key, keystr,
+					 p_ttask->task_key);
+		if (retn)
+			goto done;
+
+		if (list_empty(&context_id_list))
+			break;
+
+		found_key = false;
+		list_for_each_entry(entry, &context_id_list, list) {
+			if (!memcmp(entry->key, p_ttask->task_key, size)) {
+				found_key = true;
+				break;
+			}
+		}
+		if (!found_key)
+			valid_key = true;
+	}
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry) {
+		retn = -ENOMEM;
+		goto done;
+	}
+
+	entry->context_id = context_id;
+	memcpy(entry->key, p_ttask->task_key, size);
+	list_add_tail(&entry->list, &context_id_list);
+	retn = 0;
+
+ done:
+	return retn;
+}
+
+static struct tsem_external *allocate_external(u64 context_id,
+					       const char *keystr)
+{
+	int retn = -ENOMEM;
+	char bufr[20 + 1];
+	struct tsem_external *external;
+	struct tsem_task *t_ttask = tsem_task(current);
+	struct tsem_task *p_ttask = tsem_task(current->real_parent);
+
+	external = kzalloc(sizeof(*external), GFP_KERNEL);
+	if (!external)
+		goto done;
+
+	retn = generate_task_key(keystr, context_id, t_ttask, p_ttask);
+	if (retn)
+		goto done;
+
+	spin_lock_init(&external->export_lock);
+	INIT_LIST_HEAD(&external->export_list);
+
+	init_waitqueue_head(&external->wq);
+
+	scnprintf(bufr, sizeof(bufr), "%llu", context_id);
+	external->dentry = tsem_fs_create_external(bufr);
+	if (IS_ERR(external->dentry)) {
+		retn = PTR_ERR(external->dentry);
+		external->dentry = NULL;
+	} else
+		retn = 0;
+
+ done:
+	if (retn) {
+		memset(t_ttask->task_key, '\0', tsem_digestsize());
+		memset(p_ttask->task_key, '\0', tsem_digestsize());
+		kfree(external);
+		remove_task_key(context_id);
+		external = ERR_PTR(retn);
+	} else
+		p_ttask->tma_for_ns = context_id;
+
+	return external;
+}
+
+static struct tsem_external *allocate_export(u64 context_id)
+{
+	int retn = -ENOMEM;
+	char bufr[20 + 1];
+	struct tsem_external *external;
+
+	external = kzalloc(sizeof(*external), GFP_KERNEL);
+	if (!external)
+		goto done;
+
+	spin_lock_init(&external->export_lock);
+	INIT_LIST_HEAD(&external->export_list);
+
+	init_waitqueue_head(&external->wq);
+
+	scnprintf(bufr, sizeof(bufr), "%llu", context_id);
+	external->dentry = tsem_fs_create_external(bufr);
+	if (IS_ERR(external->dentry)) {
+		retn = PTR_ERR(external->dentry);
+		external->dentry = NULL;
+	} else
+		retn = 0;
+
+ done:
+	if (retn) {
+		kfree(external);
+		external = ERR_PTR(retn);
+	}
+	return external;
+}
+
+static void _release_inode_instances(u64 id, struct tsem_inode *tsip)
+{
+	struct tsem_inode_instance *owner, *tmp_owner;
+
+	mutex_lock(&tsip->instance_mutex);
+	list_for_each_entry_safe(owner, tmp_owner, &tsip->instance_list,
+				 list) {
+		if (id == owner->creator) {
+			list_del(&owner->list);
+			kfree(owner);
+		}
+	}
+	mutex_unlock(&tsip->instance_mutex);
+}
+
+static void wq_put(struct work_struct *work)
+{
+	struct tsem_context *ctx;
+	struct tsem_inode_entry *ie, *tmp_ie;
+
+	ctx = container_of(work, struct tsem_context, work);
+
+	mutex_lock(&ctx->inode_mutex);
+	list_for_each_entry_safe(ie, tmp_ie, &ctx->inode_list, list) {
+		list_del(&ie->list);
+		_release_inode_instances(ctx->id, ie->tsip);
+		kfree(ie);
+	}
+	mutex_unlock(&ctx->inode_mutex);
+
+	if (ctx->external) {
+		mutex_lock(&context_id_mutex);
+		remove_task_key(ctx->id);
+		mutex_unlock(&context_id_mutex);
+
+		securityfs_remove(ctx->external->dentry);
+		tsem_export_magazine_free(ctx->external);
+		kfree(ctx->external);
+	} else
+		tsem_model_free(ctx);
+
+	tsem_nsmgr_put(ctx->ops);
+
+	crypto_free_shash(ctx->tfm);
+	tsem_event_magazine_free(ctx);
+	kfree(ctx->digestname);
+	kfree(ctx);
+}
+
+static void ns_free(struct kref *kref)
+{
+	struct tsem_context *ctx;
+
+	ctx = container_of(kref, struct tsem_context, kref);
+
+	INIT_WORK(&ctx->work, wq_put);
+	if (!queue_work(system_wq, &ctx->work))
+		WARN_ON_ONCE(1);
+}
+
+/**
+ * tsem_ns_put() - Release a reference to a modeling context.
+ * @ctx: A pointer to the TMA context for which a reference is
+ *	 to be released.
+ *
+ * This function is called to release a reference to a TMA modeling
+ * domain.  The release of the last reference calls the ns_free()
+ * function that schedules the actual work to release the resources
+ * associated with the namespace to a workqueue.
+ */
+void tsem_ns_put(struct tsem_context *ctx)
+{
+	kref_put(&ctx->kref, ns_free);
+}
+
+/**
+ * tsem_ns_event_key() - Generate TMA authentication key.
+ * @task_key: A pointer to the buffer containing the task identification
+ *	      key that was randomly generated for the modeling domain.
+ * @keystr: A pointer to the buffer containing the TMA authentication key
+ *	    in ASCII hexadecimal form.
+ *
+ * This function generates the authentication key that will be used
+ * to validate a call by a TMA to set the trust status of the process.
+ *
+ * Return: This function returns 0 if the key was properly generated
+ *	   or a negative value if a hashing error occurred.
+ */
+int tsem_ns_event_key(u8 *task_key, const char *keystr, u8 *key)
+{
+	bool retn;
+	u8 tma_key[HASH_MAX_DIGESTSIZE];
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	retn = hex2bin(tma_key, keystr, tsem_digestsize());
+	if (retn)
+		return -EINVAL;
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		return retn;
+
+	retn = crypto_shash_update(shash, task_key, tsem_digestsize());
+	if (retn)
+		return retn;
+
+	return crypto_shash_finup(shash, tma_key, tsem_digestsize(), key);
+}
+
+static struct crypto_shash *configure_digest(const char *digest,
+					     char **digestname,
+					     u8 *zero_digest)
+{
+	int retn;
+	struct crypto_shash *tfm;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	*digestname = kstrdup(digest, GFP_KERNEL);
+	if (!*digestname)
+		return ERR_PTR(-ENOMEM);
+
+	tfm = crypto_alloc_shash(digest, 0, 0);
+	if (IS_ERR(tfm))
+		return tfm;
+
+	shash->tfm = tfm;
+	retn = crypto_shash_digest(shash, NULL, 0, zero_digest);
+	if (retn) {
+		crypto_free_shash(tfm);
+		tfm = NULL;
+	}
+
+	return tfm;
+}
+
+/**
+ * tsem_ns_create() - Create a TSEM modeling namespace.
+ * @type:   The type of namespace being created.
+ * @digest: A null terminated character buffer containing the name
+ *	    of the hash function that is to be used for the modeling
+ *	    domain.
+ * @ns:     The enumeration type that specifies whether the security
+ *	    event descriptions should reference the initial user
+ *	    namespace or the current user namespace.
+ * @key:    A pointer to a null-terminated buffer containing the key
+ *	    that will be used to authenticate the TMA's ability to set
+ *	    the trust status of a process.
+ * @cache_size: The number of entries to be implemented in the
+ *		atomic allocation magazines for the security modeling
+ *		namespace being created.
+ * @ops:    A pointer to the security namespace definitions and
+ *	    operations for the namespace being created.
+ *
+ * This function is used to create either an internally or externally
+ * modeled TSEM namespace.  The type of the namespace to be created
+ * is specified with the tsem_control_type enumeration value.  A
+ * request for an internally model namespace causes a new structure to be
+ * allocated that will hold the description of the security model.
+ * An externally modeled domain will have a control structure allocated
+ * that manages the export of security event descriptions to the
+ * trust orchestrator that is responsible for running the TMA
+ * implementation.
+ *
+ * Return: This function returns 0 if the namespace was created and
+ *	   a negative error value on error.
+ */
+int tsem_ns_create(const enum tsem_control_type type, const char *digest,
+		   const enum tsem_ns_reference ns, const char *key,
+		   unsigned int cache_size, const struct tsem_context_ops *ops)
+{
+	u8 zero_digest[HASH_MAX_DIGESTSIZE];
+	char *use_digest;
+	int retn = -ENOMEM;
+	u64 new_id;
+	struct tsem_task *tsk = tsem_task(current);
+	struct tsem_context *new_ctx;
+	struct tsem_model *model = NULL;
+	struct crypto_shash *tfm;
+
+	tfm = configure_digest(digest, &use_digest, zero_digest);
+	if (IS_ERR(tfm))
+		return PTR_ERR(tfm);
+
+	new_ctx = kzalloc(sizeof(*new_ctx), GFP_KERNEL);
+	if (!new_ctx)
+		return retn;
+
+	mutex_lock(&context_id_mutex);
+	new_id = context_id + 1;
+
+	retn = tsem_event_magazine_allocate(new_ctx, cache_size);
+	if (retn)
+		goto done;
+
+	if (type == TSEM_CONTROL_INTERNAL) {
+		model = tsem_model_allocate(cache_size);
+		if (!model)
+			goto done;
+		new_ctx->model = model;
+	}
+	if (type == TSEM_CONTROL_EXTERNAL) {
+		if (crypto_shash_digestsize(tfm)*2 != strlen(key)) {
+			retn = -EINVAL;
+			goto done;
+		}
+
+		new_ctx->external = allocate_external(new_id, key);
+		if (IS_ERR(new_ctx->external)) {
+			retn = PTR_ERR(new_ctx->external);
+			new_ctx->external = NULL;
+			goto done;
+		}
+
+		retn = tsem_export_magazine_allocate(new_ctx->external,
+						     cache_size);
+		if (retn)
+			goto done;
+	}
+	if (type == TSEM_CONTROL_EXPORT) {
+		new_ctx->external = allocate_export(new_id);
+		if (IS_ERR(new_ctx->external)) {
+			retn = PTR_ERR(new_ctx->external);
+			new_ctx->external = NULL;
+			goto done;
+		}
+
+		retn = tsem_export_magazine_allocate(new_ctx->external,
+						     cache_size);
+		if (retn)
+			goto done;
+
+		new_ctx->external->export_only = true;
+	}
+
+
+	kref_init(&new_ctx->kref);
+
+	new_ctx->id = new_id;
+	new_ctx->tfm = tfm;
+	new_ctx->ops = ops;
+	new_ctx->digestname = use_digest;
+	new_ctx->timestamp = ktime_get_boottime_ns();
+	memcpy(new_ctx->zero_digest, zero_digest,
+	       crypto_shash_digestsize(tfm));
+
+	mutex_init(&new_ctx->inode_mutex);
+	INIT_LIST_HEAD(&new_ctx->inode_list);
+
+	if (ns == TSEM_NS_CURRENT)
+		new_ctx->use_current_ns = true;
+	memcpy(new_ctx->actions, tsk->context->actions,
+	       sizeof(new_ctx->actions));
+	retn = 0;
+
+ done:
+	if (retn) {
+		if (type != TSEM_CONTROL_EXPORT)
+			remove_task_key(new_id);
+		crypto_free_shash(tfm);
+		tsem_event_magazine_free(new_ctx);
+		kfree(use_digest);
+		if (new_ctx->external)
+			tsem_export_magazine_free(new_ctx->external);
+		kfree(new_ctx->external);
+		kfree(new_ctx);
+		kfree(model);
+	} else {
+		context_id = new_id;
+		tsk->context = new_ctx;
+		if (type == TSEM_CONTROL_EXTERNAL)
+			retn = tsem_export_aggregate();
+		if (type == TSEM_CONTROL_INTERNAL)
+			retn = tsem_model_add_aggregate();
+	}
+
+	mutex_unlock(&context_id_mutex);
+	return retn;
+}
+
+/**
+ * tsem_ns_export_root() - Configure root namespace for export only modeling.
+ * @magazine_size:  The number of entries to be implemented in the event
+ *		    cache.
+ *
+ * This function is called to setup the root security modeling namespace
+ * for the export of security event descriptions in tsem_mode=2
+ * operation.
+ *
+ * Return: This function returns 0 if the setup of the root namespace
+ *	   for export was successul and a negative error value if
+ *	   the setup fails.
+ */
+int tsem_ns_export_root(unsigned int magazine_size)
+{
+	int retn = -ENOMEM;
+	struct tsem_context *new_ctx;
+	struct tsem_task *tsk = tsem_task(current);
+
+	new_ctx = kzalloc(sizeof(*new_ctx), GFP_KERNEL);
+	if (!new_ctx)
+		return retn;
+	*new_ctx = *tsem_context(current);
+
+	new_ctx->external = allocate_export(tsem_context(current)->id);
+	if (IS_ERR(new_ctx->external)) {
+		retn = PTR_ERR(new_ctx->external);
+		new_ctx->external = NULL;
+		goto done;
+	}
+
+	retn = tsem_export_magazine_allocate(new_ctx->external, magazine_size);
+	if (retn)
+		goto done;
+
+	new_ctx->external->export_only = true;
+
+ done:
+	if (retn) {
+		tsem_event_magazine_free(new_ctx);
+		if (new_ctx->external)
+			tsem_export_magazine_free(new_ctx->external);
+		kfree(new_ctx);
+	} else {
+		new_ctx->tfm = tsk->context->tfm;
+		new_ctx->timestamp = ktime_get_boottime_ns();
+		new_ctx->digestname = tsk->context->digestname;
+		memcpy(new_ctx->zero_digest, tsk->context->zero_digest,
+		       crypto_shash_digestsize(tsk->context->tfm));
+
+		mutex_init(&new_ctx->inode_mutex);
+		INIT_LIST_HEAD(&new_ctx->inode_list);
+
+		tsk->context = new_ctx;
+	}
+
+	return retn;
+}
Signature · 1 line
2.39.1
#34 lore raw

PATCH v4 8/14 Add security event description export facility.

In reply to Greg Wettstein

The functionality for surfacing security model events to an external modeling domain is implemented in the export.c file.

ASCII descriptions of the events are presented to a userspace trust orchestrator through the following pseudo-files:

/sys/kernel/security/tsem/external_tma/N

Where N is replaced with security namespace identifier. This identifier is a 64-bit integer value incremented each time a namespace is created

The following event types are exported:

export_event

async_event

aggregate_event

log_event

An export_event is used to surface the JSON encoded description of a security event to the trust orchestrator responsible for managing the security modeling namespace.

The async_event is identical to an export_event but the alternate definition is used to indicate to the trust orchestrator that the process that generated the event is running in atomic context.

The aggregate_event is used to provide the hardware platform measurement value to the trust orchestrator for integration into the security model by the associated trusted modeling agent.

A log_event is used to provide descriptions of security events that are invoked by untrusted processes.

The /sys/kernel/security/tsem/external_tma/N pseudo-files implement a pollable interface that the trust orchestrators can use to wait on events. After placing the event description into the device queue a process that is not running in atomic context is placed in an interruptible sleep state.

After the TMA completes modeling of the event, the trust orchestrator is responsible for using the TSEM control plane to wake the process that exported the event and set its status to either trusted or untrusted.

Security modeling namespaces, including the root modeling namespace, can be configured to run in export only mode. In this mode the security event descriptions are asynchronously exported to the trust orchestrator for driving applications such as machine learning models or other Host Intrusion Detection schemes.

---
 security/tsem/export.c | 429 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 429 insertions(+)
 create mode 100644 security/tsem/export.c
security/tsem/export.c new file +429 -0
new file mode 100644
index 000000000000..ce2a1a967a87
@@ -0,0 +1,429 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+ *
+ * This file implements the export of security relevant events to
+ * an external trust orchestrator.  The primary TSEM security
+ * documentation describes the types of events that are exported.
+ *
+ * The structures used to export each event are provided either by the
+ * kmem_cache implementation maintained in this file or from a
+ * magazine of structures that are maintained for events that are
+ * running in atomic context.
+ *
+ * The events are exported through the following control plane file:
+ *
+ * /sys/kernel/security/tsem/external_tma/N
+ *
+ * Where N is a filename consisting of the context identifier of the
+ * security modeling namespace that generated the event.
+ *
+ * A description of the security relevant event being exported is
+ * encoded in JSON format.  The TSEM ABI documentation has a
+ * description of the encoding that is used.
+ *
+ * For export events that describe a security event the JSON encoding
+ * of the event description is provided by the
+ * tsem_fs_show_trajectory() function.  This is the same function that
+ * is used to generate the security event descriptions for internally
+ * modeled namespaces.
+ *
+ * Processes that are generating security events in non-atomic context
+ * are put to sleep and placed on a wait queue to be scheduled back
+ * into execution by the external trust orchestrator after the event
+ * is modeled for conformance with the enforced security model.
+ *
+ * Processes running in atomic context export the event and continue
+ * to run.  A trust orchestrator is responsible for determing how to
+ * address a workload that generates a model violating event, either
+ * by shutting down the workload or generating a security alert via
+ * the trust orchestration
+ *
+ * Security modeling namespaces, including the root namespace, can
+ * also be configured for export only mode where the only purpose of
+ * the export event is to hand a description of the event to
+ * userspace.  This type of export is handled in the same manner as
+ * an event being modeled that is running in atomic context.
+ */
+
+#include <linux/seq_file.h>
+
+#include "tsem.h"
+
+enum export_type {
+	AGGREGATE_EVENT = 1,
+	EXPORT_EVENT,
+	EXPORT_ASYNC_EVENT,
+	LOG_EVENT
+};
+
+struct action_description {
+	enum export_type type;
+	enum tsem_action_type action;
+	char comm[TASK_COMM_LEN];
+};
+
+struct export_event {
+	struct list_head list;
+	enum export_type type;
+	union {
+		u8 *aggregate[HASH_MAX_DIGESTSIZE];
+		struct tsem_event *ep;
+		struct action_description action;
+	} u;
+};
+
+static const char * const tsem_actions[TSEM_ACTION_CNT] = {
+	"LOG",
+	"DENY"
+};
+
+static struct kmem_cache *export_cachep;
+
+static void refill_export_magazine(struct work_struct *work)
+{
+	struct export_event *exp;
+	struct tsem_external *ext;
+	struct tsem_work *ws;
+
+	ws = container_of(work, struct tsem_work, work);
+	ext = ws->u.ext;
+
+	exp = kmem_cache_zalloc(export_cachep, GFP_KERNEL);
+	if (!exp) {
+		pr_warn("tsem: Cannot refill event magazine.\n");
+		return;
+	}
+
+	spin_lock(&ws->u.ext->magazine_lock);
+	ws->u.ext->magazine[ws->index] = exp;
+	clear_bit(ws->index, ws->u.ext->magazine_index);
+
+	/*
+	 * The following memory barrier is used to cause the magazine
+	 * index to be visible after the refill of the cache slot.
+	 */
+	smp_mb__after_atomic();
+	spin_unlock(&ws->u.ext->magazine_lock);
+}
+
+static struct export_event *allocate_export(bool locked)
+{
+	unsigned int index;
+	struct export_event *exp = NULL;
+	struct tsem_external *ext = tsem_context(current)->external;
+
+	if (!locked)
+		return kmem_cache_zalloc(export_cachep, GFP_KERNEL);
+
+	spin_lock(&ext->magazine_lock);
+	index = find_first_zero_bit(ext->magazine_index, ext->magazine_size);
+	if (index < ext->magazine_size) {
+		exp = ext->magazine[index];
+		ext->ws[index].index = index;
+		ext->ws[index].u.ext = ext;
+		set_bit(index, ext->magazine_index);
+
+		/*
+		 * Similar to the issue noted in the refill_event_magazine()
+		 * function, this barrier is used to cause the consumption
+		 * of the cache entry to become visible.
+
+		 */
+		smp_mb__after_atomic();
+	}
+
+	spin_unlock(&ext->magazine_lock);
+
+	if (exp) {
+		INIT_WORK(&ext->ws[index].work, refill_export_magazine);
+		queue_work(system_highpri_wq, &ext->ws[index].work);
+		return exp;
+	}
+
+	pr_warn("tsem: Fail export allocation comm %s ns %llu cs %u.\n",
+		current->comm, tsem_context(current)->id, ext->magazine_size);
+	return NULL;
+}
+
+static void trigger_event(struct tsem_context *ctx)
+{
+	ctx->external->have_event = true;
+	wake_up_interruptible(&ctx->external->wq);
+}
+
+int tsem_export_show(struct seq_file *sf, void *v)
+{
+	struct export_event *exp = NULL;
+	struct tsem_context *ctx = tsem_context(current);
+
+	if (!ctx->id && !ctx->external)
+		return -ENODATA;
+
+	spin_lock(&ctx->external->export_lock);
+	if (!list_empty(&ctx->external->export_list)) {
+		exp = list_first_entry(&ctx->external->export_list,
+				       struct export_event, list);
+		list_del(&exp->list);
+	}
+	spin_unlock(&ctx->external->export_lock);
+
+	if (!exp)
+		return -ENODATA;
+
+	seq_putc(sf, '{');
+	tsem_fs_show_field(sf, "export");
+
+	switch (exp->type) {
+	case AGGREGATE_EVENT:
+		tsem_fs_show_key(sf, "type", "}, ", "%s", "aggregate");
+		tsem_fs_show_field(sf, "aggregate");
+		tsem_fs_show_key(sf, "value", "}", "%*phN", tsem_digestsize(),
+				 exp->u.aggregate);
+		break;
+
+	case EXPORT_EVENT:
+		tsem_fs_show_key(sf, "type", "}, ", "%s", "event");
+		tsem_fs_show_trajectory(sf, exp->u.ep);
+		tsem_event_put(exp->u.ep);
+		break;
+
+	case EXPORT_ASYNC_EVENT:
+		tsem_fs_show_key(sf, "type", "}, ", "%s", "async_event");
+		tsem_fs_show_trajectory(sf, exp->u.ep);
+		tsem_event_put(exp->u.ep);
+		break;
+
+	case LOG_EVENT:
+		tsem_fs_show_key(sf, "type", "}, ", "%s", "log");
+		tsem_fs_show_field(sf, "log");
+		tsem_fs_show_key(sf, "process", ",", "%s", exp->u.action.comm);
+		tsem_fs_show_key(sf, "event", ",", "%s",
+				 tsem_names[exp->u.action.type]);
+		tsem_fs_show_key(sf, "action", "}", "%s",
+				 tsem_actions[exp->u.action.action]);
+		break;
+	}
+	seq_puts(sf, "}\n");
+
+	kmem_cache_free(export_cachep, exp);
+	return 0;
+}
+
+/**
+ * tsem_export_event() - Export a security event description.
+ * @event: The TSEM event type number for which the log event is being
+ *	   generated.
+ *
+ * This function queues for export to an external modeling agent a
+ * security event description.
+ *
+ * Return: This function returns 0 if the export was successful or
+ *	   an error value if it was not.
+ */
+int tsem_export_event(struct tsem_event *ep)
+{
+	int retn = 0;
+	struct tsem_task *task = tsem_task(current);
+	struct tsem_context *ctx = task->context;
+	struct export_event *exp;
+
+	exp = allocate_export(ep->locked);
+	if (!exp)
+		return -ENOMEM;
+
+	exp->type = ep->locked ? EXPORT_ASYNC_EVENT : EXPORT_EVENT;
+	exp->u.ep = ep;
+	tsem_event_get(ep);
+
+	spin_lock(&ctx->external->export_lock);
+	list_add_tail(&exp->list, &ctx->external->export_list);
+	spin_unlock(&ctx->external->export_lock);
+
+	if (ctx->external->export_only || ep->locked) {
+		trigger_event(ctx);
+		return 0;
+	}
+
+	task->trust_status |= TSEM_TASK_TRUST_PENDING;
+	trigger_event(ctx);
+
+	while (task->trust_status & TSEM_TASK_TRUST_PENDING) {
+		set_current_state(TASK_INTERRUPTIBLE);
+		schedule();
+		if (signal_pending(current)) {
+			if (sigismember(&current->pending.signal, SIGKILL) ||
+			    sigismember(&current->signal->shared_pending.signal,
+					SIGKILL))
+				task->trust_status = TSEM_TASK_UNTRUSTED;
+		}
+	}
+
+	return retn;
+}
+
+/**
+ * tsem_export_action() - Exports the action taken to a security violation.
+ * @event: The TSEM event type number for which the log event is being
+ *	   generated.
+ * @locked: A boolean flag indicating whether or not the security hook
+ *	    being reported on is called in atomic context.
+ *
+ * This function queues for export a description of an event that
+ * was being disciplined.
+ *
+ * Return: This function returns 0 if the export was successful or
+ *	   an error value if it was not.
+ */
+int tsem_export_action(enum tsem_event_type event, bool locked)
+{
+	struct tsem_context *ctx = tsem_context(current);
+	struct export_event *exp;
+
+	exp = allocate_export(locked);
+	if (!exp) {
+		pr_warn("tsem: domain %llu failed export allocation.\n",
+			ctx->id);
+		return -ENOMEM;
+	}
+
+	exp->type = LOG_EVENT;
+	exp->u.action.type = event;
+	exp->u.action.action = ctx->actions[event];
+	strscpy(exp->u.action.comm, current->comm, sizeof(exp->u.action.comm));
+
+	spin_lock(&ctx->external->export_lock);
+	list_add_tail(&exp->list, &ctx->external->export_list);
+	spin_unlock(&ctx->external->export_lock);
+
+	trigger_event(ctx);
+
+	return 0;
+}
+
+/**
+ * tsem_export_aggregate() - Exports the hardware aggregate value.
+ *
+ * This function exports the hardware aggregate measurement for
+ * the platform on which the TSEM LSM is being run on.
+ *
+ * Return: This function returns a value of 0 if the export was
+ *	   successful or a non-zero return value if the export was
+ *	   not successful.
+ */
+int tsem_export_aggregate(void)
+{
+	struct tsem_context *ctx = tsem_context(current);
+	struct export_event *exp;
+
+	exp = kmem_cache_zalloc(export_cachep, GFP_KERNEL);
+	if (!exp)
+		return -ENOMEM;
+
+	exp->type = AGGREGATE_EVENT;
+	memcpy(exp->u.aggregate, tsem_trust_aggregate(), tsem_digestsize());
+
+	spin_lock(&ctx->external->export_lock);
+	list_add_tail(&exp->list, &ctx->external->export_list);
+	spin_unlock(&ctx->external->export_lock);
+
+	trigger_event(ctx);
+
+	return 0;
+}
+
+/**
+ * tsem export_magazine_allocate() - Allocate a TSEM export magazine.
+ * @ctx: A pointer to the external modeling context that the magazine is
+ *	 to be allocated for.
+ * @size: The number of entries to be created in the magazine.
+
+ * The security event export magazine is an array of export_event
+ * structures that are used to service security hooks that are called
+ * in atomic context.  Each external modeling domain has a magazine
+ * allocated to it and this function allocates and initializes the
+ * memory structures needed to manage that magazine.
+
+ * Return: This function returns a value of zero on success and a negative
+ *	   error code on failure.
+ */
+int tsem_export_magazine_allocate(struct tsem_external *ext, size_t size)
+{
+	unsigned int lp;
+	int retn = -ENOMEM;
+
+	ext->magazine_size = size;
+
+	spin_lock_init(&ext->magazine_lock);
+
+	ext->magazine_index = bitmap_zalloc(ext->magazine_size, GFP_KERNEL);
+	if (!ext->magazine_index)
+		return retn;
+
+	ext->magazine = kcalloc(ext->magazine_size, sizeof(*ext->magazine),
+				GFP_KERNEL);
+	if (!ext->magazine)
+		goto done;
+
+	for (lp = 0; lp < ext->magazine_size; ++lp) {
+		ext->magazine[lp] = kmem_cache_zalloc(export_cachep,
+						      GFP_KERNEL);
+		if (!ext->magazine[lp])
+			goto done;
+	}
+
+	ext->ws = kcalloc(ext->magazine_size, sizeof(*ext->ws), GFP_KERNEL);
+	if (ext->ws)
+		retn = 0;
+
+ done:
+	if (retn)
+		tsem_export_magazine_free(ext);
+
+	return retn;
+}
+
+/**
+ * tsem export_magazine_free() - Releases a TSEM export magazine
+ * @ctx: A pointer to the external modeling context whose magazine is
+ *	 to be released.
+ *
+ * The function is used to free the memory that was allocated by
+ * the tsem_export_magazine_allocate() function for an extenral
+ * modeling context.
+ */
+void tsem_export_magazine_free(struct tsem_external *ext)
+{
+	unsigned int lp;
+
+	for (lp = 0; lp < ext->magazine_size; ++lp)
+		kmem_cache_free(export_cachep, ext->magazine[lp]);
+
+	bitmap_free(ext->magazine_index);
+	kfree(ext->ws);
+	kfree(ext->magazine);
+}
+
+/**
+ * tsem export_cache_init() - Initialize the TSEM export cache.
+ *
+ * This function is called by the TSEM initialization function and sets
+ * up a cache for export structures that are called by security event
+ * descriptions that are generated in atomix context
+ *
+ * Return: This function returns a value of zero on success and a negative
+ *	   error code on failure.
+ */
+int __init tsem_export_cache_init(void)
+{
+
+	export_cachep = kmem_cache_create("tsem_export_cache",
+					 sizeof(struct export_event), 0,
+					 SLAB_PANIC, 0);
+	if (!export_cachep)
+		return -ENOMEM;
+
+	return 0;
+}
Signature · 1 line
2.39.1
#35 lore raw

PATCH v4 9/14 Add event processing implementation.

In reply to Greg Wettstein

The event.c file implements support for packaging the description of an event into its Context Of Execution (COE) and CELL components for subsequent modeling by an internal Trusted Modeling Agent or export to a trust orchestrator.

The tsem_event_allocate() function is called by every security event handler to allocate a structure that will be used to retain the characteristics of the security event until the namespace terminates or the event is exported to userspace.

The tsem_init_init() function converts the parameters supplied to the security handler to the values that will be retained.

The event description structures are allocated from a kmem_cache based allocation system. In addition to allocations from this source, a magazine of pre-allocated structures is used to serve requests for security events by processes running in atomic context. The magazine slots are refilled by work functions dispatched to the system high priority workqueue.

---
 security/tsem/event.c | 1846 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 1846 insertions(+)
 create mode 100644 security/tsem/event.c
security/tsem/event.c new file +1846 -0
new file mode 100644
index 000000000000..349108be77c8
@@ -0,0 +1,1846 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+ *
+ * This file handles the creation and population of the tsem_event
+ * structure that describes each security event that occurs.  The
+ * structures that are held in the CELL union of the tsem_event
+ * structure are used to deliver the characterizing parameters of a
+ * security into the tsem_event_init() function.
+ *
+ * Most of the structures used to characterize a security event use
+ * a strategy where a union is used to enclose an 'in' and 'out'
+ * structure.  The parameters that are relevant to the TSEM
+ * characterization of an event are placed in the 'in' structure.  The
+ * routines in this function are responsible for propagating these
+ * characteristics into the 'out' structure for the lifetime of the
+ * structure.
+ */
+
+#include <linux/iversion.h>
+#include <linux/user_namespace.h>
+#include <linux/base64.h>
+#include <linux/ipv6.h>
+#include <uapi/linux/prctl.h>
+
+#include "../integrity/integrity.h"
+#include "tsem.h"
+
+static struct kmem_cache *event_cachep;
+static bool created_inode(struct tsem_inode *tsip)
+{
+	return tsip->created && tsip->creator == tsem_context(current)->id;
+}
+
+static int register_directory(struct tsem_inode *tsip)
+{
+	int retn = 0;
+	struct tsem_inode_entry *entry = NULL;
+	struct tsem_context *ctx = tsem_context(current);
+
+	mutex_lock(&ctx->inode_mutex);
+	list_for_each_entry(entry, &ctx->inode_list, list) {
+		if (entry->tsip == tsip)
+			goto done;
+	}
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry) {
+		retn = -ENOMEM;
+		goto done;
+	}
+
+	entry->tsip = tsip;
+	list_add_tail(&entry->list, &ctx->inode_list);
+
+ done:
+	mutex_unlock(&ctx->inode_mutex);
+	return retn;
+}
+
+static int register_inode_create(struct inode *dir, u64 instance,
+				 char *pathname)
+{
+	char *p;
+	int retn;
+	struct tsem_inode_instance *tio = NULL;
+	struct tsem_inode *tsip = tsem_inode(dir);
+
+	p = strrchr(pathname, '/');
+	if (!p)
+		return -EINVAL;
+
+	if (tsem_context(current)->id) {
+		retn = register_directory(tsip);
+		if (retn)
+			return retn;
+	}
+
+	tio = kzalloc(sizeof(*tio), GFP_KERNEL);
+	if (!tio)
+		return -ENOMEM;
+
+	tio->pathname = ++p;
+	tio->creator = tsem_context(current)->id;
+	tio->instance = instance;
+	memcpy(tio->owner, tsem_task(current)->task_id, tsem_digestsize());
+
+	mutex_lock(&tsip->create_mutex);
+	list_add_tail(&tio->list, &tsip->create_list);
+	mutex_unlock(&tsip->create_mutex);
+
+	return 0;
+}
+
+static int get_root(struct dentry *dentry, struct tsem_path *path)
+{
+	int size, retn = 0;
+	char *p, *p1, *pathbuffer = NULL;
+	struct super_block *sb = dentry->d_sb;
+	struct inode *inode = d_backing_inode(sb->s_root);
+
+	if (MAJOR(sb->s_dev) || inode->i_op->rename)
+		path->dev = sb->s_dev;
+	else {
+		if (dentry->d_op && dentry->d_op->d_dname) {
+			pathbuffer = __getname();
+			p = dentry->d_op->d_dname(dentry, pathbuffer, 4096);
+			p1 = strchr(p, ':');
+			if (p1)
+				*p1 = '\0';
+
+			size = strlen(p) + 3;
+			path->pathname = kmalloc(size, GFP_KERNEL);
+			if (IS_ERR(path->pathname)) {
+				retn = PTR_ERR(path->pathname);
+				goto done;
+			}
+
+			strscpy(path->pathname, p, size);
+			strcat(path->pathname, ":/");
+			__putname(pathbuffer);
+		} else {
+			p = "nodev:/";
+			size = strlen(p) + 1;
+
+			path->pathname = kmalloc(size, GFP_KERNEL);
+			if (IS_ERR(path->pathname)) {
+				retn = PTR_ERR(path->pathname);
+				goto done;
+			}
+
+			strscpy(path->pathname, p, size);
+		}
+	}
+
+ done:
+	return retn;
+}
+
+static char *get_path(const struct path *path)
+{
+	int retn = 0;
+	const char *pathname = NULL;
+	char *retpath, *pathbuffer = NULL;
+
+	pathbuffer = __getname();
+	if (pathbuffer) {
+		pathname = d_absolute_path(path, pathbuffer, PATH_MAX);
+		if (IS_ERR(pathname)) {
+			__putname(pathbuffer);
+			pathbuffer = NULL;
+			pathname = NULL;
+		}
+	}
+
+	if (pathname)
+		retpath = kstrdup(pathname, GFP_KERNEL);
+	else
+		retpath = kstrdup(path->dentry->d_name.name, GFP_KERNEL);
+	if (!retpath)
+		retn = -ENOMEM;
+
+	if (pathbuffer)
+		__putname(pathbuffer);
+	if (retn)
+		retpath = ERR_PTR(retn);
+	return retpath;
+}
+
+static char *get_path_dentry(const struct dentry *dentry)
+{
+	int retn = 0;
+	const char *pathname = NULL;
+	char *retpath, *pathbuffer = NULL;
+
+	pathbuffer = __getname();
+	if (pathbuffer) {
+		pathname = dentry_path_raw(dentry, pathbuffer, PATH_MAX);
+		if (IS_ERR(pathname)) {
+			__putname(pathbuffer);
+			pathbuffer = NULL;
+			pathname = NULL;
+		}
+	}
+
+	if (pathname)
+		retpath = kstrdup(pathname, GFP_KERNEL);
+	else
+		retpath = kstrdup(dentry->d_name.name, GFP_KERNEL);
+	if (!retpath)
+		retn = -ENOMEM;
+
+	if (pathbuffer)
+		__putname(pathbuffer);
+	if (retn)
+		retpath = ERR_PTR(retn);
+	return retpath;
+}
+
+static void _fill_mount_path(struct tsem_inode *tsip, struct tsem_path *path)
+{
+	struct tsem_inode_instance *entry, *retn = NULL;
+
+	mutex_lock(&tsem_model(current)->mount_mutex);
+	list_for_each_entry(entry, &tsem_model(current)->mount_list, list) {
+		if (!strcmp(entry->pathname, path->pathname)) {
+			retn = entry;
+			break;
+		}
+	}
+
+	if (retn) {
+		path->created = true;
+		path->creator = retn->creator;
+		path->instance = retn->instance;
+		memcpy(path->owner, retn->owner, tsem_digestsize());
+	}
+	mutex_unlock(&tsem_model(current)->mount_mutex);
+}
+
+static int fill_path(const struct path *in, struct tsem_path *path)
+{
+	int retn;
+	struct tsem_model *model = tsem_model(current);
+	struct tsem_inode *tsip = tsem_inode(d_backing_inode(in->dentry));
+
+	if (created_inode(tsip)) {
+		path->created = tsip->created;
+		path->creator = tsip->creator;
+		path->instance = tsip->instance;
+		memcpy(path->owner, tsip->owner, tsem_digestsize());
+	}
+
+	retn = get_root(in->dentry, path);
+	if (retn)
+		goto done;
+
+	if (path->dev) {
+		path->pathname = get_path(in);
+		if (IS_ERR(path->pathname))
+			retn = PTR_ERR(path->pathname);
+	}
+
+	if (model && !list_empty(&model->mount_list))
+		_fill_mount_path(tsip, path);
+
+ done:
+	return retn;
+}
+
+static int fill_path_dentry(struct dentry *dentry, struct tsem_path *path)
+{
+	int retn;
+
+	retn = get_root(dentry, path);
+	if (retn)
+		goto done;
+
+	path->pathname = get_path_dentry(dentry);
+	if (IS_ERR(path->pathname))
+		retn = PTR_ERR(path->pathname);
+
+ done:
+	return retn;
+}
+
+static struct tsem_inode_digest *find_digest(struct tsem_inode *tsip)
+{
+	struct tsem_inode_digest *digest;
+
+	list_for_each_entry(digest, &tsip->digest_list, list) {
+		if (!strcmp(digest->name, tsem_context(current)->digestname))
+			return digest;
+	}
+
+	return NULL;
+}
+
+static struct tsem_inode_digest *add_digest(struct tsem_context *ctx,
+					    struct tsem_inode *tsip)
+{
+	struct tsem_inode_digest *digest;
+
+	digest = kzalloc(sizeof(*digest), GFP_KERNEL);
+	if (!digest)
+		return NULL;
+
+	digest->name = kstrdup(tsem_context(current)->digestname, GFP_KERNEL);
+	if (!digest->name)
+		return NULL;
+
+	list_add(&digest->list, &tsip->digest_list);
+
+	return digest;
+}
+
+static struct file *open_event_file(struct file *file, unsigned int *status)
+{
+	int flags;
+	struct file *alt_file;
+
+	if (!(file->f_mode & FMODE_CAN_READ)) {
+		file->f_mode |= FMODE_CAN_READ;
+		*status |= 4;
+	}
+	if (file->f_mode & FMODE_READ)
+		return file;
+
+	flags = file->f_flags & ~(O_WRONLY | O_APPEND | O_TRUNC | O_CREAT |
+				  O_NOCTTY | O_EXCL);
+	flags |= O_RDONLY;
+
+	alt_file = dentry_open(&file->f_path, flags, file->f_cred);
+	if (!IS_ERR(alt_file)) {
+		*status |= 1;
+		return alt_file;
+	}
+
+	file->f_flags |= FMODE_READ;
+	*status |= 2;
+	return file;
+}
+
+static int get_file_digest(struct file *file, struct inode *inode,
+			   loff_t size, u8 *digest)
+{
+	u8 *bufr;
+	int retn = 0, rsize;
+	unsigned int open_status = 0;
+	loff_t posn = 0;
+	struct file *read_file;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	bufr = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!bufr) {
+		retn = -ENOMEM;
+		goto done;
+	}
+
+	if (!likely(file->f_op->read || file->f_op->read_iter)) {
+		retn = -EINVAL;
+		goto done;
+	}
+	read_file = open_event_file(file, &open_status);
+
+	while (posn < size) {
+		rsize = integrity_kernel_read(read_file, posn, bufr, 4096);
+		if (rsize < 0) {
+			retn = rsize;
+			break;
+		}
+		if (rsize == 0)
+			break;
+
+		posn += rsize;
+		retn = crypto_shash_update(shash, bufr, rsize);
+		if (retn)
+			break;
+	}
+
+	kfree(bufr);
+	if (!retn)
+		retn = crypto_shash_final(shash, digest);
+
+ done:
+	if (open_status & 1)
+		fput(read_file);
+	if (open_status & 2)
+		file->f_flags &= ~FMODE_READ;
+	if (open_status & 4)
+		file->f_flags &= ~FMODE_CAN_READ;
+	return retn;
+}
+
+static int add_file_digest(struct file *file, bool pseudo_file,
+			   struct tsem_file_args *args)
+{
+	int retn = 0;
+	u8 measurement[HASH_MAX_DIGESTSIZE];
+	loff_t size;
+	struct inode *inode;
+	struct tsem_inode *tsip;
+	struct tsem_inode_digest *digest;
+	struct tsem_context *ctx = tsem_context(current);
+
+	inode = file_inode(file);
+	tsip = tsem_inode(inode);
+
+	if (created_inode(tsip) || pseudo_file || !S_ISREG(inode->i_mode)) {
+		memcpy(args->out.digest, ctx->zero_digest, tsem_digestsize());
+		return 0;
+	}
+
+	mutex_lock(&tsip->digest_mutex);
+	if (!ctx->external) {
+		retn = tsem_model_has_pseudonym(tsip, args->out.path.pathname);
+		if (retn < 0)
+			goto done;
+		if (retn) {
+			memcpy(args->out.digest, ctx->zero_digest,
+			       tsem_digestsize());
+			retn = 0;
+			goto done;
+		}
+	}
+
+	size = i_size_read(inode);
+	if (!size) {
+		memcpy(args->out.digest, ctx->zero_digest, tsem_digestsize());
+		goto done;
+	}
+
+	digest = find_digest(tsip);
+
+	if (digest && inode_eq_iversion(inode, digest->version) &&
+	    tsip->status == TSEM_INODE_COLLECTED) {
+		memcpy(args->out.digest, digest->value, tsem_digestsize());
+		goto done;
+	}
+
+	tsip->status = TSEM_INODE_COLLECTING;
+	retn = get_file_digest(file, inode, size, measurement);
+	if (retn) {
+		tsip->status = 0;
+		goto done;
+	}
+
+	if (!digest) {
+		digest = add_digest(ctx, tsip);
+		if (!digest) {
+			retn = -ENOMEM;
+			goto done;
+		}
+	}
+
+	memcpy(args->out.digest, measurement, tsem_digestsize());
+	memcpy(digest->value, measurement, tsem_digestsize());
+	digest->version = inode_query_iversion(inode);
+	tsip->status = TSEM_INODE_COLLECTED;
+
+ done:
+	mutex_unlock(&tsip->digest_mutex);
+	return retn;
+}
+
+static void fill_inode(struct inode *inode, struct tsem_inode_cell *ip)
+{
+	struct user_namespace *ns;
+
+	if (tsem_context(current)->use_current_ns)
+		ns = current_user_ns();
+	else
+		ns = &init_user_ns;
+
+	ip->uid = from_kuid(ns, inode->i_uid);
+	ip->gid = from_kgid(ns, inode->i_gid);
+	ip->mode = inode->i_mode;
+	ip->s_magic = inode->i_sb->s_magic;
+	memcpy(ip->s_id, inode->i_sb->s_id, sizeof(ip->s_id));
+	memcpy(ip->s_uuid, inode->i_sb->s_uuid.b, sizeof(ip->s_uuid));
+}
+
+static void fill_creds(const struct cred *cp, struct tsem_COE *tcp)
+{
+	struct user_namespace *ns;
+
+	if (tsem_context(current)->use_current_ns)
+		ns = current_user_ns();
+	else
+		ns = &init_user_ns;
+
+	tcp->uid = from_kuid(ns, cp->uid);
+	tcp->euid = from_kuid(ns, cp->euid);
+	tcp->suid = from_kuid(ns, cp->suid);
+
+	tcp->gid = from_kgid(ns, cp->gid);
+	tcp->egid = from_kgid(ns, cp->egid);
+	tcp->sgid = from_kgid(ns, cp->sgid);
+
+	tcp->fsuid = from_kuid(ns, cp->fsuid);
+	tcp->fsgid = from_kgid(ns, cp->fsgid);
+
+	tcp->capeff.mask = cp->cap_effective;
+
+	tcp->securebits = cp->securebits;
+}
+
+static int fill_dentry(struct dentry *dp, struct tsem_dentry *dentry)
+{
+	int retn;
+	struct tsem_inode *tsip;
+
+	retn = fill_path_dentry(dp, &dentry->path);
+	if (retn)
+		return retn;
+
+	if (d_backing_inode(dp)) {
+		dentry->have_inode = true;
+		fill_inode(d_backing_inode(dp), &dentry->inode);
+
+		tsip = tsem_inode(d_backing_inode(dp));
+		dentry->path.created = tsip->created;
+		dentry->path.creator = tsip->creator;
+		dentry->path.instance = tsip->instance;
+		memcpy(dentry->path.owner, tsem_task(current)->task_id,
+		       tsem_digestsize());
+	}
+
+	return 0;
+}
+
+static int fill_dentry_path(const struct path *path,
+			    struct tsem_dentry *dentry)
+{
+	int retn;
+	struct tsem_inode *tsip;
+
+	retn = fill_path(path, &dentry->path);
+	if (retn)
+		return retn;
+
+	if (d_backing_inode(path->dentry)) {
+		dentry->have_inode = true;
+		fill_inode(d_backing_inode(path->dentry), &dentry->inode);
+
+		tsip = tsem_inode(d_backing_inode(path->dentry));
+		dentry->path.created = tsip->created;
+		dentry->path.creator = tsip->creator;
+		dentry->path.instance = tsip->instance;
+		memcpy(dentry->path.owner, tsem_task(current)->task_id,
+		       tsem_digestsize());
+	}
+
+	return 0;
+}
+
+static u64 _get_task_inode_instance(struct tsem_inode *tsip)
+{
+	u64 instance = 0;
+	u8 *task_id = tsem_task(current)->task_id;
+	struct tsem_inode_instance *entry, *owner = NULL;
+
+	mutex_lock(&tsip->instance_mutex);
+	list_for_each_entry(entry, &tsip->instance_list, list) {
+		if (entry->creator == tsem_context(current)->id &&
+		    !memcmp(entry->owner, task_id, tsem_digestsize())) {
+			owner = entry;
+			break;
+		}
+	}
+
+	if (owner)
+		instance = ++owner->instance;
+	else {
+		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+		if (entry) {
+			instance = 1;
+			entry->instance = instance;
+			entry->creator = tsem_context(current)->id;
+			memcpy(entry->owner, task_id, tsem_digestsize());
+			list_add_tail(&entry->list, &tsip->instance_list);
+		}
+	}
+
+	mutex_unlock(&tsip->instance_mutex);
+	return instance;
+}
+
+static int get_inode(struct tsem_inode_args *args)
+{
+	int retn;
+	struct inode *dir = args->in.dir;
+	struct dentry *dentry = args->in.dentry;
+	struct tsem_inode *tsip = tsem_inode(d_backing_inode(dentry));
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		return retn;
+	args->out.dentry.path.created = tsip->created;
+	args->out.dentry.path.creator = tsip->creator;
+	args->out.dentry.path.instance = tsip->instance;
+	memcpy(args->out.dentry.path.owner, tsip->owner, tsem_digestsize());
+
+	fill_inode(dir, &args->out.dir);
+
+	return retn;
+}
+
+static int get_inode_create(struct tsem_inode_args *args)
+{
+	int retn;
+	u64 instance;
+	struct inode *dir = args->in.dir;
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	instance = _get_task_inode_instance(tsem_inode(dir));
+	if (!instance)
+		return -ENOMEM;
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		return retn;
+
+	args->out.dentry.path.created = true;
+	args->out.dentry.path.creator = tsem_context(current)->id;
+	args->out.dentry.path.instance = instance;
+	memcpy(args->out.dentry.path.owner, tsem_task(current)->task_id,
+	       tsem_digestsize());
+
+	fill_inode(dir, &args->out.dir);
+
+	retn = register_inode_create(dir, instance,
+				     args->out.dentry.path.pathname);
+	if (retn)
+		kfree(args->out.dentry.path.pathname);
+
+	return retn;
+}
+
+static int get_inode_link(struct tsem_inode_args *args)
+{
+	int retn;
+	struct inode *dir = args->in.dir;
+	struct dentry *dentry = args->in.dentry;
+	struct dentry *new_dentry = args->in.new_dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	retn = fill_dentry(new_dentry, &args->out.new_dentry);
+	if (retn)
+		goto done;
+
+	fill_inode(dir, &args->out.dir);
+
+ done:
+	if (retn) {
+		kfree(args->out.dentry.path.pathname);
+		kfree(args->out.new_dentry.path.pathname);
+	}
+
+	return retn;
+}
+
+static int get_inode_symlink(struct tsem_inode_args *args)
+{
+	int retn;
+	const char *old_name = args->in.old_name;
+	struct inode *dir = args->in.dir;
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		return retn;
+
+	fill_inode(dir, &args->out.dir);
+
+	args->out.old_name = kstrdup(old_name, GFP_KERNEL);
+	if (!args->out.old_name) {
+		kfree(args->out.dentry.path.pathname);
+		retn = -ENOMEM;
+	}
+
+	return retn;
+}
+
+static int get_inode_mknod(struct tsem_inode_args *args)
+{
+	int retn;
+	struct inode *dir = args->in.dir;
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		return retn;
+
+	fill_inode(dir, &args->out.dir);
+
+	return 0;
+}
+
+static int get_inode_rename(struct tsem_inode_args *args)
+{
+	int retn;
+	struct inode *old_dir = args->in.dir;
+	struct inode *new_dir = args->in.new_dir;
+	struct dentry *dentry = args->in.dentry;
+	struct dentry *new_dentry = args->in.new_dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		return retn;
+
+	retn = fill_dentry(new_dentry, &args->out.new_dentry);
+	if (retn) {
+		kfree(args->out.dentry.path.pathname);
+		return retn;
+	}
+
+	fill_inode(old_dir, &args->out.dir);
+	fill_inode(new_dir, &args->out.new_dir);
+
+	return 0;
+}
+
+static int get_inode_killpriv(struct tsem_inode_args *args)
+{
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	return fill_dentry(dentry, &args->out.dentry);
+}
+
+static int get_file_cell(struct tsem_file_args *args)
+{
+	int retn = 1;
+	bool pseudo_file = args->in.pseudo_file;
+	struct file *file = args->in.file;
+	struct inode *inode = file_inode(file);
+
+	memset(&args->out, '\0', sizeof(args->out));
+	inode_lock(inode);
+
+	retn = fill_path(&file->f_path, &args->out.path);
+	if (retn)
+		goto done;
+
+	args->out.flags = file->f_flags;
+	fill_inode(inode, &args->out.inode);
+
+	retn = add_file_digest(file, pseudo_file, args);
+	if (retn)
+		kfree(args->out.path.pathname);
+
+ done:
+	inode_unlock(inode);
+	return retn;
+}
+
+static void get_prlimit(struct tsem_task_prlimit_args *args)
+{
+	const struct cred *cred = args->in.cred;
+	const struct cred *tcred = args->in.tcred;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	fill_creds(cred, &args->out.cred);
+	fill_creds(tcred, &args->out.tcred);
+}
+
+static void get_prctl(struct tsem_task_prctl_args *args)
+{
+	if (args->option == PR_SET_NAME || args->option == PR_GET_NAME)
+		args->arg2 = 0;
+}
+
+static void get_socket(struct sock *sock, struct tsem_socket *args)
+{
+	args->family = sock->sk_family;
+	args->type = sock->sk_type;
+	args->protocol = sock->sk_protocol;
+	args->kern = sock->sk_kern_sock;
+	memcpy(args->owner, tsem_inode(SOCK_INODE(sock->sk_socket))->owner,
+	       tsem_digestsize());
+}
+
+static int get_socket_address(struct tsem_socket_args *args,
+			      struct sockaddr *addr)
+{
+	u8 *p;
+	int retn, size;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	p = (u8 *) addr->sa_data;
+	size = args->value - offsetof(struct sockaddr, sa_data);
+	retn = crypto_shash_digest(shash, p, size, args->out.mapping);
+
+ done:
+	return retn;
+}
+
+static int get_socket_cell(struct tsem_socket_args *args)
+
+{
+	int size, retn = 0;
+	struct sock *socka = args->in.socka;
+	struct sockaddr *addr = args->in.addr;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	get_socket(socka, &args->out.socka);
+
+	switch (args->out.socka.family) {
+	case AF_INET:
+		memcpy(&args->out.ipv4, addr, sizeof(args->out.ipv4));
+		break;
+	case AF_INET6:
+		memcpy(&args->out.ipv6, addr, sizeof(args->out.ipv6));
+		break;
+	case AF_UNIX:
+		memset(args->out.path, '\0', sizeof(args->out.path));
+		size = args->value - offsetof(struct sockaddr_un, sun_path);
+		strscpy(args->out.path, addr->sa_data, size);
+		break;
+	default:
+		retn = get_socket_address(args, addr);
+		break;
+	}
+
+	return retn;
+}
+
+static void get_socket_accept(struct tsem_socket_args *args)
+{
+	char *p;
+	int size;
+	const struct in6_addr *ipv6;
+	struct sock *socka = args->in.socka;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	get_socket(socka, &args->out.socka);
+
+	switch (args->out.socka.family) {
+	case AF_INET:
+		args->out.ipv4.sin_port = socka->sk_num;
+		args->out.ipv4.sin_addr.s_addr = socka->sk_rcv_saddr;
+		break;
+
+	case AF_INET6:
+		ipv6 = inet6_rcv_saddr(socka);
+		if (ipv6) {
+			args->out.ipv6.sin6_port = socka->sk_num;
+			args->out.ipv6.sin6_addr = *ipv6;
+		}
+		break;
+
+	case AF_UNIX:
+		memset(args->out.path, '\0', sizeof(args->out.path));
+		p = unix_sk(socka)->addr->name->sun_path;
+		size = unix_sk(socka)->addr->len -
+			offsetof(struct sockaddr_un, sun_path);
+		strscpy(args->out.path, p, size);
+		break;
+
+	default:
+		memcpy(args->out.mapping, tsem_context(current)->zero_digest,
+		       tsem_digestsize());
+		break;
+	}
+}
+
+static void get_socket_pair(struct tsem_socket_args *args)
+{
+	struct sock *socka = args->in.socka;
+	struct sock *sockb = args->in.sockb;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	get_socket(socka, &args->out.socka);
+	get_socket(sockb, &args->out.sockb);
+}
+
+static void get_socket_msg(struct tsem_socket_args *args)
+
+{
+	struct sock *socka = args->in.socka;
+	void *addr = args->in.addr;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	get_socket(socka, &args->out.socka);
+	if (addr) {
+		if (args->out.socka.family == AF_INET) {
+			args->out.have_addr = true;
+			args->out.ipv4 = *(struct sockaddr_in *) addr;
+		}
+		if (args->out.socka.family == AF_INET6) {
+			args->out.have_addr = true;
+			args->out.ipv6 = *(struct sockaddr_in6 *) addr;
+		}
+	}
+}
+
+static void get_socket_argument(struct tsem_socket_args *args)
+
+{
+	struct sock *socka = args->in.socka;
+
+	memset(&args->out, '\0', sizeof(args->out));
+	get_socket(socka, &args->out.socka);
+}
+
+static int get_inode_getattr(struct tsem_inode_attr_args *args)
+{
+	const struct path *path = args->in.path;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	return fill_dentry_path(path, &args->out.dentry);
+}
+
+static int get_inode_setattr(struct tsem_inode_attr_args *args)
+{
+	int retn;
+	struct user_namespace *ns;
+	struct dentry *dentry = args->in.dentry;
+	struct iattr *iattr = args->in.iattr;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	if (tsem_context(current)->use_current_ns)
+		ns = current_user_ns();
+	else
+		ns = &init_user_ns;
+
+	args->out.valid = iattr->ia_valid;
+	if (args->out.valid & ATTR_MODE)
+		args->out.mode = iattr->ia_mode;
+	if (args->out.valid & ATTR_UID)
+		args->out.uid = from_kuid(ns, iattr->ia_uid);
+	if (args->out.valid & ATTR_GID)
+		args->out.gid = from_kgid(ns, iattr->ia_gid);
+	if (args->out.valid & ATTR_SIZE)
+		args->out.size = iattr->ia_size;
+
+ done:
+	return retn;
+}
+
+static int get_inode_setxattr(struct tsem_inode_xattr_args *args)
+{
+	int retn;
+	const char *name = args->in.name;
+	struct dentry *dentry = args->in.dentry;
+	const void *value = args->in.value;
+	size_t size = args->in.size;
+	int flags = args->in.flags;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	args->out.size = size;
+	args->out.flags = flags;
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		return retn;
+
+	args->out.name = kstrdup(name, GFP_KERNEL);
+	if (!args->out.name) {
+		retn = -ENOMEM;
+		goto done;
+	}
+
+	args->out.value = kmalloc(size, GFP_KERNEL);
+	if (!args->out.value)
+		retn = -ENOMEM;
+	memcpy(args->out.value, value, size);
+
+	args->out.encoded_value = kzalloc(BASE64_CHARS(size) + 1, GFP_KERNEL);
+	if (!args->out.encoded_value) {
+		retn = -ENOMEM;
+		goto done;
+	}
+	base64_encode(args->out.value, size, args->out.encoded_value);
+
+ done:
+	if (retn) {
+		kfree(args->out.name);
+		kfree(args->out.value);
+		kfree(args->out.encoded_value);
+	}
+	return retn;
+}
+
+static int get_inode_getxattr(struct tsem_inode_xattr_args *args)
+{
+	int retn;
+	const char *name = args->in.name;
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_dentry(dentry, &args->out.dentry);
+	if (retn)
+		return retn;
+
+	args->out.name = kstrdup(name, GFP_KERNEL);
+	if (!args->out.name)
+		retn = -ENOMEM;
+
+	if (retn)
+		kfree(args->out.name);
+	return retn;
+}
+
+static int get_inode_listxattr(struct tsem_inode_xattr_args *args)
+{
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	return fill_dentry(dentry, &args->out.dentry);
+}
+
+static int get_kernel_module(struct tsem_kernel_args *args)
+{
+	int retn = 0;
+	char *kmod_name = args->in.kmod_name;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	args->out.kmod_name = kstrdup(kmod_name, GFP_KERNEL);
+	if (!args->out.kmod_name)
+		retn = -ENOMEM;
+
+	args->out.kmod_name = kstrdup(kmod_name, GFP_KERNEL);
+	if (!args->out.kmod_name)
+		retn = -ENOMEM;
+
+	return retn;
+}
+
+static int get_kernel_file(struct tsem_kernel_args *args)
+{
+	int retn = 0;
+	struct file *file = args->in.file;
+	struct inode *inode = file_inode(file);
+
+	memset(&args->out, '\0', sizeof(args->out));
+	inode_lock(inode);
+
+	retn = fill_path(&file->f_path, &args->out.file.out.path);
+	if (retn)
+		goto done;
+
+	args->out.file.out.flags = file->f_flags;
+	fill_inode(inode, &args->out.file.out.inode);
+
+	if (!S_ISREG(inode->i_mode))
+		goto done;
+
+	retn = add_file_digest(file, false, &args->out.file);
+	if (retn)
+		kfree(args->out.file.out.path.pathname);
+
+ done:
+	inode_unlock(inode);
+	return retn;
+}
+
+static int get_sb_mount(struct tsem_sb_args *args)
+{
+	int retn = -ENOMEM;
+	struct tsem_model *model = tsem_model(current);
+	struct tsem_inode_instance *tsio = NULL;
+	const char *dev_name = args->in.dev_name;
+	const char *type = args->in.type;
+	const struct path *path = args->in.path;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	if (dev_name) {
+		args->out.dev_name = kstrdup(dev_name, GFP_KERNEL);
+		if (!args->out.dev_name)
+			goto done;
+	}
+
+	if (type) {
+		args->out.type = kstrdup(type, GFP_KERNEL);
+		if (!args->out.type)
+			goto done;
+	}
+
+	retn = fill_path(path, &args->out.path);
+	if (retn)
+		goto done;
+
+	if (model && args->out.path.created) {
+		tsio = kzalloc(sizeof(*tsio), GFP_KERNEL);
+		if (!tsio) {
+			retn = -ENOMEM;
+			goto done;
+		}
+
+		tsio->creator = args->out.path.creator;
+		tsio->instance = args->out.path.instance;
+		tsio->pathname = args->out.path.pathname;
+		memcpy(tsio->owner, args->out.path.owner, tsem_digestsize());
+
+		mutex_lock(&model->mount_mutex);
+		list_add_tail(&tsio->list, &model->mount_list);
+		mutex_unlock(&model->mount_mutex);
+	}
+
+ done:
+	if (retn) {
+		kfree(args->out.dev_name);
+		kfree(args->out.type);
+		kfree(tsio);
+	}
+	return retn;
+}
+
+static int get_sb_umount(struct tsem_sb_args *args)
+{
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	return fill_dentry(dentry, &args->out.dentry);
+}
+
+static int get_sb_remount(struct tsem_sb_args *args)
+{
+	int retn = -ENOMEM;
+	struct super_block *sb = args->in.sb;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	args->flags = sb->s_flags;
+
+	args->out.type = kstrdup(sb->s_type->name, GFP_KERNEL);
+	if (!args->out.type)
+		goto done;
+
+	retn = fill_dentry(sb->s_root, &args->out.dentry);
+
+ done:
+	if (retn) {
+		kfree(args->out.type);
+		kfree(args->out.dentry.path.pathname);
+	}
+
+	return retn;
+}
+
+static void get_netlink(struct tsem_netlink_args *args)
+{
+	struct sock *sock = args->in.sock;
+	struct netlink_skb_parms *np = args->in.parms;
+	struct user_namespace *ns;
+
+	memset(&args->out, '\0', sizeof(args->out));
+	get_socket(sock, &args->out.sock);
+
+	if (tsem_context(current)->use_current_ns)
+		ns = current_user_ns();
+	else
+		ns = &init_user_ns;
+	args->out.uid = from_kuid(ns, np->creds.uid);
+	args->out.gid = from_kgid(ns, np->creds.gid);
+
+	args->out.portid = np->portid;
+	args->out.dst_group = np->dst_group;
+	args->out.flags = np->flags;
+	args->out.nsid_set = np->nsid_is_set;
+	args->out.nsid = np->nsid;
+}
+
+static void get_ipc_cred(struct kern_ipc_perm *perm, struct tsem_ipc_perm *tp)
+{
+	struct user_namespace *ns;
+
+	if (tsem_context(current)->use_current_ns)
+		ns = current_user_ns();
+	else
+		ns = &init_user_ns;
+
+	tp->uid = from_kuid(ns, perm->uid);
+	tp->gid = from_kgid(ns, perm->gid);
+	tp->cuid = from_kuid(ns, perm->cuid);
+	tp->cgid = from_kgid(ns, perm->cgid);
+	tp->mode = perm->mode;
+}
+
+static void get_ipc_permission(struct tsem_ipc_args *args)
+{
+	struct kern_ipc_perm *perm = args->in.perm;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	get_ipc_cred(perm, &args->out.perm);
+	memcpy(args->out.owner, tsem_ipc(perm)->owner, tsem_digestsize());
+}
+
+static void get_msg_queue_msgrcv(struct tsem_ipc_args *args)
+{
+	struct kern_ipc_perm *perm = args->in.perm;
+	struct tsem_task *target = tsem_task(args->in.target);
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	get_ipc_cred(perm, &args->out.perm);
+	memcpy(args->out.owner, tsem_ipc(perm)->owner, tsem_digestsize());
+	memcpy(args->out.target, target->task_id, tsem_digestsize());
+}
+
+static void get_key_alloc(struct tsem_key_args *args)
+{
+	const struct cred *cred = args->in.cred;
+
+	memset(&args->out, '\0', sizeof(args->out));
+	fill_creds(cred, &args->out.cred);
+}
+
+static void get_key_perm(struct tsem_key_args *args)
+{
+	const struct cred *cred = args->in.cred;
+	struct user_namespace *ns;
+	const key_ref_t ref = args->in.ref;
+	struct key *key = key_ref_to_ptr(ref);
+
+	memset(&args->out, '\0', sizeof(args->out));
+	fill_creds(cred, &args->out.cred);
+
+	args->out.possessed = is_key_possessed(ref);
+
+	if (tsem_context(current)->use_current_ns)
+		ns = current_user_ns();
+	else
+		ns = &init_user_ns;
+	args->out.uid = from_kuid(ns, key->uid);
+	args->out.gid = from_kgid(ns, key->gid);
+
+	args->out.perm = key->perm;
+	args->out.flags = key->flags;
+}
+
+static int get_sb_statfs(struct tsem_sb_args *args)
+{
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	return fill_dentry(dentry, &args->out.dentry);
+}
+
+static int get_move_mount(struct tsem_sb_args *args)
+{
+	int retn;
+	const struct path *old_path = args->in.path;
+	const struct path *new_path = args->in.path2;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	retn = fill_path(old_path, &args->out.path);
+	if (!retn)
+		retn = fill_path(new_path, &args->out.path2);
+
+	return retn;
+}
+
+static int get_quotactl(struct tsem_quota_args *args)
+{
+	int retn;
+	const struct super_block *sb = args->in.sb;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	args->out.s_flags = sb->s_flags;
+
+	args->out.fstype = kstrdup(sb->s_type->name, GFP_KERNEL);
+	if (!args->out.fstype)
+		goto done;
+
+	retn = fill_dentry(sb->s_root, &args->out.dentry);
+
+ done:
+	if (retn)
+		kfree(args->out.fstype);
+	return retn;
+}
+
+static int get_quotaon(struct tsem_quota_args *args)
+{
+	struct dentry *dentry = args->in.dentry;
+
+	memset(&args->out, '\0', sizeof(args->out));
+
+	return fill_dentry(dentry, &args->out.dentry);
+}
+
+static void event_free(struct tsem_event *ep)
+{
+	switch (ep->event) {
+	case TSEM_KERNEL_MODULE_REQUEST:
+		kfree(ep->CELL.kernel.out.kmod_name);
+		break;
+	case TSEM_KERNEL_READ_FILE:
+		kfree(ep->CELL.kernel.out.file.out.path.pathname);
+		break;
+	case TSEM_INODE_CREATE:
+	case TSEM_INODE_MKDIR:
+	case TSEM_INODE_RMDIR:
+	case TSEM_INODE_UNLINK:
+	case TSEM_INODE_MKNOD:
+	case TSEM_INODE_KILLPRIV:
+		kfree(ep->CELL.inode.out.dentry.path.pathname);
+		break;
+	case TSEM_INODE_LINK:
+		kfree(ep->CELL.inode.out.dentry.path.pathname);
+		kfree(ep->CELL.inode.out.new_dentry.path.pathname);
+		break;
+	case TSEM_INODE_SYMLINK:
+		kfree(ep->CELL.inode.out.old_name);
+		kfree(ep->CELL.inode.out.dentry.path.pathname);
+		break;
+	case TSEM_INODE_RENAME:
+		kfree(ep->CELL.inode.out.dentry.path.pathname);
+		kfree(ep->CELL.inode.out.new_dentry.path.pathname);
+		break;
+	case TSEM_FILE_OPEN:
+	case TSEM_BPRM_COMMITTED_CREDS:
+	case TSEM_FILE_IOCTL:
+	case TSEM_FILE_LOCK:
+	case TSEM_FILE_FCNTL:
+	case TSEM_FILE_RECEIVE:
+		kfree(ep->CELL.file.out.path.pathname);
+		break;
+	case TSEM_MMAP_FILE:
+		kfree(ep->CELL.mmap_file.file.out.path.pathname);
+		break;
+	case TSEM_INODE_GETATTR:
+	case TSEM_INODE_SETATTR:
+		kfree(ep->CELL.inode_attr.out.dentry.path.pathname);
+		break;
+	case TSEM_INODE_SETXATTR:
+	case TSEM_INODE_GETXATTR:
+	case TSEM_INODE_REMOVEXATTR:
+	case TSEM_INODE_LISTXATTR:
+		kfree(ep->CELL.inode_xattr.out.dentry.path.pathname);
+		kfree(ep->CELL.inode_xattr.out.name);
+		kfree(ep->CELL.inode_xattr.out.value);
+		kfree(ep->CELL.inode_xattr.out.encoded_value);
+		break;
+	case TSEM_SB_MOUNT:
+		kfree(ep->CELL.sb.out.dev_name);
+		kfree(ep->CELL.sb.out.type);
+		kfree(ep->CELL.sb.out.path.pathname);
+		break;
+	case TSEM_SB_UMOUNT:
+		kfree(ep->CELL.sb.out.dentry.path.pathname);
+		break;
+	case TSEM_SB_REMOUNT:
+		kfree(ep->CELL.sb.out.type);
+		kfree(ep->CELL.sb.out.dentry.path.pathname);
+		break;
+	case TSEM_SB_PIVOTROOT:
+	case TSEM_MOVE_MOUNT:
+		kfree(ep->CELL.sb.out.path.pathname);
+		kfree(ep->CELL.sb.out.path2.pathname);
+		break;
+	case TSEM_QUOTACTL:
+		kfree(ep->CELL.quota.out.fstype);
+		kfree(ep->CELL.quota.out.dentry.path.pathname);
+		break;
+	case TSEM_QUOTA_ON:
+		kfree(ep->CELL.quota.out.dentry.path.pathname);
+		break;
+	case TSEM_SB_STATFS:
+		kfree(ep->CELL.sb.out.dentry.path.pathname);
+		break;
+	default:
+		break;
+	}
+}
+
+static void get_COE(struct tsem_COE *COE)
+{
+	struct user_namespace *ns;
+
+	if (tsem_context(current)->use_current_ns)
+		ns = current_user_ns();
+	else
+		ns = &init_user_ns;
+
+	COE->uid = from_kuid(ns, current_uid());
+	COE->euid = from_kuid(ns, current_euid());
+	COE->suid = from_kuid(ns, current_suid());
+
+	COE->gid = from_kgid(ns, current_gid());
+	COE->egid = from_kgid(ns, current_egid());
+	COE->sgid = from_kgid(ns, current_sgid());
+
+	COE->fsuid = from_kuid(ns, current_fsuid());
+	COE->fsgid = from_kgid(ns, current_fsgid());
+
+	COE->capeff.mask = current_cred()->cap_effective;
+}
+
+/**
+ * tsem_event_generate() - Generate the default event description.
+ * @ep: A pointer to the structure that describes the event parameters.
+ *
+ * This function is responsible for initializing the data that describes
+ * the security event and is persisted for the lifetime that the event
+ * description is maintained.
+ *
+ * Return: In the event of an error this function returns an error code
+ *	   as a negative return value.  A value of zero indicates that
+ *	   the event should be bypassed.  A positive value indicates
+ *	   the event should be modeled.
+ */
+
+int tsem_event_generate(struct tsem_event *ep)
+{
+	int retn = 0;
+
+	switch (ep->event) {
+	case TSEM_NETLINK_SEND:
+		get_netlink(&ep->CELL.netlink);
+		break;
+	case TSEM_IPC_PERMISSION:
+	case TSEM_SHM_ASSOCIATE:
+	case TSEM_SHM_SHMCTL:
+	case TSEM_SHM_SHMAT:
+	case TSEM_SEM_ASSOCIATE:
+	case TSEM_SEM_SEMCTL:
+	case TSEM_SEM_SEMOP:
+	case TSEM_MSG_QUEUE_ASSOCIATE:
+	case TSEM_MSG_QUEUE_MSGCTL:
+	case TSEM_MSG_QUEUE_MSGSND:
+		get_ipc_permission(&ep->CELL.ipc);
+		break;
+	case TSEM_MSG_QUEUE_MSGRCV:
+		get_msg_queue_msgrcv(&ep->CELL.ipc);
+		break;
+	case TSEM_KEY_ALLOC:
+		get_key_alloc(&ep->CELL.key);
+		break;
+	case TSEM_KEY_PERMISSION:
+		get_key_perm(&ep->CELL.key);
+		break;
+	case TSEM_INODE_RMDIR:
+	case TSEM_INODE_UNLINK:
+		retn = get_inode(&ep->CELL.inode);
+		break;
+	case TSEM_INODE_CREATE:
+	case TSEM_INODE_MKDIR:
+		retn = get_inode_create(&ep->CELL.inode);
+		break;
+	case TSEM_INODE_LINK:
+		retn = get_inode_link(&ep->CELL.inode);
+		break;
+	case TSEM_INODE_SYMLINK:
+		retn = get_inode_symlink(&ep->CELL.inode);
+		break;
+	case TSEM_INODE_MKNOD:
+		retn = get_inode_mknod(&ep->CELL.inode);
+		break;
+	case TSEM_INODE_RENAME:
+		retn = get_inode_rename(&ep->CELL.inode);
+		break;
+	case TSEM_INODE_KILLPRIV:
+		retn = get_inode_killpriv(&ep->CELL.inode);
+		break;
+	case TSEM_FILE_OPEN:
+	case TSEM_BPRM_COMMITTED_CREDS:
+	case TSEM_FILE_IOCTL:
+	case TSEM_FILE_LOCK:
+	case TSEM_FILE_FCNTL:
+	case TSEM_FILE_RECEIVE:
+		retn = get_file_cell(&ep->CELL.file);
+		break;
+	case TSEM_MMAP_FILE:
+		if (!ep->CELL.mmap_file.anonymous)
+			retn = get_file_cell(&ep->CELL.mmap_file.file);
+		break;
+	case TSEM_TASK_PRLIMIT:
+		get_prlimit(&ep->CELL.task_prlimit);
+		break;
+	case TSEM_TASK_PRCTL:
+		get_prctl(&ep->CELL.task_prctl);
+		break;
+	case TSEM_UNIX_STREAM_CONNECT:
+	case TSEM_UNIX_MAY_SEND:
+	case TSEM_SOCKET_SOCKETPAIR:
+		get_socket_pair(&ep->CELL.socket);
+		break;
+	case TSEM_SOCKET_CONNECT:
+	case TSEM_SOCKET_BIND:
+		retn = get_socket_cell(&ep->CELL.socket);
+		break;
+	case TSEM_SOCKET_ACCEPT:
+		get_socket_accept(&ep->CELL.socket);
+		break;
+	case TSEM_SOCKET_LISTEN:
+	case TSEM_SOCKET_GETSOCKNAME:
+	case TSEM_SOCKET_GETPEERNAME:
+	case TSEM_SOCKET_SETSOCKOPT:
+	case TSEM_SOCKET_SHUTDOWN:
+	case TSEM_TUN_DEV_ATTACH_QUEUE:
+		get_socket_argument(&ep->CELL.socket);
+		break;
+	case TSEM_SOCKET_SENDMSG:
+	case TSEM_SOCKET_RECVMSG:
+		get_socket_msg(&ep->CELL.socket);
+		break;
+	case TSEM_INODE_GETATTR:
+		retn = get_inode_getattr(&ep->CELL.inode_attr);
+		break;
+	case TSEM_INODE_SETATTR:
+		retn = get_inode_setattr(&ep->CELL.inode_attr);
+		break;
+	case TSEM_INODE_SETXATTR:
+		retn = get_inode_setxattr(&ep->CELL.inode_xattr);
+		break;
+	case TSEM_INODE_GETXATTR:
+	case TSEM_INODE_REMOVEXATTR:
+		retn = get_inode_getxattr(&ep->CELL.inode_xattr);
+		break;
+	case TSEM_INODE_LISTXATTR:
+		retn = get_inode_listxattr(&ep->CELL.inode_xattr);
+		break;
+	case TSEM_KERNEL_MODULE_REQUEST:
+		retn = get_kernel_module(&ep->CELL.kernel);
+		break;
+	case TSEM_KERNEL_READ_FILE:
+		retn = get_kernel_file(&ep->CELL.kernel);
+		break;
+	case TSEM_SB_MOUNT:
+		retn = get_sb_mount(&ep->CELL.sb);
+		break;
+	case TSEM_SB_UMOUNT:
+		retn = get_sb_umount(&ep->CELL.sb);
+		break;
+	case TSEM_SB_REMOUNT:
+		retn = get_sb_remount(&ep->CELL.sb);
+		break;
+	case TSEM_SB_STATFS:
+		retn = get_sb_statfs(&ep->CELL.sb);
+		break;
+	case TSEM_SB_PIVOTROOT:
+	case TSEM_MOVE_MOUNT:
+		retn = get_move_mount(&ep->CELL.sb);
+		break;
+	case TSEM_QUOTACTL:
+		retn = get_quotactl(&ep->CELL.quota);
+		break;
+	case TSEM_QUOTA_ON:
+		retn = get_quotaon(&ep->CELL.quota);
+		break;
+	default:
+		break;
+	}
+
+	if (!retn) {
+		ep->event_free = event_free;
+		return 1;
+	}
+	return retn;
+}
+EXPORT_SYMBOL_GPL(tsem_event_generate);
+
+/**
+ * tsem_event_init() - Initialize a security event description structure.
+ * @ep: A pointer to the tsem_event structure that describes the
+ *	security event.
+ *
+ * This function is responsible for initializing the tsem_event structure
+ * and populating it based on the event type.
+ *
+ * Return: In the event of an error this function returns an error code
+ *	   as a negative return value.  A value of zero indicates that
+ *	   the event should be bypassed.  A positive value indicates
+ *	   the event should be modeled.
+ */
+int tsem_event_init(struct tsem_event *ep)
+{
+	int retn = 1;
+	u64 timestamp = ktime_get_boottime_ns();
+	struct tsem_task *task = tsem_task(current);
+
+	ep->pid = task_pid_nr(current);
+	ep->context = tsem_context(current)->id;
+	ep->instance = task->instance;
+	ep->p_instance = task->p_instance;
+	ep->timestamp = timestamp - tsem_context(current)->timestamp;
+	memcpy(ep->comm, current->comm, sizeof(ep->comm));
+	memcpy(ep->task_id, task->task_id, tsem_digestsize());
+	memcpy(ep->p_task_id, task->p_task_id, tsem_digestsize());
+
+	get_COE(&ep->COE);
+
+	if (ep->event == TSEM_BPRM_COMMITTED_CREDS)
+		return tsem_event_generate(ep);
+
+	if (!ep->no_params)
+		retn = tsem_context(current)->ops->init(ep);
+
+	if (retn > 0)
+		ep->event_number = ++tsem_context(current)->event_number;
+	return retn;
+}
+
+/**
+ * tsem_free_event() - Free a security event description.
+ * @ep: A pointer to the security event description that is to be freed.
+ *
+ * This function is responsible for freeing the resources that were
+ * allocated by the tsem_event_allocate() function.
+ */
+static void tsem_event_free(struct kref *kref)
+{
+	struct tsem_event *ep;
+
+	ep = container_of(kref, struct tsem_event, kref);
+
+	if (ep->event_free)
+		ep->event_free(ep);
+	kmem_cache_free(event_cachep, ep);
+}
+
+/**
+ * tsem_event_put() - Release a referenceto a TSEM event description.
+ *
+ * This function is called each time the use of a TSEM event description
+ * is dropped.
+ */
+void tsem_event_put(struct tsem_event *ep)
+{
+	kref_put(&ep->kref, tsem_event_free);
+}
+
+static void refill_event_magazine(struct work_struct *work)
+{
+	unsigned int index;
+	struct tsem_event *ep;
+	struct tsem_work *ws;
+
+	ws = container_of(work, struct tsem_work, work);
+
+	ep = kmem_cache_zalloc(event_cachep, GFP_KERNEL);
+	if (!ep) {
+		pr_warn("tsem: Cannot refill event magazine.\n");
+		return;
+	}
+
+	spin_lock(&ws->u.ctx->magazine_lock);
+	ws->u.ctx->magazine[ws->index] = ep;
+	clear_bit(ws->index, ws->u.ctx->magazine_index);
+
+	/*
+	 * The following memory barrier is used to cause the magazine
+	 * index to be visible after the refill of the cache slot.
+	 */
+	smp_mb__after_atomic();
+	spin_unlock(&ws->u.ctx->magazine_lock);
+
+	if (index >= ws->u.ctx->magazine_size) {
+		kmem_cache_free(event_cachep, ep);
+		WARN_ONCE(true, "Refilling event magazine with no slots.\n");
+	}
+}
+
+/**
+ * tsem_event_get() - Obtain a reference to a TSEM event description.
+ *
+ * This function is called on each invocation of the tsem_task_free
+ * function to release one of the references on the TMA modeling
+ * structure.
+ */
+void tsem_event_get(struct tsem_event *ep)
+{
+	kref_get(&ep->kref);
+}
+
+/**
+ * tsem_event_allocate() - Allocate a TSEM event description structure.
+ * @locked: A boolean flag used to indicate if the allocation is being
+ *	    done in atomic context and must be serviced from the
+ *	    pre-allocated event description structures.
+ *
+ * Return: This function returns a pointer to the allocated structure or
+ *	   a NULL pointer in the event of an allocation failure.
+ */
+struct tsem_event *tsem_event_allocate(enum tsem_event_type event, bool locked)
+{
+	unsigned int index;
+	struct tsem_event *ep = NULL;
+	struct tsem_context *ctx = tsem_context(current);
+
+	if (!locked) {
+		ep = kmem_cache_zalloc(event_cachep, GFP_KERNEL);
+		if (ep) {
+			ep->event = event;
+			kref_init(&ep->kref);
+		}
+		return ep;
+	}
+
+	spin_lock(&ctx->magazine_lock);
+	index = find_first_zero_bit(ctx->magazine_index, ctx->magazine_size);
+	if (index < ctx->magazine_size) {
+		ep = ctx->magazine[index];
+		ctx->ws[index].index = index;
+		ctx->ws[index].u.ctx = ctx;
+		set_bit(index, ctx->magazine_index);
+
+		/*
+		 * Similar to the issue noted in the refill_event_magazine()
+		 * function, this barrier is used to cause the consumption
+		 * of the cache entry to become visible.
+
+		 */
+		smp_mb__after_atomic();
+	}
+
+	spin_unlock(&ctx->magazine_lock);
+
+	if (ep) {
+		INIT_WORK(&ctx->ws[index].work, refill_event_magazine);
+		queue_work(system_highpri_wq, &ctx->ws[index].work);
+		ep->event = event;
+		ep->locked = true;
+		kref_init(&ep->kref);
+		return ep;
+	}
+
+	pr_warn("tsem: Fail event allocation comm %s ns %llu cs %u.\n",
+		current->comm, tsem_context(current)->id, ctx->magazine_size);
+	return NULL;
+}
+
+/**
+ * tsem event_magazine_allocate() - Allocate a TSEM event magazine.
+ * @ctx: A pointer to the modeling context that the magazine is
+ *	 to be allocated for.
+ * @size: The number of entries to be created in the magazine.
+
+ * The security modeling event magazine is an array of tsem_event
+ * structures that are used to service security hooks that are called
+ * in atomic context.  Each modeling domain/namespace has a magazine
+ * allocated to it and this function allocates and initializes the
+ * memory structures needed to manage that magazine.
+
+ * Return: This function returns a value of zero on success and a negative
+ *	   error code on failure.
+ */
+int tsem_event_magazine_allocate(struct tsem_context *ctx, size_t size)
+{
+	unsigned int lp;
+	int retn = -ENOMEM;
+
+	ctx->magazine_size = size;
+
+	spin_lock_init(&ctx->magazine_lock);
+
+	ctx->magazine_index = bitmap_zalloc(ctx->magazine_size, GFP_KERNEL);
+	if (!ctx->magazine_index)
+		return retn;
+
+	ctx->magazine = kcalloc(ctx->magazine_size, sizeof(*ctx->magazine),
+				GFP_KERNEL);
+	if (!ctx->magazine)
+		goto done;
+
+	for (lp = 0; lp < ctx->magazine_size; ++lp) {
+		ctx->magazine[lp] = kmem_cache_zalloc(event_cachep,
+						      GFP_KERNEL);
+		if (!ctx->magazine[lp])
+			goto done;
+	}
+
+	ctx->ws = kcalloc(ctx->magazine_size, sizeof(*ctx->ws), GFP_KERNEL);
+	if (ctx->ws)
+		retn = 0;
+
+ done:
+	if (retn)
+		tsem_event_magazine_free(ctx);
+
+	return retn;
+}
+
+/**
+ * tsem event_magazine_free() - Releases a TSEM event magazine.
+ * @ctx: A pointer to the modeling context whose magazine is to be
+ *	 released.
+ *
+ * The function is used to free the memory that was allocated by
+ * the tsem_event_magazine_allocate() function for a security
+ * modeling context.
+ */
+void tsem_event_magazine_free(struct tsem_context *ctx)
+{
+	unsigned int lp;
+
+	for (lp = 0; lp < ctx->magazine_size; ++lp)
+		kmem_cache_free(event_cachep, ctx->magazine[lp]);
+
+	bitmap_free(ctx->magazine_index);
+	kfree(ctx->ws);
+	kfree(ctx->magazine);
+}
+
+/**
+ * tsem event_cache_init() - Initialize the TSEM event cache.
+ *
+ * This function is called by the TSEM initialization function and sets
+ * up the cache that will be used to allocate tsem_event structures.
+ *
+ * Return: This function returns a value of zero on success and a negative
+ *	   error code on failure.
+ */
+int __init tsem_event_cache_init(void)
+{
+	event_cachep = kmem_cache_create("tsem_event_cache",
+					 sizeof(struct tsem_event), 0,
+					 SLAB_PANIC, 0);
+	if (!event_cachep)
+		return -ENOMEM;
+
+	return 0;
+}
Signature · 1 line
2.39.1
#36 lore raw

PATCH v4 10/14 Implement security event mapping.

In reply to Greg Wettstein

The map.c file is responsible for implenting the description of a security event into a security state coefficient. The following documentation file, provided with the TSEM implementation, contains a description of the generative functions used to create the coefficients.

Documentation/admin-guide/LSM/tsem.rst

The mapping process takes a security event description, that was packaged by the event.c file, and uses that description to drive the coefficient generation process.

How the parameters are mapped into the coefficients is responsible for implementing the security model enforced by the internal Trusted Modeling Agent implementation.

---
 security/tsem/map.c | 1536 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 1536 insertions(+)
 create mode 100644 security/tsem/map.c
security/tsem/map.c new file +1536 -0
new file mode 100644
index 000000000000..6a72b9567b99
@@ -0,0 +1,1536 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+ *
+ * This file is responsible for mapping the characteristics of
+ * security events into a security state coefficient for the internal
+ * trusted modeling agent implementation.  The primary TSEM
+ * documentation describes the generative functions that are used to
+ * conduct this mapping.
+ *
+ * The tsem_map_event() function is called by the tsem_model_event()
+ * to generate the security state coefficients for the internal
+ * modeling implementation.
+ *
+ * The other major role of this file is to provide the tsem_map_task()
+ * function that is used to generate the TASK_ID for a process.  This
+ * function is called from the tsem_bprm_committed_creds() function.
+ */
+
+#include <linux/magic.h>
+#include <uapi/linux/prctl.h>
+
+#include "tsem.h"
+
+static int add_u16(struct shash_desc *shash, u16 value)
+{
+	return crypto_shash_update(shash, (char *) &value, sizeof(value));
+}
+
+static int add_u32(struct shash_desc *shash, u32 value)
+{
+	return crypto_shash_update(shash, (char *) &value, sizeof(value));
+}
+
+static int add_u64(struct shash_desc *shash, u64 value)
+{
+	return crypto_shash_update(shash, (char *) &value, sizeof(value));
+}
+
+static int add_str(struct shash_desc *shash, char *str)
+{
+	u32 value;
+	u8 *p;
+	int retn;
+	int size;
+
+	p = (u8 *) &value;
+	value = strlen(str);
+	size = sizeof(value);
+	retn = crypto_shash_update(shash, p, size);
+	if (retn)
+		goto done;
+
+	p = (u8 *) str;
+	size = strlen(str);
+	retn = crypto_shash_update(shash, p, size);
+
+ done:
+	return retn;
+}
+
+static int get_COE_mapping(struct tsem_event *ep, u8 *mapping)
+{
+	int retn = 0;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.uid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.euid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.suid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.gid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.egid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.sgid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.fsuid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, ep->COE.fsgid);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, ep->COE.capeff.value);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_final(shash, mapping);
+
+ done:
+	return retn;
+}
+
+static int add_temp_path(struct shash_desc *shash, char *pathname,
+			 u64 instance, u8 *owner)
+{
+	char *p, ch = '\0';
+	int retn;
+
+	p = strrchr(pathname, '/');
+	if (!p)
+		return -EINVAL;
+
+	++p;
+	ch = *p;
+	if (ch)
+		*p = '\0';
+	retn = add_str(shash, pathname);
+	if (ch)
+		*p = ch;
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, instance);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, owner, tsem_digestsize());
+
+ done:
+	return retn;
+}
+
+static int add_path(struct shash_desc *shash, struct tsem_path *path)
+{
+	int retn;
+
+	if (path->dev) {
+		retn = add_u32(shash, MAJOR(path->dev));
+		if (retn)
+			goto done;
+		retn = add_u32(shash, MINOR(path->dev));
+		if (retn)
+			goto done;
+	}
+
+	if (path->created) {
+		retn = add_temp_path(shash, path->pathname, path->instance,
+				     path->owner);
+	} else
+		retn = add_str(shash, path->pathname);
+
+ done:
+	return retn;
+}
+
+static int add_inode(struct shash_desc *shash, struct tsem_inode_cell *inode)
+{
+	u32 value;
+	u8 *p = (u8 *) &value;
+	int retn;
+	int size = sizeof(value);
+
+	value = inode->uid;
+	retn = crypto_shash_update(shash, p, size);
+	if (retn)
+		goto done;
+
+	value = inode->gid;
+	retn = crypto_shash_update(shash, p, size);
+	if (retn)
+		goto done;
+
+	value = inode->mode;
+	retn = crypto_shash_update(shash, p, size);
+	if (retn)
+		goto done;
+
+	value = inode->s_magic;
+	retn = crypto_shash_update(shash, p, size);
+	if (retn)
+		goto done;
+
+	p = (u8 *) inode->s_id;
+	size = sizeof(inode->s_id);
+	retn = crypto_shash_update(shash, p, size);
+	if (retn)
+		goto done;
+
+	if (inode->s_magic == TMPFS_MAGIC)
+		p = (u8 *) uuid_null.b;
+	else
+		p = (u8 *) inode->s_uuid;
+	size = sizeof(inode->s_uuid);
+	retn = crypto_shash_update(shash, p, size);
+
+ done:
+	return retn;
+}
+
+static int add_dentry(struct shash_desc *shash, struct tsem_dentry *dentry)
+{
+	int retn;
+
+	if (dentry->have_inode)
+		retn = add_inode(shash, &dentry->inode);
+
+	if (dentry->path.dev) {
+		retn = add_u32(shash, MAJOR(dentry->path.dev));
+		if (retn)
+			goto done;
+		retn = add_u32(shash, MINOR(dentry->path.dev));
+		if (retn)
+			goto done;
+	}
+
+	if (dentry->path.created &&
+	    dentry->path.creator == tsem_context(current)->id) {
+		retn = add_temp_path(shash, dentry->path.pathname,
+				     dentry->path.instance,
+				     dentry->path.owner);
+	} else
+		retn = add_str(shash, dentry->path.pathname);
+
+ done:
+	return retn;
+}
+
+static int add_file(struct shash_desc *shash, struct tsem_file_args *args)
+{
+	int retn;
+
+	retn = add_u32(shash, args->out.flags);
+	if (retn)
+		goto done;
+
+	retn = add_inode(shash, &args->out.inode);
+	if (retn)
+		goto done;
+
+	retn = add_path(shash, &args->out.path);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, args->out.digest, tsem_digestsize());
+
+ done:
+	return retn;
+}
+
+static int add_creds(struct shash_desc *shash, struct tsem_COE *cp)
+{
+	int retn;
+
+	retn = add_u32(shash, cp->uid);
+	if (!retn)
+		goto done;
+
+	retn = add_u32(shash, cp->euid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, cp->suid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, cp->gid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, cp->egid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, cp->sgid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, cp->fsuid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, cp->fsgid);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, cp->capeff.value);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, cp->securebits);
+
+ done:
+	return retn;
+}
+
+static int add_socket(struct shash_desc *shash, struct tsem_socket *args)
+{
+	int retn;
+
+	retn = add_u32(shash, args->family);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->type);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->protocol);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, args->owner, sizeof(args->owner));
+
+ done:
+	return retn;
+}
+
+static int add_task(struct shash_desc *shash, u8 *task_id)
+{
+	return crypto_shash_update(shash, task_id, tsem_digestsize());
+}
+
+static int add_ipc_cred(struct shash_desc *shash, struct tsem_ipc_args *args)
+{
+	int retn;
+
+	retn = add_u32(shash, args->out.perm.uid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.perm.gid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.perm.cuid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.perm.cgid);
+	if (retn)
+		goto done;
+
+	retn = add_u16(shash, args->out.perm.mode);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, args->out.owner, tsem_digestsize());
+
+ done:
+	return retn;
+}
+
+static int add_socket_connect_bind(struct shash_desc *shash,
+				   struct tsem_event *ep)
+{
+	int retn;
+	char *p;
+	size_t size;
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	retn = add_socket(shash, &args->out.socka);
+	if (retn)
+		goto done;
+
+	switch (args->out.socka.family) {
+	case AF_INET:
+		retn = add_u16(shash, args->out.ipv4.sin_port);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, args->out.ipv4.sin_addr.s_addr);
+		break;
+
+	case AF_INET6:
+		retn = add_u16(shash, args->out.ipv6.sin6_port);
+		if (retn)
+			goto done;
+
+		p = (u8 *) args->out.ipv6.sin6_addr.in6_u.u6_addr8;
+		size = sizeof(args->out.ipv6.sin6_addr.in6_u.u6_addr8);
+		retn = crypto_shash_update(shash, p, size);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, args->out.ipv6.sin6_flowinfo);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, args->out.ipv6.sin6_scope_id);
+		break;
+
+	case AF_UNIX:
+		p = args->out.path;
+		size = strlen(args->out.path);
+		retn = crypto_shash_update(shash, p, size);
+		break;
+
+	default:
+		p = (u8 *) args->out.mapping;
+		size = tsem_digestsize();
+		retn = crypto_shash_update(shash, p, size);
+		break;
+	}
+
+ done:
+	return retn;
+}
+
+static int add_socket_accept(struct shash_desc *shash, struct tsem_event *ep)
+{
+	char *p;
+	int retn, size;
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	retn = add_socket(shash, &args->out.socka);
+	if (retn)
+		goto done;
+
+	switch (args->out.socka.family) {
+	case AF_INET:
+		retn = add_u16(shash, args->out.ipv4.sin_port);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, args->out.ipv4.sin_addr.s_addr);
+		break;
+
+	case AF_INET6:
+		retn = add_u16(shash, args->out.ipv6.sin6_port);
+		if (retn)
+			goto done;
+
+		p = (u8 *) args->out.ipv6.sin6_addr.in6_u.u6_addr8;
+		size = sizeof(args->out.ipv6.sin6_addr.in6_u.u6_addr8);
+		retn = crypto_shash_update(shash, p, size);
+		break;
+
+	case AF_UNIX:
+		p = args->out.path;
+		size = strlen(args->out.path);
+		retn = crypto_shash_update(shash, p, size);
+		break;
+
+	default:
+		p = args->out.mapping;
+		size = tsem_digestsize();
+		retn = crypto_shash_update(shash, p, size);
+		break;
+	}
+
+ done:
+	return retn;
+}
+
+static int add_socket_msg(struct shash_desc *shash, struct tsem_event *ep)
+{
+	char *p;
+	int size, retn;
+	struct sockaddr_in *ipv4;
+	struct sockaddr_in6 *ipv6;
+	struct tsem_socket_args *args = &ep->CELL.socket;
+
+	retn = add_socket(shash, &args->out.socka);
+	if (retn)
+		goto done;
+
+	if (!args->out.have_addr)
+		goto done;
+
+	if (args->out.socka.family == AF_INET) {
+		ipv4 = &args->out.ipv4;
+		retn = add_u16(shash, ipv4->sin_port);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ipv4->sin_addr.s_addr);
+		if (retn)
+			goto done;
+	}
+	if (args->out.socka.family == AF_INET6) {
+		ipv6 = &args->out.ipv6;
+		retn = add_u16(shash, ipv6->sin6_port);
+		if (retn)
+			goto done;
+
+		p = (u8 *) &ipv6->sin6_addr.in6_u.u6_addr8;
+		size = sizeof(ipv6->sin6_addr.in6_u.u6_addr8);
+		retn = crypto_shash_update(shash, p, size);
+		if (retn)
+			goto done;
+	}
+
+ done:
+	return retn;
+}
+
+static int add_xattr(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_inode_xattr_args *args = &ep->CELL.inode_xattr;
+
+	retn = add_dentry(shash, &args->out.dentry);
+	if (retn)
+		return retn;
+
+	if (ep->event == TSEM_INODE_LISTXATTR)
+		return 0;
+
+	retn = add_str(shash, args->out.name);
+	if (retn)
+		return retn;
+
+	if (ep->event == TSEM_INODE_GETXATTR ||
+	    ep->event == TSEM_INODE_REMOVEXATTR)
+		return 0;
+
+	retn = crypto_shash_update(shash, args->out.value, args->out.size);
+	if (retn)
+		return retn;
+
+	return add_u32(shash, args->out.flags);
+}
+
+static int add_inode_rename(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	retn = add_inode(shash, &args->out.dir);
+	if (retn)
+		goto done;
+
+	retn = add_dentry(shash, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	retn = add_inode(shash, &args->out.new_dir);
+	if (retn)
+		goto done;
+
+	retn = add_dentry(shash, &args->out.new_dentry);
+
+ done:
+	return retn;
+
+}
+
+static int add_inode_create(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn = 0;
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	retn = add_inode(shash, &args->out.dir);
+	if (retn)
+		goto done;
+
+	retn = add_dentry(shash, &ep->CELL.inode.out.dentry);
+	if (retn)
+		goto done;
+	retn = add_u16(shash, ep->CELL.inode.mode);
+
+ done:
+	return retn;
+}
+
+static int add_sb_umount(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	retn = add_dentry(shash, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->flags);
+
+ done:
+	return retn;
+}
+
+static int add_sb_remount(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	retn = add_dentry(shash, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	retn = add_str(shash, ep->CELL.sb.out.type);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, ep->CELL.sb.flags);
+
+ done:
+	return retn;
+}
+
+static int add_inode_link(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_inode_args *args = &ep->CELL.inode;
+
+	retn = add_dentry(shash, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	retn = add_inode(shash, &args->out.dir);
+	if (retn)
+		goto done;
+
+	retn = add_dentry(shash, &args->out.new_dentry);
+
+ done:
+	return retn;
+}
+
+static int add_quotactl(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_quota_args *args = &ep->CELL.quota;
+
+	retn = add_u32(shash, args->cmds);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->type);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->id);
+	if (retn)
+		goto done;
+
+	retn = add_dentry(shash, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	retn = add_str(shash, args->out.fstype);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->out.s_flags);
+
+ done:
+	return retn;
+}
+
+static int add_mmap_file(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_mmap_file_args *args = &ep->CELL.mmap_file;
+
+	if (!args->anonymous) {
+		retn = add_file(shash, &args->file);
+		if (retn)
+			goto done;
+	}
+
+	retn = add_u32(shash, args->prot);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->flags);
+
+ done:
+	return retn;
+}
+
+static int add_task_setrlimit(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	retn = add_task(shash, args->target);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->u.resource);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->cur);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->max);
+
+ done:
+	return retn;
+}
+
+static int add_move_path(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_sb_args *args = &ep->CELL.sb;
+
+	retn = add_path(shash, &args->out.path);
+	if (retn)
+		goto done;
+
+	add_path(shash, &args->out.path2);
+
+ done:
+	return retn;
+}
+
+static int add_task_kill(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	retn = add_task(shash, args->target);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->signal);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->cross_model);
+
+ done:
+	return retn;
+}
+
+static int add_ptrace_access_check(struct shash_desc *shash,
+				   struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_task_kill_args *args = &ep->CELL.task_kill;
+
+	retn = add_task(shash, args->target);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->u.resource);
+
+ done:
+	return retn;
+}
+
+static int add_capget(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_capability_args *args = &ep->CELL.capability;
+
+	retn = add_task(shash, args->target);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->effective.val);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->inheritable.val);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->permitted.val);
+
+ done:
+	return retn;
+}
+
+static int add_capset(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_capability_args *args = &ep->CELL.capability;
+
+	retn = add_u64(shash, args->effective.val);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->inheritable.val);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->permitted.val);
+
+ done:
+	return retn;
+}
+
+static int add_capable(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_capability_args *args = &ep->CELL.capability;
+
+	retn = add_u32(shash, args->cap);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->opts);
+
+ done:
+	return retn;
+}
+
+static int add_key_permission(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_key_args *args = &ep->CELL.key;
+
+	retn = add_u32(shash, args->out.possessed);
+	if (retn)
+		goto done;
+
+	retn = add_u16(shash, args->out.uid);
+	if (retn)
+		goto done;
+
+	retn = add_u16(shash, args->out.gid);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->out.flags);
+	if (retn)
+		goto done;
+
+	retn = add_creds(shash, &args->out.cred);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.perm);
+
+ done:
+	return retn;
+}
+
+static int add_inode_setattr(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_inode_attr_args *args = &ep->CELL.inode_attr;
+
+	retn = add_dentry(shash, &args->out.dentry);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.valid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.mode);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.uid);
+	if (retn)
+		goto done;
+
+	retn = add_u32(shash, args->out.gid);
+	if (retn)
+		goto done;
+
+	retn = add_u64(shash, args->out.size);
+
+ done:
+	return retn;
+}
+
+static int add_bpf(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_bpf_args *args = &ep->CELL.bpf;
+
+	retn = add_u32(shash, args->bpf.cmd);
+	if (!retn)
+		goto done;
+
+	retn = add_u32(shash, args->bpf.size);
+
+ done:
+	return retn;
+}
+
+static int add_bpf_map(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_bpf_args *args = &ep->CELL.bpf;
+
+	retn = add_u32(shash, args->map.map_type);
+	if (!retn)
+		goto done;
+
+	retn = add_u32(shash, args->map.fmode);
+
+ done:
+	return retn;
+}
+
+static int add_bpf_prog(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_bpf_args *args = &ep->CELL.bpf;
+
+	retn = add_u32(shash, args->prog.type);
+	if (!retn)
+		goto done;
+
+	retn = add_u32(shash, args->prog.attach_type);
+
+ done:
+	return retn;
+}
+
+static int add_settime(struct shash_desc *shash, struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_time_args *args = &ep->CELL.time;
+
+	if (args->have_ts) {
+		retn = add_u64(shash, ep->CELL.time.seconds);
+		if (retn)
+			goto done;
+
+		retn = add_u64(shash, ep->CELL.time.nsecs);
+		if (retn)
+			goto done;
+	}
+
+	if (args->have_tz) {
+		retn = add_u32(shash, ep->CELL.time.minuteswest);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.time.dsttime);
+		if (retn)
+			goto done;
+	}
+
+ done:
+	return retn;
+}
+
+static int get_cell_mapping(struct tsem_event *ep, u8 *mapping)
+{
+	int retn = 0, size;
+	u8 *p;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	switch (ep->event) {
+	case TSEM_NETLINK_SEND:
+		retn = add_socket(shash, &ep->CELL.socket.out.socka);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.netlink.out.uid);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.netlink.out.gid);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.netlink.out.portid);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.netlink.out.dst_group);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.netlink.out.flags);
+		if (retn)
+			goto done;
+
+		if (ep->CELL.netlink.out.nsid_set)
+			retn = add_u32(shash, ep->CELL.netlink.out.flags);
+		break;
+
+	case TSEM_IPC_PERMISSION:
+		retn = add_ipc_cred(shash, &ep->CELL.ipc);
+		if (retn)
+			goto done;
+
+		retn = add_u16(shash, ep->CELL.ipc.perm_flag);
+		break;
+
+	case TSEM_SHM_ASSOCIATE:
+	case TSEM_SHM_SHMCTL:
+	case TSEM_SHM_SHMAT:
+	case TSEM_SEM_ASSOCIATE:
+	case TSEM_SEM_SEMCTL:
+	case TSEM_MSG_QUEUE_ASSOCIATE:
+	case TSEM_MSG_QUEUE_MSGCTL:
+		retn = add_ipc_cred(shash, &ep->CELL.ipc);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.ipc.value);
+		break;
+
+	case TSEM_MSG_QUEUE_MSGRCV:
+		retn = add_ipc_cred(shash, &ep->CELL.ipc);
+		if (retn)
+			goto done;
+
+		p = ep->CELL.ipc.out.target;
+		size = tsem_digestsize();
+		retn = crypto_shash_update(shash, p, size);
+		if (retn)
+			goto done;
+
+		retn = add_u64(shash, ep->CELL.ipc.type);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.ipc.value);
+		break;
+
+	case TSEM_SEM_SEMOP:
+		retn = add_ipc_cred(shash, &ep->CELL.ipc);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.ipc.nsops);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.ipc.value);
+		break;
+
+	case TSEM_INODE_CREATE:
+		retn = add_inode_create(shash, ep);
+		break;
+
+	case TSEM_INODE_MKDIR:
+		retn = add_inode(shash, &ep->CELL.inode.out.dir);
+		if (retn)
+			goto done;
+
+		retn = add_dentry(shash, &ep->CELL.inode.out.dentry);
+		if (retn)
+			goto done;
+
+		retn = add_u16(shash, ep->CELL.inode.mode);
+		break;
+
+	case TSEM_INODE_RMDIR:
+	case TSEM_INODE_UNLINK:
+		retn = add_inode(shash, &ep->CELL.inode.out.dir);
+		if (retn)
+			goto done;
+
+		retn = add_dentry(shash, &ep->CELL.inode.out.dentry);
+		break;
+
+	case TSEM_SYSLOG:
+		retn = add_u32(shash, ep->CELL.value);
+		break;
+
+	case TSEM_SETTIME:
+		retn = add_settime(shash, ep);
+		break;
+
+	case TSEM_INODE_LINK:
+		retn = add_inode_link(shash, ep);
+		break;
+
+	case TSEM_INODE_SYMLINK:
+		retn = add_inode(shash, &ep->CELL.inode.out.dir);
+		if (retn)
+			goto done;
+
+		retn = add_dentry(shash, &ep->CELL.inode.out.dentry);
+		if (retn)
+			goto done;
+
+		retn = add_str(shash, ep->CELL.inode.out.old_name);
+		break;
+
+	case TSEM_INODE_MKNOD:
+		retn = add_inode(shash, &ep->CELL.inode.out.dir);
+		if (retn)
+			goto done;
+
+		retn = add_dentry(shash, &ep->CELL.inode.out.dentry);
+		if (retn)
+			goto done;
+
+		retn = add_u16(shash, ep->CELL.inode.mode);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.inode.dev);
+		break;
+
+	case TSEM_INODE_RENAME:
+		retn = add_inode_rename(shash, ep);
+		break;
+
+	case TSEM_INODE_KILLPRIV:
+		retn = add_dentry(shash, &ep->CELL.inode.out.dentry);
+		break;
+
+	case TSEM_FILE_OPEN:
+	case TSEM_BPRM_COMMITTED_CREDS:
+		retn = add_file(shash, &ep->CELL.file);
+		break;
+
+	case TSEM_FILE_IOCTL:
+	case TSEM_FILE_LOCK:
+	case TSEM_FILE_FCNTL:
+		retn = add_file(shash, &ep->CELL.file);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.file.cmd);
+		break;
+
+	case TSEM_FILE_RECEIVE:
+		retn = add_file(shash, &ep->CELL.file);
+		break;
+
+	case TSEM_MMAP_FILE:
+		retn = add_mmap_file(shash, ep);
+		break;
+
+	case TSEM_UNIX_STREAM_CONNECT:
+	case TSEM_UNIX_MAY_SEND:
+	case TSEM_SOCKET_SOCKETPAIR:
+		retn = add_socket(shash, &ep->CELL.socket.out.socka);
+		if (retn)
+			goto done;
+
+		retn = add_socket(shash, &ep->CELL.socket.out.sockb);
+		break;
+
+	case TSEM_SOCKET_SENDMSG:
+	case TSEM_SOCKET_RECVMSG:
+		retn = add_socket_msg(shash, ep);
+		break;
+
+	case TSEM_SOCKET_GETSOCKNAME:
+	case TSEM_SOCKET_GETPEERNAME:
+	case TSEM_TUN_DEV_ATTACH_QUEUE:
+		retn = add_socket(shash, &ep->CELL.socket.out.socka);
+		break;
+
+	case TSEM_SOCKET_CREATE:
+		retn = add_u32(shash, ep->CELL.socket.out.socka.family);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.socket.out.socka.type);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.socket.out.socka.protocol);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.socket.out.socka.kern);
+		break;
+
+	case TSEM_SOCKET_CONNECT:
+	case TSEM_SOCKET_BIND:
+		retn = add_socket_connect_bind(shash, ep);
+		break;
+
+	case TSEM_SOCKET_ACCEPT:
+		retn = add_socket_accept(shash, ep);
+		break;
+
+	case TSEM_SOCKET_LISTEN:
+	case TSEM_SOCKET_SHUTDOWN:
+		retn = add_socket(shash, &ep->CELL.socket.out.socka);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.socket.value);
+		break;
+
+	case TSEM_SOCKET_SETSOCKOPT:
+		retn = add_socket(shash, &ep->CELL.socket.out.socka);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.socket.value);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.socket.optname);
+		break;
+
+	case TSEM_KERNEL_MODULE_REQUEST:
+		retn = add_str(shash, ep->CELL.kernel.out.kmod_name);
+		break;
+
+	case TSEM_KERNEL_LOAD_DATA:
+		retn = add_u32(shash, ep->CELL.kernel.id);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.kernel.contents);
+		break;
+
+	case TSEM_KERNEL_READ_FILE:
+		retn = add_file(shash, &ep->CELL.kernel.out.file);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.kernel.id);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.kernel.contents);
+		break;
+
+	case TSEM_TASK_KILL:
+		retn = add_task_kill(shash, ep);
+		break;
+
+	case TSEM_PTRACE_ACCESS_CHECK:
+		retn = add_ptrace_access_check(shash, ep);
+		break;
+
+	case TSEM_PTRACE_TRACEME:
+		retn = add_task(shash, ep->CELL.task_kill.source);
+		break;
+
+	case TSEM_CAPGET:
+		retn = add_capget(shash, ep);
+		break;
+
+	case TSEM_CAPSET:
+		retn = add_capset(shash, ep);
+		break;
+
+	case TSEM_CAPABLE:
+		retn = add_capable(shash, ep);
+		break;
+
+	case TSEM_TASK_SETPGID:
+		p = ep->CELL.task_kill.target;
+		size = sizeof(ep->CELL.task_kill.target);
+		retn = crypto_shash_update(shash, p, size);
+		if (retn)
+			goto done;
+
+		p = ep->CELL.task_kill.source;
+		size = sizeof(ep->CELL.task_kill.source);
+		retn = crypto_shash_update(shash, p, size);
+		break;
+
+	case TSEM_TASK_GETPGID:
+	case TSEM_TASK_GETSID:
+	case TSEM_TASK_GETIOPRIO:
+	case TSEM_TASK_SETSCHEDULER:
+	case TSEM_TASK_GETSCHEDULER:
+		retn = add_task(shash, ep->CELL.task_kill.target);
+		break;
+
+	case TSEM_TASK_SETNICE:
+	case TSEM_TASK_SETIOPRIO:
+		p = ep->CELL.task_kill.target;
+		size = sizeof(ep->CELL.task_kill.target);
+		retn = crypto_shash_update(shash, p, size);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.task_kill.u.value);
+		break;
+
+	case TSEM_TASK_PRLIMIT:
+		retn = add_creds(shash, &ep->CELL.task_prlimit.out.cred);
+		if (retn)
+			goto done;
+
+		retn = add_creds(shash, &ep->CELL.task_prlimit.out.tcred);
+		if (retn)
+			goto done;
+
+		retn = add_u32(shash, ep->CELL.task_prlimit.flags);
+		break;
+
+	case TSEM_TASK_SETRLIMIT:
+		retn = add_task_setrlimit(shash, ep);
+		break;
+
+	case TSEM_TASK_PRCTL:
+		retn = add_u32(shash, ep->CELL.task_prctl.option);
+		if (retn)
+			goto done;
+
+		if (ep->CELL.task_prctl.option != PR_GET_PDEATHSIG)
+			retn = add_u64(shash, ep->CELL.task_prctl.arg2);
+		else
+			retn = add_u64(shash, 0);
+		if (retn)
+			goto done;
+
+		retn = add_u64(shash, ep->CELL.task_prctl.arg4);
+		if (retn)
+			goto done;
+
+		retn = add_u64(shash, ep->CELL.task_prctl.arg5);
+		break;
+
+	case TSEM_INODE_GETATTR:
+		retn = add_dentry(shash, &ep->CELL.inode_attr.out.dentry);
+		break;
+
+	case TSEM_INODE_SETATTR:
+		retn = add_inode_setattr(shash, ep);
+		break;
+
+	case TSEM_INODE_SETXATTR:
+	case TSEM_INODE_GETXATTR:
+	case TSEM_INODE_REMOVEXATTR:
+	case TSEM_INODE_LISTXATTR:
+		retn = add_xattr(shash, ep);
+		break;
+
+	case TSEM_KEY_ALLOC:
+		retn = add_creds(shash, &ep->CELL.key.out.cred);
+		if (retn)
+			goto done;
+
+		retn = add_u64(shash, ep->CELL.key.flags);
+		break;
+
+	case TSEM_KEY_PERMISSION:
+		retn = add_key_permission(shash, ep);
+		break;
+
+	case TSEM_SB_MOUNT:
+		if (ep->CELL.sb.out.dev_name) {
+			retn = add_str(shash, ep->CELL.sb.out.dev_name);
+			if (retn)
+				goto done;
+		}
+
+		retn = add_path(shash, &ep->CELL.sb.out.path);
+		if (retn)
+			goto done;
+
+		if (ep->CELL.sb.out.type) {
+			retn = add_str(shash, ep->CELL.sb.out.type);
+			if (retn)
+				goto done;
+		}
+
+		retn = add_u64(shash, ep->CELL.sb.flags);
+		break;
+
+	case TSEM_SB_UMOUNT:
+		retn = add_sb_umount(shash, ep);
+		break;
+
+	case TSEM_SB_REMOUNT:
+		retn = add_sb_remount(shash, ep);
+		break;
+
+	case TSEM_SB_STATFS:
+		retn = add_dentry(shash, &ep->CELL.sb.out.dentry);
+		break;
+
+	case TSEM_SB_PIVOTROOT:
+	case TSEM_MOVE_MOUNT:
+		retn = add_move_path(shash, ep);
+		break;
+
+	case TSEM_QUOTACTL:
+		retn = add_quotactl(shash, ep);
+		break;
+
+	case TSEM_QUOTA_ON:
+		retn = add_dentry(shash, &ep->CELL.quota.out.dentry);
+		break;
+
+	case TSEM_BPF:
+		retn = add_bpf(shash, ep);
+		break;
+
+	case TSEM_BPF_MAP:
+		retn = add_bpf_map(shash, ep);
+		break;
+
+	case TSEM_BPF_PROG:
+		retn = add_bpf_prog(shash, ep);
+		break;
+
+	default:
+		p = (u8 *) tsem_names[ep->event];
+		size = strlen(tsem_names[ep->event]);
+		retn = crypto_shash_update(shash, p, size);
+		if (retn)
+			goto done;
+
+		p = tsem_context(current)->zero_digest;
+		size = tsem_digestsize();
+		retn = crypto_shash_update(shash, p, size);
+		break;
+	}
+
+ done:
+	if (!retn)
+		retn = crypto_shash_final(shash, mapping);
+
+	if (ep->event == TSEM_INODE_SETXATTR) {
+		kfree(ep->CELL.inode_xattr.out.value);
+		ep->CELL.inode_xattr.out.value = NULL;
+	}
+
+	return retn;
+}
+
+static int get_event_mapping(int event, u8 *p_task_id, u8 *task_id,
+			     u8 *COE_id, u8 *cell_id, u8 *mapping)
+{
+	int retn = 0;
+	u32 event_id = (u32) event;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, tsem_names[event_id],
+				   strlen(tsem_names[event_id]));
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, p_task_id, tsem_digestsize());
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, task_id, tsem_digestsize());
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, COE_id, tsem_digestsize());
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_finup(shash, cell_id, tsem_digestsize(), mapping);
+
+ done:
+	return retn;
+}
+
+static int map_event(struct tsem_event *ep, u8 *p_task_id, u8 *task_id,
+		     u8 *event_mapping)
+{
+	int retn;
+	u8 COE_mapping[HASH_MAX_DIGESTSIZE];
+	u8 cell_mapping[HASH_MAX_DIGESTSIZE];
+
+	retn = get_COE_mapping(ep, COE_mapping);
+	if (retn)
+		goto done;
+
+	retn = get_cell_mapping(ep, cell_mapping);
+	if (retn)
+		goto done;
+
+	retn = get_event_mapping(ep->event, p_task_id, task_id, COE_mapping,
+				 cell_mapping, event_mapping);
+ done:
+	return retn;
+}
+
+/**
+ * tsem_map_task() - Create the task identity description structure.
+ * @file: A pointer to the file structure defining the executable.
+ * @task_id: Pointer to the buffer that the task id will be copied to.
+ *
+ * This function creates the security event state point that will be used
+ * as the task identifier for the generation of security state points
+ * that are created by the process that task identifier is assigned to.
+ *
+ * Return: This function returns 0 if the mapping was successfully
+ *	   created and an error value otherwise.
+ */
+int tsem_map_task(struct file *file, u8 *task_id)
+{
+	int retn;
+	u8 null_taskid[HASH_MAX_DIGESTSIZE];
+	struct tsem_event *ep;
+
+	ep = tsem_event_allocate(TSEM_BPRM_COMMITTED_CREDS, false);
+	if (!ep)
+		return -ENOMEM;
+
+	ep->CELL.file.in.file = file;
+	retn = tsem_event_init(ep);
+	if (retn > 0) {
+		memset(null_taskid, '\0', tsem_digestsize());
+		retn = map_event(ep, tsem_task(current)->p_task_id,
+				 null_taskid, task_id);
+	}
+
+	tsem_event_put(ep);
+	return retn;
+}
+
+/**
+ * tsem_map_event() - Create a security event mapping.
+ * @event: The number of the event to be mapped.
+ * @params: A pointer to the structure containing the event description
+ *	    parameters.
+ *
+ * This function creates a structure to describe a security event
+ * and maps the event into a security state coefficient.
+ *
+ * Return: This function returns a value of zero on success and a negative
+ *	   error code on failure.
+ */
+int tsem_map_event(struct tsem_event *ep)
+{
+	struct tsem_task *task = tsem_task(current);
+
+	return map_event(ep, task->p_task_id, task->task_id, ep->mapping);
+}
Signature · 1 line
2.39.1
#37 lore raw

PATCH v4 11/14 Implement the internal Trusted Modeling Agent.

In reply to Greg Wettstein

The model.c file is responsible for managing the security state information for a security modeling namespace. Important to note is that the map.c file is responsible for generating the coefficients and hence the implementation of the model. The model.c file maintains the state information and makes the decision with respect to whether or not a presented event is consistent with the model being enforced.

By default the TMA runs in free modeling mode where all security coefficients and there associated descriptions are considered valid and retained as the security execution trajectory for the namespace.

The model implementation can be 'sealed' through the TSEM control plane, a condition that causes any state points not registered in the model to be considered a 'forensics' event. The description of such an event is added to the forensics execution trajectory for the model. A forensics event does not result in permission to the event to be denied unless the model is placed in 'enforcing' mode.

The TMA implementation also supports the definition of 'base' point that is binary value equal in length to the cryptographic hash function being used by the security modeling namespace. This base point is used to linear extend each security state coefficient before it is added to the model. This allows a relying part to confirm the 'freshnes' of the functional state of the model.

The modeling implementation supports two different functional values for the model being implemented. The classic linear extension sum of all the security state points and a value referred to as the 'state' of the security model.

The classic linear extension sum is similar to what would be implemented by a Platform Configuration Register in a Trusted Platform Module. It can be used verify the order in which security event occurred but is also fragile with respect to process scheduling dependencies, particularly on SMP platforms.

The 'state' value is designed to make the measurement value invariant to these scheduling induced variations. The state value is computed by sorting the security event state points in the model in big-endian (natural hash byte order) format and then computing the extension sum over this sorted vector of points.

This value verifies that the model has only existed in a prescribed set of security states.

---
 security/tsem/model.c | 758 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 758 insertions(+)
 create mode 100644 security/tsem/model.c
security/tsem/model.c new file +758 -0
new file mode 100644
index 000000000000..013a24b88c83
@@ -0,0 +1,758 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+
+ * This file is responsible for maintaining the security model state
+ * information for the internal trusted modeling agent implementation.
+ *
+ * The state information consists of security event descriptions that
+ * have been experienced by a security modeling namespace and the
+ * security state coefficients that they map into.  Only unique
+ * descriptions and their coefficients are maintained for maintained
+ * for each security modeling namespace.
+ *
+ * An important point to note is that the routines in this file do not
+ * implement a security model.  The mapping of security event
+ * descriptions into coefficients, in the map.c file, is the
+ * functionality that actually implements the model.  Future
+ * implementations of TSEM are anticipated to provide alternate models
+ * by providing alternative implementations of the mapping routines.
+ *
+ * The generative functions used to create the security state
+ * coefficients managed by this file are fully described in the
+ * primary TSEM documentation file.
+ *
+ * As with the event.c and export.c files this file must handle events
+ * that run in both atomic and non-atomic context.  For processes that
+ * run in non-atomic context the structures that represent a security
+ * state coefficient are provided by the kmem_cache structure
+ * implement in this file.
+ *
+ * In order to support processes that are running in atomic context a
+ * magazine of structure is maintained as well.  Allocations from this
+ * magazine are replaced with a function that runs in the context of
+ * an asynchronous workqueue.
+ */
+
+#include <linux/sort.h>
+#include <linux/vmalloc.h>
+
+#include "tsem.h"
+
+struct pseudonym {
+	struct list_head list;
+	u8 mapping[HASH_MAX_DIGESTSIZE];
+};
+
+static struct kmem_cache *point_cachep;
+
+static void refill_point_magazine(struct work_struct *work)
+{
+	struct tsem_event_point *tep;
+	struct tsem_work *ws;
+
+	ws = container_of(work, struct tsem_work, work);
+
+	tep = kmem_cache_zalloc(point_cachep, GFP_KERNEL);
+	if (!tep) {
+		pr_warn("tsem: Cannot refill model point magazine.\n");
+		return;
+	}
+
+	spin_lock(&ws->u.model->magazine_lock);
+	ws->u.model->magazine[ws->index] = tep;
+	clear_bit(ws->index, ws->u.model->magazine_index);
+
+	/*
+	 * The following memory barrier is used to cause the magazine
+	 * index to be visible after the refill of the cache slot.
+	 */
+	smp_mb__after_atomic();
+
+	spin_unlock(&ws->u.model->magazine_lock);
+}
+
+static struct tsem_event_point *alloc_event_point(struct tsem_model *model,
+						  bool locked)
+{
+	unsigned int index;
+	struct tsem_event_point *tep = NULL;
+
+	if (!locked)
+		return kmem_cache_zalloc(point_cachep, GFP_KERNEL);
+
+	spin_lock(&model->magazine_lock);
+	index = find_first_zero_bit(model->magazine_index,
+				    model->magazine_size);
+	if (index < model->magazine_size) {
+		tep = model->magazine[index];
+		model->ws[index].index = index;
+		model->ws[index].u.model = model;
+		set_bit(index, model->magazine_index);
+
+		/*
+		 * Similar to the issue noted in the refill_point_magazine(),
+		 * function, this barrier is used to cause the consumption
+		 * of the cache entry to become visible.
+		 */
+		smp_mb__after_atomic();
+	}
+	spin_unlock(&model->magazine_lock);
+
+	if (tep) {
+		INIT_WORK(&model->ws[index].work, refill_point_magazine);
+		queue_work(system_wq, &model->ws[index].work);
+		return tep;
+	}
+
+	pr_warn("tsem: Fail model point allocation comm %s ns %llu cs %u.\n",
+		current->comm, tsem_context(current)->id,
+		model->magazine_size);
+	return NULL;
+
+}
+
+static int magazine_allocate(struct tsem_model *model, size_t size)
+{
+	unsigned int lp;
+	int retn = -ENOMEM;
+
+	model->magazine_size = size;
+
+	spin_lock_init(&model->magazine_lock);
+
+	model->magazine_index = bitmap_zalloc(model->magazine_size,
+					      GFP_KERNEL);
+	if (!model->magazine_index)
+		return retn;
+
+	model->magazine = kcalloc(model->magazine_size,
+				  sizeof(*model->magazine), GFP_KERNEL);
+	if (!model->magazine)
+		goto done;
+
+	for (lp = 0; lp < model->magazine_size; ++lp) {
+		model->magazine[lp] = kmem_cache_zalloc(point_cachep,
+							GFP_KERNEL);
+		if (!model->magazine[lp])
+			goto done;
+	}
+
+	model->ws = kcalloc(model->magazine_size, sizeof(*model->ws),
+			    GFP_KERNEL);
+	if (model->ws)
+		retn = 0;
+
+ done:
+	if (retn)
+		tsem_model_magazine_free(model);
+
+	return retn;
+}
+
+static int generate_pseudonym(char *pathname, u8 *pseudonym)
+{
+	int retn = 0;
+	u32 size;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	size = strlen(pathname);
+	retn = crypto_shash_update(shash, (u8 *) &size, sizeof(size));
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_finup(shash, pathname, size, pseudonym);
+
+ done:
+	return retn;
+}
+
+static struct tsem_event_point *have_point(u8 *point)
+{
+	struct tsem_event_point *entry, *retn = NULL;
+	struct tsem_context *ctx = tsem_context(current);
+	struct tsem_model *model = ctx->model;
+
+	spin_lock(&model->point_lock);
+	list_for_each_entry(entry, &model->point_list, list) {
+		if (!memcmp(entry->point, point, tsem_digestsize())) {
+			retn = entry;
+			goto done;
+		}
+	}
+
+ done:
+	spin_unlock(&model->point_lock);
+	return retn;
+}
+
+static struct tsem_event_point *add_event_point(u8 *point, bool valid,
+						bool locked)
+{
+	struct tsem_event_point *entry;
+	struct tsem_model *model = tsem_model(current);
+
+	entry = alloc_event_point(model, locked);
+	if (!entry)
+		return ERR_PTR(-ENOMEM);
+
+	entry->valid = valid;
+	memcpy(entry->point, point, tsem_digestsize());
+
+	spin_lock(&model->point_lock);
+	++model->point_count;
+	list_add_tail(&entry->list, &model->point_list);
+	spin_unlock(&model->point_lock);
+
+	return entry;
+}
+
+static int add_trajectory_point(struct tsem_event *ep)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	ep->pid = 0;
+	tsem_event_get(ep);
+
+	spin_lock(&model->trajectory_lock);
+	list_add_tail(&ep->list, &model->trajectory_list);
+	spin_unlock(&model->trajectory_lock);
+
+	return 0;
+}
+
+static int add_forensic_point(struct tsem_event *ep)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	ep->pid = 0;
+	tsem_event_get(ep);
+
+	spin_lock(&model->forensics_lock);
+	list_add_tail(&ep->list, &model->forensics_list);
+	spin_unlock(&model->forensics_lock);
+
+	return 0;
+}
+
+static int get_host_measurement(u8 *id, u8 *digest)
+{
+	int retn;
+	struct tsem_model *model = tsem_model(current);
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, model->base, tsem_digestsize());
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_finup(shash, id, tsem_digestsize(), digest);
+
+ done:
+	return retn;
+}
+
+static int update_events_measurement(struct tsem_event *ep)
+{
+	int retn;
+	u8 digest[HASH_MAX_DIGESTSIZE];
+	struct tsem_context *ctx = tsem_context(current);
+	struct tsem_model *model = ctx->model;
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	retn = get_host_measurement(ep->mapping, digest);
+	if (retn)
+		goto done;
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_update(shash, model->measurement,
+				   tsem_digestsize());
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_finup(shash, digest, tsem_digestsize(),
+				  model->measurement);
+	if (retn)
+		goto done;
+
+	if (!tsem_context(current)->id)
+		retn = tsem_trust_add_event(ep);
+
+ done:
+	return retn;
+}
+
+static int state_sort(const void *a, const void *b)
+{
+	unsigned int lp, retn = 0;
+	struct tsem_event_point *ap, *bp;
+
+	ap = *((struct tsem_event_point **) a);
+	bp = *((struct tsem_event_point **) b);
+
+	for (lp = 0; lp < tsem_digestsize(); ++lp) {
+		if (ap->point[lp] == bp->point[lp])
+			continue;
+
+		if (ap->point[lp] < bp->point[lp])
+			retn = -1;
+		else
+			retn = 1;
+		goto done;
+	}
+
+ done:
+	return retn;
+}
+
+/**
+ * tesm_model_compute_state() - Calculate a security model state value.
+ *
+ * The function generates the state value of the current modeling domain.
+ */
+void tsem_model_compute_state(void)
+{
+	u8 state[HASH_MAX_DIGESTSIZE];
+	int retn;
+	unsigned int lp, count, pt_count = 0;
+	struct list_head *end;
+	struct tsem_event_point *end_point, *entry, **points = NULL;
+	struct tsem_model *model = tsem_model(current);
+	SHASH_DESC_ON_STACK(shash, tfm);
+
+	shash->tfm = tsem_digest();
+	retn = crypto_shash_init(shash);
+	if (retn)
+		goto done;
+
+	memset(state, '\0', sizeof(state));
+	retn = crypto_shash_update(shash, state, tsem_digestsize());
+	if (retn)
+		goto done;
+
+	retn = get_host_measurement(tsem_trust_aggregate(), state);
+	if (retn)
+		goto done;
+
+	retn = crypto_shash_finup(shash, state, tsem_digestsize(), state);
+	if (retn)
+		goto done;
+
+	spin_lock(&model->point_lock);
+	end = model->point_list.prev;
+	count = model->point_count;
+	spin_unlock(&model->point_lock);
+
+	points = vmalloc(sizeof(*points) * count);
+	if (!points) {
+		retn = -ENOMEM;
+		goto done;
+	}
+
+	end_point = container_of(end, struct tsem_event_point, list);
+	list_for_each_entry(entry, &model->point_list, list) {
+		points[pt_count++] = entry;
+		if (end_point == entry)
+			break;
+	}
+	sort(points, count, sizeof(*points), state_sort, NULL);
+
+	memcpy(model->state, state, tsem_digestsize());
+	for (lp = 0; lp < pt_count; ++lp) {
+		entry = points[lp];
+
+		if (get_host_measurement(entry->point, state))
+			goto done;
+
+		if (crypto_shash_init(shash))
+			goto done;
+		if (crypto_shash_update(shash, model->state,
+					tsem_digestsize()))
+			goto done;
+		if (crypto_shash_finup(shash, state, tsem_digestsize(),
+				       model->state))
+			goto done;
+	}
+
+ done:
+	if (retn)
+		memset(model->state, '\0', tsem_digestsize());
+
+	vfree(points);
+}
+
+/**
+ * tsem_model_has_pseudonym() - Test for a model pseudonym.
+ * @tsip: A pointer to the TSEM inode security structure.
+ * @file: A pointer to the tsem_file_args structure characterizing the
+ *	  file whose pseudonym is to be checked.
+ *
+ * This function is used to test whether a pseudonym has been
+ * declared for a modeling domain.  It is up to the caller to
+ * populate the event description structure with a suitable
+ * value for the pseudonym digest.
+ *
+ * Return: If an error occurs during the pseudonym probe a negative
+ *	   return value is returned.  A zero return value indicates that
+ *	   a pseudonym was not present.  A value of one indicates that a
+ *	   pseudonym has been defined.
+ */
+int tsem_model_has_pseudonym(struct tsem_inode *tsip, char *pathname)
+{
+	int retn = 0;
+	u8 pseudo_mapping[HASH_MAX_DIGESTSIZE];
+	struct tsem_model *model = tsem_model(current);
+	struct pseudonym *entry;
+
+	retn = generate_pseudonym(pathname, pseudo_mapping);
+	if (retn)
+		goto done;
+
+	mutex_lock(&model->pseudonym_mutex);
+	list_for_each_entry(entry, &model->pseudonym_list, list) {
+		if (!memcmp(entry->mapping, pseudo_mapping,
+			    tsem_digestsize())) {
+			retn = 1;
+			goto done;
+		}
+	}
+	retn = 0;
+
+ done:
+	mutex_unlock(&model->pseudonym_mutex);
+	return retn;
+}
+
+/**
+ * tesm_model_event() - Inject a security event into a modeling domain.
+ * @ep: A pointer to the event description structure.
+ *
+ * This function is the entry point for the in kernel Trusted Modeling
+ * Agent (TMA).  It takes a description of an event encoded in a
+ * tsem_event structure and generates and updates the security model
+ * description.
+ *
+ * Return: If an error occurs during the injection of an event into a
+ *	   model a negative error value is returned.  A value of zero
+ *	   is returned if the event was successfully modeled.  The
+ *	   security status of the event is returned by encoding the value
+ *	   in the bad_COE member of the tsem_task structure.
+ */
+int tsem_model_event(struct tsem_event *ep)
+{
+	int retn;
+	struct tsem_event_point *point;
+	struct tsem_task *task = tsem_task(current);
+	struct tsem_context *ctx = task->context;
+
+	retn = ctx->ops->map(ep);
+	if (retn)
+		return retn;
+
+	point = have_point(ep->mapping);
+	if (point) {
+		++point->count;
+		if (!point->valid)
+			task->trust_status = TSEM_TASK_UNTRUSTED;
+		return 0;
+	}
+
+	retn = update_events_measurement(ep);
+	if (retn)
+		return retn;
+
+	retn = -ENOMEM;
+	if (ctx->sealed) {
+		point = add_event_point(ep->mapping, false, ep->locked);
+		if (point) {
+			retn = add_forensic_point(ep);
+			task->trust_status = TSEM_TASK_UNTRUSTED;
+		}
+	} else {
+		point = add_event_point(ep->mapping, true, ep->locked);
+		if (point)
+			retn = add_trajectory_point(ep);
+	}
+
+	if (!retn)
+		++point->count;
+	return retn;
+}
+
+/**
+ * tesm_model_load_point() - Load a security state event into a model.
+ * @point: A pointer to the array containing the security state
+ *	   point to be added to the model.
+ *
+ * This function takes the binary representation of a security state
+ * point and loads it into the current model domain.
+ *
+ * Return: If an error occurs during the processing of the security state
+ *	   point a negative return value is returned.  A return value of
+ *	   zero indicates the point was successfully loaded into the domain.
+ */
+int tsem_model_load_point(u8 *point)
+{
+	int retn = -ENOMEM;
+	struct tsem_event *ep;
+	struct tsem_context *ctx = tsem_context(current);
+
+	if (have_point(point))
+		return 0;
+
+	if (!add_event_point(point, true, false))
+		return retn;
+
+	if (!ctx->model->have_aggregate) {
+		retn = tsem_model_add_aggregate();
+		if (retn)
+			return retn;
+
+		ctx->model->have_aggregate = true;
+	}
+
+	ep = tsem_event_allocate(0, false);
+	if (!ep)
+		return retn;
+
+	kref_init(&ep->kref);
+	memcpy(ep->mapping, point, tsem_digestsize());
+	retn = update_events_measurement(ep);
+
+	tsem_event_put(ep);
+	return retn;
+}
+
+/**
+ * tesm_model_load_pseudonym() - Load a pseudonym state point to a model.
+ * @mapping: A pointer to the array containing the pseudonym state
+ *	     point that is to be added to the model.
+ *
+ * This function takes the binary representation of a file pseudonym
+ * and declares the presence of the pseudonym in the modeling domain.
+ *
+ * Return: If an error occurs during the processing of the pseudonym
+ *	   state point a negative return value is returned.  A return
+ *	   value of zero indicates the point was successfully loaded
+ *	   into the model.
+ */
+int tsem_model_load_pseudonym(u8 *mapping)
+{
+	struct pseudonym *psp = NULL;
+	struct tsem_model *model = tsem_model(current);
+
+	psp = kzalloc(sizeof(*psp), GFP_KERNEL);
+	if (!psp)
+		return -ENOMEM;
+	memcpy(psp->mapping, mapping, tsem_digestsize());
+
+	mutex_lock(&model->pseudonym_mutex);
+	list_add_tail(&psp->list, &model->pseudonym_list);
+	mutex_unlock(&model->pseudonym_mutex);
+	return 0;
+}
+
+/**
+ * tesm_model_load_base() - Load a model base point.
+ * @mapping: A pointer to the array containing the base point to be
+ *	     set for the model.
+ *
+ * This function takes the binary representation of a base point and
+ * sets this point as the base point for the model.
+ */
+void tsem_model_load_base(u8 *mapping)
+{
+	struct tsem_model *model = tsem_model(current);
+
+	memcpy(model->base, mapping, tsem_digestsize());
+}
+
+/**
+ * tesm_model_add_aggregate() - Add the hardware aggregate to a model.
+ *
+ * This function adds the hardware aggregate value to an internally
+ * modeled security domain.
+ *
+ * Return: If an error occurs during the injection of the aggregate
+ *	   value into the model a negative error value is returned.
+ *	   A return value of zero indicates the aggregate was
+ *	   successfully added.
+ */
+int tsem_model_add_aggregate(void)
+{
+	int retn;
+	struct tsem_event *ep;
+
+	ep = tsem_event_allocate(0, false);
+	if (!ep)
+		return -ENOMEM;
+
+	kref_init(&ep->kref);
+	ep->digestsize = tsem_digestsize();
+	memcpy(ep->mapping, tsem_trust_aggregate(), ep->digestsize);
+
+	retn = update_events_measurement(ep);
+	tsem_event_put(ep);
+
+	return retn;
+}
+
+/**
+ * tsem_model_allocate() - Allocates a kernel TMA modeling structure.
+ * @size: The number of slots in the event point magazine for the model.
+ *
+ * This function allocates and initializes a tsem_model structure
+ * that is used to hold modeling information for an in kernel
+ * modeling domain.
+ *
+ * Return: On success a pointer to the model description structure is
+ *	   returned.  If an error occurs an error return value is
+ *	   encoded in the returned pointer.
+ */
+struct tsem_model *tsem_model_allocate(size_t size)
+{
+	struct tsem_model *model = NULL;
+
+	model = kzalloc(sizeof(*model), GFP_KERNEL);
+	if (!model)
+		return NULL;
+
+	spin_lock_init(&model->point_lock);
+	INIT_LIST_HEAD(&model->point_list);
+	mutex_init(&model->point_end_mutex);
+
+	spin_lock_init(&model->trajectory_lock);
+	INIT_LIST_HEAD(&model->trajectory_list);
+	mutex_init(&model->trajectory_end_mutex);
+
+	spin_lock_init(&model->forensics_lock);
+	INIT_LIST_HEAD(&model->forensics_list);
+	mutex_init(&model->forensics_end_mutex);
+
+	mutex_init(&model->pseudonym_mutex);
+	INIT_LIST_HEAD(&model->pseudonym_list);
+
+	mutex_init(&model->mount_mutex);
+	INIT_LIST_HEAD(&model->mount_list);
+
+	if (magazine_allocate(model, size)) {
+		kfree(model);
+		model = NULL;
+	}
+
+	return model;
+}
+
+/**
+ * tsem_model_free() - Frees an a kernel TMA description structure.
+ * @ctx: A pointer to the TMA modeling description structure whose
+ *	 model definition is to be deleted.
+ *
+ * This function is called when the last reference to a kernel
+ * based TMA model description structure is released.
+ */
+void tsem_model_free(struct tsem_context *ctx)
+{
+	struct tsem_event_point *ep, *tmp_ep;
+	struct tsem_event *tentry, *tmp_tentry;
+	struct pseudonym *sentry, *tmp_sentry;
+	struct tsem_inode_instance *ientry, *tmp_ientry;
+	struct tsem_model *model = ctx->model;
+
+	list_for_each_entry_safe(ep, tmp_ep, &model->point_list, list) {
+		list_del(&ep->list);
+		kmem_cache_free(point_cachep, ep);
+	}
+
+	list_for_each_entry_safe(sentry, tmp_sentry, &model->pseudonym_list,
+				 list) {
+		list_del(&sentry->list);
+		kfree(sentry);
+	}
+
+	list_for_each_entry_safe(tentry, tmp_tentry, &model->trajectory_list,
+				 list) {
+		list_del(&tentry->list);
+		tsem_event_put(tentry);
+	}
+
+	list_for_each_entry_safe(ientry, tmp_ientry, &model->mount_list,
+				 list) {
+		list_del(&ientry->list);
+		kfree(ientry);
+	}
+
+	if (ctx->sealed) {
+		list_for_each_entry_safe(tentry, tmp_tentry,
+					 &model->forensics_list, list) {
+			list_del(&tentry->list);
+			tsem_event_put(tentry);
+		}
+	}
+
+	tsem_model_magazine_free(model);
+	kfree(model);
+}
+
+/**
+ * tsem_model_magazine_free: Free the event point magazine for a model domain.
+ * @model: A pointer to the model whose magazine is to be freed.
+ *
+ * This function releases all of the components of an event point
+ * magazine that has been allocated for a modeling domain.
+ */
+void tsem_model_magazine_free(struct tsem_model *model)
+{
+	unsigned int lp;
+
+	for (lp = 0; lp < model->magazine_size; ++lp)
+		kmem_cache_free(point_cachep, model->magazine[lp]);
+
+	bitmap_free(model->magazine_index);
+	kfree(model->ws);
+	kfree(model->magazine);
+}
+
+/**
+ * tsem model_init() - Initialize the TSEM event point cache.
+ * @model: A pointer to the model that is being initialized.
+ * @size: The number of slots in the event point magazine for the root
+ *	  model.
+ *
+ * This function is called by the primary TSEM initialization function
+ * and sets up the cache that will be used to dispense tsem_event_point
+ * structures for security events that are called in atomic context.
+ *
+ * Return: This function returns a value of zero on success and a negative
+ *	   error code on failure.
+ */
+int __init tsem_model_cache_init(struct tsem_model *model, size_t size)
+{
+	point_cachep = kmem_cache_create("tsem_event_point_cache",
+					 sizeof(struct tsem_event_point), 0,
+					 SLAB_PANIC, 0);
+	if (!point_cachep)
+		return -ENOMEM;
+
+	if (magazine_allocate(model, size)) {
+		kmem_cache_destroy(point_cachep);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
Signature · 1 line
2.39.1
#38 lore raw

PATCH v4 12/14 Implement configuration and methods for default model.

In reply to Greg Wettstein

The implementation of a TSEM model consists of three separate components:

- bypasses array

- event initialization method

- event coefficient mapping method.

Boolean true values in the bypasses array is used to indicate that the event should be completely bypassed.

The event initialization initializes the event description structure using the characteristics of the event.

The event coefficient mapping method is responsible for generating the security event coefficient from the event description structure.

The model0.c file provides the model components for the default deterministic model implemented by the kernel.

---
 security/tsem/model0.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 security/tsem/model0.c
security/tsem/model0.c new file +21 -0
new file mode 100644
index 000000000000..347ed8584842
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+ *
+ * This file contains methods and definitions for the 'model0'
+ * security model implementation.  This is the default model that
+ * TSEM implements.
+ */
+
+#include "tsem.h"
+
+static bool event_bypasses[TSEM_EVENT_CNT];
+
+const struct tsem_context_ops tsem_model0_ops = {
+	.name = "model0",
+	.bypasses = event_bypasses,
+	.init = tsem_event_generate,
+	.map = tsem_map_event
+};
Signature · 1 line
2.39.1
#39 lore raw

PATCH v4 13/14 Implement infrastructure for loadable security models.

In reply to Greg Wettstein

The default deterministic security model can be replaced through the use of models that are implemented with kernel loadable modules. Kernel loadable modules can be used to replace the following components of the TSEM modeling infrastructure:

- event bypasses

- event initialization

- event mapping

An alternate security model is specified for a security modeling namespace by using the following key/value pair as an argument to the creation of the security modeling namespace:

model=MODEL_NAME

Where MODEL_NAME is the name of the model compiled into the kernel loadable model that implements the model.

Befor any model can be used it must be registered through the infrastructure supplied by this commit, namely the nsmgr.c file.

Addition or removal of modules can be locked, either through the TSEM control plame or by the 'tsem_locked' kernel command-line parameter. Locking of the registation process is an irreversible process, once locked the registration/removal process will be locked until the system is rebooted.

---
 security/tsem/nsmgr.c | 255 ++++++++++++++++++++++++++++++++++++++++++
 security/tsem/nsmgr.h |  48 ++++++++
 2 files changed, 303 insertions(+)
 create mode 100644 security/tsem/nsmgr.c
 create mode 100644 security/tsem/nsmgr.h
security/tsem/nsmgr.c new file +255 -0
new file mode 100644
index 000000000000..0b34c18feb01
@@ -0,0 +1,255 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2024 Enjellic Systems Development, LLC
+ * Author: Dr. Greg Wettstein <[email protected]>
+ *
+ * This file contains infrastructure for managing the security models
+ * for TSEM that are implemented by kernel loadable modules. Any model
+ * provided by a kernel loadable module must be registered before it
+ * can be specified for a security modeling namespace.
+ *
+ * The registration of additional models, or the removal of existing
+ * models, can be prevented by 'locking' the registration process.
+ * This locking can be implemented either through the TSEM control
+ * plane or by specifying the 'tsem_locked' kernel command-line
+ * parameter.
+ */
+
+#include "tsem.h"
+#include "nsmgr.h"
+
+static bool locked;
+static bool setup_locked __ro_after_init;
+
+struct model {
+	struct list_head list;
+	const struct tsem_context_ops *ops;
+	struct module *module;
+};
+
+DEFINE_MUTEX(model_list_mutex);
+LIST_HEAD(model_list);
+
+
+static struct model *find_model(const char *name)
+{
+	struct model *found = NULL, *entry = NULL;
+
+	list_for_each_entry(entry, &model_list, list) {
+		if (!strcmp(entry->ops->name, name)) {
+			found = entry;
+			break;
+		}
+	}
+	return found;
+}
+
+/**
+ * tsem_nsmgr_put() - Release a reference to a TSEM security model.
+ *
+ * @ops: A pointer to the tsem_context_ops structure that defines
+ *	 this model.
+ *
+ * This function is the counter part to tsem_nsmgr_get() function.
+ * A check is made to determine if a module is supplying this model and
+ * if so the reference that was taken to the module when the security
+ * namespace is created is released.
+ */
+void tsem_nsmgr_put(const struct tsem_context_ops *ops)
+{
+	struct model *model;
+
+	if (!strcmp(tsem_model0_ops.name, ops->name))
+		return;
+
+	mutex_lock(&model_list_mutex);
+	model = find_model(ops->name);
+	if (!model) {
+		pr_warn("tsem: Attempt to put an unknown model.\n");
+		goto done;
+	}
+	if (model->module)
+		module_put(model->module);
+
+ done:
+	mutex_unlock(&model_list_mutex);
+}
+
+/**
+ * tsem_nsmgr_get() - Obtain a reference to a TSEM security model.
+ *
+ * @name: A null terminated character buffer containing the name of
+ *	  the security model to obtain a reference for.
+ *
+ * This function is used to determine whether or not TSEM has access
+ * to a security model named by the called.  Upon success of locating
+ * the named security model a tsem_context_ops structure is returned
+ * that implements the model.  In addition a reference is taken to
+ * the module in order to prevent its release while a security modeling
+ * namespace is using the model.
+ *
+ * Return: If the named security model is not available a NULL pointer
+ *	   is returned.  If the model is available a pointer to the
+ *	   tsem_context_ops structure that implements the model is
+ *	   returned.
+ */
+const struct tsem_context_ops *tsem_nsmgr_get(const char *name)
+{
+	struct model *model;
+	const struct tsem_context_ops *retn = NULL;
+
+	mutex_lock(&model_list_mutex);
+	model = find_model(name);
+	mutex_unlock(&model_list_mutex);
+
+	if (!model) {
+		if (request_module("%s", name))
+			return NULL;
+
+		mutex_lock(&model_list_mutex);
+		model = find_model(name);
+		mutex_unlock(&model_list_mutex);
+		if (!model)
+			return NULL;
+	}
+
+	if (model && try_module_get(model->module))
+		retn = model->ops;
+	return retn;
+}
+
+/**
+ * tsem_nsmgr_lock() - Lock the TSEM loadable module infrastructure.
+ *
+ * @by_setup: This boolean value is used to signal that the request to
+ *	      lock the model state is being done by the system setup.
+ *
+ * This function disables the registration of additional loadable
+ * modules for model implementation.  It also raises the reference count
+ * for each modules that is loaded to prevent the current modules from
+ * being unloaded.
+ *
+ * Return: This function returns 0 if the modeling infrastructure was
+ *	   locked or a negative value if locking fails.
+ */
+int tsem_nsmgr_lock(const bool by_setup)
+{
+	struct model *entry = NULL;
+
+	if (by_setup) {
+		setup_locked = true;
+		return 0;
+	}
+
+	if (setup_locked)
+		return -EINVAL;
+	if (!capable(CAP_MAC_ADMIN))
+		return -EPERM;
+	if (tsem_context(current)->id)
+		return -EINVAL;
+	if (locked)
+		return -EINVAL;
+	locked = true;
+
+	mutex_lock(&model_list_mutex);
+	list_for_each_entry(entry, &model_list, list) {
+		__module_get(entry->module);
+	}
+	mutex_unlock(&model_list_mutex);
+
+	pr_info("tsem: Model state is now locked.\n");
+	return 0;
+}
+
+/**
+ * tsem_nsmgr_register() - Register a TSEM security namespace model.
+ * @ops:    A pointer to a tsem_context_ops structure that describes
+ *	    the model that will be implemented.
+ * @module: A pointer to the module implementing the security namespace
+ *	    model.
+ *
+ * This function is used by loadable modules to register a security
+ * model that is to be available for use by security modeling namespaces.
+ *
+ * Return: This function returns 0 if the model was successfully registered
+ *	   or a negative error value if registration failed.
+ */
+int tsem_nsmgr_register(const struct tsem_context_ops *ops,
+			struct module *module)
+{
+	int retn = 0;
+	struct model *model = NULL;
+
+	if (setup_locked || locked) {
+		pr_warn("tsem: Attempt to register model in locked state.\n");
+		return -EINVAL;
+	}
+	if (!capable(CAP_MAC_ADMIN))
+		return -EPERM;
+	if (!strcmp(tsem_model0_ops.name, ops->name))
+		return -EINVAL;
+
+	mutex_lock(&model_list_mutex);
+	if (find_model(ops->name)) {
+		pr_warn("tsem: Attempt to insert identical model: %s\n",
+			ops->name);
+		retn = -EEXIST;
+		goto done;
+	}
+
+	model = kzalloc(sizeof(*model), GFP_KERNEL);
+	if (!model) {
+		retn = -ENOMEM;
+		goto done;
+	}
+
+	model->ops = ops;
+	model->module = module;
+	list_add_tail(&model->list, &model_list);
+	pr_info("tsem: Registered model: '%s'\n", ops->name);
+
+ done:
+	mutex_unlock(&model_list_mutex);
+	return retn;
+}
+EXPORT_SYMBOL_GPL(tsem_nsmgr_register);
+
+/**
+ * tsem_nsmgr_release() - Release a TSEM security namespace model.
+ * @name:   A null terminated character buffer containing the name
+ *	    of the model being implemented.
+ *
+ * This function is used to release the use of security modeling
+ * namespace model.
+ *
+ * Return: This function returns 0 if the model was successfully released
+ *	   or a negative error value if release failed.
+ */
+int tsem_nsmgr_release(const struct tsem_context_ops *ops)
+{
+	int retn = 0;
+	struct model *model;
+
+	if (setup_locked || locked) {
+		pr_warn("tsem: Attempt to release model in locked state.\n");
+		return -EINVAL;
+	}
+
+	mutex_lock(&model_list_mutex);
+
+	model = find_model(ops->name);
+	if (!model) {
+		pr_warn("tsem: Model '%s' not found for release.\n",
+			ops->name);
+		retn = -EINVAL;
+		goto done;
+	}
+	list_del(&model->list);
+
+ done:
+	mutex_unlock(&model_list_mutex);
+	pr_info("tsem: Released model: '%s'\n", ops->name);
+	return retn;
+}
+EXPORT_SYMBOL_GPL(tsem_nsmgr_release);
security/tsem/nsmgr.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.39.1
#40 lore raw

PATCH v4 14/14 Activate the configuration and build of the TSEM LSM.

In reply to Greg Wettstein

Complete the implementation by integrating TSEM into the configuration and kernel build infrastructure. This includes registration of TSEM with the LSM infrastructure and the assignment of an LSM identifier number.

---
 include/uapi/linux/lsm.h |  1 +
 security/Kconfig         | 11 ++++++-----
 security/Makefile        |  1 +
 security/security.c      |  3 ++-
 security/tsem/Kconfig    | 36 ++++++++++++++++++++++++++++++++++++
 security/tsem/Makefile   |  6 ++++++
 6 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100644 security/tsem/Kconfig
 create mode 100644 security/tsem/Makefile
include/uapi/linux/lsm.h +1 -0
index 33d8c9f4aa6b..6b63c158c1df 100644
@@ -64,6 +64,7 @@ struct lsm_ctx {
 #define LSM_ID_LANDLOCK		110
 #define LSM_ID_IMA		111
 #define LSM_ID_EVM		112
+#define LSM_ID_TSEM		113
 
 /*
  * LSM_ATTR_XXX definitions identify different LSM attributes
security/Kconfig +6 -5
index 412e76f1575d..a7802eb29034 100644
@@ -192,6 +192,7 @@ source "security/yama/Kconfig"
 source "security/safesetid/Kconfig"
 source "security/lockdown/Kconfig"
 source "security/landlock/Kconfig"
+source "security/tsem/Kconfig"
 
 source "security/integrity/Kconfig"
 
@@ -231,11 +232,11 @@ endchoice
 
 config LSM
 	string "Ordered list of enabled LSMs"
-	default "landlock,lockdown,yama,loadpin,safesetid,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK
-	default "landlock,lockdown,yama,loadpin,safesetid,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR
-	default "landlock,lockdown,yama,loadpin,safesetid,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO
-	default "landlock,lockdown,yama,loadpin,safesetid,bpf" if DEFAULT_SECURITY_DAC
-	default "landlock,lockdown,yama,loadpin,safesetid,selinux,smack,tomoyo,apparmor,bpf"
+	default "tsem,landlock,lockdown,yama,loadpin,safesetid,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK
+	default "tsem,landlock,lockdown,yama,loadpin,safesetid,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR
+	default "tsem,landlock,lockdown,yama,loadpin,safesetid,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO
+	default "tsem,landlock,lockdown,yama,loadpin,safesetid,bpf" if DEFAULT_SECURITY_DAC
+	default "tsem,landlock,lockdown,yama,loadpin,safesetid,selinux,smack,tomoyo,apparmor,bpf"
 	help
 	  A comma-separated list of LSMs, in initialization order.
 	  Any LSMs left off this list, except for those with order
security/Makefile +1 -0
index 59f238490665..1d4e0a698a2d 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM)	+= lockdown/
 obj-$(CONFIG_CGROUPS)			+= device_cgroup.o
 obj-$(CONFIG_BPF_LSM)			+= bpf/
 obj-$(CONFIG_SECURITY_LANDLOCK)		+= landlock/
+obj-$(CONFIG_SECURITY_TSEM)		+= tsem/
 
 # Object integrity file lists
 obj-$(CONFIG_INTEGRITY)			+= integrity/
security/security.c +2 -1
index e5ca08789f74..1dfd85293ad4 100644
@@ -51,7 +51,8 @@
 	(IS_ENABLED(CONFIG_BPF_LSM) ? 1 : 0) + \
 	(IS_ENABLED(CONFIG_SECURITY_LANDLOCK) ? 1 : 0) + \
 	(IS_ENABLED(CONFIG_IMA) ? 1 : 0) + \
-	(IS_ENABLED(CONFIG_EVM) ? 1 : 0))
+	(IS_ENABLED(CONFIG_EVM) ? 1 : 0) + \
+	(IS_ENABLED(CONFIG_SECURITY_TSEM) ? 1 : 0))
 
 /*
  * These are descriptions of the reasons that can be passed to the
security/tsem/Kconfig new file +36 -0
new file mode 100644
index 000000000000..2e9d54eb3acc
@@ -0,0 +1,36 @@
+config SECURITY_TSEM
+	bool "Trusted Security Event Modeling"
+	depends on SECURITY
+	depends on NET && INET
+	select SECURITY_NETWORK
+	select SECURITYFS
+	select CRYPTO
+	select CRYPTO_SHA256
+	select CRYPTO_HASH_INFO
+	select TCG_TPM if HAS_IOMEM && !UML
+	select TCG_TIS if TCG_TPM && X86
+	select TCG_CRB if TCG_TPM && ACPI
+	default n
+	help
+	  This option selects support for Trusted Security Event
+	  Modeling (TSEM).  TSEM implements the ability to model
+	  the security state of either the system at large or in a
+	  restricted namespace on the basis of the LSM security
+	  events and attributes that occur in the scope of the model.
+	  The model may be implemented either in the kernel proper
+	  or exported to an external Trusted Modeling Agent (TMA).
+	  If you are unsure how to answer this question, answer N.
+
+config SECURITY_TSEM_ROOT_MODEL_PCR
+	int "TPM PCR index for root domain"
+	depends on SECURITY_TSEM
+	range 8 14
+	default 11
+	help
+	  This configuration variable determines the TPM Platform
+	  Configuration Register (PCR) that the coefficients of
+	  security events for the root modeling domain are extended
+	  into.  The default value is one register above the default
+	  value that IMA uses for its integrity measurements, in order
+	  to avoid a conflict between the two sub-systems.  If unsure,
+	  leave the value at its default value of 11.
security/tsem/Makefile new file +6 -0
new file mode 100644
index 000000000000..5b26edbe02b0
@@ -0,0 +1,6 @@
+obj-$(CONFIG_SECURITY_TSEM) := tsem.o model.o namespace.o map.o event.o fs.o \
+	export.o trust.o model0.o
+
+ifdef CONFIG_MODULES
+obj-y += nsmgr.o
+endif
Signature · 1 line
2.39.1