diff options
author | Heinrich Schuchardt <heinrich.schuchardt@canonical.com> | 2023-09-02 09:35:23 +0200 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2023-09-11 16:31:25 -0400 |
commit | 69f4d37302583c3a2c8445ff69826c52c6fce7e3 (patch) | |
tree | a76787982bb806876a37d156dc6f0d784628185c | |
parent | 396f315520bcbcc2b710ab696326b239577c1ad2 (diff) | |
download | u-boot-69f4d37302583c3a2c8445ff69826c52c6fce7e3.zip u-boot-69f4d37302583c3a2c8445ff69826c52c6fce7e3.tar.gz u-boot-69f4d37302583c3a2c8445ff69826c52c6fce7e3.tar.bz2 |
cmd: gpt: fix calc_parts_list_len()
* Avoid incrementing by moving comma into strlen("uuid_disk=,") and
considering NUL byte.
* Appending a UUID only adds UUID_STR_LEN bytes.
Don't count the terminating NUL.
* The length of the hexadecimal representation of lba_int is
2 * sizeof(lba_int).
* We don't use a 'MiB' postfix but a '0x' prefix.
* The uuid field is only needed if configured.
Fixes: 2fcaa413b3f6 ("gpt: harden set_gpt_info() against non NULL-terminated strings")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
-rw-r--r-- | cmd/gpt.c | 34 |
1 files changed, 20 insertions, 14 deletions
@@ -162,26 +162,32 @@ static bool found_key(const char *str, const char *key) return result; } +/** + * calc_parts_list_len() - get size of partition table description + * + * @numparts: number of partitions + * Return: string size including terminating NUL + */ static int calc_parts_list_len(int numparts) { - int partlistlen = UUID_STR_LEN + 1 + strlen("uuid_disk="); - /* for the comma */ - partlistlen++; - - /* per-partition additions; numparts starts at 1, so this should be correct */ - partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN + 1); + /* number of hexadecimal digits of the lbaint_t representation */ + const int lbaint_size = 2 * sizeof(lbaint_t); + int partlistlen; + + /* media description including terminating NUL */ + partlistlen = strlen("uuid_disk=;") + UUID_STR_LEN + 1; + /* per-partition descriptions; numparts */ + partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN); /* see part.h for definition of struct disk_partition */ - partlistlen += numparts * (strlen("start=MiB,") + sizeof(lbaint_t) + 1); - partlistlen += numparts * (strlen("size=MiB,") + sizeof(lbaint_t) + 1); + partlistlen += numparts * (strlen("start=0x,") + lbaint_size); + partlistlen += numparts * (strlen("size=0x,") + lbaint_size); #ifdef CONFIG_PARTITION_TYPE_GUID partlistlen += numparts * (strlen("type=,") + UUID_STR_LEN + 1); #endif - partlistlen += numparts * strlen("bootable,"); - partlistlen += numparts * (strlen("uuid=;") + UUID_STR_LEN + 1); - /* for the terminating null */ - partlistlen++; - debug("Length of partitions_list is %d for %d partitions\n", partlistlen, - numparts); + if (IS_ENABLED(CONFIG_PARTITION_UUIDS)) + partlistlen += numparts * (strlen("uuid=;") + UUID_STR_LEN); + debug("Length of partitions_list is %d for %d partitions\n", + partlistlen, numparts); return partlistlen; } |