Repository navigation
Conversation
|
I suggest you rebase and see if CI will become green. There are compiler errors that might have been fixed. |
a90349e to
c24476b
Compare
|
Rebased onto current main, it applied cleanly and donate_cpu_lib_test.py still passes locally. Let's see what CI says now. Separately, could one of you create a Trac ticket for this? I don't have access, and I'll put the number in the title once it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix is incomplete when Python has no tarfile.data_filter, which is the case it is meant for. The new realpath check runs on the member from the loop, but extraction still calls tf.extract(member.name, ...). Extracting by name looks up the last member with that name in the archive, so a harmless first entry can pass the check while a later entry with the same name, such as a hardlink or symlink that points outside temp, gets extracted. Hardlinks also defeat the realpath check of later members: realpath cannot tell that a file inside temp is a hardlink to a file outside it, and a later regular-file member that writes through that path writes to the outside file. On Python versions with data_filter (3.12 here), the filter still catches this. The fix is to pass the member object to tf.extract. Apart from that I found no new problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | extract(member.name) extracts the last member with that name, not the one that was checked | AI review | tools/donate_cpu_lib.py:409 |
🆕 New |
@Nussu06, please fix, resolve or reply to the open and new comments: #1.
Testing
- I did not build
cppcheckortestrunner. The PR only changestools/donate_cpu_lib.pyand its Python test, so no C++ code is affected. PYTHONPATH=tools python3 -m pytest -Werror -q test/tools/donate_cpu_lib_test.pyinpr/with Python 3.12.3: 3 passed.- The same test file run against
base/:test_unpack_package_outside_tempfails as expected, and the other 2 pass. - I checked how name lookup behaves with duplicate member names, which is what the finding depends on:
# scratch/dup.py
import io, tarfile, tempfile, os
d = tempfile.mkdtemp(); p = os.path.join(d, 'x.tar')
with tarfile.open(p, 'w') as tf:
for c in (b'first\n', b'second\n'):
i = tarfile.TarInfo('a.c'); i.size = len(c); tf.addfile(i, io.BytesIO(c))
with tarfile.open(p) as tf:
for m in tf:
tf.extract(m.name, d, filter='data')
print(open(os.path.join(d, 'a.c')).read().strip())
breakpython3 -I dup.py prints second. On the first loop iteration, tf.extract(m.name) extracted the second member, not the one the loop was on.
- I did not reproduce a full escape from
temp. The impact on Python versions withoutdata_filtercomes from reading the code: the check runs on one member while another member with the same name is extracted.
Automated review of c24476b by Claude. The pull request was built and tested locally.
| continue | ||
| try: | ||
| tf.extract(member.name, temp_path) | ||
| tf.extract(member.name, temp_path, **extract_args) |
There was a problem hiding this comment.
AI review
The safety check above validates member, but tf.extract(member.name, ...) goes through getmember(name), which returns the last member with that name in the archive. A tarball can contain a harmless pkg/a.c first and a later pkg/a.c that is a hardlink or symlink pointing outside temp. The first entry passes the check, and extracting it creates the later, unchecked link. The later entry is then skipped, but by that point the link has already been created.
With filter='data' the filter still rejects the link, but on Python versions without tarfile.data_filter (the case this PR protects) nothing stops it. A hardlink is also invisible to the realpath check, so a later regular-file member that writes through that path (for example through a directory symlink that stays inside temp) writes into the file outside temp.
Passing the member object fixes this:
tf.extract(member, temp_path, **extract_args)It would be worth adding a duplicate-name case to test_unpack_package_outside_temp.
There was a problem hiding this comment.
Good catch, extracting by name picks up the last entry with that name, so the check and the extraction could be looking at different members. It now passes the member object to tf.extract. I added test_unpack_package_duplicate_name, which has a regular pkg/sym.c followed by a same-named symlink to a file outside temp, and the same for a hardlink. It fails with the old code on Python 3.10.11 (no data_filter) and passes with the fix on 3.10 and 3.11. The Trac ticket is still open on my side, if someone can create one I'll put the number in the title.
unpack_package in tools/donate_cpu_lib.py only skips archive members whose name starts with '/' or '..', so a package tarball containing pkg/../../outside/x.c, or a symlink pkg/inc.h pointing at another directory followed by pkg/inc.h/x.c, writes source files outside work_path/temp when donate-cpu.py, test-my-pr.py or compare-valueflow-options.py unpack it on Python before 3.14 (3.14 refuses these by default, but the uncaught OutsideDestinationError aborts the run). Each member's destination, and the target of symlink and hardlink members, is now resolved with realpath and skipped unless it stays inside the temp folder, and filter='data' is passed where tarfile supports it; links that stay inside the package, such as include/a.h -> ../src/a.h, are still extracted. Added test_unpack_package_outside_temp, which fails on main with Python 3.9 and 3.12 and passes with the fix on 3.9, 3.12 and 3.14, and bumped CLIENT_VERSION to 1.4.1. I don't have Trac access, so could you create a ticket for this? I'll update the title once there is a number.