Part 2 · System administration · Lesson 10 · 35 minutes
Build a program from source and control what the shell runs
- Build a small program from source and install it into a folder of your own
- Explain how PATH decides which program the shell starts
- Spot a PATH hijack and pass a variable to a child program with export
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
Your lead needs a small tool that has no package for your distribution, so you build it from source. She also asks a second question that she has met in an incident report: "Can anybody make our shell start the wrong program when we type a plain name such as ls?" You build the tool into a folder of your own, where it cannot harm the system. Then you read how the shell chooses a program, and you plant a fake one in your lab to see the attack.
Recall
What does "dpkg -S /usr/bin/tree" tell you, and why is a file that no package owns suspicious?
It names the package that owns the file. A file in /usr/bin that no package owns was copied there by hand, and somebody must explain why. It can be an intruder's program.
What is the difference between "apt remove" and "apt purge"?
remove deletes the program and keeps its settings. purge deletes the settings too.
The idea
A package manager installs ready programs. When a program has no package, you get its source: the text files that a compiler (a program that turns source code into a program that the processor can run) turns into the program. The tool make reads a Makefile, which holds rules: "to make this file, run these commands". A rule named install copies the result into a folder tree below a prefix. The prefix /usr/local needs root. A prefix in your own folder does not.
An installed program is only useful if the shell can find it. The shell keeps a list of folders in the variable PATH. It looks in each folder in order, and runs the first match.
- Source folder → make: the Makefile runs the compiler
- make → make install: copy it to PREFIX/bin
- make install → PATH lookup: the shell reads each folder of PATH
- PATH lookup → Program runs: the first match wins
A program in a folder that is not in PATH runs only with its full path. A program in an early folder of PATH wins over a later one with the same name. Both facts matter for defence.
Worked example
1. Build a program from source
Some software has no package. The usual way is ./configure (check the system), make (build) and make install (copy the files). This step builds a tiny C program with the last two. It is too small to need ./configure. You do not have to read the C: the program prints one line.
make is a tool that reads a Makefile and runs the rules in it (sudo apt install make gcc if make or the compiler cc is missing). The rule hello runs cc -o hello hello.c: the compiler cc reads the source hello.c and writes the program hello. The PREFIX variable says where to install: /usr/local is the default, and a folder of your own needs no root.
A rule line normally starts with a tab. The line .RECIPEPREFIX = > lets this lesson use > instead, because a tab is lost when you copy text. Save the two files in the folder hello-1.0.
hello-1.0/hello.c
#include <stdio.h>
int main(void)
{
puts("hello from source, version 1.0");
return 0;
}hello-1.0/Makefile
PREFIX ?= /usr/local
.RECIPEPREFIX = >
all: hello
hello: hello.c
> cc -o hello hello.c
install: hello
> install -D hello $(PREFIX)/bin/helloPack the folder, write its checksum, unpack it like a verified download, then build and install into your own folder. make -n prints what a rule would do and runs nothing.
cd ~/krlab/linux-admin
tar -czf hello-1.0.tar.gz hello-1.0
sha256sum hello-1.0.tar.gz > hello-1.0.tar.gz.sha256
rm -r hello-1.0
sha256sum -c hello-1.0.tar.gz.sha256
tar -xzf hello-1.0.tar.gz
cd hello-1.0
make
make install PREFIX=$HOME/krlab/linux-admin/local
~/krlab/linux-admin/local/bin/hello
make -n install PREFIX=/usr/local
cd ..hello-1.0.tar.gz: OK
cc -o hello hello.c
install -D hello /home/you/krlab/linux-admin/local/bin/hello
hello from source, version 1.0
install -D hello /usr/local/bin/helloA program installed from source is not in the package database: dpkg -S does not know it, and apt upgrade will never patch it. Keep a note of what you built. Read the Makefile before you run sudo make install, because it runs as root.
2. See how the shell finds a program
An environment variable is a named text value that a process passes to its children. PATH is the most important: a list of folders, separated by :. When you type ls, the shell looks for a file with that name in each folder in order, and runs the first one it finds. echo "$PATH" prints it, tr ':' '\n' turns each colon into a line end, and type -a NAME lists every program that the shell could start under that name.
echo "$PATH" | tr ':' '\n' | head -n 4
type -a ls | head -n 2/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
ls is /usr/bin/ls
ls is /bin/lsThe two ls are one file, because /bin is a link to /usr/bin. Order matters: a folder at the front wins. This is also why a program that you install into a folder outside PATH runs only with its full path.
Now put a fake ls in a folder, and that folder first in PATH. The brackets keep the change inside a sub-shell, and command -v NAME prints the file that the shell would start.
cd ~/krlab/linux-admin
mkdir -p bin
printf '#!/bin/bash\necho "gotcha: fake ls"\n' > bin/ls
chmod +x bin/ls
( PATH="$PWD/bin:$PATH"; command -v ls; ls )
command -v ls/home/you/krlab/linux-admin/bin/ls
gotcha: fake ls
/usr/bin/ls- Attacker → Fake ls: plants a fake ls in folder 1
- You → Fake ls: type ls: folder 1 is read first
- You → Real ls (blocked): folder 2 is never reached
This is PATH hijacking. It works when an attacker can write to a folder early in a victim's PATH, or when the PATH holds . or an empty entry (both mean "the current folder").
3. Pass a variable to a program
export NAME=value makes a variable go to every program that you start afterwards, and unset removes it. Do not keep a secret in a variable: /proc/PID/environ shows it to anyone who may read it. A key in a command line is worse, because ps shows it to every user.
export API_KEY=abc123
bash -c 'echo "child sees $API_KEY"'
unset API_KEYchild sees abc123Your turn
Do this in ~/krlab/linux-admin. There are no steps here.
- Install the program of step 1 a second time, with the prefix
~/krlab/linux-admin/opt. - Run it by its full path. Then ask
command -v helloand read the exit status withecho $?. The folderopt/binis not in yourPATHyet. - In a sub-shell that has
opt/binfirst inPATH, runhelloby its name. - An administrator wrote
PATH=/home/dev/bin:.:/usr/bin::/bininto a start file. Read it like the shell does, one folder at a time.
Check yourself
Why is a PATH that starts with "." dangerous?
. is the current folder. If you cd into a folder where an attacker left a file named ls, then typing ls runs the attacker's file with your rights.
You built a tool from source into /usr/local. Next month apt upgrade runs. Is the tool patched?
No. The package manager does not know the tool. You must watch its releases yourself, and build it again. This is the main cost of a source install, and the reason to prefer a package.
What does "make -n install" do, and why run it first?
It prints the commands of the rule and runs none of them. You see what install would copy and where, before it changes anything.
Why is a secret in an exported variable not safe?
Every child program gets a copy, and /proc/PID/environ shows it to anyone who may read that file. Pass a secret in a file with a tight mode, not in the environment or on a command line.
Field notes
- Read the
Makefilebefore you runsudo make install: its rules run as root. - Scripts that run with
sudoor from cron should setPATHexplicitly at the top, or call programs by their full path. - Keep a note of everything that you build from source: version, source, date and prefix. Nobody else will patch it.
sudosets its own safePATH(secure_pathin the sudoers file), so a hijack through your ownPATHdoes not reach a command that you run withsudo.