| 1 |
# fscrypt [](https://github.com/google/fscrypt/releases) |
| 2 |
|
| 3 |
[](https://github.com/google/fscrypt/actions?query=workflow%3ACI+branch%3Amaster) |
| 4 |
[](https://godoc.org/github.com/google/fscrypt) |
| 5 |
[](https://goreportcard.com/report/github.com/google/fscrypt) |
| 6 |
[](http://www.apache.org/licenses/LICENSE-2.0.html) |
| 7 |
|
| 8 |
`fscrypt` is a high-level tool for the management of [Linux native filesystem |
| 9 |
encryption](https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html). |
| 10 |
`fscrypt` manages metadata, key generation, key wrapping, PAM integration, and |
| 11 |
provides a uniform interface for creating and modifying encrypted directories. |
| 12 |
For a small low-level tool that directly sets policies, see |
| 13 |
[`fscryptctl`](https://github.com/google/fscryptctl). |
| 14 |
|
| 15 |
To use `fscrypt`, you must have a filesystem that supports the Linux native |
| 16 |
filesystem encryption API (which is also sometimes called "fscrypt"; this |
| 17 |
documentation calls it "Linux native filesystem encryption" to avoid confusion). |
| 18 |
Only certain filesystems, such as [ext4](https://en.wikipedia.org/wiki/Ext4) and |
| 19 |
[f2fs](https://en.wikipedia.org/wiki/F2FS), support this API. For a full list |
| 20 |
of supported filesystems and how to enable encryption support on each one, see |
| 21 |
[Runtime dependencies](#runtime-dependencies). |
| 22 |
|
| 23 |
For the release notes, see the [NEWS file](NEWS.md). |
| 24 |
|
| 25 |
## Table of contents |
| 26 |
|
| 27 |
- [Alternatives to consider](#alternatives-to-consider) |
| 28 |
- [Threat model](#threat-model) |
| 29 |
- [Features](#features) |
| 30 |
- [Building and installing](#building-and-installing) |
| 31 |
- [Runtime dependencies](#runtime-dependencies) |
| 32 |
- [udev dependency for cross-filesystem links](#udev-dependency-for-cross-filesystem-links) |
| 33 |
- [Configuration file](#configuration-file) |
| 34 |
- [Setting up `fscrypt` on a filesystem](#setting-up-fscrypt-on-a-filesystem) |
| 35 |
- [Setting up for login protectors](#setting-up-for-login-protectors) |
| 36 |
- [Securing your login passphrase](#securing-your-login-passphrase) |
| 37 |
- [Enabling the PAM module](#enabling-the-pam-module) |
| 38 |
- [Enabling the PAM module on Debian or Ubuntu](#enabling-the-pam-module-on-debian-or-ubuntu) |
| 39 |
- [Enabling the PAM module on Arch Linux](#enabling-the-pam-module-on-arch-linux) |
| 40 |
- [Enabling the PAM module on other Linux distros](#enabling-the-pam-module-on-other-linux-distros) |
| 41 |
- [Allowing `fscrypt` to check your login passphrase](#allowing-fscrypt-to-check-your-login-passphrase) |
| 42 |
- [Backup, restore, and recovery](#backup-restore-and-recovery) |
| 43 |
- [Encrypting existing files](#encrypting-existing-files) |
| 44 |
- [Example usage](#example-usage) |
| 45 |
- [Setting up fscrypt on a directory](#setting-up-fscrypt-on-a-directory) |
| 46 |
- [Locking and unlocking a directory](#locking-and-unlocking-a-directory) |
| 47 |
- [Protecting a directory with your login passphrase](#protecting-a-directory-with-your-login-passphrase) |
| 48 |
- [Changing a custom passphrase](#changing-a-custom-passphrase) |
| 49 |
- [Using a raw key protector](#using-a-raw-key-protector) |
| 50 |
- [Using multiple protectors for a policy](#using-multiple-protectors-for-a-policy) |
| 51 |
- [Contributing](#contributing) |
| 52 |
- [Troubleshooting](#troubleshooting) |
| 53 |
- [I changed my login passphrase, now all my directories are inaccessible](#i-changed-my-login-passphrase-now-all-my-directories-are-inaccessible) |
| 54 |
- [Directories using my login passphrase are not automatically unlocking](#directories-using-my-login-passphrase-are-not-automatically-unlocking) |
| 55 |
- [Getting "encryption not enabled" on an ext4 filesystem](#getting-encryption-not-enabled-on-an-ext4-filesystem) |
| 56 |
- [Getting "user keyring not linked into session keyring"](#getting-user-keyring-not-linked-into-session-keyring) |
| 57 |
- [Getting "Operation not permitted" when moving files into an encrypted directory](#getting-operation-not-permitted-when-moving-files-into-an-encrypted-directory) |
| 58 |
- [Getting "Package not installed" when trying to use an encrypted directory](#getting-package-not-installed-when-trying-to-use-an-encrypted-directory) |
| 59 |
- [Some processes can't access unlocked encrypted files](#some-processes-cant-access-unlocked-encrypted-files) |
| 60 |
- [Users can access other users' unlocked encrypted files](#users-can-access-other-users-unlocked-encrypted-files) |
| 61 |
- [Getting "Required key not available" when backing up locked encrypted files](#getting-required-key-not-available-when-backing-up-locked-encrypted-files) |
| 62 |
- [The reported size of encrypted symlinks is wrong](#the-reported-size-of-encrypted-symlinks-is-wrong) |
| 63 |
- [Legal](#legal) |
| 64 |
|
| 65 |
## Alternatives to consider |
| 66 |
|
| 67 |
Operating-system level storage encryption solutions work at either the |
| 68 |
filesystem or block device level. [Linux native filesystem |
| 69 |
encryption](https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html) |
| 70 |
(the solution configured by `fscrypt`) is filesystem-level; it encrypts |
| 71 |
individual directories. Only file contents and filenames are encrypted; |
| 72 |
non-filename metadata, such as timestamps, the sizes and number of files, and |
| 73 |
extended attributes, is **not** encrypted. Users choose which directories will |
| 74 |
be encrypted, and with what keys. |
| 75 |
|
| 76 |
Before using `fscrypt`, you should consider other solutions: |
| 77 |
|
| 78 |
* [**dm-crypt/LUKS**](https://en.wikipedia.org/wiki/Dm-crypt) is block device |
| 79 |
level encryption: it encrypts an entire block device (and hence an entire |
| 80 |
filesystem) with one key. Unlocking this key will unlock the entire block |
| 81 |
device. dm-crypt/LUKS is usually configured using |
| 82 |
[cryptsetup](https://gitlab.com/cryptsetup/cryptsetup/-/wikis/home). |
| 83 |
|
| 84 |
* [`systemd-homed`](https://systemd.io/HOME_DIRECTORY/) supports encrypting home |
| 85 |
directories using the same Linux native filesystem encryption API that |
| 86 |
`fscrypt` uses. Note that while the `systemd-homed` documentation refers to |
| 87 |
this as fscrypt support, it does not use the `fscrypt` tool; directories set |
| 88 |
up using `systemd-homed` cannot be managed by `fscrypt` and vice versa. |
| 89 |
`systemd-homed` has better integration with systemd than `fscrypt` does. |
| 90 |
However, `systemd-homed` (as of systemd v255) uses the |
| 91 |
["V1" Linux kernel encryption API](https://www.kernel.org/doc/html/v6.8/filesystems/fscrypt.html#limitations-of-v1-policies), |
| 92 |
while `fscrypt` prefers the "V2" API. The older API causes |
| 93 |
[known issues](#some-processes-cant-access-unlocked-encrypted-files), and |
| 94 |
migrating `systemd-home` to the "V2" API is tracked |
| 95 |
[in this `systemd` issue](https://github.com/systemd/systemd/issues/18280). |
| 96 |
Issues with `systemd-homed` should be reported to the systemd developers. |
| 97 |
|
| 98 |
* [**eCryptfs**](https://en.wikipedia.org/wiki/ECryptfs) is an alternative |
| 99 |
filesystem-level encryption solution. It is a stacked filesystem, which means |
| 100 |
it sits on top of a real filesystem, rather than being directly integrated |
| 101 |
into the real filesystem. Stacked filesystems have a couple advantages (such |
| 102 |
as working on almost any real filesystem), but also some significant |
| 103 |
disadvantages. eCryptfs is usually configured using |
| 104 |
[ecryptfs-utils](https://packages.debian.org/stretch/ecryptfs-utils). |
| 105 |
|
| 106 |
* Some Linux filesystems support encryption natively, but not in a way that is |
| 107 |
compatible with the common API that `fscrypt` uses. Examples of this are |
| 108 |
Bcachefs and ZFS. (Note: ZFS is not part of the upstream kernel.) Bcachefs |
| 109 |
encryption is similar to dm-crypt in that it encrypts the full filesystem with |
| 110 |
one key. ZFS encryption operates on a per-dataset basis. If you are using |
| 111 |
one of these filesystems, refer to the documentation for that filesystem. |
| 112 |
|
| 113 |
Which solution to use? Here are our recommendations: |
| 114 |
|
| 115 |
* eCryptfs shouldn't be used, if at all possible. eCryptfs's use of filesystem |
| 116 |
stacking causes a number of issues, and eCryptfs is no longer actively |
| 117 |
maintained. The original author of eCryptfs recommends using Linux native |
| 118 |
filesystem encryption instead. The largest users of eCryptfs (Ubuntu and |
| 119 |
Chrome OS) have switched to dm-crypt or Linux native filesystem encryption. |
| 120 |
|
| 121 |
* If you need fine-grained control of encryption within a filesystem and you are |
| 122 |
using a filesystem that supports `fscrypt`, then use `fscrypt`, or `fscrypt` |
| 123 |
together with dm-crypt/LUKS. If you don't need this, then use dm-crypt/LUKS. |
| 124 |
|
| 125 |
To understand this recommendation: consider that the main advantage of |
| 126 |
`fscrypt` is to allow different files on the same filesystem to be encrypted |
| 127 |
by different keys, and thus be unlockable, lockable, and securely deletable |
| 128 |
independently from each other. Therefore, `fscrypt` is useful in cases such |
| 129 |
as: |
| 130 |
|
| 131 |
* Multi-user systems, since each user's files can be encrypted with their |
| 132 |
own key that is unlocked by their own passphrase. |
| 133 |
|
| 134 |
* Single-user systems where it's not possible for all files to have the |
| 135 |
strongest level of protection. For example, it might be necessary for the |
| 136 |
system to boot up without user interaction. Any files that are needed to |
| 137 |
do so can only be encrypted by a hardware-protected (e.g. TPM-bound) key |
| 138 |
at best. If the user's personal files are located on the same filesystem, |
| 139 |
then with dm-crypt/LUKS the user's personal files would be limited to this |
| 140 |
weak level of protection. With `fscrypt`, the user's personal files could |
| 141 |
be fully protected using the user's passphrase. |
| 142 |
|
| 143 |
`fscrypt` isn't very useful in the following cases: |
| 144 |
|
| 145 |
* Single-user systems where the user is willing to enter a strong passphrase |
| 146 |
at boot time to unlock the entire filesystem. In this case, the main |
| 147 |
advantage of `fscrypt` would go unused, so dm-crypt/LUKS would be better |
| 148 |
as it would provide better security (due to ensuring that all files and |
| 149 |
all filesystem metadata are encrypted). |
| 150 |
|
| 151 |
* Any case where it is feasible to create a separate filesystem for every |
| 152 |
encryption key you want to use. |
| 153 |
|
| 154 |
Note: dm-crypt/LUKS and `fscrypt` aren't mutually exclusive; they can be used |
| 155 |
together when the performance hit of double encryption is tolerable. It only |
| 156 |
makes sense to do this when the keys for each encryption layer are protected |
| 157 |
in different ways, such that each layer serves a different purpose. A |
| 158 |
reasonable set-up would be to encrypt the whole filesystem with dm-crypt/LUKS |
| 159 |
using a TPM-bound key that is automatically unlocked at boot time, and also |
| 160 |
encrypt users' home directories with `fscrypt` using their login passphrases. |
| 161 |
|
| 162 |
## Threat model |
| 163 |
|
| 164 |
Like other storage encryption solutions (including dm-crypt/LUKS and eCryptfs), |
| 165 |
Linux native filesystem encryption is primarily intended to protect the |
| 166 |
confidentiality of data from a single point-in-time permanent offline compromise |
| 167 |
of the disk. For a detailed description of the threat model, see the [kernel |
| 168 |
documentation](https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#threat-model). |
| 169 |
|
| 170 |
It's worth emphasizing that none of these encryption solutions protect unlocked |
| 171 |
encrypted files from other users on the same system (that's the job of OS-level |
| 172 |
access control, such as UNIX file permissions), or from the cloud provider you |
| 173 |
may be running a virtual machine on. By themselves, they also do not protect |
| 174 |
from "evil maid" attacks, i.e. non-permanent offline compromises of the disk. |
| 175 |
|
| 176 |
## Features |
| 177 |
|
| 178 |
`fscrypt` is intended to improve upon the work in |
| 179 |
[e4crypt](http://man7.org/linux/man-pages/man8/e4crypt.8.html) by providing a |
| 180 |
more managed environment and handling more functionality in the background. |
| 181 |
`fscrypt` has a [design document](https://goo.gl/55cCrI) specifying its full |
| 182 |
architecture. See also the [kernel documentation for Linux native filesystem |
| 183 |
encryption](https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html). |
| 184 |
|
| 185 |
Briefly, `fscrypt` deals with protectors and policies. Protectors represent some |
| 186 |
secret or information used to protect the confidentiality of your data. The |
| 187 |
three currently supported protector types are: |
| 188 |
|
| 189 |
1. Your login passphrase, through [PAM](http://www.linux-pam.org/Linux-PAM-html). |
| 190 |
The included PAM module (`pam_fscrypt.so`) can automatically unlock |
| 191 |
directories protected by your login passphrase when you log in, and lock them |
| 192 |
when you log out. __IMPORTANT:__ before using a login protector, follow |
| 193 |
[Setting up for login protectors](#setting-up-for-login-protectors). |
| 194 |
|
| 195 |
2. A custom passphrase. This passphrase is hashed with |
| 196 |
[Argon2id](https://en.wikipedia.org/wiki/Argon2), by default calibrated to |
| 197 |
use all CPUs and take about 1 second. |
| 198 |
|
| 199 |
3. A raw key file. See [Using a raw key protector](#using-a-raw-key-protector). |
| 200 |
|
| 201 |
These protectors are mutable, so the information can change without needing to |
| 202 |
update any of your encrypted directories. |
| 203 |
|
| 204 |
Policies represent the actual key passed to the kernel. This "policy key" is |
| 205 |
immutable and policies are (usually) applied to a single directory. Protectors |
| 206 |
then protect policies, so that having one of the protectors for a policy is |
| 207 |
enough to get the policy key and access the data. Which protectors protect a |
| 208 |
policy can also be changed. This allows a user to change how a directory is |
| 209 |
protected without needing to reencrypt the directory's contents. |
| 210 |
|
| 211 |
Concretely, `fscrypt` contains the following functionality: |
| 212 |
* `fscrypt setup` - Creates `/etc/fscrypt.conf` and the `/.fscrypt` directory |
| 213 |
* This is the only functionality which always requires root privileges |
| 214 |
* `fscrypt setup MOUNTPOINT` - Gets a filesystem ready for use with fscrypt |
| 215 |
* `fscrypt encrypt DIRECTORY` - Encrypts an empty directory |
| 216 |
* `fscrypt unlock DIRECTORY` - Unlocks an encrypted directory |
| 217 |
* `fscrypt lock DIRECTORY` - Locks an encrypted directory |
| 218 |
* `fscrypt purge MOUNTPOINT` - Locks all encrypted directories on a filesystem |
| 219 |
* `fscrypt status [PATH]` - Gets detailed info about filesystems or paths |
| 220 |
* `fscrypt metadata` - Manages policies or protectors directly |
| 221 |
|
| 222 |
See the example usage section below or run `fscrypt COMMAND --help` for more |
| 223 |
information about each of the commands. |
| 224 |
|
| 225 |
## Building and installing |
| 226 |
|
| 227 |
`fscrypt` has a minimal set of build dependencies: |
| 228 |
* [Go](https://golang.org/doc/install) 1.23 or higher. Older versions may work |
| 229 |
but they are not tested or supported. |
| 230 |
* A C compiler (`gcc` or `clang`) |
| 231 |
* `make` |
| 232 |
* Headers for [`libpam`](http://www.linux-pam.org/). |
| 233 |
Install them with the appropriate package manager: |
| 234 |
- Debian/Ubuntu: `sudo apt install libpam0g-dev` |
| 235 |
- Red Hat: `sudo yum install pam-devel` |
| 236 |
- Arch: [`pam`](https://www.archlinux.org/packages/core/x86_64/pam/) |
| 237 |
package (usually installed by default) |
| 238 |
|
| 239 |
Once all the dependencies are installed, clone the repository by running: |
| 240 |
```shell |
| 241 |
git clone https://github.com/google/fscrypt |
| 242 |
``` |
| 243 |
Running `make` builds the binary (`fscrypt`) and PAM module (`pam_fscrypt.so`) |
| 244 |
in the `bin/` directory. |
| 245 |
|
| 246 |
Running `sudo make install` installs `fscrypt` into `/usr/local/bin`, |
| 247 |
`pam_fscrypt.so` into `/usr/local/lib/security`, and `pam_fscrypt/config` into |
| 248 |
`/usr/local/share/pam-configs`. |
| 249 |
|
| 250 |
On Debian (and Debian derivatives such as Ubuntu), use `sudo make install |
| 251 |
PREFIX=/usr` to install into `/usr` instead of the default of `/usr/local`. |
| 252 |
Ordinarily you shouldn't manually install software into `/usr`, since `/usr` is |
| 253 |
reserved for Debian's own packages. However, Debian's PAM configuration |
| 254 |
framework only recognizes configuration files in `/usr`, not in `/usr/local`. |
| 255 |
Therefore, the PAM module will only work if you install into `/usr`. Note: if |
| 256 |
you later decide to switch to using the Debian package `libpam-fscrypt`, you'll |
| 257 |
have to first manually run `sudo make uninstall PREFIX=/usr`. |
| 258 |
|
| 259 |
It is also possible to use `make install-bin` to only install the `fscrypt` |
| 260 |
binary, or `make install-pam` to only install the PAM files. |
| 261 |
|
| 262 |
Alternatively, if you only want to install the `fscrypt` binary to |
| 263 |
`$GOPATH/bin`, simply run: |
| 264 |
```shell |
| 265 |
go install github.com/google/fscrypt/cmd/fscrypt@latest |
| 266 |
``` |
| 267 |
|
| 268 |
See the `Makefile` for instructions on how to further customize the build. |
| 269 |
|
| 270 |
## Runtime dependencies |
| 271 |
|
| 272 |
To run, `fscrypt` needs the following libraries: |
| 273 |
* `libpam.so` (almost certainly already on your system) |
| 274 |
|
| 275 |
In addition, `fscrypt` requires a filesystem that supports the Linux native |
| 276 |
filesystem encryption API. Currently, the filesystems that support this are: |
| 277 |
|
| 278 |
* ext4, with upstream kernel v4.1 or later. The kernel configuration must |
| 279 |
contain `CONFIG_FS_ENCRYPTION=y` (for kernels v5.1+) or |
| 280 |
`CONFIG_EXT4_ENCRYPTION=y` or `=m` (for older kernels). The filesystem must |
| 281 |
also have the `encrypt` feature flag enabled; to enable this flag, see |
| 282 |
[here](#getting-encryption-not-enabled-on-an-ext4-filesystem). |
| 283 |
|
| 284 |
* f2fs, with upstream kernel v4.2 or later. The kernel configuration must |
| 285 |
contain `CONFIG_FS_ENCRYPTION=y` (for kernels v5.1+) or |
| 286 |
`CONFIG_F2FS_FS_ENCRYPTION=y` (for older kernels). The filesystem must also |
| 287 |
have the `encrypt` feature flag enabled; this flag can be enabled at format |
| 288 |
time by `mkfs.f2fs -O encrypt` or later by `fsck.f2fs -O encrypt`. |
| 289 |
|
| 290 |
* UBIFS, with upstream kernel v4.10 or later. The kernel configuration must |
| 291 |
contain `CONFIG_FS_ENCRYPTION=y` (for kernels v5.1+) or |
| 292 |
`CONFIG_UBIFS_FS_ENCRYPTION=y` (for older kernels). |
| 293 |
|
| 294 |
* CephFS, with upstream kernel v6.6 or later. The kernel configuration must |
| 295 |
contain `CONFIG_FS_ENCRYPTION=y`. |
| 296 |
|
| 297 |
* [Lustre](https://www.lustre.org/), with Lustre v2.14.0 or later. For details, |
| 298 |
see the Lustre documentation. Please note that Lustre is not part of the |
| 299 |
upstream Linux kernel, and its encryption implementation has not been reviewed |
| 300 |
by the authors of `fscrypt`. Questions/issues about Lustre encryption should |
| 301 |
be directed to the Lustre developers. Lustre version 2.14 does not encrypt |
| 302 |
filenames, even though it claims to, so v2.15.0 or later should be used. |
| 303 |
|
| 304 |
To check whether the needed option is enabled in your kernel, run: |
| 305 |
```shell |
| 306 |
zgrep -h ENCRYPTION /proc/config.gz /boot/config-$(uname -r) | sort | uniq |
| 307 |
``` |
| 308 |
|
| 309 |
It is also recommended to use Linux kernel v5.4 or later, since this |
| 310 |
allows the use of v2 encryption policies. v2 policies have several |
| 311 |
security and usability improvements over v1 policies. |
| 312 |
|
| 313 |
If you configure `fscrypt` to use non-default features, other kernel |
| 314 |
prerequisites may be needed too. See [Configuration |
| 315 |
file](#configuration-file). |
| 316 |
|
| 317 |
### udev dependency for cross-filesystem links |
| 318 |
|
| 319 |
Cross-filesystem metadata links (for example `.fscrypt/protectors/*.link` files |
| 320 |
that point at a protector stored on another filesystem) prefer to identify the |
| 321 |
target filesystem by UUID via `/dev/disk/by-uuid`. Those symlinks are created by |
| 322 |
the standard udev rule `60-persistent-storage.rules` (from systemd/udev, eudev, |
| 323 |
or classic udev). |
| 324 |
|
| 325 |
This is a **runtime / link-creation** dependency, not a build-time dependency: |
| 326 |
|
| 327 |
* **Link creation time:** when `fscrypt` writes a cross-filesystem link, it looks |
| 328 |
up the target filesystem UUID under `/dev/disk/by-uuid`. If that directory is |
| 329 |
missing or does not contain a usable UUID symlink for the device, `fscrypt` |
| 330 |
falls back to storing the mountpoint path only. |
| 331 |
* **Runtime (following a link):** when resolving an existing link that contains a |
| 332 |
`UUID=` entry, `fscrypt` again uses `/dev/disk/by-uuid` (with a path fallback if |
| 333 |
the UUID cannot be resolved). |
| 334 |
|
| 335 |
Most Linux distributions ship `60-persistent-storage.rules` by default. If you |
| 336 |
run a minimal or custom udev setup without that rule, ensure equivalent rules |
| 337 |
still create `/dev/disk/by-uuid/*` symlinks before relying on UUID-based |
| 338 |
cross-filesystem links. Example upstream sources: |
| 339 |
|
| 340 |
* [systemd `60-persistent-storage.rules`](https://github.com/systemd/systemd/blob/main/rules.d/60-persistent-storage.rules.in) |
| 341 |
* [eudev `60-persistent-storage.rules`](https://github.com/eudev-project/eudev/blob/master/rules/60-persistent-storage.rules) |
| 342 |
|
| 343 |
## Configuration file |
| 344 |
|
| 345 |
Running `sudo fscrypt setup` will create the configuration file |
| 346 |
`/etc/fscrypt.conf` if it doesn't already exist. It's a JSON file |
| 347 |
that looks like the following: |
| 348 |
|
| 349 |
``` |
| 350 |
{ |
| 351 |
"source": "custom_passphrase", |
| 352 |
"hash_costs": { |
| 353 |
"time": "52", |
| 354 |
"memory": "131072", |
| 355 |
"parallelism": "32" |
| 356 |
}, |
| 357 |
"options": { |
| 358 |
"padding": "32", |
| 359 |
"contents": "AES_256_XTS", |
| 360 |
"filenames": "AES_256_CTS", |
| 361 |
"policy_version": "2" |
| 362 |
}, |
| 363 |
"use_fs_keyring_for_v1_policies": false, |
| 364 |
"allow_cross_user_metadata": false |
| 365 |
} |
| 366 |
``` |
| 367 |
|
| 368 |
The fields are: |
| 369 |
|
| 370 |
* "source" is the default source for new protectors. The choices are |
| 371 |
"pam\_passphrase", "custom\_passphrase", and "raw\_key". |
| 372 |
|
| 373 |
* "hash\_costs" describes how difficult the passphrase hashing is. |
| 374 |
By default, `fscrypt setup` calibrates the hashing to use all CPUs |
| 375 |
and take about 1 second. The `--time` option to `fscrypt setup` can |
| 376 |
be used to customize this time when creating the configuration file. |
| 377 |
|
| 378 |
* "options" are the encryption options to use for new encrypted |
| 379 |
directories: |
| 380 |
|
| 381 |
* "padding" is the number of bytes by which filenames are padded |
| 382 |
before being encrypted. The choices are "32", "16", "8", and |
| 383 |
"4". "32" is recommended. |
| 384 |
|
| 385 |
* "contents" is the algorithm used to encrypt file contents. The |
| 386 |
choices are "AES_256_XTS", "AES_128_CBC", and "Adiantum". |
| 387 |
Normally, "AES_256_XTS" is recommended. |
| 388 |
|
| 389 |
* "filenames" is the algorithm used to encrypt file names. The |
| 390 |
choices are "AES_256_CTS", "AES_128_CTS", "Adiantum", and |
| 391 |
"AES_256_HCTR2". Normally, "AES_256_CTS" is recommended. |
| 392 |
|
| 393 |
To use algorithms other than "AES_256_XTS" for contents and |
| 394 |
"AES_256_CTS" for filenames, the needed algorithm(s) may need to |
| 395 |
be enabled in the Linux kernel's cryptography API. For example, |
| 396 |
to use Adiantum, `CONFIG_CRYPTO_ADIANTUM` must be set. Also, |
| 397 |
not all combinations of algorithms are allowed; for example, |
| 398 |
"Adiantum" for contents can only be paired with "Adiantum" for |
| 399 |
filenames. See the [kernel |
| 400 |
documentation](https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#encryption-modes-and-usage) |
| 401 |
for more details about the supported algorithms. |
| 402 |
|
| 403 |
* "policy\_version" is the version of encryption policy to use. |
| 404 |
The choices are "1" and "2". If unset, "1" is assumed. |
| 405 |
Directories created with policy version "2" are only usable on |
| 406 |
kernel v5.4 or later, but are preferable to version "1" if you |
| 407 |
don't mind this restriction. |
| 408 |
|
| 409 |
* "use\_fs\_keyring\_for\_v1\_policies" specifies whether to add keys for v1 |
| 410 |
encryption policies to the filesystem keyrings, rather than to user keyrings. |
| 411 |
This can solve [issues with processes being unable to access unlocked |
| 412 |
encrypted files](#some-processes-cant-access-unlocked-encrypted-files). |
| 413 |
However, it requires kernel v5.4 or later, and it makes unlocking and locking |
| 414 |
encrypted directories require root. (The PAM module will still work.) |
| 415 |
|
| 416 |
The purpose of this setting is to allow people to take advantage of some of |
| 417 |
the improvements in Linux v5.4 on encrypted directories that are also |
| 418 |
compatible with older kernels. If you don't need compatibility with older |
| 419 |
kernels, it's better to not use this setting and instead (re-)create your |
| 420 |
encrypted directories with `"policy_version": "2"`. |
| 421 |
|
| 422 |
* "allow\_cross\_user\_metadata" specifies whether `fscrypt` will allow |
| 423 |
protectors and policies from other non-root users to be read, e.g. to be |
| 424 |
offered as options by `fscrypt encrypt`. The default value is `false`, since |
| 425 |
other users might be untrusted and could create malicious files. This can be |
| 426 |
set to `true` to restore the old behavior on systems where `fscrypt` metadata |
| 427 |
needs to be shared between multiple users. Note that this option is |
| 428 |
independent from the permissions on the metadata files themselves, which are |
| 429 |
set to 0600 by default; users who wish to share their metadata files with |
| 430 |
other users would also need to explicitly change their mode to 0644. |
| 431 |
|
| 432 |
## Setting up `fscrypt` on a filesystem |
| 433 |
|
| 434 |
`fscrypt` needs some directories to exist on the filesystem on which encryption |
| 435 |
will be used: |
| 436 |
|
| 437 |
* `MOUNTPOINT/.fscrypt/policies` |
| 438 |
* `MOUNTPOINT/.fscrypt/protectors` |
| 439 |
|
| 440 |
(If login protectors are used, these must also exist on the root filesystem.) |
| 441 |
|
| 442 |
To create these directories, run `fscrypt setup MOUNTPOINT`. If MOUNTPOINT is |
| 443 |
owned by root, as is usually the case, then this command will require root. |
| 444 |
|
| 445 |
There will be one decision you'll need to make: whether non-root users will be |
| 446 |
allowed to create `fscrypt` metadata (policies and protectors). |
| 447 |
|
| 448 |
If you say `y`, then these directories will be made world-writable, with the |
| 449 |
sticky bit set so that users can't delete each other's files -- just like |
| 450 |
`/tmp`. If you say `N`, then these directories will be writable only by root. |
| 451 |
|
| 452 |
Saying `y` maximizes the usability of `fscrypt`, and on most systems it's fine |
| 453 |
to say `y`. However, on some systems this may be inappropriate, as it will |
| 454 |
allow malicious users to fill the entire filesystem unless filesystem quotas |
| 455 |
have been configured -- similar to problems that have historically existed with |
| 456 |
other world-writable directories, e.g. `/tmp`. If you are concerned about this, |
| 457 |
say `N`. If you say `N`, then you'll only be able to run `fscrypt` as root to |
| 458 |
set up encryption on users' behalf, unless you manually set custom permissions |
| 459 |
on the metadata directories to grant write access to specific users or groups. |
| 460 |
|
| 461 |
If you chose the wrong mode at `fscrypt setup` time, you can change the |
| 462 |
directory permissions at any time. To enable single-user writable mode, run: |
| 463 |
|
| 464 |
sudo chmod 0755 MOUNTPOINT/.fscrypt/* |
| 465 |
|
| 466 |
To enable world-writable mode, run: |
| 467 |
|
| 468 |
sudo chmod 1777 MOUNTPOINT/.fscrypt/* |
| 469 |
|
| 470 |
## Setting up for login protectors |
| 471 |
|
| 472 |
If you want any encrypted directories to be protected by your login passphrase, |
| 473 |
you'll need to: |
| 474 |
|
| 475 |
1. Secure your login passphrase (optional, but strongly recommended) |
| 476 |
2. Enable the PAM module (`pam_fscrypt.so`) |
| 477 |
|
| 478 |
If you installed `fscrypt` from source rather than from your distro's package |
| 479 |
manager, you may also need to allow `fscrypt` to check your login passphrase. |
| 480 |
|
| 481 |
### Securing your login passphrase |
| 482 |
|
| 483 |
Although `fscrypt` uses a strong passphrase hash algorithm, the security of |
| 484 |
login protectors is also limited by the strength of your system's passphrase |
| 485 |
hashing in `/etc/shadow`. On most Linux distributions, `/etc/shadow` by default |
| 486 |
uses SHA-512 with 5000 rounds, which is much weaker than what `fscrypt` uses. |
| 487 |
|
| 488 |
To mitigate this, you should use a strong login passphrase. |
| 489 |
|
| 490 |
If using a strong login passphrase is annoying because it needs to be entered |
| 491 |
frequently to run `sudo`, consider increasing the `sudo` timeout. That can be |
| 492 |
done by adding the following to `/etc/sudoers`: |
| 493 |
``` |
| 494 |
Defaults timestamp_timeout=60 |
| 495 |
``` |
| 496 |
|
| 497 |
You should also increase the number of rounds that your system's passphrase |
| 498 |
hashing uses (though this doesn't increase security as much as choosing a strong |
| 499 |
passphrase). To do this, find the line in `/etc/pam.d/passwd` that looks like: |
| 500 |
``` |
| 501 |
password required pam_unix.so sha512 shadow nullok |
| 502 |
``` |
| 503 |
|
| 504 |
Append `rounds=1000000` (or another number of your choice; the goal is to make |
| 505 |
the passphrase hashing take about 1 second, similar to `fscrypt`'s default): |
| 506 |
``` |
| 507 |
password required pam_unix.so sha512 shadow nullok rounds=1000000 |
| 508 |
``` |
| 509 |
|
| 510 |
Then, change your login passphrase to a new, strong passphrase: |
| 511 |
``` |
| 512 |
passwd |
| 513 |
``` |
| 514 |
|
| 515 |
If you'd like to keep the same login passphrase (not recommended, as the old |
| 516 |
passphrase hash may still be recoverable from disk), then instead run |
| 517 |
`sudo passwd $USER` and enter your existing passphrase. This re-hashes your |
| 518 |
existing passphrase with the new `rounds`. |
| 519 |
|
| 520 |
### Enabling the PAM module |
| 521 |
|
| 522 |
To enable the PAM module `pam_fscrypt.so`, follow the directions for your Linux |
| 523 |
distro below. Enabling the PAM module is needed for login passphrase-protected |
| 524 |
directories to be automatically unlocked when you log in (and be automatically |
| 525 |
locked when you log out), and for login passphrase-protected directories to |
| 526 |
remain accessible when you change your login passphrase. |
| 527 |
|
| 528 |
#### Enabling the PAM module on Debian or Ubuntu |
| 529 |
|
| 530 |
The official `libpam-fscrypt` package for Debian (and Debian derivatives such as |
| 531 |
Ubuntu) will install a configuration file for [Debian's PAM configuration |
| 532 |
framework](https://wiki.ubuntu.com/PAMConfigFrameworkSpec) to |
| 533 |
`/usr/share/pam-configs/fscrypt`. This file contains reasonable defaults for |
| 534 |
the PAM module. To automatically apply these defaults, run |
| 535 |
`sudo pam-auth-update` and follow the on-screen instructions. |
| 536 |
|
| 537 |
This file also gets installed if you build and install `fscrypt` from source, |
| 538 |
but it is only installed to the correct location if you use `make install |
| 539 |
PREFIX=/usr` to install into `/usr` instead of the default of `/usr/local`. |
| 540 |
|
| 541 |
#### Enabling the PAM module on Arch Linux |
| 542 |
|
| 543 |
On Arch Linux, follow the recommendations at the [Arch Linux |
| 544 |
Wiki](https://wiki.archlinux.org/index.php/Fscrypt#Auto-unlocking_directories). |
| 545 |
|
| 546 |
We recommend using the Arch Linux package, either `fscrypt` (official) or |
| 547 |
`fscrypt-git` (AUR). If you instead install `fscrypt` manually using `sudo make |
| 548 |
install`, then in addition to the steps on the Wiki you'll also need to [create |
| 549 |
`/etc/pam.d/fscrypt`](#allowing-fscrypt-to-check-your-login-passphrase). |
| 550 |
|
| 551 |
#### Enabling the PAM module on other Linux distros |
| 552 |
|
| 553 |
On all other Linux distros, follow the general guidance below to edit |
| 554 |
your PAM configuration files. |
| 555 |
|
| 556 |
The `fscrypt` PAM module implements the Auth, Session, and Password |
| 557 |
[types](http://www.linux-pam.org/Linux-PAM-html/sag-configuration-file.html). |
| 558 |
|
| 559 |
The Password functionality of `pam_fscrypt.so` is used to automatically rewrap |
| 560 |
a user's login protector when their unix passphrase changes. An easy way to get |
| 561 |
the working is to add the line: |
| 562 |
``` |
| 563 |
password optional pam_fscrypt.so |
| 564 |
``` |
| 565 |
after `pam_unix.so` in `/etc/pam.d/common-password` or similar. |
| 566 |
|
| 567 |
The Auth and Session functionality of `pam_fscrypt.so` are used to automatically |
| 568 |
unlock directories when logging in as a user, and lock them when logging out. |
| 569 |
An easy way to get this working is to add the line: |
| 570 |
``` |
| 571 |
auth optional pam_fscrypt.so |
| 572 |
``` |
| 573 |
after `pam_unix.so` in `/etc/pam.d/common-auth` or similar, and to add the |
| 574 |
line: |
| 575 |
``` |
| 576 |
session optional pam_fscrypt.so |
| 577 |
``` |
| 578 |
after `pam_unix.so` in `/etc/pam.d/common-session` or similar, but before |
| 579 |
`pam_systemd.so` or any other module that accesses the user's home directory or |
| 580 |
which starts processes that access the user's home directory during their |
| 581 |
session. |
| 582 |
|
| 583 |
`pam_fscrypt.so` accepts several options: |
| 584 |
|
| 585 |
* `debug`: print additional debug messages to the syslog. All hook types accept |
| 586 |
this option. |
| 587 |
|
| 588 |
* `unlock_only`: only unlock directories (at log-in); don't also lock them (at |
| 589 |
log-out). This is only relevant for the "session" hook. Note that in |
| 590 |
`fscrypt` v0.2.9 and earlier, unlock-only was the default behavior, and |
| 591 |
`lock_policies` needed to be specified to enable locking. |
| 592 |
|
| 593 |
### Allowing `fscrypt` to check your login passphrase |
| 594 |
|
| 595 |
This step is only needed if you installed `fscrypt` from source code. |
| 596 |
|
| 597 |
Some Linux distros use restrictive settings in `/etc/pam.d/other` that prevent |
| 598 |
programs from checking your login passphrase unless a per-program PAM |
| 599 |
configuration file grants access. This prevents `fscrypt` from creating any |
| 600 |
login passphrase-protected directories, even without auto-unlocking. To ensure |
| 601 |
that `fscrypt` will work properly (if you didn't install an official `fscrypt` |
| 602 |
package from your distro, which should have already handled this), also create a |
| 603 |
file `/etc/pam.d/fscrypt` containing: |
| 604 |
``` |
| 605 |
auth required pam_unix.so |
| 606 |
``` |
| 607 |
|
| 608 |
## Backup, restore, and recovery |
| 609 |
|
| 610 |
Encrypted files and directories can't be backed up while they are "locked", i.e. |
| 611 |
while they appear in encrypted form. They can only be backed up while they are |
| 612 |
unlocked, in which case they can be backed up like any other files. Note that |
| 613 |
since the encryption is transparent, the files won't be encrypted in the backup |
| 614 |
(unless the backup applies its own encryption). |
| 615 |
|
| 616 |
For the same reason (and several others), an encrypted directory can't be |
| 617 |
directly "moved" to another filesystem. However, it is possible to create a new |
| 618 |
encrypted directory on the destination filesystem using `fscrypt encrypt`, then |
| 619 |
copy the contents of the source directory into it. |
| 620 |
|
| 621 |
For directories protected by a `custom_passphrase` or `raw_key` protector, all |
| 622 |
metadata needed to unlock the directory (excluding the actual passphrase or raw |
| 623 |
key, of course) is located in the `.fscrypt` directory at the root of the |
| 624 |
filesystem that contains the encrypted directory. For example, if you have an |
| 625 |
encrypted directory `/home/$USER/private` that is protected by a custom |
| 626 |
passphrase, all `fscrypt` metadata needed to unlock the directory with that |
| 627 |
custom passphrase will be located in `/home/.fscrypt` if you are using a |
| 628 |
dedicated `/home` filesystem or in `/.fscrypt` if you aren't. If desired, you |
| 629 |
can back up the `fscrypt` metadata by making a copy of this directory, although |
| 630 |
this isn't too important since this metadata is located on the same filesystem |
| 631 |
as the encrypted directory(s). |
| 632 |
|
| 633 |
`pam_passphrase` (login passphrase) protectors are a bit different as they are |
| 634 |
always stored on the root filesystem, in `/.fscrypt`. This ties them to the |
| 635 |
specific system and ensures that each user has only a single login protector. |
| 636 |
Therefore, encrypted directories on a non-root filesystem **can't be unlocked |
| 637 |
via a login protector if the operating system is reinstalled or if the disk is |
| 638 |
connected to another system** -- even if the new system uses the same login |
| 639 |
passphrase for the user. |
| 640 |
|
| 641 |
Because of this, `fscrypt encrypt` will automatically generate a recovery |
| 642 |
passphrase when creating a login passphrase-protected directory on a non-root |
| 643 |
filesystem. The recovery passphrase is simply a `custom_passphrase` protector |
| 644 |
with a randomly generated high-entropy passphrase. Initially, the recovery |
| 645 |
passphrase is stored in a file in the encrypted directory itself; therefore, to |
| 646 |
use it you **must** record it in another secure location. It is strongly |
| 647 |
recommended to do this. Then, if ever needed, you can use `fscrypt unlock` to |
| 648 |
unlock the directory with the recovery passphrase (by choosing the recovery |
| 649 |
protector instead of the login protector). |
| 650 |
|
| 651 |
If you really want to disable the generation of a recovery passphrase, use the |
| 652 |
`--no-recovery` option. Only do this if you really know what you are doing and |
| 653 |
are prepared for potential data loss. |
| 654 |
|
| 655 |
Alternative approaches to supporting recovery of login passphrase-protected |
| 656 |
directories include the following: |
| 657 |
|
| 658 |
* Manually adding your own recovery protector, using |
| 659 |
`fscrypt metadata add-protector-to-policy`. |
| 660 |
|
| 661 |
* Backing up and restoring the `/.fscrypt` directory on the root filesystem. |
| 662 |
Note that after restoring the `/.fscrypt` directory, unlocking the login |
| 663 |
protectors will require the passphrases they had at the time the backup was |
| 664 |
made **even if they were changed later**, so make sure to remember these |
| 665 |
passphrase(s) or record them in a secure location. Also note that if the UUID |
| 666 |
of the root filesystem changed, you will need to manually fix the UUID in any |
| 667 |
`.fscrypt/protectors/*.link` files on other filesystems. |
| 668 |
|
| 669 |
The auto-generated recovery passphrases should be enough for most users, though. |
| 670 |
|
| 671 |
## Encrypting existing files |
| 672 |
|
| 673 |
`fscrypt` isn't designed to encrypt existing files, as this presents significant |
| 674 |
technical challenges and usually is impossible to do securely. Therefore, |
| 675 |
`fscrypt encrypt` only works on empty directories. |
| 676 |
|
| 677 |
Of course, it is still possible to create an encrypted directory, copy files |
| 678 |
into it, and delete the original files. The `mv` command will even work, as it |
| 679 |
will fall back to a copy and delete ([except on older |
| 680 |
kernels](#getting-operation-not-permitted-when-moving-files-into-an-encrypted-directory)). |
| 681 |
However, beware that due to the characteristics of filesystems and storage |
| 682 |
devices, this may not properly protect the files, as their original contents may |
| 683 |
still be forensically recoverable from disk even after being deleted. It's |
| 684 |
**much** better to encrypt files from the very beginning. |
| 685 |
|
| 686 |
There are only a few cases where copying files into an encrypted directory can |
| 687 |
really make sense, such as: |
| 688 |
|
| 689 |
* The source files are located on an in-memory filesystem such as `tmpfs`. |
| 690 |
|
| 691 |
* The confidentiality of the source files isn't important, e.g. they are |
| 692 |
system default files and the user hasn't added any personal files yet. |
| 693 |
|
| 694 |
* The source files are protected by a different `fscrypt` policy, the old and |
| 695 |
new policies are protected by only the same protector(s), and the old policy |
| 696 |
uses similar strength encryption. |
| 697 |
|
| 698 |
If one of the above doesn't apply, then it's probably too late to securely |
| 699 |
encrypt your existing files. |
| 700 |
|
| 701 |
As a best-effort attempt, you can use the `shred` program to try to erase the |
| 702 |
original files. Here are the recommended commands for "best-effort" encryption |
| 703 |
of an existing directory named "dir": |
| 704 |
|
| 705 |
```bash |
| 706 |
mkdir dir.new |
| 707 |
fscrypt encrypt dir.new |
| 708 |
cp -a -T dir dir.new |
| 709 |
find dir -type f -print0 | xargs -0 shred -n1 --remove=unlink |
| 710 |
rm -rf dir |
| 711 |
mv dir.new dir |
| 712 |
``` |
| 713 |
|
| 714 |
However, beware that `shred` isn't guaranteed to be effective on all storage |
| 715 |
devices and filesystems. For example, if you're using an SSD, "overwrites" of |
| 716 |
data typically go to new flash blocks, so they aren't really overwrites. |
| 717 |
|
| 718 |
Note: for reasons similar to the above, changed or removed `fscrypt` protectors |
| 719 |
aren't guaranteed to be forensically unrecoverable from disk either. Thus, the |
| 720 |
use of weak or default passphrases should be avoided, even if changed later. |
| 721 |
|
| 722 |
## Example usage |
| 723 |
|
| 724 |
All these examples assume there is an ext4 filesystem which supports |
| 725 |
encryption mounted at `/mnt/disk`. See |
| 726 |
[here](#getting-encryption-not-enabled-on-an-ext4-filesystem) for how |
| 727 |
to enable encryption support on an ext4 filesystem. |
| 728 |
|
| 729 |
### Setting up fscrypt on a directory |
| 730 |
|
| 731 |
```bash |
| 732 |
# Check which directories on our system support encryption |
| 733 |
>>>>> fscrypt status |
| 734 |
filesystems supporting encryption: 1 |
| 735 |
filesystems with fscrypt metadata: 0 |
| 736 |
|
| 737 |
MOUNTPOINT DEVICE FILESYSTEM ENCRYPTION FSCRYPT |
| 738 |
/ /dev/sda1 ext4 not enabled No |
| 739 |
/mnt/disk /dev/sdb ext4 supported No |
| 740 |
|
| 741 |
# Create the global configuration file. Nothing else necessarily needs root. |
| 742 |
>>>>> sudo fscrypt setup |
| 743 |
Defaulting to policy_version 2 because kernel supports it. |
| 744 |
Customizing passphrase hashing difficulty for this system... |
| 745 |
Created global config file at "/etc/fscrypt.conf". |
| 746 |
Allow users other than root to create fscrypt metadata on the root filesystem? |
| 747 |
(See https://github.com/google/fscrypt#setting-up-fscrypt-on-a-filesystem) [y/N] y |
| 748 |
Metadata directories created at "/.fscrypt", writable by everyone. |
| 749 |
|
| 750 |
# Start using fscrypt with our filesystem |
| 751 |
>>>>> sudo fscrypt setup /mnt/disk |
| 752 |
Allow users other than root to create fscrypt metadata on this filesystem? (See |
| 753 |
https://github.com/google/fscrypt#setting-up-fscrypt-on-a-filesystem) [y/N] y |
| 754 |
Metadata directories created at "/mnt/disk/.fscrypt", writable by everyone. |
| 755 |
|
| 756 |
# Initialize encryption on a new empty directory |
| 757 |
>>>>> mkdir /mnt/disk/dir1 |
| 758 |
>>>>> fscrypt encrypt /mnt/disk/dir1 |
| 759 |
The following protector sources are available: |
| 760 |
1 - Your login passphrase (pam_passphrase) |
| 761 |
2 - A custom passphrase (custom_passphrase) |
| 762 |
3 - A raw 256-bit key (raw_key) |
| 763 |
Enter the source number for the new protector [2 - custom_passphrase]: 2 |
| 764 |
Enter a name for the new protector: Super Secret |
| 765 |
Enter custom passphrase for protector "Super Secret": |
| 766 |
Confirm passphrase: |
| 767 |
"/mnt/disk/dir1" is now encrypted, unlocked, and ready for use. |
| 768 |
|
| 769 |
# We can see this created one policy and one protector for this directory |
| 770 |
>>>>> fscrypt status /mnt/disk |
| 771 |
ext4 filesystem "/mnt/disk" has 1 protector and 1 policy |
| 772 |
|
| 773 |
PROTECTOR LINKED DESCRIPTION |
| 774 |
7626382168311a9d No custom protector "Super Secret" |
| 775 |
|
| 776 |
POLICY UNLOCKED PROTECTORS |
| 777 |
16382f282d7b29ee27e6460151d03382 Yes 7626382168311a9d |
| 778 |
``` |
| 779 |
|
| 780 |
#### Quiet version |
| 781 |
```bash |
| 782 |
>>>>> sudo fscrypt setup --quiet --force --all-users |
| 783 |
>>>>> sudo fscrypt setup /mnt/disk --quiet --all-users |
| 784 |
>>>>> echo "hunter2" | fscrypt encrypt /mnt/disk/dir1 --quiet --source=custom_passphrase --name="Super Secret" |
| 785 |
``` |
| 786 |
|
| 787 |
### Locking and unlocking a directory |
| 788 |
|
| 789 |
```bash |
| 790 |
# Write a file to our encrypted directory. |
| 791 |
>>>>> echo "Hello World" > /mnt/disk/dir1/secret.txt |
| 792 |
>>>>> fscrypt status /mnt/disk/dir1 |
| 793 |
"/mnt/disk/dir1" is encrypted with fscrypt. |
| 794 |
|
| 795 |
Policy: 16382f282d7b29ee27e6460151d03382 |
| 796 |
Options: padding:32 contents:AES_256_XTS filenames:AES_256_CTS policy_version:2 |
| 797 |
Unlocked: Yes |
| 798 |
|
| 799 |
Protected with 1 protector: |
| 800 |
PROTECTOR LINKED DESCRIPTION |
| 801 |
7626382168311a9d No custom protector "Super Secret" |
| 802 |
|
| 803 |
# Lock the directory. Note: if using a v1 encryption policy instead |
| 804 |
# of v2, you'll need 'sudo fscrypt lock /mnt/disk/dir1 --user=$USER'. |
| 805 |
>>>>> fscrypt lock /mnt/disk/dir1 |
| 806 |
"/mnt/disk/dir1" is now locked. |
| 807 |
>>>>> fscrypt status /mnt/disk/dir1 |
| 808 |
"/mnt/disk/dir1" is encrypted with fscrypt. |
| 809 |
|
| 810 |
Policy: 16382f282d7b29ee27e6460151d03382 |
| 811 |
Options: padding:32 contents:AES_256_XTS filenames:AES_256_CTS policy_version:2 |
| 812 |
Unlocked: No |
| 813 |
|
| 814 |
Protected with 1 protector: |
| 815 |
PROTECTOR LINKED DESCRIPTION |
| 816 |
7626382168311a9d No custom protector "Super Secret" |
| 817 |
|
| 818 |
# Now the filenames and file contents are inaccessible |
| 819 |
>>>>> ls /mnt/disk/dir1 |
| 820 |
u,k20l9HrtrizDjh0zGkw2dTfBkX4T0ZDUlsOhBLl4P |
| 821 |
>>>>> cat /mnt/disk/dir1/u,k20l9HrtrizDjh0zGkw2dTfBkX4T0ZDUlsOhBLl4P |
| 822 |
cat: /mnt/disk/dir1/u,k20l9HrtrizDjh0zGkw2dTfBkX4T0ZDUlsOhBLl4P: Required key not available |
| 823 |
|
| 824 |
# Unlocking the directory makes the contents available |
| 825 |
>>>>> fscrypt unlock /mnt/disk/dir1 |
| 826 |
Enter custom passphrase for protector "Super Secret": |
| 827 |
"/mnt/disk/dir1" is now unlocked and ready for use. |
| 828 |
>>>>> fscrypt status /mnt/disk/dir1 |
| 829 |
"/mnt/disk/dir1" is encrypted with fscrypt. |
| 830 |
|
| 831 |
Policy: 16382f282d7b29ee27e6460151d03382 |
| 832 |
Options: padding:32 contents:AES_256_XTS filenames:AES_256_CTS policy_version:2 |
| 833 |
Unlocked: Yes |
| 834 |
|
| 835 |
Protected with 1 protector: |
| 836 |
PROTECTOR LINKED DESCRIPTION |
| 837 |
7626382168311a9d No custom protector "Super Secret" |
| 838 |
>>>>> cat /mnt/disk/dir1/secret.txt |
| 839 |
Hello World |
| 840 |
``` |
| 841 |
|
| 842 |
#### Quiet version |
| 843 |
```bash |
| 844 |
>>>>> fscrypt lock /mnt/disk/dir1 --quiet |
| 845 |
>>>>> echo "hunter2" | fscrypt unlock /mnt/disk/dir1 --quiet |
| 846 |
``` |
| 847 |
|
| 848 |
### Protecting a directory with your login passphrase |
| 849 |
|
| 850 |
First, ensure that you have properly [set up your system for login |
| 851 |
protectors](#setting-up-for-login-protectors). |
| 852 |
|
| 853 |
Then, you can protect directories with your login passphrase as follows: |
| 854 |
|
| 855 |
```bash |
| 856 |
# Select your login passphrase as the desired source. |
| 857 |
>>>>> mkdir /mnt/disk/dir2 |
| 858 |
>>>>> fscrypt encrypt /mnt/disk/dir2 |
| 859 |
Should we create a new protector? [y/N] y |
| 860 |
The following protector sources are available: |
| 861 |
1 - Your login passphrase (pam_passphrase) |
| 862 |
2 - A custom passphrase (custom_passphrase) |
| 863 |
3 - A raw 256-bit key (raw_key) |
| 864 |
Enter the source number for the new protector [2 - custom_passphrase]: 1 |
| 865 |
Enter login passphrase for joerichey: |
| 866 |
"/mnt/disk/dir2" is now encrypted, unlocked, and ready for use. |
| 867 |
|
| 868 |
# Note that the login protector actually sits on the root filesystem |
| 869 |
>>>>> fscrypt status /mnt/disk/dir2 |
| 870 |
"/mnt/disk/dir2" is encrypted with fscrypt. |
| 871 |
|
| 872 |
Policy: fe1c92009abc1cff4f3257c77e8134e3 |
| 873 |
Options: padding:32 contents:AES_256_XTS filenames:AES_256_CTS policy_version:2 |
| 874 |
Unlocked: Yes |
| 875 |
|
| 876 |
Protected with 1 protector: |
| 877 |
PROTECTOR LINKED DESCRIPTION |
| 878 |
6891f0a901f0065e Yes (/) login protector for joerichey |
| 879 |
>>>>> fscrypt status /mnt/disk |
| 880 |
ext4 filesystem "/mnt/disk" has 2 protectors and 2 policies |
| 881 |
|
| 882 |
PROTECTOR LINKED DESCRIPTION |
| 883 |
7626382168311a9d No custom protector "Super Secret" |
| 884 |
6891f0a901f0065e Yes (/) login protector for joerichey |
| 885 |
|
| 886 |
POLICY UNLOCKED PROTECTORS |
| 887 |
16382f282d7b29ee27e6460151d03382 Yes 7626382168311a9d |
| 888 |
fe1c92009abc1cff4f3257c77e8134e3 Yes 6891f0a901f0065e |
| 889 |
>>>>> fscrypt status / |
| 890 |
ext4 filesystem "/" has 1 protector(s) and 0 policy(ies) |
| 891 |
|
| 892 |
PROTECTOR LINKED DESCRIPTION |
| 893 |
6891f0a901f0065e No login protector for joerichey |
| 894 |
``` |
| 895 |
|
| 896 |
#### Quiet version |
| 897 |
```bash |
| 898 |
>>>>> mkdir /mnt/disk/dir2 |
| 899 |
>>>>> echo "password" | fscrypt encrypt /mnt/disk/dir2 --source=pam_passphrase --quiet |
| 900 |
``` |
| 901 |
|
| 902 |
### Changing a custom passphrase |
| 903 |
```bash |
| 904 |
# First we have to figure out which protector we wish to change. |
| 905 |
>>>>> fscrypt status /mnt/disk/dir1 |
| 906 |
"/mnt/disk/dir1" is encrypted with fscrypt. |
| 907 |
|
| 908 |
Policy: 16382f282d7b29ee27e6460151d03382 |
| 909 |
Options: padding:32 contents:AES_256_XTS filenames:AES_256_CTS policy_version:2 |
| 910 |
Unlocked: Yes |
| 911 |
|
| 912 |
Protected with 1 protector: |
| 913 |
PROTECTOR LINKED DESCRIPTION |
| 914 |
7626382168311a9d No custom protector "Super Secret" |
| 915 |
|
| 916 |
# Now specify the protector directly to the metadata command |
| 917 |
>>>>> fscrypt metadata change-passphrase --protector=/mnt/disk:7626382168311a9d |
| 918 |
Enter old custom passphrase for protector "Super Secret": |
| 919 |
Enter new custom passphrase for protector "Super Secret": |
| 920 |
Confirm passphrase: |
| 921 |
Passphrase for protector 7626382168311a9d successfully changed. |
| 922 |
``` |
| 923 |
|
| 924 |
#### Quiet version |
| 925 |
```bash |
| 926 |
>>>>> printf "hunter2\nhunter3" | fscrypt metadata change-passphrase --protector=/mnt/disk:7626382168311a9d --quiet |
| 927 |
``` |
| 928 |
|
| 929 |
### Using a raw key protector |
| 930 |
|
| 931 |
`fscrypt` also supports protectors which use raw key files as the user-provided |
| 932 |
secret. These key files must be exactly 32 bytes long and contain the raw binary |
| 933 |
data of the key. Obviously, make sure to store the key file securely (and not in |
| 934 |
the directory you are encrypting with it). If generating the keys on Linux make |
| 935 |
sure you are aware of |
| 936 |
[how randomness works](http://man7.org/linux/man-pages/man7/random.7.html) and |
| 937 |
[some common myths](https://www.2uo.de/myths-about-urandom/). |
| 938 |
|
| 939 |
```bash |
| 940 |
# Generate a 256-bit key file |
| 941 |
>>>>> head --bytes=32 /dev/urandom > secret.key |
| 942 |
|
| 943 |
# Now create a key file protector without using it on a directory. Note that we |
| 944 |
# could also use `fscrypt encrypt --key=secret.key` to achieve the same thing. |
| 945 |
>>>>> fscrypt metadata create protector /mnt/disk |
| 946 |
Create new protector on "/mnt/disk" [Y/n] y |
| 947 |
The following protector sources are available: |
| 948 |
1 - Your login passphrase (pam_passphrase) |
| 949 |
2 - A custom passphrase (custom_passphrase) |
| 950 |
3 - A raw 256-bit key (raw_key) |
| 951 |
Enter the source number for the new protector [2 - custom_passphrase]: 3 |
| 952 |
Enter a name for the new protector: Skeleton |
| 953 |
Enter key file for protector "Skeleton": secret.key |
| 954 |
Protector 2c75f519b9c9959d created on filesystem "/mnt/disk". |
| 955 |
>>>>> fscrypt status /mnt/disk |
| 956 |
ext4 filesystem "/mnt/disk" has 3 protectors and 2 policies |
| 957 |
|
| 958 |
PROTECTOR LINKED DESCRIPTION |
| 959 |
7626382168311a9d No custom protector "Super Secret" |
| 960 |
2c75f519b9c9959d No raw key protector "Skeleton" |
| 961 |
6891f0a901f0065e Yes (/) login protector for joerichey |
| 962 |
|
| 963 |
POLICY UNLOCKED PROTECTORS |
| 964 |
16382f282d7b29ee27e6460151d03382 Yes 7626382168311a9d |
| 965 |
fe1c92009abc1cff4f3257c77e8134e3 Yes 6891f0a901f0065e |
| 966 |
|
| 967 |
# Finally, we could apply this key to a directory |
| 968 |
>>>>> mkdir /mnt/disk/dir3 |
| 969 |
>>>>> fscrypt encrypt /mnt/disk/dir3 --protector=/mnt/disk:2c75f519b9c9959d |
| 970 |
Enter key file for protector "Skeleton": secret.key |
| 971 |
"/mnt/disk/dir3" is now encrypted, unlocked, and ready for use. |
| 972 |
``` |
| 973 |
|
| 974 |
#### Quiet version |
| 975 |
```bash |
| 976 |
>>>>> head --bytes=32 /dev/urandom > secret.key |
| 977 |
>>>>> fscrypt encrypt /mnt/disk/dir3 --key=secret.key --source=raw_key --name=Skeleton |
| 978 |
``` |
| 979 |
|
| 980 |
### Using multiple protectors for a policy |
| 981 |
|
| 982 |
`fscrypt` supports the idea of protecting a single directory with multiple |
| 983 |
protectors. This means having access to any of the protectors is sufficient to |
| 984 |
decrypt the directory. This is useful for sharing data or setting up access |
| 985 |
control systems. |
| 986 |
|
| 987 |
```bash |
| 988 |
# Add an existing protector to the policy for some directory |
| 989 |
>>>>> fscrypt status /mnt/disk |
| 990 |
ext4 filesystem "/mnt/disk" has 3 protectors and 3 policies |
| 991 |
|
| 992 |
PROTECTOR LINKED DESCRIPTION |
| 993 |
7626382168311a9d No custom protector "Super Secret" |
| 994 |
2c75f519b9c9959d No raw key protector "Skeleton" |
| 995 |
6891f0a901f0065e Yes (/) login protector for joerichey |
| 996 |
|
| 997 |
POLICY UNLOCKED PROTECTORS |
| 998 |
d03fb894584a4318d1780e9a7b0b47eb No 2c75f519b9c9959d |
| 999 |
16382f282d7b29ee27e6460151d03382 No 7626382168311a9d |
| 1000 |
fe1c92009abc1cff4f3257c77e8134e3 No 6891f0a901f0065e |
| 1001 |
>>>>> fscrypt status /mnt/disk/dir1 |
| 1002 |
"/mnt/disk/dir1" is encrypted with fscrypt. |
| 1003 |
|
| 1004 |
Policy: 16382f282d7b29ee27e6460151d03382 |
| 1005 |
Options: padding:32 contents:AES_256_XTS filenames:AES_256_CTS policy_version:2 |
| 1006 |
Unlocked: No |
| 1007 |
|
| 1008 |
Protected with 1 protector: |
| 1009 |
PROTECTOR LINKED DESCRIPTION |
| 1010 |
7626382168311a9d No custom protector "Super Secret" |
| 1011 |
>>>>> fscrypt metadata add-protector-to-policy --protector=/mnt/disk:2c75f519b9c9959d --policy=/mnt/disk:16382f282d7b29ee27e6460151d03382 |
| 1012 |
WARNING: All files using this policy will be accessible with this protector!! |
| 1013 |
Protect policy 16382f282d7b29ee27e6460151d03382 with protector 2c75f519b9c9959d? [Y/n] |
| 1014 |
Enter key file for protector "Skeleton": secret.key |
| 1015 |
Enter custom passphrase for protector "Super Secret": |
| 1016 |
Protector 2c75f519b9c9959d now protecting policy 16382f282d7b29ee27e6460151d03382. |
| 1017 |
>>>>> fscrypt status /mnt/disk/dir1 |
| 1018 |
"/mnt/disk/dir1" is encrypted with fscrypt. |
| 1019 |
|
| 1020 |
Policy: 16382f282d7b29ee27e6460151d03382 |
| 1021 |
Options: padding:32 contents:AES_256_XTS filenames:AES_256_CTS policy_version:2 |
| 1022 |
Unlocked: No |
| 1023 |
|
| 1024 |
Protected with 2 protectors: |
| 1025 |
PROTECTOR LINKED DESCRIPTION |
| 1026 |
7626382168311a9d No custom protector "Super Secret" |
| 1027 |
2c75f519b9c9959d No raw key protector "Skeleton" |
| 1028 |
|
| 1029 |
# Now the unlock command will prompt for which protector we want to use |
| 1030 |
>>>>> fscrypt unlock /mnt/disk/dir1 |
| 1031 |
The available protectors are: |
| 1032 |
0 - custom protector "Super Secret" |
| 1033 |
1 - raw key protector "Skeleton" |
| 1034 |
Enter the number of protector to use: 1 |
| 1035 |
Enter key file for protector "Skeleton": secret.key |
| 1036 |
"/mnt/disk/dir1" is now unlocked and ready for use. |
| 1037 |
|
| 1038 |
# The protector can also be removed from the policy (if it is not the only one) |
| 1039 |
>>>>> fscrypt metadata remove-protector-from-policy --protector=/mnt/disk:2c75f519b9c9959d --policy=/mnt/disk:16382f282d7b29ee27e6460151d03382 |
| 1040 |
WARNING: All files using this policy will NO LONGER be accessible with this protector!! |
| 1041 |
Stop protecting policy 16382f282d7b29ee27e6460151d03382 with protector 2c75f519b9c9959d? [y/N] y |
| 1042 |
Protector 2c75f519b9c9959d no longer protecting policy 16382f282d7b29ee27e6460151d03382. |
| 1043 |
``` |
| 1044 |
|
| 1045 |
#### Quiet version |
| 1046 |
```bash |
| 1047 |
>>>>> echo "hunter2" | fscrypt metadata add-protector-to-policy --protector=/mnt/disk:2c75f519b9c9959d --policy=/mnt/disk:16382f282d7b29ee27e6460151d03382 --key=secret.key --quiet |
| 1048 |
>>>>> fscrypt metadata remove-protector-from-policy --protector=/mnt/disk:2c75f519b9c9959d --policy=/mnt/disk:16382f282d7b29ee27e6460151d03382 --quiet --force |
| 1049 |
``` |
| 1050 |
|
| 1051 |
## Contributing |
| 1052 |
|
| 1053 |
We would love to accept your contributions to `fscrypt`. See the |
| 1054 |
`CONTRIBUTING.md` file for more information about signing the CLA and submitting |
| 1055 |
a pull request. |
| 1056 |
|
| 1057 |
## Troubleshooting |
| 1058 |
|
| 1059 |
In general, if you are encountering issues with `fscrypt`, |
| 1060 |
[open an issue](https://github.com/google/fscrypt/issues/new), following the |
| 1061 |
guidelines in `CONTRIBUTING.md`. We will try our best to help. |
| 1062 |
|
| 1063 |
#### I changed my login passphrase, now all my directories are inaccessible |
| 1064 |
|
| 1065 |
Usually, the PAM module `pam_fscrypt.so` will automatically detect changes to a |
| 1066 |
user's login passphrase and update the user's `fscrypt` login protector so that |
| 1067 |
they retain access their login-passphrase protected directories. However, |
| 1068 |
sometimes a user's login passphrase can become desynchronized from their |
| 1069 |
`fscrypt` login protector. This can happen if `root` assigns the user a new |
| 1070 |
passphrase without providing the old one, if the user's login passphrase is |
| 1071 |
managed by an external system such as LDAP, if the PAM module is not installed, |
| 1072 |
or if the PAM module is not properly configured. See [Enabling the PAM |
| 1073 |
module](#enabling-the-pam-module) for how to configure the PAM module. |
| 1074 |
|
| 1075 |
To fix a user's login protector, find the corresponding protector ID by running |
| 1076 |
`fscrypt status "/"`. Then, change this protector's passphrase by running: |
| 1077 |
``` |
| 1078 |
fscrypt metadata change-passphrase --protector=/:ID |
| 1079 |
``` |
| 1080 |
|
| 1081 |
#### Directories using my login passphrase are not automatically unlocking |
| 1082 |
|
| 1083 |
First, directories won't unlock if your session starts without password |
| 1084 |
authentication. The most common case of this is public-key ssh login. To |
| 1085 |
trigger a password authentication event, run `su $USER -c exit`. |
| 1086 |
|
| 1087 |
If your session did start with password authentication, then the following may |
| 1088 |
be causing the issue: |
| 1089 |
|
| 1090 |
* The PAM module might not be configured correctly. Ensure you have correctly |
| 1091 |
[configured the PAM module](#enabling-the-pam-module). |
| 1092 |
|
| 1093 |
* If your login passphrase recently changed, then it might have gotten out of |
| 1094 |
sync with your login protector. To fix this, [manually change your login |
| 1095 |
protector's |
| 1096 |
passphrase](#i-changed-my-login-passphrase-now-all-my-directories-are-inaccessible) |
| 1097 |
to get it back in sync with your actual login passphrase. |
| 1098 |
|
| 1099 |
* Due to a [bug in sshd](https://bugzilla.mindrot.org/show_bug.cgi?id=2548), |
| 1100 |
encrypted directories won't auto-unlock when logging in with ssh using the |
| 1101 |
`ChallengeResponseAuthentication` ssh authentication method, which is also |
| 1102 |
called `KbdInteractiveAuthentication`. This ssh authentication method |
| 1103 |
implements password authentication by default, so it might appear similar to |
| 1104 |
`PasswordAuthentication`. However, only `PasswordAuthentication` works with |
| 1105 |
`fscrypt`. To avoid this issue, make sure that your `/etc/ssh/sshd_config` |
| 1106 |
file contains `PasswordAuthentication yes`, `UsePAM yes`, and either |
| 1107 |
`ChallengeResponseAuthentication no` or `KbdInteractiveAuthentication no`. |
| 1108 |
|
| 1109 |
#### Getting "encryption not enabled" on an ext4 filesystem |
| 1110 |
|
| 1111 |
This is usually caused by your ext4 filesystem not having the `encrypt` feature |
| 1112 |
flag enabled. The `encrypt` feature flag allows the filesystem to contain |
| 1113 |
encrypted files. (It doesn't actually encrypt anything by itself.) |
| 1114 |
|
| 1115 |
Before enabling `encrypt` on your ext4 filesystem, first ensure that all of the |
| 1116 |
following are true for you: |
| 1117 |
|
| 1118 |
* You only need to use your filesystem on kernels v4.1 and later. |
| 1119 |
|
| 1120 |
(Kernels v4.0 and earlier can't mount ext4 filesystems that have the `encrypt` |
| 1121 |
feature flag.) |
| 1122 |
|
| 1123 |
* Either you only need to use your filesystem on kernels v5.5 and later, or your |
| 1124 |
kernel page size (run `getconf PAGE_SIZE`) and filesystem block size (run |
| 1125 |
`tune2fs -l /dev/device | grep 'Block size'`) are the same. |
| 1126 |
|
| 1127 |
(Both values will almost always be 4096, but they may differ if your |
| 1128 |
filesystem is very small, if your system uses the PowerPC CPU architecture, or |
| 1129 |
if you overrode the default block size when you created the filesystem. Only |
| 1130 |
kernels v5.5 and later support ext4 encryption in such cases.) |
| 1131 |
|
| 1132 |
* Either you aren't using GRUB to boot directly off the filesystem in question, |
| 1133 |
or you are using GRUB 2.04 or later. |
| 1134 |
|
| 1135 |
(Old versions of GRUB can't boot from ext4 filesystems that have `encrypt` |
| 1136 |
enabled. If, like most people, you have a separate `/boot` partition, you are |
| 1137 |
fine. You are also fine if you are using the GRUB Debian package `2.02-2` or |
| 1138 |
later [*not* `2.02_beta*`], including the version in Ubuntu 18.04 and later, |
| 1139 |
since the patch to support `encrypt` was backported.) |
| 1140 |
|
| 1141 |
After verifying all of the above, enable `encrypt` by running: |
| 1142 |
``` |
| 1143 |
tune2fs -O encrypt /dev/device |
| 1144 |
``` |
| 1145 |
|
| 1146 |
If you need to undo this, first delete all encrypted files and directories on |
| 1147 |
the filesystem. Then, run: |
| 1148 |
``` |
| 1149 |
fsck -fn /dev/device |
| 1150 |
debugfs -w -R "feature -encrypt" /dev/device |
| 1151 |
fsck -fn /dev/device |
| 1152 |
``` |
| 1153 |
|
| 1154 |
If you've enabled `encrypt` but you still get the "encryption not enabled" |
| 1155 |
error, then the problem is that ext4 encryption isn't enabled in your kernel |
| 1156 |
config. See [Runtime dependencies](#runtime-dependencies) for how to enable it. |
| 1157 |
|
| 1158 |
#### Getting "user keyring not linked into session keyring" |
| 1159 |
|
| 1160 |
Some older versions of Ubuntu didn't link the user keyring into the session |
| 1161 |
keyring, which caused problems with `fscrypt`. |
| 1162 |
|
| 1163 |
To avoid this issue, upgrade to Ubuntu 20.04 or later. |
| 1164 |
|
| 1165 |
#### Getting "Operation not permitted" when moving files into an encrypted directory |
| 1166 |
|
| 1167 |
Originally, filesystems didn't return the correct error code when attempting to |
| 1168 |
rename unencrypted files (or files with a different encryption policy) into an |
| 1169 |
encrypted directory. Specifically, they returned `EPERM` instead of `EXDEV`, |
| 1170 |
which caused `mv` to fail rather than fall back to a copy as expected. |
| 1171 |
|
| 1172 |
This bug was fixed in version 5.1 of the mainline Linux kernel, as well as in |
| 1173 |
versions 4.4 and later of the LTS (Long Term Support) branches of the Linux |
| 1174 |
kernel; specifically v4.19.155, 4.14.204, v4.9.242, and v4.4.242. |
| 1175 |
|
| 1176 |
If the kernel can't be upgraded, this bug can be worked around by explicitly |
| 1177 |
copying the files instead, e.g. with `cp`. |
| 1178 |
|
| 1179 |
__IMPORTANT:__ Encrypting existing files can be insecure. Before doing so, read |
| 1180 |
[Encrypting existing files](#encrypting-existing-files). |
| 1181 |
|
| 1182 |
#### Getting "Package not installed" when trying to use an encrypted directory |
| 1183 |
|
| 1184 |
Trying to create or open an encrypted file will fail with `ENOPKG` ("Package not |
| 1185 |
installed") when the kernel doesn't support one or more of the cryptographic |
| 1186 |
algorithms used by the file or its directory. Note that `fscrypt encrypt` and |
| 1187 |
`fscrypt unlock` will still succeed; it's only using the directory afterwards |
| 1188 |
that will fail. |
| 1189 |
|
| 1190 |
The kernel will always support the algorithms that `fscrypt` uses by default. |
| 1191 |
However, if you changed the contents and/or filenames encryption algorithms in |
| 1192 |
[`/etc/fscrypt.conf`](#configuration-file), then you may run into this issue. |
| 1193 |
To fix it, enable the needed `CONFIG_CRYPTO_*` options in your Linux kernel |
| 1194 |
configuration. See the [kernel |
| 1195 |
documentation](https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#encryption-modes-and-usage) |
| 1196 |
for details about which option(s) are required for each encryption mode. |
| 1197 |
|
| 1198 |
#### Some processes can't access unlocked encrypted files |
| 1199 |
|
| 1200 |
This issue is caused by a limitation in the original design of Linux native |
| 1201 |
filesystem encryption which made it difficult to ensure that all processes can |
| 1202 |
access unlocked encrypted files. This issue can manifest in many ways, such as: |
| 1203 |
|
| 1204 |
* SSH to a user with an encrypted home directory not working, even when that |
| 1205 |
directory is already unlocked |
| 1206 |
|
| 1207 |
* Docker containers being unable to access encrypted files that were unlocked |
| 1208 |
from outside the container |
| 1209 |
|
| 1210 |
* NetworkManager being unable to access certificates stored in the user's |
| 1211 |
already-unlocked encrypted home directory |
| 1212 |
|
| 1213 |
* Other system services being unable to access already-unlocked encrypted files |
| 1214 |
|
| 1215 |
* `sudo` sessions being unable to access already-unlocked encrypted files |
| 1216 |
|
| 1217 |
* A user being unable to access encrypted files that were unlocked by root |
| 1218 |
|
| 1219 |
If an OS-level error is shown, it is `ENOKEY` ("Required key not available"). |
| 1220 |
|
| 1221 |
To fix this issue, first run `fscrypt status $dir`, where `$dir` is your |
| 1222 |
encrypted directory. If the output contains `policy_version:2`, then your issue |
| 1223 |
is something else, so stop reading now. If the output contains |
| 1224 |
`policy_version:1` or doesn't contain any mention of `policy_version`, then |
| 1225 |
you'll need to upgrade your directory(s) to policy version 2. To do this: |
| 1226 |
|
| 1227 |
1. Upgrade to Linux kernel v5.4 or later. |
| 1228 |
|
| 1229 |
2. Upgrade to `fscrypt` v0.2.7 or later. |
| 1230 |
|
| 1231 |
3. Run `sudo fscrypt setup --force`. |
| 1232 |
|
| 1233 |
4. Re-encrypt your encrypted directory(s). Since files cannot be (re-)encrypted |
| 1234 |
in-place, this requires replacing them with new directories. For example: |
| 1235 |
``` |
| 1236 |
fscrypt unlock dir # if not already unlocked |
| 1237 |
mkdir dir.new |
| 1238 |
fscrypt encrypt dir.new |
| 1239 |
cp -a -T dir dir.new |
| 1240 |
find dir -type f -print0 | xargs -0 shred -n1 --remove=unlink |
| 1241 |
rm -rf dir |
| 1242 |
mv dir.new dir |
| 1243 |
``` |
| 1244 |
|
| 1245 |
You don't need to create a new protector. I.e., when `fscrypt encrypt` asks |
| 1246 |
for a protector, just choose the one you were using before. |
| 1247 |
|
| 1248 |
5. `fscrypt status` on your directory(s) should now show `policy_version:2`, |
| 1249 |
and the issue should be gone. |
| 1250 |
|
| 1251 |
Note that once your directories are using policy version 2, they will only be |
| 1252 |
usable with Linux kernel v5.4 and later and `fscrypt` v0.2.6 and later. So be |
| 1253 |
careful not to downgrade your software past those versions. |
| 1254 |
|
| 1255 |
This issue can also be fixed by setting `"use_fs_keyring_for_v1_policies": true` |
| 1256 |
in `/etc/fscrypt.conf`, as described in [Configuration |
| 1257 |
file](#configuration-file). This avoids needing to upgrade directories to |
| 1258 |
policy version 2. However, this has some limitations, and the same kernel and |
| 1259 |
`fscrypt` prerequisites still apply for this option to take effect. It is |
| 1260 |
recommended to upgrade your directories to policy version 2 instead. |
| 1261 |
|
| 1262 |
#### Users can access other users' unlocked encrypted files |
| 1263 |
|
| 1264 |
This is working as intended. When an encrypted directory is unlocked (or |
| 1265 |
locked), it is unlocked (or locked) for all users. Encryption is not access |
| 1266 |
control; the Linux kernel already has many access control mechanisms, such as |
| 1267 |
the standard UNIX file permissions, that can be used to control access to files. |
| 1268 |
|
| 1269 |
Setting the mode of your encrypted directory to `0700` will prevent users other |
| 1270 |
than the directory's owner and `root` from accessing it while it is unlocked. |
| 1271 |
In `fscrypt` v0.2.5 and later, `fscrypt encrypt` sets this mode automatically. |
| 1272 |
|
| 1273 |
Having the locked/unlocked status of directories be global instead of per-user |
| 1274 |
may seem unintuitive, but it is actually the only logical way. The encryption |
| 1275 |
is done by the filesystem, so in reality the filesystem either has the key or it |
| 1276 |
doesn't. And once it has the key, any additional checks of whether particular |
| 1277 |
users "have" the key would be OS-level access control checks (not cryptography) |
| 1278 |
that are redundant with existing OS-level access control mechanisms. |
| 1279 |
|
| 1280 |
Similarly, any attempt of the filesystem encryption feature to prevent `root` |
| 1281 |
from accessing unlocked encrypted files would be pointless. On Linux systems, |
| 1282 |
`root` is usually all-powerful and can always get access to files in ways that |
| 1283 |
cannot be prevented, e.g. `setuid()` and `ptrace()`. The only reliable way to |
| 1284 |
limit what `root` can do is via a mandatory access control system, e.g. SELinux. |
| 1285 |
|
| 1286 |
The original design of Linux native filesystem encryption actually did put the |
| 1287 |
keys into per-user keyrings. However, this caused a [massive number of |
| 1288 |
problems](#some-processes-cant-access-unlocked-encrypted-files), as it's |
| 1289 |
actually very common that encrypted files need to be accessed by processes |
| 1290 |
running under different user IDs -- even if it may not be immediately apparent. |
| 1291 |
|
| 1292 |
#### Getting "Required key not available" when backing up locked encrypted files |
| 1293 |
|
| 1294 |
Encrypted files can't be backed up while locked; you need to unlock them first. |
| 1295 |
For details, see [Backup, restore, and recovery](#backup-restore-and-recovery). |
| 1296 |
|
| 1297 |
#### The reported size of encrypted symlinks is wrong |
| 1298 |
|
| 1299 |
Originally, filesystems didn't conform to POSIX when reporting the size of |
| 1300 |
encrypted symlinks, as they gave the size of the ciphertext symlink target |
| 1301 |
rather than the size of the plaintext target. This would make the reported size |
| 1302 |
of symlinks appear to be slightly too large when queried using ``lstat()`` or |
| 1303 |
similar system calls. Most programs don't care about this, but in rare cases |
| 1304 |
programs can depend on the filesystem reporting symlink sizes correctly. |
| 1305 |
|
| 1306 |
This bug was fixed in version 5.15 of the mainline Linux kernel, as well as in |
| 1307 |
versions 4.19 and later of the LTS (Long Term Support) branches of the Linux |
| 1308 |
kernel; specifically v5.10.63, v5.4.145, and v4.19.207. |
| 1309 |
|
| 1310 |
If the kernel can't be upgraded, the only workaround for this bug is to update |
| 1311 |
any affected programs to not depend on symlink sizes being reported correctly. |
| 1312 |
|
| 1313 |
## Legal |
| 1314 |
|
| 1315 |
Copyright 2017 Google Inc. under the |
| 1316 |
[Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0); see the |
| 1317 |
`LICENSE` file for more information. |
| 1318 |
|
| 1319 |
Author: Joe Richey <[email protected]> |
| 1320 |
|
| 1321 |
This is not an official Google product. |