Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide better error message when given a locked regular file #396

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cli-tests/t_lock.out
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,18 @@ If you want to force the directory to be locked, use:
contents
"MNT/dir" is now locked.
cat: MNT/dir/file: No such file or directory

# Try to operate on locked regular file
"MNT/dir" is now locked.
[ERROR] fscrypt status: cannot operate on locked regular file
"MNT/file"

It is not possible to operate directly on a locked regular file, since the
kernel does not support this. Specify the parent directory instead. (For loose
files, any directory with the file's policy works.)
[ERROR] fscrypt unlock: cannot operate on locked regular file
"MNT/file"

It is not possible to operate directly on a locked regular file, since the
kernel does not support this. Specify the parent directory instead. (For loose
files, any directory with the file's policy works.)
11 changes: 11 additions & 0 deletions cli-tests/t_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ _expect_failure "fscrypt lock '$dir'"
cat "$dir/file"
fscrypt lock --all-users "$dir"
_expect_failure "cat '$dir/file'"

_print_header "Try to operate on locked regular file"
_reset_filesystems
rm -rf "$dir"
mkdir "$dir"
echo hunter2 | fscrypt encrypt --quiet --name=prot "$dir"
echo contents > "$dir/file"
mv "$dir/file" "$MNT/file" # Make it a loose encrypted file.
fscrypt lock "$dir"
_expect_failure "fscrypt status '$MNT/file'"
_expect_failure "fscrypt unlock '$MNT/file'"
5 changes: 5 additions & 0 deletions cmd/fscrypt/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ func getErrorSuggestions(err error) string {
return `This is usually the result of a bad PAM configuration.
Either correct the problem in your PAM stack, enable
pam_keyinit.so, or run "keyctl link @u @s".`
case *metadata.ErrLockedRegularFile:
return `It is not possible to operate directly on a locked
regular file, since the kernel does not support this.
Specify the parent directory instead. (For loose files,
any directory with the file's policy works.)`
}
switch errors.Cause(err) {
case crypto.ErrMlockUlimit:
Expand Down
13 changes: 13 additions & 0 deletions metadata/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"os"
"os/user"
"strconv"
"syscall"
"unsafe"

"github.com/pkg/errors"
Expand Down Expand Up @@ -85,6 +86,15 @@ func (err *ErrDirectoryNotOwned) Error() string {
write access to the directory.`, err.Path, owner)
}

// ErrLockedRegularFile indicates that the path is a locked regular file.
type ErrLockedRegularFile struct {
Path string
}

func (err *ErrLockedRegularFile) Error() string {
return fmt.Sprintf("cannot operate on locked regular file %q", err.Path)
}

// ErrNotEncrypted indicates that the path is not encrypted.
type ErrNotEncrypted struct {
Path string
Expand Down Expand Up @@ -164,6 +174,9 @@ func buildV2PolicyData(policy *unix.FscryptPolicyV2) *PolicyData {
func GetPolicy(path string) (*PolicyData, error) {
file, err := os.Open(path)
if err != nil {
if err.(*os.PathError).Err == syscall.ENOKEY {
return nil, &ErrLockedRegularFile{path}
}
return nil, err
}
defer file.Close()
Expand Down
Loading