blob: b35511d74e06e441e232d2b22c4d102af956c488 [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 {
25 /// Start the Binder IPC thread pool
Steven Moreland3e9debc2023-06-15 00:35:29 +000026 ///
27 /// Starts 1 thread, plus allows the kernel to lazily start up to 'num_threads'
28 /// additional threads as specified by set_thread_pool_max_thread_count.
Stephen Crane2a3c2502020-06-16 17:48:35 -070029 pub fn start_thread_pool() {
30 unsafe {
31 // Safety: Safe FFI
32 sys::ABinderProcess_startThreadPool();
33 }
34 }
35
36 /// Set the maximum number of threads that can be started in the threadpool.
37 ///
38 /// By default, after startThreadPool is called, this is 15. If it is called
Steven Moreland3e9debc2023-06-15 00:35:29 +000039 /// additional times, the thread pool size can only be increased.
Stephen Crane2a3c2502020-06-16 17:48:35 -070040 pub fn set_thread_pool_max_thread_count(num_threads: u32) {
41 unsafe {
42 // Safety: Safe FFI
43 sys::ABinderProcess_setThreadPoolMaxThreadCount(num_threads);
44 }
45 }
46
47 /// Block on the Binder IPC thread pool
Steven Moreland3e9debc2023-06-15 00:35:29 +000048 ///
49 /// This adds additional threads in addition to what is created by
50 /// set_thread_pool_max_thread_count and start_thread_pool.
Stephen Crane2a3c2502020-06-16 17:48:35 -070051 pub fn join_thread_pool() {
52 unsafe {
53 // Safety: Safe FFI
54 sys::ABinderProcess_joinThreadPool();
55 }
56 }
57}
58
59/// Static utility functions to manage Binder thread state.
60pub struct ThreadState;
61
62impl ThreadState {
63 /// This returns the calling UID assuming that this thread is called from a
64 /// thread that is processing a binder transaction (for instance, in the
65 /// implementation of
66 /// [`Remotable::on_transact`](crate::Remotable::on_transact)).
67 ///
68 /// This can be used with higher-level system services to determine the
69 /// caller's identity and check permissions.
70 ///
71 /// Available since API level 29.
72 ///
73 /// \return calling uid or the current process's UID if this thread isn't
74 /// processing a transaction.
75 pub fn get_calling_uid() -> uid_t {
76 unsafe {
77 // Safety: Safe FFI
78 sys::AIBinder_getCallingUid()
79 }
80 }
81
82 /// This returns the calling PID assuming that this thread is called from a
83 /// thread that is processing a binder transaction (for instance, in the
84 /// implementation of
85 /// [`Remotable::on_transact`](crate::Remotable::on_transact)).
86 ///
87 /// This can be used with higher-level system services to determine the
88 /// caller's identity and check permissions. However, when doing this, one
89 /// should be aware of possible TOCTOU problems when the calling process
90 /// dies and is replaced with another process with elevated permissions and
91 /// the same PID.
92 ///
93 /// Available since API level 29.
94 ///
95 /// \return calling pid or the current process's PID if this thread isn't
96 /// processing a transaction.
97 ///
98 /// If the transaction being processed is a oneway transaction, then this
99 /// method will return 0.
100 pub fn get_calling_pid() -> pid_t {
101 unsafe {
102 // Safety: Safe FFI
103 sys::AIBinder_getCallingPid()
104 }
105 }
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700106
Alice Ryhl59aeae82021-08-25 10:21:26 +0000107 /// Determine whether the current thread is currently executing an incoming transaction.
108 ///
109 /// \return true if the current thread is currently executing an incoming transaction, and false
110 /// otherwise.
111 pub fn is_handling_transaction() -> bool {
112 unsafe {
113 // Safety: Safe FFI
114 sys::AIBinder_isHandlingTransaction()
115 }
116 }
117
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700118 /// This function makes the client's security context available to the
119 /// service calling this function. This can be used for access control.
120 /// It does not suffer from the TOCTOU issues of get_calling_pid.
121 ///
122 /// Implementations of `check_permission` should use the given CStr
123 /// argument as context for selinux permission checks. If `None` is
124 /// given, the implementation should fall back to using the PID
125 /// instead.
126 ///
127 /// Note: `None` may be passed to the callback if the caller did not
128 /// `set_requesting_sid` on the serviced binder, or if the underlying
129 /// kernel is too old to support this feature.
130 pub fn with_calling_sid<T, F>(check_permission: F) -> T
131 where
Matthew Maurere268a9f2022-07-26 09:31:30 -0700132 for<'a> F: FnOnce(Option<&'a std::ffi::CStr>) -> T,
133 {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700134 // Safety: AIBinder_getCallingSid returns a c-string pointer
135 // that is valid for a transaction. Also, the string returned
136 // is thread local. By restricting the lifetime of the CStr
137 // reference to the scope of the callback, we prevent it being
138 // used beyond the guaranteed lifetime.
139 check_permission(unsafe {
140 let sid = sys::AIBinder_getCallingSid();
141 // AIBinder_getCallingSid() returns a '\0' terminated string
142 // or NULL.
143 if sid.is_null() {
144 None
145 } else {
146 Some(std::ffi::CStr::from_ptr(sid))
147 }
148 })
149 }
Stephen Crane2a3c2502020-06-16 17:48:35 -0700150}