aboutsummaryrefslogtreecommitdiff
path: root/cmd/fscrypt
AgeCommit message (Collapse)Author
2022-02-23bash_completion: fix command injection and incorrect completionsEric Biggers
Mountpoint paths might be untrusted arbitrary strings; the fscrypt bash completion script might need to complete to such strings. Unfortunately, the design of bash completion places some major footguns in the way of doing this correctly and securely: - "compgen -W" expands anything passed to it, so the argument to -W must be single-quoted to avoid an extra level of expansion. - The backslashes needed to escape meta-characters in the completed text aren't added automatically; they must be explicitly added. Note that the completion script for 'umount' used to have these same bugs (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=892179, https://github.com/util-linux/util-linux/issues/539). Fix these bugs in roughly the same way that 'umount' fixed them.
2022-02-23Make the output of 'fscrypt status' unambiguousEric Biggers
Following the example of /proc/self/mountinfo, replace the space, newline, tab, and backslash characters with octal escape sequences so that the output can be parsed unambiguously.
2021-12-23cmd/fscrypt: don't load protector in remove-protector-from-policyEric Biggers
Make remove-protector-from-policy work even if the protector cannot be loaded (for example, due to having been deleted already). Fixes https://github.com/google/fscrypt/issues/258 Fixes https://github.com/google/fscrypt/issues/272
2021-12-21README: write "Linux native filesystem encryption"Eric Biggers
"Linux filesystem encryption" sounds too vague. Write "Linux native filesystem encryption" instead.
2021-11-29cmd/fscrypt: read key from stdinDimitry Ishenko
Fixes #123
2021-10-05Adjust recovery passphrase generationEric Biggers
As per the feedback at https://github.com/google/fscrypt/issues/115 where users didn't understand that the recovery passphrase is important, restore the original behavior where recovery passphrase generation happens automatically without a prompt. This applies to the case where 'fscrypt encrypt' is using a login protector on a non-root filesystem. However, leave the --no-recovery option so that the recovery passphrase can still be disabled if the user really wants to. Also, clarify the information provided about the recovery passphrase. Update https://github.com/google/fscrypt/issues/115
2021-09-14cmd/fscrypt: recognize no-key names containing hyphenEric Biggers
In Linux 5.15, the no-key name format is changing again; see https://git.kernel.org/linus/ba47b515f5940603. isPossibleNoKeyName() sometimes doesn't recognize the new no-key names. Update it accordingly to recognize all possible no-key names. Note: isPossibleNoKeyName() is only used as a heuristic to check whether a v1-encrypted directory is incompletely locked or not. Therefore, it's not too important whether it works. However, this change is needed for cli-tests/t_v1_policy to pass.
2021-06-27cmd/fscrypt: fix detection of GRUB installationEric Biggers
Fix the GRUB detection logic to take into account that MOUNTPOINT/boot/grub might not be on the same filesystem as MOUNTPOINT, due to MOUNTPOINT/boot being another mountpoint. The warning is only appropriate when GRUB is installed on the same filesystem that encryption is going to be enabled on.
2021-04-27cmd/fscrypt: fix word mismatch "protector" => "policy"Gibeom Gwon
Fix word mismatch in usage and description of metadata create policy command.
2021-04-22cmd/fscrypt: use golang.org/x/termTobias Klauser
The golang.org/x/crypto/ssh/terminal package is deprecated and merely a wrapper around golang.org/x/term. Thus, use the latter directly.
2021-01-19cmd/fscrypt: fix missing protector error formatAlastair Hughes
Update #272
2020-11-30bash-completion: add completion scriptHenry-Joseph Audéoud
2020-11-07cmd/fscrypt: fix race condition in getPassphraseKey()Eric Biggers
Set the terminal to raw mode *before* printing the prompt. Otherwise the user (or the automated test) might enter the passphrase before the terminal gets put into raw mode. This is needed for some of the CLI tests to pass reliably in Travis CI.
2020-11-07cmd/fscrypt: fix isDirUnlockedHeuristic() on latest kernelsEric Biggers
On an "incompletely locked" directory, isDirUnlockedHeuristic() is supposed to return true, but on Linux v5.10-rc1 and later it returns false since now creating a subdirectory fails rather than succeeds. This change was intentional, so make isDirUnlockedHeuristic() apply a second heuristic too: also return true if any filenames in the directory don't appear to be valid no-key names. This fixes cli-tests/t_v1_encrypt on Linux v5.10-rc1 and later.
2020-08-09Fix nil error issue, Resolves https://github.com/google/fscrypt/issues/242bitcodr
2020-06-13cmd/fscrypt: adjust status message for v1-encrypted dirsEric Biggers
When 'fscrypt status DIR' detects that a v1-encrypted directory is still usable but its key seems to be absent, it shows the status as "Unlocked: Partially (incompletely locked)". But actually it can also be the case that the directory is unlocked by another user. Adjust the status message accordingly. This commit also fixes cli-tests/t_v1_policy.
2020-06-02cmd/fscrypt: fix 32-bit buildEric Biggers
statfs.Bsize actually has platform-dependent type, despite the Go documentation listing it as int64. Fix the build for 32-bit platforms by casting it to int64. Resolves https://github.com/google/fscrypt/issues/233
2020-05-14cmd/fscrypt: fix up path formatting in ErrDirNotEmpty suggestion (#229)Eric Biggers
Use %q, in case the paths contain whitespace. Also clean the directory path to remove trailing slashes before appending the ".new" suffix.
2020-05-13cmd/fscrypt: link to guide when interactively creating login protector (#225)Eric Biggers
Update https://github.com/google/fscrypt/issues/220
2020-05-09cmd/fscrypt: improve errorsEric Biggers
In checkEncryptable(), check whether the directory is already encrypted before checking whether it's empty. Also improve the error message for when a directory is nonempty. Finally, translate keyring.ErrKeyAddedByOtherUsers and keyring.ErrKeyFilesOpen into errors which include the directory.
2020-05-09cmd/fscrypt: remove ErrMaxPassphraseEric Biggers
This isn't actually a valid error since crypto.NewKeyFromReader() handles re-allocating the buffer to a larger size if it fills up.
2020-05-09filesystem: improve errorsEric Biggers
Introduce filesystem.ErrEncryptionNotEnabled and filesystem.ErrEncryptionNotSupported which include the Mount as context, and translate the corresponding metadata/ errors into them. Then make these errors show much better suggestions. Also replace lots of other filesystem/ errors with either custom types or with unnamed one-off errors that include more context. Fix backwards wrapping in lots of cases. Finally, don't include the mountpoint in places where it's not useful, like OS-level errors that already include the path.
2020-05-09metadata: improve errorsEric Biggers
ErrBadOwners: Rename to ErrDirectoryNotOwned for clarity, move it from cmd/fscrypt/ to metadata/ where it better belongs, and improve the message. ErrEncrypted: Rename to ErrAlreadyEncrypted for clarity, and include the path. ErrNotEncrypted: Include the path. ErrBadEncryptionOptions: Include the path and bad options. ErrEncryptionNotSupported: ErrEncryptionNotEnabled: Don't wrap with "get encryption policy %s", in preparation for wrapping these with filesystem-level context instead. Also avoid mixing together the error handling for the "get policy" and "set policy" ioctls. Make it very clear how we're handling the errors from each ioctl.
2020-05-09keyring: improve errorsEric Biggers
ErrAccessUserKeyring: Include the user, and fix the backwards wrapping. ErrSessionUserKeyring: Include the user. ErrKeyAdd: ErrKeyRemove: ErrKeySearch: ErrLinkUserKeyring: Replace these with one-off unnamed errors because they are never checked for, and this makes it easier for the callers to provide better messages, e.g. fixing the backwards wrapping.
2020-05-09crypto: improve errorsEric Biggers
ErrKeyLock: Rename to ErrMlockUlimit for clarity. ErrGetrandomFail: ErrKeyAlloc: ErrKeyFree: ErrNegativeLength: Replace these with one-off unnamed errors because these were all returned in only one place and were never checked for. Also these were all either wrapped backwards or discarded an underlying error, so fix that too.
2020-05-09actions/policy: improve errorsEric Biggers
ErrMissingPolicyMetadata: Include the mount, directory path, and metadata path. Also move the explanation into actions/ since it doesn't refer to any CLI command. ErrPolicyMetadataMismatch: Include a lot more information. Also start checking for consistency of the policy key descriptors, not just the encryption options. Add a test for this. ErrDifferentFilesystem: Include the mountpoints. ErrOnlyProtector: Clarify the message and include the protector descriptor. ErrAlreadyProtected: ErrNotProtected: Include the policy and protector descriptors. ErrAccessDeniedPossiblyV2: Make it slightly clearer what failed. Also move the explanation into actions/ since it doesn't refer to any CLI command.
2020-05-09actions/protector: improve errorsEric Biggers
ErrProtectorName: Rename to ErrLoginProtectorName for clarity, and include the name and user. ErrMissingProtectorName: Include the correct protector source. ErrDuplicateName: Rename to ErrProtectorNameExists for clarity, and remove a level of wrapping by including the name directly. ErrDuplicateUID: Rename to ErrLoginProtectorExists for clarity, and remove a level of wrapping by including the user directly.
2020-05-09actions/config: improve config file related errorsEric Biggers
ErrBadConfig: Fix backwards wrapping, include the bad config, and make it clear that this is an internal error. ErrBadConfigFile: Fix backwards wrapping, include the config file location, and adjust the suggestion slightly. ErrConfigFileExists: Include the config file location. ErrNoConfigFile: Include the config file location, and adjust the suggestion slightly.
2020-05-09cmd/fscrypt: make wrapText() support code blocksEric Biggers
Allow the input text to contain "code blocks" denoted by lines beginning with ">", e.g.: Foo bar baz: > echo foo > echo bar Instead of squashing these lines together, preserve the line breaks between them and add indentation, e.g.: Foo bar baz: echo foo echo bar
2020-05-09Try to detect incomplete locking of v1-encrypted directoryEric Biggers
'fscrypt lock' on a v1-encrypted directory doesn't warn about in-use files, as the kernel doesn't provide a way to easily detect it. Instead, implement a heuristic where we check whether a subdirectory can be created. If yes, then the directory must not be fully locked. Make both 'fscrypt lock' and 'fscrypt status' use this heuristic. Resolves https://github.com/google/fscrypt/issues/215
2020-05-09cmd/fscrypt: add FSCRYPT_CONSISTENT_OUTPUT environmental variableEric Biggers
Allow setting FSCRYPT_CONSISTENT_OUTPUT=1 in the environment to cause policies and protectors to sorted by last modification time. The CLI tests need this to make the output of 'fscrypt' ordered in a consistent way with regard to the operations performed.
2020-05-09cmd/fscrypt: add FSCRYPT_ROOT_MNT environmental variableEric Biggers
Allow overriding the mountpoint where login protectors are stored by setting the FSCRYPT_ROOT_MNT environmental variable. The CLI tests need this to avoid touching the real "/".
2020-05-09cmd/fscrypt: add FSCRYPT_CONF environmental variableEric Biggers
Allow overriding the location of fscrypt.conf by setting the FSCRYPT_CONF environmental variable. The CLI tests need this to avoid touching the real /etc/fscrypt.conf.
2020-03-23cmd: Simplify "fscrypt --version" output (#207)Joseph Richey
There's no need to include the build time, author, and copyright info in the output of "fscrypt --version". This information is: - Overly complex (the current string is hard to parse) - Inaccurate (there are other authors than just me) - Unnecessary (the Apache 2 license is for Source Code) - Makes reproducible builds impossible The default version string is just fine. Signed-off-by: Joe Richey <joerichey@google.com>
2020-03-23Improve error message when unlocking v2 policy is unsupportedEric Biggers
If trying to unlock a v2-encrypted directory fails because the kernel lacks support for v2 policies, show a better error message. This can happen if someone downgrades their kernel or tries to access encrypted directories on removable storage from a computer with an older kernel. Detecting this case is difficult since all we have to go with is EACCES when opening the directory. Implement a heuristic where if get EACCES, we actually have read access to the directory, and the kernel doesn't support v2 policies, we show the improved error message. Before: # fscrypt unlock dir [ERROR] fscrypt unlock: open dir: permission denied After: # fscrypt unlock dir [ERROR] fscrypt unlock: open dir: permission denied This may be caused by the directory using a v2 encryption policy and the current kernel not supporting it. If indeed the case, then this directory can only be used on kernel v5.4 and later. You can create directories accessible on older kernels by changing policy_version to 1 in /etc/fscrypt.conf.
2020-03-23Improve error message when setting v2 policy is unsupportedEric Biggers
If trying to encrypt a directory using a v2 policy fails due to the kernel lacking support for v2 policies, show a better error message. One way this can happen is if someone runs 'fscrypt setup' with a new kernel and then downgrades to an old kernel. Before: # echo -n hunter2 | fscrypt encrypt dir --source=custom_passphrase --name=foo --quiet [ERROR] fscrypt encrypt: inappropriate ioctl for device: system error: could not add key to the keyring After: # echo -n hunter2 | fscrypt encrypt dir --source=custom_passphrase --name=foo --quiet [ERROR] fscrypt encrypt: kernel is too old to support v2 encryption policies v2 encryption policies are only supported by kernel version 5.4 and later. Either use a newer kernel, or change policy_version to 1 in /etc/fscrypt.conf.
2020-03-23Create /etc/fscrypt.conf with policy_version 2 on kernel v5.4+Eric Biggers
v2 encryption policies are now recommended, due to various security and usability advantages over v1 policies. Many people have been running into the usability problems with v1, so it's desirable to get people onto v2 without having to manually opt-in. Therefore, when 'fscrypt setup' creates /etc/fscrypt.conf, enable policy_version 2 automatically if the kernel supports it. I decided to go with this solution over the policy_version "auto" I suggested originally because this way is simpler, it can still be changed to "auto" later if desired, and "auto" might require changing how we parse the config file (since currently the config file is mapped directly to a protobuf where policy_version is an 'int' and is shared with EncryptionOptions). Resolves https://github.com/google/fscrypt/issues/182
2020-03-23Simplify choosing the key description prefixEric Biggers
There's no real need to allow users to choose the key description prefix (a.k.a. the "service"), since on ext4 and f2fs we can just use "ext4" and "f2fs" for compatibility with all kernels both old and new, and on other filesystems we can just use "fscrypt". So, let's do that. Since this removes the point of the "--legacy" option to 'fscrypt setup' and the "compatibility" field in /etc/fscrypt.conf, remove those too. Specifically, we start ignoring the "compatibility" in existing config files and not writing it to new ones. The corresponding protobuf field number and name are reserved. We stop accepting the "--legacy" option at all, although since it was default true and there was no real reason for anyone to change it to false, probably no one will notice. If anyone does, they should just stop specifying the option. Note that this change only affects user keyrings and thus only affects v1 encryption policies, which are deprecated in favor of v2 anyway.
2020-01-29cmd/fscrypt/commands: allow disabling recovery passphrase (#193)Eric Biggers
While it's important to generate a recovery passphrase in the linked protector case to avoid data loss if the system is reinstalled, some people really don't want it (even though it can be safely ignored as it almost certainly has far more entropy than the login passphrase). As a compromise, prompt for y/n before generating it, with default y. Also, to allow disabling the recovery passphrase during noninteractive use, add a --no-recovery command-line option. Update https://github.com/google/fscrypt/issues/186
2020-01-28cmd/fscrypt/commands: clean up properly when encryptPath() failsEric Biggers
Move the deferred locking and deletion of the policy on failure to the correct places, so that it's done in all failure cases, including in the case where adding the recovery protector fails. Also make the recovery protector be locked and deleted on failure. Finally, put all the code to do deferred deprovisioning of the policy in the same place: right after it's provisioned.
2020-01-28cmd/fscrypt/errors: explicitly mark error messages as errors (#191)Eric Biggers
When an fscrypt command fails and prints an error message, in some cases it isn't clear that the message is actually an error, e.g.: fscrypt encrypt: login protectors do not need a name Make it clear by always prefixing the message with "[ERROR] ", e.g. [ERROR] fscrypt encrypt: login protectors do not need a name Update https://github.com/google/fscrypt/issues/186
2020-01-28cmd/fscrypt/setup: don't prompt to create /etc/fscrypt.conf (#190)Eric Biggers
When 'fscrypt setup' sees that /etc/fscrypt.conf doesn't exist, don't ask for confirmation before creating it. Just do it. This is the normal use, and there's not a good reason to ask the user to confirm it.
2020-01-22Automatically generate recovery passphrase when usefulEric Biggers
If a user re-installs their system (or otherwise loses the /.fscrypt directory on the root filesystem) they also lose access to any login passphrase-protected directories on other filesystems, unless additional protectors were manually added. This can be unexpected, as it may be expected that the old login passphrase would still work. We can't really fix this by storing a login protector on every filesystem because: - If a user were to have N login protectors, it would take them N times longer to log in, as every login protector would need to be unlocked. - If a user were to change their login passphrase while any external volumes were unmounted, login protectors would get out of sync. - It's preferable that an external volume isn't unlockable by itself using only a login passphrase, as login passphrases are often weak. Instead, generate a recovery passphrase when creating a login passphrase-protected directory on a non-root filesystem. The recovery passphrase is added as a custom_passphrase protector, thus giving the policy two protectors: one pam_passphrase and one custom_passphrase. Then this passphrase is written to a file in the new encrypted directory. Writing the passphrase to a file here is okay since it's encrypted, but it's obviously useless by itself; it's up to the user to store this passphrase somewhere else if they need it. Use a recovery passphrase instead of a "recovery code" that encodes the policy key directly because a passphrase is more user-friendly: it can safely be made much shorter than a key, and it works just like any other fscrypt protector. Also, it's not as critical to allow recovery when the .fscrypt directory on the *same* filesystem is deleted. Resolves https://github.com/google/fscrypt/issues/164
2020-01-05cmd/fscrypt, keyring: add --all-users option to 'fscrypt lock'Eric Biggers
Allow root to provide the --all-users option to 'fscrypt lock' to force an encryption key to be removed from the filesystem (i.e., force an encrypted directory to be locked), even if other users have added it. To implement this option, we just need to use the FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS ioctl rather than FS_IOC_REMOVE_ENCRYPTION_KEY. In theory this option could be implemented for the user keyrings case too, but it would be difficult and the user keyrings are being deprecated for fscrypt, so don't bother.
2020-01-05Keyring support for v2 encryption policiesEric Biggers
Implement adding/removing v2 encryption policy keys to/from the kernel. The kernel requires that the new ioctls FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY be used for this. Root is not required. However, non-root support brings an extra complication: the kernel keeps track of which users have called FS_IOC_ADD_ENCRYPTION_KEY for the same key. FS_IOC_REMOVE_ENCRYPTION_KEY only works as one of these users, and it only removes the calling user's claim to the key; the key is only truly removed when the last claim is removed. Implement the following behavior: - 'fscrypt unlock' and pam_fscrypt add the key for the user, even if other user(s) have it added already. This behavior is needed so that another user can't remove the key out from under the user. - 'fscrypt lock' and pam_fscrypt remove the key for the user. However, if the key wasn't truly removed because other users still have it added, 'fscrypt lock' prints a warning. - 'fscrypt status' shows whether the directory is unlocked for anyone.
2020-01-05cmd/fscrypt: adjust user and keyring validation and preparationEric Biggers
Don't force the user to provide a --user argument when running fscrypt as root if they're doing something where the TargetUser isn't actually needed, such as provisioning/deprovisioning a v1 encryption policy to/from the filesystem keyring, or creating a non-login protector. Also don't set up the user keyring (or check for it being set up) if it won't actually be used. Finally, if we'll be provisioning/deprovisioning a v1 encryption policy to/from the filesystem keyring, make sure the command is running as root, since the kernel requires this.
2020-01-05cmd/fscrypt: add 'fscrypt lock' commandEric Biggers
Add support for 'fscrypt lock'. This command "locks" a directory, undoing 'fscrypt unlock'. When the filesystem keyring is used, 'fscrypt lock' also detects when a directory wasn't fully locked due to some files still being in-use. It can then be run again later to try to finish locking the files.
2020-01-05keyring: support filesystem keyring with v1 encryption policiesEric Biggers
Linux v5.4 and later allows fscrypt keys to be added/removed directly to/from the filesystem via the new ioctls FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY. Among other benefits, these fix the key visibility problems that many users have been running into, where system services and containers can't access encrypted files. Allow the user to opt-in to using these new ioctls for their existing encrypted directories by setting in their /etc/fscrypt.conf: "use_fs_keyring_for_v1_policies": true Note that it can't really be on by default, since for v1 policies the ioctls require root, whereas user keyrings don't. I.e., setting this to true means that users will need to use 'sudo fscrypt unlock', not 'fscrypt unlock'. v2 policies won't have this restriction.
2020-01-05Add keyring packageEric Biggers
In preparation for introducing support for the new filesystem-level keyrings, move the existing user keyring management code from security/keyring.go and crypto/crypto.go into a new package, 'keyring'. This package provides functions AddEncryptionKey, RemoveEncryptionKey, and GetEncryptionKeyStatus which delegate to either the filesystem keyring (added by a later patch) or to the user keyring. This provides a common interface to both types of keyrings, to the extent possible.
2019-11-27cmd/fscrypt: preserve paragraphs in wrapText()Eric Biggers
Preserve empty lines rather than squashing them into a single space. This allows having command descriptions that contain multiple paragraphs. This also eliminates the need to have a special case for ordered lists.