Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp110775iof; Sun, 5 Jun 2022 22:42:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2eJH2U5ECC/GUnJjcMti5ivJE77PZbb6dbq3uAa7cIJQMFHxFWSWmvxo4fQzM5qkMearF X-Received: by 2002:a17:903:3296:b0:164:13db:509 with SMTP id jh22-20020a170903329600b0016413db0509mr22558373plb.128.1654494168037; Sun, 05 Jun 2022 22:42:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654494168; cv=none; d=google.com; s=arc-20160816; b=0CV7J1XCkfWTgLTrZ/yP5cx9uCKCTjR3NPRfLsXFtMI4TEi8pkFXua4+8TSZEEkhhL kg4Gin44YzsHrkBGMOxkDvlByTJ59Ca4kHlfYwP7esmdvPqgHxz/oROw25fVG2MRVt5s I8ZEkybVjsBujSPBwq5iWS6h3gak521mEhte9LCka57UcTInXYIRhIP+zTZ9wEBMtrLS tJX3JbQDcX/zD2GbJSEiDbrr6ZkCyRem4y3y7ovekf4bPusmaSzmvssSaNoTX3LsMZRV BoHcmAkedoqhGxxT3uPPfRCpdWJdKPdujfCZGREm9sbfXjSbYA++82zx5ok0twgaCchb TRlg== 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=xfqxi93CatRL+t3eDpEOBrbcQrpy26VqYf3vdX4n2ls=; b=TJbqqciSaGcYs5cTf5i/tHYa0vWaX6PmoB0EJpAchh0kBXlqVssW+xCGVnuyLVzczg M5W7uAl/Sqc5AjuwENGN+Pno9otrVjkhm89ptVOk3PWSoR3XWeZhkMEm+fcq9KJMrW0q BiLYLWvjvtkRCCZrD60lYqoemYo+LelGmO6awaY9a027eSTAOclt5lxiRsqyVjlrGOCv pqNo96QLqpDZcVoHeFffUQceMKhgXfP1g/eFvMKfotuflLK8j85bXLzDdUtjvv3E7n4f Ms3SEKqdS5kshnyfjr02KfhJ96jo3w/qHShBK1eGlDV22WQZVybxh+ywEmRDaJaE0c38 vH2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CKL14Hom; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id z15-20020a170902d54f00b001675e8a0c88si8432481plf.72.2022.06.05.22.42.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:42:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CKL14Hom; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 057F021F9D9; Sun, 5 Jun 2022 21:33:29 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345471AbiFCRtt (ORCPT + 99 others); Fri, 3 Jun 2022 13:49:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237724AbiFCRsN (ORCPT ); Fri, 3 Jun 2022 13:48:13 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2A855418C; Fri, 3 Jun 2022 10:45:09 -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 4FCC7B82419; Fri, 3 Jun 2022 17:45:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1743C385A9; Fri, 3 Jun 2022 17:45:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278307; bh=9PFnQ6U/pODrMpclM6aw2q4n7Wt6WMpJbMXQn9a3ZHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CKL14HomZ2Eq7xpeX1aBiL8lHKjGRfLlpAB9Z5/+VNmtXiwKldKQXmqITIlWO498h zpRHtqvYgCMiq8rtUmoejSMpTqHKArpoXoNPhMWjoVeOqGwZxSaXMow6SaEFu7WY57 lQYBO6oNOevKGt8BnO84kmw7cZabNLDdpx42q+zM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mikulas Patocka , Milan Broz , Mike Snitzer Subject: [PATCH 5.4 24/34] dm crypt: make printing of the key constant-time Date: Fri, 3 Jun 2022 19:43:20 +0200 Message-Id: <20220603173816.695539048@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173815.990072516@linuxfoundation.org> References: <20220603173815.990072516@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=-3.1 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 567dd8f34560fa221a6343729474536aa7ede4fd upstream. The device mapper dm-crypt target is using scnprintf("%02x", cc->key[i]) to report the current key to userspace. However, this is not a constant-time operation and it may leak information about the key via timing, via cache access patterns or via the branch predictor. Change dm-crypt's key printing to use "%c" instead of "%02x". Also introduce hex2asc() that carefully avoids any branching or memory accesses when converting a number in the range 0 ... 15 to an ascii character. Cc: stable@vger.kernel.org Signed-off-by: Mikulas Patocka Tested-by: Milan Broz Signed-off-by: Mike Snitzer Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-crypt.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -2817,6 +2817,11 @@ static int crypt_map(struct dm_target *t return DM_MAPIO_SUBMITTED; } +static char hex2asc(unsigned char c) +{ + return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27); +} + static void crypt_status(struct dm_target *ti, status_type_t type, unsigned status_flags, char *result, unsigned maxlen) { @@ -2835,9 +2840,12 @@ static void crypt_status(struct dm_targe if (cc->key_size > 0) { if (cc->key_string) DMEMIT(":%u:%s", cc->key_size, cc->key_string); - else - for (i = 0; i < cc->key_size; i++) - DMEMIT("%02x", cc->key[i]); + else { + for (i = 0; i < cc->key_size; i++) { + DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), + hex2asc(cc->key[i] & 0xf)); + } + } } else DMEMIT("-");