mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
vendor: vendor fscrypt integration dependencies
Signed-off-by: Marcel Lauhoff <marcel.lauhoff@suse.com>
This commit is contained in:
committed by
mergify[bot]
parent
cfea8d7562
commit
f8faffac89
326
vendor/github.com/google/fscrypt/keyring/fs_keyring.go
generated
vendored
Normal file
326
vendor/github.com/google/fscrypt/keyring/fs_keyring.go
generated
vendored
Normal file
@ -0,0 +1,326 @@
|
||||
/*
|
||||
* fs_keyring.go - Add/remove encryption policy keys to/from filesystem
|
||||
*
|
||||
* Copyright 2019 Google LLC
|
||||
* Author: Eric Biggers (ebiggers@google.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package keyring
|
||||
|
||||
/*
|
||||
#include <string.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"os"
|
||||
"os/user"
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/google/fscrypt/crypto"
|
||||
"github.com/google/fscrypt/filesystem"
|
||||
"github.com/google/fscrypt/security"
|
||||
"github.com/google/fscrypt/util"
|
||||
)
|
||||
|
||||
var (
|
||||
fsKeyringSupported bool
|
||||
fsKeyringSupportedKnown bool
|
||||
fsKeyringSupportedLock sync.Mutex
|
||||
)
|
||||
|
||||
func checkForFsKeyringSupport(mount *filesystem.Mount) bool {
|
||||
dir, err := os.Open(mount.Path)
|
||||
if err != nil {
|
||||
log.Printf("Unexpected error opening %q. Assuming filesystem keyring is unsupported.",
|
||||
mount.Path)
|
||||
return false
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
// FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY
|
||||
// if the ioctl isn't supported. Otherwise it should fail with EFAULT.
|
||||
//
|
||||
// Note that there's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY
|
||||
// support separately, since it's guaranteed to be available if
|
||||
// FS_IOC_ADD_ENCRYPTION_KEY is. There's also no need to check for
|
||||
// support on every filesystem separately, since either the kernel
|
||||
// supports the ioctls on all fscrypt-capable filesystems or it doesn't.
|
||||
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(), unix.FS_IOC_ADD_ENCRYPTION_KEY, 0)
|
||||
if errno == unix.ENOTTY {
|
||||
log.Printf("Kernel doesn't support filesystem keyring. Falling back to user keyring.")
|
||||
return false
|
||||
}
|
||||
if errno == unix.EFAULT {
|
||||
log.Printf("Detected support for filesystem keyring")
|
||||
} else {
|
||||
// EFAULT is expected, but as long as we didn't get ENOTTY the
|
||||
// ioctl should be available.
|
||||
log.Printf("Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY(%q, NULL): %v", mount.Path, errno)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsFsKeyringSupported returns true if the kernel supports the ioctls to
|
||||
// add/remove fscrypt keys directly to/from the filesystem. For support to be
|
||||
// detected, the given Mount must be for a filesystem that supports fscrypt.
|
||||
func IsFsKeyringSupported(mount *filesystem.Mount) bool {
|
||||
fsKeyringSupportedLock.Lock()
|
||||
defer fsKeyringSupportedLock.Unlock()
|
||||
if !fsKeyringSupportedKnown {
|
||||
fsKeyringSupported = checkForFsKeyringSupport(mount)
|
||||
fsKeyringSupportedKnown = true
|
||||
}
|
||||
return fsKeyringSupported
|
||||
}
|
||||
|
||||
// buildKeySpecifier converts the key descriptor string to an FscryptKeySpecifier.
|
||||
func buildKeySpecifier(spec *unix.FscryptKeySpecifier, descriptor string) error {
|
||||
descriptorBytes, err := hex.DecodeString(descriptor)
|
||||
if err != nil {
|
||||
return errors.Errorf("key descriptor %q is invalid", descriptor)
|
||||
}
|
||||
switch len(descriptorBytes) {
|
||||
case unix.FSCRYPT_KEY_DESCRIPTOR_SIZE:
|
||||
spec.Type = unix.FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR
|
||||
case unix.FSCRYPT_KEY_IDENTIFIER_SIZE:
|
||||
spec.Type = unix.FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
|
||||
default:
|
||||
return errors.Errorf("key descriptor %q has unknown length", descriptor)
|
||||
}
|
||||
copy(spec.U[:], descriptorBytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
type savedPrivs struct {
|
||||
ruid, euid, suid int
|
||||
}
|
||||
|
||||
// dropPrivsIfNeeded drops privileges (UIDs only) to the given user if we're
|
||||
// working with a v2 policy key, and if the user is different from the user the
|
||||
// process is currently running as.
|
||||
//
|
||||
// This is needed to change the effective UID so that FS_IOC_ADD_ENCRYPTION_KEY
|
||||
// and FS_IOC_REMOVE_ENCRYPTION_KEY will add/remove a claim to the key for the
|
||||
// intended user, and so that FS_IOC_GET_ENCRYPTION_KEY_STATUS will return the
|
||||
// correct status flags for the user.
|
||||
func dropPrivsIfNeeded(user *user.User, spec *unix.FscryptKeySpecifier) (*savedPrivs, error) {
|
||||
if spec.Type == unix.FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR {
|
||||
// v1 policy keys don't have any concept of user claims.
|
||||
return nil, nil
|
||||
}
|
||||
targetUID := util.AtoiOrPanic(user.Uid)
|
||||
ruid, euid, suid := security.GetUids()
|
||||
if euid == targetUID {
|
||||
return nil, nil
|
||||
}
|
||||
if err := security.SetUids(targetUID, targetUID, euid); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &savedPrivs{ruid, euid, suid}, nil
|
||||
}
|
||||
|
||||
// restorePrivs restores root privileges if needed.
|
||||
func restorePrivs(privs *savedPrivs) error {
|
||||
if privs != nil {
|
||||
return security.SetUids(privs.ruid, privs.euid, privs.suid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateKeyDescriptor validates that the correct key descriptor was provided.
|
||||
// This isn't really necessary; this is just an extra sanity check.
|
||||
func validateKeyDescriptor(spec *unix.FscryptKeySpecifier, descriptor string) (string, error) {
|
||||
if spec.Type != unix.FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER {
|
||||
// v1 policy key: the descriptor is chosen arbitrarily by
|
||||
// userspace, so there's nothing to validate.
|
||||
return descriptor, nil
|
||||
}
|
||||
// v2 policy key. The descriptor ("identifier" in the kernel UAPI) is
|
||||
// calculated as a cryptographic hash of the key itself. The kernel
|
||||
// ignores the provided value, and calculates and returns it itself. So
|
||||
// verify that the returned value is as expected. If it's not, the key
|
||||
// doesn't actually match the encryption policy we thought it was for.
|
||||
actual := hex.EncodeToString(spec.U[:unix.FSCRYPT_KEY_IDENTIFIER_SIZE])
|
||||
if descriptor == actual {
|
||||
return descriptor, nil
|
||||
}
|
||||
return actual,
|
||||
errors.Errorf("provided and actual key descriptors differ (%q != %q)",
|
||||
descriptor, actual)
|
||||
}
|
||||
|
||||
// fsAddEncryptionKey adds the specified encryption key to the specified filesystem.
|
||||
func fsAddEncryptionKey(key *crypto.Key, descriptor string,
|
||||
mount *filesystem.Mount, user *user.User) error {
|
||||
|
||||
dir, err := os.Open(mount.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
argKey, err := crypto.NewBlankKey(int(unsafe.Sizeof(unix.FscryptAddKeyArg{})) + key.Len())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer argKey.Wipe()
|
||||
arg := (*unix.FscryptAddKeyArg)(argKey.UnsafePtr())
|
||||
|
||||
if err = buildKeySpecifier(&arg.Key_spec, descriptor); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
raw := unsafe.Pointer(uintptr(argKey.UnsafePtr()) + unsafe.Sizeof(*arg))
|
||||
arg.Raw_size = uint32(key.Len())
|
||||
C.memcpy(raw, key.UnsafePtr(), C.size_t(key.Len()))
|
||||
|
||||
savedPrivs, err := dropPrivsIfNeeded(user, &arg.Key_spec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(),
|
||||
unix.FS_IOC_ADD_ENCRYPTION_KEY, uintptr(argKey.UnsafePtr()))
|
||||
restorePrivs(savedPrivs)
|
||||
|
||||
log.Printf("FS_IOC_ADD_ENCRYPTION_KEY(%q, %s, <raw>) = %v", mount.Path, descriptor, errno)
|
||||
if errno != 0 {
|
||||
return errors.Wrapf(errno,
|
||||
"error adding key with descriptor %s to filesystem %s",
|
||||
descriptor, mount.Path)
|
||||
}
|
||||
if descriptor, err = validateKeyDescriptor(&arg.Key_spec, descriptor); err != nil {
|
||||
fsRemoveEncryptionKey(descriptor, mount, user)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fsRemoveEncryptionKey removes the specified encryption key from the specified
|
||||
// filesystem.
|
||||
func fsRemoveEncryptionKey(descriptor string, mount *filesystem.Mount,
|
||||
user *user.User) error {
|
||||
|
||||
dir, err := os.Open(mount.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
var arg unix.FscryptRemoveKeyArg
|
||||
if err = buildKeySpecifier(&arg.Key_spec, descriptor); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ioc := uintptr(unix.FS_IOC_REMOVE_ENCRYPTION_KEY)
|
||||
iocName := "FS_IOC_REMOVE_ENCRYPTION_KEY"
|
||||
var savedPrivs *savedPrivs
|
||||
if user == nil {
|
||||
ioc = unix.FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
|
||||
iocName = "FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS"
|
||||
} else {
|
||||
savedPrivs, err = dropPrivsIfNeeded(user, &arg.Key_spec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(), ioc, uintptr(unsafe.Pointer(&arg)))
|
||||
restorePrivs(savedPrivs)
|
||||
|
||||
log.Printf("%s(%q, %s) = %v, removal_status_flags=0x%x",
|
||||
iocName, mount.Path, descriptor, errno, arg.Removal_status_flags)
|
||||
switch errno {
|
||||
case 0:
|
||||
switch {
|
||||
case arg.Removal_status_flags&unix.FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS != 0:
|
||||
return ErrKeyAddedByOtherUsers
|
||||
case arg.Removal_status_flags&unix.FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY != 0:
|
||||
return ErrKeyFilesOpen
|
||||
}
|
||||
return nil
|
||||
case unix.ENOKEY:
|
||||
// ENOKEY means either the key is completely missing or that the
|
||||
// current user doesn't have a claim to it. Distinguish between
|
||||
// these two cases by getting the key status.
|
||||
if user != nil {
|
||||
status, _ := fsGetEncryptionKeyStatus(descriptor, mount, user)
|
||||
if status == KeyPresentButOnlyOtherUsers {
|
||||
return ErrKeyAddedByOtherUsers
|
||||
}
|
||||
}
|
||||
return ErrKeyNotPresent
|
||||
default:
|
||||
return errors.Wrapf(errno,
|
||||
"error removing key with descriptor %s from filesystem %s",
|
||||
descriptor, mount.Path)
|
||||
}
|
||||
}
|
||||
|
||||
// fsGetEncryptionKeyStatus gets the status of the specified encryption key on
|
||||
// the specified filesystem.
|
||||
func fsGetEncryptionKeyStatus(descriptor string, mount *filesystem.Mount,
|
||||
user *user.User) (KeyStatus, error) {
|
||||
|
||||
dir, err := os.Open(mount.Path)
|
||||
if err != nil {
|
||||
return KeyStatusUnknown, err
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
var arg unix.FscryptGetKeyStatusArg
|
||||
err = buildKeySpecifier(&arg.Key_spec, descriptor)
|
||||
if err != nil {
|
||||
return KeyStatusUnknown, err
|
||||
}
|
||||
|
||||
savedPrivs, err := dropPrivsIfNeeded(user, &arg.Key_spec)
|
||||
if err != nil {
|
||||
return KeyStatusUnknown, err
|
||||
}
|
||||
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(),
|
||||
unix.FS_IOC_GET_ENCRYPTION_KEY_STATUS, uintptr(unsafe.Pointer(&arg)))
|
||||
restorePrivs(savedPrivs)
|
||||
|
||||
log.Printf("FS_IOC_GET_ENCRYPTION_KEY_STATUS(%q, %s) = %v, status=%d, status_flags=0x%x",
|
||||
mount.Path, descriptor, errno, arg.Status, arg.Status_flags)
|
||||
if errno != 0 {
|
||||
return KeyStatusUnknown,
|
||||
errors.Wrapf(errno,
|
||||
"error getting status of key with descriptor %s on filesystem %s",
|
||||
descriptor, mount.Path)
|
||||
}
|
||||
switch arg.Status {
|
||||
case unix.FSCRYPT_KEY_STATUS_ABSENT:
|
||||
return KeyAbsent, nil
|
||||
case unix.FSCRYPT_KEY_STATUS_PRESENT:
|
||||
if arg.Key_spec.Type != unix.FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
|
||||
(arg.Status_flags&unix.FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF) == 0 {
|
||||
return KeyPresentButOnlyOtherUsers, nil
|
||||
}
|
||||
return KeyPresent, nil
|
||||
case unix.FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED:
|
||||
return KeyAbsentButFilesBusy, nil
|
||||
default:
|
||||
return KeyStatusUnknown,
|
||||
errors.Errorf("unknown key status (%d) for key with descriptor %s on filesystem %s",
|
||||
arg.Status, descriptor, mount.Path)
|
||||
}
|
||||
}
|
175
vendor/github.com/google/fscrypt/keyring/keyring.go
generated
vendored
Normal file
175
vendor/github.com/google/fscrypt/keyring/keyring.go
generated
vendored
Normal file
@ -0,0 +1,175 @@
|
||||
/*
|
||||
* keyring.go - Add/remove encryption policy keys to/from kernel
|
||||
*
|
||||
* Copyright 2019 Google LLC
|
||||
* Author: Eric Biggers (ebiggers@google.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
// Package keyring manages adding, removing, and getting the status of
|
||||
// encryption policy keys to/from the kernel. Most public functions are in
|
||||
// keyring.go, and they delegate to either user_keyring.go or fs_keyring.go,
|
||||
// depending on whether a user keyring or a filesystem keyring is being used.
|
||||
//
|
||||
// v2 encryption policies always use the filesystem keyring.
|
||||
// v1 policies use the user keyring by default, but can be configured to use the
|
||||
// filesystem keyring instead (requires root and kernel v5.4+).
|
||||
package keyring
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"os/user"
|
||||
"strconv"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/google/fscrypt/crypto"
|
||||
"github.com/google/fscrypt/filesystem"
|
||||
"github.com/google/fscrypt/metadata"
|
||||
"github.com/google/fscrypt/util"
|
||||
)
|
||||
|
||||
// Keyring error values
|
||||
var (
|
||||
ErrKeyAddedByOtherUsers = errors.New("other users have added the key too")
|
||||
ErrKeyFilesOpen = errors.New("some files using the key are still open")
|
||||
ErrKeyNotPresent = errors.New("key not present or already removed")
|
||||
ErrV2PoliciesUnsupported = errors.New("kernel is too old to support v2 encryption policies")
|
||||
)
|
||||
|
||||
// Options are the options which specify *which* keyring the key should be
|
||||
// added/removed/gotten to, and how.
|
||||
type Options struct {
|
||||
// Mount is the filesystem to which the key should be
|
||||
// added/removed/gotten.
|
||||
Mount *filesystem.Mount
|
||||
// User is the user for whom the key should be added/removed/gotten.
|
||||
User *user.User
|
||||
// UseFsKeyringForV1Policies is true if keys for v1 encryption policies
|
||||
// should be put in the filesystem's keyring (if supported) rather than
|
||||
// in the user's keyring. Note that this makes AddEncryptionKey and
|
||||
// RemoveEncryptionKey require root privileges.
|
||||
UseFsKeyringForV1Policies bool
|
||||
}
|
||||
|
||||
func shouldUseFsKeyring(descriptor string, options *Options) (bool, error) {
|
||||
// For v1 encryption policy keys, use the filesystem keyring if
|
||||
// use_fs_keyring_for_v1_policies is set in /etc/fscrypt.conf and the
|
||||
// kernel supports it.
|
||||
if len(descriptor) == hex.EncodedLen(unix.FSCRYPT_KEY_DESCRIPTOR_SIZE) {
|
||||
return options.UseFsKeyringForV1Policies && IsFsKeyringSupported(options.Mount), nil
|
||||
}
|
||||
// For v2 encryption policy keys, always use the filesystem keyring; the
|
||||
// kernel doesn't support any other way.
|
||||
if !IsFsKeyringSupported(options.Mount) {
|
||||
return true, ErrV2PoliciesUnsupported
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// buildKeyDescription builds the description for an fscrypt key of type
|
||||
// "logon". For ext4 and f2fs, it uses the legacy filesystem-specific prefixes
|
||||
// for compatibility with kernels before v4.8 and v4.6 respectively. For other
|
||||
// filesystems it uses the generic prefix "fscrypt".
|
||||
func buildKeyDescription(options *Options, descriptor string) string {
|
||||
switch options.Mount.FilesystemType {
|
||||
case "ext4", "f2fs":
|
||||
return options.Mount.FilesystemType + ":" + descriptor
|
||||
default:
|
||||
return unix.FSCRYPT_KEY_DESC_PREFIX + descriptor
|
||||
}
|
||||
}
|
||||
|
||||
// AddEncryptionKey adds an encryption policy key to a kernel keyring. It uses
|
||||
// either the filesystem keyring for the target Mount or the user keyring for
|
||||
// the target User.
|
||||
func AddEncryptionKey(key *crypto.Key, descriptor string, options *Options) error {
|
||||
if err := util.CheckValidLength(metadata.PolicyKeyLen, key.Len()); err != nil {
|
||||
return errors.Wrap(err, "policy key")
|
||||
}
|
||||
useFsKeyring, err := shouldUseFsKeyring(descriptor, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if useFsKeyring {
|
||||
return fsAddEncryptionKey(key, descriptor, options.Mount, options.User)
|
||||
}
|
||||
return userAddKey(key, buildKeyDescription(options, descriptor), options.User)
|
||||
}
|
||||
|
||||
// RemoveEncryptionKey removes an encryption policy key from a kernel keyring.
|
||||
// It uses either the filesystem keyring for the target Mount or the user
|
||||
// keyring for the target User.
|
||||
func RemoveEncryptionKey(descriptor string, options *Options, allUsers bool) error {
|
||||
useFsKeyring, err := shouldUseFsKeyring(descriptor, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if useFsKeyring {
|
||||
user := options.User
|
||||
if allUsers {
|
||||
user = nil
|
||||
}
|
||||
return fsRemoveEncryptionKey(descriptor, options.Mount, user)
|
||||
}
|
||||
return userRemoveKey(buildKeyDescription(options, descriptor), options.User)
|
||||
}
|
||||
|
||||
// KeyStatus is an enum that represents the status of a key in a kernel keyring.
|
||||
type KeyStatus int
|
||||
|
||||
// The possible values of KeyStatus.
|
||||
const (
|
||||
KeyStatusUnknown = 0 + iota
|
||||
KeyAbsent
|
||||
KeyAbsentButFilesBusy
|
||||
KeyPresent
|
||||
KeyPresentButOnlyOtherUsers
|
||||
)
|
||||
|
||||
func (status KeyStatus) String() string {
|
||||
switch status {
|
||||
case KeyStatusUnknown:
|
||||
return "Unknown"
|
||||
case KeyAbsent:
|
||||
return "Absent"
|
||||
case KeyAbsentButFilesBusy:
|
||||
return "AbsentButFilesBusy"
|
||||
case KeyPresent:
|
||||
return "Present"
|
||||
case KeyPresentButOnlyOtherUsers:
|
||||
return "PresentButOnlyOtherUsers"
|
||||
default:
|
||||
return strconv.Itoa(int(status))
|
||||
}
|
||||
}
|
||||
|
||||
// GetEncryptionKeyStatus gets the status of an encryption policy key in a
|
||||
// kernel keyring. It uses either the filesystem keyring for the target Mount
|
||||
// or the user keyring for the target User.
|
||||
func GetEncryptionKeyStatus(descriptor string, options *Options) (KeyStatus, error) {
|
||||
useFsKeyring, err := shouldUseFsKeyring(descriptor, options)
|
||||
if err != nil {
|
||||
return KeyStatusUnknown, err
|
||||
}
|
||||
if useFsKeyring {
|
||||
return fsGetEncryptionKeyStatus(descriptor, options.Mount, options.User)
|
||||
}
|
||||
_, _, err = userFindKey(buildKeyDescription(options, descriptor), options.User)
|
||||
if err != nil {
|
||||
return KeyAbsent, nil
|
||||
}
|
||||
return KeyPresent, nil
|
||||
}
|
251
vendor/github.com/google/fscrypt/keyring/user_keyring.go
generated
vendored
Normal file
251
vendor/github.com/google/fscrypt/keyring/user_keyring.go
generated
vendored
Normal file
@ -0,0 +1,251 @@
|
||||
/*
|
||||
* user_keyring.go - Add/remove encryption policy keys to/from user keyrings.
|
||||
* This is the deprecated mechanism; see fs_keyring.go for the new mechanism.
|
||||
*
|
||||
* Copyright 2017 Google Inc.
|
||||
* Author: Joe Richey (joerichey@google.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package keyring
|
||||
|
||||
import (
|
||||
"os/user"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/google/fscrypt/crypto"
|
||||
"github.com/google/fscrypt/security"
|
||||
"github.com/google/fscrypt/util"
|
||||
)
|
||||
|
||||
// ErrAccessUserKeyring indicates that a user's keyring cannot be
|
||||
// accessed.
|
||||
type ErrAccessUserKeyring struct {
|
||||
TargetUser *user.User
|
||||
UnderlyingError error
|
||||
}
|
||||
|
||||
func (err *ErrAccessUserKeyring) Error() string {
|
||||
return fmt.Sprintf("could not access user keyring for %q: %s",
|
||||
err.TargetUser.Username, err.UnderlyingError)
|
||||
}
|
||||
|
||||
// ErrSessionUserKeyring indicates that a user's keyring is not linked
|
||||
// into the session keyring.
|
||||
type ErrSessionUserKeyring struct {
|
||||
TargetUser *user.User
|
||||
}
|
||||
|
||||
func (err *ErrSessionUserKeyring) Error() string {
|
||||
return fmt.Sprintf("user keyring for %q is not linked into the session keyring",
|
||||
err.TargetUser.Username)
|
||||
}
|
||||
|
||||
// KeyType is always logon as required by filesystem encryption.
|
||||
const KeyType = "logon"
|
||||
|
||||
// userAddKey puts the provided policy key into the user keyring for the
|
||||
// specified user with the provided description, and type logon.
|
||||
func userAddKey(key *crypto.Key, description string, targetUser *user.User) error {
|
||||
runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
// Create our payload (containing an FscryptKey)
|
||||
payload, err := crypto.NewBlankKey(int(unsafe.Sizeof(unix.FscryptKey{})))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer payload.Wipe()
|
||||
|
||||
// Cast the payload to an FscryptKey so we can initialize the fields.
|
||||
fscryptKey := (*unix.FscryptKey)(payload.UnsafePtr())
|
||||
// Mode is ignored by the kernel
|
||||
fscryptKey.Mode = 0
|
||||
fscryptKey.Size = uint32(key.Len())
|
||||
copy(fscryptKey.Raw[:], key.Data())
|
||||
|
||||
keyringID, err := UserKeyringID(targetUser, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
keyID, err := unix.AddKey(KeyType, description, payload.Data(), keyringID)
|
||||
log.Printf("KeyctlAddKey(%s, %s, <data>, %d) = %d, %v",
|
||||
KeyType, description, keyringID, keyID, err)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"error adding key with description %s to user keyring for %q",
|
||||
description, targetUser.Username)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// userRemoveKey tries to remove a policy key from the user keyring with the
|
||||
// provided description. An error is returned if the key does not exist.
|
||||
func userRemoveKey(description string, targetUser *user.User) error {
|
||||
runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
keyID, keyringID, err := userFindKey(description, targetUser)
|
||||
if err != nil {
|
||||
return ErrKeyNotPresent
|
||||
}
|
||||
|
||||
_, err = unix.KeyctlInt(unix.KEYCTL_UNLINK, keyID, keyringID, 0, 0)
|
||||
log.Printf("KeyctlUnlink(%d, %d) = %v", keyID, keyringID, err)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"error removing key with description %s from user keyring for %q",
|
||||
description, targetUser.Username)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// userFindKey tries to locate a key with the provided description in the user
|
||||
// keyring for the target user. The key ID and keyring ID are returned if we can
|
||||
// find the key. An error is returned if the key does not exist.
|
||||
func userFindKey(description string, targetUser *user.User) (int, int, error) {
|
||||
runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
keyringID, err := UserKeyringID(targetUser, false)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
keyID, err := unix.KeyctlSearch(keyringID, KeyType, description, 0)
|
||||
log.Printf("KeyctlSearch(%d, %s, %s) = %d, %v", keyringID, KeyType, description, keyID, err)
|
||||
if err != nil {
|
||||
return 0, 0, errors.Wrapf(err,
|
||||
"error searching for key %s in user keyring for %q",
|
||||
description, targetUser.Username)
|
||||
}
|
||||
return keyID, keyringID, err
|
||||
}
|
||||
|
||||
// UserKeyringID returns the key id of the target user's user keyring. We also
|
||||
// ensure that the keyring will be accessible by linking it into the thread
|
||||
// keyring and linking it into the root user keyring (permissions allowing). If
|
||||
// checkSession is true, an error is returned if a normal user requests their
|
||||
// user keyring, but it is not in the current session keyring.
|
||||
func UserKeyringID(targetUser *user.User, checkSession bool) (int, error) {
|
||||
runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
uid := util.AtoiOrPanic(targetUser.Uid)
|
||||
targetKeyring, err := userKeyringIDLookup(uid)
|
||||
if err != nil {
|
||||
return 0, &ErrAccessUserKeyring{targetUser, err}
|
||||
}
|
||||
|
||||
if !util.IsUserRoot() {
|
||||
// Make sure the returned keyring will be accessible by checking
|
||||
// that it is in the session keyring.
|
||||
if checkSession && !isUserKeyringInSession(uid) {
|
||||
return 0, &ErrSessionUserKeyring{targetUser}
|
||||
}
|
||||
return targetKeyring, nil
|
||||
}
|
||||
|
||||
// Make sure the returned keyring will be accessible by linking it into
|
||||
// the root user's user keyring (which will not be garbage collected).
|
||||
rootKeyring, err := userKeyringIDLookup(0)
|
||||
if err != nil {
|
||||
return 0, errors.Wrapf(err, "error looking up root's user keyring")
|
||||
}
|
||||
|
||||
if rootKeyring != targetKeyring {
|
||||
if err = keyringLink(targetKeyring, rootKeyring); err != nil {
|
||||
return 0, errors.Wrapf(err,
|
||||
"error linking user keyring for %q into root's user keyring",
|
||||
targetUser.Username)
|
||||
}
|
||||
}
|
||||
return targetKeyring, nil
|
||||
}
|
||||
|
||||
func userKeyringIDLookup(uid int) (keyringID int, err error) {
|
||||
|
||||
// Our goals here are to:
|
||||
// - Find the user keyring (for the provided uid)
|
||||
// - Link it into the current thread keyring (so we can use it)
|
||||
// - Make no permanent changes to the process privileges
|
||||
// Complicating this are the facts that:
|
||||
// - The value of KEY_SPEC_USER_KEYRING is determined by the ruid
|
||||
// - Keyring linking permissions use the euid
|
||||
// So we have to change both the ruid and euid to make this work,
|
||||
// setting the suid to 0 so that we can later switch back.
|
||||
ruid, euid, suid := security.GetUids()
|
||||
if ruid != uid || euid != uid {
|
||||
if err = security.SetUids(uid, uid, 0); err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
resetErr := security.SetUids(ruid, euid, suid)
|
||||
if resetErr != nil {
|
||||
err = resetErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// We get the value of KEY_SPEC_USER_KEYRING. Note that this will also
|
||||
// trigger the creation of the uid keyring if it does not yet exist.
|
||||
keyringID, err = unix.KeyctlGetKeyringID(unix.KEY_SPEC_USER_KEYRING, true)
|
||||
log.Printf("keyringID(_uid.%d) = %d, %v", uid, keyringID, err)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// We still want to use this keyring after our privileges are reset. So
|
||||
// we link it into the thread keyring, preventing a loss of access.
|
||||
//
|
||||
// We must be under LockOSThread() for this to work reliably. Note that
|
||||
// we can't just use the process keyring, since it doesn't work reliably
|
||||
// in Go programs, due to the Go runtime creating threads before the
|
||||
// program starts and has a chance to create the process keyring.
|
||||
if err = keyringLink(keyringID, unix.KEY_SPEC_THREAD_KEYRING); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return keyringID, nil
|
||||
}
|
||||
|
||||
// isUserKeyringInSession tells us if the user's uid keyring is in the current
|
||||
// session keyring.
|
||||
func isUserKeyringInSession(uid int) bool {
|
||||
// We cannot use unix.KEY_SPEC_SESSION_KEYRING directly as that might
|
||||
// create a session keyring if one does not exist.
|
||||
sessionKeyring, err := unix.KeyctlGetKeyringID(unix.KEY_SPEC_SESSION_KEYRING, false)
|
||||
log.Printf("keyringID(session) = %d, %v", sessionKeyring, err)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
description := fmt.Sprintf("_uid.%d", uid)
|
||||
id, err := unix.KeyctlSearch(sessionKeyring, "keyring", description, 0)
|
||||
log.Printf("KeyctlSearch(%d, keyring, %s) = %d, %v", sessionKeyring, description, id, err)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func keyringLink(keyID int, keyringID int) error {
|
||||
_, err := unix.KeyctlInt(unix.KEYCTL_LINK, keyID, keyringID, 0, 0)
|
||||
log.Printf("KeyctlLink(%d, %d) = %v", keyID, keyringID, err)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user