diff mbox series

[4/4] security: binder: Add transfer_charge SElinux hook

Message ID 20230109213809.418135-5-tjmercier@google.com (mailing list archive)
State Changes Requested
Delegated to: Paul Moore
Headers show
Series Track exported dma-buffers with memcg | expand

Commit Message

T.J. Mercier Jan. 9, 2023, 9:38 p.m. UTC
Any process can cause a memory charge transfer to occur to any other
process when transmitting a file descriptor through binder. This should
only be possible for central allocator processes, so a new SELinux
permission is added to restrict which processes are allowed to initiate
these charge transfers.

Signed-off-by: T.J. Mercier <tjmercier@google.com>
---
 drivers/android/binder.c            | 5 +++++
 include/linux/lsm_hook_defs.h       | 2 ++
 include/linux/lsm_hooks.h           | 6 ++++++
 include/linux/security.h            | 2 ++
 security/security.c                 | 6 ++++++
 security/selinux/hooks.c            | 9 +++++++++
 security/selinux/include/classmap.h | 2 +-
 7 files changed, 31 insertions(+), 1 deletion(-)

Comments

Casey Schaufler Jan. 9, 2023, 10:28 p.m. UTC | #1
On 1/9/2023 1:38 PM, T.J. Mercier wrote:
> Any process can cause a memory charge transfer to occur to any other
> process when transmitting a file descriptor through binder. This should
> only be possible for central allocator processes,

How is a "central allocator process" identified? If I have a LSM that
is not SELinux (e.g. AppArmor, Smack) or no LSM at all, how can/should this
be enforced? Why isn't binder enforcing this restriction itself?

>  so a new SELinux
> permission is added to restrict which processes are allowed to initiate
> these charge transfers.

Which is all perfectly reasonable if you have SELinux.

