Debian Codegraph @

.cargo/vendor/rustix/src/backend/linux_raw/param/auxv.rs

raw
1 //! Linux auxv support.
2 //!
3 //! # Safety
4 //!
5 //! This uses raw pointers to locate and read the kernel-provided auxv array.
6 #![allow(unsafe_code)]
7
8 use super::super::conv::{c_int, pass_usize, ret_usize};
9 use crate::backend::c;
10 use crate::fd::OwnedFd;
11 #[cfg(feature = "param")]
12 use crate::ffi::CStr;
13 use crate::fs::{Mode, OFlags};
14 use crate::utils::{as_ptr, check_raw_pointer};
15 #[cfg(feature = "alloc")]
16 use alloc::vec::Vec;
17 use core::mem::size_of;
18 use core::ptr::{null_mut, read_unaligned, NonNull};
19 #[cfg(feature = "runtime")]
20 use core::sync::atomic::AtomicU8;
21 use core::sync::atomic::Ordering::Relaxed;
22 use core::sync::atomic::{AtomicPtr, AtomicUsize};
23 use linux_raw_sys::elf::*;
24 use linux_raw_sys::general::{
25 AT_BASE, AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_MINSIGSTKSZ, AT_NULL, AT_PAGESZ,
26 AT_SYSINFO_EHDR,
27 };
28 #[cfg(feature = "runtime")]
29 use linux_raw_sys::general::{
30 AT_EGID, AT_ENTRY, AT_EUID, AT_GID, AT_PHDR, AT_PHENT, AT_PHNUM, AT_RANDOM, AT_SECURE, AT_UID,
31 };
32 #[cfg(feature = "alloc")]
33 use {alloc::borrow::Cow, alloc::vec};
34
35 #[cfg(feature = "param")]
36 #[inline]
37 pub(crate) fn page_size() -> usize {
38 let mut page_size = PAGE_SIZE.load(Relaxed);
39
40 if page_size == 0 {
41 init_auxv();
42 page_size = PAGE_SIZE.load(Relaxed);
43 }
44
45 page_size
46 }
47
48 #[cfg(feature = "param")]
49 #[inline]
50 pub(crate) fn clock_ticks_per_second() -> u64 {
51 let mut ticks = CLOCK_TICKS_PER_SECOND.load(Relaxed);
52
53 if ticks == 0 {
54 init_auxv();
55 ticks = CLOCK_TICKS_PER_SECOND.load(Relaxed);
56 }
57
58 ticks as u64
59 }
60
61 #[cfg(feature = "param")]
62 #[inline]
63 pub(crate) fn linux_hwcap() -> (usize, usize) {
64 let mut hwcap = HWCAP.load(Relaxed);
65 let mut hwcap2 = HWCAP2.load(Relaxed);
66
67 if hwcap == 0 || hwcap2 == 0 {
68 init_auxv();
69 hwcap = HWCAP.load(Relaxed);
70 hwcap2 = HWCAP2.load(Relaxed);
71 }
72
73 (hwcap, hwcap2)
74 }
75
76 #[cfg(feature = "param")]
77 #[inline]
78 pub(crate) fn linux_minsigstksz() -> usize {
79 let mut minsigstksz = MINSIGSTKSZ.load(Relaxed);
80
81 if minsigstksz == 0 {
82 init_auxv();
83 minsigstksz = MINSIGSTKSZ.load(Relaxed);
84 }
85
86 minsigstksz
87 }
88
89 #[cfg(feature = "param")]
90 #[inline]
91 pub(crate) fn linux_execfn() -> &'static CStr {
92 let mut execfn = EXECFN.load(Relaxed);
93
94 if execfn.is_null() {
95 init_auxv();
96 execfn = EXECFN.load(Relaxed);
97 }
98
99 // SAFETY: We assume the `AT_EXECFN` value provided by the kernel is a
100 // valid pointer to a valid NUL-terminated array of bytes.
101 unsafe { CStr::from_ptr(execfn.cast()) }
102 }
103
104 #[cfg(feature = "runtime")]
105 #[inline]
106 pub(crate) fn linux_secure() -> bool {
107 let mut secure = SECURE.load(Relaxed);
108
109 // 0 means not initialized yet.
110 if secure == 0 {
111 init_auxv();
112 secure = SECURE.load(Relaxed);
113 }
114
115 // 0 means not present. Libc `getauxval(AT_SECURE)` would return 0.
116 // 1 means not in secure mode.
117 // 2 means in secure mode.
118 secure > 1
119 }
120
121 #[cfg(feature = "runtime")]
122 #[inline]
123 pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) {
124 let mut phdr = PHDR.load(Relaxed);
125 let mut phent = PHENT.load(Relaxed);
126 let mut phnum = PHNUM.load(Relaxed);
127
128 if phdr.is_null() || phnum == 0 {
129 init_auxv();
130 phdr = PHDR.load(Relaxed);
131 phent = PHENT.load(Relaxed);
132 phnum = PHNUM.load(Relaxed);
133 }
134
135 (phdr.cast(), phent, phnum)
136 }
137
138 /// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so
139 /// if we don't see it, this function returns a null pointer.
140 ///
141 /// And, this function returns a null pointer, rather than panicking, if the
142 /// auxv records can't be read.
143 #[inline]
144 pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
145 let mut ehdr = SYSINFO_EHDR.load(Relaxed);
146
147 if ehdr.is_null() {
148 // Use `maybe_init_auxv` to to read the aux vectors if it can, but do
149 // nothing if it can't. If it can't, then we'll get a null pointer
150 // here, which our callers are prepared to deal with.
151 maybe_init_auxv();
152
153 ehdr = SYSINFO_EHDR.load(Relaxed);
154 }
155
156 ehdr
157 }
158
159 #[cfg(feature = "runtime")]
160 #[inline]
161 pub(crate) fn entry() -> usize {
162 let mut entry = ENTRY.load(Relaxed);
163
164 if entry == 0 {
165 init_auxv();
166 entry = ENTRY.load(Relaxed);
167 }
168
169 entry
170 }
171
172 #[cfg(feature = "runtime")]
173 #[inline]
174 pub(crate) fn random() -> *const [u8; 16] {
175 let mut random = RANDOM.load(Relaxed);
176
177 if random.is_null() {
178 init_auxv();
179 random = RANDOM.load(Relaxed);
180 }
181
182 random
183 }
184
185 static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);
186 static CLOCK_TICKS_PER_SECOND: AtomicUsize = AtomicUsize::new(0);
187 static HWCAP: AtomicUsize = AtomicUsize::new(0);
188 static HWCAP2: AtomicUsize = AtomicUsize::new(0);
189 static MINSIGSTKSZ: AtomicUsize = AtomicUsize::new(0);
190 static EXECFN: AtomicPtr<c::c_char> = AtomicPtr::new(null_mut());
191 static SYSINFO_EHDR: AtomicPtr<Elf_Ehdr> = AtomicPtr::new(null_mut());
192 #[cfg(feature = "runtime")]
193 static SECURE: AtomicU8 = AtomicU8::new(0);
194 #[cfg(feature = "runtime")]
195 static PHDR: AtomicPtr<Elf_Phdr> = AtomicPtr::new(null_mut());
196 #[cfg(feature = "runtime")]
197 static PHENT: AtomicUsize = AtomicUsize::new(0);
198 #[cfg(feature = "runtime")]
199 static PHNUM: AtomicUsize = AtomicUsize::new(0);
200 #[cfg(feature = "runtime")]
201 static ENTRY: AtomicUsize = AtomicUsize::new(0);
202 #[cfg(feature = "runtime")]
203 static RANDOM: AtomicPtr<[u8; 16]> = AtomicPtr::new(null_mut());
204
205 const PR_GET_AUXV: c::c_int = 0x4155_5856;
206
207 /// Use Linux >= 6.4's `PR_GET_AUXV` to read the aux records, into a provided
208 /// statically-sized buffer. Return:
209 /// - `Ok(...)` if the buffer is big enough.
210 /// - `Err(Ok(len))` if we need a buffer of length `len`.
211 /// - `Err(Err(err))` if we failed with `err`.
212 #[cold]
213 fn pr_get_auxv_static(buffer: &mut [u8; 512]) -> Result<&mut [u8], crate::io::Result<usize>> {
214 let len = unsafe {
215 ret_usize(syscall_always_asm!(
216 __NR_prctl,
217 c_int(PR_GET_AUXV),
218 buffer.as_mut_ptr(),
219 pass_usize(buffer.len()),
220 pass_usize(0),
221 pass_usize(0)
222 ))
223 .map_err(Err)?
224 };
225 if len <= buffer.len() {
226 return Ok(&mut buffer[..len]);
227 }
228 Err(Ok(len))
229 }
230
231 /// Use Linux >= 6.4's `PR_GET_AUXV` to read the aux records, using a provided
232 /// statically-sized buffer if possible, or a dynamically allocated buffer
233 /// otherwise. Return:
234 /// - Ok(...) on success.
235 /// - Err(err) on failure.
236 #[cfg(feature = "alloc")]
237 #[cold]
238 fn pr_get_auxv_dynamic(buffer: &mut [u8; 512]) -> crate::io::Result<Cow<'_, [u8]>> {
239 // First try use the static buffer.
240 let len = match pr_get_auxv_static(buffer) {
241 Ok(buffer) => return Ok(Cow::Borrowed(buffer)),
242 Err(Ok(len)) => len,
243 Err(Err(err)) => return Err(err),
244 };
245
246 // If that indicates it needs a bigger buffer, allocate one.
247 let mut buffer = vec![0u8; len];
248 let len = unsafe {
249 ret_usize(syscall_always_asm!(
250 __NR_prctl,
251 c_int(PR_GET_AUXV),
252 buffer.as_mut_ptr(),
253 pass_usize(buffer.len()),
254 pass_usize(0),
255 pass_usize(0)
256 ))?
257 };
258 assert_eq!(len, buffer.len());
259 Ok(Cow::Owned(buffer))
260 }
261
262 /// Read the auxv records and initialize the various static variables. Panic
263 /// if an error is encountered.
264 #[cold]
265 fn init_auxv() {
266 init_auxv_impl().unwrap();
267 }
268
269 /// Like `init_auxv`, but don't panic if an error is encountered. The caller
270 /// must be prepared for initialization to be skipped.
271 #[cold]
272 fn maybe_init_auxv() {
273 if let Ok(()) = init_auxv_impl() {
274 return;
275 }
276 }
277
278 /// If we don't have "use-explicitly-provided-auxv" or "use-libc-auxv", we
279 /// read the aux vector via the `prctl` `PR_GET_AUXV`, with a fallback to
280 /// /proc/self/auxv for kernels that don't support `PR_GET_AUXV`.
281 #[cold]
282 fn init_auxv_impl() -> Result<(), ()> {
283 let mut buffer = [0u8; 512];
284
285 // If we don't have "alloc", just try to read into our statically-sized
286 // buffer. This might fail due to the buffer being insufficient; we're
287 // prepared to cope, though we may do suboptimal things.
288 #[cfg(not(feature = "alloc"))]
289 let result = pr_get_auxv_static(&mut buffer);
290
291 // If we do have "alloc" then read into our statically-sized buffer if
292 // it fits, or fall back to a dynamically-allocated buffer.
293 #[cfg(feature = "alloc")]
294 let result = pr_get_auxv_dynamic(&mut buffer);
295
296 if let Ok(buffer) = result {
297 // SAFETY: We assume the kernel returns a valid auxv.
298 unsafe {
299 init_from_aux_iter(AuxPointer(buffer.as_ptr().cast())).unwrap();
300 }
301 return Ok(());
302 }
303
304 // If `PR_GET_AUXV` is unavailable, or if we don't have "alloc" and
305 // the aux records don't fit in our static buffer, then fall back to trying
306 // to open "/proc/self/auxv". We don't use `proc_self_fd` because its extra
307 // checking breaks on QEMU.
308 if let Ok(file) = crate::fs::open("/proc/self/auxv", OFlags::RDONLY, Mode::empty()) {
309 #[cfg(feature = "alloc")]
310 init_from_auxv_file(file).unwrap();
311
312 #[cfg(not(feature = "alloc"))]
313 unsafe {
314 init_from_aux_iter(AuxFile(file)).unwrap();
315 }
316
317 return Ok(());
318 }
319
320 Err(())
321 }
322
323 /// Process auxv entries from the open file `auxv`.
324 #[cfg(feature = "alloc")]
325 #[cold]
326 #[must_use]
327 fn init_from_auxv_file(auxv: OwnedFd) -> Option<()> {
328 let mut buffer = Vec::<u8>::with_capacity(512);
329 loop {
330 let cur = buffer.len();
331
332 // Request one extra byte; `Vec` will often allocate more.
333 buffer.reserve(1);
334
335 // Use all the space it allocated.
336 buffer.resize(buffer.capacity(), 0);
337
338 // Read up to that many bytes.
339 let n = match crate::io::read(&auxv, &mut buffer[cur..]) {
340 Err(crate::io::Errno::INTR) => 0,
341 Err(_err) => panic!(),
342 Ok(0) => break,
343 Ok(n) => n,
344 };
345
346 // Account for the number of bytes actually read.
347 buffer.resize(cur + n, 0_u8);
348 }
349
350 // SAFETY: We loaded from an auxv file into the buffer.
351 unsafe { init_from_aux_iter(AuxPointer(buffer.as_ptr().cast())) }
352 }
353
354 /// Process auxv entries from the auxv array pointed to by `auxp`.
355 ///
356 /// # Safety
357 ///
358 /// This must be passed a pointer to an auxv array.
359 ///
360 /// The buffer contains `Elf_aux_t` elements, though it need not be aligned;
361 /// function uses `read_unaligned` to read from it.
362 #[cold]
363 #[must_use]
364 unsafe fn init_from_aux_iter(aux_iter: impl Iterator<Item = Elf_auxv_t>) -> Option<()> {
365 let mut pagesz = 0;
366 let mut clktck = 0;
367 let mut hwcap = 0;
368 let mut hwcap2 = 0;
369 let mut minsigstksz = 0;
370 let mut execfn = null_mut();
371 let mut sysinfo_ehdr = null_mut();
372 #[cfg(feature = "runtime")]
373 let mut secure = 0;
374 #[cfg(feature = "runtime")]
375 let mut phdr = null_mut();
376 #[cfg(feature = "runtime")]
377 let mut phnum = 0;
378 #[cfg(feature = "runtime")]
379 let mut phent = 0;
380 #[cfg(feature = "runtime")]
381 let mut entry = 0;
382 #[cfg(feature = "runtime")]
383 let mut uid = None;
384 #[cfg(feature = "runtime")]
385 let mut euid = None;
386 #[cfg(feature = "runtime")]
387 let mut gid = None;
388 #[cfg(feature = "runtime")]
389 let mut egid = None;
390 #[cfg(feature = "runtime")]
391 let mut random = null_mut();
392
393 for Elf_auxv_t { a_type, a_val } in aux_iter {
394 match a_type as _ {
395 AT_PAGESZ => pagesz = a_val as usize,
396 AT_CLKTCK => clktck = a_val as usize,
397 AT_HWCAP => hwcap = a_val as usize,
398 AT_HWCAP2 => hwcap2 = a_val as usize,
399 AT_MINSIGSTKSZ => minsigstksz = a_val as usize,
400 AT_EXECFN => execfn = check_raw_pointer::<c::c_char>(a_val as *mut _)?.as_ptr(),
401 AT_SYSINFO_EHDR => sysinfo_ehdr = check_elf_base(a_val as *mut _)?.as_ptr(),
402
403 AT_BASE => {
404 // The `AT_BASE` value can be NULL in a static executable that
405 // doesn't use a dynamic linker. If so, ignore it.
406 if !a_val.is_null() {
407 let _ = check_elf_base(a_val.cast())?;
408 }
409 }
410
411 #[cfg(feature = "runtime")]
412 AT_SECURE => secure = (a_val as usize != 0) as u8 + 1,
413 #[cfg(feature = "runtime")]
414 AT_UID => uid = Some(a_val),
415 #[cfg(feature = "runtime")]
416 AT_EUID => euid = Some(a_val),
417 #[cfg(feature = "runtime")]
418 AT_GID => gid = Some(a_val),
419 #[cfg(feature = "runtime")]
420 AT_EGID => egid = Some(a_val),
421 #[cfg(feature = "runtime")]
422 AT_PHDR => phdr = check_raw_pointer::<Elf_Phdr>(a_val as *mut _)?.as_ptr(),
423 #[cfg(feature = "runtime")]
424 AT_PHNUM => phnum = a_val as usize,
425 #[cfg(feature = "runtime")]
426 AT_PHENT => phent = a_val as usize,
427 #[cfg(feature = "runtime")]
428 AT_ENTRY => entry = a_val as usize,
429 #[cfg(feature = "runtime")]
430 AT_RANDOM => random = check_raw_pointer::<[u8; 16]>(a_val as *mut _)?.as_ptr(),
431
432 AT_NULL => break,
433 _ => (),
434 }
435 }
436
437 #[cfg(feature = "runtime")]
438 assert_eq!(phent, size_of::<Elf_Phdr>());
439
440 // If we're running set-uid or set-gid, enable “secure execution” mode,
441 // which doesn't do much, but users may be depending on the things that
442 // it does do.
443 #[cfg(feature = "runtime")]
444 if uid != euid || gid != egid {
445 secure = 2;
446 }
447
448 // The base and sysinfo_ehdr (if present) matches our platform. Accept the
449 // aux values.
450 PAGE_SIZE.store(pagesz, Relaxed);
451 CLOCK_TICKS_PER_SECOND.store(clktck, Relaxed);
452 HWCAP.store(hwcap, Relaxed);
453 HWCAP2.store(hwcap2, Relaxed);
454 MINSIGSTKSZ.store(minsigstksz, Relaxed);
455 EXECFN.store(execfn, Relaxed);
456 SYSINFO_EHDR.store(sysinfo_ehdr, Relaxed);
457 #[cfg(feature = "runtime")]
458 SECURE.store(secure, Relaxed);
459 #[cfg(feature = "runtime")]
460 PHDR.store(phdr, Relaxed);
461 #[cfg(feature = "runtime")]
462 PHNUM.store(phnum, Relaxed);
463 #[cfg(feature = "runtime")]
464 ENTRY.store(entry, Relaxed);
465 #[cfg(feature = "runtime")]
466 RANDOM.store(random, Relaxed);
467
468 Some(())
469 }
470
471 /// Check that `base` is a valid pointer to the kernel-provided vDSO.
472 ///
473 /// `base` is some value we got from a `AT_SYSINFO_EHDR` aux record somewhere,
474 /// which hopefully holds the value of the kernel-provided vDSO in memory. Do a
475 /// series of checks to be as sure as we can that it's safe to use.
476 #[cold]
477 #[must_use]
478 unsafe fn check_elf_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> {
479 // If we're reading a 64-bit auxv on a 32-bit platform, we'll see a zero
480 // `a_val` because `AT_*` values are never greater than `u32::MAX`. Zero is
481 // used by libc's `getauxval` to indicate errors, so it should never be a
482 // valid value.
483 if base.is_null() {
484 return None;
485 }
486
487 let hdr = match check_raw_pointer::<Elf_Ehdr>(base as *mut _) {
488 Some(hdr) => hdr,
489 None => return None,
490 };
491
492 let hdr = hdr.as_ref();
493 if hdr.e_ident[..SELFMAG] != ELFMAG {
494 return None; // Wrong ELF magic
495 }
496 if !matches!(hdr.e_ident[EI_OSABI], ELFOSABI_SYSV | ELFOSABI_LINUX) {
497 return None; // Unrecognized ELF OS ABI
498 }
499 if hdr.e_ident[EI_ABIVERSION] != ELFABIVERSION {
500 return None; // Unrecognized ELF ABI version
501 }
502 if hdr.e_type != ET_DYN {
503 return None; // Wrong ELF type
504 }
505
506 // If ELF is extended, we'll need to adjust.
507 if hdr.e_ident[EI_VERSION] != EV_CURRENT
508 || hdr.e_ehsize as usize != size_of::<Elf_Ehdr>()
509 || hdr.e_phentsize as usize != size_of::<Elf_Phdr>()
510 {
511 return None;
512 }
513 // We don't currently support extra-large numbers of segments.
514 if hdr.e_phnum == PN_XNUM {
515 return None;
516 }
517
518 // If `e_phoff` is zero, it's more likely that we're looking at memory that
519 // has been zeroed than that the kernel has somehow aliased the `Ehdr` and
520 // the `Phdr`.
521 if hdr.e_phoff < size_of::<Elf_Ehdr>() {
522 return None;
523 }
524
525 // Verify that the `EI_CLASS`/`EI_DATA`/`e_machine` fields match the
526 // architecture we're running as. This helps catch cases where we're
527 // running under QEMU.
528 if hdr.e_ident[EI_CLASS] != ELFCLASS {
529 return None; // Wrong ELF class
530 }
531 if hdr.e_ident[EI_DATA] != ELFDATA {
532 return None; // Wrong ELF data
533 }
534 if hdr.e_machine != EM_CURRENT {
535 return None; // Wrong machine type
536 }
537
538 Some(NonNull::new_unchecked(as_ptr(hdr) as *mut _))
539 }
540
541 // Aux reading utilities
542
543 // Read auxv records from an array in memory.
544 struct AuxPointer(*const Elf_auxv_t);
545
546 impl Iterator for AuxPointer {
547 type Item = Elf_auxv_t;
548
549 #[cold]
550 fn next(&mut self) -> Option<Self::Item> {
551 unsafe {
552 let value = read_unaligned(self.0);
553 self.0 = self.0.add(1);
554 Some(value)
555 }
556 }
557 }
558
559 // Read auxv records from a file.
560 #[cfg(not(feature = "alloc"))]
561 struct AuxFile(OwnedFd);
562
563 #[cfg(not(feature = "alloc"))]
564 impl Iterator for AuxFile {
565 type Item = Elf_auxv_t;
566
567 // This implementation does lots of `read`s and it isn't amazing, but
568 // hopefully we won't use it often.
569 #[cold]
570 fn next(&mut self) -> Option<Self::Item> {
571 let mut buf = [0_u8; size_of::<Self::Item>()];
572 let mut slice = &mut buf[..];
573 while !slice.is_empty() {
574 match crate::io::read(&self.0, slice) {
575 Ok(0) => panic!("unexpected end of auxv file"),
576 Ok(n) => slice = &mut slice[n..],
577 Err(crate::io::Errno::INTR) => continue,
578 Err(err) => panic!("{:?}", err),
579 }
580 }
581 Some(unsafe { read_unaligned(buf.as_ptr().cast()) })
582 }
583 }