2023-01-09 22:09:32

by T.J. Mercier

[permalink] [raw]
Subject: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

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 <[email protected]>
---
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",
--
2.39.0.314.g84b9a713c41-goog


2023-01-09 22:42:05

by Casey Schaufler

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

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 <[email protected]>
> ---
> 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",

2023-01-10 00:28:31

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

Hi Mercier",

Thank you for the patch! Yet something to improve:

[auto build test ERROR on b7bfaa761d760e72a969d116517eaa12e404c262]

url: https://github.com/intel-lab-lkp/linux/commits/T-J-Mercier/memcg-Track-exported-dma-buffers/20230110-054247
base: b7bfaa761d760e72a969d116517eaa12e404c262
patch link: https://lore.kernel.org/r/20230109213809.418135-5-tjmercier%40google.com
patch subject: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook
config: openrisc-randconfig-r004-20230109
compiler: or1k-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/e412f67c5a40d34925284b62dde89448b8b7e208
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review T-J-Mercier/memcg-Track-exported-dma-buffers/20230110-054247
git checkout e412f67c5a40d34925284b62dde89448b8b7e208
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=openrisc olddefconfig
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=openrisc SHELL=/bin/bash drivers/android/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

drivers/android/binder.c: In function 'binder_translate_fd':
>> drivers/android/binder.c:2282:21: error: implicit declaration of function 'security_binder_transfer_charge'; did you mean 'security_binder_transfer_file'? [-Werror=implicit-function-declaration]
2282 | if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| security_binder_transfer_file
cc1: some warnings being treated as errors


vim +2282 drivers/android/binder.c

2240
2241 static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
2242 struct binder_transaction *t,
2243 struct binder_thread *thread,
2244 struct binder_transaction *in_reply_to)
2245 {
2246 struct binder_proc *proc = thread->proc;
2247 struct binder_proc *target_proc = t->to_proc;
2248 struct binder_txn_fd_fixup *fixup;
2249 struct file *file;
2250 int ret = 0;
2251 bool target_allows_fd;
2252
2253 if (in_reply_to)
2254 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2255 else
2256 target_allows_fd = t->buffer->target_node->accept_fds;
2257 if (!target_allows_fd) {
2258 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2259 proc->pid, thread->pid,
2260 in_reply_to ? "reply" : "transaction",
2261 fd);
2262 ret = -EPERM;
2263 goto err_fd_not_accepted;
2264 }
2265
2266 file = fget(fd);
2267 if (!file) {
2268 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2269 proc->pid, thread->pid, fd);
2270 ret = -EBADF;
2271 goto err_fget;
2272 }
2273 ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
2274 if (ret < 0) {
2275 ret = -EPERM;
2276 goto err_security;
2277 }
2278
2279 if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
2280 struct dma_buf *dmabuf;
2281
> 2282 if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
2283 ret = -EPERM;
2284 goto err_security;
2285 }
2286
2287 if (unlikely(!is_dma_buf_file(file))) {
2288 binder_user_error(
2289 "%d:%d got transaction with XFER_CHARGE for non-dmabuf fd, %d\n",
2290 proc->pid, thread->pid, fd);
2291 ret = -EINVAL;
2292 goto err_dmabuf;
2293 }
2294
2295 dmabuf = file->private_data;
2296 ret = dma_buf_transfer_charge(dmabuf, target_proc->tsk);
2297 if (ret) {
2298 pr_warn("%d:%d Unable to transfer DMA-BUF fd charge to %d\n",
2299 proc->pid, thread->pid, target_proc->pid);
2300 goto err_xfer;
2301 }
2302 }
2303
2304 /*
2305 * Add fixup record for this transaction. The allocation
2306 * of the fd in the target needs to be done from a
2307 * target thread.
2308 */
2309 fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
2310 if (!fixup) {
2311 ret = -ENOMEM;
2312 goto err_alloc;
2313 }
2314 fixup->file = file;
2315 fixup->offset = fd_offset;
2316 fixup->target_fd = -1;
2317 trace_binder_transaction_fd_send(t, fd, fixup->offset);
2318 list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
2319
2320 return ret;
2321
2322 err_alloc:
2323 err_xfer:
2324 err_dmabuf:
2325 err_security:
2326 fput(file);
2327 err_fget:
2328 err_fd_not_accepted:
2329 return ret;
2330 }
2331

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests


