Skip to contentSkip to conversation

lsm: Add Rust bindings with example LSM

RFC PATCH In review Lukas Fischer 2 participants 4 messages Apr 16, 2025 – Apr 22, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 lsm: Add Rust bindings with example LSM

These are the bare necessities to implement an LSM in Rust. They are in an early WIP state intended to gather feedback, mainly for the use of unsafe.

The LSM is implemented in security/rust_lsm/lsm.rs and uses the bindings to the C side directly to set itself up. This is of course not production ready, but enough to set up a minimal example of an LSM.

The lsm_info struct technically needs to be aligned to size_of::<kernel::ffi::c_ulong>(), but Rust does not allow this in combination with repr(transparent). So far this works, but it seems brittle.

To limit the scope of the implementation, the example and bindings only cover the file_permission hook.

---
As part of my master's thesis, I am developing a Rust-based LSM. These are the bindings I have created for that, so that I can register and implement an LSM in Rust.

At the moment, the bindings are a pretty direct C to Rust translation and only allow registering a single LSM. Because of this, they are part of the example LSM code and not the kernel crate. During my thesis I will unfortunately not have the time to polish them enough to be upstreamable, and since there is likely no user for them, upstreaming seems unlikely in the first place.

To me it seems like Rust bindings usually involve a trait or struct defined for the abstraction, so that the lifetime of the struct can be tied to the data/object/hardware it represents (please correct me on this). For LSMs, which are loaded once duing boot and cannot be unloaded, this seems not as relevent to me, but for consistency the final bindings should probably look similar to other existing ones.

The intent of sending the bindings is to gather feedback on the current implementation, mainly whether my use of unsafe is correct. Kind of as a review, but more as a sanity check.

I have previously asked about Rust-based LSMs and about this RFC in the Rust for Linux Zulip [1][2].

Thanks,
Lukas

[1]: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Rust-based.20LSMs
[2]: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/.22Review.22.20of.20LSM.20bindings

---
 include/linux/lsm_count.h         |   9 +-
 include/uapi/linux/lsm.h          |   1 +
 rust/bindings/bindings_helper.h   |   1 +
 scripts/generate_rust_analyzer.py |   2 +-
 security/Kconfig                  |   1 +
 security/Makefile                 |   1 +
 security/rust_lsm/Kconfig         |  10 ++
 security/rust_lsm/Makefile        |   4 +
 security/rust_lsm/bindings.rs     | 161 ++++++++++++++++++++++++++++++
 security/rust_lsm/lsm.rs          |  33 ++++++
 10 files changed, 221 insertions(+), 2 deletions(-)
 create mode 100644 security/rust_lsm/Kconfig
 create mode 100644 security/rust_lsm/Makefile
 create mode 100644 security/rust_lsm/bindings.rs
 create mode 100644 security/rust_lsm/lsm.rs
include/linux/lsm_count.h +8 -1
index 16eb49761b25..e6cc2f6d6f0c 100644
@@ -102,6 +102,12 @@
 #define IPE_ENABLED
 #endif
 
