Skip to content

Commit edf7c32

Browse files
committed
fix: pe uki extract
The PE UKI extract was returning a reader that reads upto the section `Size` which is the size on disk which could be padded, we need a limit reader reading upto `VirtualSize`. Signed-off-by: Noel Georgi <[email protected]>
1 parent 70f72c5 commit edf7c32

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

internal/pkg/uki/internal/pe/extract.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ func Extract(ukiPath string) (assetInfo AssetInfo, err error) {
3333
assetInfo.fileCloser = peFile
3434

3535
for _, section := range peFile.Sections {
36+
// read upto section.VirtualSize bytes
37+
sectionReader := io.NewSectionReader(section, 0, int64(section.VirtualSize))
38+
3639
switch section.Name {
3740
case ".initrd":
38-
assetInfo.Initrd = section.Open()
41+
assetInfo.Initrd = sectionReader
3942
case ".cmdline":
40-
assetInfo.Cmdline = section.Open()
43+
assetInfo.Cmdline = sectionReader
4144
case ".linux":
42-
assetInfo.Kernel = section.Open()
45+
assetInfo.Kernel = sectionReader
4346
}
4447
}
4548

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package pe_test
6+
7+
import (
8+
"io"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
"testing"
13+
14+
"github.com/stretchr/testify/assert"
15+
16+
"github.com/siderolabs/talos/internal/pkg/uki/internal/pe"
17+
)
18+
19+
func TestUKIExtract(t *testing.T) {
20+
srcFile := "testdata/sd-stub-amd64.efi"
21+
22+
destDir := t.TempDir()
23+
24+
destFile := filepath.Join(destDir, "vmlinuz.efi")
25+
26+
for _, section := range []string{"linux", "initrd", "cmdline"} {
27+
assert.NoError(t, os.WriteFile(filepath.Join(destDir, section), []byte(section), 0o644))
28+
}
29+
30+
assert.NoError(t, pe.AssembleNative(srcFile, destFile, []pe.Section{
31+
{
32+
Name: ".linux",
33+
Path: filepath.Join(destDir, "linux"),
34+
Measure: false,
35+
Append: true,
36+
},
37+
{
38+
Name: ".initrd",
39+
Path: filepath.Join(destDir, "initrd"),
40+
Measure: false,
41+
Append: true,
42+
},
43+
{
44+
Name: ".cmdline",
45+
Path: filepath.Join(destDir, "cmdline"),
46+
Measure: false,
47+
Append: true,
48+
},
49+
}))
50+
51+
ukiData, err := pe.Extract(destFile)
52+
assert.NoError(t, err)
53+
54+
t.Cleanup(func() {
55+
assert.NoError(t, ukiData.Close())
56+
})
57+
58+
var kernel, initrd, cmdline strings.Builder
59+
60+
_, err = io.Copy(&kernel, ukiData.Kernel)
61+
assert.NoError(t, err)
62+
63+
assert.Equal(t, "linux", kernel.String())
64+
65+
_, err = io.Copy(&initrd, ukiData.Initrd)
66+
assert.NoError(t, err)
67+
68+
assert.Equal(t, "initrd", initrd.String())
69+
70+
_, err = io.Copy(&cmdline, ukiData.Cmdline)
71+
assert.NoError(t, err)
72+
73+
assert.Equal(t, "cmdline", cmdline.String())
74+
}

0 commit comments

Comments
 (0)