Skip to content

Commit

Permalink
Compare mount by value instead of reference
Browse files Browse the repository at this point in the history
This has to be since the mounts are reloaded
each time a mount is added. In case of two
mounts mounting at the same time there will
be a race condition for applying policy.

Signed-off-by: NymanRobin <robin.nyman@est.tech>
  • Loading branch information
NymanRobin committed May 2, 2024
1 parent 54745f1 commit 068b9f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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 {
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")
}
}

0 comments on commit 068b9f8

Please sign in to comment.