]> git.apps.os.sepia.ceph.com Git - fscrypt.git/log
fscrypt.git
5 years agoImprove error message when setting v2 policy is unsupported
Eric Biggers [Wed, 18 Mar 2020 04:10:58 +0000 (21:10 -0700)]
Improve error message when setting v2 policy is unsupported

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.

5 years agoCreate /etc/fscrypt.conf with policy_version 2 on kernel v5.4+
Eric Biggers [Wed, 18 Mar 2020 04:10:58 +0000 (21:10 -0700)]
Create /etc/fscrypt.conf with policy_version 2 on kernel v5.4+

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

5 years agoSimplify choosing the key description prefix
Eric Biggers [Wed, 18 Mar 2020 04:10:58 +0000 (21:10 -0700)]
Simplify choosing the key description prefix

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.

5 years agoREADME.md: improve documentation for PAM configuration (#204)
Eric Biggers [Fri, 20 Mar 2020 04:50:04 +0000 (21:50 -0700)]
README.md: improve documentation for PAM configuration (#204)

5 years agoMakefile: clean up installation commands (#201)
Eric Biggers [Sat, 22 Feb 2020 21:28:17 +0000 (13:28 -0800)]
Makefile: clean up installation commands (#201)

Improve the documentation for the installation-related Makefile
variables, and update the commands to remove the forward slash after
$(DESTDIR) in order to remove a duplicate forward slash and match the
recommended usage.

5 years agoUse DESTDIR for install prefix (#200)
Anatol Pomozov [Wed, 19 Feb 2020 07:32:50 +0000 (23:32 -0800)]
Use DESTDIR for install prefix (#200)

DESTDIR has a well established purpose
https://www.gnu.org/prep/standards/html_node/DESTDIR.html
It is a suffix for all the files to be installed. And it is used by
package managers who installs the files into some $tmpdir before
creating a package.

Change the build commands to follow this convention.

Add BINDIR that does the same what previous did $DESTDIR.

5 years agoRelease version v0.2.6 (#198)
Joseph Richey [Tue, 11 Feb 2020 07:20:14 +0000 (23:20 -0800)]
Release version v0.2.6 (#198)

* Release version v0.2.6

Fixes #195

Also, update the encrypted API key.
My person access token had expired, this one should work now.

Signed-off-by: Joe Richey <joerichey@google.com>
5 years agocmd/fscrypt/commands: allow disabling recovery passphrase (#193)
Eric Biggers [Thu, 30 Jan 2020 03:27:10 +0000 (19:27 -0800)]
cmd/fscrypt/commands: allow disabling recovery passphrase (#193)

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

5 years agoMerge pull request #192 from ebiggers/cleanup-on-error
Eric Biggers [Thu, 30 Jan 2020 02:46:57 +0000 (18:46 -0800)]
Merge pull request #192 from ebiggers/cleanup-on-error

Clean up policies and protectors on error

5 years agoactions/policy: revert new protector links on failure
Eric Biggers [Tue, 28 Jan 2020 04:16:35 +0000 (20:16 -0800)]
actions/policy: revert new protector links on failure

Ensure that when an encryption policy is reverted (e.g. due to
encryptPath() failing after the policy was created), we also delete any
new protector links that were created for the policy, as this is not
handled by the logic that reverts new protectors.

5 years agofilesystem: don't overwrite existing protector links
Eric Biggers [Tue, 28 Jan 2020 04:16:35 +0000 (20:16 -0800)]
filesystem: don't overwrite existing protector links

When adding a protector to a policy, don't unconditionally overwrite the
protector link, because it may already exist.  Instead, if it already
exists and points to the mount, just use it.  If it already exists and
points to the wrong place, return an error.

Also add a bool to the return value of AddLinkedProtector() so that
callers can check whether the link was newly created or not.

5 years agocmd/fscrypt/commands: clean up properly when encryptPath() fails
Eric Biggers [Tue, 28 Jan 2020 04:16:35 +0000 (20:16 -0800)]
cmd/fscrypt/commands: clean up properly when encryptPath() fails

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.

5 years agoactions/recovery: revert protector if it can't be added to policy
Eric Biggers [Tue, 28 Jan 2020 04:16:35 +0000 (20:16 -0800)]
actions/recovery: revert protector if it can't be added to policy

Ensure that a failed AddRecoveryPassphrase() doesn't leave around an
unneeded protector file.

5 years agocmd/fscrypt/errors: explicitly mark error messages as errors (#191)
Eric Biggers [Tue, 28 Jan 2020 09:58:51 +0000 (01:58 -0800)]
cmd/fscrypt/errors: explicitly mark error messages as errors (#191)

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

5 years agocmd/fscrypt/setup: don't prompt to create /etc/fscrypt.conf (#190)
Eric Biggers [Tue, 28 Jan 2020 09:57:46 +0000 (01:57 -0800)]
cmd/fscrypt/setup: don't prompt to create /etc/fscrypt.conf (#190)

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.

5 years agoactions/recovery: ensure recovery passphrase is really custom_passphrase
Eric Biggers [Tue, 28 Jan 2020 03:24:30 +0000 (19:24 -0800)]
actions/recovery: ensure recovery passphrase is really custom_passphrase

If the login protector was just created by the same 'fscrypt encrypt'
command, then policy.Context.Config.Source will be pam_passphrase.  This
needs to be overridden to custom_passphrase when creating the protector
for the recovery passphrase.

This fixes the following error:

    fscrypt encrypt: login protectors do not need a name

Resolves https://github.com/google/fscrypt/issues/187
Update https://github.com/google/fscrypt/issues/186

5 years agoDocument how to check for kernel config options (#183)
ebiggers [Thu, 23 Jan 2020 21:46:18 +0000 (13:46 -0800)]
Document how to check for kernel config options (#183)

Resolves https://github.com/google/fscrypt/issues/181

5 years agoprivileges.go: remove a stale comment (#184)
ebiggers [Thu, 23 Jan 2020 21:42:10 +0000 (13:42 -0800)]
privileges.go: remove a stale comment (#184)

The workaround for Go versions before 1.10 was already removed by
commit 3022c1603d96 ("Ensure setting user privileges is reversible").

5 years agofilesystem: remove canonicalizePath() (#185)
ebiggers [Thu, 23 Jan 2020 21:41:42 +0000 (13:41 -0800)]
filesystem: remove canonicalizePath() (#185)

canonicalizePath() is now only used by an error path in
getMountFromLink(), which we can make use getDeviceName() instead.

5 years agoMerge pull request #167 from ebiggers/recovery-passphrase
ebiggers [Thu, 23 Jan 2020 03:16:20 +0000 (19:16 -0800)]
Merge pull request #167 from ebiggers/recovery-passphrase

Automatically generate recovery passphrase when useful

5 years agoAutomatically generate recovery passphrase when useful
Eric Biggers [Wed, 27 Nov 2019 20:04:13 +0000 (12:04 -0800)]
Automatically generate recovery passphrase when useful

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

5 years agoMerge pull request #148 from ebiggers/fscrypt-key-mgmt-improvements
ebiggers [Thu, 23 Jan 2020 02:28:23 +0000 (18:28 -0800)]
Merge pull request #148 from ebiggers/fscrypt-key-mgmt-improvements

Filesystem keyring and v2 encryption policy support

5 years agoREADME.md: document new settings and troubleshooting key access
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
README.md: document new settings and troubleshooting key access

Document the new /etc/fscrypt.conf settings for the filesystem keyring
and v2 encryption policies, and add a new subsection for troubleshooting
key access problems.

5 years agocmd/fscrypt, keyring: add --all-users option to 'fscrypt lock'
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
cmd/fscrypt, keyring: add --all-users option to 'fscrypt lock'

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.

5 years agoKeyring support for v2 encryption policies
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
Keyring support for v2 encryption policies

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.

5 years agoMetadata support for v2 encryption policies
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
Metadata support for v2 encryption policies

Linux v5.4 and later supports v2 encryption policies.  These have
several advantages over v1 encryption policies:

- Their encryption keys can be added/removed to/from the filesystem by
  non-root users, thus gaining the benefits of the filesystem keyring
  while also retaining support for non-root use.

- They use a more standard, secure, and flexible key derivation
  function.  Because of this, some future kernel-level fscrypt features
  will be implemented for v2 policies only.

- They prevent a denial-of-service attack where a user could associate
  the wrong key with another user's encrypted files.

Prepare the fscrypt tool to support v2 encryption policies by:

- Adding a policy_version field to the EncryptionOptions, i.e. to the
  config file and to the policy metadata files.

- Using the kernel-specified algorithm to compute the key descriptor for
  v2 policies.

- Handling setting and getting v2 policies.

Actually adding/removing the keys for v2 policies to/from the kernel is
left for the next patch.

5 years agopam_fscrypt: update to handle filesystem keyring
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
pam_fscrypt: update to handle filesystem keyring

FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY require root
for v1 policy keys, so update the PAM module to re-acquire root
privileges while provisioning/deprovisioning policies that need this.

Also, only set up the user keyring if it will actually be used.

5 years agocmd/fscrypt: adjust user and keyring validation and preparation
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
cmd/fscrypt: adjust user and keyring validation and preparation

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.

5 years agocmd/fscrypt: add 'fscrypt lock' command
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
cmd/fscrypt: add 'fscrypt lock' command

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.

5 years agokeyring: support filesystem keyring with v1 encryption policies
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
keyring: support filesystem keyring with v1 encryption policies

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.

5 years agoAdd keyring package
Eric Biggers [Mon, 16 Dec 2019 03:31:39 +0000 (19:31 -0800)]
Add keyring package

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.

5 years agoREADME.md: document /etc/fscrypt.conf
Eric Biggers [Sun, 5 Jan 2020 17:59:37 +0000 (09:59 -0800)]
README.md: document /etc/fscrypt.conf

5 years agokeyring: fix permission denied accessing user keyring (#177)
ebiggers [Mon, 16 Dec 2019 03:10:41 +0000 (19:10 -0800)]
keyring: fix permission denied accessing user keyring (#177)

When userKeyringIDLookup() looks up a user keyring, it links it into the
process keyring to ensure that the process retains the "possessor
privileges" over the user keyring, then caches the user keyring's ID.

Unfortunately, this use of the process keyring randomly fails because Go
creates threads before even init() and main() are run, and then can run
code on them later.  Since the kernel doesn't create the process keyring
until userspace requests it and the process keyring is actually a
per-thread property that's only inherited by new threads, different
threads in a Go process may see different process keyrings.

Fix this by removing the user keyring cache, switching from the process
keyring to the thread keyring, and using LockOSThread() to pin the
goroutine to an OS thread while needed to perform a keyring operation.

Resolves https://github.com/google/fscrypt/issues/176

5 years agoREADME.md: update output to match reality
Eric Biggers [Thu, 28 Nov 2019 18:06:32 +0000 (10:06 -0800)]
README.md: update output to match reality

Update the example output in the README to match reality.

Also make a few other updates to the examples to take into account that
'fscrypt purge' now drops caches by default, and that the root
filesystem doesn't need to support encryption if the encrypted
directories are being created on a different filesystem.

Resolves https://github.com/google/fscrypt/issues/62

5 years agoMerge pull request #172 from ebiggers/login-passphrase-doc-fix
Joseph Richey [Thu, 28 Nov 2019 07:43:12 +0000 (13:13 +0530)]
Merge pull request #172 from ebiggers/login-passphrase-doc-fix

README.md: remove obsolete warning about changing login passphrase

5 years agoREADME.md: remove obsolete warning about changing login passphrase
Eric Biggers [Thu, 28 Nov 2019 05:00:32 +0000 (21:00 -0800)]
README.md: remove obsolete warning about changing login passphrase

For some time now, fscrypt actually does re-wrap a user's login
protector when their login passphrase changes, provided that the PAM
configuration is correct.  Remove the obsolete paragraph.

Update https://github.com/google/fscrypt/issues/51

5 years agocmd/fscrypt: preserve paragraphs in wrapText()
Eric Biggers [Wed, 27 Nov 2019 19:55:28 +0000 (11:55 -0800)]
cmd/fscrypt: preserve paragraphs in wrapText()

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.

5 years agoRename some variables from 'target' to 'targetUser'
Eric Biggers [Wed, 27 Nov 2019 19:40:47 +0000 (11:40 -0800)]
Rename some variables from 'target' to 'targetUser'

Refer to the target User as 'targetUser' rather than simply 'target'.
This will help avoid confusion when we add support for the filesystem
keyring, since then the Mount will also be a "target".

5 years agoUse latest fscrypt declarations from sys/unix
Eric Biggers [Wed, 27 Nov 2019 19:28:14 +0000 (11:28 -0800)]
Use latest fscrypt declarations from sys/unix

Use the new name for fscrypt constants and structures which have been
given a new name.

Also use the named constant for the DIRECT_KEY fscrypt policy flag.

No change in behavior.  This is just preparing for future work.

5 years agoUpgrade to latest golang.org/x/sys module
Eric Biggers [Wed, 27 Nov 2019 19:28:14 +0000 (11:28 -0800)]
Upgrade to latest golang.org/x/sys module

Upgrade to get the new fscrypt declarations from Linux v5.4.

5 years agocmd/fscrypt: adjust message when listing protector sources
Eric Biggers [Wed, 27 Nov 2019 19:20:19 +0000 (11:20 -0800)]
cmd/fscrypt: adjust message when listing protector sources

Saying "Your data can be protected with one of the following sources" is
ambiguous because it could be interpreted to mean that an encrypted
directory can only have one type of protector.  In fact, an encrypted
directory can have multiple protectors, and they can be of any type.

Update https://github.com/google/fscrypt/issues/164

5 years agoAllow filesystem links to contain leading/trailing whitespace
Eric Biggers [Wed, 27 Nov 2019 19:07:38 +0000 (11:07 -0800)]
Allow filesystem links to contain leading/trailing whitespace

To make manually editing linked protectors slightly more user-friendly,
automatically strip any leading or trailing whitespace.  E.g. treat
"UUID=3a6d9a76-47f0-4f13-81bf-3332fbe984fb\n" the same as
"UUID=3a6d9a76-47f0-4f13-81bf-3332fbe984fb".

Update https://github.com/google/fscrypt/issues/115

5 years agotravis: set GO111MODULE=on for 'go get'
Eric Biggers [Wed, 27 Nov 2019 00:52:43 +0000 (16:52 -0800)]
travis: set GO111MODULE=on for 'go get'

Ensure that the environmental variable GO111MODULE is set to "on" when
running 'go get'.  This fixes a CI failure with Go 1.11 and 1.12.

5 years agoMerge pull request #162 from josephlr/master
Joseph Richey [Sat, 9 Nov 2019 04:31:53 +0000 (20:31 -0800)]
Merge pull request #162 from josephlr/master

Add Code of Conduct

5 years agoAdd Code of Conduct
Joe Richey [Wed, 6 Nov 2019 03:00:05 +0000 (19:00 -0800)]
Add Code of Conduct

This project has always been under a CoC, but I forgot to incude the
file when creating the repo. See also: https://opensource.google/conduct/

Signed-off-by: Joe Richey <joerichey@google.com>
5 years agoMerge pull request #154 from ebiggers/bind-mounts
Joseph Richey [Wed, 30 Oct 2019 21:49:40 +0000 (22:49 +0100)]
Merge pull request #154 from ebiggers/bind-mounts

Store fscrypt metadata in only one place per filesystem, so that bind
mounts don't get their own metadata directories (which was ambiguous,
as the same file may be accessible via multiple mounts).

Also correctly set the source device for root filesystems mounted via
the kernel command line, and fix creating linked protectors to such
filesystems.

5 years agofilesystem: add unit tests for loadMountInfo()
Eric Biggers [Tue, 29 Oct 2019 07:33:54 +0000 (00:33 -0700)]
filesystem: add unit tests for loadMountInfo()

Add a version of loadMountInfo() that takes an io.Reader parameter to
allow injecting a custom mountinfo file, then add some unit tests.

5 years agofilesystem: handle bind mounts properly
Eric Biggers [Tue, 29 Oct 2019 07:33:54 +0000 (00:33 -0700)]
filesystem: handle bind mounts properly

Currently, fscrypt treats bind mounts as separate filesystems.  This is
broken because fscrypt will look for a directory's encryption policy in
different places depending on which mount it's accessed through.  This
forces users to create an fscrypt metadata directory at every bind
mount, and to copy fscrypt metadata around between mounts.

Fix this by storing fscrypt metadata only at the root of the filesystem.

To accomplish this:

- Make mountsByDevice store only a single Mount per filesystem, rather
  than multiple.  For this Mount, choose a mount of the full filesystem
  if available, preferably a read-write mount.  If the filesystem has
  only bind mounts, store a nil entry in mountsByDevice so we can show a
  proper error message later.

- Change FindMount() and GetMount() to look up the Mount by device
  number rather than by path, so that they don't return different Mounts
  depending on which path is used.

- Change AllFilesystems() to not return bind mounts.

- Due to the above changes, the mountsByPath map is no longer needed
  outside of loadMountInfo().  So make it a local variable there.

Resolves https://github.com/google/fscrypt/issues/59

5 years agofilesystem: make link handling more robust
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: make link handling more robust

The previous patch fixed making linked protectors to /dev/root, by
setting Mount.Device to the real device node rather than /dev/root.

That's good, but it also hints that the linked protector handling is
unnecessarily fragile, as it relies on the device node name matching
exactly.  The Linux kernel allows the same device to have multiple
device nodes, and path comparisons are slow and error-prone in general.

Change it to compare the device number instead.

5 years agofilesystem: get correct device for kernel-mounted rootfs
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: get correct device for kernel-mounted rootfs

A root filesystem mounted via the kernel command line always has a
source of "/dev/root", which isn't a real device node.  This makes
fscrypt think this filesystem doesn't have a source device, which breaks
creating login passphrase-protected directories on other filesystems:

    fscrypt encrypt: filesystem /: no device for mount "/": system error: cannot create filesystem link

This also makes 'fscrypt status' show a blank source device:

    MOUNTPOINT  DEVICE          FILESYSTEM  ENCRYPTION     FSCRYPT
    /                           ext4        supported      Yes

To fix this case, update loadMountInfo() to map the device number to the
device name via sysfs rather than use the mount source field.

5 years agofilesystem: add device number utilities
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: add device number utilities

Add a utility type and functions for handling device numbers.

5 years agofilesystem: skip unnecessary mountpoint canonicalization
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: skip unnecessary mountpoint canonicalization

The kernel always shows mountpoints as absolute paths without symlinks,
so there's no need to canonicalize them in userspace.

5 years agofilesystem: switch to using /proc/self/mountinfo
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: switch to using /proc/self/mountinfo

Change loadMountInfo() to load the mounts directly from
/proc/self/mountinfo, rather than use the mntent.h C library calls.

This is needed for correct handling of bind mounts and of "/dev/root",
since /proc/self/mountinfo has extra fields which show the mounted
subtree and the filesystem's device number.  /proc/mounts lacks these
fields, and the C library calls can't provide them.

To start, this patch just switches to using /proc/self/mountinfo,
without doing anything with the extra fields yet.

As a bonus, this eliminates all C code in mountpoint.go.

5 years agofilesystem: rename getMountInfo() to loadMountInfo()
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: rename getMountInfo() to loadMountInfo()

Make it clearer that this function loads data into global data
structures, and doesn't return anything.

5 years agofilesystem: remove Mount.Options
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: remove Mount.Options

fscrypt doesn't currently do anything with the mount options, so remove
them from the Mount structure for now.

5 years agofilesystem: rename Mount.Filesystem to Mount.FilesystemType
Eric Biggers [Tue, 29 Oct 2019 07:04:39 +0000 (00:04 -0700)]
filesystem: rename Mount.Filesystem to Mount.FilesystemType

Make it clear that this refers to a type of filesystem such as "ext4",
rather than to a specific filesystem instance.

5 years agoManage tool versioning with Go modules (#161)
Joseph Richey [Sat, 26 Oct 2019 00:26:49 +0000 (17:26 -0700)]
Manage tool versioning with Go modules (#161)

See: https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module

The tool code is never actually built, but the versions are still lock
in `go.mod` and `go.sum`. We can also simplify the Makefile.

5 years agoDelete vendored code and update CI to Go 1.13 (#158)
Joseph Richey [Sat, 26 Oct 2019 00:15:27 +0000 (17:15 -0700)]
Delete vendored code and update CI to Go 1.13 (#158)

As the Go community transitions to using the modules ecosystem,
we want to only support one way of managing dependencies.
So this change moves to only using Go modules for dependency management.

This means that our effective minimum Go version increases to Go 1.11.
To account for this, we also update:
  - the documentation
  - Makefile
  - CI scripts

5 years agoAdded capacity to slice creation, when capacity is known (#159)
Vivek V [Fri, 25 Oct 2019 08:17:33 +0000 (13:47 +0530)]
Added capacity to slice creation, when capacity is known (#159)

Simple optimization to reduce memory allocations and copying when appending.

5 years agoactions/config: ensure config file is created with mode 0644 (#152)
ebiggers [Thu, 24 Oct 2019 05:18:45 +0000 (22:18 -0700)]
actions/config: ensure config file is created with mode 0644 (#152)

If the user has set a restrictive umask, e.g. 0077, then
/etc/fscrypt.conf would be created without the world-readable bit set.
Fix it by overriding the umask when creating the file.

Resolves https://github.com/google/fscrypt/issues/151

5 years agoMerge pull request #150 from ebiggers/allow-metadata-symlink
Joseph Richey [Thu, 24 Oct 2019 05:11:25 +0000 (22:11 -0700)]
Merge pull request #150 from ebiggers/allow-metadata-symlink

filesystem: allow .fscrypt to be a symlink

5 years agofilesystem: Move test-only code to test files
Joe Richey [Thu, 24 Oct 2019 05:06:13 +0000 (22:06 -0700)]
filesystem: Move test-only code to test files

This makes it easier to understand which code is actually invoked by the
command-line tool.

5 years agofilesystem: allow .fscrypt to be a symlink
Eric Biggers [Tue, 1 Oct 2019 16:43:36 +0000 (09:43 -0700)]
filesystem: allow .fscrypt to be a symlink

Support the case where the user has a read-only root filesystem (e.g.
with OSTree) and had previously created a symlink /.fscrypt pointing to
a writable location, so that login protectors can be created there.

Resolves https://github.com/google/fscrypt/issues/131

6 years agocmd/fscrypt: make 'fscrypt setup' create /.fscrypt (#149)
ebiggers [Tue, 24 Sep 2019 11:04:02 +0000 (04:04 -0700)]
cmd/fscrypt: make 'fscrypt setup' create /.fscrypt (#149)

Make the global setup command also create the metadata directory at
/.fscrypt, since that's where login protectors are placed, even when the
actual encrypted directories are on a different filesystem.

Resolves https://github.com/google/fscrypt/issues/129

6 years agoPrepare v0.2.5 (#147)
Joseph Richey [Thu, 12 Sep 2019 00:00:09 +0000 (17:00 -0700)]
Prepare v0.2.5 (#147)

Also add go version attrubute to go.mod

6 years agocmd/fscrypt: show encryption options with 'fscrypt status DIR' (#145)
ebiggers [Mon, 9 Sep 2019 21:10:11 +0000 (14:10 -0700)]
cmd/fscrypt: show encryption options with 'fscrypt status DIR' (#145)

Show the encryption options when running 'fscrypt status' on a
directory.  E.g.:

Policy:   490515286453d3f7
Options:  padding:32 contents:Adiantum filenames:Adiantum
Unlocked: Yes

6 years agowriteDataAtomic() fixes (#140)
ebiggers [Mon, 9 Sep 2019 20:41:32 +0000 (13:41 -0700)]
writeDataAtomic() fixes (#140)

* filesystem: ensure data is persisted before returning success

Sync the temporary file before renaming it, to ensure that after a
crash, the destination file isn't zero-length or otherwise incomplete.

Also sync the directory after the rename, to ensure the rename has been
persisted before returning success.

* filesystem: don't use fixed temporary file name

Using a fixed temporary file name in a world-writable sticky directory
is problematic since another user can create the file first.

Use ioutil.TempFile() to do it properly.  It uses O_EXCL under the hood
to ensure the file is newly created.

6 years agoFix various typos and grammatical errors (#141)
ebiggers [Mon, 9 Sep 2019 02:46:59 +0000 (19:46 -0700)]
Fix various typos and grammatical errors (#141)

These were found by a combination of manual review and a custom script
that checks for common errors.

Also removed an outdated sentence from the comment for setupBefore().

6 years ago.gitignore: add tags file (#144)
ebiggers [Mon, 9 Sep 2019 02:44:31 +0000 (19:44 -0700)]
.gitignore: add tags file (#144)

Add the tags file to .gitignore, for developers using
https://github.com/jstemmer/gotags.

6 years agoMerge pull request #142 from ebiggers/goimports
Joseph Richey [Thu, 29 Aug 2019 15:59:18 +0000 (08:59 -0700)]
Merge pull request #142 from ebiggers/goimports

Fix 'make format'-related CI failure

6 years agoRun 'make format' with latest version of goimports
Eric Biggers [Thu, 29 Aug 2019 04:22:36 +0000 (23:22 -0500)]
Run 'make format' with latest version of goimports

This fixes a CI failure, caused by goimports changing how it formats the
imports.

6 years agoMakefile: exclude generated files from 'make format'
Eric Biggers [Thu, 29 Aug 2019 04:22:36 +0000 (23:22 -0500)]
Makefile: exclude generated files from 'make format'

Due to a goimports update, 'make format' is now changing metadata.pb.go.
But this fix can't be committed because this file is generated by
'make gen'.

Fix this by not formatting generated files.

6 years agoREADME.md: improve documentation for moving files into encrypted dirs (#138)
ebiggers [Sun, 28 Jul 2019 02:24:19 +0000 (19:24 -0700)]
README.md: improve documentation for moving files into encrypted dirs (#138)

Resolves https://github.com/google/fscrypt/issues/124

6 years agoREADME.md: improve documentation for kernel and filesystem support (#137)
ebiggers [Sat, 27 Jul 2019 22:53:21 +0000 (15:53 -0700)]
README.md: improve documentation for kernel and filesystem support (#137)

Resolves https://github.com/google/fscrypt/issues/117
Resolves https://github.com/google/fscrypt/issues/127

6 years agoREADME.md: improve documentation of GRUB limitation (#136)
ebiggers [Sat, 27 Jul 2019 22:51:42 +0000 (15:51 -0700)]
README.md: improve documentation of GRUB limitation (#136)

Resolves https://github.com/google/fscrypt/issues/58

6 years agocmd/fscrypt: give newly encrypted directories mode 0700 (#134)
ebiggers [Sat, 27 Jul 2019 22:50:47 +0000 (15:50 -0700)]
cmd/fscrypt: give newly encrypted directories mode 0700 (#134)

Resolves https://github.com/google/fscrypt/issues/132

6 years agoUpdate Travis to use Ubuntu 18.04 and Go 1.12 (#135)
Joseph Richey [Sat, 27 Jul 2019 01:08:34 +0000 (18:08 -0700)]
Update Travis to use Ubuntu 18.04 and Go 1.12 (#135)

Fixes CI issues

6 years agoMerge pull request #122 from google/pam
Joseph Richey [Sun, 20 Jan 2019 06:06:54 +0000 (22:06 -0800)]
Merge pull request #122 from google/pam

Install pam modules/configs to the right location

6 years agoUpdate the README with correct install information
Joe Richey [Sun, 20 Jan 2019 06:04:12 +0000 (22:04 -0800)]
Update the README with correct install information

6 years agoInstall pam modules/configs to the right location
Joe Richey [Sun, 20 Jan 2019 03:27:30 +0000 (19:27 -0800)]
Install pam modules/configs to the right location

Per the FHS, manually installed programs should go under /usr/local.
This change also makes it easier to change the global installation
prefix. For example, package managers should set PREFIX=/usr

6 years agoMerge pull request #119 from ebiggers/adiantum
Joseph Richey [Fri, 18 Jan 2019 02:51:05 +0000 (18:51 -0800)]
Merge pull request #119 from ebiggers/adiantum

Add support for the Adiantum encryption mode

6 years agoMerge pull request #120 from google/tools
Joseph Richey [Fri, 18 Jan 2019 02:50:14 +0000 (18:50 -0800)]
Merge pull request #120 from google/tools

Makefile: migrate from megacheck to staticcheck

6 years agoMakefile: migrate from megacheck to staticcheck
Joe Richey joerichey@google.com [Fri, 18 Jan 2019 02:15:12 +0000 (18:15 -0800)]
Makefile: migrate from megacheck to staticcheck

This fixes travis issues as well as moving us off of deprecated tooling

6 years agoAdd support for the Adiantum encryption mode
Eric Biggers [Tue, 15 Jan 2019 02:43:25 +0000 (18:43 -0800)]
Add support for the Adiantum encryption mode

Add Adiantum support to the fscrypt userspace tool.  Supported in the
kernel since v5.0-rc1, Adiantum is a length-preserving encryption mode
based primarily on XChaCha12.  It is fast even on CPUs without AES
instructions.  Unlike XTS it is also a wide-block encryption mode.
Adiantum is supported for both contents and filenames encryption.

For Adiantum encryption policies, also make the fscrypt tool provide the
new DIRECT_KEY flag, which further improves performance by requesting
that all files be encrypted directly with the policy key.  This takes
advantage of Adiantum's support for long tweaks.

See the kernel commit "fscrypt: add Adiantum support"
(https://git.kernel.org/torvalds/c/8094c3ceb21ad938) for more details.

6 years agoMerge pull request #114 from ebiggers/fix-make-gen
Joseph Richey [Tue, 4 Dec 2018 23:10:54 +0000 (15:10 -0800)]
Merge pull request #114 from ebiggers/fix-make-gen

Makefile: use a specific protoc-gen-go version

6 years agoMakefile: use a specific protoc-gen-go version
Eric Biggers [Tue, 4 Dec 2018 22:31:20 +0000 (14:31 -0800)]
Makefile: use a specific protoc-gen-go version

'make gen' no longer works because it uses the git version of
protoc-gen-go, which is no longer compatible with the latest released
version of github.com/golang/protobuf/proto, which we're using.  Freeze
the protoc-gen-go version so that it keeps working.

6 years agoMerge pull request #110 from wjt/patch-1
Joseph Richey [Wed, 21 Nov 2018 02:26:01 +0000 (18:26 -0800)]
Merge pull request #110 from wjt/patch-1

README: fix "Debain" typo

6 years agoREADME: fix "Debain" typo
Will Thompson [Mon, 5 Nov 2018 11:54:08 +0000 (11:54 +0000)]
README: fix "Debain" typo

7 years agoMerge pull request #106 from fristonio/spell-check
Joseph Richey [Thu, 6 Sep 2018 05:49:42 +0000 (22:49 -0700)]
Merge pull request #106 from fristonio/spell-check

feat(spell-check): add make command for spell check.

7 years agofeat(spell-check): add make command for spell check.
Deepesh Pathak [Sat, 1 Sep 2018 19:47:27 +0000 (01:17 +0530)]
feat(spell-check): add make command for spell check.

* Remove spelling mistakes in the repository
* Add travis script to check for typos.
* Add command to Makefile to check for typos.
* Fixes #71

7 years agoMerge pull request #107 from google/mod
Joseph Richey [Thu, 30 Aug 2018 20:41:49 +0000 (13:41 -0700)]
Merge pull request #107 from google/mod

Use Go Modules and support Go 1.11 building

7 years agoUpdate Travis to use go 1.10 and go 1.11
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 20:33:52 +0000 (13:33 -0700)]
Update Travis to use go 1.10 and go 1.11

7 years agoBump Travis Version
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 11:14:57 +0000 (04:14 -0700)]
Bump Travis Version

7 years agoFix golint to not need a special fork
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 10:55:16 +0000 (03:55 -0700)]
Fix golint to not need a special fork

7 years agoGo 1.11 gofmt updatae
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 10:54:52 +0000 (03:54 -0700)]
Go 1.11 gofmt updatae

7 years agoUse proto.Equal instead of reflect.DeepEquals
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 10:54:08 +0000 (03:54 -0700)]
Use proto.Equal instead of reflect.DeepEquals

7 years agoUpdate Protoc Compiler to 3.6.1
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 09:32:37 +0000 (02:32 -0700)]
Update Protoc Compiler to 3.6.1

7 years agoUpdate dependancies to lastest version
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 09:24:33 +0000 (02:24 -0700)]
Update dependancies to lastest version

7 years agoDon't vendor helper tooling
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 09:19:59 +0000 (02:19 -0700)]
Don't vendor helper tooling

7 years agoRemove dep from documentation and travis build
Joe Richey joerichey@google.com [Thu, 30 Aug 2018 08:29:36 +0000 (01:29 -0700)]
Remove dep from documentation and travis build