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