userver-framework / userver

Production-ready C++ Asynchronous Framework with rich functionality
https://userver.tech
Apache License 2.0
2.36k stars 272 forks source link

Account for PATH in ProcessStarter::Exec #588

Closed Anton3 closed 1 month ago

Anton3 commented 3 months ago

As of now, Exec requires the absolute path to the executable and ignores PATH. This is inconvenient when calling system executables.

This behavior is dictated by execve, which is currently used. The solution is to use execvpe instead. But it contains a potential vulnerability.

The execlp(), execvp(), and execvpe() functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. The file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable. If this variable isn't defined, the path list defaults to the current directory followed by the list of directories returned by confstr(_CS_PATH). (This confstr(3) call typically returns the value "/bin:/usr/bin".)

If PATH is not defined (which is quite an exotic situation), then execvpe looks in the current directory.

The following logic is suggested:

if (!path.contains('/') && !env.contains("PATH")) {
  throw ...
}
execvpe

Internal issue: TAXICOMMON-8853

apolukhin commented 1 month ago

Fixed in https://github.com/userver-framework/userver/commit/575fe1623b5df323c6898e62d609f308da83dedd