Skip to content

Commit 11512c1

Browse files
Carlos Llamasgregkh
authored andcommitted
binder: fix descriptor lookup for context manager
In commit 15d9da3 ("binder: use bitmap for faster descriptor lookup"), it was incorrectly assumed that references to the context manager node should always get descriptor zero assigned to them. However, if the context manager dies and a new process takes its place, then assigning descriptor zero to the new context manager might lead to collisions, as there could still be references to the older node. This issue was reported by syzbot with the following trace: kernel BUG at drivers/android/binder.c:1173! Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP Modules linked in: CPU: 1 PID: 447 Comm: binder-util Not tainted 6.10.0-rc6-00348-g31643d84b8c3 #10 Hardware name: linux,dummy-virt (DT) pstate: 60000005 (nZCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : binder_inc_ref_for_node+0x500/0x544 lr : binder_inc_ref_for_node+0x1e4/0x544 sp : ffff80008112b940 x29: ffff80008112b940 x28: ffff0e0e40310780 x27: 0000000000000000 x26: 0000000000000001 x25: ffff0e0e40310738 x24: ffff0e0e4089ba34 x23: ffff0e0e40310b00 x22: ffff80008112bb50 x21: ffffaf7b8f246970 x20: ffffaf7b8f773f08 x19: ffff0e0e4089b800 x18: 0000000000000000 x17: 0000000000000000 x16: 0000000000000000 x15: 000000002de4aa60 x14: 0000000000000000 x13: 2de4acf000000000 x12: 0000000000000020 x11: 0000000000000018 x10: 0000000000000020 x9 : ffffaf7b90601000 x8 : ffff0e0e48739140 x7 : 0000000000000000 x6 : 000000000000003f x5 : ffff0e0e40310b28 x4 : 0000000000000000 x3 : ffff0e0e40310720 x2 : ffff0e0e40310728 x1 : 0000000000000000 x0 : ffff0e0e40310710 Call trace: binder_inc_ref_for_node+0x500/0x544 binder_transaction+0xf68/0x2620 binder_thread_write+0x5bc/0x139c binder_ioctl+0xef4/0x10c8 [...] This patch adds back the previous behavior of assigning the next non-zero descriptor if references to previous context managers still exist. It amends both strategies, the newer dbitmap code and also the legacy slow_desc_lookup_olocked(), by allowing them to start looking for available descriptors at a given offset. Fixes: 15d9da3 ("binder: use bitmap for faster descriptor lookup") Cc: [email protected] Reported-and-tested-by: [email protected] Closes: https://lore.kernel.org/all/[email protected]/ Reviewed-by: Alice Ryhl <[email protected]> Signed-off-by: Carlos Llamas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d1009d0 commit 11512c1

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

drivers/android/binder.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,13 +1044,13 @@ static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
10441044
}
10451045

10461046
/* Find the smallest unused descriptor the "slow way" */
1047-
static u32 slow_desc_lookup_olocked(struct binder_proc *proc)
1047+
static u32 slow_desc_lookup_olocked(struct binder_proc *proc, u32 offset)
10481048
{
10491049
struct binder_ref *ref;
10501050
struct rb_node *n;
10511051
u32 desc;
10521052

1053-
desc = 1;
1053+
desc = offset;
10541054
for (n = rb_first(&proc->refs_by_desc); n; n = rb_next(n)) {
10551055
ref = rb_entry(n, struct binder_ref, rb_node_desc);
10561056
if (ref->data.desc > desc)
@@ -1071,21 +1071,18 @@ static int get_ref_desc_olocked(struct binder_proc *proc,
10711071
u32 *desc)
10721072
{
10731073
struct dbitmap *dmap = &proc->dmap;
1074+
unsigned int nbits, offset;
10741075
unsigned long *new, bit;
1075-
unsigned int nbits;
10761076

10771077
/* 0 is reserved for the context manager */
1078-
if (node == proc->context->binder_context_mgr_node) {
1079-
*desc = 0;
1080-
return 0;
1081-
}
1078+
offset = (node == proc->context->binder_context_mgr_node) ? 0 : 1;
10821079

10831080
if (!dbitmap_enabled(dmap)) {
1084-
*desc = slow_desc_lookup_olocked(proc);
1081+
*desc = slow_desc_lookup_olocked(proc, offset);
10851082
return 0;
10861083
}
10871084

1088-
if (dbitmap_acquire_first_zero_bit(dmap, &bit) == 0) {
1085+
if (dbitmap_acquire_next_zero_bit(dmap, offset, &bit) == 0) {
10891086
*desc = bit;
10901087
return 0;
10911088
}

drivers/android/dbitmap.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
*
77
* Used by the binder driver to optimize the allocation of the smallest
88
* available descriptor ID. Each bit in the bitmap represents the state
9-
* of an ID, with the exception of BIT(0) which is used exclusively to
10-
* reference binder's context manager.
9+
* of an ID.
1110
*
1211
* A dbitmap can grow or shrink as needed. This part has been designed
1312
* considering that users might need to briefly release their locks in
@@ -58,11 +57,7 @@ static inline unsigned int dbitmap_shrink_nbits(struct dbitmap *dmap)
5857
if (bit < (dmap->nbits >> 2))
5958
return dmap->nbits >> 1;
6059

61-
/*
62-
* Note that find_last_bit() returns dmap->nbits when no bits
63-
* are set. While this is technically not possible here since
64-
* BIT(0) is always set, this check is left for extra safety.
65-
*/
60+
/* find_last_bit() returns dmap->nbits when no bits are set. */
6661
if (bit == dmap->nbits)
6762
return NBITS_MIN;
6863

@@ -132,16 +127,17 @@ dbitmap_grow(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)
132127
}
133128

134129
/*
135-
* Finds and sets the first zero bit in the bitmap. Upon success @bit
130+
* Finds and sets the next zero bit in the bitmap. Upon success @bit
136131
* is populated with the index and 0 is returned. Otherwise, -ENOSPC
137132
* is returned to indicate that a dbitmap_grow() is needed.
138133
*/
139134
static inline int
140-
dbitmap_acquire_first_zero_bit(struct dbitmap *dmap, unsigned long *bit)
135+
dbitmap_acquire_next_zero_bit(struct dbitmap *dmap, unsigned long offset,
136+
unsigned long *bit)
141137
{
142138
unsigned long n;
143139

144-
n = find_first_zero_bit(dmap->map, dmap->nbits);
140+
n = find_next_zero_bit(dmap->map, dmap->nbits, offset);
145141
if (n == dmap->nbits)
146142
return -ENOSPC;
147143

@@ -154,9 +150,7 @@ dbitmap_acquire_first_zero_bit(struct dbitmap *dmap, unsigned long *bit)
154150
static inline void
155151
dbitmap_clear_bit(struct dbitmap *dmap, unsigned long bit)
156152
{
157-
/* BIT(0) should always set for the context manager */
158-
if (bit)
159-
clear_bit(bit, dmap->map);
153+
clear_bit(bit, dmap->map);
160154
}
161155

162156
static inline int dbitmap_init(struct dbitmap *dmap)
@@ -168,8 +162,6 @@ static inline int dbitmap_init(struct dbitmap *dmap)
168162
}
169163

170164
dmap->nbits = NBITS_MIN;
171-
/* BIT(0) is reserved for the context manager */
172-
set_bit(0, dmap->map);
173165

174166
return 0;
175167
}

0 commit comments

Comments
 (0)