Skip to content

Commit 2751bea

Browse files
committed
donate-cpu: keep extracted package files inside the temp folder
1 parent c10a3ee commit 2751bea

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import io
1920
import sys
21+
import tarfile
2022

2123
import pytest
2224

@@ -126,3 +128,30 @@ def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch):
126128
"lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \
127129
'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \
128130
'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n'
131+
132+
def _add_tar_member(tf, name, linkname=None):
133+
info = tarfile.TarInfo(name)
134+
data = b''
135+
if linkname is not None:
136+
info.type = tarfile.SYMTYPE
137+
info.linkname = linkname
138+
else:
139+
data = b'int x;\n'
140+
info.size = len(data)
141+
tf.addfile(info, io.BytesIO(data))
142+
143+
def test_unpack_package_outside_temp(tmpdir):
144+
work_path = str(tmpdir)
145+
outside_path = os.path.join(work_path, 'outside')
146+
os.mkdir(outside_path)
147+
tgz = os.path.join(work_path, 'temp.tgz')
148+
with tarfile.open(tgz, 'w:gz') as tf:
149+
_add_tar_member(tf, 'pkg/main.c')
150+
_add_tar_member(tf, 'pkg/../../outside/dotdot.c')
151+
_add_tar_member(tf, 'pkg/inc.h', linkname=outside_path)
152+
_add_tar_member(tf, 'pkg/inc.h/symlink.c')
153+
154+
temp_path, source_found = unpack_package(work_path, tgz)
155+
assert source_found
156+
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
157+
assert os.listdir(outside_path) == []

‎tools/donate_cpu_lib.py‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
1717
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
1818
# changes)
19-
CLIENT_VERSION = "1.4.2"
19+
CLIENT_VERSION = "1.4.3"
2020

2121
# Timeout for analysis with Cppcheck in seconds
2222
CPPCHECK_TIMEOUT = 30 * 60
@@ -345,10 +345,19 @@ def download_package(work_path, package, bandwidth_limit):
345345
return destfile
346346

347347

348+
def __is_inside_path(base_path, path):
349+
try:
350+
return os.path.commonpath([base_path, path]) == base_path
351+
except ValueError:
352+
# paths are on different drives
353+
return False
354+
355+
348356
def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None):
349357
temp_path = os.path.join(work_path, 'temp')
350358
__remove_tree(temp_path)
351359
os.mkdir(temp_path)
360+
real_temp_path = os.path.realpath(temp_path)
352361

353362
header_endings = ('.hpp', '.h++', '.hxx', '.hh', '.h')
354363
cpp_source_endings = ('.cpp', '.c++', '.cxx', '.cc', '.tpp', '.txx', '.ipp', '.ixx', '.qml')
@@ -368,10 +377,19 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None
368377
total = 0
369378
extracted = 0
370379
skipped = 0
380+
# Python versions with extraction filters reject members that leave temp_path as well
381+
extract_args = {'filter': 'data'} if hasattr(tarfile, 'data_filter') else {}
371382
for member in tf:
372383
total += 1
373-
if member.name.startswith(('/', '..')):
374-
# Skip dangerous file names
384+
member_path = os.path.realpath(os.path.join(temp_path, member.name))
385+
link_path = None
386+
if member.issym():
387+
link_path = os.path.realpath(os.path.join(os.path.dirname(member_path), member.linkname))
388+
elif member.islnk():
389+
link_path = os.path.realpath(os.path.join(temp_path, member.linkname))
390+
if not __is_inside_path(real_temp_path, member_path) or \
391+
(link_path is not None and not __is_inside_path(real_temp_path, link_path)):
392+
# Skip dangerous file names and links pointing outside of temp_path
375393
# print('skipping dangerous file: ' + member.name)
376394
skipped += 1
377395
continue
@@ -389,14 +407,16 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None
389407
skipped += 1
390408
continue
391409
try:
392-
tf.extract(member.name, temp_path)
410+
tf.extract(member.name, temp_path, **extract_args)
393411
if is_source:
394412
source_found = True
395413
extracted += 1
396414
except OSError:
397415
pass
398416
except AttributeError:
399417
pass
418+
except tarfile.TarError:
419+
pass
400420
print('extracted {} of {} files (skipped {}{})'.format(extracted, total, skipped, (' / only headers' if (extracted and not source_found) else '')))
401421
return temp_path, source_found
402422

0 commit comments

Comments
 (0)