Skip to content

Commit 9aa43e0

Browse files
wdoekesjrybar-rh
authored andcommitted
pkexec: Use realpath when comparing org.freedesktop.policykit.exec.path
This changes the pkexec path that is compared from the original supplied path to the path resolved by realpath(3). That means that "/bin/something" might now be matched as "/usr/bin/something", a review of your <annotate key="org.freedesktop.policykit.exec.path"> actions might be in order. Fixes: #194 See also: systemd/systemd#34714
1 parent 11c4a81 commit 9aa43e0

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/programs/pkexec.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ main (int argc, char *argv[])
452452
gchar *action_id;
453453
gboolean allow_gui;
454454
gchar **exec_argv;
455+
gchar *path_abs;
455456
gchar *path;
456457
struct passwd pwstruct;
457458
gchar pwbuf[8192];
@@ -508,6 +509,7 @@ main (int argc, char *argv[])
508509
result = NULL;
509510
action_id = NULL;
510511
saved_env = NULL;
512+
path_abs = NULL;
511513
path = NULL;
512514
exec_argv = NULL;
513515
command_line = NULL;
@@ -624,6 +626,8 @@ main (int argc, char *argv[])
624626
* but do check this is the case.
625627
*
626628
* We also try to locate the program in the path if a non-absolute path is given.
629+
*
630+
* And then we resolve the real path of the program.
627631
*/
628632
g_assert (argv[argc] == NULL);
629633
path = g_strdup (argv[n]);
@@ -647,7 +651,7 @@ main (int argc, char *argv[])
647651
}
648652
if (path[0] != '/')
649653
{
650-
/* g_find_program_in_path() is not suspectible to attacks via the environment */
654+
/* g_find_program_in_path() is not susceptible to attacks via the environment */
651655
s = g_find_program_in_path (path);
652656
if (s == NULL)
653657
{
@@ -662,9 +666,29 @@ main (int argc, char *argv[])
662666
*/
663667
if (argv[n] != NULL)
664668
{
665-
argv[n] = path;
669+
/* Must copy because we might replace path later on. */
670+
path_abs = g_strdup(path);
671+
/* argv[n:] is used as argv arguments to execv(). The called program
672+
* sees the original called path, but we make sure it's absolute. */
673+
if (path_abs != NULL)
674+
argv[n] = path_abs;
666675
}
667676
}
677+
#if _POSIX_C_SOURCE >= 200809L
678+
s = realpath(path, NULL);
679+
#else
680+
s = NULL;
681+
# error We have to deal with realpath(3) PATH_MAX madness
682+
#endif
683+
if (s != NULL)
684+
{
685+
/* The called program resolved to the canonical location. We don't update
686+
* argv[n] this time. The called program still sees the original
687+
* called path. This is very important for multi-call binaries like
688+
* busybox. */
689+
g_free (path);
690+
path = s;
691+
}
668692
if (access (path, F_OK) != 0)
669693
{
670694
g_printerr ("Error accessing %s: %s\n", path, g_strerror (errno));
@@ -1084,6 +1108,7 @@ main (int argc, char *argv[])
10841108
}
10851109

10861110
g_free (original_cwd);
1111+
g_free (path_abs);
10871112
g_free (path);
10881113
g_free (command_line);
10891114
g_free (cmdline_short);

test/integration/pkexec/test.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,26 @@ sudo -u "$TEST_USER" expect "$TMP_DIR/SIGTRAP-on-EOF.exp" | tee "$TMP_DIR/SIGTRA
142142
grep -q "AUTHENTICATION FAILED" "$TMP_DIR/SIGTRAP-on-EOF.log"
143143
grep -q "Not authorized" "$TMP_DIR/SIGTRAP-on-EOF.log"
144144
rm -f "$TMP_DIR/SIGTRAP-on-EOF.log"
145+
146+
: "Check absolute (but not canonicalized) path"
147+
BASH_ABS=$(command -v bash)
148+
ln -s "$BASH_ABS" ./my-bash
149+
sudo -u "$TEST_USER" expect "$TMP_DIR/basic-auth.exp" "$TEST_USER_PASSWORD" ./my-bash -c true | tee "$TMP_DIR/absolute-path.log"
150+
grep -Eq "Authentication is needed to run \`/.*/${PWD##*/}/./my-bash -c true' as the super user" "$TMP_DIR/absolute-path.log"
151+
grep -q "AUTHENTICATION COMPLETE" "$TMP_DIR/absolute-path.log"
152+
rm -f "$TMP_DIR/absolute-path.log"
153+
rm -f "./my-bash"
154+
155+
: "Check canonicalized path"
156+
if command -v strace; then
157+
BASH_ABS=$(command -v bash)
158+
ln -s "$BASH_ABS" ./my-bash
159+
sudo -u "$TEST_USER" strace -s 512 -o "$TMP_DIR/canonical-path.strace" -feexecve \
160+
expect "$TMP_DIR/basic-auth.exp" "$TEST_USER_PASSWORD" ./my-bash -c true | tee "$TMP_DIR/canonical-path.log"
161+
cat "$TMP_DIR/canonical-path.strace"
162+
grep -qF "execve(\"$BASH_ABS\", [\"$PWD/./my-bash\"," "$TMP_DIR/canonical-path.strace"
163+
grep -q "AUTHENTICATION COMPLETE" "$TMP_DIR/canonical-path.log"
164+
rm -f "$TMP_DIR/canonical-path.log" "$TMP_DIR/canonical-path.strace"
165+
rm -f "./my-bash"
166+
rm -f "$TMP_DIR/preload.c" "$TMP_DIR/preload.so"
167+
fi

0 commit comments

Comments
 (0)