1#!/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 26function setup { 27 tmp_dir="$(mktemp -d tmp.XXXXXX)" 28 trap 'cleanup "${tmp_dir}"' EXIT 29 echo "${tmp_dir}" 30} 31 32function cleanup { 33 tmp_dir="$1"; shift 34 rm -rf "${tmp_dir}" 35} 36 37function run_soong { 38 local out_dir="$1"; shift 39 local targets="$1"; shift 40 if [ "$#" -ge 1 ]; then 41 local apps=$1; shift 42 TARGET_PRODUCT="${target_product}" TARGET_RELEASE="${target_release}" TARGET_BUILD_VARIANT="${target_build_variant}" OUT_DIR="${out_dir}" TARGET_BUILD_UNBUNDLED=true TARGET_BUILD_APPS=$apps \ 43 build/soong/soong_ui.bash --make-mode ${targets} 44 else 45 TARGET_PRODUCT="${target_product}" TARGET_RELEASE="${target_release}" TARGET_BUILD_VARIANT="${target_build_variant}" OUT_DIR="${out_dir}" \ 46 build/soong/soong_ui.bash --make-mode ${targets} 47 fi 48} 49 50function diff_files { 51 local file_list_file="$1"; shift 52 local files_in_spdx_file="$1"; shift 53 local partition_name="$1"; shift 54 local exclude="$1"; shift 55 56 diff "$file_list_file" "$files_in_spdx_file" $exclude 57 if [ $? != "0" ]; then 58 echo Found diffs in $f and SBOM. 59 exit 1 60 else 61 echo No diffs. 62 fi 63} 64 65function test_sbom_aosp_cf_x86_64_phone { 66 # Setup 67 out_dir="$(setup)" 68 69 # Test 70 # m droid, build sbom later in case additional dependencies might be built and included in partition images. 71 run_soong "${out_dir}" "droid dump.erofs lz4" 72 73 product_out=$out_dir/target/product/vsoc_x86_64 74 sbom_test=$product_out/sbom_test 75 mkdir -p $sbom_test 76 cp $product_out/*.img $sbom_test 77 78 # m sbom 79 run_soong "${out_dir}" sbom 80 81 # Generate installed file list from .img files in PRODUCT_OUT 82 dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs 83 lz4=$out_dir/host/linux-x86/bin/lz4 84 85 # Example output of dump.erofs is as below, and the data used in the test start 86 # at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name. 87 # Each line is captured in variable "entry", awk is used to get type and name. 88 # Output of dump.erofs: 89 # File : / 90 # Size: 160 On-disk size: 160 directory 91 # NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00% 92 # Inode size: 64 Extent size: 0 Xattr size: 16 93 # Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x 94 # Timestamp: 2023-02-14 01:15:54.000000000 95 # 96 # NID TYPE FILENAME 97 # 39 2 . 98 # 39 2 .. 99 # 47 2 app 100 # 1286748 2 bin 101 # 1286754 2 etc 102 # 5304814 2 lib 103 # 5309056 2 lib64 104 # 5309130 2 media 105 # 5388910 2 overlay 106 # 5479537 2 priv-app 107 EROFS_IMAGES="\ 108 $sbom_test/product.img \ 109 $sbom_test/system.img \ 110 $sbom_test/system_ext.img \ 111 $sbom_test/system_dlkm.img \ 112 $sbom_test/system_other.img \ 113 $sbom_test/odm.img \ 114 $sbom_test/odm_dlkm.img \ 115 $sbom_test/vendor.img \ 116 $sbom_test/vendor_dlkm.img" 117 for f in $EROFS_IMAGES; do 118 partition_name=$(basename $f | cut -d. -f1) 119 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt" 120 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt" 121 rm "$file_list_file" > /dev/null 2>&1 || true 122 all_dirs="/" 123 while [ ! -z "$all_dirs" ]; do 124 dir=$(echo "$all_dirs" | cut -d ' ' -f1) 125 all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s) 126 entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11) 127 while read -r entry; do 128 inode_type=$(echo $entry | awk -F ' ' '{print $2}') 129 name=$(echo $entry | awk -F ' ' '{print $3}') 130 case $inode_type in 131 "2") # directory 132 all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//') 133 ;; 134 "1"|"7") # 1: file, 7: symlink 135 ( 136 if [ "$partition_name" != "system" ]; then 137 # system partition is mounted to /, not to prepend partition name. 138 printf %s "/$partition_name" 139 fi 140 echo "$dir/$name" | sed 's#^//#/#' 141 ) >> "$file_list_file" 142 ;; 143 esac 144 done <<< "$entries" 145 done 146 sort -n -o "$file_list_file" "$file_list_file" 147 148 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_spdx_file" 149 if [ "$partition_name" = "system" ]; then 150 # system partition is mounted to /, so include FileName starts with /root/ too. 151 grep "FileName: /root/" $product_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_spdx_file" 152 fi 153 sort -n -o "$files_in_spdx_file" "$files_in_spdx_file" 154 155 echo ============ Diffing files in $f and SBOM 156 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" "" 157 done 158 159 RAMDISK_IMAGES="$product_out/ramdisk.img" 160 for f in $RAMDISK_IMAGES; do 161 partition_name=$(basename $f | cut -d. -f1) 162 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt" 163 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt" 164 # lz4 decompress $f to stdout 165 # cpio list all entries like ls -l 166 # grep filter normal files and symlinks 167 # awk get entry names 168 # sed remove partition name from entry names 169 $lz4 -c -d $f | cpio -tv 2>/dev/null | grep '^[-l]' | awk -F ' ' '{print $9}' | sed "s:^:/$partition_name/:" | sort -n > "$file_list_file" 170 171 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_spdx_file" 172 173 echo ============ Diffing files in $f and SBOM 174 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" "" 175 done 176 177 verify_package_verification_code "$product_out/sbom.spdx" 178 179 # Teardown 180 cleanup "${out_dir}" 181} 182 183function verify_package_verification_code { 184 local sbom_file="$1"; shift 185 186 local -a file_checksums 187 local package_product_found= 188 while read -r line; 189 do 190 if grep -q 'PackageVerificationCode' <<<"$line" 191 then 192 package_product_found=true 193 fi 194 if [ -n "$package_product_found" ] 195 then 196 if grep -q 'FileChecksum' <<< "$line" 197 then 198 checksum=$(echo $line | sed 's/^.*: //') 199 file_checksums+=("$checksum") 200 fi 201 fi 202 done <<< "$(grep -E 'PackageVerificationCode|FileChecksum' $sbom_file)" 203 IFS=$'\n' file_checksums=($(sort <<<"${file_checksums[*]}")); unset IFS 204 IFS= expected_package_verification_code=$(printf "${file_checksums[*]}" | sha1sum | sed 's/[[:space:]]*-//'); unset IFS 205 206 actual_package_verification_code=$(grep PackageVerificationCode $sbom_file | sed 's/PackageVerificationCode: //g') 207 if [ $actual_package_verification_code = $expected_package_verification_code ] 208 then 209 echo "Package verification code is correct." 210 else 211 echo "Unexpected package verification code." 212 exit 1 213 fi 214} 215 216function test_sbom_unbundled_apex { 217 # Setup 218 out_dir="$(setup)" 219 220 # run_soong to build com.android.adbd.apex 221 run_soong "${out_dir}" "sbom deapexer" "com.android.adbd" 222 223 deapexer=${out_dir}/host/linux-x86/bin/deapexer 224 debugfs=${out_dir}/host/linux-x86/bin/debugfs_static 225 apex_file=${out_dir}/target/product/module_arm64/system/apex/com.android.adbd.apex 226 echo "============ Diffing files in $apex_file and SBOM" 227 set +e 228 # deapexer prints the list of all files and directories 229 # sed extracts the file/directory names 230 # grep removes directories 231 # sed removes leading ./ in file names 232 diff -I /system/apex/com.android.adbd.apex -I apex_manifest.pb \ 233 <($deapexer --debugfs_path=$debugfs list --extents ${apex_file} | sed -E 's#(.*) \[.*\]$#\1#' | grep -v "/$" | sed -E 's#^\./(.*)#\1#' | sort -n) \ 234 <(grep '"fileName": ' ${apex_file}.spdx.json | sed -E 's/.*"fileName": "(.*)",/\1/' | sort -n ) 235 236 if [ $? != "0" ]; then 237 echo "Diffs found in $apex_file and SBOM" 238 exit 1 239 else 240 echo "No diffs." 241 fi 242 set -e 243 244 # Teardown 245 cleanup "${out_dir}" 246} 247 248function test_sbom_unbundled_apk { 249 # Setup 250 out_dir="$(setup)" 251 252 # run_soong to build Browser2.apk 253 run_soong "${out_dir}" "sbom" "Browser2" 254 255 sbom_file=${out_dir}/target/product/module_arm64/system/product/app/Browser2/Browser2.apk.spdx.json 256 echo "============ Diffing files in Browser2.apk and SBOM" 257 set +e 258 # There is only one file in SBOM of APKs 259 diff \ 260 <(echo "/system/product/app/Browser2/Browser2.apk" ) \ 261 <(grep '"fileName": ' ${sbom_file} | sed -E 's/.*"fileName": "(.*)",/\1/' ) 262 263 if [ $? != "0" ]; then 264 echo "Diffs found in $sbom_file" 265 exit 1 266 else 267 echo "No diffs." 268 fi 269 set -e 270 271 # Teardown 272 cleanup "${out_dir}" 273} 274 275target_product=aosp_cf_x86_64_phone 276target_release=trunk_staging 277target_build_variant=userdebug 278for i in "$@"; do 279 case $i in 280 TARGET_PRODUCT=*) 281 target_product=${i#*=} 282 shift 283 ;; 284 TARGET_RELEASE=*) 285 target_release=${i#*=} 286 shift 287 ;; 288 TARGET_BUILD_VARIANT=*) 289 target_build_variant=${i#*=} 290 shift 291 ;; 292 *) 293 echo "Unknown command line arguments: $i" 294 exit 1 295 ;; 296 esac 297done 298 299echo "target product: $target_product, target_release: $target_release, target build variant: $target_build_variant" 300case $target_product in 301 aosp_cf_x86_64_phone) 302 test_sbom_aosp_cf_x86_64_phone 303 ;; 304 module_arm64) 305 test_sbom_unbundled_apex 306 test_sbom_unbundled_apk 307 ;; 308 *) 309 echo "Unknown TARGET_PRODUCT: $target_product" 310 exit 1 311 ;; 312esac