I'm writing a collection of utilities in bash
that has a common library. Each script I write has to have a block of code that determines the path of the library relative to the executable. Not the actual code, but an example:
#!/bin/bash
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $DIR/../lib/utilities/functions
Instead of a preamble that tries to search for the library, I had the bright idea to use an environment variable to indicate the libraries location.
#!/bin/bash
. $TOOLS_LIBRARY_PATH
I could use a wrapper program to set that environment variable, or I could set it in my own path. There may be better ways to organize a bash
toolset, but question is:
Can I trust my environment variables?
This is one of those, I've-never-really-thought-about-it kind of questions. When programming in other languages, paths are used to find libraries, (e.g. LD_LIBRARY_PATH
, PYTHONPATH
, PERLLIB
, RUBYLIB
, CLASSPATH
, NODE_PATH
), but I've never had to stop and think about how that could be insecure.
In fact, LD_LIBRARY_PATH
has Why LD_LIBRARY_PATH
is bad to discourage its use. The Ruby and Perl library path environment variables are ignored if their security mechanisms are invoked, $SAFE
and -T
(taint mode) respectively.
My thoughts so far...
- The user could set
TOOLS_PATH_LIBRARY
to a library of their choosing, but the utility will run under their uid. They could simply run their malicious library directly withbash
. - My tools
sudo
some things. Someone could set theirTOOLS_PATH_LIBRARY
to something that takes advantage of this. But, the tools are not run viasudo
, they only invokesudo
here and there. The user would have to be asudoer
in any case, they could just callsudo
directly. - If I can't trust
TOOLS_PATH_LIBRARY
, then I can't trustPATH
. All program invocations must use absolute paths. - I've seen shell programs use aliases for programs that are absolute, so that instead of calling
ls
, use a variable, likeLS=/bin/ls
. From what I've read, this is to protect against users redefining program defaults as aliases. See: PATH, functions and security. Bash scripting best practices. . - Perl's taint mode treats all environment variables as "tainted", which foreboding, which is why I'm trying to reason about the risks of environment.
- It is not possible for one users to change another's environment, unless that user is root. Thus, I'm only concerned about a user changing their own environment to escalate privileges. See: Is there a way to change another process's environment variables?
I've rubber ducked this into an answer of sorts, but I'm still going to post it, since it isn't a pat answer.
Update: What are the security issues surrounding the use of environment variables to specify paths to libraries and executables?