26.3. Managing Credentials and Sensitive Data
A Debian host holds several kinds of secrets: service passwords, API
tokens, TLS private keys, and the administrative credentials that reach
them. Each kind leaks differently - through file permissions, through
process arguments visible in /proc, through backups that copy secrets
along with everything else - and each needs a rotation path for the day
it leaks. This section works the file, process, and backup leak paths,
rehearses rotation on the SSH credential every administrator has, places
TLS trust in its Debian context, and closes the privilege side-channels
that survive all of the above: setuid programs and file capabilities.
26.3.1. Protecting Secrets in Files, Processes, and Backups
The file path first. A secret in a file is protected by exactly two things: the file's permission bits and the privilege of the accounts that can read them. Create a practice secret and set both deliberately. On sec1, as john, from the workspace:
$ printf 'db_password=Example-Secret-Value\n' > secret.env
$ chmod 600 secret.env
$ stat -c '%a %U:%G %n' secret.env
600 john:john secret.env
The stat line is the audit form of the fact: mode 600, owned by john,
readable by no other account. The two mistakes to check for on a real
host are mode-644 secret files - the default a plain printf > file
leaves behind, which is why the explicit chmod matters - and secret
files owned by a service account but group-readable by a broader group.
The process path leaks differently: anything passed as a command
argument is visible to every user on the host through /proc, because
the kernel publishes each process's argument vector to
/proc/PID/cmdline, readable by all. Watch it happen. On sec1, as
john, start a background process whose name looks like a token -
exec -a replaces the argument the kernel reports:
$ bash -c 'exec -a ExposedToken-Example sleep 120' &
bash prints the job number and process ID and nothing else; the job keeps running in the background. Inspect it as any local user could:
$ ps -p $! -o pid,args
PID COMMAND
98430 ExposedToken-Example 120
$! expands to the background job's process ID - 98430 in this run -
and there is the token-shaped string, readable by anyone with shell
access to the host, no exploit required. The defense is structural:
secrets belong in files, environment blocks, or the credentials
directory of Section 26.2.4, never in arguments - which includes the
--password flag many client programs offer. Clean up:
$ kill 98430
Silent; the background job ends.
The backup path is the quiet one: a backup that faithfully copies a filesystem also copies every secret on it, into a file that gets relaxed permissions, off-host storage, and a longer lifetime than the host itself. The remedy is to encrypt the backup itself when it leaves. GnuPG does this with symmetric encryption - one passphrase, no key infrastructure, the right shape for archival copies. First put the secret into an archive, as john on sec1:
$ tar -czf secret-backup.tar.gz secret.env
Silent; the archive now contains the secret at the workspace's permissions. Encrypt it:
$ gpg --batch --yes --symmetric --cipher-algo AES256 --passphrase-file luks-passphrase.txt -o secret-backup.tar.gz.gpg secret-backup.tar.gz
$ rm secret-backup.tar.gz
$ ls -l secret-backup.tar.gz.gpg
-rw-rw-r-- 1 john john 245 Nov 23 16:36 secret-backup.tar.gz.gpg
The gpg and rm steps are silent; the listing confirms what exists now: the encrypted copy, and the plaintext archive gone. The options in use:
| Option | Description |
|---|---|
--symmetric |
Encrypt with a passphrase instead of a public key |
--cipher-algo AES256 |
Choose the symmetric cipher |
--batch |
Skip the prompts; used here so the exercise is scripted |
--passphrase-file FILE |
Read the passphrase from FILE in batch mode |
-o FILE |
Name the ciphertext output file |
--decrypt |
Decrypt (prompting for the passphrase without --batch) |
The batch form is used so this exercise is reproducible; without
--batch and --passphrase-file, gpg prompts for the passphrase twice
and hides the typing, which is the normal interactive path. The
exercise reuses the LUKS passphrase file for convenience; in practice
a backup passphrase is distinct from every other secret and is held
apart from the host whose data it protects - once the backup leaves,
that passphrase is the only thing standing between the archive and
whoever finds it. See what happens with the wrong passphrase:
$ gpg --batch --yes --decrypt --passphrase wrong-passphrase secret-backup.tar.gz.gpg > /dev/null
gpg: AES256.CFB encrypted data
gpg: encrypted with 1 passphrase
gpg: decryption failed: Bad session key
The ciphertext refuses the wrong key without leaking anything about its
contents. With the right one, the archive listing comes back through the
pipeline - gpg decrypts to standard output and tar -tzf - reads the
archive from it:
$ gpg --batch --quiet --decrypt --passphrase-file luks-passphrase.txt secret-backup.tar.gz.gpg | tar -tzf -
secret.env
One pipeline, two stages: gpg writes the plaintext archive to its standard output, and tar lists that stream. The file inside is intact, and the copy on disk is only as safe as the passphrase. The same discipline at scale - encrypted media, distinct credentials, off-host copies, restore rehearsal - is Chapter 15's subject; what belongs to this chapter is the property that secrets in backups stay secrets.
26.3.2. Rotating Service Credentials and Revoking Access
Rotation is the operation you perform when a credential may have leaked,
and it only counts if the old credential actually stops working. The
SSH key is the right one to rehearse on because the mechanism is fully in
your hands. john on secclient holds a chapter-dedicated key pair,
~/.ssh/id_ed25519_ch26, whose public half is installed in
~/.ssh/authorized_keys on sec1; Section 20.2.4 covers the
full treatment including authorized_keys restrictions and revocation
lists. The rotation pattern has three steps in a fixed order: authorize
the replacement, verify it, then withdraw the old one - never the reverse,
or a mistake locks everyone out.
Generate the replacement key on secclient, as john:
$ ssh-keygen -t ed25519 -N '' -f ~/.ssh/id_ed25519_ch26_new
The command prints its key fingerprint and a randomart image; -N ''
gives the key no passphrase and -f names the file. Append the new
public key to john's authorized keys on sec1 - copy the single line from
~/.ssh/id_ed25519_ch26_new.pub into ~/.ssh/authorized_keys in john's
session there, with the editor. The file's mode-600, john-owned state is
already correct. Verify the new key works before touching the old one,
on secclient, as john:
$ ssh -i ~/.ssh/id_ed25519_ch26_new john@web.sec1.lab 'id; hostname'
uid=1001(john) gid=1001(john) groups=1001(john)
sec1
Now withdraw the old credential: delete the old public-key line from
~/.ssh/authorized_keys on sec1. The old key immediately stops
authenticating, which the client demonstrates:
$ ssh -i ~/.ssh/id_ed25519_ch26 john@web.sec1.lab id
john@web.sec1.lab: Permission denied (publickey).
The server offered no key the account accepts, so the connection fails
before any shell exists. The rotation is complete: the new key is the
working credential, the old one is refused everywhere. To end the
exercise with the chapter's usual access restored, put the old
public-key line back into ~/.ssh/authorized_keys on sec1, confirm it
works again, and remove the temporary replacement:
$ ssh john@web.sec1.lab id
uid=1001(john) gid=1001(john) groups=1001(john)
$ rm -f ~/.ssh/id_ed25519_ch26_new ~/.ssh/id_ed25519_ch26_new.pub
Silent. The same three-step pattern - authorize, verify, withdraw - applies to every service credential: database passwords get a second account before the first is disabled, API tokens get reissued before the old token is revoked, and the verification step is always run from a second session, because the session doing the rotation may be the one the old credential keeps alive.
26.3.3. TLS Trust Stores and Private Certificate Authorities
TLS trust on a Debian host is anchored in one place: the system trust
store, /etc/ssl/certs, a set of symlinks assembled by
update-ca-certificates from the certificates in
/usr/local/share/ca-certificates/ and the packaged
ca-certificates bundle. Everything that links against OpenSSL, GnuTLS,
or NSS - curl, git, python, apt's https transport - verifies against
that store by default. Its management commands and their --fresh
rebuild semantics are covered in
Section 23.7.1, which also demonstrates
withdrawing a root from it.
A private CA enters the picture when the names you need to protect are
not reachable by the public PKI: internal hostnames like
web.sec1.lab, raw IP addresses, or services whose certificate policy
you want under your own control rather than a public issuer's. The
placement options differ in scope:
| Trust placement | Where it lives | Who it affects | When to choose it |
|---|---|---|---|
| System trust store | /usr/local/share/ca-certificates/ + update-ca-certificates |
Every TLS client on the host | One organization-wide root every host should trust |
| Per-command CA file | openssl -CAfile, curl --cacert and similar |
A single invocation | Verifying a CA before distributing it, or isolating trust to one workflow |
| Application trust list | nginx ssl_client_certificate, an application's CA config |
The application's peers only | Mutual TLS gates where clients, not servers, present certificates |
Container runtime certs.d |
~/.config/containers/certs.d/<registry>/ca.crt |
One user's runtime, one registry | Registry pulls without granting the runtime host-wide trust |
The narrow placement is the disciplined one: a host that trusts a private CA only for the one registry that uses it has a smaller blast radius the day that CA is compromised, which is the thread Section 26.5 pulls throughout. What the store-based options share is the object being trusted - a root certificate, never a leaf - and the rule that follows: distribute the root widely by one of these paths, and let each connection deliver its own leaf and intermediate, as the next section's figure shows. For publicly reachable names, none of this is needed: Section 23.4.2 issues certificates for public names through ACME, whose automated challenge-and-renewal cycle is the correct tool there. A private PKI is for the names ACME cannot reach and the policies you want to run yourself.
26.3.4. Limiting SUID Programs and File Capabilities
Setuid programs are a designed privilege boundary: run by any user, they
take the file owner's identity - sudo and passwd could not work
otherwise. The risk is that each one is a standing offer to turn an
ordinary account into root, so an inventory belongs in the baseline and
every unexplained entry in it belongs in an incident report. List them.
On sec1, as john:
$ sudo find / -xdev -type f -perm -4000 2>/dev/null | sort
/usr/bin/chfn
/usr/bin/chsh
/usr/bin/fusermount3
/usr/bin/gpasswd
/usr/bin/mount
/usr/bin/newgrp
/usr/bin/ntfs-3g
/usr/bin/passwd
/usr/bin/su
/usr/bin/sudo
/usr/bin/umount
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/usr/lib/openssh/ssh-keysign
/usr/lib/polkit-1/polkit-agent-helper-1
/usr/sbin/exim4
-perm -4000 matches the setuid bit and -xdev keeps the scan to one
filesystem, so a stray mount cannot slow or confuse it. The list is
Debian 13's stock set - every entry should be nameable, and on this
host each one is. Watch the mechanism work with a deliberately
unnecessary one:
$ sudo cp /usr/bin/id /usr/local/bin/id-suid && sudo chmod u+s /usr/local/bin/id-suid
$ ls -l /usr/local/bin/id-suid
-rwsr-xr-x 1 root root 51656 Nov 23 16:36 /usr/local/bin/id-suid
The s in the permission field is the bit. Run it as john:
$ /usr/local/bin/id-suid
uid=1001(john) gid=1001(john) euid=0(root) groups=1001(john)
euid=0(root) - the program executes with root's effective identity from
the first instruction, regardless of who launched it. Remove the bit and
the same program becomes harmless:
$ sudo chmod u-s /usr/local/bin/id-suid
$ ls -l /usr/local/bin/id-suid
-rwxr-xr-x 1 root root 51656 Nov 23 16:36 /usr/local/bin/id-suid
$ /usr/local/bin/id-suid
uid=1001(john) gid=1001(john) groups=1001(john)
The effective identity is john's again. Remove the demonstration file
with sudo rm /usr/local/bin/id-suid (silent) when you are done with
it. Two management facts complete the picture: dpkg-statoverride can
register a deliberately removed setuid bit so a package upgrade does
not silently restore it, and a setuid inventory taken at different times
differs meaningfully - a new entry you did not install is the
strongest single indicator of a compromised package.
File capabilities are setuid's narrow replacement: instead of the whole
root identity, a program receives one kernel capability, and only while
running that program. The classic case is binding a port below 1024. The
demonstration program, saved as bindlow.c in the workspace, binds TCP
port 81 on the loopback address and reports success:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void)
{
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(81),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
};
if (s < 0 || bind(s, (struct sockaddr *)&addr, sizeof addr) < 0) {
perror("bind");
return 1;
}
puts("bound port 81");
close(s);
return 0;
}
Compile and try it unprivileged, on sec1, as john:
$ sudo gcc -O2 -Wall -o /usr/local/bin/bindlow bindlow.c
The compile is silent. Now the attempt:
$ /usr/local/bin/bindlow
bind: Permission denied
Binding a low port needs CAP_NET_BIND_SERVICE, and an ordinary process
does not have it. Grant exactly that capability to the file:
$ sudo setcap cap_net_bind_service=ep /usr/local/bin/bindlow
$ getcap /usr/local/bin/bindlow
/usr/local/bin/bindlow cap_net_bind_service=ep
The capability tools:
| Command | Description |
|---|---|
setcap CAP=ep FILE |
Grant CAP to FILE, effective and permitted on execution |
setcap -r FILE |
Remove all file capabilities from FILE |
getcap FILE / getcap -r DIR |
Show capabilities on a file, or recursively from a directory |
The ep suffix means the capability becomes effective and permitted
when the file executes. Run the same program again:
$ /usr/local/bin/bindlow
bound port 81
One capability, granted to one file, was enough - no root identity, no permanent daemon privilege, nothing the program can pass to a shell. The trade is real, though: a file capability survives in the file's extended attributes, so it must be inventoried like setuid, and a compromised binary with a granted capability is compromised with that capability. Audit both classes at once:
$ sudo getcap -r / 2>/dev/null | sort
On the practice host this prints nothing at all - no file carries
capabilities - which is itself a baseline fact worth recording, because
getcap -r / on a host that should be clean is the one-line check
that catches an attacker's quietly privileged helper. Remove the
demonstration capability and binary when done:
$ sudo setcap -r /usr/local/bin/bindlow
$ sudo rm /usr/local/bin/bindlow
Both silent; setcap -r leaves the file ordinary again.