Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp127805iof; Sun, 5 Jun 2022 23:11:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy21rVkcxXrRarEAr18IgLzw58yfuu+DLnekn3NuTWBM65XF0sdlzy97NH8FKj/ecaijmgh X-Received: by 2002:a05:6a00:21c8:b0:4c4:4bd:dc17 with SMTP id t8-20020a056a0021c800b004c404bddc17mr22761356pfj.57.1654495902479; Sun, 05 Jun 2022 23:11:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495902; cv=none; d=google.com; s=arc-20160816; b=jX/Pbb824huGqBggBjJbmhW6tabJr41wlTBVnDCi6JmtAFxvA7SpXnYoBMu7e2W2km lsc719wqjY53XL5mLyjPGcVOTFbeNojx0Gnyczkrs1mdZ66CIeh5sdF+ym4qJqiSR8G4 ZocjyKuCbJcL9+DZ7WmRSxpzHHhH2KLkZpSCyIhUxqhw7rMXSfim5LUTNvXQhMFoXyzb sOK2U023umLt0Jzq7jf/u/aTygcPVmp80Ci4UO53GRQvt1seSlenPfkwFl3OseVx7hWR npIGSF5ocekuupRr9uj8G0QOD3reTtLdg6AZhZv40flEKpo/nyTR2dcVLE95/w0ySxTr f4hQ== 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=NRtepqaADqT1MSY8GqwObPZosGgs+3Cyu1UeqxVDC7w=; b=mxfOoCaNz5WhMUHjX4CrHi0acoV+IR/1OuCmr2PF9JSupcCErwcMEG8jzapKei33FQ QYtddoyUaXhH92Ay3jSaLu1x/80C2XEApZ385AmeWA39xXJvrIiY7jli9Ro00SYgYFjW SHNGFbTvi+YMXDL5gAD/qfAS5yOeuE52qDybdyDlfKvn7Kt+qnf4YKsJMmECD2InE7+n Gzi1EUp1lyVcA1Xb4EKZVRO5HB/c0sC+V+x/OgoxAy+fuRbl7iL6bn+IbWsR+KYVvMJ+ gcOgdcLeU4u5/DxxFj0b2twGsNrHLY7kcziE2dy7DGmJtw+2l0P39vJ96qmAiz8NJwMe Q2Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bFLmh8bM; 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 iw6-20020a170903044600b0015f0063568bsi17172274plb.340.2022.06.05.23.11.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:11:42 -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=bFLmh8bM; 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 EA60F3167F5; Sun, 5 Jun 2022 21:50:59 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344910AbiFCRmn (ORCPT + 99 others); Fri, 3 Jun 2022 13:42:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238609AbiFCRlz (ORCPT ); Fri, 3 Jun 2022 13:41:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6E4F5370A; Fri, 3 Jun 2022 10:41: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 84A4461AFE; Fri, 3 Jun 2022 17:41:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E7E2C385A9; Fri, 3 Jun 2022 17:41:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278077; bh=QqEwCMdb8k+N56RTMdbQ30Qz5GEWkNdJwW97pvrjALU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bFLmh8bMp7Vcxr3gyY+Os9984JAghrf0b0FwIWA4QxeskF7VAdvl1aJOYTF4xBHND sE5l0n4b+MHApdyJLX0Qv5rokn1dfDed5yx8p1lv8zPEIbSHIu1B4qygrd0KSr+PMG AF/HLpBjyfhwVg7X26zaxqLLKiaZviqczKuCFWqw= 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.14 17/23] dm crypt: make printing of the key constant-time Date: Fri, 3 Jun 2022 19:39:44 +0200 Message-Id: <20220603173814.885694090@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173814.362515009@linuxfoundation.org> References: <20220603173814.362515009@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 @@ -2942,6 +2942,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) { @@ -2960,9 +2965,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("-");