add binder genl for txn report
PATCH 1/1 add binder genl for txn report
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.
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 transactions.
To prevent making the already bloated binder.c even bigger, a new source file binder_genl.c is created to host those generic netlink code.
Usage example (user space pseudo code):
// enable binder report from the interested binder context(s)
struct binder_report_info info = {0, BINDER_REPORT_ALL};
ioctl(binder1, BINDER_ENABLE_REPORT, &info);
ioctl(binder2, BINDER_ENABLE_REPORT, &info);
// set optional per-process report, overriding the global one
struct binder_report_info info2;
info2.pid = getpid();
info2.flags = BINDER_REPORT_FAILED | BINDER_REPORT_OVERRIDE;
ioctl(binder2, BINDER_ENABLE_REPORT, &info2);
// open netlink socket
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
// bind netlink socket
bind(fd, struct socketaddr);
// get the family id of binder generic netlink
send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME);
int id = recv(CTRL_ATTR_FAMILY_ID);
// register the current process to receive binder reports
send(fd, id, BINDER_GENL_CMD_REGISTER);
// verify registration by reading back the registered pid
recv(fd, BINDER_GENL_ATTR_PID, &pid);
// wait and read all binder reports
while (running) {
struct binder_report report;
recv(fd, BINDER_GENL_ATTR_REPORT, &report);
// process struct binder_report
do_something(&report);
}
// clean up
close(fd);--- drivers/android/Makefile | 2 +- drivers/android/binder.c | 125 +++++++++++++++++- drivers/android/binder_genl.c | 197 ++++++++++++++++++++++++++++ drivers/android/binder_genl.h | 17 +++ drivers/android/binder_internal.h | 13 ++ drivers/android/binder_trace.h | 37 ++++++ include/uapi/linux/android/binder.h | 147 +++++++++++++++++++++ 7 files changed, 534 insertions(+), 4 deletions(-) create mode 100644 drivers/android/binder_genl.c create mode 100644 drivers/android/binder_genl.h
drivers/android/Makefile +1 -1
@@ -2,5 +2,5 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_ANDROID_BINDERFS) += binderfs.o -obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o +obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o binder_genl.o obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
drivers/android/binder.c +122 -3
170 lines not on this page · raw message
drivers/android/binder_genl.c new file +197 -0
197 lines not on this page · raw message
drivers/android/binder_genl.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2024 Google, Inc. +*/ + +#ifndef _LINUX_BINDER_GENL_H +#define _LINUX_BINDER_GENL_H + +#include "binder_internal.h" + +int binder_genl_init (void ); + +bool binder_genl_report_enabled (struct binder_proc *proc,u32 mask); + +void binder_genl_send_report (struct binder_report *report,int len); + +#endif /* _LINUX_BINDER_GENL_H */
drivers/android/binder_internal.h +13 -0
@@ -16,11 +16,21 @@#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). +*/ 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; };/**
@@ -399,6 +409,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 */
@@ -436,6 +448,7 @@ struct binder_proc {spinlock_t outer_lock;struct dentry *binderfs_entry;bool oneway_spam_detection_enabled; +u32 report_flags; };/**
drivers/android/binder_trace.h +37 -0
@@ -423,6 +423,43 @@ TRACE_EVENT(binder_return, "unknown") ); +TRACE_EVENT (binder_send_report, + TP_PROTO(struct binder_report *report, int len), +TP_ARGS (report, len), +TP_STRUCT__entry ( + __field(const char *, name) + __field(uint32_t, err) + __field(uint32_t, from_pid) + __field(uint32_t, from_tid) + __field(uint32_t, to_pid) + __field(uint32_t, to_tid) + __field(uint32_t, reply) + __field(uint32_t, flags) + __field(uint32_t, code) + __field(binder_size_t, data_size) +__field (uint32_t, len) + ), +TP_fast_assign ( + __entry->name = report->name; + __entry->err = report->err; + __entry->from_pid = report->from_pid; + __entry->from_tid = report->from_tid; + __entry->to_pid = report->to_pid; + __entry->to_tid = report->to_tid; + __entry->reply = report->reply; + __entry->flags = report->flags; + __entry->code = report->code; + __entry->data_size = report->data_size; + __entry->len = len; + ), +TP_printk ("%s: %d %d:%d -> %d:%d %s flags=0x08%x code=%d %llu %d" , + __entry->name, __entry->err, __entry->from_pid, + __entry->from_tid, __entry->to_pid, __entry->to_tid, + __entry->reply ?"reply" :"" , + __entry->flags, __entry->code, __entry->data_size, + __entry->len) +); +#endif /* _BINDER_TRACE_H */ #undef TRACE_INCLUDE_PATH
include/uapi/linux/android/binder.h +147 -0
158 lines not on this page · raw message
Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
Greg KH
maintainerQuoted 1 line · Li Li
From: Li Li <[email protected]>
Sorry, but I can not parse your Subject: line at all. Please use vowels, we don't have a lack of them :)
Also look at how other patches are formatted for these files to get an idea of how to create a good subject line.
Quoted 11 lines
Frozen tasks can't process binder transactions, so sync binder transactions will fail with
BR_FROZEN_REPLYand 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 withBR_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.
Quoted 8 lines
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 transactions.
To prevent making the already bloated
binder.ceven bigger, a new source filebinder_genl.cis created to host those generic netlink code.
"genl" is a rough abbreviation that is not going to be easy to remember, what's wrong with "binder_netlink.c"?
Quoted 33 lines
Usage example (user space pseudo code):
// enable binder report from the interested binder context(s) struct binder_report_info info = {0, BINDER_REPORT_ALL}; ioctl(binder1, BINDER_ENABLE_REPORT, &info); ioctl(binder2, BINDER_ENABLE_REPORT, &info); // set optional per-process report, overriding the global one struct binder_report_info info2; info2.pid = getpid(); info2.flags = BINDER_REPORT_FAILED | BINDER_REPORT_OVERRIDE; ioctl(binder2, BINDER_ENABLE_REPORT, &info2); // open netlink socket int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); // bind netlink socket bind(fd, struct socketaddr); // get the family id of binder generic netlink send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME); int id = recv(CTRL_ATTR_FAMILY_ID); // register the current process to receive binder reports send(fd, id, BINDER_GENL_CMD_REGISTER); // verify registration by reading back the registered pid recv(fd, BINDER_GENL_ATTR_PID, &pid); // wait and read all binder reports while (running) { struct binder_report report;
Quoted 8 lines
recv(fd, BINDER_GENL_ATTR_REPORT, &report); // process struct binder_report do_something(&report); } // clean up close(fd);
What userspace code is now going to use this and require it? How was it tested? Where is the test code? Where is the new user/kernel api that you created here documented?
You added a new ioctl here as well, why not mention that? Why is it needed? Why not always emit netlink messages? How do you turn them off?
And how does this deal with binder namespaces? Are these messages all now "global" somehow? Or are they separated properly per namespace?
thanks,
greg k-h
Quoted 9 lines · Greg KH
Quoted 1 line · Li Li
From: Li Li <[email protected]>
Sorry, but I can not parse your Subject: line at all. Please use vowels, we don't have a lack of them :)
Also look at how other patches are formatted for these files to get an idea of how to create a good subject line.
Thank you for reviewing the patch!
Sure, I'll find a more meaningful subject in v2.
Quoted 5 lines
Quoted 2 lines
To prevent making the already bloated
binder.ceven bigger, a new source filebinder_genl.cis created to host those generic netlink code."genl" is a rough abbreviation that is not going to be easy to remember, what's wrong with "
binder_netlink.c"?
It's just because genl has already been used in both of generic netlink kernel code (e.g. in linux/include/net/genetlink.h) and user space libraries
https://man7.org/linux/man-pages/man8/genl.8.html.
I'm happy to rename it to binder_netlink.c if that's easier to remember.
Quoted 41 lines
Quoted 41 lines
Usage example (user space pseudo code):
// enable binder report from the interested binder context(s) struct binder_report_info info = {0, BINDER_REPORT_ALL}; ioctl(binder1, BINDER_ENABLE_REPORT, &info); ioctl(binder2, BINDER_ENABLE_REPORT, &info); // set optional per-process report, overriding the global one struct binder_report_info info2; info2.pid = getpid(); info2.flags = BINDER_REPORT_FAILED | BINDER_REPORT_OVERRIDE; ioctl(binder2, BINDER_ENABLE_REPORT, &info2); // open netlink socket int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); // bind netlink socket bind(fd, struct socketaddr); // get the family id of binder generic netlink send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME); int id = recv(CTRL_ATTR_FAMILY_ID); // register the current process to receive binder reports send(fd, id, BINDER_GENL_CMD_REGISTER); // verify registration by reading back the registered pid recv(fd, BINDER_GENL_ATTR_PID, &pid); // wait and read all binder reports while (running) { struct binder_report report; recv(fd, BINDER_GENL_ATTR_REPORT, &report); // process struct binder_report do_something(&report); }
Quoted 6 lines
Quoted 2 lines
// clean up close(fd);What userspace code is now going to use this and require it? How was it tested? Where is the test code? Where is the new user/kernel api that you created here documented?
As mentioned in the commit message, binder is used in Android OS. But the user space administration process can do little to deal with binder transaction errors. This is tested with Android. I'll upload the user space code to AOSP. If there's a better option to host the test code, e.g. a smaller and simpler project
that uses binder, please let me know.
Quoted 4 lines
You added a new ioctl here as well, why not mention that? Why is it needed? Why not always emit netlink messages? How do you turn them off?
The generic netlink message is a convenient way for the kernel driver to send information to user space. Technically it's possible to replace this new ioctl with
a netlink message. But that requires much more unnecessary code parsing the raw byte streams and accessing internal binder_proc and other structures from netlink layer. It's much more elegant to use an ioctl with only a couple lines of
source code.
To turn them off, just call the same ioctl, resetting the flags to 0. That said, the
name of this new ioctl (BINDER_ENABLE_REPORT) isn't good enough. What do you think if I change it to BINDER_SET_NETLINK_REPORT?
Quoted 3 lines
And how does this deal with binder namespaces? Are these messages all now "global" somehow? Or are they separated properly per namespace?
The new ioctl BINDER_ENABLE_REPORT (again, it deserves a better name) sets the report_flags for its associated binder context. Each binder context has its own settings. The messages from all binder contexts are sent to user space via the same netlink socket. The user space code can enable the reports for each binder context separately and distinguish the netlink messages by the name of the binder context.
It's also possible to have one netlink socket for each binder context. But it seems
like overkill to me.
Thanks,
Li
Greg KH
maintainerQuoted 18 lines · Li Li
Quoted 9 lines · Greg KH
Quoted 1 line · Li Li
From: Li Li <[email protected]>
Sorry, but I can not parse your Subject: line at all. Please use vowels, we don't have a lack of them :)
Also look at how other patches are formatted for these files to get an idea of how to create a good subject line.
Thank you for reviewing the patch!
Sure, I'll find a more meaningful subject in v2.
Quoted 3 lines
Quoted 2 lines
To prevent making the already bloated
binder.ceven bigger, a new source filebinder_genl.cis created to host those generic netlink code.
Quoted 6 lines
Quoted 2 lines
"genl" is a rough abbreviation that is not going to be easy to remember, what's wrong with "
binder_netlink.c"?It's just because genl has already been used in both of generic netlink kernel code (e.g. in
linux/include/net/genetlink.h) and user space libraries
https://man7.org/linux/man-pages/man8/genl.8.html.
Ah, I wasn't aware of the existing names, so that's fine if it is what the networking world is used to.
Which reminds me, why aren't you asking for their review here as well to ensure that you are doing things with netlink correctly?
Quoted 10 lines
Quoted 3 lines
What userspace code is now going to use this and require it? How was it tested? Where is the test code? Where is the new user/kernel api that you created here documented?
As mentioned in the commit message, binder is used in Android OS. But the user space administration process can do little to deal with binder transaction errors. This is tested with Android. I'll upload the user space code to AOSP. If there's a better option to host the test code, e.g. a smaller and simpler project
that uses binder, please let me know.
It needs to be somewhere, otherwise we don't know how any of this is being used at all. And there was some binder "test code" somewhere, is this new functionality added to that also?
Quoted 4 lines
Quoted 3 lines
You added a new ioctl here as well, why not mention that? Why is it needed? Why not always emit netlink messages? How do you turn them off?
Quoted 8 lines
The generic netlink message is a convenient way for the kernel driver to send information to user space. Technically it's possible to replace this new ioctl with
a netlink message. But that requires much more unnecessary code parsing the raw byte streams and accessing internalbinder_procand other structures from netlink layer. It's much more elegant to use an ioctl with only a couple lines of
source code.
Then you need to document that somewhere.
Quoted 4 lines
To turn them off, just call the same ioctl, resetting the flags to 0. That said, the
name of this new ioctl (BINDER_ENABLE_REPORT) isn't good enough. What do you think if I change it toBINDER_SET_NETLINK_REPORT?
Yes, the name needs to change if you can both enable and disable reports from it.
Quoted 9 lines
Quoted 2 lines
And how does this deal with binder namespaces? Are these messages all now "global" somehow? Or are they separated properly per namespace?
The new ioctl
BINDER_ENABLE_REPORT(again, it deserves a better name) sets thereport_flagsfor its associated binder context. Each binder context has its own settings. The messages from all binder contexts are sent to user space via the same netlink socket. The user space code can enable the reports for each binder context separately and distinguish the netlink messages by the name of the binder context.
So userspace will now get all reports and has to do the parsing to determine what is, and is not, relevant for what they want? That feels like a big security hole to me, has this been audited by the Android security team yet?
Quoted 3 lines
It's also possible to have one netlink socket for each binder context. But it seems
like overkill to me.
Again, think of security issues please. Do you want all binder processes in a system to be able to see all other messages?
thanks,
greg k-h
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Li,
kernel test robot noticed the following build errors:
[auto build test ERROR on staging/staging-testing]
[also build test ERROR on staging/staging-next staging/staging-linus linus/master v6.11-rc3 next-20240814] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Li-Li/add-binder-genl-for-txn-report/20240814-150338 base: staging/staging-testing patch link: https://lore.kernel.org/r/20240812211844.4107494-1-dualli%40chromium.org patch subject: [PATCH v1] add binder genl for txn report config: x86_64-buildonly-randconfig-002-20240814 (https://download.01.org/0day-ci/archive/20240815/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240815/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All error/warnings (new ones prefixed by >>):
>> drivers/android/binder_genl.c:20: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* The registered process that would receive binder reports.
drivers/android/binder_genl.c:25: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* The policy to verify the type of the binder genl data
>> drivers/android/binder_genl.c:102: warning: cannot understand function prototype: 'struct genl_small_ops binder_genl_ops[] = '
>> drivers/android/binder_genl.c:114: warning: cannot understand function prototype: 'struct genl_family binder_gnl_family = '
--
ld: drivers/android/binder_genl.o: in function `binder_genl_cmd_doit':
>> binder_genl.c:(.text+0x38): undefined reference to `__alloc_skb'
>> ld: binder_genl.c:(.text+0x61): undefined reference to `genlmsg_put'
>> ld: binder_genl.c:(.text+0x83): undefined reference to `nla_put'
>> ld: binder_genl.c:(.text+0xb7): undefined reference to `init_net'
>> ld: binder_genl.c:(.text+0xbc): undefined reference to `netlink_unicast'
>> ld: binder_genl.c:(.text+0x135): undefined reference to `skb_trim'
>> ld: binder_genl.c:(.text+0x144): undefined reference to `sk_skb_reason_drop'
ld: binder_genl.c:(.text+0x195): undefined reference to `sk_skb_reason_drop'
ld: drivers/android/binder_genl.o: in function `binder_genl_send_report':
binder_genl.c:(.text+0x21c): undefined reference to `__alloc_skb'
ld: binder_genl.c:(.text+0x248): undefined reference to `genlmsg_put'
ld: binder_genl.c:(.text+0x267): undefined reference to `nla_put'
ld: binder_genl.c:(.text+0x298): undefined reference to `init_net'
ld: binder_genl.c:(.text+0x29d): undefined reference to `netlink_unicast'
ld: binder_genl.c:(.text+0x304): undefined reference to `skb_trim'
ld: binder_genl.c:(.text+0x313): undefined reference to `sk_skb_reason_drop'
ld: binder_genl.c:(.text+0x34c): undefined reference to `sk_skb_reason_drop'
ld: drivers/android/binder_genl.o: in function `binder_genl_init':
>> binder_genl.c:(.init.text+0x12): undefined reference to `genl_register_family'Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Li,
kernel test robot noticed the following build errors:
[auto build test ERROR on staging/staging-testing]
[also build test ERROR on staging/staging-next staging/staging-linus linus/master v6.11-rc3 next-20240814] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Li-Li/add-binder-genl-for-txn-report/20240814-150338 base: staging/staging-testing patch link: https://lore.kernel.org/r/20240812211844.4107494-1-dualli%40chromium.org patch subject: [PATCH v1] add binder genl for txn report config: x86_64-buildonly-randconfig-005-20240814 (https://download.01.org/0day-ci/archive/20240815/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240815/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
ld: vmlinux.o: in function `binder_genl_cmd_doit':
binder_genl.c:(.text+0x1284e00): undefined reference to `__alloc_skb'
ld: binder_genl.c:(.text+0x1284e2f): undefined reference to `genlmsg_put'
ld: binder_genl.c:(.text+0x1284e5c): undefined reference to `nla_put'
ld: binder_genl.c:(.text+0x1284e94): undefined reference to `init_net'
ld: binder_genl.c:(.text+0x1284e9d): undefined reference to `netlink_unicast'
ld: binder_genl.c:(.text+0x1284f04): undefined reference to `skb_trim'
ld: binder_genl.c:(.text+0x1284f18): undefined reference to `sk_skb_reason_drop'
ld: binder_genl.c:(.text+0x1284f72): undefined reference to `sk_skb_reason_drop'
ld: vmlinux.o: in function `binder_genl_send_report':
>> (.text+0x12850d9): undefined reference to `__alloc_skb' >> ld: (.text+0x128510a): undefined reference to `genlmsg_put' >> ld: (.text+0x1285131): undefined reference to `nla_put' >> ld: (.text+0x128516e): undefined reference to `init_net' >> ld: (.text+0x1285173): undefined reference to `netlink_unicast' >> ld: (.text+0x12851c2): undefined reference to `skb_trim' >> ld: (.text+0x12851d6): undefined reference to `sk_skb_reason_drop' ld: (.text+0x128527a): undefined reference to `sk_skb_reason_drop' ld: vmlinux.o: in function `binder_genl_init': >> (.init.text+0xa5bd7): undefined reference to `genl_register_family'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki