From nobody Tue Dec 16 11:30:02 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C286246BA1 for ; Fri, 25 Jul 2025 18:38:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753468725; cv=none; b=EPJ6es2UL3Zy8twtDvjZgiqCJADDckGyPUzC4qvqLcW68ddvmbxCtOZ5MBCZN0Esfi3FubF2f6UMC7YY1YAr+2VFGlLDQs8hK7wEEfO5egqrNwnfkKtYIaiMiURA8Ykl3CnnYF8XtDEq7oAoLpTTifvvnJzV41IrN1fhw4DRQ0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753468725; c=relaxed/simple; bh=ZeGfMMRhTnAG2ctNVuCX1noxNGJYbnkcz1g3/yd2XdU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eiQTaYXGnnPBvY5CXnG26YHcwEBhhLaKYVuJwaVUX2QtnjQP+kzpYTrRGuk09U+Gfnry+K3iWnL1Hr57GYWj+de4fX3oyAXxgm3sjBeKZU/TTo3K9IVIa5AJfSRV8l9ygmjaGgTLEl1hwwucPQgQWgMwhW2ytLANItLZLy//CFU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zcn4GCaG; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zcn4GCaG" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-23fba8fd56bso2665095ad.2 for ; Fri, 25 Jul 2025 11:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753468723; x=1754073523; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hERtp2g6Pc7QoTZ98mMyXxU41JG0va0Re9zNA1XThII=; b=zcn4GCaGT2kLhYWeca0zbOccYioV/CxH+cfRs9RPQPvp9jdoNxSQqPsPXp7fFuVunr xfuunE32gq3tTVkc4KhiX2gGoxH6AEAqif0M6yHs4kfdA3SdgZglAxMv0d9gqQKJl0Al pcTKLD0yHHULukaDJpUGII52RFHuE6yQ+cAztXficfHemKlvUoH9Dp9/ny7nXM29Yq27 QWBNqJ4+oM1ersI4wf7k0bfbWyaRwHTcfgvO18icxIXYvJXK0r1Orb6oaRAAgHS6De5n zYquv4oQfMfFDvZyhKwvApOKjkKgA/rP0l1tNQsW+iLprzNV39ONK9lJjBJWCvVzyHSg 4o5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753468723; x=1754073523; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hERtp2g6Pc7QoTZ98mMyXxU41JG0va0Re9zNA1XThII=; b=vmUMhrXeVEAr4n73nxv6xtzTocU+nFvFBPi5ctosHcL4TCU6NR2dWiZHp8JQsOpCrG xMT0p/aexeaQUWJdM8DWtud4avZcWDgokQZMi9RlhXJKa2VIROUe1lrfNQpDUdkdHtKT 3g7vJ4fH9Or5agK8WXBCYaY4Rhtr74MsB6gpv0RkldiJmNb0NjpXzzUIn/9rpyjZaNpn xwRouVnBgi9ddlfPJwZMd4G6yRUMb8a2j8mM6HXxOiOCK/wYtHkeaXBFlCYH4MBKoGu1 pcVgj5z4MN5w+VIDk/zfFIgKAhCNCKv4U3HLPoRVvg3C0JtlwtEQ7biozoSir6YrGnZm w2Ow== X-Forwarded-Encrypted: i=1; AJvYcCX9piEAuPmj5a96Ka54d5DDYIoZsBw4VLhpvzF1ch7vDAWrm8PGdIZxZdOHK5nkFd1X+0zkqky17GKMTK0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5BSsbuXGmpRwBNMAkEpbQJpricpPsHKxf5GSNFcJ2t4HlTQFT gwdNbHmWWW6htHmp1hYD/7gIgvdriUX85vvvdIXzPzQ7slMRLqC7y7Sp/bdZIbqhtNPeZaoNqOl r7Hu3ETni2Hyr0Q== X-Google-Smtp-Source: AGHT+IElnQ3ltmUEMUOrnqr+/5aFfilQzhfxMYT2GQzuCKhfrb3hO0WPIinsgr69dSU9OKlubrsJp7mniL69pw== X-Received: from plht16.prod.google.com ([2002:a17:903:2f10:b0:23e:3914:f342]) (user=cmllamas job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:490b:b0:234:d7c5:a0f6 with SMTP id d9443c01a7336-23fb30aaf67mr34769985ad.31.1753468722686; Fri, 25 Jul 2025 11:38:42 -0700 (PDT) Date: Fri, 25 Jul 2025 18:37:46 +0000 In-Reply-To: <20250725183811.409580-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725183811.409580-1-cmllamas@google.com> X-Mailer: git-send-email 2.50.1.470.g6ba607880d-goog Message-ID: <20250725183811.409580-4-cmllamas@google.com> Subject: [PATCH v19 3/5] binder: introduce transaction reports via netlink From: Carlos Llamas To: Alice Ryhl , Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Donald Hunter , Li Li Cc: Tiffany Yang , John Stultz , Shai Barack , "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" , kernel-team@android.com, linux-kernel@vger.kernel.org, "open list:NETWORKING [GENERAL]" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Li Li Introduce a generic netlink multicast event to report binder transaction failures to userspace. This allows subscribers to monitor these events and take appropriate actions, such as stopping a misbehaving application that is spamming a service with huge amount of transactions. The multicast event contains full details of the failed transactions, including the sender/target PIDs, payload size and specific error code. This interface is defined using a YAML spec, from which the UAPI and kernel headers and source are auto-generated. Signed-off-by: Li Li Signed-off-by: Carlos Llamas --- Documentation/netlink/specs/binder.yaml | 96 +++++++++++++++++++++ MAINTAINERS | 1 + drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 85 +++++++++++++++++- drivers/android/binder_netlink.c | 32 +++++++ drivers/android/binder_netlink.h | 21 +++++ include/uapi/linux/android/binder_netlink.h | 37 ++++++++ 8 files changed, 270 insertions(+), 5 deletions(-) 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/netlink/specs/binder.yaml b/Documentation/netlin= k/specs/binder.yaml new file mode 100644 index 000000000000..a2e54aa42448 --- /dev/null +++ b/Documentation/netlink/specs/binder.yaml @@ -0,0 +1,96 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cla= use) +# +# Copyright 2025 Google LLC +# +--- +name: binder +protocol: genetlink +uapi-header: linux/android/binder_netlink.h +doc: Binder interface over generic netlink + +attribute-sets: + - + name: report + doc: | + Attributes included within a transaction failure report. The elements + correspond directly with the specific transaction that failed, along + with the error returned to the sender e.g. BR_DEAD_REPLY. + + attributes: + - + name: error + type: u32 + doc: The enum binder_driver_return_protocol returned to the sender. + - + name: context + type: string + doc: The binder context where the transaction occurred. + - + name: from_pid + type: u32 + doc: The PID of the sender process. + - + name: from_tid + type: u32 + doc: The TID of the sender thread. + - + name: to_pid + type: u32 + doc: | + The PID of the recipient process. This attribute may not be pres= ent + if the target could not be determined. + - + name: to_tid + type: u32 + doc: | + The TID of the recipient thread. This attribute may not be prese= nt + if the target could not be determined. + - + name: is_reply + type: flag + doc: When present, indicates the failed transaction is a reply. + - + name: flags + type: u32 + doc: The bitmask of enum transaction_flags from the transaction. + - + name: code + type: u32 + doc: The application-defined code from the transaction. + - + name: data_size + type: u32 + doc: The transaction payload size in bytes. + +operations: + list: + - + name: report + doc: | + A multicast event sent to userspace subscribers to notify them abo= ut + binder transaction failures. The generated report provides the full + details of the specific transaction that failed. The intention is = for + programs to monitor these events and react to the failures as need= ed. + + attribute-set: report + mcgrp: report + event: + attributes: + - error + - context + - from_pid + - from_tid + - to_pid + - to_tid + - is_reply + - flags + - code + - data_size + +kernel-family: + headers: ["binder_internal.h"] + +mcast-groups: + list: + - + name: report diff --git a/MAINTAINERS b/MAINTAINERS index f8c8f682edf6..df8f6b31f2f8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1769,6 +1769,7 @@ M: Suren Baghdasaryan L: linux-kernel@vger.kernel.org S: Supported T: git git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git +F: Documentation/netlink/specs/binder.yaml F: drivers/android/ =20 ANDROID GOLDFISH PIC DRIVER diff --git a/drivers/android/Kconfig b/drivers/android/Kconfig index 5b3b8041f827..75af3cf472c8 100644 --- a/drivers/android/Kconfig +++ b/drivers/android/Kconfig @@ -4,6 +4,7 @@ menu "Android" config ANDROID_BINDER_IPC bool "Android Binder IPC Driver" depends on MMU + depends on NET default n help Binder is used in Android for both communication between processes, diff --git a/drivers/android/Makefile b/drivers/android/Makefile index c5d47be0276c..f422f91e026b 100644 --- a/drivers/android/Makefile +++ b/drivers/android/Makefile @@ -2,5 +2,5 @@ ccflags-y +=3D -I$(src) # needed for trace events =20 obj-$(CONFIG_ANDROID_BINDERFS) +=3D binderfs.o -obj-$(CONFIG_ANDROID_BINDER_IPC) +=3D binder.o binder_alloc.o +obj-$(CONFIG_ANDROID_BINDER_IPC) +=3D binder.o binder_alloc.o binder_netli= nk.o obj-$(CONFIG_ANDROID_BINDER_ALLOC_KUNIT_TEST) +=3D tests/ diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 95aa1fae53e2..0d37eca514f9 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -74,6 +74,7 @@ =20 #include =20 +#include "binder_netlink.h" #include "binder_internal.h" #include "binder_trace.h" =20 @@ -2993,6 +2994,67 @@ static void binder_set_txn_from_error(struct binder_= transaction *t, int id, binder_thread_dec_tmpref(from); } =20 +/** + * binder_netlink_report() - report a transaction failure via netlink + * @proc: the binder proc sending the transaction + * @t: the binder transaction that failed + * @data_size: the user provided data size for the transaction + * @error: enum binder_driver_return_protocol returned to sender + */ +static void binder_netlink_report(struct binder_proc *proc, + struct binder_transaction *t, + u32 data_size, + u32 error) +{ + const char *context =3D proc->context->name; + struct sk_buff *skb; + void *hdr; + + if (!genl_has_listeners(&binder_nl_family, &init_net, + BINDER_NLGRP_REPORT)) + return; + + skb =3D genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) + return; + + hdr =3D genlmsg_put(skb, 0, 0, &binder_nl_family, 0, BINDER_CMD_REPORT); + if (!hdr) + goto free_skb; + + if (nla_put_u32(skb, BINDER_A_REPORT_ERROR, error) || + nla_put_string(skb, BINDER_A_REPORT_CONTEXT, context) || + nla_put_u32(skb, BINDER_A_REPORT_FROM_PID, t->from_pid) || + nla_put_u32(skb, BINDER_A_REPORT_FROM_TID, t->from_tid)) + goto cancel_skb; + + if (t->to_proc && + nla_put_u32(skb, BINDER_A_REPORT_TO_PID, t->to_proc->pid)) + goto cancel_skb; + + if (t->to_thread && + nla_put_u32(skb, BINDER_A_REPORT_TO_TID, t->to_thread->pid)) + goto cancel_skb; + + if (t->is_reply && nla_put_flag(skb, BINDER_A_REPORT_IS_REPLY)) + goto cancel_skb; + + if (nla_put_u32(skb, BINDER_A_REPORT_FLAGS, t->flags) || + nla_put_u32(skb, BINDER_A_REPORT_CODE, t->code) || + nla_put_u32(skb, BINDER_A_REPORT_DATA_SIZE, data_size)) + goto cancel_skb; + + genlmsg_end(skb, hdr); + genlmsg_multicast(&binder_nl_family, skb, 0, BINDER_NLGRP_REPORT, + GFP_KERNEL); + return; + +cancel_skb: + genlmsg_cancel(skb, hdr); +free_skb: + nlmsg_free(skb); +} + static void binder_transaction(struct binder_proc *proc, struct binder_thread *thread, struct binder_transaction_data *tr, int reply, @@ -3679,10 +3741,13 @@ static void binder_transaction(struct binder_proc *= proc, return_error_line =3D __LINE__; goto err_copy_data_failed; } - if (t->buffer->oneway_spam_suspect) + if (t->buffer->oneway_spam_suspect) { tcomplete->type =3D BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT; - else + binder_netlink_report(proc, t, tr->data_size, + BR_ONEWAY_SPAM_SUSPECT); + } else { tcomplete->type =3D BINDER_WORK_TRANSACTION_COMPLETE; + } =20 if (reply) { binder_enqueue_thread_work(thread, tcomplete); @@ -3730,8 +3795,11 @@ static void binder_transaction(struct binder_proc *p= roc, * process and is put in a pending queue, waiting for the target * process to be unfrozen. */ - if (return_error =3D=3D BR_TRANSACTION_PENDING_FROZEN) + if (return_error =3D=3D BR_TRANSACTION_PENDING_FROZEN) { tcomplete->type =3D BINDER_WORK_TRANSACTION_PENDING; + binder_netlink_report(proc, t, tr->data_size, + return_error); + } binder_enqueue_thread_work(thread, tcomplete); if (return_error && return_error !=3D BR_TRANSACTION_PENDING_FROZEN) @@ -3789,6 +3857,8 @@ static void binder_transaction(struct binder_proc *pr= oc, binder_dec_node(target_node, 1, 0); binder_dec_node_tmpref(target_node); } + + binder_netlink_report(proc, t, tr->data_size, return_error); kfree(t); binder_stats_deleted(BINDER_STAT_TRANSACTION); err_alloc_t_failed: @@ -7067,12 +7137,19 @@ static int __init binder_init(void) } } =20 - ret =3D init_binderfs(); + ret =3D genl_register_family(&binder_nl_family); if (ret) goto err_init_binder_device_failed; =20 + ret =3D init_binderfs(); + if (ret) + goto err_init_binderfs_failed; + return ret; =20 +err_init_binderfs_failed: + genl_unregister_family(&binder_nl_family); + err_init_binder_device_failed: hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { misc_deregister(&device->miscdev); diff --git a/drivers/android/binder_netlink.c b/drivers/android/binder_netl= ink.c new file mode 100644 index 000000000000..f62fbca3143c --- /dev/null +++ b/drivers/android/binder_netlink.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cl= ause) +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/binder.yaml */ +/* YNL-GEN kernel source */ + +#include +#include + +#include "binder_netlink.h" + +#include +#include + +/* Ops table for binder */ +static const struct genl_split_ops binder_nl_ops[] =3D { +}; + +static const struct genl_multicast_group binder_nl_mcgrps[] =3D { + [BINDER_NLGRP_REPORT] =3D { "report", }, +}; + +struct genl_family binder_nl_family __ro_after_init =3D { + .name =3D BINDER_FAMILY_NAME, + .version =3D BINDER_FAMILY_VERSION, + .netnsok =3D true, + .parallel_ops =3D true, + .module =3D THIS_MODULE, + .split_ops =3D binder_nl_ops, + .n_split_ops =3D ARRAY_SIZE(binder_nl_ops), + .mcgrps =3D binder_nl_mcgrps, + .n_mcgrps =3D ARRAY_SIZE(binder_nl_mcgrps), +}; diff --git a/drivers/android/binder_netlink.h b/drivers/android/binder_netl= ink.h new file mode 100644 index 000000000000..f78b8ec54c53 --- /dev/null +++ b/drivers/android/binder_netlink.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cl= ause) */ +/* 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 +#include + +#include +#include + +enum { + BINDER_NLGRP_REPORT, +}; + +extern struct genl_family binder_nl_family; + +#endif /* _LINUX_BINDER_GEN_H */ diff --git a/include/uapi/linux/android/binder_netlink.h b/include/uapi/lin= ux/android/binder_netlink.h new file mode 100644 index 000000000000..b218f96d6668 --- /dev/null +++ b/include/uapi/linux/android/binder_netlink.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cl= ause) */ +/* 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 + +enum { + BINDER_A_REPORT_ERROR =3D 1, + BINDER_A_REPORT_CONTEXT, + BINDER_A_REPORT_FROM_PID, + BINDER_A_REPORT_FROM_TID, + BINDER_A_REPORT_TO_PID, + BINDER_A_REPORT_TO_TID, + BINDER_A_REPORT_IS_REPLY, + BINDER_A_REPORT_FLAGS, + BINDER_A_REPORT_CODE, + BINDER_A_REPORT_DATA_SIZE, + + __BINDER_A_REPORT_MAX, + BINDER_A_REPORT_MAX =3D (__BINDER_A_REPORT_MAX - 1) +}; + +enum { + BINDER_CMD_REPORT =3D 1, + + __BINDER_CMD_MAX, + BINDER_CMD_MAX =3D (__BINDER_CMD_MAX - 1) +}; + +#define BINDER_MCGRP_REPORT "report" + +#endif /* _UAPI_LINUX_ANDROID_BINDER_NETLINK_H */ --=20 2.50.1.470.g6ba607880d-goog