1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 use crate::sys; 18 19 use libc::{pid_t, uid_t}; 20 21 /// Static utility functions to manage Binder process state. 22 pub struct ProcessState; 23 24 impl ProcessState { 25 /// Starts the Binder IPC thread pool. 26 /// 27 /// Starts 1 thread, plus allows the kernel to lazily start up to 28 /// `num_threads` additional threads as specified by 29 /// [`set_thread_pool_max_thread_count`](Self::set_thread_pool_max_thread_count). 30 /// 31 /// This should be done before creating any Binder client or server. If 32 /// neither this nor [`join_thread_pool`](Self::join_thread_pool) are 33 /// called, then some things (such as callbacks and 34 /// [`IBinder::link_to_death`](crate::IBinder::link_to_death)) will silently 35 /// not work: the callbacks will be queued but never called as there is no 36 /// thread to call them on. start_thread_pool()37 pub fn start_thread_pool() { 38 // Safety: Safe FFI 39 unsafe { 40 sys::ABinderProcess_startThreadPool(); 41 } 42 } 43 44 /// Sets the maximum number of threads that can be started in the 45 /// threadpool. 46 /// 47 /// By default, after [`start_thread_pool`](Self::start_thread_pool) is 48 /// called, this is 15. If it is called additional times, the thread pool 49 /// size can only be increased. set_thread_pool_max_thread_count(num_threads: u32)50 pub fn set_thread_pool_max_thread_count(num_threads: u32) { 51 // Safety: Safe FFI 52 unsafe { 53 sys::ABinderProcess_setThreadPoolMaxThreadCount(num_threads); 54 } 55 } 56 57 /// Blocks on the Binder IPC thread pool by adding the current thread to the 58 /// pool. 59 /// 60 /// Note that this adds the current thread in addition to those that are 61 /// created by 62 /// [`set_thread_pool_max_thread_count`](Self::set_thread_pool_max_thread_count) 63 /// and [`start_thread_pool`](Self::start_thread_pool). join_thread_pool()64 pub fn join_thread_pool() { 65 // Safety: Safe FFI 66 unsafe { 67 sys::ABinderProcess_joinThreadPool(); 68 } 69 } 70 } 71 72 /// Static utility functions to manage Binder thread state. 73 pub struct ThreadState; 74 75 impl ThreadState { 76 /// This returns the calling UID assuming that this thread is called from a 77 /// thread that is processing a binder transaction (for instance, in the 78 /// implementation of 79 /// [`Remotable::on_transact`](crate::Remotable::on_transact)). 80 /// 81 /// This can be used with higher-level system services to determine the 82 /// caller's identity and check permissions. 83 /// 84 /// Available since API level 29. 85 /// 86 /// \return calling uid or the current process's UID if this thread isn't 87 /// processing a transaction. get_calling_uid() -> uid_t88 pub fn get_calling_uid() -> uid_t { 89 // Safety: Safe FFI 90 unsafe { sys::AIBinder_getCallingUid() } 91 } 92 93 /// This returns the calling PID assuming that this thread is called from a 94 /// thread that is processing a binder transaction (for instance, in the 95 /// implementation of 96 /// [`Remotable::on_transact`](crate::Remotable::on_transact)). 97 /// 98 /// This can be used with higher-level system services to determine the 99 /// caller's identity and check permissions. However, when doing this, one 100 /// should be aware of possible TOCTOU problems when the calling process 101 /// dies and is replaced with another process with elevated permissions and 102 /// the same PID. 103 /// 104 /// Warning: oneway transactions do not receive PID. Even if you expect 105 /// a transaction to be synchronous, a misbehaving client could send it 106 /// as a synchronous call and result in a 0 PID here. Additionally, if 107 /// there is a race and the calling process dies, the PID may still be 108 /// 0 for a synchronous call. 109 /// 110 /// Available since API level 29. 111 /// 112 /// \return calling pid or the current process's PID if this thread isn't 113 /// processing a transaction. get_calling_pid() -> pid_t114 pub fn get_calling_pid() -> pid_t { 115 // Safety: Safe FFI 116 unsafe { sys::AIBinder_getCallingPid() } 117 } 118 119 /// Determine whether the current thread is currently executing an incoming transaction. 120 /// 121 /// \return true if the current thread is currently executing an incoming transaction, and false 122 /// otherwise. is_handling_transaction() -> bool123 pub fn is_handling_transaction() -> bool { 124 // Safety: Safe FFI 125 unsafe { sys::AIBinder_isHandlingTransaction() } 126 } 127 128 /// This function makes the client's security context available to the 129 /// service calling this function. This can be used for access control. 130 /// It does not suffer from the TOCTOU issues of get_calling_pid. 131 /// 132 /// Implementations of `check_permission` should use the given CStr 133 /// argument as context for selinux permission checks. If `None` is 134 /// given, the implementation should fall back to using the PID 135 /// instead. 136 /// 137 /// Note: `None` may be passed to the callback if the caller did not 138 /// `set_requesting_sid` on the serviced binder, or if the underlying 139 /// kernel is too old to support this feature. with_calling_sid<T, F>(check_permission: F) -> T where for<'a> F: FnOnce(Option<&'a std::ffi::CStr>) -> T,140 pub fn with_calling_sid<T, F>(check_permission: F) -> T 141 where 142 for<'a> F: FnOnce(Option<&'a std::ffi::CStr>) -> T, 143 { 144 // Safety: AIBinder_getCallingSid returns a c-string pointer 145 // that is valid for a transaction. Also, the string returned 146 // is thread local. By restricting the lifetime of the CStr 147 // reference to the scope of the callback, we prevent it being 148 // used beyond the guaranteed lifetime. 149 check_permission(unsafe { 150 let sid = sys::AIBinder_getCallingSid(); 151 // AIBinder_getCallingSid() returns a '\0' terminated string 152 // or NULL. 153 if sid.is_null() { 154 None 155 } else { 156 Some(std::ffi::CStr::from_ptr(sid)) 157 } 158 }) 159 } 160 } 161