-
Notifications
You must be signed in to change notification settings - Fork 15
hash: log version and sha256 of /proc/self/exe at boot #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zvonkok
wants to merge
4
commits into
NVIDIA:main
Choose a base branch
from
zvonkok:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
18a406d
claude: add self-describing code guideline
zvonkok 8514cb5
hash: log version and sha256 of /proc/self/exe at boot
zvonkok d7f8e05
hash: stamp boot identity and guard init against non-PID-1 runs
zvonkok ca404d1
Potential fix for pull request finding
zvonkok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) NVIDIA CORPORATION | ||
|
|
||
| //! Lets operators correlate dmesg output against the cosign/Rekor digest | ||
| //! published in the release evidence bundle, so a running NVRC can be matched | ||
| //! to its release artifact independently of the build pipeline (see | ||
| //! ARCHITECTURE.md §"Provenance & Supply-Chain Security"). | ||
| //! | ||
| //! Not a security primitive: binary integrity is already enforced before | ||
| //! `execve` by dm-verity (block layer), fs-verity (file layer), and IPE | ||
| //! (see ARCHITECTURE.md §"Measured Rootfs" and §"Integrity Policy | ||
| //! Enforcement"). A compromised NVRC could lie about its own hash — the | ||
| //! trustworthy digest is the one in Rekor, not the one in dmesg. | ||
|
|
||
| use crate::macros::ResultExt; | ||
| use sha2::{Digest, Sha256}; | ||
| use std::fmt::Write as _; | ||
| use std::fs; | ||
|
|
||
| // TODO(hardened_std): add to fs path whitelist when hardened_std::fs lands | ||
| const SELF_EXE: &str = "/proc/self/exe"; | ||
| const VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
|
||
| // Untrusted dev-convenience stamp: the short commit the binary was built from, | ||
| // plus `-dirty` for an uncommitted tree, set by CI on the build command. Lets a | ||
| // dmesg glance tell a dirty/local build from a clean release; absent on a plain | ||
| // release build, which logs VERSION alone. A tampered binary can forge this — | ||
| // authoritative release identity is the sha256 vs Rekor (see above and | ||
| // ARCHITECTURE.md). | ||
| const GIT_REV: Option<&str> = option_env!("GIT_REV"); | ||
|
|
||
| pub fn self_exe() { | ||
| info!("{}", version_line()); | ||
| } | ||
|
|
||
| pub fn version_line() -> String { | ||
| let digest = sha256().or_panic(format_args!("hash {SELF_EXE}")); | ||
| boot_line(&digest, GIT_REV) | ||
| } | ||
|
|
||
| fn boot_line(digest: &str, rev: Option<&str>) -> String { | ||
| format!("NVRC version={} sha256={digest}", version(rev)) | ||
| } | ||
|
|
||
| fn version(rev: Option<&str>) -> String { | ||
| rev.map_or_else(|| VERSION.to_string(), |rev| format!("{VERSION}+{rev}")) | ||
| } | ||
|
|
||
| fn sha256() -> std::io::Result<String> { | ||
| fs::read(SELF_EXE).map(|data| hex_encode(&Sha256::digest(&data))) | ||
| } | ||
|
|
||
| fn hex_encode(bytes: &[u8]) -> String { | ||
| bytes | ||
| .iter() | ||
| .fold(String::with_capacity(bytes.len() * 2), |mut s, b| { | ||
| let _ = write!(s, "{b:02x}"); | ||
| s | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_hex_encode_empty() { | ||
| assert_eq!(hex_encode(&[]), ""); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_hex_encode_known_vector() { | ||
| assert_eq!(hex_encode(&[0x00, 0xff, 0xab, 0x12, 0x9c]), "00ffab129c"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sha256_self_returns_64_hex_chars() { | ||
| let digest = sha256().expect("hash self"); | ||
| assert_eq!(digest.len(), 64); | ||
| assert!(digest.chars().all(|c| c.is_ascii_hexdigit())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sha256_empty_string_known_vector() { | ||
| // NIST: SHA-256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 | ||
| assert_eq!( | ||
| hex_encode(&Sha256::digest(b"")), | ||
| "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sha256_abc_known_vector() { | ||
| // NIST: SHA-256("abc") = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad | ||
| assert_eq!( | ||
| hex_encode(&Sha256::digest(b"abc")), | ||
| "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_self_exe_runs_to_completion() { | ||
| self_exe(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_version_without_rev_is_bare_cargo_version() { | ||
| assert_eq!(version(None), VERSION); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_version_with_rev_appends_plus_metadata() { | ||
| assert_eq!( | ||
| version(Some("g3ccba213b033")), | ||
| format!("{VERSION}+g3ccba213b033") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_version_with_dirty_rev_preserves_dirty_suffix() { | ||
| assert_eq!( | ||
| version(Some("g3ccba213b033-dirty")), | ||
| format!("{VERSION}+g3ccba213b033-dirty") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_boot_line_of_self_carries_cargo_version_and_real_digest() { | ||
| let digest = sha256().expect("hash self"); | ||
| let line = boot_line(&digest, GIT_REV); | ||
|
|
||
| assert!(line.starts_with(&format!("NVRC version={}", env!("CARGO_PKG_VERSION")))); | ||
| assert!(line.ends_with(&format!("sha256={digest}"))); | ||
| assert_eq!( | ||
| line, | ||
| format!("NVRC version={} sha256={digest}", version(GIT_REV)) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_boot_line_release_build_logs_bare_version() { | ||
| assert_eq!( | ||
| boot_line("deadbeef", None), | ||
| format!("NVRC version={} sha256=deadbeef", env!("CARGO_PKG_VERSION")) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_boot_line_dev_build_appends_git_rev() { | ||
| assert_eq!( | ||
| boot_line("deadbeef", Some("gabc123-dirty")), | ||
| format!( | ||
| "NVRC version={}+gabc123-dirty sha256=deadbeef", | ||
| env!("CARGO_PKG_VERSION") | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) NVIDIA CORPORATION | ||
|
|
||
| use crate::hash; | ||
|
|
||
| /// NVRC's init duties (mounts, module loads, daemon forks, the poweroff panic | ||
| /// hook) would wreck a normal host, so they must only run as PID 1. Anywhere | ||
| /// else (CI smoke test, dev shell) report identity and exit before the caller | ||
| /// touches anything. | ||
| pub fn as_pid1() { | ||
| if running_as_init() { | ||
| return; | ||
| } | ||
| // No logger on this path, so print to stdout rather than via the dropped | ||
| // log macros; this is the CI smoke test's only observable output. | ||
| println!("{}", hash::version_line()); | ||
| std::process::exit(0); | ||
| } | ||
|
|
||
| // Raw SYS_getpid syscall: stays on the pure-syscall path hardened_std targets, | ||
| // and needs no /proc (unmounted this early, mount::setup runs later). | ||
| fn running_as_init() -> bool { | ||
| unsafe { libc::syscall(libc::SYS_getpid) == 1 } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.