Skip to content

Commit 40fa2d7

Browse files
committed
feat: add landlock (linux userspace sandbox feature)
1 parent 092aba5 commit 40fa2d7

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎cmd/websrv/main.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"compress/gzip"
55
"context"
66
"errors"
7+
"fmt"
78
"github.com/go-chi/chi/v5"
89
"github.com/go-chi/chi/v5/middleware"
10+
"github.com/landlock-lsm/go-landlock/landlock"
911
"github.com/ngergs/websrv/v3/internal/utils"
1012
"github.com/prometheus/client_golang/prometheus"
1113
"io/fs"
@@ -34,6 +36,9 @@ func main() {
3436
if err = setup(conf); err != nil {
3537
log.Fatal().Err(err).Msg("Error during initialization")
3638
}
39+
if err := setupLandlock(conf); err != nil {
40+
log.Fatal().Err(err).Msg("")
41+
}
3742
var wg sync.WaitGroup
3843
sigtermCtx := server.SigTermCtx(context.Background(), time.Duration(conf.ShutdownDelay)*time.Second)
3944
unzipfs, zipfs := initFs(conf)
@@ -170,3 +175,29 @@ func logErrors(errChan <-chan error) {
170175
}
171176
}
172177
}
178+
179+
// setupLandlock activates the linux landlock sandbox features on an best effort basis
180+
func setupLandlock(conf *config) error {
181+
llConf := landlock.V4.BestEffort()
182+
if err := llConf.RestrictPaths(landlock.RODirs(targetDir)); err != nil {
183+
return fmt.Errorf("error during landlock filesystem setup: %w", err)
184+
}
185+
ports := []uint16{conf.Port.Webserver}
186+
if conf.Health {
187+
ports = append(ports, conf.Port.Health)
188+
}
189+
if conf.H2C {
190+
ports = append(ports, conf.Port.H2c)
191+
}
192+
if conf.Metrics.Enabled {
193+
ports = append(ports, conf.Port.Metrics)
194+
}
195+
portRules := make([]landlock.Rule, len(ports))
196+
for i, port := range ports {
197+
portRules[i] = landlock.BindTCP(port)
198+
}
199+
if err := llConf.RestrictNet(portRules...); err != nil {
200+
return fmt.Errorf("error during landlock network setup: %w", err)
201+
}
202+
return nil
203+
}

‎go.mod‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/knadh/koanf/providers/file v0.1.0
1212
github.com/knadh/koanf/providers/structs v0.1.0
1313
github.com/knadh/koanf/v2 v2.1.1
14+
github.com/landlock-lsm/go-landlock v0.0.0-20240216195629-efb66220540a
1415
github.com/prometheus/client_golang v1.19.0
1516
github.com/rs/zerolog v1.32.0
1617
github.com/stretchr/testify v1.8.1
@@ -38,4 +39,5 @@ require (
3839
golang.org/x/text v0.14.0 // indirect
3940
google.golang.org/protobuf v1.33.0 // indirect
4041
gopkg.in/yaml.v3 v3.0.1 // indirect
42+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.69 // indirect
4143
)

‎go.sum‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3636
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
3737
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3838
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
39+
github.com/landlock-lsm/go-landlock v0.0.0-20240216195629-efb66220540a h1:dz+a1MiMQksVhejeZwqJuzPawYQBwug74J8PPtkLl9U=
40+
github.com/landlock-lsm/go-landlock v0.0.0-20240216195629-efb66220540a/go.mod h1:1NY/VPO8xm3hXw3f+M65z+PJDLUaZA5cu7OfanxoUzY=
3941
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4042
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
4143
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
@@ -90,3 +92,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
9092
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9193
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9294
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
95+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.69 h1:IdrOs1ZgwGw5CI+BH6GgVVlOt+LAXoPyh7enr8lfaXs=
96+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.69/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=

0 commit comments

Comments
 (0)