Skip to contentSkip to conversation

binder: report txn errors via generic netlink

PATCH Superseded Li Li 2 participants 9 messages Oct 31, 2024 – Nov 5, 2024

Subsystems (5)

v7 of 17 Superseded Latest is v17 →

lore
Share & tools
lore

Li Li

author
#0 lore raw

v7 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.

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

Jakub Kicinski (1):
tools: ynl-gen: allow uapi headers in sub-dirs

Li Li (1):
binder: report txn errors via generic netlink

 Documentation/admin-guide/binder_genl.rst    |  93 ++++++
 Documentation/admin-guide/index.rst          |   1 +
 Documentation/netlink/specs/binder_genl.yaml | 114 +++++++
 drivers/android/Kconfig                      |   1 +
 drivers/android/Makefile                     |   2 +-
 drivers/android/binder.c                     | 313 ++++++++++++++++++-
 drivers/android/binder_genl.c                |  38 +++
 drivers/android/binder_genl.h                |  18 ++
 drivers/android/binder_internal.h            |  22 ++
 drivers/android/binder_trace.h               |  35 +++
 drivers/android/binderfs.c                   |   4 +
 include/uapi/linux/android/binder.h          |  31 ++
 include/uapi/linux/android/binder_genl.h     |  56 ++++
 tools/net/ynl/ynl-gen-c.py                   |   1 +
 14 files changed, 721 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/admin-guide/binder_genl.rst
 create mode 100644 Documentation/netlink/specs/binder_genl.yaml
 create mode 100644 drivers/android/binder_genl.c
 create mode 100644 drivers/android/binder_genl.h
 create mode 100644 include/uapi/linux/android/binder_genl.h

base-commit: d30b56c8666d2543112152dd5d93d052eafd6bc2

Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
author
#1 lore raw

PATCH v7 1/2 tools: ynl-gen: allow uapi headers in sub-dirs

From: Jakub Kicinski <[email protected]>

Binder places its headers under include/uapi/linux/android/ Make sure replace / with _ in the uAPI header guard, the c_upper() is more strict and only converts - to _. This is likely a good constraint to have, to enforce sane naming in enums etc. But paths may include /.

---
 tools/net/ynl/ynl-gen-c.py | 1 +
 1 file changed, 1 insertion(+)
tools/net/ynl/ynl-gen-c.py +1 -0
index 1a825b4081b2..ab48114229ee 100755
@@ -2414,6 +2414,7 @@ def uapi_enum_start(family, cw, obj, ckey='', enum_name='enum-name'):
 
 def render_uapi(family, cw):
     hdr_prot = f"_UAPI_LINUX_{c_upper(family.uapi_header_name)}_H"
+    hdr_prot = hdr_prot.replace('/', '_')
     cw.p('#ifndef ' + hdr_prot)
     cw.p('#define ' + hdr_prot)
     cw.nl()
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
author
#2 lore raw

PATCH v7 2/2 binder: report txn errors via generic netlink

In reply to Li Li

From: Li Li <[email protected]>

Frozen tasks can't process binder transactions, so sync binder transactions will fail with BR_FROZEN_REPLY and async binder transactions will be queued in the kernel async binder buffer. As these queued async transactions accumulates over time, the async buffer will eventually be running out, denying all new transactions after that with BR_FAILED_REPLY.

In addition to the above cases, different kinds of binder error codes might be returned to the sender. However, the core Linux, or Android, system administration process never knows what's actually happening.

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 new binder genl sources and headers are automatically generated from the corresponding binder_genl YAML spec. Don't modify them directly.

---
 Documentation/admin-guide/binder_genl.rst    |  93 ++++++
 Documentation/admin-guide/index.rst          |   1 +
 Documentation/netlink/specs/binder_genl.yaml | 114 +++++++
 drivers/android/Kconfig                      |   1 +
 drivers/android/Makefile                     |   2 +-
 drivers/android/binder.c                     | 313 ++++++++++++++++++-
 drivers/android/binder_genl.c                |  38 +++
 drivers/android/binder_genl.h                |  18 ++
 drivers/android/binder_internal.h            |  22 ++
 drivers/android/binder_trace.h               |  35 +++
 drivers/android/binderfs.c                   |   4 +
 include/uapi/linux/android/binder.h          |  31 ++
 include/uapi/linux/android/binder_genl.h     |  56 ++++
 13 files changed, 720 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/admin-guide/binder_genl.rst
 create mode 100644 Documentation/netlink/specs/binder_genl.yaml
 create mode 100644 drivers/android/binder_genl.c
 create mode 100644 drivers/android/binder_genl.h
 create mode 100644 include/uapi/linux/android/binder_genl.h
