|
| 1 | +# Security scanning class |
| 2 | +# |
| 3 | +# Based in part on buildhistory.bbclass which was in turn based on |
| 4 | +# testlab.bbclass and packagehistory.bbclass |
| 5 | +# |
| 6 | +# Copyright (C) 2011-2015 Intel Corporation |
| 7 | +# Copyright (C) 2007-2011 Koen Kooi <koen@openembedded.org> |
| 8 | +# |
| 9 | + |
| 10 | +LICENSE = "MIT" |
| 11 | + |
| 12 | +ISAFW_WORKDIR = "${WORKDIR}/isafw" |
| 13 | +ISAFW_REPORTDIR ?= "${LOG_DIR}/isafw-report" |
| 14 | + |
| 15 | +do_analyse_sources[depends] += "cve-check-tool-native:do_populate_sysroot" |
| 16 | +do_analyse_sources[depends] += "rpm-native:do_populate_sysroot" |
| 17 | +do_analyse_sources[nostamp] = "1" |
| 18 | + |
| 19 | +# First, code to handle scanning each recipe that goes into the build |
| 20 | + |
| 21 | +do_analyse_sources[cleandirs] = "${ISAFW_WORKDIR}" |
| 22 | + |
| 23 | +python do_analyse_sources() { |
| 24 | + |
| 25 | + from isafw import * |
| 26 | + |
| 27 | + proxy = d.getVar('HTTP_PROXY', True) |
| 28 | + if not proxy : |
| 29 | + proxy = d.getVar('http_proxy', True) |
| 30 | + bb.debug(1, 'isafw: proxy is %s' % proxy) |
| 31 | + |
| 32 | + reportdir = d.getVar('ISAFW_REPORTDIR', True) |
| 33 | + if not os.path.exists(os.path.dirname(reportdir+"/internal/test")): |
| 34 | + os.makedirs(os.path.dirname(reportdir+"/internal/test")) |
| 35 | + |
| 36 | + imageSecurityAnalyser = isafw.ISA(proxy, reportdir) |
| 37 | + |
| 38 | + if not d.getVar('SRC_URI', True): |
| 39 | + # Recipe didn't fetch any sources, nothing to do here I assume? |
| 40 | + return |
| 41 | +
|
| 42 | + # Unpack the sources again, because we need the pristine sources |
| 43 | + # (we could do this after do_unpack instead and save some time, but that |
| 44 | + # would necessitate having a way of restoring the results of the scan |
| 45 | + # from sstate as well) |
| 46 | +
|
| 47 | + fetch = bb.fetch2.Fetch([], d) |
| 48 | + for url in fetch.urls: |
| 49 | + workdir = d.getVar('ISAFW_WORKDIR', True) |
| 50 | + fetch.unpack(workdir, (url,)) |
| 51 | +
|
| 52 | + recipe = isafw.ISA_package() |
| 53 | + recipe.name = d.getVar('PN', True) |
| 54 | + recipe.version = d.getVar('PV', True) |
| 55 | + licenses = d.getVar('LICENSE', True) |
| 56 | + licenses = licenses.replace("(", "") |
| 57 | + licenses = licenses.replace(")", "") |
| 58 | + recipe.licenses = licenses.split() |
| 59 | + while '|' in recipe.licenses: |
| 60 | + recipe.licenses.remove('|') |
| 61 | + while '&' in recipe.licenses: |
| 62 | + recipe.licenses.remove('&') |
| 63 | + # translate to proper format |
| 64 | + spdlicense = [] |
| 65 | + for l in recipe.licenses: |
| 66 | + spdlicense.append(canonical_license(d, l)) |
| 67 | + recipe.licenses = spdlicense |
| 68 | + recipe.path_to_sources = workdir |
| 69 | + |
| 70 | + for patch in src_patches(d): |
| 71 | + _,_,local,_,_,_=bb.fetch.decodeurl(patch) |
| 72 | + recipe.patch_files.append(os.path.basename(local)) |
| 73 | + if (not recipe.patch_files) : |
| 74 | + recipe.patch_files.append("None") |
| 75 | + # Pass the recipe object to the security framework |
| 76 | +
|
| 77 | + bb.debug(1, '%s: analyse sources in %s' % (d.getVar('PN', True), workdir)) |
| 78 | + imageSecurityAnalyser.process_package(recipe) |
| 79 | +
|
| 80 | + # If we're unpacking our own sources we might want to discard them at this point |
| 81 | + |
| 82 | + return |
| 83 | +} |
| 84 | + |
| 85 | +addtask do_analyse_sources after do_fetch before do_build |
| 86 | + |
| 87 | +addtask analyse_sources_all after do_analyse_sources |
| 88 | +do_analyse_sources_all[recrdeptask] = "do_analyse_sources_all do_analyse_sources" |
| 89 | +do_analyse_sources_all[recrdeptask] = "do_${BB_DEFAULT_TASK}" |
| 90 | +do_analyse_sources_all() { |
| 91 | + : |
| 92 | +} |
| 93 | + |
| 94 | +python() { |
| 95 | + # We probably don't need to scan native/cross |
| 96 | + if bb.data.inherits_class('native', d) or bb.data.inherits_class('cross', d): |
| 97 | + bb.build.deltask('do_analyse_sources', d) |
| 98 | +} |
| 99 | +
|
| 100 | +python analyse_image() { |
| 101 | +
|
| 102 | + # Directory where the image's entire contents can be examined |
| 103 | + rootfsdir = d.getVar('IMAGE_ROOTFS', True) |
| 104 | + |
| 105 | + imagebasename = d.getVar('IMAGE_BASENAME', True) |
| 106 | + reportdir = d.getVar('ISAFW_REPORTDIR', True) |
| 107 | + |
| 108 | + proxy = d.getVar('HTTP_PROXY', True) |
| 109 | + if not proxy : |
| 110 | + proxy = d.getVar('http_proxy', True) |
| 111 | + |
| 112 | + bb.debug(1, 'isafw: proxy is %s' % proxy) |
| 113 | + |
| 114 | + from isafw import * |
| 115 | + imageSecurityAnalyser = isafw.ISA(proxy, reportdir) |
| 116 | + |
| 117 | + pkglist = manifest2pkglist(d) |
| 118 | + |
| 119 | + kernelconf = d.getVar('STAGING_KERNEL_BUILDDIR', True) + "/.config" |
| 120 | + |
| 121 | + kernel = isafw.ISA_kernel() |
| 122 | + kernel.img_name = imagebasename |
| 123 | + kernel.path_to_config = kernelconf |
| 124 | + |
| 125 | + bb.debug(1, 'do kernel conf analysis on %s' % kernelconf) |
| 126 | + imageSecurityAnalyser.process_kernel(kernel) |
| 127 | + |
| 128 | + pkg_list = isafw.ISA_pkg_list() |
| 129 | + pkg_list.img_name = imagebasename |
| 130 | + pkg_list.path_to_list = pkglist |
| 131 | + |
| 132 | + bb.debug(1, 'do pkg list analysis on %s' % pkglist) |
| 133 | + imageSecurityAnalyser.process_pkg_list(pkg_list) |
| 134 | + |
| 135 | + fs = isafw.ISA_filesystem() |
| 136 | + fs.img_name = imagebasename |
| 137 | + fs.path_to_fs = rootfsdir |
| 138 | + |
| 139 | + bb.debug(1, 'do image analysis on %s' % rootfsdir) |
| 140 | + imageSecurityAnalyser.process_filesystem(fs) |
| 141 | +} |
| 142 | + |
| 143 | +do_rootfs[depends] += "checksec-native:do_populate_sysroot" |
| 144 | + |
| 145 | +# Ensure we wait until source analysis gets done for all recipes whose packages |
| 146 | +# are going into the image before starting with the image itself |
| 147 | +do_rootfs[recrdeptask] += "do_analyse_sources" |
| 148 | + |
| 149 | +analyse_image[fakeroot] = "1" |
| 150 | + |
| 151 | +def manifest2pkglist(d): |
| 152 | + |
| 153 | + manifest_file = d.getVar('IMAGE_MANIFEST', True) |
| 154 | + imagebasename = d.getVar('IMAGE_BASENAME', True) |
| 155 | + reportdir = d.getVar('ISAFW_REPORTDIR', True) |
| 156 | + |
| 157 | + pkglist = reportdir + "/internal/pkglist_" + imagebasename |
| 158 | + |
| 159 | + with open(pkglist, 'w') as foutput: |
| 160 | + with open(manifest_file, 'r') as finput: |
| 161 | + for line in finput: |
| 162 | + items = line.split() |
| 163 | + foutput.write(items[0] + " " + items[2] + "\n") |
| 164 | + |
| 165 | + return pkglist |
| 166 | + |
| 167 | + |
| 168 | +IMAGE_POSTPROCESS_COMMAND += " analyse_image ; " |
| 169 | + |
| 170 | +# NOTE: by the time IMAGE_POSTPROCESS_COMMAND items are called, the image |
| 171 | +# has been stripped of the package manager database (if runtime package management |
| 172 | +# is not enabled, i.e. 'package-management' is not in IMAGE_FEATURES). If you |
| 173 | +# do want to be using the package manager to operate on the image contents, you'll |
| 174 | +# need to call your function from ROOTFS_POSTINSTALL_COMMAND or |
| 175 | +# ROOTFS_POSTUNINSTALL_COMMAND instead - however if you do that you should then be |
| 176 | +# aware that what you'll be looking at isn't exactly what you will see in the image |
| 177 | +# at runtime (there will be other postprocessing functions called after yours). |
| 178 | +
|
| 179 | +# this will be run once per build in order to initialize and cleanup report dir |
| 180 | +python isafwreport_handler () { |
| 181 | +
|
| 182 | + import shutil |
| 183 | +
|
| 184 | + reportdir = e.data.getVar('ISAFW_REPORTDIR', True) |
| 185 | + if os.path.exists(os.path.dirname(reportdir+"/internal/test")): |
| 186 | + shutil.rmtree(reportdir) |
| 187 | + os.makedirs(os.path.dirname(reportdir+"/internal/test")) |
| 188 | +
|
| 189 | +} |
| 190 | +addhandler isafwreport_handler |
| 191 | +isafwreport_handler[eventmask] = "bb.event.BuildStarted" |
| 192 | +
|
0 commit comments