Skip to content

Commit b34826e

Browse files
masonclgregkh
authored andcommitted
binder: cache secctx size before release zeroes it
binder_transaction() bounds the scatter-gather buffer area with sg_buf_end_offset and subtracts the aligned LSM context size because the secctx is written at the tail of that area. The subtraction reads lsmctx.len, but that field has already been cleared by the time the line runs: security_secid_to_secctx(secid, &lsmctx) /* lsmctx.len set */ lsmctx_aligned_size = ALIGN(lsmctx.len, sizeof(u64)) extra_buffers_size += lsmctx_aligned_size ... security_release_secctx(&lsmctx) /* memset zeroes len */ ... sg_buf_end_offset = sg_buf_offset + extra_buffers_size - ALIGN(lsmctx.len, sizeof(u64)) /* ALIGN(0,8) */ security_release_secctx() does memset(cp, 0, sizeof(*cp)), so lsmctx.len reads back as 0 and the subtraction contributes nothing, leaving sg_buf_end_offset too large by the aligned secctx size on every transaction to a txn_security_ctx node. Each BINDER_TYPE_PTR object then derives buf_left = sg_buf_end_offset - sg_buf_offset as the sole upper bound on its copy, so the inflated end offset lets the copy run into the bytes that already hold the secctx. The aligned size must therefore be cached before release rather than re-read from the now-cleared field. Fix by caching it in lsmctx_aligned_size at function scope when it is first computed and subtracting lsmctx_aligned_size instead of re-reading lsmctx.len after release. Reuse the same value for the earlier buf_offset computation. Fixes: 6fba898 ("lsm: ensure the correct LSM context releaser") Cc: stable <stable@kernel.org> Assisted-by: kres:claude-opus-4-8 Signed-off-by: Chris Mason <clm@meta.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Carlos Llamas <cmllamas@google.com> Link: https://patch.msgid.link/20260603174506.1957278-1-clm@meta.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 77bfebf commit b34826e

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

drivers/android/binder.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,7 @@ static void binder_transaction(struct binder_proc *proc,
30803080
int t_debug_id = atomic_inc_return(&binder_last_id);
30813081
ktime_t t_start_time = ktime_get();
30823082
struct lsm_context lsmctx = { };
3083+
size_t lsmctx_aligned_size = 0;
30833084
LIST_HEAD(sgc_head);
30843085
LIST_HEAD(pf_head);
30853086
const void __user *user_buffer = (const void __user *)
@@ -3346,7 +3347,6 @@ static void binder_transaction(struct binder_proc *proc,
33463347

33473348
if (target_node && target_node->txn_security_ctx) {
33483349
u32 secid;
3349-
size_t added_size;
33503350

33513351
security_cred_getsecid(proc->cred, &secid);
33523352
ret = security_secid_to_secctx(secid, &lsmctx);
@@ -3358,9 +3358,9 @@ static void binder_transaction(struct binder_proc *proc,
33583358
return_error_line = __LINE__;
33593359
goto err_get_secctx_failed;
33603360
}
3361-
added_size = ALIGN(lsmctx.len, sizeof(u64));
3362-
extra_buffers_size += added_size;
3363-
if (extra_buffers_size < added_size) {
3361+
lsmctx_aligned_size = ALIGN(lsmctx.len, sizeof(u64));
3362+
extra_buffers_size += lsmctx_aligned_size;
3363+
if (extra_buffers_size < lsmctx_aligned_size) {
33643364
binder_txn_error("%d:%d integer overflow of extra_buffers_size\n",
33653365
thread->pid, proc->pid);
33663366
return_error = BR_FAILED_REPLY;
@@ -3397,7 +3397,7 @@ static void binder_transaction(struct binder_proc *proc,
33973397
size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
33983398
ALIGN(tr->offsets_size, sizeof(void *)) +
33993399
ALIGN(extra_buffers_size, sizeof(void *)) -
3400-
ALIGN(lsmctx.len, sizeof(u64));
3400+
lsmctx_aligned_size;
34013401

34023402
t->security_ctx = t->buffer->user_data + buf_offset;
34033403
err = binder_alloc_copy_to_buffer(&target_proc->alloc,
@@ -3452,7 +3452,7 @@ static void binder_transaction(struct binder_proc *proc,
34523452
off_end_offset = off_start_offset + tr->offsets_size;
34533453
sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
34543454
sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3455-
ALIGN(lsmctx.len, sizeof(u64));
3455+
lsmctx_aligned_size;
34563456
off_min = 0;
34573457
for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
34583458
buffer_offset += sizeof(binder_size_t)) {

0 commit comments

Comments
 (0)