binder: report txn errors via generic netlink
Subsystems (5)
v11 cover binder: report txn errors via generic netlink
From: Li Li <[email protected]>
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.
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
Li Li (2):
binderfs: add new binder devices to binder_devices
binder: report txn errors via generic netlink
Documentation/admin-guide/binder_netlink.rst | 110 +++++++++ Documentation/admin-guide/index.rst | 1 + Documentation/netlink/specs/binder.yaml | 108 +++++++++ drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 241 ++++++++++++++++++- drivers/android/binder_internal.h | 29 ++- drivers/android/binder_netlink.c | 39 +++ drivers/android/binder_netlink.h | 18 ++ drivers/android/binder_trace.h | 35 +++ drivers/android/binderfs.c | 2 + include/uapi/linux/android/binder_netlink.h | 55 +++++ 12 files changed, 635 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: aef25be35d23ec768eed08bfcf7ca3cf9685bc28
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH v11 1/2 binderfs: add new binder devices to binder_devices
From: Li Li <[email protected]>
When binderfs is not enabled, the binder driver parses the kernel config to create all binder devices. All of the new binder devices are stored in the list binder_devices.
When binderfs is enabled, the binder driver creates new binder devices dynamically when userspace applications call BINDER_CTL_ADD ioctl. But the devices created in this way are not stored in the same list.
This patch fixes that.
--- drivers/android/binder.c | 5 +++++ drivers/android/binder_internal.h | 11 +++++++++-- drivers/android/binderfs.c | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-)
drivers/android/binder.c +5 -0
@@ -6928,6 +6928,11 @@ const struct binder_debugfs_entry binder_debugfs_entries[] = { {}/* terminator */ }; +void binder_add_device (struct binder_device *device) +{ +hlist_add_head (&device->hlist, &binder_devices); +} +static int __init init_binder_device (const char *name) {int ret;
drivers/android/binder_internal.h +9 -2
@@ -25,8 +25,7 @@ struct binder_context {/** * struct binder_device - information about a binder device node -* @hlist: list of binder devices (only used for devices requested via -* CONFIG_ANDROID_BINDER_DEVICES) +* @hlist: list of binder devices * @miscdev: information about a binder character device node * @context: binder context information * @binderfs_inode: This is the inode of the root dentry of the super block
@@ -582,4 +581,12 @@ struct binder_object { }; }; +/** +* Add a binder device to binder_devices +* @device: the new binder device to add to the global list +* +* Not reentrant as the list is not protected by any locks +*/ +void binder_add_device (struct binder_device *device); +#endif /* _LINUX_BINDER_INTERNAL_H */
drivers/android/binderfs.c +2 -0
@@ -207,6 +207,8 @@ static int binderfs_binder_device_create(struct inode *ref_inode,fsnotify_create (root->d_inode, dentry);inode_unlock (d_inode (root)); +binder_add_device (device); +return 0 ; err:
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH v11 2/2 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 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 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 | 110 +++++++++ Documentation/admin-guide/index.rst | 1 + Documentation/netlink/specs/binder.yaml | 108 +++++++++ drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 236 ++++++++++++++++++- drivers/android/binder_internal.h | 18 ++ drivers/android/binder_netlink.c | 39 +++ drivers/android/binder_netlink.h | 18 ++ drivers/android/binder_trace.h | 35 +++ include/uapi/linux/android/binder_netlink.h | 55 +++++ 11 files changed, 619 insertions(+), 4 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 +110 -0
@@ -0,0 +1,110 @@ +.. 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 has an independent Generic +Netlink for security reason. To prevent untrusted user applications from +accessing the netlink data, the kernel driver uses unicast mode instead of +multicast. + +Basically, the user space code uses the BINDER_NETLINK_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_NETLINK_CMD_REPORT_SETUP command also +registers the current user space process to receive the reports. When the +user space process exits, the previous request will be reset automatically. + +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_NETLINK_CMD_REPORT command to send a generic netlink message to the +registered process, containing the payload defined in binder_netlink.yaml. + +More details about the flags, attributes and operations can be found at the +the doc sections in Documentations/netlink/specs/binder_netlink.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. + +.. note:: + If the userspace application that talks to the driver exits, the kernel + driver will automatically reset the configuration to the default and + stop sending more reports, which would otherwise fail. + +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_NETLINK_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]; + + // enable per-context binder report + send(fd, id, BINDER_NETLINK_CMD_REPORT_SETUP, "binder", 0, + BINDER_NETLINK_FLAG_FAILED | BINDER_NETLINK_FLAG_DELAYED); + + // confirm the per-context configuration + data = recv(fd, BINDER_NETLINK_CMD_REPLY); + char *context = nla(data)[BINDER_NETLINK_A_CMD_CONTEXT]; + __u32 pid = nla(data)[BINDER_NETLINK_A_CMD_PID]; + __u32 flags = nla(data)[BINDER_NETLINK_A_CMD_FLAGS]; + + // set optional per-process report, overriding the per-context one + send(fd, id, BINDER_NETLINK_CMD_REPORT_SETUP, "binder", getpid(), + BINDER_NETLINK_FLAG_SPAM | BINDER_REPORT_OVERRIDE); + + // confirm the optional per-process configuration + data = recv(fd, BINDER_NETLINK_CMD_REPLY); + context = nla(data)[BINDER_NETLINK_A_CMD_CONTEXT]; + pid = nla(data)[BINDER_NETLINK_A_CMD_PID]; + flags = nla(data)[BINDER_NETLINK_A_CMD_FLAGS]; + + // wait and read all binder reports + while (running) { + data = recv(fd, BINDER_NETLINK_CMD_REPORT); + auto *attr = nla(data)[BINDER_NETLINK_A_REPORT_XXX]; + + // process binder report + do_something(*attr); + } + + // clean up + send(fd, id, BINDER_NETLINK_CMD_REPORT_SETUP, 0, 0); + send(fd, id, BINDER_NETLINK_CMD_REPORT_SETUP, getpid(), 0); + close(fd);
Documentation/admin-guide/index.rst +1 -0
@@ -79,6 +79,7 @@ configure specific aspects of kernel behavior to your liking. aoe/index auxdisplay/index bcache + binder_netlink binderfs binfmt-misc blockdev/index
Documentation/netlink/specs/binder.yaml new file +108 -0
108 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 +233 -3
287 lines not on this page · raw message
drivers/android/binder_internal.h +18 -0
@@ -12,15 +12,30 @@#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_portid: the netlink socket to receive binder reports +* @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_portid; +u32 report_flags; +atomic_t report_seq; };/**
@@ -414,6 +429,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 */
@@ -452,6 +469,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 +39 -0
@@ -0,0 +1,39 @@ +// 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 source */ + +#include <net/netlink.h> +#include <net/genetlink.h> + +#include "binder_netlink.h" + +#include <uapi/linux/android/binder_netlink.h> + +/* BINDER_CMD_REPORT_SETUP - do */ +static const struct nla_policy binder_report_setup_nl_policy[BINDER_A_CMD_FLAGS +1 ] = { + [BINDER_A_CMD_CONTEXT ] = { .type =NLA_NUL_STRING , }, + [BINDER_A_CMD_PID ] = { .type =NLA_U32 , }, + [BINDER_A_CMD_FLAGS ] =NLA_POLICY_MASK (NLA_U32 ,0xf ), +}; + +/* Ops table for binder */ +static const struct genl_split_ops binder_nl_ops[] = { + { + .cmd =BINDER_CMD_REPORT_SETUP , + .doit = binder_nl_report_setup_doit, + .policy = binder_report_setup_nl_policy, + .maxattr =BINDER_A_CMD_FLAGS , + .flags =GENL_CMD_CAP_DO , + }, +}; + +struct genl_family binder_nl_family__ro_after_init = { + .name =BINDER_FAMILY_NAME , + .version =BINDER_FAMILY_VERSION , + .netnsok = true, + .parallel_ops = true, + .module =THIS_MODULE , + .split_ops = binder_nl_ops, + .n_split_ops =ARRAY_SIZE (binder_nl_ops), +};
drivers/android/binder_netlink.h new file +18 -0
@@ -0,0 +1,18 @@ +/* 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> + +int binder_nl_report_setup_doit (struct sk_buff *skb,struct genl_info *info); + +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 pid, u32 tid, u32 to_pid, + u32 to_tid, u32 reply, struct binder_transaction_data *tr), +TP_ARGS (name, err, pid, tid, to_pid, to_tid, reply, tr), +TP_STRUCT__entry ( + __field(const char *, name) + __field(u32, err) + __field(u32, pid) + __field(u32, 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->pid = pid; + __entry->tid = tid; + __entry->to_pid = to_pid; + __entry->to_tid = to_tid; + __entry->reply = reply; + __entry->flags = tr->flags; + __entry->code = tr->code; + __entry->data_size = tr->data_size; + ), +TP_printk ("%s: %d %d:%d -> %d:%d %s flags=0x08%x code=%d %llu" , + __entry->name, __entry->err, __entry->pid, __entry->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 +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Carlos Llamas
maintainerdrivers/android/binder.c static int binder_release(struct inode *nodp, struct file *filp) · line 6269
Quoted 9 lines · Li Li
From: Li Li <[email protected]>
drivers/android/binder.c +3
-0@@ -6137,6 +6264,11 @@ static int binder_release(struct inode *nodp, struct file *filp)binder_defer_work (proc,BINDER_DEFERRED_RELEASE ); +if (proc->pid == proc->context->report_portid) { + proc->context->report_portid =0 ; + proc->context->report_flags =0 ;
Isn't ->portid the pid from the netlink report manager? How is this ever going to match a certain proc->pid here? Is this manager supposed to _also_ open a regular binder fd?
It seems we are tying the cleanup of the netlink interface to the exit of the regular binder device, correct? This seems unfortunate as using the netlink interface should be independent.
I was playing around with this patch with my own PoC and now I'm stuck:
root@debian:~# ./binder-netlink ./binder-netlink: nlmsgerr No permission to set flags from 1301: Unknown error -1
Is there a different way to reset the protid?
--
Carlos Llamas
Carlos Llamas
maintainerQuoted 19 lines · Carlos Llamas
drivers/android/binder.c static int binder_release(struct inode *nodp, struct file *filp) · line 6269
Quoted 9 lines · Li Li
From: Li Li <[email protected]>
drivers/android/binder.c +3
-0@@ -6137,6 +6264,11 @@ static int binder_release(struct inode *nodp, struct file *filp)binder_defer_work (proc,BINDER_DEFERRED_RELEASE ); +if (proc->pid == proc->context->report_portid) { + proc->context->report_portid =0 ; + proc->context->report_flags =0 ;Isn't ->portid the pid from the netlink report manager? How is this ever going to match a certain
proc->pidhere? Is this manager supposed to _also_ open a regular binder fd?It seems we are tying the cleanup of the netlink interface to the exit of the regular binder device, correct? This seems unfortunate as using the netlink interface should be independent.
Quoted 6 lines
I was playing around with this patch with my own PoC and now I'm stuck:
root@debian:~# ./binder-netlink ./binder-netlink: nlmsgerr No permission to set flags from 1301: Unknown error -1Is there a different way to reset the protid?
Furthermore, this seems to be a problem when the report manager exits without a binder instance, we still think the report is enabled:
[ 202.821346] binder: Failed to send binder netlink message to 597: -111 [ 202.821421] binder: Failed to send binder netlink message to 597: -111 [ 202.821304] binder: Failed to send binder netlink message to 597: -111 [ 202.821306] binder: Failed to send binder netlink message to 597: -111 [ 202.821387] binder: Failed to send binder netlink message to 597: -111 [ 202.821464] binder: Failed to send binder netlink message to 597: -111 [ 202.821467] binder: Failed to send binder netlink message to 597: -111 [ 202.821344] binder: Failed to send binder netlink message to 597: -111 [ 202.822513] binder: Failed to send binder netlink message to 597: -111 [ 202.822152] binder: Failed to send binder netlink message to 597: -111 [ 202.822683] binder: Failed to send binder netlink message to 597: -111 [ 202.822629] binder: Failed to send binder netlink message to 597: -111
Quoted 31 lines · Carlos Llamas
Quoted 19 lines · Carlos Llamas
drivers/android/binder.c static int binder_release(struct inode *nodp, struct file *filp) · line 6269
Quoted 9 lines · Li Li
From: Li Li <[email protected]>
drivers/android/binder.c +3
-0@@ -6137,6 +6264,11 @@ static int binder_release(struct inode *nodp, struct file *filp)binder_defer_work (proc,BINDER_DEFERRED_RELEASE ); +if (proc->pid == proc->context->report_portid) { + proc->context->report_portid =0 ; + proc->context->report_flags =0 ;Isn't ->portid the pid from the netlink report manager? How is this ever going to match a certain
proc->pidhere? Is this manager supposed to _also_ open a regular binder fd?It seems we are tying the cleanup of the netlink interface to the exit of the regular binder device, correct? This seems unfortunate as using the netlink interface should be independent.
Quoted 6 lines
I was playing around with this patch with my own PoC and now I'm stuck:
root@debian:~# ./binder-netlink ./binder-netlink: nlmsgerr No permission to set flags from 1301: Unknown error -1Is there a different way to reset the protid?
Furthermore, this seems to be a problem when the report manager exits without a binder instance, we still think the report is enabled:
Quoted 12 lines
[ 202.821346] binder: Failed to send binder netlink message to 597: -111 [ 202.821421] binder: Failed to send binder netlink message to 597: -111 [ 202.821304] binder: Failed to send binder netlink message to 597: -111 [ 202.821306] binder: Failed to send binder netlink message to 597: -111 [ 202.821387] binder: Failed to send binder netlink message to 597: -111 [ 202.821464] binder: Failed to send binder netlink message to 597: -111 [ 202.821467] binder: Failed to send binder netlink message to 597: -111 [ 202.821344] binder: Failed to send binder netlink message to 597: -111 [ 202.822513] binder: Failed to send binder netlink message to 597: -111 [ 202.822152] binder: Failed to send binder netlink message to 597: -111 [ 202.822683] binder: Failed to send binder netlink message to 597: -111 [ 202.822629] binder: Failed to send binder netlink message to 597: -111
As the file path (linux/drivers/android/binder.c) suggested, binder driver is designed to work as the essential IPC in the Android OS, where binder is used by all system and user apps.
So the binder netlink is designed to be used with binder IPC.
The manager service also uses the binder interface to communicate to all other processes. When it exits, the binder file is closed, where the netlink interface is reset.
Carlos Llamas
maintainerQuoted 45 lines · Li Li
Quoted 31 lines · Carlos Llamas
Quoted 19 lines · Carlos Llamas
drivers/android/binder.c static int binder_release(struct inode *nodp, struct file *filp) · line 6269
Quoted 9 lines · Li Li
From: Li Li <[email protected]>
drivers/android/binder.c +3
-0@@ -6137,6 +6264,11 @@ static int binder_release(struct inode *nodp, struct file *filp)binder_defer_work (proc,BINDER_DEFERRED_RELEASE ); +if (proc->pid == proc->context->report_portid) { + proc->context->report_portid =0 ; + proc->context->report_flags =0 ;Isn't ->portid the pid from the netlink report manager? How is this ever going to match a certain
proc->pidhere? Is this manager supposed to _also_ open a regular binder fd?It seems we are tying the cleanup of the netlink interface to the exit of the regular binder device, correct? This seems unfortunate as using the netlink interface should be independent.
Quoted 6 lines
I was playing around with this patch with my own PoC and now I'm stuck:
root@debian:~# ./binder-netlink ./binder-netlink: nlmsgerr No permission to set flags from 1301: Unknown error -1Is there a different way to reset the protid?
Furthermore, this seems to be a problem when the report manager exits without a binder instance, we still think the report is enabled:
Quoted 12 lines
[ 202.821346] binder: Failed to send binder netlink message to 597: -111 [ 202.821421] binder: Failed to send binder netlink message to 597: -111 [ 202.821304] binder: Failed to send binder netlink message to 597: -111 [ 202.821306] binder: Failed to send binder netlink message to 597: -111 [ 202.821387] binder: Failed to send binder netlink message to 597: -111 [ 202.821464] binder: Failed to send binder netlink message to 597: -111 [ 202.821467] binder: Failed to send binder netlink message to 597: -111 [ 202.821344] binder: Failed to send binder netlink message to 597: -111 [ 202.822513] binder: Failed to send binder netlink message to 597: -111 [ 202.822152] binder: Failed to send binder netlink message to 597: -111 [ 202.822683] binder: Failed to send binder netlink message to 597: -111 [ 202.822629] binder: Failed to send binder netlink message to 597: -111
Quoted 5 lines
As the file path (
linux/drivers/android/binder.c) suggested, binder driver is designed to work as the essential IPC in the Android OS, where binder is used by all system and user apps.So the binder netlink is designed to be used with binder IPC.
Ok, I assume this decision was made because no better alternative was found. Otherwise it would be best to avoid the dependency. This could become an issue e.g. if the admin process was to be split in the future or some other restructuring happens.
That's why I ask of there is a way to cleanup the netlink info without relying on the binder fd closing. Something cleaner, there might be some callback we can install on the netlink infra? I could look later into this.
Quoted 3 lines
The manager service also uses the binder interface to communicate to all other processes. When it exits, the binder file is closed, where the netlink interface is reset.
Again, communicating with other processes via binder and setting up a transaction report should be separate functionalities that don't rely on eachother.
Also, it seems the admin process would have to initially bind() to all binder contexts preventing other from doing so? Sound like this should be restricted to certain capability or maybe via selinux (if possible) instead of relying on the portid. Thoughts?
--
Carlos Llamas
Quoted 67 lines · Carlos Llamas
Quoted 45 lines · Li Li
Quoted 31 lines · Carlos Llamas
Quoted 19 lines · Carlos Llamas
drivers/android/binder.c static int binder_release(struct inode *nodp, struct file *filp) · line 6269
Quoted 9 lines · Li Li
From: Li Li <[email protected]>
drivers/android/binder.c +3
-0@@ -6137,6 +6264,11 @@ static int binder_release(struct inode *nodp, struct file *filp)binder_defer_work (proc,BINDER_DEFERRED_RELEASE ); +if (proc->pid == proc->context->report_portid) { + proc->context->report_portid =0 ; + proc->context->report_flags =0 ;Isn't ->portid the pid from the netlink report manager? How is this ever going to match a certain
proc->pidhere? Is this manager supposed to _also_ open a regular binder fd?It seems we are tying the cleanup of the netlink interface to the exit of the regular binder device, correct? This seems unfortunate as using the netlink interface should be independent.
Quoted 6 lines
I was playing around with this patch with my own PoC and now I'm stuck:
root@debian:~# ./binder-netlink ./binder-netlink: nlmsgerr No permission to set flags from 1301: Unknown error -1Is there a different way to reset the protid?
Furthermore, this seems to be a problem when the report manager exits without a binder instance, we still think the report is enabled:
Quoted 12 lines
[ 202.821346] binder: Failed to send binder netlink message to 597: -111 [ 202.821421] binder: Failed to send binder netlink message to 597: -111 [ 202.821304] binder: Failed to send binder netlink message to 597: -111 [ 202.821306] binder: Failed to send binder netlink message to 597: -111 [ 202.821387] binder: Failed to send binder netlink message to 597: -111 [ 202.821464] binder: Failed to send binder netlink message to 597: -111 [ 202.821467] binder: Failed to send binder netlink message to 597: -111 [ 202.821344] binder: Failed to send binder netlink message to 597: -111 [ 202.822513] binder: Failed to send binder netlink message to 597: -111 [ 202.822152] binder: Failed to send binder netlink message to 597: -111 [ 202.822683] binder: Failed to send binder netlink message to 597: -111 [ 202.822629] binder: Failed to send binder netlink message to 597: -111Quoted 5 lines
As the file path (
linux/drivers/android/binder.c) suggested, binder driver is designed to work as the essential IPC in the Android OS, where binder is used by all system and user apps.So the binder netlink is designed to be used with binder IPC.
Ok, I assume this decision was made because no better alternative was found. Otherwise it would be best to avoid the dependency. This could become an issue e.g. if the admin process was to be split in the future or some other restructuring happens.
That's why I ask of there is a way to cleanup the netlink info without relying on the binder fd closing. Something cleaner, there might be some callback we can install on the netlink infra? I could look later into this.
Quoted 3 lines
The manager service also uses the binder interface to communicate to all other processes. When it exits, the binder file is closed, where the netlink interface is reset.
Quoted 8 lines
Again, communicating with other processes via binder and setting up a transaction report should be separate functionalities that don't rely on eachother.
Also, it seems the admin process would have to initially
bind()to all binder contexts preventing other from doing so? Sound like this should be restricted to certain capability or maybe via selinux (if possible) instead of relying on the portid. Thoughts?
This is a valid concern. Adding GENL_ADMIN_PERM should be enough to solve it.
Quoted 3 lines
--
Carlos Llamas
Carlos Llamas
maintainerQuoted 1 line · Li Li
This is a valid concern. Adding
GENL_ADMIN_PERMshould be enough to solve it.
Right! That seems to ask the genl stack to check for CAP_NET_ADMIN:
if ((op.flags & GENL_ADMIN_PERM) &&
!netlink_capable(skb, CAP_NET_ADMIN))
return -EPERM;... which is a much better option and we could drop the portid check to validate permissions. Something like the following (untested)?
Documentation/netlink/specs/binder.yaml +1 -0
@@ -81,6 +81,7 @@ operations: name: report-setup doc: Set flags from user space. attribute-set: cmd + flags: [ admin-perm ] do: request: ¶ms
drivers/android/binder.c +0 -7
@@ -6500,13 +6500,6 @@ int binder_nl_report_setup_doit(struct sk_buff *skb, struct genl_info *info) pid =nla_get_u32 (info->attrs[BINDER_A_CMD_PID ]); flags =nla_get_u32 (info->attrs[BINDER_A_CMD_FLAGS ]); -if (context->report_portid && context->report_portid != portid) { -NL_SET_ERR_MSG_FMT (info->extack, -"No permission to set flags from %d" , - portid); -return -EPERM ; - } -
if (!pid) {
/* Set the global flags for the whole binder context */
context->report_flags = flags;drivers/android/binder_netlink.c +1 -1
@@ -24,7 +24,7 @@ static const struct genl_split_ops binder_nl_ops[] = { .doit = binder_nl_report_setup_doit, .policy = binder_report_setup_nl_policy, .maxattr = BINDER_A_CMD_FLAGS, - .flags = GENL_CMD_CAP_DO, + .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, };
Carlos Llamas
maintainerQuoted 49 lines · Li Li
Quoted 31 lines · Carlos Llamas
Quoted 19 lines · Carlos Llamas
drivers/android/binder.c static int binder_release(struct inode *nodp, struct file *filp) · line 6269
Quoted 9 lines · Li Li
From: Li Li <[email protected]>
drivers/android/binder.c +3
-0@@ -6137,6 +6264,11 @@ static int binder_release(struct inode *nodp, struct file *filp)binder_defer_work (proc,BINDER_DEFERRED_RELEASE ); +if (proc->pid == proc->context->report_portid) { + proc->context->report_portid =0 ; + proc->context->report_flags =0 ;Isn't ->portid the pid from the netlink report manager? How is this ever going to match a certain
proc->pidhere? Is this manager supposed to _also_ open a regular binder fd?It seems we are tying the cleanup of the netlink interface to the exit of the regular binder device, correct? This seems unfortunate as using the netlink interface should be independent.
Quoted 6 lines
I was playing around with this patch with my own PoC and now I'm stuck:
root@debian:~# ./binder-netlink ./binder-netlink: nlmsgerr No permission to set flags from 1301: Unknown error -1Is there a different way to reset the protid?
Furthermore, this seems to be a problem when the report manager exits without a binder instance, we still think the report is enabled:
Quoted 12 lines
[ 202.821346] binder: Failed to send binder netlink message to 597: -111 [ 202.821421] binder: Failed to send binder netlink message to 597: -111 [ 202.821304] binder: Failed to send binder netlink message to 597: -111 [ 202.821306] binder: Failed to send binder netlink message to 597: -111 [ 202.821387] binder: Failed to send binder netlink message to 597: -111 [ 202.821464] binder: Failed to send binder netlink message to 597: -111 [ 202.821467] binder: Failed to send binder netlink message to 597: -111 [ 202.821344] binder: Failed to send binder netlink message to 597: -111 [ 202.822513] binder: Failed to send binder netlink message to 597: -111 [ 202.822152] binder: Failed to send binder netlink message to 597: -111 [ 202.822683] binder: Failed to send binder netlink message to 597: -111 [ 202.822629] binder: Failed to send binder netlink message to 597: -111As the file path (
linux/drivers/android/binder.c) suggested, binder driver is designed to work as the essential IPC in the Android OS, where binder is used by all system and user apps.
Quoted 5 lines
So the binder netlink is designed to be used with binder IPC.
The manager service also uses the binder interface to communicate to all other processes. When it exits, the binder file is closed, where the netlink interface is reset.
Did you happen to look into netlink_register_notifier()? That seems like an option to keep the device vs netlink socket interface from mixing up. I believe we could check for NETLINK_URELEASE events and do the cleanup then. I'll do a quick try.
Carlos Llamas
maintainerQuoted 4 lines · Carlos Llamas
Did you happen to look into
netlink_register_notifier()? That seems like an option to keep the device vs netlink socket interface from mixing up. I believe we could check forNETLINK_URELEASEevents and do the cleanup then. I'll do a quick try.
Yeah, this quick prototype worked for me. Although I haven't looked at the api details closely.
drivers/android/binder.c +0 -0
+static int binder_netlink_notify(struct notifier_block *nb,
+ + + + unsigned long action,
+ + + + void *data)
+{
+ struct netlink_notify *n = data;
+ struct binder_device *device;
+
+ if (action != NETLINK_URELEASE)
+ + return NOTIFY_DONE;
+
+ hlist_for_each_entry(device, &binder_devices, hlist) {
+ + if (device->context.report_portid == n->portid)
+ + + pr_info("%s: socket released\n", __func__);
+ }
+
+ return NOTIFY_DONE;
+}
+
+static struct notifier_block binder_netlink_notifier = {
+ .notifier_call = binder_netlink_notify,
+};
+
static int __init binder_init(void)
{
+ int ret;
@@ -7244,6 +7259,8 @@ static int __init binder_init(void)
+ + goto err_init_binder_device_failed;
+ }
+ netlink_register_notifier(&binder_netlink_notifier);
+
+ return ret;
err_init_binder_device_failed:
With that change we get notified when the socket that registered the report exits:
root@debian:~# ./binder-netlink report setup complete! ^C[ 63.682485] binder: binder_netlink_notify: socket released
I don't know if this would be the preferred approach to "install" a notification callback with a netlink socket but it works. wdyt?
--
Carlos Llamas
Thank you for the suggestion!
Cleaning up in the NETLINK_URELEASE notifier is better since we register the process with the netlink socket. I'll change the code accordingly.
Quoted 63 lines · Carlos Llamas
Quoted 4 lines · Carlos Llamas
Did you happen to look into
netlink_register_notifier()? That seems like an option to keep the device vs netlink socket interface from mixing up. I believe we could check forNETLINK_URELEASEevents and do the cleanup then. I'll do a quick try.Yeah, this quick prototype worked for me. Although I haven't looked at the api details closely.
drivers/android/binder.c +28
-0+static int binder_netlink_notify (struct notifier_block *nb, + + + + unsigned long action, + + + + void *data) +{ +struct netlink_notify *n = data; +struct binder_device *device; + +if (action !=NETLINK_URELEASE ) + + return NOTIFY_DONE; + +hlist_for_each_entry (device, &binder_devices, hlist) { + + if (device->context.report_portid == n->portid) + + +pr_info ("%s: socket released\n" , __func__); + } + +return NOTIFY_DONE ; +} + +static struct notifier_block binder_netlink_notifier = { + .notifier_call = binder_netlink_notify, +}; +static int __init binder_init (void ) { +int ret;@@ -7244,6 +7259,8 @@ static int __init binder_init(void) + + goto err_init_binder_device_failed; + } +netlink_register_notifier (&binder_netlink_notifier); + +return ret; err_init_binder_device_failed:With that change we get notified when the socket that registered the report exits:
root@debian:~# ./binder-netlink report setup complete! ^C[ 63.682485] binder: binder_netlink_notify: socket releasedI don't know if this would be the preferred approach to "install" a notification callback with a netlink socket but it works. wdyt?
--
Carlos Llamas
Jakub Kicinski
maintainerQuoted 3 lines · On Thu, 9 Jan 2025 11:48:24 -0800 Li Li wrote:
Cleaning up in the
NETLINK_URELEASEnotifier is better since we register the process with the netlink socket. I'll change the code accordingly.
Hm. Thought I already told you this. Maybe I'm mixing up submissions.
Please the unbind callback or possibly the sock priv infra (genl_sk_priv_get, sock_priv_destroy etc).
Carlos Llamas
maintainerQuoted 9 lines · Jakub Kicinski
Quoted 3 lines · On Thu, 9 Jan 2025 11:48:24 -0800 Li Li wrote:
Cleaning up in the
NETLINK_URELEASEnotifier is better since we register the process with the netlink socket. I'll change the code accordingly.Hm. Thought I already told you this. Maybe I'm mixing up submissions.
Please the unbind callback or possibly the sock priv infra (
genl_sk_priv_get,sock_priv_destroyetc).
Sorry, it was me that suggested NETLINK_URELEASE. BTW, I did try those genl_family callbacks first but I couldn't get them to work right away so I moved on. I'll have a closer look now to figure out what I did wrong. Thanks for the suggestion Jakub!
--
Carlos Llamas
Jakub Kicinski
maintainerQuoted 8 lines · On Thu, 9 Jan 2025 23:19:40 +0000 Carlos Llamas wrote:
Quoted 7 lines · Jakub Kicinski
Quoted 3 lines · On Thu, 9 Jan 2025 11:48:24 -0800 Li Li wrote:
Cleaning up in the
NETLINK_URELEASEnotifier is better since we register the process with the netlink socket. I'll change the code accordingly.Hm. Thought I already told you this. Maybe I'm mixing up submissions.
Quoted 7 lines
Quoted 2 lines
Please the unbind callback or possibly the sock priv infra (
genl_sk_priv_get,sock_priv_destroyetc).Sorry, it was me that suggested
NETLINK_URELEASE. BTW, I did try thosegenl_familycallbacks first but I couldn't get them to work right away so I moved on. I'll have a closer look now to figure out what I did wrong. Thanks for the suggestion Jakub!
Hm, that's probably because there is no real multicast group here :( genl_sk_priv_get() and co. may work better in that case. your suggestion of NETLINK_URELEASE may work too, tho, I think it's the most error prone
Quoted 18 lines · Jakub Kicinski
Quoted 8 lines · On Thu, 9 Jan 2025 23:19:40 +0000 Carlos Llamas wrote:
Quoted 7 lines · Jakub Kicinski
Quoted 3 lines · On Thu, 9 Jan 2025 11:48:24 -0800 Li Li wrote:
Cleaning up in the
NETLINK_URELEASEnotifier is better since we register the process with the netlink socket. I'll change the code accordingly.Hm. Thought I already told you this. Maybe I'm mixing up submissions.
Quoted 7 lines
Quoted 2 lines
Please the unbind callback or possibly the sock priv infra (
genl_sk_priv_get,sock_priv_destroyetc).Sorry, it was me that suggested
NETLINK_URELEASE. BTW, I did try thosegenl_familycallbacks first but I couldn't get them to work right away so I moved on. I'll have a closer look now to figure out what I did wrong. Thanks for the suggestion Jakub!
Quoted 4 lines
Hm, that's probably because there is no real multicast group here :(
genl_sk_priv_get()and co. may work better in that case. your suggestion ofNETLINK_URELEASEmay work too, tho, I think it's the most error prone
sock_priv_destroy works with genl_sk_priv_get().
But, I have to manually modify the generated netlink header file to satisfy CFI.
-void binder_nl_sock_priv_init(struct my_struct *priv); -void binder_nl_sock_priv_destroy(struct my_struct *priv); +void binder_nl_sock_priv_init(void *priv);
+void binder_nl_sock_priv_destroy(void *priv);
The reason is that these 2 callback functions are defined in include/net/genetlink.h as below
void (*sock_priv_init)(void *priv);
void (*sock_priv_destroy)(void *priv);
Otherwise, kernel panic when CFI is enabled.
CFI failure at genl_sk_priv_get+0x60/0x138 (target:binder_nl_sock_priv_init+0x0/0x34; expected type: 0x0ef81b7d)
Jakub, we probably need this patch. Please let me know if you have a better idea. Thanks!
tools/net/ynl/ynl-gen-c.py +0 -1
@@ -2352,8 +2352,8 @@ def print_kernel_family_struct_hdr(family, cw): cw.p(f"extern struct genl_family {family.c_name}_nl_family;") cw.nl() if 'sock-priv' in family.kernel_family: - cw.p(f'void
{family.c_name}_nl_sock_priv_init({family.kernel_family["sock-priv"]} *priv);')
- cw.p(f'void
{family.c_name}_nl_sock_priv_destroy({family.kernel_family["sock-priv"]} *priv);')
+ cw.p(f'void {family.c_name}_nl_sock_priv_init(void *priv);')
+ cw.p(f'void {family.c_name}_nl_sock_priv_destroy(void *priv);')
cw.nl()
Jakub Kicinski
maintainerQuoted 48 lines · On Mon, 13 Jan 2025 22:01:00 -0800 Li Li wrote:
Quoted 9 lines · Jakub Kicinski
Quoted 4 lines
Sorry, it was me that suggested
NETLINK_URELEASE. BTW, I did try thosegenl_familycallbacks first but I couldn't get them to work right away so I moved on. I'll have a closer look now to figure out what I did wrong. Thanks for the suggestion Jakub!Hm, that's probably because there is no real multicast group here :(
genl_sk_priv_get()and co. may work better in that case. your suggestion ofNETLINK_URELEASEmay work too, tho, I think it's the most error prone
sock_priv_destroyworks withgenl_sk_priv_get().But, I have to manually modify the generated netlink header file to satisfy CFI.
(quoted diff) +2
-2-void binder_nl_sock_priv_init(struct my_struct *priv); -void binder_nl_sock_priv_destroy(struct my_struct *priv); +void binder_nl_sock_priv_init(void *priv); +void binder_nl_sock_priv_destroy(void *priv);The reason is that these 2 callback functions are defined in
include/net/genetlink.has below
void (*sock_priv_init)(void *priv);
void (*sock_priv_destroy)(void *priv);Otherwise, kernel panic when CFI is enabled.
CFI failure at
genl_sk_priv_get+0x60/0x138(target:binder_nl_sock_priv_init+0x0/0x34; expected type:0x0ef81b7d)Jakub, we probably need this patch. Please let me know if you have a better idea. Thanks!
tools/net/ynl/ynl-gen-c.py +0
-2@@ -2352,8 +2352,8 @@ def print_kernel_family_struct_hdr(family, cw): cw.p(f"extern struct genl_family {family.c_name}_nl_family;") cw.nl() if 'sock-priv' in family.kernel_family: - cw.p(f'void {family.c_name}_nl_sock_priv_init({family.kernel_family["sock-priv"]} *priv);') - cw.p(f'void {family.c_name}_nl_sock_priv_destroy({family.kernel_family["sock-priv"]}*priv);')
tools/net/ynl/ynl-gen-c.py
Quoted 3 lines
tools/net/ynl/ynl-gen-c.py +2
-0+ cw.p(f'void {family.c_name}_nl_sock_priv_init(void *priv);') + cw.p(f'void {family.c_name}_nl_sock_priv_destroy(void *priv);') cw.nl()
Maybe we can codegen a little wrapper call. Can you try this with CFI?
---->8------------
tools/net/ynl/pyynl/ynl_gen_c.py +11 -3
@@ -2411,6 +2411,15 @@ _C_KW = { if not kernel_can_gen_family_struct(family): return + if 'sock-priv' in family.kernel_family: + # Generate "trampolines" to make CFI happy + cw.write_func("static void", f"__{family.c_name}_nl_sock_priv_init", + [f"{family.c_name}_nl_sock_priv_init(priv);"], ["void *priv"]) + cw.nl() + cw.write_func("static void", f"__{family.c_name}_nl_sock_priv_destroy", + [f"{family.c_name}_nl_sock_priv_destroy(priv);"], ["void *priv"]) + cw.nl() + cw.block_start(f"struct genl_family {family.ident_name}_nl_family __ro_after_init =") cw.p('.name\t\t= ' + family.fam_key + ',') cw.p('.version\t= ' + family.ver_key + ',')
@@ -2428,9 +2437,8 @@ _C_KW = { cw.p(f'.n_mcgrps\t= ARRAY_SIZE({family.c_name}_nl_mcgrps),') if 'sock-priv' in family.kernel_family: cw.p(f'.sock_priv_size\t= sizeof({family.kernel_family["sock-priv"]}),') - # Force cast here, actual helpers take pointer to the real type. - cw.p(f'.sock_priv_init\t= (void *){family.c_name}_nl_sock_priv_init,') - cw.p(f'.sock_priv_destroy = (void *){family.c_name}_nl_sock_priv_destroy,') + cw.p(f'.sock_priv_init\t= __{family.c_name}_nl_sock_priv_init,') + cw.p(f'.sock_priv_destroy = __{family.c_name}_nl_sock_priv_destroy,') cw.block_end(';')
Signature · 1 line
2.47.1