Documentation/admin-guide/binder_genl.rst new file +93 -0
new file mode 100644
index 000000000000..163b91dca83e
@@ -0,0 +1,93 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+===========================================================
+Generic Netlink for the Android Binder Driver (Binder Genl)
+===========================================================
+
+The Generic Netlink subsystem in the Linux kernel provides a generic way for
+the Linux kernel to communicate to the user space applications via binder
+driver. It is used to report various kinds of binder transactions 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 "set" command to request what kind
+of binder transactions reported by the kernel binder driver. The driver then
+uses "reply" command to acknowledge the request. The "set" command also
+registers the current user space process to receive the reports. When the
+user space process exits, the previous request will be reset to prevent any
+potential leaks.
+
+Currently the driver can report binder transactions that "failed" to reach
+the target process, or that are "delayed" due to the target process being
+frozen by cgroup freezer, or that are considered "spam" according to existing
+logic in binder_alloc.c.
+
+When the specified binder transactions happen, the driver uses the "report"
+command to send a generic netlink message to the registered process,
+containing the payload struct binder_report.
+
+More details about the flags, attributes and operations can be found at the
+the doc sections in Documentations/netlink/specs/binder_genl.yaml and the
+kernel-doc comments of the new source code in binder.{h|c}.
+
+Using Binder Genl
+-----------------
+
+The Binder Genl 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 to prevent leaking memory.
+
+Usage example (user space pseudo code):
+
+::
+
+    // 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 genl
+    send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME, "binder");
+    void *data = recv(CTRL_CMD_NEWFAMILY);
+    __u16 id = nla(data)[CTRL_ATTR_FAMILY_ID];
+
+    // enable per-context binder report
+    send(fd, id, BINDER_GENL_CMD_SET, 0, BINDER_GENL_FLAG_FAILED |
+            BINDER_GENL_FLAG_DELAYED);
+
+    // confirm the per-context configuration
+    void *data = recv(fd, BINDER_GENL_CMD_REPLY);
+    __u32 pid =  nla(data)[BINDER_GENL_A_CMD_PID];
+    __u32 flags = nla(data)[BINDER_GENL_A_CMD_FLAGS];
+
+    // set optional per-process report, overriding the per-context one
+    send(fd, id, BINDER_GENL_CMD_SET, getpid(),
+            BINDER_GENL_FLAG_SPAM | BINDER_REPORT_OVERRIDE);
+
+    // confirm the optional per-process configuration
+    void *data = recv(fd, BINDER_GENL_CMD_REPLY);
+    __u32 pid =  nla(data)[BINDER_GENL_A_CMD_PID];
+    __u32 flags = nla(data)[BINDER_GENL_A_CMD_FLAGS];
+
+    // wait and read all binder reports
+    while (running) {
+            void *data = recv(fd, BINDER_GENL_CMD_REPORT);
+            u32 *attr = nla(data)[BINDER_GENL_A_REPORT_XXX];
+
+            // process binder report
+            do_something(*attr);
+    }
+
+    // clean up
+    send(fd, id, BINDER_GENL_CMD_SET, 0, 0);
+    send(fd, id, BINDER_GENL_CMD_SET, getpid(), 0);
+    close(fd);
Documentation/admin-guide/index.rst +1 -0
index e85b1adf5908..b3b5cfadffe5 100644
@@ -79,6 +79,7 @@ configure specific aspects of kernel behavior to your liking.
    aoe/index
    auxdisplay/index
    bcache
+   binder_genl
    binderfs
    binfmt-misc
    blockdev/index
Documentation/netlink/specs/binder_genl.yaml new file +114 -0

114 lines not on this page · raw message

drivers/android/Kconfig +1 -0
index 07aa8ae0a058..e2fa620934e2 100644
@@ -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
index c9d3d0c99c25..d818447fbc4c 100644
@@ -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_genl.o
 obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
drivers/android/binder.c +306 -7

373 lines not on this page · raw message

