Commit graph

6185 commits

Author SHA1 Message Date
bunnie
b87dde9208 add new signature version code to api 2024-01-08 01:44:01 +08:00
bunnie
3da1ed181e set hash default value to 0x30 ("0")
this makes it much easier to detect if a loader failed to set
a hash in other routines.
2024-01-08 00:21:06 +08:00
bunnie
7b8a598020 wire up the hash computation to the hash consumption 2024-01-08 00:17:54 +08:00
bunnie
7719dbcb69 write hash into the environment variable
Convert the fs_prehash into a hex ascii string, suitable for environment variables
The initial loader environment is hard-coded, so we use a hard-coded offset for the string
destination. It was a deliberate decision to not include a generic environment variable
handler in the loader because we want to keep the loader compact and with a small attack
surface; a generic environment handling routine would add significant size without much
benefit.

Note that the main purpose for environment variables is for test & debug tooling, where
single programs are run in hosted or emulated environments with arguments passed for fast
debugging. The sole purpose for the environment variable in the loader's context is to
this computed hash onto the userspace environments.
2024-01-08 00:16:48 +08:00
bunnie
8f3229c9ea convert secboot to have an argument for returning the hash
also compute the hash and return the value as part of this patch
2024-01-08 00:16:20 +08:00
bunnie
2f47a9d4f2 more rework of pre-hash argument
While ideally we'd be as light-fingered as possible on the cryptographic
APIs, unfortunately the reality is we have exactly one hardware
hash engine and we can't just leave the context laying around.

Given that the verification routine would consume the hash, that
would make it unavailable for use later on to pass to the system.
So, this patch set causes the pre-hash algorithm to take a simple
slice-u8 instead of a whole hash context.

The downside of this is it erases the type information about
needing a 64-bit hash to go into the context. This is partially
mitigated with an assert that gives us a run-time check of this
property.
2024-01-08 00:13:35 +08:00
bunnie
4b002c34f5 Merge branch 'main' into prehash 2024-01-07 23:42:22 +08:00
bunnie
0750644e45
Merge pull request #479 from betrusted-io/process-arguments
Support process arguments
2024-01-07 23:39:45 +08:00
Sean Cross
60f2a17649 kernel: support receiving process arguments
Support passing process arguments in register 2 as part of arguments to
a process.

This requires updating the `InitialProcess` struct that is received from
the loader, and so requires modeifications to be made in tandem.

Signed-off-by: Sean Cross <sean@xobs.io>
2024-01-07 23:02:58 +08:00
Sean Cross
d8c6881554 loader: pass environment to processes via stack
Load environment variables to processes by placing the value onto the
stack. This will support additional data in the future such as command
line arguments.

Note this is a breaking change and requires a kernel modification.

Signed-off-by: Sean Cross <sean@xobs.io>
2024-01-07 22:36:34 +08:00
bunnie
96328cd2b9 bump getrandom version to allow compatiblity with sigchat crates 2024-01-06 18:25:16 +08:00
bunnie
d44ab2f2aa add the .a artifacts to the tree for the asm.S
these are just sync'd to the asm.S and checked in.

Eventually, we should migrate that .S file into using
in-line assembly macros, but now that I look at it, I remember
why it hasn't been done yet -- there are some fancy conditionals
used to enable compatibility with different architectures that
I don't know off the top of my head how to translate into
in-line assembly syntax.
2024-01-06 16:41:40 +08:00
bunnie
c3f3ef91b2 fix #474
dbf28fbd61 from 9 months ago
actually broke the resume path, but we didn't see it until now
because the .a files are checked into the tree.
2024-01-06 16:41:31 +08:00
bunnie
d91df62678 add susres testing feature to the ticktimer-test-client 2024-01-06 16:41:02 +08:00
bunnie
163f8a23ac add susres testing feature to the ticktimer-test-client 2024-01-06 16:39:35 +08:00
bunnie
385e15084a add susres testing feature to the ticktimer-test-client 2024-01-06 15:45:20 +08:00
bunnie
10154b8372 add some comments and an assert to check consistency 2024-01-05 16:47:31 +08:00
bunnie
61686007c7 modify the signing routine to work with an unmapped kernel region
Now, we finally get to addressing the root cause of #472.

We can't map all of kernel memory anymore, because exclusivity of
ownership of pages is now properly enforced. This means that XIP
images, which run directly out of FLASH, map their code region of
FLASH into their processes. This disallows measurement of their
code spaces by the rootkeys crate; previously we had mapped all
of code space and could measure everyone's code but it also means
we had a double-ownership.

Having resolved that single-ownership is cleaner, we now have
a scheme where the loader, which sees all of the kernel anyways,
computes a hash and signature check of the kernel at boot time,
and pashes the unsigned hash on to the environment variables of Xous
(environment variables pending @xobs commits).

This commit fixes up all the kernel signing routines to work with
a now disjoint kernel memory space. In particular, the key signing
routine needs access to three parts of the FLASH region devoted
to storing `xous.img` (which is loosely and imprecisely referred
to as "kernel memory"; this does lead to confusion thinking that
we're referring to the RAM devoted to the kernel, but for this
discussion we are talking about the window of FLASH where `xous.img`
is written, along with other metadata related to it):

- the first page, where the signature is stored
- the end of the kernel, where the kernel length check is stored
- the end of the entire flash window, where the backup records
are stored

The first page is fairly self-explanatory, this is the signature,
some version information, and an unprotected length hint for the
kernel.

Because the length hint in the signature record is unprotected,
we have to repeat it at the end of the kernel data and include
it in the signed region. Thus, there is a window of memory
that is 2 pages large that captures the dynamically varying
(dynamically varying as in build-to-build this offset changes so
it can't be a `const`) location of the length data. 2 pages
are mapped because the region of interest can fall across a
page boundary, so we map the potential lower page and upper
page that captures the length field (although in most cases
the upper page is not used, it is harmless to map it pre-emptively).
A mask is provided so that we can use the previous compuattions
for offsets and just mask it to compute the offest within this
window.

The end of the flash window, the last page, contains the backup
records. This is a set of metadata that records information
about PDDB backups including the checksums of the regions and
encrypted access keys.

This patch attempts to find all accesses to kernel space and
bin them into their respective regions, while adjust their offsets.

Testing will require us to check signing, updating, and backups --
all aspects of the machine lifecycle are impacted!
2024-01-05 16:31:19 +08:00
bunnie
c73df68e15 add sha512-digest wrapper class
this is a trait wrapper that allows us to take pre-hash as
a `[u8]` and wrap it into a thing that hase a finalize() method
and the requisite traits to satisfy Rust, allowing us to pass
our loader-generated, environment-variable-passed hash into
an otherwise unawares cryptography crate.

mea culpa.
2024-01-05 16:29:41 +08:00
bunnie
b247a5cc0e dependency updates for pre-hash signing
most of them are just shifting existing dependencies around but
somehow we're also now depending on pem-rfc7468. This slipped in
because we now use a `der` method to interpret a pkcs-8 query.

You know, this is kind of silly in the end because in practice,
the key file is like literally 64 bytes and we're pulling in net about
four orders of magnitude more code to make sure we parse those
64 bytes in exactly the right way -- because we have the *whole*
spec pulled in as crates even though we only ever use a tiny section
of it.

otoh, just vendoring in code or hard-coding an offset in a key file
seems "broken". But maybe that's a false economy because I can
point to the one sin I commit in doing a trivial interpretation
of the key file (e.g. hard-coding an offset with some minimal
format checking), whereas probably in the tens of thousands
of lines of code pulled in to interpret the pkcs-8 format "correctly"
which I actually didn't review explicitly, there are many more
sins, but because I've externalized them into crates I can say
I can sleep well at night thinking I did the right thing by
avoiding that one sin. It's like buying index funds and yet not
feeling remorse about the actions of the corporations within
the fund because you didn't explicitly endorse their actions but
you still get to participate in their gains.

Somehow, this has turned into a commentary on the human condition.
2024-01-05 16:20:43 +08:00
bunnie
c51c3cfe79 add std as a target for engine-sha512
we actually do have a `std` now in Xous, it wasn't there before
because we made this crate before we had `std`.

However, I'm not quite sure what apis we should be adding if we
have `std`...probably something else should be added, we'll probably
trip over it when someone tries to compile against a crate that
actually uses the `std`-specific APIs.
2024-01-05 16:19:05 +08:00
bunnie
4d850bffbc modulate the sha512 target based on the host configuration
xous targets use the Precursor hardware engine for hashing

all others use a software-only implementation. Not the most
performant but the code is vendored in
directly from the rustcrypto crate.
2024-01-05 16:17:59 +08:00
bunnie
2addc83f76 add an implementation for x86, etc. targets to run sha512 hashes
this is necessary because we are doing ed25519-dalek on the local
machine and the original signing crate used to implement signatures
("ring") does not support pre-hash mode.

maybe could do with cleaning up the build to eliminate "ring" and
just go with ed25519-dalek for all signings now, but we already
have "ring" as a dependency for other reasons so let's go
with conservative small changes for now.
2024-01-05 16:16:13 +08:00
bunnie
887f475489 add the requisite dependencies for pre-hash signing to the tools crate 2024-01-05 16:15:16 +08:00
bunnie
d49d46ee0d pre-hash sign the kernel image; normal signing for loader image
The loader image uses the original signing method, because changing
that would require us to modify the gateware ROM, which is a huge
pain in the ass, and doesn't buy us anything.

Thus only the kernel signing uses the pre-hash method.
2024-01-05 16:14:09 +08:00
bunnie
259852c1c6 add a routine to the signer that can do pre-hash images
This requires us to modify our dependencies to have dalek-ed25519
as one of our dependencies.

Which requires another patch in this commit set to implement
sha512 for hosts, since we patch the sha2 crate for all targets
to our local version.
2024-01-05 16:12:55 +08:00
bunnie
1454637915 add sha as a depedency of the loader
this is exposed now that we have to explicitly pre-compute a hash
as part of boot.
2024-01-05 16:09:03 +08:00
bunnie
5894e71095 convert bootloader to use prehash method
This applies to the *kernel* image.

The loader itself is still signed with the conventional signature.

Version number of the loader is also incremented.
2024-01-05 16:08:22 +08:00
bunnie
d88e446a33 finalize pre-hash before initializing a new hash
we only have one hash engine in hardware, and we want to use
hardware hashing at boot to speed things up as much as possible
and reduce code size of the bootloader.

This patch causes the pre-hash to finalize before a new hash
is initialized which folds in all the other key parameters.
2024-01-05 16:07:23 +08:00
bunnie
41b654fe83 add --debug-loader option to xtask
passing this flag causes the loader to print debug output.
This slows down the loading process but gives you some visibility
into what's going on (maybe also an idea for the future to add
VDBG flags for verbosity, they exist in the code but not exposed
as command line options).
2024-01-05 16:03:06 +08:00
bunnie
de9af9cfd1 add std test templates to shellchat
some tests cherry-picked out of the `std` suite to help look
into locking problems in the kernel. Behind a feature gate
of `locktests`
2024-01-04 23:19:51 +08:00
Sean Cross
f02d296ecc Merge branch 'main' of github.com:betrusted-io/xous-core 2024-01-04 21:41:37 +08:00
bunnie
8ec08c9c76 fix rust v1.75.0 warnings that are probably benign
a lot of re-import warnings have cropped up. While I appreciate
their studiousness on this, the problem is that removing some
of these exports in the API crate could mean that future users
who want access to some API features that aren't used in this
configuration are now unaccessible.

