Skip to contentSkip to conversation

fetch2: harden deb/ipk unpack command argument

PATCH New Anders Heimer 1 participant 3 messages May 18

lore
Share & tools
lore
#0 lore raw

cover fetch2: harden deb/ipk unpack command argument

Hi,

This series hardens fetch2 unpack handling in two small, separate steps.

The first patch fixes deb/ipk unpack handling by selecting the data archive member only from the set of supported data member names. This avoids passing unsupported ar member names through to the shell command. It also quotes the package path used by the deb/ipk unpack command.

The second patch is separate hardening for the striplevel parameter, which is appended to tar arguments before the unpack command is run. It validates striplevel as a decimal component count before using it.

Regression coverage is added to FetcherLocalTest for supported deb/ipk data members, unsupported data member names, quoted package filenames, and invalid striplevel values.

Tests run:
BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch.FetcherLocalTest

Anders Heimer (2):
fetch2: validate deb/ipk data member names
fetch2: validate striplevel parameter

 lib/bb/fetch2/__init__.py | 15 +++++++---
 lib/bb/tests/fetch.py     | 63 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 fetch2: validate deb/ipk data member names

The deb/ipk unpack path selects a data archive member from 'ar -t' output and then passes that member name to a shell command. Previously, any member beginning with data.tar. was selected.

Only select known deb/ipk data archive member names when datafile is created. Quote the package path used in the shell command as it can come from the local fetch path.

Add local fetcher regression coverage for quoted package filenames, valid compressed data members, and unsupported or unsafe data member names.

---
 lib/bb/fetch2/__init__.py | 10 +++++---
 lib/bb/tests/fetch.py     | 53 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 3 deletions(-)
lib/bb/fetch2/__init__.py +7 -3
index b6cb4c530..dc93e64a9 100644
@@ -23,6 +23,7 @@ import collections
 import subprocess
 import pickle
 import errno
+import shlex
 import bb.utils
 import bb.checksum
 import bb.process
@@ -1592,16 +1593,19 @@ class FetchMethod(object):
             elif file.endswith('.deb') or file.endswith('.ipk'):
                 output = subprocess.check_output(['ar', '-t', file], preexec_fn=subprocess_setup)
                 datafile = None
+                valid_datafiles = ('data.tar', 'data.tar.gz', 'data.tar.xz',
+                                   'data.tar.zst', 'data.tar.bz2', 'data.tar.lzma')
                 if output:
                     for line in output.decode().splitlines():
-                        if line.startswith('data.tar.') or line == 'data.tar':
+                        if line in valid_datafiles:
                             datafile = line
                             break
                     else:
-                        raise UnpackError("Unable to unpack deb/ipk package - does not contain data.tar* file", urldata.url)
+                        raise UnpackError("Unable to unpack deb/ipk package - does not contain supported data.tar* file", urldata.url)
                 else:
                     raise UnpackError("Unable to unpack deb/ipk package - could not list contents", urldata.url)
-                cmd = 'ar x %s %s && %s -p -f %s && rm %s' % (file, datafile, tar_cmd, datafile, datafile)
+                quoted_datafile = shlex.quote(datafile)
+                cmd = 'ar x %s %s && %s -p -f %s && rm %s' % (shlex.quote(file), quoted_datafile, tar_cmd, quoted_datafile, quoted_datafile)
 
         # If 'subdir' param exists, create a dir and use it as destination for unpack cmd
         if 'subdir' in urldata.parm:
lib/bb/tests/fetch.py +53 -0

71 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 fetch2: validate striplevel parameter

In reply to Anders Heimer

The striplevel URL parameter is appended to tar_cmd, which is later run through the shell. Validate it as a decimal count before using it in the tar arguments.

---
 lib/bb/fetch2/__init__.py |  5 ++++-
 lib/bb/tests/fetch.py     | 10 ++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
lib/bb/fetch2/__init__.py +4 -1
index dc93e64a9..77fc1b15b 100644
@@ -1545,7 +1545,10 @@ class FetchMethod(object):
         if unpack:
             tar_cmd = 'tar --extract --no-same-owner'
             if 'striplevel' in urldata.parm:
-                tar_cmd += ' --strip-components=%s' %  urldata.parm['striplevel']
+                striplevel = urldata.parm['striplevel']
+                if not striplevel.isdigit():
+                    raise UnpackError("Invalid striplevel parameter: %s" % striplevel, urldata.url)
+                tar_cmd += ' --strip-components=%s' % striplevel
             if file.endswith('.tar'):
                 cmd = '%s -f %s' % (tar_cmd, file)
             elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'):
lib/bb/tests/fetch.py +10 -0
index bece59e62..5850589e0 100644
@@ -866,6 +866,16 @@ class FetcherLocalTest(FetcherTest):
 
         self.assertIn("does not contain supported data.tar* file", str(context.exception))
 
+    def assertInvalidStriplevel(self, value):
+        with self.assertRaises(bb.fetch2.UnpackError) as context:
+            self.fetchUnpack(['file://archive.tar;subdir=bar;striplevel=%s' % value])
+        self.assertIn("Invalid striplevel parameter", str(context.exception))
+
+    def test_local_striplevel_rejects_invalid_values(self):
+        for value in ("abc", "", "-1", "1\n", "1 2"):
+            with self.subTest(striplevel=repr(value)):
+                self.assertInvalidStriplevel(value)
+
     def dummyGitTest(self, suffix):
         # Create dummy local Git repo
         src_dir = tempfile.mkdtemp(dir=self.tempdir,
Signature · 1 line
2.34.1