-
-
Notifications
You must be signed in to change notification settings - Fork 18k
nixos/postgresql: set up sandboxing #344925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ebffcc
f800d8e
0f1e2a1
70a6092
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -623,7 +623,46 @@ in | |
| TimeoutSec = 120; | ||
|
|
||
| ExecStart = "${postgresql}/bin/postgres"; | ||
|
|
||
| # Hardening | ||
| CapabilityBoundingSet = [ "" ]; | ||
| DevicePolicy = "closed"; | ||
| PrivateTmp = true; | ||
| ProtectHome = true; | ||
| ProtectSystem = "strict"; | ||
| MemoryDenyWriteExecute = lib.mkDefault (cfg.settings.jit == "off"); | ||
| NoNewPrivileges = true; | ||
| LockPersonality = true; | ||
| PrivateDevices = true; | ||
| PrivateMounts = true; | ||
| ProcSubset = "pid"; | ||
| ProtectClock = true; | ||
| ProtectControlGroups = true; | ||
| ProtectHostname = true; | ||
| ProtectKernelLogs = true; | ||
| ProtectKernelModules = true; | ||
| ProtectKernelTunables = true; | ||
| ProtectProc = "invisible"; | ||
| RemoveIPC = true; | ||
| RestrictAddressFamilies = [ | ||
| "AF_INET" | ||
| "AF_INET6" | ||
| "AF_NETLINK" # used for network interface enumeration | ||
| "AF_UNIX" | ||
| ]; | ||
| RestrictNamespaces = true; | ||
| RestrictRealtime = true; | ||
| RestrictSUIDSGID = true; | ||
| SystemCallArchitectures = "native"; | ||
| SystemCallFilter = [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use plv8 extension and the database server crashes every time a query using plv8 function is called: Is there a way to determine which system calls are needed? I tried SystemCallFilter = [ "" ];
SystemCallLog = [ "@privileged ~@resources ~@system-service" ];but it only made it crash with Reverting this whole PR works as a workaround.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if the system call filter is the issue, you should see an error in the kernel log (or auditd, I'm not entirely sure). But given that this is v8, there might be another problem: can you try setting
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to set up a vm test for plv8 - then we could just bisect through the list of changes?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I guess I would've just turned off more and more flags until it works. But yeah. @jtojnar never used this extension, but any chance you can share a reproducer?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even just calling
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, tried adding
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know what triggers the second error? Would be nice to be able to repro.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a pretty big SQL query including custom window functions: Original queryWITH
recent_event AS (
SELECT
id
FROM
event
WHERE
level = 'world'
AND "end" < CURRENT_DATE
AND "end" > '2022-01-01'
ORDER BY
"end" DESC
LIMIT
2
),
past_team AS (
SELECT
*,
0.5 AS coef,
6 AS CEILING,
position_in_class (age, 'ultraveteran') OVER (
w
ORDER BY
score DESC,
time ASC
) AS position_uv,
count(nullif(is_in_class (age, 'ultraveteran'), FALSE)) OVER w AS limit_uv,
position_in_class (age, 'superveteran') OVER (
w
ORDER BY
score DESC,
time ASC
) AS position_sv,
count(nullif(is_in_class (age, 'superveteran'), FALSE)) OVER w AS limit_sv,
position_in_class (age, 'veteran') OVER (
w
ORDER BY
score DESC,
time ASC
) AS position_v,
count(nullif(is_in_class (age, 'veteran'), FALSE)) OVER w AS limit_v,
position_in_class (age, 'open') OVER (
w
ORDER BY
score DESC,
time ASC
) AS position_o,
count(nullif(is_in_class (age, 'open'), FALSE)) OVER w AS limit_o,
position_in_class (age, 'junior') OVER (
w
ORDER BY
score DESC,
time ASC
) AS position_j,
count(nullif(is_in_class (age, 'junior'), FALSE)) OVER w AS limit_j
FROM
team
WHERE
event_id IN (
SELECT
*
FROM
recent_event
)
WINDOW
w AS (
PARTITION BY
event_id,
gender
)
),
past_team_crit AS (
SELECT
*,
(
position_uv <= CEILING
AND position_uv <= ceil(limit_uv * coef)
) AS prequalified_uv,
(
position_sv <= CEILING
AND position_sv <= ceil(limit_sv * coef)
) AS prequalified_sv,
(
position_v <= CEILING
AND position_v <= ceil(limit_v * coef)
) AS prequalified_v,
(
position_o <= CEILING
AND position_o <= ceil(limit_o * coef)
) AS prequalified_o,
(
position_j <= CEILING
AND position_j <= ceil(limit_j * coef)
) AS prequalified_j
FROM
past_team
)
SELECT
*
FROM
member
LEFT JOIN past_team_crit ON member.event_id = past_team_crit.event_id
AND member.team_id = past_team_crit.id
WHERE
prequalified_uv
OR prequalified_sv
OR prequalified_v
OR prequalified_o
OR prequalified_j;I managed to pare it down slightly but it is not clear what is causing it: Reduced queryWITH
recent_event AS (
SELECT
id
FROM
event
WHERE
level = 'world'
AND "end" < CURRENT_DATE
AND "end" > '2022-01-01'
ORDER BY
"end" DESC
LIMIT
2
),
past_team AS (
SELECT
*,
0.5 AS coef,
6 AS CEILING,
position_in_class (age, 'ultraveteran') OVER (
w
ORDER BY
score DESC,
time ASC
) AS position_uv,
count(nullif(is_in_class (age, 'ultraveteran'), FALSE)) OVER w AS limit_uv
FROM
team
WHERE
event_id IN (
SELECT
*
FROM
recent_event
)
WINDOW
w AS (
PARTITION BY
event_id,
gender
)
)
SELECT
*,
(
position_uv <= CEILING
AND position_uv <= ceil(limit_uv * coef)
) AS prequalified_uv
FROM
past_teamCuriously, the crash triggers if I remove ultraveteran category or if I only keep ultraveteran category, but not when I keep any other category. I should really move the logic out of SQL.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, that is probably too big for a reproducer in a nixos test. Either way #355010 should be ready to go. Feel free to pick that into your tree, if that fits your workflow.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so looking at nodejs/node#55509, it is probably indeed caused by V8 turning on JIT. I managed to make PLV8 (presumably) enable JIT with the following: DO $$
let xs = [];
for (let i = 0, n = 4000000000; i < n; i++) {
xs.push(Math.round(Math.random() * n))
}
console.log(Math.sum(xs));
$$ LANGUAGE plv8; |
||
| "@system-service" | ||
| "~@privileged @resources" | ||
| ]; | ||
| UMask = if groupAccessAvailable then "0027" else "0077"; | ||
| } | ||
| (mkIf (cfg.dataDir != "/var/lib/postgresql") { | ||
| ReadWritePaths = [ cfg.dataDir ]; | ||
| }) | ||
| (mkIf (cfg.dataDir == "/var/lib/postgresql/${cfg.package.psqlSchema}") { | ||
| StateDirectory = "postgresql postgresql/${cfg.package.psqlSchema}"; | ||
| StateDirectoryMode = if groupAccessAvailable then "0750" else "0700"; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.