Skip to content
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

postgres password appears in process table #563

Open
freedge opened this issue Apr 12, 2024 · 2 comments
Open

postgres password appears in process table #563

freedge opened this issue Apr 12, 2024 · 2 comments
Assignees

Comments

@freedge
Copy link

freedge commented Apr 12, 2024

Container platform

OCP 4

Version

quay.io/sclorg/postgresql-15-c9s:latest from awx-operator

OS version of the container image

CentOS Stream 9

Bugzilla, Jira

No response

Description

this image runs psql with the password set on the command line. As a result it appears in the process table and is recorded by auditing tools.

eg

as deployed by awx-operator, the postgres container will execute a

psql        --set ON_ERROR_STOP=1 --set=username=awx --set=password=ZTH7V8R1wg2GwI..

Reproducer

cd 16 && ; podman build -t db -f ./Dockerfile.c9s
sudo auditctl  -a exit,always -F arch=x86_64 -S execve
podman run -ti -v /var/lib/pgsql/data --name db  -e POSTGRESQL_USER=awx -e POSTGRESQL_PASSWORD=lepassword -e POSTGRESQL_DATABASE=awx -e POSTGRESQL_MASTER_USER=lemaster -e POSTGRESQL_MASTER_PASSWORD=lemaster -e POSTGRESQL_ADMIN_PASSWORD=more --rm db
sudo grep psql /var/log/audit/audit.log | grep lepassword
type=EXECVE msg=audit(1713081027.200:82065): argc=5 a0="psql" a1="--set" a2="ON_ERROR_STOP=1" a3="--set=username=awx" a4="--set=password=lepassword"

something in this fashion would work

--- a/16/root/usr/share/container-scripts/postgresql/start/set_passwords.sh
+++ b/16/root/usr/share/container-scripts/postgresql/start/set_passwords.sh
@@ -1,23 +1,21 @@
 #!/bin/bash

-_psql () { psql --set ON_ERROR_STOP=1 "$@" ; }
+_psql () { setsid psql --set ON_ERROR_STOP=1 "$@" ; }

 if [[ ",$postinitdb_actions," = *,simple_db,* ]]; then
-_psql --set=username="$POSTGRESQL_USER" \
-      --set=password="$POSTGRESQL_PASSWORD" \
-<<< "ALTER USER :\"username\" WITH ENCRYPTED PASSWORD :'password';"
+(echo "${POSTGRESQL_PASSWORD}" ; echo "${POSTGRESQL_PASSWORD}"
+) | _psql --set=username="$POSTGRESQL_USER" \
+      -f <(echo '\password :username')
 fi
@pkubatrh pkubatrh assigned pkubatrh and fila43 and unassigned pkubatrh Apr 24, 2024
@pkubatrh
Copy link
Member

Thanks for the report. This makes sense to fix. Let's take a look.

@hhorak
Copy link
Member

hhorak commented Sep 4, 2024

The suggested solution is quite clever indeed. I've been thinking a bit how to approach this with a bit more straightforward solution and without shell hacks, and found out possibility to get an env variable value this way:

postgres=# \set mypass `echo $POSTGRESQL_ADMIN_PASSWORD`
postgres=# select :'mypass';
 ?column? 
----------
 test
(1 row)

However, I'm not sure whether this way is safe from the SQL injection perspective - something worth re-checking on postgresql forum?

Another way would be to install python3-psycopg2 and write a simple python script that would do what we need -- installing it into the container would only be a few hundreds of kB more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants