Skip to content

Commit 2b246a1

Browse files
committed
Support for mounting merged verified CIMs
This commit adds the new CimFS API that mounts the merged verified CIMs. Merge mounting verified CIM requires passing the root hash of the merged CIM. All reads happening on this mounted verified CIM will then be compared with this root hash for integrity check. This commit also adds the tests for the same. (A test that simply mounts verified CIMs was removed as that is being tested as a part of other verified CIM tests.) GCS sidecar code is also updated to use the newly added verified CIM mount APIs. Signed-off-by: Amit Barve <ambarve@microsoft.com>
1 parent 36f0201 commit 2b246a1

File tree

6 files changed

+180
-32
lines changed

6 files changed

+180
-32
lines changed

internal/gcs-sidecar/handlers.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,14 @@ func (b *Bridge) modifySettings(req *request) (err error) {
677677
}
678678

679679
if len(layerCIMs) > 1 {
680-
// Get the topmost merge CIM and invoke the MountMergedBlockCIMs
681-
_, err := cimfs.MountMergedBlockCIMs(layerCIMs[0], layerCIMs[1:], wcowBlockCimMounts.MountFlags, wcowBlockCimMounts.VolumeGUID)
680+
_, err = cimfs.MountMergedVerifiedBlockCIMs(layerCIMs[0], layerCIMs[1:], wcowBlockCimMounts.MountFlags, wcowBlockCimMounts.VolumeGUID, layerDigests[0])
682681
if err != nil {
683682
return fmt.Errorf("error mounting multilayer block cims: %w", err)
684683
}
685684
} else {
686-
_, err := cimfs.Mount(filepath.Join(layerCIMs[0].BlockPath, layerCIMs[0].CimName), wcowBlockCimMounts.VolumeGUID, wcowBlockCimMounts.MountFlags)
685+
_, err = cimfs.MountVerifiedBlockCIM(layerCIMs[0], wcowBlockCimMounts.MountFlags, wcowBlockCimMounts.VolumeGUID, layerDigests[0])
687686
if err != nil {
688-
return fmt.Errorf("error mounting merged block cims: %w", err)
687+
return fmt.Errorf("error mounting verified block cim: %w", err)
689688
}
690689
}
691690

internal/winapi/cimfs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ type CimFsImagePath struct {
5959
//sys CimSealImage(blockCimPath string, hashSize *uint64, fixedHeaderSize *uint64, hash *byte) (hr error) = cimfs.CimSealImage?
6060
//sys CimGetVerificationInformation(blockCimPath string, isSealed *uint32, hashSize *uint64, signatureSize *uint64, fixedHeaderSize *uint64, hash *byte, signature *byte) (hr error) = cimfs.CimGetVerificationInformation?
6161
//sys CimMountVerifiedImage(imagePath string, fsName string, flags uint32, volumeID *g, hashSize uint16, hash *byte) (hr error) = cimfs.CimMountVerifiedImage?
62+
//sys CimMergeMountVerifiedImage(numCimPaths uint32, backingImagePaths *CimFsImagePath, flags uint32, volumeID *g, hashSize uint16, hash *byte) (hr error) = cimfs.CimMergeMountVerifiedImage

internal/winapi/zsyscall_windows.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cimfs/cim_test.go

Lines changed: 99 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -679,33 +679,6 @@ func TestMergedLinksInMergedBlockCIMs(rootT *testing.T) {
679679
}
680680
}
681681

