llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Zachary Henkel (ZacharyHenkel)

<details>
<summary>Changes</summary>

Python's tempfile.mktemp() is deprecated and insecure: it returns a path 
without creating the file, leaving a TOCTOU/symlink window before the file is 
opened. Replace every use, choosing the temp API by lifetime:

- compiler-rt android_common.py adb(): tempfile.TemporaryFile (anonymous, 
auto-deleted), read back via seek(0); drops manual close()/unlink().
- compiler-rt android_common.py pull_from_device(): tempfile.TemporaryDirectory 
so "adb pull" writes into a private, auto-cleaned directory.
- lldb examples delta.py / gdbremote.py start_gdb_log(): 
tempfile.NamedTemporaryFile(delete=False); the log must outlive the call for a 
later stop_gdb_log, so it is intentionally not auto-deleted.

Reported by internal static analysis and contributed upstream for the benefit 
of the wider community.

Assisted-by: GitHub Copilot CLI (Claude Opus 4.8)

I do not have commit access and will need someone to submit on my behalf.

---
Full diff: https://github.com/llvm/llvm-project/pull/210123.diff


3 Files Affected:

- (modified) 
compiler-rt/test/sanitizer_common/android_commands/android_common.py (+18-23) 
- (modified) lldb/examples/python/delta.py (+2-1) 
- (modified) lldb/examples/python/gdbremote.py (+2-1) 


``````````diff
diff --git 
a/compiler-rt/test/sanitizer_common/android_commands/android_common.py 
b/compiler-rt/test/sanitizer_common/android_commands/android_common.py
index 4e7d15c9ebc23..6c7c410108a74 100644
--- a/compiler-rt/test/sanitizer_common/android_commands/android_common.py
+++ b/compiler-rt/test/sanitizer_common/android_commands/android_common.py
@@ -18,33 +18,28 @@ def host_to_device_path(path):
 def adb(args, attempts=1, timeout_sec=600):
     if verbose:
         print(args)
-    tmpname = tempfile.mktemp()
-    out = open(tmpname, "w")
-    ret = 255
-    while attempts > 0 and ret != 0:
-        attempts -= 1
-        ret = subprocess.call(
-            ["timeout", str(timeout_sec), ADB] + args,
-            stdout=out,
-            stderr=subprocess.STDOUT,
-        )
-    if ret != 0:
-        print("adb command failed", args)
-        print(tmpname)
-        out.close()
-        out = open(tmpname, "r")
-        print(out.read())
-    out.close()
-    os.unlink(tmpname)
+    with tempfile.TemporaryFile(mode="w+") as out:
+        ret = 255
+        while attempts > 0 and ret != 0:
+            attempts -= 1
+            ret = subprocess.call(
+                ["timeout", str(timeout_sec), ADB] + args,
+                stdout=out,
+                stderr=subprocess.STDOUT,
+            )
+        if ret != 0:
+            print("adb command failed", args)
+            out.seek(0)
+            print(out.read())
     return ret
 
 
 def pull_from_device(path):
-    tmp = tempfile.mktemp()
-    adb(["pull", path, tmp], 5, 60)
-    text = open(tmp, "r").read()
-    os.unlink(tmp)
-    return text
+    with tempfile.TemporaryDirectory() as tmp_dir:
+        tmp = os.path.join(tmp_dir, "pulled")
+        adb(["pull", path, tmp], 5, 60)
+        with open(tmp, "r") as f:
+            return f.read()
 
 
 def push_to_device(path):
diff --git a/lldb/examples/python/delta.py b/lldb/examples/python/delta.py
old mode 100755
new mode 100644
index 35f155bdea100..e84185b1ae05a
--- a/lldb/examples/python/delta.py
+++ b/lldb/examples/python/delta.py
@@ -35,7 +35,8 @@ def start_gdb_log(debugger, command, result, dict):
     else:
         args_len = len(args)
         if args_len == 0:
-            log_file = tempfile.mktemp()
+            with tempfile.NamedTemporaryFile(delete=False) as tmp:
+                log_file = tmp.name
         elif len(args) == 1:
             log_file = args[0]
 
diff --git a/lldb/examples/python/gdbremote.py 
b/lldb/examples/python/gdbremote.py
old mode 100755
new mode 100644
index 2f2d82c3d3d54..a215a878ef0a0
--- a/lldb/examples/python/gdbremote.py
+++ b/lldb/examples/python/gdbremote.py
@@ -229,7 +229,8 @@ def start_gdb_log(debugger, command, result, dict):
     else:
         args_len = len(args)
         if args_len == 0:
-            g_log_file = tempfile.mktemp()
+            with tempfile.NamedTemporaryFile(delete=False) as tmp:
+                g_log_file = tmp.name
         elif len(args) == 1:
             g_log_file = args[0]
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/210123
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to