Attachments:
(No filename) (5.12 kB)
config (174.23 kB)
Download all attachments

2023-01-10 01:11:07

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

Hi Mercier",

Thank you for the patch! Yet something to improve:

[auto build test ERROR on b7bfaa761d760e72a969d116517eaa12e404c262]

url: https://github.com/intel-lab-lkp/linux/commits/T-J-Mercier/memcg-Track-exported-dma-buffers/20230110-054247
base: b7bfaa761d760e72a969d116517eaa12e404c262
patch link: https://lore.kernel.org/r/20230109213809.418135-5-tjmercier%40google.com
patch subject: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook
config: arm64-randconfig-r036-20230109
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 8d9828ef5aa9688500657d36cd2aefbe12bbd162)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/e412f67c5a40d34925284b62dde89448b8b7e208
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review T-J-Mercier/memcg-Track-exported-dma-buffers/20230110-054247
git checkout e412f67c5a40d34925284b62dde89448b8b7e208
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 olddefconfig
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/android/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

>> drivers/android/binder.c:2282:7: error: call to undeclared function 'security_binder_transfer_charge'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
^
drivers/android/binder.c:2282:7: note: did you mean 'security_binder_transfer_file'?
include/linux/security.h:545:19: note: 'security_binder_transfer_file' declared here
static inline int security_binder_transfer_file(const struct cred *from,
^
1 error generated.


vim +/security_binder_transfer_charge +2282 drivers/android/binder.c

2240
2241 static int binder_translate_fd(u32 fd, binder_size_t fd_offset, __u32 flags,
2242 struct binder_transaction *t,
2243 struct binder_thread *thread,
2244 struct binder_transaction *in_reply_to)
2245 {
2246 struct binder_proc *proc = thread->proc;
2247 struct binder_proc *target_proc = t->to_proc;
2248 struct binder_txn_fd_fixup *fixup;
2249 struct file *file;
2250 int ret = 0;
2251 bool target_allows_fd;
2252
2253 if (in_reply_to)
2254 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2255 else
2256 target_allows_fd = t->buffer->target_node->accept_fds;
2257 if (!target_allows_fd) {
2258 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2259 proc->pid, thread->pid,
2260 in_reply_to ? "reply" : "transaction",
2261 fd);
2262 ret = -EPERM;
2263 goto err_fd_not_accepted;
2264 }
2265
2266 file = fget(fd);
2267 if (!file) {
2268 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2269 proc->pid, thread->pid, fd);
2270 ret = -EBADF;
2271 goto err_fget;
2272 }
2273 ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
2274 if (ret < 0) {
2275 ret = -EPERM;
2276 goto err_security;
2277 }
2278
2279 if (IS_ENABLED(CONFIG_MEMCG) && (flags & BINDER_FD_FLAG_XFER_CHARGE)) {
2280 struct dma_buf *dmabuf;
2281
> 2282 if (security_binder_transfer_charge(proc->cred, target_proc->cred)) {
2283 ret = -EPERM;
2284 goto err_security;
2285 }
2286
2287 if (unlikely(!is_dma_buf_file(file))) {
2288 binder_user_error(
2289 "%d:%d got transaction with XFER_CHARGE for non-dmabuf fd, %d\n",
2290 proc->pid, thread->pid, fd);
2291 ret = -EINVAL;
2292 goto err_dmabuf;
2293 }
2294
2295 dmabuf = file->private_data;
2296 ret = dma_buf_transfer_charge(dmabuf, target_proc->tsk);
2297 if (ret) {
2298 pr_warn("%d:%d Unable to transfer DMA-BUF fd charge to %d\n",
2299 proc->pid, thread->pid, target_proc->pid);
2300 goto err_xfer;
2301 }
2302 }
2303
2304 /*
2305 * Add fixup record for this transaction. The allocation
2306 * of the fd in the target needs to be done from a
2307 * target thread.
2308 */
2309 fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
2310 if (!fixup) {
2311 ret = -ENOMEM;
2312 goto err_alloc;
2313 }
2314 fixup->file = file;
2315 fixup->offset = fd_offset;
2316 fixup->target_fd = -1;
2317 trace_binder_transaction_fd_send(t, fd, fixup->offset);
2318 list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
2319
2320 return ret;
2321
2322 err_alloc:
2323 err_xfer:
2324 err_dmabuf:
2325 err_security:
2326 fput(file);
2327 err_fget:
2328 err_fd_not_accepted:
2329 return ret;
2330 }
2331

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests


