linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
To: zohar@linux.vnet.ibm.com, jmorris@namei.org,
	rusty@rustcorp.com.au, dhowells@redhat.com,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Mimi Zohar <zohar@us.ibm.com>
Subject: [RFC v2 3/7] integrity: create and inititialize a keyring with builtin public key
Date: Wed, 15 Aug 2012 21:43:08 +0300	[thread overview]
Message-ID: <66e0f332c823b82701a48aba320f589cec27e904.1345055639.git.dmitry.kasatkin@intel.com> (raw)
In-Reply-To: <cover.1345055638.git.dmitry.kasatkin@intel.com>
In-Reply-To: <cover.1345055638.git.dmitry.kasatkin@intel.com>

From: Mimi Zohar <zohar@linux.vnet.ibm.com>

Create and initialize a keyring with the builtin public key. This could
be an ephemeral key, created and destroyed during module install for
custom built kernels, or a key used to label the entire filesystem for
EVM/IMA-appraisal. Uses .incbin based on David Howell's post.

Load the builtin public key on the specified keyring, creating the
keyring if it doesn't already exist.

Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
---
 security/integrity/Kconfig         |   10 ++++
 security/integrity/Makefile        |   17 +++++++
 security/integrity/digsig_pubkey.c |   96 ++++++++++++++++++++++++++++++++++++
 security/integrity/integrity.h     |   10 ++++
 4 files changed, 133 insertions(+)
 create mode 100644 security/integrity/digsig_pubkey.c

diff --git a/security/integrity/Kconfig b/security/integrity/Kconfig
index 5bd1cc1..f789018 100644
--- a/security/integrity/Kconfig
+++ b/security/integrity/Kconfig
@@ -17,5 +17,15 @@ config INTEGRITY_SIGNATURE
 	  This is useful for evm and module keyrings, when keys are
 	  usually only added from initramfs.
 
+config INTEGRITY_PUBKEY
+	boolean "Create a keyring and initialize with builtin public key"
+	depends on INTEGRITY_SIGNATURE
+	default n
+	help
+	  Create and initialize a keyring with the builtin public key. This could
+	  be an ephemeral key, created and destroyed during module install for
+	  custom built kernels, or a key used to label the entire filesystem for
+	  EVM/IMA-appraisal.
+
 source security/integrity/ima/Kconfig
 source security/integrity/evm/Kconfig
diff --git a/security/integrity/Makefile b/security/integrity/Makefile
index d43799c..cf234f5 100644
--- a/security/integrity/Makefile
+++ b/security/integrity/Makefile
@@ -11,3 +11,20 @@ subdir-$(CONFIG_IMA)			+= ima
 obj-$(CONFIG_IMA)			+= ima/built-in.o
 subdir-$(CONFIG_EVM)			+= evm
 obj-$(CONFIG_EVM)			+= evm/built-in.o
+
+ifeq ($(CONFIG_INTEGRITY_PUBKEY),y)
+
+obj-y += digsig_pubkey.o
+
+pubkeybin = pubkey.bin
+
+$(obj)/digsig_pubkey.o: $(pubkeybin)
+
+$(pubkeybin): FORCE
+	@if [ ! -s $(pubkeybin) ]; then \
+		echo "$(pubkeybin) is missing or empty..."; \
+		if [ ! -f $(pubkeybin) ]; then touch $(pubkeybin); fi; \
+	fi
+
+endif
+
diff --git a/security/integrity/digsig_pubkey.c b/security/integrity/digsig_pubkey.c
new file mode 100644
index 0000000..c714a60
--- /dev/null
+++ b/security/integrity/digsig_pubkey.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2012 IBM Corporation
+ * Copyright (C) 2012 Intel Corporation
+ *
+ * Author:
+ * Mimi Zohar <zohar@us.ibm.com>
+ * Dmitry Kasatkin <dmitry.kasatkin@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2 of the License.
+ *
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/sched.h>
+#include <linux/cred.h>
+#include <linux/err.h>
+#include <linux/key-type.h>
+#include <keys/user-type.h>
+
+#include "integrity.h"
+
+static const char *keyring_name[INTEGRITY_KEYRING_MAX] = {
+	"_evm",
+	"_module",
+	"_ima",
+};
+
+asm(".section .init.data,\"aw\"\n"
+	"pubkey:\n"
+	".incbin \"pubkey.bin\"\n"
+	"pubkey_end:"
+);
+
+extern __initdata const u8 pubkey[];
+extern __initdata const u8 pubkey_end[];
+
+/*
+ * integrity_init_keyring - create and initialize the specified keryring
+ *
+ * Create and initialize a keyring with the builtin public key. This could
+ * be an ephemeral key, created and destroyed during module install for
+ * custom built kernels, or a key used to label the entire filesystem for
+ * EVM/IMA-appraisal.
+ *
+ * Load the builtin public key on the specified keyring, creating the
+ * keyring if it doesn't already exist.
+ *
+ * Return 0 on success.
+ */
+int integrity_init_keyring(const unsigned int id)
+{
+	const struct cred *cred = current_cred();
+	struct user_struct *user = cred->user;
+	struct key *new_keyring, *key;
+	u8 digest[SHA1_DIGEST_SIZE];
+	char keyid[20];
+	int ret, pubkey_size = pubkey_end - pubkey;
+
+	if (pubkey_size == 0) {
+		pr_info("pubkey is missing, skipping...\n");
+		return 0;
+	}
+
+	new_keyring = keyring_alloc(keyring_name[id], user->uid, (gid_t) -1,
+				    cred, KEY_ALLOC_NOT_IN_QUOTA,
+				    user->uid_keyring);
+	if (IS_ERR(new_keyring)) {
+		ret = PTR_ERR(new_keyring);
+		goto out;
+	}
+
+	ret = integrity_calc_digest("sha1", pubkey, pubkey_size, digest);
+	if (ret < 0)
+		goto out;
+
+	sprintf(keyid, "%llX", __be64_to_cpup((uint64_t *)(digest+12)));
+
+	key = key_alloc(&key_type_user, keyid, 0, 0, current_cred(),
+			(KEY_POS_ALL & ~KEY_POS_SETATTR) |
+			KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_READ,
+			KEY_ALLOC_NOT_IN_QUOTA);
+	if (IS_ERR(key)) {
+		ret = PTR_ERR(key);
+		goto out;
+	}
+
+	ret = key_instantiate_and_link(key, pubkey, pubkey_end - pubkey,
+				       new_keyring, NULL);
+out:
+	pr_info("integrity: loaded public key %s on %s %s\n", keyid,
+		keyring_name[id], !ret ? "succeeded" : "failed");
+	return ret;
+}
diff --git a/security/integrity/integrity.h b/security/integrity/integrity.h
index 48ee2d4..1070db8 100644
--- a/security/integrity/integrity.h
+++ b/security/integrity/integrity.h
@@ -13,6 +13,7 @@
 
 #include <linux/types.h>
 #include <linux/integrity.h>
