| 1 |
/* |
| 2 |
* keyring.go - Add/remove encryption policy keys to/from kernel |
| 3 |
* |
| 4 |
* Copyright 2019 Google LLC |
| 5 |
* Author: Eric Biggers ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
// Package keyring manages adding, removing, and getting the status of |
| 21 |
// encryption policy keys to/from the kernel. Most public functions are in |
| 22 |
// keyring.go, and they delegate to either user_keyring.go or fs_keyring.go, |
| 23 |
// depending on whether a user keyring or a filesystem keyring is being used. |
| 24 |
// |
| 25 |
// v2 encryption policies always use the filesystem keyring. |
| 26 |
// v1 policies use the user keyring by default, but can be configured to use the |
| 27 |
// filesystem keyring instead (requires root and kernel v5.4+). |
| 28 |
package keyring |
| 29 |
|
| 30 |
import ( |
| 31 |
"encoding/hex" |
| 32 |
"os/user" |
| 33 |
"strconv" |
| 34 |
|
| 35 |
"github.com/pkg/errors" |
| 36 |
"golang.org/x/sys/unix" |
| 37 |
|
| 38 |
"github.com/google/fscrypt/crypto" |
| 39 |
"github.com/google/fscrypt/filesystem" |
| 40 |
"github.com/google/fscrypt/metadata" |
| 41 |
"github.com/google/fscrypt/util" |
| 42 |
) |
| 43 |
|
| 44 |
// Keyring error values |
| 45 |
var ( |
| 46 |
ErrKeyAddedByOtherUsers = errors.New("other users have added the key too") |
| 47 |
ErrKeyFilesOpen = errors.New("some files using the key are still open") |
| 48 |
ErrKeyNotPresent = errors.New("key not present or already removed") |
| 49 |
ErrV2PoliciesUnsupported = errors.New("kernel is too old to support v2 encryption policies") |
| 50 |
) |
| 51 |
|
| 52 |
// Options are the options which specify *which* keyring the key should be |
| 53 |
// added/removed/gotten to, and how. |
| 54 |
type Options struct { |
| 55 |
// Mount is the filesystem to which the key should be |
| 56 |
// added/removed/gotten. |
| 57 |
Mount *filesystem.Mount |
| 58 |
// User is the user for whom the key should be added/removed/gotten. |
| 59 |
User *user.User |
| 60 |
// UseFsKeyringForV1Policies is true if keys for v1 encryption policies |
| 61 |
// should be put in the filesystem's keyring (if supported) rather than |
| 62 |
// in the user's keyring. Note that this makes AddEncryptionKey and |
| 63 |
// RemoveEncryptionKey require root privileges. |
| 64 |
UseFsKeyringForV1Policies bool |
| 65 |
} |
| 66 |
|
| 67 |
func shouldUseFsKeyring(descriptor string, options *Options) (bool, error) { |
| 68 |
// For v1 encryption policy keys, use the filesystem keyring if |
| 69 |
// use_fs_keyring_for_v1_policies is set in /etc/fscrypt.conf and the |
| 70 |
// kernel supports it. |
| 71 |
if len(descriptor) == hex.EncodedLen(unix.FSCRYPT_KEY_DESCRIPTOR_SIZE) { |
| 72 |
return options.UseFsKeyringForV1Policies && IsFsKeyringSupported(options.Mount), nil |
| 73 |
} |
| 74 |
// For v2 encryption policy keys, always use the filesystem keyring; the |
| 75 |
// kernel doesn't support any other way. |
| 76 |
if !IsFsKeyringSupported(options.Mount) { |
| 77 |
return true, ErrV2PoliciesUnsupported |
| 78 |
} |
| 79 |
return true, nil |
| 80 |
} |
| 81 |
|
| 82 |
// buildKeyDescription builds the description for an fscrypt key of type |
| 83 |
// "logon". For ext4 and f2fs, it uses the legacy filesystem-specific prefixes |
| 84 |
// for compatibility with kernels before v4.8 and v4.6 respectively. For other |
| 85 |
// filesystems it uses the generic prefix "fscrypt". |
| 86 |
func buildKeyDescription(options *Options, descriptor string) string { |
| 87 |
switch options.Mount.FilesystemType { |
| 88 |
case "ext4", "f2fs": |
| 89 |
return options.Mount.FilesystemType + ":" + descriptor |
| 90 |
default: |
| 91 |
return unix.FSCRYPT_KEY_DESC_PREFIX + descriptor |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// AddEncryptionKey adds an encryption policy key to a kernel keyring. It uses |
| 96 |
// either the filesystem keyring for the target Mount or the user keyring for |
| 97 |
// the target User. |
| 98 |
func AddEncryptionKey(key *crypto.Key, descriptor string, options *Options) error { |
| 99 |
if err := util.CheckValidLength(metadata.PolicyKeyLen, key.Len()); err != nil { |
| 100 |
return errors.Wrap(err, "policy key") |
| 101 |
} |
| 102 |
useFsKeyring, err := shouldUseFsKeyring(descriptor, options) |
| 103 |
if err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
if useFsKeyring { |
| 107 |
return fsAddEncryptionKey(key, descriptor, options.Mount, options.User) |
| 108 |
} |
| 109 |
return userAddKey(key, buildKeyDescription(options, descriptor), options.User) |
| 110 |
} |
| 111 |
|
| 112 |
// RemoveEncryptionKey removes an encryption policy key from a kernel keyring. |
| 113 |
// It uses either the filesystem keyring for the target Mount or the user |
| 114 |
// keyring for the target User. |
| 115 |
func RemoveEncryptionKey(descriptor string, options *Options, allUsers bool) error { |
| 116 |
useFsKeyring, err := shouldUseFsKeyring(descriptor, options) |
| 117 |
if err != nil { |
| 118 |
return err |
| 119 |
} |
| 120 |
if useFsKeyring { |
| 121 |
user := options.User |
| 122 |
if allUsers { |
| 123 |
user = nil |
| 124 |
} |
| 125 |
return fsRemoveEncryptionKey(descriptor, options.Mount, user) |
| 126 |
} |
| 127 |
return userRemoveKey(buildKeyDescription(options, descriptor), options.User) |
| 128 |
} |
| 129 |
|
| 130 |
// KeyStatus is an enum that represents the status of a key in a kernel keyring. |
| 131 |
type KeyStatus int |
| 132 |
|
| 133 |
// The possible values of KeyStatus. |
| 134 |
const ( |
| 135 |
KeyStatusUnknown = 0 + iota |
| 136 |
KeyAbsent |
| 137 |
KeyAbsentButFilesBusy |
| 138 |
KeyPresent |
| 139 |
KeyPresentButOnlyOtherUsers |
| 140 |
) |
| 141 |
|
| 142 |
func (status KeyStatus) String() string { |
| 143 |
switch status { |
| 144 |
case KeyStatusUnknown: |
| 145 |
return "Unknown" |
| 146 |
case KeyAbsent: |
| 147 |
return "Absent" |
| 148 |
case KeyAbsentButFilesBusy: |
| 149 |
return "AbsentButFilesBusy" |
| 150 |
case KeyPresent: |
| 151 |
return "Present" |
| 152 |
case KeyPresentButOnlyOtherUsers: |
| 153 |
return "PresentButOnlyOtherUsers" |
| 154 |
default: |
| 155 |
return strconv.Itoa(int(status)) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// GetEncryptionKeyStatus gets the status of an encryption policy key in a |
| 160 |
// kernel keyring. It uses either the filesystem keyring for the target Mount |
| 161 |
// or the user keyring for the target User. |
| 162 |
func GetEncryptionKeyStatus(descriptor string, options *Options) (KeyStatus, error) { |
| 163 |
useFsKeyring, err := shouldUseFsKeyring(descriptor, options) |
| 164 |
if err != nil { |
| 165 |
return KeyStatusUnknown, err |
| 166 |
} |
| 167 |
if useFsKeyring { |
| 168 |
return fsGetEncryptionKeyStatus(descriptor, options.Mount, options.User) |
| 169 |
} |
| 170 |
_, _, err = userFindKey(buildKeyDescription(options, descriptor), options.User) |
| 171 |
if err != nil { |
| 172 |
return KeyAbsent, nil |
| 173 |
} |
| 174 |
return KeyPresent, nil |
| 175 |
} |