Skip to content

Commit

Permalink
nixos/postgresql: create infrastructure for relaxing systemd hardening
Browse files Browse the repository at this point in the history
By matching on the package names of the plugins passed into the package
we can relax the systemd unit hardening as needed.
  • Loading branch information
mweinelt authored and Ma27 committed Nov 13, 2024
1 parent 3991c84 commit bbddb96
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
42 changes: 24 additions & 18 deletions nixos/modules/services/databases/postgresql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

let
inherit (lib)
any
attrValues
concatMapStrings
concatStringsSep
Expand Down Expand Up @@ -30,19 +31,19 @@ let

cfg = config.services.postgresql;

postgresql =
let
# ensure that
# services.postgresql = {
# enableJIT = true;
# package = pkgs.postgresql_<major>;
# };
# works.
base = if cfg.enableJIT then cfg.package.withJIT else cfg.package.withoutJIT;
in
if cfg.extensions == []
then base
else base.withPackages cfg.extensions;
# ensure that
# services.postgresql = {
# enableJIT = true;
# package = pkgs.postgresql_<major>;
# };
# works.
basePackage = if cfg.enableJIT
then cfg.package.withJIT
else cfg.package.withoutJIT;

postgresql = if cfg.extensions == []
then basePackage
else basePackage.withPackages cfg.extensions;

toStr = value:
if true == value then "yes"
Expand All @@ -59,6 +60,9 @@ let
'';

groupAccessAvailable = versionAtLeast postgresql.version "11.0";

extensionNames = map (extension: extension.pname) postgresql.installedExtensions;
extensionInstalled = extension: elem extension extensionNames;
in

{
Expand Down Expand Up @@ -630,7 +634,7 @@ in
PrivateTmp = true;
ProtectHome = true;
ProtectSystem = "strict";
MemoryDenyWriteExecute = lib.mkDefault (cfg.settings.jit == "off");
MemoryDenyWriteExecute = lib.mkDefault (cfg.settings.jit == "off" && (!any extensionInstalled [ "plv8" ]));
NoNewPrivileges = true;
LockPersonality = true;
PrivateDevices = true;
Expand All @@ -654,10 +658,12 @@ in
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
SystemCallFilter = [
"@system-service"
"~@privileged @resources"
];
SystemCallFilter =
[
"@system-service"
"~@privileged @resources"
]
++ lib.optionals (any extensionInstalled [ "plv8" ]) [ "@pkey" ];
UMask = if groupAccessAvailable then "0027" else "0077";
}
(mkIf (cfg.dataDir != "/var/lib/postgresql") {
Expand Down
32 changes: 20 additions & 12 deletions pkgs/servers/sql/postgresql/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -344,25 +344,33 @@ let
};
});

postgresqlWithPackages = { postgresql, buildEnv }: f: buildEnv {
postgresqlWithPackages = { postgresql, buildEnv }: f: let
installedExtensions = f postgresql.pkgs;
in buildEnv {
name = "${postgresql.pname}-and-plugins-${postgresql.version}";
paths = f postgresql.pkgs ++ [
paths = installedExtensions ++ [
postgresql
postgresql.man # in case user installs this into environment
];

pathsToLink = ["/"];

passthru.version = postgresql.version;
passthru.psqlSchema = postgresql.psqlSchema;
passthru.withJIT = postgresqlWithPackages {
inherit buildEnv;
postgresql = postgresql.withJIT;
} f;
passthru.withoutJIT = postgresqlWithPackages {
inherit buildEnv;
postgresql = postgresql.withoutJIT;
} f;
passthru = {
inherit installedExtensions;
inherit (postgresql)
psqlSchema
version
;

withJIT = postgresqlWithPackages {
inherit buildEnv;
postgresql = postgresql.withJIT;
} f;
withoutJIT = postgresqlWithPackages {
inherit buildEnv;
postgresql = postgresql.withoutJIT;
} f;
};
};

in
Expand Down

0 comments on commit bbddb96

Please sign in to comment.