linux_libc_auxv/lib.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*/
24//! # libc-auxv - Build and Parse the Initial Linux Stack Layout for Different Address Spaces
25//!
26//! Linux passes an initial stack layout to applications, that contains `argc`, `argv`, `envp`, and the `auxiliary vector`
27//! right above the stack pointer. The libc of a Linux program parses this sturcture in its `_start`-symbol ("crt0") and
28//! passes the right pointers as arguments to `main` afterwards. This crate helps to construct and parse this data structure
29//! in `no_std` environments and for different address spaces.
30//!
31//! ## How does this differ from <https://crates.io/crates/crt0stack> and <https://crates.io/crates/auxv>?
32//! This crate supports `no_std`-contexts plus allows construction the data structure for a different address
33//! space, i.e. the address space of a user application.
34//!
35//! When I started creating this crate, I only knew about the latter. It doesn't support `no_std`. Because
36//! the first one supports `no_std` but not different address spaces, I still had to create this one.
37//! The typical use case for me is to create the data structure for a different address space, like Linux does.
38//!
39//! ## Functionality
40//! ✅ build data structure for current address space \
41//! ✅ build data structure for **different address space** \
42//! ✅ parse data structure for current address space + output referenced data/pointers \
43//! ✅ parse data structure for **different address space** + prevent memory error / no dereferencing of pointers
44//!
45//! ## Limitations
46//!
47//! ### 32 vs 64 bit
48//! The auxiliary vector contains pairs of type `(usize, usize)`. Hence, each entry takes 8 bytes on 32-bit systems
49//! and 16 byte on 64-bit systems. Currently, this crate produces the auxiliary vector for the architecture it is
50//! compiled with. If necessary, create an issue or a PR and this will be a runtime setting. I never tested it
51//! on a 32-bit system, but I am confident it will work.
52//!
53//! ### Auxiliary Vector vs Stack Layout
54//! Right now, this crate can only build and serialize the whole initial stack layout but not the auxiliary vector
55//! standalone.
56//!
57//! ## Code Example
58//! ```rust
59//! use linux_libc_auxv::{AuxVar, InitialLinuxLibcStackLayout, InitialLinuxLibcStackLayoutBuilder};
60//!
61//! // Minimal example that builds the initial linux libc stack layout and parses it again.
62//!
63//! let builder = InitialLinuxLibcStackLayoutBuilder::new()
64//! // can contain terminating zero; not mandatory in the builder
65//! .add_arg_v("./first_arg\0")
66//! .add_arg_v("./second_arg")
67//! .add_env_v("FOO=BAR\0")
68//! .add_env_v("PATH=/bin")
69//! .add_aux_v(AuxVar::Clktck(100))
70//! .add_aux_v(AuxVar::Random([
71//! 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
72//! ]))
73//! .add_aux_v(AuxVar::ExecFn("/usr/bin/foo"))
74//! .add_aux_v(AuxVar::Platform("x86_64"));
75//!
76//! // memory where we serialize the data structure into
77//! let mut buf = vec![0; builder.total_size()];
78//!
79//! // assume user stack is at 0x7fff0000
80//! let user_base_addr = 0x7fff0000;
81//! unsafe {
82//! builder.serialize_into_buf(buf.as_mut_slice(), user_base_addr);
83//! }
84//!
85//! // So far, this is memory safe, as long as the slice is valid memory. No pointers are
86//! // dereferenced yet.
87//! let parsed = InitialLinuxLibcStackLayout::from(buf.as_slice());
88//!
89//! println!("There are {} arguments.", parsed.argc());
90//! println!(
91//! "There are {} environment variables.",
92//! parsed.envv_ptr_iter().count()
93//! );
94//! println!(
95//! "There are {} auxiliary vector entries/AT variables.",
96//! parsed.aux_serialized_iter().count()
97//! );
98//!
99//! println!(" argv");
100//! // ptr iter is safe for other address spaces; the other only because here user_addr == write_addr
101//! for (i, arg) in parsed.argv_ptr_iter().enumerate() {
102//! println!(" [{}] @ {:?}", i, arg);
103//! }
104//!
105//! println!(" envp");
106//! // ptr iter is safe for other address spaces; the other only because here user_addr == write_addr
107//! for (i, env) in parsed.envv_ptr_iter().enumerate() {
108//! println!(" [{}] @ {:?}", i, env);
109//! }
110//!
111//! println!(" aux");
112//! // ptr iter is safe for other address spaces; the other only because here user_addr == write_addr
113//! for aux in parsed.aux_serialized_iter() {
114//! if aux.key().value_in_data_area() {
115//! println!(" {:?} => @ {:?}", aux.key(), aux.val() as *const u8);
116//! } else {
117//! println!(" {:?} => {:?}", aux.key(), aux.val() as *const u8);
118//! }
119//! }
120//! ```
121//!
122//! ### Code Example Output
123//! ```text
124//! There are 2 arguments.
125//! There are 2 environment variables.
126//! There are 5 auxiliary vector entries/AT variables.
127//! argv
128//! [0] @ 0x7fff00b0
129//! [1] @ 0x7fff00bc
130//! envp
131//! [0] @ 0x7fff00c9
132//! [1] @ 0x7fff00d1
133//! aux
134//! Platform => @ 0x7fff0090
135//! Clktck => 0x64
136//! Random => @ 0x7fff0097
137//! ExecFn => @ 0x7fff00db
138//! Null => 0x0
139//! ```
140//!
141//! ## Terminology (in Code)
142//! The whole data structure is called `InitialLinuxLibcStackLayout` by me. There is no official name. It contains
143//! the arguments (`argc` and `argv`), the environment variables (`envp` or `envv`), and the auxiliary vector
144//! (`AT-variables`, `auxv`, `aux-pairs`, `aux entries`).
145//!
146//! The `argv`-array will reference data in the `argv data area`, the `envv`-array will reference data in the
147//! `envv data area`, and some of the `auxv`-values might reference data in the `auxv data area`.
148//!
149//! Sometimes (in some articles), the auxiliary vector even describes the whole data structure.
150//!
151//! ## Layout of the Data Structure
152//! ```text
153//! null [HIGH ADDRESS]
154//! filename (c string)
155//! <env data area>
156//! <args data area>
157//! // round up to 16 byte
158//! <aux vec data area>
159//! // round up to 16 byte alignment
160//! AT_VAR_3 = <points to aux vec data area>
161//! AT_VAR_2 = integer
162//! AT_VAR_1 = integer
163//! // round up to 16 byte alignment
164//! envv[2] = null
165//! envv[1] = <points to env data area>
166//! envv[0] = <points to env data area>
167//! argv[2] = null
168//! argv[1] = <points to args data area>
169//! argv[0] = <points to args data area>
170//! argc = integer <libc entry stack top> [LOW ADDRESS]
171//! ```
172//!
173//! ## MSRV
174//! 1.56.1 stable / Rust edition 2021
175//!
176//! ## Background Information & Links
177//! - <https://lwn.net/Articles/631631/> (good overview with ASCII graphics)
178//! - <https://lwn.net/Articles/519085/>
179//! - <https://elixir.bootlin.com/linux/v5.15.5/source/fs/binfmt_elf.c#L257> (code in Linux that constructs `auxv`)
180
181#![deny(
182 clippy::all,
183 clippy::cargo,
184 clippy::nursery,
185 // clippy::restriction,
186 // clippy::pedantic
187)]
188// now allow a few rules which are denied by the above statement
189// --> they are ridiculous and not necessary
190#![allow(
191 clippy::suboptimal_flops,
192 clippy::redundant_pub_crate,
193 clippy::fallible_impl_from
194)]
195#![deny(missing_debug_implementations)]
196#![deny(rustdoc::all)]
197#![no_std]
198
199mod aux_var;
200mod builder;
201mod cstr_util;
202mod parser;
203
204pub use aux_var::*;
205pub use builder::*;
206pub use parser::*;
207
208#[macro_use]
209extern crate alloc;
210
211#[cfg_attr(test, macro_use)]
212#[cfg(test)]
213extern crate std;