Skip to content

Commit f19aa2d

Browse files
author
Samuel Ortiz
committed
validate: Check that the given namespace path is a symlink
When checking if the provided networking namespace is the host one or not, we should first check if it's a symbolic link or not as in some cases we can use persistent networking namespace under e.g. /var/run/netns/. Signed-off-by: Samuel Ortiz <[email protected]>
1 parent 34f23cb commit f19aa2d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

libcontainer/configs/validate/validator.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ func (v *ConfigValidator) sysctl(config *configs.Config) error {
148148
return nil
149149
}
150150

151+
func isSymbolicLink(path string) (bool, error) {
152+
fi, err := os.Lstat(path)
153+
if err != nil {
154+
return false, err
155+
}
156+
157+
return fi.Mode()&os.ModeSymlink == os.ModeSymlink, nil
158+
}
159+
151160
// checkHostNs checks whether network sysctl is used in host namespace.
152161
func checkHostNs(sysctlConfig string, path string) error {
153162
var currentProcessNetns = "/proc/self/ns/net"
@@ -156,6 +165,19 @@ func checkHostNs(sysctlConfig string, path string) error {
156165
if err != nil {
157166
return fmt.Errorf("read soft link %q error", currentProcessNetns)
158167
}
168+
169+
// First check if the provided path is a symbolic link
170+
symLink, err := isSymbolicLink(path)
171+
if err != nil {
172+
return fmt.Errorf("could not check that %q is a symlink: %v", path, err)
173+
}
174+
175+
if symLink == false {
176+
// The provided namespace is not a symbolic link,
177+
// it is not the host namespace.
178+
return nil
179+
}
180+
159181
// readlink on the path provided in the struct
160182
destOfContainer, err := os.Readlink(path)
161183
if err != nil {

0 commit comments

Comments
 (0)