lib: Prevent unintended sign extensions
authorJacob Garber <jgarber1@ualberta.ca>
Fri, 9 Aug 2019 23:13:59 +0000 (17:13 -0600)
committerAnup Patel <anup.patel@wdc.com>
Mon, 12 Aug 2019 03:56:38 +0000 (09:26 +0530)
commit5e4021a2f5ca346d1c12b80d346c1a2e7eb4b501
treef30705dc9c46ec8c2e58573f3542ad53f84a77ba
parent6ddf71e6e90878ee107b0d9f98492ad27d31c73a
lib: Prevent unintended sign extensions

In the last argument to sbi_memset() we essentially have the following
multiplication:

    size_t = u16 * u16

Due to C's integer semantics, both u16's are implicitly converted to int
before the multiplication, which cannot hold all possible values of a
u16 * u16. If the multiplication overflows, the intermediate result will
be a negative number. On 64-bit platforms, this will be sign-extended to
a huge integer in the conversion to a u64 (aka size_t). Being the size
argument to sbi_memset(), this could potentially cause a large
out-of-bounds write. The solution is to manually cast one of the u16 to
a size_t, which will make it large enough to avoid the implicit
conversion and any overflow.

Signed-off-by: Jacob Garber <jgarber1@ualberta.ca>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
lib/sbi/sbi_fifo.c