[PATCH] Enforce inherent limitations of dos and dvh partition table formats.

Jim Meyering meyering at redhat.com
Thu Jan 10 13:51:56 UTC 2008


* libparted/disk.c (_check_partition): Enforce the 32-bit limitation
on a partition's starting sector number and length (in sectors).
With the usual 512-byte sector size, this limits the maximum
partition size to just under 2TB.
(_partition_max_start, _partition_max_len): New functions.
(_check_partition): Use them.
* tests/t4100-msdos-partition-limits.sh: New file.  Test vs. msdos.
* tests/t4100-dvh-partition-limits.sh: New file.  Test vs. dvh.
* tests/Makefile.am (TESTS): Add t4100-msdos-partition-limits.sh
and t4100-dvh-partition-limits.sh.

Signed-off-by: Jim Meyering <meyering at redhat.com>
---
 libparted/disk.c                      |   67 +++++++++++++-
 tests/Makefile.am                     |    4 +-
 tests/t4100-dvh-partition-limits.sh   |  169 +++++++++++++++++++++++++++++++++
 tests/t4100-msdos-partition-limits.sh |  168 ++++++++++++++++++++++++++++++++
 4 files changed, 406 insertions(+), 2 deletions(-)
 create mode 100755 tests/t4100-dvh-partition-limits.sh
 create mode 100755 tests/t4100-msdos-partition-limits.sh

diff --git a/libparted/disk.c b/libparted/disk.c
index 087fbbf..ec09996 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -1,6 +1,6 @@
  /*
     libparted - a library for manipulating disk partitions
-    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007
+    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008
                   Free Software Foundation, Inc.

     This program is free software; you can redistribute it and/or modify
@@ -38,6 +38,7 @@
 #include <parted/debug.h>

 #include "architecture.h"
+#include "intprops.h"

 #if ENABLE_NLS
 #  include <libintl.h>
@@ -1695,6 +1696,31 @@ _check_extended_partition (PedDisk* disk, PedPartition* part)
 	return 1;
 }

+static PedSector
+_partition_max_start (char const *label_type)
+{
+  /* List partition table names (a la disk->type->name) for which
+     the partition length, in sectors, must fit in 32 bytes.  */
+  static char const *const max_32[] = {"msdos", "dvh"};
+  unsigned int i;
+
+  for (i = 0; i < sizeof max_32 / sizeof *max_32; i++)
+    if (strcmp (label_type, max_32[i]) == 0)
+      return UINT32_MAX;
+
+  return TYPE_MAXIMUM (PedSector);
+}
+
+static PedSector
+_partition_max_len (char const *label_type)
+{
+  /* NOTE: for now, they happen to be the same, so don't
+     duplicate needlessly.  Of course, if there's some format
+     with different length and starting sector limits, then
+     these functions will diverge.  */
+  return _partition_max_start (label_type);
+}
+
 static int
 _check_partition (PedDisk* disk, PedPartition* part)
 {
@@ -1735,6 +1761,45 @@ _check_partition (PedDisk* disk, PedPartition* part)
 		return 0;
 	}

+	if (!(part->type & PED_PARTITION_METADATA)) {
+		char const *label_type = disk->type->name;
+		/* Enforce some restrictions inherent in the DOS
+		   partition table format.  Without these, one would be able
+		   to create a 2TB partition (or larger), and it would work,
+		   but only until the next reboot.  This was insidious: the
+		   too-large partition would work initially, because with
+		   Linux-2.4.x and newer we set the partition start sector
+		   and length (in sectors) accurately and directly via the
+		   BLKPG ioctl.  However, only the last 32 bits of each
+		   number would be written to the partition table, and the
+		   next time the system would read/use those corrupted numbers
+		   it would usually complain about an invalid partition.
+                   The same applies to the starting sector number.  */
+
+		if (part->geom.length > _partition_max_len (label_type)) {
+			ped_exception_throw (
+				PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+				_("partition length of %jd sectors exceeds the "
+                                  "%s-partition-table-imposed maximum of %jd"),
+				part->geom.length,
+                                label_type,
+                                _partition_max_len (label_type));
+			return 0;
+		}
+
+		/* The starting sector number must fit in 32 bytes.  */
+		if (part->geom.start > _partition_max_start (label_type)) {
+			ped_exception_throw (
+				PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+				_("starting sector number, %jd exceeds the"
+                                  " %s-partition-table-imposed maximum of %jd"),
+                                part->geom.start,
+                                label_type,
+                                _partition_max_start (label_type));
+			return 0;
+		}
+	}
+
 	return 1;
 }

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3a3020e..e493e46 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -7,7 +7,9 @@ TESTS = \
   t2000-mkfs.sh \
   t2100-mkswap.sh \
   t3000-constraints.sh \
