Skip to content

Commit 6a698a6

Browse files
committed
Add unit test for SetupLandlock
Signed-off-by: Kailun Qin <[email protected]>
1 parent 6f64c97 commit 6a698a6

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

libcontainer/specconv/spec_linux_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package specconv
22

33
import (
44
"os"
5+
"reflect"
56
"strings"
67
"testing"
78

89
dbus "github.com/godbus/dbus/v5"
10+
ll "github.com/landlock-lsm/go-landlock/landlock"
911
"github.com/opencontainers/runc/libcontainer/configs"
1012
"github.com/opencontainers/runc/libcontainer/configs/validate"
1113
"github.com/opencontainers/runc/libcontainer/devices"
@@ -185,6 +187,103 @@ func TestSetupSeccompWrongArchitecture(t *testing.T) {
185187
}
186188
}
187189

190+
func TestSetupLandlock(t *testing.T) {
191+
conf := &specs.Landlock{
192+
Ruleset: &specs.LandlockRuleset{
193+
HandledAccessFS: []specs.LandlockFSAction{
194+
specs.FSActExecute,
195+
specs.FSActWriteFile,
196+
specs.FSActReadFile,
197+
specs.FSActReadDir,
198+
specs.FSActRemoveDir,
199+
specs.FSActRemoveFile,
200+
specs.FSActMakeChar,
201+
specs.FSActMakeDir,
202+
specs.FSActMakeReg,
203+
specs.FSActMakeSock,
204+
specs.FSActMakeFifo,
205+
specs.FSActMakeBlock,
206+
specs.FSActMakeSym,
207+
},
208+
},
209+
Rules: &specs.LandlockRules{
210+
PathBeneath: []specs.LandlockRulePathBeneath{
211+
{
212+
AllowedAccess: []specs.LandlockFSAction{
213+
specs.FSActExecute,
214+
specs.FSActReadFile,
215+
specs.FSActReadDir,
216+
},
217+
Paths: []string{
218+
"/usr",
219+
"/bin",
220+
},
221+
},
222+
{
223+
AllowedAccess: []specs.LandlockFSAction{
224+
specs.FSActExecute,
225+
specs.FSActWriteFile,
226+
specs.FSActReadFile,
227+
specs.FSActRemoveFile,
228+
specs.FSActMakeChar,
229+
specs.FSActMakeReg,
230+
specs.FSActMakeSock,
231+
specs.FSActMakeFifo,
232+
specs.FSActMakeBlock,
233+
specs.FSActMakeSym,
234+
},
235+
Paths: []string{
236+
"/tmp",
237+
},
238+
},
239+
},
240+
},
241+
DisableBestEffort: false,
242+
}
243+
244+
landlock, err := SetupLandlock(conf)
245+
if err != nil {
246+
t.Errorf("Couldn't create Landlock config: %v", err)
247+
}
248+
249+
// Execute | WriteFile | ReadFile | ReadDir | RemoveDir | RemoveFile | MakeChar |
250+
// MakeDir | MakeReg | MakeSock | MakeFifo | MakeBlock | MakeSym
251+
expectedRulesetAccess := ll.AccessFSSet(0x1FFF)
252+
ruleset := landlock.Ruleset
253+
if ruleset.HandledAccessFS != expectedRulesetAccess {
254+
t.Errorf("Expected ruleset not found, expected %v, got: %v",
255+
expectedRulesetAccess, ruleset.HandledAccessFS)
256+
}
257+
258+
pathRules := landlock.Rules.PathBeneath
259+
260+
pathRulesLength := len(pathRules)
261+
if pathRulesLength != 2 {
262+
t.Errorf("Expected 2 path beneath rules, got :%d", pathRulesLength)
263+
}
264+
265+
expectedPathRulesAccess := []configs.RulePathBeneath{
266+
{
267+
// Execute | ReadFile | ReadDir
268+
AllowedAccess: 0xD,
269+
Paths: []string{"/usr", "/bin"},
270+
},
271+
{
272+
// Execute | WriteFile | ReadFile | RemoveFile | MakeChar | MakeReg | MakeSock | MakeFifo |
273+
// MakeBlock | MakeSym
274+
AllowedAccess: 0x1F67,
275+
Paths: []string{"/tmp"},
276+
},
277+
}
278+
279+
for i, rule := range pathRules {
280+
if !reflect.DeepEqual(*rule, expectedPathRulesAccess[i]) {
281+
t.Errorf("Wrong rule conversion for the rule %d under test, expected %v, got: %v",
282+
i, expectedPathRulesAccess[i], rule)
283+
}
284+
}
285+
}
286+
188287
func TestSetupSeccomp(t *testing.T) {
189288
errnoRet := uint(55)
190289
conf := &specs.LinuxSeccomp{

0 commit comments

Comments
 (0)