From 0188611b9cf332ff83f95e0bdbdcbd74c946465c Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 3 Oct 2012 04:12:43 +0200
Subject: [PATCH] Remove constant for disk wipe block size
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It is dangerous to have this block size as a global constant as that
could give the impression of it being easily changed. Doing so without
further adjustments to how β€œdd” is called will lead to disks not being
wiped properly.

Two additional parameter checks are added to β€œbackend.BlockdevWipe”.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Bernardo Dal Seno <bdalseno@google.com>
---
 lib/backend.py   | 11 ++++++++++-
 lib/constants.py |  1 -
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index 5ffeccbcf..912cd8c18 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -1595,8 +1595,13 @@ def _WipeDevice(path, offset, size):
   @param size: The size in MiB to write
 
   """
+  # Internal sizes are always in Mebibytes; if the following "dd" command
+  # should use a different block size the offset and size given to this
+  # function must be adjusted accordingly before being passed to "dd".
+  block_size = 1024 * 1024
+
   cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
-         "bs=%d" % constants.WIPE_BLOCK_SIZE, "oflag=direct", "of=%s" % path,
+         "bs=%s" % block_size, "oflag=direct", "of=%s" % path,
          "count=%d" % size]
   result = utils.RunCmd(cmd)
 
@@ -1625,6 +1630,10 @@ def BlockdevWipe(disk, offset, size):
     _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
 
   # Do cross verify some of the parameters
+  if offset < 0:
+    _Fail("Negative offset")
+  if size < 0:
+    _Fail("Negative size")
   if offset > rdev.size:
     _Fail("Offset is bigger than device size")
   if (offset + size) > rdev.size:
diff --git a/lib/constants.py b/lib/constants.py
index 6bb31a88d..1d9787c97 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -129,7 +129,6 @@ CPU_PINNING_ALL_KVM = 0xFFFFFFFF
 
 # Wipe
 DD_CMD = "dd"
-WIPE_BLOCK_SIZE = 1024 ** 2
 MAX_WIPE_CHUNK = 1024 # 1GB
 MIN_WIPE_CHUNK_PERCENT = 10
 
-- 
GitLab