From 0ea8950ff12bd97ebe3cc4bbc9c5cac5652fb0a9 Mon Sep 17 00:00:00 2001 From: Zach van Rijn Date: Wed, 29 Nov 2023 22:31:21 -0600 Subject: database: run triggers with r*id = e*id The 'apk' utility is intended to run as root, however it is not strictly necessary depending on the tasks it needs to execute. If invoked as euid 0 but ruid something else, the mismatch is detected by the (shell) interpreter, which sets euid to ruid, which can cause a trigger expecting privileges to fail silently. Fixes: horizon#379 --- src/database.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/database.c b/src/database.c index 9864543..b916d59 100644 --- a/src/database.c +++ b/src/database.c @@ -1949,6 +1949,19 @@ int apk_db_run_script(struct apk_database *db, char *fn, char **argv) exit(127); } + if (getuid() != geteuid()) + { + if (setgid(getegid()) != 0) { + apk_error("%s: setgid: %s", basename(fn), strerror(errno)); + exit(127); + } + + if (setuid(geteuid()) != 0) { + apk_error("%s: setuid: %s", basename(fn), strerror(errno)); + exit(127); + } + } + execve(fn, argv, environment); exit(127); /* should not get here */ } -- cgit v1.2.3-60-g2f50