18

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 with bash.
  • My tools sudo some things. Someone could set their TOOLS_PATH_LIBRARY to something that takes advantage of this. But, the tools are not run via sudo, they only invoke sudo here and there. The user would have to be a sudoer in any case, they could just call sudo directly.
  • If I can't trust TOOLS_PATH_LIBRARY, then I can't trust PATH. 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, like LS=/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?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Alan Gutierrez
  • 15,339
  • 1
  • 18
  • 17
  • 5
    Of course you can't. You can't trust anything, ever. But if you don't assume it is correct, where are you? You can't do anything. – Chris Morgan Apr 10 '12 at 13:48
  • If you can make it resolve all directories without extra variables make it such, be kind to your direct users. But also leave the backdoor in the form of the environment variable to be kind to those who's going to be integrating with your library. Assume variables are set correctly (see Chris Morgan's comment for "why"). – bobah Apr 10 '12 at 13:52
  • 1
    Nice post, you've identified a lot of issues. Interesting stuff, but seems you're bordering on analysis paralysis (as Chris says). Security is about layers, right? If you can't trust your users, you need more layers to keep them from shooting you in the foot. People can get a PHD in security issues, and still get their MilSpec or banking-secure systems get hacked, so you'll have to figure out where the trade-off in time spent between preparing for an attach versus the probability of successful attach is for your current project, its legitimate users and other stake holders. Good luck. – shellter Apr 10 '12 at 13:59
  • 2
    What exactly are you trying to protect from? A user (with shell access) running things under his own UID which you didn't intend doesn't sound like a threat to me. Even changing what you put into `sudo` – as long as running the programs allowed in `sudoers` is not prone to attacks. Yes, they can set `TOOLS_PATH_LIBRARY` to something weird. They also can do that with `${BASH_SOURCE[0]}`, that is only marginally more difficult (for non-`sudo` invocations). – Christopher Creutzig Apr 10 '12 at 15:08
  • @christopher-creutzig Yes, it seems that any action a user could take by evaluating code in `TOOLS_LIBRARY_PATH`, they could just as easily take directly. Yes, it seems that `TOOLS_LIBRARY_PATH` is no more or less secure than `PATH` itself. I'm wondering if there is something I'm not considering. Maybe my question is; what are the security issues surrounding the use of environment variables to specify paths to libraries and executables? – Alan Gutierrez Apr 10 '12 at 16:22
  • @AlanGutierrez: I updated my answer to address your updated question, using CVE-2010-3847 as an example. – Corey Henderson Apr 10 '12 at 17:08

3 Answers3

4

While mechanisms exist in various programs to prevent the modification of environment variables, the bottom line is, no you can't trust environment variables. The security concern is very basic:

Anytime a user can change what is expected to be executed, the possibility for a vulnerability to be exploited arises.

Case-in-point, have a look at CVE-2010-3847. With that, an underprivileged attacker with write access to a file system containing a setuid or setgid binaries could use this flaw to escalate their privileges. It involves an environment variable being modified by the user.

CVE-2011-1095 is another example, and doesn't involve SUID binaries. Do a google search for "glibc cve environment" to see the kinds of stuff that people were able to do with environment variable modifications. Pretty crafty.

Your concerns really boil down to your statement of:

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 with bash.

Key phrase here - run their malicious library. This assumes their library is owned by their UID as well.

This is where a security framework will do you lots of good. One that I have written that focuses exclusively on this problem you can find here:

https://github.com/cormander/tpe-lkm

The module stops execve/mmap/mprotect calls on files that are writable, or not owned by a trusted user (root). As long as they're not able to put malicious code into a file/directory owned by the trusted user, they can't exploit the system in this way.

If you're using SUID binaries or sudo that include those variables, you might want to consider enabling the "paranoid" and "strict" options to stop even root from trusting non-root owned binaries.

I should mention that this Trusted Path Execution method protects direct execution with binaries and shared libraries. It does little (if anything) against interpreted languages, as they parse bytecode and not execute it directly. So you still need some degree of care with PYTHONPATH, PERLLIB, CLASSPATH, etc and use the language's safety mechanisms that you mentioned.

Corey Henderson
  • 7,239
  • 1
  • 39
  • 43
  • CVE-2010-3847 involves a SUID binary, since the OP doesn't have any of their own SUID code (fixing `sudo` is outside their remit, since any attack of sudo inside their code can be exploited as easily from outside), I don't think this can be a risk? – Douglas Leeder Apr 10 '12 at 17:19
  • @DouglasLeeder: that was just an example. I included CVE-2011-1095 as well. The list goes on and on, I just can't remember them all off the top of my head. – Corey Henderson Apr 10 '12 at 17:24
  • "Anytime a user can change what is expected to be executed, the possibility for a vulnerability to be exploited arises." - I think you have to add 'at a different security level', otherwise the user altering what command they're typing falls foul of that. – Douglas Leeder Apr 10 '12 at 17:42
2

Short answer:

Assuming the user is able to run programs and code of her own choice anyway, you do not have to trust anything they feed you, including the environment. If the account is limited in some ways (no shell access, no write access to file systems that allow execution), that may change the picture, but as long as your scripts are only doing things the user could do herself, why protect against malicious interference?


Longer answer:

There are at least two separate issues to consider in terms of security problems:

  • What and whom do we need to guard against?
    • (closely related question: What can our program actually do and what could it potentially break?)
  • How do we do that?

If and as long as your program is running under the user ID of the user starting the program and providing all the input (i.e., the only one who could mount any attack), there are only rare situations where it makes sense at all to harden the program against attacks. Anti-copy protection and sharing high-scores comes to mind, but that group of things is not just concerned with inputs, but probably even more so with stopping the user from reading code and memory. Hiding the code of a shell script without some kind of suid/sgid trickery is nothing I'd know how to do; the best I could think of would be obscuring the code.

In this situation, anything the user could trick the program into doing, they could do without the help of the tool, too, so trying to “protect” against attacks by this user is moot. Your description does not read as if you'd actually need any protection against attacks.


Assuming you do need protection, you simply cannot rely on environment variables – and if you fail to reset things like LD_LIBRARY_PATH and LD_PRELOAD, even calling tools with absolute paths like /bin/id or /bin/ls won't give you a reliable answer, unless that tool happens to be statically compiled. This is why sudo has env_reset enabled by default and why running suid programs has to ignore certain environment variables. Note that this means that your point that TOOLS_PATH_LIBRARY and PATH are equally trustworthy may be true in your situation, but is not necessarily true in other situations' border cases: a sysadmin may reset PATH for sudo usage, but let non-standard environment variables pass through.

As pointed out above, argv[0] (or its bash equivalent ${BASH_SOURCE[0]}) is no more reliable than environment variables. Not only can the user simply make a copy or symlink of your original file; execve or bash's exec -a foo bar allows putting anything into argv[0].

Christopher Creutzig
  • 8,656
  • 35
  • 45
  • No, this helps. This addresses what I'm asking. Maybe I'll put more backstory in my next question. I'm not trying to hide code or protect data. I had the bright idea to use an environment variable as a path to a library, typed it out, and then thought it looked an awful lot like an `eval`. I thought, this has got to be a face palming security hole. The more I thought about it, the more I thought not. Now I'm thinking not. – Alan Gutierrez Apr 11 '12 at 08:59
  • So, what you're saying then is: "If and as long as your program is running under the user ID of the user starting the program and providing all the input (i.e., the only one who could mount any attack)," then you **don't have to trust** the environment. – Alan Gutierrez Apr 11 '12 at 09:14
  • Assuming the user is able to run programs and code of her own choice anyway, yes. If the account is limited in some ways (no shell access, no write access to file systems that allow execution), that may change the picture, but as long as your scripts are only doing things the user could do herself, why protect against malicious interference? – Christopher Creutzig Apr 12 '12 at 06:37
  • Christopher: I like your answer. May I be so bold as to suggest stripping the preamble to make it a better answer? "As long as your program is running under the user ID of the user starting..." That is the substance of your answer. While you're at it, remove "Let me add that I don't think I've added all that much..." It's not true. – Alan Gutierrez Apr 13 '12 at 19:57
  • Christopher: If I may be so bold; can you punch up your answer? The crux of your answer starts with "As long as your program is running under the user ID of the user starting the program..." I believe it would be a stronger answer for the skimming reader if it lead with that paragraph. I believe you buried the lede. – Alan Gutierrez Apr 13 '12 at 20:00
0

You can never trust someone else's environment.

Why not simply create a new user that contains all of this important code. Then, you can either get the information directly from /etc/passwd or use the ~foo syntax to find the user's home directory.

# One way to get home directory of util_user
DIR=$(/usr/bin/awk -F: '$1 == "util_user" {print $6}' /etc/passwd)

# Another way which works in BASH and Kornshell
[ -d ~util_dir ] && DIR=~util_dir


# Make sure DIR is set!
if  [ -z "$DIR" ]
then
   echo "Something's wrong!"
   exit
fi
David W.
  • 105,218
  • 39
  • 216
  • 337