Skip to content

Commit

Permalink
security : check /proc is mounted with secure options
Browse files Browse the repository at this point in the history
Signed-off-by: Shahriyar Jalayeri <[email protected]>
  • Loading branch information
shjala committed Nov 6, 2023
1 parent b30a2c3 commit d630be0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
36 changes: 36 additions & 0 deletions tests/sec/remote.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sec_test

import (
"encoding/json"
"fmt"
"io"
"os"
Expand All @@ -11,6 +12,12 @@ import (
"github.com/lf-edge/eden/pkg/utils"
)

type mount struct {
Point string `json:"point"`
Type string `json:"type"`
Options string `json:"options"`
}

type remoteNode struct {
openEVEC *openevec.OpenEVEC
}
Expand Down Expand Up @@ -86,3 +93,32 @@ func (node *remoteNode) readFile(fileName string) ([]byte, error) {
command := fmt.Sprintf("cat %s", fileName)
return node.runCommand(command)
}

func (node *remoteNode) getMountPoints(mtype string) ([]mount, error) {
mount_command := "mount -l"

Check failure on line 98 in tests/sec/remote.go

View workflow job for this annotation

GitHub Actions / yetus

golangcilint: var-naming: don't use underscores in Go names; var mount_command should be mountCommand (revive)

Check failure on line 98 in tests/sec/remote.go

View workflow job for this annotation

GitHub Actions / yetus

revive: don't use underscores in Go names; var mount_command should be mountCommand https://revive.run/r#var-naming
if mtype != "" {
mount_command = fmt.Sprintf("mount -l -t %s", mtype)
}

command := mount_command + ` | awk '
BEGIN { print " [ "}
{
printf " %s {\"point\": \"%s\", \"type\": \"%s\", \"options\": \"%s\"}", separator, $3, $5, $6;
separator = ",";
}
END { print " ] " }
'`

out, err := node.runCommand(command)
if err != nil {
return nil, err
}

var mounts []mount
if err := json.Unmarshal(out, &mounts); err != nil {
return nil, err

}

return mounts, nil
}
31 changes: 30 additions & 1 deletion tests/sec/sec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func TestMain(m *testing.M) {
func TestAppArmorEnabled(t *testing.T) {

Check failure on line 93 in tests/sec/sec_test.go

View workflow job for this annotation

GitHub Actions / yetus

golangcilint: Function TestAppArmorEnabled missing the call to method parallel (paralleltest)
log.Println("TestAppArmorEnabled started")
defer log.Println("TestAppArmorEnabled finished")
t.Parallel()

edgeNode := tc.GetEdgeNode(tc.WithTest(t))
tc.WaitForState(edgeNode, 60)
Expand All @@ -108,3 +107,33 @@ func TestAppArmorEnabled(t *testing.T) {
t.Fatal("AppArmor is not enabled")
}
}

func TestCheckProcMountOptions(t *testing.T) {

Check failure on line 111 in tests/sec/sec_test.go

View workflow job for this annotation

GitHub Actions / yetus

golangcilint: Function TestCheckProcMountOptions missing the call to method parallel (paralleltest)
log.Println("TestCheckProcMountOptions started")
defer log.Println("TestCheckProcMountOptions finished")

edgeNode := tc.GetEdgeNode(tc.WithTest(t))
tc.WaitForState(edgeNode, 60)

procMounts, err := rnode.getMountPoints("proc")
if err != nil {
t.Fatal(err)
}

for _, mount := range procMounts {
// check if mount options contains nosuid, nodev and noexec, hidepid=2
if !strings.Contains(mount.Options, "nosuid") {
t.Fatalf("Mount options for /proc on %s doesn't contain nosuid", mount.Point)
}
if !strings.Contains(mount.Options, "nodev") {
t.Fatalf("Mount options for /proc on %s doesn't contain nodev", mount.Point)
}
if !strings.Contains(mount.Options, "noexec") {
t.Fatalf("Mount options for /proc on %s doesn't contain noexec", mount.Point)
}
if !strings.Contains(mount.Options, "hidepid=2") {
// TODO: set hidepid=2 and make this a fatal error

Check failure on line 135 in tests/sec/sec_test.go

View workflow job for this annotation

GitHub Actions / yetus

golangcilint: tests/sec/sec_test.go:135: Line contains TODO/BUG/FIXME: "TODO: set hidepid=2 and make this a fata..." (godox)
t.Logf("[!!!WARNING!!!] Mount options for /proc on %s doesn't contain hidepid=2", mount.Point)
}
}
}

0 comments on commit d630be0

Please sign in to comment.