drivers/android/binder_genl.c new file +38 -0
new file mode 100644
index 000000000000..f100a6c49f38
@@ -0,0 +1,38 @@
+// 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_genl.yaml */
+/* YNL-GEN kernel source */
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include "binder_genl.h"
+
+#include <uapi/linux/android/binder_genl.h>
+
+/* BINDER_GENL_CMD_SET - do */
+static const struct nla_policy binder_genl_set_nl_policy[BINDER_GENL_A_CMD_FLAGS + 1] = {
+	[BINDER_GENL_A_CMD_PID] = { .type = NLA_U32, },
+	[BINDER_GENL_A_CMD_FLAGS] = { .type = NLA_U32, },
+};
+
+/* Ops table for binder_genl */
+static const struct genl_split_ops binder_genl_nl_ops[] = {
+	{
+		.cmd		= BINDER_GENL_CMD_SET,
+		.doit		= binder_genl_nl_set_doit,
+		.policy		= binder_genl_set_nl_policy,
+		.maxattr	= BINDER_GENL_A_CMD_FLAGS,
+		.flags		= GENL_CMD_CAP_DO,
+	},
+};
+
+struct genl_family binder_genl_nl_family __ro_after_init = {
+	.name		= BINDER_GENL_FAMILY_NAME,
+	.version	= BINDER_GENL_FAMILY_VERSION,
+	.netnsok	= true,
+	.parallel_ops	= true,
+	.module		= THIS_MODULE,
+	.split_ops	= binder_genl_nl_ops,
+	.n_split_ops	= ARRAY_SIZE(binder_genl_nl_ops),
+};
drivers/android/binder_genl.h new file +18 -0
new file mode 100644
index 000000000000..9d68c155b7c4
@@ -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_genl.yaml */
+/* YNL-GEN kernel header */
+
+#ifndef _LINUX_BINDER_GENL_GEN_H
+#define _LINUX_BINDER_GENL_GEN_H
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include <uapi/linux/android/binder_genl.h>
+
+int binder_genl_nl_set_doit(struct sk_buff *skb, struct genl_info *info);
+
+extern struct genl_family binder_genl_nl_family;
+
+#endif /* _LINUX_BINDER_GENL_GEN_H */
drivers/android/binder_internal.h +22 -0
index f8d6be682f23..4abc8e3940c3 100644
@@ -12,15 +12,32 @@
 #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
+ * @genl_family:             the generic netlink family
+ * @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;
+	struct genl_family genl_family;
+	u32 report_portid;
+	u32 report_flags;
+	atomic_t report_seq;
 };
 
 /**
@@ -415,6 +432,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_report_flag).
  *
  * Bookkeeping structure for binder processes
  */
@@ -453,6 +472,7 @@ struct binder_proc {
 	spinlock_t outer_lock;
 	struct dentry *binderfs_entry;
 	bool oneway_spam_detection_enabled;
+	u32 report_flags;
 };
 
 /**
@@ -582,4 +602,6 @@ struct binder_object {
 	};
 };
 
+int binder_genl_init(struct genl_family *family, const char *name);
+
 #endif /* _LINUX_BINDER_INTERNAL_H */
drivers/android/binder_trace.h +35 -0
index fe38c6fc65d0..551b6c9d9f73 100644
@@ -423,6 +423,41 @@ TRACE_EVENT(binder_return,
 			  "unknown")
 );
 
