Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1832486iob; Thu, 5 May 2022 09:01:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxaYOsb9h6TJ8io+trwQx/H6PIb3fgcXuuvGAgnqdD6Wn7KwUj6bPn4K3SX+twpps8tHyGH X-Received: by 2002:a5d:9d83:0:b0:65a:9ffb:a80f with SMTP id ay3-20020a5d9d83000000b0065a9ffba80fmr3886351iob.116.1651766517955; Thu, 05 May 2022 09:01:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651766517; cv=none; d=google.com; s=arc-20160816; b=up4P8++7RuroqtwFxn56ZVdE0VvJPajZzhEznMUQv7D62DUd8xKpogrDJ+CevqiYuM 9HQS35sizn4VTpsE/Ywtr6Xo9Tqw6Ly2xKr5UClLZmAPGMbyttLduUM1aXng+w/1QScZ lfvNtHfTQvyaOVCrHw1hvoYXf9Y2KfhS9O32Pn8U3ky1Z94rdHVbvJkv/55+4zqc5Ogw OZihHDvbOICuTkLj6sZyjI7xgiQc23vZ9Ngcu15C1MeD7GoWPNPIjJsWn25duWWtBpSy i8+l0VrJeu2iWHUkZEynv6IfJO7w+OJV0exxxpqVyBGiSgY297RIjnwlOVMmuUEoY4v6 9kVQ== 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=cw9qvvywMP723tRCaccnEE9NQVs6S3Ust1h/axfDsHo=; b=oQXhOTNtxlGagfv4K5A1KkGqnaYACUykPvvjyo9nr5mQxoyoAb46w1ZXRbHSGJQsaz bccC6xaXnf1nV0CIepKp5VBzvyMUzzAFr86BnC13fr3QzB19utyZSqNYOCBlrxET4Mb/ 3kvAjqG6RB3W8UOYXKWi13OjzQvyrDnEPPLS6ckiD218d47VuOXFK7x0MvkSI5+Ia5H8 Co048vUepVu60HpThESsFZdPet1hlw/BqahElQtTgKMkwOE9TqOg+kEv+kmzH/EOsQQY cJRM6cEW1WVJucEkegaKAInhOxlJdQb5B/Joz7iiGZ2+xkoG/xGZoTt6oD7DWzCP717U RjHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Iq6ySQPB; 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 w9-20020a056e02190900b002c81cd4f3c8si1550129ilu.141.2022.05.05.09.01.43; Thu, 05 May 2022 09:01:57 -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=Iq6ySQPB; 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 S1354950AbiEDRD1 (ORCPT + 99 others); Wed, 4 May 2022 13:03:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354601AbiEDQ6y (ORCPT ); Wed, 4 May 2022 12:58:54 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 484EF1F629; Wed, 4 May 2022 09:50:45 -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 ams.source.kernel.org (Postfix) with ESMTPS id EDD9AB82554; Wed, 4 May 2022 16:50:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96A49C385A5; Wed, 4 May 2022 16:50:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651683042; bh=7dpsaI8+DZ3trt2V0mBtNEbPk+Gy1oRkzaJHsKmOU9M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Iq6ySQPBnQKL3Rrdu6Sx2XB25iK+3+3MwYHy6zuS6fyvbHtiOgb5DRgh2Khgn8UVc bIaMrMG2s3glsNwFcFL0vFSxp7UhtGcetA5dppAvYjsPb9Td0KLf/w0Dj90vmBpNxV KMLjfPmdcN4uwVPIEYWsPbsOWrKARKHbnYELZlDU= 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.10 033/129] hex2bin: make the function hex_to_bin constant-time Date: Wed, 4 May 2022 18:43:45 +0200 Message-Id: <20220504153023.908308980@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504153021.299025455@linuxfoundation.org> References: <20220504153021.299025455@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 @@ -635,7 +635,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);