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

sanity: use proper SELinux context when mounting squashfs #6652

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion sanity/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/squashfs"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/selinux"
)

func init() {
Expand Down Expand Up @@ -96,7 +98,14 @@ func checkSquashfsMount() error {
if err != nil {
return err
}
cmd := exec.Command("mount", "-t", fstype, tmpSquashfsFile.Name(), tmpMountDir)
options := []string{"-t", fstype}
if release.SELinuxLevel() != release.NoSELinux {
if ctx := selinux.SnapMountContext(); ctx != "" {
options = append(options, "-o", "context="+ctx)
}
}
options = append(options, tmpSquashfsFile.Name(), tmpMountDir)
cmd := exec.Command("mount", options...)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cannot mount squashfs image using %q: %v", fstype, osutil.OutputErr(output, err))
Expand Down
27 changes: 27 additions & 0 deletions sanity/squashfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
. "gopkg.in/check.v1"

"github.com/snapcore/snapd/osutil/squashfs"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/sanity"
"github.com/snapcore/snapd/testutil"
)
Expand Down Expand Up @@ -93,3 +94,29 @@ func (s *sanitySuite) TestCheckSquashfsMountWrongContent(c *C) {
c.Check(mockMount.Calls(), HasLen, 1)
c.Check(mockUmount.Calls(), HasLen, 1)
}

func (s *sanitySuite) TestCheckSquashfsMountSELinuxContext(c *C) {
restore := squashfs.MockUseFuse(false)
defer restore()

mockMount := testutil.MockCommand(c, "mount", "echo 'mock ran'")
defer mockMount.Restore()

mockUmount := testutil.MockCommand(c, "umount", "")
defer mockUmount.Restore()

mockSELinux := release.MockSELinuxIsEnabled(func() (bool, error) { return true, nil })
defer mockSELinux()

err := sanity.CheckSquashfsMount()
c.Assert(err, ErrorMatches, `squashfs mount returned no err but canary file cannot be read`)

c.Check(mockMount.Calls(), HasLen, 1)
c.Check(mockUmount.Calls(), HasLen, 1)
squashfsFile := mockMount.Calls()[0][5]
mountPoint := mockMount.Calls()[0][6]

c.Check(mockMount.Calls(), DeepEquals, [][]string{
{"mount", "-t", "squashfs", "-o", "context=system_u:object_r:snappy_snap_t:s0", squashfsFile, mountPoint},
})
}