Skip to content

Commit 989e55b

Browse files
mendessdanielocfb
authored andcommitted
Remove UB caused by box aliasing
1 parent bff4ec9 commit 989e55b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

libbpf-rs/src/skeleton.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ impl<'a> ObjectSkeletonConfigBuilder<'a> {
181181
s.data = self.data.as_ptr() as *mut c_void;
182182
s.data_sz = self.data.len() as c_ulong;
183183

184-
s.obj = &mut *self.p;
184+
// Give s ownership over the box
185+
s.obj = Box::into_raw(self.p);
185186

186187
let maps_layout = Self::build_maps(&mut self.maps, &mut s, &mut string_pool);
187188
let progs_layout = Self::build_progs(&mut self.progs, &mut s, &mut string_pool);
188189

189190
Ok(ObjectSkeletonConfig {
190191
inner: s,
191-
obj: self.p,
192192
maps: self.maps,
193193
progs: self.progs,
194194
maps_layout,
@@ -209,7 +209,6 @@ impl<'a> ObjectSkeletonConfigBuilder<'a> {
209209
#[derive(Debug)]
210210
pub struct ObjectSkeletonConfig<'a> {
211211
inner: bpf_object_skeleton,
212-
obj: Box<*mut bpf_object>,
213212
maps: Vec<MapSkelConfig>,
214213
progs: Vec<ProgSkelConfig>,
215214
/// Layout necessary to `dealloc` memory
@@ -229,7 +228,7 @@ impl<'a> ObjectSkeletonConfig<'a> {
229228

230229
/// Warning: the returned pointer is only valid while the `ObjectSkeletonConfig` is alive.
231230
pub fn object_ptr(&mut self) -> *mut bpf_object {
232-
*self.obj
231+
unsafe { *self.inner.obj }
233232
}
234233

235234
/// Returns the `mmaped` pointer for a map at the specified `index`.
@@ -291,5 +290,7 @@ impl<'a> Drop for ObjectSkeletonConfig<'a> {
291290
dealloc(self.inner.progs as _, layout);
292291
}
293292
}
293+
294+
unsafe { Box::from_raw(self.inner.obj) };
294295
}
295296
}

0 commit comments

Comments
 (0)