Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
No functional change.  The large number of partitions in the test is to
ensure that the code correctly handles such partition tables without a
buffer overflow.  Previous versions of the code did have a buffer
overflow due to incorrectly computing the buffer size.
  • Loading branch information
DemiMarie committed Dec 8, 2024
1 parent 7e389c7 commit 01c0506
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
checks:tests:
after_script:
- (cd qrexec-lib; gcov *.c || :)
- (cd gptfixer; gcov *.c || :)
- ./ci/codecov-wrapper --gcov
before_script:
- sudo dnf install -y python3-pip 'pkgconfig(icu-uc)' sequoia-sqv
Expand All @@ -11,6 +12,7 @@ checks:tests:
- python3 -m coverage run -m unittest discover -v imgconverter -p test.py
- export CFLAGS="--coverage -DCOVERAGE" LDFLAGS=--coverage
- make -C qrexec-lib check NO_REBUILD_TABLE=1
- gptfixer/test.sh
stage: checks
tags:
- docker
Expand Down
42 changes: 42 additions & 0 deletions gptfixer/layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
label: gpt
label-id: F4796A2A-E377-45BD-B539-D6D49E569055
device: /dev/loop0
unit: sectors
first-lba: 24
sector-size: 512
table-length: 33
last-lba: 41943029

/dev/loop0p1 : start=32, size=409592, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=FA4D6529-56DA-47C7-AE88-E2DFECB72621, name="EFI System"
/dev/loop0p2 : start=411648, size= 4096, type=21686148-6449-6E6F-744E-656564454649, uuid=1E6C9DB4-1E91-46C4-846A-2030DCB13B8C, name="BIOS boot partition"
/dev/loop0p3 : start=415744, size= 4096, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=5DCECC9F-71B8-4357-8039-1BEBB7A48653, name="/home"
/dev/loop0p4 : start=419840, size= 4096, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=3EA4A03F-ACCF-4EDF-99EC-8E5F2B382459, name="/usr"
/dev/loop0p5 : start=423944,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p6 : start=423952,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p7 : start=423960,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p8 : start=423968,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p9 : start=423976,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p10 : start=423984,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p11 : start=423992,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p12 : start=424000,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p13 : start=424008,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p14 : start=424016,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p15 : start=424024,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p16 : start=424032,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p17 : start=424040,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p18 : start=424048,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p19 : start=424056,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p20 : start=424064,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p21 : start=424072,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p22 : start=424080,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p23 : start=424088,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p24 : start=424096,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p25 : start=424104,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p26 : start=424112,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p27 : start=424120,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p28 : start=424128,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p29 : start=424136,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p30 : start=424144,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p31 : start=424152,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p32 : start=424160,size=8, type=00000000-0000-0000-0000-000000000000,uuid=00000000-0000-0000-0000-000000000000,name=""
/dev/loop0p33 : start=424168,size=41518848,type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,uuid=693244E6-3E07-47BF-AD79-ACADE4293FE7,name="Root filesystem"
38 changes: 38 additions & 0 deletions gptfixer/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash --
set -euo pipefail

case $0 in (/*) cd "${0%/*}/";; (*/*) cd "./${0%/*}";; (*) :;; esac
make -C .. gptfixer/gpt
chk () {
loopdev=$(sudo losetup --nooverlap --find --sector-size "$1" --show -- dummy.img)
if [[ "$loopdev" != '/dev/loop0' ]]; then
printf 'Loop device is not /dev/loop0 (got %q), expect test failure\n' "$loopdev"
fi >&3
echo Dumping broken partition table
sudo sfdisk --label=gpt --dump -- "$loopdev"
sudo ./gpt fix "$loopdev"
echo Dumping fixed partition table
sudo sfdisk --label=gpt --dump -- "$loopdev"
sudo losetup -d "$loopdev"
}

go () (
set -x
truncate -s 0 dummy.img
truncate -s 20GiB dummy.img
sfdisk --force dummy.img < layout
chk 4096
chk 512
)

case "$#,${1-}" in
('1,update') go 3>&2 > test.sh.stdout 2> test.sh.stderr;;
(0,)
tmpdir=$(mktemp -d)
go 3>&2 > "$tmpdir/stdout" 2> "$tmpdir/stderr"
diff -u -- "$tmpdir/stdout" test.sh.stdout
diff -u -- "$tmpdir/stderr" test.sh.stderr
rm -rf -- "$tmpdir"
;;
(*) echo "Usage: test.sh [update]" >&2; exit 1;;
esac
27 changes: 27 additions & 0 deletions gptfixer/test.sh.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
+ truncate -s 0 dummy.img
+ truncate -s 20GiB dummy.img
+ sfdisk --force dummy.img
+ chk 4096
++ sudo losetup --nooverlap --find --sector-size 4096 --show -- dummy.img
+ loopdev=/dev/loop0
+ [[ /dev/loop0 != \/\d\e\v\/\l\o\o\p\0 ]]
+ echo Dumping broken partition table
+ sudo sfdisk --label=gpt --dump -- /dev/loop0
GPT PMBR size mismatch (41943039 != 5242879) will be corrected by write.
+ sudo ./gpt fix /dev/loop0
gpt: Found GPT with different sector size, altering
+ echo Dumping fixed partition table
+ sudo sfdisk --label=gpt --dump -- /dev/loop0
+ sudo losetup -d /dev/loop0
+ chk 512
++ sudo losetup --nooverlap --find --sector-size 512 --show -- dummy.img
+ loopdev=/dev/loop0
+ [[ /dev/loop0 != \/\d\e\v\/\l\o\o\p\0 ]]
+ echo Dumping broken partition table
+ sudo sfdisk --label=gpt --dump -- /dev/loop0
GPT PMBR size mismatch (5242879 != 41943039) will be corrected by write.
+ sudo ./gpt fix /dev/loop0
gpt: Found GPT with different sector size, altering
+ echo Dumping fixed partition table
+ sudo sfdisk --label=gpt --dump -- /dev/loop0
+ sudo losetup -d /dev/loop0
Loading

0 comments on commit 01c0506

Please sign in to comment.