Articles / Security

  • backups
  • threat-modelling
  • restic
  • borgbackup
  • ransomware
  • security

How to Threat Model a Backup Server That Can Delete Its Own Copies

Your backup server destroys backups on purpose, probably every night. It has to, because disks fill up. So alongside "can an attacker delete my backups?", you need to ask "can an attacker get my own pruning job to do it for them?"

The second question is easy to miss, because the deletion runs with proper credentials, from the proper machine, on schedule, and logs a cheerful success.

List every route to "gone"

Before listing attackers, list the ways a copy can stop existing. If a route can destroy data, it goes on the diagram, even when it is a feature you set up yourself.

  • The client that writes backups, if its credential can also delete.
  • The scheduled retention job and its policy file.
  • Root on the backup server, who can ignore the backup tool and use rm.
  • The storage account or hypervisor underneath, including "delete bucket" and "delete volume".
  • Replication that faithfully copies a deletion to the second site.
  • Whoever holds the encryption key or password, since losing or changing access can be as final as deleting.

For each route, write down three things: which credential it needs, where that credential lives, and how long before the damage cannot be undone. The third column is usually the uncomfortable one.

Assume one box at a time is hostile

Take each machine in turn, hand it to the attacker, and ask what survives. Anything that dies with a single compromised box is your first fix.

  • The client: it can send rubbish from now on, but it should not be able to remove last week.
  • The backup server: assume every local protection is switched off. Only a copy it cannot reach or alter counts.
  • The admin machine that prunes: it has full delete rights by design, so it should do very little else. No email, no browsing, no CI jobs.
  • The cloud or hypervisor login: it sits below all your careful backup permissions and does not care about them.

If the backup server prunes itself from its own cron, the second and third boxes are the same box. That can be a reasonable choice for a home setup, but write it down as an accepted risk.

"Append-only" means different things

Read what your particular tool promises, because the label hides real differences.

The restic rest-server --append-only mode allows new backups but prevents existing ones being deleted or modified through the server. It does nothing about someone with a shell on that host.

Borg 1.x is subtler. In append-only mode, borg delete and borg prune are still allowed, but they only mark data as deleted. The Borg notes explain that a later write from a client without the restriction can make those deletions permanent.

Quick detour, because this is a lovely trap. A hostile client marks everything deleted and nothing visibly breaks on disk. Then your trusted admin machine turns up, does its routine housekeeping, and finishes the job. The attacker only has to wait for you. Check the repository's transaction history before writing to it from the privileged side.

The retention policy takes attacker input

A retention policy is a program that decides what to destroy, and some of its input comes from the machines being backed up. In restic, that includes snapshot timestamps.

The restic documentation on forget spells out the attack. Someone with only append access adds empty snapshots dated slightly after the real ones. A rule such as --keep-weekly then keeps the impostor for that week and discards the genuine snapshot. Append-only can hold perfectly and you still lose it when you run forget.

The documented advice for append-only repositories is to use --keep-within, which keeps everything inside a time window instead of picking one winner per period.

Hang on, a window measured from where? From the latest snapshot rather than today. That is handy when a laptop has been off for a month, since its history does not age out. It also means a newly added fake moves the window. The docs warn that if real backups have been missing for longer than the window, an attacker can still get all of them removed.

Put a gate between the plan and the delete

Do not let the policy's output go straight to destruction. Generate a plan, check it, then delete exactly what you checked. This Go program reads restic's dry-run JSON and refuses plans that look odd:

  • any snapshot in a group it is about to prune is dated in the future;
  • a group has no recent snapshot left to keep;
  • the plan removes more than a handful of snapshots.
// prunegate reads `restic forget --dry-run --json` on stdin and prints the
// snapshot IDs to remove, or fails if the plan looks wrong.
package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"os"
	"time"
)

type snapshot struct {
	ID   string    `json:"id"`
	Time time.Time `json:"time"`
}

