Skip to content

Commit b028f2d

Browse files
Devindevin-ai-integration[bot]
andcommitted
Drive remaining unsafe to the sys boundary
Second sweep: every remaining ad-hoc unsafe block in business logic now delegates to src/sys.rs wrappers or uses plain safe Rust. - tracee/mem.rs: process_vm_*/PTRACE_PEEK|POKEDATA via sys::; the errno-carrying word primitives keep C's clear-errno convention. File is now unsafe-free. - tracee/reg.rs: byte-offset register access rewritten as slice ops on sys::as_bytes views of user_regs_struct — no more pointer arithmetic. - tracee/seccomp.rs: transmute_copy(&statfs64.f_fsid) replaced by an offset_of!+as_bytes read; getresuid/getresgid/statfs64 via sys::. - syscall/{enter,exit,socket,seccomp,rlimit,heap}.rs: errno clears, lstat/stat/readlink/prlimit/prctl calls via sys::; sockaddr_un path copies use safe zip loops; ioctl/poll wrappers. - execve/{enter,exit,ldso,shebang,auxv}.rs: pread/open/access/fchmod via sys::; iterate_program_headers closure no longer smuggles raw pointers (plain captures suffice); loader File via sys::file_from_fd. - path/{mod,canon,binding,glue,f2fs}.rs: lstat/realpath/getcwd/readlink/ mkdir/mknod/fork/waitpid via sys::; fix_symlink_size's misaligned buf.cast::<stat>() replaced by a typed struct (same latent UB fix as fake_id0). - extension/sysvipc/*: helper-process socket glue (pipe/fork/dup2/ exec/listen/accept/read/write) via sys::; SCM_RIGHTS cmsghdr built by byte layout instead of CMSG_* macros; tmpfile() replaced by an O_TMPFILE wrapper. - ptrace/{mod,wait}.rs: ptrace emulation calls via sys::ptrace (which now takes usize words — no raw-pointer args in a safe fn). - extension/kompat.rs uname, cli/mod.rs setenv/getpid, main.rs atexit, lib.rs strerror — all via sys::. - sys.rs gains: process_vm_read/write/writev_bufs, prlimit64, dup2, file_from_fd, tmpfile_fd, statfs64, strerror. Result: `unsafe` outside the kernel boundary dropped from ~276 sites across 43 files to 12 (sys.rs=100 audited wrappers, elf.rs=8 POD-union accessors, event.rs=3 signal/uname sites, netlink.rs=1 documented CStr::from_ptr). Every remaining site carries a SAFETY comment; unsafe_op_in_unsafe_fn stays denied. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent aabe707 commit b028f2d

37 files changed

Lines changed: 676 additions & 713 deletions

‎src/cli/mod.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,8 @@ fn initialize_cwd(tracee: &mut Tracee) -> Result<(), i32> {
923923
path.chop_finality();
924924

925925
tracee.fs.borrow_mut().cwd = path.clone();
926-
unsafe { libc::setenv(c"PWD".as_ptr(), path.as_c_bytes().as_ptr() as *const _, 1) };
926+
let value = std::ffi::CString::new(path.as_bytes()).unwrap_or_default();
927+
crate::sys::setenv(c"PWD", &value, true);
927928
Ok(())
928929
}
929930

@@ -1160,7 +1161,7 @@ fn expand_front_variable(string: &str) -> String {
11601161
pub fn run(tracee_rc: &TraceeRef, args: &[String]) -> i32 {
11611162
{
11621163
let mut t = tracee_rc.borrow_mut();
1163-
let pid = unsafe { libc::getpid() };
1164+
let pid = crate::sys::getpid();
11641165
let old_key = t.pid;
11651166
t.pid = pid;
11661167
drop(t);

‎src/execve/aoxp.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,5 @@ pub fn push_array_of_xpointers(tracee: &mut Tracee, array: &mut XPointerArray, r
233233

234234
/// Clear thread-local errno before a `peek_word` (mirrors `errno = 0` in C).
235235
fn clear_errno() {
236-
unsafe {
237-
*libc::__errno_location() = 0;
238-
}
236+
crate::sys::clear_errno();
239237
}

‎src/execve/auxv.rs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ pub struct ElfAuxVector {
3737
pub fn get_elf_aux_vectors_address(tracee: &Tracee) -> Word {
3838
let w = sizeof_word(tracee) as Word;
3939
let mut address = peek_reg(tracee, RegVersion::Current, Reg::StackPointer);
40-
unsafe { *libc::__errno_location() = 0 };
40+
crate::sys::clear_errno();
4141

4242
// argc, then argv[] + NULL.
4343
let argc = peek_word(tracee, address);
44-
if crate::path::errno() != 0 {
44+
if crate::sys::errno() != 0 {
4545
return 0;
4646
}
4747
address += (1 + argc + 1) * w;
4848

4949
// envp[] + NULL.
5050
loop {
5151
let data = peek_word(tracee, address);
52-
if crate::path::errno() != 0 {
52+
if crate::sys::errno() != 0 {
5353
return 0;
5454
}
5555
address += w;
@@ -66,17 +66,17 @@ pub fn fetch_elf_aux_vectors(tracee: &Tracee, address: Word) -> Option<Vec<ElfAu
6666
let mut address = address;
6767
let mut vectors = Vec::new();
6868
loop {
69-
unsafe { *libc::__errno_location() = 0 };
69+
crate::sys::clear_errno();
7070
let atype = peek_word(tracee, address);
71-
if crate::path::errno() != 0 {
71+
if crate::sys::errno() != 0 {
7272
return None;
7373
}
7474
address += w;
7575
if atype == AT_NULL {
7676
break;
7777
}
7878
let value = peek_word(tracee, address);
79-
if crate::path::errno() != 0 {
79+
if crate::sys::errno() != 0 {
8080
return None;
8181
}
8282
address += w;
@@ -97,15 +97,15 @@ pub fn push_elf_aux_vectors(tracee: &Tracee, vectors: &[ElfAuxVector], address:
9797
.iter()
9898
.chain(std::iter::once(&ElfAuxVector::default()))
9999
{
100-
unsafe { *libc::__errno_location() = 0 };
100+
crate::sys::clear_errno();
101101
poke_word(tracee, address, v.atype);
102-
if crate::path::errno() != 0 {
103-
return -crate::path::errno();
102+
if crate::sys::errno() != 0 {
103+
return -crate::sys::errno();
104104
}
105105
address += w;
106106
poke_word(tracee, address, v.value);
107-
if crate::path::errno() != 0 {
108-
return -crate::path::errno();
107+
if crate::sys::errno() != 0 {
108+
return -crate::sys::errno();
109109
}
110110
address += w;
111111
if v.atype == AT_NULL {

‎src/execve/elf.rs‎

Lines changed: 86 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,42 @@ pub const DT_RPATH: i64 = 15;
121121
pub const DT_RUNPATH: i64 = 29;
122122

123123
impl ElfHeader {
124+
/// POD-union views. Both variants are plain-data layouts sharing the
125+
/// same storage; every bit pattern is a valid value for either, so
126+
/// a mis-guessed read is a logic error, never UB.
127+
#[inline]
128+
fn as32(&self) -> &ElfHeader32 {
129+
// SAFETY: ElfHeader32 is all-POD; any stored bit pattern is valid.
130+
unsafe { &self.class32 }
131+
}
132+
#[inline]
133+
fn as64(&self) -> &ElfHeader64 {
134+
// SAFETY: ElfHeader64 is all-POD; any stored bit pattern is valid.
135+
unsafe { &self.class64 }
136+
}
137+
#[inline]
138+
fn as32_mut(&mut self) -> &mut ElfHeader32 {
139+
// SAFETY: ElfHeader32 is all-POD; any stored bit pattern is valid.
140+
unsafe { &mut self.class32 }
141+
}
142+
#[inline]
143+
fn as64_mut(&mut self) -> &mut ElfHeader64 {
144+
// SAFETY: ElfHeader64 is all-POD; any stored bit pattern is valid.
145+
unsafe { &mut self.class64 }
146+
}
147+
/// Mutate `e_entry` in the active class.
148+
pub fn set_entry_bias(&mut self, delta: u64) {
149+
if self.is_class64() {
150+
let e = self.as64_mut().e_entry;
151+
self.as64_mut().e_entry = e.wrapping_add(delta);
152+
} else {
153+
let e = self.as32_mut().e_entry;
154+
self.as32_mut().e_entry = e.wrapping_add(delta as u32);
155+
}
156+
}
124157
#[inline]
125158
pub fn ident(&self, index: usize) -> u8 {
126-
unsafe { self.class32.e_ident[index] }
159+
self.as32().e_ident[index]
127160
}
128161
#[inline]
129162
pub fn is_class32(&self) -> bool {
@@ -140,19 +173,17 @@ impl ElfHeader {
140173
f32: impl Fn(&ElfHeader32) -> u32,
141174
f64: impl Fn(&ElfHeader64) -> u64,
142175
) -> u64 {
143-
unsafe {
144-
if self.is_class64() {
145-
f64(&self.class64)
146-
} else {
147-
f32(&self.class32) as u64
148-
}
176+
if self.is_class64() {
177+
f64(self.as64())
178+
} else {
179+
f32(self.as32()) as u64
149180
}
150181
}
151182
pub fn e_type(&self) -> u16 {
152-
unsafe { self.class64.e_type }
183+
self.as64().e_type
153184
}
154185
pub fn e_machine(&self) -> u16 {
155-
unsafe { self.class64.e_machine }
186+
self.as64().e_machine
156187
}
157188
pub fn e_entry(&self) -> u64 {
158189
self.field64(|h| h.e_entry, |h| h.e_entry)
@@ -161,30 +192,39 @@ impl ElfHeader {
161192
self.field64(|h| h.e_phoff, |h| h.e_phoff)
162193
}
163194
pub fn e_phentsize(&self) -> u16 {
164-
unsafe { self.class64.e_phentsize }
195+
self.as64().e_phentsize
165196
}
166197
pub fn e_phnum(&self) -> u16 {
167-
unsafe { self.class64.e_phnum }
198+
self.as64().e_phnum
168199
}
169200
pub fn is_position_independent(&self) -> bool {
170201
self.e_type() == ET_DYN
171202
}
172203
}
173204

174205
impl ProgramHeader {
206+
/// POD-union views — see `ElfHeader::as32`.
207+
#[inline]
208+
fn as32(&self) -> &ProgramHeader32 {
209+
// SAFETY: all-POD variant; any stored bit pattern is valid.
210+
unsafe { &self.class32 }
211+
}
212+
#[inline]
213+
fn as64(&self) -> &ProgramHeader64 {
214+
// SAFETY: all-POD variant; any stored bit pattern is valid.
215+
unsafe { &self.class64 }
216+
}
175217
#[inline]
176218
pub fn field(
177219
&self,
178220
ehdr: &ElfHeader,
179221
f32: impl Fn(&ProgramHeader32) -> u32,
180222
f64: impl Fn(&ProgramHeader64) -> u64,
181223
) -> u64 {
182-
unsafe {
183-
if ehdr.is_class64() {
184-
f64(&self.class64)
185-
} else {
186-
f32(&self.class32) as u64
187-
}
224+
if ehdr.is_class64() {
225+
f64(self.as64())
226+
} else {
227+
f32(self.as32()) as u64
188228
}
189229
}
190230
pub fn p_type(&self, ehdr: &ElfHeader) -> u64 {
@@ -208,19 +248,28 @@ impl ProgramHeader {
208248
}
209249

210250
impl DynamicEntry {
251+
/// POD-union views — see `ElfHeader::as32`.
252+
#[inline]
253+
fn as32(&self) -> &DynamicEntry32 {
254+
// SAFETY: all-POD variant; any stored bit pattern is valid.
255+
unsafe { &self.class32 }
256+
}
257+
#[inline]
258+
fn as64(&self) -> &DynamicEntry64 {
259+
// SAFETY: all-POD variant; any stored bit pattern is valid.
260+
unsafe { &self.class64 }
261+
}
211262
#[inline]
212263
pub fn field(
213264
&self,
214265
ehdr: &ElfHeader,
215266
f32: impl Fn(&DynamicEntry32) -> u32,
216267
f64: impl Fn(&DynamicEntry64) -> u64,
217268
) -> u64 {
218-
unsafe {
219-
if ehdr.is_class64() {
220-
f64(&self.class64)
221-
} else {
222-
f32(&self.class32) as u64
223-
}
269+
if ehdr.is_class64() {
270+
f64(self.as64())
271+
} else {
272+
f32(self.as32()) as u64
224273
}
225274
}
226275
pub fn d_tag(&self, ehdr: &ElfHeader) -> i64 {
@@ -240,32 +289,26 @@ pub fn known_phentsize(header: &ElfHeader, size: u64) -> bool {
240289
/// returns Ok((fd, header)); the fd is left open at offset 0.
241290
pub fn open_elf(t_path: &[u8]) -> Result<(RawFd, ElfHeader), i32> {
242291
let c = std::ffi::CString::new(t_path).map_err(|_| -libc::EINVAL)?;
243-
let fd = unsafe { libc::open(c.as_ptr(), libc::O_RDONLY) };
292+
let fd = crate::sys::open(&c, libc::O_RDONLY, 0);
244293
if fd < 0 {
245-
return Err(-crate::path::errno());
294+
return Err(-crate::sys::errno());
246295
}
247-
let mut ehdr: ElfHeader = unsafe { std::mem::zeroed() };
248-
let n = unsafe {
249-
libc::read(
250-
fd,
251-
&mut ehdr as *mut _ as *mut libc::c_void,
252-
std::mem::size_of::<ElfHeader>(),
253-
)
254-
};
296+
let mut ehdr: ElfHeader = crate::sys::zeroed();
297+
let n = crate::sys::read(fd, crate::sys::as_bytes_mut(&mut ehdr));
255298
if n < std::mem::size_of::<ElfHeader32>() as isize {
256-
unsafe { libc::close(fd) };
299+
crate::sys::close(fd);
257300
return Err(-libc::ENOEXEC);
258301
}
259302
if ehdr.ident(0) != 0x7f
260303
|| ehdr.ident(1) != b'E'
261304
|| ehdr.ident(2) != b'L'
262305
|| ehdr.ident(3) != b'F'
263306
{
264-
unsafe { libc::close(fd) };
307+
crate::sys::close(fd);
265308
return Err(-libc::ENOEXEC);
266309
}
267310
if !ehdr.is_class32() && !ehdr.is_class64() {
268-
unsafe { libc::close(fd) };
311+
crate::sys::close(fd);
269312
return Err(-libc::ENOEXEC);
270313
}
271314
Ok((fd, ehdr))
@@ -282,7 +325,7 @@ pub fn is_host_elf(tracee: &Tracee, t_path: &[u8]) -> bool {
282325
}
283326
match open_elf(t_path) {
284327
Ok((fd, ehdr)) => {
285-
unsafe { libc::close(fd) };
328+
crate::sys::close(fd);
286329
if crate::arch::HOST_ELF_MACHINE.contains(&ehdr.e_machine()) {
287330
crate::verbose!(
288331
Some(tracee),
@@ -313,16 +356,13 @@ pub fn iterate_program_headers(
313356
return -libc::EINVAL;
314357
}
315358
for i in 0..phnum {
316-
let mut phdr: ProgramHeader = unsafe { std::mem::zeroed() };
359+
let mut phdr: ProgramHeader = crate::sys::zeroed();
317360
let off = phoff + i * phentsize;
318-
let n = unsafe {
319-
libc::pread(
320-
fd,
321-
&mut phdr as *mut _ as *mut libc::c_void,
322-
phentsize as usize,
323-
off as i64,
324-
)
325-
};
361+
let n = crate::sys::pread(
362+
fd,
363+
&mut crate::sys::as_bytes_mut(&mut phdr)[..phentsize as usize],
364+
off as i64,
365+
);
326366
if n != phentsize as isize {
327367
return -libc::EIO;
328368
}

0 commit comments

Comments
 (0)