add toggle for disabling newly added USB devices
authorDaniel Micay <danielmicay@gmail.com>
Tue, 16 May 2017 21:51:48 +0000 (17:51 -0400)
committerMichael Benedict <michaelbt@live.com>
Wed, 1 Jan 2020 14:49:49 +0000 (01:49 +1100)
Based on the public grsecurity patches.

drivers/usb/core/hub.c
kernel/sysctl.c

index 0ceaeca81c9208404c9a9f49f453b4598de3c15a..4b2f5cc9e0ea8b704f035c74341fdff81d9b218a 100644 (file)
@@ -39,6 +39,8 @@
 #define USB_VENDOR_GENESYS_LOGIC               0x05e3
 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND       0x01
 
+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. */
@@ -4746,6 +4748,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 88b03bf6b30a40d74d2d53dc521c4e94a2ae51ac..f4262115a5d08316d11caa0f2b87482aca1e0b93 100644 (file)
 #if defined(CONFIG_SYSCTL)
 
 /* External variables not in a header file. */
+#if IS_ENABLED(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;
@@ -875,6 +879,17 @@ static struct ctl_table kern_table[] = {
                .extra1         = &zero,
                .extra2         = &two,
        },
+#endif
+#if IS_ENABLED(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",