:p
atchew
Login
From: Li Li <dualli@google.com> 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/20240812211844.4107494-1-dualli@chromium.org/ 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/20241011064427.1565287-1-dualli@chromium.org/ 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/20241021182821.1259487-1-dualli@chromium.org/ 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/20241021191233.1334897-1-dualli@chromium.org/ 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/20241025075102.1785960-1-dualli@chromium.org/ The 6th version has some trivial kernel doc fixes, without modifying any other source code. https://lore.kernel.org/lkml/20241028101952.775731-1-dualli@chromium.org/ 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 -- 2.47.0.163.g1226f6d8fa-goog
From: Jakub Kicinski <kuba@kernel.org> 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 /. Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Li Li <dualli@google.com> --- tools/net/ynl/ynl-gen-c.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py index XXXXXXX..XXXXXXX 100755 --- a/tools/net/ynl/ynl-gen-c.py +++ b/tools/net/ynl/ynl-gen-c.py @@ -XXX,XX +XXX,XX @@ 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() -- 2.47.0.163.g1226f6d8fa-goog
From: Li Li <dualli@google.com> 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. Signed-off-by: Li Li <dualli@google.com> --- 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 diff --git a/Documentation/admin-guide/binder_genl.rst b/Documentation/admin-guide/binder_genl.rst new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/Documentation/admin-guide/binder_genl.rst @@ -XXX,XX +XXX,XX @@ +.. 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); diff --git a/Documentation/admin-guide/index.rst b/Documentation/admin-guide/index.rst index XXXXXXX..XXXXXXX 100644 --- a/Documentation/admin-guide/index.rst +++ b/Documentation/admin-guide/index.rst @@ -XXX,XX +XXX,XX @@ configure specific aspects of kernel behavior to your liking. aoe/index auxdisplay/index bcache + binder_genl binderfs binfmt-misc blockdev/index diff --git a/Documentation/netlink/specs/binder_genl.yaml b/Documentation/netlink/specs/binder_genl.yaml new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/Documentation/netlink/specs/binder_genl.yaml @@ -XXX,XX +XXX,XX @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) + +name: binder_genl +protocol: genetlink +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. + value-start: 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 + + do: + request: ¶ms + attributes: + - pid + - flags + reply: *params + - + name: reply + doc: Acknowledge the above "set" request, echoing the same params. + - + name: report + doc: Send the requested binder transaction reports to user space. diff --git a/drivers/android/Kconfig b/drivers/android/Kconfig index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/Kconfig +++ b/drivers/android/Kconfig @@ -XXX,XX +XXX,XX @@ 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, diff --git a/drivers/android/Makefile b/drivers/android/Makefile index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/Makefile +++ b/drivers/android/Makefile @@ -XXX,XX +XXX,XX @@ 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 diff --git a/drivers/android/binder.c b/drivers/android/binder.c index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -XXX,XX +XXX,XX @@ #include <linux/cacheflush.h> +#include "binder_genl.h" #include "binder_internal.h" #include "binder_trace.h" @@ -XXX,XX +XXX,XX @@ static void binder_set_txn_from_error(struct binder_transaction *t, int id, binder_thread_dec_tmpref(from); } +/** + * binder_find_proc() - set binder report flags + * @pid: the target process + */ +static struct binder_proc *binder_find_proc(int pid) +{ + struct binder_proc *proc; + + mutex_lock(&binder_procs_lock); + hlist_for_each_entry(proc, &binder_procs, proc_node) { + if (proc->pid == pid) { + mutex_unlock(&binder_procs_lock); + return proc; + } + } + mutex_unlock(&binder_procs_lock); + + return NULL; +} + +/** + * 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. + */ +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))) { + 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; +} + +/** + * binder_genl_report_enabled() - check if binder genl reports are enabled + * @proc: the binder_proc to check + * @mask: the categories of binder genl reports + * + * Returns true if certain binder genl reports are enabled for this binder + * proc (when per-process overriding takes effect) or context. + */ +static bool binder_genl_report_enabled(struct binder_proc *proc, u32 mask) +{ + struct binder_context *context = proc->context; + + if (!context->report_portid) + return false; + + if (proc->report_flags & BINDER_GENL_FLAG_OVERRIDE) + return (proc->report_flags & mask) != 0; + else + return (context->report_flags & mask) != 0; +} + +/** + * binder_genl_send_report() - send one binder genl report + * @context: the binder context + * @err: copy of binder_driver_return_protocol returned to the sender + * @pid: sender process + * @tid: sender thread + * @to_pid: target process + * @to_tid: target thread + * @reply: whether the binder transaction is a reply + * @tr: the binder transaction data + * + * Packs the report data into a "report" binder genl message and send it. + */ +static void binder_genl_send_report(struct binder_context *context, u32 err, + u32 pid, u32 tid, u32 to_pid, u32 to_tid, + u32 reply, + struct binder_transaction_data *tr) +{ + int payload; + int ret; + struct sk_buff *skb; + void *hdr; + + trace_binder_send_report(context->name, err, pid, tid, to_pid, to_tid, + reply, tr); + + payload = nla_total_size(sizeof(u32)) * BINDER_GENL_A_REPORT_MAX; + skb = genlmsg_new(payload + GENL_HDRLEN, GFP_KERNEL); + if (!skb) { + pr_err("Failed to alloc binder genl message\n"); + return; + } + + hdr = genlmsg_put(skb, 0, atomic_inc_return(&context->report_seq), + &context->genl_family, 0, BINDER_GENL_CMD_REPORT); + if (!hdr) + goto free_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_ERR, err)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_FROM_PID, pid)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_FROM_TID, tid)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_TO_PID, to_pid)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_TO_TID, to_tid)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_REPLY, reply)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_FLAGS, tr->flags)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_CODE, tr->code)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_GENL_A_REPORT_DATA_SIZE, tr->data_size)) + goto cancel_skb; + + genlmsg_end(skb, hdr); + + ret = genlmsg_unicast(&init_net, skb, context->report_portid); + if (ret < 0) + pr_err("Failed to send binder genl message to %d: %d\n", + context->report_portid, ret); + return; + +cancel_skb: + pr_err("Failed to add report attributes to binder genl message\n"); + genlmsg_cancel(skb, hdr); +free_skb: + pr_err("Free binder genl report message on error\n"); + nlmsg_free(skb); +} + static void binder_transaction(struct binder_proc *proc, struct binder_thread *thread, struct binder_transaction_data *tr, int reply, @@ -XXX,XX +XXX,XX @@ static void binder_transaction(struct binder_proc *proc, return_error_line = __LINE__; goto err_copy_data_failed; } - if (t->buffer->oneway_spam_suspect) + if (t->buffer->oneway_spam_suspect) { tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT; - else + if (binder_genl_report_enabled(proc, BINDER_GENL_FLAG_SPAM)) + binder_genl_send_report(context, + BR_ONEWAY_SPAM_SUSPECT, + proc->pid, thread->pid, + target_proc ? target_proc->pid : 0, + target_thread ? target_thread->pid : 0, + reply, tr); + } else { tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; + } t->work.type = BINDER_WORK_TRANSACTION; if (reply) { @@ -XXX,XX +XXX,XX @@ static void binder_transaction(struct binder_proc *proc, * process and is put in a pending queue, waiting for the target * process to be unfrozen. */ - if (return_error == BR_TRANSACTION_PENDING_FROZEN) + if (return_error == BR_TRANSACTION_PENDING_FROZEN) { tcomplete->type = BINDER_WORK_TRANSACTION_PENDING; + if (binder_genl_report_enabled(proc, BINDER_GENL_FLAG_DELAYED)) + binder_genl_send_report(context, return_error, + proc->pid, thread->pid, + target_proc ? target_proc->pid : 0, + target_thread ? target_thread->pid : 0, + reply, tr); + } binder_enqueue_thread_work(thread, tcomplete); if (return_error && return_error != BR_TRANSACTION_PENDING_FROZEN) @@ -XXX,XX +XXX,XX @@ static void binder_transaction(struct binder_proc *proc, binder_dec_node_tmpref(target_node); } + if (binder_genl_report_enabled(proc, BINDER_GENL_FLAG_FAILED)) + binder_genl_send_report(context, return_error, + proc->pid, thread->pid, + target_proc ? target_proc->pid : 0, + target_thread ? target_thread->pid : 0, + reply, tr); + binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, "%d:%d transaction %s to %d:%d failed %d/%d/%d, size %lld-%lld line %d\n", proc->pid, thread->pid, reply ? "reply" : @@ -XXX,XX +XXX,XX @@ 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; + } + return 0; } @@ -XXX,XX +XXX,XX @@ 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) +{ + 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]) { + pr_err("Attributes not set\n"); + 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); + 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; + } + + 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); + 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)) + 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, @@ -XXX,XX +XXX,XX @@ const struct binder_debugfs_entry binder_debugfs_entries[] = { {} /* terminator */ }; +/** + * 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; + + memcpy(family, &binder_genl_nl_family, sizeof(*family)); + strscpy(family->name, name, GENL_NAMSIZ); + ret = genl_register_family(family); + if (ret) { + pr_err("Failed to register binder genl: %s\n", name); + return ret; + } + + return 0; +} + static int __init init_binder_device(const char *name) { int ret; @@ -XXX,XX +XXX,XX @@ static int __init init_binder_device(const char *name) mutex_init(&binder_device->context.context_mgr_node_lock); ret = misc_register(&binder_device->miscdev); - if (ret < 0) { - kfree(binder_device); - return ret; - } + if (ret < 0) + goto err_free_dev; + + binder_device->context.report_seq = (atomic_t)ATOMIC_INIT(0); + ret = binder_genl_init(&binder_device->context.genl_family, name); + if (ret < 0) + goto err_misc_deregister; hlist_add_head(&binder_device->hlist, &binder_devices); + return ret; + +err_misc_deregister: + misc_deregister(&binder_device->miscdev); + +err_free_dev: + kfree(binder_device); + return ret; } diff --git a/drivers/android/binder_genl.c b/drivers/android/binder_genl.c new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/drivers/android/binder_genl.c @@ -XXX,XX +XXX,XX @@ +// 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), +}; diff --git a/drivers/android/binder_genl.h b/drivers/android/binder_genl.h new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/drivers/android/binder_genl.h @@ -XXX,XX +XXX,XX @@ +/* 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 */ diff --git a/drivers/android/binder_internal.h b/drivers/android/binder_internal.h index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binder_internal.h +++ b/drivers/android/binder_internal.h @@ -XXX,XX +XXX,XX @@ #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; }; /** @@ -XXX,XX +XXX,XX @@ 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 */ @@ -XXX,XX +XXX,XX @@ struct binder_proc { spinlock_t outer_lock; struct dentry *binderfs_entry; bool oneway_spam_detection_enabled; + u32 report_flags; }; /** @@ -XXX,XX +XXX,XX @@ struct binder_object { }; }; +int binder_genl_init(struct genl_family *family, const char *name); + #endif /* _LINUX_BINDER_INTERNAL_H */ diff --git a/drivers/android/binder_trace.h b/drivers/android/binder_trace.h index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binder_trace.h +++ b/drivers/android/binder_trace.h @@ -XXX,XX +XXX,XX @@ 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 diff --git a/drivers/android/binderfs.c b/drivers/android/binderfs.c index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binderfs.c +++ b/drivers/android/binderfs.c @@ -XXX,XX +XXX,XX @@ 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: diff --git a/include/uapi/linux/android/binder.h b/include/uapi/linux/android/binder.h index XXXXXXX..XXXXXXX 100644 --- a/include/uapi/linux/android/binder.h +++ b/include/uapi/linux/android/binder.h @@ -XXX,XX +XXX,XX @@ 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 */ diff --git a/include/uapi/linux/android/binder_genl.h b/include/uapi/linux/android/binder_genl.h new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/include/uapi/linux/android/binder_genl.h @@ -XXX,XX +XXX,XX @@ +/* 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 uapi header */ + +#ifndef _UAPI_LINUX_ANDROID_BINDER_GENL_H +#define _UAPI_LINUX_ANDROID_BINDER_GENL_H + +#define BINDER_GENL_FAMILY_NAME "binder_genl" +#define BINDER_GENL_FAMILY_VERSION 1 + +/** + * enum binder_genl_flag - Used with "set" and "reply" command below, defining + * what kind of binder transactions reported to the user space administration + * process. + */ +enum binder_genl_flag { + BINDER_GENL_FLAG_FAILED = 1, + BINDER_GENL_FLAG_DELAYED = 2, + BINDER_GENL_FLAG_SPAM = 4, + BINDER_GENL_FLAG_OVERRIDE = 8, +}; + +enum { + BINDER_GENL_A_CMD_PID = 1, + BINDER_GENL_A_CMD_FLAGS, + + __BINDER_GENL_A_CMD_MAX, + BINDER_GENL_A_CMD_MAX = (__BINDER_GENL_A_CMD_MAX - 1) +}; + +enum { + BINDER_GENL_A_REPORT_ERR = 1, + BINDER_GENL_A_REPORT_FROM_PID, + BINDER_GENL_A_REPORT_FROM_TID, + BINDER_GENL_A_REPORT_TO_PID, + BINDER_GENL_A_REPORT_TO_TID, + BINDER_GENL_A_REPORT_REPLY, + BINDER_GENL_A_REPORT_FLAGS, + BINDER_GENL_A_REPORT_CODE, + BINDER_GENL_A_REPORT_DATA_SIZE, + + __BINDER_GENL_A_REPORT_MAX, + BINDER_GENL_A_REPORT_MAX = (__BINDER_GENL_A_REPORT_MAX - 1) +}; + +enum { + BINDER_GENL_CMD_SET = 1, + BINDER_GENL_CMD_REPLY, + BINDER_GENL_CMD_REPORT, + + __BINDER_GENL_CMD_MAX, + BINDER_GENL_CMD_MAX = (__BINDER_GENL_CMD_MAX - 1) +}; + +#endif /* _UAPI_LINUX_ANDROID_BINDER_GENL_H */ -- 2.47.0.163.g1226f6d8fa-goog
From: Li Li <dualli@google.com> 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/20240812211844.4107494-1-dualli@chromium.org/ 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/20241011064427.1565287-1-dualli@chromium.org/ 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/20241021182821.1259487-1-dualli@chromium.org/ 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/20241021191233.1334897-1-dualli@chromium.org/ 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/20241025075102.1785960-1-dualli@chromium.org/ The 6th version has some trivial kernel doc fixes, without modifying any other source code. https://lore.kernel.org/lkml/20241028101952.775731-1-dualli@chromium.org/ 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/20241031092504.840708-1-dualli@chromium.org/ 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/20241113193239.2113577-1-dualli@chromium.org/ 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/20241209192247.3371436-1-dualli@chromium.org/ 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/20241212224114.888373-1-dualli@chromium.org/ The 11th version simplifies the yaml filename to avoid redundant words in variable names. This also makes binder netlink yaml more aligned with other existing netlink specs. Another trivial change is to use reverse xmas tree for function variables. https://lore.kernel.org/all/20241218203740.4081865-1-dualli@chromium.org/ The 12th version makes Documentation/admin-guide/binder_netlink.rst aligned with the binder netlink yaml change introduced in the 11th revision. It doesn't change any source code. https://lore.kernel.org/all/20241218212935.4162907-1-dualli@chromium.org/ The 13th version removes the unnecessary dependency to binder file ops. Now the netlink configuration is reset using sock_priv_destroy. It also requires CAP_NET_ADMIN to send commands to the driver. One of the patches ("binderfs: add new binder devices to binder_devices") has been merged to linux-next. To avoid conflict, switch to linux-next master branch and remove the merged one. Adding sock_priv into netlink spec results in CFI failure, which is fixed by the new trampoline patches. https://lore.kernel.org/all/20250115102950.563615-1-dualli@chromium.org/ The 14th version fix the code style issue by wrapping the sock priv in a separate struct, as suggested by Jakub. The other 2 patches are no longer included in this patchset as the equvilent fix has already been merged to upstream linux master branch, as well as net & net-next. This version has already been rebased to TOT of linux-next. https://lore.kernel.org/all/20250118080939.2835687-1-dualli@chromium.org/ The 15th version switches from unicast to multicast per feedback and feature requriements from binder users. With this change, multiple user space processes can listen to the binder reports from the kernel driver at the same time. To receive the multicast messages, those user space processs should query the mcast group id and join the mcast group. In the previous unicast solution, a portid is saved in the kernel driver to prevent unauthorized process to send commands to the kernel driver. In this multicast solution, this is replaced by a new "setup_report" permission in the "binder" class. Meanwhile, the sock_priv_destroy callback and CAP_NET_ADMIN restriction are no longer required in favor of the multicast solution and the new "setup_report" permission. v1: add a global binder genl socket for all contexts v2: change to per-context binder genl for security reason replace the new ioctl with a netlink command add corresponding doc Documentation/admin-guide/binder_genl.rst add user space test code in AOSP v3: use YNL spec (./tools/net/ynl/ynl-regen.sh) fix documentation index v4: change the subject of the patch and remove unsed #if 0 v5: improve the kernel doc and the init function remove unsupported uapi-header in YAML v6: fix some trivial kernel doc issues v7: break the binary struct binder_report into individual attributes v8: use multiplex netlink message in a unified netlink family improve the YAML spec to be consistent with netlink tradition v9: remove unnecessary check to netlink flags and message payloads v10: improve the readability of kernel doc and variable names v11: rename binder_netlinnk.yaml to binder.yaml use reverse xmas tree for function variables v12: make kernel doc aligned with source code v13: use sock_priv_destroy to cleanup netlink require CAP_NET_ADMIN to send netlink commands add trampolines in ynl-gen to fix CFI failure v14: wrap the sock priv in a separate struct v15: switch from unicast to multicast netlink message add a "setup_report" permission in the "binder" class add generic_netlink to binder_features Li Li (2): binder: report txn errors via generic netlink binder: generic netlink binder_features flag Thiébaud Weksteen (1): lsm, selinux: Add setup_report permission to binder Documentation/admin-guide/binder_netlink.rst | 108 +++++++++ Documentation/admin-guide/index.rst | 1 + Documentation/netlink/specs/binder.yaml | 116 +++++++++ drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 229 +++++++++++++++++- drivers/android/binder_internal.h | 16 ++ drivers/android/binder_netlink.c | 46 ++++ drivers/android/binder_netlink.h | 23 ++ drivers/android/binder_trace.h | 35 +++ drivers/android/binderfs.c | 8 + include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 1 + include/uapi/linux/android/binder_netlink.h | 57 +++++ security/security.c | 13 + security/selinux/hooks.c | 7 + security/selinux/include/classmap.h | 3 +- .../filesystems/binderfs/binderfs_test.c | 1 + 18 files changed, 663 insertions(+), 5 deletions(-) create mode 100644 Documentation/admin-guide/binder_netlink.rst create mode 100644 Documentation/netlink/specs/binder.yaml create mode 100644 drivers/android/binder_netlink.c create mode 100644 drivers/android/binder_netlink.h create mode 100644 include/uapi/linux/android/binder_netlink.h base-commit: 8433c776e1eb1371f5cd40b5fd3a61f9c7b7f3ad -- 2.48.1.658.g4767266eb4-goog
From: Thiébaud Weksteen <tweek@google.com> Introduce a new permission "setup_report" to the "binder" class. This persmission controls the ability to set up the binder generic netlink driver to report certain binder transactions. Signed-off-by: Thiébaud Weksteen <tweek@google.com> Signed-off-by: Li Li <dualli@google.com> --- include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 1 + security/security.c | 13 +++++++++++++ security/selinux/hooks.c | 7 +++++++ security/selinux/include/classmap.h | 3 ++- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index XXXXXXX..XXXXXXX 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -XXX,XX +XXX,XX @@ LSM_HOOK(int, 0, binder_transfer_binder, const struct cred *from, const struct cred *to) LSM_HOOK(int, 0, binder_transfer_file, const struct cred *from, const struct cred *to, const struct file *file) +LSM_HOOK(int, 0, binder_setup_report, const struct cred *to) LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child, unsigned int mode) LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent) diff --git a/include/linux/security.h b/include/linux/security.h index XXXXXXX..XXXXXXX 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -XXX,XX +XXX,XX @@ int security_binder_transfer_binder(const struct cred *from, const struct cred *to); int security_binder_transfer_file(const struct cred *from, const struct cred *to, const struct file *file); +int security_binder_setup_report(const struct cred *to); int security_ptrace_access_check(struct task_struct *child, unsigned int mode); int security_ptrace_traceme(struct task_struct *parent); int security_capget(const struct task_struct *target, diff --git a/security/security.c b/security/security.c index XXXXXXX..XXXXXXX 100644 --- a/security/security.c +++ b/security/security.c @@ -XXX,XX +XXX,XX @@ int security_binder_transfer_file(const struct cred *from, return call_int_hook(binder_transfer_file, from, to, file); } +/** + * security_binder_setup_report() - Check if process allowed to set up binder reports. + * @to: receiving process + * + * Check whether @to is allowed to set up binder reports. + * + * Return: Returns 0 if permission is granted. + */ +int security_binder_setup_report(const struct cred *to) +{ + return call_int_hook(binder_setup_report, to); +} + /** * security_ptrace_access_check() - Check if tracing is allowed * @child: target process diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index XXXXXXX..XXXXXXX 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -XXX,XX +XXX,XX @@ static int selinux_binder_transfer_file(const struct cred *from, &ad); } +static int selinux_binder_setup_report(const struct cred *to) +{ + return avc_has_perm(current_sid(), cred_sid(to), SECCLASS_BINDER, + BINDER__SETUP_REPORT, NULL); +} + static int selinux_ptrace_access_check(struct task_struct *child, unsigned int mode) { @@ -XXX,XX +XXX,XX @@ static struct security_hook_list selinux_hooks[] __ro_after_init = { LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), + LSM_HOOK_INIT(binder_setup_report, selinux_binder_setup_report), LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h index XXXXXXX..XXXXXXX 100644 --- a/security/selinux/include/classmap.h +++ b/security/selinux/include/classmap.h @@ -XXX,XX +XXX,XX @@ const struct security_class_mapping secclass_map[] = { { "kernel_service", { "use_as_override", "create_files_as", NULL } }, { "tun_socket", { COMMON_SOCK_PERMS, "attach_queue", NULL } }, { "binder", - { "impersonate", "call", "set_context_mgr", "transfer", NULL } }, + { "impersonate", "call", "set_context_mgr", "transfer", + "setup_report", NULL } }, { "cap_userns", { COMMON_CAP_PERMS, NULL } }, { "cap2_userns", { COMMON_CAP2_PERMS, NULL } }, { "sctp_socket", -- 2.48.1.658.g4767266eb4-goog
From: Li Li <dualli@google.com> Introduce generic netlink messages into the binder driver so that the Linux/Android system administration processes can listen to important events and take corresponding actions, like stopping a broken app from attacking the OS by sending huge amount of spamming binder transactions. The binder netlink sources and headers are automatically generated from the corresponding binder netlink YAML spec. Don't modify them directly. Signed-off-by: Li Li <dualli@google.com> --- Documentation/admin-guide/binder_netlink.rst | 108 +++++++++ Documentation/admin-guide/index.rst | 1 + Documentation/netlink/specs/binder.yaml | 116 ++++++++++ drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 229 ++++++++++++++++++- drivers/android/binder_internal.h | 16 ++ drivers/android/binder_netlink.c | 46 ++++ drivers/android/binder_netlink.h | 23 ++ drivers/android/binder_trace.h | 35 +++ include/uapi/linux/android/binder_netlink.h | 57 +++++ 11 files changed, 630 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 diff --git a/Documentation/admin-guide/binder_netlink.rst b/Documentation/admin-guide/binder_netlink.rst new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/Documentation/admin-guide/binder_netlink.rst @@ -XXX,XX +XXX,XX @@ +.. SPDX-License-Identifier: GPL-2.0 + +============================================================== +Generic Netlink for the Android Binder Driver (Binder Netlink) +============================================================== + +The Generic Netlink subsystem in the Linux kernel provides a generic way for +the Linux kernel to communicate with the user space applications via binder +driver. It is used to report binder transaction errors and warnings to user +space administration process. The driver allows multiple binder devices and +their corresponding binder contexts. Each context and their processes can be +configured independently to report their own transactions. + +Basically, the user space code uses the BINDER_CMD_REPORT_SETUP command to +request what kind of binder transactions should be reported by the driver. +The driver then echoes the attributes in a reply message to acknowledge the +request. The BINDER_CMD_REPORT_SETUP command also registers the current +user space process to receive the reports. This BINDER_CMD_REPORT_SETUP +command is protected by SELinux, to prevent unauthorized user apps from +triggering unexpected reports. + +Currently the driver reports these binder transaction errors and warnings. +1. "FAILED" transactions that fail to reach the target process; +2. "ASYNC_FROZEN" transactions that are delayed due to the target process +being frozen by cgroup freezer; or +3. "SPAM" transactions that are considered spamming according to existing +logic in binder_alloc.c. + +When the specified binder transactions happen, the driver uses the +BINDER_CMD_REPORT command to send a generic netlink message to the +registered process, containing the payload defined in binder.yaml. + +More details about the flags, attributes and operations can be found at the +the doc sections in Documentations/netlink/specs/binder.yaml and the +kernel-doc comments of the new source code in binder.{h|c}. + +Using Binder Netlink +-------------------- + +The Binder Netlink can be used in the same way as any other generic netlink +drivers. Userspace application uses a raw netlink socket to send commands +to and receive packets from the kernel driver. + +Usage example (user space pseudo code): + +:: + /* + * send() below is overloaded to pack netlink commands and attributes + * to nlattr/genlmsghdr/nlmsghdr and then send to the netlink socket. + * + * recv() below is overloaded to receive the raw netlink message from + * the netlink socket, parse nlmsghdr/genlmsghdr to find the netlink + * command and then return the nlattr payload. + */ + + // open netlink socket + int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); + + // bind netlink socket + bind(fd, struct socketaddr); + + // get the family id of the binder netlink + send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME, + BINDER_FAMILY_NAME); + void *data = recv(CTRL_CMD_NEWFAMILY); + if (!has_nla_type(data, CTRL_ATTR_FAMILY_ID)) { + // Binder Netlink isn't available on this version of Linux kernel + return; + } + __u16 id = nla(data)[CTRL_ATTR_FAMILY_ID]; + __u32 grp = nla(data)[CTRL_ATTR_MCAST_GROUPS][CTRL_ATTR_MCAST_GRP_ID]; + + // join the mcast group to listen to the binder netlink messages + setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &grp, sizeof(grp)); + + // enable per-context binder report + send(fd, id, BINDER_CMD_REPORT_SETUP, "binder", 0, + BINDER_FLAG_FAILED | BINDER_FLAG_DELAYED); + + // confirm the per-context configuration + data = recv(fd, BINDER_CMD_REPLY); + char *context = nla(data)[BINDER_A_CMD_CONTEXT]; + __u32 pid = nla(data)[BINDER_A_CMD_PID]; + __u32 flags = nla(data)[BINDER_A_CMD_FLAGS]; + + // set optional per-process report, overriding the per-context one + send(fd, id, BINDER_CMD_REPORT_SETUP, "binder", getpid(), + BINDER_FLAG_SPAM | BINDER_REPORT_OVERRIDE); + + // confirm the optional per-process configuration + data = recv(fd, BINDER_CMD_REPLY); + context = nla(data)[BINDER_A_CMD_CONTEXT]; + pid = nla(data)[BINDER_A_CMD_PID]; + flags = nla(data)[BINDER_A_CMD_FLAGS]; + + // wait and read all binder reports + while (running) { + data = recv(fd, BINDER_CMD_REPORT); + auto *attr = nla(data)[BINDER_A_REPORT_XXX]; + + // process binder report + do_something(*attr); + } + + // clean up + send(fd, id, BINDER_CMD_REPORT_SETUP, 0, 0); + send(fd, id, BINDER_CMD_REPORT_SETUP, getpid(), 0); + close(fd); diff --git a/Documentation/admin-guide/index.rst b/Documentation/admin-guide/index.rst index XXXXXXX..XXXXXXX 100644 --- a/Documentation/admin-guide/index.rst +++ b/Documentation/admin-guide/index.rst @@ -XXX,XX +XXX,XX @@ Block-layer and filesystem administration :maxdepth: 1 bcache + binder_netlink binderfs blockdev/index cifs/index diff --git a/Documentation/netlink/specs/binder.yaml b/Documentation/netlink/specs/binder.yaml new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/Documentation/netlink/specs/binder.yaml @@ -XXX,XX +XXX,XX @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) + +name: binder +protocol: genetlink +uapi-header: linux/android/binder_netlink.h +doc: Netlink protocol to report binder transaction errors and warnings. + +definitions: + - + type: flags + name: flag + doc: Define what kind of binder transactions should be reported. + entries: [ failed, async-frozen, spam, override ] + +attribute-sets: + - + name: cmd + doc: The supported attributes of "report-setup" command. + attributes: + - + name: context + type: string + doc: The binder context to enable binder netlink report. + - + name: pid + type: u32 + doc: The binder proc to enable binder netlink report. + - + name: flags + type: u32 + enum: flag + doc: What kind of binder transactions should be reported. + - + name: report + doc: The supported attributes of "report" command + attributes: + - + name: context + type: string + doc: The binder context where the binder netlink report happens. + - + name: err + type: u32 + doc: Copy of binder_driver_return_protocol returned to the sender. + - + name: from_pid + type: u32 + doc: Sender pid of the corresponding binder transaction. + - + name: from_tid + type: u32 + doc: Sender tid of the corresponding binder transaction. + - + name: to_pid + type: u32 + doc: Target pid of the corresponding binder transaction. + - + name: to_tid + type: u32 + doc: Target tid of the corresponding binder transaction. + - + name: reply + type: u32 + doc: 1 means the transaction is a reply, 0 otherwise. + - + name: flags + type: u32 + doc: Copy of binder_transaction_data->flags. + - + name: code + type: u32 + doc: Copy of binder_transaction_data->code. + - + name: data_size + type: u32 + doc: Copy of binder_transaction_data->data_size. + +operations: + list: + - + name: report-setup + doc: Set flags from user space. + attribute-set: cmd + + do: + request: ¶ms + attributes: + - context + - pid + - flags + reply: *params + - + name: report + doc: Send the requested reports to user space. + attribute-set: report + + event: + attributes: + - context + - err + - from_pid + - from_tid + - to_pid + - to_tid + - reply + - flags + - code + - data_size + +kernel-family: + headers: [ "binder_internal.h" ] + +mcast-groups: + list: + - + name: report diff --git a/drivers/android/Kconfig b/drivers/android/Kconfig index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/Kconfig +++ b/drivers/android/Kconfig @@ -XXX,XX +XXX,XX @@ 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, diff --git a/drivers/android/Makefile b/drivers/android/Makefile index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/Makefile +++ b/drivers/android/Makefile @@ -XXX,XX +XXX,XX @@ 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 diff --git a/drivers/android/binder.c b/drivers/android/binder.c index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -XXX,XX +XXX,XX @@ #include <linux/cacheflush.h> +#include "binder_netlink.h" #include "binder_internal.h" #include "binder_trace.h" @@ -XXX,XX +XXX,XX @@ static void binder_set_txn_from_error(struct binder_transaction *t, int id, binder_thread_dec_tmpref(from); } +/** + * binder_find_proc() - find the binder_proc by pid + * @pid: the target process + * + * Returns the struct binder_proc if the pid is found, or NULL otherwise. + */ +static struct binder_proc *binder_find_proc(int pid) +{ + struct binder_proc *proc; + + mutex_lock(&binder_procs_lock); + hlist_for_each_entry(proc, &binder_procs, proc_node) { + if (proc->pid == pid) + break; + } + mutex_unlock(&binder_procs_lock); + + return proc; +} + +/** + * binder_netlink_enabled() - check if binder netlink reports are enabled + * @proc: the binder_proc to check + * @mask: the categories of binder netlink reports + * + * Returns true if certain binder netlink reports are enabled for this binder + * proc (when per-process overriding takes effect) or context. + */ +static bool binder_netlink_enabled(struct binder_proc *proc, u32 mask) +{ + struct binder_context *context = proc->context; + + if (!genl_has_listeners(&binder_nl_family, &init_net, BINDER_NLGRP_REPORT)) + return false; + + if (proc->report_flags & BINDER_FLAG_OVERRIDE) + return (proc->report_flags & mask) != 0; + else + return (context->report_flags & mask) != 0; +} + +/** + * binder_netlink_report() - report one binder netlink event + * @context: the binder context + * @err: copy of binder_driver_return_protocol returned to the sender + * @pid: sender process + * @tid: sender thread + * @to_pid: target process + * @to_tid: target thread + * @reply: whether the binder transaction is a reply + * @tr: the binder transaction data + * + * Packs the report data into a binder netlink message and send it. + */ +static void binder_netlink_report(struct binder_context *context, u32 err, + u32 pid, u32 tid, u32 to_pid, u32 to_tid, + u32 reply, + struct binder_transaction_data *tr) +{ + struct sk_buff *skb; + void *hdr; + int ret; + + trace_binder_netlink_report(context->name, err, pid, tid, to_pid, + to_tid, reply, tr); + + skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + pr_err("Failed to alloc binder netlink message\n"); + return; + } + + hdr = genlmsg_put(skb, 0, atomic_inc_return(&context->report_seq), + &binder_nl_family, 0, BINDER_CMD_REPORT); + if (!hdr) + goto free_skb; + + if (nla_put_string(skb, BINDER_A_REPORT_CONTEXT, context->name) || + nla_put_u32(skb, BINDER_A_REPORT_ERR, err) || + nla_put_u32(skb, BINDER_A_REPORT_FROM_PID, pid) || + nla_put_u32(skb, BINDER_A_REPORT_FROM_TID, tid) || + nla_put_u32(skb, BINDER_A_REPORT_TO_PID, to_pid) || + nla_put_u32(skb, BINDER_A_REPORT_TO_TID, to_tid) || + nla_put_u32(skb, BINDER_A_REPORT_REPLY, reply) || + nla_put_u32(skb, BINDER_A_REPORT_FLAGS, tr->flags) || + nla_put_u32(skb, BINDER_A_REPORT_CODE, tr->code) || + nla_put_u32(skb, BINDER_A_REPORT_DATA_SIZE, tr->data_size)) + goto cancel_skb; + + genlmsg_end(skb, hdr); + + ret = genlmsg_multicast(&binder_nl_family, skb, 0, BINDER_NLGRP_REPORT, GFP_KERNEL); + if (ret < 0) + pr_err("Failed to send binder netlink message: %d\n", ret); + return; + +cancel_skb: + pr_err("Failed to add attributes to binder netlink message\n"); + genlmsg_cancel(skb, hdr); +free_skb: + pr_err("Free binder netlink report message on error\n"); + nlmsg_free(skb); +} + static void binder_transaction(struct binder_proc *proc, struct binder_thread *thread, struct binder_transaction_data *tr, int reply, @@ -XXX,XX +XXX,XX @@ static void binder_transaction(struct binder_proc *proc, return_error_line = __LINE__; goto err_copy_data_failed; } - if (t->buffer->oneway_spam_suspect) + if (t->buffer->oneway_spam_suspect) { tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT; - else + if (binder_netlink_enabled(proc, BINDER_FLAG_SPAM)) + binder_netlink_report(context, BR_ONEWAY_SPAM_SUSPECT, + proc->pid, thread->pid, + target_proc ? target_proc->pid : 0, + target_thread ? target_thread->pid : 0, + reply, tr); + } else { tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; + } t->work.type = BINDER_WORK_TRANSACTION; if (reply) { @@ -XXX,XX +XXX,XX @@ static void binder_transaction(struct binder_proc *proc, * process and is put in a pending queue, waiting for the target * process to be unfrozen. */ - if (return_error == BR_TRANSACTION_PENDING_FROZEN) + if (return_error == BR_TRANSACTION_PENDING_FROZEN) { tcomplete->type = BINDER_WORK_TRANSACTION_PENDING; + if (binder_netlink_enabled(proc, BINDER_FLAG_ASYNC_FROZEN)) + binder_netlink_report(context, return_error, + proc->pid, thread->pid, + target_proc ? target_proc->pid : 0, + target_thread ? target_thread->pid : 0, + reply, tr); + } binder_enqueue_thread_work(thread, tcomplete); if (return_error && return_error != BR_TRANSACTION_PENDING_FROZEN) @@ -XXX,XX +XXX,XX @@ static void binder_transaction(struct binder_proc *proc, binder_dec_node_tmpref(target_node); } + if (binder_netlink_enabled(proc, BINDER_FLAG_FAILED)) + binder_netlink_report(context, return_error, + proc->pid, thread->pid, + target_proc ? target_proc->pid : 0, + target_thread ? target_thread->pid : 0, + reply, tr); + binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, "%d:%d transaction %s to %d:%d failed %d/%d/%d, code %u size %lld-%lld line %d\n", proc->pid, thread->pid, reply ? "reply" : @@ -XXX,XX +XXX,XX @@ binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) mutex_unlock(&binder_deferred_lock); } +/** + * binder_nl_report_setup_doit() - netlink .doit handler + * @skb: the metadata struct passed from netlink driver + * @info: the generic netlink struct passed from netlink driver + * + * Implements the .doit function to process binder netlink commands. + */ +int binder_nl_report_setup_doit(struct sk_buff *skb, struct genl_info *info) +{ + struct binder_context *context = NULL; + struct binder_device *device; + struct binder_proc *proc; + u32 flags, pid; + void *hdr; + int ret; + + ret = security_binder_setup_report(current_cred()); + if (ret < 0) { + NL_SET_ERR_MSG(info->extack, "Permission denied"); + return ret; + } + + hlist_for_each_entry(device, &binder_devices, hlist) { + if (!nla_strcmp(info->attrs[BINDER_A_CMD_CONTEXT], + device->context.name)) { + context = &device->context; + break; + } + } + + if (!context) { + NL_SET_ERR_MSG(info->extack, "Unknown binder context"); + return -EINVAL; + } + + pid = nla_get_u32(info->attrs[BINDER_A_CMD_PID]); + flags = nla_get_u32(info->attrs[BINDER_A_CMD_FLAGS]); + + if (!pid) { + /* 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) { + NL_SET_ERR_MSG_FMT(info->extack, + "Invalid binder report pid %u", + pid); + ret = -EINVAL; + goto err_exit; + } + + proc->report_flags = flags; + } + + skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + pr_err("Failed to alloc binder netlink reply message\n"); + ret = -ENOMEM; + goto err_exit; + } + + hdr = genlmsg_iput(skb, info); + if (!hdr) + goto free_skb; + + if (nla_put_string(skb, BINDER_A_CMD_CONTEXT, context->name) || + nla_put_u32(skb, BINDER_A_CMD_PID, pid) || + nla_put_u32(skb, BINDER_A_CMD_FLAGS, flags)) + goto cancel_skb; + + genlmsg_end(skb, hdr); + + if (genlmsg_reply(skb, info)) { + pr_err("Failed to send binder netlink reply message\n"); + ret = -EFAULT; + goto err_exit; + } + + return 0; + +cancel_skb: + pr_err("Failed to add reply attributes to binder netlink message\n"); + genlmsg_cancel(skb, hdr); +free_skb: + pr_err("Free binder netlink reply message on error\n"); + nlmsg_free(skb); + ret = -EMSGSIZE; +err_exit: + return ret; +} + static void print_binder_transaction_ilocked(struct seq_file *m, struct binder_proc *proc, const char *prefix, @@ -XXX,XX +XXX,XX @@ static int __init binder_init(void) if (ret) goto err_init_binder_device_failed; + ret = genl_register_family(&binder_nl_family); + if (ret) { + pr_err("Failed to register binder netlink family\n"); + goto err_init_binder_device_failed; + } return ret; err_init_binder_device_failed: diff --git a/drivers/android/binder_internal.h b/drivers/android/binder_internal.h index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binder_internal.h +++ b/drivers/android/binder_internal.h @@ -XXX,XX +XXX,XX @@ #include <linux/stddef.h> #include <linux/types.h> #include <linux/uidgid.h> +#include <net/genetlink.h> #include <uapi/linux/android/binderfs.h> #include "binder_alloc.h" #include "dbitmap.h" +/** + * struct binder_context - information about a binder domain + * @binder_context_mgr_node: the context manager + * @context_mgr_node_lock: the lock protecting the above context manager node + * @binder_context_mgr_uid: the uid of the above context manager + * @name: the name of the binder device + * @report_flags: the categories of binder transactions that would + * be reported (see enum binder_report_flag). + * @report_seq: the seq number of the generic netlink report + */ struct binder_context { struct binder_node *binder_context_mgr_node; struct mutex context_mgr_node_lock; kuid_t binder_context_mgr_uid; const char *name; + u32 report_flags; + atomic_t report_seq; }; /** @@ -XXX,XX +XXX,XX @@ 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 */ @@ -XXX,XX +XXX,XX @@ struct binder_proc { spinlock_t outer_lock; struct dentry *binderfs_entry; bool oneway_spam_detection_enabled; + u32 report_flags; }; /** diff --git a/drivers/android/binder_netlink.c b/drivers/android/binder_netlink.c new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/drivers/android/binder_netlink.c @@ -XXX,XX +XXX,XX @@ +// 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> +#include <binder_internal.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, + }, +}; + +static const struct genl_multicast_group binder_nl_mcgrps[] = { + [BINDER_NLGRP_REPORT] = { "report", }, +}; + +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), + .mcgrps = binder_nl_mcgrps, + .n_mcgrps = ARRAY_SIZE(binder_nl_mcgrps), +}; diff --git a/drivers/android/binder_netlink.h b/drivers/android/binder_netlink.h new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/drivers/android/binder_netlink.h @@ -XXX,XX +XXX,XX @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/binder.yaml */ +/* YNL-GEN kernel header */ + +#ifndef _LINUX_BINDER_GEN_H +#define _LINUX_BINDER_GEN_H + +#include <net/netlink.h> +#include <net/genetlink.h> + +#include <uapi/linux/android/binder_netlink.h> +#include <binder_internal.h> + +int binder_nl_report_setup_doit(struct sk_buff *skb, struct genl_info *info); + +enum { + BINDER_NLGRP_REPORT, +}; + +extern struct genl_family binder_nl_family; + +#endif /* _LINUX_BINDER_GEN_H */ diff --git a/drivers/android/binder_trace.h b/drivers/android/binder_trace.h index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binder_trace.h +++ b/drivers/android/binder_trace.h @@ -XXX,XX +XXX,XX @@ 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 diff --git a/include/uapi/linux/android/binder_netlink.h b/include/uapi/linux/android/binder_netlink.h new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/include/uapi/linux/android/binder_netlink.h @@ -XXX,XX +XXX,XX @@ +/* 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 uapi header */ + +#ifndef _UAPI_LINUX_ANDROID_BINDER_NETLINK_H +#define _UAPI_LINUX_ANDROID_BINDER_NETLINK_H + +#define BINDER_FAMILY_NAME "binder" +#define BINDER_FAMILY_VERSION 1 + +/* + * Define what kind of binder transactions should be reported. + */ +enum binder_flag { + BINDER_FLAG_FAILED = 1, + BINDER_FLAG_ASYNC_FROZEN = 2, + BINDER_FLAG_SPAM = 4, + BINDER_FLAG_OVERRIDE = 8, +}; + +enum { + BINDER_A_CMD_CONTEXT = 1, + BINDER_A_CMD_PID, + BINDER_A_CMD_FLAGS, + + __BINDER_A_CMD_MAX, + BINDER_A_CMD_MAX = (__BINDER_A_CMD_MAX - 1) +}; + +enum { + BINDER_A_REPORT_CONTEXT = 1, + BINDER_A_REPORT_ERR, + BINDER_A_REPORT_FROM_PID, + BINDER_A_REPORT_FROM_TID, + BINDER_A_REPORT_TO_PID, + BINDER_A_REPORT_TO_TID, + BINDER_A_REPORT_REPLY, + BINDER_A_REPORT_FLAGS, + BINDER_A_REPORT_CODE, + BINDER_A_REPORT_DATA_SIZE, + + __BINDER_A_REPORT_MAX, + BINDER_A_REPORT_MAX = (__BINDER_A_REPORT_MAX - 1) +}; + +enum { + BINDER_CMD_REPORT_SETUP = 1, + BINDER_CMD_REPORT, + + __BINDER_CMD_MAX, + BINDER_CMD_MAX = (__BINDER_CMD_MAX - 1) +}; + +#define BINDER_MCGRP_REPORT "report" + +#endif /* _UAPI_LINUX_ANDROID_BINDER_NETLINK_H */ -- 2.48.1.658.g4767266eb4-goog
From: Li Li <dualli@google.com> Add a flag to binder_features to indicate that the generic netlink feature is available. Signed-off-by: Li Li <dualli@google.com> --- drivers/android/binderfs.c | 8 ++++++++ .../selftests/filesystems/binderfs/binderfs_test.c | 1 + 2 files changed, 9 insertions(+) diff --git a/drivers/android/binderfs.c b/drivers/android/binderfs.c index XXXXXXX..XXXXXXX 100644 --- a/drivers/android/binderfs.c +++ b/drivers/android/binderfs.c @@ -XXX,XX +XXX,XX @@ struct binder_features { bool oneway_spam_detection; bool extended_error; bool freeze_notification; + bool generic_netlink; }; static const struct constant_table binderfs_param_stats[] = { @@ -XXX,XX +XXX,XX @@ static struct binder_features binder_features = { .oneway_spam_detection = true, .extended_error = true, .freeze_notification = true, + .generic_netlink = true, }; static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb) @@ -XXX,XX +XXX,XX @@ static int init_binder_features(struct super_block *sb) if (IS_ERR(dentry)) return PTR_ERR(dentry); + dentry = binderfs_create_file(dir, "generic_netlink", + &binder_features_fops, + &binder_features.generic_netlink); + if (IS_ERR(dentry)) + return PTR_ERR(dentry); + return 0; } diff --git a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c index XXXXXXX..XXXXXXX 100644 --- a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c +++ b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c @@ -XXX,XX +XXX,XX @@ static int __do_binderfs_test(struct __test_metadata *_metadata) "oneway_spam_detection", "extended_error", "freeze_notification", + "generic_netlink", }; change_mountns(_metadata); -- 2.48.1.658.g4767266eb4-goog