Skip to content

Commit ad889bd

Browse files
authored
Merge pull request #451 from oasisprotocol/kostko/fix/deterministic-umask
Use deterministic umask during build
2 parents bc19916 + 3a3102c commit ad889bd

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

cmd/rofl/build/artifacts.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,22 @@ func createSquashFs(buildEnv env.ExecEnv, fn, dir string) (int64, error) {
305305
return fi.Size(), nil
306306
}
307307

308+
// sha256File computes a SHA-256 digest of the file with the given filename and returns a
309+
// hex-encoded hash.
310+
func sha256File(fn string) (string, error) {
311+
f, err := os.Open(fn)
312+
if err != nil {
313+
return "", fmt.Errorf("failed to open filesystem file: %w", err)
314+
}
315+
defer f.Close()
316+
317+
h := sha256.New()
318+
if _, err = io.Copy(h, f); err != nil {
319+
return "", fmt.Errorf("failed to read filesystem file: %w", err)
320+
}
321+
return hex.EncodeToString(h.Sum([]byte{})), nil
322+
}
323+
308324
// createVerityHashTree creates the verity Merkle hash tree and returns the root hash.
309325
func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, error) {
310326
// Print a nicer error message in case veritysetup is missing.
@@ -314,16 +330,10 @@ func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, er
314330
}
315331

316332
// Generate a deterministic salt by hashing the filesystem.
317-
f, err := os.Open(fsFn)
333+
salt, err := sha256File(fsFn)
318334
if err != nil {
319-
return "", fmt.Errorf("failed to open filesystem file: %w", err)
320-
}
321-
defer f.Close()
322-
h := sha256.New()
323-
if _, err = io.Copy(h, f); err != nil {
324-
return "", fmt.Errorf("failed to read filesystem file: %w", err)
335+
return "", err
325336
}
326-
salt := h.Sum([]byte{})
327337

328338
rootHashFn := hashFn + ".roothash"
329339

@@ -332,7 +342,7 @@ func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, er
332342
"--data-block-size=4096",
333343
"--hash-block-size=4096",
334344
"--uuid=00000000-0000-0000-0000-000000000000",
335-
"--salt="+hex.EncodeToString(salt),
345+
"--salt="+salt,
336346
"--root-hash-file="+rootHashFn,
337347
fsFn,
338348
hashFn,

cmd/rofl/build/artifacts_other.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ func extractHandleSpecialNode(path string, header *tar.Header) error {
1515
func extractChtimes(path string, atime, mtime time.Time) error {
1616
return os.Chtimes(path, atime, mtime)
1717
}
18+
19+
func setUmask(mask int) {
20+
}

cmd/rofl/build/artifacts_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ func extractChtimes(path string, atime, mtime time.Time) error {
2828
mtv := unix.NsecToTimeval(mtime.UnixNano())
2929
return unix.Lutimes(path, []unix.Timeval{atv, mtv})
3030
}
31+
32+
func setUmask(mask int) {
33+
unix.Umask(mask)
34+
}

cmd/rofl/build/build.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ var (
4343
Use: "build",
4444
Short: "Build a ROFL application",
4545
Args: cobra.NoArgs,
46-
RunE: func(_ *cobra.Command, _ []string) error {
46+
RunE: func(cmd *cobra.Command, _ []string) error {
47+
cmd.SilenceUsage = true
4748
cfg := cliConfig.Global()
4849
npa := common.GetNPASelection(cfg)
4950
manifest, deployment := roflCommon.LoadManifestAndSetNPA(cfg, npa, deploymentName, &roflCommon.ManifestOptions{
@@ -76,6 +77,9 @@ var (
7677
}
7778
defer os.RemoveAll(tmpDir)
7879

80+
// Ensure deterministic umask for builds.
81+
setUmask(0o002)
82+
7983
var buildEnv env.ExecEnv
8084
switch {
8185
case manifest.Artifacts.Builder == "" || noDocker:

cmd/rofl/build/tdx.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ func tdxPrepareStage2(
148148

149149
// Add runtime as init.
150150
fmt.Println("Adding runtime as init...")
151+
152+
initHash, err := sha256File(initPath)
153+
if err != nil {
154+
return nil, err
155+
}
156+
fmt.Printf("Runtime hash: %s\n", initHash)
157+
151158
if err := copyFile(initPath, filepath.Join(rootfsDir, "init"), 0o755); err != nil {
152159
return nil, err
153160
}

main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package main
22

3-
import "github.com/oasisprotocol/cli/cmd"
3+
import (
4+
"os"
5+
6+
"github.com/oasisprotocol/cli/cmd"
7+
)
48

59
func main() {
6-
_ = cmd.Execute()
10+
if err := cmd.Execute(); err != nil {
11+
os.Exit(1)
12+
}
713
}

0 commit comments

Comments
 (0)