All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Marek Behún" <marek.behun@nic.cz>
To: u-boot@lists.denx.de
Subject: [PATCH u-boot v4 01/36] regmap: fix a serious pointer casting bug
Date: Thu, 20 May 2021 13:23:50 +0200	[thread overview]
Message-ID: <20210520112425.25166-2-marek.behun@nic.cz> (raw)
In-Reply-To: <20210520112425.25166-1-marek.behun@nic.cz>

There is a serious bug in regmap_read() and regmap_write() functions
where an uint pointer is cast to (void *) which is then cast to (u8 *),
(u16 *), (u32 *) or (u64 *), depending on register width of the map.

For example given a regmap with 16-bit register width the code
	int val = 0x12340000;
	regmap_read(map, 0, &val);
only changes the lower 16 bits of val on little-endian machines.
The upper 16 bits will remain 0x1234.

Nobody noticed this probably because this bug can be triggered with
regmap_write() only on big-endian architectures (which are not used by
many people anymore), and on little endian this bug has consequences
only if register width is 8 or 16 bits and also the memory place to
which regmap_read() should store it's result has non-zero upper bits,
which it seems doesn't happen anywhere in U-Boot normally. CI managed to
trigger this bug in unit test of dm_test_devm_regmap_field when compiled
for sandbox_defconfig using LTO.

Fix this by utilizing an union { u8; u16; u32; u64; } and reading data
into this union / writing data from this union.

Signed-off-by: Marek Beh?n <marek.behun@nic.cz>
Cc: Simon Glass <sjg@chromium.org>
Cc: Heiko Schocher <hs@denx.de>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Pratyush Yadav <p.yadav@ti.com>
---
 drivers/core/regmap.c | 59 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 57 insertions(+), 2 deletions(-)

diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c
index b51ce108c1..3206f3d112 100644
--- a/drivers/core/regmap.c
+++ b/drivers/core/regmap.c
@@ -435,7 +435,36 @@ int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len)
 
 int regmap_read(struct regmap *map, uint offset, uint *valp)
 {
-	return regmap_raw_read(map, offset, valp, map->width);
+	union {
+		u8 v8;
+		u16 v16;
+		u32 v32;
+		u64 v64;
+	} u;
+	int res;
+
+	res = regmap_raw_read(map, offset, &u, map->width);
+	if (res)
+		return res;
+
+	switch (map->width) {
+	case REGMAP_SIZE_8:
+		*valp = u.v8;
+		break;
+	case REGMAP_SIZE_16:
+		*valp = u.v16;
+		break;
+	case REGMAP_SIZE_32:
+		*valp = u.v32;
+		break;
+	case REGMAP_SIZE_64:
+		*valp = u.v64;
+		break;
+	default:
+		unreachable();
+	}
+
+	return 0;
 }
 
 static inline void __write_8(u8 *addr, const u8 *val,
@@ -546,7 +575,33 @@ int regmap_raw_write(struct regmap *map, uint offset, const void *val,
 
 int regmap_write(struct regmap *map, uint offset, uint val)
 {
-	return regmap_raw_write(map, offset, &val, map->width);
+	union {
+		u8 v8;
+		u16 v16;
+		u32 v32;
+		u64 v64;
+	} u;
+
+	switch (map->width) {
+	case REGMAP_SIZE_8:
+		u.v8 = val;
+		break;
+	case REGMAP_SIZE_16:
+		u.v16 = val;
+		break;
+	case REGMAP_SIZE_32:
+		u.v32 = val;
+		break;
+	case REGMAP_SIZE_64:
+		u.v64 = val;
+		break;
+	default:
+		debug("%s: regmap size %zu unknown\n", __func__,
+		      (size_t)map->width);
+		return -EINVAL;
+	}
+
+	return regmap_raw_write(map, offset, &u, map->width);
 }
 
 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
-- 
2.26.3

  reply	other threads:[~2021-05-20 11:23 UTC|newest]

Thread overview: 111+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-20 11:23 [PATCH u-boot v4 00/36] U-Boot LTO (Sandbox + Some ARM boards) Marek Behún
2021-05-20 11:23 ` Marek Behún [this message]
2021-05-20 17:51   ` [PATCH u-boot v4 01/36] regmap: fix a serious pointer casting bug Simon Glass
2021-05-20 19:30     ` Marek Behun
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 02/36] checkpatch: require quotes around section name in the __section() macro Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 03/36] treewide: Convert macro and uses of __section(foo) to __section("foo") Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 04/36] compiler.h: align the __ADDRESSABLE macro with Linux' version Marek Behún
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 05/36] test/py: improve regular expression for ut subtest symbol matcher Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 06/36] string: make memcpy(), memset(), memcmp() and memmove() visible for LTO Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 07/36] efi_loader: fix warning when linking with LTO Marek Behún
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 08/36] efi_loader: add Sphinx doc for __efi_runtime and __efi_runtime_data Marek Behún
2021-05-25  0:54   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 09/36] efi_loader: add macro for const EFI runtime data Marek Behún
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:23 ` [PATCH u-boot v4 10/36] efi_selftest: compiler flags for efi_selftest_miniapp_exception.o Marek Behún
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 11/36] lib: crc32: put the crc_table variable into efi_runtime_rodata section Marek Behún
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 12/36] Makefile, Makefile.spl: cosmetic change Marek Behún
2021-05-25  0:55   ` Tom Rini
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 13/36] build: use thin archives instead of incremental linking Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 14/36] build: support building with Link Time Optimizations Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 15/36] build: link with --build-id=none Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 16/36] sandbox: errno: avoid conflict with libc's errno Marek Behún
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 17/36] sandbox: use sections instead of symbols for getopt array boundaries Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 18/36] sandbox: make LTO available Marek Behún
2021-05-25  0:55   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 19/36] sandbox: enable LTO by default Marek Behún
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 20/36] ARM: global_data: make set_gd() work for armv5 and armv6 Marek Behún
2021-05-20 17:51   ` Simon Glass
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 21/36] ARM: make gd a function call for LTO and set via set_gd() Marek Behún
2021-05-20 17:52   ` Simon Glass
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 22/36] ARM: fix LTO build for some thumb-interwork cases Marek Behún
2021-05-20 17:52   ` Simon Glass
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 23/36] ARM: fix LTO for imx28_xea Marek Behún
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 24/36] ARM: fix LTO for apf27 Marek Behún
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 25/36] ARM: fix LTO for keystone Marek Behún
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 26/36] ARM: kona: fix clk_bsc_enable() type mismatch for LTO Marek Behún
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 27/36] ARM: imx8m: fix imx_eqos_txclk_set_rate() " Marek Behún
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 28/36] ARM: fix LTO for seaboard Marek Behún
2021-05-20 17:52   ` Simon Glass
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 29/36] ARM: fix LTO for rockchip and samsung Marek Behún
2021-05-22  2:45   ` Kever Yang
2021-05-25  0:56   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 30/36] ARM: omap3: fix LTO for DM3730 (and possibly other omap3 boards) Marek Behún
2021-05-25  0:57   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 31/36] armv8: SPL: discard relocation information Marek Behún
2021-05-20 17:52   ` Simon Glass
2021-05-25  0:57   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 32/36] ata: ahci: fix ahci_link_up() type mismatch for LTO Marek Behún
2021-05-25  0:57   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 33/36] ARM: make LTO available Marek Behún
2021-05-20 17:52   ` Simon Glass
2021-05-25  0:57   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 34/36] ARM: don't use -ffunction-sections/-fdata-sections with LTO build Marek Behún
2021-05-20 17:52   ` Simon Glass
2021-05-25  0:57   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 35/36] ARM: don't use --gc-sections with LTO when using private libgcc Marek Behún
2021-05-20 17:52   ` Simon Glass
2021-05-25  0:57   ` Tom Rini
2021-05-20 11:24 ` [PATCH u-boot v4 36/36] ARM: enable LTO for some boards Marek Behún
2021-05-20 18:56   ` Adam Ford
2021-05-21 14:11     ` Tom Rini
2021-05-21 16:00       ` Marek Behún
2021-05-21 16:56         ` Tom Rini
2021-05-24 15:40           ` Tom Rini
2021-05-24 15:58             ` Marek Behun
2021-05-24 16:23               ` Tom Rini
2021-05-24 17:09               ` Tom Rini
2021-05-24 17:44                 ` Tom Rini
2021-05-24 19:19                   ` Marek Behun
2021-05-24 19:56                     ` Tom Rini
2021-05-24 19:54               ` Tom Rini
2021-05-21 16:00       ` Marek Behún
2021-05-25  0:57   ` Tom Rini
2021-06-01 14:59 ` [PATCH u-boot v4 00/36] U-Boot LTO (Sandbox + Some ARM boards) Patrick DELAUNAY
2021-06-01 15:05   ` Tom Rini
2021-06-01 16:22   ` Marek Behún
2021-06-01 16:55     ` Heinrich Schuchardt
2021-06-01 17:23       ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210520112425.25166-2-marek.behun@nic.cz \
    --to=marek.behun@nic.cz \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.