Skip to content

Commit feba390

Browse files
Martijn Coenengregkh
authored andcommitted
binder: Split flat_binder_object
flat_binder_object is used for both handling binder objects and file descriptors, even though the two are mostly independent. Since we'll have more fixup objects in binder in the future, instead of extending flat_binder_object again, split out file descriptors to their own object while retaining backwards compatibility to existing user-space clients. All binder objects just share a header. Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Martijn Coenen <maco@google.com> Cc: Arve Hjønnevåg <arve@android.com> Cc: Amit Pundir <amit.pundir@linaro.org> Cc: Serban Constantinescu <serban.constantinescu@arm.com> Cc: Dmitry Shmidt <dimitrysh@google.com> Cc: Rom Lemarchand <romlem@google.com> Cc: Android Kernel Team <kernel-team@android.com> Signed-off-by: Martijn Coenen <maco@google.com> Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8fa8bea commit feba390

2 files changed

Lines changed: 137 additions & 52 deletions

File tree

‎drivers/android/binder.c‎

Lines changed: 109 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
145145
binder_stop_on_user_error = 2; \
146146
} while (0)
147147

148+
#define to_flat_binder_object(hdr) \
149+
container_of(hdr, struct flat_binder_object, hdr)
150+
151+
#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
152+
148153
enum binder_stat_types {
149154
BINDER_STAT_PROC,
150155
BINDER_STAT_THREAD,
@@ -1240,6 +1245,47 @@ static void binder_send_failed_reply(struct binder_transaction *t,
12401245
}
12411246
}
12421247

