9

Suppose there's a binary application that always writes its data to /tmp.

How could I spoof/mock /tmp for the sake of this binary as some other directory (e.g. home/tmp)?

Let's assume I have no means of modifying this binary to force it to use a different directory.

2 Answers 2

11

You can run the application in a chroot environment i.e. the / the application sees is not the real /. You create a complete new file system hierarchy and mount (--bind) everything you need into it. The relevant point is: You can mount the real ~/tmp to the /tmp in the chroot environment.

Instead of using chroot (which requires superuser privilege) you may do more or less the same with Linux containers (lxc). I am not familiar with lxc but as it's a normal user process to the host system you do not need to be the superuser for such configurations within the container.

8
  • Great answer, I hadn't heard of either chroot or lxc being capable of doing this. It's also really nice to know there's a way of accomplishing it without being a superuser. Commented Jun 4, 2014 at 14:57
  • 3
    @Nobilis 98 upvotes missing for this to be recognized as a great answer... Commented Jun 4, 2014 at 15:00
  • 2
    Beware, however, that chroot requires additional setup (you are replacing the whole of /, not just /tmp, so any access to /etc, /var, etc, will also be inside the "jail") and creates security concerns of its own (the "jailed" program may be able to manipulate parts of the file system which would normally be off-limits if you aren't careful with the permissions when setting up your fake /). Commented Jun 4, 2014 at 18:03
  • @IMSoP Would you mind explaining "may be able to manipulate parts of the file system which would normally be off-limits" in more detail? Commented Jun 4, 2014 at 18:23
  • @HaukeLaging If the new / is not restricted to be written only by root, the "jailed" user can create or replace files which seem to be in key system locations, such as /etc/passwd; this can then be used for privilege escalation which would not be possible outside the chroot. Many Linux FTP servers, which traditionally use chroot to hide the rest of the filesystem, now refuse to do so if the directory is writeable by a non-root user. Commented Jun 4, 2014 at 18:46
8

Most POSIX compliant software would honor the TMPDIR environment variable e.g.

env TMPDIR=~/mytmp /path/to/application 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.