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
Fsverity content verification #10007
Open
Jenkins-J
wants to merge
5
commits into
containerd:main
Choose a base branch
from
Jenkins-J:fsverity-content-verification
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+430
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
36e1ad4
Implement fsverity functionality
Jenkins-J 9ba5d37
Create fsverity unit tests
Jenkins-J ca67f2c
Cache IsSupported in Content Plugin
Jenkins-J 10e03e6
Move cached integrity supported value
Jenkins-J b3dc6e2
Clean up IsSupported function
Jenkins-J File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
"unsafe" | ||
|
||
"github.com/containerd/containerd/v2/pkg/kernelversion" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
type fsverityEnableArg struct { | ||
version uint32 | ||
hashAlgorithm uint32 | ||
blockSize uint32 | ||
saltSize uint32 | ||
saltPtr uint64 | ||
sigSize uint32 | ||
reserved1 uint32 | ||
sigPtr uint64 | ||
reserved2 [11]uint64 | ||
} | ||
|
||
const ( | ||
defaultBlockSize int = 4096 | ||
maxDigestSize uint16 = 64 | ||
) | ||
|
||
func IsSupported(rootPath string) (bool, error) { | ||
minKernelVersion := kernelversion.KernelVersion{Kernel: 5, Major: 4} | ||
s, err := kernelversion.GreaterEqualThan(minKernelVersion) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
integrityDir, err := os.MkdirTemp(rootPath, ".fsverity-check-*") | ||
if err != nil { | ||
return false, err | ||
} | ||
defer os.RemoveAll(integrityDir) | ||
|
||
digestPath := filepath.Join(integrityDir, "supported") | ||
digestFile, err := os.Create(digestPath) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
digestFile.Close() | ||
|
||
eerr := Enable(digestPath) | ||
if eerr != nil { | ||
return false, eerr | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func IsEnabled(path string) (bool, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
var attr int | ||
|
||
_, _, flagErr := unix.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(unix.FS_IOC_GETFLAGS), uintptr(unsafe.Pointer(&attr))) | ||
if flagErr != 0 { | ||
return false, fmt.Errorf("error getting inode flags: %w", flagErr) | ||
} | ||
|
||
if attr&unix.FS_VERITY_FL == unix.FS_VERITY_FL { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func Enable(path string) error { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var args = &fsverityEnableArg{} | ||
args.version = 1 | ||
args.hashAlgorithm = 1 | ||
|
||
// fsverity block size should be the minimum between the page size | ||
// and the file system block size | ||
// If neither value is retrieved successfully, set fsverity block size to the default value | ||
blockSize := unix.Getpagesize() | ||
|
||
s := unix.Stat_t{} | ||
serr := unix.Stat(path, &s) | ||
if serr == nil && int(s.Blksize) < blockSize { | ||
blockSize = int(s.Blksize) | ||
} | ||
|
||
if blockSize <= 0 { | ||
blockSize = defaultBlockSize | ||
} | ||
|
||
args.blockSize = uint32(blockSize) | ||
|
||
_, _, errno := unix.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(unix.FS_IOC_ENABLE_VERITY), uintptr(unsafe.Pointer(args))) | ||
if errno != 0 { | ||
return fmt.Errorf("enable fsverity failed: %w", errno) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//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 "fmt" | ||
|
||
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") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
content.Provider
needs to callFS_IOC_MEASURE_VERITY
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fsverity module will automatically verify the integrity of the data on every read operation of the enabled file. If fsverity detects corruption, the read operation will return an error. The case where we would need to call
FS_IOC_MEASURE_VERITY
manually is if we wanted to detect intentional, malicious corruption of the blob data. In that case, we would measure the data after fsverity is enabled on the blob, safely store the known-good digest, then when the blob data a is read by the provider, callFS_IOC_MEASURE_VERITY
again and compare the returned digest to the known-good value.I'd be happy to implement this if the malicious corruption of blob data is a case containerd should cover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it would be nice to cover malicious corruption.
Can be another PR.