Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Running processes as root helps attackers #18

Closed
szepeviktor opened this issue May 28, 2021 · 15 comments
Closed

Running processes as root helps attackers #18

szepeviktor opened this issue May 28, 2021 · 15 comments
Labels
⚡️ Enhancement Items that are new features requested to be added.

Comments

@szepeviktor
Copy link
Contributor

szepeviktor commented May 28, 2021

Please consider avoiding root (UID 0) and system users (UID 1-999) e.g. www-data.
Least privileges come with normal users.

@szepeviktor szepeviktor changed the title Running processes as root help attackers Running processes as root helps attackers May 28, 2021
@jaydrogers
Copy link
Member

I totally agree and this is on my radar.

I am hoping this will be fixed in #15.

The only problem I had was if I set it to www-data in this image, then later on I want to add a PHP package (like php-redis or something), I was running into errors that www-data did not have permissions to install.

I will definitely be revisiting this because its one of the bigger worries I had about this set up.

@szepeviktor
Copy link
Contributor Author

Please be aware that www-data is a "system user" having higher privileges than normal users.
See UID-s in the top comment.

@szepeviktor
Copy link
Contributor Author

then later on I want to add a PHP package

Setting user in a Docker image should be the last step.

I hope you do not intent to install packages in a running container!

@jaydrogers jaydrogers added the ⚡️ Enhancement Items that are new features requested to be added. label May 28, 2021
@jaydrogers
Copy link
Member

I hope you do not intent to install packages in a running container!

No, this would be like this...

Problem

  • If I set a USER 12345 to run the serversideup/php:8.0-fpm-nginx as, how would down stream docker images add their project dependencies?

For example

(I think I tried this earlier)

On a downstream docker project, I might want a new Dockerfile that depends off of the base image from ServerSideUp:

FROM serversideup/php:8.0-fpm-nginx

RUN apt update && apt install php-redis

☝️ If I have USER 12345 (telling the container to run as UID 12345 from the parent image), this will fail because USER 12345 cannot install php-redis.

Thoughts?

@szepeviktor
Copy link
Contributor Author

how would down stream docker images add their project dependencies?

USER 0
MANAGE DEPS
USER 12345

Done!

@jaydrogers
Copy link
Member

I will definitely give this a whirl, thanks!!

@jaydrogers jaydrogers added this to the v1.2 milestone Jun 5, 2021
@jaydrogers
Copy link
Member

Here is an update on this:

Problem

  • When I use S6 Overlay, I cannot get logging to work on the php-fpm image

Screen Shot 2021-06-22 at 5 25 58 PM

How to recreate the problem

1. Copy my Dockerfile

####################################################
# Server Side Up -  PHP 7.4 / FPM image 
#####################################################

FROM serversideup/php:beta-7.4-cli

LABEL maintainer="Jay Rogers (@jaydrogers)"

# Set default PHP environment variables

ENV PHP_DATE_TIMEZONE="UTC" \
    PHP_DISPLAY_ERRORS=On \
    PHP_ERROR_REPORTING="E_ALL & ~E_DEPRECATED & ~E_STRICT" \
    PHP_MEMORY_LIMIT="256M" \
    PHP_MAX_EXECUTION_TIME="99" \
    PHP_POST_MAX_SIZE="100M" \
    PHP_UPLOAD_MAX_FILE_SIZE="100M" \
    PHP_POOL_NAME="www" \
    PHP_PM_CONTROL=dynamic \
    PHP_PM_MAX_CHILDREN="20" \
    PHP_PM_START_SERVERS="2" \
    PHP_PM_MIN_SPARE_SERVERS="1" \
    PHP_PM_MAX_SPARE_SERVERS="3"

# Install FPM
RUN apt-get update \
    && apt-get -y --no-install-recommends install \
        php7.4-fpm \
        && echo "Allow pool name to be set via env, default it to 'www'..." \
    && sed -i -e 's/\[www\]/\[$\{PHP_POOL_NAME\}]/g' /etc/php/7.4/fpm/pool.d/www.conf \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

