diff options
author | Joshua Watt <JPEWhacker@gmail.com> | 2021-08-11 09:51:58 -0500 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2021-08-13 14:44:06 +0100 |
commit | fa6c07bc1a585f204dbdc28704f61448edb8fdc8 (patch) | |
tree | 19f6807449c6fc9893bede34fefffddce590800c /meta/classes/cve-check.bbclass | |
parent | 3ae80177fb78799f6b42240a6ac8fb4753521aec (diff) | |
download | poky-fa6c07bc1a585f204dbdc28704f61448edb8fdc8.tar.gz |
classes/cve-check: Move get_patches_cves to library
Moving the function will allow other classes to capture which CVEs have
been patched, in particular SBoM generation.
Also add a function to capture the CPE ID from the CVE Product and
Version
(From OE-Core rev: 75d34259a715120be1d023e4fd7b6b4b125f2443)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes/cve-check.bbclass')
-rw-r--r-- | meta/classes/cve-check.bbclass | 62 |
1 files changed, 2 insertions, 60 deletions
diff --git a/meta/classes/cve-check.bbclass b/meta/classes/cve-check.bbclass index 04d19f53fd..70d1988a70 100644 --- a/meta/classes/cve-check.bbclass +++ b/meta/classes/cve-check.bbclass | |||
@@ -94,10 +94,11 @@ python do_cve_check () { | |||
94 | """ | 94 | """ |
95 | Check recipe for patched and unpatched CVEs | 95 | Check recipe for patched and unpatched CVEs |
96 | """ | 96 | """ |
97 | from oe.cve_check import get_patched_cves | ||
97 | 98 | ||
98 | if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")): | 99 | if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")): |
99 | try: | 100 | try: |
100 | patched_cves = get_patches_cves(d) | 101 | patched_cves = get_patched_cves(d) |
101 | except FileNotFoundError: | 102 | except FileNotFoundError: |
102 | bb.fatal("Failure in searching patches") | 103 | bb.fatal("Failure in searching patches") |
103 | whitelisted, patched, unpatched = check_cves(d, patched_cves) | 104 | whitelisted, patched, unpatched = check_cves(d, patched_cves) |
@@ -156,65 +157,6 @@ python cve_check_write_rootfs_manifest () { | |||
156 | ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}" | 157 | ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}" |
157 | do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}" | 158 | do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}" |
158 | 159 | ||
159 | def get_patches_cves(d): | ||
160 | """ | ||
161 | Get patches that solve CVEs using the "CVE: " tag. | ||
162 | """ | ||
163 | |||
164 | import re | ||
165 | |||
166 | pn = d.getVar("PN") | ||
167 | cve_match = re.compile("CVE:( CVE\-\d{4}\-\d+)+") | ||
168 | |||
169 | # Matches the last "CVE-YYYY-ID" in the file name, also if written | ||
170 | # in lowercase. Possible to have multiple CVE IDs in a single | ||
171 | # file name, but only the last one will be detected from the file name. | ||
172 | # However, patch files contents addressing multiple CVE IDs are supported | ||
173 | # (cve_match regular expression) | ||
174 | |||
175 | cve_file_name_match = re.compile(".*([Cc][Vv][Ee]\-\d{4}\-\d+)") | ||
176 | |||
177 | patched_cves = set() | ||
178 | bb.debug(2, "Looking for patches that solves CVEs for %s" % pn) | ||
179 | for url in src_patches(d): | ||
180 | patch_file = bb.fetch.decodeurl(url)[2] | ||
181 | |||
182 | if not os.path.isfile(patch_file): | ||
183 | bb.error("File Not found: %s" % patch_file) | ||
184 | raise FileNotFoundError | ||
185 | |||
186 | # Check patch file name for CVE ID | ||
187 | fname_match = cve_file_name_match.search(patch_file) | ||
188 | if fname_match: | ||
189 | cve = fname_match.group(1).upper() | ||
190 | patched_cves.add(cve) | ||
191 | bb.debug(2, "Found CVE %s from patch file name %s" % (cve, patch_file)) | ||
192 | |||
193 | with open(patch_file, "r", encoding="utf-8") as f: | ||
194 | try: | ||
195 | patch_text = f.read() | ||
196 | except UnicodeDecodeError: | ||
197 | bb.debug(1, "Failed to read patch %s using UTF-8 encoding" | ||
198 | " trying with iso8859-1" % patch_file) | ||
199 | f.close() | ||
200 | with open(patch_file, "r", encoding="iso8859-1") as f: | ||
201 | patch_text = f.read() | ||
202 | |||
203 | # Search for one or more "CVE: " lines | ||
204 | text_match = False | ||
205 | for match in cve_match.finditer(patch_text): | ||
206 | # Get only the CVEs without the "CVE: " tag | ||
207 | cves = patch_text[match.start()+5:match.end()] | ||
208 | for cve in cves.split(): | ||
209 | bb.debug(2, "Patch %s solves %s" % (patch_file, cve)) | ||
210 | patched_cves.add(cve) | ||
211 | text_match = True | ||
212 | |||
213 | if not fname_match and not text_match: | ||
214 | bb.debug(2, "Patch %s doesn't solve CVEs" % patch_file) | ||
215 | |||
216 | return patched_cves | ||
217 | |||
218 | def check_cves(d, patched_cves): | 160 | def check_cves(d, patched_cves): |
219 | """ | 161 | """ |
220 | Connect to the NVD database and find unpatched cves. | 162 | Connect to the NVD database and find unpatched cves. |