+TRACE_EVENT(binder_send_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
drivers/android/binderfs.c +4 -0
index ad1fa7abc323..b2c5b04bf2ab 100644
@@ -207,6 +207,10 @@ static int binderfs_binder_device_create(struct inode *ref_inode,
 	fsnotify_create(root->d_inode, dentry);
 	inode_unlock(d_inode(root));
 
+	ret = binder_genl_init(&device->context.genl_family, name);
+	if (ret < 0)
+		goto err;
+
 	return 0;
 
 err:
include/uapi/linux/android/binder.h +31 -0
index 1fd92021a573..d95776448db9 100644
@@ -588,5 +588,36 @@ enum binder_driver_command_protocol {
 	 */
 };
 
+/*
+ * struct binder_report - reports an abnormal binder transaction
+ * @err:	copy of binder_driver_return_protocol returned to the sender
+ * @from_pid:	sender pid of the corresponding binder transaction
+ * @from_tid:	sender tid of the corresponding binder transaction
+ * @to_pid:	target pid of the corresponding binder transaction
+ * @to_tid:	target tid of the corresponding binder transaction
+ * @reply:	1 means the txn is a reply, 0 otherwise
+ * @flags:	copy of binder_transaction_data->flags
+ * @code:	copy of binder_transaction_data->code
+ * @data_size:	copy of binder_transaction_data->data_size
+ *
+ * When a binder transaction fails to reach the target process or is not
+ * delivered on time, an error command BR_XXX is returned from the kernel
+ * binder driver to the user space sender. This is considered an abnormal
+ * binder transaction. The most important information about this abnormal
+ * binder transaction will be packed into this binder_report struct and sent
+ * to the registered user space administration process via generic netlink.
+ */
+struct binder_report {
+	__u32 err;
+	__u32 from_pid;
+	__u32 from_tid;
+	__u32 to_pid;
+	__u32 to_tid;
+	__u32 reply;
+	__u32 flags;
+	__u32 code;
+	binder_size_t data_size;
+};
+
 #endif /* _UAPI_LINUX_BINDER_H */
 
include/uapi/linux/android/binder_genl.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
maintainer
#3 lore raw
Quoted 5 lines · On Thu, 31 Oct 2024 02:25:04 -0700 Li Li wrote:

+===========================================================

Documentation/admin-guide/binder_genl.rst new file +4 -0
+Generic Netlink for the Android Binder Driver (Binder Genl)
+===========================================================
+
+The Generic Netlink subsystem in the Linux kernel provides a generic way for

Documentation/admin-guide/binder_genl.rst line 17

Quoted 10 lines
Documentation/admin-guide/binder_genl.rst new file +10 -0
+the Linux kernel to communicate to the user space applications via binder
+driver. It is used to report various kinds of binder transactions 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 "set" command to request what kind
+of binder transactions reported by the kernel binder driver. The driver then

Something is missing here. s/reported/should be reported/ ?

Quoted 98 lines
Documentation/admin-guide/binder_genl.rst new file +76 -0
+uses "reply" command to acknowledge the request. The "set" command also
+registers the current user space process to receive the reports. When the
+user space process exits, the previous request will be reset to prevent any
+potential leaks.
+
+Currently the driver can report binder transactions that "failed" to reach
+the target process, or that are "delayed" due to the target process being
+frozen by cgroup freezer, or that are considered "spam" according to existing
+logic in binder_alloc.c.
+
+When the specified binder transactions happen, the driver uses the "report"
+command to send a generic netlink message to the registered process,
+containing the payload struct binder_report.
+
+More details about the flags, attributes and operations can be found at the
+the doc sections in Documentations/netlink/specs/binder_genl.yaml and the
+kernel-doc comments of the new source code in binder.{h|c}.
+
+Using Binder Genl
+-----------------
+
+The Binder Genl 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 to prevent leaking memory.
+
+Usage example (user space pseudo code):
+
+::
+
+    // 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 genl
+    send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME, "binder");
+    void *data = recv(CTRL_CMD_NEWFAMILY);
+    __u16 id = nla(data)[CTRL_ATTR_FAMILY_ID];
+
+    // enable per-context binder report
+    send(fd, id, BINDER_GENL_CMD_SET, 0, BINDER_GENL_FLAG_FAILED |
+            BINDER_GENL_FLAG_DELAYED);
+
+    // confirm the per-context configuration
+    void *data = recv(fd, BINDER_GENL_CMD_REPLY);
+    __u32 pid =  nla(data)[BINDER_GENL_A_CMD_PID];
+    __u32 flags = nla(data)[BINDER_GENL_A_CMD_FLAGS];
+
+    // set optional per-process report, overriding the per-context one
+    send(fd, id, BINDER_GENL_CMD_SET, getpid(),
+            BINDER_GENL_FLAG_SPAM | BINDER_REPORT_OVERRIDE);
+
+    // confirm the optional per-process configuration
+    void *data = recv(fd, BINDER_GENL_CMD_REPLY);
+    __u32 pid =  nla(data)[BINDER_GENL_A_CMD_PID];
+    __u32 flags = nla(data)[BINDER_GENL_A_CMD_FLAGS];
+
+    // wait and read all binder reports
+    while (running) {
+            void *data = recv(fd, BINDER_GENL_CMD_REPORT);
+            u32 *attr = nla(data)[BINDER_GENL_A_REPORT_XXX];
+
+            // process binder report
+            do_something(*attr);
+    }
+
+    // clean up
+    send(fd, id, BINDER_GENL_CMD_SET, 0, 0);
+    send(fd, id, BINDER_GENL_CMD_SET, getpid(), 0);
+    close(fd);
Documentation/admin-guide/index.rst +1 -0
index e85b1adf5908..b3b5cfadffe5 100644
@@ -79,6 +79,7 @@ configure specific aspects of kernel behavior to your liking.
    aoe/index
    auxdisplay/index
    bcache
+   binder_genl
    binderfs
    binfmt-misc
    blockdev/index
Documentation/netlink/specs/binder_genl.yaml new file +4 -0
new file mode 100644
index 000000000000..35e5f0120fc7
@@ -0,0 +1,114 @@
+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+
+name: binder_genl
+protocol: genetlink

Documentation/netlink/specs/binder_genl.yaml line 14

Quoted 10 lines
Documentation/netlink/specs/binder_genl.yaml new file +10 -0
+uapi-header: linux/android/binder_genl.h
+doc: Netlink protocol to report binder transaction errors and warnings.
+
+definitions:
+  -
+    type: flags
+    name: flag
+    doc: |
+      Used with "set" and "reply" command below, defining what kind of binder
+      transactions reported to the user space administration process.

Please the references to which commands use given enum and attr. The YAML specs are automatically rendered as HTML:
https://docs.kernel.org/next/networking/netlink_spec/net_shaper.html
so hopefully it's clear which attrs are used where.

Documentation/netlink/specs/binder_genl.yaml line 15

Quoted 1 line
Documentation/netlink/specs/binder_genl.yaml new file +1 -0
+    value-start: 0

I thought flags default to starting from 0 (or rather (1<<0)) Please delete this if it's not needed.

Quoted 86 lines
Documentation/netlink/specs/binder_genl.yaml new file +86 -0
+    entries: [ failed, delayed, spam, override ]
+
+attribute-sets:
+  -
+    name: cmd
+    doc: The supported attributes of "set" and "reply" commands
+    attributes:
+      -
+        name: pid
+        type: u32
+        doc: |
+          What binder proc or context to enable binder genl report,
+          used by "set" and "reply" command below.
+      -
+        name: flags
+        type: u32
+        doc: |
+          What kind of binder transactions reported via binder genl,
+          used by "set" and "reply" command below.
+  -
+    name: report
+    doc: The supported attributes of "report" command
+    attributes:
+      -
+        name: err
+        type: u32
+        doc: |
+          Copy of binder_driver_return_protocol returned to the sender,
+          used by "report" command below.
+      -
+        name: from_pid
+        type: u32
+        doc: |
+          Sender pid of the corresponding binder transaction
+          used by "report" command below.
+      -
+        name: from_tid
+        type: u32
+        doc: |
+          Sender tid of the corresponding binder transaction
+          used by "report" command below.
+      -
+        name: to_pid
+        type: u32
+        doc: |
+          Target pid of the corresponding binder transaction
+          used by "report" command below.
+      -
+        name: to_tid
+        type: u32
+        doc: |
+          Target tid of the corresponding binder transaction
+          used by "report" command below.
+      -
+        name: reply
+        type: u32
+        doc: |
+          1 means the transaction is a reply, 0 otherwise
+          used by "report" command below.
+      -
+        name: flags
+        type: u32
+        doc: |
+          Copy of binder_transaction_data->flags
+          used by "report" command below.
+      -
+        name: code
+        type: u32
+        doc: |
+          Copy of binder_transaction_data->code
+          used by "report" command below.
+      -
+        name: data_size
+        type: u32
+        doc: |
+          Copy of binder_transaction_data->data_size
+          used by "report" command below.
+
+operations:
+  list:
+    -
+      name: set
+      doc: |
+        Set flags from user space, requesting what kinds of binder
+        transactions to report.
+      attribute-set: cmd

Documentation/netlink/specs/binder_genl.yaml line 111

Quoted 10 lines
Documentation/netlink/specs/binder_genl.yaml new file +10 -0
+
+      do:
+        request: &params
+          attributes:
+            - pid
+            - flags
+        reply: *params
+    -
+      name: reply
+      doc: Acknowledge the above "set" request, echoing the same params.

Hm, this looks strange. We shouldn't need a separate op entry for reply. Operations are request + response.

Documentation/netlink/specs/binder_genl.yaml line 114

Quoted 3 lines
Documentation/netlink/specs/binder_genl.yaml new file +3 -0
+    -
+      name: report
+      doc: Send the requested binder transaction reports to user space.

This is probably an event and contain the list of fields carried:
https://docs.kernel.org/next/userspace-api/netlink/specs.html
The list of fields is needed for user space C and C++ code gen.

Quoted 8 lines
Documentation/netlink/specs/binder_genl.yaml new file +8 -0
+/**
+ * binder_genl_set_report() - set binder report flags
+ * @context:	the binder context to set the flags
+ * @pid:	the target process
+ * @flags:	the flags to set
+ *
+ * If pid is 0, the flags are applied to the whole binder context.
+ * Otherwise, the flags are applied to the specific process only.

Documentation/netlink/specs/binder_genl.yaml

Quoted 10 lines
Documentation/netlink/specs/binder_genl.yaml new file +10 -0
+ */
+static int binder_genl_set_report(struct binder_context *context, u32 pid,
+				  u32 flags)
+{
+	struct binder_proc *proc;
+
+	if (flags != (flags & (BINDER_GENL_FLAG_OVERRIDE
+			| BINDER_GENL_FLAG_FAILED
+			| BINDER_GENL_FLAG_DELAYED
+			| BINDER_GENL_FLAG_SPAM))) {

please add

enum: flag

to the definition of the flags field in YAML.
Netlink will auto-generate a policy checking that only values defined in the enum are allowed.

Quoted 33 lines
Documentation/netlink/specs/binder_genl.yaml new file +28 -0
+		pr_err("Invalid binder report flags: %u\n", flags);
+		return -EINVAL;
+	}
+
+	if (!pid) {
+		/* Set the global flags for the whole binder context */
+		context->report_flags = flags;
+	} else {
+		/* Set the per-process flags */
+		proc = binder_find_proc(pid);
+		if (!proc) {
+			pr_err("Invalid binder report pid %u\n", pid);
+			return -EINVAL;
+		}
+
+		proc->report_flags = flags;
+	}
+
+	return 0;
+}
@@ -6311,6 +6500,83 @@ binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
 	mutex_unlock(&binder_deferred_lock);
 }
 
