From nobody Fri Dec 27 10:10:55 2024 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 524EC198A17 for ; Mon, 9 Dec 2024 19:22:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733772179; cv=none; b=IQDO5e5HYQyWD6n+jR9yTYq7Zzz24PkONVEfd0rIK1eaBh5rSx2uAhZDCnb3eXNxuuZOoFeEU14xJV4hEm/5KfGy8ZDYmh0jiTQS0mS7yOCpVXDt7PCPXoMWtRg0TFQI8tnhf/9Ga+PoPvUu20IfqoFCg0vBwbGcD2CWrTzhzCc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733772179; c=relaxed/simple; bh=9fRocrd/U9wpjtmuV2DlKXD93yMnSkv0jrtzRqtkp3o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VO8zKZH03SNVxPy0NZT5I2r2LKG5GWPEG1yJD0A4yad8YeS1PkOrK7WSV87yprQEJRNjkzaeYNk9SsOQF/pSm4nOuBDau95rTmOo1JJ2vF0+t2eeqWn0hz2NKTPXHtLzlVhGV142EK3jpAupGM6K8QssPh0ycT7rnDusm19MdQg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=fPb4tPB9; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="fPb4tPB9" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-21661be2c2dso7905475ad.1 for ; Mon, 09 Dec 2024 11:22:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1733772177; x=1734376977; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eLFMgiWn0M/Bow/bFF/XQPoIbOEOGef03iAsZ0LfnTo=; b=fPb4tPB96vIGy1c2CeX1OFpKmBHaR/nrZyE4uottYJjh1pIZtRaaVV/YaJwCMa5Yb3 9KmHED1hBnzPQRG05b0KYzwXUn+DQ8RELJ3W21dNpvneLkB9LgZEUO0Hq42LItiRxjib hFsQDPNA4yoKQgBy/ysgeLwWjJHEKYIagxCQk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733772177; x=1734376977; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eLFMgiWn0M/Bow/bFF/XQPoIbOEOGef03iAsZ0LfnTo=; b=j1vG600DH8JPIfmXGizmS4tsyQNqHKNJWVfwJjg0r48QzapgZZ1B1bisWLKNf8xozu +KTNfpP7pd6wE85NE4V8G7AV9W4ihq5v+UGtIRAvfPp322tkC7yVF3BJMQ3ZWRf9fJAK cBCV4eWogMPSGSIs21OSenIDiz+Vc4YBvtYUjPtig6H7RxVRHk9txaCZJzHbQdK4aukD Cdc1SnTi0+PZJWBdvVmrzSeMHFCvW3V80DpwGLERzTswq5YA3j+Ngl9vmU2EO+9P5bo0 +kNGxfGRaMsJfvwsKFbazvLntzMfeCcy9y2qwhs+Zvi9notJuhMEx5YdlnPXiB+zFRNY jnBQ== X-Forwarded-Encrypted: i=1; AJvYcCX4iSgEjUH0kSPqNijGKvSrk2gkeL8LJBhxMAPfdXWNLN8q0WSRKgOSOVrys9eks81z/wezj03OtGsGwnw=@vger.kernel.org X-Gm-Message-State: AOJu0YwG6PN0NgpVj5Af5Q83yuVsMmEaCDs1BSekxPL0AGBPSlxXM8xS hjtIiA96YWXqoRN69YarS8Z/J+C4HntokmVjbydtRY9LiY9Zn8oBJonAaTan6g== X-Gm-Gg: ASbGncvyv7v4kZ0LiKimnQbYRLTJY3xOh2FqqQovAvA0wfssClRQxG/PlINaVj9lRHo +VrqzUIUHqomzgOC211OI4xmZoIMxEkF1LPOfls5WXdUJp7drfpexsm8f9PnptN/z1rpftD2jN4 xAwwB5ofrHRPRryYJNZ93oGlarbeb2udMM3Hs2dW93O1emTnqJBNFTEXaGRzLkb71sb2p5SGxQt Oo/LjKl1xrQNTLzqGFdr8R/zvROz7QkdRA/ObLwxvAeBFxJ0QHPXBFUBM4nrrjVFAWxyGn1ODIt lTXfE0O2bNB56XhMsOD/HogbxiwmQ6lhim7Y2Ow= X-Google-Smtp-Source: AGHT+IFpE1rqYXyxV/cb0cRTJ/cBbuIDYpyiymqlW3k/kc8mR99n1DUmq7L5t0l5gOCIT3D7o50sUA== X-Received: by 2002:a17:902:fc46:b0:215:83e1:99ff with SMTP id d9443c01a7336-2166a00836bmr22960905ad.27.1733772176442; Mon, 09 Dec 2024 11:22:56 -0800 (PST) Received: from li-cloudtop.c.googlers.com.com (141.161.16.34.bc.googleusercontent.com. [34.16.161.141]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21645b94602sm27363375ad.96.2024.12.09.11.22.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 11:22:55 -0800 (PST) From: Li Li To: dualli@google.com, corbet@lwn.net, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, donald.hunter@gmail.com, gregkh@linuxfoundation.org, arve@android.com, tkjos@android.com, maco@android.com, joel@joelfernandes.org, brauner@kernel.org, cmllamas@google.com, surenb@google.com, arnd@arndb.de, masahiroy@kernel.org, bagasdotme@gmail.com, horms@kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, netdev@vger.kernel.org, hridya@google.com, smoreland@google.com Cc: kernel-team@android.com Subject: [PATCH net-next v9 1/1] binder: report txn errors via generic netlink Date: Mon, 9 Dec 2024 11:22:47 -0800 Message-ID: <20241209192247.3371436-2-dualli@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241209192247.3371436-1-dualli@chromium.org> References: <20241209192247.3371436-1-dualli@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Li Li 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 --- Documentation/admin-guide/binder_genl.rst | 96 +++++++ Documentation/admin-guide/index.rst | 1 + Documentation/netlink/specs/binder_genl.yaml | 108 ++++++++ drivers/android/Kconfig | 1 + drivers/android/Makefile | 2 +- drivers/android/binder.c | 277 ++++++++++++++++++- drivers/android/binder_genl.c | 39 +++ drivers/android/binder_genl.h | 18 ++ drivers/android/binder_internal.h | 27 +- drivers/android/binder_trace.h | 35 +++ drivers/android/binderfs.c | 2 + include/uapi/linux/android/binder_genl.h | 55 ++++ 12 files changed, 655 insertions(+), 6 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/admi= n-guide/binder_genl.rst new file mode 100644 index 000000000000..b395d7a9849b --- /dev/null +++ b/Documentation/admin-guide/binder_genl.rst @@ -0,0 +1,96 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Generic Netlink for the Android Binder Driver (Binder Genl) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The Generic Netlink subsystem in the Linux kernel provides a generic way f= or +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 Gener= ic +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 should be reported by the kernel binder driver. The +driver then echoes the attributes in a reply message 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 reque= st +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 exist= ing +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 =3D 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_GENL_FAMILY_NAME); + void *data =3D recv(CTRL_CMD_NEWFAMILY); + __u16 id =3D nla(data)[CTRL_ATTR_FAMILY_ID]; + + // enable per-context binder report + send(fd, id, BINDER_GENL_CMD_SET, "binder", 0, BINDER_GENL_FLAG_FAILED= | + BINDER_GENL_FLAG_DELAYED); + + // confirm the per-context configuration + data =3D recv(fd, BINDER_GENL_CMD_REPLY); + char *context =3D nla(data)[BINDER_GENL_A_CMD_CONTEXT]; + __u32 pid =3D nla(data)[BINDER_GENL_A_CMD_PID]; + __u32 flags =3D nla(data)[BINDER_GENL_A_CMD_FLAGS]; + + // set optional per-process report, overriding the per-context one + send(fd, id, BINDER_GENL_CMD_SET, "binder", getpid(), + BINDER_GENL_FLAG_SPAM | BINDER_REPORT_OVERRIDE); + + // confirm the optional per-process configuration + data =3D recv(fd, BINDER_GENL_CMD_REPLY); + context =3D nla(data)[BINDER_GENL_A_CMD_CONTEXT]; + pid =3D nla(data)[BINDER_GENL_A_CMD_PID]; + flags =3D nla(data)[BINDER_GENL_A_CMD_FLAGS]; + + // wait and read all binder reports + while (running) { + data =3D recv(fd, BINDER_GENL_CMD_REPORT); + auto *attr =3D 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-guid= e/index.rst index e85b1adf5908..b3b5cfadffe5 100644 --- a/Documentation/admin-guide/index.rst +++ b/Documentation/admin-guide/index.rst @@ -79,6 +79,7 @@ configure specific aspects of kernel behavior to your lik= ing. aoe/index auxdisplay/index bcache + binder_genl binderfs binfmt-misc blockdev/index diff --git a/Documentation/netlink/specs/binder_genl.yaml b/Documentation/n= etlink/specs/binder_genl.yaml new file mode 100644 index 000000000000..3c6024b9a689 --- /dev/null +++ b/Documentation/netlink/specs/binder_genl.yaml @@ -0,0 +1,108 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cla= use) + +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: Define what kind of binder transactions should be reported. + entries: [ failed, delayed, spam, override ] + +attribute-sets: + - + name: cmd + doc: The supported attributes of "set" and "reply" commands + attributes: + - + name: context + type: string + doc: The binder context to enable binder genl report. + - + name: pid + type: u32 + doc: The binder proc to enable binder genl 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 genl 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: set + 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 diff --git a/drivers/android/Kconfig b/drivers/android/Kconfig index 07aa8ae0a058..e2fa620934e2 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 c9d3d0c99c25..d818447fbc4c 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_genl.o obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) +=3D binder_alloc_selftest.o diff --git a/drivers/android/binder.c b/drivers/android/binder.c index ef353ca13c35..eaa9b763ef20 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -72,6 +72,7 @@ =20 #include =20 +#include "binder_genl.h" #include "binder_internal.h" #include "binder_trace.h" =20 @@ -2990,6 +2991,150 @@ static void binder_set_txn_from_error(struct binder= _transaction *t, int id, binder_thread_dec_tmpref(from); } =20 +/** + * 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 =3D=3D 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 !=3D (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 =3D flags; + } else { + /* Set the per-process flags */ + proc =3D binder_find_proc(pid); + if (!proc) { + pr_err("Invalid binder report pid %u\n", pid); + return -EINVAL; + } + + proc->report_flags =3D 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 =3D proc->context; + + if (!context->report_portid) + return false; + + if (proc->report_flags & BINDER_GENL_FLAG_OVERRIDE) + return (proc->report_flags & mask) !=3D 0; + else + return (context->report_flags & mask) !=3D 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 er= r, + u32 pid, u32 tid, u32 to_pid, u32 to_tid, + u32 reply, + struct binder_transaction_data *tr) +{ + int ret; + struct sk_buff *skb; + void *hdr; + + trace_binder_send_report(context->name, err, pid, tid, to_pid, to_tid, + reply, tr); + + skb =3D genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + pr_err("Failed to alloc binder genl message\n"); + return; + } + + hdr =3D genlmsg_put(skb, 0, atomic_inc_return(&context->report_seq), + &binder_genl_nl_family, 0, BINDER_GENL_CMD_REPORT); + if (!hdr) + goto free_skb; + + if (nla_put_string(skb, BINDER_GENL_A_REPORT_CONTEXT, context->name) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_ERR, err) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_FROM_PID, pid) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_FROM_TID, tid) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_TO_PID, to_pid) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_TO_TID, to_tid) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_REPLY, reply) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_FLAGS, tr->flags) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_CODE, tr->code) || + nla_put_u32(skb, BINDER_GENL_A_REPORT_DATA_SIZE, tr->data_size)) + goto cancel_skb; + + genlmsg_end(skb, hdr); + + ret =3D 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, @@ -3684,10 +3829,18 @@ 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 + 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 =3D BINDER_WORK_TRANSACTION_COMPLETE; + } t->work.type =3D BINDER_WORK_TRANSACTION; =20 if (reply) { @@ -3737,8 +3890,15 @@ 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; + 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 !=3D BR_TRANSACTION_PENDING_FROZEN) @@ -3800,6 +3960,13 @@ static void binder_transaction(struct binder_proc *p= roc, binder_dec_node_tmpref(target_node); } =20 + 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" : @@ -6137,6 +6304,11 @@ static int binder_release(struct inode *nodp, struct= file *filp) =20 binder_defer_work(proc, BINDER_DEFERRED_RELEASE); =20 + if (proc->pid =3D=3D proc->context->report_portid) { + proc->context->report_portid =3D 0; + proc->context->report_flags =3D 0; + } + return 0; } =20 @@ -6335,6 +6507,87 @@ binder_defer_work(struct binder_proc *proc, enum bin= der_deferred_state defer) mutex_unlock(&binder_deferred_lock); } =20 +/** + * 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 portid; + u32 pid; + u32 flags; + void *hdr; + struct binder_device *device; + struct binder_context *context =3D NULL; + + hlist_for_each_entry(device, &binder_devices, hlist) { + if (!nla_strcmp(info->attrs[BINDER_GENL_A_CMD_CONTEXT], + device->context.name)) { + context =3D &device->context; + break; + } + } + + if (!context) { + NL_SET_ERR_MSG(info->extack, "Unknown binder context\n"); + return -EINVAL; + } + + portid =3D nlmsg_hdr(skb)->nlmsg_pid; + pid =3D nla_get_u32(info->attrs[BINDER_GENL_A_CMD_PID]); + flags =3D nla_get_u32(info->attrs[BINDER_GENL_A_CMD_FLAGS]); + + if (context->report_portid && context->report_portid !=3D portid) { + NL_SET_ERR_MSG_FMT(info->extack, + "No permission to set flags from %d\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; + } + + skb =3D genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + pr_err("Failed to alloc binder genl reply message\n"); + return -ENOMEM; + } + + hdr =3D genlmsg_iput(skb, info); + if (!hdr) + goto free_skb; + + if (nla_put_string(skb, BINDER_GENL_A_CMD_CONTEXT, context->name) || + nla_put_u32(skb, BINDER_GENL_A_CMD_PID, pid) || + 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 =3D 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, @@ -6928,6 +7181,17 @@ const struct binder_debugfs_entry binder_debugfs_ent= ries[] =3D { {} /* terminator */ }; =20 +/** + * Add a binder device to binder_devices + * @device: the new binder device to add to the global list + * + * Not reentrant as the list is not protected by any locks + */ +void binder_add_device(struct binder_device *device) +{ + hlist_add_head(&device->hlist, &binder_devices); +} + static int __init init_binder_device(const char *name) { int ret; @@ -6953,6 +7217,7 @@ static int __init init_binder_device(const char *name) } =20 hlist_add_head(&binder_device->hlist, &binder_devices); + binder_device->context.report_seq =3D (atomic_t)ATOMIC_INIT(0); =20 return ret; } @@ -7009,6 +7274,12 @@ static int __init binder_init(void) if (ret) goto err_init_binder_device_failed; =20 + ret =3D genl_register_family(&binder_genl_nl_family); + if (ret) { + pr_err("Failed to register binder genl family\n"); + goto err_init_binder_device_failed; + } + return ret; =20 err_init_binder_device_failed: diff --git a/drivers/android/binder_genl.c b/drivers/android/binder_genl.c new file mode 100644 index 000000000000..29d79bdd9649 --- /dev/null +++ b/drivers/android/binder_genl.c @@ -0,0 +1,39 @@ +// 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_genl.yaml */ +/* YNL-GEN kernel source */ + +#include +#include + +#include "binder_genl.h" + +#include + +/* BINDER_GENL_CMD_SET - do */ +static const struct nla_policy binder_genl_set_nl_policy[BINDER_GENL_A_CMD= _FLAGS + 1] =3D { + [BINDER_GENL_A_CMD_CONTEXT] =3D { .type =3D NLA_NUL_STRING, }, + [BINDER_GENL_A_CMD_PID] =3D { .type =3D NLA_U32, }, + [BINDER_GENL_A_CMD_FLAGS] =3D NLA_POLICY_MASK(NLA_U32, 0xf), +}; + +/* Ops table for binder_genl */ +static const struct genl_split_ops binder_genl_nl_ops[] =3D { + { + .cmd =3D BINDER_GENL_CMD_SET, + .doit =3D binder_genl_nl_set_doit, + .policy =3D binder_genl_set_nl_policy, + .maxattr =3D BINDER_GENL_A_CMD_FLAGS, + .flags =3D GENL_CMD_CAP_DO, + }, +}; + +struct genl_family binder_genl_nl_family __ro_after_init =3D { + .name =3D BINDER_GENL_FAMILY_NAME, + .version =3D BINDER_GENL_FAMILY_VERSION, + .netnsok =3D true, + .parallel_ops =3D true, + .module =3D THIS_MODULE, + .split_ops =3D binder_genl_nl_ops, + .n_split_ops =3D 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 000000000000..9d68c155b7c4 --- /dev/null +++ b/drivers/android/binder_genl.h @@ -0,0 +1,18 @@ +/* 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_genl.yaml */ +/* YNL-GEN kernel header */ + +#ifndef _LINUX_BINDER_GENL_GEN_H +#define _LINUX_BINDER_GENL_GEN_H + +#include +#include + +#include + +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_int= ernal.h index f8d6be682f23..cc745367a519 100644 --- a/drivers/android/binder_internal.h +++ b/drivers/android/binder_internal.h @@ -12,21 +12,35 @@ #include #include #include +#include #include #include "binder_alloc.h" #include "dbitmap.h" =20 +/** + * struct binder_context - information about a binder domain + * @binder_context_mgr_node: the context manager + * @context_mgr_node_lock: the lock protecting the above context manager= node + * @binder_context_mgr_uid: the uid of the above context manager + * @name: the name of the binder device + * @report_portid: the netlink socket to receive binder reports + * @report_flags: the categories of binder transactions that wo= uld + * be reported (see enum binder_report_flag). + * @report_seq: the seq number of the generic netlink report + */ struct binder_context { struct binder_node *binder_context_mgr_node; struct mutex context_mgr_node_lock; kuid_t binder_context_mgr_uid; const char *name; + u32 report_portid; + u32 report_flags; + atomic_t report_seq; }; =20 /** * struct binder_device - information about a binder device node - * @hlist: list of binder devices (only used for devices requeste= d via - * CONFIG_ANDROID_BINDER_DEVICES) + * @hlist: list of binder devices * @miscdev: information about a binder character device node * @context: binder context information * @binderfs_inode: This is the inode of the root dentry of the super block @@ -415,6 +429,8 @@ struct binder_ref { * @binderfs_entry: process-specific binderfs log file * @oneway_spam_detection_enabled: process enabled oneway spam detection * or not + * @report_flags: the categories of binder transactions that would + * be reported (see enum binder_genl_flag). * * Bookkeeping structure for binder processes */ @@ -453,6 +469,7 @@ struct binder_proc { spinlock_t outer_lock; struct dentry *binderfs_entry; bool oneway_spam_detection_enabled; + u32 report_flags; }; =20 /** @@ -582,4 +599,10 @@ struct binder_object { }; }; =20 +/** + * Add a binder device to binder_devices + * @device: the new binder device to add to the global list + */ +void binder_add_device(struct binder_device *device); + #endif /* _LINUX_BINDER_INTERNAL_H */ diff --git a/drivers/android/binder_trace.h b/drivers/android/binder_trace.h index fe38c6fc65d0..551b6c9d9f73 100644 --- a/drivers/android/binder_trace.h +++ b/drivers/android/binder_trace.h @@ -423,6 +423,41 @@ TRACE_EVENT(binder_return, "unknown") ); =20 +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 =3D name; + __entry->err =3D err; + __entry->pid =3D pid; + __entry->tid =3D tid; + __entry->to_pid =3D to_pid; + __entry->to_tid =3D to_tid; + __entry->reply =3D reply; + __entry->flags =3D tr->flags; + __entry->code =3D tr->code; + __entry->data_size =3D tr->data_size; + ), + TP_printk("%s: %d %d:%d -> %d:%d %s flags=3D0x08%x code=3D%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 */ =20 #undef TRACE_INCLUDE_PATH diff --git a/drivers/android/binderfs.c b/drivers/android/binderfs.c index ad1fa7abc323..bc6bae76ccaf 100644 --- a/drivers/android/binderfs.c +++ b/drivers/android/binderfs.c @@ -207,6 +207,8 @@ static int binderfs_binder_device_create(struct inode *= ref_inode, fsnotify_create(root->d_inode, dentry); inode_unlock(d_inode(root)); =20 + binder_add_device(device); + return 0; =20 err: diff --git a/include/uapi/linux/android/binder_genl.h b/include/uapi/linux/= android/binder_genl.h new file mode 100644 index 000000000000..f39b5c81e477 --- /dev/null +++ b/include/uapi/linux/android/binder_genl.h @@ -0,0 +1,55 @@ +/* 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_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 + +/* + * Define what kind of binder transactions should be reported. + */ +enum binder_genl_flag { + BINDER_GENL_FLAG_FAILED =3D 1, + BINDER_GENL_FLAG_DELAYED =3D 2, + BINDER_GENL_FLAG_SPAM =3D 4, + BINDER_GENL_FLAG_OVERRIDE =3D 8, +}; + +enum { + BINDER_GENL_A_CMD_CONTEXT =3D 1, + BINDER_GENL_A_CMD_PID, + BINDER_GENL_A_CMD_FLAGS, + + __BINDER_GENL_A_CMD_MAX, + BINDER_GENL_A_CMD_MAX =3D (__BINDER_GENL_A_CMD_MAX - 1) +}; + +enum { + BINDER_GENL_A_REPORT_CONTEXT =3D 1, + BINDER_GENL_A_REPORT_ERR, + 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 =3D (__BINDER_GENL_A_REPORT_MAX - 1) +}; + +enum { + BINDER_GENL_CMD_SET =3D 1, + BINDER_GENL_CMD_REPORT, + + __BINDER_GENL_CMD_MAX, + BINDER_GENL_CMD_MAX =3D (__BINDER_GENL_CMD_MAX - 1) +}; + +#endif /* _UAPI_LINUX_ANDROID_BINDER_GENL_H */ --=20 2.47.0.338.g60cca15819-goog