Skip to content

Commit 76a395c

Browse files
authored
[secure boot] Support rw files allowlist (sonic-net#4585)
* Support rw files allowlist for Sonic Secure Boot * Improve the performance * fix bug * Move the config description into a md file * Change to use a simple way to remove the blank line * Support chmod a-x in rw folder * Change function name * Change some unnecessary words
1 parent edeb40f commit 76a395c

5 files changed

Lines changed: 83 additions & 0 deletions

File tree

build_image.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ elif [ "$IMAGE_TYPE" = "aboot" ]; then
133133
sed -i -e "s/%%IMAGE_VERSION%%/$IMAGE_VERSION/g" files/Aboot/boot0
134134
pushd files/Aboot && zip -g $OLDPWD/$OUTPUT_ABOOT_IMAGE boot0; popd
135135
pushd files/Aboot && zip -g $OLDPWD/$ABOOT_BOOT_IMAGE boot0; popd
136+
pushd files/image_config/secureboot && zip -g $OLDPWD/$OUTPUT_ABOOT_IMAGE allowlist_paths.conf; popd
136137
echo "$IMAGE_VERSION" >> .imagehash
137138
zip -g $OUTPUT_ABOOT_IMAGE .imagehash
138139
zip -g $ABOOT_BOOT_IMAGE .imagehash

files/Aboot/boot0.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ write_boot_configs() {
399399
fi
400400
fi
401401

402+
# setting secure_boot_enable=y when secure boot enabled
403+
[ -f /bin/securebootctl ] && securebootctl secureboot -display | grep -i "Secure Boot enable" -q && echo "secure_boot_enable=y" >> /tmp/append
404+
402405
mkdir -p "$image_path"
403406
cat /tmp/append > $cmdline_image
404407
[ -s ${target_path}/machine.conf ] || write_machine_config
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
home/.*
2+
var/core/.*
3+
var/log/.*
4+
etc/adjtime
5+
etc/default/ntp
6+
etc/dhcp/dhclient.conf
7+
etc/ebtables.filter
8+
etc/group
9+
etc/gshadow
10+
etc/hostname
11+
etc/hosts
12+
etc/machine-id
13+
etc/network/interfaces
14+
etc/nsswitch.conf
15+
etc/ntp.conf
16+
etc/pam.d/common-auth-sonic
17+
etc/pam.d/sshd
18+
etc/pam.d/login
19+
etc/pam.d/sshd.old
20+
etc/passwd
21+
etc/rsyslog.conf
22+
etc/shadow
23+
etc/sonic/acl.json
24+
etc/sonic/config_db.json
25+
etc/sonic/minigraph.xml
26+
etc/sonic/old_config/.*
27+
etc/sonic/snmp.yml
28+
etc/sonic/updategraph.conf
29+
etc/ssh/ssh_host_rsa_key.pub
30+
etc/ssh/ssh_host_rsa_key
31+
etc/subgid
32+
etc/subuid
33+
etc/tacplus_nss.conf
34+
etc/tacplus_user
35+
lib/systemd/system/[email protected]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Configuration Guide
2+
It is the patterns of the relative paths in /host/image-{{hash}}/rw folder.
3+
The patterns will not be used if the Sonic Secure Boot feature is not enabled.
4+
The files that are not in the allowlist will be removed when the Sonic System cold reboot.
5+
6+
### Example config to add all the files in a folder to allowlist
7+
home/.*
8+
9+
### Example config to add a file to allowlist
10+
etc/nsswitch.conf
11+

files/initramfs-tools/union-mount.j2

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,43 @@ set_tmpfs_log_partition_size()
3939
[ $maxsize -le $varlogsize ] && varlogsize=$maxsize
4040
}
4141

42+
remove_not_in_allowlist_files()
43+
{
44+
image_dir=$1
45+
allowlist_file=${rootmnt}/host/$image_dir/allowlist_paths.conf
46+
47+
# Return if the secure_boot_enable option is not set
48+
if ! (cat /proc/cmdline | grep -i -q "secure_boot_enable=[y1]"); then
49+
return
50+
fi
51+
52+
# Return if the allowlist file does not exist
53+
if ! test -f "${allowlist_file}"; then
54+
echo "The file ${allowlist_file} is missing, failed to mount rw folder." 1>&2
55+
exit 1
56+
fi
57+
58+
rw_dir=${rootmnt}/host/$image_dir/rw
59+
60+
# Set the grep pattern file, remove the blank line in config file
61+
allowlist_pattern_file=${rootmnt}/host/$image_dir/allowlist_paths.pattern
62+
awk -v rw_dir="$rw_dir" 'NF {print rw_dir"/"$0"$"}' ${allowlist_file} > $allowlist_pattern_file
63+
64+
# Find the files in the rw folder, and remove the files not in the allowlist
65+
find ${rw_dir} -type f | grep -v -f $allowlist_pattern_file | xargs /bin/rm -f
66+
rm -f $allowlist_pattern_file
67+
}
68+
4269
## Mount the overlay file system: rw layer over squashfs
4370
image_dir=$(cat /proc/cmdline | sed -e 's/.*loop=\(\S*\)\/.*/\1/')
4471
mkdir -p ${rootmnt}/host/$image_dir/rw
4572
mkdir -p ${rootmnt}/host/$image_dir/work
73+
## Remove the files not in allowlist in the rw folder
74+
remove_not_in_allowlist_files "$image_dir"
75+
## Remove the executable permission for all the files in rw folder except home folder
76+
rw_dir=${rootmnt}/host/$image_dir/rw
77+
find ${rw_dir} -type f -not -path ${rw_dir}/home -exec chmod a-x {} +
78+
4679
mount -n -o lowerdir=${rootmnt},upperdir=${rootmnt}/host/$image_dir/rw,workdir=${rootmnt}/host/$image_dir/work -t overlay root-overlay ${rootmnt}
4780
## Check if the root block device is still there
4881
[ -b ${ROOT} ] || mdev -s

0 commit comments

Comments
 (0)