Skip to contentSkip to conversation

Check compiled files to filter kernel CVEs

PATCH Superseded [email protected] 3 participants 9 messages Apr 16, 2025 – Apr 22, 2025

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover Check compiled files to filter kernel CVEs

From: Daniel Turull <[email protected]>

Since kernel.org became a CNA, more information is available in the published CVEs, including details about which files are affected by a given CVE.

This series adds functionality to fetch the database from kernel.org CNA information:
https://git.kernel.org/pub/scm/linux/security/vulns.git

To filter out CVEs that are not applicable, I developed a new bbclass that uses files compiled with the build kernel configuration. This approach requires a compiled kernel, which is why it's implemented as a new class rather than an extension of the existing cve-check.

In the future, an alternative approach could involve incorporating the affected files into SPDX rather than saving them independently and run an external tool to filter the CVEs.

Comments and suggestions are welcome.

Daniel Turull

Daniel Turull (2):
linux-vulns: fetch kernel.org CNA info
cve-check-kernel: verify kernel CVEs using programFile from kernel.org
CNA

 meta/classes/cve-check-kernel.bbclass     | 132 ++++++++++++++++++++++
 meta/conf/distro/include/maintainers.inc  |   1 +
 meta/recipes-core/meta/linux-vulns_git.bb |  42 +++++++
 3 files changed, 175 insertions(+)
 create mode 100644 meta/classes/cve-check-kernel.bbclass
 create mode 100644 meta/recipes-core/meta/linux-vulns_git.bb
#1 lore raw

PATCH 1/2 linux-vulns: fetch kernel.org CNA info

From: Daniel Turull <[email protected]>

Add CVE data source for kernel.org.

It includes more information than the one provided by NVD.

---
 meta/conf/distro/include/maintainers.inc  |  1 +
 meta/recipes-core/meta/linux-vulns_git.bb | 42 +++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 meta/recipes-core/meta/linux-vulns_git.bb
meta/conf/distro/include/maintainers.inc +1 -0
index 8065287c17..ec427fe6a4 100644
@@ -468,6 +468,7 @@ RECIPE_MAINTAINER:pn-lighttpd = "Unassigned <[email protected]>"
 RECIPE_MAINTAINER:pn-linux-dummy = "Unassigned <[email protected]>"
 RECIPE_MAINTAINER:pn-linux-firmware = "Otavio Salvador <[email protected]>"
 RECIPE_MAINTAINER:pn-linux-libc-headers = "Bruce Ashfield <[email protected]>"
+RECIPE_MAINTAINER:pn-linux-vulns = "Unassigned <[email protected]>"
 RECIPE_MAINTAINER:pn-linux-yocto = "Bruce Ashfield <[email protected]>"
 RECIPE_MAINTAINER:pn-linux-yocto-dev = "Bruce Ashfield <[email protected]>"
 RECIPE_MAINTAINER:pn-linux-yocto-rt = "Bruce Ashfield <[email protected]>"
meta/recipes-core/meta/linux-vulns_git.bb new file +42 -0

42 lines not on this page · raw message

#2 lore raw

RE: [PATCH 1/2] linux-vulns: fetch kernel.org CNA info

Hello,

this approach will not work.
DL_DIR can be shared across all (parallel) builds (also to multiple machines via NFS) so the symlinks would be overwriting each other. Also AUTOREV is not suitable for recipes in core as it makes bitbake to access network on each recipe parsing (so even when the class is not active).

Ideally this should have format like NVD/FKIE feeds where update timestamp is used to determine if update is needed or not. The data needs to be copied to folder within the build.

Peter

Quoted 96 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, April 16, 2025 16:29
To: [email protected]
Cc: [email protected]; [email protected]; Marko, Peter (FT D EU SK BFS1) <[email protected]>; [email protected]; [email protected]; Daniel Turull <[email protected]>
Subject: [PATCH 1/2] linux-vulns: fetch kernel.org CNA info

From: Daniel Turull <[email protected]>

Add CVE data source for kernel.org.

It includes more information than the one provided by NVD.

---
 meta/conf/distro/include/maintainers.inc  |  1 +
 meta/recipes-core/meta/linux-vulns_git.bb | 42 +++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 meta/recipes-core/meta/linux-vulns_git.bb
meta/conf/distro/include/maintainers.inc +0 -0
index 8065287c17..ec427fe6a4 100644
@@ -468,6 +468,7 @@ RECIPE_MAINTAINER:pn-lighttpd = "Unassigned

<[email protected]>"
RECIPE_MAINTAINER:pn-linux-dummy = "Unassigned
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-firmware = "Otavio Salvador
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-libc-headers = "Bruce Ashfield
<[email protected]>" +RECIPE_MAINTAINER:pn-linux-vulns = "Unassigned <[email protected]>"
RECIPE_MAINTAINER:pn-linux-yocto = "Bruce Ashfield
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-yocto-dev = "Bruce Ashfield
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-yocto-rt = "Bruce Ashfield
<[email protected]>"

meta/recipes- renamed from meta/recipes-core/meta/linux-vulns_git.bb +0 -0

core/meta/linux-vulns_git.bb
new file mode 100644
index 0000000000..158790f082

