stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Mikulas Patocka <mpatocka@redhat.com>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH 4.19 20/88] hex2bin: make the function hex_to_bin constant-time
Date: Tue, 10 May 2022 15:07:05 +0200	[thread overview]
Message-ID: <20220510130734.334896733@linuxfoundation.org> (raw)
In-Reply-To: <20220510130733.735278074@linuxfoundation.org>

From: Mikulas Patocka <mpatocka@redhat.com>

commit e5be15767e7e284351853cbaba80cde8620341fb upstream.

The function hex2bin is used to load cryptographic keys into device
mapper targets dm-crypt and dm-integrity.  It should take constant time
independent on the processed data, so that concurrently running
unprivileged code can't infer any information about the keys via
microarchitectural convert channels.

This patch changes the function hex_to_bin so that it contains no
branches and no memory accesses.

Note that this shouldn't cause performance degradation because the size
of the new function is the same as the size of the old function (on
x86-64) - and the new function causes no branch misprediction penalties.

I compile-tested this function with gcc on aarch64 alpha arm hppa hppa64
i386 ia64 m68k mips32 mips64 powerpc powerpc64 riscv sh4 s390x sparc32
sparc64 x86_64 and with clang on aarch64 arm hexagon i386 mips32 mips64
powerpc powerpc64 s390x sparc32 sparc64 x86_64 to verify that there are
no branches in the generated code.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Cc: stable@vger.kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 include/linux/kernel.h |    2 +-
 lib/hexdump.c          |   32 +++++++++++++++++++++++++-------
 2 files changed, 26 insertions(+), 8 deletions(-)

--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -631,7 +631,7 @@ static inline char *hex_byte_pack_upper(
 	return buf;
 }
 
-extern int hex_to_bin(char ch);
+extern int hex_to_bin(unsigned char ch);
 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
 extern char *bin2hex(char *dst, const void *src, size_t count);
 
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -25,15 +25,33 @@ EXPORT_SYMBOL(hex_asc_upper);
  *
  * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  * input.
+ *
+ * This function is used to load cryptographic keys, so it is coded in such a
+ * way that there are no conditions or memory accesses that depend on data.
+ *
+ * Explanation of the logic:
+ * (ch - '9' - 1) is negative if ch <= '9'
+ * ('0' - 1 - ch) is negative if ch >= '0'
+ * we "and" these two values, so the result is negative if ch is in the range
+ *	'0' ... '9'
+ * we are only interested in the sign, so we do a shift ">> 8"; note that right
+ *	shift of a negative value is implementation-defined, so we cast the
+ *	value to (unsigned) before the shift --- we have 0xffffff if ch is in
+ *	the range '0' ... '9', 0 otherwise
+ * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is
+ *	in the range '0' ... '9', 0 otherwise
+ * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0'
+ *	... '9', -1 otherwise
+ * the next line is similar to the previous one, but we need to decode both
+ *	uppercase and lowercase letters, so we use (ch & 0xdf), which converts
+ *	lowercase to uppercase
  */
