2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
/*
|
|
|
|
Package setup handles setting up rejoinclient and recoveryserver for the disk-mapper.
|
|
|
|
|
|
|
|
On success of either of these services, the state disk is decrypted and the node is tainted as initialized by updating it's PCRs.
|
|
|
|
*/
|
2022-04-11 08:25:19 -04:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2022-09-08 08:45:27 -04:00
|
|
|
"context"
|
2022-04-11 08:25:19 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
2022-09-08 08:45:27 -04:00
|
|
|
"fmt"
|
2023-02-24 08:25:39 -05:00
|
|
|
"io/fs"
|
2022-04-11 08:25:19 -04:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-08-01 10:51:34 -04:00
|
|
|
"strconv"
|
2022-09-08 08:45:27 -04:00
|
|
|
"sync"
|
2022-04-11 08:25:19 -04:00
|
|
|
"syscall"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/internal/systemd"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
2023-03-08 08:13:57 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/initialize"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/nodestate"
|
2022-04-11 08:25:19 -04:00
|
|
|
"github.com/spf13/afero"
|
2023-02-24 08:25:39 -05:00
|
|
|
"go.uber.org/zap"
|
2022-04-11 08:25:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyPath = "/run/cryptsetup-keys.d"
|
|
|
|
keyFile = "state.key"
|
|
|
|
stateDiskMappedName = "state"
|
|
|
|
stateDiskMountPath = "/var/run/state"
|
2022-08-15 08:50:03 -04:00
|
|
|
cryptsetupOptions = "cipher=aes-xts-plain64,integrity=hmac-sha256"
|
2022-04-11 08:25:19 -04:00
|
|
|
stateInfoPath = stateDiskMountPath + "/constellation/node_state.json"
|
2023-06-09 04:35:52 -04:00
|
|
|
msrdonly = 0x1 // same as syscall.MS_RDONLY
|
2022-04-11 08:25:19 -04:00
|
|
|
)
|
|
|
|
|
2022-10-05 09:02:46 -04:00
|
|
|
// Manager handles formatting, mapping, mounting and unmounting of state disks.
|
|
|
|
type Manager struct {
|
2023-03-14 09:17:36 -04:00
|
|
|
log *logger.Logger
|
|
|
|
csp string
|
|
|
|
diskPath string
|
|
|
|
fs afero.Afero
|
|
|
|
mapper DeviceMapper
|
|
|
|
mounter Mounter
|
|
|
|
config ConfigurationGenerator
|
|
|
|
openDevice vtpm.TPMOpenFunc
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a SetupManager with the given parameters.
|
2022-09-08 08:45:27 -04:00
|
|
|
func New(log *logger.Logger, csp string, diskPath string, fs afero.Afero,
|
2023-03-14 09:17:36 -04:00
|
|
|
mapper DeviceMapper, mounter Mounter, openDevice vtpm.TPMOpenFunc,
|
2022-10-05 09:02:46 -04:00
|
|
|
) *Manager {
|
|
|
|
return &Manager{
|
2023-03-14 09:17:36 -04:00
|
|
|
log: log,
|
|
|
|
csp: csp,
|
|
|
|
diskPath: diskPath,
|
|
|
|
fs: fs,
|
|
|
|
mapper: mapper,
|
|
|
|
mounter: mounter,
|
|
|
|
config: systemd.New(fs),
|
|
|
|
openDevice: openDevice,
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareExistingDisk requests and waits for a decryption key to remap the encrypted state disk.
|
|
|
|
// Once the disk is mapped, the function taints the node as initialized by updating it's PCRs.
|
2022-10-05 09:02:46 -04:00
|
|
|
func (s *Manager) PrepareExistingDisk(recover RecoveryDoer) error {
|
2022-04-11 08:25:19 -04:00
|
|
|
uuid := s.mapper.DiskUUID()
|
2023-02-24 08:25:39 -05:00
|
|
|
s.log.With(zap.String("uuid", uuid)).Infof("Preparing existing state disk")
|
2022-08-01 10:51:34 -04:00
|
|
|
endpoint := net.JoinHostPort("0.0.0.0", strconv.Itoa(constants.RecoveryPort))
|
2022-09-08 08:45:27 -04:00
|
|
|
|
|
|
|
passphrase, measurementSecret, err := recover.Do(uuid, endpoint)
|
2022-04-11 08:25:19 -04:00
|
|
|
if err != nil {
|
2022-09-08 08:45:27 -04:00
|
|
|
return fmt.Errorf("failed to perform recovery: %w", err)
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.mapper.MapDisk(stateDiskMappedName, string(passphrase)); err != nil {
|
2022-09-08 08:45:27 -04:00
|
|
|
return err
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.mounter.MkdirAll(stateDiskMountPath, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-24 08:25:39 -05:00
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
// we do not care about cleaning up the mount point on error, since any errors returned here should cause a boot failure
|
2023-06-09 04:35:52 -04:00
|
|
|
if err := s.mounter.Mount(filepath.Join("/dev/mapper/", stateDiskMappedName), stateDiskMountPath, "ext4", msrdonly, ""); err != nil {
|
2022-04-11 08:25:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt, err := s.readMeasurementSalt(stateInfoPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-24 08:25:39 -05:00
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
clusterID, err := attestation.DeriveClusterID(measurementSecret, measurementSalt)
|
2022-04-11 08:25:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// taint the node as initialized
|
2023-03-14 09:17:36 -04:00
|
|
|
if err := initialize.MarkNodeAsBootstrapped(s.openDevice, clusterID); err != nil {
|
2022-04-11 08:25:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-15 08:50:03 -04:00
|
|
|
if err := s.saveConfiguration(passphrase); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
return s.mounter.Unmount(stateDiskMountPath, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareNewDisk prepares an instances state disk by formatting the disk as a LUKS device using a random passphrase.
|
2022-10-05 09:02:46 -04:00
|
|
|
func (s *Manager) PrepareNewDisk() error {
|
2023-02-24 08:25:39 -05:00
|
|
|
s.log.With(zap.String("uuid", s.mapper.DiskUUID())).Infof("Preparing new state disk")
|
2022-04-11 08:25:19 -04:00
|
|
|
|
|
|
|
// generate and save temporary passphrase
|
2022-07-26 04:58:39 -04:00
|
|
|
passphrase := make([]byte, crypto.RNGLengthDefault)
|
2022-04-11 08:25:19 -04:00
|
|
|
if _, err := rand.Read(passphrase); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-15 08:50:03 -04:00
|
|
|
if err := s.saveConfiguration(passphrase); err != nil {
|
2022-04-11 08:25:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.mapper.FormatDisk(string(passphrase)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.mapper.MapDisk(stateDiskMappedName, string(passphrase))
|
|
|
|
}
|
|
|
|
|
2022-10-05 09:02:46 -04:00
|
|
|
func (s *Manager) readMeasurementSalt(path string) ([]byte, error) {
|
2022-04-11 08:25:19 -04:00
|
|
|
handler := file.NewHandler(s.fs)
|
|
|
|
var state nodestate.NodeState
|
|
|
|
if err := handler.ReadJSON(path, &state); err != nil {
|
2022-07-26 04:58:39 -04:00
|
|
|
return nil, err
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
if len(state.MeasurementSalt) != crypto.RNGLengthDefault {
|
|
|
|
return nil, errors.New("missing state information to retaint node")
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
return state.MeasurementSalt, nil
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
2022-08-15 08:50:03 -04:00
|
|
|
|
|
|
|
// saveConfiguration saves the given passphrase and cryptsetup mapping configuration to disk.
|
2022-10-05 09:02:46 -04:00
|
|
|
func (s *Manager) saveConfiguration(passphrase []byte) error {
|
2022-08-15 08:50:03 -04:00
|
|
|
// passphrase
|
|
|
|
if err := s.fs.MkdirAll(keyPath, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.fs.WriteFile(filepath.Join(keyPath, keyFile), passphrase, 0o400); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// systemd cryptsetup unit
|
|
|
|
return s.config.Generate(stateDiskMappedName, s.diskPath, filepath.Join(keyPath, keyFile), cryptsetupOptions)
|
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
|
2023-02-24 08:25:39 -05:00
|
|
|
// LogDevices logs all available block devices and partitions (lsblk like).
|
|
|
|
func (s *Manager) LogDevices() error {
|
|
|
|
var devices []fs.FileInfo
|
|
|
|
dirs, err := os.ReadDir("/sys/class/block")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, file := range dirs {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fileInfo, err := file.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
devices = append(devices, fileInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Infof("List of all available block devices and partitions:")
|
|
|
|
for _, device := range devices {
|
|
|
|
var stat syscall.Statfs_t
|
|
|
|
dev := "/dev/" + device.Name()
|
|
|
|
if err := syscall.Statfs(dev, &stat); err != nil {
|
|
|
|
s.log.With(zap.Error(err)).Errorf("failed to statfs %s", dev)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the raw size, in bytes
|
|
|
|
size := stat.Blocks * uint64(stat.Bsize)
|
|
|
|
free := stat.Bfree * uint64(stat.Bsize)
|
|
|
|
avail := stat.Bavail * uint64(stat.Bsize)
|
|
|
|
|
|
|
|
s.log.Infof(
|
|
|
|
"Name: %-15s, Size: %-10d, Mode: %s, ModTime: %s, Size = %-10d, Free = %-10d, Available = %-10d\n",
|
|
|
|
dev,
|
|
|
|
device.Size(),
|
|
|
|
device.Mode(),
|
|
|
|
device.ModTime(),
|
|
|
|
size,
|
|
|
|
free,
|
|
|
|
avail)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// RecoveryServer interface serves a recovery server.
|
2022-09-14 07:25:42 -04:00
|
|
|
type RecoveryServer interface {
|
2022-09-08 08:45:27 -04:00
|
|
|
Serve(context.Context, net.Listener, string) (key, secret []byte, err error)
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// RejoinClient interface starts a rejoin client.
|
2022-09-14 07:25:42 -04:00
|
|
|
type RejoinClient interface {
|
2022-09-08 08:45:27 -04:00
|
|
|
Start(context.Context, string) (key, secret []byte)
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// NodeRecoverer bundles a RecoveryServer and RejoinClient.
|
2022-10-05 09:02:46 -04:00
|
|
|
type NodeRecoverer struct {
|
2022-09-14 07:25:42 -04:00
|
|
|
recoveryServer RecoveryServer
|
|
|
|
rejoinClient RejoinClient
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeRecoverer initializes a new nodeRecoverer.
|
2022-10-05 09:02:46 -04:00
|
|
|
func NewNodeRecoverer(recoveryServer RecoveryServer, rejoinClient RejoinClient) *NodeRecoverer {
|
|
|
|
return &NodeRecoverer{
|
2022-09-08 08:45:27 -04:00
|
|
|
recoveryServer: recoveryServer,
|
|
|
|
rejoinClient: rejoinClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do performs a recovery procedure on the given state disk.
|
|
|
|
// The method starts a gRPC server to allow manual recovery by a user.
|
|
|
|
// At the same time it tries to request a decryption key from all available Constellation control-plane nodes.
|
2022-10-05 09:02:46 -04:00
|
|
|
func (r *NodeRecoverer) Do(uuid, endpoint string) (passphrase, measurementSecret []byte, err error) {
|
2022-09-08 08:45:27 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-02-24 08:25:39 -05:00
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
lis, err := net.Listen("tcp", endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer lis.Close()
|
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
key, secret, serveErr := r.recoveryServer.Serve(ctx, lis, uuid)
|
|
|
|
once.Do(func() {
|
|
|
|
cancel()
|
|
|
|
passphrase = key
|
|
|
|
measurementSecret = secret
|
|
|
|
})
|
|
|
|
if serveErr != nil && !errors.Is(serveErr, context.Canceled) {
|
|
|
|
err = serveErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
key, secret := r.rejoinClient.Start(ctx, uuid)
|
|
|
|
once.Do(func() {
|
|
|
|
cancel()
|
|
|
|
passphrase = key
|
|
|
|
measurementSecret = secret
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return passphrase, measurementSecret, err
|
|
|
|
}
|