| 1 |
/* |
| 2 |
* fs_keyring.go - Add/remove encryption policy keys to/from filesystem |
| 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 |
| 21 |
|
| 22 |
/* |
| 23 |
#include <string.h> |
| 24 |
*/ |
| 25 |
import "C" |
| 26 |
|
| 27 |
import ( |
| 28 |
"encoding/hex" |
| 29 |
"log" |
| 30 |
"os" |
| 31 |
"os/user" |
| 32 |
"sync" |
| 33 |
"unsafe" |
| 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/security" |
| 41 |
"github.com/google/fscrypt/util" |
| 42 |
) |
| 43 |
|
| 44 |
var ( |
| 45 |
fsKeyringSupported bool |
| 46 |
fsKeyringSupportedKnown bool |
| 47 |
fsKeyringSupportedLock sync.Mutex |
| 48 |
) |
| 49 |
|
| 50 |
func checkForFsKeyringSupport(mount *filesystem.Mount) bool { |
| 51 |
dir, err := os.Open(mount.Path) |
| 52 |
if err != nil { |
| 53 |
log.Printf("Unexpected error opening %q. Assuming filesystem keyring is unsupported.", |
| 54 |
mount.Path) |
| 55 |
return false |
| 56 |
} |
| 57 |
defer dir.Close() |
| 58 |
|
| 59 |
// FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY |
| 60 |
// if the ioctl isn't supported. Otherwise it should fail with EFAULT. |
| 61 |
// |
| 62 |
// Note that there's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY |
| 63 |
// support separately, since it's guaranteed to be available if |
| 64 |
// FS_IOC_ADD_ENCRYPTION_KEY is. There's also no need to check for |
| 65 |
// support on every filesystem separately, since either the kernel |
| 66 |
// supports the ioctls on all fscrypt-capable filesystems or it doesn't. |
| 67 |
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(), unix.FS_IOC_ADD_ENCRYPTION_KEY, 0) |
| 68 |
if errno == unix.ENOTTY { |
| 69 |
log.Printf("Kernel doesn't support filesystem keyring. Falling back to user keyring.") |
| 70 |
return false |
| 71 |
} |
| 72 |
if errno == unix.EFAULT { |
| 73 |
log.Printf("Detected support for filesystem keyring") |
| 74 |
} else { |
| 75 |
// EFAULT is expected, but as long as we didn't get ENOTTY the |
| 76 |
// ioctl should be available. |
| 77 |
log.Printf("Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY(%q, NULL): %v", mount.Path, errno) |
| 78 |
} |
| 79 |
return true |
| 80 |
} |
| 81 |
|
| 82 |
// IsFsKeyringSupported returns true if the kernel supports the ioctls to |
| 83 |
// add/remove fscrypt keys directly to/from the filesystem. For support to be |
| 84 |
// detected, the given Mount must be for a filesystem that supports fscrypt. |
| 85 |
func IsFsKeyringSupported(mount *filesystem.Mount) bool { |
| 86 |
fsKeyringSupportedLock.Lock() |
| 87 |
defer fsKeyringSupportedLock.Unlock() |
| 88 |
if !fsKeyringSupportedKnown { |
| 89 |
fsKeyringSupported = checkForFsKeyringSupport(mount) |
| 90 |
fsKeyringSupportedKnown = true |
| 91 |
} |
| 92 |
return fsKeyringSupported |
| 93 |
} |
| 94 |
|
| 95 |
// buildKeySpecifier converts the key descriptor string to an FscryptKeySpecifier. |
| 96 |
func buildKeySpecifier(spec *unix.FscryptKeySpecifier, descriptor string) error { |
| 97 |
descriptorBytes, err := hex.DecodeString(descriptor) |
| 98 |
if err != nil { |
| 99 |
return errors.Errorf("key descriptor %q is invalid", descriptor) |
| 100 |
} |
| 101 |
switch len(descriptorBytes) { |
| 102 |
case unix.FSCRYPT_KEY_DESCRIPTOR_SIZE: |
| 103 |
spec.Type = unix.FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR |
| 104 |
case unix.FSCRYPT_KEY_IDENTIFIER_SIZE: |
| 105 |
spec.Type = unix.FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER |
| 106 |
default: |
| 107 |
return errors.Errorf("key descriptor %q has unknown length", descriptor) |
| 108 |
} |
| 109 |
copy(spec.U[:], descriptorBytes) |
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
type savedPrivs struct { |
| 114 |
ruid, euid, suid int |
| 115 |
} |
| 116 |
|
| 117 |
// dropPrivsIfNeeded drops privileges (UIDs only) to the given user if we're |
| 118 |
// working with a v2 policy key, and if the user is different from the user the |
| 119 |
// process is currently running as. |
| 120 |
// |
| 121 |
// This is needed to change the effective UID so that FS_IOC_ADD_ENCRYPTION_KEY |
| 122 |
// and FS_IOC_REMOVE_ENCRYPTION_KEY will add/remove a claim to the key for the |
| 123 |
// intended user, and so that FS_IOC_GET_ENCRYPTION_KEY_STATUS will return the |
| 124 |
// correct status flags for the user. |
| 125 |
func dropPrivsIfNeeded(user *user.User, spec *unix.FscryptKeySpecifier) (*savedPrivs, error) { |
| 126 |
if spec.Type == unix.FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR { |
| 127 |
// v1 policy keys don't have any concept of user claims. |
| 128 |
return nil, nil |
| 129 |
} |
| 130 |
targetUID := util.AtoiOrPanic(user.Uid) |
| 131 |
ruid, euid, suid := security.GetUids() |
| 132 |
if euid == targetUID { |
| 133 |
return nil, nil |
| 134 |
} |
| 135 |
if err := security.SetUids(targetUID, targetUID, euid); err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
return &savedPrivs{ruid, euid, suid}, nil |
| 139 |
} |
| 140 |
|
| 141 |
// restorePrivs restores root privileges if needed. |
| 142 |
func restorePrivs(privs *savedPrivs) error { |
| 143 |
if privs != nil { |
| 144 |
return security.SetUids(privs.ruid, privs.euid, privs.suid) |
| 145 |
} |
| 146 |
return nil |
| 147 |
} |
| 148 |
|
| 149 |
// validateKeyDescriptor validates that the correct key descriptor was provided. |
| 150 |
// This isn't really necessary; this is just an extra sanity check. |
| 151 |
func validateKeyDescriptor(spec *unix.FscryptKeySpecifier, descriptor string) (string, error) { |
| 152 |
if spec.Type != unix.FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER { |
| 153 |
// v1 policy key: the descriptor is chosen arbitrarily by |
| 154 |
// userspace, so there's nothing to validate. |
| 155 |
return descriptor, nil |
| 156 |
} |
| 157 |
// v2 policy key. The descriptor ("identifier" in the kernel UAPI) is |
| 158 |
// calculated as a cryptographic hash of the key itself. The kernel |
| 159 |
// ignores the provided value, and calculates and returns it itself. So |
| 160 |
// verify that the returned value is as expected. If it's not, the key |
| 161 |
// doesn't actually match the encryption policy we thought it was for. |
| 162 |
actual := hex.EncodeToString(spec.U[:unix.FSCRYPT_KEY_IDENTIFIER_SIZE]) |
| 163 |
if descriptor == actual { |
| 164 |
return descriptor, nil |
| 165 |
} |
| 166 |
return actual, |
| 167 |
errors.Errorf("provided and actual key descriptors differ (%q != %q)", |
| 168 |
descriptor, actual) |
| 169 |
} |
| 170 |
|
| 171 |
// fsAddEncryptionKey adds the specified encryption key to the specified filesystem. |
| 172 |
func fsAddEncryptionKey(key *crypto.Key, descriptor string, |
| 173 |
mount *filesystem.Mount, user *user.User) error { |
| 174 |
|
| 175 |
dir, err := os.Open(mount.Path) |
| 176 |
if err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
defer dir.Close() |
| 180 |
|
| 181 |
argKey, err := crypto.NewBlankKey(int(unsafe.Sizeof(unix.FscryptAddKeyArg{})) + key.Len()) |
| 182 |
if err != nil { |
| 183 |
return err |
| 184 |
} |
| 185 |
defer argKey.Wipe() |
| 186 |
arg := (*unix.FscryptAddKeyArg)(argKey.UnsafePtr()) |
| 187 |
|
| 188 |
if err = buildKeySpecifier(&arg.Key_spec, descriptor); err != nil { |
| 189 |
return err |
| 190 |
} |
| 191 |
|
| 192 |
raw := unsafe.Pointer(uintptr(argKey.UnsafePtr()) + unsafe.Sizeof(*arg)) |
| 193 |
arg.Raw_size = uint32(key.Len()) |
| 194 |
C.memcpy(raw, key.UnsafePtr(), C.size_t(key.Len())) |
| 195 |
|
| 196 |
savedPrivs, err := dropPrivsIfNeeded(user, &arg.Key_spec) |
| 197 |
if err != nil { |
| 198 |
return err |
| 199 |
} |
| 200 |
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(), |
| 201 |
unix.FS_IOC_ADD_ENCRYPTION_KEY, uintptr(argKey.UnsafePtr())) |
| 202 |
restorePrivs(savedPrivs) |
| 203 |
|
| 204 |
log.Printf("FS_IOC_ADD_ENCRYPTION_KEY(%q, %s, <raw>) = %v", mount.Path, descriptor, errno) |
| 205 |
if errno != 0 { |
| 206 |
return errors.Wrapf(errno, |
| 207 |
"error adding key with descriptor %s to filesystem %s", |
| 208 |
descriptor, mount.Path) |
| 209 |
} |
| 210 |
if descriptor, err = validateKeyDescriptor(&arg.Key_spec, descriptor); err != nil { |
| 211 |
fsRemoveEncryptionKey(descriptor, mount, user) |
| 212 |
return err |
| 213 |
} |
| 214 |
return nil |
| 215 |
} |
| 216 |
|
| 217 |
// fsRemoveEncryptionKey removes the specified encryption key from the specified |
| 218 |
// filesystem. |
| 219 |
func fsRemoveEncryptionKey(descriptor string, mount *filesystem.Mount, |
| 220 |
user *user.User) error { |
| 221 |
|
| 222 |
dir, err := os.Open(mount.Path) |
| 223 |
if err != nil { |
| 224 |
return err |
| 225 |
} |
| 226 |
defer dir.Close() |
| 227 |
|
| 228 |
var arg unix.FscryptRemoveKeyArg |
| 229 |
if err = buildKeySpecifier(&arg.Key_spec, descriptor); err != nil { |
| 230 |
return err |
| 231 |
} |
| 232 |
|
| 233 |
ioc := uintptr(unix.FS_IOC_REMOVE_ENCRYPTION_KEY) |
| 234 |
iocName := "FS_IOC_REMOVE_ENCRYPTION_KEY" |
| 235 |
var savedPrivs *savedPrivs |
| 236 |
if user == nil { |
| 237 |
ioc = unix.FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS |
| 238 |
iocName = "FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS" |
| 239 |
} else { |
| 240 |
savedPrivs, err = dropPrivsIfNeeded(user, &arg.Key_spec) |
| 241 |
if err != nil { |
| 242 |
return err |
| 243 |
} |
| 244 |
} |
| 245 |
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(), ioc, uintptr(unsafe.Pointer(&arg))) |
| 246 |
restorePrivs(savedPrivs) |
| 247 |
|
| 248 |
log.Printf("%s(%q, %s) = %v, removal_status_flags=0x%x", |
| 249 |
iocName, mount.Path, descriptor, errno, arg.Removal_status_flags) |
| 250 |
switch errno { |
| 251 |
case 0: |
| 252 |
switch { |
| 253 |
case arg.Removal_status_flags&unix.FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS != 0: |
| 254 |
return ErrKeyAddedByOtherUsers |
| 255 |
case arg.Removal_status_flags&unix.FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY != 0: |
| 256 |
return ErrKeyFilesOpen |
| 257 |
} |
| 258 |
return nil |
| 259 |
case unix.ENOKEY: |
| 260 |
// ENOKEY means either the key is completely missing or that the |
| 261 |
// current user doesn't have a claim to it. Distinguish between |
| 262 |
// these two cases by getting the key status. |
| 263 |
if user != nil { |
| 264 |
status, _ := fsGetEncryptionKeyStatus(descriptor, mount, user) |
| 265 |
if status == KeyPresentButOnlyOtherUsers { |
| 266 |
return ErrKeyAddedByOtherUsers |
| 267 |
} |
| 268 |
} |
| 269 |
return ErrKeyNotPresent |
| 270 |
default: |
| 271 |
return errors.Wrapf(errno, |
| 272 |
"error removing key with descriptor %s from filesystem %s", |
| 273 |
descriptor, mount.Path) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// fsGetEncryptionKeyStatus gets the status of the specified encryption key on |
| 278 |
// the specified filesystem. |
| 279 |
func fsGetEncryptionKeyStatus(descriptor string, mount *filesystem.Mount, |
| 280 |
user *user.User) (KeyStatus, error) { |
| 281 |
|
| 282 |
dir, err := os.Open(mount.Path) |
| 283 |
if err != nil { |
| 284 |
return KeyStatusUnknown, err |
| 285 |
} |
| 286 |
defer dir.Close() |
| 287 |
|
| 288 |
var arg unix.FscryptGetKeyStatusArg |
| 289 |
err = buildKeySpecifier(&arg.Key_spec, descriptor) |
| 290 |
if err != nil { |
| 291 |
return KeyStatusUnknown, err |
| 292 |
} |
| 293 |
|
| 294 |
savedPrivs, err := dropPrivsIfNeeded(user, &arg.Key_spec) |
| 295 |
if err != nil { |
| 296 |
return KeyStatusUnknown, err |
| 297 |
} |
| 298 |
_, _, errno := unix.Syscall(unix.SYS_IOCTL, dir.Fd(), |
| 299 |
unix.FS_IOC_GET_ENCRYPTION_KEY_STATUS, uintptr(unsafe.Pointer(&arg))) |
| 300 |
restorePrivs(savedPrivs) |
| 301 |
|
| 302 |
log.Printf("FS_IOC_GET_ENCRYPTION_KEY_STATUS(%q, %s) = %v, status=%d, status_flags=0x%x", |
| 303 |
mount.Path, descriptor, errno, arg.Status, arg.Status_flags) |
| 304 |
if errno != 0 { |
| 305 |
return KeyStatusUnknown, |
| 306 |
errors.Wrapf(errno, |
| 307 |
"error getting status of key with descriptor %s on filesystem %s", |
| 308 |
descriptor, mount.Path) |
| 309 |
} |
| 310 |
switch arg.Status { |
| 311 |
case unix.FSCRYPT_KEY_STATUS_ABSENT: |
| 312 |
return KeyAbsent, nil |
| 313 |
case unix.FSCRYPT_KEY_STATUS_PRESENT: |
| 314 |
if arg.Key_spec.Type != unix.FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR && |
| 315 |
(arg.Status_flags&unix.FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF) == 0 { |
| 316 |
return KeyPresentButOnlyOtherUsers, nil |
| 317 |
} |
| 318 |
return KeyPresent, nil |
| 319 |
case unix.FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED: |
| 320 |
return KeyAbsentButFilesBusy, nil |
| 321 |
default: |
| 322 |
return KeyStatusUnknown, |
| 323 |
errors.Errorf("unknown key status (%d) for key with descriptor %s on filesystem %s", |
| 324 |
arg.Status, descriptor, mount.Path) |
| 325 |
} |
| 326 |
} |