[parted-devel] [PATCH 1/2] libparted: enforce dos partition limit

Jim Meyering jim at meyering.net
Mon Jan 23 19:13:25 UTC 2012


H. Peter Anvin wrote:
> On 01/23/2012 10:22 AM, Phillip Susi wrote:
>>
>> I think the enforcement needs to stay the way it is.  GPT enforces its
>> limit, and arch/linux.c assumes that the limit the partition table
>> advertises is actual and correct.  If that isn't the case, then linux.c
>> and anyone else asking the pt for its limit probably needs refactored to
>> not do so.  Advertising a limit that isn't actually a limit is wrong.
>>
>> If you want to raise the limit to 256 or even higher then you could have
>> more than 256 partitions on msdos, even though it isn't supported on linux.
>>
>> Theoretically linux.c should then throw an exception complaining if the
>> partition count exceeds that supported by the kernel, though I'm not
>> sure fixing that is particularly important since parted has gone this
>> long without complaining if you exceeded the old limit of 16, and now
>> the limit is 256 it is even less of an issue.
>
> GPT enforces its limit because *that's how many slots there are in the
> partition table* (by the way, you *do* handle GPT partition tables with
> more than 128 slots, right?)
>
> Newer Linux kernel s have an overflow system which is at least supposed
> to allow for additional partitions without problems; if that is broken
> then that is a kernel bug, and playing games in parted will just make it
> harder to diagnose and fix.

To my dismay, parted could not even create a 100-partition GPT table.
But it was quick/easy to fix.
With the following, it can do 128.

However, considering that we hard-code the de-facto standard
value of "128" in the GPT header, currently there is no way to write
GPT tables with a different number of partition table entries.


>From 4b61aedfb63cc97b405649f7261feb54cfdcdbfa Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Mon, 23 Jan 2012 19:55:39 +0100
Subject: [PATCH 1/2] libparted: gpt: avoid invalid free with many (~100)
 partitions

* parted/parted.c (do_print): Remove invalid free.
Bug introduced via v1.8.8.1-19-gb56d69c.
* NEWS (Bug fixes): Mention this.
---
 NEWS            |    3 +++
 parted/parted.c |    1 -
 2 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/NEWS b/NEWS
index 9d189dc..484034b 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,9 @@ GNU parted NEWS                                    -*- outline -*-

 ** Bug fixes

+  libparted: avoid an invalid free when creating many partitions in
+  a GPT partition table.  [bug introduced in parted-1.9.0]
+
   The msdos partition table claimed a maximum partition count of 16
   but would allow you to go beyond that.  This resulted in the kernel
   not being informed of those partitions.  Corrected to enforce the
diff --git a/parted/parted.c b/parted/parted.c
index 96f9c34..02c5fdb 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1171,7 +1171,6 @@ do_print (PedDevice** dev)
                     //PED_ASSERT (row.cols == caption.cols)
                     table_add_row_from_strlist (table, row);
                     str_list_destroy (row);
-                    free (tmp);
                     free (start);
                     free (end);
                     free (size);
--
1.7.9.rc2.2.g183d6


>From 5b6fa1a471d2e4bf6c6ad9e2c0a1b01c4f185164 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Mon, 23 Jan 2012 20:01:30 +0100
Subject: [PATCH 2/2] tests: exercise today's bug fix, creating a GPT table
 with 128 entries

* tests/t0212-gpt-many-partitions.sh: New test.
* tests/Makefile.am (TESTS): Add it.
---
 tests/Makefile.am                  |    1 +
 tests/t0212-gpt-many-partitions.sh |   48 ++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)
 create mode 100644 tests/t0212-gpt-many-partitions.sh

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 88efbdd..17486e2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -22,6 +22,7 @@ TESTS = \
   t0208-mkpart-end-in-IEC.sh \
   t0210-gpt-resized-partition-entry-array.sh \
   t0211-gpt-rewrite-header.sh \
+  t0212-gpt-many-partitions.sh \
   t0220-gpt-msftres.sh \
   t0250-gpt.sh \
   t0280-gpt-corrupt.sh \
diff --git a/tests/t0212-gpt-many-partitions.sh b/tests/t0212-gpt-many-partitions.sh
new file mode 100644
index 0000000..b33cd5a
--- /dev/null
+++ b/tests/t0212-gpt-many-partitions.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+# gpt: create many partitions
+# Before parted-3.1, this would provoke an invalid free.
+
+# Copyright (C) 2012 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/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted $srcdir
+
+ss=$sector_size_
+
+ns=300
+dev=dev-file
+# create a file large enough to hold a GPT partition table
+dd if=/dev/null of=$dev bs=$ss seek=$ns || framework_failure
+
+for i in $(seq 128); do
+  s=$((34+i-1))
+  cmd="$cmd mkpart p$i ${s}s ${s}s"
+done
+parted -m -a min -s $dev mklabel gpt $cmd u s p > out 2>&1 || fail=1
+
+nl='
+'
+exp=$(printf '%s\n' 'BYT;' "...:${ns}s:file:$ss:$ss:gpt:;")"$nl"
+
+for i in $(seq 128); do
+  s=$((34+i-1))
+  exp="$exp$i:${s}s:${s}s:1s::p$i:;$nl"
+done
+printf %s "$exp" > exp || fail=1
+
+sed '2s/^[^:]*:/...:/' out > k && mv k out
+compare exp out || fail=1
+
+Exit $fail
--
1.7.9.rc2.2.g183d6



More information about the parted-devel mailing list