Skip to content

Commit

Permalink
Create fsverity unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: James Jenkins <James.Jenkins@ibm.com>
  • Loading branch information
Jenkins-J committed Apr 9, 2024
1 parent 36e1ad4 commit efc8f9b
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/fsverity/fsverity_linux.go
Expand Up @@ -97,6 +97,7 @@ func IsEnabled(path string) (bool, error) {
if err != nil {
return false, err
}
defer f.Close()

var attr int

Expand Down
4 changes: 4 additions & 0 deletions pkg/fsverity/fsverity_other.go
Expand Up @@ -24,6 +24,10 @@ func IsSupported(rootPath string) (bool, error) {
return false, fmt.Errorf("fsverity is only supported on Linux systems")
}

func IsEnabled(path string) (bool, error) {
return false, fmt.Errorf("fsverity is only supported on Linux systems")
}

func Enable(_ string) error {
return fmt.Errorf("fsverity is only supported on Linux systems")
}
228 changes: 228 additions & 0 deletions pkg/fsverity/fsverity_test.go
@@ -0,0 +1,228 @@
//go:build linux

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fsverity

import (
"bufio"
"encoding/binary"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"testing"

"github.com/containerd/containerd/v2/pkg/testutil"
)

type superblockROFeatures struct {
_ [100]byte
Features uint32
}

func TestEnable(t *testing.T) {

testutil.RequiresRoot(t)

rootDir := filepath.Join(t.TempDir(), "content")
err := os.Mkdir(rootDir, 0755)
if err != nil {
t.Errorf("could not create temporary directory: %s", err.Error())
}

device, err := resolveDevicePath(rootDir)
if err != nil {
t.Skipf("invalid device: %s", err.Error())
}

enabled, err := ext4IsVerity(device)
if !enabled || err != nil {
t.Skipf("fsverity not enabled on the file system: %s", err.Error())
}

verityFile := filepath.Join(rootDir, "fsverityFile")
f, err := os.Create(verityFile)
if err != nil {
t.Errorf("could not create fsverity test file: %s", err.Error())
}

err = f.Close()
if err != nil {
t.Errorf("error closing fsverity test file: %s", err.Error())
}

defer func() {
err := os.Remove(verityFile)
if err != nil {
t.Logf("error removing fsverity test file: %s", err.Error())
}
}()

err = Enable(verityFile)
if err != nil {
t.Errorf("fsverity Enable failed: %s", err.Error())
}
}

func TestIsEnabled(t *testing.T) {

testDir := filepath.Join(t.TempDir(), "content")
err := os.Mkdir(testDir, 0755)
if err != nil {
t.Errorf("could not create temporary directory: %s", err.Error())
}

if supported, err := IsSupported(testDir); !supported || err != nil {
t.Skipf("fsverity is not supported")
}

verityFile := filepath.Join(testDir, "fsverityFile")
f, err := os.Create(verityFile)
if err != nil {
t.Errorf("could not create fsverity test file: %s", err.Error())
}

err = f.Close()
if err != nil {
t.Errorf("error closing fsverity test file: %s", err.Error())
}

defer func() {
err := os.Remove(verityFile)
if err != nil {
t.Logf("error removing fsverity test file: %s", err.Error())
}
}()

err = Enable(verityFile)
if err != nil {
t.Errorf("fsverity Enable failed: %s", err.Error())
}

if enabled, err := IsEnabled(verityFile); !enabled || err != nil {
t.Errorf("expected fsverity to be enabled on file, received enabled: %t; error: %s", enabled, err.Error())
}
}

func resolveDevicePath(path string) (string, error) {
var devicePath string

s, err := os.Stat(path)
if err != nil {
return devicePath, err
}

sys := s.Sys()
stat, ok := sys.(*syscall.Stat_t)
if !ok {
return devicePath, fmt.Errorf("type assert to syscall.Stat_t failed")
}

// resolve to device path
maj := (stat.Dev >> 8) & 0xff
min := stat.Dev & 0xff

m, err := os.Open("/proc/self/mountinfo")
if err != nil {
return devicePath, err
}

defer func() {
err := m.Close()
if err != nil {
fmt.Printf("could not close mountinfo\n")
}
}()

// scan for major:minor id and get the device path
scanner := bufio.NewScanner(m)

var entry string
sub := fmt.Sprintf("%d:%d", maj, min)
for scanner.Scan() {
if strings.Contains(scanner.Text(), sub) {
entry = scanner.Text()
break
}
}
if entry == "" {
return devicePath, fmt.Errorf("device mount not found for device id %s", sub)
}

entryReader := strings.NewReader(entry)
extScan := bufio.NewScanner(entryReader)
extScan.Split(bufio.ScanWords)

var word string
for (word != "-") && extScan.Scan() {
word = extScan.Text()
}

if !extScan.Scan() {
return devicePath, fmt.Errorf("scanning mounts failed: %w", extScan.Err())
}
fs := extScan.Text()

if fs != "ext4" {
return devicePath, fmt.Errorf("not an ext4 file system, skipping device")
}

if !extScan.Scan() {
return devicePath, fmt.Errorf("scanning mounts failed: %w", extScan.Err())
}
devicePath = extScan.Text()

return devicePath, nil
}

func ext4IsVerity(fpath string) (bool, error) {
b := superblockROFeatures{}

r, err := os.Open(fpath)
if err != nil {
return false, err
}

defer func() {
err := r.Close()
if err != nil {
fmt.Printf("failed to close %s: %s\n", fpath, err.Error())
}
}()

// seek to superblock
_, err = r.Seek(1024, 0)
if err != nil {
return false, err
}

err = binary.Read(r, binary.LittleEndian, &b)
if err != nil {
return false, err
}

// extract fsverity flag
var verityMask uint32 = 0x8000
res := verityMask & b.Features
if res > 0 {
return true, nil
}

return false, fmt.Errorf("fsverity not enabled on ext4 file system %s", fpath)
}
13 changes: 13 additions & 0 deletions plugins/content/local/store_test.go
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/containerd/containerd/v2/core/content"
"github.com/containerd/containerd/v2/core/content/testsuite"
"github.com/containerd/containerd/v2/internal/randutil"
"github.com/containerd/containerd/v2/pkg/fsverity"
"github.com/containerd/containerd/v2/pkg/testutil"
"github.com/containerd/errdefs"

Expand Down Expand Up @@ -193,6 +194,18 @@ func TestContentWriter(t *testing.T) {
t.Fatal("mismatched data written to disk")
}

// ensure fsverity is enabled on blob if fsverity is supported
ok, err := fsverity.IsSupported(tmpdir)
if !ok || err != nil {
t.Log("fsverity not supported, skipping fsverity check")
return
}

ok, err = fsverity.IsEnabled(path)
if !ok || err != nil {
t.Fatal(err)
}

}

func TestWalkBlobs(t *testing.T) {
Expand Down

0 comments on commit efc8f9b

Please sign in to comment.