>
> Signed-off-by: T.J. Mercier <tjmercier@google.com>
> ---
>  drivers/android/binder.c            | 5 +++++
>  include/linux/lsm_hook_defs.h       | 2 ++
>  include/linux/lsm_hooks.h           | 6 ++++++
>  include/linux/security.h            | 2 ++
>  security/security.c                 | 6 ++++++
>  security/selinux/hooks.c            | 9 +++++++++
>  security/selinux/include/classmap.h | 2 +-
>  7 files changed, 31 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> index 9830848c8d25..9063db04826d 100644
> --- a/drivers/android/binder.c
> +++ b/drivers/android/binder.c
> @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
>  	if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
>  		struct dma_buf *dmabuf;
>  
> +		if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> +			ret = -EPERM;
> +			goto err_security;
> +		}
> +
>  		if (unlikely(!is_dma_buf_file(file))) {
>  			binder_user_error(
>  				"%d:%d got transaction with XFER_CHARGE for non-dmabuf fd, %d\n",
> diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
> index ed6cb2ac55fa..8db2a958557e 100644
> --- a/include/linux/lsm_hook_defs.h
> +++ b/include/linux/lsm_hook_defs.h
> @@ -33,6 +33,8 @@ 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, binder_transfer_charge, const struct cred *from,
> +	 const struct cred *to)
>  LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child,
>  	 unsigned int mode)
>  LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent)
> diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
> index 0a5ba81f7367..39c40c7bf519 100644
> --- a/include/linux/lsm_hooks.h
> +++ b/include/linux/lsm_hooks.h
> @@ -1385,6 +1385,12 @@
>   *	@file contains the struct file being transferred.
>   *	@to contains the struct cred for the receiving process.
>   *	Return 0 if permission is granted.
> + * @binder_transfer_charge:
> + *	Check whether @from is allowed to transfer the memory charge for a
> + *	buffer out of its cgroup to @to.
> + *	@from contains the struct cred for the sending process.
> + *	@to contains the struct cred for the receiving process.
> + *	Return 0 if permission is granted.
>   *
>   * @ptrace_access_check:
>   *	Check permission before allowing the current process to trace the
> diff --git a/include/linux/security.h b/include/linux/security.h
> index 5b67f208f7de..3b7472308430 100644
> --- a/include/linux/security.h
> +++ b/include/linux/security.h
> @@ -270,6 +270,8 @@ 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_binder_transfer_charge(const struct cred *from,
> +				    const struct cred *to);
>  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,
> diff --git a/security/security.c b/security/security.c
> index d1571900a8c7..97e1e74d1ff2 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -801,6 +801,12 @@ int security_binder_transfer_file(const struct cred *from,
>  	return call_int_hook(binder_transfer_file, 0, from, to, file);
>  }
>  
> +int security_binder_transfer_charge(const struct cred *from,
> +				    const struct cred *to)
> +{
> +	return call_int_hook(binder_transfer_charge, 0, from, to);
> +}
> +
>  int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
>  {
>  	return call_int_hook(ptrace_access_check, 0, child, mode);
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 3c5be76a9199..823ef14924bd 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
>  			    &ad);
>  }
>  
> +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> +{
> +	return avc_has_perm(&selinux_state,
> +			    cred_sid(from), cred_sid(to),
> +			    SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> +			    NULL);
> +}
> +
>  static int selinux_ptrace_access_check(struct task_struct *child,
>  				       unsigned int mode)
>  {
> @@ -7052,6 +7060,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
>  	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
>  	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
>  	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
> +	LSM_HOOK_INIT(binder_transfer_charge, selinux_binder_transfer_charge),
>  
>  	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
>  	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
> diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
> index a3c380775d41..2eef180d10d7 100644
> --- a/security/selinux/include/classmap.h
> +++ b/security/selinux/include/classmap.h
> @@ -172,7 +172,7 @@ const struct security_class_mapping secclass_map[] = {
>  	{ "tun_socket",
>  	  { COMMON_SOCK_PERMS, "attach_queue", NULL } },
>  	{ "binder", { "impersonate", "call", "set_context_mgr", "transfer",
> -		      NULL } },
> +		      "transfer_charge", NULL } },
>  	{ "cap_userns",
>  	  { COMMON_CAP_PERMS, NULL } },
>  	{ "cap2_userns",
T.J. Mercier Jan. 10, 2023, 12:30 a.m. UTC | #2
On Mon, Jan 9, 2023 at 2:28 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
>
> On 1/9/2023 1:38 PM, T.J. Mercier wrote:
> > Any process can cause a memory charge transfer to occur to any other
> > process when transmitting a file descriptor through binder. This should
> > only be possible for central allocator processes,
>
> How is a "central allocator process" identified?

Any process with the transfer_charge permission. On Android this is
the graphics allocator HAL which would have this added to its policy.

> If I have a LSM that
> is not SELinux (e.g. AppArmor, Smack) or no LSM at all, how can/should this
> be enforced?

Sorry, why would you be expecting enforcement with no LSM? Are you
suggesting that this check should be different than the ones that
already exist for Binder here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/lsm_hook_defs.h#n29

> Why isn't binder enforcing this restriction itself?

Binder has no direct knowledge of which process has been designated as
an allocator / charge transferrer. That is defined externally by
whoever configures the system.

> >  so a new SELinux
> > permission is added to restrict which processes are allowed to initiate
> > these charge transfers.
>
> Which is all perfectly reasonable if you have SELinux.
>
> >
> > Signed-off-by: T.J. Mercier <tjmercier@google.com>
> > ---
> >  drivers/android/binder.c            | 5 +++++
> >  include/linux/lsm_hook_defs.h       | 2 ++
> >  include/linux/lsm_hooks.h           | 6 ++++++
> >  include/linux/security.h            | 2 ++
> >  security/security.c                 | 6 ++++++
> >  security/selinux/hooks.c            | 9 +++++++++
> >  security/selinux/include/classmap.h | 2 +-
> >  7 files changed, 31 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> > index 9830848c8d25..9063db04826d 100644
> > --- a/drivers/android/binder.c
> > +++ b/drivers/android/binder.c
> > @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
> >       if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
> >               struct dma_buf *dmabuf;
> >
> > +             if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> > +                     ret = -EPERM;
> > +                     goto err_security;
> > +             }
> > +
> >               if (unlikely(!is_dma_buf_file(file))) {
> >                       binder_user_error(
> >                               "%d:%d got transaction with XFER_CHARGE for non-dmabuf fd, %d\n",
> > diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
> > index ed6cb2ac55fa..8db2a958557e 100644
> > --- a/include/linux/lsm_hook_defs.h
> > +++ b/include/linux/lsm_hook_defs.h
> > @@ -33,6 +33,8 @@ 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, binder_transfer_charge, const struct cred *from,
> > +      const struct cred *to)
> >  LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child,
> >        unsigned int mode)
> >  LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent)
> > diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
> > index 0a5ba81f7367..39c40c7bf519 100644
> > --- a/include/linux/lsm_hooks.h
> > +++ b/include/linux/lsm_hooks.h
> > @@ -1385,6 +1385,12 @@
> >   *   @file contains the struct file being transferred.
> >   *   @to contains the struct cred for the receiving process.
> >   *   Return 0 if permission is granted.
> > + * @binder_transfer_charge:
> > + *   Check whether @from is allowed to transfer the memory charge for a
> > + *   buffer out of its cgroup to @to.
> > + *   @from contains the struct cred for the sending process.
> > + *   @to contains the struct cred for the receiving process.
> > + *   Return 0 if permission is granted.
> >   *
> >   * @ptrace_access_check:
> >   *   Check permission before allowing the current process to trace the
> > diff --git a/include/linux/security.h b/include/linux/security.h
> > index 5b67f208f7de..3b7472308430 100644
> > --- a/include/linux/security.h
> > +++ b/include/linux/security.h
> > @@ -270,6 +270,8 @@ 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_binder_transfer_charge(const struct cred *from,
> > +                                 const struct cred *to);
> >  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,
> > diff --git a/security/security.c b/security/security.c
> > index d1571900a8c7..97e1e74d1ff2 100644
> > --- a/security/security.c
> > +++ b/security/security.c
> > @@ -801,6 +801,12 @@ int security_binder_transfer_file(const struct cred *from,
> >       return call_int_hook(binder_transfer_file, 0, from, to, file);
> >  }
> >
> > +int security_binder_transfer_charge(const struct cred *from,
> > +                                 const struct cred *to)
> > +{
> > +     return call_int_hook(binder_transfer_charge, 0, from, to);
> > +}
> > +
> >  int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
> >  {
> >       return call_int_hook(ptrace_access_check, 0, child, mode);
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index 3c5be76a9199..823ef14924bd 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
> >                           &ad);
> >  }
> >
> > +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> > +{
> > +     return avc_has_perm(&selinux_state,
> > +                         cred_sid(from), cred_sid(to),
> > +                         SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> > +                         NULL);
> > +}
> > +
> >  static int selinux_ptrace_access_check(struct task_struct *child,
> >                                      unsigned int mode)
> >  {
> > @@ -7052,6 +7060,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
> >       LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
> >       LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
> >       LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
> > +     LSM_HOOK_INIT(binder_transfer_charge, selinux_binder_transfer_charge),
> >
> >       LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
> >       LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
> > diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
> > index a3c380775d41..2eef180d10d7 100644
> > --- a/security/selinux/include/classmap.h
> > +++ b/security/selinux/include/classmap.h
> > @@ -172,7 +172,7 @@ const struct security_class_mapping secclass_map[] = {
> >       { "tun_socket",
> >         { COMMON_SOCK_PERMS, "attach_queue", NULL } },
> >       { "binder", { "impersonate", "call", "set_context_mgr", "transfer",
> > -                   NULL } },
> > +                   "transfer_charge", NULL } },
> >       { "cap_userns",
> >         { COMMON_CAP_PERMS, NULL } },
> >       { "cap2_userns",
Casey Schaufler Jan. 10, 2023, 7:39 p.m. UTC | #3
On 1/9/2023 4:30 PM, T.J. Mercier wrote:
> On Mon, Jan 9, 2023 at 2:28 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
>> On 1/9/2023 1:38 PM, T.J. Mercier wrote:
>>> Any process can cause a memory charge transfer to occur to any other
>>> process when transmitting a file descriptor through binder. This should
>>> only be possible for central allocator processes,
>> How is a "central allocator process" identified?
> Any process with the transfer_charge permission. On Android this is
> the graphics allocator HAL which would have this added to its policy.

OK. You're putting SELinux policy directly into the name of the LSM hook.

>
>> If I have a LSM that
>> is not SELinux (e.g. AppArmor, Smack) or no LSM at all, how can/should this
>> be enforced?
> Sorry, why would you be expecting enforcement with no LSM?

Because the LSM is supposed to be a set of *additional* restrictions.
If there are no restrictions when there's no LSM, you can't add to
existing restrictions. If binder works correctly without any restrictions
that's fine. It seems odd that you'd add SELinux restrictions if there
are no basic restrictions. If, on the other hand, binder doesn't have
native restrictions because it always assumes SELinux, it ought to have
a CONFIG dependency on SELinux.

None of which is really important.

>  Are you
> suggesting that this check should be different than the ones that
> already exist for Binder here?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/lsm_hook_defs.h#n29

This one seems just a little bit more like an implementation of
SELinux policy in the hook than some of the others. If there is no way
to identify the special process without SELinux policy it's going to
be really difficult for a different LSM to utilize the hook.

>
>> Why isn't binder enforcing this restriction itself?
> Binder has no direct knowledge of which process has been designated as
> an allocator / charge transferrer. That is defined externally by
> whoever configures the system.

So the attribute isn't a binder attribute, it's an SELinux attribute?
That isn't appropriate in the LSM interface, at least not explicitly.

>
>>>  so a new SELinux
>>> permission is added to restrict which processes are allowed to initiate
>>> these charge transfers.
>> Which is all perfectly reasonable if you have SELinux.
>>
>>> Signed-off-by: T.J. Mercier <tjmercier@google.com>
>>> ---
>>>  drivers/android/binder.c            | 5 +++++
>>>  include/linux/lsm_hook_defs.h       | 2 ++
>>>  include/linux/lsm_hooks.h           | 6 ++++++
>>>  include/linux/security.h            | 2 ++
>>>  security/security.c                 | 6 ++++++
>>>  security/selinux/hooks.c            | 9 +++++++++
>>>  security/selinux/include/classmap.h | 2 +-
>>>  7 files changed, 31 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/android/binder.c b/drivers/android/binder.c
>>> index 9830848c8d25..9063db04826d 100644
>>> --- a/drivers/android/binder.c
>>> +++ b/drivers/android/binder.c
>>> @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
>>>       if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
>>>               struct dma_buf *dmabuf;
>>>
>>> +             if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
>>> +                     ret = -EPERM;
>>> +                     goto err_security;
>>> +             }
>>> +
>>>               if (unlikely(!is_dma_buf_file(file))) {
>>>                       binder_user_error(
>>>                               "%d:%d got transaction with XFER_CHARGE for non-dmabuf fd, %d\n",
>>> diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
>>> index ed6cb2ac55fa..8db2a958557e 100644
>>> --- a/include/linux/lsm_hook_defs.h
>>> +++ b/include/linux/lsm_hook_defs.h
>>> @@ -33,6 +33,8 @@ 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, binder_transfer_charge, const struct cred *from,
>>> +      const struct cred *to)
>>>  LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child,
>>>        unsigned int mode)
>>>  LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent)
>>> diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
>>> index 0a5ba81f7367..39c40c7bf519 100644
>>> --- a/include/linux/lsm_hooks.h
>>> +++ b/include/linux/lsm_hooks.h
>>> @@ -1385,6 +1385,12 @@
>>>   *   @file contains the struct file being transferred.
>>>   *   @to contains the struct cred for the receiving process.
>>>   *   Return 0 if permission is granted.
>>> + * @binder_transfer_charge:
>>> + *   Check whether @from is allowed to transfer the memory charge for a
>>> + *   buffer out of its cgroup to @to.
>>> + *   @from contains the struct cred for the sending process.
>>> + *   @to contains the struct cred for the receiving process.
>>> + *   Return 0 if permission is granted.
>>>   *
>>>   * @ptrace_access_check:
>>>   *   Check permission before allowing the current process to trace the
>>> diff --git a/include/linux/security.h b/include/linux/security.h
>>> index 5b67f208f7de..3b7472308430 100644
>>> --- a/include/linux/security.h
>>> +++ b/include/linux/security.h
>>> @@ -270,6 +270,8 @@ 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_binder_transfer_charge(const struct cred *from,
>>> +                                 const struct cred *to);
>>>  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,
>>> diff --git a/security/security.c b/security/security.c
>>> index d1571900a8c7..97e1e74d1ff2 100644
>>> --- a/security/security.c
>>> +++ b/security/security.c
>>> @@ -801,6 +801,12 @@ int security_binder_transfer_file(const struct cred *from,
>>>       return call_int_hook(binder_transfer_file, 0, from, to, file);
>>>  }
>>>
>>> +int security_binder_transfer_charge(const struct cred *from,
>>> +                                 const struct cred *to)
>>> +{
>>> +     return call_int_hook(binder_transfer_charge, 0, from, to);
>>> +}
>>> +
>>>  int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
>>>  {
>>>       return call_int_hook(ptrace_access_check, 0, child, mode);
>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>> index 3c5be76a9199..823ef14924bd 100644
>>> --- a/security/selinux/hooks.c
>>> +++ b/security/selinux/hooks.c
>>> @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
>>>                           &ad);
>>>  }
>>>
>>> +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
>>> +{
>>> +     return avc_has_perm(&selinux_state,
>>> +                         cred_sid(from), cred_sid(to),
>>> +                         SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
>>> +                         NULL);
>>> +}
>>> +
>>>  static int selinux_ptrace_access_check(struct task_struct *child,
>>>                                      unsigned int mode)
>>>  {
>>> @@ -7052,6 +7060,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
>>>       LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
>>>       LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
>>>       LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
>>> +     LSM_HOOK_INIT(binder_transfer_charge, selinux_binder_transfer_charge),
>>>
>>>       LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
>>>       LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
>>> diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
>>> index a3c380775d41..2eef180d10d7 100644
>>> --- a/security/selinux/include/classmap.h
>>> +++ b/security/selinux/include/classmap.h
>>> @@ -172,7 +172,7 @@ const struct security_class_mapping secclass_map[] = {
>>>       { "tun_socket",
>>>         { COMMON_SOCK_PERMS, "attach_queue", NULL } },
>>>       { "binder", { "impersonate", "call", "set_context_mgr", "transfer",
>>> -                   NULL } },
>>> +                   "transfer_charge", NULL } },
>>>       { "cap_userns",
>>>         { COMMON_CAP_PERMS, NULL } },
>>>       { "cap2_userns",
Paul Moore Jan. 11, 2023, 11 p.m. UTC | #4
On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <tjmercier@google.com> wrote:
>
> Any process can cause a memory charge transfer to occur to any other
> process when transmitting a file descriptor through binder. This should
> only be possible for central allocator processes, so a new SELinux
> permission is added to restrict which processes are allowed to initiate
> these charge transfers.
>
> Signed-off-by: T.J. Mercier <tjmercier@google.com>
> ---
>  drivers/android/binder.c            | 5 +++++
>  include/linux/lsm_hook_defs.h       | 2 ++
>  include/linux/lsm_hooks.h           | 6 ++++++
>  include/linux/security.h            | 2 ++
>  security/security.c                 | 6 ++++++
>  security/selinux/hooks.c            | 9 +++++++++
>  security/selinux/include/classmap.h | 2 +-
>  7 files changed, 31 insertions(+), 1 deletion(-)

Hi T.J.,

A few things come to mind when looking at this patchset, but let me
start with the big one first: you only sent 0/4 and 4/4 to the LSM and
SELinux lists, so that's all I'm seeing in my inbox to review, and
it's hard to make sense of what you want to do with just these
snippets.  This makes me cranky, and less inclined to spend the time
to give this a proper review, because there are plenty of other things
which need attention and don't require me having to hunt down missing
pieces.  Yes, I'm aware of b4/lei, and while they are great tools, my
workflow was pretty well established before they came into existence
and I still do things the good ol' fashioned way with mailing lists,
etc.

Make the patch reviewer's life easy whenever you can, it will rarely
(ever?) backfire, I promise.

> diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> index 9830848c8d25..9063db04826d 100644
> --- a/drivers/android/binder.c
> +++ b/drivers/android/binder.c
> @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
>         if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
>                 struct dma_buf *dmabuf;
>
> +               if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> +                       ret = -EPERM;
> +                       goto err_security;
> +               }