Attachments:
(No filename) (5.41 kB)
config (198.21 kB)
Download all attachments

2023-01-10 01:15:23

by T.J. Mercier

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On Mon, Jan 9, 2023 at 2:28 PM Casey Schaufler <[email protected]> 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 <[email protected]>
> > ---
> > 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",

2023-01-10 20:00:13

by Casey Schaufler

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On 1/9/2023 4:30 PM, T.J. Mercier wrote:
> On Mon, Jan 9, 2023 at 2:28 PM Casey Schaufler <[email protected]> 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 <[email protected]>
>>> ---
>>> 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",

2023-01-12 00:15:51

by Paul Moore

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <[email protected]> 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 <[email protected]>
> ---
> 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

2023-01-12 00:25:47

by T.J. Mercier

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <[email protected]> wrote:
>
> On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <[email protected]> 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 <[email protected]>
> > ---
> > 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/[email protected]/

> > 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/[email protected]/

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

2023-01-12 00:32:59

by T.J. Mercier

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On Tue, Jan 10, 2023 at 11:39 AM Casey Schaufler <[email protected]> wrote:
>
> On 1/9/2023 4:30 PM, T.J. Mercier wrote:
> > On Mon, Jan 9, 2023 at 2:28 PM Casey Schaufler <[email protected]> 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 <[email protected]>
> >>> ---
> >>> 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",

2023-01-12 21:32:46

by Paul Moore

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On Wed, Jan 11, 2023 at 7:21 PM T.J. Mercier <[email protected]> wrote:
>
> On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <[email protected]> wrote:
> >
> > On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <[email protected]> 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 <[email protected]>
> > > ---
> > > 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/[email protected]/
>
> > > 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/[email protected]/
>
> 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.

--
paul-moore.com

2023-01-12 21:59:50

by T.J. Mercier

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On Thu, Jan 12, 2023 at 12:45 PM Paul Moore <[email protected]> wrote:
>
> On Wed, Jan 11, 2023 at 7:21 PM T.J. Mercier <[email protected]> wrote:
> >
> > On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <[email protected]> wrote:
> > >
> > > On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <[email protected]> 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 <[email protected]>
> > > > ---
> > > > 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/[email protected]/
> >
> > > > 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/[email protected]/
> >
> > 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/[email protected]/


> --
> paul-moore.com

2023-01-12 22:24:53

by Paul Moore

[permalink] [raw]
Subject: Re: [PATCH 4/4] security: binder: Add transfer_charge SElinux hook

On Thu, Jan 12, 2023 at 4:36 PM T.J. Mercier <[email protected]> wrote:
> On Thu, Jan 12, 2023 at 12:45 PM Paul Moore <[email protected]> wrote:
> > On Wed, Jan 11, 2023 at 7:21 PM T.J. Mercier <[email protected]> wrote:
> > > On Wed, Jan 11, 2023 at 3:00 PM Paul Moore <[email protected]> wrote:
> > > > On Mon, Jan 9, 2023 at 4:38 PM T.J. Mercier <[email protected]> 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 <[email protected]>
> > > > > ---
> > > > > 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/[email protected]/
> > >
> > > > > 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/[email protected]/
> > >
> > > 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).

--
paul-moore.com