isr_cache/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//! # Opinionated cache for OS kernel profiles
//!
//! This crate provides a caching mechanism for profiles generated and used by
//! the [`isr`] crate family. It offers several features to streamline the process
//! of accessing and managing symbol information, including methods for
//! downloading necessary debug symbols for Windows (PDB files) and Linux
//! (DWARF debug info and system map).
//!
//! ## Usage
//!
//! The main component of this crate is the [`IsrCache`] struct.
//!
//! Example of loading a profile from a PDB file using the CodeView information:
//!
//! ```rust
//! use isr::{
//!     download::pdb::CodeView,
//!     cache::{IsrCache, JsonCodec},
//! };
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # std::env::set_current_dir("../..")?;
//! // Create a new cache instance.
//! let cache = IsrCache::<JsonCodec>::new("cache")?;
//!
//! // Use the CodeView information of the Windows 10.0.18362.356 kernel.
//! let codeview = CodeView {
//!     path: String::from("ntkrnlmp.pdb"),
//!     guid: String::from("ce7ffb00c20b87500211456b3e905c471"),
//! };
//!
//! // Fetch and create (or get existing) the entry.
//! let entry = cache.entry_from_codeview(codeview)?;
//!
//! // Get the profile from the entry.
//! let profile = entry.profile()?;
//! # Ok(())
//! # }
//! ```
//!
//! Example of loading a profile based on a Linux kernel banner:
//!
//! ```rust
//! use isr::cache::{IsrCache, JsonCodec};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # std::env::set_current_dir("../..")?;
//! // Create a new cache instance.
//! let cache = IsrCache::<JsonCodec>::new("cache")?;
//!
//! // Use the Linux banner of the Ubuntu 6.8.0-40.40~22.04.3-generic kernel.
//! let banner = "Linux version 6.8.0-40-generic \
//!               (buildd@lcy02-amd64-078) \
//!               (x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) \
//!               12.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38) \
//!               #40~22.04.3-Ubuntu SMP PREEMPT_DYNAMIC \
//!               Tue Jul 30 17:30:19 UTC 2 \
//!               (Ubuntu 6.8.0-40.40~22.04.3-generic 6.8.12)";
//!
//! // Fetch and create (or get existing) the entry.
//! // Note that the download of Linux debug symbols may take a while.
//! let entry = cache.entry_from_linux_banner(banner)?;
//!
//! // Get the profile from the entry.
//! let profile = entry.profile()?;
//! # Ok(())
//! # }
//! ```
//!
//! Consult the [`vmi`] crate for more information on how to download debug
//! symbols for introspected VMs.
//!
//! [`isr`]: ../isr/index.html
//! [`vmi`]: ../vmi/index.html

mod codec;
mod error;

use std::{
    fs::File,
    path::{Path, PathBuf},
};

pub use isr_core::Profile;
pub use isr_dl_linux::{
    LinuxBanner, LinuxVersionSignature, UbuntuDownloader, UbuntuVersionSignature,
};
pub use isr_dl_pdb::{CodeView, PdbDownloader};
use memmap2::Mmap;

pub use self::{
    codec::{BincodeCodec, Codec, JsonCodec, MsgpackCodec},
    error::Error,
};

/// An entry in the [`IsrCache`].
pub struct Entry<C>
where
    C: Codec,
{
    /// The path to the profile.
    profile_path: PathBuf,

    /// The raw profile data.
    data: Mmap,

    /// The codec used to encode and decode the profile.
    _codec: std::marker::PhantomData<C>,
}

impl<C> Entry<C>
where
    C: Codec,
{
    /// Creates a new entry from the profile path.
    pub fn new(profile_path: PathBuf) -> Result<Self, Error> {
        let data = unsafe { Mmap::map(&File::open(&profile_path)?)? };
        Ok(Self {
            profile_path,
            data,
            _codec: std::marker::PhantomData,
        })
    }

    /// Returns the path to the profile.
    pub fn profile_path(&self) -> &Path {
        &self.profile_path
    }

    /// Returns the raw profile data.
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Decodes the profile from the entry.
    pub fn profile(&self) -> Result<Profile, C::DecodeError> {
        C::decode(&self.data)
    }
}