This is where I believe I'm missing the proper context, as this
version of binder_translate_fd() differs from what I see in Linus'
tree.  However, the version in Linus' tree does have a LSM hook,
security_binder_transfer_file(), which is passed both the credentials
you are using above and based solely on the level of indentation shown
in the chunk of code above, it seems like the existing hook might be
suitable?

> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 3c5be76a9199..823ef14924bd 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
>                             &ad);
>  }
>
> +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> +{
> +       return avc_has_perm(&selinux_state,
> +                           cred_sid(from), cred_sid(to),
> +                           SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> +                           NULL);
> +}

Generally speaking SELinux doesn't really worry about resource
accounting controls so this seems a bit out of place, but perhaps the
larger question is do you see this being sufficiently distinct from
the existing binder:transfer permission?  In other words, would you
ever want to grant a domain the ability to transfer a file *without*
also granting it the ability to transfer the memory charge?  You need
to help me explain why we need an additional permission for this,
because I don't currently see the need.

>  static int selinux_ptrace_access_check(struct task_struct *child,
>                                        unsigned int mode)
>  {
> @@ -7052,6 +7060,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
>         LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
>         LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
>         LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
> +       LSM_HOOK_INIT(binder_transfer_charge, selinux_binder_transfer_charge),
>
>         LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
>         LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
> diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
> index a3c380775d41..2eef180d10d7 100644
> --- a/security/selinux/include/classmap.h
> +++ b/security/selinux/include/classmap.h
> @@ -172,7 +172,7 @@ const struct security_class_mapping secclass_map[] = {
>         { "tun_socket",
>           { COMMON_SOCK_PERMS, "attach_queue", NULL } },
>         { "binder", { "impersonate", "call", "set_context_mgr", "transfer",
> -                     NULL } },
> +                     "transfer_charge", NULL } },
>         { "cap_userns",
>           { COMMON_CAP_PERMS, NULL } },
>         { "cap2_userns",
> --
> 2.39.0.314.g84b9a713c41-goog

--
paul-moore.com
T.J. Mercier Jan. 12, 2023, 12:21 a.m. UTC | #5
On Tue, Jan 10, 2023 at 11:39 AM Casey Schaufler <casey@schaufler-ca.com> wrote:
>
> On 1/9/2023 4:30 PM, T.J. Mercier wrote:
> > On Mon, Jan 9, 2023 at 2:28 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
> >> On 1/9/2023 1:38 PM, T.J. Mercier wrote:
> >>> Any process can cause a memory charge transfer to occur to any other
> >>> process when transmitting a file descriptor through binder. This should
> >>> only be possible for central allocator processes,
> >> How is a "central allocator process" identified?
> > Any process with the transfer_charge permission. On Android this is
> > the graphics allocator HAL which would have this added to its policy.
>
> OK. You're putting SELinux policy directly into the name of the LSM hook.
>
> >
> >> If I have a LSM that
> >> is not SELinux (e.g. AppArmor, Smack) or no LSM at all, how can/should this
> >> be enforced?
> > Sorry, why would you be expecting enforcement with no LSM?
>
> Because the LSM is supposed to be a set of *additional* restrictions.
> If there are no restrictions when there's no LSM, you can't add to
> existing restrictions. If binder works correctly without any restrictions
> that's fine. It seems odd that you'd add SELinux restrictions if there
> are no basic restrictions. If, on the other hand, binder doesn't have
> native restrictions because it always assumes SELinux, it ought to have
> a CONFIG dependency on SELinux.
>
> None of which is really important.
>
> >  Are you
> > suggesting that this check should be different than the ones that
> > already exist for Binder here?
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/lsm_hook_defs.h#n29
>
> This one seems just a little bit more like an implementation of
> SELinux policy in the hook than some of the others. If there is no way
> to identify the special process without SELinux policy it's going to
> be really difficult for a different LSM to utilize the hook.
>
I think I see what you're saying... there is potentially no way to
have an equivalent attribute with other LSMs?

> >
> >> Why isn't binder enforcing this restriction itself?
> > Binder has no direct knowledge of which process has been designated as
> > an allocator / charge transferrer. That is defined externally by
> > whoever configures the system.
>
> So the attribute isn't a binder attribute, it's an SELinux attribute?
> That isn't appropriate in the LSM interface, at least not explicitly.
>
The transfer feature is something offered by binder, but mapped to a
process only in the SELinux policy.

> >
> >>>  so a new SELinux
> >>> permission is added to restrict which processes are allowed to initiate
> >>> these charge transfers.
> >> Which is all perfectly reasonable if you have SELinux.
> >>
> >>> Signed-off-by: T.J. Mercier <tjmercier@google.com>
> >>> ---
> >>>  drivers/android/binder.c            | 5 +++++
> >>>  include/linux/lsm_hook_defs.h       | 2 ++
> >>>  include/linux/lsm_hooks.h           | 6 ++++++
> >>>  include/linux/security.h            | 2 ++
> >>>  security/security.c                 | 6 ++++++
> >>>  security/selinux/hooks.c            | 9 +++++++++
> >>>  security/selinux/include/classmap.h | 2 +-
> >>>  7 files changed, 31 insertions(+), 1 deletion(-)
> >>>
> >>> diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> >>> index 9830848c8d25..9063db04826d 100644
> >>> --- a/drivers/android/binder.c
> >>> +++ b/drivers/android/binder.c
> >>> @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
> >>>       if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
> >>>               struct dma_buf *dmabuf;
> >>>
> >>> +             if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> >>> +                     ret = -EPERM;
> >>> +                     goto err_security;
> >>> +             }
> >>> +
> >>>               if (unlikely(!is_dma_buf_file(file))) {
> >>>                       binder_user_error(
> >>>                               "%d:%d got transaction with XFER_CHARGE for non-dmabuf fd, %d\n",
> >>> diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
> >>> index ed6cb2ac55fa..8db2a958557e 100644
> >>> --- a/include/linux/lsm_hook_defs.h
> >>> +++ b/include/linux/lsm_hook_defs.h
> >>> @@ -33,6 +33,8 @@ 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, binder_transfer_charge, const struct cred *from,
> >>> +      const struct cred *to)
> >>>  LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child,
> >>>        unsigned int mode)
> >>>  LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent)
> >>> diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
> >>> index 0a5ba81f7367..39c40c7bf519 100644
> >>> --- a/include/linux/lsm_hooks.h
> >>> +++ b/include/linux/lsm_hooks.h
> >>> @@ -1385,6 +1385,12 @@
> >>>   *   @file contains the struct file being transferred.
> >>>   *   @to contains the struct cred for the receiving process.
> >>>   *   Return 0 if permission is granted.
> >>> + * @binder_transfer_charge:
> >>> + *   Check whether @from is allowed to transfer the memory charge for a
> >>> + *   buffer out of its cgroup to @to.
> >>> + *   @from contains the struct cred for the sending process.
> >>> + *   @to contains the struct cred for the receiving process.
> >>> + *   Return 0 if permission is granted.
> >>>   *
> >>>   * @ptrace_access_check:
> >>>   *   Check permission before allowing the current process to trace the
> >>> diff --git a/include/linux/security.h b/include/linux/security.h
> >>> index 5b67f208f7de..3b7472308430 100644
> >>> --- a/include/linux/security.h
> >>> +++ b/include/linux/security.h
> >>> @@ -270,6 +270,8 @@ 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_binder_transfer_charge(const struct cred *from,
> >>> +                                 const struct cred *to);
> >>>  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,
> >>> diff --git a/security/security.c b/security/security.c
> >>> index d1571900a8c7..97e1e74d1ff2 100644
> >>> --- a/security/security.c
> >>> +++ b/security/security.c
> >>> @@ -801,6 +801,12 @@ int security_binder_transfer_file(const struct cred *from,
> >>>       return call_int_hook(binder_transfer_file, 0, from, to, file);
> >>>  }
> >>>
> >>> +int security_binder_transfer_charge(const struct cred *from,
> >>> +                                 const struct cred *to)
> >>> +{
> >>> +     return call_int_hook(binder_transfer_charge, 0, from, to);
> >>> +}
> >>> +
> >>>  int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
> >>>  {
> >>>       return call_int_hook(ptrace_access_check, 0, child, mode);
> >>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> >>> index 3c5be76a9199..823ef14924bd 100644
> >>> --- a/security/selinux/hooks.c
> >>> +++ b/security/selinux/hooks.c
> >>> @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
> >>>                           &ad);
> >>>  }
> >>>
> >>> +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> >>> +{
> >>> +     return avc_has_perm(&selinux_state,
> >>> +                         cred_sid(from), cred_sid(to),
> >>> +                         SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> >>> +                         NULL);
> >>> +}
> >>> +
> >>>  static int selinux_ptrace_access_check(struct task_struct *child,
> >>>                                      unsigned int mode)
> >>>  {
> >>> @@ -7052,6 +7060,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
> >>>       LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
> >>>       LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
> >>>       LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
> >>> +     LSM_HOOK_INIT(binder_transfer_charge, selinux_binder_transfer_charge),
> >>>
> >>>       LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
> >>>       LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
> >>> diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
> >>> index a3c380775d41..2eef180d10d7 100644
> >>> --- a/security/selinux/include/classmap.h
> >>> +++ b/security/selinux/include/classmap.h
> >>> @@ -172,7 +172,7 @@ const struct security_class_mapping secclass_map[] = {
> >>>       { "tun_socket",
> >>>         { COMMON_SOCK_PERMS, "attach_queue", NULL } },
> >>>       { "binder", { "impersonate", "call", "set_context_mgr", "transfer",
> >>> -                   NULL } },
> >>> +                   "transfer_charge", NULL } },
> >>>       { "cap_userns",
> >>>         { COMMON_CAP_PERMS, NULL } },
> >>>       { "cap2_userns",
T.J. Mercier Jan. 12, 2023, 12:21 a.m. UTC | #6
On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <paul@paul-moore.com> wrote:
>
> On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <tjmercier@google.com> wrote:
> >
> > Any process can cause a memory charge transfer to occur to any other
> > process when transmitting a file descriptor through binder. This should
> > only be possible for central allocator processes, so a new SELinux
> > permission is added to restrict which processes are allowed to initiate
> > these charge transfers.
> >
> > Signed-off-by: T.J. Mercier <tjmercier@google.com>
> > ---
> >  drivers/android/binder.c            | 5 +++++
> >  include/linux/lsm_hook_defs.h       | 2 ++
> >  include/linux/lsm_hooks.h           | 6 ++++++
> >  include/linux/security.h            | 2 ++
> >  security/security.c                 | 6 ++++++
> >  security/selinux/hooks.c            | 9 +++++++++
> >  security/selinux/include/classmap.h | 2 +-
> >  7 files changed, 31 insertions(+), 1 deletion(-)
>
> Hi T.J.,
>
> A few things come to mind when looking at this patchset, but let me
> start with the big one first: you only sent 0/4 and 4/4 to the LSM and
> SELinux lists, so that's all I'm seeing in my inbox to review, and
> it's hard to make sense of what you want to do with just these
> snippets.  This makes me cranky, and less inclined to spend the time
> to give this a proper review, because there are plenty of other things
> which need attention and don't require me having to hunt down missing
> pieces.  Yes, I'm aware of b4/lei, and while they are great tools, my
> workflow was pretty well established before they came into existence
> and I still do things the good ol' fashioned way with mailing lists,
> etc.
>
> Make the patch reviewer's life easy whenever you can, it will rarely
> (ever?) backfire, I promise.
>
Hi Paul, sorry about that. I have git send-email calling
get_maintainer.pl to automatically figure out the recipients, and I
think that's why it only sent particular patches to a subset of lists.
Looks like the list of recipients for each patch should be a union of
all patches. Thank you for taking a look anyway! Here's a lore link:
https://lore.kernel.org/lkml/20230109213809.418135-1-tjmercier@google.com/

> > diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> > index 9830848c8d25..9063db04826d 100644
> > --- a/drivers/android/binder.c
> > +++ b/drivers/android/binder.c
> > @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
> >         if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
> >                 struct dma_buf *dmabuf;
> >
> > +               if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> > +                       ret = -EPERM;
> > +                       goto err_security;
> > +               }
>
> This is where I believe I'm missing the proper context, as this
> version of binder_translate_fd() differs from what I see in Linus'
> tree.  However, the version in Linus' tree does have a LSM hook,
> security_binder_transfer_file(), which is passed both the credentials
> you are using above and based solely on the level of indentation shown
> in the chunk of code above, it seems like the existing hook might be
> suitable?
>
Yes, patch 3 plumbs through flags to this function:
https://lore.kernel.org/lkml/20230109213809.418135-4-tjmercier@google.com/

