From: Simon THOBY <git@nightmared.fr>
To: linux-security-module@vger.kernel.org
Cc: Simon THOBY <git@nightmared.fr>,
linux-integrity@vger.kernel.org, linux-doc@vger.kernel.org
Subject: [RFC PATCH 5/9] Loadpol LSM: add a sysctl to lock the policy
Date: Wed, 21 May 2025 16:01:09 +0200 [thread overview]
Message-ID: <20250521140121.591482-6-git@nightmared.fr> (raw)
In-Reply-To: <20250521140121.591482-1-git@nightmared.fr>
Once the policy is properly configurd, users may want to lock that
policy to ensure no future change can be applied to it.
Add a sysctl that can be toggled to lock the policy.
Signed-off-by: Simon THOBY <git@nightmared.fr>
---
security/loadpol/loadpol_fs.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/security/loadpol/loadpol_fs.c b/security/loadpol/loadpol_fs.c
index 9134d11718a0..1fec94de9f40 100644
--- a/security/loadpol/loadpol_fs.c
+++ b/security/loadpol/loadpol_fs.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only
#include "linux/array_size.h"
+#include <linux/sysctl.h>
#include <linux/security.h>
#include "loadpol.h"
@@ -8,8 +9,22 @@
static struct dentry *securityfs_dir;
static struct dentry *securityfs_policy;
+static bool policy_locked;
static DEFINE_MUTEX(policy_write_mutex);
+static const struct ctl_table sysctls[] = {
+ {
+ .procname = "locked",
+ .data = &policy_locked,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ /* only allow a transition from 0 (not locked) to 1 (locked) */
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_ONE,
+ .extra2 = SYSCTL_ONE,
+ },
+};
+
static const struct seq_operations loadpol_policy_seqops = {
.start = loadpol_policy_start,
.next = loadpol_policy_next,
@@ -33,6 +48,13 @@ static ssize_t loadpol_write_policy(struct file *file, const char __user *buf,
char *data;
ssize_t ret;
+ /* Once the policy is locked, modifications are blocked */
+ if (policy_locked) {
+ pr_warn("Loadpol is locked, the policy cannot be modified");
+ ret = -EPERM;
+ goto out;
+ }
+
/*
* arbitrary size limit (to prevent a DoS but still allow loading a policy with a few
* thousands of entries)
@@ -81,8 +103,15 @@ static const struct file_operations loadpol_policy_ops = {
static int __init loadpol_init_fs(void)
{
+ struct ctl_table_header *sysctl_hdr = NULL;
int ret;
+ sysctl_hdr = register_sysctl_sz("security/" LOADPOL_NAME, sysctls, ARRAY_SIZE(sysctls));
+ if (IS_ERR(sysctl_hdr)) {
+ ret = PTR_ERR(sysctl_hdr);
+ goto err;
+ }
+
securityfs_dir = securityfs_create_dir(LOADPOL_NAME, NULL);
if (IS_ERR(securityfs_dir)) {
ret = PTR_ERR(securityfs_dir);
@@ -99,6 +128,8 @@ static int __init loadpol_init_fs(void)
return 0;
err:
+ if (!IS_ERR(sysctl_hdr))
+ unregister_sysctl_table(sysctl_hdr);
securityfs_remove(securityfs_policy);
securityfs_remove(securityfs_dir);
return ret;
--
2.49.0
next prev parent reply other threads:[~2025-05-21 14:01 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-21 14:01 [RFC PATCH 0/9] Introducing the Loadpol LSM Simon THOBY
2025-05-21 14:01 ` [RFC PATCH 1/9] LSM: Introduce a new hook: security_kernel_module_load Simon THOBY
2025-05-21 22:03 ` Serge E. Hallyn
2025-05-22 8:57 ` Simon Thoby
2025-05-21 14:01 ` [RFC PATCH 2/9] Introduce a new LSM: loadpol Simon THOBY
2025-05-21 14:01 ` [RFC PATCH 3/9] Loadpol LSM: filter kernel module request according to the policy Simon THOBY
2025-05-21 15:47 ` Casey Schaufler
2025-05-21 16:21 ` Randy Dunlap
2025-05-21 16:26 ` Simon Thoby
2025-05-21 14:01 ` [RFC PATCH 4/9] Loadpol LSM: add a file in securityfs to read/modify " Simon THOBY
2025-05-21 14:01 ` Simon THOBY [this message]
2025-05-21 14:01 ` [RFC PATCH 6/9] Loadpol LSM: emit an audit log Simon THOBY
2025-05-21 14:01 ` [RFC PATCH 7/9] module: expose the list of blacklisted modules Simon THOBY
2025-05-21 14:01 ` [RFC PATCH 8/9] Loadpol LSM: include the blacklisted kernel modules in the policy Simon THOBY
2025-05-21 14:01 ` [RFC PATCH 9/9] Loadpol LSM: add a minimal documentation Simon THOBY
2025-05-21 16:26 ` Randy Dunlap
2025-05-21 16:29 ` Simon Thoby
2025-05-21 21:31 ` Paul Moore
2025-05-22 9:23 ` Simon Thoby
2025-05-29 23:49 ` Paul Moore
2025-05-30 7:03 ` Simon Thoby
2025-05-30 14:59 ` Paul Moore
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250521140121.591482-6-git@nightmared.fr \
--to=git@nightmared.fr \
--cc=linux-doc@vger.kernel.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).