blob: a3a2562eb1605dcf3a45c6088765b0eb7b9edcd3 [file] [log] [blame]
Stephen Crane2a3c2502020-06-16 17:48:35 -07001/*
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
17use crate::sys;
18
19use libc::{pid_t, uid_t};
20
21/// Static utility functions to manage Binder process state.
22pub struct ProcessState;
23
24impl ProcessState {
Andrew Walbranfee58632023-06-22 17:36:48 +000025 /// Starts the Binder IPC thread pool.
Steven Moreland3e9debc2023-06-15 00:35:29 +000026 ///
Andrew Walbranfee58632023-06-22 17:36:48 +000027 /// 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.
Stephen Crane2a3c2502020-06-16 17:48:35 -070037 pub fn start_thread_pool() {
Andrew Walbran2f3ff9f2023-07-07 16:58:13 +010038 // Safety: Safe FFI
Stephen Crane2a3c2502020-06-16 17:48:35 -070039 unsafe {
Stephen Crane2a3c2502020-06-16 17:48:35 -070040 sys::ABinderProcess_startThreadPool();
41 }
42 }
43
Andrew Walbranfee58632023-06-22 17:36:48 +000044 /// Sets the maximum number of threads that can be started in the
45 /// threadpool.
Stephen Crane2a3c2502020-06-16 17:48:35 -070046 ///
Andrew Walbranfee58632023-06-22 17:36:48 +000047 /// 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.
Stephen Crane2a3c2502020-06-16 17:48:35 -070050 pub fn set_thread_pool_max_thread_count(num_threads: u32) {
Andrew Walbran2f3ff9f2023-07-07 16:58:13 +010051 // Safety: Safe FFI
Stephen Crane2a3c2502020-06-16 17:48:35 -070052 unsafe {
Stephen Crane2a3c2502020-06-16 17:48:35 -070053 sys::ABinderProcess_setThreadPoolMaxThreadCount(num_threads);
54 }
55 }
56
Andrew Walbranfee58632023-06-22 17:36:48 +000057 /// Blocks on the Binder IPC thread pool by adding the current thread to the
58 /// pool.
Steven Moreland3e9debc2023-06-15 00:35:29 +000059 ///
Andrew Walbranfee58632023-06-22 17:36:48 +000060 /// 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).
Stephen Crane2a3c2502020-06-16 17:48:35 -070064 pub fn join_thread_pool() {
Andrew Walbran2f3ff9f2023-07-07 16:58:13 +010065 // Safety: Safe FFI
Stephen Crane2a3c2502020-06-16 17:48:35 -070066 unsafe {
Stephen Crane2a3c2502020-06-16 17:48:35 -070067 sys::ABinderProcess_joinThreadPool();
68 }
69 }
70}
71
72/// Static utility functions to manage Binder thread state.
73pub struct ThreadState;
74
75impl 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.
88 pub fn get_calling_uid() -> uid_t {
Andrew Walbran2f3ff9f2023-07-07 16:58:13 +010089 // Safety: Safe FFI
90 unsafe { sys::AIBinder_getCallingUid() }
Stephen Crane2a3c2502020-06-16 17:48:35 -070091 }
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 /// Available since API level 29.
105 ///
106 /// \return calling pid or the current process's PID if this thread isn't
107 /// processing a transaction.
108 ///
109 /// If the transaction being processed is a oneway transaction, then this
110 /// method will return 0.
111 pub fn get_calling_pid() -> pid_t {
Andrew Walbran2f3ff9f2023-07-07 16:58:13 +0100112 // Safety: Safe FFI
113 unsafe { sys::AIBinder_getCallingPid() }
Stephen Crane2a3c2502020-06-16 17:48:35 -0700114 }
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700115
Alice Ryhl59aeae82021-08-25 10:21:26 +0000116 /// Determine whether the current thread is currently executing an incoming transaction.
117 ///
118 /// \return true if the current thread is currently executing an incoming transaction, and false
119 /// otherwise.
120 pub fn is_handling_transaction() -> bool {
Andrew Walbran2f3ff9f2023-07-07 16:58:13 +0100121 // Safety: Safe FFI
122 unsafe { sys::AIBinder_isHandlingTransaction() }
Alice Ryhl59aeae82021-08-25 10:21:26 +0000123 }
124
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700125 /// This function makes the client's security context available to the
126 /// service calling this function. This can be used for access control.
127 /// It does not suffer from the TOCTOU issues of get_calling_pid.
128 ///
129 /// Implementations of `check_permission` should use the given CStr
130 /// argument as context for selinux permission checks. If `None` is
131 /// given, the implementation should fall back to using the PID
132 /// instead.
133 ///
134 /// Note: `None` may be passed to the callback if the caller did not
135 /// `set_requesting_sid` on the serviced binder, or if the underlying
136 /// kernel is too old to support this feature.
137 pub fn with_calling_sid<T, F>(check_permission: F) -> T
138 where
Matthew Maurere268a9f2022-07-26 09:31:30 -0700139 for<'a> F: FnOnce(Option<&'a std::ffi::CStr>) -> T,
140 {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700141 // Safety: AIBinder_getCallingSid returns a c-string pointer
142 // that is valid for a transaction. Also, the string returned
143 // is thread local. By restricting the lifetime of the CStr
144 // reference to the scope of the callback, we prevent it being
145 // used beyond the guaranteed lifetime.
146 check_permission(unsafe {
147 let sid = sys::AIBinder_getCallingSid();
148 // AIBinder_getCallingSid() returns a '\0' terminated string
149 // or NULL.
150 if sid.is_null() {
151 None
152 } else {
153 Some(std::ffi::CStr::from_ptr(sid))
154 }
155 })
156 }
Stephen Crane2a3c2502020-06-16 17:48:35 -0700157}