USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
|
Manages and keeps track of active USB devices. More...
#include <DeviceManager.hpp>
Public Types | |
enum | EventType { EventType::Present = 0, EventType::Insert = 1, EventType::Update = 2, EventType::Remove = 3 } |
Type of event that took place on the device. More... | |
enum | AuthorizedDefaultType { AuthorizedDefaultType::Keep = -128, AuthorizedDefaultType::Wired = -1, AuthorizedDefaultType::None = 0, AuthorizedDefaultType::All = 1, AuthorizedDefaultType::Internal = 2 } |
Defines which devices are authorized by default. More... | |
Public Member Functions | |
DeviceManager (DeviceManagerHooks &hooks) | |
Constructs new device manager object with given hooks. More... | |
DeviceManager (const DeviceManager &rhs) | |
Constructs new device manager from a given device manager. More... | |
const DeviceManager & | operator= (const DeviceManager &rhs) |
Constructs new device manager based on given device manager and assignes it to this. More... | |
virtual | ~DeviceManager () |
Default destructor. | |
virtual void | setEnumerationOnlyMode (bool state)=0 |
When a device is inserted into a system it's authorized default will stay unchanged. More... | |
virtual void | start ()=0 |
Starts the deamon for monitoring USB device events. | |
virtual void | stop ()=0 |
Stops the daemon that is monitoring USB device events. | |
virtual void | scan ()=0 |
Scans the system for USB devices. | |
virtual void | scan (const std::string &devpath)=0 |
Scan for USB devices on given path. More... | |
void | setAuthorizedDefault (AuthorizedDefaultType authorized) |
Enables you to set the default authorization of USB devices. More... | |
AuthorizedDefaultType | getAuthorizedDefault () const |
Returns current authorized default setting. More... | |
void | setRestoreControllerDeviceState (bool enabled) |
Controls whether USBGuard daemon tries to restore the device attribute values to the state before modification on shutdown. More... | |
bool | getRestoreControllerDeviceState () const |
Returns true if restore controller device state is enabled, false otherwise. More... | |
virtual std::shared_ptr< Device > | applyDevicePolicy (uint32_t id, Rule::Target target)=0 |
Applies given policy to device with given ID. More... | |
virtual void | insertDevice (std::shared_ptr< Device > device) |
Adds new device into a device map. More... | |
std::shared_ptr< Device > | removeDevice (uint32_t id) |
Removes device with given ID from device map. More... | |
std::vector< std::shared_ptr< Device > > | getDeviceList () |
Returns a copy of the list of active USB devices. More... | |
std::vector< std::shared_ptr< Device > > | getDeviceList (const Rule &query) |
Returns a copy of the list of active USB devices with rule that applies to given query. More... | |
std::shared_ptr< Device > | getDevice (uint32_t id) |
Returns device from a device map with a given ID. More... | |
std::mutex & | refDeviceMapMutex () |
Returns reference to device map mutex. More... | |
void | DeviceEvent (EventType event, std::shared_ptr< Device > device) |
Acknowledges daemon instance hooks about given event. More... | |
void | DeviceException (const std::string &message) |
Acknowledges daemon instance hooks about given exception. More... | |
Static Public Member Functions | |
static uint32_t | eventTypeToInteger (EventType event) |
Casts event to it's integer representation. More... | |
static EventType | eventTypeFromInteger (uint32_t event_integer) |
Converts integer to it's event representation. More... | |
static std::string | eventTypeToString (EventType event) |
Converts event to it's string representation. More... | |
static int32_t | authorizedDefaultTypeToInteger (AuthorizedDefaultType authorized_default) |
Casts authorized default to it's integer representation. More... | |
static AuthorizedDefaultType | authorizedDefaultTypeFromInteger (int32_t authorized_default_integer) |
Converts integer to it's authorized default representation. More... | |
static AuthorizedDefaultType | authorizedDefaultTypeFromString (const std::string &authorized_default_string) |
Converts string to it's authorized default representation. More... | |
static const std::string | authorizedDefaultTypeToString (AuthorizedDefaultType authorized_default) |
Converts authorized default to it's string representation. More... | |
static std::shared_ptr< DeviceManager > | create (DeviceManagerHooks &hooks, const std::string &backend) |
Creates UEventDeviceManager if backend is "uevent" or creates UMockDevDeviceManager if backend is "umockdev". More... | |
Manages and keeps track of active USB devices.
Devices are stored in the device map. When an event occurs on a device, the device manager aknowledges the hooks about the event.
|
strong |
Defines which devices are authorized by default.
|
strong |
usbguard::DeviceManager::DeviceManager | ( | DeviceManagerHooks & | hooks | ) |
Constructs new device manager object with given hooks.
hooks | Device manager hook that will get notified when an event or an exception occurs. |
usbguard::DeviceManager::DeviceManager | ( | const DeviceManager & | rhs | ) |
Constructs new device manager from a given device manager.
rhs | Device manager to copy. |
|
pure virtual |
Applies given policy to device with given ID.
id | ID of the device to which policy should be applied. |
target | Policy to apply. |
|
static |
Converts integer to it's authorized default representation.
authorized_default_integer | Integer that should be converted. |
runtime_error | If given integer is not a valid authorized default. |
|
static |
Converts string to it's authorized default representation.
authorized_default_string | String that should be converted. |
Exception | If given string does not contain valid authorized default. |
|
static |
Casts authorized default to it's integer representation.
authorized_default | Authorized default to cast to integer. |
|
static |
Converts authorized default to it's string representation.
authorized_default | Authorized default that should be converted. |
USBGUARD_BUG | If given authorized default is invalid. |
|
static |
Creates UEventDeviceManager
if backend is "uevent" or creates UMockDevDeviceManager
if backend is "umockdev".
umockdev - Umockdev based device manager capable of simulating devices based on umockdev-record files. Useful for testing.
hooks | Device manager hooks. |
backend | Type of device manager to create. |
UEventDeviceManager
or UMockDevDeviceManager
based on chosen backend. Exception | If given backend is invalid. |
void usbguard::DeviceManager::DeviceEvent | ( | DeviceManager::EventType | event, |
std::shared_ptr< Device > | device | ||
) |
Acknowledges daemon instance hooks about given event.
Calls dmHookDeviceEvent() on object registered as hook.
event | Event that arose. |
device | Device that is part of the event. |
void usbguard::DeviceManager::DeviceException | ( | const std::string & | message | ) |
Acknowledges daemon instance hooks about given exception.
Calls dmHookDeviceException() on object registered as hook.
message | Message of the exception. |
|
static |
|
static |
Casts event to it's integer representation.
event | Event to cast to integer. |
|
static |
Converts event to it's string representation.
event | Event that should be converted. |
USBGUARD_BUG | If given event is invalid. |
DeviceManager::AuthorizedDefaultType usbguard::DeviceManager::getAuthorizedDefault | ( | ) | const |
Returns current authorized default setting.
std::shared_ptr< Device > usbguard::DeviceManager::getDevice | ( | uint32_t | id | ) |
Returns device from a device map with a given ID.
id | ID of the device. |
Exception | If there is no device with such ID in device map. |
std::vector< std::shared_ptr< Device > > usbguard::DeviceManager::getDeviceList | ( | ) |
Returns a copy of the list of active USB devices.
std::vector< std::shared_ptr< Device > > usbguard::DeviceManager::getDeviceList | ( | const Rule & | query | ) |
Returns a copy of the list of active USB devices with rule that applies to given query.
query | Rule to apply to. |
bool usbguard::DeviceManager::getRestoreControllerDeviceState | ( | ) | const |
Returns true if restore controller device state is enabled, false otherwise.
|
virtual |
Adds new device into a device map.
Device ID will be assigned by the device manager hooks.
device | Device to be added into map of devices. |
const DeviceManager & usbguard::DeviceManager::operator= | ( | const DeviceManager & | rhs | ) |
Constructs new device manager based on given device manager and assignes it to this.
rhs | Device manager to copy. |
std::mutex& usbguard::DeviceManager::refDeviceMapMutex | ( | ) |
Returns reference to device map mutex.
std::shared_ptr< Device > usbguard::DeviceManager::removeDevice | ( | uint32_t | id | ) |
Removes device with given ID from device map.
id | ID of the device that shall be removed. |
Exception | If device with such ID does not exist in device map. |
|
pure virtual |
Scan for USB devices on given path.
devpath | Path to scan. |
void usbguard::DeviceManager::setAuthorizedDefault | ( | DeviceManager::AuthorizedDefaultType | authorized | ) |
Enables you to set the default authorization of USB devices.
authorized | authorized default to be set. |
|
pure virtual |
When a device is inserted into a system it's authorized default will stay unchanged.
state | If true, enables enumeration only mode. |
void usbguard::DeviceManager::setRestoreControllerDeviceState | ( | bool | enabled | ) |
Controls whether USBGuard daemon tries to restore the device attribute values to the state before modification on shutdown.
The USBGuard daemon modifies some attributes of controller devices like the default authorization state of new child device instances. Using this setting, you can control whether the daemon will try to restore the attribute values to the state before modification on shutdown.
enabled | Value to be set. |