Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp7823897ioo; Fri, 3 Jun 2022 14:32:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2q5WnEAEPWuNlziebDyWMn8uvTEM8glOziZPluVojTq8tBYXPCT4HGzyEAu/7+r4LS8h0 X-Received: by 2002:aa7:94a6:0:b0:51b:ddb8:1fcf with SMTP id a6-20020aa794a6000000b0051bddb81fcfmr6317293pfl.23.1654291942262; Fri, 03 Jun 2022 14:32:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654291942; cv=none; d=google.com; s=arc-20160816; b=CK7a9x4u/Otpv6B0gt3qrWLWOc0Jsbj9i+sJVElt9y5Xi1oviPpBDQ3jUvIDrd9whd nsxLkqUf2CO2Qp2wKY6NpW7EkzhVM9CbNJxZj+0Ik7Cxv8W2mtJV9Zb+MA6uewQBQrsx G9Pk6L48FWJE2sppYNFySxMupfVgf6JEUnRMSwWqmeg414ZEgdQGuxUCQZn+k2lwa3/U lgfzKBQ+2I8YvIvBiRR8qTAuIwYUQoSDqFMtgwyM6zQ8ROZoJJ86yWCGjM6DXJQ2Fmu9 qc65fZH2RWwtio0JGOFKfJf5d4GRnUAEoG8hGqH1elWANllYpyinQn3+FnWHM7Be82JQ 6wfg== 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=/BIaE+AfqFJhH9uvFXvTds+L2Ow1lNCTjR0tZEK8feM=; b=K4cfpio1XGyQ4mN6PdeIsFOJLnrJjM70mlcXam8DfQUwlBu9WhnIp7EOyj5yvNmYRN cUDCZlxzuxQgeMIL/MHl3rziNmiOxYHOmIyD8JkNY2Fzbepz4Pr1DdMDJZY1PwTTutm1 EQvSU5uVvIEYczylC9rmyQUh/1kaL4/7CIclUafLkshiRpoOyZvR754oGyUOs/UJv812 z35qZqdFAqfY9dqopudiAch6YAKiOjt7T2Jaq+hBRpMhkOHf7sc00RdwqIjDe8++OIRA ajrueSRtz8DVpR5QotUhMC8HikBnedt6CXD1UMfGRISCeP+O5AyF1/DhY9nl4/smpCDY CAPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0zOPD0SE; 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 m37-20020a635825000000b003fadcc14015si11407989pgb.15.2022.06.03.14.32.07; Fri, 03 Jun 2022 14:32:22 -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=0zOPD0SE; 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 S1344909AbiFCRoj (ORCPT + 99 others); Fri, 3 Jun 2022 13:44:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344895AbiFCRnt (ORCPT ); Fri, 3 Jun 2022 13:43:49 -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 BA90153A52; Fri, 3 Jun 2022 10:42:20 -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 355C761AE4; Fri, 3 Jun 2022 17:42:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30E13C385A9; Fri, 3 Jun 2022 17:42:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278139; bh=FtO2RLN9CrpjYCc5A46uz2lhr5W97xly3lDUSs8kygk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0zOPD0SE6ttI17jl3mYnbwEHdzx/EtveJulRmSexQ0W9OIgHroLk8v6LWuckUF2m3 GLNRRNBkXun+wJ4RePFWKaJ5FQviXvl8d18cLjHmLBtt96NrI5wfnw/OoR0tgE1y6/ lJFNAYfiXP/WwRDBRqjE+2ohptug72xG1XE5Yiik= 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 4.19 22/30] dm crypt: make printing of the key constant-time Date: Fri, 3 Jun 2022 19:39:50 +0200 Message-Id: <20220603173815.747049681@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173815.088143764@linuxfoundation.org> References: <20220603173815.088143764@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 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 @@ -2932,6 +2932,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) { @@ -2950,9 +2955,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("-");