+#if IS_ENABLED(CONFIG_SECURITY_RUST_LSM)
+#define RUST_LSM_ENABLED 1,
+#else
+#define RUST_LSM_ENABLED
+#endif
+
 /*
  *  There is a trailing comma that we need to be accounted for. This is done by
  *  using a skipped argument in __COUNT_LSMS
@@ -124,7 +130,8 @@
 		LANDLOCK_ENABLED	\
 		IMA_ENABLED		\
 		EVM_ENABLED		\
-		IPE_ENABLED)
+		IPE_ENABLED		\
+		RUST_LSM_ENABLED)
 
 #else
 
include/uapi/linux/lsm.h +1 -0
index 938593dfd5da..938f7694e6ed 100644
@@ -65,6 +65,7 @@ struct lsm_ctx {
 #define LSM_ID_IMA		111
 #define LSM_ID_EVM		112
 #define LSM_ID_IPE		113
+#define LSM_ID_RUST_LSM		114
 
 /*
  * LSM_ATTR_XXX definitions identify different LSM attributes
rust/bindings/bindings_helper.h +1 -0
index ab37e1d35c70..f941a1caf141 100644
@@ -21,6 +21,7 @@
 #include <linux/fs.h>
 #include <linux/jiffies.h>
 #include <linux/jump_label.h>
+#include <linux/lsm_hooks.h>
 #include <linux/mdio.h>
 #include <linux/miscdevice.h>
 #include <linux/of_device.h>
scripts/generate_rust_analyzer.py +1 -1
index cd41bc906fbd..204bc144a0ab 100755
@@ -144,7 +144,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
     # Then, the rest outside of `rust/`.
     #
     # We explicitly mention the top-level folders we want to cover.
-    extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
+    extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers", "security"))
     if external_src is not None:
         extra_dirs = [external_src]
     for folder in extra_dirs:
security/Kconfig +1 -0
index 4816fc74f81e..7f8707e7f80b 100644
@@ -230,6 +230,7 @@ source "security/safesetid/Kconfig"
 source "security/lockdown/Kconfig"
 source "security/landlock/Kconfig"
 source "security/ipe/Kconfig"
+source "security/rust_lsm/Kconfig"
 
 source "security/integrity/Kconfig"
 
security/Makefile +1 -0
index 22ff4c8bd8ce..ba482f051511 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_CGROUPS)			+= device_cgroup.o
 obj-$(CONFIG_BPF_LSM)			+= bpf/
 obj-$(CONFIG_SECURITY_LANDLOCK)		+= landlock/
 obj-$(CONFIG_SECURITY_IPE)		+= ipe/
+obj-$(CONFIG_SECURITY_RUST_LSM)		+= rust_lsm/
 
 # Object integrity file lists
 obj-$(CONFIG_INTEGRITY)			+= integrity/
security/rust_lsm/Kconfig new file +10 -0
new file mode 100644
index 000000000000..d3e8bc2907f2
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config SECURITY_RUST_LSM
+	bool "Rust example LSM Support"
+	depends on RUST && SECURITY
+	default n
+	help
+	  Enable the Rust example LSM.
+
+	  If you are unsure how to answer this question, answer N.
security/rust_lsm/Makefile new file +4 -0
new file mode 100644
index 000000000000..14b651a82ca6
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+ccflags-y += -I$(src)			# needed for trace events
+
+obj-$(CONFIG_SECURITY_RUST_LSM)		+= lsm.o
security/rust_lsm/bindings.rs new file +161 -0

161 lines not on this page · raw message

security/rust_lsm/lsm.rs new file +33 -0
new file mode 100644
index 000000000000..9ac777d5a168
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust example LSM.
+//!
+//! Rust-based example LSM to demonstrate an initial version of bindings to C.
+
+mod bindings;
+
+use kernel::{fs::LocalFile, prelude::*};
+
+/// Prefix to appear before log messages printed from within this crate.
+const __LOG_PREFIX: &[u8] = b"rust_lsm\0";
+
+/// LSM init function.
+///
+/// Called exactly once from `init()` in [`bindings`] when the LSM is registered.
+fn init() -> Result {
+    // Custom init logic could go here
+    Ok(())
+}
+
+/// `file_permission` hook implementation.
+///
+/// Gets called for every file access that needs to be checked.
+fn file_permission(_file: &LocalFile, _mask: i32) -> Result<bool> {
+    // Arbitrary policy logic could go here
+    let resolution = true;
+
+    // Uncomment this for lots of spam in dmesg
+    // pr_info!("file_permission hook called");
+
+    Ok(resolution)
+}

base-commit: 8ffd015db85fea3e15a77027fda6c02ced4d2444

Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 14 lines · Lukas Fischer

These are the bare necessities to implement an LSM in Rust. They are in an early WIP state intended to gather feedback, mainly for the use of unsafe.

The LSM is implemented in security/rust_lsm/lsm.rs and uses the bindings to the C side directly to set itself up. This is of course not production ready, but enough to set up a minimal example of an LSM.

The lsm_info struct technically needs to be aligned to size_of::<kernel::ffi::c_ulong>(), but Rust does not allow this in combination with repr(transparent). So far this works, but it seems brittle.

Quoted 8 lines

To limit the scope of the implementation, the example and bindings only cover the file_permission hook.

---
As part of my master's thesis, I am developing a Rust-based LSM. These are the bindings I have created for that, so that I can register and implement an LSM in Rust.

Thanks for sharing this Lukas. My Rust knowledge is still far too basic to offer any constructive review of the Rust code, but I'm happy to see some effort being put into looking at what would be required to support a LSM written in Rust.

It isn't clear to me if this is simply an exercise in seeing what Rust/C interfaces would be needed to implement a Rust based LSM, or if you ultimately have a LSM you would like to submit upstream and this is the necessary groundwork so you can implement it in Rust. Unless it is the latter, I'm not sure this is something that is a candidate for merging into the upstream Linux kernel as we don't merge "demo" type LSMs. If you are intending to develop a proper LSM, we do have some guidelines that may help explain what is expected:

* https://github.com/LinuxSecurityModule/kernel/blob/main/README.md

Signature · 1 line
paul-moore.com
#2 lore raw
Quoted 4 lines · Paul Moore

Thanks for sharing this Lukas. My Rust knowledge is still far too basic to offer any constructive review of the Rust code, but I'm happy to see some effort being put into looking at what would be required to support a LSM written in Rust.

Hi Paul,

that's alright, I was mainly asking the Rust for Linux contributors for feedback on that, but I wanted to keep you in the loop as well.

Quoted 10 lines

It isn't clear to me if this is simply an exercise in seeing what Rust/C interfaces would be needed to implement a Rust based LSM, or if you ultimately have a LSM you would like to submit upstream and this is the necessary groundwork so you can implement it in Rust. Unless it is the latter, I'm not sure this is something that is a candidate for merging into the upstream Linux kernel as we don't merge "demo" type LSMs. If you are intending to develop a proper LSM, we do have some guidelines that may help explain what is expected:

* https://github.com/LinuxSecurityModule/kernel/blob/main/README.md

thanks for the feedback, I guess I was missing some context in the initial mail. The LSM I'm using it for in my thesis is more of a research testbed (or "demo"), so I never intended to upstream that. Since I still needed to create bindings to implement that in Rust, I figured I would post them to the lists to get some feedback and to get things started in case someone wants to implement an actual upstreamed LSM in Rust in the future. This is why I marked this "RFC PATCH", it is not intended for upstreaming, only for feedback.

If there is interest in it, I might polish the bindings after the thesis, so that they can be properly used for an actual LSM. In the state they are currently in they do allow writing an LSM in Rust, but not in a way a safe Rust abstraction should.

Thanks,
Lukas

maintainer
#3 lore raw
Quoted 29 lines · Lukas Fischer
Quoted 4 lines · Paul Moore

Thanks for sharing this Lukas. My Rust knowledge is still far too basic to offer any constructive review of the Rust code, but I'm happy to see some effort being put into looking at what would be required to support a LSM written in Rust.

Hi Paul,

that's alright, I was mainly asking the Rust for Linux contributors for feedback on that, but I wanted to keep you in the loop as well.

Quoted 10 lines

It isn't clear to me if this is simply an exercise in seeing what Rust/C interfaces would be needed to implement a Rust based LSM, or if you ultimately have a LSM you would like to submit upstream and this is the necessary groundwork so you can implement it in Rust. Unless it is the latter, I'm not sure this is something that is a candidate for merging into the upstream Linux kernel as we don't merge "demo" type LSMs. If you are intending to develop a proper LSM, we do have some guidelines that may help explain what is expected:

* https://github.com/LinuxSecurityModule/kernel/blob/main/README.md

thanks for the feedback, I guess I was missing some context in the initial mail. The LSM I'm using it for in my thesis is more of a research testbed (or "demo"), so I never intended to upstream that. Since I still needed to create bindings to implement that in Rust, I figured I would post them to the lists to get some feedback and to get things started in case someone wants to implement an actual upstreamed LSM in Rust in the future. This is why I marked this "RFC PATCH", it is not intended for upstreaming, only for feedback.

Quoted 4 lines

If there is interest in it, I might polish the bindings after the thesis, so that they can be properly used for an actual LSM. In the state they are currently in they do allow writing an LSM in Rust, but not in a way a safe Rust abstraction should.

Thanks for the background, that's helpful. If anything changes and you decide that you do want to implement a proper LSM in Rust and propose it for upstream inclusion please let me know.

Signature · 1 line
paul-moore.com