-int hex_to_bin(char ch)
+int hex_to_bin(unsigned char ch)
 {
-	if ((ch >= '0') && (ch <= '9'))
-		return ch - '0';
-	ch = tolower(ch);
-	if ((ch >= 'a') && (ch <= 'f'))
-		return ch - 'a' + 10;
-	return -1;
+	unsigned char cu = ch & 0xdf;
+	return -1 +
+		((ch - '0' +  1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
+		((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
 }
 EXPORT_SYMBOL(hex_to_bin);
 



  parent reply	other threads:[~2022-05-10 13:24 UTC|newest]

Thread overview: 98+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-10 13:06 [PATCH 4.19 00/88] 4.19.242-rc1 review Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 01/88] usb: mtu3: fix USB 3.0 dual-role-switch from device to host Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 02/88] USB: quirks: add a Realtek card reader Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 03/88] USB: quirks: add STRING quirk for VCOM device Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 04/88] USB: serial: whiteheat: fix heap overflow in WHITEHEAT_GET_DTR_RTS Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 05/88] USB: serial: cp210x: add PIDs for Kamstrup USB Meter Reader Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 06/88] USB: serial: option: add support for Cinterion MV32-WA/MV32-WB Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 07/88] USB: serial: option: add Telit 0x1057, 0x1058, 0x1075 compositions Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 08/88] xhci: stop polling roothubs after shutdown Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 09/88] iio: dac: ad5592r: Fix the missing return value Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 10/88] iio: dac: ad5446: Fix read_raw not returning set value Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 11/88] iio: magnetometer: ak8975: Fix the error handling in ak8975_power_on() Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 12/88] usb: misc: fix improper handling of refcount in uss720_probe() Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 13/88] usb: gadget: uvc: Fix crash when encoding data for usb request Greg Kroah-Hartman
2022-05-10 13:06 ` [PATCH 4.19 14/88] usb: gadget: configfs: clear deactivation flag in configfs_composite_unbind() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 15/88] usb: dwc3: core: Fix tx/rx threshold settings Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 16/88] usb: dwc3: gadget: Return proper request status Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 17/88] serial: imx: fix overrun interrupts in DMA mode Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 18/88] serial: 8250: Also set sticky MCR bits in console restoration Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 19/88] serial: 8250: Correct the clock for EndRun PTP/1588 PCIe device Greg Kroah-Hartman
2022-05-10 13:07 ` Greg Kroah-Hartman [this message]
2022-05-10 13:07 ` [PATCH 4.19 21/88] hex2bin: fix access beyond string end Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 22/88] mtd: rawnand: fix ecc parameters for mt7622 Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 23/88] USB: Fix xhci event ring dequeue pointer ERDP update issue Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 24/88] ARM: dts: imx6qdl-apalis: Fix sgtl5000 detection issue Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 25/88] phy: samsung: Fix missing of_node_put() in exynos_sata_phy_probe Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 26/88] phy: samsung: exynos5250-sata: fix missing device put in probe error paths Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 27/88] ARM: OMAP2+: Fix refcount leak in omap_gic_of_init Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 28/88] ARM: dts: Fix mmc order for omap3-gta04 Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 29/88] ARM: dts: logicpd-som-lv: Fix wrong pinmuxing on OMAP35 Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 30/88] ipvs: correctly print the memory size of ip_vs_conn_tab Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 31/88] mtd: rawnand: Fix return value check of wait_for_completion_timeout Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 32/88] tcp: md5: incorrect tcp_header_len for incoming connections Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 33/88] sctp: check asoc strreset_chunk in sctp_generate_reconf_event Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 34/88] ARM: dts: imx6ull-colibri: fix vqmmc regulator Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 35/88] pinctrl: pistachio: fix use of irq_of_parse_and_map() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 36/88] net: hns3: add validity check for message data length Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 37/88] ip_gre: Make o_seqno start from 0 in native mode Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 38/88] tcp: fix potential xmit stalls caused by TCP_NOTSENT_LOWAT Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 39/88] bus: sunxi-rsb: Fix the return value of sunxi_rsb_device_create() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 40/88] clk: sunxi: sun9i-mmc: check return value after calling platform_get_resource() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 41/88] net: bcmgenet: hide status block before TX timestamping Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 42/88] bnx2x: fix napi API usage sequence Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 43/88] ASoC: wm8731: Disable the regulator when probing fails Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 44/88] ip6_gre: Avoid updating tunnel->tun_hlen in __gre6_xmit() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 45/88] x86: __memcpy_flushcache: fix wrong alignment if size > 2^32 Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 46/88] cifs: destage any unwritten data to the server before calling copychunk_write Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 47/88] drivers: net: hippi: Fix deadlock in rr_close() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 48/88] x86/cpu: Load microcode during restore_processor_state() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 49/88] tty: n_gsm: fix wrong signal octet encoding in convergence layer type 2 Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 50/88] tty: n_gsm: fix malformed counter for out of frame data Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 51/88] netfilter: nft_socket: only do sk lookups when indev is available Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 52/88] tty: n_gsm: fix insufficient txframe size Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 53/88] tty: n_gsm: fix missing explicit ldisc flush Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 54/88] tty: n_gsm: fix wrong command retry handling Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 55/88] tty: n_gsm: fix wrong command frame length field encoding Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 56/88] tty: n_gsm: fix incorrect UA handling Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 57/88] drm/vgem: Close use-after-free race in vgem_gem_create Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 58/88] MIPS: Fix CP0 counter erratum detection for R4k CPUs Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 59/88] parisc: Merge model and model name into one line in /proc/cpuinfo Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 60/88] ALSA: fireworks: fix wrong return count shorter than expected by 4 bytes Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 61/88] gpiolib: of: fix bounds check for gpio-reserved-ranges Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 62/88] Revert "SUNRPC: attempt AF_LOCAL connect on setup" Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 63/88] firewire: fix potential uaf in outbound_phy_packet_callback() Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 64/88] firewire: remove check of list iterator against head past the loop body Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 65/88] firewire: core: extend card->lock in fw_core_handle_bus_reset Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 66/88] genirq: Synchronize interrupt thread startup Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 67/88] ASoC: wm8958: Fix change notifications for DSP controls Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 68/88] can: grcan: grcan_close(): fix deadlock Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 69/88] can: grcan: use ofdev->dev when allocating DMA memory Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 70/88] nfc: replace improper check device_is_registered() in netlink related functions Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 71/88] nfc: nfcmrvl: main: reorder destructive operations in nfcmrvl_nci_unregister_dev to avoid bugs Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 72/88] NFC: netlink: fix sleep in atomic bug when firmware download timeout Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 73/88] hwmon: (adt7470) Fix warning on module removal Greg Kroah-Hartman
2022-05-10 13:07 ` [PATCH 4.19 74/88] ASoC: dmaengine: Restore NULL prepare_slave_config() callback Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 75/88] net: stmmac: dwmac-sun8i: add missing of_node_put() in sun8i_dwmac_register_mdio_mux() Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 76/88] net: emaclite: Add error handling for of_address_to_resource() Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 77/88] selftests: mirror_gre_bridge_1q: Avoid changing PVID while interface is operational Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 78/88] smsc911x: allow using IRQ0 Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 79/88] btrfs: always log symlinks in full mode Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 80/88] net: igmp: respect RCU rules in ip_mc_source() and ip_mc_msfilter() Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 81/88] kvm: x86/cpuid: Only provide CPUID leaf 0xA if host has architectural PMU Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 82/88] mm: fix unexpected zeroed page mapping with zram swap Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 83/88] tcp: make sure treq->af_specific is initialized Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 84/88] dm: fix mempool NULL pointer race when completing IO Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 85/88] dm: interlock pending dm_io and dm_wait_for_bios_completion Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 86/88] PCI: aardvark: Clear all MSIs at setup Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 87/88] PCI: aardvark: Fix reading MSI interrupt number Greg Kroah-Hartman
2022-05-10 13:08 ` [PATCH 4.19 88/88] mmc: rtsx: add 74 Clocks in power on flow Greg Kroah-Hartman
2022-05-10 18:07 ` [PATCH 4.19 00/88] 4.19.242-rc1 review Pavel Machek
2022-05-10 20:34 ` Sudip Mukherjee
2022-05-11  5:40   ` Greg Kroah-Hartman
2022-05-10 22:46 ` Shuah Khan
2022-05-11  1:11 ` Guenter Roeck
2022-05-11  1:56 ` Samuel Zou
2022-05-11  9:19 ` Jon Hunter
2022-05-11  9:57 ` Naresh Kamboju
2022-05-11 10:08 ` Sudip Mukherjee

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=20220510130734.334896733@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).