| 1 | // Copyright 2016-2018 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package ns_test |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "sync" |
| 23 | |
| 24 | . "github.com/onsi/ginkgo/v2" |
| 25 | . "github.com/onsi/gomega" |
| 26 | "golang.org/x/sys/unix" |
| 27 | |
| 28 | "github.com/containernetworking/plugins/pkg/ns" |
| 29 | "github.com/containernetworking/plugins/pkg/testutils" |
| 30 | ) |
| 31 | |
| 32 | func getInodeCurNetNS() (uint64, error) { |
| 33 | curNS, err := ns.GetCurrentNS() |
| 34 | if err != nil { |
| 35 | return 0, err |
| 36 | } |
| 37 | defer curNS.Close() |
| 38 | return getInodeNS(curNS) |
| 39 | } |
| 40 | |
| 41 | func getInodeNS(netns ns.NetNS) (uint64, error) { |
| 42 | return getInodeFd(int(netns.Fd())) |
| 43 | } |
| 44 | |
| 45 | func getInode(path string) (uint64, error) { |
| 46 | file, err := os.Open(path) |
| 47 | if err != nil { |
| 48 | return 0, err |
| 49 | } |
| 50 | defer file.Close() |
| 51 | return getInodeFd(int(file.Fd())) |
| 52 | } |
| 53 | |
| 54 | func getInodeFd(fd int) (uint64, error) { |
| 55 | stat := &unix.Stat_t{} |
| 56 | err := unix.Fstat(fd, stat) |
| 57 | return stat.Ino, err |
| 58 | } |
| 59 | |
| 60 | var _ = Describe("Linux namespace operations", func() { |
| 61 | Describe("WithNetNS", func() { |
| 62 | var ( |
| 63 | originalNetNS ns.NetNS |
| 64 | targetNetNS ns.NetNS |
| 65 | ) |
| 66 | |
| 67 | BeforeEach(func() { |
| 68 | var err error |
| 69 | |
| 70 | originalNetNS, err = testutils.NewNS() |
| 71 | Expect(err).NotTo(HaveOccurred()) |
| 72 | |
| 73 | targetNetNS, err = testutils.NewNS() |
| 74 | Expect(err).NotTo(HaveOccurred()) |
| 75 | }) |
| 76 | |
| 77 | AfterEach(func() { |
| 78 | targetNetNS.Close() |
| 79 | originalNetNS.Close() |
| 80 | |
| 81 | Expect(testutils.UnmountNS(targetNetNS)).To(Succeed()) |
| 82 | Expect(testutils.UnmountNS(originalNetNS)).To(Succeed()) |
| 83 | }) |
| 84 | |
| 85 | It("executes the callback within the target network namespace", func() { |
| 86 | expectedInode, err := getInodeNS(targetNetNS) |
| 87 | Expect(err).NotTo(HaveOccurred()) |
| 88 | |
| 89 | err = targetNetNS.Do(func(ns.NetNS) error { |
| 90 | defer GinkgoRecover() |
| 91 | |
| 92 | actualInode, err := getInodeCurNetNS() |
| 93 | Expect(err).NotTo(HaveOccurred()) |
| 94 | Expect(actualInode).To(Equal(expectedInode)) |
| 95 | return nil |
| 96 | }) |
| 97 | Expect(err).NotTo(HaveOccurred()) |
| 98 | }) |
| 99 | |
| 100 | It("provides the original namespace as the argument to the callback", func() { |
| 101 | // Ensure we start in originalNetNS |
| 102 | err := originalNetNS.Do(func(ns.NetNS) error { |
| 103 | defer GinkgoRecover() |
| 104 | |
| 105 | origNSInode, err := getInodeNS(originalNetNS) |
| 106 | Expect(err).NotTo(HaveOccurred()) |
| 107 | |
| 108 | err = targetNetNS.Do(func(hostNS ns.NetNS) error { |
| 109 | defer GinkgoRecover() |
| 110 | |
| 111 | hostNSInode, err := getInodeNS(hostNS) |
| 112 | Expect(err).NotTo(HaveOccurred()) |
| 113 | Expect(hostNSInode).To(Equal(origNSInode)) |
| 114 | return nil |
| 115 | }) |
| 116 | Expect(err).NotTo(HaveOccurred()) |
| 117 | return nil |
| 118 | }) |
| 119 | Expect(err).NotTo(HaveOccurred()) |
| 120 | }) |
| 121 | |
| 122 | Context("when called concurrently", func() { |
| 123 | It("provides the original namespace as the argument to the callback", func() { |
| 124 | concurrency := 200 |
| 125 | origNS, err := ns.GetCurrentNS() |
| 126 | Expect(err).NotTo(HaveOccurred()) |
| 127 | origNSInode, err := getInodeNS(origNS) |
| 128 | Expect(err).NotTo(HaveOccurred()) |
| 129 | |
| 130 | var wg sync.WaitGroup |
| 131 | wg.Add(concurrency) |
| 132 | for i := 0; i < concurrency; i++ { |
| 133 | go func() { |
| 134 | defer wg.Done() |
| 135 | targetNetNS.Do(func(hostNS ns.NetNS) error { |
| 136 | defer GinkgoRecover() |
| 137 | |
| 138 | hostNSInode, err := getInodeNS(hostNS) |
| 139 | Expect(err).NotTo(HaveOccurred()) |
| 140 | Expect(hostNSInode).To(Equal(origNSInode)) |
| 141 | return nil |
| 142 | }) |
| 143 | }() |
| 144 | } |
| 145 | wg.Wait() |
| 146 | }) |
| 147 | }) |
| 148 | |
| 149 | Context("when the callback returns an error", func() { |
| 150 | It("restores the calling thread to the original namespace before returning", func() { |
| 151 | err := originalNetNS.Do(func(ns.NetNS) error { |
| 152 | defer GinkgoRecover() |
| 153 | |
| 154 | preTestInode, err := getInodeCurNetNS() |
| 155 | Expect(err).NotTo(HaveOccurred()) |
| 156 | |
| 157 | _ = targetNetNS.Do(func(ns.NetNS) error { |
| 158 | return errors.New("potato") |
| 159 | }) |
| 160 | |
| 161 | postTestInode, err := getInodeCurNetNS() |
| 162 | Expect(err).NotTo(HaveOccurred()) |
| 163 | Expect(postTestInode).To(Equal(preTestInode)) |
| 164 | return nil |
| 165 | }) |
| 166 | Expect(err).NotTo(HaveOccurred()) |
| 167 | }) |
| 168 | |
| 169 | It("returns the error from the callback", func() { |
| 170 | err := targetNetNS.Do(func(ns.NetNS) error { |
| 171 | return errors.New("potato") |
| 172 | }) |
| 173 | Expect(err).To(MatchError("potato")) |
| 174 | }) |
| 175 | }) |
| 176 | |
| 177 | Describe("validating inode mapping to namespaces", func() { |
| 178 | It("checks that different namespaces have different inodes", func() { |
| 179 | origNSInode, err := getInodeNS(originalNetNS) |
| 180 | Expect(err).NotTo(HaveOccurred()) |
| 181 | |
| 182 | testNsInode, err := getInodeNS(targetNetNS) |
| 183 | Expect(err).NotTo(HaveOccurred()) |
| 184 | |
| 185 | Expect(testNsInode).NotTo(Equal(uint64(0))) |
| 186 | Expect(testNsInode).NotTo(Equal(origNSInode)) |
| 187 | }) |
| 188 | |
| 189 | It("should not leak a closed netns onto any threads in the process", func() { |
| 190 | By("creating a new netns") |
| 191 | createdNetNS, err := testutils.NewNS() |
| 192 | Expect(err).NotTo(HaveOccurred()) |
| 193 | |
| 194 | By("discovering the inode of the created netns") |
| 195 | createdNetNSInode, err := getInodeNS(createdNetNS) |
| 196 | Expect(err).NotTo(HaveOccurred()) |
| 197 | createdNetNS.Close() |
| 198 | Expect(testutils.UnmountNS(createdNetNS)).NotTo(HaveOccurred()) |
| 199 | |
| 200 | By("comparing against the netns inode of every thread in the process") |
| 201 | for _, netnsPath := range allNetNSInCurrentProcess() { |
| 202 | netnsInode, err := getInode(netnsPath) |
| 203 | if !os.IsNotExist(err) { |
| 204 | Expect(err).NotTo(HaveOccurred()) |
| 205 | } |
| 206 | Expect(netnsInode).NotTo(Equal(createdNetNSInode)) |
| 207 | } |
| 208 | }) |
| 209 | |
| 210 | It("fails when the path is not a namespace", func() { |
| 211 | tempFile, err := os.CreateTemp("", "nstest") |
| 212 | Expect(err).NotTo(HaveOccurred()) |
| 213 | defer tempFile.Close() |
| 214 | |
| 215 | nspath := tempFile.Name() |
| 216 | defer os.Remove(nspath) |
| 217 | |
| 218 | _, err = ns.GetNS(nspath) |
| 219 | Expect(err).To(HaveOccurred()) |
| 220 | Expect(err).To(BeAssignableToTypeOf(ns.NSPathNotNSErr{})) |
| 221 | Expect(err).NotTo(BeAssignableToTypeOf(ns.NSPathNotExistErr{})) |
| 222 | }) |
| 223 | }) |
| 224 | |
| 225 | Describe("closing a network namespace", func() { |
| 226 | It("should prevent further operations", func() { |
| 227 | createdNetNS, err := testutils.NewNS() |
| 228 | defer testutils.UnmountNS(createdNetNS) |
| 229 | Expect(err).NotTo(HaveOccurred()) |
| 230 | |
| 231 | err = createdNetNS.Close() |
| 232 | Expect(err).NotTo(HaveOccurred()) |
| 233 | |
| 234 | err = createdNetNS.Do(func(ns.NetNS) error { return nil }) |
| 235 | Expect(err).To(HaveOccurred()) |
| 236 | |
| 237 | err = createdNetNS.Set() |
| 238 | Expect(err).To(HaveOccurred()) |
| 239 | }) |
| 240 | |
| 241 | It("should only work once", func() { |
| 242 | createdNetNS, err := testutils.NewNS() |
| 243 | Expect(err).NotTo(HaveOccurred()) |
| 244 | defer testutils.UnmountNS(createdNetNS) |
| 245 | |
| 246 | err = createdNetNS.Close() |
| 247 | Expect(err).NotTo(HaveOccurred()) |
| 248 | |
| 249 | err = createdNetNS.Close() |
| 250 | Expect(err).To(HaveOccurred()) |
| 251 | }) |
| 252 | }) |
| 253 | }) |
| 254 | |
| 255 | Describe("IsNSorErr", func() { |
| 256 | It("should detect a namespace", func() { |
| 257 | createdNetNS, err := testutils.NewNS() |
| 258 | Expect(err).NotTo(HaveOccurred()) |
| 259 | defer testutils.UnmountNS(createdNetNS) |
| 260 | err = ns.IsNSorErr(createdNetNS.Path()) |
| 261 | Expect(err).NotTo(HaveOccurred()) |
| 262 | }) |
| 263 | |
| 264 | It("should refuse other paths", func() { |
| 265 | tempFile, err := os.CreateTemp("", "nstest") |
| 266 | Expect(err).NotTo(HaveOccurred()) |
| 267 | defer tempFile.Close() |
| 268 | |
| 269 | nspath := tempFile.Name() |
| 270 | defer os.Remove(nspath) |
| 271 | |
| 272 | err = ns.IsNSorErr(nspath) |
| 273 | Expect(err).To(HaveOccurred()) |
| 274 | Expect(err).To(BeAssignableToTypeOf(ns.NSPathNotNSErr{})) |
| 275 | Expect(err).NotTo(BeAssignableToTypeOf(ns.NSPathNotExistErr{})) |
| 276 | }) |
| 277 | |
| 278 | It("should error on non-existing paths", func() { |
| 279 | err := ns.IsNSorErr("/tmp/IDoNotExist") |
| 280 | Expect(err).To(HaveOccurred()) |
| 281 | Expect(err).To(BeAssignableToTypeOf(ns.NSPathNotExistErr{})) |
| 282 | Expect(err).NotTo(BeAssignableToTypeOf(ns.NSPathNotNSErr{})) |
| 283 | }) |
| 284 | }) |
| 285 | }) |
| 286 | |
| 287 | func allNetNSInCurrentProcess() []string { |
| 288 | pid := unix.Getpid() |
| 289 | paths, err := filepath.Glob(fmt.Sprintf("/proc/%d/task/*/ns/net", pid)) |
| 290 | Expect(err).NotTo(HaveOccurred()) |
| 291 | return paths |
| 292 | } |