add toggle for disabling newly added USB devices
authorDaniel Micay <danielmicay@gmail.com>
Tue, 16 May 2017 21:51:48 +0000 (17:51 -0400)
committerCosmin Tanislav <demonsingur@gmail.com>
Mon, 22 Apr 2024 17:24:02 +0000 (20:24 +0300)
Based on the public grsecurity patches.

Change-Id: I2cbea91b351cda7d098f4e1aa73dff1acbd23cce
Signed-off-by: Daniel Micay <danielmicay@gmail.com>
drivers/usb/core/hub.c
kernel/sysctl.c

index c0d03c2ccbd55eb4042309ca3ea7d789a2529637..d50fdfa227a630ffaa916f335e0821110cd7ee24 100644 (file)
@@ -46,6 +46,8 @@
 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND       0x01
 #define HUB_QUIRK_DISABLE_AUTOSUSPEND          0x02
 
+extern int deny_new_usb;
+
 /* Protect struct usb_device->state and ->children members
  * Note: Both are also protected by ->dev.sem, except that ->state can
  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
@@ -4922,6 +4924,12 @@ static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
                        goto done;
                return;
        }
+
+       if (deny_new_usb) {
+               dev_err(&port_dev->dev, "denied insert of USB device on port %d\n", port1);
+               goto done;
+       }
+
        if (hub_is_superspeed(hub->hdev))
                unit_load = 150;
        else
index 285bf132d7e97131831a56c9dd00ba3f07aabf8f..1e321f19c9f35ee77f844c670dd3372682cd6ba1 100644 (file)
 #if defined(CONFIG_SYSCTL)
 
 /* External variables not in a header file. */
+#ifdef CONFIG_USB
+int deny_new_usb __read_mostly = 0;
+EXPORT_SYMBOL(deny_new_usb);
+#endif
 extern int suid_dumpable;
 #ifdef CONFIG_COREDUMP
 extern int core_uses_pid;
@@ -931,6 +935,18 @@ static struct ctl_table kern_table[] = {
                .extra2         = &two,
        },
 #endif
+
+#ifdef CONFIG_USB
+       {
+               .procname       = "deny_new_usb",
+               .data           = &deny_new_usb,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = proc_dointvec_minmax_sysadmin,
+               .extra1         = &zero,
+               .extra2         = &one,
+       },
+#endif
        {
                .procname       = "ngroups_max",
                .data           = &ngroups_max,