Skip to content

Commit

Permalink
Add range checking in addPartitionSet.
Browse files Browse the repository at this point in the history
This checks that partitions provided to addPartitionSet are inside
the ranges allowed.

Note that it does still allow for partitions to overlap themselves
or to overlap existing partitions, but will give a reasonable error
if you try to give a partition.Last or partition.New that are not sane.
  • Loading branch information
Scott Moser authored and rchamarthy committed Apr 8, 2020
1 parent 2dbf4b1 commit 332aee6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
15 changes: 14 additions & 1 deletion linux/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,24 @@ func addPartitionSet(d disko.Disk, pSet disko.PartitionSet) error {
return err
}

maxEnd := ((d.Size - uint64(d.SectorSize)*33) / disko.Mebibyte) * disko.Mebibyte
minStart := disko.Mebibyte

for _, p := range pSet {
gptTable.Partitions[p.Number-1] = toGPTPartition(p, d.SectorSize)

if p.Start < minStart {
return fmt.Errorf("partition %d start (%d) is too low. Must be >= %d",
p.Number, p.Start, minStart)
}

if p.Last >= maxEnd {
return fmt.Errorf("partition %d Last (%d) is too high. Must be < %d",
p.Number, p.Last, maxEnd)
}

if err := zeroStartEnd(fp, int64(p.Start), int64(p.Last)); err != nil {
return fmt.Errorf("failed to zero partition %d: %s", p.ID, err)
return fmt.Errorf("failed to zero partition %d: %s", p.Number, err)
}
}

Expand Down
52 changes: 52 additions & 0 deletions linux/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,55 @@ func TestDeletePartition(t *testing.T) {
t.Fatalf("There were %d partitions after delete, expected 0", len(pSet))
}
}

func TestBadPartition(t *testing.T) {
tmpd, err := ioutil.TempDir("", "disko_test")
if err != nil {
t.Fatalf("Failed to create tempdir: %s", err)
}

defer os.RemoveAll(tmpd)

fpath := path.Join(tmpd, "mydisk")
fsize := uint64(200 * 1024 * 1024) // nolint:gomnd

if err := ioutil.WriteFile(fpath, []byte{}, 0644); err != nil {
t.Fatalf("Failed to write to a temp file: %s", err)
}

if err := os.Truncate(fpath, int64(fsize)); err != nil {
t.Fatalf("Failed create empty file: %s", err)
}

disk := disko.Disk{
Name: "mydisk",
Path: fpath,
Size: fsize,
SectorSize: sectorSize512,
}

fs := disk.FreeSpaces()
myGUID := disko.GenGUID()

part := disko.Partition{
Start: 1024, // nolint:gomnd
Last: fs[0].Last,
Type: partid.LinuxLVM,
Name: "mytest partition",
ID: myGUID,
Number: uint(1),
}

err = addPartitionSet(disk, disko.PartitionSet{part.Number: part})
if err == nil {
t.Errorf("Created partition with OOB start (%d). should have failed", part.Start)
}

part.Start = fs[0].Start
part.Last = disk.Size - 1

err = addPartitionSet(disk, disko.PartitionSet{part.Number: part})
if err == nil {
t.Errorf("Created partition with OOB end (%d). should have failed", part.Last)
}
}

0 comments on commit 332aee6

Please sign in to comment.