osbase/gpt: align partition end
When adding a partition with size=-1, also align the end of the
partition. Otherwise, writes at the end of the partition could write to
the hardware blocks containing the alternate GPT, which increasees the
risk of corruption.
Change-Id: Ib29d0a18c7d91526d1b53a7992974d08255a2e3e
Reviewed-on: https://review.monogon.dev/c/monogon/+/3365
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Tested-by: Jenkins CI
diff --git a/osbase/gpt/gpt.go b/osbase/gpt/gpt.go
index 8b1fc4d..033a34d 100644
--- a/osbase/gpt/gpt.go
+++ b/osbase/gpt/gpt.go
@@ -245,10 +245,13 @@
// Number of blocks the partition should occupy, rounded up.
blocks := (size + blockSize - 1) / blockSize
if size == -1 {
- if largestFreeSpace == 0 {
+ // When size is -1, use the largest free space. Align the size to ensure
+ // that the partition does not overlap the hardware blocks containing the
+ // alternate GPT.
+ blocks = largestFreeSpace / alignBlocks * alignBlocks
+ if blocks == 0 {
return errors.New("no free space")
}
- blocks = largestFreeSpace
} else if size <= 0 {
return fmt.Errorf("partition size (%d bytes) must be positive or the special value -1", size)
}