+#include <linux/rbtree.h>
 #include <crypto/sha.h>
 
 /* iint cache flags */
@@ -76,5 +77,14 @@ static inline int integrity_digsig_verify(const unsigned int id,
 
 #endif /* CONFIG_INTEGRITY_SIGNATURE */
 
+#ifdef CONFIG_INTEGRITY_PUBKEY
+int integrity_init_keyring(const unsigned int id);
+#else
+static inline int integrity_init_keyring(const unsigned int id)
+{
+	return 0;
+}
+#endif /* CONFIG_INTEGRITY_PUBKEY */
+
 /* set during initialization */
 extern int iint_initialized;
-- 
1.7.9.5


  parent reply	other threads:[~2012-08-15 18:44 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-15 18:43 [RFC v2 0/7] modsig: signature based kernel module integrity verfication Dmitry Kasatkin
2012-08-15 18:43 ` [RFC v2 1/7] integrity: added digest calculation function Dmitry Kasatkin
2012-08-15 20:11   ` Serge Hallyn
2012-08-15 21:11     ` Kasatkin, Dmitry
2012-08-16 20:32       ` Kasatkin, Dmitry
2012-08-16 21:39         ` Serge Hallyn
2012-08-20  2:59   ` Rusty Russell
2012-08-22 16:38     ` Kasatkin, Dmitry
2012-08-15 18:43 ` [RFC v2 2/7] keys: initialize root uid and session keyrings early Dmitry Kasatkin
2012-08-16 18:26   ` Josh Boyer
2012-08-16 19:08     ` Mimi Zohar
2012-08-16 19:13       ` Josh Boyer
2012-08-16 19:45         ` Mimi Zohar
2012-08-16 19:59           ` Josh Boyer
2012-08-16 20:01             ` Mimi Zohar
2012-08-17 21:27               ` Eric W. Biederman
2012-08-15 18:43 ` Dmitry Kasatkin [this message]
2012-08-16 18:37   ` [RFC v2 3/7] integrity: create and inititialize a keyring with builtin public key Josh Boyer
2012-08-16 19:28     ` Mimi Zohar
2012-08-17  6:06       ` Kasatkin, Dmitry
2012-08-16 21:11     ` Kasatkin, Dmitry
2012-08-15 18:43 ` [RFC v2 4/7] modsig: add integrity_module_check hook Dmitry Kasatkin
2012-08-15 20:16   ` Serge Hallyn
2012-08-15 21:13     ` Kasatkin, Dmitry
2012-08-17  5:45       ` Kasatkin, Dmitry
2012-08-16 18:49   ` Josh Boyer
2012-08-16 19:56     ` Kasatkin, Dmitry
2012-09-03 23:06   ` Rusty Russell
2012-08-15 18:43 ` [RFC v2 5/7] modsig: verify module integrity based on signature Dmitry Kasatkin
2012-08-15 18:43 ` [RFC v2 6/7] modsig: initialize the _module public key keyring Dmitry Kasatkin
2012-08-16 18:54   ` Josh Boyer
2012-08-16 19:57     ` Mimi Zohar
2012-08-15 18:43 ` [RFC v2 7/7] modsig: build rules and scripts to generate keys and sign modules Dmitry Kasatkin
2012-08-16 19:10   ` Josh Boyer
2012-08-16 20:12     ` Kasatkin, Dmitry
2012-08-16 20:31       ` Josh Boyer
2012-08-16 21:04         ` Kasatkin, Dmitry
2012-08-17  0:53           ` Mimi Zohar
2012-08-17 11:40             ` Josh Boyer
2012-08-17 17:08               ` Mimi Zohar
2012-08-17 17:44                 ` Josh Boyer
2012-08-17 17:52                   ` Josh Boyer
2012-08-20  1:05                   ` Mimi Zohar
2012-08-20 12:32                     ` Josh Boyer
2012-08-20 13:13                       ` Mimi Zohar
2012-08-20 14:23                         ` Josh Boyer
2012-08-16 20:12     ` Mimi Zohar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=66e0f332c823b82701a48aba320f589cec27e904.1345055639.git.dmitry.kasatkin@intel.com \
    --to=dmitry.kasatkin@intel.com \
    --cc=dhowells@redhat.com \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    --cc=zohar@linux.vnet.ibm.com \
    --cc=zohar@us.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).