-
Notifications
You must be signed in to change notification settings - Fork 29
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
Add logic for reading KASLR offset #951
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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,147 @@ | ||
use std::error::Error as StdError; | ||
use std::fs::File; | ||
use std::io; | ||
use std::io::Read as _; | ||
use std::path::Path; | ||
use std::str; | ||
use std::str::FromStr; | ||
|
||
use crate::elf; | ||
use crate::elf::types::ElfN_Nhdr; | ||
use crate::elf::BackendImpl; | ||
use crate::elf::ElfParser; | ||
use crate::util::align_up_u32; | ||
use crate::util::from_radix_16; | ||
use crate::util::split_bytes; | ||
use crate::Addr; | ||
use crate::Error; | ||
use crate::ErrorExt as _; | ||
use crate::IntoError as _; | ||
use crate::Result; | ||
|
||
use super::normalizer::Output; | ||
|
||
|
||
/// The absolute path to the `kcore` `proc` node. | ||
const PROC_KCORE: &str = "/proc/kcore"; | ||
/// The name of the `VMCOREINFO` ELF note. | ||
/// | ||
/// See https://www.kernel.org/doc/html/latest/admin-guide/kdump/vmcoreinfo.html | ||
const VMCOREINFO_NAME: &[u8] = b"VMCOREINFO\0"; | ||
|
||
|
||
/// "Parse" the VMCOREINFO descriptor. | ||
/// | ||
/// This underspecified blob roughly has the following format: | ||
/// ``` | ||
/// OSRELEASE=6.2.15-100.fc36.x86_64 | ||
/// BUILD-ID=d3d01c80278f8927486b7f01d0ab6be77784dceb | ||
/// PAGESIZE=4096 | ||
/// SYMBOL(init_uts_ns)=ffffffffb72b8160 | ||
/// OFFSET(uts_namespace.name)=0 | ||
/// [...] | ||
/// ``` | ||
fn parse_vmcoreinfo_desc(desc: &[u8]) -> impl Iterator<Item = (&[u8], &[u8])> { | ||
desc.split(|&b| b == b'\n') | ||
.filter_map(|line| split_bytes(line, |b| b == b'=')) | ||
} | ||
|
||
/// Find and read the `KERNELOFFSET` note in a "kcore" file represented by | ||
/// `parser` (i.e., already opened as an ELF). | ||
fn find_kaslr_offset(parser: &ElfParser<File>) -> Result<Option<u64>> { | ||
let phdrs = parser.program_headers()?; | ||
for phdr in phdrs.iter(0) { | ||
if phdr.type_() != elf::types::PT_NOTE { | ||
continue | ||
} | ||
|
||
let file = parser.backend(); | ||
let mut offset = phdr.offset(); | ||
|
||
// Iterate through all available notes. See `elf(5)` for | ||
// details. | ||
while offset + (size_of::<ElfN_Nhdr>() as u64) <= phdr.file_size() { | ||
let nhdr = file | ||
.read_pod_obj::<ElfN_Nhdr>(offset) | ||
.context("failed to read kcore note header")?; | ||
offset += size_of::<ElfN_Nhdr>() as u64; | ||
|
||
let name = if nhdr.n_namesz > 0 { | ||
let name = file.read_pod_slice::<u8>(offset, nhdr.n_namesz as _)?; | ||
offset += u64::from(align_up_u32(nhdr.n_namesz, 4)); | ||
Some(name) | ||
} else { | ||
None | ||
}; | ||
|
||
// We are looking for the note named `VMCOREINFO`. | ||
if name.as_deref() == Some(VMCOREINFO_NAME) { | ||
if nhdr.n_descsz > 0 { | ||
let desc = file.read_pod_slice::<u8>(offset, nhdr.n_descsz as _)?; | ||
let offset = parse_vmcoreinfo_desc(&desc) | ||
.find(|(key, _value)| key == b"KERNELOFFSET") | ||
// The value is in hexadecimal format. Go figure. | ||
.map(|(_key, value)| { | ||
from_radix_16(value).ok_or_invalid_data(|| { | ||
format!("failed to parse KERNELOFFSET value `{value:x?}`") | ||
}) | ||
}) | ||
.transpose(); | ||
return offset | ||
} | ||
|
||
// There shouldn't be multiple notes with that name, | ||
// but I suppose it can't hurt to keep checking...? | ||
} | ||
|
||
offset += u64::from(align_up_u32(nhdr.n_descsz, 4)); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use test_log::test; | ||
|
||
use crate::ErrorKind; | ||
|
||
|
||
/// Check that we can parse a dummy VMCOREINFO descriptor. | ||
#[test] | ||
fn vmcoreinfo_desc_parsing() { | ||
let desc = b"OSRELEASE=6.2.15-100.fc36.x86_64 | ||
BUILD-ID=d3d01c80278f8927486b7f01d0ab6be77784dceb | ||
SYMBOL(init_uts_ns)=ffffffffb72b8160 | ||
OFFSET(uts_namespace.name)=0 | ||
PAGESIZE=4096 | ||
"; | ||
|
||
let page_size = parse_vmcoreinfo_desc(desc) | ||
.find(|(key, _value)| key == b"PAGESIZE") | ||
.map(|(_key, value)| value) | ||
.unwrap(); | ||
assert_eq!(page_size, b"4096"); | ||
} | ||
|
||
/// Check that we can determine the system's KASLR state. | ||
#[test] | ||
fn kaslr_offset_reading() { | ||
// Always attempt reading the KASLR to exercise the VMCOREINFO | ||
// parsing path. | ||
// Note that we cannot use the regular mmap based ELF parser | ||
// backend for this file, as it cannot be mmap'ed. We have to | ||
// fall back to using regular I/O instead. | ||
let parser = match ElfParser::open_non_mmap(PROC_KCORE) { | ||
Ok(parser) => parser, | ||
Err(err) if err.kind() == ErrorKind::NotFound => return, | ||
Err(err) => panic!("{err}"), | ||
}; | ||
// We care about the parsing logic, but can't make any claims | ||
// about the expected offset at this point. | ||
let _offset = find_kaslr_offset(&parser).unwrap(); | ||
} | ||
} |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: blazesym parses Elf notes at least in two places now, right? Why not add a proper iterator support and clean up this code (and the one that does build ID, right?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I thought about it, but couldn't find a good abstraction and the implementations have to exhibit certain differences for technical reasons. It's just a few lines of code, so I think it's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No big deal and can be done as a follow up. But I do find all those offset checks, file size, etc quite mundane and error prone, so I still feel like iterator would be an improvement. It can return Nhdr + name/descr slices, which would save a bunch of error check and otherwise distracting plumbing. But up to you.