meta/recipes-core/meta/linux-vulns_git.bb new file +2 -0
@@ -0,0 +1,42 @@
+SUMMARY = "CVE information from kernel.org"
+DESCRIPTION = "Repo for tracking and maintaining the CVE identifiers reserved

\

meta/recipes-core/meta/linux-vulns_git.bb new file +4 -0
+and assigned to the Linux kernel project."
+HOMEPAGE = "https://git.kernel.org/pub/scm/linux/security/vulns.git/about/"
+LICENSE = "GPL-2.0-only & cve-tou"
+LIC_FILES_CHKSUM = "file://LICENSES/GPL-2.0-

only.txt;md5=c89d4ad08368966d8df5a90ea96bebe4\
+ file://LICENSES/cve-
tou.txt;md5=0d1f8ff7666c210e0b0404fd9d7e6703"

meta/recipes-core/meta/linux-vulns_git.bb new file +3 -0
+SECTION = "base"
+
+SRC_URI =

"git://git.kernel.org/pub/scm/linux/security/vulns;branch=master;protocol=https"

meta/recipes-core/meta/linux-vulns_git.bb new file +32 -0
+inherit native
+
+SRCREV="${AUTOREV}"
+PV = "1.0-git-${SRCREV}"
+
+S = "${WORKDIR}/git"
+
+KERNEL_CNA_REPO ??= "${DL_DIR}/CVE_CHECK/vulns"
+
+python do_unpack:append(){
+    # Make symbolic link so it is easy to find
+    import os
+    source_path = d.getVar("S")
+    link_path = d.getVar("KERNEL_CNA_REPO")
+    if os.path.exists(link_path):
+         os.remove(link_path)
+    bb.utils.mkdirhier(os.path.dirname(link_path))
+    os.symlink(source_path, link_path)
+}
+
+deltask do_patch
+deltask do_configure
+deltask do_compile
+deltask do_install
+deltask do_cve_check
+deltask do_populate_sysroot
+deltask do_runtime_spdx
+deltask do_create_spdx
+deltask do_populate_lic
+do_fetch[nostamp] = "1"
+
+EXCLUDE_FROM_WORLD = "1"
#3 lore raw

RE: [PATCH 1/2] linux-vulns: fetch kernel.org CNA info

Thanks for the reply Peter,

I'll look into have a similar approach as the NVD/FKIE feeds and submit a second version

I have also noticed that the autorev doesn't work towards premirrors.

Best regards,
Daniel

-----Original Message-----
From: Marko, Peter <[email protected]>
Sent: Thursday, 17 April 2025 10:20
To: Daniel Turull <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH 1/2] linux-vulns: fetch kernel.org CNA info

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Hello,

this approach will not work.
DL_DIR can be shared across all (parallel) builds (also to multiple machines via NFS) so the symlinks would be overwriting each other. Also AUTOREV is not suitable for recipes in core as it makes bitbake to access network on each recipe parsing (so even when the class is not active).

Ideally this should have format like NVD/FKIE feeds where update timestamp is used to determine if update is needed or not. The data needs to be copied to folder within the build.

Peter

Quoted 95 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, April 16, 2025 16:29
To: [email protected]
Cc: [email protected]; [email protected]; Marko, Peter (FT D EU SK BFS1) <[email protected]>; [email protected]; [email protected]; Daniel Turull <[email protected]>
Subject: [PATCH 1/2] linux-vulns: fetch kernel.org CNA info

From: Daniel Turull <[email protected]>

Add CVE data source for kernel.org.

It includes more information than the one provided by NVD.

---
 meta/conf/distro/include/maintainers.inc  |  1 +
meta/recipes-core/meta/linux-vulns_git.bb | 42 +++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 meta/recipes-core/meta/linux-vulns_git.bb
meta/conf/distro/include/maintainers.inc +0 -0
index 8065287c17..ec427fe6a4 100644
@@ -468,6 +468,7 @@ RECIPE_MAINTAINER:pn-lighttpd = "Unassigned

<[email protected]>"
RECIPE_MAINTAINER:pn-linux-dummy = "Unassigned
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-firmware = "Otavio Salvador
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-libc-headers = "Bruce Ashfield
<[email protected]>" +RECIPE_MAINTAINER:pn-linux-vulns = "Unassigned <[email protected]>"
RECIPE_MAINTAINER:pn-linux-yocto = "Bruce Ashfield
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-yocto-dev = "Bruce Ashfield
<[email protected]>"
RECIPE_MAINTAINER:pn-linux-yocto-rt = "Bruce Ashfield
<[email protected]>"

meta/recipes- renamed from meta/recipes-core/meta/linux-vulns_git.bb +0 -0

core/meta/linux-vulns_git.bb new file mode 100644 index 0000000000..158790f082

meta/recipes-core/meta/linux-vulns_git.bb new file +3 -0
@@ -0,0 +1,42 @@
+SUMMARY = "CVE information from kernel.org"
+DESCRIPTION = "Repo for tracking and maintaining the CVE identifiers
+reserved

\

meta/recipes-core/meta/linux-vulns_git.bb new file +4 -0
+and assigned to the Linux kernel project."
+HOMEPAGE = "https://git.kernel.org/pub/scm/linux/security/vulns.git/about/"
+LICENSE = "GPL-2.0-only & cve-tou"
+LIC_FILES_CHKSUM = "file://LICENSES/GPL-2.0-

