@@ -7,9 +7,12 @@ import (
77 "os"
88 "strings"
99
10+ "github.com/opencontainers/runc/libcontainer/configs"
1011 "github.com/syndtr/gocapability/capability"
1112)
1213
14+ const allCapabilityTypes = capability .CAPS | capability .BOUNDS | capability .AMBS
15+
1316var capabilityMap map [string ]capability.Cap
1417
1518func init () {
@@ -28,40 +31,84 @@ func init() {
2831 }
2932}
3033
31- func newCapWhitelist (caps []string ) (* whitelist , error ) {
32- l := []capability.Cap {}
33- for _ , c := range caps {
34+ func newContainerCapList (capConfig * configs.Capabilities ) (* containerCapabilities , error ) {
35+ bounding := []capability.Cap {}
36+ for _ , c := range capConfig .Bounding {
37+ v , ok := capabilityMap [c ]
38+ if ! ok {
39+ return nil , fmt .Errorf ("unknown capability %q" , c )
40+ }
41+ bounding = append (bounding , v )
42+ }
43+ effective := []capability.Cap {}
44+ for _ , c := range capConfig .Effective {
45+ v , ok := capabilityMap [c ]
46+ if ! ok {
47+ return nil , fmt .Errorf ("unknown capability %q" , c )
48+ }
49+ effective = append (effective , v )
50+ }
51+ inheritable := []capability.Cap {}
52+ for _ , c := range capConfig .Inheritable {
53+ v , ok := capabilityMap [c ]
54+ if ! ok {
55+ return nil , fmt .Errorf ("unknown capability %q" , c )
56+ }
57+ inheritable = append (inheritable , v )
58+ }
59+ permitted := []capability.Cap {}
60+ for _ , c := range capConfig .Permitted {
61+ v , ok := capabilityMap [c ]
62+ if ! ok {
63+ return nil , fmt .Errorf ("unknown capability %q" , c )
64+ }
65+ permitted = append (permitted , v )
66+ }
67+ ambient := []capability.Cap {}
68+ for _ , c := range capConfig .Ambient {
3469 v , ok := capabilityMap [c ]
3570 if ! ok {
3671 return nil , fmt .Errorf ("unknown capability %q" , c )
3772 }
38- l = append (l , v )
73+ ambient = append (ambient , v )
3974 }
4075 pid , err := capability .NewPid (os .Getpid ())
4176 if err != nil {
4277 return nil , err
4378 }
44- return & whitelist {
45- keep : l ,
46- pid : pid ,
79+ return & containerCapabilities {
80+ bounding : bounding ,
81+ effective : effective ,
82+ inheritable : inheritable ,
83+ permitted : permitted ,
84+ ambient : ambient ,
85+ pid : pid ,
4786 }, nil
4887}
4988
50- type whitelist struct {
51- pid capability.Capabilities
52- keep []capability.Cap
89+ type containerCapabilities struct {
90+ pid capability.Capabilities
91+ bounding []capability.Cap
92+ effective []capability.Cap
93+ inheritable []capability.Cap
94+ permitted []capability.Cap
95+ ambient []capability.Cap
5396}
5497
55- // dropBoundingSet drops the capability bounding set to those specified in the whitelist.
56- func (w * whitelist ) dropBoundingSet () error {
57- w .pid .Clear (capability .BOUNDS )
58- w .pid .Set (capability .BOUNDS , w . keep ... )
59- return w .pid .Apply (capability .BOUNDS )
98+ // ApplyBoundingSet sets the capability bounding set to those specified in the whitelist.
99+ func (c * containerCapabilities ) ApplyBoundingSet () error {
100+ c .pid .Clear (capability .BOUNDS )
101+ c .pid .Set (capability .BOUNDS , c . bounding ... )
102+ return c .pid .Apply (capability .BOUNDS )
60103}
61104
62- // drop drops all capabilities for the current process except those specified in the whitelist.
63- func (w * whitelist ) drop () error {
64- w .pid .Clear (allCapabilityTypes )
65- w .pid .Set (allCapabilityTypes , w .keep ... )
66- return w .pid .Apply (allCapabilityTypes )
105+ // Apply sets all the capabilities for the current process in the config.
106+ func (c * containerCapabilities ) ApplyCaps () error {
107+ c .pid .Clear (allCapabilityTypes )
108+ c .pid .Set (capability .BOUNDS , c .bounding ... )
109+ c .pid .Set (capability .PERMITTED , c .permitted ... )
110+ c .pid .Set (capability .INHERITABLE , c .inheritable ... )
111+ c .pid .Set (capability .EFFECTIVE , c .effective ... )
112+ c .pid .Set (capability .AMBIENT , c .ambient ... )
113+ return c .pid .Apply (allCapabilityTypes )
67114}
0 commit comments