binder: use cred instead of task for security context
Subsystems (4)
v5 cover binder: use cred instead of task for security context
This series fixes the possible use of an incorrect security context when checking selinux permissions, getting a security ID, or lookup up the euid.
The previous behavior was to save the group_leader 'struct task_struct' in binder_open() and using that to obtain security IDs or euids.
This has been shown to be unreliable, so this series instead saves the 'struct cred' of the task that called binder_open(). This cred is used for these lookups instead of the task.
v1 and v2 of this series were a single patch "binder: use euid from" cred instead of using task". During review, Stephen Smalley identified two more related issues so the corresponding patches were added to the series.
v3:
- add 2 patches to fix getsecid and euid
v4:
- fix minor checkpatch issues
- fix build-break for !CONFIG_SECURITY
v5:
- reorder/refactor patches as suggested by Stephen Smalley so eiud fix is first and saves the cred during binder_open()
- set *secid=0 for !CONFIG_SECURITY version of secuirty_cred_getsecid()
Todd Kjos (3):
binder: use euid from cred instead of using task
binder: use cred instead of task for selinux checks
binder: use cred instead of task for getsecid
drivers/android/binder.c | 14 ++++++++------ drivers/android/binder_internal.h | 4 ++++ include/linux/lsm_hook_defs.h | 14 +++++++------- include/linux/lsm_hooks.h | 14 +++++++------- include/linux/security.h | 28 ++++++++++++++-------------- security/security.c | 14 +++++++------- security/selinux/hooks.c | 48 +++++++++++++----------------------------------- 7 files changed, 60 insertions(+), 76 deletions(-)
PATCH v5 1/3 binder: use euid from cred instead of using task
Save the 'struct cred' associated with a binder process at initial open to avoid potential race conditions when converting to an euid.
Set a transaction's sender_euid from the 'struct cred' saved at binder_open() instead of looking up the euid from the binder proc's 'struct task'. This ensures the euid is associated with the security context that of the task that opened binder.
---
v3: added this patch to series (as 3/3)
v5:
- combined with saving of 'struct cred' during binder_open()
- reordered to 1/1 as suggested by Stephen Smalley
drivers/android/binder.c | 4 +++- drivers/android/binder_internal.h | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-)
drivers/android/binder.c +3 -1
@@ -2711,7 +2711,7 @@ static void binder_transaction(struct binder_proc *proc, t->from = thread; else t->from = NULL; - t->sender_euid =task_euid (proc->tsk); + t->sender_euid = proc->cred->euid; t->to_proc = target_proc; t->to_thread = target_thread; t->code = tr->code;
@@ -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); }
@@ -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 +4 -0
@@ -364,6 +364,9 @@ struct binder_ref {* (invariant after initialized) * @tsk task_struct for group_leader of process * (invariant after initialized) +* @cred struct cred associated with the `struct file` +* in binder_open() +* (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 +429,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;
Signature · 1 line
2.33.0.882.g93a45727a2-goog
PATCH v5 2/3 binder: use cred instead of task for selinux checks
In reply to Todd KjosSince 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 using the 'struct cred' saved during binder_open and pass it to the selinux subsystem.
hooks for SELinux.")
---
v2: updated comments as suggested by Paul Moore
v3: added 2 patches to fix related issues
v5: moved 'struct cred' save during binder_open() to patch 1/3
drivers/android/binder.c | 12 ++++----- include/linux/lsm_hook_defs.h | 14 +++++----- include/linux/lsm_hooks.h | 14 +++++----- include/linux/security.h | 28 ++++++++++---------- security/security.c | 14 +++++----- security/selinux/hooks.c | 48 ++++++++++------------------------- 6 files changed, 54 insertions(+), 76 deletions(-)
drivers/android/binder.c +6 -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__;
@@ -4565,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)) {
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/lsm_hooks.h +7 -7
@@ -1313,22 +1313,22 @@* * @binder_set_context_mgr: * Check whether @mgr is allowed to be the binder context manager. -* @mgr contains the task_struct for the task being registered. +* @mgr contains the struct cred for the current binder process. * Return 0 if permission is granted. * @binder_transaction: * Check whether @from is allowed to invoke a binder transaction call * to @to. -* @from contains the task_struct for the sending task. -* @to contains the task_struct for the receiving task. +* @from contains the struct cred for the sending process. +* @to contains the struct cred for the receiving process. * @binder_transfer_binder: * Check whether @from is allowed to transfer a binder reference to @to. -* @from contains the task_struct for the sending task. -* @to contains the task_struct for the receiving task. +* @from contains the struct cred for the sending process. +* @to contains the struct cred for the receiving process. * @binder_transfer_file: * Check whether @from is allowed to transfer @file to @to. -* @from contains the task_struct for the sending task. +* @from contains the struct cred for the sending process. * @file contains the struct file being transferred. -* @to contains the task_struct for the receiving task. +* @to contains the struct cred for the receiving process. * * @ptrace_access_check: * Check permission before allowing the current process to trace the
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.882.g93a45727a2-goog
PATCH v5 3/3 binder: use cred instead of task for getsecid
In reply to Todd KjosUse the 'struct cred' saved at binder_open() to lookup the security ID via security_cred_getsecid(). This ensures that the security context that opened binder is the one used to generate the secctx.
security context")
---
v3: added this patch to series
v4: fix build-break for !CONFIG_SECURITY
v5: set *secid=0 for !CONFIG_SECURITY version of secuirty_cred_getsecid()
drivers/android/binder.c | 11 +---------- include/linux/security.h | 5 +++++ 2 files changed, 6 insertions(+), 10 deletions(-)
drivers/android/binder.c +1 -10
@@ -2722,16 +2722,7 @@ static void binder_transaction(struct binder_proc *proc,u32 secid;size_t added_size; -/* -* Arguably this should be the task's subjective LSM secid but -* we can't reliably access the subjective creds of a task -* other than our own so we must use the objective creds, 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. -*/ -security_task_getsecid_obj (proc->tsk, &secid); +security_cred_getsecid (proc->cred, &secid); ret =security_secid_to_secctx (secid, &secctx, &secctx_sz);if (ret) { return_error =BR_FAILED_REPLY ;
include/linux/security.h +5 -0
@@ -1041,6 +1041,11 @@ static inline void security_transfer_creds(struct cred *new, { } +static inline void security_cred_getsecid (const struct cred *c,u32 *secid) +{ + *secid =0 ; +} +static inline int security_kernel_act_as (struct cred *cred,u32 secid) {return 0 ;
Signature · 1 line
2.33.0.882.g93a45727a2-goog
Quoted 33 lines · Todd Kjos
This series fixes the possible use of an incorrect security context when checking selinux permissions, getting a security ID, or lookup up the euid.
The previous behavior was to save the
group_leader'structtask_struct' inbinder_open()and using that to obtain security IDs or euids.This has been shown to be unreliable, so this series instead saves the 'struct cred' of the task that called
binder_open(). This cred is used for these lookups instead of the task.v1 and v2 of this series were a single patch "binder: use euid from" cred instead of using task". During review, Stephen Smalley identified two more related issues so the corresponding patches were added to the series.
v3:
- add 2 patches to fix getsecid and euidv4:
- fix minor checkpatch issues
- fix build-break for !CONFIG_SECURITYv5:
- reorder/refactor patches as suggested by Stephen Smalley so eiud fix is first and saves the cred duringbinder_open()
- set *secid=0 for !CONFIG_SECURITY version ofsecuirty_cred_getsecid()Todd Kjos (3):
binder: use euid from cred instead of using task
binder: use cred instead of task for selinux checks
binder: use cred instead of task for getsecid
Quoted 8 lines
drivers/android/binder.c | 14 ++++++++------ drivers/android/binder_internal.h | 4 ++++ include/linux/lsm_hook_defs.h | 14 +++++++------- include/linux/lsm_hooks.h | 14 +++++++------- include/linux/security.h | 28 ++++++++++++++-------------- security/security.c | 14 +++++++------- security/selinux/hooks.c | 48 +++++++++++++----------------------------------- 7 files changed, 60 insertions(+), 76 deletions(-)
For the series:
Paul Moore
maintainerQuoted 11 lines · Todd Kjos
This series fixes the possible use of an incorrect security context when checking selinux permissions, getting a security ID, or lookup up the euid.
The previous behavior was to save the
group_leader'structtask_struct' inbinder_open()and using that to obtain security IDs or euids.This has been shown to be unreliable, so this series instead saves the 'struct cred' of the task that called
binder_open(). This cred is used for these lookups instead of the task.
Hi Todd,
I just merged all three patches into selinux/next, thanks for your help patience on this patchset. Ultimately I merged these patches into selinux/next as opposed to selinux/stable-5.15 because I felt that a couple of weeks in -next before going to Linus would be a good thing. I'm also not certain how widespread binder is outside of Android so I figured the practical difference between next and stable-5.15 is likely very small. Regardless, all of your Fixes and stable tags remain in the patches so as soon as they go up to Linus during the next merge window the stable folks will be notified.
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 16 lines · Paul Moore
Quoted 11 lines · Todd Kjos
This series fixes the possible use of an incorrect security context when checking selinux permissions, getting a security ID, or lookup up the euid.
The previous behavior was to save the
group_leader'structtask_struct' inbinder_open()and using that to obtain security IDs or euids.This has been shown to be unreliable, so this series instead saves the 'struct cred' of the task that called
binder_open(). This cred is used for these lookups instead of the task.Hi Todd,
Quoted 9 lines
I just merged all three patches into selinux/next, thanks for your help patience on this patchset. Ultimately I merged these patches into selinux/next as opposed to selinux/stable-5.15 because I felt that a couple of weeks in -next before going to Linus would be a good thing. I'm also not certain how widespread binder is outside of Android so I figured the practical difference between next and stable-5.15 is likely very small. Regardless, all of your Fixes and stable tags remain in the patches so as soon as they go up to Linus during the next merge window the stable folks will be notified.
Thanks Paul. This all sounds fine.
Quoted 4 lines
--
paul moore
www.paul-moore.com