# Apply PHP configuration file
# COPY etc/php/fpm/pool.d/y-override-php-defaults.conf /etc/php/7.4/fpm/pool.d/y-override-php-defaults.conf

CMD ["/usr/sbin/php-fpm7.4", "-O" ]

# Open up fcgi port
EXPOSE 9000

2. Build my Dockerfile locally
Run this from the folder where you placed the Dockerfile:

docker build --pull . -t localhost/php:7.4-fpm

3. Run the local image
Run the

docker run -it --rm --name fpm localhost/php:7.4-fpm

Important note:

  1. The Dockerfile above depends on the beta image (serversideup/php:beta-7.4-cli), which is built from this file https://github.com/serversideup/docker-php/blob/dev/php/7.4/cli/Dockerfile
  2. The CLI image is based off of serversideup/docker-baseimage-s6-overlay-ubuntu:20.04, which is located in this new repo https://github.com/serversideup/docker-baseimage-s6-overlay-ubuntu/blob/main/Dockerfile

Things that concern me

I don't even know if this is possible to run as a "non-root" user due to how PHP-FPM is structured. I'm pretty sure PHP-FPM needs root in order to start its processes.

Other repos that are running PHP as "root"

These very talented groups are also not running things as an unprivileged user:

  1. bitnami/php-fpm
  2. linuxserver/docker-nextcloud (great example of a PHP app running on S6 Overlay... they aren't PHP-FPM though...)

What I think I might have to do

My gut feeling is telling be that I will:

  • Need to continue to run the container as root
  • Use the configurations of NGINX & PHP-FPM to select the proper user (my 9999 user)

Calling in help

@szepeviktor: Are you aware of any examples of projects running PHP as an unprivileged user?

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Jun 22, 2021

Hello! It is highly popular to give a sh*t about what is(is going on) inside a container.
People do not have time to follow CVE-s. So there's such a thing as breaking out of a container.

Actually it is a novice mistake to run something as root - no matter whether inside a container or not.

S6 Overlay needs to run as root but it does not mean that PHP-FPM needs too.
PHP-FPM opens a socket file and a TCP socket, starts threads, creates a PID file - what is the problem here?

@szepeviktor
Copy link
Contributor Author

Are you aware of any examples of projects running PHP as an unprivileged user?

I think PHP-FPM operates this way

kép

The master process runs as root, workers run as a normal user.

@jaydrogers
Copy link
Member

Thanks for chiming in!

You confirmed my assumptions. I will need to remove the extra arguments on this line:

ENTRYPOINT ["/init", "/bin/execlineb", "-s0", "-c", "export HOME $WEBUSER_HOME s6-setuidgid webuser $@"]

If I leave those lines in there above, PHP-FPM will not be able to start correctly because its trying to start the master process as webuser (userid 9999).

Instead, I will have root start the masterprocess, which will then use the PHP-FPM config to start the children as webuser (userid 9999).

Does that sound like a good approach?

@szepeviktor
Copy link
Contributor Author

BTW Debian uses https://github.com/krallin/tini

@szepeviktor
Copy link
Contributor Author

Does that sound like a good approach?

At first glace yes.
Staring a lot at htop may tell you much more.

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Jun 22, 2021

php-fpm.conf could have daemonize = no
and I think PHP-FPM could run as non-privileged user if you don't set user and group in pool config.

@jaydrogers
Copy link
Member

php-fpm.conf could have daemonize = no

Thanks! I have this set already.

and I think PHP-FPM could run as non-privileged user if you don't set user and group in pool config.

Interesting! I will play around with this. Thanks!!

@szepeviktor
Copy link
Contributor Author

It is open-source. You can spend weeks with it!

@jaydrogers jaydrogers removed this from the v1.2 milestone Jul 22, 2021
@serversideup serversideup locked and limited conversation to collaborators Sep 22, 2022
@jaydrogers jaydrogers converted this issue into discussion #71 Sep 22, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
⚡️ Enhancement Items that are new features requested to be added.
Projects
None yet
Development

No branches or pull requests

2 participants