SD Core for Linux LC2.0-0

Encrypting the data SD keeps on disk

Putting SD's four data trees on an encrypted volume, what that protects, what it does not, and how the machine must be unlocked at boot.

This page is about the bottom layer in the table on the encryption page: the disk. SD itself is unchanged by it. The encryption is done by the operating system, to the volume the SD directories live on. The encryption page explains the other two layers, which are the application's.

SD folds case, so a command may be typed in either case. Commands are shown here in lower case.

The short version#

Encrypt the volume, then move the four data trees onto it. A stolen or discarded disk, or a backup copied off the machine, then holds ciphertext. It protects nothing once the volume is open. While SD is running on an open volume, anyone who can log in as root, as the sdsys administrator or as an SD user reads the data as plaintext, and so does anyone who mounts it.

Two things must be set up for this to hold, and neither is done by the installer:

  1. SD must not start on the bare directories when the volume is absent. An ordering drop-in does this (see below).
  2. Swap must be encrypted or switched off, because SD's shared memory can be paged out to it.

Where SD keeps its data#

Path What is there
/usr/local/sdsys the register (accounts/), $cred/, the audit file, the error log, dumps/ and the binaries
/home/sd user_accounts and group_accounts, the account files
/var/lib/sd-net per-user, per-server SDNet credentials, mode 600
/etc/sd-tls api.pem, the TLS private key, root-owned

/etc/sd.conf stays plaintext. It holds paths only, and it is read before any of the volume is mounted. Keep it on the root filesystem.

The shared memory segment (0x53434c01 on a standard install) is in RAM and is not a file. It is recreated each time SD starts.

The sort work area is not under SDSYS. SD's default places are TEMPDIR, which is $TMP or /tmp, and SORTWORK, which follows TEMPDIR. The sort files are named ~SDS<user>.<n>. A sort that does not fit in memory writes its work files there, and they hold the keys being sorted in plain text. So the sort area must be moved onto the volume as well:

# in /etc/sd.conf
SORTWORK=/home/sd/sortwork

Create the directory on the volume first, with the sticky bit set:

sudo install -d -m 1777 /home/sd/sortwork

If that directory does not exist when SD starts, SD falls back silently to TEMPDIR, which is /tmp. The ordering drop-in below stops SD starting before the volume is open, and so keeps this from happening on a normal boot. TEMPDIR itself, for SD's other temporary files, is not covered by this page.

Backups. BACKUPDIR is not set by default. BACKUP.ACCOUNT writes plain zip files wherever BACKUPDIR points. A copy taken off the machine is not encrypted by this recipe unless the backup directory is on the encrypted volume, and even then only until the file leaves it.

Method one: LUKS2 on a volume, the four trees bound over it#

This is the method for the whole data set, end to end.

Install the tools#

sudo apt-get install -y cryptsetup systemd-cryptsetup

cryptsetup is in /usr/sbin, which is not on an ordinary user's path, so run it as sudo /usr/sbin/cryptsetup. On Debian 13 the boot-time generator systemd-cryptsetup@.service comes from the separate systemd-cryptsetup package. With only cryptsetup installed, the boot unit does not exist and the start fails.

Create the container#

sudo modprobe loop dm_crypt
truncate -s 300M /var/tmp/sd-luks.img
printf '%s' "PASSPHRASE" | sudo /usr/sbin/cryptsetup luksFormat --type luks2 -q --key-file=- /var/tmp/sd-luks.img
printf '%s' "PASSPHRASE" | sudo /usr/sbin/cryptsetup open --key-file=- /var/tmp/sd-luks.img sdcrypt
sudo mkfs.ext4 -q -L sdluks /dev/mapper/sdcrypt
sudo mkdir -p /mnt/sd-luks
sudo mount /dev/mapper/sdcrypt /mnt/sd-luks

The container uses aes-xts-plain64 with a 512-bit key, an argon2id keyslot and 4096-byte sectors. In a real installation size the image to the data, and put it on a disk you choose, not in /var/tmp; the 300 MB size here is only an example.

