Jacob Garber has uploaded this change for review.

View Change

util/cbfstool: Use 64 bit integers in multiplications

The operands in these multiplications are 16 bit integers, which are
implicitly converted to signed int's before doing the multiplication.
To prevent possible overflow and other sign troubles, cast them to the
appropriate 64 bit types they are stored in before multiplying.

Change-Id: I5391221d46d620d0e5bd629e2f9680be7a53342e
Signed-off-by: Jacob Garber <jgarber1@ualberta.ca>
Found-by: Coverity CID 12297{03,04,05,06,07,08,09,10}
---
M util/cbfstool/elfheaders.c
1 file changed, 6 insertions(+), 6 deletions(-)

git pull ssh://review.coreboot.org:29418/coreboot refs/changes/86/33986/1
diff --git a/util/cbfstool/elfheaders.c b/util/cbfstool/elfheaders.c
index 8da54d0..1c43fdb 100644
--- a/util/cbfstool/elfheaders.c
+++ b/util/cbfstool/elfheaders.c
@@ -262,7 +262,7 @@
* per the ELF spec, You'd be surprised how many ELF
* readers miss this little detail.
*/
- buffer_splice(&b, in, ehdr->e_phoff, ehdr->e_phentsize * ehdr->e_phnum);
+ buffer_splice(&b, in, ehdr->e_phoff, (size_t)ehdr->e_phentsize * ehdr->e_phnum);
if (check_size(in, ehdr->e_phoff, buffer_size(&b), "program headers"))
return -1;

@@ -304,7 +304,7 @@
* per the ELF spec, You'd be surprised how many ELF
* readers miss this little detail.
*/
- buffer_splice(&b, in, ehdr->e_shoff, ehdr->e_shentsize * ehdr->e_shnum);
+ buffer_splice(&b, in, ehdr->e_shoff, (size_t)ehdr->e_shentsize * ehdr->e_shnum);
if (check_size(in, ehdr->e_shoff, buffer_size(&b), "section headers"))
return -1;

@@ -1180,8 +1180,8 @@
ew->ehdr.e_shnum = ew->num_secs;
metadata_size = 0;
metadata_size += ew->ehdr.e_ehsize;
- metadata_size += ew->ehdr.e_shnum * ew->ehdr.e_shentsize;
- metadata_size += ew->ehdr.e_phnum * ew->ehdr.e_phentsize;
+ metadata_size += (Elf64_Xword)ew->ehdr.e_shnum * ew->ehdr.e_shentsize;
+ metadata_size += (Elf64_Xword)ew->ehdr.e_phnum * ew->ehdr.e_phentsize;
shstroffset = metadata_size;
/* Align up section header string size and metadata size to 4KiB */
metadata_size = ALIGN(metadata_size + shstrlen, 4096);
@@ -1200,11 +1200,11 @@
*/
ew->ehdr.e_shoff = ew->ehdr.e_ehsize;
ew->ehdr.e_phoff = ew->ehdr.e_shoff +
- ew->ehdr.e_shnum * ew->ehdr.e_shentsize;
+ (Elf64_Off)ew->ehdr.e_shnum * ew->ehdr.e_shentsize;

buffer_splice(&metadata, out, 0, metadata_size);
buffer_splice(&phdrs, out, ew->ehdr.e_phoff,
- ew->ehdr.e_phnum * ew->ehdr.e_phentsize);
+ (size_t)ew->ehdr.e_phnum * ew->ehdr.e_phentsize);
buffer_splice(&data, out, metadata_size, program_size);
/* Set up the section header string table contents. */
strtab = &ew->shstrtab_sec->content;

To view, visit change 33986. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Change-Id: I5391221d46d620d0e5bd629e2f9680be7a53342e
Gerrit-Change-Number: 33986
Gerrit-PatchSet: 1
Gerrit-Owner: Jacob Garber <jgarber1@ualberta.ca>
Gerrit-MessageType: newchange