Lorenz Brun | f95909d | 2019-09-11 19:48:26 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package storage |
| 18 | |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 19 | import ( |
| 20 | "fmt" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "os/exec" |
| 24 | "path/filepath" |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 25 | "sync" |
| 26 | |
| 27 | "go.uber.org/zap" |
| 28 | "golang.org/x/sys/unix" |
Hendrik Hofstadt | 8efe51e | 2020-02-28 12:53:41 +0100 | [diff] [blame] | 29 | |
| 30 | "git.monogon.dev/source/nexantic.git/core/pkg/tpm" |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | const ( |
| 34 | dataMountPath = "/data" |
| 35 | espMountPath = "/esp" |
| 36 | espDataPath = espMountPath + "/EFI/smalltown" |
| 37 | etcdSealedKeyLocation = espDataPath + "/data-key.bin" |
| 38 | ) |
| 39 | |
| 40 | type Manager struct { |
| 41 | logger *zap.Logger |
| 42 | dataReady bool |
| 43 | initializationError error |
| 44 | mutex sync.RWMutex |
| 45 | } |
| 46 | |
| 47 | func Initialize(logger *zap.Logger) (*Manager, error) { |
| 48 | if err := FindPartitions(); err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | |
| 52 | if err := os.Mkdir("/esp", 0755); err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | // We're mounting ESP sync for reliability, this lowers our chances of getting half-written files |
| 57 | if err := unix.Mount(ESPDevicePath, espMountPath, "vfat", unix.MS_NOEXEC|unix.MS_NODEV|unix.MS_SYNC, ""); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | |
| 61 | manager := &Manager{ |
| 62 | logger: logger, |
| 63 | dataReady: false, |
| 64 | } |
| 65 | |
| 66 | manager.mutex.Lock() |
| 67 | defer manager.mutex.Unlock() |
| 68 | |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 69 | return manager, nil |
| 70 | } |
| 71 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 72 | var keySize uint16 = 256 / 8 |
| 73 | |
| 74 | // MountData mounts the Smalltown data partition with the given global unlock key. It automatically |
| 75 | // unseals the local unlock key from the TPM. |
| 76 | func (s *Manager) MountData(globalUnlockKey []byte) error { |
| 77 | localPath, err := s.GetPathInPlace(PlaceESP, "local_unlock.bin") |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 78 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 79 | return fmt.Errorf("failed to find ESP mount: %w", err) |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 80 | } |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 81 | localUnlockBlob, err := ioutil.ReadFile(localPath) |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 82 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 83 | return fmt.Errorf("failed to read local unlock file from ESP: %w", err) |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 84 | } |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 85 | localUnlockKey, err := tpm.Unseal(localUnlockBlob) |
| 86 | if err != nil { |
| 87 | return fmt.Errorf("failed to unseal local unlock key: %w", err) |
| 88 | } |
| 89 | |
| 90 | key := make([]byte, keySize) |
| 91 | for i := uint16(0); i < keySize; i++ { |
| 92 | key[i] = localUnlockKey[i] ^ globalUnlockKey[i] |
| 93 | } |
| 94 | |
| 95 | if err := MapEncryptedBlockDevice("data", SmalltownDataCryptPath, key); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | if err := s.mountData(); err != nil { |
| 99 | return err |
| 100 | } |
| 101 | s.mutex.Lock() |
| 102 | s.dataReady = true |
| 103 | s.mutex.Unlock() |
| 104 | s.logger.Info("Mounted encrypted storage") |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // InitializeData initializes the Smalltown data partition and returns the global unlock key. It seals |
| 109 | // the local portion into the TPM and stores the blob on the ESP. This is a potentially slow |
| 110 | // operation since it touches the whole partition. |
| 111 | func (s *Manager) InitializeData() ([]byte, error) { |
| 112 | localUnlockKey, err := tpm.GenerateSafeKey(keySize) |
| 113 | if err != nil { |
| 114 | return []byte{}, fmt.Errorf("failed to generate safe key: %w", err) |
| 115 | } |
| 116 | globalUnlockKey, err := tpm.GenerateSafeKey(keySize) |
| 117 | if err != nil { |
| 118 | return []byte{}, fmt.Errorf("failed to generate safe key: %w", err) |
| 119 | } |
| 120 | |
| 121 | localUnlockBlob, err := tpm.Seal(localUnlockKey, tpm.SecureBootPCRs) |
| 122 | if err != nil { |
| 123 | return []byte{}, fmt.Errorf("failed to seal local unlock key: %w", err) |
| 124 | } |
| 125 | |
| 126 | // The actual key is generated by XORing together the localUnlockKey and the globalUnlockKey |
| 127 | // This provides us with a mathematical guarantee that the resulting key cannot be recovered |
| 128 | // whithout knowledge of both parts. |
| 129 | key := make([]byte, keySize) |
| 130 | for i := uint16(0); i < keySize; i++ { |
| 131 | key[i] = localUnlockKey[i] ^ globalUnlockKey[i] |
| 132 | } |
| 133 | |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 134 | if err := InitializeEncryptedBlockDevice("data", SmalltownDataCryptPath, key); err != nil { |
| 135 | s.logger.Error("Failed to initialize encrypted block device", zap.Error(err)) |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 136 | return []byte{}, fmt.Errorf("failed to initialize encrypted block device: %w", err) |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 137 | } |
| 138 | mkfsCmd := exec.Command("/bin/mkfs.xfs", "-qf", "/dev/data") |
| 139 | if _, err := mkfsCmd.Output(); err != nil { |
| 140 | s.logger.Error("Failed to format encrypted block device", zap.Error(err)) |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 141 | return []byte{}, fmt.Errorf("failed to format encrypted block device: %w", err) |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if err := s.mountData(); err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 145 | return []byte{}, err |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | s.mutex.Lock() |
| 149 | s.dataReady = true |
| 150 | s.mutex.Unlock() |
| 151 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 152 | localPath, err := s.GetPathInPlace(PlaceESP, "local_unlock.bin") |
| 153 | if err != nil { |
| 154 | return []byte{}, fmt.Errorf("failed to find ESP mount: %w", err) |
| 155 | } |
| 156 | if err := ioutil.WriteFile(localPath, localUnlockBlob, 0600); err != nil { |
| 157 | return []byte{}, fmt.Errorf("failed to write local unlock file to ESP: %w", err) |
| 158 | } |
| 159 | |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 160 | s.logger.Info("Initialized encrypted storage") |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 161 | return globalUnlockKey, nil |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func (s *Manager) mountData() error { |
| 165 | if err := os.Mkdir("/data", 0755); err != nil { |
| 166 | return err |
| 167 | } |
| 168 | |
Lorenz Brun | b15abad | 2020-04-16 11:17:12 +0200 | [diff] [blame] | 169 | if err := unix.Mount("/dev/data", "/data", "xfs", unix.MS_NOEXEC|unix.MS_NODEV, "pquota"); err != nil { |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 170 | return err |
| 171 | } |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | // GetPathInPlace returns a path in the given place |
| 176 | // It may return ErrNotInitialized if the place you're trying to access |
| 177 | // is not initialized or ErrUnknownPlace if the place is not known |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 178 | func (s *Manager) GetPathInPlace(place DataPlace, path string) (string, error) { |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 179 | s.mutex.RLock() |
| 180 | defer s.mutex.RUnlock() |
| 181 | switch place { |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 182 | case PlaceESP: |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 183 | return filepath.Join(espDataPath, path), nil |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 184 | case PlaceData: |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 185 | if s.dataReady { |
| 186 | return filepath.Join(dataMountPath, path), nil |
| 187 | } |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 188 | return "", ErrNotInitialized |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 189 | default: |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 190 | return "", ErrUnknownPlace |
Lorenz Brun | dd8c80e | 2019-10-07 16:19:49 +0200 | [diff] [blame] | 191 | } |
| 192 | } |