Skip to content

Commit

Permalink
nixos/chrony: clean up, rework to be a little closer to upstream
Browse files Browse the repository at this point in the history
Most importantly, this sets PrivateTmp, ProtectHome, and ProtectSystem
so that Chrony flaws are mitigated, should they occur.

Moving to ProtectSystem=full however, requires moving the chrony key
files under /var/lib/chrony -- which should be fine, anyway.

This also ensures ConditionCapability=CAP_SYS_TIME is set, ensuring
that chronyd will only be launched in an environment where such a
capability can be granted.

Signed-off-by: Austin Seipp <aseipp@pobox.com>
(cherry picked from commit 0ce90d5)
  • Loading branch information
thoughtpolice committed Sep 24, 2018
1 parent f34ef9e commit b0f8181
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions nixos/modules/services/networking/chrony.nix
Expand Up @@ -3,12 +3,10 @@
with lib;

let
cfg = config.services.chrony;

stateDir = "/var/lib/chrony";

keyFile = "/etc/chrony.keys";

cfg = config.services.chrony;
keyFile = "${stateDir}/chrony.keys";

configFile = pkgs.writeText "chrony.conf" ''
${concatMapStringsSep "\n" (server: "server " + server) cfg.servers}
Expand All @@ -19,26 +17,18 @@ let
}
driftfile ${stateDir}/chrony.drift
keyfile ${keyFile}
${optionalString (!config.time.hardwareClockInLocalTime) "rtconutc"}
${cfg.extraConfig}
'';

chronyFlags = "-n -m -u chrony -f ${configFile} ${toString cfg.extraFlags}";

chronyFlags = "-m -u chrony -f ${configFile} ${toString cfg.extraFlags}";
in

{

###### interface

options = {

services.chrony = {

enable = mkOption {
default = false;
description = ''
Expand Down Expand Up @@ -83,15 +73,9 @@ in
description = "Extra flags passed to the chronyd command.";
};
};

};


###### implementation

config = mkIf cfg.enable {

# Make chronyc available in the system path
environment.systemPackages = [ pkgs.chrony ];

users.groups = singleton
Expand All @@ -113,26 +97,30 @@ in
{ description = "chrony NTP daemon";

wantedBy = [ "multi-user.target" ];
wants = [ "time-sync.target" ];
before = [ "time-sync.target" ];
after = [ "network.target" ];
wants = [ "time-sync.target" ];
before = [ "time-sync.target" ];
after = [ "network.target" ];
conflicts = [ "ntpd.service" "systemd-timesyncd.service" ];

path = [ pkgs.chrony ];

preStart =
''
mkdir -m 0755 -p ${stateDir}
touch ${keyFile}
chmod 0640 ${keyFile}
chown chrony:chrony ${stateDir} ${keyFile}
'';
preStart = ''
mkdir -m 0755 -p ${stateDir}
touch ${keyFile}
chmod 0640 ${keyFile}
chown chrony:chrony ${stateDir} ${keyFile}
'';

serviceConfig =
{ ExecStart = "${pkgs.chrony}/bin/chronyd ${chronyFlags}";
{ Type = "forking";
ExecStart = "${pkgs.chrony}/bin/chronyd ${chronyFlags}";

ProtectHome = "yes";
ProtectSystem = "full";
PrivateTmp = "yes";

ConditionCapability = "CAP_SYS_TIME";
};
};

};

}

0 comments on commit b0f8181

Please sign in to comment.