I don't think the existing hook is suitable, which I've tried to explain below.

> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index 3c5be76a9199..823ef14924bd 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
> >                             &ad);
> >  }
> >
> > +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> > +{
> > +       return avc_has_perm(&selinux_state,
> > +                           cred_sid(from), cred_sid(to),
> > +                           SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> > +                           NULL);
> > +}
>
> Generally speaking SELinux doesn't really worry about resource
> accounting controls so this seems a bit out of place, but perhaps the
> larger question is do you see this being sufficiently distinct from
> the existing binder:transfer permission?  In other words, would you
> ever want to grant a domain the ability to transfer a file *without*
> also granting it the ability to transfer the memory charge?  You need
> to help me explain why we need an additional permission for this,
> because I don't currently see the need.
>
Yes, and that's actually more often the case than not. A file here
means a file descriptor that points at any type of resource: file on
disk, memfd, dmabuf, etc. Currently there exists policy that restricts
which processes are allowed to interact with FDs over binder using the
security_binder_transfer_file hook you reference. [1] However this new
transfer_charge permission is meant to restrict the ability of a FD
sender to transfer the memory charge associated with that FD (if one
exists) to a recipient (who may or may not want to accept the memory
charge). So the memory charge is independent of (potentially one-time,
read-only) access to the FD. I thought this would be a good idea for
two reasons:
1) The recipient has no control over whether to accept the memory
charge or not. If the recipient shouldn't have the memory charge
associated with a shared/loaned resource attributed to it, the
recipient doesn't have a way to prevent that. When the charge is
transferred, it could initiate memory reclaim on the recipient, or
make it a bigger target for a future process kill than it should be.
2) Only certain senders should be doing this. Without this control,
any sender could circumvent process memory limits by transferring
memory charges that should be attributed to them onto others.

