Repository navigation
donate-cpu: keep extracted package files inside the temp folder #8973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2751bea
6d6d7d6
1ea1577
2f91952
f5cfa23
60f7858
9b6cc75
69d5fb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| # Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/ | ||
| # Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic | ||
| # changes) | ||
| CLIENT_VERSION = "1.4.2" | ||
| CLIENT_VERSION = "1.4.3" | ||
|
|
||
| # Timeout for analysis with Cppcheck in seconds | ||
| CPPCHECK_TIMEOUT = 30 * 60 | ||
|
|
@@ -345,10 +345,88 @@ def download_package(work_path, package, bandwidth_limit): | |
| return destfile | ||
|
|
||
|
|
||
| def __is_inside_path(base_path, path): | ||
| try: | ||
| return os.path.commonpath([base_path, path]) == base_path | ||
| except ValueError: | ||
| # paths are on different drives | ||
| return False | ||
|
|
||
|
|
||
| def __remove_link(path): | ||
| try: | ||
| os.remove(path) | ||
| except OSError: | ||
| # a directory symlink on Windows | ||
| os.rmdir(path) | ||
|
|
||
|
|
||
| def __find_escaping_links(real_temp_path): | ||
| # Returns the links below real_temp_path that resolve outside of it, and whether a folder had to | ||
| # be made accessible to look at its content. Raises OSError for a folder that is inaccessible | ||
| # even then. | ||
| import stat | ||
| fixed = False | ||
|
|
||
| def make_accessible(path): | ||
| # A directory member extracted after the links can have taken the owner read or search bit | ||
| # off its folder. Without the read bit it cannot be listed, and without the search bit its | ||
| # entries cannot be told apart from links. | ||
| nonlocal fixed | ||
| mode = stat.S_IMODE(os.lstat(path).st_mode) | ||
| missing = (stat.S_IRUSR | stat.S_IXUSR) & ~mode | ||
| if not missing: | ||
| return False | ||
| os.chmod(path, mode | missing) | ||
| fixed = True | ||
| return True | ||
|
|
||
| def onerror(e): | ||
| if not make_accessible(e.filename): | ||
| raise e | ||
|
|
||
| links = [] | ||
| for root, dirs, files in os.walk(real_temp_path, onerror=onerror): | ||
| if make_accessible(root): | ||
| # the entries were listed without the search bit needed to stat them, so start over | ||
| break | ||
| for name in dirs + files: | ||
| path = os.path.join(root, name) | ||
| if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)): | ||
| links.append(path) | ||
| return links, fixed | ||
|
|
||
|
|
||
| def __remove_escaping_links(real_temp_path): | ||
| # A link that still pointed inside real_temp_path when it was extracted can be redirected | ||
| # by a link that is extracted later, so all links are checked again afterwards. Removing a | ||
| # link changes what the remaining ones resolve to, so this is repeated until nothing is left. | ||
| # Raises OSError if a folder cannot be listed or a link cannot be removed. | ||
| import stat | ||
| removed = 0 | ||
| while True: | ||
| links, fixed = __find_escaping_links(real_temp_path) | ||
| if fixed: | ||
| # a folder whose content the walk could not see is accessible now, so look again | ||
| continue | ||
| if not links: | ||
| return removed | ||
| for path in links: | ||
| try: | ||
| __remove_link(path) | ||
| except OSError: | ||
| # a directory member extracted after the link can have made its folder read-only | ||
| parent = os.path.dirname(path) | ||
| os.chmod(parent, stat.S_IMODE(os.lstat(parent).st_mode) | stat.S_IWUSR) | ||
| __remove_link(path) | ||
| removed += 1 | ||
|
|
||
|
|
||
| def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None): | ||
| temp_path = os.path.join(work_path, 'temp') | ||
| __remove_tree(temp_path) | ||
| os.mkdir(temp_path) | ||
| real_temp_path = os.path.realpath(temp_path) | ||
|
|
||
| header_endings = ('.hpp', '.h++', '.hxx', '.hh', '.h') | ||
| cpp_source_endings = ('.cpp', '.c++', '.cxx', '.cc', '.tpp', '.txx', '.ipp', '.ixx', '.qml') | ||
|
|
@@ -368,10 +446,21 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None | |
| total = 0 | ||
| extracted = 0 | ||
| skipped = 0 | ||
| # Python versions with extraction filters reject members that leave temp_path as well | ||
| extract_args = {'filter': 'data'} if hasattr(tarfile, 'data_filter') else {} | ||
| for member in tf: | ||
| total += 1 | ||
| if member.name.startswith(('/', '..')): | ||
| # Skip dangerous file names | ||
| member_path = os.path.realpath(os.path.join(temp_path, member.name)) | ||
| link_path = None | ||
| if member.issym(): | ||
| # resolve from the directory the link is created in, not through an existing link with the same name | ||
| link_parent = os.path.realpath(os.path.dirname(os.path.join(temp_path, member.name))) | ||
| link_path = os.path.realpath(os.path.join(link_parent, member.linkname)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI review This After extraction, Checking each member when it is extracted can't catch this, because the target can change after the check. There are two options. One is to reject symlinks whose
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, the per-member check can't cover this since the target can still change after the check. I went with the second option rather than rejecting every '..' in linkname, because in-tree links like include/a.h -> ../src/a.h are common in real packages and I'd rather keep extracting them. After the extraction loop, __remove_escaping_links() walks temp and unlinks every symlink whose realpath is outside temp_path, repeating until a pass finds nothing, and the removed ones move from the extracted to the skipped count. Writes through such a link were already prevented, since each member's own destination is resolved with realpath at the time it is extracted, so this only has to deal with the links themselves. Added test_unpack_package_link_redirected_later with your pkg/l.h -> d.h/../../outside plus pkg/d.h -> . case, and an in-tree pkg/include/main.c -> ../src/main.c to show those survive. It fails on main (l.h resolving to work_path/outside) and passes with the fix on 3.9.6, 3.10, 3.12 and 3.14, both with filter='data' and with tarfile.data_filter removed. pylint --py-version 3.7 is clean apart from the pre-existing psutil import warning. Still no Trac ticket for this one, could someone create it so I can put the number in the title? |
||
| elif member.islnk(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI review AI review This check only looks at the hardlink's own I reproduced this with The fix is to keep tarfile from ever taking the fallback for hardlinks. Only extract a hardlink member when |
||
| link_path = os.path.realpath(os.path.join(temp_path, member.linkname)) | ||
| if not __is_inside_path(real_temp_path, member_path) or \ | ||
| (link_path is not None and not __is_inside_path(real_temp_path, link_path)): | ||
| # Skip dangerous file names and links pointing outside of temp_path | ||
| # print('skipping dangerous file: ' + member.name) | ||
| skipped += 1 | ||
| continue | ||
|
|
@@ -389,14 +478,25 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None | |
| skipped += 1 | ||
| continue | ||
| try: | ||
| tf.extract(member.name, temp_path) | ||
| tf.extract(member, temp_path, **extract_args) | ||
| if is_source: | ||
| source_found = True | ||
| extracted += 1 | ||
| except OSError: | ||
| pass | ||
| except AttributeError: | ||
| pass | ||
| except (tarfile.TarError, KeyError): | ||
| # KeyError: hardlink to a member that is neither on disk nor in the archive | ||
| pass | ||
| try: | ||
| removed = __remove_escaping_links(real_temp_path) | ||
| except OSError as e: | ||
| # do not analyse a package with a link pointing outside of temp_path | ||
| print('Failed to remove link pointing outside of {}: {}'.format(temp_path, e)) | ||
| return temp_path, False | ||
| extracted -= removed | ||
| skipped += removed | ||
| print('extracted {} of {} files (skipped {}{})'.format(extracted, total, skipped, (' / only headers' if (extracted and not source_found) else ''))) | ||
| return temp_path, source_found | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AI review
onerroronly runs whenos.walkfails to list a folder. A folder withrbut nox, for example a header-named directory memberpkg/q.hwith mode0o644or0o600extracted after the links, can still be listed. Butentry.is_dir()and theos.path.islink(path)on line 384 both need tolstatthrough that folder. They fail withEACCES, andislinkreturnsFalse. Sopkg/q.h/l.h -> d.h/../../../outsideis never reported, nothing is removed, andunpack_packagereturnssource_found=True.I reproduced this on Python 3.12 with
tarfile.data_filterdeleted, as uid 1000. The archive is the one fromtest_unpack_package_link_in_unreadable_dir, withdirmode=0o644. The output isextracted 4 of 4 files (skipped 0), and afterchmod u+x,l.hresolves towork_path/outside. The impact is limited, as in the 0311 case, because cppcheck can't enter that folder either.A possible fix: in the walk, add
S_IRUSR | S_IXUSRto every directory indirs(and toroot) that lacks one of them, and setfixed. Or checkos.access(path, os.R_OK | os.X_OK)for each directory. Usingdirmode=0o644in a parametrized version of the test would cover this case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that folder can still be listed, but every lstat inside it fails, so
entry.is_dir()andos.path.islink()both come back wrong and the link is invisible to the walk.__find_escaping_linksnow checks the folder it is about to look into as well, not only the onesos.walkcould not list, and adds the owner read and search bits when either is missing, then starts the walk over. Turnedtest_unpack_package_link_in_unreadable_dirintotest_unpack_package_link_in_inaccessible_dirparametrized over 0o311 and 0o644. It also makes the folder accessible before asserting, since otherwiselexistshides the leftover link from the test too.The 0o644 case fails without the change on 3.9.6 and on 3.12 with
tarfile.data_filterdeleted, withl.hleft resolving towork_path/outside, and both cases pass with it on 3.9.6, 3.11 and 3.12, withfilter='data'and without. All 12 tests in the file pass. pylint --py-version 3.7 reports nothing new beyond the pre-existing psutil import-error.This PR still has no Trac ticket, could one of you create it so I can put the number in the title?