From patchwork Wed Nov 16 22:38:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mat Martineau X-Patchwork-Id: 9433073 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 82D236021C for ; Wed, 16 Nov 2016 22:38:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7407728FBD for ; Wed, 16 Nov 2016 22:38:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 68B4C2909A; Wed, 16 Nov 2016 22:38:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1383528FFE for ; Wed, 16 Nov 2016 22:38:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1424397AbcKPWiQ (ORCPT ); Wed, 16 Nov 2016 17:38:16 -0500 Received: from mga14.intel.com ([192.55.52.115]:48432 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1423882AbcKPWiN (ORCPT ); Wed, 16 Nov 2016 17:38:13 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP; 16 Nov 2016 14:38:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,650,1473145200"; d="scan'208";a="5260901" Received: from mjmartin-nuc01.wa.intel.com ([10.232.97.135]) by orsmga002.jf.intel.com with ESMTP; 16 Nov 2016 14:38:10 -0800 From: Mat Martineau To: keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, dhowells@redhat.com Cc: Mat Martineau , zohar@linux.vnet.ibm.com Subject: [PATCH v9 07/11] KEYS: Parse keyring payload for restrict options Date: Wed, 16 Nov 2016 14:38:02 -0800 Message-Id: <20161116223806.14601-8-mathew.j.martineau@linux.intel.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161116223806.14601-1-mathew.j.martineau@linux.intel.com> References: <20161116223806.14601-1-mathew.j.martineau@linux.intel.com> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Keyrings recently gained restrict_link capabilities that allow individual keys to be validated prior to linking. This functionality was only available using internal kernel APIs. Configuring keyring restrictions from userspace must work around these constraints: - The restrict_link pointer must be set through keyring_alloc(). - The add_key system call has a fixed set of parameters. When creating keyrings, the payload was previously required to be NULL. To support restrict_link configuration, the payload is now parsed for keyring options if a payload is provided. The expected option payload format is: "restrict=[:]" The format of the restriction information is specified by each key type. Signed-off-by: Mat Martineau --- Documentation/security/keys.txt | 13 +++++- security/keys/keyring.c | 88 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/Documentation/security/keys.txt b/Documentation/security/keys.txt index cade1de..b0647a0 100644 --- a/Documentation/security/keys.txt +++ b/Documentation/security/keys.txt @@ -418,8 +418,17 @@ The main syscalls are: the type. The payload is plen in size, and plen can be zero for an empty payload. - A new keyring can be generated by setting type "keyring", the keyring name - as the description (or NULL) and setting the payload to NULL. + A new keyring can be generated by setting type "keyring" and the keyring + name as the description (or NULL). The payload can be NULL or contain + keyring link restriction parameters using the following format: + + "restrict=:" + + "type" is a registered key type. "options" vary depending on the key type, + and are passed to the lookup_restrict function for the requested type. + The options may specify a specific method and relevant data for the + restriction (such as signature verification or constraints on key + payload). User defined keys can be created by specifying type "user". It is recommended that a user defined key's description by prefixed with a type diff --git a/security/keys/keyring.c b/security/keys/keyring.c index 07f680b..0aefaff 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -23,6 +23,13 @@ #include "internal.h" /* + * Layout of preparse payload + */ +enum { + keyring_restrict_link, +}; + +/* * When plumbing the depths of the key tree, this sets a hard limit * set on how deep we're willing to go. */ @@ -126,28 +133,103 @@ static void keyring_publish_name(struct key *keyring) } /* + * Parse the keyring options and fill in the required keyring members. + * + * Expected format is: "restrict=[:]" + * + * Returns 0 if the option string is valid (including the empty case), + * otherwise -EINVAL. + */ +static int keyring_datablob_parse(char *datablob, + struct key_preparsed_payload *prep) +{ + char *type_name; + struct key_type *restrict_type = NULL; + struct key_restriction *restrict_link; + int ret = -EINVAL; + const char restrict_prefix[] = "restrict="; + + if (*datablob == '\0') + return 0; + + if (!strstarts(datablob, restrict_prefix)) + return -EINVAL; + + datablob += strlen(restrict_prefix); + + type_name = strsep(&datablob, ":"); + + restrict_type = key_type_lookup(type_name); + if (IS_ERR(restrict_type)) + return -EINVAL; + + if (!restrict_type->lookup_restrict) + goto error; + + restrict_link = restrict_type->lookup_restrict(datablob); + if (IS_ERR(restrict_link)) + goto error; + + prep->payload.data[keyring_restrict_link] = restrict_link; + ret = 0; + +error: + key_type_put(restrict_type); + + return ret; +} + +/* * Preparse a keyring payload */ static int keyring_preparse(struct key_preparsed_payload *prep) { - return prep->datalen != 0 ? -EINVAL : 0; + char *datablob; + size_t datalen = prep->datalen; + int ret = 0; + + if (datalen) { + datablob = kmalloc(datalen + 1, GFP_KERNEL); + if (!datablob) + return -ENOMEM; + + memcpy(datablob, prep->data, datalen); + datablob[datalen] = '\0'; + + ret = keyring_datablob_parse(datablob, prep); + + kfree(datablob); + } + + return ret; } /* - * Free a preparse of a user defined key payload + * Free a preparse of a keyring payload */ static void keyring_free_preparse(struct key_preparsed_payload *prep) { + struct key_restriction *keyres; + + keyres = prep->payload.data[keyring_restrict_link]; + + if (keyres && keyres->free_data) { + keyres->free_data(keyres->data); + kfree(keyres); + } } /* * Initialise a keyring. * - * Returns 0 on success, -EINVAL if given any data. + * Returns 0 on success. */ static int keyring_instantiate(struct key *keyring, struct key_preparsed_payload *prep) { + keyring->restrict_link = prep->payload.data[keyring_restrict_link]; + prep->payload.data[keyring_restrict_link] = NULL; + assoc_array_init(&keyring->keys); /* make the keyring available by name if it has one */ keyring_publish_name(keyring);