[PATCH 2/2] gpt: "read-only" operation could clobber the pMBR in another way

Jim Meyering meyering at redhat.com
Wed Feb 10 16:11:25 UTC 2010


A read-only operation like "parted $dev print" would overwrite $dev's
pMBR when exactly one of the primary and backup tables was corrupt.
* libparted/labels/gpt.c (gpt_read): Clear "write_back" in those
two cases.  Hans De Goede spotted this bug by inspection.
* NEWS (Bug fixes): Mention it.
* tests/t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh: New test.
* tests/Makefile.am (TESTS): Add
t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh.
---
 NEWS                                               |    5 ++
 libparted/labels/gpt.c                             |    2 +
 tests/Makefile.am                                  |    1 +
 ...gpt-print-with-corrupt-primary-clobbers-pmbr.sh |   56 ++++++++++++++++++++
 4 files changed, 64 insertions(+), 0 deletions(-)
 create mode 100755 tests/t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh

diff --git a/NEWS b/NEWS
index 96ea96c..28f87de 100644
--- a/NEWS
+++ b/NEWS
@@ -22,6 +22,11 @@ GNU parted NEWS                                    -*- outline -*-
   gpt: read-only operation could clobber MBR part of hybrid GPT+MBR table
   [bug introduced in parted-2.1]

+  gpt: a read-only operation like "parted $dev print" would overwrite $dev's
+  protective MBR when exactly one of the primary and backup GPT tables was
+  found to be corrupt.
+  [bug introduced prior to parted-1.8.0]
+
   "make install" no longer installs tests programs named disk and label


diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index ea96a3b..48d580e 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -984,6 +984,7 @@ gpt_read (PedDisk *disk)
         goto error_free_gpt;

       gpt = primary_gpt;
+      write_back = 0;
     }
   else /* !primary_gpt && backup_gpt */
     {
@@ -996,6 +997,7 @@ gpt_read (PedDisk *disk)
         goto error_free_gpt;

       gpt = backup_gpt;
+      write_back = 0;
     }
   backup_gpt = NULL;
   primary_gpt = NULL;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 38922f6..8008400 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -10,6 +10,7 @@ TESTS = \
   t0201-gpt.sh \
   t0202-gpt-pmbr.sh \
   t0205-gpt-list-clobbers-pmbr.sh \
+  t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh \
   t0220-gpt-msftres.sh \
   t0250-gpt.sh \
   t0280-gpt-corrupt.sh \
diff --git a/tests/t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh b/tests/t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh
new file mode 100755
index 0000000..f47549e
--- /dev/null
+++ b/tests/t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+# Ensure that printing a GPT partition table does not modify the pMBR.
+# Much like t0205, but with the addition of a corrupt PTE in primary table,
+# "parted $device print" would modify $device.
+
+# Copyright (C) 2010 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  parted --version
+fi
+
+: ${srcdir=.}
+. $srcdir/t-lib.sh
+
+fail=0
+
+ss=$sector_size_
+n_sectors=400
+dev=dev-file
+
+dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
+parted -s $dev mklabel gpt                     || fail=1
+parted -s $dev mkpart p1 128s 255s             || fail=1
+
+parted -m -s $dev u s p                        || fail=1
+
+# Write non-NUL bytes all over the MBR, so we're likely to see any change.
+# However, be careful to leave the type of the first partition, 0xEE,
+# as well as the final two magic bytes.
+printf '%0450d\xee%059d\x55\xaa' 0 0 | dd of=$dev count=1 conv=notrunc || fail=1
+
+dd if=$dev of=before count=1 || fail=1
+
+orig_byte=$(gpt_corrupt_primary_table_ $dev $ss) || fail=1
+
+parted -m -s $dev u s p || fail=1
+
+dd if=$dev of=after count=1 || fail=1
+
+cmp before after || fail=1
+
+Exit $fail
--
1.7.0.rc2.170.gbc565



More information about the parted-devel mailing list