Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4675823iob; Sun, 8 May 2022 21:34:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTQona6XTcsuGwod15HMs3SDhziRh4L1Ego8LyVZewCVBmrbHjad3eEerZ3a7IQwmR2+aV X-Received: by 2002:a17:90b:3649:b0:1db:a201:5373 with SMTP id nh9-20020a17090b364900b001dba2015373mr16232333pjb.175.1652070893713; Sun, 08 May 2022 21:34:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652070893; cv=none; d=google.com; s=arc-20160816; b=ybIWID6O9826webHo+SGiAPBL+NlCeNM7irJcXVfDiR00NkeEmuJgK1wX+R2IiQ2J2 shVmN6pJpBZti3aMZRlduCuyP0+3xaVZhjC8wqA3hQslz65s6lIlJGxLHsSyBi8DK+65 Lpwy/7E8JP5UxULi45YT0r3pfgFT+xRk0NlGSbuETaAabYVhtW1jnxqKAbyZ3dMV4aUE zDoQq2CB5NcG6ywR7udwMnH9SN7Cl7kHGQedbkj6SuCj2afl0oi220DNRVvlXllUnm/V KKqrKZeDu3y7u5PPjJfCFtb322AsxJ7f9xtM25vfd2sJXvM4iKh7r1pvisTuXxBGH1vt 0TrA== 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=FslE9tsa2gO4a2gpZVE3u6yh9EweJ+nBSdKl/x0oRm8=; b=L5zBTKbAQORhR5LJAphwjGaCeQwTnsBmce9zfaZeyXaEFlAPyAmFBsgWnzkU0fMR/Y sidjB0o2EIh0/nM3fyh84AcoHtaqX1IaokhHb/tu3bT1NK43YR1DURIObBLAwqBTuwJf B6CcBlhj4DG29f3AEeXI7oKy+ouIqVTZmXZjWkMfU+nbFpZOfypbxCyQqskupOualhfE BEu3e1IcTveZv3IRcJFWEUQaxetYTu/VqZRrfkM8p6y1YQdHLU8xtq9bwjyBLZkfGzmU SNfBm/1RhD2D2avwTMqSeA+AA4On43hImH+D/1HuEzHO8QJ00P8mLx3o35xFTahXZodm YR2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=w6o+ZIYO; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id fs23-20020a17090af29700b001d653c53a03si18600743pjb.11.2022.05.08.21.34.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 21:34:53 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=w6o+ZIYO; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 12F4F125890; Sun, 8 May 2022 21:34:21 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358441AbiEDRP7 (ORCPT + 99 others); Wed, 4 May 2022 13:15:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355709AbiEDREk (ORCPT ); Wed, 4 May 2022 13:04:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B5284F470; Wed, 4 May 2022 09:53:17 -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 4BD5861852; Wed, 4 May 2022 16:53:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94340C385A4; Wed, 4 May 2022 16:53:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651683196; bh=vzK/3MQfktlOQvm7xuFwJAiiuTHOPQGeUfSkCAJbx3s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w6o+ZIYO+flgNnbuSmSyOYr7KA3Jv9vzxvBxgB5Oe4YWw9vBXOm6oHnMOXdekUOPv OmAqD9GI5/jsamCpYhkznyWa6KZYmdHZdBRvv6LLOjDu/3ORVRxQH9sYTsYJR8akpP h+1yuzQqiVsEyYqbU1V9Yn8NlGxCSGFO3qtlWT/s= 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.15 039/177] hex2bin: make the function hex_to_bin constant-time Date: Wed, 4 May 2022 18:43:52 +0200 Message-Id: <20220504153056.413198047@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504153053.873100034@linuxfoundation.org> References: <20220504153053.873100034@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=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 @@ -277,7 +277,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 @@ -22,15 +22,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);