Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp2113760iob; Thu, 5 May 2022 16:15:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXZ5Ri3HWNR4RnYdL2IH/K8dFOZUAP7vUPg+mL/gmRquQ+uUxGvmuQM5uktwG+awjI1z5H X-Received: by 2002:a17:90b:1945:b0:1dc:69bc:69f with SMTP id nk5-20020a17090b194500b001dc69bc069fmr8829365pjb.217.1651792514227; Thu, 05 May 2022 16:15:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651792514; cv=none; d=google.com; s=arc-20160816; b=N56702aQ40pakAPEc56KcRKoE21EbrO97ot5T704uHUnmr5w2qRl+8XmhTdobgn5WD Z6scZKo/BBw113FsJ4f86BkbJ3MaHTXhi8/FwmmUiAVMBfySzoys50zpvvU8Izs0CMdF NjX2GAvj1kk5CNPYr8IHbeN029R+ycxWtsxzMcxSWCXZCe2dqFjFj+RcgSpgKNetWDUA b97F7cU+QhpXQL2SpNBeFBi7t8d4aNv3hmKmy9UBlzLr0D3+OzOZIetz7M0UgRjzhd3X 2qZXfFHExJUqpr2O0io+ZX4F5bOHeX5K0CiKx1FCxuOKfFCymEhFgrX5uq/8HyCEPCwV mMCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kIrbb4CdH7msB6/ZGOQUdrmsgZnd9VgOBwoujzEUW6Y=; b=iw/LQTPv+TKFxFOme32X5biQk1PfcRpu4oh/GwfiZX9B9I+KOqOfgKAGFK1ikNvino W5AZQLT9mFuY54sjp4FDwFV1eWcRzA6cvRY3fyfqABMEHiOZR99f+UheaUgEgDA5UWNd hZ0ocd+mm2zAg2cNQa6xuXoNp+Jsnyogbfc6ahU7n05KLlftcprsvj96F5+pu7xAT6Hm 444wcMwGRrTKpqmhW9YjE/58r3kquYAc599Tvb5rYgu1NgrQJ665ft7kYWbbBO15jIqy 69RA5gIkB7SDpb/VLzyaKdC5ctOUUqdC9BJ9Bgll5/KXmFsUm5+39qBs5AOeIg+C+gWL tDKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=amt5omZQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e14-20020a17090301ce00b001588a10836bsi625351plh.9.2022.05.05.16.14.58; Thu, 05 May 2022 16:15:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=amt5omZQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353671AbiEDQxS (ORCPT + 99 others); Wed, 4 May 2022 12:53:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353732AbiEDQwL (ORCPT ); Wed, 4 May 2022 12:52:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4079947056; Wed, 4 May 2022 09:48:30 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BBB2061771; Wed, 4 May 2022 16:48:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14A49C385A5; Wed, 4 May 2022 16:48:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651682909; bh=gdTM5hy+TRGp+10HhYbWtMpiP8eyq3P5BofsxZqDWhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=amt5omZQdWiIKtPsPMLx30OD2CeoG/96679BE9UFlG30W5lAyEuwLcPmOpEA/dxh9 ZsHxeCjX1Bwb5SBhW4E6EoXST10qz0VCNZn2ifHrUXjhuoit4/HMLUd0xDAG/PU3ls yd/V3GmF/o5bUD2h5gyQSytvtp0F1dQO6EuEV4RY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mikulas Patocka , Linus Torvalds Subject: [PATCH 5.4 27/84] hex2bin: make the function hex_to_bin constant-time Date: Wed, 4 May 2022 18:44:08 +0200 Message-Id: <20220504152929.700822760@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504152927.744120418@linuxfoundation.org> References: <20220504152927.744120418@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mikulas Patocka 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 Cc: stable@vger.kernel.org Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- 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 @@ -627,7 +627,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 @@ -21,15 +21,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);