]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: improve identification of ISO9660 partition
authorDaniel Drake <drake@endlessm.com>
Mon, 16 Dec 2019 06:16:48 +0000 (14:16 +0800)
committerDaniel Drake <drake@endlessm.com>
Mon, 16 Dec 2019 06:29:24 +0000 (14:29 +0800)
Recent changes to the iso9660 handler attempt to better handle the case
where the media has both an ordinary ISO9660 filesystem, and a partition
table with a partition entry pointing at the ISO9660 filesystem.

Rather than assuming the presence of a partition table means that there
is an ISO9660 partition, check that such a partition exists before
deciding upon which device the metadata should be presented. One real
world example of this is the grub-mkrescue image format; add a unit test
to cover that.

Secondly, even if we find an appropriate entry in the partition table,
verify that we actually have a device that corresponds to the partition
where we would then proceed to expose this metadata. In the case of a
CD/DVD (i.e. /dev/sr0) we would not normally expect to see devices
corresponding to the partitions, in that case the ISO metadata should be
presented on the whole disk (/dev/sr0).

Fixes: 7ef86a0891 ("libblkid: improve handling of ISO files with partition tables")
Link: https://bugzilla.redhat.com/show_bug.cgi?id=1783066
Signed-off-by: Daniel Drake <drake@endlessm.com>
libblkid/src/superblocks/iso9660.c
tests/expected/blkid/iso-partitions-grub [new file with mode: 0644]
tests/ts/blkid/iso-partitions
tests/ts/blkid/iso-partitions-grub.img.xz [new file with mode: 0644]

index 730939f7064814ab9610d44719bcbe6de4a0d6ba..fd7f7cf816df71dcc62e3068d2eae9eaeb354aa0 100644 (file)
@@ -19,7 +19,9 @@
 #include <stdint.h>
 #include <ctype.h>
 
+#include "iso9660.h"
 #include "superblocks.h"