only.txt;md5=c89d4ad08368966d8df5a90ea96bebe4\
+ file://LICENSES/cve-
tou.txt;md5=0d1f8ff7666c210e0b0404fd9d7e6703"

meta/recipes-core/meta/linux-vulns_git.bb new file +3 -0
+SECTION = "base"
+
+SRC_URI =

"git://git.kernel.org/pub/scm/linux/security/vulns;branch=master;protocol=https"

meta/recipes-core/meta/linux-vulns_git.bb new file +31 -0
+inherit native
+
+SRCREV="${AUTOREV}"
+PV = "1.0-git-${SRCREV}"
+
+S = "${WORKDIR}/git"
+
+KERNEL_CNA_REPO ??= "${DL_DIR}/CVE_CHECK/vulns"
+
+python do_unpack:append(){
+    # Make symbolic link so it is easy to find
+    import os
+    source_path = d.getVar("S")
+    link_path = d.getVar("KERNEL_CNA_REPO")
+    if os.path.exists(link_path):
+         os.remove(link_path)
+    bb.utils.mkdirhier(os.path.dirname(link_path))
+    os.symlink(source_path, link_path) }
+
+deltask do_patch
+deltask do_configure
+deltask do_compile
+deltask do_install
+deltask do_cve_check
+deltask do_populate_sysroot
+deltask do_runtime_spdx
+deltask do_create_spdx
+deltask do_populate_lic
+do_fetch[nostamp] = "1"
+
+EXCLUDE_FROM_WORLD = "1"
#4 lore raw

PATCH 2/2 cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

In reply to [email protected]

From: Daniel Turull <[email protected]>

This class is used to check the CVEs against a specific kernel configuration. Depends on data from the kernel.org CNA
https://git.kernel.org/pub/scm/linux/security/vulns.git
The CVE data provided by kernel.org includes the files that are affected by a given CVE and are provided as json files

It requires the kernel to be compiled to be able to extract which files are used. It is created as optional check on top cve-check.

To enable add in your local.conf
INHERIT += cve-check
INHERIT += cve-check-kernel

To test it
bitbake virtual/kernel -c cve_check_kernel

---
 meta/classes/cve-check-kernel.bbclass | 132 ++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
 create mode 100644 meta/classes/cve-check-kernel.bbclass
meta/classes/cve-check-kernel.bbclass new file +132 -0
new file mode 100644
index 0000000000..c01f0efd1d
@@ -0,0 +1,132 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+# This class is used to check the CVEs against a specific kernel configuration.
+# Depends on data from the kernel.org CNA
+# https://git.kernel.org/pub/scm/linux/security/vulns.git
+# The CVE data provided by kernel.org includes the files that are
+# affected by a given CVE and are provided as json files
+#
+# It requires the kernel to be compiled to be able to extract which files are
+# used. It is created as optional check on top cve-check.
+#
+# To enable add in your local.conf
+# INHERIT += cve-check
+# INHERIT += cve-check-kernel
+#
+# Then execute
+# bitbake virtual/kernel -c cve_check_kernel
+#
+
+KERNEL_FILES_DIR ?= "${LOG_DIR}/cve/kernel_files"
+KERNEL_SRC_FILES ?= "${KERNEL_FILES_DIR}/compile_commands.json"
+KERNEL_CNA_REPO ?= "${DL_DIR}/CVE_CHECK/vulns"
+
+python () {
+    if not bb.data.inherits_class("cve-check", d):
+        raise bb.parse.SkipRecipe("Skip cve-check-kernel when cve-check class is not loaded.")
+
+    if d.getVar('PN', True) == d.getVar("PREFERRED_PROVIDER_virtual/kernel", True):
+        bb.build.addtask('do_save_compiled_files', None, 'do_compile_kernelmodules', d)
+        bb.build.addtask('do_cve_check_kernel', 'do_build', None, d)
+        d.appendVarFlag('do_cve_check_kernel', 'depends', 'virtual/kernel:do_cve_check ')
+        d.appendVarFlag('do_cve_check_kernel', 'depends', 'virtual/kernel:do_compile_kernelmodules ')
+        d.appendVarFlag('do_cve_check_kernel', 'depends', 'linux-vulns:do_unpack ')
+}
+
+do_save_compiled_files() {
+    bbplain "Fetching compiled files"
+    mkdir -p ${KERNEL_FILES_DIR}
+    ${S}/scripts/clang-tools/gen_compile_commands.py -o ${KERNEL_SRC_FILES}
+}
+
+def get_files_in_cve(d, cve):
+    import os
+    import glob
+    import json
+    datadir = d.getVar('KERNEL_CNA_REPO', True)
+    pattern = os.path.join(datadir, '**', f"{cve}.json")
+    cve_files = glob.glob(pattern, recursive=True)
+    files_affected = []
+    if len(cve_files) == 0:
+        return None
+    # Assuming one match
+    with open(cve_files[0]) as f:
+        k_cve = json.load(f)
+        for item in k_cve['containers']['cna']['affected']:
+            if item["defaultStatus"] == "affected":
+                if "programFiles" in item:
+                    files = item['programFiles']
+                    files_affected.extend(files)
+    if len(files_affected) == 0:
+        return None
+    return files_affected
+
+python do_cve_check_kernel() {
+    import json
+    bb.plain("Updating CVEs using compiled files")
+    kfiles = []
+    cves = {}
+    affected= []
+
+    with open(d.getVar('KERNEL_SRC_FILES', True), 'r') as file:
+        for item in json.load(file):
+            kfiles.append(item['file'].replace(f"{d.getVar('S')}/",""))
+    bb.debug(1, f"Total used kernel source files: {len(kfiles)}")
+
+    # We want to use the file in log directory
+    deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
+    cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
+    direct_file = d.getVar("CVE_CHECK_LOG_JSON")
+    fragment_file = os.path.basename(deploy_file)
+    fragment_path = os.path.join(cvelogpath, fragment_file)
+
+    with open(fragment_path, 'r') as file:
+        cves = json.load(file)
+
+    total = 0
+    for cve in cves['package'][0]['issue']:
+        status = cve['status']
+        id = cve['id']
+
+        if status == 'Unpatched':
+             is_affected = False
+             total += 1
+             affected_files = get_files_in_cve(d, id)
+             if affected_files is None:
+                  bb.debug(1, f"No file information for {id}")
+                  affected.append(id)
+                  is_affected = True
+                  continue
+             for f in affected_files:
+                 if f in kfiles:
+                     bb.debug(1, f"File match in {id}: {f}")
+                     affected.append(id)
+                     is_affected = True
+                     break
+             if not is_affected:
+                  bb.debug(1, f"Changing status. Files in {id} not compiled. {affected_files}")
+                  cve["status"] = "Ignored"
+                  cve["detail"] = "not-applicable-config"
+                  cve["description"] = f"Source code not compiled by config. {affected_files}"
+
+    # Update cve files generated from cve-check
+    write_string = json.dumps(cves, indent=2)
+    with open(direct_file, 'w') as f:
+        bb.note("Writing file %s with CVE information" % direct_file)
+        f.write(write_string)
+    if d.getVar("CVE_CHECK_COPY_FILES") == "1":
+        with open(deploy_file, "w") as f:
+            f.write(write_string)
+    if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
+        with open(fragment_path, "w") as f:
+            f.write(write_string)
+
+    # Summary
+    bb.warn(f"Before filter we have {total} CVEs")
+    bb.warn(f"After programFile filter we have {len(affected)}")
+    bb.warn(f"Affected CVEs after filtering: {affected}")
+}
+
#5 lore raw

