Skip to content

Commit 2669b8b

Browse files
Christian Braunergregkh
authored andcommitted
binder: prevent UAF for binderfs devices
On binder_release(), binder_defer_work(proc, BINDER_DEFERRED_RELEASE) is called which punts the actual cleanup operation to a workqueue. At some point, binder_deferred_func() will be called which will end up calling binder_deferred_release() which will retrieve and cleanup the binder_context attach to this struct binder_proc. If we trace back where this binder_context is attached to binder_proc we see that it is set in binder_open() and is taken from the struct binder_device it is associated with. This obviously assumes that the struct binder_device that context is attached to is _never_ freed. While that might be true for devtmpfs binder devices it is most certainly wrong for binderfs binder devices. So, assume binder_open() is called on a binderfs binder devices. We now stash away the struct binder_context associated with that struct binder_devices: proc->context = &binder_dev->context; /* binderfs stashes devices in i_private */ if (is_binderfs_device(nodp)) { binder_dev = nodp->i_private; info = nodp->i_sb->s_fs_info; binder_binderfs_dir_entry_proc = info->proc_log_dir; } else { . . . proc->context = &binder_dev->context; Now let's assume that the binderfs instance for that binder devices is shutdown via umount() and/or the mount namespace associated with it goes away. As long as there is still an fd open for that binderfs binder device things are fine. But let's assume we now close the last fd for that binderfs binder device. Now binder_release() is called and punts to the workqueue. Assume that the workqueue has quite a bit of stuff to do and doesn't get to cleaning up the struct binder_proc and the associated struct binder_context with it for that binderfs binder device right away. In the meantime, the VFS is killing the super block and is ultimately calling sb->evict_inode() which means it will call binderfs_evict_inode() which does: static void binderfs_evict_inode(struct inode *inode) { struct binder_device *device = inode->i_private; struct binderfs_info *info = BINDERFS_I(inode); clear_inode(inode); if (!S_ISCHR(inode->i_mode) || !device) return; mutex_lock(&binderfs_minors_mutex); --info->device_count; ida_free(&binderfs_minors, device->miscdev.minor); mutex_unlock(&binderfs_minors_mutex); kfree(device->context.name); kfree(device); } thereby freeing the struct binder_device including struct binder_context. Now the workqueue finally has time to get around to cleaning up struct binder_proc and is now trying to access the associate struct binder_context. Since it's already freed it will OOPs. Fix this by holding an additional reference to the inode that is only released once the workqueue is done cleaning up struct binder_proc. This is an easy alternative to introducing separate refcounting on struct binder_device which we can always do later if it becomes necessary. This is an alternative fix to 51d8a7e ("binder: prevent UAF read in print_binder_transaction_log_entry()"). Fixes: 3ad20fe ("binder: implement binderfs") Fixes: 03e2e07 ("binder: Make transaction_log available in binderfs") Related : 51d8a7e ("binder: prevent UAF read in print_binder_transaction_log_entry()") Cc: [email protected] Signed-off-by: Christian Brauner <[email protected]> Acked-by: Todd Kjos <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent f8788d8 commit 2669b8b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

drivers/android/binder.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5221,7 +5221,7 @@ static int binder_open(struct inode *nodp, struct file *filp)
52215221
proc->default_priority = task_nice(current);
52225222
/* binderfs stashes devices in i_private */
52235223
if (is_binderfs_device(nodp)) {
5224-
binder_dev = nodp->i_private;
5224+
binder_dev = binderfs_device_get(nodp->i_private);
52255225
info = nodp->i_sb->s_fs_info;
52265226
binder_binderfs_dir_entry_proc = info->proc_log_dir;
52275227
} else {
@@ -5405,6 +5405,7 @@ static int binder_node_release(struct binder_node *node, int refs)
54055405
static void binder_deferred_release(struct binder_proc *proc)
54065406
{
54075407
struct binder_context *context = proc->context;
5408+
struct binder_device *device;
54085409
struct rb_node *n;
54095410
int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
54105411

@@ -5484,6 +5485,8 @@ static void binder_deferred_release(struct binder_proc *proc)
54845485
outgoing_refs, active_transactions);
54855486

54865487
binder_proc_dec_tmpref(proc);
5488+
device = container_of(proc->context, struct binder_device, context);
5489+
binderfs_device_put(device);
54875490
}
54885491

54895492
static void binder_deferred_func(struct work_struct *work)

drivers/android/binder_internal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ struct binder_device {
3535
struct inode *binderfs_inode;
3636
};
3737

38+
static inline struct binder_device *binderfs_device_get(struct binder_device *dev)
39+
{
40+
if (dev->binderfs_inode)
41+
ihold(dev->binderfs_inode);
42+
return dev;
43+
}
44+
45+
static inline void binderfs_device_put(struct binder_device *dev)
46+
{
47+
if (dev->binderfs_inode)
48+
iput(dev->binderfs_inode);
49+
}
50+
3851
/**
3952
* binderfs_mount_opts - mount options for binderfs
4053
* @max: maximum number of allocatable binderfs binder devices

0 commit comments

Comments
 (0)