Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp574568imb; Fri, 1 Mar 2019 08:13:12 -0800 (PST) X-Google-Smtp-Source: APXvYqz7o8S4c8fj1MxtUs+UVsP8ednPI/7ZxfaUGyzZ5EVSqEAqiPSLwq+nAcg69i9B0MKS4k5/ X-Received: by 2002:a63:6a48:: with SMTP id f69mr5464433pgc.7.1551456792652; Fri, 01 Mar 2019 08:13:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551456792; cv=none; d=google.com; s=arc-20160816; b=G4gylWr09IiA/bt4vnC7zxOHtSbL5eYFXud1Yrx+37zpjNfIy8uyOzG5u5iw+60hIW YlD87ramiiXuH9w/Nmn6KXNV56dza31Q77QWd2GL1bld3Q2hWQuSdxQ+R+4QRyT8S60J eIkObgSD3b17cWQUc/FCZ16SooCdQrYHdsxpkff5qWUut2NVcnGhslIRmoFV1t4CLLvU aOIJv0LIJTRInbf2WrlVIcf9fCBGB151vKzZqi38PBfgqDSFgPs2Z8C3knKauPTqN05U yHo2TXHanR7/fNJv0UQEgcXmQV/6EGRCYpDJPBJYPi+UINBKWhE5YTIId0en7ybf9poi /VKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=tayicmVSFuQ4WCDWDSzZ4vl1KBZoT3UuFqil3+3QHkc=; b=W+N+DxPThyfyC2UK9jCUWLHnmx/yuB+cNuwGsfhhKOLyX28Nv/jdck9XhqOyUp1AY6 V/yBhOGhNC/QiRejTNumH8LM5WBBhPpE7LhX0lNMMvyKsC7pF3V4Fu3bbTCjHthxLT9R c8RM7P5nRqD/jHBZfK3hkKZg4HF4NrImrmAVI1w3w7LDSbXfWoF6Eu4kp3AiPVPPk5Gf e0cOEEoV3VrY3EYN0+R0CD2/zRSqJcaMp8oZk4IAe6dArSh4ssy1FwgnTwu6R4ny+UhE FbCyvWelBj5JWNmEXdJLF7PZIGqSUn85V3kUzKTnJjVHWOfv86OFzLaHWSQvI7rV/WCh sdbA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n14si7487125pgv.520.2019.03.01.08.12.57; Fri, 01 Mar 2019 08:13:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389088AbfCAQKk (ORCPT + 99 others); Fri, 1 Mar 2019 11:10:40 -0500 Received: from inva020.nxp.com ([92.121.34.13]:37564 "EHLO inva020.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727952AbfCAQK3 (ORCPT ); Fri, 1 Mar 2019 11:10:29 -0500 Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 690F91A0077; Fri, 1 Mar 2019 17:10:26 +0100 (CET) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 5B4B61A0085; Fri, 1 Mar 2019 17:10:26 +0100 (CET) Received: from sopsvulinux.ea.freescale.net (sopsvulinux.ea.freescale.net [10.161.69.178]) by inva024.eu-rdc02.nxp.com (Postfix) with ESMTP id 35E60205ED; Fri, 1 Mar 2019 17:10:26 +0100 (CET) Received: from SOPDPUATS05.ea.freescale.net (SOPDPUATS05.ea.freescale.net [10.161.66.238]) by sopsvulinux.ea.freescale.net (Postfix) with ESMTP id 5B1D582C7D; Fri, 1 Mar 2019 17:08:45 +0100 (CET) From: Franck LENORMAND To: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, keyrings@vger.kernel.org Cc: franck.lenormand@nxp.com, horia.geanta@nxp.com, silvano.dininno@nxp.com, agk@redhat.com, snitzer@redhat.com, dm-devel@redhat.com, dhowells@redhat.com, jmorris@namei.org, serge@hallyn.com Subject: [RFC PATCH 2/2] dm-crypt: Use any key type which is registered Date: Fri, 1 Mar 2019 17:09:59 +0100 Message-Id: <1551456599-10603-3-git-send-email-franck.lenormand@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1551456599-10603-1-git-send-email-franck.lenormand@nxp.com> References: <1551456599-10603-1-git-send-email-franck.lenormand@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There was only 2 key_type supported by dm-crypt which limits other implementations. This patch allows to use any key_type which is registered obtaining the key_type from key framework. This also remove the compilation dependency between dm-crypt and key implementations. Signed-off-by: Franck LENORMAND --- drivers/md/dm-crypt.c | 11 ++++++----- include/linux/key-type.h | 2 ++ security/keys/key.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index dd538e6..e25efc2 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -35,6 +35,7 @@ #include #include /* for struct rtattr and RTA macros only */ #include +#include #include @@ -2010,6 +2011,7 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string int ret; struct key *key; const struct user_key_payload *ukp; + struct key_type *type; /* * Reject key_string with whitespace. dm core currently lacks code for @@ -2025,16 +2027,15 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string if (!key_desc || key_desc == key_string || !strlen(key_desc + 1)) return -EINVAL; - if (strncmp(key_string, "logon:", key_desc - key_string + 1) && - strncmp(key_string, "user:", key_desc - key_string + 1)) - return -EINVAL; + type = get_key_type(key_string, key_desc - key_string); + if (!type) + return -ENOENT; new_key_string = kstrdup(key_string, GFP_KERNEL); if (!new_key_string) return -ENOMEM; - key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user, - key_desc + 1, NULL); + key = request_key(type, key_desc + 1, NULL); if (IS_ERR(key)) { kzfree(new_key_string); return PTR_ERR(key); diff --git a/include/linux/key-type.h b/include/linux/key-type.h index bc9af55..2b2167b 100644 --- a/include/linux/key-type.h +++ b/include/linux/key-type.h @@ -176,6 +176,8 @@ extern struct key_type key_type_keyring; extern int register_key_type(struct key_type *ktype); extern void unregister_key_type(struct key_type *ktype); +extern struct key_type *get_key_type(const char *type_name, size_t string_size); + extern int key_payload_reserve(struct key *key, size_t datalen); extern int key_instantiate_and_link(struct key *key, const void *data, diff --git a/security/keys/key.c b/security/keys/key.c index 44a80d6..ef76114 100644 --- a/security/keys/key.c +++ b/security/keys/key.c @@ -1156,6 +1156,48 @@ void unregister_key_type(struct key_type *ktype) } EXPORT_SYMBOL(unregister_key_type); +/** + * get_key_type - Get the type of key using its name + * @type_name: Name of the key type to get + * @string_size: Size of the string to match + * + * The functions support null ended string (string_size == 0) as well as + * pointer on a string matching a number of characters (string_size > 0) + * + * Returns a pointer on the key type if successful, -ENOENT if the key type + * is not registered. + */ +struct key_type *get_key_type(const char *type_name, size_t string_size) +{ + struct key_type *p; + struct key_type *ktype = ERR_PTR(-ENOENT); + + if (!type_name) + return ktype; + + down_write(&key_types_sem); + + /* Search the key type in the list */ + list_for_each_entry(p, &key_types_list, link) { + if (string_size) { + if (strncmp(p->name, type_name, string_size) == 0) { + ktype = p; + break; + } + } else { + if (strcmp(p->name, type_name) == 0) { + ktype = p; + break; + } + } + } + + up_read(&key_types_sem); + + return ktype; +} +EXPORT_SYMBOL(get_key_type); + /* * Initialise the key management state. */ -- 2.7.4