Replace PASSPHRASE with the passphrase. Do not write it into a script or a history file. Unlocking a passphrase keyslot needs about 1 GiB of free memory, which is the cryptsetup default.

Move the data, in order#

Stop SD first. Do not copy the trees while SD is running.

sudo systemctl stop sd.service
for d in usr/local/sdsys home/sd var/lib/sd-net etc/sd-tls; do
  sudo mkdir -p /mnt/sd-luks/$d
  sudo rsync -aHAX --numeric-ids /$d/ /mnt/sd-luks/$d/
done
for d in usr/local/sdsys home/sd var/lib/sd-net etc/sd-tls; do
  sudo mount --bind /mnt/sd-luks/$d /$d
done
sudo systemctl start sd.service

rsync -aHAX --numeric-ids keeps ownership, permissions, hard links, ACLs and extended attributes, so sdsys:sdusers and the root-only /etc/sd-tls come across unchanged.

The old plaintext is still there. The bind mounts hide the original directories but do not remove them. They remain on the root disk under the mount points. Deleting files does not guarantee that their blocks are erased on ext4 or on an SSD. The cleanest order is to encrypt before any data enters the system, which means a fresh install. Otherwise treat the old blocks of the disk as exposed.

Tear down, in order#

Teardown is the reverse of the start, and stopping the service releases the shared memory.

sudo systemctl stop sd.service
for d in etc/sd-tls var/lib/sd-net home/sd usr/local/sdsys; do
  sudo umount /$d
done
sudo umount /mnt/sd-luks
sudo /usr/sbin/cryptsetup close sdcrypt
sync

Checking a closed volume#

With the volume closed, the image should show no text of yours. Search it for a word you know is in a record, and for the record id, the account path and the SDSYS path; each count should be 0:

sudo grep -a -c "a text you wrote into a record" /var/tmp/sd-luks.img
sudo strings -a /var/tmp/sd-luks.img | grep -c "a text you wrote into a record"

file describes the image as "LUKS encrypted file, ver 2". A search is evidence about the bytes that were searched, not a proof about every byte.

Unlock at boot#

The volume must be opened and mounted before sd.service starts, and SD must not start if it cannot be. Two parts do this, and neither is shipped by the installer.

The ordering drop-in#

# /etc/systemd/system/sd.service.d/encrypted-data.conf
[Unit]
RequiresMountsFor=/usr/local/sdsys /home/sd /var/lib/sd-net /etc/sd-tls

With the four bind mounts in /etc/fstab, each marked to wait for the volume:

/dev/mapper/sdcrypt /mnt/sd-luks ext4 noauto,nofail,x-systemd.requires=systemd-cryptsetup@sdcrypt.service 0 0
/mnt/sd-luks/usr/local/sdsys /usr/local/sdsys none bind,noauto,nofail,x-systemd.requires-mounts-for=/mnt/sd-luks 0 0
/mnt/sd-luks/home/sd /home/sd none bind,noauto,nofail,x-systemd.requires-mounts-for=/mnt/sd-luks 0 0
/mnt/sd-luks/var/lib/sd-net /var/lib/sd-net none bind,noauto,nofail,x-systemd.requires-mounts-for=/mnt/sd-luks 0 0
/mnt/sd-luks/etc/sd-tls /etc/sd-tls none bind,noauto,nofail,x-systemd.requires-mounts-for=/mnt/sd-luks 0 0

Then:

sudo systemctl daemon-reload
systemctl list-dependencies sd.service
systemctl show -p RequiresMountsFor sd.service

The first command lists the four .mount units. The second echoes the four paths.

Without the drop-in, SD starts on the wrong disk. With the volume not mounted and no ordering, systemctl start sd.service reports active, and SD runs on the unencrypted directories underneath the mount points: the data written earlier is not found, and anything written then lands in plaintext. With the drop-in and the fstab entries the same start fails with "A dependency job for sd.service failed", and SD stays down.

Unlock by keyfile#

# /etc/crypttab
sdcrypt /var/tmp/sd-luks.img /var/tmp/sd-keyfile luks,nofail

With a keyfile, systemctl start sd.service from a fully locked state opens the container, mounts it, binds the four paths and starts SD.

