Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ironlib is able to detect ineffective wipes #135

Merged
Merged
15 changes: 14 additions & 1 deletion actions/storage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ func (s *StorageControllerAction) WipeDisk(ctx context.Context, logicalName stri
if err != nil {
return err
}
// Watermark disk
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("%s | Initiating watermarking process", logicalName)
check := utils.ApplyWatermarks(logicalName)

return util.WipeDisk(ctx, logicalName)
err = util.WipeDisk(ctx, logicalName)
if err != nil {
return err
}
log.Printf("%s | Checking if the watermark has been removed", logicalName)
err = check()
if err != nil {
return err
}
log.Printf("%s | Watermarks has been removed", logicalName)
return nil
}
2 changes: 1 addition & 1 deletion examples/diskwipe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ func main() {
if err != nil {
logger.Fatal(err)
}
fmt.Println("Wiped")
fmt.Println("Wiped successfully!")
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
}
114 changes: 114 additions & 0 deletions utils/watermark_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package utils

import (
"crypto/rand"
"fmt"
"io"
"math/big"
"os"
"slices"

"github.com/pkg/errors"
)

const (
bufferSize = 512
numWatermarks = 10
)

type watermark struct {
position int64
data []byte
}

func ApplyWatermarks(logicalName string) func() error {
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
// Write open
file, err := os.OpenFile(logicalName, os.O_WRONLY, 0)
if err != nil {
return func() error {
return err
}
}
defer file.Close()

// Get disk or partition size
fileSize, err := file.Seek(0, io.SeekEnd)
if err != nil {
return func() error {
return err
}
}
// Write watermarks on random locations
watermarks := writeWatermarks(file, 0, fileSize, numWatermarks)
if len(watermarks) != numWatermarks {
return func() error {
ErrorWritingWatermarks := errors.New("Error writing watermarks in the file")
return fmt.Errorf("%s | %w", logicalName, ErrorWritingWatermarks)
}
}

checker := func() error {
file, err := os.OpenFile(logicalName, os.O_RDONLY, 0)
if err != nil {
return err
}
defer file.Close()

for _, watermark := range watermarks {
_, err = file.Seek(watermark.position, io.SeekStart)
if err != nil {
return err
}
// Read the watermark written to the position
currentValue := make([]byte, bufferSize)
_, err = io.ReadFull(file, currentValue)
if err != nil {
return err
}
// Check if the watermark is still in the disk
if slices.Equal(currentValue, watermark.data) {
ErrorExistingWatermark := errors.New("Error existing watermark in the file")
return fmt.Errorf("%s | %w", logicalName, ErrorExistingWatermark)
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil
}
return checker
}

func writeWatermarks(file *os.File, a, b int64, count int) []watermark {
if count == 1 {
data := make([]byte, bufferSize)
_, err := rand.Read(data)
if err != nil {
return nil
}
offset, err := rand.Int(rand.Reader, big.NewInt(b-a-bufferSize))
if err != nil {
return nil
}
randomPosition := int64(offset.Uint64()) + a
_, err = file.Seek(randomPosition, io.SeekStart)
if err != nil {
return nil
}
_, err = file.Write(data)
if err != nil {
return nil
}

w := watermark{
position: randomPosition,
data: data,
}
return []watermark{w}
}
// Divide the intervals into two equal parts (approximately)
mid := (a + b) / 2

// Return recursively the call of function with the two remaining intervals
leftCount := count / 2
rightCount := count - leftCount

return append(writeWatermarks(file, a, mid-1, leftCount), writeWatermarks(file, mid, b, rightCount)...)
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
}
Loading