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