[1] https://cs.android.com/search?q=%22fd%20use%22&ss=android%2Fplatform%2Fsuperproject:system%2Fsepolicy%2F



> >  static int selinux_ptrace_access_check(struct task_struct *child,
> >                                        unsigned int mode)
> >  {
> > @@ -7052,6 +7060,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
> >         LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
> >         LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
> >         LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
> > +       LSM_HOOK_INIT(binder_transfer_charge, selinux_binder_transfer_charge),
> >
> >         LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
> >         LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
> > diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
> > index a3c380775d41..2eef180d10d7 100644
> > --- a/security/selinux/include/classmap.h
> > +++ b/security/selinux/include/classmap.h
> > @@ -172,7 +172,7 @@ const struct security_class_mapping secclass_map[] = {
> >         { "tun_socket",
> >           { COMMON_SOCK_PERMS, "attach_queue", NULL } },
> >         { "binder", { "impersonate", "call", "set_context_mgr", "transfer",
> > -                     NULL } },
> > +                     "transfer_charge", NULL } },
> >         { "cap_userns",
> >           { COMMON_CAP_PERMS, NULL } },
> >         { "cap2_userns",
> > --
> > 2.39.0.314.g84b9a713c41-goog
>
> --
> paul-moore.com
Paul Moore Jan. 12, 2023, 8:45 p.m. UTC | #7
On Wed, Jan 11, 2023 at 7:21 PM T.J. Mercier <tjmercier@google.com> wrote:
>
> On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <paul@paul-moore.com> wrote:
> >
> > On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <tjmercier@google.com> wrote:
> > >
> > > Any process can cause a memory charge transfer to occur to any other
> > > process when transmitting a file descriptor through binder. This should
> > > only be possible for central allocator processes, so a new SELinux
> > > permission is added to restrict which processes are allowed to initiate
> > > these charge transfers.
> > >
> > > Signed-off-by: T.J. Mercier <tjmercier@google.com>
> > > ---
> > >  drivers/android/binder.c            | 5 +++++
> > >  include/linux/lsm_hook_defs.h       | 2 ++
> > >  include/linux/lsm_hooks.h           | 6 ++++++
> > >  include/linux/security.h            | 2 ++
> > >  security/security.c                 | 6 ++++++
> > >  security/selinux/hooks.c            | 9 +++++++++
> > >  security/selinux/include/classmap.h | 2 +-
> > >  7 files changed, 31 insertions(+), 1 deletion(-)
> >
> > Hi T.J.,
> >
> > A few things come to mind when looking at this patchset, but let me
> > start with the big one first: you only sent 0/4 and 4/4 to the LSM and
> > SELinux lists, so that's all I'm seeing in my inbox to review, and
> > it's hard to make sense of what you want to do with just these
> > snippets.  This makes me cranky, and less inclined to spend the time
> > to give this a proper review, because there are plenty of other things
> > which need attention and don't require me having to hunt down missing
> > pieces.  Yes, I'm aware of b4/lei, and while they are great tools, my
> > workflow was pretty well established before they came into existence
> > and I still do things the good ol' fashioned way with mailing lists,
> > etc.
> >
> > Make the patch reviewer's life easy whenever you can, it will rarely
> > (ever?) backfire, I promise.
>
> Hi Paul, sorry about that. I have git send-email calling
> get_maintainer.pl to automatically figure out the recipients, and I
> think that's why it only sent particular patches to a subset of lists.
> Looks like the list of recipients for each patch should be a union of
> all patches. Thank you for taking a look anyway! Here's a lore link:
> https://lore.kernel.org/lkml/20230109213809.418135-1-tjmercier@google.com/
>
> > > diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> > > index 9830848c8d25..9063db04826d 100644
> > > --- a/drivers/android/binder.c
> > > +++ b/drivers/android/binder.c
> > > @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
> > >         if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
> > >                 struct dma_buf *dmabuf;
> > >
> > > +               if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> > > +                       ret = -EPERM;
> > > +                       goto err_security;
> > > +               }
> >
> > This is where I believe I'm missing the proper context, as this
> > version of binder_translate_fd() differs from what I see in Linus'
> > tree.  However, the version in Linus' tree does have a LSM hook,
> > security_binder_transfer_file(), which is passed both the credentials
> > you are using above and based solely on the level of indentation shown
> > in the chunk of code above, it seems like the existing hook might be
> > suitable?
>
> Yes, patch 3 plumbs through flags to this function:
> https://lore.kernel.org/lkml/20230109213809.418135-4-tjmercier@google.com/
>
> I don't think the existing hook is suitable, which I've tried to explain below.

