From ad296391f932764de697dd0bfcfa6f529b69a6cb Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 18 Apr 2020 22:32:29 -0700 Subject: [PATCH 1/4] [clang-tidy] fix wrong *cmp usage Found with bugprone-suspicious-string-compare Signed-off-by: Rosen Penev --- libblkid/src/partitions/atari.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libblkid/src/partitions/atari.c b/libblkid/src/partitions/atari.c index 48c322670..f8b6fb5b5 100644 --- a/libblkid/src/partitions/atari.c +++ b/libblkid/src/partitions/atari.c @@ -164,7 +164,7 @@ static int parse_extended(blkid_probe pr, blkid_partlist ls, if (!IS_ACTIVE(xrs->part[i+1])) break; - if (memcmp(xrs->part[i+1].id, "XGM", 3)) + if (memcmp(xrs->part[i+1].id, "XGM", 3) != 0) return 0; xstart = x0start + be32_to_cpu(xrs->part[i+1].start); -- 2.39.1 From 2cc76d50d7a14bef8e7b07fab11b26c9e49d36a2 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 9 Oct 2020 13:06:08 +0200 Subject: [PATCH 2/4] libblkid: fix Atari prober logic Addresses: https://github.com/karelzak/util-linux/issues/1159 Addresses: https://github.com/karelzak/util-linux/issues/1116 Signed-off-by: Karel Zak --- libblkid/src/partitions/atari.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libblkid/src/partitions/atari.c b/libblkid/src/partitions/atari.c index f8b6fb5b5..f9404f9e1 100644 --- a/libblkid/src/partitions/atari.c +++ b/libblkid/src/partitions/atari.c @@ -199,11 +199,10 @@ static int probe_atari_pt(blkid_probe pr, hdsize = blkid_probe_get_size(pr) / 512; - /* Look for validly looking primary partition */ - for (i = 0; ; i++) { - if (i >= ARRAY_SIZE(rs->part)) - goto nothing; - + /* + * At least one valid partition required + */ + for (i = 0; i < 4; i++) { if (IS_PARTDEF_VALID(rs->part[i], hdsize)) { if (blkid_probe_set_magic(pr, offsetof(struct atari_rootsector, part[i]), @@ -214,6 +213,9 @@ static int probe_atari_pt(blkid_probe pr, } } + if (i == 4) + goto nothing; + if (blkid_partitions_need_typeonly(pr)) /* caller does not ask for details about partitions */ return BLKID_PROBE_OK; -- 2.39.1 From 282ceadc3a72fc07dd0388b8880fd751490bb87f Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 13 Oct 2020 16:19:20 +0200 Subject: [PATCH 3/4] libblkid: make Atari more robust * ignore large disks * check in-table stored device size * check bad sectors list * check partition dimensions against in-table device size Addresses: https://github.com/karelzak/util-linux/issues/1159 Signed-off-by: Karel Zak --- libblkid/src/partitions/atari.c | 57 +++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/libblkid/src/partitions/atari.c b/libblkid/src/partitions/atari.c index f9404f9e1..3f9f4df53 100644 --- a/libblkid/src/partitions/atari.c +++ b/libblkid/src/partitions/atari.c @@ -74,16 +74,27 @@ static int linux_isalnum(unsigned char c) { #define IS_ACTIVE(partdef) ((partdef).flags & 1) -#define IS_PARTDEF_VALID(partdef, hdsize) \ - ( \ - (partdef).flags & 1 && \ - isalnum((partdef).id[0]) && \ - isalnum((partdef).id[1]) && \ - isalnum((partdef).id[2]) && \ - be32_to_cpu((partdef).start) <= (hdsize) && \ - be32_to_cpu((partdef).start) + \ - be32_to_cpu((partdef).size) <= (hdsize) \ - ) +static int is_valid_dimension(uint32_t start, uint32_t size, uint32_t maxoff) +{ + uint64_t end = start + size; + + return end >= start + && 0 < start && start <= maxoff + && 0 < size && size <= maxoff + && 0 < end && end <= maxoff; +} + +static int is_valid_partition(struct atari_part_def *part, uint32_t maxoff) +{ + uint32_t start = be32_to_cpu(part->start), + size = be32_to_cpu(part->size); + + return (part->flags & 1) + && isalnum(part->id[0]) + && isalnum(part->id[1]) + && isalnum(part->id[2]) + && is_valid_dimension(start, size, maxoff); +} static int is_id_common(char *id) { @@ -184,12 +195,20 @@ static int probe_atari_pt(blkid_probe pr, unsigned i; int has_xgm = 0; int rc = 0; - off_t hdsize; + uint32_t rssize; /* size in sectors from root sector */ + uint64_t size; /* size in sectors from system */ /* Atari partition is not defined for other sector sizes */ if (blkid_probe_get_sectorsize(pr) != 512) goto nothing; + size = blkid_probe_get_size(pr) / 512; + + /* Atari is not well defined to support large disks */ + if (size > INT32_MAX) + goto nothing; + + /* read root sector */ rs = (struct atari_rootsector *) blkid_probe_get_sector(pr, 0); if (!rs) { if (errno) @@ -197,13 +216,24 @@ static int probe_atari_pt(blkid_probe pr, goto nothing; } - hdsize = blkid_probe_get_size(pr) / 512; + rssize = be32_to_cpu(rs->hd_size); + + /* check number of sectors stored in the root sector */ + if (rssize < 2 || rssize > size) + goto nothing; + + /* check list of bad blocks */ + if ((rs->bsl_start || rs->bsl_len) + && !is_valid_dimension(be32_to_cpu(rs->bsl_start), + be32_to_cpu(rs->bsl_len), + rssize)) + goto nothing; /* * At least one valid partition required */ for (i = 0; i < 4; i++) { - if (IS_PARTDEF_VALID(rs->part[i], hdsize)) { + if (is_valid_partition(&rs->part[i], rssize)) { if (blkid_probe_set_magic(pr, offsetof(struct atari_rootsector, part[i]), sizeof(rs->part[i].flags) + sizeof(rs->part[i].id), @@ -235,7 +265,6 @@ static int probe_atari_pt(blkid_probe pr, blkid_partlist_increment_partno(ls); continue; } - if (!memcmp(p->id, "XGM", 3)) { has_xgm = 1; rc = parse_extended(pr, ls, tab, p); -- 2.39.1 From c70b4f2a5b99876d230b8f4f413c3bb3ee6647f1 Mon Sep 17 00:00:00 2001 From: Samanta Navarro Date: Tue, 10 Nov 2020 11:48:04 +0100 Subject: [PATCH 4/4] libblkid: limit amount of parsed partitions The linux kernel does not support more than 256 partitions (DISK_MAX_PARTS). The atari and mac block devices have no such limits. Use dos logical partition limit for atari as well (100). Use the kernel limit for mac (256). Signed-off-by: Samanta Navarro --- libblkid/src/partitions/atari.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libblkid/src/partitions/atari.c b/libblkid/src/partitions/atari.c index 3f9f4df53..314f04763 100644 --- a/libblkid/src/partitions/atari.c +++ b/libblkid/src/partitions/atari.c @@ -141,12 +141,16 @@ static int parse_extended(blkid_probe pr, blkid_partlist ls, blkid_parttable tab, struct atari_part_def *part) { uint32_t x0start, xstart; - unsigned i = 0; + unsigned ct = 0, i = 0; int rc; x0start = xstart = be32_to_cpu(part->start); while (1) { struct atari_rootsector *xrs; + + if (++ct > 100) + break; + xrs = (struct atari_rootsector *) blkid_probe_get_sector(pr, xstart); if (!xrs) { if (errno) -- 2.39.1