Support writing grub modules in Rust
cover Support writing grub modules in Rust
grub is a trusted component of the secure boot process, including "traditional" GPG-based secure boot, UEFI-based secure boot, and the WIP secure boot process using appended signatures. Most of grub is written in C and has suffered from a number of memory-unsafety issues in the past, something about which many of us have very distinct and unpleasant memories!
Rust is a systems programming language suitable for low-level code. Rust can provide strong compile-time guarantees about memory safety. It also is reasonably easy for Rust code to interoperate with C.
Grub's modular design may work in our favour here. Potentially vulnerable components such as image and file-system parsers are written as individual modules. Can we progressively rewrite these modules in a safer language?
This patch set provides my efforts so far in making this possible.
It allows a module written almost entirely in Rust to be built and run (in qemu) on x86_64-{emu,efi}, arm64-efi and powerpc-ieee1275.
It still needs work, but it's at the stage where it's not entirely trivial, and where I want to start getting more feedback on it.
There are a few comments on things that need to be changed further on in the series, but I think the biggest overarching change we probably want to move towards doing more of what the Rust for Linux (kernel) project has done: drop Cargo and build everything (except tests) directly with rustc. This is a pain because Cargo is magic, but it will resolve some hairy issues.
(For example, currently on x86_64 platforms you can't have multiple Rust modules because each defines a __rust_probestack intrinsic despite my attempts to disable it. IMO that's a rustc bug and I have reported it. But, there are other intrinsics that Rust code could validly call, and we don't want implementations linked into each module, we want the modules to call into the kernel. This is - as far as I can tell - impossible to do with Cargo builds; we need finer control.)
Another big issue where we want to follow the Rust for Linux project is how to handle failing allocations: by default in Rust that kills your program (!), a situation which is entirely unacceptable for the linux kernel. It's also unacceptable in Grub, so we should figure out how they avoid it and learn from them.
I have proposed a Linux Plumbers talk in the systems boot stream to discuss this further. I'd also welcome on-list discussions.
(Now, back to addressing the review comments on my previous patch series!)
Daniel Axtens (7):
emu: support grub_memalign
Rust: module build infrastructure
Rust: add a slightly more idiomatic wrapper around command handling
Rust: add the rust_hello module
powerpc: Support Rust
x86_64-efi: Support Rust
arm64-efi: Support Rust
.gitignore | 1 + Makefile.util.def | 6 ++ conf/Makefile.common | 1 + configure.ac | 71 +++++++++++++++++++ gentpl.py | 28 +++++++- grub-core/Makefile.am | 20 ++++++ grub-core/Makefile.core.def | 9 +++ grub-core/commands/rust-hello/.gitignore | 1 + grub-core/commands/rust-hello/Cargo.lock | 24 +++++++ grub-core/commands/rust-hello/Cargo.toml | 16 +++++ grub-core/commands/rust-hello/src/lib.rs | 57 +++++++++++++++ grub-core/commands/rust-hello/wrap.c | 8 +++ grub-core/kern/emu/mm.c | 6 ++ grub-core/lib/rust/bindings.h | 4 ++ grub-core/lib/rust/conftest/Cargo.lock | 7 ++ grub-core/lib/rust/conftest/Cargo.toml | 10 +++ grub-core/lib/rust/conftest/src/lib.rs | 10 +++ grub-core/lib/rust/grub/.gitignore | 1 + grub-core/lib/rust/grub/Cargo.toml | 8 +++ grub-core/lib/rust/grub/src/command.rs | 50 +++++++++++++ grub-core/lib/rust/grub/src/lib.rs | 64 +++++++++++++++++ grub-core/lib/rust/targets/arm64-efi.json | 27 +++++++ .../lib/rust/targets/powerpc-ieee1275.json | 28 ++++++++ grub-core/lib/rust/targets/x86_64-efi.json | 27 +++++++ grub-core/lib/rust/targets/x86_64-emu.json | 27 +++++++ include/grub/dl.h | 21 +++++- include/grub/mm.h | 2 - tests/test_rust.in | 19 +++++ 28 files changed, 547 insertions(+), 6 deletions(-) create mode 100644 grub-core/commands/rust-hello/.gitignore create mode 100644 grub-core/commands/rust-hello/Cargo.lock create mode 100644 grub-core/commands/rust-hello/Cargo.toml create mode 100644 grub-core/commands/rust-hello/src/lib.rs create mode 100644 grub-core/commands/rust-hello/wrap.c create mode 100644 grub-core/lib/rust/bindings.h create mode 100644 grub-core/lib/rust/conftest/Cargo.lock create mode 100644 grub-core/lib/rust/conftest/Cargo.toml create mode 100644 grub-core/lib/rust/conftest/src/lib.rs create mode 100644 grub-core/lib/rust/grub/.gitignore create mode 100644 grub-core/lib/rust/grub/Cargo.toml create mode 100644 grub-core/lib/rust/grub/src/command.rs create mode 100644 grub-core/lib/rust/grub/src/lib.rs create mode 100644 grub-core/lib/rust/targets/arm64-efi.json create mode 100644 grub-core/lib/rust/targets/powerpc-ieee1275.json create mode 100644 grub-core/lib/rust/targets/x86_64-efi.json create mode 100644 grub-core/lib/rust/targets/x86_64-emu.json create mode 100644 tests/test_rust.in
Signature · 1 line
2.30.2
RFC PATCH 1/7 emu: support grub_memalign
This is trivial to do and simplifies Rust support enormously.
--- grub-core/kern/emu/mm.c | 6 ++++++ include/grub/mm.h | 2 -- 2 files changed, 6 insertions(+), 2 deletions(-)
grub-core/kern/emu/mm.c +6 -0
@@ -73,3 +73,9 @@ grub_realloc (void *ptr, grub_size_t size)grub_error (GRUB_ERR_OUT_OF_MEMORY ,N_ ("out of memory" ));return ret; } + +void * +grub_memalign (grub_size_t align,grub_size_t size) +{ +return aligned_alloc (align, size); +}
include/grub/mm.h +0 -2
@@ -34,9 +34,7 @@ void *EXPORT_FUNC(grub_malloc) (grub_size_t size);void *EXPORT_FUNC (grub_zalloc ) (grub_size_t size);void EXPORT_FUNC (grub_free ) (void *ptr);void *EXPORT_FUNC (grub_realloc ) (void *ptr,grub_size_t size); -#ifndef GRUB_MACHINE_EMU void *EXPORT_FUNC (grub_memalign ) (grub_size_t align,grub_size_t size); -#endif void grub_mm_check_real (const char *file,int line);#define grub_mm_check () grub_mm_check_real (GRUB_FILE, __LINE__);
Signature · 1 line
2.30.2
RFC PATCH 2/7 Rust: module build infrastructure
In reply to Daniel AxtensMake the grub build infrastructure able to detect a Rust toolchain on a supported platform and build modules partially written in Rust.
In order to use this you will need:
- to be building for x86_64-emu (more platforms added later)
- to have a nightly rust toolchain:
rustup toolchain install nightly
- to install the Rust standard library sources:
rustup component add --toolchain nightly rust-src
- bindgen
I'm fairly sure I installed this from my distro.
Key components
==============
configure.ac
------------
configure will check for the presence of rustc, cargo and bindgen.
configure will verify that it can build a minimal freestanding static library for the target.
Teach configure:
--disable-rust / --enable-rust: force Rust support off/on
--enable-rust-debug: build rust modules in debug mode rather than release
mode
Introduce COND_RUST to mark when rust is available.
gentpl.py
---------
Teach gentpl.py about some rust keywords for building modules. (These are demonstrated later on in the series.)
grub-core/lib/rust
------------------
This comprises 3 parts: bindings, a core support 'library', and a target definition.
1. Bindings
Teach the build system how to make Rust bindings of the C funtions with bindgen, and how to clean up after Rust builds.
2. Core support
Provide a basic Rust interface for grub (lib.rs). Rust code is built in the freestanding/embedded mode, so we don't have access to the standard library ('std'). We do provide rust modules with access to 'core' and 'alloc', giving them core features and dynamic memory. To do this we need to supply 2 helpers:
- an allocator: teach Rust to call grub_memalign()/grub_free() Currently failed Rust allocations will panic; this is definitely something I want to fix, as the Linux kernel rust project has done.
- panic: call grub_fatal when a rust panic occurs.
In future I want to print a bit more info, but so far I haven't actually caused a panic yet.
3. Target
Provide a description of x86_64-emu as a Rust target.json file. This is based on the autogenerated target.json file, with some changes to represent the compile flags used in the rest of grub (no mmx or sse, soft-float, mcmodel=large, no redzone) and some general assumptions grub makes (specifically that it runs singlethreaded).
This doesn't actually make anything with Rust, but it lays the foundations.
--- conf/Makefile.common | 1 + configure.ac | 71 ++++++++++++++++++++++ gentpl.py | 28 ++++++++- grub-core/Makefile.am | 20 ++++++ grub-core/lib/rust/bindings.h | 4 ++ grub-core/lib/rust/conftest/Cargo.lock | 7 +++ grub-core/lib/rust/conftest/Cargo.toml | 10 +++ grub-core/lib/rust/conftest/src/lib.rs | 10 +++ grub-core/lib/rust/grub/.gitignore | 1 + grub-core/lib/rust/grub/Cargo.toml | 8 +++ grub-core/lib/rust/grub/src/lib.rs | 63 +++++++++++++++++++ grub-core/lib/rust/targets/x86_64-emu.json | 27 ++++++++ include/grub/dl.h | 21 ++++++- 13 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 grub-core/lib/rust/bindings.h create mode 100644 grub-core/lib/rust/conftest/Cargo.lock create mode 100644 grub-core/lib/rust/conftest/Cargo.toml create mode 100644 grub-core/lib/rust/conftest/src/lib.rs create mode 100644 grub-core/lib/rust/grub/.gitignore create mode 100644 grub-core/lib/rust/grub/Cargo.toml create mode 100644 grub-core/lib/rust/grub/src/lib.rs create mode 100644 grub-core/lib/rust/targets/x86_64-emu.json
conf/Makefile.common +1 -0
@@ -125,6 +125,7 @@ TESTS = EXTRA_DIST = CLEANFILES = BUILT_SOURCES = +RUST_BUILDDIRS = # Rules for Automake input
configure.ac +71 -0
106 lines not on this page · raw message
gentpl.py +25 -3
@@ -490,6 +490,9 @@ def set_canonical_name_suffix(suffix): def cname(defn): return canonical_name_re.sub('_', defn['name'] + canonical_name_suffix) +def cratelibname(defn): + return canonical_name_re.sub('_', "lib" + defn['name']) + ".a" + def rule(target, source, cmd): if cmd[0] == "\n": output("\n" + target + ": " + source + cmd.replace("\n", "\n\t") + "\n")
@@ -629,6 +632,9 @@ def platform_values(defn, platform, suffix): def extra_dist(defn): return foreach_value(defn, "extra_dist", lambda value: value + " ") +def rust_sources(defn): + return foreach_value(defn, "rust", lambda value: value + " ") + def platform_sources(defn, p): return platform_values(defn, p, "") def platform_nodist_sources(defn, p): return platform_values(defn, p, "_nodist")
@@ -682,14 +688,30 @@ def module(defn, platform): gvar_add("MODULE_FILES", name + ".module$(EXEEXT)") var_set(cname(defn) + "_SOURCES", platform_sources(defn, platform) + " ## platform sources") - var_set("nodist_" + cname(defn) + "_SOURCES", platform_nodist_sources(defn, platform) + " ## platform nodist sources") - var_set(cname(defn) + "_LDADD", platform_ldadd(defn, platform)) + var_set(cname(defn) + "_RUSTSOURCES", rust_sources(defn)) var_set(cname(defn) + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_MODULE) " + platform_cflags(defn, platform)) var_set(cname(defn) + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_MODULE) " + platform_ldflags(defn, platform)) var_set(cname(defn) + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_MODULE) " + platform_cppflags(defn, platform)) var_set(cname(defn) + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_MODULE) " + platform_ccasflags(defn, platform)) var_set(cname(defn) + "_DEPENDENCIES", "$(TARGET_OBJ2ELF) " + platform_dependencies(defn, platform)) + if 'crate' in defn: + rustlib = defn['crate'] + "/target/$(rust_target)/$(RUST_TARGET_SUBDIR)/" + cratelibname(defn) + output(""" +""" + rustlib + ": $(" + cname(defn) + """_RUSTSOURCES) $(COMMON_RUSTSOURCES) + cd $(abs_srcdir)/""" + defn['crate'] + """; \\ + CARGO_TARGET_DIR=$(abs_builddir)/""" + defn['crate'] + """/target cargo +nightly build $(CARGO_RELEASE_ARGS) --target=$(RUST_TARGET_PATH) -Zbuild-std=core,alloc +""") + gvar_add("RUST_BUILDDIRS", "$(abs_builddir)/" + defn['crate'] + "/target/$(rust_target)/$(RUST_TARGET_SUBDIR)/") + else: + rustlib = "" + + var_set(cname(defn) + "_RUSTLIBRARY", rustlib) + var_set(cname(defn) + "_LDADD", platform_ldadd(defn, platform) + " $(" + cname(defn) + "_RUSTLIBRARY)") + # the rust library needs to be a built source so that automake builds it + # before attempting to build the module. putting it in marker and ldadd is insufficient + var_set("nodist_" + cname(defn) + "_SOURCES", platform_nodist_sources(defn, platform) + " $(" + cname(defn) + "_RUSTLIBRARY) ## platform nodist sources and built rust libraries") + gvar_add("dist_noinst_DATA", extra_dist(defn)) gvar_add("BUILT_SOURCES", "$(nodist_" + cname(defn) + "_SOURCES)") gvar_add("CLEANFILES", "$(nodist_" + cname(defn) + "_SOURCES)")
@@ -698,7 +720,7 @@ def module(defn, platform): gvar_add("MARKER_FILES", name + ".marker") gvar_add("CLEANFILES", name + ".marker") output(""" -""" + name + """.marker: $(""" + cname(defn) + """_SOURCES) $(nodist_""" + cname(defn) + """_SOURCES) +""" + name + ".marker: $(" + cname(defn) + "_SOURCES) $(nodist_" + cname(defn) + """_SOURCES) $(TARGET_CPP) -DGRUB_LST_GENERATOR $(CPPFLAGS_MARKER) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(""" + cname(defn) + """_CPPFLAGS) $(CPPFLAGS) $^ > [email protected] || (rm -f $@; exit 1) grep 'MARKER' [email protected] > $@; rm -f [email protected] """)
grub-core/Makefile.am +20 -0
@@ -26,6 +26,23 @@ CFLAGS_LIBRARY += $(CFLAGS_PLATFORM) -fno-builtin CPPFLAGS_LIBRARY += $(CPPFLAGS_PLATFORM) CCASFLAGS_LIBRARY += $(CCASFLAGS_PLATFORM) +if RUST_DEBUG_RELEASE +CARGO_RELEASE_ARGS= +else +CARGO_RELEASE_ARGS=--release +endif + +if COND_RUST +lib/rust/grub/src/bindings.rs: lib/rust/bindings.h + bindgen --use-core lib/rust/bindings.h --ctypes-prefix=cty -o lib/rust/grub/src/bindings.rs -- -I ../include -I.. -DGRUB_FILE='"<rust bindings>"' + +CLEANFILES += lib/rust/grub/src/bindings.rs + +COMMON_RUSTSOURCES = lib/rust/grub/src/bindings.rs lib/rust/grub/src/lib.rs +else +COMMON_RUSTSOURCES = +endif + build-grub-pep2elf$(BUILD_EXEEXT): $(top_srcdir)/util/grub-pe2elf.c $(top_srcdir)/grub-core/kern/emu/misc.c $(top_srcdir)/util/misc.c $(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_TARGET_WORDSIZE=64 -DGRUB_UTIL=1 -DGRUB_BUILD_PROGRAM_NAME=\"build-grub-pep2elf\" $^ CLEANFILES += build-grub-pep2elf$(BUILD_EXEEXT)
@@ -504,3 +521,6 @@ windowsdir: $(PROGRAMS) $(starfield_DATA) $(platform_DATA) for x in $(platform_DATA); do \ cp -fp $$x $(windowsdir)/$(target_cpu)-$(platform)/$$x; \ done + +clean-local: + rm -rf $(RUST_BUILDDIRS)
grub-core/lib/rust/bindings.h new file +4 -0
@@ -0,0 +1,4 @@ +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/normal.h> +#include <grub/dl.h>
grub-core/lib/rust/conftest/Cargo.lock new file +7 -0
@@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "conftest" +version = "0.1.0"
grub-core/lib/rust/conftest/Cargo.toml new file +10 -0
@@ -0,0 +1,10 @@ +[package] +name = "conftest" +version = "0.1.0" +edition = "2018" + +[lib] +crate_type = ["staticlib"] + + +[dependencies]
grub-core/lib/rust/conftest/src/lib.rs new file +10 -0
@@ -0,0 +1,10 @@ +#![no_std] + +use core::panic::PanicInfo ; + +pub fn foo () {} + +#[panic_handler] +fn panic (_panic: &PanicInfo <'_>) -> ! { +loop {} +}
grub-core/lib/rust/grub/.gitignore new file +1 -0
@@ -0,0 +1 @@ +src/bindings.rs
grub-core/lib/rust/grub/Cargo.toml new file +8 -0
@@ -0,0 +1,8 @@ +[package] +name = "grub" +version = "0.1.0" +authors = ["Daniel Axtens <[email protected]>"] +edition = "2018" + +[dependencies] +cty = "^0.2"
grub-core/lib/rust/grub/src/lib.rs new file +63 -0
63 lines not on this page · raw message
grub-core/lib/rust/targets/x86_64-emu.json new file +27 -0
@@ -0,0 +1,27 @@ +{ + "arch": "x86_64", + "cpu": "x86-64", + "env": "gnu", + "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", + "dynamic-linking": true, + "llvm-target": "x86_64-unknown-linux-gnu", + "max-atomic-width": 64, + "os": "none", + "position-independent-executables": true, + "pre-link-args": { + "gcc": [ + "-m64" + ] + }, + "target-pointer-width": "64", + "features": "-mmx,-sse,+soft-float", + "relocation-model": "static", + "code-model": "large", + "disable-redzone": true, + "panic-strategy": "abort", + "singlethread": true, + "no-builtins": true, + "stack-probes": { + "kind": "none" + } +}
include/grub/dl.h +20 -1
@@ -38,7 +38,26 @@#ifndef GRUB_MOD_INIT -#if !defined (GRUB_UTIL ) && !defined (GRUB_MACHINE_EMU ) && !defined (GRUB_KERNEL ) +#if defined(RUST_WRAPPER ) + +/* Rust modules always use grub_##name##_{init,fini} */ + +#define GRUB_MOD_INIT (name) \ +void grub_##name##_init(void); \ +static void __attribute__((used)) \ +grub_mod_init (grub_dl_t mod __attribute__ ((unused))) \ +{ \ + grub_##name##_init(); \ +} + +#define GRUB_MOD_FINI (name) \ +void grub_##name##_fini(void); \ +static void __attribute__((used)) \ +grub_mod_fini (void) \ +{ \ + grub_##name##_fini(); \ +} +#elif !defined (GRUB_UTIL ) && !defined (GRUB_MACHINE_EMU ) && !defined (GRUB_KERNEL )#define GRUB_MOD_INIT (name) \ static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
Signature · 1 line
2.30.2
RFC PATCH 3/7 Rust: add a slightly more idiomatic wrapper around command handling
In reply to Daniel AxtensAdd a way to do registering and deregistering of commands that is just a tiny bit more "Rust-y".
--- grub-core/Makefile.am | 2 +- grub-core/lib/rust/grub/src/command.rs | 50 ++++++++++++++++++++++++++ grub-core/lib/rust/grub/src/lib.rs | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 grub-core/lib/rust/grub/src/command.rs
grub-core/Makefile.am +1 -1
@@ -38,7 +38,7 @@ lib/rust/grub/src/bindings.rs: lib/rust/bindings.h CLEANFILES += lib/rust/grub/src/bindings.rs -COMMON_RUSTSOURCES = lib/rust/grub/src/bindings.rs lib/rust/grub/src/lib.rs +COMMON_RUSTSOURCES = lib/rust/grub/src/bindings.rs lib/rust/grub/src/lib.rs lib/rust/grub/src/command.rs else COMMON_RUSTSOURCES = endif
grub-core/lib/rust/grub/src/command.rs new file +50 -0
50 lines not on this page · raw message
grub-core/lib/rust/grub/src/lib.rs +1 -0
@@ -6,6 +6,7 @@#![feature(alloc_error_handler)] pub mod bindings; +pub mod command;use bindings::grub_size_t;use core::alloc::GlobalAlloc ;use core::alloc::Layout ;
Signature · 1 line
2.30.2
RFC PATCH 4/7 Rust: add the rust_hello module
In reply to Daniel AxtensBuild an actual module with Rust!
This module, built whenever COND_RUST is enabled, does the following:
- Prints a message when the module is loaded.
- Registers a command which prints hello when run.
- Deregisters the command when the module is unloaded.
You can build it with the usual make, no special magic required.
A test is supplied.
A few outstanding to-dos:
- The Rust source doesn't get preprocessed in such a way as to allow the normal module to insert the little shim command that loads the module when called. (the command.lst stuff)
- I haven't looked at how to specify sections etc in Rust so I use wrap.c to specify a license.
- Bindgen uses the 'cty' module to specify c-types. That's MIT licensed, which is GPLv3 compatible (AIUI) but maybe we want to write our own. The linux kernel has one of it's own which is GPLv2 only, I'm hoping to ask if they can dual-license it so we can borrow it.
- The module does somewhat expose that more idiomatic layers around the bindings would be very nice.
--- .gitignore | 1 + Makefile.util.def | 6 +++ grub-core/Makefile.core.def | 9 ++++ grub-core/commands/rust-hello/.gitignore | 1 + grub-core/commands/rust-hello/Cargo.lock | 24 ++++++++++ grub-core/commands/rust-hello/Cargo.toml | 16 +++++++ grub-core/commands/rust-hello/src/lib.rs | 57 ++++++++++++++++++++++++ grub-core/commands/rust-hello/wrap.c | 8 ++++ tests/test_rust.in | 19 ++++++++ 9 files changed, 141 insertions(+) create mode 100644 grub-core/commands/rust-hello/.gitignore create mode 100644 grub-core/commands/rust-hello/Cargo.lock create mode 100644 grub-core/commands/rust-hello/Cargo.toml create mode 100644 grub-core/commands/rust-hello/src/lib.rs create mode 100644 grub-core/commands/rust-hello/wrap.c create mode 100644 tests/test_rust.in
.gitignore +1 -0
@@ -264,6 +264,7 @@ widthspec.bin /stamp-h1 /syslinux_test /tar_test +/test_rust /test_sha512sum /test_unset /tests/syslinux/ubuntu10.04_grub.cfg
Makefile.util.def +6 -0
@@ -1211,6 +1211,12 @@ script = { common = tests/syslinux_test.in; }; +script = { + testcase; + name = test_rust; + common = tests/test_rust.in; +}; + program = { testcase; name = example_unit_test;
grub-core/Makefile.core.def +9 -0
@@ -2527,3 +2527,12 @@ module = { common = commands/i386/wrmsr.c; enable = x86; }; + +module = { + name = rust_hello; + common = commands/rust-hello/wrap.c; + rust = commands/rust-hello/src/lib.rs; + crate = commands/rust-hello; + + condition = COND_RUST; +};
grub-core/commands/rust-hello/.gitignore new file +1 -0
@@ -0,0 +1 @@ +target
grub-core/commands/rust-hello/Cargo.lock new file +24 -0
@@ -0,0 +1,24 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cty" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7313c0d620d0cb4dbd9d019e461a4beb501071ff46ec0ab933efb4daa76d73e3" + +[[package]] +name = "grub" +version = "0.1.0" +dependencies = [ + "cty", +] + +[[package]] +name = "rust-hello" +version = "0.1.0" +dependencies = [ + "cty", + "grub", +]
grub-core/commands/rust-hello/Cargo.toml new file +16 -0
@@ -0,0 +1,16 @@ +[package] +name = "rust-hello" +version = "0.1.0" +authors = ["Daniel Axtens <[email protected]>"] +edition = "2018" + +[lib] +crate_type = ["staticlib"] + +[profile.release] +panic="abort" +lto = true + +[dependencies] +grub = { path = "../../lib/rust/grub" } +cty = "^0.2"
grub-core/commands/rust-hello/src/lib.rs new file +57 -0
57 lines not on this page · raw message
grub-core/commands/rust-hello/wrap.c new file +8 -0
@@ -0,0 +1,8 @@ +#define RUST_WRAPPER +#include <grub/dl.h> + +GRUB_MOD_LICENSE ("GPLv3+" ); +/* rust code defines grub_rust_hello_{init,fini}, this is just for the +scripts that determine modules */ +GRUB_MOD_INIT (rust_hello); +GRUB_MOD_FINI (rust_hello);
tests/test_rust.in new file +19 -0
@@ -0,0 +1,19 @@ +#! @BUILD_SHEBANG@ + +. "@builddir@/grub-core/modinfo.sh" + +out=`echo "insmod rust_hello; rust_hello;" | @builddir@/grub-shell` + +if [ ${grub_modinfo_platform} != emu ]; then + if ! echo $out | grep "Hello from Rust" > /dev/null; then + echo "Did not see hello from rust module" + exit 1 + fi +fi + +if echo $out | grep "Hello from command written in Rust" > /dev/null; then + exit 0; +else + echo "Did not see hello from rust command" + exit 1; +fi
Signature · 1 line
2.30.2
RFC PATCH 5/7 powerpc: Support Rust
In reply to Daniel AxtensThis is all required to get Rust working with powerpc.
Again based mostly on the rustc target.json emitter with a few tweaks for grub.
To get the rust toolchain:
rustup target add powerpc-unknown-linux-gnu --toolchain nightly
--- .../lib/rust/targets/powerpc-ieee1275.json | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 grub-core/lib/rust/targets/powerpc-ieee1275.json
grub-core/lib/rust/targets/powerpc-ieee1275.json new file +28 -0
@@ -0,0 +1,28 @@ +{ + "arch": "powerpc", + "data-layout": "E-m:e-p:32:32-i64:64-n32", + "dynamic-linking": true, + "env": "gnu", + "llvm-target": "powerpc-unknown-linux-gnu", + "max-atomic-width": 32, + "os": "none", + "position-independent-executables": true, + "pre-link-args": { + "gcc": [ + "-m32" + ] + }, + "target-endian": "big", + "target-family": [ + "unix" + ], + "target-mcount": "_mcount", + "target-pointer-width": "32", + "features": "-altivec,-vsx,-hard-float", + "relocation-model": "static", + "code-model": "large", + "disable-redzone": true, + "panic-strategy": "abort", + "singlethread": true, + "no-builtins": true +}
Signature · 1 line
2.30.2
RFC PATCH 6/7 x86_64-efi: Support Rust
In reply to Daniel AxtensOnly tested in qemu, not on real hardware.
--- grub-core/lib/rust/targets/x86_64-efi.json | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 grub-core/lib/rust/targets/x86_64-efi.json
grub-core/lib/rust/targets/x86_64-efi.json new file +27 -0
@@ -0,0 +1,27 @@ +{ + "arch": "x86_64", + "cpu": "x86-64", + "env": "gnu", + "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", + "dynamic-linking": true, + "llvm-target": "x86_64-unknown-linux-gnu", + "max-atomic-width": 64, + "os": "none", + "position-independent-executables": true, + "pre-link-args": { + "gcc": [ + "-m64" + ] + }, + "target-pointer-width": "64", + "features": "-mmx,-sse,+soft-float", + "relocation-model": "static", + "code-model": "large", + "disable-redzone": true, + "panic-strategy": "abort", + "singlethread": true, + "no-builtins": true, + "stack-probes": { + "kind": "none" + } +}
Signature · 1 line
2.30.2
RFC PATCH 7/7 arm64-efi: Support Rust
In reply to Daniel AxtensOnly tested on qemu, not real hardware.
To get the rust toolchain:
rustup target add aarch64-unknown-linux-gnu --toolchain nightly
--- grub-core/lib/rust/targets/arm64-efi.json | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 grub-core/lib/rust/targets/arm64-efi.json
grub-core/lib/rust/targets/arm64-efi.json new file +27 -0
@@ -0,0 +1,27 @@ +{ + "arch": "aarch64", + "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", + "dynamic-linking": true, + "env": "gnu", + "llvm-target": "aarch64-unknown-linux-gnu", + "max-atomic-width": 128, + "os": "none", + "position-independent-executables": true, + "supported-sanitizers": [ + "address", + "leak", + "memory", + "thread", + "hwaddress" + ], + "target-mcount": "\u0001_mcount", + "target-pointer-width": "64", + "features": "-fp,-neon", + "disable-redzone": true, + "panic-strategy": "abort", + "singlethread": true, + "no-builtins": true, + "stack-probes": { + "kind": "none" + } +}
Signature · 1 line
2.30.2
Quoted 47 lines · Daniel Axtens
grub is a trusted component of the secure boot process, including "traditional" GPG-based secure boot, UEFI-based secure boot, and the WIP secure boot process using appended signatures. Most of grub is written in C and has suffered from a number of memory-unsafety issues in the past, something about which many of us have very distinct and unpleasant memories!
Rust is a systems programming language suitable for low-level code. Rust can provide strong compile-time guarantees about memory safety. It also is reasonably easy for Rust code to interoperate with C.
Grub's modular design may work in our favour here. Potentially vulnerable components such as image and file-system parsers are written as individual modules. Can we progressively rewrite these modules in a safer language?
This patch set provides my efforts so far in making this possible.
It allows a module written almost entirely in Rust to be built and run (in qemu) on x86_64-{emu,efi}, arm64-efi and powerpc-ieee1275.
It still needs work, but it's at the stage where it's not entirely trivial, and where I want to start getting more feedback on it.
There are a few comments on things that need to be changed further on in the series, but I think the biggest overarching change we probably want to move towards doing more of what the Rust for Linux (kernel) project has done: drop Cargo and build everything (except tests) directly with rustc. This is a pain because Cargo is magic, but it will resolve some hairy issues.
(For example, currently on
x86_64platforms you can't have multiple Rust modules because each defines a__rust_probestackintrinsic despite my attempts to disable it. IMO that's a rustc bug and I have reported it. But, there are other intrinsics that Rust code could validly call, and we don't want implementations linked into each module, we want the modules to call into the kernel. This is - as far as I can tell - impossible to do with Cargo builds; we need finer control.)Another big issue where we want to follow the Rust for Linux project is how to handle failing allocations: by default in Rust that kills your program (!), a situation which is entirely unacceptable for the linux kernel. It's also unacceptable in Grub, so we should figure out how they avoid it and learn from them.
Quoted 5 lines
I have proposed a Linux Plumbers talk in the systems boot stream to discuss this further. I'd also welcome on-list discussions.
(Now, back to addressing the review comments on my previous patch series!)
Hello Daniel,
riscv64 is one of the architectures supported by GRUB. Why is it missing in your series?
Best regards
Heinrich
Quoted 58 lines
Daniel Axtens (7):
emu: supportgrub_memalign
Rust: module build infrastructure
Rust: add a slightly more idiomatic wrapper around command handling
Rust: add therust_hellomodule
powerpc: Support Rust
x86_64-efi: Support Rust
arm64-efi: Support Rust.gitignore | 1 + Makefile.util.def | 6 ++ conf/Makefile.common | 1 + configure.ac | 71 +++++++++++++++++++ gentpl.py | 28 +++++++- grub-core/Makefile.am | 20 ++++++ grub-core/Makefile.core.def | 9 +++ grub-core/commands/rust-hello/.gitignore | 1 + grub-core/commands/rust-hello/Cargo.lock | 24 +++++++ grub-core/commands/rust-hello/Cargo.toml | 16 +++++ grub-core/commands/rust-hello/src/lib.rs | 57 +++++++++++++++ grub-core/commands/rust-hello/wrap.c | 8 +++ grub-core/kern/emu/mm.c | 6 ++ grub-core/lib/rust/bindings.h | 4 ++ grub-core/lib/rust/conftest/Cargo.lock | 7 ++ grub-core/lib/rust/conftest/Cargo.toml | 10 +++ grub-core/lib/rust/conftest/src/lib.rs | 10 +++ grub-core/lib/rust/grub/.gitignore | 1 + grub-core/lib/rust/grub/Cargo.toml | 8 +++ grub-core/lib/rust/grub/src/command.rs | 50 +++++++++++++ grub-core/lib/rust/grub/src/lib.rs | 64 +++++++++++++++++ grub-core/lib/rust/targets/arm64-efi.json | 27 +++++++ .../lib/rust/targets/powerpc-ieee1275.json | 28 ++++++++ grub-core/lib/rust/targets/x86_64-efi.json | 27 +++++++ grub-core/lib/rust/targets/x86_64-emu.json | 27 +++++++ include/grub/dl.h | 21 +++++- include/grub/mm.h | 2 - tests/test_rust.in | 19 +++++ 28 files changed, 547 insertions(+), 6 deletions(-)create mode 100644 grub-core/commands/rust-hello/.gitignore create mode 100644 grub-core/commands/rust-hello/Cargo.lock create mode 100644
grub-core/commands/rust-hello/Cargo.tomlcreate mode 100644grub-core/commands/rust-hello/src/lib.rscreate mode 100644grub-core/commands/rust-hello/wrap.ccreate mode 100644grub-core/lib/rust/bindings.h
create mode 100644 grub-core/lib/rust/conftest/Cargo.lock create mode 100644grub-core/lib/rust/conftest/Cargo.tomlcreate mode 100644grub-core/lib/rust/conftest/src/lib.rscreate mode 100644 grub-core/lib/rust/grub/.gitignore create mode 100644grub-core/lib/rust/grub/Cargo.tomlcreate mode 100644grub-core/lib/rust/grub/src/command.rscreate mode 100644grub-core/lib/rust/grub/src/lib.rscreate mode 100644grub-core/lib/rust/targets/arm64-efi.jsoncreate mode 100644grub-core/lib/rust/targets/powerpc-ieee1275.jsoncreate mode 100644grub-core/lib/rust/targets/x86_64-efi.jsoncreate mode 100644grub-core/lib/rust/targets/x86_64-emu.jsoncreate mode 100644 tests/test_rust.in
Quoted 51 lines · Heinrich Schuchardt
Quoted 50 lines · Daniel Axtens
grub is a trusted component of the secure boot process, including "traditional" GPG-based secure boot, UEFI-based secure boot, and the WIP secure boot process using appended signatures. Most of grub is written in C and has suffered from a number of memory-unsafety issues in the past, something about which many of us have very distinct and unpleasant memories!
Rust is a systems programming language suitable for low-level code. Rust can provide strong compile-time guarantees about memory safety. It also is reasonably easy for Rust code to interoperate with C.
Grub's modular design may work in our favour here. Potentially vulnerable components such as image and file-system parsers are written as individual modules. Can we progressively rewrite these modules in a safer language?
This patch set provides my efforts so far in making this possible.
It allows a module written almost entirely in Rust to be built and run (in qemu) on x86_64-{emu,efi}, arm64-efi and powerpc-ieee1275.
It still needs work, but it's at the stage where it's not entirely trivial, and where I want to start getting more feedback on it.
There are a few comments on things that need to be changed further on in the series, but I think the biggest overarching change we probably want to move towards doing more of what the Rust for Linux (kernel) project has done: drop Cargo and build everything (except tests) directly with rustc. This is a pain because Cargo is magic, but it will resolve some hairy issues.
(For example, currently on
x86_64platforms you can't have multiple Rust modules because each defines a__rust_probestackintrinsic despite my attempts to disable it. IMO that's a rustc bug and I have reported it. But, there are other intrinsics that Rust code could validly call, and we don't want implementations linked into each module, we want the modules to call into the kernel. This is - as far as I can tell - impossible to do with Cargo builds; we need finer control.)Another big issue where we want to follow the Rust for Linux project is how to handle failing allocations: by default in Rust that kills your program (!), a situation which is entirely unacceptable for the linux kernel. It's also unacceptable in Grub, so we should figure out how they avoid it and learn from them.
I have proposed a Linux Plumbers talk in the systems boot stream to discuss this further. I'd also welcome on-list discussions.
Quoted 7 lines
Quoted 2 lines
(Now, back to addressing the review comments on my previous patch series!)
Hello Daniel,
riscv64 is one of the architectures supported by GRUB. Why is it missing in your series?
I simply didn't try it. I figured the 4 platforms I tried were sufficient to demonstrate things and that people who were interested in other platforms could create their own target.json files and post them.
It's probably easy enough to add support as LLVM and Rust support riscv64.
You could try creating a riscv64-efi.json yourself based on:
rustc +nightly -Z unstable-options --target=riscv64-unknown-linux-gnu \
--print target-spec-json > grub-core/lib/rust/targets/riscv64-efi.json
and then modify the resultant json file to disable hard float etc, set singlethread mode, set necessary code model flags, etc. (You can compare the target.json files for other targets to see what I mean.)
Kind regards,
Daniel
Quoted 63 lines
Best regards
Heinrich
Quoted 58 lines
Daniel Axtens (7):
emu: supportgrub_memalign
Rust: module build infrastructure
Rust: add a slightly more idiomatic wrapper around command handling
Rust: add therust_hellomodule
powerpc: Support Rust
x86_64-efi: Support Rust
arm64-efi: Support Rust.gitignore | 1 + Makefile.util.def | 6 ++ conf/Makefile.common | 1 + configure.ac | 71 +++++++++++++++++++ gentpl.py | 28 +++++++- grub-core/Makefile.am | 20 ++++++ grub-core/Makefile.core.def | 9 +++ grub-core/commands/rust-hello/.gitignore | 1 + grub-core/commands/rust-hello/Cargo.lock | 24 +++++++ grub-core/commands/rust-hello/Cargo.toml | 16 +++++ grub-core/commands/rust-hello/src/lib.rs | 57 +++++++++++++++ grub-core/commands/rust-hello/wrap.c | 8 +++ grub-core/kern/emu/mm.c | 6 ++ grub-core/lib/rust/bindings.h | 4 ++ grub-core/lib/rust/conftest/Cargo.lock | 7 ++ grub-core/lib/rust/conftest/Cargo.toml | 10 +++ grub-core/lib/rust/conftest/src/lib.rs | 10 +++ grub-core/lib/rust/grub/.gitignore | 1 + grub-core/lib/rust/grub/Cargo.toml | 8 +++ grub-core/lib/rust/grub/src/command.rs | 50 +++++++++++++ grub-core/lib/rust/grub/src/lib.rs | 64 +++++++++++++++++ grub-core/lib/rust/targets/arm64-efi.json | 27 +++++++ .../lib/rust/targets/powerpc-ieee1275.json | 28 ++++++++ grub-core/lib/rust/targets/x86_64-efi.json | 27 +++++++ grub-core/lib/rust/targets/x86_64-emu.json | 27 +++++++ include/grub/dl.h | 21 +++++- include/grub/mm.h | 2 - tests/test_rust.in | 19 +++++ 28 files changed, 547 insertions(+), 6 deletions(-)create mode 100644 grub-core/commands/rust-hello/.gitignore create mode 100644 grub-core/commands/rust-hello/Cargo.lock create mode 100644
grub-core/commands/rust-hello/Cargo.tomlcreate mode 100644grub-core/commands/rust-hello/src/lib.rscreate mode 100644grub-core/commands/rust-hello/wrap.ccreate mode 100644grub-core/lib/rust/bindings.h
create mode 100644 grub-core/lib/rust/conftest/Cargo.lock create mode 100644grub-core/lib/rust/conftest/Cargo.tomlcreate mode 100644grub-core/lib/rust/conftest/src/lib.rscreate mode 100644 grub-core/lib/rust/grub/.gitignore create mode 100644grub-core/lib/rust/grub/Cargo.tomlcreate mode 100644grub-core/lib/rust/grub/src/command.rscreate mode 100644grub-core/lib/rust/grub/src/lib.rscreate mode 100644grub-core/lib/rust/targets/arm64-efi.jsoncreate mode 100644grub-core/lib/rust/targets/powerpc-ieee1275.jsoncreate mode 100644grub-core/lib/rust/targets/x86_64-efi.jsoncreate mode 100644grub-core/lib/rust/targets/x86_64-emu.jsoncreate mode 100644 tests/test_rust.in