/// A cache for OS kernel profiles.
///
/// Manages the download and extraction of necessary debug symbols.
/// Uses a [`Codec`] to encode and decode profiles. The default codec is
/// [`JsonCodec`].
pub struct IsrCache<C = JsonCodec>
where
    C: Codec,
{
    /// The directory where cached profiles are stored.
    directory: PathBuf,

    /// The codec used to encode and decode profiles.
    _codec: std::marker::PhantomData<C>,
}

impl<C> IsrCache<C>
where
    C: Codec,
{
    /// Creates a new `IsrCache` instance, initializing it with the provided
    /// directory. If the directory doesn't exist, it attempts to create it.
    pub fn new(directory: impl Into<PathBuf>) -> Result<Self, Error> {
        let directory = directory.into();
        std::fs::create_dir_all(&directory)?;

        Ok(Self {
            directory,
            _codec: std::marker::PhantomData,
        })
    }

    /// Creates or retrieves a cached profile from a [`CodeView`] debug
    /// information structure.
    ///
    /// If a profile for the given `CodeView` information already exists in
    /// the cache, its path is returned. Otherwise, the necessary PDB file is
    /// downloaded, the profile is generated and stored in the cache, and its
    /// path is returned.
    #[cfg(feature = "pdb")]
    pub fn entry_from_codeview(&self, codeview: CodeView) -> Result<Entry<C>, Error> {
        let path = Path::new(&codeview.path);

        // <cache>/windows/ntkrnlmp.pdb/3844dbb920174967be7aa4a2c20430fa2
        let destination = self
            .directory
            .join("windows")
            .join(path)
            .join(&codeview.guid);

        std::fs::create_dir_all(&destination)?;

        // <cache>/windows/ntkrnlmp.pdb/3844dbb920174967be7aa4a2c20430fa2/ntkrnlmp.pdb
        let pdb_path = destination.join(path);
        if !pdb_path.exists() {
            PdbDownloader::new(codeview.clone())
                .with_output(&pdb_path)
                .download()?;
        }

        // <cache>/windows/ntkrnlmp.pdb/3844dbb920174967be7aa4a2c20430fa2/profile<.ext>
        let profile_path = destination.join("profile").with_extension(C::EXTENSION);

        match File::create_new(&profile_path) {
            Ok(profile_file) => {
                let pdb_file = File::open(&pdb_path)?;
                isr_pdb::create_profile(pdb_file, |profile| C::encode(profile_file, profile))?;
            }
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
                tracing::info!(?profile_path, "profile already exists");
            }
            Err(err) => return Err(err.into()),
        }

        Entry::new(profile_path)
    }

    /// Creates or retrieves a cached profile from a PE file.
    ///
    /// Extracts the [`CodeView`] debug information from the PE file and
    /// delegates to [`entry_from_codeview`].
    ///
    /// [`entry_from_codeview`]: Self::entry_from_codeview
    #[cfg(feature = "pdb")]
    pub fn entry_from_pe(&self, path: impl AsRef<Path>) -> Result<Entry<C>, Error> {
        self.entry_from_codeview(CodeView::from_path(path).map_err(isr_dl_pdb::Error::from)?)
    }

    /// Creates or retrieves a cached profile based on a Linux kernel banner.
    ///
    /// Parses the banner to determine the kernel version and downloads the
    /// necessary debug symbols and system map if not present in the cache.
    /// Generates and stores the profile, returning its path.
    #[cfg(feature = "linux")]
    pub fn entry_from_linux_banner(&self, linux_banner: &str) -> Result<Entry<C>, Error> {
        let banner = match LinuxBanner::parse(linux_banner) {
            Some(banner) => banner,
            None => return Err(Error::InvalidBanner),
        };

        let destination_path = match banner.version_signature {
            Some(LinuxVersionSignature::Ubuntu(version_signature)) => {
                self.download_from_ubuntu_version_signature(version_signature)?
            }
            _ => return Err(Error::InvalidBanner),
        };

        let profile_path = destination_path
            .join("profile")
            .with_extension(C::EXTENSION);

        match File::create_new(&profile_path) {
            Ok(profile_file) => {
                let kernel_file = File::open(destination_path.join("vmlinux-dbgsym"))?;
                let systemmap_file = File::open(destination_path.join("System.map"))?;
                isr_dwarf::create_profile(kernel_file, systemmap_file, |profile| {
                    C::encode(profile_file, profile)
                })?;
            }
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
                tracing::info!(?profile_path, "profile already exists");
            }
            Err(err) => return Err(err.into()),
        }

        Entry::new(profile_path)
    }

    /// Downloads and extracts the required debug symbols from the Ubuntu
    /// repositories based on the Ubuntu version signature in the Linux banner.
    ///
    /// Returns the path to the directory containing the downloaded and
    /// extracted files.
    #[cfg(feature = "linux")]
    fn download_from_ubuntu_version_signature(
        &self,
        version_signature: UbuntuVersionSignature,
    ) -> Result<PathBuf, isr_dl_linux::Error> {
        let UbuntuVersionSignature {
            release,
            revision,
            kernel_flavour,
            ..
        } = version_signature;

        // <cache>/ubuntu
        let downloader = UbuntuDownloader::new(&release, &revision, &kernel_flavour)
            .with_output_directory(self.directory.join("ubuntu"));

        // <cache>/ubuntu/6.8.0-40.40~22.04.3-generic
        let destination_path = downloader.destination_path();

        // Download only what's necessary.

        // <cache>/ubuntu/6.8.0-40.40~22.04.3-generic/linux-image.deb
        // <cache>/ubuntu/6.8.0-40.40~22.04.3-generic/vmlinuz
        let downloader = match destination_path.join("linux-image.deb").exists() {
            false => downloader
                .download_linux_image_as("linux-image.deb")
                .extract_linux_image_as("vmlinuz"),
            true => {
                tracing::info!("linux-image.deb already exists");
                downloader
            }
        };

        // <cache>/ubuntu/6.8.0-40.40~22.04.3-generic/linux-image-dbgsym.deb
        // <cache>/ubuntu/6.8.0-40.40~22.04.3-generic/vmlinux-dbgsym
        let downloader = match destination_path.join("linux-image-dbgsym.deb").exists() {
            false => downloader
                .download_linux_image_dbgsym_as("linux-image-dbgsym.deb")
                .extract_linux_image_dbgsym_as("vmlinux-dbgsym"),
            true => {
                tracing::info!("linux-image-dbgsym.deb already exists");
                downloader
            }
        };

        // <cache>/ubuntu/6.8.0-40.40~22.04.3-generic/linux-modules.deb
        // <cache>/ubuntu/6.8.0-40.40~22.04.3-generic/System.map
        let downloader = match destination_path.join("linux-modules.deb").exists() {
            false => downloader
                .download_linux_modules_as("linux-modules.deb")
                .extract_systemmap_as("System.map"),
            true => {
                tracing::info!("linux-modules.deb already exists");
                downloader
            }
        };

        match downloader.skip_existing().download() {
            Ok(_paths) => (),
            // UbuntuDownloader::download() returns Err(InvalidOptions) if
            // there's nothing to download.
            Err(isr_dl_linux::ubuntu::Error::InvalidOptions) => {
                tracing::info!("nothing to download");
            }
            Err(err) => return Err(err.into()),
        }

        Ok(destination_path)
    }
}