Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1252712pxb; Wed, 10 Feb 2021 04:09:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJxAWKPWijEYicdvTMBSU+GbhiirRc116bh2pLGuqhlNhFEVT0kmMlOk1AyjIg8lJZtcGBQq X-Received: by 2002:aa7:d2c7:: with SMTP id k7mr2851683edr.374.1612958943456; Wed, 10 Feb 2021 04:09:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612958943; cv=none; d=google.com; s=arc-20160816; b=hmHb6wSyUFAV0SGcWsJMLzNLRLKaXXowzf1AyVlOcfg8t436G/yuVgnQN52d3D4Hki 5QPygdIUoKgaWa6/lyaC8tkhfDtrQz1a3zUgTcwoWvLoHyUg/eJQjyiq7RoUpGTPE4WU uzAOIWcbYFV48RYEKPCcbF2jJy14dYqQEZbFsn20dFHSo6hdfsF8r0TCpinel9NkY0JN 6Pa+GEMQZwAkh/owUgZM7itXdGj4zEx633VP7uyGkK2d/E8rykWinS2vz4UV+KMNOSPS ewnbTZhPKLLPk4fkyKsPquiLed9wGtrIVI+ECSgr3vPJ+LvBDguCG2ASrWn20iY7FtBk A6Mw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=JazRcKdFXeERbK5ozIk2Kj7nt7LVXsgOG7wfwF/6AMo=; b=puYWxzd5nPCW8b63Q/G7YwXO0zjd3aDCnyH4f11T12Mxv5y6UeCz80U86rhdFPzKZB vUho+eFPS+F/Pye+tCkwgbyDcRzq8MPwVf9GE/Srm783UO1iFntzD7anCwQE1anAPmxS +JePsMNtiq1+rhMod3+FPdnFzY2GAnO3doPmX6XSsmTzpjM2J2HPx8Yk68amWd7mXvQt Xi+W1BwZ358p0Fvdk0v27Msj/GWSS3u/azPHVx39yeS5yGVBDYQr7rTMQx14umjmE3ep hovFbFudb1wzRFH8s5uO7OFNTL6rFi7q7FDXsDmp54EcBdPHkbhl3L0EYm9DZ7D84qON PZgQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k4si1267605edq.62.2021.02.10.04.08.38; Wed, 10 Feb 2021 04:09:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231321AbhBJMID (ORCPT + 99 others); Wed, 10 Feb 2021 07:08:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231428AbhBJMGU (ORCPT ); Wed, 10 Feb 2021 07:06:20 -0500 Received: from smtp-bc08.mail.infomaniak.ch (smtp-bc08.mail.infomaniak.ch [IPv6:2001:1600:4:17::bc08]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93036C06178B for ; Wed, 10 Feb 2021 04:04:00 -0800 (PST) Received: from smtp-3-0001.mail.infomaniak.ch (unknown [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4DbJMF6RxzzMprjB; Wed, 10 Feb 2021 13:03:53 +0100 (CET) Received: from localhost (unknown [23.97.221.149]) by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4DbJMD45Mbzlh8T3; Wed, 10 Feb 2021 13:03:52 +0100 (CET) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: David Howells , David Woodhouse , Jarkko Sakkinen Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , "David S . Miller" , Eric Snowberg , Herbert Xu , James Morris , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Mimi Zohar , "Serge E . Hallyn" , Tyler Hicks , keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v6 3/5] certs: Make blacklist_vet_description() more strict Date: Wed, 10 Feb 2021 13:04:08 +0100 Message-Id: <20210210120410.471693-4-mic@digikod.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210210120410.471693-1-mic@digikod.net> References: <20210210120410.471693-1-mic@digikod.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Mickaël Salaün Before exposing this new key type to user space, make sure that only meaningful blacklisted hashes are accepted. This is also checked for builtin blacklisted hashes, but a following commit make sure that the user will notice (at built time) and will fix the configuration if it already included errors. Check that a blacklist key description starts with a valid prefix and then a valid hexadecimal string. Cc: David Howells Cc: David Woodhouse Cc: Eric Snowberg Signed-off-by: Mickaël Salaün Reviewed-by: Jarkko Sakkinen --- Changes since v5: * Add Reviewed-by Jarkko. Changes since v2: * Fix typo in blacklist_vet_description() comment, spotted by Tyler Hicks. * Add Jarkko's Acked-by. Changes since v1: * Return ENOPKG (instead of EINVAL) when a hash is greater than the maximum currently known hash (suggested by David Howells). --- certs/blacklist.c | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/certs/blacklist.c b/certs/blacklist.c index f467b10030fb..069d1dd0fa05 100644 --- a/certs/blacklist.c +++ b/certs/blacklist.c @@ -19,6 +19,16 @@ #include "blacklist.h" #include "common.h" +/* + * According to crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo(), + * the size of the currently longest supported hash algorithm is 512 bits, + * which translates into 128 hex characters. + */ +#define MAX_HASH_LEN 128 + +static const char tbs_prefix[] = "tbs"; +static const char bin_prefix[] = "bin"; + static struct key *blacklist_keyring; extern __initconst const u8 revocation_certificate_list[]; @@ -30,24 +40,40 @@ extern __initconst const unsigned long revocation_certificate_list_size; */ static int blacklist_vet_description(const char *desc) { - int n = 0; - - if (*desc == ':') - return -EINVAL; - for (; *desc; desc++) - if (*desc == ':') - goto found_colon; + int i, prefix_len, tbs_step = 0, bin_step = 0; + + /* The following algorithm only works if prefix lengths match. */ + BUILD_BUG_ON(sizeof(tbs_prefix) != sizeof(bin_prefix)); + prefix_len = sizeof(tbs_prefix) - 1; + for (i = 0; *desc; desc++, i++) { + if (*desc == ':') { + if (tbs_step == prefix_len) + goto found_colon; + if (bin_step == prefix_len) + goto found_colon; + return -EINVAL; + } + if (i >= prefix_len) + return -EINVAL; + if (*desc == tbs_prefix[i]) + tbs_step++; + if (*desc == bin_prefix[i]) + bin_step++; + } return -EINVAL; found_colon: desc++; - for (; *desc; desc++) { + for (i = 0; *desc && i < MAX_HASH_LEN; desc++, i++) { if (!isxdigit(*desc) || isupper(*desc)) return -EINVAL; - n++; } + if (*desc) + /* The hash is greater than MAX_HASH_LEN. */ + return -ENOPKG; - if (n == 0 || n & 1) + /* Checks for an even number of hexadecimal characters. */ + if (i == 0 || i & 1) return -EINVAL; return 0; } -- 2.30.0