Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 15bdc7d

Browse files
geofftalex
authored andcommitted
user_ptr: Use wrapping_add instead of add to avoid UB
`pointer::add` and `pointer::offset` turn into a `getelementptr inbounds`, which is UB if it does not point to a valid object or one past a valid object (i.e., it enables compiler optimizations that make that assumption). Raw pointers to userspace are not pointers to valid objects. `pointer::wrapping_add` and `pointer::wrapping_offset` turn into a `getelementptr`, which is always defined (and so they're both safe).
1 parent 0e28e5c commit 15bdc7d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/user_ptr.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ impl UserSlicePtrReader {
6161
if res != 0 {
6262
return Err(error::Error::EFAULT);
6363
}
64-
unsafe {
65-
self.0 = self.0.add(data.len());
66-
}
64+
// Since this is not a pointer to a valid object in our program,
65+
// we cannot use `add`, which has C-style rules for defined
66+
// behavior.
67+
self.0 = self.0.wrapping_offset(data.len());
6768
self.1 -= data.len();
6869
return Ok(());
6970
}
@@ -86,9 +87,10 @@ impl UserSlicePtrWriter {
8687
if res != 0 {
8788
return Err(error::Error::EFAULT);
8889
}
89-
unsafe {
90-
self.0 = self.0.add(data.len());
91-
}
90+
// Since this is not a pointer to a valid object in our program,
91+
// we cannot use `add`, which has C-style rules for defined
92+
// behavior.
93+
self.0 = self.0.wrapping_offset(data.len());
9294
self.1 -= data.len();
9395
Ok(())
9496
}

0 commit comments

Comments
 (0)