run-command: mark path lookup errors with ENOENT

Since commit e3a434468f (run-command: use the async-signal-safe execv instead of execvp, 2017-04-19), prepare_cmd() does its own PATH lookup for any commands we run (on non-Windows platforms). However, its logic does not match the old execvp call when we fail to find a matching entry in the PATH. Instead of feeding the name directly to execv, execvp would consider that an ENOENT error. By continuing and passing the name directly to execv, we effectively behave as if "." was included at the end of the PATH. This can have confusing and even dangerous results. The fix itself is pretty straight-forward. There's a new test in t0061 to cover this explicitly, and I've also added a duplicate of the ENOENT test to ensure that we return the correct errno for this case. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 24, 2018 at 03:38 UTC 321fd82389742398d2924640ce3a61791fd27d60
2 files changed +29 -5
run-command.c
+17 -4
@@ -378,7 +378,7 @@ static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
378 set_error_routine(old_errfn);
379 }
380
381 -static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
381 +static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
382 {
383 if (!cmd->argv[0])
384 die("BUG: command is empty");
@@ -401,16 +401,22 @@ static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
401 /*
402 * If there are no '/' characters in the command then perform a path
403 * lookup and use the resolved path as the command to exec. If there
404 - * are no '/' characters or if the command wasn't found in the path,
405 - * have exec attempt to invoke the command directly.
404 + * are '/' characters, we have exec attempt to invoke the command
405 + * directly.
406 */
407 if (!strchr(out->argv[1], '/')) {
408 char *program = locate_in_PATH(out->argv[1]);
409 if (program) {
410 free((char *)out->argv[1]);
411 out->argv[1] = program;
412 + } else {
413 + argv_array_clear(out);
414 + errno = ENOENT;
415 + return -1;
416 }
417 }
418 +
419 + return 0;
420 }
421
422 static char **prep_childenv(const char *const *deltaenv)
@@ -635,6 +641,12 @@ fail_pipe:
641 struct child_err cerr;
642 struct atfork_state as;
643
644 + if (prepare_cmd(&argv, cmd) < 0) {
645 + failed_errno = errno;
646 + cmd->pid = -1;
647 + goto end_of_spawn;
648 + }
649 +
650 if (pipe(notify_pipe))
651 notify_pipe[0] = notify_pipe[1] = -1;
652
@@ -645,7 +657,6 @@ fail_pipe:
657 set_cloexec(null_fd);
658 }
659
648 - prepare_cmd(&argv, cmd);
660 childenv = prep_childenv(cmd->env);
661 atfork_prepare(&as);
662
@@ -773,6 +784,8 @@ fail_pipe:
784 argv_array_clear(&argv);
785 free(childenv);
786 }
787 +end_of_spawn:
788 +
789 #else
790 {
791 int fhin = 0, fhout = 1, fherr = 2;
t/t0061-run-command.sh
+12 -1
@@ -13,10 +13,14 @@ cat >hello-script <<-EOF
13 EOF
14 >empty
15
16 -test_expect_success 'start_command reports ENOENT' '
16 +test_expect_success 'start_command reports ENOENT (slash)' '
17 test-run-command start-command-ENOENT ./does-not-exist
18 '
19
20 +test_expect_success 'start_command reports ENOENT (no slash)' '
21 + test-run-command start-command-ENOENT does-not-exist
22 +'
23 +
24 test_expect_success 'run_command can run a command' '
25 cat hello-script >hello.sh &&
26 chmod +x hello.sh &&
@@ -26,6 +30,13 @@ test_expect_success 'run_command can run a command' '
30 test_cmp empty err
31 '
32
33 +test_expect_success 'run_command is restricted to PATH' '
34 + write_script should-not-run <<-\EOF &&
35 + echo yikes
36 + EOF
37 + test_must_fail test-run-command run-command should-not-run
38 +'
39 +
40 test_expect_success !MINGW 'run_command can run a script without a #! line' '
41 cat >hello <<-\EOF &&
42 cat hello-script