platform/chrome: Fix a possible UAF via revocable
Subsystems (6)
Tzung-Bi Shih
maintainerv2 cover platform/chrome: Fix a possible UAF via revocable
This is a follow-up series of [1]. It tries to fix a possible UAF in the fops of cros_ec_chardev after the underlying protocol device has gone by using revocable.
The 1st patch introduces the revocable which is an implementation of ideas from the talk [2].
The 2nd and 3rd patches add test cases for revocable in Kunit and selftest.
The 4th patch converts existing protocol devices to resource providers of cros_ec_device.
The 5th patch converts cros_ec_chardev to a resource consumer of cros_ec_device to fix the UAF.
[1] https://lore.kernel.org/chrome-platform/[email protected]/
[2] https://lpc.events/event/17/contributions/1627/
v2:
- Rename "ref_proxy" -> "revocable".
- Add test cases in Kunit and selftest.
v1: https://lore.kernel.org/chrome-platform/[email protected]/
Tzung-Bi Shih (5):
revocable: Revocable resource management
revocable: Add Kunit test cases
selftests: revocable: Add kselftest cases
platform/chrome: Protect cros_ec_device lifecycle with revocable platform/chrome: cros_ec_chardev: Consume cros_ec_device via revocable
.../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 151 ++++++++++++ MAINTAINERS | 9 + drivers/base/Kconfig | 8 + drivers/base/Makefile | 5 +- drivers/base/revocable.c | 229 ++++++++++++++++++ drivers/base/revocable_test.c | 110 +++++++++ drivers/platform/chrome/cros_ec_chardev.c | 124 +++++++--- drivers/platform/chrome/cros_ec_i2c.c | 5 + drivers/platform/chrome/cros_ec_ishtp.c | 5 + drivers/platform/chrome/cros_ec_lpc.c | 5 + drivers/platform/chrome/cros_ec_rpmsg.c | 5 + drivers/platform/chrome/cros_ec_spi.c | 4 + drivers/platform/chrome/cros_ec_uart.c | 5 + include/linux/platform_data/cros_ec_proto.h | 4 + include/linux/revocable.h | 37 +++ tools/testing/selftests/Makefile | 1 + .../selftests/drivers/base/revocable/Makefile | 7 + .../drivers/base/revocable/revocable_test.c | 116 +++++++++ .../drivers/base/revocable/test-revocable.sh | 39 +++ .../base/revocable/test_modules/Makefile | 10 + .../revocable/test_modules/revocable_test.c | 188 ++++++++++++++ 22 files changed, 1027 insertions(+), 41 deletions(-) create mode 100644 Documentation/driver-api/driver-model/revocable.rst create mode 100644 drivers/base/revocable.c create mode 100644 drivers/base/revocable_test.c create mode 100644 include/linux/revocable.h create mode 100644 tools/testing/selftests/drivers/base/revocable/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/revocable_test.c create mode 100755 tools/testing/selftests/drivers/base/revocable/test-revocable.sh create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/revocable_test.c
Signature · 1 line
2.51.0.rc1.167.g924127e9c0-goog
Tzung-Bi Shih
maintainerPATCH v2 1/5 revocable: Revocable resource management
Some resources can be removed asynchronously, for example, resources provided by a hot-pluggable device like USB. When holding a reference to such a resource, it's possible for the resource to be removed and its memory freed, leading to use-after-free errors on subsequent access.
Introduce the revocable to establish weak references to such resources. It allows a resource consumer to safely attempt to access a resource that might be freed at any time by the resource provider.
The implementation uses a provider/consumer model built on Sleepable RCU (SRCU) to guarantee safe memory access:
- A resource provider allocates a struct revocable_provider and initializes it with a pointer to the resource.
- A resource consumer that wants to access the resource allocates a struct revocable which holds a reference to the provider.
- To access the resource, the consumer uses revocable_try_access(). This function enters an SRCU read-side critical section and returns the pointer to the resource. If the provider has already freed the resource, it returns NULL. After use, the consumer calls revocable_release() to exit the SRCU critical section. The REVOCABLE() is a convenient helper for doing that.
- When the provider needs to remove the resource, it calls revocable_provider_free(). This function sets the internal resource pointer to NULL and then calls synchronize_srcu() to wait for all current readers to finish before the resource can be completely torn down.
---
v2:
- Rename "ref_proxy" -> "revocable".
- Add introduction in kernel-doc format in revocable.c.
- Add MAINTAINERS entry.
- Add copyright.
- Move from lib/ to drivers/base/.
- EXPORT_SYMBOL() -> EXPORT_SYMBOL_GPL().
- Add Documentation/.
- Rename _get() -> try_access(); _put() -> release().
- Fix a sparse warning by removing the redundant __rcu annotations.
- Fix a sparse warning by adding __acquires() and __releases() annotations.
v1: https://lore.kernel.org/chrome-platform/[email protected]
.../driver-api/driver-model/index.rst | 1 + .../driver-api/driver-model/revocable.rst | 151 ++++++++++++ MAINTAINERS | 7 + drivers/base/Makefile | 2 +- drivers/base/revocable.c | 229 ++++++++++++++++++ include/linux/revocable.h | 37 +++ 6 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 Documentation/driver-api/driver-model/revocable.rst create mode 100644 drivers/base/revocable.c create mode 100644 include/linux/revocable.h
Documentation/driver-api/driver-model/index.rst +1 -0
@@ -14,6 +14,7 @@ Driver Model overview platform porting + revocable .. only:: subproject and html
Documentation/driver-api/driver-model/revocable.rst new file +151 -0
151 lines not on this page · raw message
MAINTAINERS +7 -0
@@ -21663,6 +21663,13 @@ F: include/uapi/linux/rseq.h F: kernel/rseq.c F: tools/testing/selftests/rseq/ +REVOCABLE RESOURCE MANAGEMENT +M: Tzung-Bi Shih <[email protected]> +L: [email protected] +S: Maintained +F: drivers/base/revocable.c +F: include/linux/revocable.h + RFKILL M: Johannes Berg <[email protected]> L: [email protected]
drivers/base/Makefile +1 -1
@@ -6,7 +6,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ cpu.o firmware.o init.o map.o devres.o \ attribute_container.o transport_class.o \ topology.o container.o property.o cacheinfo.o \ - swnode.o faux.o + swnode.o faux.o revocable.o obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/
drivers/base/revocable.c new file +229 -0
229 lines not on this page · raw message
include/linux/revocable.h new file +37 -0
@@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2025 Google LLC +*/ + +#ifndef __LINUX_REVOCABLE_H +#define __LINUX_REVOCABLE_H + +#include <linux/cleanup.h> + +struct device ; +struct revocable ; +struct revocable_provider ; + +struct revocable_provider *revocable_provider_alloc (void *res); +void revocable_provider_free (struct revocable_provider *rp); +struct revocable_provider *devm_revocable_provider_alloc (struct device *dev, +void *res); + +struct revocable *revocable_alloc (struct revocable_provider *rp); +void revocable_free (struct revocable *rev); +void *revocable_try_access(struct revocable *rev) __acquires(&rev->rp->srcu); +void revocable_release(struct revocable *rev) __releases(&rev->rp->srcu); + +DEFINE_FREE(revocable, struct revocable *, if (_T) revocable_release(_T)) + +#define _REVOCABLE (_rev, _label, _res) \ + for (struct revocable *__UNIQUE_ID(name) __free(revocable) = _rev; \ + (_res = revocable_try_access(_rev)) || true; ({ goto _label; })) \ + if (0) { \ +_label: \ + break; \ + } else + +#define REVOCABLE (_rev, _res) _REVOCABLE(_rev, __UNIQUE_ID(label), _res) + +#endif /* __LINUX_REVOCABLE_H */
Signature · 1 line
2.51.0.rc1.167.g924127e9c0-goog
Tzung-Bi Shih
maintainerPATCH v2 2/5 revocable: Add Kunit test cases
In reply to Tzung-Bi ShihAdd Kunit test cases for the revocable API.
The test cases cover the following scenarios:
- Basic: Verifies that a consumer can successfully access the resource provided via the provider.
- Revocation: Verifies that after the provider revokes the resource, the consumer correctly receives a NULL pointer on a subsequent access.
- Macro: Same as "Revocation" but uses the REVOCABLE().
A way to run the test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
revocable_test---
v2:
- New in the series.
MAINTAINERS | 1 + drivers/base/Kconfig | 8 +++ drivers/base/Makefile | 3 + drivers/base/revocable_test.c | 110 ++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 drivers/base/revocable_test.c
MAINTAINERS +1 -0
@@ -21668,6 +21668,7 @@ M: Tzung-Bi Shih <[email protected]> L: [email protected] S: Maintained F: drivers/base/revocable.c +F: drivers/base/revocable_test.c F: include/linux/revocable.h RFKILL
drivers/base/Kconfig +8 -0
@@ -244,3 +244,11 @@ config FW_DEVLINK_SYNC_STATE_TIMEOUT work on. endmenu + +# Kunit test cases +config REVOCABLE_KUNIT_TEST + tristate "Kunit tests for revocable" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Kunit tests for the revocable API.
drivers/base/Makefile +3 -0
@@ -35,3 +35,6 @@ ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG # define_trace.h needs to know how to find our header CFLAGS_trace.o := -I$(src) obj-$(CONFIG_TRACING) += trace.o + +# Kunit test cases +obj-$(CONFIG_REVOCABLE_KUNIT_TEST) += revocable_test.o
drivers/base/revocable_test.c new file +110 -0
110 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.167.g924127e9c0-goog
Tzung-Bi Shih
maintainerPATCH v2 3/5 selftests: revocable: Add kselftest cases
In reply to Tzung-Bi ShihAdd kselftest cases for the revocable API.
The test consists of three parts:
- A kernel module (revocable_test.ko) that creates a debugfs interface with /provider and /consumer files.
- A user-space C program (revocable_test) that uses the kselftest harness to interact with the debugfs files.
- An orchestrating shell script (test-revocable.sh) that loads the module, runs the C program, and unloads the module.
The test cases cover the following scenarios:
- Basic: Verifies that a consumer can successfully access the resource provided via the provider.
- Revocation: Verifies that after the provider revokes the resource, the consumer correctly receives a NULL pointer on a subsequent access.
- Macro: Same as "Revocation" but uses the REVOCABLE().
---
v2:
- New in the series.
MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + .../selftests/drivers/base/revocable/Makefile | 7 + .../drivers/base/revocable/revocable_test.c | 116 +++++++++++ .../drivers/base/revocable/test-revocable.sh | 39 ++++ .../base/revocable/test_modules/Makefile | 10 + .../revocable/test_modules/revocable_test.c | 188 ++++++++++++++++++ 7 files changed, 362 insertions(+) create mode 100644 tools/testing/selftests/drivers/base/revocable/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/revocable_test.c create mode 100755 tools/testing/selftests/drivers/base/revocable/test-revocable.sh create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/Makefile create mode 100644 tools/testing/selftests/drivers/base/revocable/test_modules/revocable_test.c
MAINTAINERS +1 -0
@@ -21670,6 +21670,7 @@ S: Maintained F: drivers/base/revocable.c F: drivers/base/revocable_test.c F: include/linux/revocable.h +F: tools/testing/selftests/drivers/base/revocable/ RFKILL M: Johannes Berg <[email protected]>
tools/testing/selftests/Makefile +1 -0
@@ -17,6 +17,7 @@ TARGETS += damon TARGETS += devices/error_logs TARGETS += devices/probe TARGETS += dmabuf-heaps +TARGETS += drivers/base/revocable TARGETS += drivers/dma-buf TARGETS += drivers/ntsync TARGETS += drivers/s390x/uvdevice
tools/testing/selftests/drivers/base/revocable/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_MODS_DIR := test_modules +TEST_GEN_PROGS_EXTENDED := revocable_test +TEST_PROGS := test-revocable.sh + +include ../../../lib.mk
tools/testing/selftests/drivers/base/revocable/revocable_test.c new file +116 -0
116 lines not on this page · raw message
tools/testing/selftests/drivers/base/revocable/test-revocable.sh new file +39 -0
@@ -0,0 +1,39 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +mod_name="revocable_test" +ksft_fail=1 +ksft_skip=4 + +if [ "$(id -u)" -ne 0 ]; then + echo "$0: Must be run as root" + exit "$ksft_skip" +fi + +if ! which insmod > /dev/null 2>&1; then + echo "$0: Need insmod" + exit "$ksft_skip" +fi + +if ! which rmmod > /dev/null 2>&1; then + echo "$0: Need rmmod" + exit "$ksft_skip" +fi + +insmod test_modules/"${mod_name}".ko + +if [ ! -d /sys/kernel/debug/revocable_test/ ]; then + mount -t debugfs none /sys/kernel/debug/ + + if [ ! -d /sys/kernel/debug/revocable_test/ ]; then + echo "$0: Error mounting debugfs" + exit "$ksft_fail" + fi +fi + +./revocable_test +ret=$? + +rmmod "${mod_name}" + +exit "${ret}"
tools/testing/selftests/drivers/base/revocable/test_modules/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= /lib/modules/$(shell uname -r)/build + +obj-m += revocable_test.o + +all: + $(Q)$(MAKE) -C $(KDIR) M=$(TESTMODS_DIR) + +clean: + $(Q)$(MAKE) -C $(KDIR) M=$(TESTMODS_DIR) clean
tools/testing/selftests/drivers/base/revocable/test_modules/revocable_test.c new file +188 -0
188 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.167.g924127e9c0-goog
Tzung-Bi Shih
maintainerPATCH v2 4/5 platform/chrome: Protect cros_ec_device lifecycle with revocable
In reply to Tzung-Bi ShihThe cros_ec_device can be unregistered when the underlying device is removed. Other kernel drivers that interact with the EC may hold a pointer to the cros_ec_device, creating a risk of a use-after-free error if the EC device is removed while still being referenced.
To prevent this, leverage the revocable and convert the underlying device drivers to resource providers of cros_ec_device.
---
v2:
- Rename "ref_proxy" -> "revocable".
v1: https://lore.kernel.org/chrome-platform/[email protected]
drivers/platform/chrome/cros_ec_i2c.c | 5 +++++ drivers/platform/chrome/cros_ec_ishtp.c | 5 +++++ drivers/platform/chrome/cros_ec_lpc.c | 5 +++++ drivers/platform/chrome/cros_ec_rpmsg.c | 5 +++++ drivers/platform/chrome/cros_ec_spi.c | 4 ++++ drivers/platform/chrome/cros_ec_uart.c | 5 +++++ include/linux/platform_data/cros_ec_proto.h | 4 ++++ 7 files changed, 33 insertions(+)
drivers/platform/chrome/cros_ec_i2c.c +5 -0
@@ -12,6 +12,7 @@#include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_device.h> +#include <linux/revocable.h> #include <linux/slab.h> #include "cros_ec.h"
@@ -296,6 +297,10 @@ static int cros_ec_i2c_probe(struct i2c_client *client)if (!ec_dev)return -ENOMEM ; + ec_dev->revocable_provider =devm_revocable_provider_alloc (dev, ec_dev); +if (!ec_dev->revocable_provider) +return -ENOMEM ; +i2c_set_clientdata (client, ec_dev); ec_dev->dev = dev; ec_dev->priv = client;
drivers/platform/chrome/cros_ec_ishtp.c +5 -0
@@ -12,6 +12,7 @@#include <linux/pci.h> #include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> +#include <linux/revocable.h> #include <linux/intel-ish-client-if.h> #include "cros_ec.h"
@@ -547,6 +548,10 @@ static int cros_ec_dev_init(struct ishtp_cl_data *client_data)if (!ec_dev)return -ENOMEM ; + ec_dev->revocable_provider =devm_revocable_provider_alloc (dev, ec_dev); +if (!ec_dev->revocable_provider) +return -ENOMEM ; + client_data->ec_dev = ec_dev; dev->driver_data = ec_dev;
drivers/platform/chrome/cros_ec_lpc.c +5 -0
@@ -23,6 +23,7 @@#include <linux/platform_device.h> #include <linux/printk.h> #include <linux/reboot.h> +#include <linux/revocable.h> #include <linux/suspend.h> #include "cros_ec.h"
@@ -641,6 +642,10 @@ static int cros_ec_lpc_probe(struct platform_device *pdev)if (!ec_dev)return -ENOMEM ; + ec_dev->revocable_provider =devm_revocable_provider_alloc (dev, ec_dev); +if (!ec_dev->revocable_provider) +return -ENOMEM ; +platform_set_drvdata (pdev, ec_dev); ec_dev->dev = dev; ec_dev->phys_name =dev_name (dev);
drivers/platform/chrome/cros_ec_rpmsg.c +5 -0
@@ -10,6 +10,7 @@#include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_device.h> +#include <linux/revocable.h> #include <linux/rpmsg.h> #include <linux/slab.h>
@@ -220,6 +221,10 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)if (!ec_dev)return -ENOMEM ; + ec_dev->revocable_provider =devm_revocable_provider_alloc (dev, ec_dev); +if (!ec_dev->revocable_provider) +return -ENOMEM ; + ec_rpmsg =devm_kzalloc (dev,sizeof (*ec_rpmsg),GFP_KERNEL );if (!ec_rpmsg)return -ENOMEM ;
drivers/platform/chrome/cros_ec_spi.c +4 -0
@@ -10,6 +10,7 @@#include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_device.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/spi/spi.h> #include <uapi/linux/sched/types.h>
@@ -752,6 +753,9 @@ static int cros_ec_spi_probe(struct spi_device *spi) ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL );if (!ec_dev)return -ENOMEM ; + ec_dev->revocable_provider =devm_revocable_provider_alloc (dev, ec_dev); +if (!ec_dev->revocable_provider) +return -ENOMEM ;/* Check for any DT properties */ cros_ec_spi_dt_probe (ec_spi, dev);
drivers/platform/chrome/cros_ec_uart.c +5 -0
@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/platform_data/cros_ec_proto.h> +#include <linux/revocable.h> #include <linux/serdev.h> #include <linux/slab.h> #include <uapi/linux/sched/types.h>
@@ -263,6 +264,10 @@ static int cros_ec_uart_probe(struct serdev_device *serdev)if (!ec_dev)return -ENOMEM ; + ec_dev->revocable_provider =devm_revocable_provider_alloc (dev, ec_dev); +if (!ec_dev->revocable_provider) +return -ENOMEM ; +serdev_device_set_drvdata (serdev, ec_dev);init_waitqueue_head (&ec_uart->response.wait_queue);
include/linux/platform_data/cros_ec_proto.h +4 -0
@@ -12,6 +12,7 @@#include <linux/lockdep_types.h> #include <linux/mutex.h> #include <linux/notifier.h> +#include <linux/revocable.h> #include <linux/platform_data/cros_ec_commands.h>
@@ -158,6 +159,7 @@ struct cros_ec_command {* @pd: The platform_device used by the mfd driver to interface with the * PD behind an EC. * @panic_notifier: EC panic notifier. +* @revocable_provider: The revocable_provider to this device. */ struct cros_ec_device {/* These are used by other drivers that want to talk to the EC */
@@ -203,6 +205,8 @@ struct cros_ec_device {struct platform_device *pd;struct blocking_notifier_head panic_notifier; + +struct revocable_provider *revocable_provider; };/**
Signature · 1 line
2.51.0.rc1.167.g924127e9c0-goog
Tzung-Bi Shih
maintainerPATCH v2 5/5 platform/chrome: cros_ec_chardev: Consume cros_ec_device via revocable
In reply to Tzung-Bi ShihThe cros_ec_chardev driver provides a character device interface to the ChromeOS EC. A file handle to this device can remain open in userspace even if the underlying EC device is removed.
This creates a classic use-after-free vulnerability. Any file operation (ioctl, release, etc.) on the open handle after the EC device has gone would access a stale pointer, leading to a system crash.
To prevent this, leverage the revocable and convert cros_ec_chardev to a resource consumer of cros_ec_device.
---
v2:
- Rename "ref_proxy" -> "revocable".
- Fix a sparse warning by removing the redundant __rcu annotation.
v1: https://lore.kernel.org/chrome-platform/[email protected]
drivers/platform/chrome/cros_ec_chardev.c | 124 +++++++++++++++------- 1 file changed, 84 insertions(+), 40 deletions(-)
drivers/platform/chrome/cros_ec_chardev.c +84 -40
@@ -22,6 +22,7 @@#include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_device.h> #include <linux/poll.h> +#include <linux/revocable.h> #include <linux/slab.h> #include <linux/types.h> #include <linux/uaccess.h>
@@ -32,7 +33,7 @@#define CROS_MAX_EVENT_LEN PAGE_SIZEstruct chardev_priv { -struct cros_ec_device *ec_dev; +struct revocable *ec_dev_rev;struct notifier_block notifier;wait_queue_head_t wait_event;unsigned long event_mask;
@@ -55,6 +56,7 @@ static int ec_get_version(struct chardev_priv *priv, char *str, int maxlen) };struct ec_response_get_version *resp;struct cros_ec_command *msg; +struct cros_ec_device *ec_dev;int ret; msg =kzalloc (sizeof (*msg) +sizeof (*resp),GFP_KERNEL );
@@ -64,12 +66,19 @@ static int ec_get_version(struct chardev_priv *priv, char *str, int maxlen) msg->command =EC_CMD_GET_VERSION + priv->cmd_offset; msg->insize =sizeof (*resp); - ret =cros_ec_cmd_xfer_status (priv->ec_dev, msg); -if (ret <0 ) { -snprintf (str, maxlen, -"Unknown EC version, returned error: %d\n" , - msg->result); - goto exit; + REVOCABLE(priv->ec_dev_rev, ec_dev) { +if (!ec_dev) { + ret = -ENODEV ; + goto exit; + } + + ret =cros_ec_cmd_xfer_status (ec_dev, msg); +if (ret <0 ) { +snprintf (str, maxlen, +"Unknown EC version, returned error: %d\n" , + msg->result); + goto exit; + } } resp = (struct ec_response_get_version *)msg->data;
@@ -92,22 +101,30 @@ static int cros_ec_chardev_mkbp_event(struct notifier_block *nb, { struct chardev_priv *priv = container_of(nb, struct chardev_priv, notifier); -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct ec_event *event; -unsigned long event_bit =1 << ec_dev->event_data.event_type; -int total_size =sizeof (*event) + ec_dev->event_size; +unsigned long event_bit; +int total_size; + + REVOCABLE(priv->ec_dev_rev, ec_dev) { +if (!ec_dev) +return NOTIFY_DONE ; + + event_bit =1 << ec_dev->event_data.event_type; + total_size =sizeof (*event) + ec_dev->event_size; -if (!(event_bit & priv->event_mask) || - (priv->event_len + total_size) >CROS_MAX_EVENT_LEN ) -return NOTIFY_DONE ; +if (!(event_bit & priv->event_mask) || + (priv->event_len + total_size) >CROS_MAX_EVENT_LEN ) +return NOTIFY_DONE ; - event =kzalloc (total_size,GFP_KERNEL ); -if (!event) -return NOTIFY_DONE ; + event =kzalloc (total_size,GFP_KERNEL ); +if (!event) +return NOTIFY_DONE ; - event->size = ec_dev->event_size; - event->event_type = ec_dev->event_data.event_type; -memcpy (event->data, &ec_dev->event_data.data, ec_dev->event_size); + event->size = ec_dev->event_size; + event->event_type = ec_dev->event_data.event_type; +memcpy (event->data, &ec_dev->event_data.data, ec_dev->event_size); + }spin_lock (&priv->wait_event.lock);list_add_tail (&event->node, &priv->events);
@@ -166,7 +183,12 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)if (!priv)return -ENOMEM ; - priv->ec_dev = ec_dev; + priv->ec_dev_rev =revocable_alloc (ec_dev->revocable_provider); +if (!priv->ec_dev_rev) { + ret = -ENOMEM ; + goto err; + } + priv->cmd_offset = ec->cmd_offset; filp->private_data = priv;INIT_LIST_HEAD (&priv->events);
@@ -178,9 +200,14 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp) &priv->notifier);if (ret) {dev_err (ec_dev->dev,"failed to register event notifier\n" ); -kfree (priv); + goto err; } +return 0 ; +err: +if (priv->ec_dev_rev) +revocable_free (priv->ec_dev_rev); +kfree (priv);return ret; }
@@ -251,11 +278,15 @@ static ssize_t cros_ec_chardev_read(struct file *filp, char __user *buffer,static int cros_ec_chardev_release (struct inode *inode,struct file *filp) {struct chardev_priv *priv = filp->private_data; -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct ec_event *event, *e; -blocking_notifier_chain_unregister (&ec_dev->event_notifier, - &priv->notifier); + REVOCABLE(priv->ec_dev_rev, ec_dev) { +if (ec_dev) +blocking_notifier_chain_unregister (&ec_dev->event_notifier, + &priv->notifier); + } +revocable_free (priv->ec_dev_rev);list_for_each_entry_safe (event, e, &priv->events, node) {list_del (&event->node);
@@ -273,6 +304,7 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *a {struct cros_ec_command *s_cmd;struct cros_ec_command u_cmd; +struct cros_ec_device *ec_dev;long ret;if (copy_from_user (&u_cmd, arg,sizeof (u_cmd)))
@@ -299,10 +331,17 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *a } s_cmd->command += priv->cmd_offset; - ret =cros_ec_cmd_xfer (priv->ec_dev, s_cmd); -/* Only copy data to userland if data was received. */ -if (ret <0 ) - goto exit; + REVOCABLE(priv->ec_dev_rev, ec_dev) { +if (!ec_dev) { + ret = -ENODEV ; + goto exit; + } + + ret =cros_ec_cmd_xfer (ec_dev, s_cmd); +/* Only copy data to userland if data was received. */ +if (ret <0 ) + goto exit; + }if (copy_to_user (arg, s_cmd,sizeof (*s_cmd) + s_cmd->insize)) ret = -EFAULT ;
@@ -313,24 +352,29 @@ static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *astatic long cros_ec_chardev_ioctl_readmem (struct chardev_priv *priv,void __user *arg) { -struct cros_ec_device *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev;struct cros_ec_readmem s_mem = { };long num; -/* Not every platform supports direct reads */ -if (!ec_dev->cmd_readmem) -return -ENOTTY ; + REVOCABLE(priv->ec_dev_rev, ec_dev) { +if (!ec_dev) +return -ENODEV ; -if (copy_from_user (&s_mem, arg,sizeof (s_mem))) -return -EFAULT ; +/* Not every platform supports direct reads */ +if (!ec_dev->cmd_readmem) +return -ENOTTY ; -if (s_mem.bytes >sizeof (s_mem.buffer)) -return -EINVAL ; +if (copy_from_user (&s_mem, arg,sizeof (s_mem))) +return -EFAULT ; - num = ec_dev->cmd_readmem (ec_dev, s_mem.offset, s_mem.bytes, - s_mem.buffer); -if (num <=0 ) -return num; +if (s_mem.bytes >sizeof (s_mem.buffer)) +return -EINVAL ; + + num = ec_dev->cmd_readmem (ec_dev, s_mem.offset, s_mem.bytes, + s_mem.buffer); +if (num <=0 ) +return num; + }if (copy_to_user ((void __user *)arg, &s_mem,sizeof (s_mem)))return -EFAULT ;
Signature · 1 line
2.51.0.rc1.167.g924127e9c0-goog