binder: report txn errors via generic netlink
Subsystems (8)
v17 cover binder: report txn errors via generic netlink
From: Li Li <[email protected]>
=== Notes for resending the patchset ===
Resend the v17 patchset (no code change) to linux-security-module per request.
https://lore.kernel.org/all/[email protected]/
In short, the kernel patchset includes 3 patches and a cover letter:
- [PATCH v17 0/3] binder: report txn errors via generic netlink
- [PATCH v17 1/3] lsm, selinux: Add setup_report permission to binder
- [PATCH v17 2/3] binder: report txn errors via generic netlink
- [PATCH v17 3/3] binder: transaction report binder_features flag
The corresponding test (for https://github.com/SELinuxProject/selinux-testsuite)
and its dependency (for https://github.com/SELinuxProject/refpolicy):
- [PATCH] binder: add setup_report permission
- [PATCH v2] policy,tests: add test for new permission binder:setup_report
=== Notes END ===
It's a known issue that neither the frozen processes nor the system administration process of the OS can correctly deal with failed binder transactions. The reason is that there's no reliable way for the user space administration process to fetch the binder errors from the kernel binder driver.
Android is such an OS suffering from this issue. Since cgroup freezer was used to freeze user applications to save battery, innocent frozen apps have to be killed when they receive sync binder transactions or when their async binder buffer is running out.
This patch introduces the Linux generic netlink messages into the binder driver so that the Linux/Android system administration process can listen to important events and take corresponding actions, like stopping a broken app from attacking the OS by sending huge amount of spamming binder transactiions.
The 1st version uses a global generic netlink for all binder contexts, raising potential security concerns. There were a few other feedbacks like request to kernel docs and test code. The thread can be found at
https://lore.kernel.org/lkml/[email protected]/
The 2nd version fixes those issues and has been tested on the latest version of AOSP. See https://r.android.com/3305462 for how userspace is going to use this feature and the test code. It can be found at
https://lore.kernel.org/lkml/[email protected]/
The 3rd version replaces the handcrafted netlink source code with the netlink protocal specs in YAML. It also fixes the documentation issues.
https://lore.kernel.org/lkml/[email protected]/
The 4th version just containsi trivial fixes, making the subject of the patch aligned with the subject of the cover letter.
https://lore.kernel.org/lkml/[email protected]/
The 5th version incorporates the suggested fixes to the kernel doc and the init function. It also removes the unsupported uapi-header in YAML that contains "/" for subdirectory.
https://lore.kernel.org/lkml/[email protected]/
The 6th version has some trivial kernel doc fixes, without modifying any other source code.
https://lore.kernel.org/lkml/[email protected]/
The 7th version breaks the binary struct netlink message into individual attributes to better support automatic error checking. Thanks Jakub for improving ynl-gen.
https://lore.kernel.org/all/[email protected]/
The 8th version solves the multi-genl-family issue by demuxing the messages based on a new context attribute. It also improves the YAML spec to be consistent with netlink tradition. A Huge 'Thank You' to Jakub who taught me a lot about the netlink protocol!
https://lore.kernel.org/all/[email protected]/
The 9th version only contains a few trivial fixes, removing a redundant pr_err and unnecessary payload check. The ynl-gen patch to allow uapi header in sub-dirs has been merged so it's no longer included in this patch set.
https://lore.kernel.org/all/[email protected]/
The 10th version renames binder genl to binder netlink, improves the readability of the kernel doc and uses more descriptive variable names. The function binder_add_device() is moved out to a new commit per request. It also fixes a warning about newline used in NL_SET_ERR_MSG. Thanks Carlos for his valuable suggestions!
https://lore.kernel.org/all/[email protected]/
The 11th version simplifies the yaml filename to avoid redundant words in variable names. This also makes binder netlink yaml more aligned with other existing netlink specs. Another trivial change is to use reverse xmas tree for function variables.
https://lore.kernel.org/all/[email protected]/
The 12th version makes Documentation/admin-guide/binder_netlink.rst aligned with the binder netlink yaml change introduced in the 11th revision. It doesn't change any source code.
https://lore.kernel.org/all/[email protected]/
The 13th version removes the unnecessary dependency to binder file ops. Now the netlink configuration is reset using sock_priv_destroy. It also requires CAP_NET_ADMIN to send commands to the driver. One of the patches ("binderfs: add new binder devices to binder_devices") has been merged to linux-next. To avoid conflict, switch to linux-next master branch and remove the merged one. Adding sock_priv into netlink spec results in CFI failure, which is fixed by the new trampoline patches.
https://lore.kernel.org/all/[email protected]/
The 14th version fix the code style issue by wrapping the sock priv in a separate struct, as suggested by Jakub. The other 2 patches are no longer included in this patchset as the equvilent fix has already been merged to upstream linux master branch, as well as net & net-next. This version has already been rebased to TOT of linux-next.
https://lore.kernel.org/all/[email protected]/
The 15th version switches from unicast to multicast per feedback and feature requriements from binder users. With this change, multiple user space processes can listen to the binder reports from the kernel driver at the same time. To receive the multicast messages, those user space processs should query the mcast group id and join the mcast group. In the previous unicast solution, a portid is saved in the kernel driver to prevent unauthorized process to send commands to the kernel driver. In this multicast solution, this is replaced by a new "setup_report" permission in the "binder" class. Meanwhile, the sock_priv_destroy callback and CAP_NET_ADMIN restriction are no longer required in favor of the multicast solution and the new "setup_report" permission.
https://lore.kernel.org/all/[email protected]/
The 16th version fixes the build error when CONFIG_SECURITY is disabled. There's no other changes. Thank you, kernel test robot <[email protected]>!
https://lore.kernel.org/all/[email protected]/
The 17th version corrects the issue in binder_find_proc() that multiple binder_proc instances might match the same pid. It also simplifies the parameters of binder_netlink_report(). This patchset also includes a patch to SELinuxProject/refpolicy for the new permission and another patch to SELinuxProject/selinux-testsuite to test this new permission.
v1: add a global binder genl socket for all contexts
v2: change to per-context binder genl for security reason
replace the new ioctl with a netlink command
add corresponding doc Documentation/admin-guide/binder_genl.rst add user space test code in AOSP
v3: use YNL spec (./tools/net/ynl/ynl-regen.sh)
fix documentation index
v4: change the subject of the patch and remove unsed #if 0
v5: improve the kernel doc and the init function
remove unsupported uapi-header in YAML
v6: fix some trivial kernel doc issues
v7: break the binary struct binder_report into individual attributes
v8: use multiplex netlink message in a unified netlink family
improve the YAML spec to be consistent with netlink tradition
v9: remove unnecessary check to netlink flags and message payloads
v10: improve the readability of kernel doc and variable names
v11: rename binder_netlinnk.yaml to binder.yaml
use reverse xmas tree for function variables
v12: make kernel doc aligned with source code
v13: use sock_priv_destroy to cleanup netlink
require CAP_NET_ADMIN to send netlink commands
add trampolines in ynl-gen to fix CFI failure
v14: wrap the sock priv in a separate struct
v15: switch from unicast to multicast netlink message
add a "setup_report" permission in the "binder" class add generic_netlink to binder_features
v16: fix build error with CONFIG_SECURITY disabled
v17: deal with multiple binder_proc with the same pid
simplify parameters of binder_netlink_report()
add test for the new permission binder:setup_report
Li Li (2):
binder: report txn errors via generic netlink
binder: transaction report binder_features flag
Thiébaud Weksteen (1):
lsm, selinux: Add setup_report permission to binder
Documentation/admin-guide/binder_netlink.rst | 108 +++++++++ Documentation/admin-guide/index.rst | 1 + Documentation/netlink/specs/binder.yaml | 116 ++++++++++ drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 215 +++++++++++++++++- drivers/android/binder_internal.h | 16 ++ drivers/android/binder_netlink.c | 46 ++++ drivers/android/binder_netlink.h | 23 ++ drivers/android/binder_trace.h | 35 +++ drivers/android/binderfs.c | 8 + include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 6 + include/uapi/linux/android/binder_netlink.h | 57 +++++ security/security.c | 13 ++ security/selinux/hooks.c | 7 + security/selinux/include/classmap.h | 3 +- .../filesystems/binderfs/binderfs_test.c | 1 + 18 files changed, 653 insertions(+), 6 deletions(-) create mode 100644 Documentation/admin-guide/binder_netlink.rst create mode 100644 Documentation/netlink/specs/binder.yaml create mode 100644 drivers/android/binder_netlink.c create mode 100644 drivers/android/binder_netlink.h create mode 100644 include/uapi/linux/android/binder_netlink.h
base-commit: 5b37f7bfff3b1582c34be8fb23968b226db71ebd
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
PATCH RESEND v17 1/3 lsm, selinux: Add setup_report permission to binder
From: Thiébaud Weksteen <[email protected]>
Introduce a new permission "setup_report" to the "binder" class. This persmission controls the ability to set up the binder generic netlink driver to report certain binder transactions.
--- include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 6 ++++++ security/security.c | 13 +++++++++++++ security/selinux/hooks.c | 7 +++++++ security/selinux/include/classmap.h | 3 ++- 5 files changed, 29 insertions(+), 1 deletion(-)
include/linux/lsm_hook_defs.h +1 -0
@@ -33,6 +33,7 @@ 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, const struct file *file) +LSM_HOOK(int, 0, binder_setup_report, 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)
include/linux/security.h +6 -0
@@ -338,6 +338,7 @@ 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,const struct file *file); +int security_binder_setup_report (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(const struct task_struct *target,
@@ -657,6 +658,11 @@ static inline int security_binder_transfer_file(const struct cred *from,return 0 ; } +static inline int security_binder_setup_report (const struct cred *to) +{ +return 0 ; +} +static inline int security_ptrace_access_check (struct task_struct *child,unsigned int mode) {
security/security.c +13 -0
@@ -1043,6 +1043,19 @@ int security_binder_transfer_file(const struct cred *from,return call_int_hook (binder_transfer_file, from, to, file); } +/** +* security_binder_setup_report() - Check if binder report is allowed +* @to: receiving process +* +* Check whether @to is allowed to set up binder reports. +* +* Return: Returns 0 if permission is granted. +*/ +int security_binder_setup_report (const struct cred *to) +{ +return call_int_hook (binder_setup_report, to); +} +/** * security_ptrace_access_check() - Check if tracing is allowed * @child: target process
security/selinux/hooks.c +7 -0
@@ -2103,6 +2103,12 @@ static int selinux_binder_transfer_file(const struct cred *from, &ad); } +static int selinux_binder_setup_report (const struct cred *to) +{ +return avc_has_perm (current_sid (),cred_sid (to),SECCLASS_BINDER , +BINDER__SETUP_REPORT ,NULL ); +} +static int selinux_ptrace_access_check (struct task_struct *child,unsigned int mode) {
@@ -7295,6 +7301,7 @@ static struct security_hook_list selinux_hooks[] __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_setup_report, selinux_binder_setup_report),LSM_HOOK_INIT (ptrace_access_check, selinux_ptrace_access_check), LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
security/selinux/include/classmap.h +2 -1
@@ -133,7 +133,8 @@ const struct security_class_mapping secclass_map[] = { { "kernel_service", { "use_as_override", "create_files_as", NULL } }, { "tun_socket", { COMMON_SOCK_PERMS, "attach_queue", NULL } }, { "binder", - { "impersonate", "call", "set_context_mgr", "transfer", NULL } }, + { "impersonate", "call", "set_context_mgr", "transfer", + "setup_report", NULL } }, { "cap_userns", { COMMON_CAP_PERMS, NULL } }, { "cap2_userns", { COMMON_CAP2_PERMS, NULL } }, { "sctp_socket",
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Simon Horman
reviewerRe: [PATCH RESEND v17 1/3] lsm, selinux: Add setup_report permission to binder
Quoted 4 lines · Li Li
From: Thiébaud Weksteen <[email protected]>
Introduce a new permission "
setup_report" to the "binder" class. This persmission controls the ability to set up the binder generic
nit: permission
Flagged by checkpatch.pl --codespell
Quoted 4 lines
netlink driver to report certain binder transactions.
Signed-off-by Thiébaud Weksteen <[email protected]>Signed-off-by Li Li <[email protected]>
...
Re: [PATCH RESEND v17 1/3] lsm, selinux: Add setup_report permission to binder
Quoted 11 lines · Simon Horman
Quoted 4 lines · Li Li
From: Thiébaud Weksteen <[email protected]>
Introduce a new permission "
setup_report" to the "binder" class. This persmission controls the ability to set up the binder genericnit: permission
Flagged by checkpatch.pl --codespell
Would fix this typo along with other changes in the next version. Thank you for catching this!
Quoted 6 lines
Quoted 4 lines
netlink driver to report certain binder transactions.
Signed-off-by Thiébaud Weksteen <[email protected]>Signed-off-by Li Li <[email protected]>...
Paul Moore
maintainerRe: [PATCH v17 1/3] lsm, selinux: Add setup_report permission to binder
In reply to Li LiQuoted 7 lines · On Apr 16, 2025 Li Li <[email protected]> wrote:
Introduce a new permission "
setup_report" to the "binder" class. This persmission controls the ability to set up the binder generic netlink driver to report certain binder transactions.Signed-off-by Thiébaud Weksteen <[email protected]>Signed-off-by Li Li <[email protected]>
Quoted 7 lines
--- include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 6 ++++++ security/security.c | 13 +++++++++++++ security/selinux/hooks.c | 7 +++++++ security/selinux/include/classmap.h | 3 ++- 5 files changed, 29 insertions(+), 1 deletion(-)
When possible, it is helpful to include at least one caller in the patch which adds a new LSM hook as it helps put the hook in context. With that in mind, I think it would be best to reorder this patchset so that patch 2/3 comes first and this patch comes second, with this patch including the change to binder_nl_report_setup_doit() which adds the call to the new LSM hook.
--
paul-moore.com
Carlos Llamas
maintainerRe: [PATCH v17 1/3] lsm, selinux: Add setup_report permission to binder
Quoted 16 lines · Paul Moore
Quoted 7 lines · On Apr 16, 2025 Li Li <[email protected]> wrote:
Introduce a new permission "
setup_report" to the "binder" class. This persmission controls the ability to set up the binder generic netlink driver to report certain binder transactions.Signed-off-by Thiébaud Weksteen <[email protected]>Signed-off-by Li Li <[email protected]>Quoted 7 lines
--- include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 6 ++++++ security/security.c | 13 +++++++++++++ security/selinux/hooks.c | 7 +++++++ security/selinux/include/classmap.h | 3 ++- 5 files changed, 29 insertions(+), 1 deletion(-)
Quoted 6 lines
When possible, it is helpful to include at least one caller in the patch which adds a new LSM hook as it helps put the hook in context. With that in mind, I think it would be best to reorder this patchset so that patch 2/3 comes first and this patch comes second, with this patch including the change to
binder_nl_report_setup_doit()which adds the call to the new LSM hook.
Ok, I can take care for this. I'll be taking over Li's patchset now, so next version I'll reorder the series and add the caller. Thanks!
--
Carlos Llamas
PATCH RESEND v17 2/3 binder: report txn errors via generic netlink
In reply to Li LiFrom: Li Li <[email protected]>
Introduce generic netlink messages into the binder driver so that the Linux/Android system administration processes can listen to important events and take corresponding actions, like stopping a broken app from attacking the OS by sending huge amount of spamming binder transactions.
The binder netlink sources and headers are automatically generated from the corresponding binder netlink YAML spec. Don't modify them directly.
--- Documentation/admin-guide/binder_netlink.rst | 108 ++++++++++ Documentation/admin-guide/index.rst | 1 + Documentation/netlink/specs/binder.yaml | 116 ++++++++++ drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 215 ++++++++++++++++++- drivers/android/binder_internal.h | 16 ++ drivers/android/binder_netlink.c | 46 ++++ drivers/android/binder_netlink.h | 23 ++ drivers/android/binder_trace.h | 35 +++ include/uapi/linux/android/binder_netlink.h | 57 +++++ 11 files changed, 615 insertions(+), 5 deletions(-) create mode 100644 Documentation/admin-guide/binder_netlink.rst create mode 100644 Documentation/netlink/specs/binder.yaml create mode 100644 drivers/android/binder_netlink.c create mode 100644 drivers/android/binder_netlink.h create mode 100644 include/uapi/linux/android/binder_netlink.h
Documentation/admin-guide/binder_netlink.rst new file +108 -0
@@ -0,0 +1,108 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============================================================== +Generic Netlink for the Android Binder Driver (Binder Netlink) +============================================================== + +The Generic Netlink subsystem in the Linux kernel provides a generic way for +the Linux kernel to communicate with the user space applications via binder +driver. It is used to report binder transaction errors and warnings to user +space administration process. The driver allows multiple binder devices and +their corresponding binder contexts. Each context and their processes can be +configured independently to report their own transactions. + +Basically, the user space code uses the BINDER_CMD_REPORT_SETUP command to +request what kind of binder transactions should be reported by the driver. +The driver then echoes the attributes in a reply message to acknowledge the +request. The BINDER_CMD_REPORT_SETUP command also registers the current +user space process to receive the reports. This BINDER_CMD_REPORT_SETUP +command is protected by SELinux, to prevent unauthorized user apps from +triggering unexpected reports. + +Currently the driver reports these binder transaction errors and warnings. +1. "FAILED" transactions that fail to reach the target process; +2. "ASYNC_FROZEN" transactions that are delayed due to the target process +being frozen by cgroup freezer; or +3. "SPAM" transactions that are considered spamming according to existing +logic in binder_alloc.c. + +When the specified binder transactions happen, the driver uses the +BINDER_CMD_REPORT command to send a generic netlink message to the +registered process, containing the payload defined in binder.yaml. + +More details about the flags, attributes and operations can be found at the +the doc sections in Documentations/netlink/specs/binder.yaml and the +kernel-doc comments of the new source code in binder.{h|c}. + +Using Binder Netlink +-------------------- + +The Binder Netlink can be used in the same way as any other generic netlink +drivers. Userspace application uses a raw netlink socket to send commands +to and receive packets from the kernel driver. + +Usage example (user space pseudo code): + +:: + /* + * send() below is overloaded to pack netlink commands and attributes + * to nlattr/genlmsghdr/nlmsghdr and then send to the netlink socket. + * + * recv() below is overloaded to receive the raw netlink message from + * the netlink socket, parse nlmsghdr/genlmsghdr to find the netlink + * command and then return the nlattr payload. + */ + + // open netlink socket + int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); + + // bind netlink socket + bind(fd, struct socketaddr); + + // get the family id of the binder netlink + send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME, + BINDER_FAMILY_NAME); + void *data = recv(CTRL_CMD_NEWFAMILY); + if (!has_nla_type(data, CTRL_ATTR_FAMILY_ID)) { + // Binder Netlink isn't available on this version of Linux kernel + return; + } + __u16 id = nla(data)[CTRL_ATTR_FAMILY_ID]; + __u32 grp = nla(data)[CTRL_ATTR_MCAST_GROUPS][CTRL_ATTR_MCAST_GRP_ID]; + + // join the mcast group to listen to the binder netlink messages + setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &grp, sizeof(grp)); + + // enable per-context binder report + send(fd, id, BINDER_CMD_REPORT_SETUP, "binder", 0, + BINDER_FLAG_FAILED | BINDER_FLAG_DELAYED); + + // confirm the per-context configuration + data = recv(fd, BINDER_CMD_REPLY); + char *context = nla(data)[BINDER_A_CMD_CONTEXT]; + __u32 pid = nla(data)[BINDER_A_CMD_PID]; + __u32 flags = nla(data)[BINDER_A_CMD_FLAGS]; + + // set optional per-process report, overriding the per-context one + send(fd, id, BINDER_CMD_REPORT_SETUP, "binder", getpid(), + BINDER_FLAG_SPAM | BINDER_REPORT_OVERRIDE); + + // confirm the optional per-process configuration + data = recv(fd, BINDER_CMD_REPLY); + context = nla(data)[BINDER_A_CMD_CONTEXT]; + pid = nla(data)[BINDER_A_CMD_PID]; + flags = nla(data)[BINDER_A_CMD_FLAGS]; + + // wait and read all binder reports + while (running) { + data = recv(fd, BINDER_CMD_REPORT); + auto *attr = nla(data)[BINDER_A_REPORT_XXX]; + + // process binder report + do_something(*attr); + } + + // clean up + send(fd, id, BINDER_CMD_REPORT_SETUP, 0, 0); + send(fd, id, BINDER_CMD_REPORT_SETUP, getpid(), 0); + close(fd);
Documentation/admin-guide/index.rst +1 -0
@@ -120,6 +120,7 @@ Block-layer and filesystem administration :maxdepth: 1 bcache + binder_netlink binderfs blockdev/index cifs/index
Documentation/netlink/specs/binder.yaml new file +116 -0
116 lines not on this page · raw message
drivers/android/Kconfig +1 -0
@@ -4,6 +4,7 @@ menu "Android" config ANDROID_BINDER_IPC bool "Android Binder IPC Driver" depends on MMU + depends on NET default n help Binder is used in Android for both communication between processes,
drivers/android/Makefile +1 -1
@@ -2,5 +2,5 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_ANDROID_BINDERFS) += binderfs.o -obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o +obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o binder_netlink.o obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
drivers/android/binder.c +211 -4
265 lines not on this page · raw message
drivers/android/binder_internal.h +16 -0
@@ -11,15 +11,28 @@#include <linux/stddef.h> #include <linux/types.h> #include <linux/uidgid.h> +#include <net/genetlink.h> #include <uapi/linux/android/binderfs.h> #include "binder_alloc.h" #include "dbitmap.h" +/** +* struct binder_context - information about a binder domain +* @binder_context_mgr_node: the context manager +* @context_mgr_node_lock: the lock protecting the above context manager node +* @binder_context_mgr_uid: the uid of the above context manager +* @name: the name of the binder device +* @report_flags: the categories of binder transactions that would +* be reported (see enum binder_report_flag). +* @report_seq: the seq number of the generic netlink report +*/ struct binder_context {struct binder_node *binder_context_mgr_node;struct mutex context_mgr_node_lock;kuid_t binder_context_mgr_uid;const char *name; +u32 report_flags; +atomic_t report_seq; };/**
@@ -413,6 +426,8 @@ struct binder_ref {* @binderfs_entry: process-specific binderfs log file * @oneway_spam_detection_enabled: process enabled oneway spam detection * or not +* @report_flags: the categories of binder transactions that would +* be reported (see enum binder_genl_flag). * * Bookkeeping structure for binder processes */
@@ -451,6 +466,7 @@ struct binder_proc {spinlock_t outer_lock;struct dentry *binderfs_entry;bool oneway_spam_detection_enabled; +u32 report_flags; };/**
drivers/android/binder_netlink.c new file +46 -0
46 lines not on this page · raw message
drivers/android/binder_netlink.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/binder.yaml */ +/* YNL-GEN kernel header */ + +#ifndef _LINUX_BINDER_GEN_H +#define _LINUX_BINDER_GEN_H + +#include <net/netlink.h> +#include <net/genetlink.h> + +#include <uapi/linux/android/binder_netlink.h> +#include <binder_internal.h> + +int binder_nl_report_setup_doit (struct sk_buff *skb,struct genl_info *info); + +enum { +BINDER_NLGRP_REPORT , +}; + +extern struct genl_family binder_nl_family; + +#endif /* _LINUX_BINDER_GEN_H */
drivers/android/binder_trace.h +35 -0
@@ -423,6 +423,41 @@ TRACE_EVENT(binder_return, "unknown") ); +TRACE_EVENT (binder_netlink_report, + TP_PROTO(const char *name, u32 err, u32 reply, + struct binder_transaction *t), +TP_ARGS (name, err, reply, t), +TP_STRUCT__entry ( + __field(const char *, name) + __field(u32, err) + __field(u32, from_pid) + __field(u32, from_tid) + __field(u32, to_pid) + __field(u32, to_tid) + __field(u32, reply) + __field(u32, flags) + __field(u32, code) +__field (binder_size_t, data_size) + ), +TP_fast_assign ( + __entry->name = name; + __entry->err = err; + __entry->from_pid = t->from_pid; + __entry->from_tid = t->from_tid; + __entry->to_pid = t->to_proc ? t->to_proc->pid : 0; + __entry->to_tid = t->to_thread ? t->to_thread->pid : 0; + __entry->reply = reply; + __entry->flags = t->flags; + __entry->code = t->code; + __entry->data_size = t->buffer->data_size; + ), +TP_printk ("%s: %d %d:%d -> %d:%d %s flags=0x08%x code=%d %llu" , + __entry->name, __entry->err, __entry->from_pid, + __entry->from_tid, __entry->to_pid, __entry->to_tid, + __entry->reply ?"reply" :"" , __entry->flags, + __entry->code, __entry->data_size) +); +#endif /* _BINDER_TRACE_H */ #undef TRACE_INCLUDE_PATH
include/uapi/linux/android/binder_netlink.h new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Simon Horman
reviewerQuoted 11 lines · Li Li
From: Li Li <[email protected]>
Introduce generic netlink messages into the binder driver so that the Linux/Android system administration processes can listen to important events and take corresponding actions, like stopping a broken app from attacking the OS by sending huge amount of spamming binder transactions.
The binder netlink sources and headers are automatically generated from the corresponding binder netlink YAML spec. Don't modify them directly.
Signed-off-by Li Li <[email protected]>
Hi Li Li,
Quoted 1 line
drivers/android/binder.c +0
-0
...
drivers/android/binder.c static void binder_transaction(struct binder_proc *proc,
Quoted 35 lines
static void binder_transaction(struct
binder_proc*proc,drivers/android/binder.c +11
-3struct binder_thread *thread, struct binder_transaction_data *tr, int reply,@@ -3683,10 +3764,14 @@ static void binder_transaction(struct binder_proc *proc, return_error_line = __LINE__; goto err_copy_data_failed; } -if (t->buffer->oneway_spam_suspect) +if (t->buffer->oneway_spam_suspect) { tcomplete->type =BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ; -else +if (binder_netlink_enabled (proc,BINDER_FLAG_SPAM )) +binder_netlink_report (context,BR_ONEWAY_SPAM_SUSPECT , + reply, t); + }else { tcomplete->type =BINDER_WORK_TRANSACTION_COMPLETE ; + } t->work.type =BINDER_WORK_TRANSACTION ;if (reply) {@@ -3736,8 +3821,12 @@ static void binder_transaction(struct binder_proc *proc,* process and is put in a pending queue, waiting for the target * process to be unfrozen. */ -if (return_error ==BR_TRANSACTION_PENDING_FROZEN ) +if (return_error ==BR_TRANSACTION_PENDING_FROZEN ) { tcomplete->type =BINDER_WORK_TRANSACTION_PENDING ; +if (binder_netlink_enabled (proc,BINDER_FLAG_ASYNC_FROZEN )) +binder_netlink_report (context, return_error, + reply, t); + }binder_enqueue_thread_work (thread, tcomplete);if (return_error && return_error !=BR_TRANSACTION_PENDING_FROZEN )@@ -3799,6 +3888,10 @@ static void binder_transaction(struct binder_proc *proc,
The code preceding this hunk looks like this:
err_alloc_tcomplete_failed:
if (trace_binder_txn_latency_free_enabled()) binder_txn_latency_free(t); kfree(t); binder_stats_deleted(BINDER_STAT_TRANSACTION);
err_alloc_t_failed:err_bad_todo_list:err_bad_call_stack:err_empty_call_stack:err_dead_binder:err_invalid_target_handle:
if (target_node) {
binder_dec_node(target_node, 1, 0);
binder_dec_node_tmpref(target_node);
}1. The labels err_bad_todo_list, err_bad_call_stack, err_empty_call_stack, and err_invalid_target_handle may be jumped to before t is initialised.
2. In the err_alloc_tcomplete_failed label t is kfree'd.
However, the call to binder_netlink_report below will dereference t.
Flagged by Smatch.
Quoted 10 lines
drivers/android/binder.c +4
-0binder_dec_node_tmpref (target_node); } +if (binder_netlink_enabled (proc,BINDER_FLAG_FAILED )) +binder_netlink_report (context, return_error, + reply, t); + binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, "%d:%d transaction %s to %d:%d failed %d/%d/%d, code %u size %lld-%lld line %d\n", proc->pid, thread->pid, reply ? "reply" :
...
Quoted 61 lines
drivers/android/binder.c +61
-0+/** +* binder_nl_report_setup_doit() - netlink .doit handler +* @skb: the metadata struct passed from netlink driver +* @info: the generic netlink struct passed from netlink driver +* +* Implements the .doit function to process binder netlink commands. +*/ +int binder_nl_report_setup_doit (struct sk_buff *skb,struct genl_info *info) +{ +struct binder_context *context =NULL ; +struct binder_device *device; +struct binder_proc *proc; +u32 flags, pid; +bool found; +void *hdr; +int ret; + + ret =security_binder_setup_report (current_cred ()); +if (ret <0 ) { +NL_SET_ERR_MSG (info->extack,"Permission denied" ); +return ret; + } + +if (nla_len (info->attrs[BINDER_A_CMD_CONTEXT ])) { +/* Search the specified binder context */ +hlist_for_each_entry (device, &binder_devices, hlist) { +if (!nla_strcmp (info->attrs[BINDER_A_CMD_CONTEXT ], + device->context.name)) { + context = &device->context; +break ; + } + } + +if (!context) { +NL_SET_ERR_MSG (info->extack,"Invalid binder context" ); +return -EINVAL ; + } + } + + pid =nla_get_u32 (info->attrs[BINDER_A_CMD_PID ]); + flags =nla_get_u32 (info->attrs[BINDER_A_CMD_FLAGS ]); + +if (!pid) { +if (!context) { +NL_SET_ERR_MSG (info->extack, +"Invalid binder context and pid" ); +return -EINVAL ; + } + +/* Set the global flags for the whole binder context */ + context->report_flags = flags; + }else { +/* Set the per-process flags */ + found = false; +mutex_lock (&binder_procs_lock); +hlist_for_each_entry (proc, &binder_procs, proc_node) { +if (proc->pid == pid + && (proc->context == context || !context)) { + proc->report_flags = flags; + found = true; + }
drivers/android/binder.c binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6500
Quoted 10 lines
drivers/android/binder.c +10
-0+ } +mutex_unlock (&binder_procs_lock); + +if (!found) { +NL_SET_ERR_MSG_FMT (info->extack, +"Invalid binder report pid %u" , + pid); +return -EINVAL ; + } + }
Within the above conditions it is assumed that context may be NULL.
drivers/android/binder.c binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6512
Quoted 12 lines
drivers/android/binder.c +12
-0+ + skb =genlmsg_new (GENLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!skb) { +pr_err ("Failed to alloc binder netlink reply message\n" ); +return -ENOMEM ; + } + + hdr =genlmsg_iput (skb, info); +if (!hdr) + goto free_skb; + + if (nla_put_string(skb, BINDER_A_CMD_CONTEXT, context->name) ||
But here context is dereferenced unconditionally.
This does not seem consistent.
Flagged by Smatch.
Quoted 23 lines
drivers/android/binder.c +23
-0+ nla_put_u32(skb, BINDER_A_CMD_PID, pid) || + nla_put_u32(skb, BINDER_A_CMD_FLAGS, flags)) + goto cancel_skb; + +genlmsg_end (skb, hdr); + +if (genlmsg_reply (skb, info)) { +pr_err ("Failed to send binder netlink reply message\n" ); +return -EFAULT ; + } + +return 0 ; + +cancel_skb: +pr_err ("Failed to add reply attributes to binder netlink message\n" ); +genlmsg_cancel (skb, hdr); +free_skb: +pr_err ("Free binder netlink reply message on error\n" ); +nlmsg_free (skb); + ret = -EMSGSIZE ; + +return ret; +}
...
Quoted 78 lines · Simon Horman
Quoted 11 lines · Li Li
From: Li Li <[email protected]>
Introduce generic netlink messages into the binder driver so that the Linux/Android system administration processes can listen to important events and take corresponding actions, like stopping a broken app from attacking the OS by sending huge amount of spamming binder transactions.
The binder netlink sources and headers are automatically generated from the corresponding binder netlink YAML spec. Don't modify them directly.
Signed-off-by Li Li <[email protected]>Hi Li Li,
Quoted 1 line
drivers/android/binder.c +0
-0...
drivers/android/binder.c static void binder_transaction(struct binder_proc *proc,
Quoted 35 lines
static void binder_transaction(struct
binder_proc*proc,
structbinder_thread*thread, structbinder_transaction_data*tr, int reply,drivers/android/binder.c +11
-3@@ -3683,10 +3764,14 @@ static void binder_transaction(struct binder_proc *proc, return_error_line = __LINE__; goto err_copy_data_failed; } -if (t->buffer->oneway_spam_suspect) +if (t->buffer->oneway_spam_suspect) { tcomplete->type =BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ; -else +if (binder_netlink_enabled (proc,BINDER_FLAG_SPAM )) +binder_netlink_report (context,BR_ONEWAY_SPAM_SUSPECT , + reply, t); + }else { tcomplete->type =BINDER_WORK_TRANSACTION_COMPLETE ; + } t->work.type =BINDER_WORK_TRANSACTION ;if (reply) {@@ -3736,8 +3821,12 @@ static void binder_transaction(struct binder_proc *proc,* process and is put in a pending queue, waiting for the target * process to be unfrozen. */ -if (return_error ==BR_TRANSACTION_PENDING_FROZEN ) +if (return_error ==BR_TRANSACTION_PENDING_FROZEN ) { tcomplete->type =BINDER_WORK_TRANSACTION_PENDING ; +if (binder_netlink_enabled (proc,BINDER_FLAG_ASYNC_FROZEN )) +binder_netlink_report (context, return_error, + reply, t); + }binder_enqueue_thread_work (thread, tcomplete);if (return_error && return_error !=BR_TRANSACTION_PENDING_FROZEN )@@ -3799,6 +3888,10 @@ static void binder_transaction(struct binder_proc *proc,The code preceding this hunk looks like this:
err_alloc_tcomplete_failed:if (trace_binder_txn_latency_free_enabled()) binder_txn_latency_free(t); kfree(t); binder_stats_deleted(BINDER_STAT_TRANSACTION);
err_alloc_t_failed:err_bad_todo_list:err_bad_call_stack:err_empty_call_stack:err_dead_binder:err_invalid_target_handle:if (target_node) { binder_dec_node(target_node, 1, 0); binder_dec_node_tmpref(target_node); }1. The labels
err_bad_todo_list,err_bad_call_stack,err_empty_call_stack, anderr_invalid_target_handlemay be jumped to before t is initialised.
Quoted 6 lines
2. In the
err_alloc_tcomplete_failedlabel t is kfree'd.However, the call to
binder_netlink_reportbelow will dereference t.Flagged by Smatch.
Thank you for flagging this! Let me double check the lifecycle of t.
Quoted 103 lines
Quoted 10 lines
drivers/android/binder.c +4
-0binder_dec_node_tmpref (target_node); } +if (binder_netlink_enabled (proc,BINDER_FLAG_FAILED )) +binder_netlink_report (context, return_error, + reply, t); + binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, "%d:%d transaction %s to %d:%d failed %d/%d/%d, code %u size %lld-%lld line %d\n", proc->pid, thread->pid, reply ? "reply" :...
Quoted 61 lines
drivers/android/binder.c +61
-0+/** +* binder_nl_report_setup_doit() - netlink .doit handler +* @skb: the metadata struct passed from netlink driver +* @info: the generic netlink struct passed from netlink driver +* +* Implements the .doit function to process binder netlink commands. +*/ +int binder_nl_report_setup_doit (struct sk_buff *skb,struct genl_info *info) +{ +struct binder_context *context =NULL ; +struct binder_device *device; +struct binder_proc *proc; +u32 flags, pid; +bool found; +void *hdr; +int ret; + + ret =security_binder_setup_report (current_cred ()); +if (ret <0 ) { +NL_SET_ERR_MSG (info->extack,"Permission denied" ); +return ret; + } + +if (nla_len (info->attrs[BINDER_A_CMD_CONTEXT ])) { +/* Search the specified binder context */ +hlist_for_each_entry (device, &binder_devices, hlist) { +if (!nla_strcmp (info->attrs[BINDER_A_CMD_CONTEXT ], + device->context.name)) { + context = &device->context; +break ; + } + } + +if (!context) { +NL_SET_ERR_MSG (info->extack,"Invalid binder context" ); +return -EINVAL ; + } + } + + pid =nla_get_u32 (info->attrs[BINDER_A_CMD_PID ]); + flags =nla_get_u32 (info->attrs[BINDER_A_CMD_FLAGS ]); + +if (!pid) { +if (!context) { +NL_SET_ERR_MSG (info->extack, +"Invalid binder context and pid" ); +return -EINVAL ; + } + +/* Set the global flags for the whole binder context */ + context->report_flags = flags; + }else { +/* Set the per-process flags */ + found = false; +mutex_lock (&binder_procs_lock); +hlist_for_each_entry (proc, &binder_procs, proc_node) { +if (proc->pid == pid + && (proc->context == context || !context)) { + proc->report_flags = flags; + found = true; + }drivers/android/binder.c binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6500
Quoted 10 lines
drivers/android/binder.c +10
-0+ } +mutex_unlock (&binder_procs_lock); + +if (!found) { +NL_SET_ERR_MSG_FMT (info->extack, +"Invalid binder report pid %u" , + pid); +return -EINVAL ; + } + }Within the above conditions it is assumed that context may be NULL.
drivers/android/binder.c binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6512
Quoted 12 lines
drivers/android/binder.c +12
-0+ + skb =genlmsg_new (GENLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!skb) { +pr_err ("Failed to alloc binder netlink reply message\n" ); +return -ENOMEM ; + } + + hdr =genlmsg_iput (skb, info); +if (!hdr) + goto free_skb; + + if (nla_put_string(skb, BINDER_A_CMD_CONTEXT, context->name) ||But here context is dereferenced unconditionally.
This does not seem consistent.
Quoted 2 lines
Flagged by Smatch.
Indeed, I should use proc->context->name here.
Quoted 25 lines
Quoted 23 lines
drivers/android/binder.c +23
-0+ nla_put_u32(skb, BINDER_A_CMD_PID, pid) || + nla_put_u32(skb, BINDER_A_CMD_FLAGS, flags)) + goto cancel_skb; + +genlmsg_end (skb, hdr); + +if (genlmsg_reply (skb, info)) { +pr_err ("Failed to send binder netlink reply message\n" ); +return -EFAULT ; + } + +return 0 ; + +cancel_skb: +pr_err ("Failed to add reply attributes to binder netlink message\n" ); +genlmsg_cancel (skb, hdr); +free_skb: +pr_err ("Free binder netlink reply message on error\n" ); +nlmsg_free (skb); + ret = -EMSGSIZE ; + +return ret; +}...
PATCH RESEND v17 3/3 binder: transaction report binder_features flag
In reply to Li LiFrom: Li Li <[email protected]>
Add a flag to binder_features to indicate that the transaction report feature via generic netlink is available.
--- drivers/android/binderfs.c | 8 ++++++++ .../selftests/filesystems/binderfs/binderfs_test.c | 1 + 2 files changed, 9 insertions(+)
drivers/android/binderfs.c +8 -0
@@ -59,6 +59,7 @@ struct binder_features {bool oneway_spam_detection;bool extended_error;bool freeze_notification; +bool transaction_report; };static const struct constant_table binderfs_param_stats[] = {
@@ -76,6 +77,7 @@ static struct binder_features binder_features = { .oneway_spam_detection = true, .extended_error = true, .freeze_notification = true, + .transaction_report = true, };static inline struct binderfs_info *BINDERFS_SB (const struct super_block *sb)
@@ -619,6 +621,12 @@ static int init_binder_features(struct super_block *sb)if (IS_ERR (dentry))return PTR_ERR (dentry); + dentry =binderfs_create_file (dir,"transaction_report" , + &binder_features_fops, + &binder_features.transaction_report); +if (IS_ERR (dentry)) +return PTR_ERR (dentry); +return 0 ; }
tools/testing/selftests/filesystems/binderfs/binderfs_test.c +1 -0
@@ -65,6 +65,7 @@ static int __do_binderfs_test(struct __test_metadata *_metadata)"oneway_spam_detection" ,"extended_error" ,"freeze_notification" , +"transaction_report" , };change_mountns (_metadata);
Signature · 1 line
2.49.0.805.g082f7c87e0-goog