RE: [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

Hello,

I really like what is this new class trying to do.
Sadly, the kernel repo does not offer kernel config hints for CVEs, but the files should also filter out a lot.

Current implementation seems to only overwrite file generated by cve-check for kernel recipe, so it's suitable only for generating cve metrics. I see a major problem with this as it does not influence VEX and SPDX, which will become "the thing" very soon when the new regulations become active. Not sure how difficult it will be, but my recommendation would be to somehow get this integrated into oe.get_patched_cves function.

Also, since this class is not enabled by default, there need to be tests implemented to make sure that it does not break.

Peter

Quoted 182 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, April 16, 2025 16:29
To: [email protected]
Cc: [email protected]; [email protected]; Marko, Peter (FT D EU SK BFS1) <[email protected]>; [email protected]; [email protected]; Daniel Turull <[email protected]>
Subject: [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

From: Daniel Turull <[email protected]>

This class is used to check the CVEs against a specific kernel configuration. Depends on data from the kernel.org CNA
https://git.kernel.org/pub/scm/linux/security/vulns.git
The CVE data provided by kernel.org includes the files that are affected by a given CVE and are provided as json files

It requires the kernel to be compiled to be able to extract which files are used. It is created as optional check on top cve-check.

To enable add in your local.conf
INHERIT += cve-check
INHERIT += cve-check-kernel

To test it
bitbake virtual/kernel -c cve_check_kernel

---
 meta/classes/cve-check-kernel.bbclass | 132 ++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
 create mode 100644 meta/classes/cve-check-kernel.bbclass
meta/classes/cve-check- renamed from meta/classes/cve-check-kernel.bbclass +0 -0

kernel.bbclass
new file mode 100644
index 0000000000..c01f0efd1d

meta/classes/cve-check-kernel.bbclass new file +29 -0
@@ -0,0 +1,132 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+# This class is used to check the CVEs against a specific kernel configuration.
+# Depends on data from the kernel.org CNA
+# https://git.kernel.org/pub/scm/linux/security/vulns.git
+# The CVE data provided by kernel.org includes the files that are
+# affected by a given CVE and are provided as json files
+#
+# It requires the kernel to be compiled to be able to extract which files are
+# used. It is created as optional check on top cve-check.
+#
+# To enable add in your local.conf
+# INHERIT += cve-check
+# INHERIT += cve-check-kernel
+#
+# Then execute
+# bitbake virtual/kernel -c cve_check_kernel
+#
+
+KERNEL_FILES_DIR ?= "${LOG_DIR}/cve/kernel_files"
+KERNEL_SRC_FILES ?= "${KERNEL_FILES_DIR}/compile_commands.json"
+KERNEL_CNA_REPO ?= "${DL_DIR}/CVE_CHECK/vulns"
+
+python () {
+    if not bb.data.inherits_class("cve-check", d):
+        raise bb.parse.SkipRecipe("Skip cve-check-kernel when cve-check class is

not loaded.")

meta/classes/cve-check-kernel.bbclass new file +2 -0
+
+    if d.getVar('PN', True) == d.getVar("PREFERRED_PROVIDER_virtual/kernel",

True):
+ bb.build.addtask('do_save_compiled_files', None,
'do_compile_kernelmodules', d)

meta/classes/cve-check-kernel.bbclass new file +2 -0
+        bb.build.addtask('do_cve_check_kernel', 'do_build', None, d)
+        d.appendVarFlag('do_cve_check_kernel', 'depends',

'virtual/kernel:do_cve_check ')
+ d.appendVarFlag('do_cve_check_kernel', 'depends',
'virtual/kernel:do_compile_kernelmodules ')
+ d.appendVarFlag('do_cve_check_kernel', 'depends', 'linux-vulns:do_unpack
')

meta/classes/cve-check-kernel.bbclass new file +6 -0
+}
+
+do_save_compiled_files() {
+    bbplain "Fetching compiled files"
+    mkdir -p ${KERNEL_FILES_DIR}
+    ${S}/scripts/clang-tools/gen_compile_commands.py -o

${KERNEL_SRC_FILES}

meta/classes/cve-check-kernel.bbclass new file +68 -0
+}
+
+def get_files_in_cve(d, cve):
+    import os
+    import glob
+    import json
+    datadir = d.getVar('KERNEL_CNA_REPO', True)
+    pattern = os.path.join(datadir, '**', f"{cve}.json")
+    cve_files = glob.glob(pattern, recursive=True)
+    files_affected = []
+    if len(cve_files) == 0:
+        return None
+    # Assuming one match
+    with open(cve_files[0]) as f:
+        k_cve = json.load(f)
+        for item in k_cve['containers']['cna']['affected']:
+            if item["defaultStatus"] == "affected":
+                if "programFiles" in item:
+                    files = item['programFiles']
+                    files_affected.extend(files)
+    if len(files_affected) == 0:
+        return None
+    return files_affected
+
+python do_cve_check_kernel() {
+    import json
+    bb.plain("Updating CVEs using compiled files")
+    kfiles = []
+    cves = {}
+    affected= []
+
+    with open(d.getVar('KERNEL_SRC_FILES', True), 'r') as file:
+        for item in json.load(file):
+            kfiles.append(item['file'].replace(f"{d.getVar('S')}/",""))
+    bb.debug(1, f"Total used kernel source files: {len(kfiles)}")
+
+    # We want to use the file in log directory
+    deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
+    cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
+    direct_file = d.getVar("CVE_CHECK_LOG_JSON")
+    fragment_file = os.path.basename(deploy_file)
+    fragment_path = os.path.join(cvelogpath, fragment_file)
+
+    with open(fragment_path, 'r') as file:
+        cves = json.load(file)
+
+    total = 0
+    for cve in cves['package'][0]['issue']:
+        status = cve['status']
+        id = cve['id']
+
+        if status == 'Unpatched':
+             is_affected = False
+             total += 1
+             affected_files = get_files_in_cve(d, id)
+             if affected_files is None:
+                  bb.debug(1, f"No file information for {id}")
+                  affected.append(id)
+                  is_affected = True
+                  continue
+             for f in affected_files:
+                 if f in kfiles:
+                     bb.debug(1, f"File match in {id}: {f}")
+                     affected.append(id)
+                     is_affected = True
+                     break
+             if not is_affected:
+                  bb.debug(1, f"Changing status. Files in {id} not compiled.

{affected_files}")

meta/classes/cve-check-kernel.bbclass new file +3 -0
+                  cve["status"] = "Ignored"
+                  cve["detail"] = "not-applicable-config"
+                  cve["description"] = f"Source code not compiled by config.

{affected_files}"

meta/classes/cve-check-kernel.bbclass new file +19 -0
+
+    # Update cve files generated from cve-check
+    write_string = json.dumps(cves, indent=2)
+    with open(direct_file, 'w') as f:
+        bb.note("Writing file %s with CVE information" % direct_file)
+        f.write(write_string)
+    if d.getVar("CVE_CHECK_COPY_FILES") == "1":
+        with open(deploy_file, "w") as f:
+            f.write(write_string)
+    if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
+        with open(fragment_path, "w") as f:
+            f.write(write_string)
+
+    # Summary
+    bb.warn(f"Before filter we have {total} CVEs")
+    bb.warn(f"After programFile filter we have {len(affected)}")
+    bb.warn(f"Affected CVEs after filtering: {affected}")
+}
+
#6 lore raw

RE: [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

Hi Peter,
Thanks for the suggestions.

The data to filter relies on the information on the CVE, so we could potentially filter out at the time of the build in the vex file, once I fix the previous patch. But this will become outdated for the new CVEs.

I'll try to update the SPDX generation to include only the compiled files and not all as it is now.

Then it could probably be used by cve-check, vex or an external tool using spdx information.

Best regards,
Daniel

-----Original Message-----
From: Marko, Peter <[email protected]>
Sent: Thursday, 17 April 2025 10:56
To: Daniel Turull <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Hello,

I really like what is this new class trying to do.
Sadly, the kernel repo does not offer kernel config hints for CVEs, but the files should also filter out a lot.

Current implementation seems to only overwrite file generated by cve-check for kernel recipe, so it's suitable only for generating cve metrics. I see a major problem with this as it does not influence VEX and SPDX, which will become "the thing" very soon when the new regulations become active. Not sure how difficult it will be, but my recommendation would be to somehow get this integrated into oe.get_patched_cves function.

Also, since this class is not enabled by default, there need to be tests implemented to make sure that it does not break.

Peter

Quoted 192 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, April 16, 2025 16:29
To: [email protected]
Cc: [email protected]; [email protected]; Marko, Peter (FT D EU SK BFS1) <[email protected]>; [email protected]; [email protected]; Daniel Turull <[email protected]>
Subject: [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

From: Daniel Turull <[email protected]>

This class is used to check the CVEs against a specific kernel configuration. Depends on data from the kernel.org CNA
https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgit.
kernel.org%2Fpub%2Fscm%2Flinux%2Fsecurity%2Fvulns.git&data=05%7C02%7Cd aniel.turull%40ericsson.com%7C31a9203398c447bfef3408dd7d8dbf07%7C92e84 cebfbfd47abbe52080c6b87953f%7C0%7C0%7C638804769948856423%7CUnknown%7CT WFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiI sIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=Q0WxBfDbU13MSWCrg Jk3J6QT4d%2F5YDBsPXtyBcD85ow%3D&reserved=0
The CVE data provided by kernel.org includes the files that are affected by a given CVE and are provided as json files

It requires the kernel to be compiled to be able to extract which files are used. It is created as optional check on top cve-check.

To enable add in your local.conf
INHERIT += cve-check
INHERIT += cve-check-kernel

To test it
bitbake virtual/kernel -c cve_check_kernel

---
 meta/classes/cve-check-kernel.bbclass | 132 
++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
 create mode 100644 meta/classes/cve-check-kernel.bbclass
(quoted diff) +0 -0

b/meta/classes/cve-check- kernel.bbclass new file mode 100644 index 0000000000..c01f0efd1d

meta/classes/cve-check-kernel.bbclass new file +32 -0
@@ -0,0 +1,132 @@
+#
+# Copyright OpenEmbedded Contributors # # SPDX-License-Identifier: 
+MIT # # This class is used to check the CVEs against a specific 
+kernel configuration.
+# Depends on data from the kernel.org CNA # 
+https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgit
+.kernel.org%2Fpub%2Fscm%2Flinux%2Fsecurity%2Fvulns.git&data=05%7C02%7
+Cdaniel.turull%40ericsson.com%7C31a9203398c447bfef3408dd7d8dbf07%7C92
+e84cebfbfd47abbe52080c6b87953f%7C0%7C0%7C638804769948876469%7CUnknown
+%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW
+4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=dB9lDl%2Bl%
+2BeySgLjiiDcz7IiplC7pB4fvWnN8Gl6ljD0%3D&reserved=0
+# The CVE data provided by kernel.org includes the files that are # 
+affected by a given CVE and are provided as json files # # It 
+requires the kernel to be compiled to be able to extract which files 
+are # used. It is created as optional check on top cve-check.
+#
+# To enable add in your local.conf
+# INHERIT += cve-check
+# INHERIT += cve-check-kernel
+#
+# Then execute
+# bitbake virtual/kernel -c cve_check_kernel #
+
+KERNEL_FILES_DIR ?= "${LOG_DIR}/cve/kernel_files"
+KERNEL_SRC_FILES ?= "${KERNEL_FILES_DIR}/compile_commands.json"
+KERNEL_CNA_REPO ?= "${DL_DIR}/CVE_CHECK/vulns"
+
+python () {
+    if not bb.data.inherits_class("cve-check", d):
+        raise bb.parse.SkipRecipe("Skip cve-check-kernel when 
+cve-check class is

not loaded.")

meta/classes/cve-check-kernel.bbclass new file +3 -0
+
+    if d.getVar('PN', True) == 
+ d.getVar("PREFERRED_PROVIDER_virtual/kernel",

True):
+ bb.build.addtask('do_save_compiled_files', None,
'do_compile_kernelmodules', d)

meta/classes/cve-check-kernel.bbclass new file +2 -0
+        bb.build.addtask('do_cve_check_kernel', 'do_build', None, d)
+        d.appendVarFlag('do_cve_check_kernel', 'depends',

'virtual/kernel:do_cve_check ')
+ d.appendVarFlag('do_cve_check_kernel', 'depends',
'virtual/kernel:do_compile_kernelmodules ')
+ d.appendVarFlag('do_cve_check_kernel', 'depends',
+ 'linux-vulns:do_unpack
')

meta/classes/cve-check-kernel.bbclass new file +6 -0
+}
+
+do_save_compiled_files() {
+    bbplain "Fetching compiled files"
+    mkdir -p ${KERNEL_FILES_DIR}
+    ${S}/scripts/clang-tools/gen_compile_commands.py -o

${KERNEL_SRC_FILES}

meta/classes/cve-check-kernel.bbclass new file +68 -0
+}
+
+def get_files_in_cve(d, cve):
+    import os
+    import glob
+    import json
+    datadir = d.getVar('KERNEL_CNA_REPO', True)
+    pattern = os.path.join(datadir, '**', f"{cve}.json")
+    cve_files = glob.glob(pattern, recursive=True)
+    files_affected = []
+    if len(cve_files) == 0:
+        return None
+    # Assuming one match
+    with open(cve_files[0]) as f:
+        k_cve = json.load(f)
+        for item in k_cve['containers']['cna']['affected']:
+            if item["defaultStatus"] == "affected":
+                if "programFiles" in item:
+                    files = item['programFiles']
+                    files_affected.extend(files)
+    if len(files_affected) == 0:
+        return None
+    return files_affected
+
+python do_cve_check_kernel() {
+    import json
+    bb.plain("Updating CVEs using compiled files")
+    kfiles = []
+    cves = {}
+    affected= []
+
+    with open(d.getVar('KERNEL_SRC_FILES', True), 'r') as file:
+        for item in json.load(file):
+            kfiles.append(item['file'].replace(f"{d.getVar('S')}/",""))
+    bb.debug(1, f"Total used kernel source files: {len(kfiles)}")
+
+    # We want to use the file in log directory
+    deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
+    cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
+    direct_file = d.getVar("CVE_CHECK_LOG_JSON")
+    fragment_file = os.path.basename(deploy_file)
+    fragment_path = os.path.join(cvelogpath, fragment_file)
+
+    with open(fragment_path, 'r') as file:
+        cves = json.load(file)
+
+    total = 0
+    for cve in cves['package'][0]['issue']:
+        status = cve['status']
+        id = cve['id']
+
+        if status == 'Unpatched':
+             is_affected = False
+             total += 1
+             affected_files = get_files_in_cve(d, id)
+             if affected_files is None:
+                  bb.debug(1, f"No file information for {id}")
+                  affected.append(id)
+                  is_affected = True
+                  continue
+             for f in affected_files:
+                 if f in kfiles:
+                     bb.debug(1, f"File match in {id}: {f}")
+                     affected.append(id)
+                     is_affected = True
+                     break
+             if not is_affected:
+                  bb.debug(1, f"Changing status. Files in {id} not compiled.

{affected_files}")

meta/classes/cve-check-kernel.bbclass new file +3 -0
+                  cve["status"] = "Ignored"
+                  cve["detail"] = "not-applicable-config"
+                  cve["description"] = f"Source code not compiled by config.

{affected_files}"

meta/classes/cve-check-kernel.bbclass new file +18 -0
+
+    # Update cve files generated from cve-check
+    write_string = json.dumps(cves, indent=2)
+    with open(direct_file, 'w') as f:
+        bb.note("Writing file %s with CVE information" % direct_file)
+        f.write(write_string)
+    if d.getVar("CVE_CHECK_COPY_FILES") == "1":
+        with open(deploy_file, "w") as f:
+            f.write(write_string)
+    if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
+        with open(fragment_path, "w") as f:
+            f.write(write_string)
+
+    # Summary
+    bb.warn(f"Before filter we have {total} CVEs")
+    bb.warn(f"After programFile filter we have {len(affected)}")
+    bb.warn(f"Affected CVEs after filtering: {affected}") }
+
#7 lore raw

Re: [OE-core] [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

In reply to [email protected]

Hi Daniel,

Quoted 53 lines · Daniel Turull via lists.openembedded.org

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

From: Daniel Turull <[email protected]>

This class is used to check the CVEs against a specific kernel configuration. Depends on data from the kernel.org CNA
https://git.kernel.org/pub/scm/linux/security/vulns.git
The CVE data provided by kernel.org includes the files that are affected by a given CVE and are provided as json files

It requires the kernel to be compiled to be able to extract which files are used. It is created as optional check on top cve-check.

To enable add in your local.conf
INHERIT += cve-check
INHERIT += cve-check-kernel

To test it
bitbake virtual/kernel -c cve_check_kernel

---
  meta/classes/cve-check-kernel.bbclass | 132 ++++++++++++++++++++++++++
  1 file changed, 132 insertions(+)

create mode 100644 meta/classes/cve-check-kernel.bbclass

meta/classes/cve-check-kernel.bbclass new file +21 -0
new file mode 100644
index 0000000000..c01f0efd1d
@@ -0,0 +1,132 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+# This class is used to check the CVEs against a specific kernel configuration.
+# Depends on data from the kernel.org CNA
+# https://git.kernel.org/pub/scm/linux/security/vulns.git
+# The CVE data provided by kernel.org includes the files that are
+# affected by a given CVE and are provided as json files
+#
+# It requires the kernel to be compiled to be able to extract which files are
+# used. It is created as optional check on top cve-check.
+#
+# To enable add in your local.conf
+# INHERIT += cve-check
+# INHERIT += cve-check-kernel
+#
+# Then execute
+# bitbake virtual/kernel -c cve_check_kernel
+#

meta/classes/cve-check-kernel.bbclass line 31

Quoted 10 lines
meta/classes/cve-check-kernel.bbclass new file +10 -0
+
+KERNEL_FILES_DIR ?= "${LOG_DIR}/cve/kernel_files"
+KERNEL_SRC_FILES ?= "${KERNEL_FILES_DIR}/compile_commands.json"
+KERNEL_CNA_REPO ?= "${DL_DIR}/CVE_CHECK/vulns"
+
+python () {
+    if not bb.data.inherits_class("cve-check", d):
+        raise bb.parse.SkipRecipe("Skip cve-check-kernel when cve-check class is not loaded.")
+
+    if d.getVar('PN', True) == d.getVar("PREFERRED_PROVIDER_virtual/kernel", True):

I don't think this will work reliably?

I think you could have another virtual provider in PREFERRED_PROVIDER_virtual/kernel and then this wouldn't match.

I have no clue how to handle that properly.

I'm not sure how to make sure we're always using the latest linux-vulns database (and so not rely on the cache(s)) but at the same time allow building without network access. I guess we could provide some hash or tarball for linux-vulns by default, for building without network access?

Cheers,
Quentin

#8 lore raw

RE: [OE-core] [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

Thanks Quentin for pointing it out. I'll rewrite both patches after Peter's comments.

I could use what is in linux-yocto:

d.getVar("KERNEL_PACKAGE_NAME") == "kernel" and d.getVar("PREFERRED_PROVIDER_virtual/kernel") != d.getVar("PN")

or also check the CVE_PRODUCT is "linux_kernel", since these are the CVEs that we care.

Daniel

-----Original Message-----
From: Quentin Schulz <[email protected]>
Sent: Friday, 18 April 2025 13:29
To: Daniel Turull <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [OE-core] [PATCH 2/2] cve-check-kernel: verify kernel CVEs using programFile from kernel.org CNA

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Hi Daniel,

Quoted 65 lines · Daniel Turull via lists.openembedded.org

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

From: Daniel Turull <[email protected]>

This class is used to check the CVEs against a specific kernel configuration. Depends on data from the kernel.org CNA
https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgit.
kernel.org%2Fpub%2Fscm%2Flinux%2Fsecurity%2Fvulns.git&data=05%7C02%7Cd aniel.turull%40ericsson.com%7C863488aa743246e0ca1308dd7e6c3ddd%7C92e84 cebfbfd47abbe52080c6b87953f%7C0%7C0%7C638805725556277888%7CUnknown%7CT WFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiI sIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=uT1dd7yOnr4v8B%2B VLjfZboS5H4mjMnu0TATcJJCoX3k%3D&reserved=0
The CVE data provided by kernel.org includes the files that are affected by a given CVE and are provided as json files

It requires the kernel to be compiled to be able to extract which files are used. It is created as optional check on top cve-check.

To enable add in your local.conf
INHERIT += cve-check
INHERIT += cve-check-kernel

To test it
bitbake virtual/kernel -c cve_check_kernel

---
  meta/classes/cve-check-kernel.bbclass | 132 ++++++++++++++++++++++++++
  1 file changed, 132 insertions(+)

create mode 100644 meta/classes/cve-check-kernel.bbclass

meta/classes/cve-check-kernel.bbclass new file +24 -0
new file mode 100644
index 0000000000..c01f0efd1d
@@ -0,0 +1,132 @@
+#
+# Copyright OpenEmbedded Contributors # # SPDX-License-Identifier: 
+MIT # # This class is used to check the CVEs against a specific 
+kernel configuration.
+# Depends on data from the kernel.org CNA # 
+https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgit
+.kernel.org%2Fpub%2Fscm%2Flinux%2Fsecurity%2Fvulns.git&data=05%7C02%7
+Cdaniel.turull%40ericsson.com%7C863488aa743246e0ca1308dd7e6c3ddd%7C92
+e84cebfbfd47abbe52080c6b87953f%7C0%7C0%7C638805725556296524%7CUnknown
+%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW
+4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=hScueht1OLz
+GKBqh3g5IuiRTKN3ulPwBpCuRruJu%2BaQ%3D&reserved=0
+# The CVE data provided by kernel.org includes the files that are # 
+affected by a given CVE and are provided as json files # # It 
+requires the kernel to be compiled to be able to extract which files 
+are # used. It is created as optional check on top cve-check.
+#
+# To enable add in your local.conf
+# INHERIT += cve-check
+# INHERIT += cve-check-kernel
+#
+# Then execute
+# bitbake virtual/kernel -c cve_check_kernel #
+

meta/classes/cve-check-kernel.bbclass line 34

Quoted 10 lines
meta/classes/cve-check-kernel.bbclass new file +10 -0
+KERNEL_FILES_DIR ?= "${LOG_DIR}/cve/kernel_files"
+KERNEL_SRC_FILES ?= "${KERNEL_FILES_DIR}/compile_commands.json"
+KERNEL_CNA_REPO ?= "${DL_DIR}/CVE_CHECK/vulns"
+
+python () {
+    if not bb.data.inherits_class("cve-check", d):
+        raise bb.parse.SkipRecipe("Skip cve-check-kernel when 
+cve-check class is not loaded.")
+
+    if d.getVar('PN', True) == d.getVar("PREFERRED_PROVIDER_virtual/kernel", True):

I don't think this will work reliably?

I think you could have another virtual provider in PREFERRED_PROVIDER_virtual/kernel and then this wouldn't match.

I have no clue how to handle that properly.

I'm not sure how to make sure we're always using the latest linux-vulns database (and so not rely on the cache(s)) but at the same time allow building without network access. I guess we could provide some hash or tarball for linux-vulns by default, for building without network access?

Cheers,
Quentin