-
Notifications
You must be signed in to change notification settings - Fork 394
/
disk_unlock.go
169 lines (144 loc) · 5.48 KB
/
disk_unlock.go
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
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2022 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The disk_unlock command is used to unlock a disk drive with a
// HSS-derived password, and rescan the drive to enumerate the
// unlocked partitions.
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/u-root/u-root/pkg/hsskey"
"github.com/u-root/u-root/pkg/mount/block"
"github.com/u-root/u-root/pkg/mount/scuzz"
)
const (
// Master Password ID for SKM-based unlock.
skmMPI = 0x0601
)
var (
disk = flag.String("disk", "/dev/sda", "The disk to be unlocked")
verbose = flag.Bool("d", false, "print debug output")
verboseNoSanitize = flag.Bool("dangerously-disable-sanitize", false, "Print sensitive information - this should only be used for testing!")
noRereadPartitions = flag.Bool("no-reread-partitions", false, "Only attempt to unlock the disk, don't re-read the partition table.")
retries = flag.Int("num_retries", 1, "Number of times to retry password if unlocking fails for any reason other than the password being wrong.")
salt = flag.String("salt", hsskey.DefaultPasswordSalt, "Salt for password generation")
eepromPattern = flag.String("eeprom-pattern", "", "The pattern used to match EEPROM sysfs paths where the Host Secret Seeds are located")
hssFiles = flag.String("hss-files", "", "Comma deliminated paths to files containing a Host Secret Seed (HSS) to use")
)
func verboseLog(msg string) {
if *verbose {
log.Print(msg)
}
}
// writeFile is ioutil.WriteFile but disallows creating new file
func writeFile(filename string, contents string) error {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_SYNC, 0)
if err != nil {
return err
}
wlen, err := file.WriteString(contents)
if err != nil && wlen < len(contents) {
err = io.ErrShortWrite
}
// If Close() fails this likely indicates a write failure.
if errClose := file.Close(); err == nil {
err = errClose
}
return err
}
func main() {
flag.Parse()
// Open the disk. Read its identity, and use it to unlock the disk.
sgdisk, err := scuzz.NewSGDisk(*disk)
if err != nil {
log.Fatalf("failed to open disk %v: %v", *disk, err)
}
info, err := sgdisk.Identify()
if err != nil {
log.Fatalf("failed to read disk %v identity: %v", *disk, err)
}
verboseLog(fmt.Sprintf("Disk info for %s: %s", *disk, info.String()))
// Obtain 32 byte Host Secret Seed (HSS) from IPMI.
hssList, err := hsskey.GetAllHss(os.Stdout, *verboseNoSanitize, *eepromPattern, *hssFiles)
if err != nil {
log.Fatalf("error getting HSS: %v", err)
}
if len(hssList) == 0 {
log.Fatalf("no HSS found - can't unlock disk.")
}
verboseLog(fmt.Sprintf("Found %d Host Secret Seeds.", len(hssList)))
switch {
case !info.SecurityStatus.SecurityEnabled():
log.Printf("Disk security is not enabled on %v.", *disk)
return
case info.SecurityStatus.SecurityFrozen():
// If the disk is frozen, its security state cannot be changed until the next
// power on or hardware reset. Disk unlock will fail anyways, so return.
// This is unlikely to occur, since someone would need to freeze the drive's
// security state between the last AC cycle and this code being run.
log.Print("Disk security is frozen. Power cycle the machine to unfreeze the disk.")
return
case !info.SecurityStatus.SecurityLocked():
log.Print("Disk is already unlocked.")
return
case info.SecurityStatus.SecurityCountExpired():
// If the security count is expired, this means too many attempts have been
// made to unlock the disk. Reset this with an AC cycle or hardware reset
// on the disk.
log.Fatalf("Security count expired on disk. Reset the password counter by power cycling the disk.")
case info.MasterRevision != skmMPI:
log.Fatalf("Disk is locked with unknown master password ID: %X (Do you have skm tools installed?)", info.MasterRevision)
}
// Try using each HSS to unlock the disk - only 1 should work.
unlocked := false
TryAllHSS:
for i, hss := range hssList {
key, err := hsskey.GenPassword(hss, *salt, info.Serial, info.Model)
if err != nil {
log.Printf("Couldn't generate password with HSS %d: %v", i, err)
continue
}
for r := 0; r < *retries; r++ {
if err := sgdisk.Unlock(string(key), false); err != nil {
log.Printf("Couldn't unlock disk with HSS %d: %v", i, err)
} else {
unlocked = true
break TryAllHSS
}
}
}
if unlocked {
log.Printf("Successfully unlocked disk %s.", *disk)
} else {
log.Fatalf("Failed to unlock disk %s with any HSS.", *disk)
}
if *noRereadPartitions {
return
}
// Rescans all LUNs/Channels/Targets on the scsi_host. This ensures the kernel
// updates the ATA driver to see the newly unlocked disk.
verboseLog("Rescanning scsi...")
if err := writeFile("/sys/class/scsi_host/host0/scan", "- - -"); err != nil {
log.Fatalf("couldn't rescan SCSI to reload newly unlocked disk: %v", err)
}
// Update partitions on the on the disk.
verboseLog("Reloading disk partitions...")
diskdev, err := block.Device(*disk)
if err != nil {
log.Fatalf("Could not find %s: %v", *disk, err)
}
if err := diskdev.ReadPartitionTable(); err != nil {
log.Fatalf("Could not re-read partition table: %v", err)
}
glob := filepath.Join("/sys/class/block", diskdev.Name+"*")
parts, err := filepath.Glob(glob)
if err != nil {
log.Fatalf("Could not find disk partitions: %v", err)
}
verboseLog(fmt.Sprintf("Found these %s unlocked partitions: %v", *disk, parts))
}