blob: 2534b205b96d3f7474062a8350fcd59dbf452923 [file] [log] [blame]
Wei Li73032c82023-03-15 18:31:38 +00001#!/bin/bash
2
3# Copyright (C) 2023 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -uo pipefail
18
19# Integration test for verifying generated SBOM for cuttlefish device.
20
21if [ ! -e "build/make/core/Makefile" ]; then
22 echo "$0 must be run from the top of the Android source tree."
23 exit 1
24fi
25
Wei Licd9948f2023-06-22 17:52:31 -070026function setup {
27 tmp_dir="$(mktemp -d tmp.XXXXXX)"
28 trap 'cleanup "${tmp_dir}"' EXIT
29 echo "${tmp_dir}"
30}
31
Wei Li73032c82023-03-15 18:31:38 +000032function cleanup {
Wei Licd9948f2023-06-22 17:52:31 -070033 tmp_dir="$1"; shift
Wei Li73032c82023-03-15 18:31:38 +000034 rm -rf "${tmp_dir}"
35}
Wei Li8a8d5a92023-03-29 13:41:34 -070036
37function run_soong {
Wei Licd9948f2023-06-22 17:52:31 -070038 target_product="$1";shift
39 out_dir="$1"; shift
40 targets="$1"; shift
41 if [ "$#" -ge 1 ]; then
42 apps=$1; shift
43 TARGET_PRODUCT="${target_product}" TARGET_BUILD_VARIANT=userdebug OUT_DIR="${out_dir}" TARGET_BUILD_UNBUNDLED=true TARGET_BUILD_APPS=$apps build/soong/soong_ui.bash --make-mode ${targets}
44 else
45 TARGET_PRODUCT="${target_product}" TARGET_BUILD_VARIANT=userdebug OUT_DIR="${out_dir}" build/soong/soong_ui.bash --make-mode ${targets}
46 fi
Wei Li8a8d5a92023-03-29 13:41:34 -070047}
48
Wei Li7c1e7f02023-03-29 17:43:35 -070049function diff_files {
Wei Licd9948f2023-06-22 17:52:31 -070050 file_list_file="$1"; shift
51 files_in_spdx_file="$1"; shift
52 partition_name="$1"; shift
53 exclude=
54 if [ -v 'diff_excludes[$partition_name]' ]; then
55 exclude=${diff_excludes[$partition_name]}
Wei Li8a8d5a92023-03-29 13:41:34 -070056 fi
Wei Li8a8d5a92023-03-29 13:41:34 -070057
Wei Licd9948f2023-06-22 17:52:31 -070058 diff "$file_list_file" "$files_in_spdx_file" $exclude
59 if [ $? != "0" ]; then
60 echo Found diffs in $f and SBOM.
61 exit 1
62 else
63 echo No diffs.
64 fi
65}
Wei Li8a8d5a92023-03-29 13:41:34 -070066
Wei Licd9948f2023-06-22 17:52:31 -070067function test_sbom_aosp_cf_x86_64_phone {
68 # Setup
69 out_dir="$(setup)"
Wei Li8a8d5a92023-03-29 13:41:34 -070070
Wei Licd9948f2023-06-22 17:52:31 -070071 # Test
72 # m droid, build sbom later in case additional dependencies might be built and included in partition images.
73 run_soong "aosp_cf_x86_64_phone" "${out_dir}" "droid dump.erofs lz4"
Wei Li8a8d5a92023-03-29 13:41:34 -070074
Wei Licd9948f2023-06-22 17:52:31 -070075 product_out=$out_dir/target/product/vsoc_x86_64
76 sbom_test=$product_out/sbom_test
77 mkdir -p $sbom_test
78 cp $product_out/*.img $sbom_test
79
80 # m sbom
81 run_soong "aosp_cf_x86_64_phone" "${out_dir}" sbom
82
83 # Generate installed file list from .img files in PRODUCT_OUT
84 dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs
85 lz4=$out_dir/host/linux-x86/bin/lz4
86
87 declare -A diff_excludes
Wei Li08246b92023-07-17 11:24:54 -070088 diff_excludes[product]="\
89 -I /product/etc/aconfig_flags.textproto \
90 -I /product/etc/build_flags.json"
91 diff_excludes[vendor]="\
92 -I /vendor/lib64/libkeystore2_crypto.so \
93 -I /vendor/etc/aconfig_flags.textproto \
94 -I /vendor/etc/build_flags.json"
Wei Licd9948f2023-06-22 17:52:31 -070095 diff_excludes[system]="\
96 -I /bin \
97 -I /bugreports \
98 -I /cache \
99 -I /d \
100 -I /etc \
101 -I /init \
102 -I /odm/app \
103 -I /odm/bin \
104 -I /odm_dlkm/etc \
105 -I /odm/etc \
106 -I /odm/firmware \
107 -I /odm/framework \
108 -I /odm/lib \
109 -I /odm/lib64 \
110 -I /odm/overlay \
111 -I /odm/priv-app \
112 -I /odm/usr \
113 -I /sdcard \
Wei Li08246b92023-07-17 11:24:54 -0700114 -I /system/etc/aconfig_flags.textproto \
115 -I /system/etc/build_flags.json \
Wei Licd9948f2023-06-22 17:52:31 -0700116 -I /system/lib64/android.hardware.confirmationui@1.0.so \
117 -I /system/lib64/android.hardware.confirmationui-V1-ndk.so \
118 -I /system/lib64/android.hardware.keymaster@4.1.so \
119 -I /system/lib64/android.hardware.security.rkp-V3-ndk.so \
120 -I /system/lib64/android.hardware.security.sharedsecret-V1-ndk.so \
121 -I /system/lib64/android.security.compat-ndk.so \
Wei Li9a8df662023-07-13 10:29:55 -0700122 -I /system/lib64/libcuttlefish_allocd_utils.so \
123 -I /system/lib64/libcuttlefish_device_config_proto.so \
124 -I /system/lib64/libcuttlefish_device_config.so \
125 -I /system/lib64/libcuttlefish_fs.so \
126 -I /system/lib64/libcuttlefish_kernel_log_monitor_utils.so \
127 -I /system/lib64/libcuttlefish_utils.so \
128 -I /system/lib64/libfruit.so \
129 -I /system/lib64/libgflags.so \
Wei Licd9948f2023-06-22 17:52:31 -0700130 -I /system/lib64/libkeymaster4_1support.so \
131 -I /system/lib64/libkeymaster4support.so \
132 -I /system/lib64/libkeymint.so \
133 -I /system/lib64/libkeystore2_aaid.so \
134 -I /system/lib64/libkeystore2_apc_compat.so \
135 -I /system/lib64/libkeystore2_crypto.so \
136 -I /system/lib64/libkeystore-attestation-application-id.so \
137 -I /system/lib64/libkm_compat_service.so \
138 -I /system/lib64/libkm_compat.so \
139 -I /system/lib64/vndk-29 \
140 -I /system/lib64/vndk-sp-29 \
141 -I /system/lib/vndk-29 \
142 -I /system/lib/vndk-sp-29 \
143 -I /system/usr/icu \
144 -I /vendor_dlkm/etc"
Wei Li08246b92023-07-17 11:24:54 -0700145 diff_excludes[system_ext]="\
146 -I /system_ext/etc/aconfig_flags.textproto \
147 -I /system_ext/etc/build_flags.json"
Wei Licd9948f2023-06-22 17:52:31 -0700148
149 # Example output of dump.erofs is as below, and the data used in the test start
150 # at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name.
151 # Each line is captured in variable "entry", awk is used to get type and name.
152 # Output of dump.erofs:
153 # File : /
154 # Size: 160 On-disk size: 160 directory
155 # NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
156 # Inode size: 64 Extent size: 0 Xattr size: 16
157 # Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
158 # Timestamp: 2023-02-14 01:15:54.000000000
159 #
160 # NID TYPE FILENAME
161 # 39 2 .
162 # 39 2 ..
163 # 47 2 app
164 # 1286748 2 bin
165 # 1286754 2 etc
166 # 5304814 2 lib
167 # 5309056 2 lib64
168 # 5309130 2 media
169 # 5388910 2 overlay
170 # 5479537 2 priv-app
171 EROFS_IMAGES="\
172 $sbom_test/product.img \
173 $sbom_test/system.img \
174 $sbom_test/system_ext.img \
175 $sbom_test/system_dlkm.img \
176 $sbom_test/system_other.img \
177 $sbom_test/odm.img \
178 $sbom_test/odm_dlkm.img \
179 $sbom_test/vendor.img \
180 $sbom_test/vendor_dlkm.img"
181 for f in $EROFS_IMAGES; do
182 partition_name=$(basename $f | cut -d. -f1)
183 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
184 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
185 rm "$file_list_file" > /dev/null 2>&1 || true
186 all_dirs="/"
187 while [ ! -z "$all_dirs" ]; do
188 dir=$(echo "$all_dirs" | cut -d ' ' -f1)
189 all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s)
190 entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11)
191 while read -r entry; do
192 inode_type=$(echo $entry | awk -F ' ' '{print $2}')
193 name=$(echo $entry | awk -F ' ' '{print $3}')
194 case $inode_type in
195 "2") # directory
196 all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//')
197 ;;
198 "1"|"7") # 1: file, 7: symlink
199 (
200 if [ "$partition_name" != "system" ]; then
201 # system partition is mounted to /, not to prepend partition name.
202 printf %s "/$partition_name"
203 fi
204 echo "$dir/$name" | sed 's#^//#/#'
205 ) >> "$file_list_file"
206 ;;
207 esac
208 done <<< "$entries"
209 done
210 sort -n -o "$file_list_file" "$file_list_file"
211
212 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_spdx_file"
213 if [ "$partition_name" = "system" ]; then
214 # system partition is mounted to /, so include FileName starts with /root/ too.
215 grep "FileName: /root/" $product_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_spdx_file"
216 fi
217 sort -n -o "$files_in_spdx_file" "$files_in_spdx_file"
218
219 echo ============ Diffing files in $f and SBOM
220 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
221 done
222
223 RAMDISK_IMAGES="$product_out/ramdisk.img"
224 for f in $RAMDISK_IMAGES; do
225 partition_name=$(basename $f | cut -d. -f1)
226 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
227 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
228 # lz4 decompress $f to stdout
229 # cpio list all entries like ls -l
230 # grep filter normal files and symlinks
231 # awk get entry names
232 # sed remove partition name from entry names
233 $lz4 -c -d $f | cpio -tv 2>/dev/null | grep '^[-l]' | awk -F ' ' '{print $9}' | sed "s:^:/$partition_name/:" | sort -n > "$file_list_file"
234
235 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_spdx_file"
236
237 echo ============ Diffing files in $f and SBOM
238 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
239 done
240
Wei Li8ea2ffc2023-08-01 15:20:38 -0700241 verify_package_verification_code "$product_out/sbom.spdx"
242
Wei Licd9948f2023-06-22 17:52:31 -0700243 # Teardown
244 cleanup "${out_dir}"
245}
246
Wei Li8ea2ffc2023-08-01 15:20:38 -0700247function verify_package_verification_code {
248 local sbom_file="$1"; shift
249
250 local -a file_checksums
251 local package_product_found=
252 while read -r line;
253 do
254 if grep -q 'PackageVerificationCode' <<<"$line"
255 then
256 package_product_found=true
257 fi
258 if [ -n "$package_product_found" ]
259 then
260 if grep -q 'FileChecksum' <<< "$line"
261 then
262 checksum=$(echo $line | sed 's/^.*: //')
263 file_checksums+=("$checksum")
264 fi
265 fi
266 done <<< "$(grep -E 'PackageVerificationCode|FileChecksum' $sbom_file)"
267 IFS=$'\n' file_checksums=($(sort <<<"${file_checksums[*]}")); unset IFS
268 IFS= expected_package_verification_code=$(printf "${file_checksums[*]}" | sha1sum | sed 's/[[:space:]]*-//'); unset IFS
269
270 actual_package_verification_code=$(grep PackageVerificationCode $sbom_file | sed 's/PackageVerificationCode: //g')
271 if [ $actual_package_verification_code = $expected_package_verification_code ]
272 then
273 echo "Package verification code is correct."
274 else
275 echo "Unexpected package verification code."
276 exit 1
277 fi
278}
279
Wei Li59df0ee2023-07-10 15:50:38 -0700280function test_sbom_unbundled_apex {
281 # Setup
282 out_dir="$(setup)"
283
284 # run_soong to build com.android.adbd.apex
285 run_soong "module_arm64" "${out_dir}" "sbom deapexer" "com.android.adbd"
286
287 deapexer=${out_dir}/host/linux-x86/bin/deapexer
288 debugfs=${out_dir}/host/linux-x86/bin/debugfs_static
289 apex_file=${out_dir}/target/product/module_arm64/system/apex/com.android.adbd.apex
290 echo "============ Diffing files in $apex_file and SBOM"
291 set +e
292 # deapexer prints the list of all files and directories
293 # sed extracts the file/directory names
294 # grep removes directories
295 # sed removes leading ./ in file names
296 diff -I /system/apex/com.android.adbd.apex -I apex_manifest.pb \
297 <($deapexer --debugfs_path=$debugfs list --extents ${apex_file} | sed -E 's#(.*) \[.*\]$#\1#' | grep -v "/$" | sed -E 's#^\./(.*)#\1#' | sort -n) \
298 <(grep '"fileName": ' ${apex_file}.spdx.json | sed -E 's/.*"fileName": "(.*)",/\1/' | sort -n )
299
300 if [ $? != "0" ]; then
301 echo "Diffs found in $apex_file and SBOM"
302 exit 1
303 else
304 echo "No diffs."
305 fi
306 set -e
307
308 # Teardown
309 cleanup "${out_dir}"
310}
311
312function test_sbom_unbundled_apk {
313 # Setup
314 out_dir="$(setup)"
315
316 # run_soong to build Browser2.apk
317 run_soong "module_arm64" "${out_dir}" "sbom" "Browser2"
318
319 sbom_file=${out_dir}/target/product/module_arm64/system/product/app/Browser2/Browser2.apk.spdx.json
320 echo "============ Diffing files in Browser2.apk and SBOM"
321 set +e
322 # There is only one file in SBOM of APKs
323 diff \
324 <(echo "/system/product/app/Browser2/Browser2.apk" ) \
325 <(grep '"fileName": ' ${sbom_file} | sed -E 's/.*"fileName": "(.*)",/\1/' )
326
327 if [ $? != "0" ]; then
328 echo "Diffs found in $sbom_file"
329 exit 1
330 else
331 echo "No diffs."
332 fi
333 set -e
334
335 # Teardown
336 cleanup "${out_dir}"
337}
338
339test_sbom_aosp_cf_x86_64_phone
340test_sbom_unbundled_apex
341test_sbom_unbundled_apk