In this particular case the issue of what permission checks are done
for a given LSM, SELinux in this case, appears to be independent of if
we need a new, different, or second LSM hook.  Unless I missed
something the only real difference with this new hook is that is sits
behind a conditional checking if memory control groups are enabled and
if a transfer charge was specified; it seems like passing the @flags
parameter into the existing LSM hook would allow you to use the
existing hook (it is called before the new hook, right?)?

> > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > > index 3c5be76a9199..823ef14924bd 100644
> > > --- a/security/selinux/hooks.c
> > > +++ b/security/selinux/hooks.c
> > > @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
> > >                             &ad);
> > >  }
> > >
> > > +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> > > +{
> > > +       return avc_has_perm(&selinux_state,
> > > +                           cred_sid(from), cred_sid(to),
> > > +                           SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> > > +                           NULL);
> > > +}
> >
> > Generally speaking SELinux doesn't really worry about resource
> > accounting controls so this seems a bit out of place, but perhaps the
> > larger question is do you see this being sufficiently distinct from
> > the existing binder:transfer permission?  In other words, would you
> > ever want to grant a domain the ability to transfer a file *without*
> > also granting it the ability to transfer the memory charge?  You need
> > to help me explain why we need an additional permission for this,
> > because I don't currently see the need.
> >
> Yes, and that's actually more often the case than not. A file here
> means a file descriptor that points at any type of resource: file on
> disk, memfd, dmabuf, etc. Currently there exists policy that restricts
> which processes are allowed to interact with FDs over binder using the
> security_binder_transfer_file hook you reference. [1] However this new
> transfer_charge permission is meant to restrict the ability of a FD
> sender to transfer the memory charge associated with that FD (if one
> exists) to a recipient (who may or may not want to accept the memory
> charge). So the memory charge is independent of (potentially one-time,
> read-only) access to the FD.

Without a more comprehensive set of LSM/SELinux access controls around
resource management (which would need discussion beyond just this
thread/patch) I'm not sure we want to start patching in one-off
controls like this.

I haven't looked, but are there any traditional/DAC access controls
around transfering memory changes from one task to another?  It seems
like there *should* be, and if so, it seems like that would be the
right approach at the moment ... if you're not already doing that in
the other patches in the patchset.
T.J. Mercier Jan. 12, 2023, 9:36 p.m. UTC | #8
On Thu, Jan 12, 2023 at 12:45 PM Paul Moore <paul@paul-moore.com> wrote:
>
> On Wed, Jan 11, 2023 at 7:21 PM T.J. Mercier <tjmercier@google.com> wrote:
> >
> > On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <paul@paul-moore.com> wrote:
> > >
> > > On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <tjmercier@google.com> wrote:
> > > >
> > > > Any process can cause a memory charge transfer to occur to any other
> > > > process when transmitting a file descriptor through binder. This should
> > > > only be possible for central allocator processes, so a new SELinux
> > > > permission is added to restrict which processes are allowed to initiate
> > > > these charge transfers.
> > > >
> > > > Signed-off-by: T.J. Mercier <tjmercier@google.com>
> > > > ---
> > > >  drivers/android/binder.c            | 5 +++++
> > > >  include/linux/lsm_hook_defs.h       | 2 ++
> > > >  include/linux/lsm_hooks.h           | 6 ++++++
> > > >  include/linux/security.h            | 2 ++
> > > >  security/security.c                 | 6 ++++++
> > > >  security/selinux/hooks.c            | 9 +++++++++
> > > >  security/selinux/include/classmap.h | 2 +-
> > > >  7 files changed, 31 insertions(+), 1 deletion(-)
> > >
> > > Hi T.J.,
> > >
> > > A few things come to mind when looking at this patchset, but let me
> > > start with the big one first: you only sent 0/4 and 4/4 to the LSM and
> > > SELinux lists, so that's all I'm seeing in my inbox to review, and
> > > it's hard to make sense of what you want to do with just these
> > > snippets.  This makes me cranky, and less inclined to spend the time
> > > to give this a proper review, because there are plenty of other things
> > > which need attention and don't require me having to hunt down missing
> > > pieces.  Yes, I'm aware of b4/lei, and while they are great tools, my
> > > workflow was pretty well established before they came into existence
> > > and I still do things the good ol' fashioned way with mailing lists,
> > > etc.
> > >
> > > Make the patch reviewer's life easy whenever you can, it will rarely
> > > (ever?) backfire, I promise.
> >
> > Hi Paul, sorry about that. I have git send-email calling
> > get_maintainer.pl to automatically figure out the recipients, and I
> > think that's why it only sent particular patches to a subset of lists.
> > Looks like the list of recipients for each patch should be a union of
> > all patches. Thank you for taking a look anyway! Here's a lore link:
> > https://lore.kernel.org/lkml/20230109213809.418135-1-tjmercier@google.com/
> >
> > > > diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> > > > index 9830848c8d25..9063db04826d 100644
> > > > --- a/drivers/android/binder.c
> > > > +++ b/drivers/android/binder.c
> > > > @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
> > > >         if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
> > > >                 struct dma_buf *dmabuf;
> > > >
> > > > +               if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> > > > +                       ret = -EPERM;
> > > > +                       goto err_security;
> > > > +               }
> > >
> > > This is where I believe I'm missing the proper context, as this
> > > version of binder_translate_fd() differs from what I see in Linus'
> > > tree.  However, the version in Linus' tree does have a LSM hook,
> > > security_binder_transfer_file(), which is passed both the credentials
> > > you are using above and based solely on the level of indentation shown
> > > in the chunk of code above, it seems like the existing hook might be
> > > suitable?
> >
> > Yes, patch 3 plumbs through flags to this function:
> > https://lore.kernel.org/lkml/20230109213809.418135-4-tjmercier@google.com/
> >
> > I don't think the existing hook is suitable, which I've tried to explain below.
>
> In this particular case the issue of what permission checks are done
> for a given LSM, SELinux in this case, appears to be independent of if
> we need a new, different, or second LSM hook.  Unless I missed
> something the only real difference with this new hook is that is sits
> behind a conditional checking if memory control groups are enabled and
> if a transfer charge was specified; it seems like passing the @flags
> parameter into the existing LSM hook would allow you to use the
> existing hook (it is called before the new hook, right?)?
>
Ah yes, that sounds like it would work. Thank you.

> > > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > > > index 3c5be76a9199..823ef14924bd 100644
> > > > --- a/security/selinux/hooks.c
> > > > +++ b/security/selinux/hooks.c
> > > > @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
> > > >                             &ad);
> > > >  }
> > > >
> > > > +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> > > > +{
> > > > +       return avc_has_perm(&selinux_state,
> > > > +                           cred_sid(from), cred_sid(to),
> > > > +                           SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> > > > +                           NULL);
> > > > +}
> > >
> > > Generally speaking SELinux doesn't really worry about resource
> > > accounting controls so this seems a bit out of place, but perhaps the
> > > larger question is do you see this being sufficiently distinct from
> > > the existing binder:transfer permission?  In other words, would you
> > > ever want to grant a domain the ability to transfer a file *without*
> > > also granting it the ability to transfer the memory charge?  You need
> > > to help me explain why we need an additional permission for this,
> > > because I don't currently see the need.
> > >
> > Yes, and that's actually more often the case than not. A file here
> > means a file descriptor that points at any type of resource: file on
> > disk, memfd, dmabuf, etc. Currently there exists policy that restricts
> > which processes are allowed to interact with FDs over binder using the
> > security_binder_transfer_file hook you reference. [1] However this new
> > transfer_charge permission is meant to restrict the ability of a FD
> > sender to transfer the memory charge associated with that FD (if one
> > exists) to a recipient (who may or may not want to accept the memory
> > charge). So the memory charge is independent of (potentially one-time,
> > read-only) access to the FD.
>
> Without a more comprehensive set of LSM/SELinux access controls around
> resource management (which would need discussion beyond just this
> thread/patch) I'm not sure we want to start patching in one-off
> controls like this.
>
Understood, I'll try reusing security_binder_transfer_file.

> I haven't looked, but are there any traditional/DAC access controls
> around transfering memory changes from one task to another?  It seems
> like there *should* be, and if so, it seems like that would be the
> right approach at the moment ... if you're not already doing that in
> the other patches in the patchset.
>
I'm not aware of controls associated with individual objects like
dmabufs. While it's not quite the same thing, I do see that support
for charge migration tied to task migration was intentionally dropped
for cgroup2 and is now deprecated for cgroup1 because it's difficult
and expensive. However that seems like a much bigger job than dealing
with the memory backing an individual object when that object is
handed off between processes (the object ownership moves, not the
task).

https://lore.kernel.org/all/20221206171340.139790-4-hannes@cmpxchg.org/


> --
> paul-moore.com
Paul Moore Jan. 12, 2023, 9:54 p.m. UTC | #9
On Thu, Jan 12, 2023 at 4:36 PM T.J. Mercier <tjmercier@google.com> wrote:
> On Thu, Jan 12, 2023 at 12:45 PM Paul Moore <paul@paul-moore.com> wrote:
> > On Wed, Jan 11, 2023 at 7:21 PM T.J. Mercier <tjmercier@google.com> wrote:
> > > On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <paul@paul-moore.com> wrote:
> > > > On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <tjmercier@google.com> wrote:
> > > > >
> > > > > Any process can cause a memory charge transfer to occur to any other
> > > > > process when transmitting a file descriptor through binder. This should
> > > > > only be possible for central allocator processes, so a new SELinux
> > > > > permission is added to restrict which processes are allowed to initiate
> > > > > these charge transfers.
> > > > >
> > > > > Signed-off-by: T.J. Mercier <tjmercier@google.com>
> > > > > ---
> > > > >  drivers/android/binder.c            | 5 +++++
> > > > >  include/linux/lsm_hook_defs.h       | 2 ++
> > > > >  include/linux/lsm_hooks.h           | 6 ++++++
> > > > >  include/linux/security.h            | 2 ++
> > > > >  security/security.c                 | 6 ++++++
> > > > >  security/selinux/hooks.c            | 9 +++++++++
> > > > >  security/selinux/include/classmap.h | 2 +-
> > > > >  7 files changed, 31 insertions(+), 1 deletion(-)
> > > >
> > > > Hi T.J.,
> > > >
> > > > A few things come to mind when looking at this patchset, but let me
> > > > start with the big one first: you only sent 0/4 and 4/4 to the LSM and
> > > > SELinux lists, so that's all I'm seeing in my inbox to review, and
> > > > it's hard to make sense of what you want to do with just these
> > > > snippets.  This makes me cranky, and less inclined to spend the time
> > > > to give this a proper review, because there are plenty of other things
> > > > which need attention and don't require me having to hunt down missing
> > > > pieces.  Yes, I'm aware of b4/lei, and while they are great tools, my
> > > > workflow was pretty well established before they came into existence
> > > > and I still do things the good ol' fashioned way with mailing lists,
> > > > etc.
> > > >
> > > > Make the patch reviewer's life easy whenever you can, it will rarely
> > > > (ever?) backfire, I promise.
> > >
> > > Hi Paul, sorry about that. I have git send-email calling
> > > get_maintainer.pl to automatically figure out the recipients, and I
> > > think that's why it only sent particular patches to a subset of lists.
> > > Looks like the list of recipients for each patch should be a union of
> > > all patches. Thank you for taking a look anyway! Here's a lore link:
> > > https://lore.kernel.org/lkml/20230109213809.418135-1-tjmercier@google.com/
> > >
> > > > > diff --git a/drivers/android/binder.c b/drivers/android/binder.c
> > > > > index 9830848c8d25..9063db04826d 100644
> > > > > --- a/drivers/android/binder.c
> > > > > +++ b/drivers/android/binder.c
> > > > > @@ -2279,6 +2279,11 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
> > > > >         if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
> > > > >                 struct dma_buf *dmabuf;
> > > > >
> > > > > +               if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
> > > > > +                       ret = -EPERM;
> > > > > +                       goto err_security;
> > > > > +               }
> > > >
> > > > This is where I believe I'm missing the proper context, as this
> > > > version of binder_translate_fd() differs from what I see in Linus'
> > > > tree.  However, the version in Linus' tree does have a LSM hook,
> > > > security_binder_transfer_file(), which is passed both the credentials
> > > > you are using above and based solely on the level of indentation shown
> > > > in the chunk of code above, it seems like the existing hook might be
> > > > suitable?
> > >
> > > Yes, patch 3 plumbs through flags to this function:
> > > https://lore.kernel.org/lkml/20230109213809.418135-4-tjmercier@google.com/
> > >
> > > I don't think the existing hook is suitable, which I've tried to explain below.
> >
> > In this particular case the issue of what permission checks are done
> > for a given LSM, SELinux in this case, appears to be independent of if
> > we need a new, different, or second LSM hook.  Unless I missed
> > something the only real difference with this new hook is that is sits
> > behind a conditional checking if memory control groups are enabled and
> > if a transfer charge was specified; it seems like passing the @flags
> > parameter into the existing LSM hook would allow you to use the
> > existing hook (it is called before the new hook, right?)?
> >
> Ah yes, that sounds like it would work. Thank you.
>
> > > > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > > > > index 3c5be76a9199..823ef14924bd 100644
> > > > > --- a/security/selinux/hooks.c
> > > > > +++ b/security/selinux/hooks.c
> > > > > @@ -2066,6 +2066,14 @@ static int selinux_binder_transfer_file(const struct cred *from,
> > > > >                             &ad);
> > > > >  }
> > > > >
> > > > > +static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
> > > > > +{
> > > > > +       return avc_has_perm(&selinux_state,
> > > > > +                           cred_sid(from), cred_sid(to),
> > > > > +                           SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
> > > > > +                           NULL);
> > > > > +}
> > > >
> > > > Generally speaking SELinux doesn't really worry about resource
> > > > accounting controls so this seems a bit out of place, but perhaps the
> > > > larger question is do you see this being sufficiently distinct from
> > > > the existing binder:transfer permission?  In other words, would you
> > > > ever want to grant a domain the ability to transfer a file *without*
> > > > also granting it the ability to transfer the memory charge?  You need
> > > > to help me explain why we need an additional permission for this,
> > > > because I don't currently see the need.
> > > >
> > > Yes, and that's actually more often the case than not. A file here
> > > means a file descriptor that points at any type of resource: file on
> > > disk, memfd, dmabuf, etc. Currently there exists policy that restricts
> > > which processes are allowed to interact with FDs over binder using the
> > > security_binder_transfer_file hook you reference. [1] However this new
> > > transfer_charge permission is meant to restrict the ability of a FD
> > > sender to transfer the memory charge associated with that FD (if one
> > > exists) to a recipient (who may or may not want to accept the memory
> > > charge). So the memory charge is independent of (potentially one-time,
> > > read-only) access to the FD.
> >
> > Without a more comprehensive set of LSM/SELinux access controls around
> > resource management (which would need discussion beyond just this
> > thread/patch) I'm not sure we want to start patching in one-off
> > controls like this.
>
> Understood, I'll try reusing security_binder_transfer_file.

