@@ -15,60 +15,56 @@ use crate::{
1515///
1616/// # Invariants
1717///
18- /// The `secdata` and `seclen` fields correspond to a valid security context as returned by a
19- /// successful call to `security_secid_to_secctx`, that has not yet been destroyed by calling
20- /// `security_release_secctx`.
18+ /// The `ctx` field corresponds to a valid security context as returned by a successful call to
19+ /// `security_secid_to_secctx`, that has not yet been destroyed by `security_release_secctx`.
2120pub struct SecurityCtx {
22- secdata : * mut core:: ffi:: c_char ,
23- seclen : usize ,
21+ ctx : bindings:: lsm_context ,
2422}
2523
2624impl SecurityCtx {
2725 /// Get the security context given its id.
2826 pub fn from_secid ( secid : u32 ) -> Result < Self > {
29- let mut secdata = core:: ptr:: null_mut ( ) ;
30- let mut seclen = 0u32 ;
31- // SAFETY: Just a C FFI call. The pointers are valid for writes.
32- to_result ( unsafe { bindings:: security_secid_to_secctx ( secid, & mut secdata, & mut seclen) } ) ?;
27+ // SAFETY: `struct lsm_context` can be initialized to all zeros.
28+ let mut ctx: bindings:: lsm_context = unsafe { core:: mem:: zeroed ( ) } ;
29+
30+ // SAFETY: Just a C FFI call. The pointer is valid for writes.
31+ to_result ( unsafe { bindings:: security_secid_to_secctx ( secid, & mut ctx) } ) ?;
3332
3433 // INVARIANT: If the above call did not fail, then we have a valid security context.
35- Ok ( Self {
36- secdata,
37- seclen : seclen as usize ,
38- } )
34+ Ok ( Self { ctx } )
3935 }
4036
4137 /// Returns whether the security context is empty.
4238 pub fn is_empty ( & self ) -> bool {
43- self . seclen == 0
39+ self . ctx . len == 0
4440 }
4541
4642 /// Returns the length of this security context.
4743 pub fn len ( & self ) -> usize {
48- self . seclen
44+ self . ctx . len as usize
4945 }
5046
5147 /// Returns the bytes for this security context.
5248 pub fn as_bytes ( & self ) -> & [ u8 ] {
53- let ptr = self . secdata ;
49+ let ptr = self . ctx . context ;
5450 if ptr. is_null ( ) {
55- debug_assert_eq ! ( self . seclen , 0 ) ;
51+ debug_assert_eq ! ( self . len ( ) , 0 ) ;
5652 // We can't pass a null pointer to `slice::from_raw_parts` even if the length is zero.
5753 return & [ ] ;
5854 }
5955
6056 // SAFETY: The call to `security_secid_to_secctx` guarantees that the pointer is valid for
61- // `seclen ` bytes. Furthermore, if the length is zero, then we have ensured that the
57+ // `self.len() ` bytes. Furthermore, if the length is zero, then we have ensured that the
6258 // pointer is not null.
63- unsafe { core:: slice:: from_raw_parts ( ptr. cast ( ) , self . seclen ) }
59+ unsafe { core:: slice:: from_raw_parts ( ptr. cast ( ) , self . len ( ) ) }
6460 }
6561}
6662
6763impl Drop for SecurityCtx {
6864 fn drop ( & mut self ) {
69- // SAFETY: By the invariant of `Self`, this frees a pointer that came from a successful
65+ // SAFETY: By the invariant of `Self`, this frees a context that came from a successful
7066 // call to `security_secid_to_secctx` and has not yet been destroyed by
7167 // `security_release_secctx`.
72- unsafe { bindings:: security_release_secctx ( self . secdata , self . seclen as u32 ) } ;
68+ unsafe { bindings:: security_release_secctx ( & mut self . ctx ) } ;
7369 }
7470}
0 commit comments