2020-08-20 17:08:02 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.usbguard;
|
|
|
|
|
|
|
|
# valid policy options
|
|
|
|
policy = (types.enum [ "allow" "block" "reject" "keep" "apply-policy" ]);
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
defaultRuleFile = "/var/lib/usbguard/rules.conf";
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# decide what file to use for rules
|
2020-08-20 17:08:02 +00:00
|
|
|
ruleFile = if cfg.rules != null then pkgs.writeText "usbguard-rules" cfg.rules else defaultRuleFile;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
daemonConf = ''
|
2020-08-20 17:08:02 +00:00
|
|
|
# generated by nixos/modules/services/security/usbguard.nix
|
|
|
|
RuleFile=${ruleFile}
|
2023-07-15 17:15:38 +00:00
|
|
|
ImplicitPolicyTarget=${cfg.implicitPolicyTarget}
|
2020-08-20 17:08:02 +00:00
|
|
|
PresentDevicePolicy=${cfg.presentDevicePolicy}
|
|
|
|
PresentControllerPolicy=${cfg.presentControllerPolicy}
|
|
|
|
InsertedDevicePolicy=${cfg.insertedDevicePolicy}
|
2020-10-27 00:29:36 +00:00
|
|
|
RestoreControllerDeviceState=${boolToString cfg.restoreControllerDeviceState}
|
2020-08-20 17:08:02 +00:00
|
|
|
# this does not seem useful for endusers to change
|
|
|
|
DeviceManagerBackend=uevent
|
|
|
|
IPCAllowedUsers=${concatStringsSep " " cfg.IPCAllowedUsers}
|
|
|
|
IPCAllowedGroups=${concatStringsSep " " cfg.IPCAllowedGroups}
|
|
|
|
IPCAccessControlFiles=/var/lib/usbguard/IPCAccessControl.d/
|
2020-10-27 00:29:36 +00:00
|
|
|
DeviceRulesWithPort=${boolToString cfg.deviceRulesWithPort}
|
2020-08-20 17:08:02 +00:00
|
|
|
# HACK: that way audit logs still land in the journal
|
|
|
|
AuditFilePath=/dev/null
|
|
|
|
'';
|
|
|
|
|
|
|
|
daemonConfFile = pkgs.writeText "usbguard-daemon-conf" daemonConf;
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.usbguard = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "USBGuard daemon");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.usbguard;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.usbguard";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The usbguard package to use. If you do not need the Qt GUI, use
|
2022-08-12 12:06:08 +00:00
|
|
|
`pkgs.usbguard-nox` to save disk space.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
rules = mkOption {
|
|
|
|
type = types.nullOr types.lines;
|
|
|
|
default = null;
|
|
|
|
example = ''
|
|
|
|
allow with-interface equals { 08:*:* }
|
|
|
|
'';
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-08-20 17:08:02 +00:00
|
|
|
The USBGuard daemon will load this as the policy rule set.
|
|
|
|
As these rules are NixOS managed they are immutable and can't
|
|
|
|
be changed by the IPC interface.
|
|
|
|
|
|
|
|
If you do not set this option, the USBGuard daemon will load
|
2022-08-21 13:32:41 +00:00
|
|
|
it's policy rule set from `${defaultRuleFile}`.
|
2020-08-20 17:08:02 +00:00
|
|
|
This file can be changed manually or via the IPC interface.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
Running `usbguard generate-policy` as root will
|
2020-08-20 17:08:02 +00:00
|
|
|
generate a config for your currently plugged in devices.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
For more details see {manpage}`usbguard-rules.conf(5)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
implicitPolicyTarget = mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
type = policy;
|
|
|
|
default = "block";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
How to treat USB devices that don't match any rule in the policy.
|
|
|
|
Target should be one of allow, block or reject (logically remove the
|
|
|
|
device node from the system).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
presentDevicePolicy = mkOption {
|
|
|
|
type = policy;
|
|
|
|
default = "apply-policy";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
How to treat USB devices that are already connected when the daemon
|
|
|
|
starts. Policy should be one of allow, block, reject, keep (keep
|
|
|
|
whatever state the device is currently in) or apply-policy (evaluate
|
|
|
|
the rule set for every present device).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
presentControllerPolicy = mkOption {
|
|
|
|
type = policy;
|
|
|
|
default = "keep";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
How to treat USB controller devices that are already connected when
|
|
|
|
the daemon starts. One of allow, block, reject, keep or apply-policy.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
insertedDevicePolicy = mkOption {
|
|
|
|
type = policy;
|
|
|
|
default = "apply-policy";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
How to treat USB devices that are already connected after the daemon
|
|
|
|
starts. One of block, reject, apply-policy.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
restoreControllerDeviceState = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The USBGuard daemon modifies some attributes of controller
|
|
|
|
devices like the default authorization state of new child device
|
2022-12-28 21:21:41 +00:00
|
|
|
instances. Using this setting, you can control whether the daemon
|
2020-04-24 23:36:52 +00:00
|
|
|
will try to restore the attribute values to the state before
|
2022-12-28 21:21:41 +00:00
|
|
|
modification on shutdown.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
IPCAllowedUsers = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ "root" ];
|
|
|
|
example = [ "root" "yourusername" ];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
A list of usernames that the daemon will accept IPC connections from.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
IPCAllowedGroups = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "wheel" ];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
A list of groupnames that the daemon will accept IPC connections
|
|
|
|
from.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
deviceRulesWithPort = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Generate device specific rules including the "via-port" attribute.
|
|
|
|
'';
|
|
|
|
};
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
dbus.enable = mkEnableOption (lib.mdDoc "USBGuard dbus daemon");
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
systemd.services = {
|
|
|
|
usbguard = {
|
|
|
|
description = "USBGuard daemon";
|
|
|
|
|
|
|
|
wantedBy = [ "basic.target" ];
|
|
|
|
wants = [ "systemd-udevd.service" ];
|
|
|
|
|
|
|
|
# make sure an empty rule file exists
|
|
|
|
preStart = ''[ -f "${ruleFile}" ] || touch ${ruleFile}'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
ExecStart = "${cfg.package}/bin/usbguard-daemon -P -k -c ${daemonConfFile}";
|
|
|
|
Restart = "on-failure";
|
|
|
|
|
|
|
|
StateDirectory = [
|
|
|
|
"usbguard"
|
|
|
|
"usbguard/IPCAccessControl.d"
|
|
|
|
];
|
|
|
|
|
|
|
|
AmbientCapabilities = "";
|
|
|
|
CapabilityBoundingSet = "CAP_CHOWN CAP_FOWNER";
|
|
|
|
DeviceAllow = "/dev/null rw";
|
|
|
|
DevicePolicy = "strict";
|
|
|
|
IPAddressDeny = "any";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectSystem = true;
|
|
|
|
ReadOnlyPaths = "-/";
|
|
|
|
ReadWritePaths = "-/dev/shm -/tmp";
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_NETLINK" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = "@system-service";
|
|
|
|
UMask = "0077";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
usbguard-dbus = mkIf cfg.dbus.enable {
|
|
|
|
description = "USBGuard D-Bus Service";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
requires = [ "usbguard.service" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "dbus";
|
|
|
|
BusName = "org.usbguard1";
|
|
|
|
ExecStart = "${cfg.package}/bin/usbguard-dbus --system";
|
|
|
|
Restart = "on-failure";
|
|
|
|
};
|
|
|
|
|
|
|
|
aliases = [ "dbus-org.usbguard.service" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
security.polkit.extraConfig =
|
|
|
|
let
|
|
|
|
groupCheck = (lib.concatStrings (map
|
|
|
|
(g: "subject.isInGroup(\"${g}\") || ")
|
|
|
|
cfg.IPCAllowedGroups))
|
|
|
|
+ "false";
|
|
|
|
in
|
|
|
|
optionalString cfg.dbus.enable ''
|
|
|
|
polkit.addRule(function(action, subject) {
|
|
|
|
if ((action.id == "org.usbguard.Policy1.listRules" ||
|
|
|
|
action.id == "org.usbguard.Policy1.appendRule" ||
|
|
|
|
action.id == "org.usbguard.Policy1.removeRule" ||
|
|
|
|
action.id == "org.usbguard.Devices1.applyDevicePolicy" ||
|
|
|
|
action.id == "org.usbguard.Devices1.listDevices" ||
|
|
|
|
action.id == "org.usbguard1.getParameter" ||
|
|
|
|
action.id == "org.usbguard1.setParameter") &&
|
|
|
|
subject.active == true && subject.local == true &&
|
|
|
|
(${groupCheck})) {
|
|
|
|
return polkit.Result.YES;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2020-08-20 17:08:02 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "usbguard" "ruleFile" ] "The usbguard module now uses ${defaultRuleFile} as ruleFile. Alternatively, use services.usbguard.rules to configure rules.")
|
|
|
|
(mkRemovedOptionModule [ "services" "usbguard" "IPCAccessControlFiles" ] "The usbguard module now hardcodes IPCAccessControlFiles to /var/lib/usbguard/IPCAccessControl.d.")
|
|
|
|
(mkRemovedOptionModule [ "services" "usbguard" "auditFilePath" ] "Removed usbguard module audit log files. Audit logs can be found in the systemd journal.")
|
2023-07-15 17:15:38 +00:00
|
|
|
(mkRenamedOptionModule [ "services" "usbguard" "implictPolicyTarget" ] [ "services" "usbguard" "implicitPolicyTarget" ])
|
2020-08-20 17:08:02 +00:00
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|