Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use offline registry library to generate minimum hive #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions internal/wclayer/converttobaselayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,63 @@ package wclayer

import (
"context"
"fmt"
"os"
"path/filepath"
"syscall"

"github.com/Microsoft/hcsshim/internal/hcserror"
"github.com/Microsoft/hcsshim/internal/longpath"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/internal/safefile"
"github.com/Microsoft/hcsshim/internal/winapi"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"golang.org/x/sys/windows"
)

var hiveNames = []string{"DEFAULT", "SAM", "SECURITY", "SOFTWARE", "SYSTEM"}

// Ensure the given file exists as an ordinary file, and create a zero-length file if not.
func ensureFile(path string, root *os.File) error {
stat, err := safefile.LstatRelative(path, root)
if err != nil && os.IsNotExist(err) {
newFile, err := safefile.OpenRelative(path, root, 0, syscall.FILE_SHARE_WRITE, winapi.FILE_CREATE, 0)
if err != nil {
return err
}
return newFile.Close()
// Ensure the given file exists as an ordinary file, and create a minimal hive file if not.
func ensureHive(path string, root *os.File) (err error) {
_, err = safefile.LstatRelative(path, root)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("accessing %s: %w", path, err)
}

version := windows.RtlGetVersion()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance for this to go wrong due to manifest shenanigans? Is there any risk in hard-coding 10.0?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hives we're generating use an old format. I think we could even use Windows XP as a version and still be fine. My understanding is that without manifesting, we default to Windows 8 which is fine I think. I can hard code 10.0 if you prefer.

if version == nil {
return fmt.Errorf("failed to get OS version")
}

var fullPath string
fullPath, err = longpath.LongAbs(filepath.Join(root.Name(), path))
if err != nil {
return err
return fmt.Errorf("getting path: %w", err)
}

if !stat.Mode().IsRegular() {
fullPath := filepath.Join(root.Name(), path)
return errors.Errorf("%s has unexpected file mode %s", fullPath, stat.Mode().String())
var key syscall.Handle
err = winapi.ORCreateHive(&key)
if err != nil {
return fmt.Errorf("creating hive: %w", err)
}

defer func() {
closeErr := winapi.ORCloseHive(&key)
if closeErr != nil {
err = fmt.Errorf("closing hive key: %w", closeErr)
}
}()

var hivePath *uint16
hivePath, err = syscall.UTF16PtrFromString(fullPath)
if err != nil {
return fmt.Errorf("getting path: %w", err)
}

err = winapi.ORSaveHive(key, hivePath, version.MajorVersion, version.MinorVersion)
if err != nil {
return fmt.Errorf("saving hive: %w", err)
}

return nil
gabriel-samfira marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -48,7 +73,7 @@ func ensureBaseLayer(root *os.File) (hasUtilityVM bool, err error) {

for _, hiveName := range hiveNames {
hivePath := filepath.Join(hiveSourcePath, hiveName)
if err = ensureFile(hivePath, root); err != nil {
if err = ensureHive(hivePath, root); err != nil {
return
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/winapi/ofreg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package winapi

//sys ORCreateHive(key *syscall.Handle) (regerrno error) = offreg.ORCreateHive
//sys ORSaveHive(key syscall.Handle, file *uint16, OsMajorVersion uint32, OsMinorVersion uint32) (regerrno error) = offreg.ORSaveHive
//sys ORCloseHive(key *syscall.Handle) (regerrno error) = offreg.ORCloseHive
2 changes: 1 addition & 1 deletion internal/winapi/winapi.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package winapi

//go:generate go run ..\..\mksyscall_windows.go -output zsyscall_windows.go bindflt.go user.go console.go system.go net.go path.go thread.go jobobject.go logon.go memory.go process.go processor.go devices.go filesystem.go errors.go
//go:generate go run ..\..\mksyscall_windows.go -output zsyscall_windows.go bindflt.go user.go console.go system.go net.go path.go thread.go jobobject.go logon.go memory.go process.go processor.go devices.go filesystem.go errors.go ofreg.go
28 changes: 28 additions & 0 deletions internal/winapi/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.