-  t3100-resize-ext2-partion.sh
+  t3100-resize-ext2-partion.sh \
+  t4100-msdos-partition-limits.sh \
+  t4100-dvh-partition-limits.sh

 EXTRA_DIST = \
   $(TESTS) test-lib.sh mkdtemp
diff --git a/tests/t4100-dvh-partition-limits.sh b/tests/t4100-dvh-partition-limits.sh
new file mode 100755
index 0000000..6e0a3ee
--- /dev/null
+++ b/tests/t4100-dvh-partition-limits.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+# Copyright (C) 2008 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/>.
+
+test_description='enforce limits on partition start sector and length'
+
+# Need root privileges to use mount.
+privileges_required_=1
+
+. ./init.sh
+
+####################################################
+# Create and mount a file system capable of dealing with >=2TB files.
+# We must be able to create a file with an apparent length of 2TB or larger.
+# It needn't be a large file system.
+fs=fs_file
+mp=`pwd`/mount-point
+n=4096
+
+test_expect_success \
+    'create an XFS file system' \
+    '
+    dd if=/dev/zero of=$fs bs=1MB count=2 seek=20 &&
+    mkfs.xfs -q $fs &&
+    mkdir "$mp"
+
+    '
+
+# Unmount upon interrupt, failure, etc., as well as upon normal completion.
+cleanup_() { cd "$test_dir_" && umount "$mp" > /dev/null 2>&1; }
+
+test_expect_success \
+    'mount it' \
+    '
+    mount -o loop $fs "$mp" &&
+    cd "$mp"
+
+    '
+dev=loop-file
+
+do_mkpart()
+{
+  start_sector=$1
+  end_sector=$2
+  # echo '********' $(echo $end_sector - $start_sector + 1 |bc)
+  dd if=/dev/zero of=$dev bs=1b count=2k seek=$end_sector 2> /dev/null &&
+  parted -s $dev mklabel $table_type &&
+  parted -s $dev mkpart p xfs ${start_sector}s ${end_sector}s
+}
+
+# Specify the starting sector number and length in sectors,
+# rather than start and end.
+do_mkpart_start_and_len()
+{
+  start_sector=$1
+  len=$2
+  end_sector=$(echo $start_sector + $len - 1|bc)
+  do_mkpart $start_sector $end_sector
+}
+
+for table_type in dvh; do
+
+test_expect_success \
+    "$table_type: a partition length of 2^32-1 works." \
+    '
+    end=$(echo $n+2^32-2|bc) &&
+    do_mkpart $n $end
+    '
+
+test_expect_success \
+    'print the result' \
+    'parted -s $dev unit s p > out 2>&1 &&
+     sed -n "/^  *1  *$n/s/  */ /gp" out|sed "s/  *\$//" > k && mv k out &&
+     echo " 1 ${n}s ${end}s 4294967295s primary" > exp &&
+     diff -u out exp
+    '
+
+test_expect_failure \
+    "$table_type: a partition length of exactly 2^32 sectors provokes failure." \
+    'do_mkpart $n $(echo $n+2^32-1|bc) > err 2>&1'
+
+bad_part_length()
+{ echo "Error: partition length of $1 sectors exceeds the"\
+  "$table_type-partition-table-imposed maximum of 4294967295"; }
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_part_length 4294967296 > exp && diff -u err exp'
+
+# FIXME: investigate this.
+# Unexpectedly to me, both of these failed with this same diagnostic:
+#
+#   Error: partition length of 4294967296 sectors exceeds the \
+#   DOS-partition-table-imposed maximum of 2^32-1" > exp &&
+#
+# I expected the one below to fail with a length of _4294967297_.
+# Debugging, I see that _check_partition *does* detect this,
+# but the diagnostic doesn't get displayed because of the wonders
+# of parted's exception mechanism.
+
+test_expect_failure \
+    "$table_type: a partition length of 2^32+1 sectors provokes failure." \
+    'do_mkpart $n $(echo $n+2^32|bc) > err 2>&1'
+
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_part_length 4294967297 > exp && diff -u err exp'
+
+# =========================================================
+# Now consider partition starting sector numbers.
+bad_start_sector()
+{ echo "Error: starting sector number, $1 exceeds the"\
+  "$table_type-partition-table-imposed maximum of 4294967295"; }
+
+test_expect_success \
+    "$table_type: a partition start sector number of 2^32-1 works." \
+    'do_mkpart_start_and_len $(echo 2^32-1|bc) 1000'
+
+# FIXME: this partition number 9 (not requested!) looks totally bogus
+# FIXME: For now, we just expect what the code produces.
+# FIXME: In the long run, figure out if it's sensible.
+cat > exp <<EOF
+Model:  (file)
+Disk: 4294970342s
+Sector size (logical/physical): 512B/512B
+Partition Table: $table_type
+
+Number  Start        End          Size   Type      File system  Name  Flags
+ 9      0s           4095s        4096s  extended
+ 1      4294967295s  4294968294s  1000s  primary
+
+EOF
+
+test_expect_success \
+    'print the result' \
+    'parted -s $dev unit s p > out 2>&1 &&
+     sed "s/Disk .*:/Disk:/;s/ *$//" out > k && mv k out &&
+     diff -u out exp
+    '
+
+test_expect_failure \
+    "$table_type: a partition start sector number of 2^32 must fail." \
+    'do_mkpart_start_and_len $(echo 2^32|bc) 1000 > err 2>&1'
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_start_sector 4294967296 > exp && diff -u err exp'
+
+test_expect_failure \
+    "$table_type: a partition start sector number of 2^32+1 must fail, too." \
+    'do_mkpart_start_and_len $(echo 2^32+1|bc) 1000 > err 2>&1'
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_start_sector 4294967297 > exp && diff -u err exp'
+
+done
+
+test_done
diff --git a/tests/t4100-msdos-partition-limits.sh b/tests/t4100-msdos-partition-limits.sh
new file mode 100755
index 0000000..cedbf83
--- /dev/null
+++ b/tests/t4100-msdos-partition-limits.sh
@@ -0,0 +1,168 @@
+#!/bin/sh
+
+# Copyright (C) 2008 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/>.
+
+test_description='enforce limits on partition start sector and length'
+
+# Need root privileges to use mount.
+privileges_required_=1
+
+. ./init.sh
+
+####################################################
+# Create and mount a file system capable of dealing with >=2TB files.
+# We must be able to create a file with an apparent length of 2TB or larger.
+# It needn't be a large file system.
+fs=fs_file
+mp=`pwd`/mount-point
+n=4096
+
+test_expect_success \
+    'create an XFS file system' \
+    '
+    dd if=/dev/zero of=$fs bs=1MB count=2 seek=20 &&
+    mkfs.xfs -q $fs &&
+    mkdir "$mp"
+
+    '
+
+# Unmount upon interrupt, failure, etc., as well as upon normal completion.
+cleanup_() { cd "$test_dir_" && umount "$mp" > /dev/null 2>&1; }
+
+test_expect_success \
+    'mount it' \
+    '
+    mount -o loop $fs "$mp" &&
+    cd "$mp"
+
+    '
+dev=loop-file
+
+do_mkpart()
+{
+  start_sector=$1
+  end_sector=$2
+  # echo '********' $(echo $end_sector - $start_sector + 1 |bc)
+  dd if=/dev/zero of=$dev bs=1b count=2k seek=$end_sector 2> /dev/null &&
+  parted -s $dev mklabel $table_type &&
+  parted -s $dev mkpart p xfs ${start_sector}s ${end_sector}s
+}
+
+# Specify the starting sector number and length in sectors,
+# rather than start and end.
+do_mkpart_start_and_len()
+{
+  start_sector=$1
+  len=$2
+  end_sector=$(echo $start_sector + $len - 1|bc)
+  do_mkpart $start_sector $end_sector
+}
+
+for table_type in msdos; do
+
+test_expect_success \
+    "$table_type: a partition length of 2^32-1 works." \
+    '
+    end=$(echo $n+2^32-2|bc) &&
+    do_mkpart $n $end
+    '
+
+test_expect_success \
+    'print the result' \
+    'parted -s $dev unit s p > out 2>&1 &&
+     sed -n "/^  *1  *$n/s/  */ /gp" out|sed "s/  *\$//" > k && mv k out &&
+     echo " 1 ${n}s ${end}s 4294967295s primary" > exp &&
+     diff -u out exp
+    '
+
+test_expect_failure \
+    "$table_type: a partition length of exactly 2^32 sectors provokes failure." \
+    'do_mkpart $n $(echo $n+2^32-1|bc) > err 2>&1'
+
+bad_part_length()
+{ echo "Error: partition length of $1 sectors exceeds the"\
+  "$table_type-partition-table-imposed maximum of 4294967295"; }
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_part_length 4294967296 > exp && diff -u err exp'
+
+# FIXME: investigate this.
+# Unexpectedly to me, both of these failed with this same diagnostic:
+#
+#   Error: partition length of 4294967296 sectors exceeds the \
+#   DOS-partition-table-imposed maximum of 2^32-1" > exp &&
+#
+# I expected the one below to fail with a length of _4294967297_.
+# Debugging, I see that _check_partition *does* detect this,
+# but the diagnostic doesn't get displayed because of the wonders
+# of parted's exception mechanism.
+
+test_expect_failure \
+    "$table_type: a partition length of 2^32+1 sectors provokes failure." \
+    'do_mkpart $n $(echo $n+2^32|bc) > err 2>&1'
+
+# FIXME: odd that we asked for 2^32+1, yet the diagnostic says 2^32
+# FIXME: Probably due to constraints.
+# FIXME: For now, just accept the current output.
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_part_length 4294967296 > exp && diff -u err exp'
+
+# =========================================================
+# Now consider partition starting sector numbers.
+bad_start_sector()
+{ echo "Error: starting sector number, $1 exceeds the"\
+  "$table_type-partition-table-imposed maximum of 4294967295"; }
+
+test_expect_success \
+    "$table_type: a partition start sector number of 2^32-1 works." \
+    'do_mkpart_start_and_len $(echo 2^32-1|bc) 1000'
+
+cat > exp <<EOF
+Model:  (file)
+Disk: 4294970342s
+Sector size (logical/physical): 512B/512B
+Partition Table: $table_type
+
+Number  Start        End          Size   Type     File system  Flags
+ 1      4294967295s  4294968294s  1000s  primary
+
+EOF
+
+test_expect_success \
+    'print the result' \
+    'parted -s $dev unit s p > out 2>&1 &&
+     sed "s/Disk .*:/Disk:/;s/ *$//" out > k && mv k out &&
+     diff -u out exp
+    '
+
+test_expect_failure \
+    "$table_type: a partition start sector number of 2^32 must fail." \
+    'do_mkpart_start_and_len $(echo 2^32|bc) 1000 > err 2>&1'
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_start_sector 4294967296 > exp && diff -u err exp'
+
+test_expect_failure \
+    "$table_type: a partition start sector number of 2^32+1 must fail, too." \
+    'do_mkpart_start_and_len $(echo 2^32+1|bc) 1000 > err 2>&1'
+test_expect_success \
+    'check for new diagnostic' \
+    'bad_start_sector 4294967296 > exp && diff -u err exp'
+
+done
+
+test_done
-- 
1.5.4.3.g6752




More information about the parted-devel mailing list