I think it's not a huge problem at the moment, just spam-cleaning
the warnings because there are too many to deal with carefully
at the moment.
2024-01-04 19:15:28 +08:00
bunnie
0d80912f0a fix warning in 1.75.0; breaking change with earlier rust versions
This is unfortunate, and calling this out in a single commit because
this is a dubious decision to achieve warning-free code for such
a minor thing.
2024-01-04 19:13:20 +08:00
bunnie
5f25e2b992 add rand_xorshift to the project 2024-01-04 18:31:36 +08:00
bunnie
f88cca4401 cleanup hosted mode implementation for xous-log 2024-01-04 18:31:16 +08:00
bunnie
c9407a2d47 harmonize hosted mode to latest kernel API for get_pid() 2024-01-04 18:30:48 +08:00
bunnie
4efddceab0
Merge pull request #471 from betrusted-io/stability-refactor
Stability refactor
2024-01-04 18:20:02 +08:00
Sean Cross
061abb05db xous-log: stdout: insert \r when printing \n
When printing messages to stdout or stderr, insert `\r` whenever we
encounter `\n`.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 19:01:19 +08:00
Sean Cross
9a1191c0d2 emulation: ComConnector: remove IInterestingType
Apparently SPI connectors are no longer interesting. Remove this
designator as it no longer exists.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
b12c08e998 emulation: soc: add _zicsr_zifencei to rv32imac
Add "_zicsr_zifencei" to "rv32imac" when describing the CPU core. This
is required by newer versions of the spec, and will eventually be a
requirement of Renode.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
beeca26823 emulation: ec: add new riscv suffixes for csr/fence
The riscv foundation changed the descriptor of riscv cores. We must now
add "_zicsr_zifencei" to the "rv32i" designation in order to ensure we
have access to CSRs and the "FENCE.I" instruction.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
88020c99e0 loader: add pid to map_page() call
Add the pid as the page owner to `map_page()` calls. This ensures pages
are owned by their respective processes, and removes a lot of
`change_owner()` calls that immediately follow page mapping.

It also allows for superpages to be owned by their respective processes.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
e0c62e5480 loader: don't reserve the "susres" page
The "susres" page should be owned by the "susres" server and not the
kernel. Avoid mapping this page to the kernel to prevent it from getting
double-mapped.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
6b5b4a5d21 kernel: services: don't crash when a process quits
When enumerating processes, the `find_process()` process didn't consult
whether a process was actually free. This resulted in a crash,
particularly when attaching a debugger to a crashed process.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
b95d442e20 kernel: debug: add "check memory" command
This command ensures memory is all owned by the correct process and is
unique in its ownership.

It takes a long time to run, so ensure the watchdog timer is disabled.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
e44d009b84 kernel: mem: add function for verifying mappings
Add a function to ensure mappings are unique across the whole system.
This takes a very long time and can crash if the watchdog timer isn't
paused.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
64ef828152 emulation: ComConnector: remove IInterestingType
Apparently SPI connectors are no longer interesting. Remove this
designator as it no longer exists.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:38:30 +08:00
Sean Cross
88aa259599 emulation: soc: add _zicsr_zifencei to rv32imac
Add "_zicsr_zifencei" to "rv32imac" when describing the CPU core. This
is required by newer versions of the spec, and will eventually be a
requirement of Renode.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:37:37 +08:00
Sean Cross
08abed53bd emulation: ec: add new riscv suffixes for csr/fence
The riscv foundation changed the descriptor of riscv cores. We must now
add "_zicsr_zifencei" to the "rv32i" designation in order to ensure we
have access to CSRs and the "FENCE.I" instruction.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:36:31 +08:00