diff options
| author | Eric Biggers <ebiggers@google.com> | 2020-05-09 14:17:17 -0700 |
|---|---|---|
| committer | Eric Biggers <ebiggers@google.com> | 2020-05-09 15:16:13 -0700 |
| commit | de51add609bc74b7247ec4776bd694abbea24a45 (patch) | |
| tree | b5f304a4ecc101a5410bb2274d129dbc7dad6441 /cmd/fscrypt/commands.go | |
| parent | fb99b37a05696db4ceabb793e5f16727ec854ed1 (diff) | |
Try to detect incomplete locking of v1-encrypted directory
'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
Diffstat (limited to 'cmd/fscrypt/commands.go')
| -rw-r--r-- | cmd/fscrypt/commands.go | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/cmd/fscrypt/commands.go b/cmd/fscrypt/commands.go index ec75584..51cf136 100644 --- a/cmd/fscrypt/commands.go +++ b/cmd/fscrypt/commands.go @@ -496,30 +496,55 @@ func lockAction(c *cli.Context) error { if err = validateKeyringPrereqs(ctx, policy); err != nil { return newExitError(c, err) } - // Check if directory is already locked - if policy.IsFullyDeprovisioned() { - log.Printf("policy %s is already fully deprovisioned", policy.Descriptor()) - return newExitError(c, errors.Wrapf(ErrPolicyLocked, path)) - } - // Check for permission to drop caches, if it will be needed. + // Check for permission to drop caches, if it may be needed. if policy.NeedsUserKeyring() && dropCachesFlag.Value && !util.IsUserRoot() { return newExitError(c, ErrDropCachesPerm) } if err = policy.Deprovision(allUsersFlag.Value); err != nil { - return newExitError(c, err) + if err != keyring.ErrKeyNotPresent { + return newExitError(c, err) + } + // Key is no longer present. Normally that means the directory + // is already locked; in that case we exit with an error. But + // if the policy uses the user keyring (v1 policies only), then + // the directory might have been incompletely locked earlier, + // due to open files. Try to detect that case and finish + // locking the directory by dropping caches again. + if !policy.NeedsUserKeyring() || !isDirUnlockedHeuristic(path) { + log.Printf("policy %s is already fully deprovisioned", policy.Descriptor()) + return newExitError(c, errors.Wrapf(ErrPolicyLocked, path)) + } } if policy.NeedsUserKeyring() { if err = dropCachesIfRequested(c, ctx); err != nil { return newExitError(c, err) } + if isDirUnlockedHeuristic(path) { + return newExitError(c, keyring.ErrKeyFilesOpen) + } } fmt.Fprintf(c.App.Writer, "%q is now locked.\n", path) return nil } +// isDirUnlockedHeuristic returns true if we can create a subdirectory of the +// given directory and therefore it is definitely still unlocked. It returns +// false if the directory is probably locked (though it could also be unlocked). +// +// This is only useful if the directory's policy uses the user keyring, since +// otherwise the status can be easily found via the filesystem keyring. +func isDirUnlockedHeuristic(dirPath string) bool { + subdirPath := filepath.Join(dirPath, "fscrypt-is-dir-unlocked") + if err := os.Mkdir(subdirPath, 0700); err == nil { + os.Remove(subdirPath) + return true + } + return false +} + // Purge removes all the policy keys from the keyring (also need unmount). var Purge = cli.Command{ Name: "purge", |