+/**
+ * binder_genl_nl_set_doit() - .doit handler for BINDER_GENL_CMD_SET
+ * @skb:	the metadata struct passed from netlink driver
+ * @info:	the generic netlink struct passed from netlink driver
+ *
+ * Implements the .doit function to process binder genl commands.
+ */
+int binder_genl_nl_set_doit(struct sk_buff *skb, struct genl_info *info)

Documentation/netlink/specs/binder_genl.yaml binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6520

Quoted 10 lines
Documentation/netlink/specs/binder_genl.yaml new file +10 -0
+{
+	int payload;
+	int portid;
+	u32 pid;
+	u32 flags;
+	void *hdr;
+	struct binder_context *context;
+
+	/* Both attributes are required for BINDER_GENL_CMD_SET */
+	if (!info->attrs[BINDER_GENL_A_CMD_PID] || !info->attrs[BINDER_GENL_A_CMD_FLAGS]) {

Use GENL_REQ_ATTR_CHECK, it will set metadata to let user space know which attrs are missing

Documentation/netlink/specs/binder_genl.yaml binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6521

Quoted 1 line
Documentation/netlink/specs/binder_genl.yaml new file +1 -0
+		pr_err("Attributes not set\n");

and then you can delete this

Documentation/netlink/specs/binder_genl.yaml binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6532

Quoted 11 lines
Documentation/netlink/specs/binder_genl.yaml new file +11 -0
+		return -EINVAL;
+	}
+
+	portid = nlmsg_hdr(skb)->nlmsg_pid;
+	pid = nla_get_u32(info->attrs[BINDER_GENL_A_CMD_PID]);
+	flags = nla_get_u32(info->attrs[BINDER_GENL_A_CMD_FLAGS]);
+	context = container_of(info->family, struct binder_context,
+			       genl_family);
+
+	if (context->report_portid && context->report_portid != portid) {
+		pr_err("No permission to set report flags from %u\n", portid);

It's better to communicate the errors to application using extack (inside the netlink reply) using NL_SET_ERR_MSG(info->extack, "yourmsg")

Quoted 7 lines
Documentation/netlink/specs/binder_genl.yaml new file +7 -0
+		return -EPERM;
+	}
+
+	if (binder_genl_set_report(context, pid, flags) < 0) {
+		pr_err("Failed to set report flags %u for %u\n", flags, pid);
+		return -EINVAL;
+	}

Documentation/netlink/specs/binder_genl.yaml binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6549

Quoted 10 lines
Documentation/netlink/specs/binder_genl.yaml new file +10 -0
+
+	payload = nla_total_size(sizeof(pid)) + nla_total_size(sizeof(flags));
+	skb = genlmsg_new(payload + GENL_HDRLEN, GFP_KERNEL);
+	if (!skb) {
+		pr_err("Failed to alloc binder genl reply message\n");
+		return -ENOMEM;
+	}
+
+	hdr = genlmsg_put_reply(skb, info, info->family, 0,
+				BINDER_GENL_CMD_REPLY);

Use the same ID as the request, the BINDER_GENL_CMD_REPLY shouldn't exist. And then you can use genlmsg_iput().

Documentation/netlink/specs/binder_genl.yaml binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) · line 6556

