[PATCH v2 19/24] security: usbgadget: add usbgadget security policy
Sascha Hauer
s.hauer at pengutronix.de
Wed Sep 17 06:53:39 PDT 2025
USB gadget might be considered dangerous in secure environments, so add
a security policy for it.
Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
Sconfig | 1 +
common/usbgadget.c | 26 ++++++++++++++++++++++++++
drivers/usb/gadget/Sconfig | 11 +++++++++++
drivers/usb/gadget/composite.c | 4 ++++
drivers/usb/gadget/legacy/serial.c | 4 ++++
5 files changed, 46 insertions(+)
diff --git a/Sconfig b/Sconfig
index f1ae5c7a1482058d170f99c75b706f66a954a9cb..7d7657e79061f4bf200519cf1fab8810b544f97e 100644
--- a/Sconfig
+++ b/Sconfig
@@ -6,4 +6,5 @@ source "scripts/Sconfig.include"
source "security/Sconfig"
source "common/Sconfig"
+source "drivers/usb/gadget/Sconfig"
source "commands/Sconfig"
diff --git a/common/usbgadget.c b/common/usbgadget.c
index 1333eaa413eafb766dc74e37e14d5082e93879ac..dc7b3ddc808ee0b1c63d81b914ff915bcc92a863 100644
--- a/common/usbgadget.c
+++ b/common/usbgadget.c
@@ -18,6 +18,7 @@
#include <globalvar.h>
#include <magicvar.h>
#include <system-partitions.h>
+#include <security/config.h>
static int autostart;
static int nv_loaded;
@@ -81,6 +82,9 @@ int usbgadget_register(struct f_multi_opts *opts)
int ret;
struct device *dev;
+ if (!IS_ALLOWED(SCONFIG_USB_GADGET))
+ return -EPERM;
+
/*
* Creating a gadget with both DFU and Fastboot may not work.
* fastboot 1:8.1.0+r23-5 can deal with it, but dfu-util 0.9
@@ -106,6 +110,9 @@ int usbgadget_prepare_register(const struct usbgadget_funcs *funcs)
struct f_multi_opts *opts;
int ret;
+ if (!IS_ALLOWED(SCONFIG_USB_GADGET))
+ return -EPERM;
+
opts = usbgadget_prepare(funcs);
if (IS_ERR(opts))
return PTR_ERR(opts);
@@ -161,6 +168,21 @@ void usbgadget_autostart(bool enable)
usbgadget_do_autostart();
}
+static void usbgadget_sconfig_update(struct sconfig_notifier_block *nb,
+ enum security_config_option opt,
+ bool allowed)
+{
+ if (allowed) {
+ if (autostart)
+ usbgadget_do_autostart();
+ } else {
+ usb_multi_unregister();
+ usb_serial_unregister();
+ }
+}
+
+static struct sconfig_notifier_block sconfig_notifier;
+
static int usbgadget_globalvars_init(void)
{
globalvar_add_simple_bool("usbgadget.acm", &acm);
@@ -169,6 +191,10 @@ static int usbgadget_globalvars_init(void)
globalvar_add_bool("usbgadget.autostart", usbgadget_autostart_set,
&autostart, NULL);
+ sconfig_register_handler_filtered(&sconfig_notifier,
+ usbgadget_sconfig_update,
+ SCONFIG_USB_GADGET);
+
return 0;
}
coredevice_initcall(usbgadget_globalvars_init);
diff --git a/drivers/usb/gadget/Sconfig b/drivers/usb/gadget/Sconfig
new file mode 100644
index 0000000000000000000000000000000000000000..38797301e514c203f75f876fa54971846d39d292
--- /dev/null
+++ b/drivers/usb/gadget/Sconfig
@@ -0,0 +1,11 @@
+
+menu "USB Gadget Policy"
+
+config USB_GADGET
+ bool "Allow USB gadget"
+ depends on $(kconfig-enabled,USB_GADGET)
+ help
+ USB gadget support might be potentially dangerous in secure environments.
+ The DFU and fastboot protocols allow to modify system partitions.
+
+endmenu
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 98f7b5bf7fb412b77f8a926375df3f0ffbbc96da..2a9984e37ad3ec1325822eb2d328481ceb3ff906 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -15,6 +15,7 @@
#include <linux/usb/composite.h>
#include <linux/bitfield.h>
#include <linux/uuid.h>
+#include <security/config.h>
#include <asm/unaligned.h>
#include <asm/byteorder.h>
@@ -2489,6 +2490,9 @@ int usb_composite_probe(struct usb_composite_driver *driver)
{
struct usb_gadget_driver *gadget_driver;
+ if (!IS_ALLOWED(SCONFIG_USB_GADGET))
+ return -EPERM;
+
if (!driver || !driver->dev || !driver->bind)
return -EINVAL;
diff --git a/drivers/usb/gadget/legacy/serial.c b/drivers/usb/gadget/legacy/serial.c
index 913d174a917794e7cab0f8006b8fd814887029f4..700e9db3a57462e331936efd303c6614544b251a 100644
--- a/drivers/usb/gadget/legacy/serial.c
+++ b/drivers/usb/gadget/legacy/serial.c
@@ -15,6 +15,7 @@
#include <linux/usb/gadget.h>
#include <linux/usb/composite.h>
#include <linux/usb/usbserial.h>
+#include <security/config.h>
#include <asm/byteorder.h>
#include "u_serial.h"
@@ -253,6 +254,9 @@ int usb_serial_register(struct usb_serial_pdata *pdata)
{
int ret;
+ if (!IS_ALLOWED(SCONFIG_USB_GADGET))
+ return -EPERM;
+
if (usb_serial_registered) {
pr_err("USB serial gadget already registered\n");
return -EBUSY;
--
2.47.3
More information about the barebox
mailing list