Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH net-next] selftests: net: py: preserve existing namespaces on creation failure
@ 2026-11-22 15:48 Jingjing Xu
  2026-12-01  3:00 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: Jingjing Xu @ 2026-11-22 15:48 UTC (permalink / raw)
  To: netdev; +Cc: linux-kselftest, kuba, edumazet, pabeni, horms, shuah, willemb

NetNS records its name before running ip netns add. If creation fails
because that name already exists, destruction of the partially built
object deletes the pre-existing handle that it never acquired.

Initialize the owned name to None and record it only after creation
succeeds. Successful creation and context cleanup keep their behavior.

Run this as root from tools/testing/selftests/net. Its mount and network
namespaces are private; it prints False before the fix and True after:

  unshare -mn --propagation private sh -ec '
    mount -t tmpfs tmpfs /run; mkdir /run/netns
    exec python3 -
  ' <<'PYCODE'
  import gc, os
  from lib.py import CmdExitFailure, NetNS, ip
  ip("netns add review-existing")
  try: NetNS("review-existing")
  except CmdExitFailure: pass
  gc.collect()
  print(os.path.exists("/run/netns/review-existing"))
  PYCODE

Found by AI-assisted inspection and reproduced with real ip netns calls.
Eight ownership/lifecycle checks go from six to eight passing, including
preservation of the original inode and marker interface. Explicit,
automatic and empty-name creation, body exceptions and repeated cleanup
were checked. Current in-tree callers normally use generated names;
this is not a claim of frequent collisions. Removing a named handle does
not necessarily destroy all namespace references. No kernel modification
or fault injection was used, and no hardware suite was run.

Fixes: 1880f272d2f9 ("selftests: drv-net: construct environment for running tests which require an endpoint")
Signed-off-by: Jingjing Xu <xujingjing@cmss.chinamobile.com>
---
 tools/testing/selftests/net/lib/py/netns.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/net/lib/py/netns.py b/tools/testing/selftests/net/lib/py/netns.py
index 526f6aa80077..981be5f842ff 100644
--- a/tools/testing/selftests/net/lib/py/netns.py
+++ b/tools/testing/selftests/net/lib/py/netns.py
@@ -15,11 +15,11 @@ libc = ctypes.cdll.LoadLibrary('libc.so.6')
 
 class NetNS:
     def __init__(self, name=None):
-        if name:
-            self.name = name
-        else:
-            self.name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
-        ip('netns add ' + self.name)
+        self.name = None
+        if not name:
+            name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
+        ip('netns add ' + name)
+        self.name = name
 
     def __del__(self):
         if self.name:
base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e
-- 
2.53.0




^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  3:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 15:48 [PATCH net-next] selftests: net: py: preserve existing namespaces on creation failure Jingjing Xu
2026-12-01  3:00 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox