From: "Al Cooper" <alcooperx@gmail.com>
To: cjb@laptop.org, linux-mmc@vger.kernel.org
Cc: Al Cooper <acooper@broadcom.com>
Subject: [PATCH 1/7] mmc: lock: Use the kernel "KEYS" subsystem to get a card's password
Date: Tue, 13 Aug 2013 11:21:24 -0400 [thread overview]
Message-ID: <1376407290-21477-2-git-send-email-alcooperx@gmail.com> (raw)
In-Reply-To: <1376407290-21477-1-git-send-email-alcooperx@gmail.com>
From: Al Cooper <acooper@broadcom.com>
Use the kernel "KEYS" subsystem to get a password for a card based on
the card's CID. This code was based on a patch set submitted by
Anderson Briglia in 2006.
refs #SWLINUX-2545
Signed-off-by: Al Cooper <acooper@broadcom.com>
---
drivers/mmc/core/Kconfig | 8 +++++
drivers/mmc/core/core.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
drivers/mmc/core/core.h | 16 ++++++++-
3 files changed, 109 insertions(+), 1 deletion(-)
diff --git a/drivers/mmc/core/Kconfig b/drivers/mmc/core/Kconfig
index 269d072..b0ba79d 100644
--- a/drivers/mmc/core/Kconfig
+++ b/drivers/mmc/core/Kconfig
@@ -26,3 +26,11 @@ config MMC_CLKGATE
support handling this in order for it to be of any use.
If unsure, say N.
+
+config MMC_LOCK
+ bool "MMC/SD password based card lock/unlock"
+ select KEYS
+ help
+ This will add the ability to lock/unlock SD and MMC cards.
+
+ If unsure, say N.
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 49a5bca..510927f 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -27,6 +27,7 @@
#include <linux/fault-inject.h>
#include <linux/random.h>
#include <linux/slab.h>
+#include <linux/key-type.h>
#include <linux/mmc/card.h>
#include <linux/mmc/host.h>
@@ -2705,6 +2706,78 @@ void mmc_init_context_info(struct mmc_host *host)
init_waitqueue_head(&host->context_info.wait);
}
+#ifdef CONFIG_MMC_LOCK
+
+int mmc_get_password(struct mmc_card *card, struct mmc_password *password)
+{
+ struct key *mmc_key;
+ char key_desc[(sizeof(card->raw_cid) * 2) + 1];
+
+ /* Use the CID to uniquely identify the card */
+ snprintf(key_desc, sizeof(key_desc), "%08x%08x%08x%08x",
+ card->raw_cid[0], card->raw_cid[1],
+ card->raw_cid[2], card->raw_cid[3]);
+
+ mmc_key = request_key(&mmc_key_type, key_desc,
+ "password");
+ if (IS_ERR(mmc_key)) {
+ dev_warn(&card->dev, "Error, request_key %ld\n",
+ PTR_ERR(mmc_key));
+ return PTR_ERR(mmc_key);
+ }
+ dev_dbg(&card->dev, "Found matching key\n");
+ memcpy(&password->password, mmc_key->payload.data,
+ mmc_key->datalen);
+ password->length = mmc_key->datalen;
+ key_put(mmc_key);
+
+ return 0;
+}
+
+
+static int mmc_key_instantiate(struct key *key,
+ struct key_preparsed_payload *prep)
+{
+ char *payload;
+
+ if (prep->datalen <= 0 || prep->datalen > MMC_PASSWORD_MAX ||
+ !prep->data) {
+ pr_warn("Invalid data\n");
+ return -EINVAL;
+ }
+
+ payload = kmalloc(prep->datalen, GFP_KERNEL);
+ if (!payload)
+ return -ENOMEM;
+ memcpy(payload, prep->data, prep->datalen);
+ key->payload.data = payload;
+ key->datalen = prep->datalen;
+ return 0;
+}
+
+static int mmc_key_match(const struct key *key, const void *description)
+{
+ pr_debug("mmc_key_match: %s, %s\n",
+ key->description, (char *)description);
+ return strcmp(key->description, description) == 0;
+}
+
+/*
+ * dispose of the data dangling from the corpse of a mmc key
+ */
+static void mmc_key_destroy(struct key *key)
+{
+ kfree(key->payload.data);
+}
+
+struct key_type mmc_key_type = {
+ .name = "mmc",
+ .instantiate = mmc_key_instantiate,
+ .match = mmc_key_match,
+ .destroy = mmc_key_destroy,
+};
+#endif /* CONFIG_MMC_LOCK */
+
static int __init mmc_init(void)
{
int ret;
@@ -2725,8 +2798,18 @@ static int __init mmc_init(void)
if (ret)
goto unregister_host_class;
+#ifdef CONFIG_MMC_LOCK
+ ret = register_key_type(&mmc_key_type);
+ if (ret)
+ goto unregister_sdio_bus;
+#endif /* CONFIG_MMC_LOCK */
+
return 0;
+#ifdef CONFIG_MMC_LOCK
+unregister_sdio_bus:
+ sdio_unregister_bus();
+#endif /* CONFIG_MMC_LOCK */
unregister_host_class:
mmc_unregister_host_class();
unregister_bus:
@@ -2739,6 +2822,9 @@ destroy_workqueue:
static void __exit mmc_exit(void)
{
+#ifdef CONFIG_MMC_LOCK
+ unregister_key_type(&mmc_key_type);
+#endif /* CONFIG_MMC_LOCK */
sdio_unregister_bus();
mmc_unregister_host_class();
mmc_unregister_bus();
diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
index 5345d15..dcf516d 100644
--- a/drivers/mmc/core/core.h
+++ b/drivers/mmc/core/core.h
@@ -81,5 +81,19 @@ void mmc_add_card_debugfs(struct mmc_card *card);
void mmc_remove_card_debugfs(struct mmc_card *card);
void mmc_init_context_info(struct mmc_host *host);
-#endif
+/* Lock/Unlock functionality */
+int mmc_unlock_card(struct mmc_card *card);
+
+#ifdef CONFIG_MMC_LOCK
+#define MMC_PASSWORD_MAX 16
+struct mmc_password {
+ char password[MMC_PASSWORD_MAX];
+ int length;
+};
+extern struct key_type mmc_key_type;
+
+int mmc_get_password(struct mmc_card *card, struct mmc_password *password);
+#endif /* CONFIG_MMC_LOCK */
+
+#endif
--
1.8.1.3
next prev parent reply other threads:[~2013-08-13 15:22 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-13 15:21 [PATCH 0/7] Add password protected lock/unlock support for SD/MMC Al Cooper
2013-08-13 15:21 ` Al Cooper [this message]
2013-08-15 1:16 ` [PATCH 1/7] mmc: lock: Use the kernel "KEYS" subsystem to get a card's password Brian Norris
2013-08-23 10:34 ` Ulf Hansson
2013-08-13 15:21 ` [PATCH 2/7] mmc: lock: Add low level LOCK_UNLOCK command Al Cooper
2013-08-13 15:21 ` [PATCH 3/7] mmc: lock: Add funtion to unlock a password locked card Al Cooper
2013-08-13 15:21 ` [PATCH 4/7] mmc: lock: Add card lock/unlock maintenance commands Al Cooper
2013-08-13 15:21 ` [PATCH 5/7] mmc: lock: Change SD init functionality to handle locked SD cards Al Cooper
2013-08-13 15:21 ` [PATCH 6/7] mmc: lock: Prevent partition table read for locked cards Al Cooper
2013-08-13 15:21 ` [PATCH 7/7] mmc: lock: Change MMC init to handle " Al Cooper
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=1376407290-21477-2-git-send-email-alcooperx@gmail.com \
--to=alcooperx@gmail.com \
--cc=acooper@broadcom.com \
--cc=cjb@laptop.org \
--cc=linux-mmc@vger.kernel.org \
/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).