Security: Use of exec() to Execute File Contents#12331
Security: Use of exec() to Execute File Contents#12331barttran2k wants to merge 1 commit intoaio-libs:masterfrom
Conversation
The `calc_headers` function reads the content of `hdrs.py` and executes it using `exec(code, globs)`. While the file is from within the project, if an attacker could modify `hdrs.py` (e.g., via a supply-chain attack or compromised repository), arbitrary code would be executed during the build/code-generation process. Affected files: gen.py Signed-off-by: Trần Bách <45133811+barttran2k@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12331 +/- ##
==========================================
- Coverage 99.11% 98.06% -1.06%
==========================================
Files 130 128 -2
Lines 45609 45565 -44
Branches 2405 2395 -10
==========================================
- Hits 45207 44684 -523
- Misses 272 669 +397
- Partials 130 212 +82
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Merging this PR will improve performance by ×3.1
Performance Changes
Comparing |
|
It is expected that anything with an even remote security implications should be reported following the responsible disclosure procedure per policy. Although, I don't really see how this is a vulnerability — if someone has enough access to replace a module on the system, that system would be compromised already. |
|
It's broken 54 tests, so that doesn't seem right at all... |
|
But, yeah, if an attacker can modify hdrs.py, why don't they just modify gen.py instead, given that's the file being executed? |
Problem
The
calc_headersfunction reads the content ofhdrs.pyand executes it usingexec(code, globs). While the file is from within the project, if an attacker could modifyhdrs.py(e.g., via a supply-chain attack or compromised repository), arbitrary code would be executed during the build/code-generation process.Severity:
lowFile:
tools/gen.pySolution
Consider parsing the file using AST (
ast.parse+ast.literal_eval) to extract only the needed constant values instead of executing the entire file. Alternatively, import the module directly rather than using exec().Changes
tools/gen.py(modified)Testing