1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// -*- Mode: Go; indent-tabs-mode: t -*-
//go:build !nosecboot
/*
* Copyright (C) 2024 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package secboot
import (
"fmt"
"strings"
"golang.org/x/sys/unix"
sb "github.com/snapcore/secboot"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/secboot/keys"
)
type bootstrappedContainer struct {
tempContainerKeySlot string
devicePath string
key DiskUnlockKey
finished bool
}
func newLUKS2KeyDataWriterImpl(devicePath string, name string) (KeyDataWriter, error) {
return sb.NewLUKS2KeyDataWriter(devicePath, name)
}
var (
newLUKS2KeyDataWriter = newLUKS2KeyDataWriterImpl
unixAddKey = unix.AddKey
)
func slotNameOrDefault(slotName string) string {
if slotName == "" {
return "default"
}
return slotName
}
func (bc *bootstrappedContainer) AddKey(slotName string, newKey []byte) error {
if bc.finished {
return fmt.Errorf("internal error: bootstrapped container was a already finished")
}
if err := sbAddLUKS2ContainerUnlockKey(bc.devicePath, slotNameOrDefault(slotName), sb.DiskUnlockKey(bc.key), sb.DiskUnlockKey(newKey)); err != nil {
return err
}
return nil
}
func (bc *bootstrappedContainer) AddRecoveryKey(slotName string, rkey keys.RecoveryKey) error {
if bc.finished {
return fmt.Errorf("internal error: bootstrapped container was a already finished")
}
if slotName == "" {
slotName = "default-recovery"
}
if err := sbAddLUKS2ContainerRecoveryKey(bc.devicePath, slotName, sb.DiskUnlockKey(bc.key), sb.RecoveryKey(rkey)); err != nil {
return err
}
return nil
}
func (bc *bootstrappedContainer) GetTokenWriter(slotName string) (KeyDataWriter, error) {
return newLUKS2KeyDataWriter(bc.devicePath, slotNameOrDefault(slotName))
}
func (bc *bootstrappedContainer) RemoveBootstrapKey() error {
if bc.finished {
return nil
}
bc.finished = true
if err := sbDeleteLUKS2ContainerKey(bc.devicePath, bc.tempContainerKeySlot); err != nil {
return fmt.Errorf("cannot remove bootstrap key: %v", err)
}
return nil
}
func (bc *bootstrappedContainer) RegisterKeyAsUsed(primaryKey []byte, unlockKey []byte) {
// secboot unlocking does not fail when it cannot save keys to the kerying. So
// we also want to have a similar behavior and just print warnings in this function.
devlinks, err := disksDevlinks(bc.devicePath)
if err != nil {
logger.Noticef("warning: cannot find symlinks for %s: %v", bc.devicePath, err)
return
}
var uuidDevlink string
for _, devlink := range devlinks {
if strings.HasPrefix(devlink, "/dev/disk/by-uuid/") {
uuidDevlink = devlink
break
}
}
if uuidDevlink == "" {
logger.Noticef("warning: missing by-uuid symlink for %s", bc.devicePath)
return
}
logger.Debugf("registering kerying keys for %s (%s)", bc.devicePath, uuidDevlink)
// Format of key for secboot is <prefix>:<path>:<purpose>.
// See internal/keyring/keyring.go in secboot.
// "purpose" is either "aux" or "unlock".
// See crypt.go in secboot.
if _, err := unixAddKey("user", fmt.Sprintf("%s:%s:unlock", defaultKeyringPrefix, uuidDevlink), unlockKey, unix.KEY_SPEC_USER_KEYRING); err != nil {
logger.Noticef("warning: cannot register unlock key for %s: %v", uuidDevlink, err)
}
if _, err := unixAddKey("user", fmt.Sprintf("%s:%s:aux", defaultKeyringPrefix, uuidDevlink), primaryKey, unix.KEY_SPEC_USER_KEYRING); err != nil {
logger.Noticef("warning: cannot register primary key for %s: %v", uuidDevlink, err)
}
}
func createBootstrappedContainerImpl(key DiskUnlockKey, devicePath string) BootstrappedContainer {
return &bootstrappedContainer{
tempContainerKeySlot: "bootstrap-key",
devicePath: devicePath,
key: key,
finished: false,
}
}
func init() {
CreateBootstrappedContainer = createBootstrappedContainerImpl
}
func MockCreateBootstrappedContainer(f func(key DiskUnlockKey, devicePath string) BootstrappedContainer) func() {
osutil.MustBeTestBinary("MockCreateBootstrappedContainer can be only called from tests")
old := CreateBootstrappedContainer
CreateBootstrappedContainer = f
return func() {
CreateBootstrappedContainer = old
}
}
|