@@ -262,3 +262,43 @@ void ipe_audit_policy_load(const struct ipe_policy *const p)
audit_log_end(ab);
}
+
+/**
+ * ipe_audit_enforce: Audit a change in IPE's enforcement state
+ * @ctx: Supplies a pointer to the contexts whose state changed.
+ */
+void ipe_audit_enforce(const struct ipe_context *const ctx)
+{
+ struct audit_buffer *ab;
+ bool enforcing = false;
+
+ ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_TRUST_STATUS);
+ if (!ab)
+ return;
+
+ rcu_read_lock();
+ enforcing = READ_ONCE(ctx->enforce);
+ rcu_read_unlock();
+
+ audit_log_format(ab, "IPE enforce=%d", enforcing);
+
+ audit_log_end(ab);
+}
+
+/**
+ * emit_enforcement: Emit the enforcement state of IPE started with.
+ *
+ * Return:
+ * 0 - Always
+ */
+static int emit_enforcement(void)
+{
+ struct ipe_context *ctx = NULL;
+
+ ctx = ipe_current_ctx();
+ ipe_audit_enforce(ctx);
+ ipe_put_ctx(ctx);
+ return 0;
+}
+
+late_initcall(emit_enforcement);
@@ -16,6 +16,7 @@ void ipe_audit_match(const struct ipe_eval_ctx *const ctx,
bool enforce);
void ipe_audit_policy_load(const struct ipe_policy *const p);
void ipe_audit_policy_activation(const struct ipe_policy *const p);
+void ipe_audit_enforce(const struct ipe_context *const ctx);
#else
static inline void ipe_audit_match(const struct ipe_eval_ctx *const ctx,
enum ipe_match match_type,
@@ -31,6 +32,10 @@ static inline void ipe_audit_policy_load(const struct ipe_policy *const p)
static inline void ipe_audit_policy_activation(const struct ipe_policy *const p)
{
}
+
+static inline void ipe_audit_enforce(const struct ipe_context *const ctx)
+{
+}
#endif /* CONFIG_AUDIT */
#endif /* IPE_AUDIT_H */
@@ -16,6 +16,7 @@
#include <linux/moduleparam.h>
static bool success_audit;
+static bool enforce = true;
/**
* ver_to_u64: convert an internal ipe_policy_version to a u64
@@ -151,6 +152,7 @@ static struct ipe_context *create_ctx(void)
INIT_LIST_HEAD(&ctx->policies);
refcount_set(&ctx->refcount, 1);
spin_lock_init(&ctx->lock);
+ WRITE_ONCE(ctx->enforce, true);
return ctx;
@@ -337,6 +339,7 @@ int __init ipe_init_ctx(void)
spin_lock(&lns->lock);
WRITE_ONCE(lns->success_audit, success_audit);
+ WRITE_ONCE(lns->enforce, enforce);
spin_unlock(&lns->lock);
rcu_assign_pointer(*ipe_tsk_ctx(current), lns);
@@ -355,3 +358,6 @@ int __init ipe_init_ctx(void)
module_param(success_audit, bool, 0400);
MODULE_PARM_DESC(success_audit, "Start IPE with success auditing enabled");
+
+module_param(enforce, bool, 0400);
+MODULE_PARM_DESC(enforce, "Start IPE in enforce or permissive mode");
@@ -14,6 +14,7 @@
struct ipe_context {
struct ipe_policy __rcu *active_policy;
+ bool __rcu enforce;
bool __rcu success_audit;
@@ -86,6 +86,10 @@ static int evaluate(const struct ipe_eval_ctx *const ctx)
if (!pol)
goto out;
+ rcu_read_lock();
+ enforcing = READ_ONCE(ctx->ci_ctx->enforce);
+ rcu_read_unlock();
+
rules = &pol->parsed->rules[ctx->op];
list_for_each_entry(rule, &rules->rules, next) {
@@ -114,6 +118,8 @@ static int evaluate(const struct ipe_eval_ctx *const ctx)
if (action == ipe_action_deny)
rc = -EACCES;
+ if (!enforcing)
+ rc = 0;
out:
ipe_put_policy(pol);
return rc;
@@ -13,8 +13,73 @@
static struct dentry *np __ro_after_init;
static struct dentry *root __ro_after_init;
static struct dentry *config __ro_after_init;
+static struct dentry *enforce __ro_after_init;
static struct dentry *success_audit __ro_after_init;
+/**
+ * setenforce: Write handler for the securityfs node, "ipe/enforce"
+ * @f: Supplies a file structure representing the securityfs node.
+ * @data: Supplies a buffer passed to the write syscall
+ * @len: Supplies the length of @data
+ * @offset: unused.
+ *
+ * Return:
+ * >0 - Success, Length of buffer written
+ * <0 - Error
+ */
+static ssize_t setenforce(struct file *f, const char __user *data,
+ size_t len, loff_t *offset)
+{
+ int rc = 0;
+ bool value;
+ struct ipe_context *ctx;
+
+ if (!file_ns_capable(f, &init_user_ns, CAP_MAC_ADMIN))
+ return -EPERM;
+
+ rc = kstrtobool_from_user(data, len, &value);
+ if (rc)
+ return rc;
+
+ ctx = ipe_current_ctx();
+
+ spin_lock(&ctx->lock);
+ WRITE_ONCE(ctx->enforce, value);
+ spin_unlock(&ctx->lock);
+ synchronize_rcu();
+
+ ipe_audit_enforce(ctx);
+ ipe_put_ctx(ctx);
+ return len;
+}
+
+/**
+ * getenforce: Read handler for the securityfs node, "ipe/enforce"
+ * @f: Supplies a file structure representing the securityfs node.
+ * @data: Supplies a buffer passed to the read syscall
+ * @len: Supplies the length of @data
+ * @offset: unused.
+ *
+ * Return:
+ * >0 - Success, Length of buffer written
+ * <0 - Error
+ */
+static ssize_t getenforce(struct file *f, char __user *data,
+ size_t len, loff_t *offset)
+{
+ const char *result;
+ struct ipe_context *ctx;
+
+ ctx = ipe_current_ctx();
+
+ rcu_read_lock();
+ result = ((READ_ONCE(ctx->enforce)) ? "1" : "0");
+ rcu_read_unlock();
+
+ ipe_put_ctx(ctx);
+ return simple_read_from_buffer(data, len, offset, result, 2);
+}
+
/**
* setaudit: Write handler for the securityfs node, "ipe/success_audit"
* @f: Supplies a file structure representing the securityfs node.
@@ -185,6 +250,11 @@ static const struct file_operations np_fops = {
.write = new_policy,
};
+static const struct file_operations enforce_fops = {
+ .write = setenforce,
+ .read = getenforce,
+};
+
static const struct file_operations audit_fops = {
.write = setaudit,
.read = getaudit,
@@ -230,6 +300,13 @@ static int __init ipe_init_securityfs(void)
goto err;
}
+ enforce = securityfs_create_file("enforce", 0600, root, NULL,
+ &enforce_fops);
+ if (IS_ERR(enforce)) {
+ rc = PTR_ERR(enforce);
+ goto err;
+ }
+
ctx->policy_root = securityfs_create_dir("policies", root);
if (IS_ERR(ctx->policy_root)) {
rc = PTR_ERR(ctx->policy_root);
@@ -241,6 +318,7 @@ static int __init ipe_init_securityfs(void)
securityfs_remove(np);
securityfs_remove(root);
securityfs_remove(config);
+ securityfs_remove(enforce);
securityfs_remove(success_audit);
securityfs_remove(ctx->policy_root);
return rc;