Skip to content

Commit d8f8f33

Browse files
committed
wip poc
1 parent 835591b commit d8f8f33

30 files changed

+2817
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/cyphar/filepath-securejoin v0.4.1
1010
github.com/docker/go-units v0.5.0
1111
github.com/godbus/dbus/v5 v5.1.0
12+
github.com/landlock-lsm/go-landlock v0.0.0-20250303204525-1544bccde3a3
1213
github.com/moby/sys/capability v0.4.0
1314
github.com/moby/sys/mountinfo v0.7.2
1415
github.com/moby/sys/user v0.4.0
@@ -31,4 +32,5 @@ require (
3132
github.com/cilium/ebpf v0.17.3 // indirect
3233
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
3334
github.com/russross/blackfriday/v2 v2.1.0 // indirect
35+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 // indirect
3436
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3030
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
3131
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3232
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
33+
github.com/landlock-lsm/go-landlock v0.0.0-20250303204525-1544bccde3a3 h1:zcMi8R8vP0WrrXlFMNUBpDy/ydo3sTnCcUPowq1XmSc=
34+
github.com/landlock-lsm/go-landlock v0.0.0-20250303204525-1544bccde3a3/go.mod h1:RSub3ourNF8Hf+swvw49Catm3s7HVf4hzdFxDUnEzdA=
3335
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
3436
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
3537
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
@@ -93,3 +95,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
9395
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9496
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9597
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
98+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 h1:HsB2G/rEQiYyo1bGoQqHZ/Bvd6x1rERQTNdPr1FyWjI=
99+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=

libcontainer/configs/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ type Config struct {
238238

239239
// ExecCPUAffinity is CPU affinity for a non-init process to be run in the container.
240240
ExecCPUAffinity *CPUAffinity `json:"exec_cpu_affinity,omitempty"`
241+
242+
// Landlock contains configuration for Landlock LSM restrictions.
243+
Landlock *LandlockConfig `json:"landlock,omitempty"`
241244
}
242245

243246
// Scheduler is based on the Linux sched_setattr(2) syscall.

libcontainer/configs/landlock.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package configs
2+
3+
type LandlockConfig struct {
4+
Mode string `json:"mode"` // "enforce"|"best-effort"
5+
RoDirs []string `json:"roDirs"`
6+
RwDirs []string `json:"rwDirs"`
7+
WithRefer []string `json:"withRefer"` // dirs that need cross-dir rename/link
8+
IoctlDev []string `json:"ioctlDev"` // device paths requiring ioctl
9+
BindTCP []uint16 `json:"bindTCP"`
10+
ConnectTCP []uint16 `json:"connectTCP"`
11+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package landlock
2+
3+
import (
4+
"fmt"
5+
6+
ll "github.com/landlock-lsm/go-landlock/landlock"
7+
"github.com/opencontainers/runc/libcontainer/configs"
8+
)
9+
10+
func Apply(cfg *configs.LandlockConfig) error {
11+
if cfg == nil {
12+
return nil
13+
}
14+
15+
// Choose ABI + fallback policy
16+
var c ll.Config
17+
switch cfg.Mode {
18+
case "best-effort":
19+
c = ll.V5.BestEffort() // V5 covers FS+NET+ioctl-dev; will step down automatically
20+
case "enforce":
21+
c = ll.V5 // or ll.V6 once you use scopes
22+
default:
23+
c = ll.V5.BestEffort()
24+
}
25+
26+
var rules []ll.Rule
27+
28+
if len(cfg.RoDirs) > 0 {
29+
rules = append(rules, ll.RODirs(cfg.RoDirs...))
30+
}
31+
if len(cfg.RwDirs) > 0 {
32+
rules = append(rules, ll.RWDirs(cfg.RwDirs...))
33+
}
34+
for _, d := range cfg.WithRefer {
35+
rules = append(rules, ll.RWDirs(d).WithRefer())
36+
}
37+
for _, d := range cfg.IoctlDev {
38+
rules = append(rules, ll.RODirs(d).WithIoctlDev())
39+
}
40+
41+
for _, p := range cfg.BindTCP {
42+
rules = append(rules, ll.BindTCP(p))
43+
}
44+
for _, p := range cfg.ConnectTCP {
45+
rules = append(rules, ll.ConnectTCP(p))
46+
}
47+
48+
// This sets PR_SET_NO_NEW_PRIVS as needed and then restricts self.
49+
// The library internally queries ABI and degrades if BestEffort().
50+
if err := c.Restrict(rules...); err != nil {
51+
if cfg.Mode == "enforce" {
52+
return fmt.Errorf("landlock enforce failed: %w", err)
53+
}
54+
}
55+
return nil
56+
}

libcontainer/standard_init_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/opencontainers/runc/libcontainer/apparmor"
1616
"github.com/opencontainers/runc/libcontainer/configs"
1717
"github.com/opencontainers/runc/libcontainer/keys"
18+
"github.com/opencontainers/runc/libcontainer/landlock"
1819
"github.com/opencontainers/runc/libcontainer/seccomp"
1920
"github.com/opencontainers/runc/libcontainer/system"
2021
"github.com/opencontainers/runc/libcontainer/utils"
@@ -238,6 +239,12 @@ func (l *linuxStandardInit) Init() error {
238239
}
239240
}
240241

242+
if l.config.Config.Landlock != nil {
243+
if err := landlock.Apply(l.config.Config.Landlock); err != nil {
244+
return fmt.Errorf("failed to apply landlock restrictions: %w", err)
245+
}
246+
}
247+
241248
// Set personality if specified.
242249
if l.config.Config.Personality != nil {
243250
if err := setupPersonality(l.config.Config); err != nil {

vendor/github.com/landlock-lsm/go-landlock/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/landlock-lsm/go-landlock/landlock/abi_versions.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/landlock-lsm/go-landlock/landlock/accessfs.go

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/landlock-lsm/go-landlock/landlock/accessnet.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)