Well, yes, it looks like you should reuse the existing hook, but that
is missing the point here - I'm not convinced we want this new SELinux
control without some additional discussion and work around the
desirability and practicality of adding SELinux controls to resource
management.

> > I haven't looked, but are there any traditional/DAC access controls
> > around transfering memory changes from one task to another?  It seems
> > like there *should* be, and if so, it seems like that would be the
> > right approach at the moment ... if you're not already doing that in
> > the other patches in the patchset.
>
> I'm not aware of controls associated with individual objects like
> dmabufs. While it's not quite the same thing, I do see that support
> for charge migration tied to task migration was intentionally dropped
> for cgroup2 and is now deprecated for cgroup1 because it's difficult
> and expensive. However that seems like a much bigger job than dealing
> with the memory backing an individual object when that object is
> handed off between processes (the object ownership moves, not the
> task).

I would suggest you should start talking to the cgroup folks about
this, that seems like a much better first step than jumping straight
to a LSM hook to control just one type of charge transfer.  At the
very least, that is the start of the much larger conversation that
would be needed to add resource management controls to SELinux (and
other LSMs that wanted to implement similar controls).
diff mbox series

Patch

diff --git a/drivers/android/binder.c b/drivers/android/binder.c
index 9830848c8d25..9063db04826d 100644
--- a/drivers/android/binder.c
+++ b/drivers/android/binder.c
@@ -2279,6 +2279,11 @@  static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
 	if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
 		struct dma_buf *dmabuf;
 
+		if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
+			ret = -EPERM;
+			goto err_security;
+		}
+
 		if (unlikely(!is_dma_buf_file(file))) {
 			binder_user_error(
 				"%d:%d got transaction with XFER_CHARGE for non-dmabuf fd, %d\n",
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index ed6cb2ac55fa..8db2a958557e 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -33,6 +33,8 @@  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, binder_transfer_charge, const struct cred *from,
+	 const struct cred *to)
 LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child,
 	 unsigned int mode)
 LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent)
diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index 0a5ba81f7367..39c40c7bf519 100644
--- a/include/linux/lsm_hooks.h
+++ b/include/linux/lsm_hooks.h
@@ -1385,6 +1385,12 @@ 
  *	@file contains the struct file being transferred.
  *	@to contains the struct cred for the receiving process.
  *	Return 0 if permission is granted.
+ * @binder_transfer_charge:
+ *	Check whether @from is allowed to transfer the memory charge for a
+ *	buffer out of its cgroup to @to.
+ *	@from contains the struct cred for the sending process.
+ *	@to contains the struct cred for the receiving process.
+ *	Return 0 if permission is granted.
  *
  * @ptrace_access_check:
  *	Check permission before allowing the current process to trace the
diff --git a/include/linux/security.h b/include/linux/security.h
index 5b67f208f7de..3b7472308430 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -270,6 +270,8 @@  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_binder_transfer_charge(const struct cred *from,
+				    const struct cred *to);
 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,
diff --git a/security/security.c b/security/security.c
index d1571900a8c7..97e1e74d1ff2 100644
--- a/security/security.c
+++ b/security/security.c
@@ -801,6 +801,12 @@  int security_binder_transfer_file(const struct cred *from,
 	return call_int_hook(binder_transfer_file, 0, from, to, file);
 }
 
+int security_binder_transfer_charge(const struct cred *from,
+				    const struct cred *to)
+{
+	return call_int_hook(binder_transfer_charge, 0, from, to);
+}
+
 int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
 {
 	return call_int_hook(ptrace_access_check, 0, child, mode);
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 3c5be76a9199..823ef14924bd 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2066,6 +2066,14 @@  static int selinux_binder_transfer_file(const struct cred *from,
 			    &ad);
 }
 
+static int selinux_binder_transfer_charge(const struct cred *from, const struct cred *to)
+{
+	return avc_has_perm(&selinux_state,
+			    cred_sid(from), cred_sid(to),
+			    SECCLASS_BINDER, BINDER__TRANSFER_CHARGE,
+			    NULL);
+}
+
 static int selinux_ptrace_access_check(struct task_struct *child,
 				       unsigned int mode)
 {
@@ -7052,6 +7060,7 @@  static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
+	LSM_HOOK_INIT(binder_transfer_charge, selinux_binder_transfer_charge),
 
 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
index a3c380775d41..2eef180d10d7 100644
--- a/security/selinux/include/classmap.h
+++ b/security/selinux/include/classmap.h
@@ -172,7 +172,7 @@  const struct security_class_mapping secclass_map[] = {
 	{ "tun_socket",
 	  { COMMON_SOCK_PERMS, "attach_queue", NULL } },
 	{ "binder", { "impersonate", "call", "set_context_mgr", "transfer",
-		      NULL } },
+		      "transfer_charge", NULL } },
 	{ "cap_userns",
 	  { COMMON_CAP_PERMS, NULL } },
 	{ "cap2_userns",