blob: 366b12405708187e0116337a4a316cdec45fb9d3 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* binder.c
3 *
4 * Android IPC Subsystem
5 *
6 * Copyright (C) 2007-2008 Google, Inc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9/*
10 * Locking overview
11 *
12 * There are 3 main spinlocks which must be acquired in the
13 * order shown:
14 *
15 * 1) proc->outer_lock : protects binder_ref
16 * binder_proc_lock() and binder_proc_unlock() are
17 * used to acq/rel.
18 * 2) node->lock : protects most fields of binder_node.
19 * binder_node_lock() and binder_node_unlock() are
20 * used to acq/rel
21 * 3) proc->inner_lock : protects the thread and node lists
22 * (proc->threads, proc->waiting_threads, proc->nodes)
23 * and all todo lists associated with the binder_proc
24 * (proc->todo, thread->todo, proc->delivered_death and
25 * node->async_todo), as well as thread->transaction_stack
26 * binder_inner_proc_lock() and binder_inner_proc_unlock()
27 * are used to acq/rel
28 *
29 * Any lock under procA must never be nested under any lock at the same
30 * level or below on procB.
31 *
32 * Functions that require a lock held on entry indicate which lock
33 * in the suffix of the function name:
34 *
35 * foo_olocked() : requires node->outer_lock
36 * foo_nlocked() : requires node->lock
37 * foo_ilocked() : requires proc->inner_lock
38 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
39 * foo_nilocked(): requires node->lock and proc->inner_lock
40 * ...
41 */
42
43#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44
45#include <linux/fdtable.h>
46#include <linux/file.h>
47#include <linux/freezer.h>
48#include <linux/fs.h>
49#include <linux/list.h>
50#include <linux/miscdevice.h>
51#include <linux/module.h>
52#include <linux/mutex.h>
53#include <linux/nsproxy.h>
54#include <linux/poll.h>
55#include <linux/debugfs.h>
56#include <linux/rbtree.h>
57#include <linux/sched/signal.h>
58#include <linux/sched/mm.h>
59#include <linux/seq_file.h>
David Brazdil0f672f62019-12-10 10:32:29 +000060#include <linux/string.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061#include <linux/uaccess.h>
62#include <linux/pid_namespace.h>
63#include <linux/security.h>
64#include <linux/spinlock.h>
65#include <linux/ratelimit.h>
David Brazdil0f672f62019-12-10 10:32:29 +000066#include <linux/syscalls.h>
67#include <linux/task_work.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020068#include <linux/sizes.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069
70#include <uapi/linux/android/binder.h>
David Brazdil0f672f62019-12-10 10:32:29 +000071#include <uapi/linux/android/binderfs.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072
73#include <asm/cacheflush.h>
74
75#include "binder_alloc.h"
David Brazdil0f672f62019-12-10 10:32:29 +000076#include "binder_internal.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077#include "binder_trace.h"
78
79static HLIST_HEAD(binder_deferred_list);
80static DEFINE_MUTEX(binder_deferred_lock);
81
82static HLIST_HEAD(binder_devices);
83static HLIST_HEAD(binder_procs);
84static DEFINE_MUTEX(binder_procs_lock);
85
86static HLIST_HEAD(binder_dead_nodes);
87static DEFINE_SPINLOCK(binder_dead_nodes_lock);
88
89static struct dentry *binder_debugfs_dir_entry_root;
90static struct dentry *binder_debugfs_dir_entry_proc;
91static atomic_t binder_last_id;
92
David Brazdil0f672f62019-12-10 10:32:29 +000093static int proc_show(struct seq_file *m, void *unused);
94DEFINE_SHOW_ATTRIBUTE(proc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
97
98enum {
99 BINDER_DEBUG_USER_ERROR = 1U << 0,
100 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
101 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
102 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
103 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
104 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
105 BINDER_DEBUG_READ_WRITE = 1U << 6,
106 BINDER_DEBUG_USER_REFS = 1U << 7,
107 BINDER_DEBUG_THREADS = 1U << 8,
108 BINDER_DEBUG_TRANSACTION = 1U << 9,
109 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
110 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
111 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
112 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
113 BINDER_DEBUG_SPINLOCKS = 1U << 14,
114};
115static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
116 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
117module_param_named(debug_mask, binder_debug_mask, uint, 0644);
118
David Brazdil0f672f62019-12-10 10:32:29 +0000119char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120module_param_named(devices, binder_devices_param, charp, 0444);
121
122static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
123static int binder_stop_on_user_error;
124
125static int binder_set_stop_on_user_error(const char *val,
126 const struct kernel_param *kp)
127{
128 int ret;
129
130 ret = param_set_int(val, kp);
131 if (binder_stop_on_user_error < 2)
132 wake_up(&binder_user_error_wait);
133 return ret;
134}
135module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
136 param_get_int, &binder_stop_on_user_error, 0644);
137
138#define binder_debug(mask, x...) \
139 do { \
140 if (binder_debug_mask & mask) \
141 pr_info_ratelimited(x); \
142 } while (0)
143
144#define binder_user_error(x...) \
145 do { \
146 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
147 pr_info_ratelimited(x); \
148 if (binder_stop_on_user_error) \
149 binder_stop_on_user_error = 2; \
150 } while (0)
151
152#define to_flat_binder_object(hdr) \
153 container_of(hdr, struct flat_binder_object, hdr)
154
155#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
156
157#define to_binder_buffer_object(hdr) \
158 container_of(hdr, struct binder_buffer_object, hdr)
159
160#define to_binder_fd_array_object(hdr) \
161 container_of(hdr, struct binder_fd_array_object, hdr)
162
163enum binder_stat_types {
164 BINDER_STAT_PROC,
165 BINDER_STAT_THREAD,
166 BINDER_STAT_NODE,
167 BINDER_STAT_REF,
168 BINDER_STAT_DEATH,
169 BINDER_STAT_TRANSACTION,
170 BINDER_STAT_TRANSACTION_COMPLETE,
171 BINDER_STAT_COUNT
172};
173
174struct binder_stats {
175 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
176 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
177 atomic_t obj_created[BINDER_STAT_COUNT];
178 atomic_t obj_deleted[BINDER_STAT_COUNT];
179};
180
181static struct binder_stats binder_stats;
182
183static inline void binder_stats_deleted(enum binder_stat_types type)
184{
185 atomic_inc(&binder_stats.obj_deleted[type]);
186}
187
188static inline void binder_stats_created(enum binder_stat_types type)
189{
190 atomic_inc(&binder_stats.obj_created[type]);
191}
192
David Brazdil0f672f62019-12-10 10:32:29 +0000193struct binder_transaction_log binder_transaction_log;
194struct binder_transaction_log binder_transaction_log_failed;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195
196static struct binder_transaction_log_entry *binder_transaction_log_add(
197 struct binder_transaction_log *log)
198{
199 struct binder_transaction_log_entry *e;
200 unsigned int cur = atomic_inc_return(&log->cur);
201
202 if (cur >= ARRAY_SIZE(log->entry))
203 log->full = true;
204 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
205 WRITE_ONCE(e->debug_id_done, 0);
206 /*
207 * write-barrier to synchronize access to e->debug_id_done.
208 * We make sure the initialized 0 value is seen before
209 * memset() other fields are zeroed by memset.
210 */
211 smp_wmb();
212 memset(e, 0, sizeof(*e));
213 return e;
214}
215
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216/**
217 * struct binder_work - work enqueued on a worklist
218 * @entry: node enqueued on list
219 * @type: type of work to be performed
220 *
221 * There are separate work lists for proc, thread, and node (async).
222 */
223struct binder_work {
224 struct list_head entry;
225
Olivier Deprez0e641232021-09-23 10:07:05 +0200226 enum binder_work_type {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227 BINDER_WORK_TRANSACTION = 1,
228 BINDER_WORK_TRANSACTION_COMPLETE,
229 BINDER_WORK_RETURN_ERROR,
230 BINDER_WORK_NODE,
231 BINDER_WORK_DEAD_BINDER,
232 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
233 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
234 } type;
235};
236
237struct binder_error {
238 struct binder_work work;
239 uint32_t cmd;
240};
241
242/**
243 * struct binder_node - binder node bookkeeping
244 * @debug_id: unique ID for debugging
245 * (invariant after initialized)
246 * @lock: lock for node fields
247 * @work: worklist element for node work
248 * (protected by @proc->inner_lock)
249 * @rb_node: element for proc->nodes tree
250 * (protected by @proc->inner_lock)
251 * @dead_node: element for binder_dead_nodes list
252 * (protected by binder_dead_nodes_lock)
253 * @proc: binder_proc that owns this node
254 * (invariant after initialized)
255 * @refs: list of references on this node
256 * (protected by @lock)
257 * @internal_strong_refs: used to take strong references when
258 * initiating a transaction
259 * (protected by @proc->inner_lock if @proc
260 * and by @lock)
261 * @local_weak_refs: weak user refs from local process
262 * (protected by @proc->inner_lock if @proc
263 * and by @lock)
264 * @local_strong_refs: strong user refs from local process
265 * (protected by @proc->inner_lock if @proc
266 * and by @lock)
267 * @tmp_refs: temporary kernel refs
268 * (protected by @proc->inner_lock while @proc
269 * is valid, and by binder_dead_nodes_lock
270 * if @proc is NULL. During inc/dec and node release
271 * it is also protected by @lock to provide safety
272 * as the node dies and @proc becomes NULL)
273 * @ptr: userspace pointer for node
274 * (invariant, no lock needed)
275 * @cookie: userspace cookie for node
276 * (invariant, no lock needed)
277 * @has_strong_ref: userspace notified of strong ref
278 * (protected by @proc->inner_lock if @proc
279 * and by @lock)
280 * @pending_strong_ref: userspace has acked notification of strong ref
281 * (protected by @proc->inner_lock if @proc
282 * and by @lock)
283 * @has_weak_ref: userspace notified of weak ref
284 * (protected by @proc->inner_lock if @proc
285 * and by @lock)
286 * @pending_weak_ref: userspace has acked notification of weak ref
287 * (protected by @proc->inner_lock if @proc
288 * and by @lock)
289 * @has_async_transaction: async transaction to node in progress
290 * (protected by @lock)
291 * @accept_fds: file descriptor operations supported for node
292 * (invariant after initialized)
293 * @min_priority: minimum scheduling priority
294 * (invariant after initialized)
David Brazdil0f672f62019-12-10 10:32:29 +0000295 * @txn_security_ctx: require sender's security context
296 * (invariant after initialized)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297 * @async_todo: list of async work items
298 * (protected by @proc->inner_lock)
299 *
300 * Bookkeeping structure for binder nodes.
301 */
302struct binder_node {
303 int debug_id;
304 spinlock_t lock;
305 struct binder_work work;
306 union {
307 struct rb_node rb_node;
308 struct hlist_node dead_node;
309 };
310 struct binder_proc *proc;
311 struct hlist_head refs;
312 int internal_strong_refs;
313 int local_weak_refs;
314 int local_strong_refs;
315 int tmp_refs;
316 binder_uintptr_t ptr;
317 binder_uintptr_t cookie;
318 struct {
319 /*
320 * bitfield elements protected by
321 * proc inner_lock
322 */
323 u8 has_strong_ref:1;
324 u8 pending_strong_ref:1;
325 u8 has_weak_ref:1;
326 u8 pending_weak_ref:1;
327 };
328 struct {
329 /*
330 * invariant after initialization
331 */
332 u8 accept_fds:1;
David Brazdil0f672f62019-12-10 10:32:29 +0000333 u8 txn_security_ctx:1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000334 u8 min_priority;
335 };
336 bool has_async_transaction;
337 struct list_head async_todo;
338};
339
340struct binder_ref_death {
341 /**
342 * @work: worklist element for death notifications
343 * (protected by inner_lock of the proc that
344 * this ref belongs to)
345 */
346 struct binder_work work;
347 binder_uintptr_t cookie;
348};
349
350/**
351 * struct binder_ref_data - binder_ref counts and id
352 * @debug_id: unique ID for the ref
353 * @desc: unique userspace handle for ref
354 * @strong: strong ref count (debugging only if not locked)
355 * @weak: weak ref count (debugging only if not locked)
356 *
357 * Structure to hold ref count and ref id information. Since
358 * the actual ref can only be accessed with a lock, this structure
359 * is used to return information about the ref to callers of
360 * ref inc/dec functions.
361 */
362struct binder_ref_data {
363 int debug_id;
364 uint32_t desc;
365 int strong;
366 int weak;
367};
368
369/**
370 * struct binder_ref - struct to track references on nodes
371 * @data: binder_ref_data containing id, handle, and current refcounts
372 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
373 * @rb_node_node: node for lookup by @node in proc's rb_tree
374 * @node_entry: list entry for node->refs list in target node
375 * (protected by @node->lock)
376 * @proc: binder_proc containing ref
377 * @node: binder_node of target node. When cleaning up a
378 * ref for deletion in binder_cleanup_ref, a non-NULL
379 * @node indicates the node must be freed
380 * @death: pointer to death notification (ref_death) if requested
381 * (protected by @node->lock)
382 *
383 * Structure to track references from procA to target node (on procB). This
384 * structure is unsafe to access without holding @proc->outer_lock.
385 */
386struct binder_ref {
387 /* Lookups needed: */
388 /* node + proc => ref (transaction) */
389 /* desc + proc => ref (transaction, inc/dec ref) */
390 /* node => refs + procs (proc exit) */
391 struct binder_ref_data data;
392 struct rb_node rb_node_desc;
393 struct rb_node rb_node_node;
394 struct hlist_node node_entry;
395 struct binder_proc *proc;
396 struct binder_node *node;
397 struct binder_ref_death *death;
398};
399
400enum binder_deferred_state {
David Brazdil0f672f62019-12-10 10:32:29 +0000401 BINDER_DEFERRED_FLUSH = 0x01,
402 BINDER_DEFERRED_RELEASE = 0x02,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403};
404
405/**
406 * struct binder_proc - binder process bookkeeping
407 * @proc_node: element for binder_procs list
408 * @threads: rbtree of binder_threads in this proc
409 * (protected by @inner_lock)
410 * @nodes: rbtree of binder nodes associated with
411 * this proc ordered by node->ptr
412 * (protected by @inner_lock)
413 * @refs_by_desc: rbtree of refs ordered by ref->desc
414 * (protected by @outer_lock)
415 * @refs_by_node: rbtree of refs ordered by ref->node
416 * (protected by @outer_lock)
417 * @waiting_threads: threads currently waiting for proc work
418 * (protected by @inner_lock)
419 * @pid PID of group_leader of process
420 * (invariant after initialized)
421 * @tsk task_struct for group_leader of process
422 * (invariant after initialized)
Olivier Deprez157378f2022-04-04 15:47:50 +0200423 * @cred struct cred associated with the `struct file`
424 * in binder_open()
425 * (invariant after initialized)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426 * @deferred_work_node: element for binder_deferred_list
427 * (protected by binder_deferred_lock)
428 * @deferred_work: bitmap of deferred work to perform
429 * (protected by binder_deferred_lock)
430 * @is_dead: process is dead and awaiting free
431 * when outstanding transactions are cleaned up
432 * (protected by @inner_lock)
433 * @todo: list of work for this process
434 * (protected by @inner_lock)
435 * @stats: per-process binder statistics
436 * (atomics, no lock needed)
437 * @delivered_death: list of delivered death notification
438 * (protected by @inner_lock)
439 * @max_threads: cap on number of binder threads
440 * (protected by @inner_lock)
441 * @requested_threads: number of binder threads requested but not
442 * yet started. In current implementation, can
443 * only be 0 or 1.
444 * (protected by @inner_lock)
445 * @requested_threads_started: number binder threads started
446 * (protected by @inner_lock)
447 * @tmp_ref: temporary reference to indicate proc is in use
448 * (protected by @inner_lock)
449 * @default_priority: default scheduler priority
450 * (invariant after initialized)
451 * @debugfs_entry: debugfs node
452 * @alloc: binder allocator bookkeeping
453 * @context: binder_context for this proc
454 * (invariant after initialized)
455 * @inner_lock: can nest under outer_lock and/or node lock
456 * @outer_lock: no nesting under innor or node lock
457 * Lock order: 1) outer, 2) node, 3) inner
David Brazdil0f672f62019-12-10 10:32:29 +0000458 * @binderfs_entry: process-specific binderfs log file
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000459 *
460 * Bookkeeping structure for binder processes
461 */
462struct binder_proc {
463 struct hlist_node proc_node;
464 struct rb_root threads;
465 struct rb_root nodes;
466 struct rb_root refs_by_desc;
467 struct rb_root refs_by_node;
468 struct list_head waiting_threads;
469 int pid;
470 struct task_struct *tsk;
Olivier Deprez157378f2022-04-04 15:47:50 +0200471 const struct cred *cred;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472 struct hlist_node deferred_work_node;
473 int deferred_work;
474 bool is_dead;
475
476 struct list_head todo;
477 struct binder_stats stats;
478 struct list_head delivered_death;
479 int max_threads;
480 int requested_threads;
481 int requested_threads_started;
482 int tmp_ref;
483 long default_priority;
484 struct dentry *debugfs_entry;
485 struct binder_alloc alloc;
486 struct binder_context *context;
487 spinlock_t inner_lock;
488 spinlock_t outer_lock;
David Brazdil0f672f62019-12-10 10:32:29 +0000489 struct dentry *binderfs_entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000490};
491
492enum {
493 BINDER_LOOPER_STATE_REGISTERED = 0x01,
494 BINDER_LOOPER_STATE_ENTERED = 0x02,
495 BINDER_LOOPER_STATE_EXITED = 0x04,
496 BINDER_LOOPER_STATE_INVALID = 0x08,
497 BINDER_LOOPER_STATE_WAITING = 0x10,
498 BINDER_LOOPER_STATE_POLL = 0x20,
499};
500
501/**
502 * struct binder_thread - binder thread bookkeeping
503 * @proc: binder process for this thread
504 * (invariant after initialization)
505 * @rb_node: element for proc->threads rbtree
506 * (protected by @proc->inner_lock)
507 * @waiting_thread_node: element for @proc->waiting_threads list
508 * (protected by @proc->inner_lock)
509 * @pid: PID for this thread
510 * (invariant after initialization)
511 * @looper: bitmap of looping state
512 * (only accessed by this thread)
513 * @looper_needs_return: looping thread needs to exit driver
514 * (no lock needed)
515 * @transaction_stack: stack of in-progress transactions for this thread
516 * (protected by @proc->inner_lock)
517 * @todo: list of work to do for this thread
518 * (protected by @proc->inner_lock)
519 * @process_todo: whether work in @todo should be processed
520 * (protected by @proc->inner_lock)
521 * @return_error: transaction errors reported by this thread
522 * (only accessed by this thread)
523 * @reply_error: transaction errors reported by target thread
524 * (protected by @proc->inner_lock)
525 * @wait: wait queue for thread work
526 * @stats: per-thread statistics
527 * (atomics, no lock needed)
528 * @tmp_ref: temporary reference to indicate thread is in use
529 * (atomic since @proc->inner_lock cannot
530 * always be acquired)
531 * @is_dead: thread is dead and awaiting free
532 * when outstanding transactions are cleaned up
533 * (protected by @proc->inner_lock)
534 *
535 * Bookkeeping structure for binder threads.
536 */
537struct binder_thread {
538 struct binder_proc *proc;
539 struct rb_node rb_node;
540 struct list_head waiting_thread_node;
541 int pid;
542 int looper; /* only modified by this thread */
543 bool looper_need_return; /* can be written by other thread */
544 struct binder_transaction *transaction_stack;
545 struct list_head todo;
546 bool process_todo;
547 struct binder_error return_error;
548 struct binder_error reply_error;
549 wait_queue_head_t wait;
550 struct binder_stats stats;
551 atomic_t tmp_ref;
552 bool is_dead;
553};
554
David Brazdil0f672f62019-12-10 10:32:29 +0000555/**
556 * struct binder_txn_fd_fixup - transaction fd fixup list element
557 * @fixup_entry: list entry
558 * @file: struct file to be associated with new fd
559 * @offset: offset in buffer data to this fixup
560 *
561 * List element for fd fixups in a transaction. Since file
562 * descriptors need to be allocated in the context of the
563 * target process, we pass each fd to be processed in this
564 * struct.
565 */
566struct binder_txn_fd_fixup {
567 struct list_head fixup_entry;
568 struct file *file;
569 size_t offset;
570};
571
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572struct binder_transaction {
573 int debug_id;
574 struct binder_work work;
575 struct binder_thread *from;
576 struct binder_transaction *from_parent;
577 struct binder_proc *to_proc;
578 struct binder_thread *to_thread;
579 struct binder_transaction *to_parent;
580 unsigned need_reply:1;
581 /* unsigned is_dead:1; */ /* not used at the moment */
582
583 struct binder_buffer *buffer;
584 unsigned int code;
585 unsigned int flags;
586 long priority;
587 long saved_priority;
588 kuid_t sender_euid;
David Brazdil0f672f62019-12-10 10:32:29 +0000589 struct list_head fd_fixups;
590 binder_uintptr_t security_ctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591 /**
592 * @lock: protects @from, @to_proc, and @to_thread
593 *
594 * @from, @to_proc, and @to_thread can be set to NULL
595 * during thread teardown
596 */
597 spinlock_t lock;
598};
599
600/**
David Brazdil0f672f62019-12-10 10:32:29 +0000601 * struct binder_object - union of flat binder object types
602 * @hdr: generic object header
603 * @fbo: binder object (nodes and refs)
604 * @fdo: file descriptor object
605 * @bbo: binder buffer pointer
606 * @fdao: file descriptor array
607 *
608 * Used for type-independent object copies
609 */
610struct binder_object {
611 union {
612 struct binder_object_header hdr;
613 struct flat_binder_object fbo;
614 struct binder_fd_object fdo;
615 struct binder_buffer_object bbo;
616 struct binder_fd_array_object fdao;
617 };
618};
619
620/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000621 * binder_proc_lock() - Acquire outer lock for given binder_proc
622 * @proc: struct binder_proc to acquire
623 *
624 * Acquires proc->outer_lock. Used to protect binder_ref
625 * structures associated with the given proc.
626 */
627#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
628static void
629_binder_proc_lock(struct binder_proc *proc, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000630 __acquires(&proc->outer_lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000631{
632 binder_debug(BINDER_DEBUG_SPINLOCKS,
633 "%s: line=%d\n", __func__, line);
634 spin_lock(&proc->outer_lock);
635}
636
637/**
638 * binder_proc_unlock() - Release spinlock for given binder_proc
639 * @proc: struct binder_proc to acquire
640 *
641 * Release lock acquired via binder_proc_lock()
642 */
643#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
644static void
645_binder_proc_unlock(struct binder_proc *proc, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000646 __releases(&proc->outer_lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000647{
648 binder_debug(BINDER_DEBUG_SPINLOCKS,
649 "%s: line=%d\n", __func__, line);
650 spin_unlock(&proc->outer_lock);
651}
652
653/**
654 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
655 * @proc: struct binder_proc to acquire
656 *
657 * Acquires proc->inner_lock. Used to protect todo lists
658 */
659#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
660static void
661_binder_inner_proc_lock(struct binder_proc *proc, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000662 __acquires(&proc->inner_lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000663{
664 binder_debug(BINDER_DEBUG_SPINLOCKS,
665 "%s: line=%d\n", __func__, line);
666 spin_lock(&proc->inner_lock);
667}
668
669/**
670 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
671 * @proc: struct binder_proc to acquire
672 *
673 * Release lock acquired via binder_inner_proc_lock()
674 */
675#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
676static void
677_binder_inner_proc_unlock(struct binder_proc *proc, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000678 __releases(&proc->inner_lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679{
680 binder_debug(BINDER_DEBUG_SPINLOCKS,
681 "%s: line=%d\n", __func__, line);
682 spin_unlock(&proc->inner_lock);
683}
684
685/**
686 * binder_node_lock() - Acquire spinlock for given binder_node
687 * @node: struct binder_node to acquire
688 *
689 * Acquires node->lock. Used to protect binder_node fields
690 */
691#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
692static void
693_binder_node_lock(struct binder_node *node, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000694 __acquires(&node->lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695{
696 binder_debug(BINDER_DEBUG_SPINLOCKS,
697 "%s: line=%d\n", __func__, line);
698 spin_lock(&node->lock);
699}
700
701/**
702 * binder_node_unlock() - Release spinlock for given binder_proc
703 * @node: struct binder_node to acquire
704 *
705 * Release lock acquired via binder_node_lock()
706 */
707#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
708static void
709_binder_node_unlock(struct binder_node *node, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000710 __releases(&node->lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000711{
712 binder_debug(BINDER_DEBUG_SPINLOCKS,
713 "%s: line=%d\n", __func__, line);
714 spin_unlock(&node->lock);
715}
716
717/**
718 * binder_node_inner_lock() - Acquire node and inner locks
719 * @node: struct binder_node to acquire
720 *
721 * Acquires node->lock. If node->proc also acquires
722 * proc->inner_lock. Used to protect binder_node fields
723 */
724#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
725static void
726_binder_node_inner_lock(struct binder_node *node, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000727 __acquires(&node->lock) __acquires(&node->proc->inner_lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000728{
729 binder_debug(BINDER_DEBUG_SPINLOCKS,
730 "%s: line=%d\n", __func__, line);
731 spin_lock(&node->lock);
732 if (node->proc)
733 binder_inner_proc_lock(node->proc);
David Brazdil0f672f62019-12-10 10:32:29 +0000734 else
735 /* annotation for sparse */
736 __acquire(&node->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000737}
738
739/**
740 * binder_node_unlock() - Release node and inner locks
741 * @node: struct binder_node to acquire
742 *
743 * Release lock acquired via binder_node_lock()
744 */
745#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
746static void
747_binder_node_inner_unlock(struct binder_node *node, int line)
David Brazdil0f672f62019-12-10 10:32:29 +0000748 __releases(&node->lock) __releases(&node->proc->inner_lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000749{
750 struct binder_proc *proc = node->proc;
751
752 binder_debug(BINDER_DEBUG_SPINLOCKS,
753 "%s: line=%d\n", __func__, line);
754 if (proc)
755 binder_inner_proc_unlock(proc);
David Brazdil0f672f62019-12-10 10:32:29 +0000756 else
757 /* annotation for sparse */
758 __release(&node->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000759 spin_unlock(&node->lock);
760}
761
762static bool binder_worklist_empty_ilocked(struct list_head *list)
763{
764 return list_empty(list);
765}
766
767/**
768 * binder_worklist_empty() - Check if no items on the work list
769 * @proc: binder_proc associated with list
770 * @list: list to check
771 *
772 * Return: true if there are no items on list, else false
773 */
774static bool binder_worklist_empty(struct binder_proc *proc,
775 struct list_head *list)
776{
777 bool ret;
778
779 binder_inner_proc_lock(proc);
780 ret = binder_worklist_empty_ilocked(list);
781 binder_inner_proc_unlock(proc);
782 return ret;
783}
784
785/**
786 * binder_enqueue_work_ilocked() - Add an item to the work list
787 * @work: struct binder_work to add to list
788 * @target_list: list to add work to
789 *
790 * Adds the work to the specified list. Asserts that work
791 * is not already on a list.
792 *
793 * Requires the proc->inner_lock to be held.
794 */
795static void
796binder_enqueue_work_ilocked(struct binder_work *work,
797 struct list_head *target_list)
798{
799 BUG_ON(target_list == NULL);
800 BUG_ON(work->entry.next && !list_empty(&work->entry));
801 list_add_tail(&work->entry, target_list);
802}
803
804/**
805 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
806 * @thread: thread to queue work to
807 * @work: struct binder_work to add to list
808 *
809 * Adds the work to the todo list of the thread. Doesn't set the process_todo
810 * flag, which means that (if it wasn't already set) the thread will go to
811 * sleep without handling this work when it calls read.
812 *
813 * Requires the proc->inner_lock to be held.
814 */
815static void
816binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
817 struct binder_work *work)
818{
David Brazdil0f672f62019-12-10 10:32:29 +0000819 WARN_ON(!list_empty(&thread->waiting_thread_node));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820 binder_enqueue_work_ilocked(work, &thread->todo);
821}
822
823/**
824 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
825 * @thread: thread to queue work to
826 * @work: struct binder_work to add to list
827 *
828 * Adds the work to the todo list of the thread, and enables processing
829 * of the todo queue.
830 *
831 * Requires the proc->inner_lock to be held.
832 */
833static void
834binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
835 struct binder_work *work)
836{
David Brazdil0f672f62019-12-10 10:32:29 +0000837 WARN_ON(!list_empty(&thread->waiting_thread_node));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000838 binder_enqueue_work_ilocked(work, &thread->todo);
839 thread->process_todo = true;
840}
841
842/**
843 * binder_enqueue_thread_work() - Add an item to the thread work list
844 * @thread: thread to queue work to
845 * @work: struct binder_work to add to list
846 *
847 * Adds the work to the todo list of the thread, and enables processing
848 * of the todo queue.
849 */
850static void
851binder_enqueue_thread_work(struct binder_thread *thread,
852 struct binder_work *work)
853{
854 binder_inner_proc_lock(thread->proc);
855 binder_enqueue_thread_work_ilocked(thread, work);
856 binder_inner_proc_unlock(thread->proc);
857}
858
859static void
860binder_dequeue_work_ilocked(struct binder_work *work)
861{
862 list_del_init(&work->entry);
863}
864
865/**
866 * binder_dequeue_work() - Removes an item from the work list
867 * @proc: binder_proc associated with list
868 * @work: struct binder_work to remove from list
869 *
870 * Removes the specified work item from whatever list it is on.
871 * Can safely be called if work is not on any list.
872 */
873static void
874binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
875{
876 binder_inner_proc_lock(proc);
877 binder_dequeue_work_ilocked(work);
878 binder_inner_proc_unlock(proc);
879}
880
881static struct binder_work *binder_dequeue_work_head_ilocked(
882 struct list_head *list)
883{
884 struct binder_work *w;
885
886 w = list_first_entry_or_null(list, struct binder_work, entry);
887 if (w)
888 list_del_init(&w->entry);
889 return w;
890}
891
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000892static void
893binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
894static void binder_free_thread(struct binder_thread *thread);
895static void binder_free_proc(struct binder_proc *proc);
896static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
897
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000898static bool binder_has_work_ilocked(struct binder_thread *thread,
899 bool do_proc_work)
900{
901 return thread->process_todo ||
902 thread->looper_need_return ||
903 (do_proc_work &&
904 !binder_worklist_empty_ilocked(&thread->proc->todo));
905}
906
907static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
908{
909 bool has_work;
910
911 binder_inner_proc_lock(thread->proc);
912 has_work = binder_has_work_ilocked(thread, do_proc_work);
913 binder_inner_proc_unlock(thread->proc);
914
915 return has_work;
916}
917
918static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
919{
920 return !thread->transaction_stack &&
921 binder_worklist_empty_ilocked(&thread->todo) &&
922 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
923 BINDER_LOOPER_STATE_REGISTERED));
924}
925
926static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
927 bool sync)
928{
929 struct rb_node *n;
930 struct binder_thread *thread;
931
932 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
933 thread = rb_entry(n, struct binder_thread, rb_node);
934 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
935 binder_available_for_proc_work_ilocked(thread)) {
936 if (sync)
937 wake_up_interruptible_sync(&thread->wait);
938 else
939 wake_up_interruptible(&thread->wait);
940 }
941 }
942}
943
944/**
945 * binder_select_thread_ilocked() - selects a thread for doing proc work.
946 * @proc: process to select a thread from
947 *
948 * Note that calling this function moves the thread off the waiting_threads
949 * list, so it can only be woken up by the caller of this function, or a
950 * signal. Therefore, callers *should* always wake up the thread this function
951 * returns.
952 *
953 * Return: If there's a thread currently waiting for process work,
954 * returns that thread. Otherwise returns NULL.
955 */
956static struct binder_thread *
957binder_select_thread_ilocked(struct binder_proc *proc)
958{
959 struct binder_thread *thread;
960
961 assert_spin_locked(&proc->inner_lock);
962 thread = list_first_entry_or_null(&proc->waiting_threads,
963 struct binder_thread,
964 waiting_thread_node);
965
966 if (thread)
967 list_del_init(&thread->waiting_thread_node);
968
969 return thread;
970}
971
972/**
973 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
974 * @proc: process to wake up a thread in
975 * @thread: specific thread to wake-up (may be NULL)
976 * @sync: whether to do a synchronous wake-up
977 *
978 * This function wakes up a thread in the @proc process.
979 * The caller may provide a specific thread to wake-up in
980 * the @thread parameter. If @thread is NULL, this function
981 * will wake up threads that have called poll().
982 *
983 * Note that for this function to work as expected, callers
984 * should first call binder_select_thread() to find a thread
985 * to handle the work (if they don't have a thread already),
986 * and pass the result into the @thread parameter.
987 */
988static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
989 struct binder_thread *thread,
990 bool sync)
991{
992 assert_spin_locked(&proc->inner_lock);
993
994 if (thread) {
995 if (sync)
996 wake_up_interruptible_sync(&thread->wait);
997 else
998 wake_up_interruptible(&thread->wait);
999 return;
1000 }
1001
1002 /* Didn't find a thread waiting for proc work; this can happen
1003 * in two scenarios:
1004 * 1. All threads are busy handling transactions
1005 * In that case, one of those threads should call back into
1006 * the kernel driver soon and pick up this work.
1007 * 2. Threads are using the (e)poll interface, in which case
1008 * they may be blocked on the waitqueue without having been
1009 * added to waiting_threads. For this case, we just iterate
1010 * over all threads not handling transaction work, and
1011 * wake them all up. We wake all because we don't know whether
1012 * a thread that called into (e)poll is handling non-binder
1013 * work currently.
1014 */
1015 binder_wakeup_poll_threads_ilocked(proc, sync);
1016}
1017
1018static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1019{
1020 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1021
1022 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1023}
1024
1025static void binder_set_nice(long nice)
1026{
1027 long min_nice;
1028
1029 if (can_nice(current, nice)) {
1030 set_user_nice(current, nice);
1031 return;
1032 }
1033 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
1034 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1035 "%d: nice value %ld not allowed use %ld instead\n",
1036 current->pid, nice, min_nice);
1037 set_user_nice(current, min_nice);
1038 if (min_nice <= MAX_NICE)
1039 return;
1040 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
1041}
1042
1043static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1044 binder_uintptr_t ptr)
1045{
1046 struct rb_node *n = proc->nodes.rb_node;
1047 struct binder_node *node;
1048
1049 assert_spin_locked(&proc->inner_lock);
1050
1051 while (n) {
1052 node = rb_entry(n, struct binder_node, rb_node);
1053
1054 if (ptr < node->ptr)
1055 n = n->rb_left;
1056 else if (ptr > node->ptr)
1057 n = n->rb_right;
1058 else {
1059 /*
1060 * take an implicit weak reference
1061 * to ensure node stays alive until
1062 * call to binder_put_node()
1063 */
1064 binder_inc_node_tmpref_ilocked(node);
1065 return node;
1066 }
1067 }
1068 return NULL;
1069}
1070
1071static struct binder_node *binder_get_node(struct binder_proc *proc,
1072 binder_uintptr_t ptr)
1073{
1074 struct binder_node *node;
1075
1076 binder_inner_proc_lock(proc);
1077 node = binder_get_node_ilocked(proc, ptr);
1078 binder_inner_proc_unlock(proc);
1079 return node;
1080}
1081
1082static struct binder_node *binder_init_node_ilocked(
1083 struct binder_proc *proc,
1084 struct binder_node *new_node,
1085 struct flat_binder_object *fp)
1086{
1087 struct rb_node **p = &proc->nodes.rb_node;
1088 struct rb_node *parent = NULL;
1089 struct binder_node *node;
1090 binder_uintptr_t ptr = fp ? fp->binder : 0;
1091 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1092 __u32 flags = fp ? fp->flags : 0;
1093
1094 assert_spin_locked(&proc->inner_lock);
1095
1096 while (*p) {
1097
1098 parent = *p;
1099 node = rb_entry(parent, struct binder_node, rb_node);
1100
1101 if (ptr < node->ptr)
1102 p = &(*p)->rb_left;
1103 else if (ptr > node->ptr)
1104 p = &(*p)->rb_right;
1105 else {
1106 /*
1107 * A matching node is already in
1108 * the rb tree. Abandon the init
1109 * and return it.
1110 */
1111 binder_inc_node_tmpref_ilocked(node);
1112 return node;
1113 }
1114 }
1115 node = new_node;
1116 binder_stats_created(BINDER_STAT_NODE);
1117 node->tmp_refs++;
1118 rb_link_node(&node->rb_node, parent, p);
1119 rb_insert_color(&node->rb_node, &proc->nodes);
1120 node->debug_id = atomic_inc_return(&binder_last_id);
1121 node->proc = proc;
1122 node->ptr = ptr;
1123 node->cookie = cookie;
1124 node->work.type = BINDER_WORK_NODE;
1125 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1126 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
David Brazdil0f672f62019-12-10 10:32:29 +00001127 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001128 spin_lock_init(&node->lock);
1129 INIT_LIST_HEAD(&node->work.entry);
1130 INIT_LIST_HEAD(&node->async_todo);
1131 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1132 "%d:%d node %d u%016llx c%016llx created\n",
1133 proc->pid, current->pid, node->debug_id,
1134 (u64)node->ptr, (u64)node->cookie);
1135
1136 return node;
1137}
1138
1139static struct binder_node *binder_new_node(struct binder_proc *proc,
1140 struct flat_binder_object *fp)
1141{
1142 struct binder_node *node;
1143 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1144
1145 if (!new_node)
1146 return NULL;
1147 binder_inner_proc_lock(proc);
1148 node = binder_init_node_ilocked(proc, new_node, fp);
1149 binder_inner_proc_unlock(proc);
1150 if (node != new_node)
1151 /*
1152 * The node was already added by another thread
1153 */
1154 kfree(new_node);
1155
1156 return node;
1157}
1158
1159static void binder_free_node(struct binder_node *node)
1160{
1161 kfree(node);
1162 binder_stats_deleted(BINDER_STAT_NODE);
1163}
1164
1165static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1166 int internal,
1167 struct list_head *target_list)
1168{
1169 struct binder_proc *proc = node->proc;
1170
1171 assert_spin_locked(&node->lock);
1172 if (proc)
1173 assert_spin_locked(&proc->inner_lock);
1174 if (strong) {
1175 if (internal) {
1176 if (target_list == NULL &&
1177 node->internal_strong_refs == 0 &&
1178 !(node->proc &&
1179 node == node->proc->context->binder_context_mgr_node &&
1180 node->has_strong_ref)) {
1181 pr_err("invalid inc strong node for %d\n",
1182 node->debug_id);
1183 return -EINVAL;
1184 }
1185 node->internal_strong_refs++;
1186 } else
1187 node->local_strong_refs++;
1188 if (!node->has_strong_ref && target_list) {
David Brazdil0f672f62019-12-10 10:32:29 +00001189 struct binder_thread *thread = container_of(target_list,
1190 struct binder_thread, todo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191 binder_dequeue_work_ilocked(&node->work);
David Brazdil0f672f62019-12-10 10:32:29 +00001192 BUG_ON(&thread->todo != target_list);
1193 binder_enqueue_deferred_thread_work_ilocked(thread,
1194 &node->work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001195 }
1196 } else {
1197 if (!internal)
1198 node->local_weak_refs++;
1199 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1200 if (target_list == NULL) {
1201 pr_err("invalid inc weak node for %d\n",
1202 node->debug_id);
1203 return -EINVAL;
1204 }
1205 /*
1206 * See comment above
1207 */
1208 binder_enqueue_work_ilocked(&node->work, target_list);
1209 }
1210 }
1211 return 0;
1212}
1213
1214static int binder_inc_node(struct binder_node *node, int strong, int internal,
1215 struct list_head *target_list)
1216{
1217 int ret;
1218
1219 binder_node_inner_lock(node);
1220 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1221 binder_node_inner_unlock(node);
1222
1223 return ret;
1224}
1225
1226static bool binder_dec_node_nilocked(struct binder_node *node,
1227 int strong, int internal)
1228{
1229 struct binder_proc *proc = node->proc;
1230
1231 assert_spin_locked(&node->lock);
1232 if (proc)
1233 assert_spin_locked(&proc->inner_lock);
1234 if (strong) {
1235 if (internal)
1236 node->internal_strong_refs--;
1237 else
1238 node->local_strong_refs--;
1239 if (node->local_strong_refs || node->internal_strong_refs)
1240 return false;
1241 } else {
1242 if (!internal)
1243 node->local_weak_refs--;
1244 if (node->local_weak_refs || node->tmp_refs ||
1245 !hlist_empty(&node->refs))
1246 return false;
1247 }
1248
1249 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
1250 if (list_empty(&node->work.entry)) {
1251 binder_enqueue_work_ilocked(&node->work, &proc->todo);
1252 binder_wakeup_proc_ilocked(proc);
1253 }
1254 } else {
1255 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1256 !node->local_weak_refs && !node->tmp_refs) {
1257 if (proc) {
1258 binder_dequeue_work_ilocked(&node->work);
1259 rb_erase(&node->rb_node, &proc->nodes);
1260 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1261 "refless node %d deleted\n",
1262 node->debug_id);
1263 } else {
1264 BUG_ON(!list_empty(&node->work.entry));
1265 spin_lock(&binder_dead_nodes_lock);
1266 /*
1267 * tmp_refs could have changed so
1268 * check it again
1269 */
1270 if (node->tmp_refs) {
1271 spin_unlock(&binder_dead_nodes_lock);
1272 return false;
1273 }
1274 hlist_del(&node->dead_node);
1275 spin_unlock(&binder_dead_nodes_lock);
1276 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1277 "dead node %d deleted\n",
1278 node->debug_id);
1279 }
1280 return true;
1281 }
1282 }
1283 return false;
1284}
1285
1286static void binder_dec_node(struct binder_node *node, int strong, int internal)
1287{
1288 bool free_node;
1289
1290 binder_node_inner_lock(node);
1291 free_node = binder_dec_node_nilocked(node, strong, internal);
1292 binder_node_inner_unlock(node);
1293 if (free_node)
1294 binder_free_node(node);
1295}
1296
1297static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
1298{
1299 /*
1300 * No call to binder_inc_node() is needed since we
1301 * don't need to inform userspace of any changes to
1302 * tmp_refs
1303 */
1304 node->tmp_refs++;
1305}
1306
1307/**
1308 * binder_inc_node_tmpref() - take a temporary reference on node
1309 * @node: node to reference
1310 *
1311 * Take reference on node to prevent the node from being freed
1312 * while referenced only by a local variable. The inner lock is
1313 * needed to serialize with the node work on the queue (which
1314 * isn't needed after the node is dead). If the node is dead
1315 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1316 * node->tmp_refs against dead-node-only cases where the node
1317 * lock cannot be acquired (eg traversing the dead node list to
1318 * print nodes)
1319 */
1320static void binder_inc_node_tmpref(struct binder_node *node)
1321{
1322 binder_node_lock(node);
1323 if (node->proc)
1324 binder_inner_proc_lock(node->proc);
1325 else
1326 spin_lock(&binder_dead_nodes_lock);
1327 binder_inc_node_tmpref_ilocked(node);
1328 if (node->proc)
1329 binder_inner_proc_unlock(node->proc);
1330 else
1331 spin_unlock(&binder_dead_nodes_lock);
1332 binder_node_unlock(node);
1333}
1334
1335/**
1336 * binder_dec_node_tmpref() - remove a temporary reference on node
1337 * @node: node to reference
1338 *
1339 * Release temporary reference on node taken via binder_inc_node_tmpref()
1340 */
1341static void binder_dec_node_tmpref(struct binder_node *node)
1342{
1343 bool free_node;
1344
1345 binder_node_inner_lock(node);
1346 if (!node->proc)
1347 spin_lock(&binder_dead_nodes_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001348 else
1349 __acquire(&binder_dead_nodes_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001350 node->tmp_refs--;
1351 BUG_ON(node->tmp_refs < 0);
1352 if (!node->proc)
1353 spin_unlock(&binder_dead_nodes_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001354 else
1355 __release(&binder_dead_nodes_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001356 /*
1357 * Call binder_dec_node() to check if all refcounts are 0
1358 * and cleanup is needed. Calling with strong=0 and internal=1
1359 * causes no actual reference to be released in binder_dec_node().
1360 * If that changes, a change is needed here too.
1361 */
1362 free_node = binder_dec_node_nilocked(node, 0, 1);
1363 binder_node_inner_unlock(node);
1364 if (free_node)
1365 binder_free_node(node);
1366}
1367
1368static void binder_put_node(struct binder_node *node)
1369{
1370 binder_dec_node_tmpref(node);
1371}
1372
1373static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1374 u32 desc, bool need_strong_ref)
1375{
1376 struct rb_node *n = proc->refs_by_desc.rb_node;
1377 struct binder_ref *ref;
1378
1379 while (n) {
1380 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1381
1382 if (desc < ref->data.desc) {
1383 n = n->rb_left;
1384 } else if (desc > ref->data.desc) {
1385 n = n->rb_right;
1386 } else if (need_strong_ref && !ref->data.strong) {
1387 binder_user_error("tried to use weak ref as strong ref\n");
1388 return NULL;
1389 } else {
1390 return ref;
1391 }
1392 }
1393 return NULL;
1394}
1395
1396/**
1397 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1398 * @proc: binder_proc that owns the ref
1399 * @node: binder_node of target
1400 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1401 *
1402 * Look up the ref for the given node and return it if it exists
1403 *
1404 * If it doesn't exist and the caller provides a newly allocated
1405 * ref, initialize the fields of the newly allocated ref and insert
1406 * into the given proc rb_trees and node refs list.
1407 *
1408 * Return: the ref for node. It is possible that another thread
1409 * allocated/initialized the ref first in which case the
1410 * returned ref would be different than the passed-in
1411 * new_ref. new_ref must be kfree'd by the caller in
1412 * this case.
1413 */
1414static struct binder_ref *binder_get_ref_for_node_olocked(
1415 struct binder_proc *proc,
1416 struct binder_node *node,
1417 struct binder_ref *new_ref)
1418{
1419 struct binder_context *context = proc->context;
1420 struct rb_node **p = &proc->refs_by_node.rb_node;
1421 struct rb_node *parent = NULL;
1422 struct binder_ref *ref;
1423 struct rb_node *n;
1424
1425 while (*p) {
1426 parent = *p;
1427 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1428
1429 if (node < ref->node)
1430 p = &(*p)->rb_left;
1431 else if (node > ref->node)
1432 p = &(*p)->rb_right;
1433 else
1434 return ref;
1435 }
1436 if (!new_ref)
1437 return NULL;
1438
1439 binder_stats_created(BINDER_STAT_REF);
1440 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1441 new_ref->proc = proc;
1442 new_ref->node = node;
1443 rb_link_node(&new_ref->rb_node_node, parent, p);
1444 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1445
1446 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1447 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1448 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1449 if (ref->data.desc > new_ref->data.desc)
1450 break;
1451 new_ref->data.desc = ref->data.desc + 1;
1452 }
1453
1454 p = &proc->refs_by_desc.rb_node;
1455 while (*p) {
1456 parent = *p;
1457 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1458
1459 if (new_ref->data.desc < ref->data.desc)
1460 p = &(*p)->rb_left;
1461 else if (new_ref->data.desc > ref->data.desc)
1462 p = &(*p)->rb_right;
1463 else
1464 BUG();
1465 }
1466 rb_link_node(&new_ref->rb_node_desc, parent, p);
1467 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1468
1469 binder_node_lock(node);
1470 hlist_add_head(&new_ref->node_entry, &node->refs);
1471
1472 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1473 "%d new ref %d desc %d for node %d\n",
1474 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1475 node->debug_id);
1476 binder_node_unlock(node);
1477 return new_ref;
1478}
1479
1480static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1481{
1482 bool delete_node = false;
1483
1484 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1485 "%d delete ref %d desc %d for node %d\n",
1486 ref->proc->pid, ref->data.debug_id, ref->data.desc,
1487 ref->node->debug_id);
1488
1489 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1490 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1491
1492 binder_node_inner_lock(ref->node);
1493 if (ref->data.strong)
1494 binder_dec_node_nilocked(ref->node, 1, 1);
1495
1496 hlist_del(&ref->node_entry);
1497 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1498 binder_node_inner_unlock(ref->node);
1499 /*
1500 * Clear ref->node unless we want the caller to free the node
1501 */
1502 if (!delete_node) {
1503 /*
1504 * The caller uses ref->node to determine
1505 * whether the node needs to be freed. Clear
1506 * it since the node is still alive.
1507 */
1508 ref->node = NULL;
1509 }
1510
1511 if (ref->death) {
1512 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1513 "%d delete ref %d desc %d has death notification\n",
1514 ref->proc->pid, ref->data.debug_id,
1515 ref->data.desc);
1516 binder_dequeue_work(ref->proc, &ref->death->work);
1517 binder_stats_deleted(BINDER_STAT_DEATH);
1518 }
1519 binder_stats_deleted(BINDER_STAT_REF);
1520}
1521
1522/**
1523 * binder_inc_ref_olocked() - increment the ref for given handle
1524 * @ref: ref to be incremented
1525 * @strong: if true, strong increment, else weak
1526 * @target_list: list to queue node work on
1527 *
1528 * Increment the ref. @ref->proc->outer_lock must be held on entry
1529 *
1530 * Return: 0, if successful, else errno
1531 */
1532static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1533 struct list_head *target_list)
1534{
1535 int ret;
1536
1537 if (strong) {
1538 if (ref->data.strong == 0) {
1539 ret = binder_inc_node(ref->node, 1, 1, target_list);
1540 if (ret)
1541 return ret;
1542 }
1543 ref->data.strong++;
1544 } else {
1545 if (ref->data.weak == 0) {
1546 ret = binder_inc_node(ref->node, 0, 1, target_list);
1547 if (ret)
1548 return ret;
1549 }
1550 ref->data.weak++;
1551 }
1552 return 0;
1553}
1554
1555/**
1556 * binder_dec_ref() - dec the ref for given handle
1557 * @ref: ref to be decremented
1558 * @strong: if true, strong decrement, else weak
1559 *
1560 * Decrement the ref.
1561 *
1562 * Return: true if ref is cleaned up and ready to be freed
1563 */
1564static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1565{
1566 if (strong) {
1567 if (ref->data.strong == 0) {
1568 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1569 ref->proc->pid, ref->data.debug_id,
1570 ref->data.desc, ref->data.strong,
1571 ref->data.weak);
1572 return false;
1573 }
1574 ref->data.strong--;
1575 if (ref->data.strong == 0)
1576 binder_dec_node(ref->node, strong, 1);
1577 } else {
1578 if (ref->data.weak == 0) {
1579 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1580 ref->proc->pid, ref->data.debug_id,
1581 ref->data.desc, ref->data.strong,
1582 ref->data.weak);
1583 return false;
1584 }
1585 ref->data.weak--;
1586 }
1587 if (ref->data.strong == 0 && ref->data.weak == 0) {
1588 binder_cleanup_ref_olocked(ref);
1589 return true;
1590 }
1591 return false;
1592}
1593
1594/**
1595 * binder_get_node_from_ref() - get the node from the given proc/desc
1596 * @proc: proc containing the ref
1597 * @desc: the handle associated with the ref
1598 * @need_strong_ref: if true, only return node if ref is strong
1599 * @rdata: the id/refcount data for the ref
1600 *
1601 * Given a proc and ref handle, return the associated binder_node
1602 *
1603 * Return: a binder_node or NULL if not found or not strong when strong required
1604 */
1605static struct binder_node *binder_get_node_from_ref(
1606 struct binder_proc *proc,
1607 u32 desc, bool need_strong_ref,
1608 struct binder_ref_data *rdata)
1609{
1610 struct binder_node *node;
1611 struct binder_ref *ref;
1612
1613 binder_proc_lock(proc);
1614 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1615 if (!ref)
1616 goto err_no_ref;
1617 node = ref->node;
1618 /*
1619 * Take an implicit reference on the node to ensure
1620 * it stays alive until the call to binder_put_node()
1621 */
1622 binder_inc_node_tmpref(node);
1623 if (rdata)
1624 *rdata = ref->data;
1625 binder_proc_unlock(proc);
1626
1627 return node;
1628
1629err_no_ref:
1630 binder_proc_unlock(proc);
1631 return NULL;
1632}
1633
1634/**
1635 * binder_free_ref() - free the binder_ref
1636 * @ref: ref to free
1637 *
1638 * Free the binder_ref. Free the binder_node indicated by ref->node
1639 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1640 */
1641static void binder_free_ref(struct binder_ref *ref)
1642{
1643 if (ref->node)
1644 binder_free_node(ref->node);
1645 kfree(ref->death);
1646 kfree(ref);
1647}
1648
1649/**
1650 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1651 * @proc: proc containing the ref
1652 * @desc: the handle associated with the ref
1653 * @increment: true=inc reference, false=dec reference
1654 * @strong: true=strong reference, false=weak reference
1655 * @rdata: the id/refcount data for the ref
1656 *
1657 * Given a proc and ref handle, increment or decrement the ref
1658 * according to "increment" arg.
1659 *
1660 * Return: 0 if successful, else errno
1661 */
1662static int binder_update_ref_for_handle(struct binder_proc *proc,
1663 uint32_t desc, bool increment, bool strong,
1664 struct binder_ref_data *rdata)
1665{
1666 int ret = 0;
1667 struct binder_ref *ref;
1668 bool delete_ref = false;
1669
1670 binder_proc_lock(proc);
1671 ref = binder_get_ref_olocked(proc, desc, strong);
1672 if (!ref) {
1673 ret = -EINVAL;
1674 goto err_no_ref;
1675 }
1676 if (increment)
1677 ret = binder_inc_ref_olocked(ref, strong, NULL);
1678 else
1679 delete_ref = binder_dec_ref_olocked(ref, strong);
1680
1681 if (rdata)
1682 *rdata = ref->data;
1683 binder_proc_unlock(proc);
1684
1685 if (delete_ref)
1686 binder_free_ref(ref);
1687 return ret;
1688
1689err_no_ref:
1690 binder_proc_unlock(proc);
1691 return ret;
1692}
1693
1694/**
1695 * binder_dec_ref_for_handle() - dec the ref for given handle
1696 * @proc: proc containing the ref
1697 * @desc: the handle associated with the ref
1698 * @strong: true=strong reference, false=weak reference
1699 * @rdata: the id/refcount data for the ref
1700 *
1701 * Just calls binder_update_ref_for_handle() to decrement the ref.
1702 *
1703 * Return: 0 if successful, else errno
1704 */
1705static int binder_dec_ref_for_handle(struct binder_proc *proc,
1706 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1707{
1708 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1709}
1710
1711
1712/**
1713 * binder_inc_ref_for_node() - increment the ref for given proc/node
1714 * @proc: proc containing the ref
1715 * @node: target node
1716 * @strong: true=strong reference, false=weak reference
1717 * @target_list: worklist to use if node is incremented
1718 * @rdata: the id/refcount data for the ref
1719 *
1720 * Given a proc and node, increment the ref. Create the ref if it
1721 * doesn't already exist
1722 *
1723 * Return: 0 if successful, else errno
1724 */
1725static int binder_inc_ref_for_node(struct binder_proc *proc,
1726 struct binder_node *node,
1727 bool strong,
1728 struct list_head *target_list,
1729 struct binder_ref_data *rdata)
1730{
1731 struct binder_ref *ref;
1732 struct binder_ref *new_ref = NULL;
1733 int ret = 0;
1734
1735 binder_proc_lock(proc);
1736 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1737 if (!ref) {
1738 binder_proc_unlock(proc);
1739 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1740 if (!new_ref)
1741 return -ENOMEM;
1742 binder_proc_lock(proc);
1743 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1744 }
1745 ret = binder_inc_ref_olocked(ref, strong, target_list);
1746 *rdata = ref->data;
1747 binder_proc_unlock(proc);
1748 if (new_ref && ref != new_ref)
1749 /*
1750 * Another thread created the ref first so
1751 * free the one we allocated
1752 */
1753 kfree(new_ref);
1754 return ret;
1755}
1756
1757static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1758 struct binder_transaction *t)
1759{
1760 BUG_ON(!target_thread);
1761 assert_spin_locked(&target_thread->proc->inner_lock);
1762 BUG_ON(target_thread->transaction_stack != t);
1763 BUG_ON(target_thread->transaction_stack->from != target_thread);
1764 target_thread->transaction_stack =
1765 target_thread->transaction_stack->from_parent;
1766 t->from = NULL;
1767}
1768
1769/**
1770 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1771 * @thread: thread to decrement
1772 *
1773 * A thread needs to be kept alive while being used to create or
1774 * handle a transaction. binder_get_txn_from() is used to safely
1775 * extract t->from from a binder_transaction and keep the thread
1776 * indicated by t->from from being freed. When done with that
1777 * binder_thread, this function is called to decrement the
1778 * tmp_ref and free if appropriate (thread has been released
1779 * and no transaction being processed by the driver)
1780 */
1781static void binder_thread_dec_tmpref(struct binder_thread *thread)
1782{
1783 /*
1784 * atomic is used to protect the counter value while
1785 * it cannot reach zero or thread->is_dead is false
1786 */
1787 binder_inner_proc_lock(thread->proc);
1788 atomic_dec(&thread->tmp_ref);
1789 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
1790 binder_inner_proc_unlock(thread->proc);
1791 binder_free_thread(thread);
1792 return;
1793 }
1794 binder_inner_proc_unlock(thread->proc);
1795}
1796
1797/**
1798 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1799 * @proc: proc to decrement
1800 *
1801 * A binder_proc needs to be kept alive while being used to create or
1802 * handle a transaction. proc->tmp_ref is incremented when
1803 * creating a new transaction or the binder_proc is currently in-use
1804 * by threads that are being released. When done with the binder_proc,
1805 * this function is called to decrement the counter and free the
1806 * proc if appropriate (proc has been released, all threads have
1807 * been released and not currenly in-use to process a transaction).
1808 */
1809static void binder_proc_dec_tmpref(struct binder_proc *proc)
1810{
1811 binder_inner_proc_lock(proc);
1812 proc->tmp_ref--;
1813 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1814 !proc->tmp_ref) {
1815 binder_inner_proc_unlock(proc);
1816 binder_free_proc(proc);
1817 return;
1818 }
1819 binder_inner_proc_unlock(proc);
1820}
1821
1822/**
1823 * binder_get_txn_from() - safely extract the "from" thread in transaction
1824 * @t: binder transaction for t->from
1825 *
1826 * Atomically return the "from" thread and increment the tmp_ref
1827 * count for the thread to ensure it stays alive until
1828 * binder_thread_dec_tmpref() is called.
1829 *
1830 * Return: the value of t->from
1831 */
1832static struct binder_thread *binder_get_txn_from(
1833 struct binder_transaction *t)
1834{
1835 struct binder_thread *from;
1836
1837 spin_lock(&t->lock);
1838 from = t->from;
1839 if (from)
1840 atomic_inc(&from->tmp_ref);
1841 spin_unlock(&t->lock);
1842 return from;
1843}
1844
1845/**
1846 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1847 * @t: binder transaction for t->from
1848 *
1849 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1850 * to guarantee that the thread cannot be released while operating on it.
1851 * The caller must call binder_inner_proc_unlock() to release the inner lock
1852 * as well as call binder_dec_thread_txn() to release the reference.
1853 *
1854 * Return: the value of t->from
1855 */
1856static struct binder_thread *binder_get_txn_from_and_acq_inner(
1857 struct binder_transaction *t)
David Brazdil0f672f62019-12-10 10:32:29 +00001858 __acquires(&t->from->proc->inner_lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001859{
1860 struct binder_thread *from;
1861
1862 from = binder_get_txn_from(t);
David Brazdil0f672f62019-12-10 10:32:29 +00001863 if (!from) {
1864 __acquire(&from->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001865 return NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00001866 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001867 binder_inner_proc_lock(from->proc);
1868 if (t->from) {
1869 BUG_ON(from != t->from);
1870 return from;
1871 }
1872 binder_inner_proc_unlock(from->proc);
David Brazdil0f672f62019-12-10 10:32:29 +00001873 __acquire(&from->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001874 binder_thread_dec_tmpref(from);
1875 return NULL;
1876}
1877
David Brazdil0f672f62019-12-10 10:32:29 +00001878/**
1879 * binder_free_txn_fixups() - free unprocessed fd fixups
1880 * @t: binder transaction for t->from
1881 *
1882 * If the transaction is being torn down prior to being
1883 * processed by the target process, free all of the
1884 * fd fixups and fput the file structs. It is safe to
1885 * call this function after the fixups have been
1886 * processed -- in that case, the list will be empty.
1887 */
1888static void binder_free_txn_fixups(struct binder_transaction *t)
1889{
1890 struct binder_txn_fd_fixup *fixup, *tmp;
1891
1892 list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
1893 fput(fixup->file);
1894 list_del(&fixup->fixup_entry);
1895 kfree(fixup);
1896 }
1897}
1898
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001899static void binder_free_transaction(struct binder_transaction *t)
1900{
David Brazdil0f672f62019-12-10 10:32:29 +00001901 struct binder_proc *target_proc = t->to_proc;
1902
1903 if (target_proc) {
1904 binder_inner_proc_lock(target_proc);
1905 if (t->buffer)
1906 t->buffer->transaction = NULL;
1907 binder_inner_proc_unlock(target_proc);
1908 }
1909 /*
1910 * If the transaction has no target_proc, then
1911 * t->buffer->transaction has already been cleared.
1912 */
1913 binder_free_txn_fixups(t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001914 kfree(t);
1915 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1916}
1917
1918static void binder_send_failed_reply(struct binder_transaction *t,
1919 uint32_t error_code)
1920{
1921 struct binder_thread *target_thread;
1922 struct binder_transaction *next;
1923
1924 BUG_ON(t->flags & TF_ONE_WAY);
1925 while (1) {
1926 target_thread = binder_get_txn_from_and_acq_inner(t);
1927 if (target_thread) {
1928 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1929 "send failed reply for transaction %d to %d:%d\n",
1930 t->debug_id,
1931 target_thread->proc->pid,
1932 target_thread->pid);
1933
1934 binder_pop_transaction_ilocked(target_thread, t);
1935 if (target_thread->reply_error.cmd == BR_OK) {
1936 target_thread->reply_error.cmd = error_code;
1937 binder_enqueue_thread_work_ilocked(
1938 target_thread,
1939 &target_thread->reply_error.work);
1940 wake_up_interruptible(&target_thread->wait);
1941 } else {
1942 /*
1943 * Cannot get here for normal operation, but
1944 * we can if multiple synchronous transactions
1945 * are sent without blocking for responses.
1946 * Just ignore the 2nd error in this case.
1947 */
1948 pr_warn("Unexpected reply error: %u\n",
1949 target_thread->reply_error.cmd);
1950 }
1951 binder_inner_proc_unlock(target_thread->proc);
1952 binder_thread_dec_tmpref(target_thread);
1953 binder_free_transaction(t);
1954 return;
1955 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001956 __release(&target_thread->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001957 next = t->from_parent;
1958
1959 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1960 "send failed reply for transaction %d, target dead\n",
1961 t->debug_id);
1962
1963 binder_free_transaction(t);
1964 if (next == NULL) {
1965 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1966 "reply failed, no target thread at root\n");
1967 return;
1968 }
1969 t = next;
1970 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1971 "reply failed, no target thread -- retry %d\n",
1972 t->debug_id);
1973 }
1974}
1975
1976/**
1977 * binder_cleanup_transaction() - cleans up undelivered transaction
1978 * @t: transaction that needs to be cleaned up
1979 * @reason: reason the transaction wasn't delivered
1980 * @error_code: error to return to caller (if synchronous call)
1981 */
1982static void binder_cleanup_transaction(struct binder_transaction *t,
1983 const char *reason,
1984 uint32_t error_code)
1985{
1986 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
1987 binder_send_failed_reply(t, error_code);
1988 } else {
1989 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
1990 "undelivered transaction %d, %s\n",
1991 t->debug_id, reason);
1992 binder_free_transaction(t);
1993 }
1994}
1995
1996/**
David Brazdil0f672f62019-12-10 10:32:29 +00001997 * binder_get_object() - gets object and checks for valid metadata
1998 * @proc: binder_proc owning the buffer
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001999 * @buffer: binder_buffer that we're parsing.
David Brazdil0f672f62019-12-10 10:32:29 +00002000 * @offset: offset in the @buffer at which to validate an object.
2001 * @object: struct binder_object to read into
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002002 *
2003 * Return: If there's a valid metadata object at @offset in @buffer, the
David Brazdil0f672f62019-12-10 10:32:29 +00002004 * size of that object. Otherwise, it returns zero. The object
2005 * is read into the struct binder_object pointed to by @object.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002006 */
David Brazdil0f672f62019-12-10 10:32:29 +00002007static size_t binder_get_object(struct binder_proc *proc,
2008 struct binder_buffer *buffer,
2009 unsigned long offset,
2010 struct binder_object *object)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002011{
David Brazdil0f672f62019-12-10 10:32:29 +00002012 size_t read_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002013 struct binder_object_header *hdr;
2014 size_t object_size = 0;
2015
David Brazdil0f672f62019-12-10 10:32:29 +00002016 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
2017 if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
2018 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2019 offset, read_size))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002020 return 0;
2021
David Brazdil0f672f62019-12-10 10:32:29 +00002022 /* Ok, now see if we read a complete object. */
2023 hdr = &object->hdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002024 switch (hdr->type) {
2025 case BINDER_TYPE_BINDER:
2026 case BINDER_TYPE_WEAK_BINDER:
2027 case BINDER_TYPE_HANDLE:
2028 case BINDER_TYPE_WEAK_HANDLE:
2029 object_size = sizeof(struct flat_binder_object);
2030 break;
2031 case BINDER_TYPE_FD:
2032 object_size = sizeof(struct binder_fd_object);
2033 break;
2034 case BINDER_TYPE_PTR:
2035 object_size = sizeof(struct binder_buffer_object);
2036 break;
2037 case BINDER_TYPE_FDA:
2038 object_size = sizeof(struct binder_fd_array_object);
2039 break;
2040 default:
2041 return 0;
2042 }
2043 if (offset <= buffer->data_size - object_size &&
2044 buffer->data_size >= object_size)
2045 return object_size;
2046 else
2047 return 0;
2048}
2049
2050/**
2051 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
David Brazdil0f672f62019-12-10 10:32:29 +00002052 * @proc: binder_proc owning the buffer
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002053 * @b: binder_buffer containing the object
David Brazdil0f672f62019-12-10 10:32:29 +00002054 * @object: struct binder_object to read into
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002055 * @index: index in offset array at which the binder_buffer_object is
2056 * located
David Brazdil0f672f62019-12-10 10:32:29 +00002057 * @start_offset: points to the start of the offset array
2058 * @object_offsetp: offset of @object read from @b
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002059 * @num_valid: the number of valid offsets in the offset array
2060 *
2061 * Return: If @index is within the valid range of the offset array
2062 * described by @start and @num_valid, and if there's a valid
2063 * binder_buffer_object at the offset found in index @index
2064 * of the offset array, that object is returned. Otherwise,
2065 * %NULL is returned.
2066 * Note that the offset found in index @index itself is not
2067 * verified; this function assumes that @num_valid elements
2068 * from @start were previously verified to have valid offsets.
David Brazdil0f672f62019-12-10 10:32:29 +00002069 * If @object_offsetp is non-NULL, then the offset within
2070 * @b is written to it.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002071 */
David Brazdil0f672f62019-12-10 10:32:29 +00002072static struct binder_buffer_object *binder_validate_ptr(
2073 struct binder_proc *proc,
2074 struct binder_buffer *b,
2075 struct binder_object *object,
2076 binder_size_t index,
2077 binder_size_t start_offset,
2078 binder_size_t *object_offsetp,
2079 binder_size_t num_valid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002080{
David Brazdil0f672f62019-12-10 10:32:29 +00002081 size_t object_size;
2082 binder_size_t object_offset;
2083 unsigned long buffer_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002084
2085 if (index >= num_valid)
2086 return NULL;
2087
David Brazdil0f672f62019-12-10 10:32:29 +00002088 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2089 if (binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2090 b, buffer_offset,
2091 sizeof(object_offset)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002092 return NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00002093 object_size = binder_get_object(proc, b, object_offset, object);
2094 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
2095 return NULL;
2096 if (object_offsetp)
2097 *object_offsetp = object_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002098
David Brazdil0f672f62019-12-10 10:32:29 +00002099 return &object->bbo;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002100}
2101
2102/**
2103 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
David Brazdil0f672f62019-12-10 10:32:29 +00002104 * @proc: binder_proc owning the buffer
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002105 * @b: transaction buffer
David Brazdil0f672f62019-12-10 10:32:29 +00002106 * @objects_start_offset: offset to start of objects buffer
2107 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2108 * @fixup_offset: start offset in @buffer to fix up
2109 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2110 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002111 *
2112 * Return: %true if a fixup in buffer @buffer at offset @offset is
2113 * allowed.
2114 *
2115 * For safety reasons, we only allow fixups inside a buffer to happen
2116 * at increasing offsets; additionally, we only allow fixup on the last
2117 * buffer object that was verified, or one of its parents.
2118 *
2119 * Example of what is allowed:
2120 *
2121 * A
2122 * B (parent = A, offset = 0)
2123 * C (parent = A, offset = 16)
2124 * D (parent = C, offset = 0)
2125 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2126 *
2127 * Examples of what is not allowed:
2128 *
2129 * Decreasing offsets within the same parent:
2130 * A
2131 * C (parent = A, offset = 16)
2132 * B (parent = A, offset = 0) // decreasing offset within A
2133 *
2134 * Referring to a parent that wasn't the last object or any of its parents:
2135 * A
2136 * B (parent = A, offset = 0)
2137 * C (parent = A, offset = 0)
2138 * C (parent = A, offset = 16)
2139 * D (parent = B, offset = 0) // B is not A or any of A's parents
2140 */
David Brazdil0f672f62019-12-10 10:32:29 +00002141static bool binder_validate_fixup(struct binder_proc *proc,
2142 struct binder_buffer *b,
2143 binder_size_t objects_start_offset,
2144 binder_size_t buffer_obj_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002145 binder_size_t fixup_offset,
David Brazdil0f672f62019-12-10 10:32:29 +00002146 binder_size_t last_obj_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002147 binder_size_t last_min_offset)
2148{
David Brazdil0f672f62019-12-10 10:32:29 +00002149 if (!last_obj_offset) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002150 /* Nothing to fix up in */
2151 return false;
2152 }
2153
David Brazdil0f672f62019-12-10 10:32:29 +00002154 while (last_obj_offset != buffer_obj_offset) {
2155 unsigned long buffer_offset;
2156 struct binder_object last_object;
2157 struct binder_buffer_object *last_bbo;
2158 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2159 &last_object);
2160 if (object_size != sizeof(*last_bbo))
2161 return false;
2162
2163 last_bbo = &last_object.bbo;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002164 /*
2165 * Safe to retrieve the parent of last_obj, since it
2166 * was already previously verified by the driver.
2167 */
David Brazdil0f672f62019-12-10 10:32:29 +00002168 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002169 return false;
David Brazdil0f672f62019-12-10 10:32:29 +00002170 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2171 buffer_offset = objects_start_offset +
2172 sizeof(binder_size_t) * last_bbo->parent;
2173 if (binder_alloc_copy_from_buffer(&proc->alloc,
2174 &last_obj_offset,
2175 b, buffer_offset,
2176 sizeof(last_obj_offset)))
2177 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002178 }
2179 return (fixup_offset >= last_min_offset);
2180}
2181
David Brazdil0f672f62019-12-10 10:32:29 +00002182/**
2183 * struct binder_task_work_cb - for deferred close
2184 *
2185 * @twork: callback_head for task work
2186 * @fd: fd to close
2187 *
2188 * Structure to pass task work to be handled after
2189 * returning from binder_ioctl() via task_work_add().
2190 */
2191struct binder_task_work_cb {
2192 struct callback_head twork;
2193 struct file *file;
2194};
2195
2196/**
2197 * binder_do_fd_close() - close list of file descriptors
2198 * @twork: callback head for task work
2199 *
2200 * It is not safe to call ksys_close() during the binder_ioctl()
2201 * function if there is a chance that binder's own file descriptor
2202 * might be closed. This is to meet the requirements for using
2203 * fdget() (see comments for __fget_light()). Therefore use
2204 * task_work_add() to schedule the close operation once we have
2205 * returned from binder_ioctl(). This function is a callback
2206 * for that mechanism and does the actual ksys_close() on the
2207 * given file descriptor.
2208 */
2209static void binder_do_fd_close(struct callback_head *twork)
2210{
2211 struct binder_task_work_cb *twcb = container_of(twork,
2212 struct binder_task_work_cb, twork);
2213
2214 fput(twcb->file);
2215 kfree(twcb);
2216}
2217
2218/**
2219 * binder_deferred_fd_close() - schedule a close for the given file-descriptor
2220 * @fd: file-descriptor to close
2221 *
2222 * See comments in binder_do_fd_close(). This function is used to schedule
2223 * a file-descriptor to be closed after returning from binder_ioctl().
2224 */
2225static void binder_deferred_fd_close(int fd)
2226{
2227 struct binder_task_work_cb *twcb;
2228
2229 twcb = kzalloc(sizeof(*twcb), GFP_KERNEL);
2230 if (!twcb)
2231 return;
2232 init_task_work(&twcb->twork, binder_do_fd_close);
2233 __close_fd_get_file(fd, &twcb->file);
Olivier Deprez157378f2022-04-04 15:47:50 +02002234 if (twcb->file) {
2235 filp_close(twcb->file, current->files);
2236 task_work_add(current, &twcb->twork, TWA_RESUME);
2237 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00002238 kfree(twcb);
Olivier Deprez157378f2022-04-04 15:47:50 +02002239 }
David Brazdil0f672f62019-12-10 10:32:29 +00002240}
2241
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002242static void binder_transaction_buffer_release(struct binder_proc *proc,
Olivier Deprez157378f2022-04-04 15:47:50 +02002243 struct binder_thread *thread,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002244 struct binder_buffer *buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00002245 binder_size_t failed_at,
2246 bool is_failure)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002247{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002248 int debug_id = buffer->debug_id;
David Brazdil0f672f62019-12-10 10:32:29 +00002249 binder_size_t off_start_offset, buffer_offset, off_end_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002250
2251 binder_debug(BINDER_DEBUG_TRANSACTION,
David Brazdil0f672f62019-12-10 10:32:29 +00002252 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002253 proc->pid, buffer->debug_id,
David Brazdil0f672f62019-12-10 10:32:29 +00002254 buffer->data_size, buffer->offsets_size,
2255 (unsigned long long)failed_at);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002256
2257 if (buffer->target_node)
2258 binder_dec_node(buffer->target_node, 1, 0);
2259
David Brazdil0f672f62019-12-10 10:32:29 +00002260 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
Olivier Deprez157378f2022-04-04 15:47:50 +02002261 off_end_offset = is_failure && failed_at ? failed_at :
David Brazdil0f672f62019-12-10 10:32:29 +00002262 off_start_offset + buffer->offsets_size;
2263 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2264 buffer_offset += sizeof(binder_size_t)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002265 struct binder_object_header *hdr;
David Brazdil0f672f62019-12-10 10:32:29 +00002266 size_t object_size = 0;
2267 struct binder_object object;
2268 binder_size_t object_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002269
David Brazdil0f672f62019-12-10 10:32:29 +00002270 if (!binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2271 buffer, buffer_offset,
2272 sizeof(object_offset)))
2273 object_size = binder_get_object(proc, buffer,
2274 object_offset, &object);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002275 if (object_size == 0) {
2276 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002277 debug_id, (u64)object_offset, buffer->data_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002278 continue;
2279 }
David Brazdil0f672f62019-12-10 10:32:29 +00002280 hdr = &object.hdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002281 switch (hdr->type) {
2282 case BINDER_TYPE_BINDER:
2283 case BINDER_TYPE_WEAK_BINDER: {
2284 struct flat_binder_object *fp;
2285 struct binder_node *node;
2286
2287 fp = to_flat_binder_object(hdr);
2288 node = binder_get_node(proc, fp->binder);
2289 if (node == NULL) {
2290 pr_err("transaction release %d bad node %016llx\n",
2291 debug_id, (u64)fp->binder);
2292 break;
2293 }
2294 binder_debug(BINDER_DEBUG_TRANSACTION,
2295 " node %d u%016llx\n",
2296 node->debug_id, (u64)node->ptr);
2297 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2298 0);
2299 binder_put_node(node);
2300 } break;
2301 case BINDER_TYPE_HANDLE:
2302 case BINDER_TYPE_WEAK_HANDLE: {
2303 struct flat_binder_object *fp;
2304 struct binder_ref_data rdata;
2305 int ret;
2306
2307 fp = to_flat_binder_object(hdr);
2308 ret = binder_dec_ref_for_handle(proc, fp->handle,
2309 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2310
2311 if (ret) {
2312 pr_err("transaction release %d bad handle %d, ret = %d\n",
2313 debug_id, fp->handle, ret);
2314 break;
2315 }
2316 binder_debug(BINDER_DEBUG_TRANSACTION,
2317 " ref %d desc %d\n",
2318 rdata.debug_id, rdata.desc);
2319 } break;
2320
2321 case BINDER_TYPE_FD: {
David Brazdil0f672f62019-12-10 10:32:29 +00002322 /*
2323 * No need to close the file here since user-space
2324 * closes it for for successfully delivered
2325 * transactions. For transactions that weren't
2326 * delivered, the new fd was never allocated so
2327 * there is no need to close and the fput on the
2328 * file is done when the transaction is torn
2329 * down.
2330 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002331 } break;
2332 case BINDER_TYPE_PTR:
2333 /*
2334 * Nothing to do here, this will get cleaned up when the
2335 * transaction buffer gets freed
2336 */
2337 break;
2338 case BINDER_TYPE_FDA: {
2339 struct binder_fd_array_object *fda;
2340 struct binder_buffer_object *parent;
David Brazdil0f672f62019-12-10 10:32:29 +00002341 struct binder_object ptr_object;
2342 binder_size_t fda_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002343 size_t fd_index;
2344 binder_size_t fd_buf_size;
David Brazdil0f672f62019-12-10 10:32:29 +00002345 binder_size_t num_valid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002346
Olivier Deprez157378f2022-04-04 15:47:50 +02002347 if (is_failure) {
David Brazdil0f672f62019-12-10 10:32:29 +00002348 /*
David Brazdil0f672f62019-12-10 10:32:29 +00002349 * The fd fixups have not been applied so no
2350 * fds need to be closed.
2351 */
2352 continue;
2353 }
2354
2355 num_valid = (buffer_offset - off_start_offset) /
2356 sizeof(binder_size_t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002357 fda = to_binder_fd_array_object(hdr);
David Brazdil0f672f62019-12-10 10:32:29 +00002358 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2359 fda->parent,
2360 off_start_offset,
2361 NULL,
2362 num_valid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002363 if (!parent) {
2364 pr_err("transaction release %d bad parent offset\n",
2365 debug_id);
2366 continue;
2367 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002368 fd_buf_size = sizeof(u32) * fda->num_fds;
2369 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2370 pr_err("transaction release %d invalid number of fds (%lld)\n",
2371 debug_id, (u64)fda->num_fds);
2372 continue;
2373 }
2374 if (fd_buf_size > parent->length ||
2375 fda->parent_offset > parent->length - fd_buf_size) {
2376 /* No space for all file descriptors here. */
2377 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2378 debug_id, (u64)fda->num_fds);
2379 continue;
2380 }
David Brazdil0f672f62019-12-10 10:32:29 +00002381 /*
2382 * the source data for binder_buffer_object is visible
2383 * to user-space and the @buffer element is the user
2384 * pointer to the buffer_object containing the fd_array.
2385 * Convert the address to an offset relative to
2386 * the base of the transaction buffer.
2387 */
2388 fda_offset =
2389 (parent->buffer - (uintptr_t)buffer->user_data) +
2390 fda->parent_offset;
2391 for (fd_index = 0; fd_index < fda->num_fds;
2392 fd_index++) {
2393 u32 fd;
2394 int err;
2395 binder_size_t offset = fda_offset +
2396 fd_index * sizeof(fd);
2397
2398 err = binder_alloc_copy_from_buffer(
2399 &proc->alloc, &fd, buffer,
2400 offset, sizeof(fd));
2401 WARN_ON(err);
Olivier Deprez157378f2022-04-04 15:47:50 +02002402 if (!err) {
David Brazdil0f672f62019-12-10 10:32:29 +00002403 binder_deferred_fd_close(fd);
Olivier Deprez157378f2022-04-04 15:47:50 +02002404 /*
2405 * Need to make sure the thread goes
2406 * back to userspace to complete the
2407 * deferred close
2408 */
2409 if (thread)
2410 thread->looper_need_return = true;
2411 }
David Brazdil0f672f62019-12-10 10:32:29 +00002412 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002413 } break;
2414 default:
2415 pr_err("transaction release %d bad object type %x\n",
2416 debug_id, hdr->type);
2417 break;
2418 }
2419 }
2420}
2421
2422static int binder_translate_binder(struct flat_binder_object *fp,
2423 struct binder_transaction *t,
2424 struct binder_thread *thread)
2425{
2426 struct binder_node *node;
2427 struct binder_proc *proc = thread->proc;
2428 struct binder_proc *target_proc = t->to_proc;
2429 struct binder_ref_data rdata;
2430 int ret = 0;
2431
2432 node = binder_get_node(proc, fp->binder);
2433 if (!node) {
2434 node = binder_new_node(proc, fp);
2435 if (!node)
2436 return -ENOMEM;
2437 }
2438 if (fp->cookie != node->cookie) {
2439 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2440 proc->pid, thread->pid, (u64)fp->binder,
2441 node->debug_id, (u64)fp->cookie,
2442 (u64)node->cookie);
2443 ret = -EINVAL;
2444 goto done;
2445 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002446 if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002447 ret = -EPERM;
2448 goto done;
2449 }
2450
2451 ret = binder_inc_ref_for_node(target_proc, node,
2452 fp->hdr.type == BINDER_TYPE_BINDER,
2453 &thread->todo, &rdata);
2454 if (ret)
2455 goto done;
2456
2457 if (fp->hdr.type == BINDER_TYPE_BINDER)
2458 fp->hdr.type = BINDER_TYPE_HANDLE;
2459 else
2460 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2461 fp->binder = 0;
2462 fp->handle = rdata.desc;
2463 fp->cookie = 0;
2464
2465 trace_binder_transaction_node_to_ref(t, node, &rdata);
2466 binder_debug(BINDER_DEBUG_TRANSACTION,
2467 " node %d u%016llx -> ref %d desc %d\n",
2468 node->debug_id, (u64)node->ptr,
2469 rdata.debug_id, rdata.desc);
2470done:
2471 binder_put_node(node);
2472 return ret;
2473}
2474
2475static int binder_translate_handle(struct flat_binder_object *fp,
2476 struct binder_transaction *t,
2477 struct binder_thread *thread)
2478{
2479 struct binder_proc *proc = thread->proc;
2480 struct binder_proc *target_proc = t->to_proc;
2481 struct binder_node *node;
2482 struct binder_ref_data src_rdata;
2483 int ret = 0;
2484
2485 node = binder_get_node_from_ref(proc, fp->handle,
2486 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2487 if (!node) {
2488 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2489 proc->pid, thread->pid, fp->handle);
2490 return -EINVAL;
2491 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002492 if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002493 ret = -EPERM;
2494 goto done;
2495 }
2496
2497 binder_node_lock(node);
2498 if (node->proc == target_proc) {
2499 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2500 fp->hdr.type = BINDER_TYPE_BINDER;
2501 else
2502 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2503 fp->binder = node->ptr;
2504 fp->cookie = node->cookie;
2505 if (node->proc)
2506 binder_inner_proc_lock(node->proc);
David Brazdil0f672f62019-12-10 10:32:29 +00002507 else
2508 __acquire(&node->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002509 binder_inc_node_nilocked(node,
2510 fp->hdr.type == BINDER_TYPE_BINDER,
2511 0, NULL);
2512 if (node->proc)
2513 binder_inner_proc_unlock(node->proc);
David Brazdil0f672f62019-12-10 10:32:29 +00002514 else
2515 __release(&node->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002516 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2517 binder_debug(BINDER_DEBUG_TRANSACTION,
2518 " ref %d desc %d -> node %d u%016llx\n",
2519 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2520 (u64)node->ptr);
2521 binder_node_unlock(node);
2522 } else {
2523 struct binder_ref_data dest_rdata;
2524
2525 binder_node_unlock(node);
2526 ret = binder_inc_ref_for_node(target_proc, node,
2527 fp->hdr.type == BINDER_TYPE_HANDLE,
2528 NULL, &dest_rdata);
2529 if (ret)
2530 goto done;
2531
2532 fp->binder = 0;
2533 fp->handle = dest_rdata.desc;
2534 fp->cookie = 0;
2535 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2536 &dest_rdata);
2537 binder_debug(BINDER_DEBUG_TRANSACTION,
2538 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2539 src_rdata.debug_id, src_rdata.desc,
2540 dest_rdata.debug_id, dest_rdata.desc,
2541 node->debug_id);
2542 }
2543done:
2544 binder_put_node(node);
2545 return ret;
2546}
2547
David Brazdil0f672f62019-12-10 10:32:29 +00002548static int binder_translate_fd(u32 fd, binder_size_t fd_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002549 struct binder_transaction *t,
2550 struct binder_thread *thread,
2551 struct binder_transaction *in_reply_to)
2552{
2553 struct binder_proc *proc = thread->proc;
2554 struct binder_proc *target_proc = t->to_proc;
David Brazdil0f672f62019-12-10 10:32:29 +00002555 struct binder_txn_fd_fixup *fixup;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002556 struct file *file;
David Brazdil0f672f62019-12-10 10:32:29 +00002557 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002558 bool target_allows_fd;
2559
2560 if (in_reply_to)
2561 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2562 else
2563 target_allows_fd = t->buffer->target_node->accept_fds;
2564 if (!target_allows_fd) {
2565 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2566 proc->pid, thread->pid,
2567 in_reply_to ? "reply" : "transaction",
2568 fd);
2569 ret = -EPERM;
2570 goto err_fd_not_accepted;
2571 }
2572
2573 file = fget(fd);
2574 if (!file) {
2575 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2576 proc->pid, thread->pid, fd);
2577 ret = -EBADF;
2578 goto err_fget;
2579 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002580 ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002581 if (ret < 0) {
2582 ret = -EPERM;
2583 goto err_security;
2584 }
2585
David Brazdil0f672f62019-12-10 10:32:29 +00002586 /*
2587 * Add fixup record for this transaction. The allocation
2588 * of the fd in the target needs to be done from a
2589 * target thread.
2590 */
2591 fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
2592 if (!fixup) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002593 ret = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002594 goto err_alloc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002595 }
David Brazdil0f672f62019-12-10 10:32:29 +00002596 fixup->file = file;
2597 fixup->offset = fd_offset;
2598 trace_binder_transaction_fd_send(t, fd, fixup->offset);
2599 list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002600
David Brazdil0f672f62019-12-10 10:32:29 +00002601 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002602
David Brazdil0f672f62019-12-10 10:32:29 +00002603err_alloc:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002604err_security:
2605 fput(file);
2606err_fget:
2607err_fd_not_accepted:
2608 return ret;
2609}
2610
2611static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2612 struct binder_buffer_object *parent,
2613 struct binder_transaction *t,
2614 struct binder_thread *thread,
2615 struct binder_transaction *in_reply_to)
2616{
David Brazdil0f672f62019-12-10 10:32:29 +00002617 binder_size_t fdi, fd_buf_size;
2618 binder_size_t fda_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002619 struct binder_proc *proc = thread->proc;
2620 struct binder_proc *target_proc = t->to_proc;
2621
2622 fd_buf_size = sizeof(u32) * fda->num_fds;
2623 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2624 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2625 proc->pid, thread->pid, (u64)fda->num_fds);
2626 return -EINVAL;
2627 }
2628 if (fd_buf_size > parent->length ||
2629 fda->parent_offset > parent->length - fd_buf_size) {
2630 /* No space for all file descriptors here. */
2631 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2632 proc->pid, thread->pid, (u64)fda->num_fds);
2633 return -EINVAL;
2634 }
2635 /*
David Brazdil0f672f62019-12-10 10:32:29 +00002636 * the source data for binder_buffer_object is visible
2637 * to user-space and the @buffer element is the user
2638 * pointer to the buffer_object containing the fd_array.
2639 * Convert the address to an offset relative to
2640 * the base of the transaction buffer.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002641 */
David Brazdil0f672f62019-12-10 10:32:29 +00002642 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2643 fda->parent_offset;
2644 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002645 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2646 proc->pid, thread->pid);
2647 return -EINVAL;
2648 }
2649 for (fdi = 0; fdi < fda->num_fds; fdi++) {
David Brazdil0f672f62019-12-10 10:32:29 +00002650 u32 fd;
2651 int ret;
2652 binder_size_t offset = fda_offset + fdi * sizeof(fd);
2653
2654 ret = binder_alloc_copy_from_buffer(&target_proc->alloc,
2655 &fd, t->buffer,
2656 offset, sizeof(fd));
2657 if (!ret)
2658 ret = binder_translate_fd(fd, offset, t, thread,
2659 in_reply_to);
Olivier Deprez157378f2022-04-04 15:47:50 +02002660 if (ret)
2661 return ret > 0 ? -EINVAL : ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002662 }
2663 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002664}
2665
2666static int binder_fixup_parent(struct binder_transaction *t,
2667 struct binder_thread *thread,
2668 struct binder_buffer_object *bp,
David Brazdil0f672f62019-12-10 10:32:29 +00002669 binder_size_t off_start_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002670 binder_size_t num_valid,
David Brazdil0f672f62019-12-10 10:32:29 +00002671 binder_size_t last_fixup_obj_off,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002672 binder_size_t last_fixup_min_off)
2673{
2674 struct binder_buffer_object *parent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002675 struct binder_buffer *b = t->buffer;
2676 struct binder_proc *proc = thread->proc;
2677 struct binder_proc *target_proc = t->to_proc;
David Brazdil0f672f62019-12-10 10:32:29 +00002678 struct binder_object object;
2679 binder_size_t buffer_offset;
2680 binder_size_t parent_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002681
2682 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2683 return 0;
2684
David Brazdil0f672f62019-12-10 10:32:29 +00002685 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2686 off_start_offset, &parent_offset,
2687 num_valid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002688 if (!parent) {
2689 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2690 proc->pid, thread->pid);
2691 return -EINVAL;
2692 }
2693
David Brazdil0f672f62019-12-10 10:32:29 +00002694 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2695 parent_offset, bp->parent_offset,
2696 last_fixup_obj_off,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002697 last_fixup_min_off)) {
2698 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2699 proc->pid, thread->pid);
2700 return -EINVAL;
2701 }
2702
2703 if (parent->length < sizeof(binder_uintptr_t) ||
2704 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2705 /* No space for a pointer here! */
2706 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2707 proc->pid, thread->pid);
2708 return -EINVAL;
2709 }
David Brazdil0f672f62019-12-10 10:32:29 +00002710 buffer_offset = bp->parent_offset +
2711 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
2712 if (binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2713 &bp->buffer, sizeof(bp->buffer))) {
2714 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2715 proc->pid, thread->pid);
2716 return -EINVAL;
2717 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002718
2719 return 0;
2720}
2721
2722/**
2723 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2724 * @t: transaction to send
2725 * @proc: process to send the transaction to
2726 * @thread: thread in @proc to send the transaction to (may be NULL)
2727 *
2728 * This function queues a transaction to the specified process. It will try
2729 * to find a thread in the target process to handle the transaction and
2730 * wake it up. If no thread is found, the work is queued to the proc
2731 * waitqueue.
2732 *
2733 * If the @thread parameter is not NULL, the transaction is always queued
2734 * to the waitlist of that specific thread.
2735 *
2736 * Return: true if the transactions was successfully queued
2737 * false if the target process or thread is dead
2738 */
2739static bool binder_proc_transaction(struct binder_transaction *t,
2740 struct binder_proc *proc,
2741 struct binder_thread *thread)
2742{
2743 struct binder_node *node = t->buffer->target_node;
2744 bool oneway = !!(t->flags & TF_ONE_WAY);
2745 bool pending_async = false;
2746
2747 BUG_ON(!node);
2748 binder_node_lock(node);
2749 if (oneway) {
2750 BUG_ON(thread);
Olivier Deprez157378f2022-04-04 15:47:50 +02002751 if (node->has_async_transaction)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002752 pending_async = true;
Olivier Deprez157378f2022-04-04 15:47:50 +02002753 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002754 node->has_async_transaction = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002755 }
2756
2757 binder_inner_proc_lock(proc);
2758
2759 if (proc->is_dead || (thread && thread->is_dead)) {
2760 binder_inner_proc_unlock(proc);
2761 binder_node_unlock(node);
2762 return false;
2763 }
2764
2765 if (!thread && !pending_async)
2766 thread = binder_select_thread_ilocked(proc);
2767
2768 if (thread)
2769 binder_enqueue_thread_work_ilocked(thread, &t->work);
2770 else if (!pending_async)
2771 binder_enqueue_work_ilocked(&t->work, &proc->todo);
2772 else
2773 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
2774
2775 if (!pending_async)
2776 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2777
2778 binder_inner_proc_unlock(proc);
2779 binder_node_unlock(node);
2780
2781 return true;
2782}
2783
2784/**
2785 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2786 * @node: struct binder_node for which to get refs
2787 * @proc: returns @node->proc if valid
2788 * @error: if no @proc then returns BR_DEAD_REPLY
2789 *
2790 * User-space normally keeps the node alive when creating a transaction
2791 * since it has a reference to the target. The local strong ref keeps it
2792 * alive if the sending process dies before the target process processes
2793 * the transaction. If the source process is malicious or has a reference
2794 * counting bug, relying on the local strong ref can fail.
2795 *
2796 * Since user-space can cause the local strong ref to go away, we also take
2797 * a tmpref on the node to ensure it survives while we are constructing
2798 * the transaction. We also need a tmpref on the proc while we are
2799 * constructing the transaction, so we take that here as well.
2800 *
2801 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2802 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2803 * target proc has died, @error is set to BR_DEAD_REPLY
2804 */
2805static struct binder_node *binder_get_node_refs_for_txn(
2806 struct binder_node *node,
2807 struct binder_proc **procp,
2808 uint32_t *error)
2809{
2810 struct binder_node *target_node = NULL;
2811
2812 binder_node_inner_lock(node);
2813 if (node->proc) {
2814 target_node = node;
2815 binder_inc_node_nilocked(node, 1, 0, NULL);
2816 binder_inc_node_tmpref_ilocked(node);
2817 node->proc->tmp_ref++;
2818 *procp = node->proc;
2819 } else
2820 *error = BR_DEAD_REPLY;
2821 binder_node_inner_unlock(node);
2822
2823 return target_node;
2824}
2825
2826static void binder_transaction(struct binder_proc *proc,
2827 struct binder_thread *thread,
2828 struct binder_transaction_data *tr, int reply,
2829 binder_size_t extra_buffers_size)
2830{
2831 int ret;
2832 struct binder_transaction *t;
David Brazdil0f672f62019-12-10 10:32:29 +00002833 struct binder_work *w;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002834 struct binder_work *tcomplete;
David Brazdil0f672f62019-12-10 10:32:29 +00002835 binder_size_t buffer_offset = 0;
2836 binder_size_t off_start_offset, off_end_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002837 binder_size_t off_min;
David Brazdil0f672f62019-12-10 10:32:29 +00002838 binder_size_t sg_buf_offset, sg_buf_end_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002839 struct binder_proc *target_proc = NULL;
2840 struct binder_thread *target_thread = NULL;
2841 struct binder_node *target_node = NULL;
2842 struct binder_transaction *in_reply_to = NULL;
2843 struct binder_transaction_log_entry *e;
2844 uint32_t return_error = 0;
2845 uint32_t return_error_param = 0;
2846 uint32_t return_error_line = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002847 binder_size_t last_fixup_obj_off = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002848 binder_size_t last_fixup_min_off = 0;
2849 struct binder_context *context = proc->context;
2850 int t_debug_id = atomic_inc_return(&binder_last_id);
David Brazdil0f672f62019-12-10 10:32:29 +00002851 char *secctx = NULL;
2852 u32 secctx_sz = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002853
2854 e = binder_transaction_log_add(&binder_transaction_log);
2855 e->debug_id = t_debug_id;
2856 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2857 e->from_proc = proc->pid;
2858 e->from_thread = thread->pid;
2859 e->target_handle = tr->target.handle;
2860 e->data_size = tr->data_size;
2861 e->offsets_size = tr->offsets_size;
David Brazdil0f672f62019-12-10 10:32:29 +00002862 strscpy(e->context_name, proc->context->name, BINDERFS_MAX_NAME);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002863
2864 if (reply) {
2865 binder_inner_proc_lock(proc);
2866 in_reply_to = thread->transaction_stack;
2867 if (in_reply_to == NULL) {
2868 binder_inner_proc_unlock(proc);
2869 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2870 proc->pid, thread->pid);
2871 return_error = BR_FAILED_REPLY;
2872 return_error_param = -EPROTO;
2873 return_error_line = __LINE__;
2874 goto err_empty_call_stack;
2875 }
2876 if (in_reply_to->to_thread != thread) {
2877 spin_lock(&in_reply_to->lock);
2878 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2879 proc->pid, thread->pid, in_reply_to->debug_id,
2880 in_reply_to->to_proc ?
2881 in_reply_to->to_proc->pid : 0,
2882 in_reply_to->to_thread ?
2883 in_reply_to->to_thread->pid : 0);
2884 spin_unlock(&in_reply_to->lock);
2885 binder_inner_proc_unlock(proc);
2886 return_error = BR_FAILED_REPLY;
2887 return_error_param = -EPROTO;
2888 return_error_line = __LINE__;
2889 in_reply_to = NULL;
2890 goto err_bad_call_stack;
2891 }
2892 thread->transaction_stack = in_reply_to->to_parent;
2893 binder_inner_proc_unlock(proc);
2894 binder_set_nice(in_reply_to->saved_priority);
2895 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2896 if (target_thread == NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +00002897 /* annotation for sparse */
2898 __release(&target_thread->proc->inner_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002899 return_error = BR_DEAD_REPLY;
2900 return_error_line = __LINE__;
2901 goto err_dead_binder;
2902 }
2903 if (target_thread->transaction_stack != in_reply_to) {
2904 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2905 proc->pid, thread->pid,
2906 target_thread->transaction_stack ?
2907 target_thread->transaction_stack->debug_id : 0,
2908 in_reply_to->debug_id);
2909 binder_inner_proc_unlock(target_thread->proc);
2910 return_error = BR_FAILED_REPLY;
2911 return_error_param = -EPROTO;
2912 return_error_line = __LINE__;
2913 in_reply_to = NULL;
2914 target_thread = NULL;
2915 goto err_dead_binder;
2916 }
2917 target_proc = target_thread->proc;
2918 target_proc->tmp_ref++;
2919 binder_inner_proc_unlock(target_thread->proc);
2920 } else {
2921 if (tr->target.handle) {
2922 struct binder_ref *ref;
2923
2924 /*
2925 * There must already be a strong ref
2926 * on this node. If so, do a strong
2927 * increment on the node to ensure it
2928 * stays alive until the transaction is
2929 * done.
2930 */
2931 binder_proc_lock(proc);
2932 ref = binder_get_ref_olocked(proc, tr->target.handle,
2933 true);
2934 if (ref) {
2935 target_node = binder_get_node_refs_for_txn(
2936 ref->node, &target_proc,
2937 &return_error);
2938 } else {
2939 binder_user_error("%d:%d got transaction to invalid handle\n",
2940 proc->pid, thread->pid);
2941 return_error = BR_FAILED_REPLY;
2942 }
2943 binder_proc_unlock(proc);
2944 } else {
2945 mutex_lock(&context->context_mgr_node_lock);
2946 target_node = context->binder_context_mgr_node;
2947 if (target_node)
2948 target_node = binder_get_node_refs_for_txn(
2949 target_node, &target_proc,
2950 &return_error);
2951 else
2952 return_error = BR_DEAD_REPLY;
2953 mutex_unlock(&context->context_mgr_node_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00002954 if (target_node && target_proc->pid == proc->pid) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002955 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2956 proc->pid, thread->pid);
2957 return_error = BR_FAILED_REPLY;
2958 return_error_param = -EINVAL;
2959 return_error_line = __LINE__;
2960 goto err_invalid_target_handle;
2961 }
2962 }
2963 if (!target_node) {
2964 /*
2965 * return_error is set above
2966 */
2967 return_error_param = -EINVAL;
2968 return_error_line = __LINE__;
2969 goto err_dead_binder;
2970 }
2971 e->to_node = target_node->debug_id;
Olivier Deprez0e641232021-09-23 10:07:05 +02002972 if (WARN_ON(proc == target_proc)) {
2973 return_error = BR_FAILED_REPLY;
2974 return_error_param = -EINVAL;
2975 return_error_line = __LINE__;
2976 goto err_invalid_target_handle;
2977 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002978 if (security_binder_transaction(proc->cred,
2979 target_proc->cred) < 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002980 return_error = BR_FAILED_REPLY;
2981 return_error_param = -EPERM;
2982 return_error_line = __LINE__;
2983 goto err_invalid_target_handle;
2984 }
2985 binder_inner_proc_lock(proc);
David Brazdil0f672f62019-12-10 10:32:29 +00002986
2987 w = list_first_entry_or_null(&thread->todo,
2988 struct binder_work, entry);
2989 if (!(tr->flags & TF_ONE_WAY) && w &&
2990 w->type == BINDER_WORK_TRANSACTION) {
2991 /*
2992 * Do not allow new outgoing transaction from a
2993 * thread that has a transaction at the head of
2994 * its todo list. Only need to check the head
2995 * because binder_select_thread_ilocked picks a
2996 * thread from proc->waiting_threads to enqueue
2997 * the transaction, and nothing is queued to the
2998 * todo list while the thread is on waiting_threads.
2999 */
3000 binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
3001 proc->pid, thread->pid);
3002 binder_inner_proc_unlock(proc);
3003 return_error = BR_FAILED_REPLY;
3004 return_error_param = -EPROTO;
3005 return_error_line = __LINE__;
3006 goto err_bad_todo_list;
3007 }
3008
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003009 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3010 struct binder_transaction *tmp;
3011
3012 tmp = thread->transaction_stack;
3013 if (tmp->to_thread != thread) {
3014 spin_lock(&tmp->lock);
3015 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3016 proc->pid, thread->pid, tmp->debug_id,
3017 tmp->to_proc ? tmp->to_proc->pid : 0,
3018 tmp->to_thread ?
3019 tmp->to_thread->pid : 0);
3020 spin_unlock(&tmp->lock);
3021 binder_inner_proc_unlock(proc);
3022 return_error = BR_FAILED_REPLY;
3023 return_error_param = -EPROTO;
3024 return_error_line = __LINE__;
3025 goto err_bad_call_stack;
3026 }
3027 while (tmp) {
3028 struct binder_thread *from;
3029
3030 spin_lock(&tmp->lock);
3031 from = tmp->from;
3032 if (from && from->proc == target_proc) {
3033 atomic_inc(&from->tmp_ref);
3034 target_thread = from;
3035 spin_unlock(&tmp->lock);
3036 break;
3037 }
3038 spin_unlock(&tmp->lock);
3039 tmp = tmp->from_parent;
3040 }
3041 }
3042 binder_inner_proc_unlock(proc);
3043 }
3044 if (target_thread)
3045 e->to_thread = target_thread->pid;
3046 e->to_proc = target_proc->pid;
3047
3048 /* TODO: reuse incoming transaction for reply */
3049 t = kzalloc(sizeof(*t), GFP_KERNEL);
3050 if (t == NULL) {
3051 return_error = BR_FAILED_REPLY;
3052 return_error_param = -ENOMEM;
3053 return_error_line = __LINE__;
3054 goto err_alloc_t_failed;
3055 }
David Brazdil0f672f62019-12-10 10:32:29 +00003056 INIT_LIST_HEAD(&t->fd_fixups);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003057 binder_stats_created(BINDER_STAT_TRANSACTION);
3058 spin_lock_init(&t->lock);
3059
3060 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3061 if (tcomplete == NULL) {
3062 return_error = BR_FAILED_REPLY;
3063 return_error_param = -ENOMEM;
3064 return_error_line = __LINE__;
3065 goto err_alloc_tcomplete_failed;
3066 }
3067 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3068
3069 t->debug_id = t_debug_id;
3070
3071 if (reply)
3072 binder_debug(BINDER_DEBUG_TRANSACTION,
3073 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3074 proc->pid, thread->pid, t->debug_id,
3075 target_proc->pid, target_thread->pid,
3076 (u64)tr->data.ptr.buffer,
3077 (u64)tr->data.ptr.offsets,
3078 (u64)tr->data_size, (u64)tr->offsets_size,
3079 (u64)extra_buffers_size);
3080 else
3081 binder_debug(BINDER_DEBUG_TRANSACTION,
3082 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3083 proc->pid, thread->pid, t->debug_id,
3084 target_proc->pid, target_node->debug_id,
3085 (u64)tr->data.ptr.buffer,
3086 (u64)tr->data.ptr.offsets,
3087 (u64)tr->data_size, (u64)tr->offsets_size,
3088 (u64)extra_buffers_size);
3089
3090 if (!reply && !(tr->flags & TF_ONE_WAY))
3091 t->from = thread;
3092 else
3093 t->from = NULL;
3094 t->sender_euid = task_euid(proc->tsk);
3095 t->to_proc = target_proc;
3096 t->to_thread = target_thread;
3097 t->code = tr->code;
3098 t->flags = tr->flags;
3099 t->priority = task_nice(current);
3100
David Brazdil0f672f62019-12-10 10:32:29 +00003101 if (target_node && target_node->txn_security_ctx) {
3102 u32 secid;
3103 size_t added_size;
3104
Olivier Deprez157378f2022-04-04 15:47:50 +02003105 security_cred_getsecid(proc->cred, &secid);
David Brazdil0f672f62019-12-10 10:32:29 +00003106 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3107 if (ret) {
3108 return_error = BR_FAILED_REPLY;
3109 return_error_param = ret;
3110 return_error_line = __LINE__;
3111 goto err_get_secctx_failed;
3112 }
3113 added_size = ALIGN(secctx_sz, sizeof(u64));
3114 extra_buffers_size += added_size;
3115 if (extra_buffers_size < added_size) {
3116 /* integer overflow of extra_buffers_size */
3117 return_error = BR_FAILED_REPLY;
3118 return_error_param = EINVAL;
3119 return_error_line = __LINE__;
3120 goto err_bad_extra_size;
3121 }
3122 }
3123
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003124 trace_binder_transaction(reply, t, target_node);
3125
3126 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
3127 tr->offsets_size, extra_buffers_size,
Olivier Deprez157378f2022-04-04 15:47:50 +02003128 !reply && (t->flags & TF_ONE_WAY), current->tgid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003129 if (IS_ERR(t->buffer)) {
3130 /*
3131 * -ESRCH indicates VMA cleared. The target is dying.
3132 */
3133 return_error_param = PTR_ERR(t->buffer);
3134 return_error = return_error_param == -ESRCH ?
3135 BR_DEAD_REPLY : BR_FAILED_REPLY;
3136 return_error_line = __LINE__;
3137 t->buffer = NULL;
3138 goto err_binder_alloc_buf_failed;
3139 }
David Brazdil0f672f62019-12-10 10:32:29 +00003140 if (secctx) {
3141 int err;
3142 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3143 ALIGN(tr->offsets_size, sizeof(void *)) +
3144 ALIGN(extra_buffers_size, sizeof(void *)) -
3145 ALIGN(secctx_sz, sizeof(u64));
3146
3147 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
3148 err = binder_alloc_copy_to_buffer(&target_proc->alloc,
3149 t->buffer, buf_offset,
3150 secctx, secctx_sz);
3151 if (err) {
3152 t->security_ctx = 0;
3153 WARN_ON(1);
3154 }
3155 security_release_secctx(secctx, secctx_sz);
3156 secctx = NULL;
3157 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003158 t->buffer->debug_id = t->debug_id;
3159 t->buffer->transaction = t;
3160 t->buffer->target_node = target_node;
Olivier Deprez0e641232021-09-23 10:07:05 +02003161 t->buffer->clear_on_free = !!(t->flags & TF_CLEAR_BUF);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003162 trace_binder_transaction_alloc_buf(t->buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003163
David Brazdil0f672f62019-12-10 10:32:29 +00003164 if (binder_alloc_copy_user_to_buffer(
3165 &target_proc->alloc,
3166 t->buffer, 0,
3167 (const void __user *)
3168 (uintptr_t)tr->data.ptr.buffer,
3169 tr->data_size)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003170 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3171 proc->pid, thread->pid);
3172 return_error = BR_FAILED_REPLY;
3173 return_error_param = -EFAULT;
3174 return_error_line = __LINE__;
3175 goto err_copy_data_failed;
3176 }
David Brazdil0f672f62019-12-10 10:32:29 +00003177 if (binder_alloc_copy_user_to_buffer(
3178 &target_proc->alloc,
3179 t->buffer,
3180 ALIGN(tr->data_size, sizeof(void *)),
3181 (const void __user *)
3182 (uintptr_t)tr->data.ptr.offsets,
3183 tr->offsets_size)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003184 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3185 proc->pid, thread->pid);
3186 return_error = BR_FAILED_REPLY;
3187 return_error_param = -EFAULT;
3188 return_error_line = __LINE__;
3189 goto err_copy_data_failed;
3190 }
3191 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3192 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3193 proc->pid, thread->pid, (u64)tr->offsets_size);
3194 return_error = BR_FAILED_REPLY;
3195 return_error_param = -EINVAL;
3196 return_error_line = __LINE__;
3197 goto err_bad_offset;
3198 }
3199 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3200 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3201 proc->pid, thread->pid,
3202 (u64)extra_buffers_size);
3203 return_error = BR_FAILED_REPLY;
3204 return_error_param = -EINVAL;
3205 return_error_line = __LINE__;
3206 goto err_bad_offset;
3207 }
David Brazdil0f672f62019-12-10 10:32:29 +00003208 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3209 buffer_offset = off_start_offset;
3210 off_end_offset = off_start_offset + tr->offsets_size;
3211 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
3212 sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3213 ALIGN(secctx_sz, sizeof(u64));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003214 off_min = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00003215 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3216 buffer_offset += sizeof(binder_size_t)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003217 struct binder_object_header *hdr;
David Brazdil0f672f62019-12-10 10:32:29 +00003218 size_t object_size;
3219 struct binder_object object;
3220 binder_size_t object_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003221
David Brazdil0f672f62019-12-10 10:32:29 +00003222 if (binder_alloc_copy_from_buffer(&target_proc->alloc,
3223 &object_offset,
3224 t->buffer,
3225 buffer_offset,
3226 sizeof(object_offset))) {
3227 return_error = BR_FAILED_REPLY;
3228 return_error_param = -EINVAL;
3229 return_error_line = __LINE__;
3230 goto err_bad_offset;
3231 }
3232 object_size = binder_get_object(target_proc, t->buffer,
3233 object_offset, &object);
3234 if (object_size == 0 || object_offset < off_min) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003235 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
David Brazdil0f672f62019-12-10 10:32:29 +00003236 proc->pid, thread->pid,
3237 (u64)object_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003238 (u64)off_min,
3239 (u64)t->buffer->data_size);
3240 return_error = BR_FAILED_REPLY;
3241 return_error_param = -EINVAL;
3242 return_error_line = __LINE__;
3243 goto err_bad_offset;
3244 }
3245
David Brazdil0f672f62019-12-10 10:32:29 +00003246 hdr = &object.hdr;
3247 off_min = object_offset + object_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003248 switch (hdr->type) {
3249 case BINDER_TYPE_BINDER:
3250 case BINDER_TYPE_WEAK_BINDER: {
3251 struct flat_binder_object *fp;
3252
3253 fp = to_flat_binder_object(hdr);
3254 ret = binder_translate_binder(fp, t, thread);
David Brazdil0f672f62019-12-10 10:32:29 +00003255
3256 if (ret < 0 ||
3257 binder_alloc_copy_to_buffer(&target_proc->alloc,
3258 t->buffer,
3259 object_offset,
3260 fp, sizeof(*fp))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003261 return_error = BR_FAILED_REPLY;
3262 return_error_param = ret;
3263 return_error_line = __LINE__;
3264 goto err_translate_failed;
3265 }
3266 } break;
3267 case BINDER_TYPE_HANDLE:
3268 case BINDER_TYPE_WEAK_HANDLE: {
3269 struct flat_binder_object *fp;
3270
3271 fp = to_flat_binder_object(hdr);
3272 ret = binder_translate_handle(fp, t, thread);
David Brazdil0f672f62019-12-10 10:32:29 +00003273 if (ret < 0 ||
3274 binder_alloc_copy_to_buffer(&target_proc->alloc,
3275 t->buffer,
3276 object_offset,
3277 fp, sizeof(*fp))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003278 return_error = BR_FAILED_REPLY;
3279 return_error_param = ret;
3280 return_error_line = __LINE__;
3281 goto err_translate_failed;
3282 }
3283 } break;
3284
3285 case BINDER_TYPE_FD: {
3286 struct binder_fd_object *fp = to_binder_fd_object(hdr);
David Brazdil0f672f62019-12-10 10:32:29 +00003287 binder_size_t fd_offset = object_offset +
3288 (uintptr_t)&fp->fd - (uintptr_t)fp;
3289 int ret = binder_translate_fd(fp->fd, fd_offset, t,
3290 thread, in_reply_to);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003291
David Brazdil0f672f62019-12-10 10:32:29 +00003292 fp->pad_binder = 0;
3293 if (ret < 0 ||
3294 binder_alloc_copy_to_buffer(&target_proc->alloc,
3295 t->buffer,
3296 object_offset,
3297 fp, sizeof(*fp))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003298 return_error = BR_FAILED_REPLY;
David Brazdil0f672f62019-12-10 10:32:29 +00003299 return_error_param = ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003300 return_error_line = __LINE__;
3301 goto err_translate_failed;
3302 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003303 } break;
3304 case BINDER_TYPE_FDA: {
David Brazdil0f672f62019-12-10 10:32:29 +00003305 struct binder_object ptr_object;
3306 binder_size_t parent_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003307 struct binder_fd_array_object *fda =
3308 to_binder_fd_array_object(hdr);
Olivier Deprez0e641232021-09-23 10:07:05 +02003309 size_t num_valid = (buffer_offset - off_start_offset) /
David Brazdil0f672f62019-12-10 10:32:29 +00003310 sizeof(binder_size_t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003311 struct binder_buffer_object *parent =
David Brazdil0f672f62019-12-10 10:32:29 +00003312 binder_validate_ptr(target_proc, t->buffer,
3313 &ptr_object, fda->parent,
3314 off_start_offset,
3315 &parent_offset,
3316 num_valid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003317 if (!parent) {
3318 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3319 proc->pid, thread->pid);
3320 return_error = BR_FAILED_REPLY;
3321 return_error_param = -EINVAL;
3322 return_error_line = __LINE__;
3323 goto err_bad_parent;
3324 }
David Brazdil0f672f62019-12-10 10:32:29 +00003325 if (!binder_validate_fixup(target_proc, t->buffer,
3326 off_start_offset,
3327 parent_offset,
3328 fda->parent_offset,
3329 last_fixup_obj_off,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003330 last_fixup_min_off)) {
3331 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3332 proc->pid, thread->pid);
3333 return_error = BR_FAILED_REPLY;
3334 return_error_param = -EINVAL;
3335 return_error_line = __LINE__;
3336 goto err_bad_parent;
3337 }
3338 ret = binder_translate_fd_array(fda, parent, t, thread,
3339 in_reply_to);
3340 if (ret < 0) {
3341 return_error = BR_FAILED_REPLY;
3342 return_error_param = ret;
3343 return_error_line = __LINE__;
3344 goto err_translate_failed;
3345 }
David Brazdil0f672f62019-12-10 10:32:29 +00003346 last_fixup_obj_off = parent_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003347 last_fixup_min_off =
3348 fda->parent_offset + sizeof(u32) * fda->num_fds;
3349 } break;
3350 case BINDER_TYPE_PTR: {
3351 struct binder_buffer_object *bp =
3352 to_binder_buffer_object(hdr);
David Brazdil0f672f62019-12-10 10:32:29 +00003353 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3354 size_t num_valid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003355
3356 if (bp->length > buf_left) {
3357 binder_user_error("%d:%d got transaction with too large buffer\n",
3358 proc->pid, thread->pid);
3359 return_error = BR_FAILED_REPLY;
3360 return_error_param = -EINVAL;
3361 return_error_line = __LINE__;
3362 goto err_bad_offset;
3363 }
David Brazdil0f672f62019-12-10 10:32:29 +00003364 if (binder_alloc_copy_user_to_buffer(
3365 &target_proc->alloc,
3366 t->buffer,
3367 sg_buf_offset,
3368 (const void __user *)
3369 (uintptr_t)bp->buffer,
3370 bp->length)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003371 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3372 proc->pid, thread->pid);
3373 return_error_param = -EFAULT;
3374 return_error = BR_FAILED_REPLY;
3375 return_error_line = __LINE__;
3376 goto err_copy_data_failed;
3377 }
3378 /* Fixup buffer pointer to target proc address space */
David Brazdil0f672f62019-12-10 10:32:29 +00003379 bp->buffer = (uintptr_t)
3380 t->buffer->user_data + sg_buf_offset;
3381 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003382
Olivier Deprez0e641232021-09-23 10:07:05 +02003383 num_valid = (buffer_offset - off_start_offset) /
David Brazdil0f672f62019-12-10 10:32:29 +00003384 sizeof(binder_size_t);
3385 ret = binder_fixup_parent(t, thread, bp,
3386 off_start_offset,
3387 num_valid,
3388 last_fixup_obj_off,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003389 last_fixup_min_off);
David Brazdil0f672f62019-12-10 10:32:29 +00003390 if (ret < 0 ||
3391 binder_alloc_copy_to_buffer(&target_proc->alloc,
3392 t->buffer,
3393 object_offset,
3394 bp, sizeof(*bp))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003395 return_error = BR_FAILED_REPLY;
3396 return_error_param = ret;
3397 return_error_line = __LINE__;
3398 goto err_translate_failed;
3399 }
David Brazdil0f672f62019-12-10 10:32:29 +00003400 last_fixup_obj_off = object_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003401 last_fixup_min_off = 0;
3402 } break;
3403 default:
3404 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3405 proc->pid, thread->pid, hdr->type);
3406 return_error = BR_FAILED_REPLY;
3407 return_error_param = -EINVAL;
3408 return_error_line = __LINE__;
3409 goto err_bad_object_type;
3410 }
3411 }
3412 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3413 t->work.type = BINDER_WORK_TRANSACTION;
3414
3415 if (reply) {
3416 binder_enqueue_thread_work(thread, tcomplete);
3417 binder_inner_proc_lock(target_proc);
3418 if (target_thread->is_dead) {
3419 binder_inner_proc_unlock(target_proc);
3420 goto err_dead_proc_or_thread;
3421 }
3422 BUG_ON(t->buffer->async_transaction != 0);
3423 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3424 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3425 binder_inner_proc_unlock(target_proc);
3426 wake_up_interruptible_sync(&target_thread->wait);
3427 binder_free_transaction(in_reply_to);
3428 } else if (!(t->flags & TF_ONE_WAY)) {
3429 BUG_ON(t->buffer->async_transaction != 0);
3430 binder_inner_proc_lock(proc);
3431 /*
3432 * Defer the TRANSACTION_COMPLETE, so we don't return to
3433 * userspace immediately; this allows the target process to
3434 * immediately start processing this transaction, reducing
3435 * latency. We will then return the TRANSACTION_COMPLETE when
3436 * the target replies (or there is an error).
3437 */
3438 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3439 t->need_reply = 1;
3440 t->from_parent = thread->transaction_stack;
3441 thread->transaction_stack = t;
3442 binder_inner_proc_unlock(proc);
3443 if (!binder_proc_transaction(t, target_proc, target_thread)) {
3444 binder_inner_proc_lock(proc);
3445 binder_pop_transaction_ilocked(thread, t);
3446 binder_inner_proc_unlock(proc);
3447 goto err_dead_proc_or_thread;
3448 }
3449 } else {
3450 BUG_ON(target_node == NULL);
3451 BUG_ON(t->buffer->async_transaction != 1);
3452 binder_enqueue_thread_work(thread, tcomplete);
3453 if (!binder_proc_transaction(t, target_proc, NULL))
3454 goto err_dead_proc_or_thread;
3455 }
3456 if (target_thread)
3457 binder_thread_dec_tmpref(target_thread);
3458 binder_proc_dec_tmpref(target_proc);
3459 if (target_node)
3460 binder_dec_node_tmpref(target_node);
3461 /*
3462 * write barrier to synchronize with initialization
3463 * of log entry
3464 */
3465 smp_wmb();
3466 WRITE_ONCE(e->debug_id_done, t_debug_id);
3467 return;
3468
3469err_dead_proc_or_thread:
3470 return_error = BR_DEAD_REPLY;
3471 return_error_line = __LINE__;
3472 binder_dequeue_work(proc, tcomplete);
3473err_translate_failed:
3474err_bad_object_type:
3475err_bad_offset:
3476err_bad_parent:
3477err_copy_data_failed:
David Brazdil0f672f62019-12-10 10:32:29 +00003478 binder_free_txn_fixups(t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003479 trace_binder_transaction_failed_buffer_release(t->buffer);
Olivier Deprez157378f2022-04-04 15:47:50 +02003480 binder_transaction_buffer_release(target_proc, NULL, t->buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00003481 buffer_offset, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003482 if (target_node)
3483 binder_dec_node_tmpref(target_node);
3484 target_node = NULL;
3485 t->buffer->transaction = NULL;
3486 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3487err_binder_alloc_buf_failed:
David Brazdil0f672f62019-12-10 10:32:29 +00003488err_bad_extra_size:
3489 if (secctx)
3490 security_release_secctx(secctx, secctx_sz);
3491err_get_secctx_failed:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003492 kfree(tcomplete);
3493 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3494err_alloc_tcomplete_failed:
3495 kfree(t);
3496 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3497err_alloc_t_failed:
David Brazdil0f672f62019-12-10 10:32:29 +00003498err_bad_todo_list:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003499err_bad_call_stack:
3500err_empty_call_stack:
3501err_dead_binder:
3502err_invalid_target_handle:
3503 if (target_thread)
3504 binder_thread_dec_tmpref(target_thread);
3505 if (target_proc)
3506 binder_proc_dec_tmpref(target_proc);
3507 if (target_node) {
3508 binder_dec_node(target_node, 1, 0);
3509 binder_dec_node_tmpref(target_node);
3510 }
3511
3512 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3513 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3514 proc->pid, thread->pid, return_error, return_error_param,
3515 (u64)tr->data_size, (u64)tr->offsets_size,
3516 return_error_line);
3517
3518 {
3519 struct binder_transaction_log_entry *fe;
3520
3521 e->return_error = return_error;
3522 e->return_error_param = return_error_param;
3523 e->return_error_line = return_error_line;
3524 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3525 *fe = *e;
3526 /*
3527 * write barrier to synchronize with initialization
3528 * of log entry
3529 */
3530 smp_wmb();
3531 WRITE_ONCE(e->debug_id_done, t_debug_id);
3532 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3533 }
3534
3535 BUG_ON(thread->return_error.cmd != BR_OK);
3536 if (in_reply_to) {
3537 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3538 binder_enqueue_thread_work(thread, &thread->return_error.work);
3539 binder_send_failed_reply(in_reply_to, return_error);
3540 } else {
3541 thread->return_error.cmd = return_error;
3542 binder_enqueue_thread_work(thread, &thread->return_error.work);
3543 }
3544}
3545
David Brazdil0f672f62019-12-10 10:32:29 +00003546/**
3547 * binder_free_buf() - free the specified buffer
3548 * @proc: binder proc that owns buffer
3549 * @buffer: buffer to be freed
Olivier Deprez157378f2022-04-04 15:47:50 +02003550 * @is_failure: failed to send transaction
David Brazdil0f672f62019-12-10 10:32:29 +00003551 *
3552 * If buffer for an async transaction, enqueue the next async
3553 * transaction from the node.
3554 *
3555 * Cleanup buffer and free it.
3556 */
3557static void
Olivier Deprez157378f2022-04-04 15:47:50 +02003558binder_free_buf(struct binder_proc *proc,
3559 struct binder_thread *thread,
3560 struct binder_buffer *buffer, bool is_failure)
David Brazdil0f672f62019-12-10 10:32:29 +00003561{
3562 binder_inner_proc_lock(proc);
3563 if (buffer->transaction) {
3564 buffer->transaction->buffer = NULL;
3565 buffer->transaction = NULL;
3566 }
3567 binder_inner_proc_unlock(proc);
3568 if (buffer->async_transaction && buffer->target_node) {
3569 struct binder_node *buf_node;
3570 struct binder_work *w;
3571
3572 buf_node = buffer->target_node;
3573 binder_node_inner_lock(buf_node);
3574 BUG_ON(!buf_node->has_async_transaction);
3575 BUG_ON(buf_node->proc != proc);
3576 w = binder_dequeue_work_head_ilocked(
3577 &buf_node->async_todo);
3578 if (!w) {
3579 buf_node->has_async_transaction = false;
3580 } else {
3581 binder_enqueue_work_ilocked(
3582 w, &proc->todo);
3583 binder_wakeup_proc_ilocked(proc);
3584 }
3585 binder_node_inner_unlock(buf_node);
3586 }
3587 trace_binder_transaction_buffer_release(buffer);
Olivier Deprez157378f2022-04-04 15:47:50 +02003588 binder_transaction_buffer_release(proc, thread, buffer, 0, is_failure);
David Brazdil0f672f62019-12-10 10:32:29 +00003589 binder_alloc_free_buf(&proc->alloc, buffer);
3590}
3591
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003592static int binder_thread_write(struct binder_proc *proc,
3593 struct binder_thread *thread,
3594 binder_uintptr_t binder_buffer, size_t size,
3595 binder_size_t *consumed)
3596{
3597 uint32_t cmd;
3598 struct binder_context *context = proc->context;
3599 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3600 void __user *ptr = buffer + *consumed;
3601 void __user *end = buffer + size;
3602
3603 while (ptr < end && thread->return_error.cmd == BR_OK) {
3604 int ret;
3605
3606 if (get_user(cmd, (uint32_t __user *)ptr))
3607 return -EFAULT;
3608 ptr += sizeof(uint32_t);
3609 trace_binder_command(cmd);
3610 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3611 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3612 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3613 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3614 }
3615 switch (cmd) {
3616 case BC_INCREFS:
3617 case BC_ACQUIRE:
3618 case BC_RELEASE:
3619 case BC_DECREFS: {
3620 uint32_t target;
3621 const char *debug_string;
3622 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3623 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3624 struct binder_ref_data rdata;
3625
3626 if (get_user(target, (uint32_t __user *)ptr))
3627 return -EFAULT;
3628
3629 ptr += sizeof(uint32_t);
3630 ret = -1;
3631 if (increment && !target) {
3632 struct binder_node *ctx_mgr_node;
3633 mutex_lock(&context->context_mgr_node_lock);
3634 ctx_mgr_node = context->binder_context_mgr_node;
Olivier Deprez0e641232021-09-23 10:07:05 +02003635 if (ctx_mgr_node) {
3636 if (ctx_mgr_node->proc == proc) {
3637 binder_user_error("%d:%d context manager tried to acquire desc 0\n",
3638 proc->pid, thread->pid);
3639 mutex_unlock(&context->context_mgr_node_lock);
3640 return -EINVAL;
3641 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003642 ret = binder_inc_ref_for_node(
3643 proc, ctx_mgr_node,
3644 strong, NULL, &rdata);
Olivier Deprez0e641232021-09-23 10:07:05 +02003645 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003646 mutex_unlock(&context->context_mgr_node_lock);
3647 }
3648 if (ret)
3649 ret = binder_update_ref_for_handle(
3650 proc, target, increment, strong,
3651 &rdata);
3652 if (!ret && rdata.desc != target) {
3653 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3654 proc->pid, thread->pid,
3655 target, rdata.desc);
3656 }
3657 switch (cmd) {
3658 case BC_INCREFS:
3659 debug_string = "IncRefs";
3660 break;
3661 case BC_ACQUIRE:
3662 debug_string = "Acquire";
3663 break;
3664 case BC_RELEASE:
3665 debug_string = "Release";
3666 break;
3667 case BC_DECREFS:
3668 default:
3669 debug_string = "DecRefs";
3670 break;
3671 }
3672 if (ret) {
3673 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3674 proc->pid, thread->pid, debug_string,
3675 strong, target, ret);
3676 break;
3677 }
3678 binder_debug(BINDER_DEBUG_USER_REFS,
3679 "%d:%d %s ref %d desc %d s %d w %d\n",
3680 proc->pid, thread->pid, debug_string,
3681 rdata.debug_id, rdata.desc, rdata.strong,
3682 rdata.weak);
3683 break;
3684 }
3685 case BC_INCREFS_DONE:
3686 case BC_ACQUIRE_DONE: {
3687 binder_uintptr_t node_ptr;
3688 binder_uintptr_t cookie;
3689 struct binder_node *node;
3690 bool free_node;
3691
3692 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3693 return -EFAULT;
3694 ptr += sizeof(binder_uintptr_t);
3695 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3696 return -EFAULT;
3697 ptr += sizeof(binder_uintptr_t);
3698 node = binder_get_node(proc, node_ptr);
3699 if (node == NULL) {
3700 binder_user_error("%d:%d %s u%016llx no match\n",
3701 proc->pid, thread->pid,
3702 cmd == BC_INCREFS_DONE ?
3703 "BC_INCREFS_DONE" :
3704 "BC_ACQUIRE_DONE",
3705 (u64)node_ptr);
3706 break;
3707 }
3708 if (cookie != node->cookie) {
3709 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3710 proc->pid, thread->pid,
3711 cmd == BC_INCREFS_DONE ?
3712 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3713 (u64)node_ptr, node->debug_id,
3714 (u64)cookie, (u64)node->cookie);
3715 binder_put_node(node);
3716 break;
3717 }
3718 binder_node_inner_lock(node);
3719 if (cmd == BC_ACQUIRE_DONE) {
3720 if (node->pending_strong_ref == 0) {
3721 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3722 proc->pid, thread->pid,
3723 node->debug_id);
3724 binder_node_inner_unlock(node);
3725 binder_put_node(node);
3726 break;
3727 }
3728 node->pending_strong_ref = 0;
3729 } else {
3730 if (node->pending_weak_ref == 0) {
3731 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3732 proc->pid, thread->pid,
3733 node->debug_id);
3734 binder_node_inner_unlock(node);
3735 binder_put_node(node);
3736 break;
3737 }
3738 node->pending_weak_ref = 0;
3739 }
3740 free_node = binder_dec_node_nilocked(node,
3741 cmd == BC_ACQUIRE_DONE, 0);
3742 WARN_ON(free_node);
3743 binder_debug(BINDER_DEBUG_USER_REFS,
3744 "%d:%d %s node %d ls %d lw %d tr %d\n",
3745 proc->pid, thread->pid,
3746 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3747 node->debug_id, node->local_strong_refs,
3748 node->local_weak_refs, node->tmp_refs);
3749 binder_node_inner_unlock(node);
3750 binder_put_node(node);
3751 break;
3752 }
3753 case BC_ATTEMPT_ACQUIRE:
3754 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3755 return -EINVAL;
3756 case BC_ACQUIRE_RESULT:
3757 pr_err("BC_ACQUIRE_RESULT not supported\n");
3758 return -EINVAL;
3759
3760 case BC_FREE_BUFFER: {
3761 binder_uintptr_t data_ptr;
3762 struct binder_buffer *buffer;
3763
3764 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3765 return -EFAULT;
3766 ptr += sizeof(binder_uintptr_t);
3767
3768 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3769 data_ptr);
3770 if (IS_ERR_OR_NULL(buffer)) {
3771 if (PTR_ERR(buffer) == -EPERM) {
3772 binder_user_error(
3773 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3774 proc->pid, thread->pid,
3775 (u64)data_ptr);
3776 } else {
3777 binder_user_error(
3778 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3779 proc->pid, thread->pid,
3780 (u64)data_ptr);
3781 }
3782 break;
3783 }
3784 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3785 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3786 proc->pid, thread->pid, (u64)data_ptr,
3787 buffer->debug_id,
3788 buffer->transaction ? "active" : "finished");
Olivier Deprez157378f2022-04-04 15:47:50 +02003789 binder_free_buf(proc, thread, buffer, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003790 break;
3791 }
3792
3793 case BC_TRANSACTION_SG:
3794 case BC_REPLY_SG: {
3795 struct binder_transaction_data_sg tr;
3796
3797 if (copy_from_user(&tr, ptr, sizeof(tr)))
3798 return -EFAULT;
3799 ptr += sizeof(tr);
3800 binder_transaction(proc, thread, &tr.transaction_data,
3801 cmd == BC_REPLY_SG, tr.buffers_size);
3802 break;
3803 }
3804 case BC_TRANSACTION:
3805 case BC_REPLY: {
3806 struct binder_transaction_data tr;
3807
3808 if (copy_from_user(&tr, ptr, sizeof(tr)))
3809 return -EFAULT;
3810 ptr += sizeof(tr);
3811 binder_transaction(proc, thread, &tr,
3812 cmd == BC_REPLY, 0);
3813 break;
3814 }
3815
3816 case BC_REGISTER_LOOPER:
3817 binder_debug(BINDER_DEBUG_THREADS,
3818 "%d:%d BC_REGISTER_LOOPER\n",
3819 proc->pid, thread->pid);
3820 binder_inner_proc_lock(proc);
3821 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3822 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3823 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3824 proc->pid, thread->pid);
3825 } else if (proc->requested_threads == 0) {
3826 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3827 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3828 proc->pid, thread->pid);
3829 } else {
3830 proc->requested_threads--;
3831 proc->requested_threads_started++;
3832 }
3833 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3834 binder_inner_proc_unlock(proc);
3835 break;
3836 case BC_ENTER_LOOPER:
3837 binder_debug(BINDER_DEBUG_THREADS,
3838 "%d:%d BC_ENTER_LOOPER\n",
3839 proc->pid, thread->pid);
3840 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3841 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3842 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3843 proc->pid, thread->pid);
3844 }
3845 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3846 break;
3847 case BC_EXIT_LOOPER:
3848 binder_debug(BINDER_DEBUG_THREADS,
3849 "%d:%d BC_EXIT_LOOPER\n",
3850 proc->pid, thread->pid);
3851 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3852 break;
3853
3854 case BC_REQUEST_DEATH_NOTIFICATION:
3855 case BC_CLEAR_DEATH_NOTIFICATION: {
3856 uint32_t target;
3857 binder_uintptr_t cookie;
3858 struct binder_ref *ref;
3859 struct binder_ref_death *death = NULL;
3860
3861 if (get_user(target, (uint32_t __user *)ptr))
3862 return -EFAULT;
3863 ptr += sizeof(uint32_t);
3864 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3865 return -EFAULT;
3866 ptr += sizeof(binder_uintptr_t);
3867 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3868 /*
3869 * Allocate memory for death notification
3870 * before taking lock
3871 */
3872 death = kzalloc(sizeof(*death), GFP_KERNEL);
3873 if (death == NULL) {
3874 WARN_ON(thread->return_error.cmd !=
3875 BR_OK);
3876 thread->return_error.cmd = BR_ERROR;
3877 binder_enqueue_thread_work(
3878 thread,
3879 &thread->return_error.work);
3880 binder_debug(
3881 BINDER_DEBUG_FAILED_TRANSACTION,
3882 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3883 proc->pid, thread->pid);
3884 break;
3885 }
3886 }
3887 binder_proc_lock(proc);
3888 ref = binder_get_ref_olocked(proc, target, false);
3889 if (ref == NULL) {
3890 binder_user_error("%d:%d %s invalid ref %d\n",
3891 proc->pid, thread->pid,
3892 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3893 "BC_REQUEST_DEATH_NOTIFICATION" :
3894 "BC_CLEAR_DEATH_NOTIFICATION",
3895 target);
3896 binder_proc_unlock(proc);
3897 kfree(death);
3898 break;
3899 }
3900
3901 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3902 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3903 proc->pid, thread->pid,
3904 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3905 "BC_REQUEST_DEATH_NOTIFICATION" :
3906 "BC_CLEAR_DEATH_NOTIFICATION",
3907 (u64)cookie, ref->data.debug_id,
3908 ref->data.desc, ref->data.strong,
3909 ref->data.weak, ref->node->debug_id);
3910
3911 binder_node_lock(ref->node);
3912 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3913 if (ref->death) {
3914 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3915 proc->pid, thread->pid);
3916 binder_node_unlock(ref->node);
3917 binder_proc_unlock(proc);
3918 kfree(death);
3919 break;
3920 }
3921 binder_stats_created(BINDER_STAT_DEATH);
3922 INIT_LIST_HEAD(&death->work.entry);
3923 death->cookie = cookie;
3924 ref->death = death;
3925 if (ref->node->proc == NULL) {
3926 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3927
3928 binder_inner_proc_lock(proc);
3929 binder_enqueue_work_ilocked(
3930 &ref->death->work, &proc->todo);
3931 binder_wakeup_proc_ilocked(proc);
3932 binder_inner_proc_unlock(proc);
3933 }
3934 } else {
3935 if (ref->death == NULL) {
3936 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3937 proc->pid, thread->pid);
3938 binder_node_unlock(ref->node);
3939 binder_proc_unlock(proc);
3940 break;
3941 }
3942 death = ref->death;
3943 if (death->cookie != cookie) {
3944 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3945 proc->pid, thread->pid,
3946 (u64)death->cookie,
3947 (u64)cookie);
3948 binder_node_unlock(ref->node);
3949 binder_proc_unlock(proc);
3950 break;
3951 }
3952 ref->death = NULL;
3953 binder_inner_proc_lock(proc);
3954 if (list_empty(&death->work.entry)) {
3955 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3956 if (thread->looper &
3957 (BINDER_LOOPER_STATE_REGISTERED |
3958 BINDER_LOOPER_STATE_ENTERED))
3959 binder_enqueue_thread_work_ilocked(
3960 thread,
3961 &death->work);
3962 else {
3963 binder_enqueue_work_ilocked(
3964 &death->work,
3965 &proc->todo);
3966 binder_wakeup_proc_ilocked(
3967 proc);
3968 }
3969 } else {
3970 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3971 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3972 }
3973 binder_inner_proc_unlock(proc);
3974 }
3975 binder_node_unlock(ref->node);
3976 binder_proc_unlock(proc);
3977 } break;
3978 case BC_DEAD_BINDER_DONE: {
3979 struct binder_work *w;
3980 binder_uintptr_t cookie;
3981 struct binder_ref_death *death = NULL;
3982
3983 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3984 return -EFAULT;
3985
3986 ptr += sizeof(cookie);
3987 binder_inner_proc_lock(proc);
3988 list_for_each_entry(w, &proc->delivered_death,
3989 entry) {
3990 struct binder_ref_death *tmp_death =
3991 container_of(w,
3992 struct binder_ref_death,
3993 work);
3994
3995 if (tmp_death->cookie == cookie) {
3996 death = tmp_death;
3997 break;
3998 }
3999 }
4000 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4001 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
4002 proc->pid, thread->pid, (u64)cookie,
4003 death);
4004 if (death == NULL) {
4005 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4006 proc->pid, thread->pid, (u64)cookie);
4007 binder_inner_proc_unlock(proc);
4008 break;
4009 }
4010 binder_dequeue_work_ilocked(&death->work);
4011 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4012 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
4013 if (thread->looper &
4014 (BINDER_LOOPER_STATE_REGISTERED |
4015 BINDER_LOOPER_STATE_ENTERED))
4016 binder_enqueue_thread_work_ilocked(
4017 thread, &death->work);
4018 else {
4019 binder_enqueue_work_ilocked(
4020 &death->work,
4021 &proc->todo);
4022 binder_wakeup_proc_ilocked(proc);
4023 }
4024 }
4025 binder_inner_proc_unlock(proc);
4026 } break;
4027
4028 default:
4029 pr_err("%d:%d unknown command %d\n",
4030 proc->pid, thread->pid, cmd);
4031 return -EINVAL;
4032 }
4033 *consumed = ptr - buffer;
4034 }
4035 return 0;
4036}
4037
4038static void binder_stat_br(struct binder_proc *proc,
4039 struct binder_thread *thread, uint32_t cmd)
4040{
4041 trace_binder_return(cmd);
4042 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
4043 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4044 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4045 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
4046 }
4047}
4048
4049static int binder_put_node_cmd(struct binder_proc *proc,
4050 struct binder_thread *thread,
4051 void __user **ptrp,
4052 binder_uintptr_t node_ptr,
4053 binder_uintptr_t node_cookie,
4054 int node_debug_id,
4055 uint32_t cmd, const char *cmd_name)
4056{
4057 void __user *ptr = *ptrp;
4058
4059 if (put_user(cmd, (uint32_t __user *)ptr))
4060 return -EFAULT;
4061 ptr += sizeof(uint32_t);
4062
4063 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4064 return -EFAULT;
4065 ptr += sizeof(binder_uintptr_t);
4066
4067 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4068 return -EFAULT;
4069 ptr += sizeof(binder_uintptr_t);
4070
4071 binder_stat_br(proc, thread, cmd);
4072 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4073 proc->pid, thread->pid, cmd_name, node_debug_id,
4074 (u64)node_ptr, (u64)node_cookie);
4075
4076 *ptrp = ptr;
4077 return 0;
4078}
4079
4080static int binder_wait_for_work(struct binder_thread *thread,
4081 bool do_proc_work)
4082{
4083 DEFINE_WAIT(wait);
4084 struct binder_proc *proc = thread->proc;
4085 int ret = 0;
4086
4087 freezer_do_not_count();
4088 binder_inner_proc_lock(proc);
4089 for (;;) {
4090 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4091 if (binder_has_work_ilocked(thread, do_proc_work))
4092 break;
4093 if (do_proc_work)
4094 list_add(&thread->waiting_thread_node,
4095 &proc->waiting_threads);
4096 binder_inner_proc_unlock(proc);
4097 schedule();
4098 binder_inner_proc_lock(proc);
4099 list_del_init(&thread->waiting_thread_node);
4100 if (signal_pending(current)) {
4101 ret = -ERESTARTSYS;
4102 break;
4103 }
4104 }
4105 finish_wait(&thread->wait, &wait);
4106 binder_inner_proc_unlock(proc);
4107 freezer_count();
4108
4109 return ret;
4110}
4111
David Brazdil0f672f62019-12-10 10:32:29 +00004112/**
4113 * binder_apply_fd_fixups() - finish fd translation
4114 * @proc: binder_proc associated @t->buffer
4115 * @t: binder transaction with list of fd fixups
4116 *
4117 * Now that we are in the context of the transaction target
4118 * process, we can allocate and install fds. Process the
4119 * list of fds to translate and fixup the buffer with the
4120 * new fds.
4121 *
4122 * If we fail to allocate an fd, then free the resources by
4123 * fput'ing files that have not been processed and ksys_close'ing
4124 * any fds that have already been allocated.
4125 */
4126static int binder_apply_fd_fixups(struct binder_proc *proc,
4127 struct binder_transaction *t)
4128{
4129 struct binder_txn_fd_fixup *fixup, *tmp;
4130 int ret = 0;
4131
4132 list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) {
4133 int fd = get_unused_fd_flags(O_CLOEXEC);
4134
4135 if (fd < 0) {
4136 binder_debug(BINDER_DEBUG_TRANSACTION,
4137 "failed fd fixup txn %d fd %d\n",
4138 t->debug_id, fd);
4139 ret = -ENOMEM;
4140 break;
4141 }
4142 binder_debug(BINDER_DEBUG_TRANSACTION,
4143 "fd fixup txn %d fd %d\n",
4144 t->debug_id, fd);
4145 trace_binder_transaction_fd_recv(t, fd, fixup->offset);
4146 fd_install(fd, fixup->file);
4147 fixup->file = NULL;
4148 if (binder_alloc_copy_to_buffer(&proc->alloc, t->buffer,
4149 fixup->offset, &fd,
4150 sizeof(u32))) {
4151 ret = -EINVAL;
4152 break;
4153 }
4154 }
4155 list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
4156 if (fixup->file) {
4157 fput(fixup->file);
4158 } else if (ret) {
4159 u32 fd;
4160 int err;
4161
4162 err = binder_alloc_copy_from_buffer(&proc->alloc, &fd,
4163 t->buffer,
4164 fixup->offset,
4165 sizeof(fd));
4166 WARN_ON(err);
4167 if (!err)
4168 binder_deferred_fd_close(fd);
4169 }
4170 list_del(&fixup->fixup_entry);
4171 kfree(fixup);
4172 }
4173
4174 return ret;
4175}
4176
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004177static int binder_thread_read(struct binder_proc *proc,
4178 struct binder_thread *thread,
4179 binder_uintptr_t binder_buffer, size_t size,
4180 binder_size_t *consumed, int non_block)
4181{
4182 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4183 void __user *ptr = buffer + *consumed;
4184 void __user *end = buffer + size;
4185
4186 int ret = 0;
4187 int wait_for_proc_work;
4188
4189 if (*consumed == 0) {
4190 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4191 return -EFAULT;
4192 ptr += sizeof(uint32_t);
4193 }
4194
4195retry:
4196 binder_inner_proc_lock(proc);
4197 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4198 binder_inner_proc_unlock(proc);
4199
4200 thread->looper |= BINDER_LOOPER_STATE_WAITING;
4201
4202 trace_binder_wait_for_work(wait_for_proc_work,
4203 !!thread->transaction_stack,
4204 !binder_worklist_empty(proc, &thread->todo));
4205 if (wait_for_proc_work) {
4206 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4207 BINDER_LOOPER_STATE_ENTERED))) {
4208 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4209 proc->pid, thread->pid, thread->looper);
4210 wait_event_interruptible(binder_user_error_wait,
4211 binder_stop_on_user_error < 2);
4212 }
4213 binder_set_nice(proc->default_priority);
4214 }
4215
4216 if (non_block) {
4217 if (!binder_has_work(thread, wait_for_proc_work))
4218 ret = -EAGAIN;
4219 } else {
4220 ret = binder_wait_for_work(thread, wait_for_proc_work);
4221 }
4222
4223 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4224
4225 if (ret)
4226 return ret;
4227
4228 while (1) {
4229 uint32_t cmd;
David Brazdil0f672f62019-12-10 10:32:29 +00004230 struct binder_transaction_data_secctx tr;
4231 struct binder_transaction_data *trd = &tr.transaction_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004232 struct binder_work *w = NULL;
4233 struct list_head *list = NULL;
4234 struct binder_transaction *t = NULL;
4235 struct binder_thread *t_from;
David Brazdil0f672f62019-12-10 10:32:29 +00004236 size_t trsize = sizeof(*trd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004237
4238 binder_inner_proc_lock(proc);
4239 if (!binder_worklist_empty_ilocked(&thread->todo))
4240 list = &thread->todo;
4241 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4242 wait_for_proc_work)
4243 list = &proc->todo;
4244 else {
4245 binder_inner_proc_unlock(proc);
4246
4247 /* no data added */
4248 if (ptr - buffer == 4 && !thread->looper_need_return)
4249 goto retry;
4250 break;
4251 }
4252
4253 if (end - ptr < sizeof(tr) + 4) {
4254 binder_inner_proc_unlock(proc);
4255 break;
4256 }
4257 w = binder_dequeue_work_head_ilocked(list);
4258 if (binder_worklist_empty_ilocked(&thread->todo))
4259 thread->process_todo = false;
4260
4261 switch (w->type) {
4262 case BINDER_WORK_TRANSACTION: {
4263 binder_inner_proc_unlock(proc);
4264 t = container_of(w, struct binder_transaction, work);
4265 } break;
4266 case BINDER_WORK_RETURN_ERROR: {
4267 struct binder_error *e = container_of(
4268 w, struct binder_error, work);
4269
4270 WARN_ON(e->cmd == BR_OK);
4271 binder_inner_proc_unlock(proc);
4272 if (put_user(e->cmd, (uint32_t __user *)ptr))
4273 return -EFAULT;
4274 cmd = e->cmd;
4275 e->cmd = BR_OK;
4276 ptr += sizeof(uint32_t);
4277
4278 binder_stat_br(proc, thread, cmd);
4279 } break;
4280 case BINDER_WORK_TRANSACTION_COMPLETE: {
4281 binder_inner_proc_unlock(proc);
4282 cmd = BR_TRANSACTION_COMPLETE;
David Brazdil0f672f62019-12-10 10:32:29 +00004283 kfree(w);
4284 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004285 if (put_user(cmd, (uint32_t __user *)ptr))
4286 return -EFAULT;
4287 ptr += sizeof(uint32_t);
4288
4289 binder_stat_br(proc, thread, cmd);
4290 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4291 "%d:%d BR_TRANSACTION_COMPLETE\n",
4292 proc->pid, thread->pid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004293 } break;
4294 case BINDER_WORK_NODE: {
4295 struct binder_node *node = container_of(w, struct binder_node, work);
4296 int strong, weak;
4297 binder_uintptr_t node_ptr = node->ptr;
4298 binder_uintptr_t node_cookie = node->cookie;
4299 int node_debug_id = node->debug_id;
4300 int has_weak_ref;
4301 int has_strong_ref;
4302 void __user *orig_ptr = ptr;
4303
4304 BUG_ON(proc != node->proc);
4305 strong = node->internal_strong_refs ||
4306 node->local_strong_refs;
4307 weak = !hlist_empty(&node->refs) ||
4308 node->local_weak_refs ||
4309 node->tmp_refs || strong;
4310 has_strong_ref = node->has_strong_ref;
4311 has_weak_ref = node->has_weak_ref;
4312
4313 if (weak && !has_weak_ref) {
4314 node->has_weak_ref = 1;
4315 node->pending_weak_ref = 1;
4316 node->local_weak_refs++;
4317 }
4318 if (strong && !has_strong_ref) {
4319 node->has_strong_ref = 1;
4320 node->pending_strong_ref = 1;
4321 node->local_strong_refs++;
4322 }
4323 if (!strong && has_strong_ref)
4324 node->has_strong_ref = 0;
4325 if (!weak && has_weak_ref)
4326 node->has_weak_ref = 0;
4327 if (!weak && !strong) {
4328 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4329 "%d:%d node %d u%016llx c%016llx deleted\n",
4330 proc->pid, thread->pid,
4331 node_debug_id,
4332 (u64)node_ptr,
4333 (u64)node_cookie);
4334 rb_erase(&node->rb_node, &proc->nodes);
4335 binder_inner_proc_unlock(proc);
4336 binder_node_lock(node);
4337 /*
4338 * Acquire the node lock before freeing the
4339 * node to serialize with other threads that
4340 * may have been holding the node lock while
4341 * decrementing this node (avoids race where
4342 * this thread frees while the other thread
4343 * is unlocking the node after the final
4344 * decrement)
4345 */
4346 binder_node_unlock(node);
4347 binder_free_node(node);
4348 } else
4349 binder_inner_proc_unlock(proc);
4350
4351 if (weak && !has_weak_ref)
4352 ret = binder_put_node_cmd(
4353 proc, thread, &ptr, node_ptr,
4354 node_cookie, node_debug_id,
4355 BR_INCREFS, "BR_INCREFS");
4356 if (!ret && strong && !has_strong_ref)
4357 ret = binder_put_node_cmd(
4358 proc, thread, &ptr, node_ptr,
4359 node_cookie, node_debug_id,
4360 BR_ACQUIRE, "BR_ACQUIRE");
4361 if (!ret && !strong && has_strong_ref)
4362 ret = binder_put_node_cmd(
4363 proc, thread, &ptr, node_ptr,
4364 node_cookie, node_debug_id,
4365 BR_RELEASE, "BR_RELEASE");
4366 if (!ret && !weak && has_weak_ref)
4367 ret = binder_put_node_cmd(
4368 proc, thread, &ptr, node_ptr,
4369 node_cookie, node_debug_id,
4370 BR_DECREFS, "BR_DECREFS");
4371 if (orig_ptr == ptr)
4372 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4373 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4374 proc->pid, thread->pid,
4375 node_debug_id,
4376 (u64)node_ptr,
4377 (u64)node_cookie);
4378 if (ret)
4379 return ret;
4380 } break;
4381 case BINDER_WORK_DEAD_BINDER:
4382 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4383 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4384 struct binder_ref_death *death;
4385 uint32_t cmd;
4386 binder_uintptr_t cookie;
4387
4388 death = container_of(w, struct binder_ref_death, work);
4389 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4390 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4391 else
4392 cmd = BR_DEAD_BINDER;
4393 cookie = death->cookie;
4394
4395 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4396 "%d:%d %s %016llx\n",
4397 proc->pid, thread->pid,
4398 cmd == BR_DEAD_BINDER ?
4399 "BR_DEAD_BINDER" :
4400 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4401 (u64)cookie);
4402 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4403 binder_inner_proc_unlock(proc);
4404 kfree(death);
4405 binder_stats_deleted(BINDER_STAT_DEATH);
4406 } else {
4407 binder_enqueue_work_ilocked(
4408 w, &proc->delivered_death);
4409 binder_inner_proc_unlock(proc);
4410 }
4411 if (put_user(cmd, (uint32_t __user *)ptr))
4412 return -EFAULT;
4413 ptr += sizeof(uint32_t);
4414 if (put_user(cookie,
4415 (binder_uintptr_t __user *)ptr))
4416 return -EFAULT;
4417 ptr += sizeof(binder_uintptr_t);
4418 binder_stat_br(proc, thread, cmd);
4419 if (cmd == BR_DEAD_BINDER)
4420 goto done; /* DEAD_BINDER notifications can cause transactions */
4421 } break;
David Brazdil0f672f62019-12-10 10:32:29 +00004422 default:
4423 binder_inner_proc_unlock(proc);
4424 pr_err("%d:%d: bad work type %d\n",
4425 proc->pid, thread->pid, w->type);
4426 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004427 }
4428
4429 if (!t)
4430 continue;
4431
4432 BUG_ON(t->buffer == NULL);
4433 if (t->buffer->target_node) {
4434 struct binder_node *target_node = t->buffer->target_node;
4435
David Brazdil0f672f62019-12-10 10:32:29 +00004436 trd->target.ptr = target_node->ptr;
4437 trd->cookie = target_node->cookie;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004438 t->saved_priority = task_nice(current);
4439 if (t->priority < target_node->min_priority &&
4440 !(t->flags & TF_ONE_WAY))
4441 binder_set_nice(t->priority);
4442 else if (!(t->flags & TF_ONE_WAY) ||
4443 t->saved_priority > target_node->min_priority)
4444 binder_set_nice(target_node->min_priority);
4445 cmd = BR_TRANSACTION;
4446 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00004447 trd->target.ptr = 0;
4448 trd->cookie = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004449 cmd = BR_REPLY;
4450 }
David Brazdil0f672f62019-12-10 10:32:29 +00004451 trd->code = t->code;
4452 trd->flags = t->flags;
4453 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004454
4455 t_from = binder_get_txn_from(t);
4456 if (t_from) {
4457 struct task_struct *sender = t_from->proc->tsk;
4458
David Brazdil0f672f62019-12-10 10:32:29 +00004459 trd->sender_pid =
4460 task_tgid_nr_ns(sender,
4461 task_active_pid_ns(current));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004462 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00004463 trd->sender_pid = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004464 }
4465
David Brazdil0f672f62019-12-10 10:32:29 +00004466 ret = binder_apply_fd_fixups(proc, t);
4467 if (ret) {
4468 struct binder_buffer *buffer = t->buffer;
4469 bool oneway = !!(t->flags & TF_ONE_WAY);
4470 int tid = t->debug_id;
4471
4472 if (t_from)
4473 binder_thread_dec_tmpref(t_from);
4474 buffer->transaction = NULL;
4475 binder_cleanup_transaction(t, "fd fixups failed",
4476 BR_FAILED_REPLY);
Olivier Deprez157378f2022-04-04 15:47:50 +02004477 binder_free_buf(proc, thread, buffer, true);
David Brazdil0f672f62019-12-10 10:32:29 +00004478 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
4479 "%d:%d %stransaction %d fd fixups failed %d/%d, line %d\n",
4480 proc->pid, thread->pid,
4481 oneway ? "async " :
4482 (cmd == BR_REPLY ? "reply " : ""),
4483 tid, BR_FAILED_REPLY, ret, __LINE__);
4484 if (cmd == BR_REPLY) {
4485 cmd = BR_FAILED_REPLY;
4486 if (put_user(cmd, (uint32_t __user *)ptr))
4487 return -EFAULT;
4488 ptr += sizeof(uint32_t);
4489 binder_stat_br(proc, thread, cmd);
4490 break;
4491 }
4492 continue;
4493 }
4494 trd->data_size = t->buffer->data_size;
4495 trd->offsets_size = t->buffer->offsets_size;
4496 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
4497 trd->data.ptr.offsets = trd->data.ptr.buffer +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004498 ALIGN(t->buffer->data_size,
4499 sizeof(void *));
4500
David Brazdil0f672f62019-12-10 10:32:29 +00004501 tr.secctx = t->security_ctx;
4502 if (t->security_ctx) {
4503 cmd = BR_TRANSACTION_SEC_CTX;
4504 trsize = sizeof(tr);
4505 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004506 if (put_user(cmd, (uint32_t __user *)ptr)) {
4507 if (t_from)
4508 binder_thread_dec_tmpref(t_from);
4509
4510 binder_cleanup_transaction(t, "put_user failed",
4511 BR_FAILED_REPLY);
4512
4513 return -EFAULT;
4514 }
4515 ptr += sizeof(uint32_t);
David Brazdil0f672f62019-12-10 10:32:29 +00004516 if (copy_to_user(ptr, &tr, trsize)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004517 if (t_from)
4518 binder_thread_dec_tmpref(t_from);
4519
4520 binder_cleanup_transaction(t, "copy_to_user failed",
4521 BR_FAILED_REPLY);
4522
4523 return -EFAULT;
4524 }
David Brazdil0f672f62019-12-10 10:32:29 +00004525 ptr += trsize;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004526
4527 trace_binder_transaction_received(t);
4528 binder_stat_br(proc, thread, cmd);
4529 binder_debug(BINDER_DEBUG_TRANSACTION,
4530 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4531 proc->pid, thread->pid,
4532 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
David Brazdil0f672f62019-12-10 10:32:29 +00004533 (cmd == BR_TRANSACTION_SEC_CTX) ?
4534 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004535 t->debug_id, t_from ? t_from->proc->pid : 0,
4536 t_from ? t_from->pid : 0, cmd,
4537 t->buffer->data_size, t->buffer->offsets_size,
David Brazdil0f672f62019-12-10 10:32:29 +00004538 (u64)trd->data.ptr.buffer,
4539 (u64)trd->data.ptr.offsets);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004540
4541 if (t_from)
4542 binder_thread_dec_tmpref(t_from);
4543 t->buffer->allow_user_free = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00004544 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004545 binder_inner_proc_lock(thread->proc);
4546 t->to_parent = thread->transaction_stack;
4547 t->to_thread = thread;
4548 thread->transaction_stack = t;
4549 binder_inner_proc_unlock(thread->proc);
4550 } else {
4551 binder_free_transaction(t);
4552 }
4553 break;
4554 }
4555
4556done:
4557
4558 *consumed = ptr - buffer;
4559 binder_inner_proc_lock(proc);
4560 if (proc->requested_threads == 0 &&
4561 list_empty(&thread->proc->waiting_threads) &&
4562 proc->requested_threads_started < proc->max_threads &&
4563 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4564 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4565 /*spawn a new thread if we leave this out */) {
4566 proc->requested_threads++;
4567 binder_inner_proc_unlock(proc);
4568 binder_debug(BINDER_DEBUG_THREADS,
4569 "%d:%d BR_SPAWN_LOOPER\n",
4570 proc->pid, thread->pid);
4571 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4572 return -EFAULT;
4573 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4574 } else
4575 binder_inner_proc_unlock(proc);
4576 return 0;
4577}
4578
4579static void binder_release_work(struct binder_proc *proc,
4580 struct list_head *list)
4581{
4582 struct binder_work *w;
Olivier Deprez0e641232021-09-23 10:07:05 +02004583 enum binder_work_type wtype;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004584
4585 while (1) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004586 binder_inner_proc_lock(proc);
4587 w = binder_dequeue_work_head_ilocked(list);
4588 wtype = w ? w->type : 0;
4589 binder_inner_proc_unlock(proc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004590 if (!w)
4591 return;
4592
Olivier Deprez0e641232021-09-23 10:07:05 +02004593 switch (wtype) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004594 case BINDER_WORK_TRANSACTION: {
4595 struct binder_transaction *t;
4596
4597 t = container_of(w, struct binder_transaction, work);
4598
4599 binder_cleanup_transaction(t, "process died.",
4600 BR_DEAD_REPLY);
4601 } break;
4602 case BINDER_WORK_RETURN_ERROR: {
4603 struct binder_error *e = container_of(
4604 w, struct binder_error, work);
4605
4606 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4607 "undelivered TRANSACTION_ERROR: %u\n",
4608 e->cmd);
4609 } break;
4610 case BINDER_WORK_TRANSACTION_COMPLETE: {
4611 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4612 "undelivered TRANSACTION_COMPLETE\n");
4613 kfree(w);
4614 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4615 } break;
4616 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4617 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4618 struct binder_ref_death *death;
4619
4620 death = container_of(w, struct binder_ref_death, work);
4621 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4622 "undelivered death notification, %016llx\n",
4623 (u64)death->cookie);
4624 kfree(death);
4625 binder_stats_deleted(BINDER_STAT_DEATH);
4626 } break;
Olivier Deprez0e641232021-09-23 10:07:05 +02004627 case BINDER_WORK_NODE:
4628 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004629 default:
4630 pr_err("unexpected work type, %d, not freed\n",
Olivier Deprez0e641232021-09-23 10:07:05 +02004631 wtype);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004632 break;
4633 }
4634 }
4635
4636}
4637
4638static struct binder_thread *binder_get_thread_ilocked(
4639 struct binder_proc *proc, struct binder_thread *new_thread)
4640{
4641 struct binder_thread *thread = NULL;
4642 struct rb_node *parent = NULL;
4643 struct rb_node **p = &proc->threads.rb_node;
4644
4645 while (*p) {
4646 parent = *p;
4647 thread = rb_entry(parent, struct binder_thread, rb_node);
4648
4649 if (current->pid < thread->pid)
4650 p = &(*p)->rb_left;
4651 else if (current->pid > thread->pid)
4652 p = &(*p)->rb_right;
4653 else
4654 return thread;
4655 }
4656 if (!new_thread)
4657 return NULL;
4658 thread = new_thread;
4659 binder_stats_created(BINDER_STAT_THREAD);
4660 thread->proc = proc;
4661 thread->pid = current->pid;
4662 atomic_set(&thread->tmp_ref, 0);
4663 init_waitqueue_head(&thread->wait);
4664 INIT_LIST_HEAD(&thread->todo);
4665 rb_link_node(&thread->rb_node, parent, p);
4666 rb_insert_color(&thread->rb_node, &proc->threads);
4667 thread->looper_need_return = true;
4668 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4669 thread->return_error.cmd = BR_OK;
4670 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4671 thread->reply_error.cmd = BR_OK;
4672 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4673 return thread;
4674}
4675
4676static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4677{
4678 struct binder_thread *thread;
4679 struct binder_thread *new_thread;
4680
4681 binder_inner_proc_lock(proc);
4682 thread = binder_get_thread_ilocked(proc, NULL);
4683 binder_inner_proc_unlock(proc);
4684 if (!thread) {
4685 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4686 if (new_thread == NULL)
4687 return NULL;
4688 binder_inner_proc_lock(proc);
4689 thread = binder_get_thread_ilocked(proc, new_thread);
4690 binder_inner_proc_unlock(proc);
4691 if (thread != new_thread)
4692 kfree(new_thread);
4693 }
4694 return thread;
4695}
4696
4697static void binder_free_proc(struct binder_proc *proc)
4698{
Olivier Deprez0e641232021-09-23 10:07:05 +02004699 struct binder_device *device;
4700
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004701 BUG_ON(!list_empty(&proc->todo));
4702 BUG_ON(!list_empty(&proc->delivered_death));
Olivier Deprez0e641232021-09-23 10:07:05 +02004703 device = container_of(proc->context, struct binder_device, context);
4704 if (refcount_dec_and_test(&device->ref)) {
4705 kfree(proc->context->name);
4706 kfree(device);
4707 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004708 binder_alloc_deferred_release(&proc->alloc);
4709 put_task_struct(proc->tsk);
Olivier Deprez157378f2022-04-04 15:47:50 +02004710 put_cred(proc->cred);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004711 binder_stats_deleted(BINDER_STAT_PROC);
4712 kfree(proc);
4713}
4714
4715static void binder_free_thread(struct binder_thread *thread)
4716{
4717 BUG_ON(!list_empty(&thread->todo));
4718 binder_stats_deleted(BINDER_STAT_THREAD);
4719 binder_proc_dec_tmpref(thread->proc);
4720 kfree(thread);
4721}
4722
4723static int binder_thread_release(struct binder_proc *proc,
4724 struct binder_thread *thread)
4725{
4726 struct binder_transaction *t;
4727 struct binder_transaction *send_reply = NULL;
4728 int active_transactions = 0;
4729 struct binder_transaction *last_t = NULL;
4730
4731 binder_inner_proc_lock(thread->proc);
4732 /*
4733 * take a ref on the proc so it survives
4734 * after we remove this thread from proc->threads.
4735 * The corresponding dec is when we actually
4736 * free the thread in binder_free_thread()
4737 */
4738 proc->tmp_ref++;
4739 /*
4740 * take a ref on this thread to ensure it
4741 * survives while we are releasing it
4742 */
4743 atomic_inc(&thread->tmp_ref);
4744 rb_erase(&thread->rb_node, &proc->threads);
4745 t = thread->transaction_stack;
4746 if (t) {
4747 spin_lock(&t->lock);
4748 if (t->to_thread == thread)
4749 send_reply = t;
David Brazdil0f672f62019-12-10 10:32:29 +00004750 } else {
4751 __acquire(&t->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004752 }
4753 thread->is_dead = true;
4754
4755 while (t) {
4756 last_t = t;
4757 active_transactions++;
4758 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4759 "release %d:%d transaction %d %s, still active\n",
4760 proc->pid, thread->pid,
4761 t->debug_id,
4762 (t->to_thread == thread) ? "in" : "out");
4763
4764 if (t->to_thread == thread) {
4765 t->to_proc = NULL;
4766 t->to_thread = NULL;
4767 if (t->buffer) {
4768 t->buffer->transaction = NULL;
4769 t->buffer = NULL;
4770 }
4771 t = t->to_parent;
4772 } else if (t->from == thread) {
4773 t->from = NULL;
4774 t = t->from_parent;
4775 } else
4776 BUG();
4777 spin_unlock(&last_t->lock);
4778 if (t)
4779 spin_lock(&t->lock);
David Brazdil0f672f62019-12-10 10:32:29 +00004780 else
4781 __acquire(&t->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004782 }
David Brazdil0f672f62019-12-10 10:32:29 +00004783 /* annotation for sparse, lock not acquired in last iteration above */
4784 __release(&t->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004785
4786 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02004787 * If this thread used poll, make sure we remove the waitqueue from any
4788 * poll data structures holding it.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004789 */
Olivier Deprez157378f2022-04-04 15:47:50 +02004790 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4791 wake_up_pollfree(&thread->wait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004792
4793 binder_inner_proc_unlock(thread->proc);
4794
4795 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02004796 * This is needed to avoid races between wake_up_pollfree() above and
4797 * someone else removing the last entry from the queue for other reasons
4798 * (e.g. ep_remove_wait_queue() being called due to an epoll file
4799 * descriptor being closed). Such other users hold an RCU read lock, so
4800 * we can be sure they're done after we call synchronize_rcu().
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004801 */
4802 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4803 synchronize_rcu();
4804
4805 if (send_reply)
4806 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4807 binder_release_work(proc, &thread->todo);
4808 binder_thread_dec_tmpref(thread);
4809 return active_transactions;
4810}
4811
4812static __poll_t binder_poll(struct file *filp,
4813 struct poll_table_struct *wait)
4814{
4815 struct binder_proc *proc = filp->private_data;
4816 struct binder_thread *thread = NULL;
4817 bool wait_for_proc_work;
4818
4819 thread = binder_get_thread(proc);
4820 if (!thread)
4821 return POLLERR;
4822
4823 binder_inner_proc_lock(thread->proc);
4824 thread->looper |= BINDER_LOOPER_STATE_POLL;
4825 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4826
4827 binder_inner_proc_unlock(thread->proc);
4828
4829 poll_wait(filp, &thread->wait, wait);
4830
4831 if (binder_has_work(thread, wait_for_proc_work))
4832 return EPOLLIN;
4833
4834 return 0;
4835}
4836
4837static int binder_ioctl_write_read(struct file *filp,
4838 unsigned int cmd, unsigned long arg,
4839 struct binder_thread *thread)
4840{
4841 int ret = 0;
4842 struct binder_proc *proc = filp->private_data;
4843 unsigned int size = _IOC_SIZE(cmd);
4844 void __user *ubuf = (void __user *)arg;
4845 struct binder_write_read bwr;
4846
4847 if (size != sizeof(struct binder_write_read)) {
4848 ret = -EINVAL;
4849 goto out;
4850 }
4851 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4852 ret = -EFAULT;
4853 goto out;
4854 }
4855 binder_debug(BINDER_DEBUG_READ_WRITE,
4856 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4857 proc->pid, thread->pid,
4858 (u64)bwr.write_size, (u64)bwr.write_buffer,
4859 (u64)bwr.read_size, (u64)bwr.read_buffer);
4860
4861 if (bwr.write_size > 0) {
4862 ret = binder_thread_write(proc, thread,
4863 bwr.write_buffer,
4864 bwr.write_size,
4865 &bwr.write_consumed);
4866 trace_binder_write_done(ret);
4867 if (ret < 0) {
4868 bwr.read_consumed = 0;
4869 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4870 ret = -EFAULT;
4871 goto out;
4872 }
4873 }
4874 if (bwr.read_size > 0) {
4875 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4876 bwr.read_size,
4877 &bwr.read_consumed,
4878 filp->f_flags & O_NONBLOCK);
4879 trace_binder_read_done(ret);
4880 binder_inner_proc_lock(proc);
4881 if (!binder_worklist_empty_ilocked(&proc->todo))
4882 binder_wakeup_proc_ilocked(proc);
4883 binder_inner_proc_unlock(proc);
4884 if (ret < 0) {
4885 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4886 ret = -EFAULT;
4887 goto out;
4888 }
4889 }
4890 binder_debug(BINDER_DEBUG_READ_WRITE,
4891 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4892 proc->pid, thread->pid,
4893 (u64)bwr.write_consumed, (u64)bwr.write_size,
4894 (u64)bwr.read_consumed, (u64)bwr.read_size);
4895 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4896 ret = -EFAULT;
4897 goto out;
4898 }
4899out:
4900 return ret;
4901}
4902
David Brazdil0f672f62019-12-10 10:32:29 +00004903static int binder_ioctl_set_ctx_mgr(struct file *filp,
4904 struct flat_binder_object *fbo)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004905{
4906 int ret = 0;
4907 struct binder_proc *proc = filp->private_data;
4908 struct binder_context *context = proc->context;
4909 struct binder_node *new_node;
4910 kuid_t curr_euid = current_euid();
4911
4912 mutex_lock(&context->context_mgr_node_lock);
4913 if (context->binder_context_mgr_node) {
4914 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4915 ret = -EBUSY;
4916 goto out;
4917 }
Olivier Deprez157378f2022-04-04 15:47:50 +02004918 ret = security_binder_set_context_mgr(proc->cred);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004919 if (ret < 0)
4920 goto out;
4921 if (uid_valid(context->binder_context_mgr_uid)) {
4922 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4923 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4924 from_kuid(&init_user_ns, curr_euid),
4925 from_kuid(&init_user_ns,
4926 context->binder_context_mgr_uid));
4927 ret = -EPERM;
4928 goto out;
4929 }
4930 } else {
4931 context->binder_context_mgr_uid = curr_euid;
4932 }
David Brazdil0f672f62019-12-10 10:32:29 +00004933 new_node = binder_new_node(proc, fbo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004934 if (!new_node) {
4935 ret = -ENOMEM;
4936 goto out;
4937 }
4938 binder_node_lock(new_node);
4939 new_node->local_weak_refs++;
4940 new_node->local_strong_refs++;
4941 new_node->has_strong_ref = 1;
4942 new_node->has_weak_ref = 1;
4943 context->binder_context_mgr_node = new_node;
4944 binder_node_unlock(new_node);
4945 binder_put_node(new_node);
4946out:
4947 mutex_unlock(&context->context_mgr_node_lock);
4948 return ret;
4949}
4950
David Brazdil0f672f62019-12-10 10:32:29 +00004951static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4952 struct binder_node_info_for_ref *info)
4953{
4954 struct binder_node *node;
4955 struct binder_context *context = proc->context;
4956 __u32 handle = info->handle;
4957
4958 if (info->strong_count || info->weak_count || info->reserved1 ||
4959 info->reserved2 || info->reserved3) {
4960 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4961 proc->pid);
4962 return -EINVAL;
4963 }
4964
4965 /* This ioctl may only be used by the context manager */
4966 mutex_lock(&context->context_mgr_node_lock);
4967 if (!context->binder_context_mgr_node ||
4968 context->binder_context_mgr_node->proc != proc) {
4969 mutex_unlock(&context->context_mgr_node_lock);
4970 return -EPERM;
4971 }
4972 mutex_unlock(&context->context_mgr_node_lock);
4973
4974 node = binder_get_node_from_ref(proc, handle, true, NULL);
4975 if (!node)
4976 return -EINVAL;
4977
4978 info->strong_count = node->local_strong_refs +
4979 node->internal_strong_refs;
4980 info->weak_count = node->local_weak_refs;
4981
4982 binder_put_node(node);
4983
4984 return 0;
4985}
4986
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004987static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4988 struct binder_node_debug_info *info)
4989{
4990 struct rb_node *n;
4991 binder_uintptr_t ptr = info->ptr;
4992
4993 memset(info, 0, sizeof(*info));
4994
4995 binder_inner_proc_lock(proc);
4996 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4997 struct binder_node *node = rb_entry(n, struct binder_node,
4998 rb_node);
4999 if (node->ptr > ptr) {
5000 info->ptr = node->ptr;
5001 info->cookie = node->cookie;
5002 info->has_strong_ref = node->has_strong_ref;
5003 info->has_weak_ref = node->has_weak_ref;
5004 break;
5005 }
5006 }
5007 binder_inner_proc_unlock(proc);
5008
5009 return 0;
5010}
5011
5012static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5013{
5014 int ret;
5015 struct binder_proc *proc = filp->private_data;
5016 struct binder_thread *thread;
5017 unsigned int size = _IOC_SIZE(cmd);
5018 void __user *ubuf = (void __user *)arg;
5019
5020 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5021 proc->pid, current->pid, cmd, arg);*/
5022
5023 binder_selftest_alloc(&proc->alloc);
5024
5025 trace_binder_ioctl(cmd, arg);
5026
5027 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5028 if (ret)
5029 goto err_unlocked;
5030
5031 thread = binder_get_thread(proc);
5032 if (thread == NULL) {
5033 ret = -ENOMEM;
5034 goto err;
5035 }
5036
5037 switch (cmd) {
5038 case BINDER_WRITE_READ:
5039 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
5040 if (ret)
5041 goto err;
5042 break;
5043 case BINDER_SET_MAX_THREADS: {
5044 int max_threads;
5045
5046 if (copy_from_user(&max_threads, ubuf,
5047 sizeof(max_threads))) {
5048 ret = -EINVAL;
5049 goto err;
5050 }
5051 binder_inner_proc_lock(proc);
5052 proc->max_threads = max_threads;
5053 binder_inner_proc_unlock(proc);
5054 break;
5055 }
David Brazdil0f672f62019-12-10 10:32:29 +00005056 case BINDER_SET_CONTEXT_MGR_EXT: {
5057 struct flat_binder_object fbo;
5058
5059 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5060 ret = -EINVAL;
5061 goto err;
5062 }
5063 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5064 if (ret)
5065 goto err;
5066 break;
5067 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005068 case BINDER_SET_CONTEXT_MGR:
David Brazdil0f672f62019-12-10 10:32:29 +00005069 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005070 if (ret)
5071 goto err;
5072 break;
5073 case BINDER_THREAD_EXIT:
5074 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
5075 proc->pid, thread->pid);
5076 binder_thread_release(proc, thread);
5077 thread = NULL;
5078 break;
5079 case BINDER_VERSION: {
5080 struct binder_version __user *ver = ubuf;
5081
5082 if (size != sizeof(struct binder_version)) {
5083 ret = -EINVAL;
5084 goto err;
5085 }
5086 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5087 &ver->protocol_version)) {
5088 ret = -EINVAL;
5089 goto err;
5090 }
5091 break;
5092 }
David Brazdil0f672f62019-12-10 10:32:29 +00005093 case BINDER_GET_NODE_INFO_FOR_REF: {
5094 struct binder_node_info_for_ref info;
5095
5096 if (copy_from_user(&info, ubuf, sizeof(info))) {
5097 ret = -EFAULT;
5098 goto err;
5099 }
5100
5101 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5102 if (ret < 0)
5103 goto err;
5104
5105 if (copy_to_user(ubuf, &info, sizeof(info))) {
5106 ret = -EFAULT;
5107 goto err;
5108 }
5109
5110 break;
5111 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005112 case BINDER_GET_NODE_DEBUG_INFO: {
5113 struct binder_node_debug_info info;
5114
5115 if (copy_from_user(&info, ubuf, sizeof(info))) {
5116 ret = -EFAULT;
5117 goto err;
5118 }
5119
5120 ret = binder_ioctl_get_node_debug_info(proc, &info);
5121 if (ret < 0)
5122 goto err;
5123
5124 if (copy_to_user(ubuf, &info, sizeof(info))) {
5125 ret = -EFAULT;
5126 goto err;
5127 }
5128 break;
5129 }
5130 default:
5131 ret = -EINVAL;
5132 goto err;
5133 }
5134 ret = 0;
5135err:
5136 if (thread)
5137 thread->looper_need_return = false;
5138 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5139 if (ret && ret != -ERESTARTSYS)
5140 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
5141err_unlocked:
5142 trace_binder_ioctl_done(ret);
5143 return ret;
5144}
5145
5146static void binder_vma_open(struct vm_area_struct *vma)
5147{
5148 struct binder_proc *proc = vma->vm_private_data;
5149
5150 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5151 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5152 proc->pid, vma->vm_start, vma->vm_end,
5153 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5154 (unsigned long)pgprot_val(vma->vm_page_prot));
5155}
5156
5157static void binder_vma_close(struct vm_area_struct *vma)
5158{
5159 struct binder_proc *proc = vma->vm_private_data;
5160
5161 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5162 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5163 proc->pid, vma->vm_start, vma->vm_end,
5164 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5165 (unsigned long)pgprot_val(vma->vm_page_prot));
5166 binder_alloc_vma_close(&proc->alloc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005167}
5168
5169static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
5170{
5171 return VM_FAULT_SIGBUS;
5172}
5173
5174static const struct vm_operations_struct binder_vm_ops = {
5175 .open = binder_vma_open,
5176 .close = binder_vma_close,
5177 .fault = binder_vm_fault,
5178};
5179
5180static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5181{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005182 struct binder_proc *proc = filp->private_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005183
5184 if (proc->tsk != current->group_leader)
5185 return -EINVAL;
5186
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005187 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5188 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5189 __func__, proc->pid, vma->vm_start, vma->vm_end,
5190 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5191 (unsigned long)pgprot_val(vma->vm_page_prot));
5192
5193 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
Olivier Deprez157378f2022-04-04 15:47:50 +02005194 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
5195 proc->pid, vma->vm_start, vma->vm_end, "bad vm_flags", -EPERM);
5196 return -EPERM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005197 }
5198 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5199 vma->vm_flags &= ~VM_MAYWRITE;
5200
5201 vma->vm_ops = &binder_vm_ops;
5202 vma->vm_private_data = proc;
5203
Olivier Deprez157378f2022-04-04 15:47:50 +02005204 return binder_alloc_mmap_handler(&proc->alloc, vma);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005205}
5206
5207static int binder_open(struct inode *nodp, struct file *filp)
5208{
Olivier Deprez0e641232021-09-23 10:07:05 +02005209 struct binder_proc *proc, *itr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005210 struct binder_device *binder_dev;
David Brazdil0f672f62019-12-10 10:32:29 +00005211 struct binderfs_info *info;
5212 struct dentry *binder_binderfs_dir_entry_proc = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +02005213 bool existing_pid = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005214
5215 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
5216 current->group_leader->pid, current->pid);
5217
5218 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5219 if (proc == NULL)
5220 return -ENOMEM;
5221 spin_lock_init(&proc->inner_lock);
5222 spin_lock_init(&proc->outer_lock);
5223 get_task_struct(current->group_leader);
5224 proc->tsk = current->group_leader;
Olivier Deprez157378f2022-04-04 15:47:50 +02005225 proc->cred = get_cred(filp->f_cred);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005226 INIT_LIST_HEAD(&proc->todo);
5227 proc->default_priority = task_nice(current);
David Brazdil0f672f62019-12-10 10:32:29 +00005228 /* binderfs stashes devices in i_private */
5229 if (is_binderfs_device(nodp)) {
5230 binder_dev = nodp->i_private;
5231 info = nodp->i_sb->s_fs_info;
5232 binder_binderfs_dir_entry_proc = info->proc_log_dir;
5233 } else {
5234 binder_dev = container_of(filp->private_data,
5235 struct binder_device, miscdev);
5236 }
Olivier Deprez0e641232021-09-23 10:07:05 +02005237 refcount_inc(&binder_dev->ref);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005238 proc->context = &binder_dev->context;
5239 binder_alloc_init(&proc->alloc);
5240
5241 binder_stats_created(BINDER_STAT_PROC);
5242 proc->pid = current->group_leader->pid;
5243 INIT_LIST_HEAD(&proc->delivered_death);
5244 INIT_LIST_HEAD(&proc->waiting_threads);
5245 filp->private_data = proc;
5246
5247 mutex_lock(&binder_procs_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02005248 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5249 if (itr->pid == proc->pid) {
5250 existing_pid = true;
5251 break;
5252 }
5253 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005254 hlist_add_head(&proc->proc_node, &binder_procs);
5255 mutex_unlock(&binder_procs_lock);
5256
Olivier Deprez0e641232021-09-23 10:07:05 +02005257 if (binder_debugfs_dir_entry_proc && !existing_pid) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005258 char strbuf[11];
5259
5260 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5261 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02005262 * proc debug entries are shared between contexts.
5263 * Only create for the first PID to avoid debugfs log spamming
5264 * The printing code will anyway print all contexts for a given
5265 * PID so this is not a problem.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005266 */
5267 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
5268 binder_debugfs_dir_entry_proc,
5269 (void *)(unsigned long)proc->pid,
David Brazdil0f672f62019-12-10 10:32:29 +00005270 &proc_fops);
5271 }
5272
Olivier Deprez0e641232021-09-23 10:07:05 +02005273 if (binder_binderfs_dir_entry_proc && !existing_pid) {
David Brazdil0f672f62019-12-10 10:32:29 +00005274 char strbuf[11];
5275 struct dentry *binderfs_entry;
5276
5277 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5278 /*
5279 * Similar to debugfs, the process specific log file is shared
Olivier Deprez0e641232021-09-23 10:07:05 +02005280 * between contexts. Only create for the first PID.
5281 * This is ok since same as debugfs, the log file will contain
5282 * information on all contexts of a given PID.
David Brazdil0f672f62019-12-10 10:32:29 +00005283 */
5284 binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc,
5285 strbuf, &proc_fops, (void *)(unsigned long)proc->pid);
5286 if (!IS_ERR(binderfs_entry)) {
5287 proc->binderfs_entry = binderfs_entry;
5288 } else {
5289 int error;
5290
5291 error = PTR_ERR(binderfs_entry);
Olivier Deprez0e641232021-09-23 10:07:05 +02005292 pr_warn("Unable to create file %s in binderfs (error %d)\n",
5293 strbuf, error);
David Brazdil0f672f62019-12-10 10:32:29 +00005294 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005295 }
5296
5297 return 0;
5298}
5299
5300static int binder_flush(struct file *filp, fl_owner_t id)
5301{
5302 struct binder_proc *proc = filp->private_data;
5303
5304 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5305
5306 return 0;
5307}
5308
5309static void binder_deferred_flush(struct binder_proc *proc)
5310{
5311 struct rb_node *n;
5312 int wake_count = 0;
5313
5314 binder_inner_proc_lock(proc);
5315 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5316 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
5317
5318 thread->looper_need_return = true;
5319 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5320 wake_up_interruptible(&thread->wait);
5321 wake_count++;
5322 }
5323 }
5324 binder_inner_proc_unlock(proc);
5325
5326 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5327 "binder_flush: %d woke %d threads\n", proc->pid,
5328 wake_count);
5329}
5330
5331static int binder_release(struct inode *nodp, struct file *filp)
5332{
5333 struct binder_proc *proc = filp->private_data;
5334
5335 debugfs_remove(proc->debugfs_entry);
David Brazdil0f672f62019-12-10 10:32:29 +00005336
5337 if (proc->binderfs_entry) {
5338 binderfs_remove_file(proc->binderfs_entry);
5339 proc->binderfs_entry = NULL;
5340 }
5341
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005342 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5343
5344 return 0;
5345}
5346
5347static int binder_node_release(struct binder_node *node, int refs)
5348{
5349 struct binder_ref *ref;
5350 int death = 0;
5351 struct binder_proc *proc = node->proc;
5352
5353 binder_release_work(proc, &node->async_todo);
5354
5355 binder_node_lock(node);
5356 binder_inner_proc_lock(proc);
5357 binder_dequeue_work_ilocked(&node->work);
5358 /*
5359 * The caller must have taken a temporary ref on the node,
5360 */
5361 BUG_ON(!node->tmp_refs);
5362 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
5363 binder_inner_proc_unlock(proc);
5364 binder_node_unlock(node);
5365 binder_free_node(node);
5366
5367 return refs;
5368 }
5369
5370 node->proc = NULL;
5371 node->local_strong_refs = 0;
5372 node->local_weak_refs = 0;
5373 binder_inner_proc_unlock(proc);
5374
5375 spin_lock(&binder_dead_nodes_lock);
5376 hlist_add_head(&node->dead_node, &binder_dead_nodes);
5377 spin_unlock(&binder_dead_nodes_lock);
5378
5379 hlist_for_each_entry(ref, &node->refs, node_entry) {
5380 refs++;
5381 /*
5382 * Need the node lock to synchronize
5383 * with new notification requests and the
5384 * inner lock to synchronize with queued
5385 * death notifications.
5386 */
5387 binder_inner_proc_lock(ref->proc);
5388 if (!ref->death) {
5389 binder_inner_proc_unlock(ref->proc);
5390 continue;
5391 }
5392
5393 death++;
5394
5395 BUG_ON(!list_empty(&ref->death->work.entry));
5396 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5397 binder_enqueue_work_ilocked(&ref->death->work,
5398 &ref->proc->todo);
5399 binder_wakeup_proc_ilocked(ref->proc);
5400 binder_inner_proc_unlock(ref->proc);
5401 }
5402
5403 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5404 "node %d now dead, refs %d, death %d\n",
5405 node->debug_id, refs, death);
5406 binder_node_unlock(node);
5407 binder_put_node(node);
5408
5409 return refs;
5410}
5411
5412static void binder_deferred_release(struct binder_proc *proc)
5413{
5414 struct binder_context *context = proc->context;
5415 struct rb_node *n;
5416 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
5417
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005418 mutex_lock(&binder_procs_lock);
5419 hlist_del(&proc->proc_node);
5420 mutex_unlock(&binder_procs_lock);
5421
5422 mutex_lock(&context->context_mgr_node_lock);
5423 if (context->binder_context_mgr_node &&
5424 context->binder_context_mgr_node->proc == proc) {
5425 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5426 "%s: %d context_mgr_node gone\n",
5427 __func__, proc->pid);
5428 context->binder_context_mgr_node = NULL;
5429 }
5430 mutex_unlock(&context->context_mgr_node_lock);
5431 binder_inner_proc_lock(proc);
5432 /*
5433 * Make sure proc stays alive after we
5434 * remove all the threads
5435 */
5436 proc->tmp_ref++;
5437
5438 proc->is_dead = true;
5439 threads = 0;
5440 active_transactions = 0;
5441 while ((n = rb_first(&proc->threads))) {
5442 struct binder_thread *thread;
5443
5444 thread = rb_entry(n, struct binder_thread, rb_node);
5445 binder_inner_proc_unlock(proc);
5446 threads++;
5447 active_transactions += binder_thread_release(proc, thread);
5448 binder_inner_proc_lock(proc);
5449 }
5450
5451 nodes = 0;
5452 incoming_refs = 0;
5453 while ((n = rb_first(&proc->nodes))) {
5454 struct binder_node *node;
5455
5456 node = rb_entry(n, struct binder_node, rb_node);
5457 nodes++;
5458 /*
5459 * take a temporary ref on the node before
5460 * calling binder_node_release() which will either
5461 * kfree() the node or call binder_put_node()
5462 */
5463 binder_inc_node_tmpref_ilocked(node);
5464 rb_erase(&node->rb_node, &proc->nodes);
5465 binder_inner_proc_unlock(proc);
5466 incoming_refs = binder_node_release(node, incoming_refs);
5467 binder_inner_proc_lock(proc);
5468 }
5469 binder_inner_proc_unlock(proc);
5470
5471 outgoing_refs = 0;
5472 binder_proc_lock(proc);
5473 while ((n = rb_first(&proc->refs_by_desc))) {
5474 struct binder_ref *ref;
5475
5476 ref = rb_entry(n, struct binder_ref, rb_node_desc);
5477 outgoing_refs++;
5478 binder_cleanup_ref_olocked(ref);
5479 binder_proc_unlock(proc);
5480 binder_free_ref(ref);
5481 binder_proc_lock(proc);
5482 }
5483 binder_proc_unlock(proc);
5484
5485 binder_release_work(proc, &proc->todo);
5486 binder_release_work(proc, &proc->delivered_death);
5487
5488 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5489 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5490 __func__, proc->pid, threads, nodes, incoming_refs,
5491 outgoing_refs, active_transactions);
5492
5493 binder_proc_dec_tmpref(proc);
5494}
5495
5496static void binder_deferred_func(struct work_struct *work)
5497{
5498 struct binder_proc *proc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005499
5500 int defer;
5501
5502 do {
5503 mutex_lock(&binder_deferred_lock);
5504 if (!hlist_empty(&binder_deferred_list)) {
5505 proc = hlist_entry(binder_deferred_list.first,
5506 struct binder_proc, deferred_work_node);
5507 hlist_del_init(&proc->deferred_work_node);
5508 defer = proc->deferred_work;
5509 proc->deferred_work = 0;
5510 } else {
5511 proc = NULL;
5512 defer = 0;
5513 }
5514 mutex_unlock(&binder_deferred_lock);
5515
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005516 if (defer & BINDER_DEFERRED_FLUSH)
5517 binder_deferred_flush(proc);
5518
5519 if (defer & BINDER_DEFERRED_RELEASE)
5520 binder_deferred_release(proc); /* frees proc */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005521 } while (proc);
5522}
5523static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5524
5525static void
5526binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5527{
5528 mutex_lock(&binder_deferred_lock);
5529 proc->deferred_work |= defer;
5530 if (hlist_unhashed(&proc->deferred_work_node)) {
5531 hlist_add_head(&proc->deferred_work_node,
5532 &binder_deferred_list);
5533 schedule_work(&binder_deferred_work);
5534 }
5535 mutex_unlock(&binder_deferred_lock);
5536}
5537
5538static void print_binder_transaction_ilocked(struct seq_file *m,
5539 struct binder_proc *proc,
5540 const char *prefix,
5541 struct binder_transaction *t)
5542{
5543 struct binder_proc *to_proc;
5544 struct binder_buffer *buffer = t->buffer;
5545
5546 spin_lock(&t->lock);
5547 to_proc = t->to_proc;
5548 seq_printf(m,
5549 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
5550 prefix, t->debug_id, t,
5551 t->from ? t->from->proc->pid : 0,
5552 t->from ? t->from->pid : 0,
5553 to_proc ? to_proc->pid : 0,
5554 t->to_thread ? t->to_thread->pid : 0,
5555 t->code, t->flags, t->priority, t->need_reply);
5556 spin_unlock(&t->lock);
5557
5558 if (proc != to_proc) {
5559 /*
5560 * Can only safely deref buffer if we are holding the
5561 * correct proc inner lock for this node
5562 */
5563 seq_puts(m, "\n");
5564 return;
5565 }
5566
5567 if (buffer == NULL) {
5568 seq_puts(m, " buffer free\n");
5569 return;
5570 }
5571 if (buffer->target_node)
5572 seq_printf(m, " node %d", buffer->target_node->debug_id);
5573 seq_printf(m, " size %zd:%zd data %pK\n",
5574 buffer->data_size, buffer->offsets_size,
David Brazdil0f672f62019-12-10 10:32:29 +00005575 buffer->user_data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005576}
5577
5578static void print_binder_work_ilocked(struct seq_file *m,
5579 struct binder_proc *proc,
5580 const char *prefix,
5581 const char *transaction_prefix,
5582 struct binder_work *w)
5583{
5584 struct binder_node *node;
5585 struct binder_transaction *t;
5586
5587 switch (w->type) {
5588 case BINDER_WORK_TRANSACTION:
5589 t = container_of(w, struct binder_transaction, work);
5590 print_binder_transaction_ilocked(
5591 m, proc, transaction_prefix, t);
5592 break;
5593 case BINDER_WORK_RETURN_ERROR: {
5594 struct binder_error *e = container_of(
5595 w, struct binder_error, work);
5596
5597 seq_printf(m, "%stransaction error: %u\n",
5598 prefix, e->cmd);
5599 } break;
5600 case BINDER_WORK_TRANSACTION_COMPLETE:
5601 seq_printf(m, "%stransaction complete\n", prefix);
5602 break;
5603 case BINDER_WORK_NODE:
5604 node = container_of(w, struct binder_node, work);
5605 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5606 prefix, node->debug_id,
5607 (u64)node->ptr, (u64)node->cookie);
5608 break;
5609 case BINDER_WORK_DEAD_BINDER:
5610 seq_printf(m, "%shas dead binder\n", prefix);
5611 break;
5612 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5613 seq_printf(m, "%shas cleared dead binder\n", prefix);
5614 break;
5615 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5616 seq_printf(m, "%shas cleared death notification\n", prefix);
5617 break;
5618 default:
5619 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5620 break;
5621 }
5622}
5623
5624static void print_binder_thread_ilocked(struct seq_file *m,
5625 struct binder_thread *thread,
5626 int print_always)
5627{
5628 struct binder_transaction *t;
5629 struct binder_work *w;
5630 size_t start_pos = m->count;
5631 size_t header_pos;
5632
5633 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5634 thread->pid, thread->looper,
5635 thread->looper_need_return,
5636 atomic_read(&thread->tmp_ref));
5637 header_pos = m->count;
5638 t = thread->transaction_stack;
5639 while (t) {
5640 if (t->from == thread) {
5641 print_binder_transaction_ilocked(m, thread->proc,
5642 " outgoing transaction", t);
5643 t = t->from_parent;
5644 } else if (t->to_thread == thread) {
5645 print_binder_transaction_ilocked(m, thread->proc,
5646 " incoming transaction", t);
5647 t = t->to_parent;
5648 } else {
5649 print_binder_transaction_ilocked(m, thread->proc,
5650 " bad transaction", t);
5651 t = NULL;
5652 }
5653 }
5654 list_for_each_entry(w, &thread->todo, entry) {
5655 print_binder_work_ilocked(m, thread->proc, " ",
5656 " pending transaction", w);
5657 }
5658 if (!print_always && m->count == header_pos)
5659 m->count = start_pos;
5660}
5661
5662static void print_binder_node_nilocked(struct seq_file *m,
5663 struct binder_node *node)
5664{
5665 struct binder_ref *ref;
5666 struct binder_work *w;
5667 int count;
5668
5669 count = 0;
5670 hlist_for_each_entry(ref, &node->refs, node_entry)
5671 count++;
5672
5673 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5674 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5675 node->has_strong_ref, node->has_weak_ref,
5676 node->local_strong_refs, node->local_weak_refs,
5677 node->internal_strong_refs, count, node->tmp_refs);
5678 if (count) {
5679 seq_puts(m, " proc");
5680 hlist_for_each_entry(ref, &node->refs, node_entry)
5681 seq_printf(m, " %d", ref->proc->pid);
5682 }
5683 seq_puts(m, "\n");
5684 if (node->proc) {
5685 list_for_each_entry(w, &node->async_todo, entry)
5686 print_binder_work_ilocked(m, node->proc, " ",
5687 " pending async transaction", w);
5688 }
5689}
5690
5691static void print_binder_ref_olocked(struct seq_file *m,
5692 struct binder_ref *ref)
5693{
5694 binder_node_lock(ref->node);
5695 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5696 ref->data.debug_id, ref->data.desc,
5697 ref->node->proc ? "" : "dead ",
5698 ref->node->debug_id, ref->data.strong,
5699 ref->data.weak, ref->death);
5700 binder_node_unlock(ref->node);
5701}
5702
5703static void print_binder_proc(struct seq_file *m,
5704 struct binder_proc *proc, int print_all)
5705{
5706 struct binder_work *w;
5707 struct rb_node *n;
5708 size_t start_pos = m->count;
5709 size_t header_pos;
5710 struct binder_node *last_node = NULL;
5711
5712 seq_printf(m, "proc %d\n", proc->pid);
5713 seq_printf(m, "context %s\n", proc->context->name);
5714 header_pos = m->count;
5715
5716 binder_inner_proc_lock(proc);
5717 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5718 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5719 rb_node), print_all);
5720
5721 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5722 struct binder_node *node = rb_entry(n, struct binder_node,
5723 rb_node);
David Brazdil0f672f62019-12-10 10:32:29 +00005724 if (!print_all && !node->has_async_transaction)
5725 continue;
5726
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005727 /*
5728 * take a temporary reference on the node so it
5729 * survives and isn't removed from the tree
5730 * while we print it.
5731 */
5732 binder_inc_node_tmpref_ilocked(node);
5733 /* Need to drop inner lock to take node lock */
5734 binder_inner_proc_unlock(proc);
5735 if (last_node)
5736 binder_put_node(last_node);
5737 binder_node_inner_lock(node);
5738 print_binder_node_nilocked(m, node);
5739 binder_node_inner_unlock(node);
5740 last_node = node;
5741 binder_inner_proc_lock(proc);
5742 }
5743 binder_inner_proc_unlock(proc);
5744 if (last_node)
5745 binder_put_node(last_node);
5746
5747 if (print_all) {
5748 binder_proc_lock(proc);
5749 for (n = rb_first(&proc->refs_by_desc);
5750 n != NULL;
5751 n = rb_next(n))
5752 print_binder_ref_olocked(m, rb_entry(n,
5753 struct binder_ref,
5754 rb_node_desc));
5755 binder_proc_unlock(proc);
5756 }
5757 binder_alloc_print_allocated(m, &proc->alloc);
5758 binder_inner_proc_lock(proc);
5759 list_for_each_entry(w, &proc->todo, entry)
5760 print_binder_work_ilocked(m, proc, " ",
5761 " pending transaction", w);
5762 list_for_each_entry(w, &proc->delivered_death, entry) {
5763 seq_puts(m, " has delivered dead binder\n");
5764 break;
5765 }
5766 binder_inner_proc_unlock(proc);
5767 if (!print_all && m->count == header_pos)
5768 m->count = start_pos;
5769}
5770
5771static const char * const binder_return_strings[] = {
5772 "BR_ERROR",
5773 "BR_OK",
5774 "BR_TRANSACTION",
5775 "BR_REPLY",
5776 "BR_ACQUIRE_RESULT",
5777 "BR_DEAD_REPLY",
5778 "BR_TRANSACTION_COMPLETE",
5779 "BR_INCREFS",
5780 "BR_ACQUIRE",
5781 "BR_RELEASE",
5782 "BR_DECREFS",
5783 "BR_ATTEMPT_ACQUIRE",
5784 "BR_NOOP",
5785 "BR_SPAWN_LOOPER",
5786 "BR_FINISHED",
5787 "BR_DEAD_BINDER",
5788 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5789 "BR_FAILED_REPLY"
5790};
5791
5792static const char * const binder_command_strings[] = {
5793 "BC_TRANSACTION",
5794 "BC_REPLY",
5795 "BC_ACQUIRE_RESULT",
5796 "BC_FREE_BUFFER",
5797 "BC_INCREFS",
5798 "BC_ACQUIRE",
5799 "BC_RELEASE",
5800 "BC_DECREFS",
5801 "BC_INCREFS_DONE",
5802 "BC_ACQUIRE_DONE",
5803 "BC_ATTEMPT_ACQUIRE",
5804 "BC_REGISTER_LOOPER",
5805 "BC_ENTER_LOOPER",
5806 "BC_EXIT_LOOPER",
5807 "BC_REQUEST_DEATH_NOTIFICATION",
5808 "BC_CLEAR_DEATH_NOTIFICATION",
5809 "BC_DEAD_BINDER_DONE",
5810 "BC_TRANSACTION_SG",
5811 "BC_REPLY_SG",
5812};
5813
5814static const char * const binder_objstat_strings[] = {
5815 "proc",
5816 "thread",
5817 "node",
5818 "ref",
5819 "death",
5820 "transaction",
5821 "transaction_complete"
5822};
5823
5824static void print_binder_stats(struct seq_file *m, const char *prefix,
5825 struct binder_stats *stats)
5826{
5827 int i;
5828
5829 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5830 ARRAY_SIZE(binder_command_strings));
5831 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
5832 int temp = atomic_read(&stats->bc[i]);
5833
5834 if (temp)
5835 seq_printf(m, "%s%s: %d\n", prefix,
5836 binder_command_strings[i], temp);
5837 }
5838
5839 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5840 ARRAY_SIZE(binder_return_strings));
5841 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
5842 int temp = atomic_read(&stats->br[i]);
5843
5844 if (temp)
5845 seq_printf(m, "%s%s: %d\n", prefix,
5846 binder_return_strings[i], temp);
5847 }
5848
5849 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5850 ARRAY_SIZE(binder_objstat_strings));
5851 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5852 ARRAY_SIZE(stats->obj_deleted));
5853 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
5854 int created = atomic_read(&stats->obj_created[i]);
5855 int deleted = atomic_read(&stats->obj_deleted[i]);
5856
5857 if (created || deleted)
5858 seq_printf(m, "%s%s: active %d total %d\n",
5859 prefix,
5860 binder_objstat_strings[i],
5861 created - deleted,
5862 created);
5863 }
5864}
5865
5866static void print_binder_proc_stats(struct seq_file *m,
5867 struct binder_proc *proc)
5868{
5869 struct binder_work *w;
5870 struct binder_thread *thread;
5871 struct rb_node *n;
5872 int count, strong, weak, ready_threads;
5873 size_t free_async_space =
5874 binder_alloc_get_free_async_space(&proc->alloc);
5875
5876 seq_printf(m, "proc %d\n", proc->pid);
5877 seq_printf(m, "context %s\n", proc->context->name);
5878 count = 0;
5879 ready_threads = 0;
5880 binder_inner_proc_lock(proc);
5881 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5882 count++;
5883
5884 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5885 ready_threads++;
5886
5887 seq_printf(m, " threads: %d\n", count);
5888 seq_printf(m, " requested threads: %d+%d/%d\n"
5889 " ready threads %d\n"
5890 " free async space %zd\n", proc->requested_threads,
5891 proc->requested_threads_started, proc->max_threads,
5892 ready_threads,
5893 free_async_space);
5894 count = 0;
5895 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5896 count++;
5897 binder_inner_proc_unlock(proc);
5898 seq_printf(m, " nodes: %d\n", count);
5899 count = 0;
5900 strong = 0;
5901 weak = 0;
5902 binder_proc_lock(proc);
5903 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5904 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5905 rb_node_desc);
5906 count++;
5907 strong += ref->data.strong;
5908 weak += ref->data.weak;
5909 }
5910 binder_proc_unlock(proc);
5911 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
5912
5913 count = binder_alloc_get_allocated_count(&proc->alloc);
5914 seq_printf(m, " buffers: %d\n", count);
5915
5916 binder_alloc_print_pages(m, &proc->alloc);
5917
5918 count = 0;
5919 binder_inner_proc_lock(proc);
5920 list_for_each_entry(w, &proc->todo, entry) {
5921 if (w->type == BINDER_WORK_TRANSACTION)
5922 count++;
5923 }
5924 binder_inner_proc_unlock(proc);
5925 seq_printf(m, " pending transactions: %d\n", count);
5926
5927 print_binder_stats(m, " ", &proc->stats);
5928}
5929
5930
David Brazdil0f672f62019-12-10 10:32:29 +00005931int binder_state_show(struct seq_file *m, void *unused)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005932{
5933 struct binder_proc *proc;
5934 struct binder_node *node;
5935 struct binder_node *last_node = NULL;
5936
5937 seq_puts(m, "binder state:\n");
5938
5939 spin_lock(&binder_dead_nodes_lock);
5940 if (!hlist_empty(&binder_dead_nodes))
5941 seq_puts(m, "dead nodes:\n");
5942 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5943 /*
5944 * take a temporary reference on the node so it
5945 * survives and isn't removed from the list
5946 * while we print it.
5947 */
5948 node->tmp_refs++;
5949 spin_unlock(&binder_dead_nodes_lock);
5950 if (last_node)
5951 binder_put_node(last_node);
5952 binder_node_lock(node);
5953 print_binder_node_nilocked(m, node);
5954 binder_node_unlock(node);
5955 last_node = node;
5956 spin_lock(&binder_dead_nodes_lock);
5957 }
5958 spin_unlock(&binder_dead_nodes_lock);
5959 if (last_node)
5960 binder_put_node(last_node);
5961
5962 mutex_lock(&binder_procs_lock);
5963 hlist_for_each_entry(proc, &binder_procs, proc_node)
5964 print_binder_proc(m, proc, 1);
5965 mutex_unlock(&binder_procs_lock);
5966
5967 return 0;
5968}
5969
David Brazdil0f672f62019-12-10 10:32:29 +00005970int binder_stats_show(struct seq_file *m, void *unused)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005971{
5972 struct binder_proc *proc;
5973
5974 seq_puts(m, "binder stats:\n");
5975
5976 print_binder_stats(m, "", &binder_stats);
5977
5978 mutex_lock(&binder_procs_lock);
5979 hlist_for_each_entry(proc, &binder_procs, proc_node)
5980 print_binder_proc_stats(m, proc);
5981 mutex_unlock(&binder_procs_lock);
5982
5983 return 0;
5984}
5985
David Brazdil0f672f62019-12-10 10:32:29 +00005986int binder_transactions_show(struct seq_file *m, void *unused)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005987{
5988 struct binder_proc *proc;
5989
5990 seq_puts(m, "binder transactions:\n");
5991 mutex_lock(&binder_procs_lock);
5992 hlist_for_each_entry(proc, &binder_procs, proc_node)
5993 print_binder_proc(m, proc, 0);
5994 mutex_unlock(&binder_procs_lock);
5995
5996 return 0;
5997}
5998
David Brazdil0f672f62019-12-10 10:32:29 +00005999static int proc_show(struct seq_file *m, void *unused)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006000{
6001 struct binder_proc *itr;
6002 int pid = (unsigned long)m->private;
6003
6004 mutex_lock(&binder_procs_lock);
6005 hlist_for_each_entry(itr, &binder_procs, proc_node) {
6006 if (itr->pid == pid) {
6007 seq_puts(m, "binder proc state:\n");
6008 print_binder_proc(m, itr, 1);
6009 }
6010 }
6011 mutex_unlock(&binder_procs_lock);
6012
6013 return 0;
6014}
6015
6016static void print_binder_transaction_log_entry(struct seq_file *m,
6017 struct binder_transaction_log_entry *e)
6018{
6019 int debug_id = READ_ONCE(e->debug_id_done);
6020 /*
6021 * read barrier to guarantee debug_id_done read before
6022 * we print the log values
6023 */
6024 smp_rmb();
6025 seq_printf(m,
6026 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
6027 e->debug_id, (e->call_type == 2) ? "reply" :
6028 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
6029 e->from_thread, e->to_proc, e->to_thread, e->context_name,
6030 e->to_node, e->target_handle, e->data_size, e->offsets_size,
6031 e->return_error, e->return_error_param,
6032 e->return_error_line);
6033 /*
6034 * read-barrier to guarantee read of debug_id_done after
6035 * done printing the fields of the entry
6036 */
6037 smp_rmb();
6038 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6039 "\n" : " (incomplete)\n");
6040}
6041
David Brazdil0f672f62019-12-10 10:32:29 +00006042int binder_transaction_log_show(struct seq_file *m, void *unused)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006043{
6044 struct binder_transaction_log *log = m->private;
6045 unsigned int log_cur = atomic_read(&log->cur);
6046 unsigned int count;
6047 unsigned int cur;
6048 int i;
6049
6050 count = log_cur + 1;
6051 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6052 0 : count % ARRAY_SIZE(log->entry);
6053 if (count > ARRAY_SIZE(log->entry) || log->full)
6054 count = ARRAY_SIZE(log->entry);
6055 for (i = 0; i < count; i++) {
6056 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6057
6058 print_binder_transaction_log_entry(m, &log->entry[index]);
6059 }
6060 return 0;
6061}
6062
David Brazdil0f672f62019-12-10 10:32:29 +00006063const struct file_operations binder_fops = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006064 .owner = THIS_MODULE,
6065 .poll = binder_poll,
6066 .unlocked_ioctl = binder_ioctl,
Olivier Deprez157378f2022-04-04 15:47:50 +02006067 .compat_ioctl = compat_ptr_ioctl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006068 .mmap = binder_mmap,
6069 .open = binder_open,
6070 .flush = binder_flush,
6071 .release = binder_release,
6072};
6073
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006074static int __init init_binder_device(const char *name)
6075{
6076 int ret;
6077 struct binder_device *binder_device;
6078
6079 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6080 if (!binder_device)
6081 return -ENOMEM;
6082
6083 binder_device->miscdev.fops = &binder_fops;
6084 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6085 binder_device->miscdev.name = name;
6086
Olivier Deprez0e641232021-09-23 10:07:05 +02006087 refcount_set(&binder_device->ref, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006088 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6089 binder_device->context.name = name;
6090 mutex_init(&binder_device->context.context_mgr_node_lock);
6091
6092 ret = misc_register(&binder_device->miscdev);
6093 if (ret < 0) {
6094 kfree(binder_device);
6095 return ret;
6096 }
6097
6098 hlist_add_head(&binder_device->hlist, &binder_devices);
6099
6100 return ret;
6101}
6102
6103static int __init binder_init(void)
6104{
6105 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +00006106 char *device_name, *device_tmp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006107 struct binder_device *device;
6108 struct hlist_node *tmp;
David Brazdil0f672f62019-12-10 10:32:29 +00006109 char *device_names = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006110
6111 ret = binder_alloc_shrinker_init();
6112 if (ret)
6113 return ret;
6114
6115 atomic_set(&binder_transaction_log.cur, ~0U);
6116 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6117
6118 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6119 if (binder_debugfs_dir_entry_root)
6120 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6121 binder_debugfs_dir_entry_root);
6122
6123 if (binder_debugfs_dir_entry_root) {
6124 debugfs_create_file("state",
6125 0444,
6126 binder_debugfs_dir_entry_root,
6127 NULL,
6128 &binder_state_fops);
6129 debugfs_create_file("stats",
6130 0444,
6131 binder_debugfs_dir_entry_root,
6132 NULL,
6133 &binder_stats_fops);
6134 debugfs_create_file("transactions",
6135 0444,
6136 binder_debugfs_dir_entry_root,
6137 NULL,
6138 &binder_transactions_fops);
6139 debugfs_create_file("transaction_log",
6140 0444,
6141 binder_debugfs_dir_entry_root,
6142 &binder_transaction_log,
6143 &binder_transaction_log_fops);
6144 debugfs_create_file("failed_transaction_log",
6145 0444,
6146 binder_debugfs_dir_entry_root,
6147 &binder_transaction_log_failed,
6148 &binder_transaction_log_fops);
6149 }
6150
David Brazdil0f672f62019-12-10 10:32:29 +00006151 if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
6152 strcmp(binder_devices_param, "") != 0) {
6153 /*
6154 * Copy the module_parameter string, because we don't want to
6155 * tokenize it in-place.
6156 */
6157 device_names = kstrdup(binder_devices_param, GFP_KERNEL);
6158 if (!device_names) {
6159 ret = -ENOMEM;
6160 goto err_alloc_device_names_failed;
6161 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006162
David Brazdil0f672f62019-12-10 10:32:29 +00006163 device_tmp = device_names;
6164 while ((device_name = strsep(&device_tmp, ","))) {
6165 ret = init_binder_device(device_name);
6166 if (ret)
6167 goto err_init_binder_device_failed;
6168 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006169 }
6170
David Brazdil0f672f62019-12-10 10:32:29 +00006171 ret = init_binderfs();
6172 if (ret)
6173 goto err_init_binder_device_failed;
6174
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006175 return ret;
6176
6177err_init_binder_device_failed:
6178 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6179 misc_deregister(&device->miscdev);
6180 hlist_del(&device->hlist);
6181 kfree(device);
6182 }
6183
6184 kfree(device_names);
6185
6186err_alloc_device_names_failed:
6187 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6188
6189 return ret;
6190}
6191
6192device_initcall(binder_init);
6193
6194#define CREATE_TRACE_POINTS
6195#include "binder_trace.h"
6196
6197MODULE_LICENSE("GPL v2");