Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp377686pxv; Thu, 22 Jul 2021 02:21:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcNE5JfNQCCldcfz+2gap8GPgTE/7C+N/RTkbUTY/yBHIUGp4bpPgJ+x3DTOmcPEz/L52V X-Received: by 2002:aa7:db54:: with SMTP id n20mr53375562edt.21.1626945692540; Thu, 22 Jul 2021 02:21:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626945692; cv=none; d=google.com; s=arc-20160816; b=oT6//V8pmzLffj4cmzLNxBettbn1J01c3zpZuHHzRijE2uyUzcCJQE1GKS5yM6bv5r eKMOoUcV3dl5anhHtuo80PjUNTw3OJMraLrnJ78TtEA14QKzd2qU6hnbKAxPQDtYhcGL 79f09cjdBPvQN3/xQCWMjkyFSoO/1yCYRYmUUlsuQiqbbCfPllxA2xi0uRs1/CG4BLpB J6CVQLosBkcl/CrINkn8SeKqkbPwMrkCcaOrlPrUTLHl6+SafDtWvNeoOP3DMNeNG1SF BhBvV4x0AWFuK248CKrbYpdiRXtDigkEh+PBH7+ponYrryay+7sQ+r0CTY4zn+Y0n5Fq oMAQ== 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=WfJy0/i3Ql5jlZH3r7011TQpJ0wwyiUEhrsl5cCW+1Q=; b=ggVLqdWMqnGU0cB+XVe0dLfUm69gUUEwoSSvyc+TjAj/AxYYqs8Kv1nlEabhkjuX1V C26qB/j91fNBHDji2EkKvE9PE8GvtHKHxQBTqLCZbfz5BA3mUPmYOzFVnmUndpY3wU+X iMUX3NEwtzTraNDjFCgPW3dHbabq7BAW8do6uQxHKIdGBTy4YjYLY2XxeehVZFvsdjgG mlY0sqc7BKgMAarBxGjXUa77zoJoplAE/ZkNeuVh2hSeE/DPFcQZsWvmMdB02ZkKDNBR 0bsUfFs4Db8P0UaWRmtnfPEuIASBxtd3CVhpzfOMotWe1rJO7E5n69dWqN6zvoqITINo ZSoQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 y13si14941739ejb.410.2021.07.22.02.21.08; Thu, 22 Jul 2021 02:21:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231445AbhGVIiF (ORCPT + 99 others); Thu, 22 Jul 2021 04:38:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231355AbhGVIhz (ORCPT ); Thu, 22 Jul 2021 04:37:55 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACBAEC0613CF for ; Thu, 22 Jul 2021 02:18:30 -0700 (PDT) Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m6UqE-0001NL-VW; Thu, 22 Jul 2021 11:18:06 +0200 Received: from afa by dude.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1m6UqC-0001Cm-EB; Thu, 22 Jul 2021 11:18:04 +0200 From: Ahmad Fatoum To: David Howells , Jarkko Sakkinen , James Morris , "Serge E. Hallyn" , Alasdair Kergon , Mike Snitzer , dm-devel@redhat.com, Song Liu , Richard Weinberger Cc: kernel@pengutronix.de, Ahmad Fatoum , linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, keyrings@vger.kernel.org, linux-mtd@lists.infradead.org, linux-security-module@vger.kernel.org, linux-integrity@vger.kernel.org Subject: [RFC PATCH v1 1/4] keys: introduce key_extract_material helper Date: Thu, 22 Jul 2021 11:17:59 +0200 Message-Id: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: afa@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While keys of differing type have a common struct key definition, there is no common scheme to the payload and key material extraction differs. For kernel functionality that supports different key types, this means duplicated code for key material extraction and because key type is discriminated by a pointer to a global, users need to replicate reachability checks as well, so builtin code doesn't depend on a key type symbol offered by a module. Make this easier by adding a common helper with initial support for user, logon, encrypted and trusted keys. The code is taken from dm-crypt, which is migrated to use the helper in a later commit. The implementation must be partially in a header to support configurations where the key type symbol is defined in a module, but key support in general is built-in. Signed-off-by: Ahmad Fatoum --- I am not sure whether the chosen header and source file are the best places for this. This could be made header-only too if that's preferred. To: David Howells To: Jarkko Sakkinen To: James Morris To: "Serge E. Hallyn" To: Alasdair Kergon To: Mike Snitzer To: dm-devel@redhat.com To: Song Liu To: Richard Weinberger Cc: linux-kernel@vger.kernel.org Cc: linux-raid@vger.kernel.org Cc: keyrings@vger.kernel.org Cc: linux-mtd@lists.infradead.org Cc: linux-security-module@vger.kernel.org Cc: linux-integrity@vger.kernel.org --- include/linux/key.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ security/keys/key.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/include/linux/key.h b/include/linux/key.h index 7febc4881363..916612f5b313 100644 --- a/include/linux/key.h +++ b/include/linux/key.h @@ -20,6 +20,8 @@ #include #include #include +#include +#include #ifdef __KERNEL__ #include @@ -487,6 +489,48 @@ extern void key_fsuid_changed(struct cred *new_cred); extern void key_fsgid_changed(struct cred *new_cred); extern void key_init(void); +/* + * internal use, so key core code need not link against + * all supported key types + * */ +enum __key_type { + KEY_TYPE_UNKNOWN, KEY_TYPE_USER, KEY_TYPE_ENCRYPTED, KEY_TYPE_TRUSTED +}; + +const void *__key_extract_material(const struct key *key, enum __key_type type, + unsigned int *len); + +/** + * key_extract_material - Extract decrypted data out of a key + * @key: a logon, user, encrypted or trusted key + * @len: pointer to variable to store key size into + * + * Extract decrypted data out of supported key types + * + * Returns a pointer to the key material if successfull or an error + * pointer if key type is not compiled in, the buffer is too + * small or the key was revoked. + */ +static inline const void *key_extract_material(const struct key *key, + unsigned int *len) +{ + extern struct key_type key_type_user; + extern struct key_type key_type_logon; + extern struct key_type key_type_encrypted; + extern struct key_type key_type_trusted; + enum __key_type type = KEY_TYPE_UNKNOWN; + const struct key_type *t = key->type; + + if (t == &key_type_logon || t == &key_type_user) + type = KEY_TYPE_USER; + else if (IS_REACHABLE(CONFIG_ENCRYPTED_KEYS) && t == &key_type_encrypted) + type = KEY_TYPE_ENCRYPTED; + else if (IS_REACHABLE(CONFIG_TRUSTED_KEYS) && t == &key_type_trusted) + type = KEY_TYPE_TRUSTED; + + return __key_extract_material(key, type, len); +} + #else /* CONFIG_KEYS */ #define key_validate(k) 0 @@ -504,6 +548,7 @@ extern void key_init(void); #define key_init() do { } while(0) #define key_free_user_ns(ns) do { } while(0) #define key_remove_domain(d) do { } while(0) +#define key_extract_material(k, l) ERR_PTR(-EINVAL) #endif /* CONFIG_KEYS */ #endif /* __KERNEL__ */ diff --git a/security/keys/key.c b/security/keys/key.c index c45afdd1dfbb..69cd1cb8c413 100644 --- a/security/keys/key.c +++ b/security/keys/key.c @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include "internal.h" struct kmem_cache *key_jar; @@ -1140,6 +1143,43 @@ int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep) } EXPORT_SYMBOL(generic_key_instantiate); +const void *__key_extract_material(const struct key *key, + enum __key_type type, unsigned int *len) +{ + const struct encrypted_key_payload *ekp; + const struct trusted_key_payload *tkp; + const struct user_key_payload *ukp; + + switch (type) { + case KEY_TYPE_USER: + ukp = user_key_payload_locked(key); + if (!ukp) + break; + + *len = ukp->datalen; + return ukp->data; + case KEY_TYPE_ENCRYPTED: + ekp = key->payload.data[0]; + if (!ekp) + break; + + *len = ekp->decrypted_datalen; + return ekp->decrypted_data; + case KEY_TYPE_TRUSTED: + tkp = key->payload.data[0]; + if (!tkp) + break; + + *len = tkp->key_len; + return tkp->key; + default: + return ERR_PTR(-EINVAL); + } + + return ERR_PTR(-EKEYREVOKED); +} +EXPORT_SYMBOL(__key_extract_material); + /** * register_key_type - Register a type of key. * @ktype: The new key type. -- git-series 0.9.1