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

Compare mount by value instead of reference #413

Merged
merged 1 commit into from
May 7, 2024
Merged
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
5 changes: 3 additions & 2 deletions actions/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log"
"os"
"os/user"
"reflect"

"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -452,7 +453,7 @@ func (policy *Policy) AddProtector(protector *Protector) error {

// If the protector is on a different filesystem, we need to add a link
// to it on the policy's filesystem.
if policy.Context.Mount != protector.Context.Mount {
if !reflect.DeepEqual(policy.Context.Mount, protector.Context.Mount) {
log.Printf("policy on %s\n protector on %s\n", policy.Context.Mount, protector.Context.Mount)
ownerIfCreating, err := getOwnerOfMetadataForProtector(protector)
if err != nil {
Expand Down Expand Up @@ -525,7 +526,7 @@ func (policy *Policy) RemoveProtector(protectorDescriptor string) error {
func (policy *Policy) Apply(path string) error {
NymanRobin marked this conversation as resolved.
Show resolved Hide resolved
if pathMount, err := filesystem.FindMount(path); err != nil {
return err
} else if pathMount != policy.Context.Mount {
} else if !reflect.DeepEqual(pathMount, policy.Context.Mount) {
return &ErrDifferentFilesystem{policy.Context.Mount, pathMount}
}

Expand Down
19 changes: 19 additions & 0 deletions filesystem/mountpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -544,3 +545,21 @@ func BenchmarkLoadFirst(b *testing.B) {
}
}
}

// Test mount comparison by values instead of by reference,
// as the map mountsByDevice gets recreated on each load.
// This ensures that concurrent mounts work properly.
func TestMountComparison(t *testing.T) {
var mountinfo = `
15 0 259:3 / /home rw,relatime shared:1 - ext4 /dev/root rw,data=ordered
`
beginLoadMountInfoTest()
defer endLoadMountInfoTest()
loadMountInfoFromString(mountinfo)
firstMnt := mountForDevice("259:3")
loadMountInfoFromString(mountinfo)
secondMnt := mountForDevice("259:3")
if !reflect.DeepEqual(firstMnt, secondMnt) {
t.Errorf("Mount comparison does not work")
}
}
Loading