Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit ecc7c30

Browse files
committed
Adding yocto integration files
1 parent b466d4a commit ecc7c30

File tree

6 files changed

+1148
-1
lines changed

6 files changed

+1148
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ License
3939
-------
4040

4141
ISA FW and its plugins are available under the terms of the BSD 3-Clause License. Please check the LICENSE file for further details.
42-
42+
The integration of ISA FW is licensed under the license specified in each corresponding file under integration folder.
4343
Copyright (C) 2015 Intel Corporation

integration/yocto/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Usage
2+
-----
3+
4+
In order to use isafw during the image build, drop the isafw.bbclass
5+
to your target layer class folder, as well as other bb recipes to
6+
the corresponding recipe folder (recipes-devtools is a good default
7+
location). Then add the following line to your build/conf/local.conf
8+
file:
9+
10+
INHERIT += "isafw"
11+
12+
Also, some isafw plugins require network connection, so in case of a
13+
proxy setup please make sure to export http_proxy variable into your
14+
environment.
15+
16+
In order to produce image reports, you can execute image build
17+
normally. For example:
18+
19+
bitbake target-image
20+
21+
If you are only interested to produce a report based on packages
22+
and without building an image, please use:
23+
24+
bitbake -c analyse_sources_all target-image
25+
26+
27+
Logs
28+
----
29+
30+
All isafw plugins by default create their logs under the
31+
${LOG_DIR}/isafw-report/ directory, where ${LOG_DIR} is a bitbake
32+
default location for log files. If you wish to change this location,
33+
please define ISAFW_REPORTDIR variable in your local.conf file.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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

Comments
 (0)