Quoted 7 lines
Documentation/netlink/specs/binder_genl.yaml new file +7 -0
+	if (!hdr)
+		goto free_skb;
+
+	if (nla_put_u32(skb, BINDER_GENL_A_CMD_PID, pid))
+		goto cancel_skb;
+
+	if (nla_put_u32(skb, BINDER_GENL_A_CMD_FLAGS, flags))

it's typical to chain all the nla_puts..

	if (nla_put_u32(skb, BINDER_GENL_A_CMD_PID, pid) ||
	    nla_put_u32(skb, BINDER_GENL_A_CMD_FLAGS, flags))

to avoid repeating the goto's for every field.

Quoted 33 lines
Documentation/netlink/specs/binder_genl.yaml new file +26 -0
+		goto cancel_skb;
+
+	genlmsg_end(skb, hdr);
+
+	if (genlmsg_reply(skb, info)) {
+		pr_err("Failed to send binder genl reply message\n");
+		return -EFAULT;
+	}
+
+	if (!context->report_portid)
+		context->report_portid = portid;
+
+	return 0;
+
+cancel_skb:
+	pr_err("Failed to add reply attributes to binder genl message\n");
+	genlmsg_cancel(skb, hdr);
+free_skb:
+	pr_err("Free binder genl reply message on error\n");
+	nlmsg_free(skb);
+	return -EMSGSIZE;
+}
+
 static void print_binder_transaction_ilocked(struct seq_file *m,
 					     struct binder_proc *proc,
 					     const char *prefix,
@@ -6894,6 +7160,28 @@ const struct binder_debugfs_entry binder_debugfs_entries[] = {
 	{} /* terminator */
 };
 
+/**
+ * binder_genl_init() - initialize binder generic netlink
+ * @family:	the generic netlink family

Documentation/netlink/specs/binder_genl.yaml const struct binder_debugfs_entry binder_debugfs_entries[] = { · line 7175

Quoted 10 lines
Documentation/netlink/specs/binder_genl.yaml new file +10 -0
+ * @name:	the binder device name
+ *
+ * Registers the binder generic netlink family.
+ */
+int binder_genl_init(struct genl_family *family, const char *name)
+{
+	int ret;
+
+	memcpy(family, &binder_genl_nl_family, sizeof(*family));
+	strscpy(family->name, name, GENL_NAMSIZ);

You're trying to register multiple families with different names? The family defines the language / protocol. If you have multiple entities to multiplex you should do that based on attributes inside the messages.

Quoted 8 lines
Documentation/netlink/specs/binder_genl.yaml new file +8 -0
+	ret = genl_register_family(family);
+	if (ret) {
+		pr_err("Failed to register binder genl: %s\n", name);
+		return ret;
+	}
+
+	return 0;
+}
author
#4 lore raw
Quoted 10 lines
Quoted 10 lines
drivers/android/binder.c +10 -0
+/**
+ * binder_genl_init() - initialize binder generic netlink
+ * @family:  the generic netlink family
+ * @name:    the binder device name
+ *
+ * Registers the binder generic netlink family.
+ */
+int binder_genl_init(struct genl_family *family, const char *name)
+{
+     int ret;
Quoted 9 lines

drivers/android/binder.c const struct binder_debugfs_entry binder_debugfs_entries[] = { · line 7175

Quoted 3 lines
drivers/android/binder.c +3 -0
+
+     memcpy(family, &binder_genl_nl_family, sizeof(*family));
+     strscpy(family->name, name, GENL_NAMSIZ);

You're trying to register multiple families with different names? The family defines the language / protocol. If you have multiple entities to multiplex you should do that based on attributes inside the messages.

My initial plan was to use a single "binder" family, which was more straightforward and cleaner. As Android uses multiple binder contexts to isolate system framework and vendor domains[1], Grek KH suggested the netlink messages from different binder contexts should also be isolated for security reason[2]. Personally I'm fine with either approach. Please kindly advice which implementation is better.

And I'll fix other issues you mentioned above.

[1]
https://source.android.com/docs/core/architecture/hidl/binder-ipc

[2]
https://lore.kernel.org/lkml/2024081350-establish-direness-38ee@gregkh/

Quoted 9 lines
Quoted 8 lines
drivers/android/binder.c +8 -0
+     ret = genl_register_family(family);
+     if (ret) {
+             pr_err("Failed to register binder genl: %s\n", name);
+             return ret;
+     }
+
+     return 0;
+}
maintainer
#5 lore raw
Quoted 5 lines · On Sun, 3 Nov 2024 22:25:44 -0800 Li Li wrote:
Quoted 4 lines

You're trying to register multiple families with different names? The family defines the language / protocol. If you have multiple entities to multiplex you should do that based on attributes inside the messages.

Quoted 8 lines

My initial plan was to use a single "binder" family, which was more straightforward and cleaner. As Android uses multiple binder contexts to isolate system framework and vendor domains[1], Grek KH suggested the netlink messages from different binder contexts should also be isolated for security reason[2]. Personally I'm fine with either approach. Please kindly advice which implementation is better.

And I'll fix other issues you mentioned above.

Greg is obviously right, but using different family names will not help you in any way. There is no action of "opening" a socket for a generic netlink family, one generic netlink socket can talk to all families. The only built in checking netlink provides is that you can declare an operation as requiring admin privileges, or network capability (namespaced or global).

Unless those are good enough for you - I think you should do all the security isolation within your code, manually.

author
#6 lore raw
Quoted 23 lines · Jakub Kicinski
Quoted 5 lines · On Sun, 3 Nov 2024 22:25:44 -0800 Li Li wrote:
Quoted 4 lines

You're trying to register multiple families with different names? The family defines the language / protocol. If you have multiple entities to multiplex you should do that based on attributes inside the messages.

Quoted 8 lines

My initial plan was to use a single "binder" family, which was more straightforward and cleaner. As Android uses multiple binder contexts to isolate system framework and vendor domains[1], Grek KH suggested the netlink messages from different binder contexts should also be isolated for security reason[2]. Personally I'm fine with either approach. Please kindly advice which implementation is better.

And I'll fix other issues you mentioned above.

Greg is obviously right, but using different family names will not help you in any way. There is no action of "opening" a socket for a generic netlink family, one generic netlink socket can talk to all families. The only built in checking netlink provides is that you can declare an operation as requiring admin privileges, or network capability (namespaced or global).

Quoted 2 lines

Unless those are good enough for you - I think you should do all the security isolation within your code, manually.

That's why binder genl uses unicast instead of multicast. The administration process of the OS (Android in this case) always runs before any other user applications, which registers itself to the kernel binder driver and uses it exclusively. With a unified family name, the same userspace admin process has access to all binder contexts. With separate family names, each domain admin process can register itself to the corresponding binder context.

So, do you think the current implementation of registering multiple families with different names acceptable? Or is there a better way to do it? Thank you very much!

maintainer
#7 lore raw
Quoted 6 lines · On Mon, 4 Nov 2024 09:12:37 -0800 Li Li wrote:

That's why binder genl uses unicast instead of multicast. The administration process of the OS (Android in this case) always runs before any other user applications, which registers itself to the kernel binder driver and uses it exclusively. With a unified family name, the same userspace admin process has access to all binder contexts. With separate family names, each domain admin process can register itself to the corresponding binder context.

Side note - it'd be useful for my understanding to know what the binder families would be. You register them in binderfs_binder_device_create(), what creates the devices and how many do we expect?

Back to answering - I don't know why the same process would have to bind to all domains. You record which portid is bound to the context, the portid identifies the socket.

BTW portids can get reused, do you need to do something when the admin process dies? To prevent normal user from binding to the same portid?

Quoted 3 lines

So, do you think the current implementation of registering multiple families with different names acceptable? Or is there a better way to do it? Thank you very much!

I don't see what having the separate families buys you. The genl family ID is added to the netlink message in nlmsg_type. That's it, it doesn't do anything else.
You can add an attribute to the message to carry context->name and you'll be able to demux the messages.

author
#8 lore raw
Quoted 9 lines · Jakub Kicinski
Quoted 6 lines · On Mon, 4 Nov 2024 09:12:37 -0800 Li Li wrote:

That's why binder genl uses unicast instead of multicast. The administration process of the OS (Android in this case) always runs before any other user applications, which registers itself to the kernel binder driver and uses it exclusively. With a unified family name, the same userspace admin process has access to all binder contexts. With separate family names, each domain admin process can register itself to the corresponding binder context.

Quoted 3 lines

Side note - it'd be useful for my understanding to know what the binder families would be. You register them in binderfs_binder_device_create(), what creates the devices and how many do we expect?

There are 2 ways to create a new binder device.
1. Provide a list of devices (as strings) in boot cmd;
2. Call an API provided by binderfs in the system init process.

There are 3 domains as mentioned at
https://source.android.com/docs/core/architecture/hidl/binder-ipc#ipc

Quoted 7 lines

Back to answering - I don't know why the same process would have to bind to all domains. You record which portid is bound to the context, the portid identifies the socket.

BTW portids can get reused, do you need to do something when the admin process dies? To prevent normal user from binding to the same portid?

When the admin process dies, the kernel driver automatically releases the binder fd and resets the netlink. So a normal user won't be able to bind to the same portid. The admin process will respawn itself and re-initialize everything.

Quoted 10 lines
Quoted 4 lines

So, do you think the current implementation of registering multiple families with different names acceptable? Or is there a better way to do it? Thank you very much!

I don't see what having the separate families buys you. The genl family ID is added to the netlink message in nlmsg_type. That's it, it doesn't do anything else.
You can add an attribute to the message to carry context->name and you'll be able to demux the messages.

You're right. Even though my userspace code uses different sockets to talk to different binder genl families, the generic netlink socket doesn't enforce this. Other apps can still use one socket to talk to different families.

I'll add a new attribute to demux the message, which should be able to achieve the same functionality.