+#include "sysfs.h"
 
 struct iso9660_date {
        unsigned char year[4];
@@ -43,11 +45,13 @@ struct iso_volume_descriptor {
        unsigned char   unused[8];
        unsigned char   space_size[8];
        unsigned char   escape_sequences[8];
-       unsigned char  unused1[222];
+       unsigned char  unused1[32];
+       unsigned char  logical_block_size[4];
+       unsigned char  unused2[186];
        unsigned char  publisher_id[128];
-       unsigned char  unused2[128];
+       unsigned char  unused3[128];
        unsigned char  application_id[128];
-       unsigned char  unused3[111];
+       unsigned char  unused4[111];
        struct iso9660_date created;
        struct iso9660_date modified;
 } __attribute__((packed));
@@ -169,15 +173,22 @@ static int is_str_empty(const unsigned char *str, size_t len)
 /*
  * The ISO format specifically avoids the first 32kb to allow for a
  * partition table to be added, if desired.
- * When an ISO contains a partition table, the usual thing to do is to
- * have a partition that points at the iso filesystem. In such case,
+ * When an ISO contains a partition table, it is common to have a partition
+ * that points at the iso mani filesystem. In such case,
  * we want to only probe the iso metadata for the corresponding partition
  * device, avoiding returning the metadata for the parent block device.
  */
-static bool isofs_belongs_to_device(blkid_probe pr)
+static bool isofs_belongs_to_device(blkid_probe pr, struct iso_volume_descriptor *iso)
 {
-       dev_t devno;
+       dev_t devno, disk_devno, isopart_devno;
        blkid_partlist ls;
+       blkid_loff_t pr_offset;
+       int isopart_partno = -1;
+       int nparts, i;
+       int num_sectors, sector_size;
+       long iso_size;
+       blkid_partition par;
+       struct path_cxt *pc;
 
        /* Get device number, but if that fails, assume we aren't dealing
         * with partitions, and continue probing. */
@@ -191,10 +202,49 @@ static bool isofs_belongs_to_device(blkid_probe pr)
        if (!ls)
                return true;
 
-       /* Check that the device we're working with corresponds to an
-        * entry in the partition table. If so, this is the correct
-        * device to return the iso metadata on. */
-       return blkid_partlist_devno_to_partition(ls, devno) != NULL;
+       /* Calculate size of ISO9660 filesystem */
+       num_sectors = isonum_733(iso->space_size, false);
+       sector_size = isonum_723(iso->logical_block_size, false);
+       iso_size = ((long)num_sectors * (long)sector_size) >> 9;
+
+       /* Look for a partition that matches the ISO9660 filesystem start sector
+        * and size. */
+       pr_offset = blkid_probe_get_offset(pr);
+       nparts = blkid_partlist_numof_partitions(ls);
+       for (i = 0; i < nparts; i++) {
+               par = blkid_partlist_get_partition(ls, i);
+               if (blkid_partition_get_start(par) == pr_offset &&
+                   blkid_partition_get_size(par) == iso_size) {
+                       isopart_partno = blkid_partition_get_partno(par);
+                       break;
+               }
+       }
+
+       /* If we didn't find a matching partition, consider the current
+        * device as an appropriate owner of the ISO9660 filesystem. */
+       if (isopart_partno == -1)
+               return true;
+
+       /* A partition matching the ISO9660 filesystem was found. Look for
+        * a devno that corresponds to this partition. */
+       disk_devno = blkid_probe_get_wholedisk_devno(pr);
+       pc = ul_new_sysfs_path(disk_devno, NULL, NULL);
+       if (!pc)
+               return true;
+
+       isopart_devno = sysfs_blkdev_partno_to_devno(pc, isopart_partno);
+       ul_unref_path(pc);
+
+       /* If no ISO9660 partition devno was found, consider the current device
+        * as an appropriate owner of the filesystem. This can happen for CD/DVDs,
+        * where partitions may exist in the table, but are not usually probed by
+        * the kernel. */
+       if (!isopart_devno)
+               return true;
+
+       /* We found a partition that matches the ISO9660 filesystem. Check that it
+        * corresponds to the device that we are probing. */
+       return devno == isopart_devno;
 }
 
 /* iso9660 [+ Microsoft Joliet Extension] */
@@ -214,7 +264,7 @@ static int probe_iso9660(blkid_probe pr, const struct blkid_idmag *mag)
 
        /* Check if the iso metadata should be returned on a different device
         * instead of this one. */
-       if (!isofs_belongs_to_device(pr))
+       if (!isofs_belongs_to_device(pr, iso))
                return 1;
 
        memcpy(label, iso->volume_id, sizeof(label));
diff --git a/tests/expected/blkid/iso-partitions-grub b/tests/expected/blkid/iso-partitions-grub
new file mode 100644 (file)
index 0000000..9a403a8
--- /dev/null
@@ -0,0 +1,10 @@
+ID_FS_BLOCK_SIZE=2048
+ID_FS_UUID=2019-12-16-06-07-35-00
+ID_FS_UUID_ENC=2019-12-16-06-07-35-00
+ID_FS_BOOT_SYSTEM_ID=EL TORITO SPECIFICATION
+ID_FS_LABEL=ISOIMAGE
+ID_FS_LABEL_ENC=ISOIMAGE
+ID_FS_TYPE=iso9660
+ID_FS_USAGE=filesystem
+ID_PART_TABLE_UUID=4b46ece2-6efb-4bf9-90a6-858c95e5581d
+ID_PART_TABLE_TYPE=gpt
index 4aaa164f7d747c3ead372aeb3a80c0611dd1b66d..cfa0a93fff9591b40cd25d5ee66f8f82a32ac364 100755 (executable)
@@ -69,4 +69,23 @@ udevadm settle
 $TS_CMD_BLKID -p -o udev ${TS_DEVICE} >> $TS_OUTPUT
 ts_finalize_subtest
 
+# This image (created by grub-mkrescue) has a partition table where the
+# partitions do not point to the ISO9660 filesystem.
+xz -dc ${TS_SELF}/iso-partitions-grub.img.xz > ${TS_DEVICE}
+udevadm settle
+
+ts_init_subtest "grub"
+$TS_CMD_PARTX -a ${TS_DEVICE} &>/dev/null
+udevadm settle
+
+# Check that the ISO metadata is shown on the main disk device
+$TS_CMD_BLKID -p -o udev ${TS_DEVICE} >> $TS_OUTPUT
+
+# substitute major/minor number before comparison
+sed -i \
+  -e 's/^\(ID_PART_ENTRY_DISK\)=.*/\1=__ts_majorminor__/' \
+  $TS_OUTPUT
+
+ts_finalize_subtest
+
 ts_finalize
diff --git a/tests/ts/blkid/iso-partitions-grub.img.xz b/tests/ts/blkid/iso-partitions-grub.img.xz
new file mode 100644 (file)
index 0000000..7fe886a
Binary files /dev/null and b/tests/ts/blkid/iso-partitions-grub.img.xz differ