nixos/phpfpm: run as non root user for enhanced security#73223
nixos/phpfpm: run as non root user for enhanced security#73223aanderse wants to merge 1 commit intoNixOS:masterfrom
Conversation
|
@aanderse I don't have time to review this rn, but I'll do so later. In the meantime: Example: If your service wants to bind a privileged port (< 1024), and is not running as root, you need to add |
|
Having some issues getting this to work properly with socket activation. Will get back to it when I have some time. Any help/testing appreciated. |
florianjacob
left a comment
There was a problem hiding this comment.
Socket activation, yeah! 🥳
How do the problems manifest? From my previous socket activation experience, everything seems to be allright? :/ |
After circling back to this I have found the problem! Another case of PEBKAC seems to be the culprit, as I can't reproduce any problems now and everything is running smoothly... Next steps:
|
|
@aanderse I think we should add some phpfpm-specific tests to Those should include both connection via unix file sockets and tcp. |
7eef418 to
ee20f29
Compare
|
@GrahamcOfBorg test phpfpm |
|
Sorry for the noise up until this point. I believe this PR is ready for review now. Thanks for the help so far. |
|
It seems that the correct way to make |
|
@aanderse I was playing around with php-fpm and I have it kind of running without socket units. While socket units really are the best solution, I'm working around that with the following systemd service options: This way, php-fpm is allowed to chown the socket to any user. However, this gets really nasty with the permissions of /run/phpfpm, which is why I believe the best way to solve this are socket units. |
There was a problem hiding this comment.
Looks like a change to master merged poorly? Will fix shortly.
There was a problem hiding this comment.
This options not used
дек 24 19:50:05 NixOS-Test php-fpm[6321]: [NOTICE] [pool test-02] 'user' directive is ignored when FPM is not running as root
дек 24 19:50:05 NixOS-Test php-fpm[6321]: [NOTICE] [pool test-02] 'group' directive is ignored when FPM is not running as root
There was a problem hiding this comment.
After delete these lines - Service stops starting.
Worked with this lines:
RuntimeDirectory = "phpfpm-{pool}";
RuntimeDirectoryMode = "0750";
With RuntimeDirectory = "phpfpm"; and 2 pools the directory /run/phpfpm is recreated. Need use "phpfpm-{pool}";
There was a problem hiding this comment.
my example patch
diff --git a/nixos/modules/services/web-servers/phpfpm/default.nix b/nixos/modules/services/web-servers/phpfpm/default.nix
index 31f4ae5445f..9e0177c22c8 100644
--- a/nixos/modules/services/web-servers/phpfpm/default.nix
+++ b/nixos/modules/services/web-servers/phpfpm/default.nix
@@ -5,8 +5,6 @@ with lib;
let
cfg = config.services.phpfpm;
- runtimeDir = "/run/phpfpm";
-
toStr = value:
if true == value then "yes"
else if false == value then "no"
@@ -42,7 +40,7 @@ let
options = {
socket = mkOption {
type = types.str;
- default = "${runtimeDir}/${name}.sock";
+ default = "/run/phpfpm-${name}/${name}.sock";
readOnly = true;
description = ''
Path to the unix socket file on which to accept FastCGI requests.
@@ -136,8 +134,6 @@ let
"listen.owner" = poolOpts.user;
"listen.group" = poolOpts.group;
"listen.mode" = "0660";
- "user" = poolOpts.user;
- "group" = poolOpts.group;
};
};
};
@@ -147,6 +143,7 @@ in {
(mkRemovedOptionModule [ "services" "phpfpm" "poolConfigs" ] "Use services.phpfpm.pools instead.")
(mkRemovedOptionModule [ "services" "phpfpm" "phpIni" ] "")
(mkRemovedOptionModule [ "services" "phpfpm" "listen" ] "Reference the read-only option config.services.phpfpm.pools.<name>.socket to access the path of your socket.")
+ ];
options = {
services.phpfpm = {
@@ -329,6 +326,8 @@ in {
KillMode = mkIf createSocket "process";
User = poolOpts.user;
Group = poolOpts.group;
+ RuntimeDirectory = "phpfpm-${pool}";
+ RuntimeDirectoryMode = "0750";
};
}
) cfg.pools;
There was a problem hiding this comment.
listen.owner, listen.group, listen.mode, user, and group are all used by the nixos module and will not be used by phpfpm anymore. The reason they remain is documentation. If those values aren't there users can be confused what is happening. Tricky because systemd is taking care of everything that phpfpm used to... but better than leaving users without any idea IMO.
There was a problem hiding this comment.
RuntimeDirectory shouldn't be needed anymore because systemd socket is taking care of directory and socket creation.
There was a problem hiding this comment.
Options user and group writed to phpfpm configuration:
cat /nix/store/...-phpfpm-test-01.conf
[global]
daemonize = no
error_log = syslog
rlimit_core = unlimited
rlimit_files = 131072
[test-01]
group = nginx
listen = /run/phpfpm-test-01/test-01.sock
listen.group = nginx
listen.mode = 0660
listen.owner = nginx
pm = dynamic
pm.max_children = 20
pm.max_requests = 400
pm.max_spare_servers = 12
pm.min_spare_servers = 2
pm.start_servers = 4
user = nginx
Ma27
left a comment
There was a problem hiding this comment.
WIthout having it tested yet, this seems fine to me and could be merged IMHO as soon as the remaining comments are resolved.
|
@Ma27 there are still issues unresolved. I haven't had time to figure out exactly how to reproduce them, but this should not be merged as it will break something. I'm hoping to get back to this at some point... |
|
Found bug - after change |
|
@Izorkin yeah I'm sort of stuck on this for now. It seems our activation scripts are pretty buggy, especially surrounding sockets, so I'm at a point where I think this PR is stuck until that is fixed. |
|
@aanderse variant without systemd-socket? |
The proper solution is to use a |
|
@Izorkin since you asked on
If there is a scenario I have overlooked in which we can run ping @flokli as a reminder of our tentative meeting to discuss |
|
I don't see such problems with this patch: Example configuration: Result: |
|
@Izorkin you must be missing some configuration here because |
|
@aanderse yes, need add |
|
@Izorkin after playing with I'm closing this PR and will open a new PR which includes your recommendations soon. |
Motivation for this change
Security.
NOTE: I'm not specifically familiar with linux capabilities beyond the man page so if I'm missing anything inAmbientCapabilitiesorCapabilitiesBoundingSetplease let me know.Things done
sandboxinnix.confon non-NixOS linux)nix-shell -p nix-review --run "nix-review wip"./result/bin/)nix path-info -Sbefore and after)Notify maintainers
cc @