binder: use cred instead of task for selinux checks
Subsystems (4)
PATCH 1/1 binder: use cred instead of task for selinux checks
Save the struct cred associated with a binder process at initial open to avoid potential race conditions when converting to a security ID.
Since binder was integrated with selinux, it has passed 'struct task_struct' associated with the binder_proc to represent the source and target of transactions. The conversion of task to SID was then done in the hook implementations. It turns out that there are race conditions which can result in an incorrect security context being used.
Fix by saving the 'struct cred' during binder_open and pass it to the selinux subsystem.
hooks for SELinux.")
--- drivers/android/binder.c | 14 +++++---- drivers/android/binder_internal.h | 3 ++ include/linux/lsm_hook_defs.h | 14 ++++----- include/linux/security.h | 28 +++++++++--------- security/security.c | 14 ++++----- security/selinux/hooks.c | 48 +++++++++---------------------- 6 files changed, 52 insertions(+), 69 deletions(-)
drivers/android/binder.c +8 -6
@@ -2056,7 +2056,7 @@ static int binder_translate_binder(struct flat_binder_object *fp, ret = -EINVAL ; goto done; } -if (security_binder_transfer_binder (proc->tsk, target_proc->tsk)) { +if (security_binder_transfer_binder (proc->cred, target_proc->cred)) { ret = -EPERM ; goto done; }
@@ -2102,7 +2102,7 @@ static int binder_translate_handle(struct flat_binder_object *fp, proc->pid, thread->pid, fp->handle);return -EINVAL ; } -if (security_binder_transfer_binder (proc->tsk, target_proc->tsk)) { +if (security_binder_transfer_binder (proc->cred, target_proc->cred)) { ret = -EPERM ; goto done; }
@@ -2190,7 +2190,7 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, ret = -EBADF ; goto err_fget; } - ret =security_binder_transfer_file (proc->tsk, target_proc->tsk, file); + ret =security_binder_transfer_file (proc->cred, target_proc->cred, file);if (ret <0 ) { ret = -EPERM ; goto err_security;
@@ -2595,8 +2595,8 @@ static void binder_transaction(struct binder_proc *proc, return_error_line = __LINE__; goto err_invalid_target_handle; } -if (security_binder_transaction (proc->tsk, - target_proc->tsk) <0 ) { +if (security_binder_transaction (proc->cred, + target_proc->cred) <0 ) { return_error =BR_FAILED_REPLY ; return_error_param = -EPERM ; return_error_line = __LINE__;
@@ -4353,6 +4353,7 @@ static void binder_free_proc(struct binder_proc *proc) }binder_alloc_deferred_release (&proc->alloc);put_task_struct (proc->tsk); +put_cred (proc->cred);binder_stats_deleted (BINDER_STAT_PROC );kfree (proc); }
@@ -4564,7 +4565,7 @@ static int binder_ioctl_set_ctx_mgr(struct file *filp, ret = -EBUSY ; goto out; } - ret =security_binder_set_context_mgr (proc->tsk); + ret =security_binder_set_context_mgr (proc->cred);if (ret <0 ) goto out;if (uid_valid (context->binder_context_mgr_uid)) {
@@ -5055,6 +5056,7 @@ static int binder_open(struct inode *nodp, struct file *filp)spin_lock_init (&proc->outer_lock);get_task_struct (current->group_leader); proc->tsk = current->group_leader; + proc->cred =get_cred (filp->f_cred);INIT_LIST_HEAD (&proc->todo);init_waitqueue_head (&proc->freeze_wait); proc->default_priority =task_nice (current);
drivers/android/binder_internal.h +3 -0
@@ -364,6 +364,8 @@ struct binder_ref {* (invariant after initialized) * @tsk task_struct for group_leader of process * (invariant after initialized) +* @cred struct cred for group_leader of process +* (invariant after initialized) * @deferred_work_node: element for binder_deferred_list * (protected by binder_deferred_lock) * @deferred_work: bitmap of deferred work to perform
@@ -426,6 +428,7 @@ struct binder_proc {struct list_head waiting_threads;int pid;struct task_struct *tsk; +const struct cred *cred;struct hlist_node deferred_work_node;int deferred_work;int outstanding_txns;
include/linux/lsm_hook_defs.h +7 -7
@@ -26,13 +26,13 @@* #undef LSM_HOOK * }; */ -LSM_HOOK(int, 0, binder_set_context_mgr, struct task_struct *mgr) -LSM_HOOK(int, 0, binder_transaction, struct task_struct *from, - struct task_struct *to) -LSM_HOOK(int, 0, binder_transfer_binder, struct task_struct *from, - struct task_struct *to) -LSM_HOOK(int, 0, binder_transfer_file, struct task_struct *from, - struct task_struct *to, struct file *file) +LSM_HOOK(int, 0, binder_set_context_mgr, const struct cred *mgr) +LSM_HOOK(int, 0, binder_transaction, const struct cred *from, + const struct cred *to) +LSM_HOOK(int, 0, binder_transfer_binder, const struct cred *from, + const struct cred *to) +LSM_HOOK(int, 0, binder_transfer_file, const struct cred *from, + const struct cred *to, struct file *file) LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child, unsigned int mode) LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent)
include/linux/security.h +14 -14
@@ -258,13 +258,13 @@ extern int security_init(void);extern int early_security_init (void );/* Security operations */ -int security_binder_set_context_mgr (struct task_struct *mgr); -int security_binder_transaction (struct task_struct *from, -struct task_struct *to); -int security_binder_transfer_binder (struct task_struct *from, -struct task_struct *to); -int security_binder_transfer_file (struct task_struct *from, -struct task_struct *to,struct file *file); +int security_binder_set_context_mgr (const struct cred *mgr); +int security_binder_transaction (const struct cred *from, +const struct cred *to); +int security_binder_transfer_binder (const struct cred *from, +const struct cred *to); +int security_binder_transfer_file (const struct cred *from, +const struct cred *to,struct file *file);int security_ptrace_access_check (struct task_struct *child,unsigned int mode);int security_ptrace_traceme (struct task_struct *parent); int security_capget(struct task_struct *target,
@@ -508,25 +508,25 @@ static inline int early_security_init(void)return 0 ; } -static inline int security_binder_set_context_mgr (struct task_struct *mgr) +static inline int security_binder_set_context_mgr (const struct cred *mgr) {return 0 ; } -static inline int security_binder_transaction (struct task_struct *from, -struct task_struct *to) +static inline int security_binder_transaction (const struct cred *from, +const struct cred *to) {return 0 ; } -static inline int security_binder_transfer_binder (struct task_struct *from, -struct task_struct *to) +static inline int security_binder_transfer_binder (const struct cred *from, +const struct cred *to) {return 0 ; } -static inline int security_binder_transfer_file (struct task_struct *from, -struct task_struct *to, +static inline int security_binder_transfer_file (const struct cred *from, +const struct cred *to,struct file *file) {return 0 ;
security/security.c +7 -7
@@ -747,25 +747,25 @@ static int lsm_superblock_alloc(struct super_block *sb)/* Security operations */ -int security_binder_set_context_mgr (struct task_struct *mgr) +int security_binder_set_context_mgr (const struct cred *mgr) {return call_int_hook (binder_set_context_mgr,0 , mgr); } -int security_binder_transaction (struct task_struct *from, -struct task_struct *to) +int security_binder_transaction (const struct cred *from, +const struct cred *to) {return call_int_hook (binder_transaction,0 , from, to); } -int security_binder_transfer_binder (struct task_struct *from, -struct task_struct *to) +int security_binder_transfer_binder (const struct cred *from, +const struct cred *to) {return call_int_hook (binder_transfer_binder,0 , from, to); } -int security_binder_transfer_file (struct task_struct *from, -struct task_struct *to,struct file *file) +int security_binder_transfer_file (const struct cred *from, +const struct cred *to,struct file *file) {return call_int_hook (binder_transfer_file,0 , from, to, file); }
security/selinux/hooks.c +13 -35
84 lines not on this page · raw message
Signature · 1 line
2.33.0.800.g4c38ced690-goog
Paul Moore
maintainerQuoted 19 lines · Todd Kjos
Save the struct cred associated with a binder process at initial open to avoid potential race conditions when converting to a security ID.
Since binder was integrated with selinux, it has passed 'struct
task_struct' associated with thebinder_procto represent the source and target of transactions. The conversion of task to SID was then done in the hook implementations. It turns out that there are race conditions which can result in an incorrect security context being used.Fix by saving the 'struct cred' during
binder_openand pass it to the selinux subsystem.Fixes 79af73079d75 ("Add security hooks to binder and implement thehooks for SELinux.")
Signed-off-by Todd Kjos <[email protected]>Cc [email protected] # 5.14 (need backport for earlier stables)
Quoted 8 lines
--- drivers/android/binder.c | 14 +++++---- drivers/android/binder_internal.h | 3 ++ include/linux/lsm_hook_defs.h | 14 ++++----- include/linux/security.h | 28 +++++++++--------- security/security.c | 14 ++++----- security/selinux/hooks.c | 48 +++++++++---------------------- 6 files changed, 52 insertions(+), 69 deletions(-)
Thanks Todd, I'm happy to see someone with a better understanding of binder than me pitch in to clean this up :) A couple of quick comments/questions below ...
drivers/android/binder.c static int binder_open(struct inode *nodp, struct file *filp) · line 5059
Quoted 9 lines
drivers/android/binder.c +1
-0@@ -5055,6 +5056,7 @@ static int binder_open(struct inode *nodp, struct file *filp)spin_lock_init (&proc->outer_lock);get_task_struct (current->group_leader); proc->tsk = current->group_leader; + proc->cred =get_cred (filp->f_cred);
Is it *always* true that filp->f_cred is going to be the same as current->group_leader->cred? Or rather does this help resolve the issue of wanting the subjective creds but not being able to access them mentioned in the task_sid_binder() comment? If the latter, it might be nice to add something to the related comment in struct binder_ref (below).
drivers/android/binder_internal.h struct binder_ref { · line 368
Quoted 13 lines
drivers/android/binder.c +0
-0INIT_LIST_HEAD (&proc->todo);init_waitqueue_head (&proc->freeze_wait); proc->default_priority =task_nice (current);drivers/android/binder_internal.h +2
-0@@ -364,6 +364,8 @@ struct binder_ref {* (invariant after initialized) * @tsk task_struct for group_leader of process * (invariant after initialized) +* @cred struct cred for group_leader of process +* (invariant after initialized)
Related to the question above. At the very least the comment should probably be changed to indicate to make it clear the creds are coming directly from the binder file/device and not always the group_leader.
Quoted 30 lines
security/selinux/hooks.c +0
-22@@ -255,29 +255,6 @@ static inline u32 task_sid_obj(const struct task_struct *task)return sid; } -/* -* get the security ID of a task for use with binder -*/ -static inline u32 task_sid_binder (const struct task_struct *task) -{ -/* -* In many case where this function is used we should be using the -* task's subjective SID, but we can't reliably access the subjective -* creds of a task other than our own so we must use the objective -* creds/SID, which are safe to access. The downside is that if a task -* is temporarily overriding it's creds it will not be reflected here; -* however, it isn't clear that binder would handle that case well -* anyway. -* -* If this ever changes and we can safely reference the subjective -* creds/SID of another task, this function will make it easier to -* identify the various places where we make use of the task SIDs in -* the binder code. It is also likely that we will need to adjust -* the main drivers/android binder code as well. -*/ -return task_sid_obj (task); -}
Signature · 2 lines
paul moore www.paul-moore.com
Paul Moore
maintainerQuoted 29 lines · Paul Moore
Quoted 19 lines · Todd Kjos
Save the struct cred associated with a binder process at initial open to avoid potential race conditions when converting to a security ID.
Since binder was integrated with selinux, it has passed 'struct
task_struct' associated with thebinder_procto represent the source and target of transactions. The conversion of task to SID was then done in the hook implementations. It turns out that there are race conditions which can result in an incorrect security context being used.Fix by saving the 'struct cred' during
binder_openand pass it to the selinux subsystem.Fixes 79af73079d75 ("Add security hooks to binder and implement thehooks for SELinux.")
Signed-off-by Todd Kjos <[email protected]>Cc [email protected] # 5.14 (need backport for earlier stables)Quoted 8 lines
--- drivers/android/binder.c | 14 +++++---- drivers/android/binder_internal.h | 3 ++ include/linux/lsm_hook_defs.h | 14 ++++----- include/linux/security.h | 28 +++++++++--------- security/security.c | 14 ++++----- security/selinux/hooks.c | 48 +++++++++---------------------- 6 files changed, 52 insertions(+), 69 deletions(-)
Quoted 3 lines
Thanks Todd, I'm happy to see someone with a better understanding of binder than me pitch in to clean this up :) A couple of quick comments/questions below ...
Ooops, I was a little over zealous when trimming my response and I accidentally cut off my comment that the associated comment blocks in include/linux/lsm_hooks.h should also be updated to reflect the binder LSM hook changes.
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 34 lines · Paul Moore
Quoted 19 lines · Todd Kjos
Save the struct cred associated with a binder process at initial open to avoid potential race conditions when converting to a security ID.
Since binder was integrated with selinux, it has passed 'struct
task_struct' associated with thebinder_procto represent the source and target of transactions. The conversion of task to SID was then done in the hook implementations. It turns out that there are race conditions which can result in an incorrect security context being used.Fix by saving the 'struct cred' during
binder_openand pass it to the selinux subsystem.Fixes 79af73079d75 ("Add security hooks to binder and implement thehooks for SELinux.")
Signed-off-by Todd Kjos <[email protected]>Cc [email protected] # 5.14 (need backport for earlier stables)Quoted 8 lines
--- drivers/android/binder.c | 14 +++++---- drivers/android/binder_internal.h | 3 ++ include/linux/lsm_hook_defs.h | 14 ++++----- include/linux/security.h | 28 +++++++++--------- security/security.c | 14 ++++----- security/selinux/hooks.c | 48 +++++++++---------------------- 6 files changed, 52 insertions(+), 69 deletions(-)Thanks Todd, I'm happy to see someone with a better understanding of binder than me pitch in to clean this up :) A couple of quick comments/questions below ...
Quoted 12 lines
drivers/android/binder.c static int binder_open(struct inode *nodp, struct file *filp) · line 5059
Quoted 9 lines
drivers/android/binder.c +1
-0@@ -5055,6 +5056,7 @@ static int binder_open(struct inode *nodp, struct file *filp)spin_lock_init (&proc->outer_lock);get_task_struct (current->group_leader); proc->tsk = current->group_leader; + proc->cred =get_cred (filp->f_cred);Is it *always* true that
filp->f_credis going to be the same ascurrent->group_leader->cred?
Not necessarily -- it is current->cred of the task in binder_open() (not group_leader). This is fine. We used to set proc->tsk to current, but switched to group_leader a few years ago to make it easier to detect the same process with multiple opens during mmap (to solve some unrelated issues). We still use group_leader for that purpose, but for the cred, the current cred in binder_open() is sufficient.
Quoted 5 lines
Or rather does this help resolve the
issue of wanting the subjective creds but not being able to access them mentioned in thetask_sid_binder()comment? If the latter, it might be nice to add something to the related comment in structbinder_ref(below).
Yes, we want the subjective cred so that is part of the point. I started with "proc->cred = get_task_cred(current->group_leader)" and got feedback that the "subjective" cred is preferred to avoid some subtle races that could be introduced, for example, if /dev/binder is opened through io_uring.
Quoted 10 lines
Quoted 10 lines
drivers/android/binder.c +0
-0INIT_LIST_HEAD (&proc->todo);init_waitqueue_head (&proc->freeze_wait); proc->default_priority =task_nice (current);drivers/android/binder_internal.h +0
-0@@ -364,6 +364,8 @@ struct binder_ref {* (invariant after initialized)
Quoted 8 lines
drivers/android/binder_internal.h struct binder_ref { · line 368
Quoted 4 lines
drivers/android/binder_internal.h +2
-0* @tsk task_struct for group_leader of process * (invariant after initialized) +* @cred struct cred for group_leader of process +* (invariant after initialized) Related to the question above. At the very least the comment should probably be changed to indicate to make it clear the creds are coming directly from the binder file/device and not always the
group_leader.
Good catch. Will update the comment (it's actually struct binder_proc).
Quoted 27 lines
Quoted 27 lines
security/selinux/hooks.c +0
-18@@ -255,29 +255,6 @@ static inline u32 task_sid_obj(const struct task_struct *task)return sid; } -/* -* get the security ID of a task for use with binder -*/ -static inline u32 task_sid_binder (const struct task_struct *task) -{ -/* -* In many case where this function is used we should be using the -* task's subjective SID, but we can't reliably access the subjective -* creds of a task other than our own so we must use the objective -* creds/SID, which are safe to access. The downside is that if a task -* is temporarily overriding it's creds it will not be reflected here; -* however, it isn't clear that binder would handle that case well -* anyway. -* -* If this ever changes and we can safely reference the subjective -* creds/SID of another task, this function will make it easier to -* identify the various places where we make use of the task SIDs in -* the binder code. It is also likely that we will need to adjust
Quoted 8 lines
security/selinux/hooks.c static inline u32 task_sid_obj(const struct task_struct *task) · line 279
Quoted 4 lines
security/selinux/hooks.c +0
-4-* the main drivers/android binder code as well. -*/ -return task_sid_obj (task); -}--
paul moore
www.paul-moore.com
and from your next response:
Quoted 4 lines
Ooops, I was a little over zealous when trimming my response and I accidentally cut off my comment that the associated comment blocks in
include/linux/lsm_hooks.hshould also be updated to reflect the binder LSM hook changes.
Thanks for pointing this out! I didn't notice these comment blocks.
-Todd