Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp132057iof; Sun, 5 Jun 2022 23:18:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyeXnATKENORn2TL9ie0otbOuvZxqRuB44IlmQcQ0hbki4yocWHEE30KWiqPMbVdIK4hwQN X-Received: by 2002:a17:90b:4c0f:b0:1e6:9e44:edcb with SMTP id na15-20020a17090b4c0f00b001e69e44edcbmr21030031pjb.2.1654496325192; Sun, 05 Jun 2022 23:18:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654496325; cv=none; d=google.com; s=arc-20160816; b=LpUpaHXKlWtJRgDRzOMB1Ahdy2mGmJRqETe6GnvuR6UCVSyGOCMh71lgFtrS9hg+lW DWLTN/lzpcu+XxO7iMPHSNhMVb8cAbAjgzTzEYwS5PdxVOHTM3T2IxaDDT4Vk3ME4pGh 8sD+T/gvHQtmuznwtLG/m1OJ+DRZuu9f+BLJqf9eF0N1/OGAdLyuWg81vT/JDTMOLOzw +qIA+kQVG+wVJT3zdPNGxttjdGcojUolzYYImj8B8u2nbcKFAUsRWdisdhOhrQOWzd8B EJSXZjrRh3y3vv34RARddrizOkyYhf+OkG8rsnbnCJOpTP9sgDk3DC/z5XwhTWiRpsat zDTQ== 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=Udgs7n+rDd45xpljzgmMWuRuOTTVJlsnhoVu6Xh6GbU=; b=bKvCdcd6nowjXU1neJzMBHBMJ8OoJREeGNNmv8IwYo18pwM8PAffp6ArCzmk8yX8nJ AAw53stkuSj9K0G7LXFjhGJfI6gvsc5VmsAPscwoETb0Sbo/NCftWec18/5UatfPg/r+ +IF/YMIL3+BlYZSMWMYi/R7y5zj0wEYqmQYGUmNqxPeNlgsuAMfyNltbDpaIqB56sLI3 EW/cFVZ0pBZEvMnbseWGR2bOL0bFCBJRw6pcxrK0Ybucx+qUHbNOm6oRnHr2/vJte46H LlaNeYsz+jUW3c84MhFpu5Satw9LP8XASWLUKH0yFTUhCOwUSkVs8eR12F6sOI2DQ15n GMjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TYRd9Wj9; 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 l65-20020a639144000000b003fb0b79d84esi20030605pge.732.2022.06.05.23.18.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:18:45 -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=TYRd9Wj9; 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 7E56D2C4F8F; Sun, 5 Jun 2022 21:59:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344944AbiFCSSS (ORCPT + 99 others); Fri, 3 Jun 2022 14:18:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347621AbiFCSGK (ORCPT ); Fri, 3 Jun 2022 14:06:10 -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 D5C6A5C668; Fri, 3 Jun 2022 10:59:12 -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 97ED161244; Fri, 3 Jun 2022 17:59:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 966C9C385A9; Fri, 3 Jun 2022 17:59:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654279152; bh=zNBriAybDk2IYMCrYLDMQmf+Q0OdY4Eg8wnW7cnhBSw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TYRd9Wj9k8Yit1iGwy9nXhhCLNOvwSqoI6v90trB1aSzGXYlA80tQPzY4BKkbiL2B Fv+FbtIfk5yevSN/OV+/0+g4wzzAftWn0yVXKSy/+7kTOUCVgbL5CZPFgGSSaXAgmU /4izC0y+1DYt4+o8pnWvJp6iruTnoRk3Q679ezj8= 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.18 40/67] dm crypt: make printing of the key constant-time Date: Fri, 3 Jun 2022 19:43:41 +0200 Message-Id: <20220603173821.888219448@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173820.731531504@linuxfoundation.org> References: <20220603173820.731531504@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 @@ -3439,6 +3439,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) { @@ -3457,9 +3462,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("-");