The trade-off, stated plainly. A keyfile on the same machine as the container gives no protection against anyone who can read that machine's root disk. It protects only a container that is taken away on its own, without the machine. For the keyfile to mean anything it must live on a second device, or be released by a TPM, and SD provides neither. The keyfile in the example is in /var/tmp only to show the commands; do not keep a real one there.

Unlock by passphrase#

# /etc/crypttab
sdcrypt /var/tmp/sd-luks.img none luks,nofail,timeout=90s,tries=1

With the key field none, the boot waits for a passphrase. Answer the question through systemd's password-agent protocol:

sudo systemctl start sd.service --no-block
# the question is in /run/systemd/ask-password/ask.*; its Socket= line gives the reply path
printf '%s' "PASSPHRASE" | sudo /usr/lib/systemd/systemd-reply-password 1 SOCKET

Once the passphrase is accepted SD starts on the volume. With nobody answering, the start fails closed, as above (timeout= in crypttab sets how long the boot waits).

The trade-off. A passphrase is typed at every boot, so a machine that restarts unattended stays down until someone unlocks it, and a WantedBy=default.target service with a failed dependency leaves SD down after boot until someone runs systemctl start sd.service. That is the intended behavior for stolen-disk protection. It is not a convenience.

Method two: fscrypt on a dedicated ext4 volume#

Use fscrypt on a separate ext4 volume, not on the root filesystem. A filesystem needs the encrypt feature. It can be added with tune2fs -O encrypt, but that cannot be undone with tune2fs, so do not change the root filesystem. To use fscrypt for the data, make the volume with mkfs.ext4 -O encrypt from the start.

truncate -s 300M /var/tmp/sd-fsc.img
sudo losetup -f --show /var/tmp/sd-fsc.img
sudo mkfs.ext4 -q -O encrypt -L sdfsc /dev/loopN
sudo mount /dev/loopN /mnt/sd-fsc
sudo fscrypt setup --quiet --force
sudo fscrypt setup /mnt/sd-fsc --quiet
sudo mkdir /mnt/sd-fsc/sdenc
printf '%s\n' "PASSPHRASE" | sudo fscrypt encrypt /mnt/sd-fsc/sdenc --source=custom_passphrase --name=sdenc-prot --quiet

/dev/loopN is the loop device losetup reported. Replace PASSPHRASE as before.

Do not install libpam-fscrypt. It enables itself in /etc/pam.d/common-auth, common-password and common-session by default. If it is installed, remove it and check that no fscrypt line remains in PAM. Install only the fscrypt package.

fscrypt setup writes /etc/fscrypt.conf and a /.fscrypt directory on the root filesystem. Those are part of the set-up and are not on the volume.

fscrypt status of the directory shows policy version 2, contents AES_256_XTS, filenames AES_256_CTS, and Unlocked: Yes.

Copy the four trees into sdenc/ under the same relative paths, bind them over their usual places, and start SD as in method one. To lock the directory, stop SD and remove the binds first, then:

sudo fscrypt lock /mnt/sd-fsc/sdenc

While it is locked, ls shows encrypted names and the contents cannot be read. Unlock with:

printf '%s\n' "PASSPHRASE" | sudo fscrypt unlock /mnt/sd-fsc/sdenc --quiet

fscrypt hides contents and file names only. File sizes, owners, modes, the directory structure, timestamps and the top-level unencrypted name (here sdenc) remain visible in the raw image. The boot ordering and unlock arrangements in the section above are written for method one; for fscrypt, work out the unlock at boot for your own machine.

What this does not cover#

What to do, in order#

  1. Decide where the volume lives, and keep its passphrase or keyfile where a stolen machine does not give it away.
  2. Set SORTWORK in /etc/sd.conf to a directory on the volume, created with mode 1777.
  3. With SD stopped, create the container and move the four trees as above.
  4. Write the ordering drop-in and the fstab and crypttab entries. Check them with systemctl list-dependencies sd.service and systemctl show -p RequiresMountsFor sd.service.
  5. Encrypt or switch off swap, and decide the backup directory's location.
  6. Test a start from a locked state before trusting it. The start must fail.