1248+
/**
1249+
* binder_validate_object() - checks for a valid metadata object in a buffer.
1250+
* @buffer: binder_buffer that we're parsing.
1251+
* @offset: offset in the buffer at which to validate an object.
1252+
*
1253+
* Return: If there's a valid metadata object at @offset in @buffer, the
1254+
* size of that object. Otherwise, it returns zero.
1255+
*/
1256+
static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1257+
{
1258+
/* Check if we can read a header first */
1259+
struct binder_object_header *hdr;
1260+
size_t object_size = 0;
1261+
1262+
if (offset > buffer->data_size - sizeof(*hdr) ||
1263+
buffer->data_size < sizeof(*hdr) ||
1264+
!IS_ALIGNED(offset, sizeof(u32)))
1265+
return 0;
1266+
1267+
/* Ok, now see if we can read a complete object. */
1268+
hdr = (struct binder_object_header *)(buffer->data + offset);
1269+
switch (hdr->type) {
1270+
case BINDER_TYPE_BINDER:
1271+
case BINDER_TYPE_WEAK_BINDER:
1272+
case BINDER_TYPE_HANDLE:
1273+
case BINDER_TYPE_WEAK_HANDLE:
1274+
object_size = sizeof(struct flat_binder_object);
1275+
break;
1276+
case BINDER_TYPE_FD:
1277+
object_size = sizeof(struct binder_fd_object);
1278+
break;
1279+
default:
1280+
return 0;
1281+
}
1282+
if (offset <= buffer->data_size - object_size &&
1283+
buffer->data_size >= object_size)
1284+
return object_size;
1285+
else
1286+
return 0;
1287+
}
1288+
12431289
static void binder_transaction_buffer_release(struct binder_proc *proc,
12441290
struct binder_buffer *buffer,
12451291
binder_size_t *failed_at)
@@ -1262,21 +1308,23 @@ static void binder_transaction_buffer_release(struct binder_proc *proc,
12621308
else
12631309
off_end = (void *)offp + buffer->offsets_size;
12641310
for (; offp < off_end; offp++) {
1265-
struct flat_binder_object *fp;
1311+
struct binder_object_header *hdr;
1312+
size_t object_size = binder_validate_object(buffer, *offp);
12661313

1267-
if (*offp > buffer->data_size - sizeof(*fp) ||
1268-
buffer->data_size < sizeof(*fp) ||
1269-
!IS_ALIGNED(*offp, sizeof(u32))) {
1270-
pr_err("transaction release %d bad offset %lld, size %zd\n",
1314+
if (object_size == 0) {
1315+
pr_err("transaction release %d bad object at offset %lld, size %zd\n",
12711316
debug_id, (u64)*offp, buffer->data_size);
12721317
continue;
12731318
}
1274-
fp = (struct flat_binder_object *)(buffer->data + *offp);
1275-
switch (fp->type) {
1319+
hdr = (struct binder_object_header *)(buffer->data + *offp);
1320+
switch (hdr->type) {
12761321
case BINDER_TYPE_BINDER:
12771322
case BINDER_TYPE_WEAK_BINDER: {
1278-
struct binder_node *node = binder_get_node(proc, fp->binder);
1323+
struct flat_binder_object *fp;
1324+
struct binder_node *node;
12791325

1326+
fp = to_flat_binder_object(hdr);
1327+
node = binder_get_node(proc, fp->binder);
12801328
if (node == NULL) {
12811329
pr_err("transaction release %d bad node %016llx\n",
12821330
debug_id, (u64)fp->binder);
@@ -1285,15 +1333,17 @@ static void binder_transaction_buffer_release(struct binder_proc *proc,
12851333
binder_debug(BINDER_DEBUG_TRANSACTION,
12861334
" node %d u%016llx\n",
12871335
node->debug_id, (u64)node->ptr);
1288-
binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1336+
binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
1337+
0);
12891338
} break;
12901339
case BINDER_TYPE_HANDLE:
12911340
case BINDER_TYPE_WEAK_HANDLE: {
1341+
struct flat_binder_object *fp;
12921342
struct binder_ref *ref;
12931343

1344+
fp = to_flat_binder_object(hdr);
12941345
ref = binder_get_ref(proc, fp->handle,
1295-
fp->type == BINDER_TYPE_HANDLE);
1296-
1346+
hdr->type == BINDER_TYPE_HANDLE);
12971347
if (ref == NULL) {
12981348
pr_err("transaction release %d bad handle %d\n",
12991349
debug_id, fp->handle);
@@ -1302,19 +1352,21 @@ static void binder_transaction_buffer_release(struct binder_proc *proc,
13021352
binder_debug(BINDER_DEBUG_TRANSACTION,
13031353
" ref %d desc %d (node %d)\n",
13041354
ref->debug_id, ref->desc, ref->node->debug_id);
1305-
binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1355+
binder_dec_ref(ref, hdr->type == BINDER_TYPE_HANDLE);
13061356
} break;
13071357

1308-
case BINDER_TYPE_FD:
1358+
case BINDER_TYPE_FD: {
1359+
struct binder_fd_object *fp = to_binder_fd_object(hdr);
1360+
13091361
binder_debug(BINDER_DEBUG_TRANSACTION,
1310-
" fd %d\n", fp->handle);
1362+
" fd %d\n", fp->fd);
13111363
if (failed_at)
1312-
task_close_fd(proc, fp->handle);
1313-
break;
1364+
task_close_fd(proc, fp->fd);
1365+
} break;
13141366

13151367
default:
13161368
pr_err("transaction release %d bad object type %x\n",
1317-
debug_id, fp->type);
1369+
debug_id, hdr->type);
13181370
break;
13191371
}
13201372
}
@@ -1531,28 +1583,29 @@ static void binder_transaction(struct binder_proc *proc,
15311583
off_end = (void *)offp + tr->offsets_size;
15321584
off_min = 0;
15331585
for (; offp < off_end; offp++) {
1534-
struct flat_binder_object *fp;
1586+
struct binder_object_header *hdr;
1587+
size_t object_size = binder_validate_object(t->buffer, *offp);
15351588

1536-
if (*offp > t->buffer->data_size - sizeof(*fp) ||
1537-
*offp < off_min ||
1538-
t->buffer->data_size < sizeof(*fp) ||
1539-
!IS_ALIGNED(*offp, sizeof(u32))) {
1540-
binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1589+
if (object_size == 0 || *offp < off_min) {
1590+
binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
15411591
proc->pid, thread->pid, (u64)*offp,
15421592
(u64)off_min,
1543-
(u64)(t->buffer->data_size -
1544-
sizeof(*fp)));
1593+
(u64)t->buffer->data_size);
15451594
return_error = BR_FAILED_REPLY;
15461595
goto err_bad_offset;
15471596
}
1548-
fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1549-
off_min = *offp + sizeof(struct flat_binder_object);
1550-
switch (fp->type) {
1597+
1598+
hdr = (struct binder_object_header *)(t->buffer->data + *offp);
1599+
off_min = *offp + object_size;
1600+
switch (hdr->type) {
15511601
case BINDER_TYPE_BINDER:
15521602
case BINDER_TYPE_WEAK_BINDER: {
1603+
struct flat_binder_object *fp;
1604+
struct binder_node *node;
15531605
struct binder_ref *ref;
1554-
struct binder_node *node = binder_get_node(proc, fp->binder);
15551606

1607+
fp = to_flat_binder_object(hdr);
1608+
node = binder_get_node(proc, fp->binder);
15561609
if (node == NULL) {
15571610
node = binder_new_node(proc, fp->binder, fp->cookie);
15581611
if (node == NULL) {
@@ -1580,14 +1633,14 @@ static void binder_transaction(struct binder_proc *proc,
15801633
return_error = BR_FAILED_REPLY;
15811634
goto err_binder_get_ref_for_node_failed;
15821635
}
1583-
if (fp->type == BINDER_TYPE_BINDER)
1584-
fp->type = BINDER_TYPE_HANDLE;
1636+
if (hdr->type == BINDER_TYPE_BINDER)
1637+
hdr->type = BINDER_TYPE_HANDLE;
15851638
else
1586-
fp->type = BINDER_TYPE_WEAK_HANDLE;
1639+
hdr->type = BINDER_TYPE_WEAK_HANDLE;
15871640
fp->binder = 0;
15881641
fp->handle = ref->desc;
15891642
fp->cookie = 0;
1590-
binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1643+
binder_inc_ref(ref, hdr->type == BINDER_TYPE_HANDLE,
15911644
&thread->todo);
15921645

15931646
trace_binder_transaction_node_to_ref(t, node, ref);
@@ -1598,11 +1651,12 @@ static void binder_transaction(struct binder_proc *proc,
15981651
} break;
15991652
case BINDER_TYPE_HANDLE:
16001653
case BINDER_TYPE_WEAK_HANDLE: {
1654+
struct flat_binder_object *fp;
16011655
struct binder_ref *ref;
16021656

1657+
fp = to_flat_binder_object(hdr);
16031658
ref = binder_get_ref(proc, fp->handle,
1604-
fp->type == BINDER_TYPE_HANDLE);
1605-
1659+
hdr->type == BINDER_TYPE_HANDLE);
16061660
if (ref == NULL) {
16071661
binder_user_error("%d:%d got transaction with invalid handle, %d\n",
16081662
proc->pid,
@@ -1616,13 +1670,15 @@ static void binder_transaction(struct binder_proc *proc,
16161670
goto err_binder_get_ref_failed;
16171671
}
16181672
if (ref->node->proc == target_proc) {
1619-
if (fp->type == BINDER_TYPE_HANDLE)
1620-
fp->type = BINDER_TYPE_BINDER;
1673+
if (hdr->type == BINDER_TYPE_HANDLE)
1674+
hdr->type = BINDER_TYPE_BINDER;
16211675
else
1622-
fp->type = BINDER_TYPE_WEAK_BINDER;
1676+
hdr->type = BINDER_TYPE_WEAK_BINDER;
16231677
fp->binder = ref->node->ptr;
16241678
fp->cookie = ref->node->cookie;
1625-
binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1679+
binder_inc_node(ref->node,
1680+
hdr->type == BINDER_TYPE_BINDER,
1681+
0, NULL);
16261682
trace_binder_transaction_ref_to_node(t, ref);
16271683
binder_debug(BINDER_DEBUG_TRANSACTION,
16281684
" ref %d desc %d -> node %d u%016llx\n",
@@ -1639,7 +1695,9 @@ static void binder_transaction(struct binder_proc *proc,
16391695
fp->binder = 0;
16401696
fp->handle = new_ref->desc;
16411697
fp->cookie = 0;
1642-
binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1698+
binder_inc_ref(new_ref,
1699+
hdr->type == BINDER_TYPE_HANDLE,
1700+
NULL);
16431701
trace_binder_transaction_ref_to_ref(t, ref,
16441702
new_ref);
16451703
binder_debug(BINDER_DEBUG_TRANSACTION,
@@ -1652,25 +1710,26 @@ static void binder_transaction(struct binder_proc *proc,
16521710
case BINDER_TYPE_FD: {
16531711
int target_fd;
16541712
struct file *file;
1713+
struct binder_fd_object *fp = to_binder_fd_object(hdr);
16551714

16561715
if (reply) {
16571716
if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
16581717
binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1659-
proc->pid, thread->pid, fp->handle);
1718+
proc->pid, thread->pid, fp->fd);
16601719
return_error = BR_FAILED_REPLY;
16611720
goto err_fd_not_allowed;
16621721
}
16631722
} else if (!target_node->accept_fds) {
16641723
binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1665-
proc->pid, thread->pid, fp->handle);
1724+
proc->pid, thread->pid, fp->fd);
16661725
return_error = BR_FAILED_REPLY;
16671726
goto err_fd_not_allowed;
16681727
}
16691728

1670-
file = fget(fp->handle);
1729+
file = fget(fp->fd);
16711730
if (file == NULL) {
16721731
binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1673-
proc->pid, thread->pid, fp->handle);
1732+
proc->pid, thread->pid, fp->fd);
16741733
return_error = BR_FAILED_REPLY;
16751734
goto err_fget_failed;
16761735
}
@@ -1688,17 +1747,18 @@ static void binder_transaction(struct binder_proc *proc,
16881747
goto err_get_unused_fd_failed;
16891748
}
16901749
task_fd_install(target_proc, target_fd, file);
1691-
trace_binder_transaction_fd(t, fp->handle, target_fd);
1750+
trace_binder_transaction_fd(t, fp->fd, target_fd);
16921751
binder_debug(BINDER_DEBUG_TRANSACTION,
1693-
" fd %d -> %d\n", fp->handle, target_fd);
1752+
" fd %d -> %d\n", fp->fd,
1753+
target_fd);
16941754
/* TODO: fput? */
1695-
fp->binder = 0;
1696-
fp->handle = target_fd;
1755+
fp->pad_binder = 0;
1756+
fp->fd = target_fd;
16971757
} break;
16981758

16991759
default:
17001760
binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1701-
proc->pid, thread->pid, fp->type);
1761+
proc->pid, thread->pid, hdr->type);
17021762
return_error = BR_FAILED_REPLY;
17031763
goto err_bad_object_type;
17041764
}

‎include/uapi/linux/android/binder.h‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ typedef __u64 binder_size_t;
4848
typedef __u64 binder_uintptr_t;
4949
#endif
5050

51+
/**
52+
* struct binder_object_header - header shared by all binder metadata objects.
53+
* @type: type of the object
54+
*/
55+
struct binder_object_header {
56+
__u32 type;
57+
};
58+
5159
/*
5260
* This is the flattened representation of a Binder object for transfer
5361
* between processes. The 'offsets' supplied as part of a binder transaction
@@ -56,9 +64,8 @@ typedef __u64 binder_uintptr_t;
5664
* between processes.
5765
*/
5866
struct flat_binder_object {
59-
/* 8 bytes for large_flat_header. */
60-
__u32 type;
61-
__u32 flags;
67+
struct binder_object_header hdr;
68+
__u32 flags;
6269

6370
/* 8 bytes of data. */
6471
union {
@@ -70,6 +77,24 @@ struct flat_binder_object {
7077
binder_uintptr_t cookie;
7178
};
7279

80+
/**
81+
* struct binder_fd_object - describes a filedescriptor to be fixed up.
82+
* @hdr: common header structure
83+
* @pad_flags: padding to remain compatible with old userspace code
84+
* @pad_binder: padding to remain compatible with old userspace code
85+
* @fd: file descriptor
86+
* @cookie: opaque data, used by user-space
87+
*/
88+
struct binder_fd_object {
89+
struct binder_object_header hdr;
90+
__u32 pad_flags;
91+
union {
92+
binder_uintptr_t pad_binder;
93+
__u32 fd;
94+
};
95+
96+
binder_uintptr_t cookie;
97+
};
7398
/*
7499
* On 64-bit platforms where user code may run in 32-bits the driver must
75100
* translate the buffer (and local binder) addresses appropriately.

0 commit comments

Comments
 (0)