|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Linux Security Modules (LSM). |
| 4 | +//! |
| 5 | +//! C header: [`include/linux/security.h`](../../../../include/linux/security.h). |
| 6 | +
|
| 7 | +use crate::{bindings, c_types, error::Error, file::File, task::Task, Result}; |
| 8 | + |
| 9 | +extern "C" { |
| 10 | + #[allow(improper_ctypes)] |
| 11 | + fn rust_helper_security_binder_set_context_mgr( |
| 12 | + mgr: *mut bindings::task_struct, |
| 13 | + ) -> c_types::c_int; |
| 14 | + #[allow(improper_ctypes)] |
| 15 | + fn rust_helper_security_binder_transaction( |
| 16 | + from: *mut bindings::task_struct, |
| 17 | + to: *mut bindings::task_struct, |
| 18 | + ) -> c_types::c_int; |
| 19 | + #[allow(improper_ctypes)] |
| 20 | + fn rust_helper_security_binder_transfer_binder( |
| 21 | + from: *mut bindings::task_struct, |
| 22 | + to: *mut bindings::task_struct, |
| 23 | + ) -> c_types::c_int; |
| 24 | + #[allow(improper_ctypes)] |
| 25 | + fn rust_helper_security_binder_transfer_file( |
| 26 | + from: *mut bindings::task_struct, |
| 27 | + to: *mut bindings::task_struct, |
| 28 | + file: *mut bindings::file, |
| 29 | + ) -> c_types::c_int; |
| 30 | +} |
| 31 | + |
| 32 | +/// Calls the security modules to determine if the given task can become the manager of a binder |
| 33 | +/// context. |
| 34 | +pub fn binder_set_context_mgr(mgr: &Task) -> Result { |
| 35 | + // SAFETY: By the `Task` invariants, `mgr.ptr` is valid. |
| 36 | + let ret = unsafe { rust_helper_security_binder_set_context_mgr(mgr.ptr) }; |
| 37 | + if ret != 0 { |
| 38 | + Err(Error::from_kernel_errno(ret)) |
| 39 | + } else { |
| 40 | + Ok(()) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Calls the security modules to determine if binder transactions are allowed from task `from` to |
| 45 | +/// task `to`. |
| 46 | +pub fn binder_transaction(from: &Task, to: &Task) -> Result { |
| 47 | + // SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid. |
| 48 | + let ret = unsafe { rust_helper_security_binder_transaction(from.ptr, to.ptr) }; |
| 49 | + if ret != 0 { |
| 50 | + Err(Error::from_kernel_errno(ret)) |
| 51 | + } else { |
| 52 | + Ok(()) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Calls the security modules to determine if task `from` is allowed to send binder objects |
| 57 | +/// (owned by itself or other processes) to task `to` through a binder transaction. |
| 58 | +pub fn binder_transfer_binder(from: &Task, to: &Task) -> Result { |
| 59 | + // SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid. |
| 60 | + let ret = unsafe { rust_helper_security_binder_transfer_binder(from.ptr, to.ptr) }; |
| 61 | + if ret != 0 { |
| 62 | + Err(Error::from_kernel_errno(ret)) |
| 63 | + } else { |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Calls the security modules to determine if task `from` is allowed to send the given file to |
| 69 | +/// task `to` (which would get its own file descriptor) through a binder transaction. |
| 70 | +pub fn binder_transfer_file(from: &Task, to: &Task, file: &File) -> Result { |
| 71 | + // SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid. Similarly, by the |
| 72 | + // `File` invariants, `file.ptr` is also valid. |
| 73 | + let ret = unsafe { rust_helper_security_binder_transfer_file(from.ptr, to.ptr, file.ptr) }; |
| 74 | + if ret != 0 { |
| 75 | + Err(Error::from_kernel_errno(ret)) |
| 76 | + } else { |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | +} |
0 commit comments