Skip to content

Commit e6cba4f

Browse files
committed
tools/mrtool: allow to prepend ima paths
Signed-off-by: Simon Ott <simon.ott@aisec.fraunhofer.de>
1 parent 9020eab commit e6cba4f

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

tools/mrtool/precomputetpm/ima.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
ar "github.com/Fraunhofer-AISEC/cmc/attestationreport"
3131
)
3232

33-
func performImaPrecomputation(pcr int, bootAggregate []byte, paths []string, strip string, imaTemplate string) ([]*ar.ReferenceValue, error) {
33+
func performImaPrecomputation(pcr int, bootAggregate []byte, paths []string, strip, prepend string, imaTemplate string) ([]*ar.ReferenceValue, error) {
3434

3535
refvals := make([]*ar.ReferenceValue, 0)
3636
fileCh := make(chan string, 100)
@@ -57,9 +57,9 @@ func performImaPrecomputation(pcr int, bootAggregate []byte, paths []string, str
5757
go func() {
5858
defer wg.Done()
5959
for path := range fileCh {
60-
refval, err := precomputeImaEntry(path, strip, imaTemplate, pcr, true)
60+
refval, err := precomputeImaEntry(path, strip, prepend, imaTemplate, pcr, true)
6161
if err != nil {
62-
log.Debugf("error hashing %q: %v", path, err)
62+
log.Errorf("error hashing %q: %v", path, err)
6363
continue
6464
}
6565
log.Tracef("%s: %s", refval.SubType, hex.EncodeToString(refval.Sha256))
@@ -151,27 +151,27 @@ func precomputeImaBootAggregate(hash []byte, template string, pcr int, optional
151151
return r, nil
152152
}
153153

154-
func precomputeImaEntry(path, strip, template string, pcr int, optional bool) (*ar.ReferenceValue, error) {
154+
func precomputeImaEntry(path, strip, prepend, template string, pcr int, optional bool) (*ar.ReferenceValue, error) {
155155

156156
fileHash, err := hashFile(path)
157157
if err != nil {
158158
return nil, fmt.Errorf("failed to hash file: %w", err)
159159
}
160160

161-
strippedPath := stripPrefix(path, strip)
161+
hashedPath := modifyPath(path, strip, prepend)
162162

163-
tmpl, err := precomputeImaTemplate(fileHash, strippedPath, template)
163+
tmpl, err := precomputeImaTemplate(fileHash, hashedPath, template)
164164
if err != nil {
165165
return nil, fmt.Errorf("failed to precompute ima template: %w", err)
166166
}
167167

168168
// Create reference value
169169
r := &ar.ReferenceValue{
170170
Type: "TPM Reference Value",
171-
SubType: filepath.Base(strippedPath),
171+
SubType: filepath.Base(hashedPath),
172172
Index: pcr,
173173
Sha256: tmpl,
174-
Description: strippedPath,
174+
Description: hashedPath,
175175
Optional: optional,
176176
}
177177

@@ -224,12 +224,10 @@ func hashFile(path string) ([]byte, error) {
224224
return h.Sum(nil), nil
225225
}
226226

227-
func stripPrefix(s, prefix string) string {
227+
func modifyPath(s, prefix, prepend string) string {
228228
if s == "" {
229229
return ""
230230
}
231-
if prefix != "" && strings.HasPrefix(s, prefix) {
232-
return s[len(prefix):]
233-
}
234-
return s
231+
s = strings.TrimPrefix(s, prefix)
232+
return prepend + s
235233
}

tools/mrtool/precomputetpm/pcrs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ func PrecomputePcr9(c *Config) (*ar.ReferenceValue, []*ar.ReferenceValue, error)
617617

618618
func PrecomputePcr10(c *Config) (*ar.ReferenceValue, []*ar.ReferenceValue, error) {
619619

620-
refvals, err := performImaPrecomputation(10, c.BootAggregate, c.ImaPaths, c.ImaStrip, c.ImaTemplate)
620+
refvals, err := performImaPrecomputation(10, c.BootAggregate, c.ImaPaths, c.ImaStrip, c.ImaPrepend, c.ImaTemplate)
621621
if err != nil {
622622
return nil, nil, fmt.Errorf("failed to precompute IMA refvals: %w", err)
623623
}

tools/mrtool/precomputetpm/run.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Config struct {
4242
MokLists []string
4343
ImaPaths []string
4444
ImaStrip string
45+
ImaPrepend string
4546
ImaTemplate string
4647
BootAggregate []byte
4748
PrintAggregate bool
@@ -50,9 +51,10 @@ type Config struct {
5051
const (
5152
systemUuidFlag = "systemuuid"
5253
grubcmdsFlag = "grubcmds"
53-
pathFlag = "ima-path"
54+
pathFlag = "paths"
5455
imaPathFlag = "ima-path"
5556
imaStripFlag = "ima-strip"
57+
imaPrependFlag = "ima-prepend"
5658
imaTemplateFlag = "ima-template"
5759
bootAggregateFlag = "boot-aggregate"
5860
printAggregateFlag = "print-aggregate"
@@ -71,9 +73,14 @@ var flags = []cli.Flag{
7173
Name: imaStripFlag,
7274
Usage: "Optional ima path prefix which is stripped from the actual path in the output",
7375
},
76+
&cli.StringFlag{
77+
Name: imaPrependFlag,
78+
Usage: "Optional ima path segment which is prepended the actual path in the output",
79+
},
7480
&cli.StringFlag{
7581
Name: imaTemplateFlag,
7682
Usage: "IMA template name (ima-ng or ima-sig)",
83+
Value: "ima-sig",
7784
},
7885
&cli.StringFlag{
7986
Name: bootAggregateFlag,
@@ -186,9 +193,12 @@ func getConfig(cmd *cli.Command) (*Config, error) {
186193
if cmd.IsSet(imaStripFlag) {
187194
c.ImaStrip = cmd.String(imaStripFlag)
188195
}
189-
if cmd.IsSet(imaTemplateFlag) {
190-
c.ImaTemplate = cmd.String(imaTemplateFlag)
196+
if cmd.IsSet(imaPrependFlag) {
197+
c.ImaPrepend = cmd.String(imaPrependFlag)
191198
}
199+
200+
c.ImaTemplate = cmd.String(imaTemplateFlag)
201+
192202
if cmd.IsSet(bootAggregateFlag) {
193203
b, err := hex.DecodeString(cmd.String(bootAggregateFlag))
194204
if err != nil {
@@ -239,6 +249,9 @@ func (c *Config) print() {
239249
if c.ImaStrip != "" {
240250
log.Debugf("\tIMA strip : %q", c.ImaStrip)
241251
}
252+
if c.ImaPrepend != "" {
253+
log.Debugf("\tIMA prepend : %q", c.ImaPrepend)
254+
}
242255
if c.BootAggregate != nil {
243256
log.Debugf("\tboot-aggregate: %q", hex.EncodeToString(c.BootAggregate))
244257
}

0 commit comments

Comments
 (0)