support/scripts: make CVE class independent of the Packaage class
authorGregory CLEMENT <gregory.clement@bootlin.com>
Fri, 24 Jul 2020 15:43:52 +0000 (17:43 +0200)
committerThomas Petazzoni <thomas.petazzoni@bootlin.com>
Fri, 28 Aug 2020 09:00:40 +0000 (11:00 +0200)
The affects method of the CVE uses the Package class defined in
pkg-stats. The purpose of migrating the CVE class outside of pkg-stats
was to be able to reuse it from other scripts. So let's remove the
Package dependency and only use the needed information.

Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
support/scripts/cve.py
support/scripts/pkg-stats

index e812da810c5ec918b657f46bac6f94f1a1bd7b24..6396019e0ea330db669b1a7868a84460c8ef456f 100755 (executable)
@@ -190,21 +190,21 @@ class CVE:
         """The set of package names referred by this CVE definition"""
         return set(p['product'] for p in self.each_cpe())
 
-    def affects(self, br_pkg):
+    def affects(self, name, version, cve_ignore_list):
         """
         True if the Buildroot Package object passed as argument is affected
         by this CVE.
         """
-        if br_pkg.is_cve_ignored(self.identifier):
+        if self.identifier in cve_ignore_list:
             return self.CVE_DOESNT_AFFECT
 
-        pkg_version = distutils.version.LooseVersion(br_pkg.current_version)
+        pkg_version = distutils.version.LooseVersion(version)
         if not hasattr(pkg_version, "version"):
-            print("Cannot parse package '%s' version '%s'" % (br_pkg.name, br_pkg.current_version))
+            print("Cannot parse package '%s' version '%s'" % (name, version))
             pkg_version = None
 
         for cpe in self.each_cpe():
-            if cpe['product'] != br_pkg.name:
+            if cpe['product'] != name:
                 continue
             if cpe['v_start'] == '-':
                 return self.CVE_AFFECTS
index 5e2213fb5cf5abbced10447f88b9ba5eea1b6bae..be586a2c2e84991050fccf6b7281ce55cb7e7242 100755 (executable)
@@ -236,11 +236,12 @@ class Package:
                     self.status['pkg-check'] = ("error", "{} warnings".format(self.warnings))
                 return
 
-    def is_cve_ignored(self, cve):
+    @property
+    def ignored_cves(self):
         """
-        Tells if the CVE is ignored by the package
+        Give the list of CVEs ignored by the package
         """
-        return cve in self.all_ignored_cves.get(self.pkgvar(), [])
+        return list(self.all_ignored_cves.get(self.pkgvar(), []))
 
     def set_developers(self, developers):
         """
@@ -536,9 +537,10 @@ def check_package_cves(nvd_path, packages):
 
     for cve in cvecheck.CVE.read_nvd_dir(nvd_path):
         for pkg_name in cve.pkg_names:
-            if pkg_name in packages and cve.affects(packages[pkg_name]) == cve.CVE_AFFECTS:
-                packages[pkg_name].cves.append(cve.identifier)
-
+            if pkg_name in packages:
+                pkg = packages[pkg_name]
+                if cve.affects(pkg.name, pkg.current_version, pkg.ignored_cves) == cve.CVE_AFFECTS :
+                    pkg.cves.append(cve.identifier)
 
 def calculate_stats(packages):
     stats = defaultdict(int)