Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aa30746

Browse files
committedJun 23, 2017
Make SCC with less capabilities more restrictive.
1 parent 64fa9d6 commit aa30746

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed
 

‎pkg/security/scc/byrestrictions.go

+33
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func pointValue(constraint *kapi.SecurityContextConstraints) int {
2828
// add points based on volume requests
2929
points += volumePointValue(constraint)
3030

31+
// add points based on capabilities
32+
points += capabilitiesPointValue(constraint)
33+
3134
// strategies in order of least restrictive to most restrictive
3235
switch constraint.SELinuxContext.Type {
3336
case kapi.SELinuxStrategyRunAsAny:
@@ -82,3 +85,33 @@ func volumePointValue(scc *kapi.SecurityContextConstraints) int {
8285
}
8386
return 0
8487
}
88+
89+
// hasCap checks for needle in haystack.
90+
func hasCap(needle kapi.Capability, haystack []kapi.Capability) bool {
91+
for _, c := range haystack {
92+
if needle == c {
93+
return true
94+
}
95+
}
96+
return false
97+
}
98+
99+
// capabilitiesPointValue returns a score based on the capabilities allowed,
100+
// added, or removed by the SCC.
101+
func capabilitiesPointValue(scc *kapi.SecurityContextConstraints) int {
102+
points := 500
103+
points += 30 * len(scc.DefaultAddCapabilities)
104+
if hasCap(kapi.CapabilityAll, scc.AllowedCapabilities) {
105+
points += 300
106+
} else {
107+
points += 10 * len(scc.AllowedCapabilities)
108+
}
109+
points -= 50 * len(scc.RequiredDropCapabilities)
110+
if (points > 1000) {
111+
return 1000
112+
} else if (points < 0) {
113+
return 0
114+
} else {
115+
return points
116+
}
117+
}

‎pkg/security/scc/byrestrictions_test.go

+60-5
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ func TestPointValue(t *testing.T) {
3939
// run through all combos of user strategy + seLinux strategy + priv
4040
for userStrategy, userStrategyPoints := range userStrategies {
4141
for seLinuxStrategy, seLinuxStrategyPoints := range seLinuxStrategies {
42-
expectedPoints := privilegedPoints + userStrategyPoints + seLinuxStrategyPoints
42+
expectedPoints := 500 + privilegedPoints + userStrategyPoints + seLinuxStrategyPoints
4343
scc := newSCC(true, seLinuxStrategy, userStrategy)
4444
actualPoints := pointValue(scc)
4545

4646
if actualPoints != expectedPoints {
4747
t.Errorf("privileged, user: %v, seLinux %v expected %d score but got %d", userStrategy, seLinuxStrategy, expectedPoints, actualPoints)
4848
}
4949

50-
expectedPoints = userStrategyPoints + seLinuxStrategyPoints
50+
expectedPoints = 500 + userStrategyPoints + seLinuxStrategyPoints
5151
scc = newSCC(false, seLinuxStrategy, userStrategy)
5252
actualPoints = pointValue(scc)
5353

@@ -57,12 +57,13 @@ func TestPointValue(t *testing.T) {
5757
}
5858
}
5959

60-
// sanity check to ensure volume score is added (specific volumes scores are tested below
60+
// sanity check to ensure volume and capabilities scores are added (specific volumes
61+
// and capabilities scores are tested below
6162
scc := newSCC(false, kapi.SELinuxStrategyMustRunAs, kapi.RunAsUserStrategyMustRunAs)
6263
scc.Volumes = []kapi.FSType{kapi.FSTypeHostPath}
6364
actualPoints := pointValue(scc)
64-
if actualPoints != 120000 { //10000 (SELinux) + 10000 (User) + 100000 (host path volume)
65-
t.Errorf("volume score was not added to the scc point value correctly!")
65+
if actualPoints != 120500 { //10000 (SELinux) + 10000 (User) + 100000 (host path volume) + 500 capabilities
66+
t.Errorf("volume score was not added to the scc point value correctly, got %d!", actualPoints)
6667
}
6768
}
6869

@@ -172,3 +173,57 @@ func TestVolumePointValue(t *testing.T) {
172173
}
173174
}
174175
}
176+
177+
func TestCapabilitiesPointValue(t *testing.T) {
178+
newSCC := func(def []kapi.Capability, allow []kapi.Capability, drop []kapi.Capability) *kapi.SecurityContextConstraints {
179+
return &kapi.SecurityContextConstraints{
180+
DefaultAddCapabilities: def,
181+
AllowedCapabilities: allow,
182+
RequiredDropCapabilities: drop,
183+
}
184+
}
185+
186+
tests := map[string]struct {
187+
scc *kapi.SecurityContextConstraints
188+
expectedPoints int
189+
}{
190+
"nothing specified": {
191+
scc: newSCC([]kapi.Capability{}, []kapi.Capability{}, []kapi.Capability{}),
Has a comment. Original line has a comment.
192+
expectedPoints: 500,
193+
},
194+
"default": {
195+
scc: newSCC([]kapi.Capability{"KILL", "MKNOD"},
196+
[]kapi.Capability{},
197+
[]kapi.Capability{}),
198+
expectedPoints: 560,
199+
},
200+
"allow": {
201+
scc: newSCC([]kapi.Capability{},
202+
[]kapi.Capability{"KILL", "MKNOD"},
203+
[]kapi.Capability{}),
204+
expectedPoints: 520,
205+
},
206+
"allow all": {
207+
scc: newSCC([]kapi.Capability{}, []kapi.Capability{"*"}, []kapi.Capability{}),
208+
expectedPoints: 800,
209+
},
210+
"drop": {
211+
scc: newSCC([]kapi.Capability{},
212+
[]kapi.Capability{},
213+
[]kapi.Capability{"KILL", "MKNOD"}),
214+
expectedPoints: 400,
215+
},
216+
"mixture": {
217+
scc: newSCC([]kapi.Capability{"SETUID", "SETGID"},
218+
[]kapi.Capability{"*"},
219+
[]kapi.Capability{"SYS_CHROOT"}),
220+
expectedPoints: 810,
221+
},
222+
}
223+
for k, v := range tests {
224+
actualPoints := capabilitiesPointValue(v.scc)
225+
if actualPoints != v.expectedPoints {
226+
t.Errorf("%s expected %d capability score but got %d", k, v.expectedPoints, actualPoints)
227+
}
228+
}
229+
}

0 commit comments

Comments
 (0)
Please sign in to comment.