type group struct {
	Keep   []snapshot `json:"keep"`
	Remove []snapshot `json:"remove"`
}

const (
	maxRemove = 3
	maxAge    = 36 * time.Hour
)

func approve(r io.Reader, now time.Time) ([]string, error) {
	var groups []group
	if err := json.NewDecoder(r).Decode(&groups); err != nil {
		return nil, fmt.Errorf("reading plan: %w", err)
	}
	var ids []string
	for _, g := range groups {
		var newest time.Time
		for _, s := range g.Keep {
			if s.Time.After(newest) {
				newest = s.Time
			}
		}
		for _, s := range g.Remove {
			if s.Time.After(now) {
				return nil, errors.New("snapshot dated in the future")
			}
			ids = append(ids, s.ID)
		}
		if newest.After(now) {
			return nil, errors.New("snapshot dated in the future")
		}
		if now.Sub(newest) > maxAge {
			return nil, errors.New("a group has no recent snapshot to keep")
		}
	}
	if len(ids) > maxRemove {
		return nil, fmt.Errorf("plan removes %d snapshots, limit is %d", len(ids), maxRemove)
	}
	return ids, nil
}

func main() {
	ids, err := approve(os.Stdin, time.Now())
	if err != nil {
		fmt.Fprintln(os.Stderr, "prunegate:", err)
		os.Exit(1)
	}
	for _, id := range ids {
		fmt.Println(id)
	}
}

The wrapper runs on the admin machine, with the repository and password settings already in its environment:

#!/usr/bin/env bash
set -euo pipefail

plan=$(restic forget --dry-run --json --keep-within 30d)
ids=$(prunegate <<<"$plan")

if [ -n "$ids" ]; then
    # Unquoted on purpose: one argument per snapshot ID.
    restic forget $ids
    restic prune
fi

The wrapper passes explicit IDs for a reason: running the policy a second time would let snapshots added after the check change the answer.

A table test of approve should cover at least a normal plan, a stale group, a future date, too many removals and unreadable input. Try the wrapper against a throwaway repository on your restic version before trusting it with a real one.

Know what the gate cannot see

Treat the gate as a tripwire; passing it does not prove the backups are good. Its freshness check trusts the same client-supplied timestamps as restic does.

  • A forged recent snapshot satisfies the age check.
  • A patient attacker can stay under the removal limit and erode history a few snapshots a night.
  • A snapshot full of encrypted rubbish looks fine from its metadata.
  • Root on the admin machine can simply skip the gate.

The limits also need tuning. Three removals suits one machine backing up nightly; ten machines, or a first prune with a backlog, will trip it. When it does trip, look at the plan before raising the number.

Keep one copy that says no

You want at least one copy whose deletion rules your backup system cannot change. That might be an offline disk, a second server that pulls and keeps its own history, or object storage with a lock.

With S3 Object Lock, the mode matters:

  • Governance mode can be bypassed by a principal with s3:BypassGovernanceRetention who asks for the bypass.
  • Compliance mode stops any user, including the account root, deleting or overwriting a protected version until retention expires.

A locked version can still be hidden behind a delete marker or a newer version. The old bytes survive, but your restore procedure must know how to find them. Compliance mode also means your own mistakes stay, and stay billable, so test with short retention first.

S3-compatible services do not all behave identically, so try to delete a locked version yourself, using the most powerful credential you have, and see what happens.

Give yourself time to notice

Retention is also your detection deadline. If you keep 30 days and take 40 to notice that something has been quietly wrong, the clean copy may have gone.

So the last part of the model is who finds out, and how:

  • Alert from somewhere other than the backup server when snapshot counts drop or backups stop arriving.
  • Treat a refused prune as an alert, not a line in a log nobody reads.
  • Restore real files on a schedule and compare them with something known.
  • Have a written way to pause all pruning quickly. During an incident, a full disk is a better problem than a tidy empty one.