| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::{
|
| 10 | bindings,
|
| 11 | cred::Credential,
|
| 12 | error::{to_result, Result},
|
| 13 | fs::File,
|
| 14 | };
|
| 15 |
|
| 16 | |
| 17 | |
| 18 | #[inline]
|
| 19 | pub fn binder_set_context_mgr(mgr: &Credential) -> Result {
|
| 20 | |
| 21 | to_result(unsafe { bindings::security_binder_set_context_mgr(mgr.as_ptr()) })
|
| 22 | }
|
| 23 |
|
| 24 | |
| 25 | |
| 26 | #[inline]
|
| 27 | pub fn binder_transaction(from: &Credential, to: &Credential) -> Result {
|
| 28 | |
| 29 | to_result(unsafe { bindings::security_binder_transaction(from.as_ptr(), to.as_ptr()) })
|
| 30 | }
|
| 31 |
|
| 32 | |
| 33 | |
| 34 | #[inline]
|
| 35 | pub fn binder_transfer_binder(from: &Credential, to: &Credential) -> Result {
|
| 36 | |
| 37 | to_result(unsafe { bindings::security_binder_transfer_binder(from.as_ptr(), to.as_ptr()) })
|
| 38 | }
|
| 39 |
|
| 40 | |
| 41 | |
| 42 | #[inline]
|
| 43 | pub fn binder_transfer_file(from: &Credential, to: &Credential, file: &File) -> Result {
|
| 44 | |
| 45 | |
| 46 | to_result(unsafe {
|
| 47 | bindings::security_binder_transfer_file(from.as_ptr(), to.as_ptr(), file.as_ptr())
|
| 48 | })
|
| 49 | }
|
| 50 |
|
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | pub struct SecurityCtx {
|
| 58 | ctx: bindings::lsm_context,
|
| 59 | }
|
| 60 |
|
| 61 | impl SecurityCtx {
|
| 62 | |
| 63 | #[inline]
|
| 64 | pub fn from_secid(secid: u32) -> Result<Self> {
|
| 65 | |
| 66 | let mut ctx: bindings::lsm_context = unsafe { core::mem::zeroed() };
|
| 67 |
|
| 68 | |
| 69 | to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut ctx) })?;
|
| 70 |
|
| 71 | |
| 72 | Ok(Self { ctx })
|
| 73 | }
|
| 74 |
|
| 75 | |
| 76 | #[inline]
|
| 77 | pub fn is_empty(&self) -> bool {
|
| 78 | self.ctx.len == 0
|
| 79 | }
|
| 80 |
|
| 81 | |
| 82 | #[inline]
|
| 83 | pub fn len(&self) -> usize {
|
| 84 | self.ctx.len as usize
|
| 85 | }
|
| 86 |
|
| 87 | |
| 88 | #[inline]
|
| 89 | pub fn as_bytes(&self) -> &[u8] {
|
| 90 | let ptr = self.ctx.context;
|
| 91 | if ptr.is_null() {
|
| 92 | debug_assert_eq!(self.len(), 0);
|
| 93 | |
| 94 | return &[];
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | |
| 99 | |
| 100 | unsafe { core::slice::from_raw_parts(ptr.cast(), self.len()) }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | impl Drop for SecurityCtx {
|
| 105 | #[inline]
|
| 106 | fn drop(&mut self) {
|
| 107 | |
| 108 | |
| 109 | unsafe { bindings::security_release_secctx(&mut self.ctx) };
|
| 110 | }
|
| 111 | }
|