context.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2025 Google LLC.
  3. use kernel::{
  4. alloc::kvec::KVVec,
  5. error::code::*,
  6. prelude::*,
  7. security,
  8. str::{CStr, CString},
  9. sync::{Arc, Mutex},
  10. task::Kuid,
  11. };
  12. use crate::{error::BinderError, node::NodeRef, process::Process};
  13. kernel::sync::global_lock! {
  14. // SAFETY: We call `init` in the module initializer, so it's initialized before first use.
  15. pub(crate) unsafe(uninit) static CONTEXTS: Mutex<ContextList> = ContextList {
  16. contexts: KVVec::new(),
  17. };
  18. }
  19. pub(crate) struct ContextList {
  20. contexts: KVVec<Arc<Context>>,
  21. }
  22. pub(crate) fn get_all_contexts() -> Result<KVVec<Arc<Context>>> {
  23. let lock = CONTEXTS.lock();
  24. let mut ctxs = KVVec::with_capacity(lock.contexts.len(), GFP_KERNEL)?;
  25. for ctx in lock.contexts.iter() {
  26. ctxs.push(ctx.clone(), GFP_KERNEL)?;
  27. }
  28. Ok(ctxs)
  29. }
  30. /// This struct keeps track of the processes using this context, and which process is the context
  31. /// manager.
  32. struct Manager {
  33. node: Option<NodeRef>,
  34. uid: Option<Kuid>,
  35. all_procs: KVVec<Arc<Process>>,
  36. }
  37. /// There is one context per binder file (/dev/binder, /dev/hwbinder, etc)
  38. #[pin_data]
  39. pub(crate) struct Context {
  40. #[pin]
  41. manager: Mutex<Manager>,
  42. pub(crate) name: CString,
  43. }
  44. impl Context {
  45. pub(crate) fn new(name: &CStr) -> Result<Arc<Self>> {
  46. let name = CString::try_from(name)?;
  47. let ctx = Arc::pin_init(
  48. try_pin_init!(Context {
  49. name,
  50. manager <- kernel::new_mutex!(Manager {
  51. all_procs: KVVec::new(),
  52. node: None,
  53. uid: None,
  54. }, "Context::manager"),
  55. }),
  56. GFP_KERNEL,
  57. )?;
  58. CONTEXTS.lock().contexts.push(ctx.clone(), GFP_KERNEL)?;
  59. Ok(ctx)
  60. }
  61. /// Called when the file for this context is unlinked.
  62. ///
  63. /// No-op if called twice.
  64. pub(crate) fn deregister(self: &Arc<Self>) {
  65. // Safe removal using retain
  66. CONTEXTS.lock().contexts.retain(|c| !Arc::ptr_eq(c, self));
  67. }
  68. pub(crate) fn register_process(self: &Arc<Self>, proc: Arc<Process>) -> Result {
  69. if !Arc::ptr_eq(self, &proc.ctx) {
  70. pr_err!("Context::register_process called on the wrong context.");
  71. return Err(EINVAL);
  72. }
  73. self.manager.lock().all_procs.push(proc, GFP_KERNEL)?;
  74. Ok(())
  75. }
  76. pub(crate) fn deregister_process(self: &Arc<Self>, proc: &Arc<Process>) {
  77. if !Arc::ptr_eq(self, &proc.ctx) {
  78. pr_err!("Context::deregister_process called on the wrong context.");
  79. return;
  80. }
  81. let mut manager = self.manager.lock();
  82. manager.all_procs.retain(|p| !Arc::ptr_eq(p, proc));
  83. }
  84. pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> Result {
  85. let mut manager = self.manager.lock();
  86. if manager.node.is_some() {
  87. pr_warn!("BINDER_SET_CONTEXT_MGR already set");
  88. return Err(EBUSY);
  89. }
  90. security::binder_set_context_mgr(&node_ref.node.owner.cred)?;
  91. // If the context manager has been set before, ensure that we use the same euid.
  92. let caller_uid = Kuid::current_euid();
  93. if let Some(ref uid) = manager.uid {
  94. if *uid != caller_uid {
  95. return Err(EPERM);
  96. }
  97. }
  98. manager.node = Some(node_ref);
  99. manager.uid = Some(caller_uid);
  100. Ok(())
  101. }
  102. pub(crate) fn unset_manager_node(&self) {
  103. let node_ref = self.manager.lock().node.take();
  104. drop(node_ref);
  105. }
  106. pub(crate) fn get_manager_node(&self, strong: bool) -> Result<NodeRef, BinderError> {
  107. self.manager
  108. .lock()
  109. .node
  110. .as_ref()
  111. .ok_or_else(BinderError::new_dead)?
  112. .clone(strong)
  113. .map_err(BinderError::from)
  114. }
  115. pub(crate) fn for_each_proc<F>(&self, mut func: F)
  116. where
  117. F: FnMut(&Process),
  118. {
  119. let lock = self.manager.lock();
  120. for proc in &lock.all_procs {
  121. func(proc);
  122. }
  123. }
  124. pub(crate) fn get_all_procs(&self) -> Result<KVVec<Arc<Process>>> {
  125. let lock = self.manager.lock();
  126. let mut procs = KVVec::with_capacity(lock.all_procs.len(), GFP_KERNEL)?;
  127. for proc in lock.all_procs.iter() {
  128. procs.push(Arc::clone(proc), GFP_KERNEL)?;
  129. }
  130. Ok(procs)
  131. }
  132. pub(crate) fn get_procs_with_pid(&self, pid: i32) -> Result<KVVec<Arc<Process>>> {
  133. let lock = self.manager.lock();
  134. let mut matching_procs = KVVec::new();
  135. for proc in lock.all_procs.iter() {
  136. if proc.task.pid() == pid {
  137. matching_procs.push(Arc::clone(proc), GFP_KERNEL)?;
  138. }
  139. }
  140. Ok(matching_procs)
  141. }
  142. }