kernel: 5.10: update nvmem subsystem to the 5.15 state
[openwrt/openwrt.git] / target / linux / generic / backport-5.10 / 803-v5.13-0005-nvmem-core-Fix-unintentional-sign-extension-issue.patch
1 From 55022fdeace8e432f008787ce03703bdcc9c3ca9 Mon Sep 17 00:00:00 2001
2 From: Colin Ian King <colin.king@canonical.com>
3 Date: Tue, 30 Mar 2021 12:12:38 +0100
4 Subject: [PATCH] nvmem: core: Fix unintentional sign extension issue
5
6 The shifting of the u8 integer buf[3] by 24 bits to the left will
7 be promoted to a 32 bit signed int and then sign-extended to a
8 u64. In the event that the top bit of buf[3] is set then all
9 then all the upper 32 bits of the u64 end up as also being set
10 because of the sign-extension. Fix this by casting buf[i] to
11 a u64 before the shift.
12
13 Fixes: a28e824fb827 ("nvmem: core: Add functions to make number reading easy")
14 Reviewed-by: Douglas Anderson <dianders@chromium.org>
15 Signed-off-by: Colin Ian King <colin.king@canonical.com>
16 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
17 Addresses-Coverity: ("Unintended sign extension")
18 Link: https://lore.kernel.org/r/20210330111241.19401-8-srinivas.kandagatla@linaro.org
19 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
20 ---
21 drivers/nvmem/core.c | 2 +-
22 1 file changed, 1 insertion(+), 1 deletion(-)
23
24 --- a/drivers/nvmem/core.c
25 +++ b/drivers/nvmem/core.c
26 @@ -1699,7 +1699,7 @@ int nvmem_cell_read_variable_le_u64(stru
27 /* Copy w/ implicit endian conversion */
28 *val = 0;
29 for (i = 0; i < len; i++)
30 - *val |= buf[i] << (8 * i);
31 + *val |= (uint64_t)buf[i] << (8 * i);
32
33 kfree(buf);
34