Kernel: Pass absolute path to shebang interpreter

When you invoke a binary with a shebang line, the `execve` syscall
makes sure to pass along command line arguments to the shebang
interpreter including the path to the binary to execute.

This does not work well when the binary lives in $PATH. For example,
given this script living in `/usr/local/bin/my-script`:

  #!/bin/my-interpreter
  echo "well hello friends"

When executing it as `my-script` from outside `/usr/local/bin/`, it is
executed as `/bin/my-interpreter my-script`. To make sure that the
interpreter can find the binary to execute, we need to replace the
first argument with an absolute path to the binary, so that the
resulting command is:

  /bin/my-interpreter /usr/local/bin/my-script
This commit is contained in:
Jelle Raaijmakers 2021-06-13 17:20:56 +02:00 committed by Andreas Kling
parent 26250779d1
commit 30abfc2b21
Notes: sideshowbarker 2024-07-18 12:17:43 +09:00

View file

@ -849,6 +849,7 @@ KResult Process::exec(String path, Vector<String> arguments, Vector<String> envi
if (!shebang_result.is_error()) {
auto shebang_words = shebang_result.release_value();
auto shebang_path = shebang_words.first();
arguments[0] = move(path);
if (!arguments.try_prepend(move(shebang_words)))
return ENOMEM;
return exec(move(shebang_path), move(arguments), move(environment), ++recursion_depth);