File tree Expand file tree Collapse file tree 2 files changed +60
-3
lines changed
libcontainer/configs/validate Expand file tree Collapse file tree 2 files changed +60
-3
lines changed Original file line number Diff line number Diff line change @@ -125,14 +125,36 @@ func (v *ConfigValidator) sysctl(config *configs.Config) error {
125125 }
126126 }
127127 if strings .HasPrefix (s , "net." ) {
128- if config .Namespaces .Contains (configs .NEWNET ) {
129- continue
130- } else {
128+ if ! config .Namespaces .Contains (configs .NEWNET ) {
131129 return fmt .Errorf ("sysctl %q is not allowed in the hosts network namespace" , s )
132130 }
131+ if path := config .Namespaces .PathOf (configs .NEWNET ); path != "" {
132+ if err := checkHostNs (s , path ); err != nil {
133+ return err
134+ }
135+ }
133136 }
134137 return fmt .Errorf ("sysctl %q is not in a separate kernel namespace" , s )
135138 }
136139
137140 return nil
138141}
142+
143+ // checkHostNs checks whether network sysctl is used in host namespace.
144+ func checkHostNs (sysctlConfig string , path string ) error {
145+ var currentProcessNetns = "/proc/self/ns/net"
146+ // readlink on the current processes network namespace
147+ destOfCurrentProcess , err := os .Readlink (currentProcessNetns )
148+ if err != nil {
149+ return fmt .Errorf ("read soft link %q error" , currentProcessNetns )
150+ }
151+ // readlink on the path provided in the struct
152+ destOfContainer , err := os .Readlink (path )
153+ if err != nil {
154+ return fmt .Errorf ("read soft link %q error" , path )
155+ }
156+ if destOfContainer == destOfCurrentProcess {
157+ return fmt .Errorf ("sysctl %q is not allowed in the hosts network namespace" , sysctlConfig )
158+ }
159+ return nil
160+ }
Original file line number Diff line number Diff line change @@ -201,3 +201,38 @@ func TestValidateSysctl(t *testing.T) {
201201 }
202202 }
203203}
204+
205+ func TestValidateSysctlWithSameNs (t * testing.T ) {
206+ config := & configs.Config {
207+ Rootfs : "/var" ,
208+ Sysctl : map [string ]string {"net.ctl" : "ctl" },
209+ Namespaces : configs .Namespaces (
210+ []configs.Namespace {
211+ {
212+ Type : configs .NEWNET ,
213+ Path : "/proc/self/ns/net" ,
214+ },
215+ },
216+ ),
217+ }
218+
219+ validator := validate .New ()
220+ err := validator .Validate (config )
221+ if err == nil {
222+ t .Error ("Expected error to occur but it was nil" )
223+ }
224+ }
225+
226+ func TestValidateSysctlWithoutNETNamespace (t * testing.T ) {
227+ config := & configs.Config {
228+ Rootfs : "/var" ,
229+ Sysctl : map [string ]string {"net.ctl" : "ctl" },
230+ Namespaces : []configs.Namespace {},
231+ }
232+
233+ validator := validate .New ()
234+ err := validator .Validate (config )
235+ if err == nil {
236+ t .Error ("Expected error to occur but it was nil" )
237+ }
238+ }
You can’t perform that action at this time.
0 commit comments