test: replace insecure tempfile.mktemp with mkstemp

tempfile.mktemp is deprecated because it only generates a filename
without creating the file, introducing a TOCTOU race condition where
another process could create a file with the same name between the
mktemp call and the subsequent file operation.

Replace it with tempfile.mkstemp, which atomically creates the file
and returns a file descriptor, eliminating the race window.

Assisted-by: Claude Code (claude-opus-4-6)
Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:
Adrian Reber 2026-06-17 12:37:35 +00:00 committed by Andrei Vagin
parent 8781b651e3
commit 6233eaa5f0
2 changed files with 5 additions and 4 deletions

View file

@ -223,8 +223,9 @@ class ns_flavor:
except OSError as e:
if e.errno != errno.EEXIST:
raise
dst = tempfile.mktemp(".tso", "",
self.root + os.path.dirname(fname))
fd, dst = tempfile.mkstemp(".tso", "",
self.root + os.path.dirname(fname))
os.close(fd)
shutil.copy2(fname, dst)
os.rename(dst, tfname)

View file

@ -14,8 +14,8 @@ class criu_config:
preload=False,
nowait=False):
config_path = tempfile.mktemp(".conf", "criu-%s-" % action)
with open(config_path, "w") as config_fd:
config_fd_raw, config_path = tempfile.mkstemp(".conf", "criu-%s-" % action)
with os.fdopen(config_fd_raw, "w") as config_fd:
for arg in args:
if arg.startswith("--"):
config_fd.write("\n")