minimal/
minimal.rs

1/*
2MIT License
3
4Copyright (c) 2021 Philipp Schuster
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24use linux_libc_auxv::{AuxVar, InitialLinuxLibcStackLayout, InitialLinuxLibcStackLayoutBuilder};
25
26/// Minimal example that builds the initial linux libc stack layout and parses it again.
27fn main() {
28    let builder = InitialLinuxLibcStackLayoutBuilder::new()
29        // can contain terminating zero; not mandatory in the builder
30        .add_arg_v("./first_arg\0")
31        .add_arg_v("./second_arg")
32        .add_env_v("FOO=BAR\0")
33        .add_env_v("PATH=/bin")
34        .add_aux_v(AuxVar::Clktck(100))
35        .add_aux_v(AuxVar::Random([
36            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
37        ]))
38        .add_aux_v(AuxVar::ExecFn("/usr/bin/foo"))
39        .add_aux_v(AuxVar::Platform("x86_64"));
40
41    // memory where we serialize the data structure into
42    let mut buf = vec![0; builder.total_size()];
43
44    // assume user stack is at 0x7fff0000
45    let user_base_addr = 0x7fff0000;
46    unsafe {
47        builder.serialize_into_buf(buf.as_mut_slice(), user_base_addr);
48    }
49
50    // So far, this is memory safe, as long as the slice is valid memory. No pointers are
51    // dereferenced yet.
52    let parsed = InitialLinuxLibcStackLayout::from(buf.as_slice());
53
54    println!("There are {} arguments.", parsed.argc());
55    println!(
56        "There are {} environment variables.",
57        parsed.envv_ptr_iter().count()
58    );
59    println!(
60        "There are {} auxiliary vector entries/AT variables.",
61        parsed.aux_serialized_iter().count()
62    );
63
64    println!("  argv");
65    // ptr iter is safe for other address spaces; the other only because here user_addr == write_addr
66    for (i, arg) in parsed.argv_ptr_iter().enumerate() {
67        println!("    [{}] @ {:?}", i, arg);
68    }
69
70    println!("  envp");
71    // ptr iter is safe for other address spaces; the other only because here user_addr == write_addr
72    for (i, env) in parsed.envv_ptr_iter().enumerate() {
73        println!("    [{}] @ {:?}", i, env);
74    }
75
76    println!("  aux");
77    // ptr iter is safe for other address spaces; the other only because here user_addr == write_addr
78    for aux in parsed.aux_serialized_iter() {
79        if aux.key().value_in_data_area() {
80            println!("    {:?} => @ {:?}", aux.key(), aux.val() as *const u8);
81        } else {
82            println!("    {:?} => {:?}", aux.key(), aux.val() as *const u8);
83        }
84    }
85}