forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uinit.go
161 lines (137 loc) · 4.09 KB
/
uinit.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
// Copyright 2019 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.
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/u-root/iscsinl"
"github.com/u-root/u-root/pkg/cmdline"
"github.com/u-root/u-root/pkg/dhclient"
slaunch "github.com/u-root/u-root/pkg/securelaunch"
"github.com/u-root/u-root/pkg/securelaunch/policy"
"github.com/u-root/u-root/pkg/securelaunch/tpm"
)
var (
slDebug = flag.Bool("d", false, "enable debug logs")
)
func checkDebugFlag() {
/*
* check if uroot.uinitargs=-d is set in kernel cmdline.
* if set, slaunch.Debug is set to log.Printf.
*/
flag.Parse()
if flag.NArg() > 1 {
log.Fatal("Incorrect number of arguments")
}
if *slDebug {
slaunch.Debug = log.Printf
slaunch.Debug("debug flag is set. Logging Enabled.")
}
}
/*
* main parses platform policy file, and based on the inputs,
* performs measurements and then launches a target kernel.
*
* steps followed by sluinit:
* 1. if debug flag is set, enable logging.
* 2. gets the TPM handle
* 3. Gets secure launch policy file entered by user.
* 4. calls collectors to collect measurements(hashes) a.k.a evidence.
*/
func main() {
checkDebugFlag()
err := scanIscsiDrives()
if err != nil {
log.Printf("NO ISCSI DRIVES found, err=[%v]", err)
}
defer unmountAndExit() // called only on error, on success we kexec
slaunch.Debug("********Step 1: init completed. starting main ********")
if err := tpm.New(); err != nil {
log.Printf("tpm.New() failed. err=%v", err)
return
}
defer tpm.Close()
slaunch.Debug("********Step 2: locate and parse SL Policy ********")
p, err := policy.Get()
if err != nil {
log.Printf("failed to get policy err=%v", err)
return
}
slaunch.Debug("policy file successfully parsed")
slaunch.Debug("********Step 3: Collecting Evidence ********")
for _, c := range p.Collectors {
slaunch.Debug("Input Collector: %v", c)
if e := c.Collect(); e != nil {
log.Printf("Collector %v failed, err = %v", c, e)
}
}
slaunch.Debug("Collectors completed")
slaunch.Debug("********Step 4: Measuring target kernel, initrd ********")
if err := p.Launcher.MeasureKernel(); err != nil {
log.Printf("Launcher.MeasureKernel failed err=%v", err)
return
}
slaunch.Debug("********Step 5: Parse eventlogs *********")
if err := p.EventLog.Parse(); err != nil {
log.Printf("EventLog.Parse() failed err=%v", err)
return
}
slaunch.Debug("*****Step 6: Dump logs to disk *******")
if err := slaunch.ClearPersistQueue(); err != nil {
log.Printf("ClearPersistQueue failed err=%v", err)
return
}
slaunch.Debug("********Step *: Unmount all ********")
slaunch.UnmountAll()
slaunch.Debug("********Step 7: Launcher called to Boot ********")
if err := p.Launcher.Boot(); err != nil {
log.Printf("Boot failed. err=%s", err)
return
}
}
// unmountAndExit is called on error and unmounts all devices.
// sluinit ends here.
func unmountAndExit() {
slaunch.UnmountAll()
time.Sleep(5 * time.Second) // let queued up debug statements get printed
os.Exit(1)
}
// scanIscsiDrives calls dhcleint to parse cmdline and
// iscsinl to mount iscsi drives.
func scanIscsiDrives() error {
val, ok := cmdline.Flag("netroot")
if !ok {
return errors.New("netroot flag is not set")
}
slaunch.Debug("netroot flag is set with val=%s", val)
target, volume, err := dhclient.ParseISCSIURI(val)
if err != nil {
return fmt.Errorf("dhclient ISCSI parser failed err=%v", err)
}
slaunch.Debug("resolved ip:port=%s", target)
slaunch.Debug("resolved vol=%v", volume)
slaunch.Debug("Scanning kernel cmd line for *rd.iscsi.initiator* flag")
initiatorName, ok := cmdline.Flag("rd.iscsi.initiator")
if !ok {
return errors.New("rd.iscsi.initiator flag is not set")
}
devices, err := iscsinl.MountIscsi(
iscsinl.WithInitiator(initiatorName),
iscsinl.WithTarget(target.String(), volume),
iscsinl.WithCmdsMax(128),
iscsinl.WithQueueDepth(16),
iscsinl.WithScheduler("noop"),
)
if err != nil {
return err
}
for i := range devices {
slaunch.Debug("Mounted at dev %v", devices[i])
}
return nil
}