682-
func TestVerifiedSingleFileBlockCIM(t *testing.T) {
683-
if !IsVerifiedCimSupported() {
684-
t.Skipf("verified CIMs are not supported")
685-
}
686-
687-
// contents to write to the CIM
688-
testContents := []tuple{
689-
{"foo.txt", []byte("foo1"), false},
690-
{"bar.txt", []byte("bar"), false},
691-
}
692-
693-
root := t.TempDir()
694-
blockPath := filepath.Join(root, "layer.bcim")
695-
tc := &testVerifiedBlockCIM{
696-
BlockCIM: BlockCIM{
697-
Type: BlockCIMTypeSingleFile,
698-
BlockPath: blockPath,
699-
CimName: "layer.cim",
700-
}}
701-
writer := openNewCIM(t, tc)
702-
writeCIM(t, writer, testContents)
703-
704-
mountvol := mountCIM(t, tc, CimMountVerifiedCim|CimMountSingleFileCim)
705-
706-
compareContent(t, mountvol, testContents)
707-
}
708-
709682
func TestVerifiedSingleFileBlockCIMMount(t *testing.T) {
710683
if !IsVerifiedCimSupported() {
711684
t.Skipf("verified CIMs are not supported")
@@ -794,3 +767,102 @@ func TestVerifiedSingleFileBlockCIMMountReadFailure(t *testing.T) {
794767
t.Fatalf("expected integrity violation error")
795768
}
796769
}
770+
771+
func TestMergedVerifiedBlockCIMs(rootT *testing.T) {
772+
if !IsVerifiedCimSupported() {
773+
rootT.Skipf("verified BlockCIMs are not supported")
774+
}
775+
776+
// A slice of 3 slices, 1 slice for contents of each CIM
777+
testContents := [][]tuple{
778+
{{"foo.txt", []byte("foo1"), false}},
779+
{{"bar.txt", []byte("bar"), false}},
780+
{{"foo.txt", []byte("foo2"), false}},
781+
}
782+
// create 3 separate block CIMs
783+
nCIMs := len(testContents)
784+
785+
// test merging for both SingleFile & BlockDevice type of block CIMs
786+
type testBlock struct {
787+
name string
788+
blockType BlockCIMType
789+
mountFlag uint32
790+
blockPathGenerator func(t *testing.T, dir string) string
791+
}
792+
793+
tests := []testBlock{
794+
{
795+
name: "single file",
796+
blockType: BlockCIMTypeSingleFile,
797+
mountFlag: CimMountSingleFileCim,
798+
blockPathGenerator: func(t *testing.T, dir string) string {
799+
t.Helper()
800+
return filepath.Join(dir, "layer.bcim")
801+
},
802+
},
803+
{
804+
name: "block device",
805+
blockType: BlockCIMTypeDevice,
806+
mountFlag: CimMountBlockDeviceCim,
807+
blockPathGenerator: func(t *testing.T, dir string) string {
808+
t.Helper()
809+
return createBlockDevice(t, dir)
810+
},
811+
},
812+
}
813+
814+
for _, test := range tests {
815+
rootT.Run(test.name, func(t *testing.T) {
816+
sourceCIMs := make([]*BlockCIM, 0, nCIMs)
817+
for i := 0; i < nCIMs; i++ {
818+
root := t.TempDir()
819+
blockPath := test.blockPathGenerator(t, root)
820+
tc := &testVerifiedBlockCIM{
821+
BlockCIM: BlockCIM{
822+
Type: test.blockType,
823+
BlockPath: blockPath,
824+
CimName: "layer.cim",
825+
}}
826+
writer := openNewCIM(t, tc)
827+
writeCIM(t, writer, testContents[i])
828+
sourceCIMs = append(sourceCIMs, &tc.BlockCIM)
829+
}
830+
831+
mergedBlockPath := test.blockPathGenerator(t, t.TempDir())
832+
// prepare a merged CIM
833+
mergedCIM := &BlockCIM{
834+
Type: test.blockType,
835+
BlockPath: mergedBlockPath,
836+
CimName: "merged.cim",
837+
}
838+
839+
if err := MergeBlockCIMsWithOpts(context.Background(), mergedCIM, sourceCIMs, WithDataIntegrity()); err != nil {
840+
t.Fatalf("failed to merge block CIMs: %s", err)
841+
}
842+
843+
rootHash, err := GetVerificationInfo(mergedBlockPath)
844+
if err != nil {
845+
t.Fatalf("failed to get verification info: %s", err)
846+
}
847+
848+
// mount and read the contents of the cim
849+
volumeGUID, err := guid.NewV4()
850+
if err != nil {
851+
t.Fatalf("generate cim mount GUID: %s", err)
852+
}
853+
854+
mountvol, err := MountMergedVerifiedBlockCIMs(mergedCIM, sourceCIMs, test.mountFlag, volumeGUID, rootHash)
855+
if err != nil {
856+
t.Fatalf("failed to mount merged block CIMs: %s\n", err)
857+
}
858+
defer func() {
859+
if err := Unmount(mountvol); err != nil {
860+
t.Logf("CIM unmount failed: %s", err)
861+
}
862+
}()
863+
// since we are merging, only 1 foo.txt (from the 1st CIM) should
864+
// show up
865+
compareContent(t, mountvol, []tuple{testContents[0][0], testContents[1][0]})
866+
})
867+
}
868+
}

pkg/cimfs/cim_writer_windows.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (c *CimFsWriter) Close() (err error) {
346346
}
347347
if c.sealOnClose {
348348
if err = sealBlockCIM(filepath.Dir(c.name)); err != nil {
349-
return &OpError{Cim: c.name, Op: "seal", Err: err}
349+
return &OpError{Cim: filepath.Dir(c.name), Op: "seal", Err: err}
350350
}
351351
}
352352
return nil
@@ -506,6 +506,10 @@ func MergeBlockCIMs(mergedCIM *BlockCIM, sourceCIMs []*BlockCIM) (err error) {
506506
func sealBlockCIM(blockPath string) error {
507507
var hashSize, fixedHeaderSize uint64
508508
hashBuf := make([]byte, cimHashSize)
509+
510+
// the blockPath could be a path to a block device or a file. In either case there should be no trailing backslash.
511+
blockPath = strings.TrimSuffix(blockPath, "\\")
512+
509513
if err := winapi.CimSealImage(blockPath, &hashSize, &fixedHeaderSize, &hashBuf[0]); err != nil {
510514
return fmt.Errorf("failed to seal block CIM: %w", err)
511515
} else if hashSize != cimHashSize {

pkg/cimfs/mount_cim.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,63 @@ func MountVerifiedBlockCIM(bCIM *BlockCIM, mountFlags uint32, volumeGUID guid.GU
151151
}
152152
return fmt.Sprintf("\\\\?\\Volume{%s}\\", volumeGUID.String()), nil
153153
}
154+
155+
// MountMergedVerifiedBlockCIMs mounts the given merged verified BlockCIM (usually created
156+
// with `MergeBlockCIMs`) at a volume with given GUID, with the given root hash. The
157+
// `sourceCIMs` MUST be identical to the `sourceCIMs` passed to `MergeBlockCIMs` when
158+
// creating this merged CIM. The root hash is usually returned when the CIM is sealed or
159+
// the root hash can be queried from a block CIM. In case of merged CIMs, the root hash of
160+
// the merged CIM should be passed here. Every read on the mounted volume will be verified
161+
// to match against the provided root hash if it doesn't, the read will fail. The source
162+
// CIMs and the merged CIM MUST have been created with the verified creation flag.
163+
func MountMergedVerifiedBlockCIMs(mergedCIM *BlockCIM, sourceCIMs []*BlockCIM, mountFlags uint32, volumeGUID guid.GUID, rootHash []byte) (string, error) {
164+
if !IsVerifiedCimSupported() {
165+
return "", fmt.Errorf("verified CIMs aren't supported on this OS version")
166+
} else if len(sourceCIMs) < 2 {
167+
return "", fmt.Errorf("need at least 2 source CIMs, got %d: %w", len(sourceCIMs), os.ErrInvalid)
168+
} else if len(rootHash) != cimHashSize {
169+
return "", fmt.Errorf("unexpected root hash size %d, expected size is %d", len(rootHash), cimHashSize)
170+
}
171+
172+
switch mergedCIM.Type {
173+
case BlockCIMTypeDevice:
174+
mountFlags |= CimMountBlockDeviceCim
175+
case BlockCIMTypeSingleFile:
176+
mountFlags |= CimMountSingleFileCim
177+
default:
178+
return "", fmt.Errorf("invalid block CIM type `%d`", mergedCIM.Type)
179+
}
180+
181+
for _, sCIM := range sourceCIMs {
182+
if sCIM.Type != mergedCIM.Type {
183+
return "", fmt.Errorf("source CIM (%s) type doesn't match with merged CIM type: %w", sCIM.String(), os.ErrInvalid)
184+
}
185+
}
186+
187+
// win32 mount merged CIM API expects an array of all CIMs. 0th entry in the array
188+
// should be the merged CIM. All remaining entries should be the source CIM paths
189+
// in the same order that was used while creating the merged CIM.
190+
allcims := append([]*BlockCIM{mergedCIM}, sourceCIMs...)
191+
cimsToMerge := []winapi.CimFsImagePath{}
192+
for _, bcim := range allcims {
193+
// Trailing backslashes cause problems-remove those
194+
imageDir, err := windows.UTF16PtrFromString(strings.TrimRight(bcim.BlockPath, `\`))
195+
if err != nil {
196+
return "", fmt.Errorf("convert string to utf16: %w", err)
197+
}
198+
cimName, err := windows.UTF16PtrFromString(bcim.CimName)
199+
if err != nil {
200+
return "", fmt.Errorf("convert string to utf16: %w", err)
201+
}
202+
203+
cimsToMerge = append(cimsToMerge, winapi.CimFsImagePath{
204+
ImageDir: imageDir,
205+
ImageName: cimName,
206+
})
207+
}
208+
209+
if err := winapi.CimMergeMountVerifiedImage(uint32(len(cimsToMerge)), &cimsToMerge[0], mountFlags, &volumeGUID, cimHashSize, &rootHash[0]); err != nil {
210+
return "", &MountError{Cim: filepath.Join(mergedCIM.BlockPath, mergedCIM.CimName), Op: "MountMergedVerified", Err: err}
211+
}
212+
return fmt.Sprintf(VolumePathFormat, volumeGUID.String()), nil
213+
}

0 commit comments

Comments
 (0)