diff options
| author | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 02:54:59 +0000 |
|---|---|---|
| committer | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 02:54:59 +0000 |
| commit | d0ca262e7cc6d2ad86905e429ae84574d20b34ff (patch) | |
| tree | 846e9c373c9694983d7421571e27471653a096ca /tools/rtti_scan.py | |
| parent | d0f0a510f3523ada86905fb5a2d922ae5ef2b8c5 (diff) | |
rtti_scan: the swapped-in report keeps the old report's permission bits
NamedTemporaryFile creates its file 0600, so a new report lost the umask
default (0644 under umask 022) and a replaced 0640 report lost group read
(found by @VictorJulianiR on Ubuntu). The temp file now gets the old report's
mode, or 0666 minus the umask for a new report, before os.replace.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/rtti_scan.py')
| -rw-r--r-- | tools/rtti_scan.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/tools/rtti_scan.py b/tools/rtti_scan.py index 92e26bd14..654742ed6 100644 --- a/tools/rtti_scan.py +++ b/tools/rtti_scan.py | |||
| @@ -18,6 +18,7 @@ import argparse | |||
| 18 | import json | 18 | import json |
| 19 | import os | 19 | import os |
| 20 | import re | 20 | import re |
| 21 | import stat | ||
| 21 | import struct | 22 | import struct |
| 22 | import sys | 23 | import sys |
| 23 | import tempfile | 24 | import tempfile |
| @@ -189,9 +190,21 @@ def scan_pe64_rtti(data: bytes) -> dict: | |||
| 189 | } | 190 | } |
| 190 | 191 | ||
| 191 | 192 | ||
| 193 | def _report_mode(path): | ||
| 194 | """The permission bits the report should end up with: those of the report it replaces, or what open() would | ||
| 195 | give a new file (0666 minus the umask). NamedTemporaryFile creates its file 0600.""" | ||
| 196 | try: | ||
| 197 | return stat.S_IMODE(os.stat(path).st_mode) | ||
| 198 | except FileNotFoundError: | ||
| 199 | mask = os.umask(0) | ||
| 200 | os.umask(mask) | ||
| 201 | return 0o666 & ~mask | ||
| 202 | |||
| 203 | |||
| 192 | def write_report(path, text): | 204 | def write_report(path, text): |
| 193 | """Write the report next to its destination and swap it in, so a write that fails part-way (a full disk, a | 205 | """Write the report next to its destination and swap it in, so a write that fails part-way (a full disk, a |
| 194 | locked range) leaves the previous report as it was instead of an empty or truncated file.""" | 206 | locked range) leaves the previous report as it was instead of an empty or truncated file. The result keeps |
| 207 | the previous report's permission bits, or gets the usual ones for a new file.""" | ||
| 195 | parent = os.path.dirname(os.path.abspath(path)) | 208 | parent = os.path.dirname(os.path.abspath(path)) |
| 196 | tmp = None | 209 | tmp = None |
| 197 | try: | 210 | try: |
| @@ -199,6 +212,7 @@ def write_report(path, text): | |||
| 199 | delete=False) as f: | 212 | delete=False) as f: |
| 200 | tmp = f.name | 213 | tmp = f.name |
| 201 | f.write(text) | 214 | f.write(text) |
| 215 | os.chmod(tmp, _report_mode(path)) | ||
| 202 | os.replace(tmp, path) | 216 | os.replace(tmp, path) |
| 203 | except BaseException: | 217 | except BaseException: |
| 204 | if tmp is not None: | 218 | if tmp is not None: |