[PATCH v10 06/12] nfit/libnvdimm: add set passphrase support for Intel nvdimms

2018-09-26 Thread Dave Jiang
Add support for setting and/or updating passphrase on the Intel nvdimms.
The passphrase is pulled from userspace through the kernel key management.
We trigger the update via writing "update  " to the
sysfs attribute "security". If no  exists (for enabling security)
then a 0 should be used. The state of the security can also be read via the
"security" attribute. libnvdimm will generically support the key_change
API call.

Signed-off-by: Dave Jiang 
---
 drivers/acpi/nfit/intel.c  |   68 
 drivers/nvdimm/dimm_devs.c |  248 
 include/linux/libnvdimm.h  |5 +
 3 files changed, 321 insertions(+)

diff --git a/drivers/acpi/nfit/intel.c b/drivers/acpi/nfit/intel.c
index 4bfc1c1da339..314eae7e02d7 100644
--- a/drivers/acpi/nfit/intel.c
+++ b/drivers/acpi/nfit/intel.c
@@ -18,6 +18,73 @@
 #include "intel.h"
 #include "nfit.h"
 
+/*
+ * The update passphrase takes the old passphrase and the new passphrase
+ * and send those to the nvdimm. The nvdimm will verify the old
+ * passphrase and then update it with the new passphrase if pending
+ * verification. The function will pass in a zeroed passphrase field
+ * if the old passphrase is NULL. This typically happens when we are
+ * enabling security from the disabled state.
+ */
+static int intel_dimm_security_update_passphrase(
+   struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
+   const struct nvdimm_key_data *old_data,
+   const struct nvdimm_key_data *new_data)
+{
+   struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
+   int cmd_rc, rc = 0;
+   struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
+   struct {
+   struct nd_cmd_pkg pkg;
+   struct nd_intel_set_passphrase cmd;
+   } nd_cmd = {
+   .pkg = {
+   .nd_command = NVDIMM_INTEL_SET_PASSPHRASE,
+   .nd_family = NVDIMM_FAMILY_INTEL,
+   .nd_size_in = ND_INTEL_PASSPHRASE_SIZE * 2,
+   .nd_size_out = ND_INTEL_STATUS_SIZE,
+   .nd_fw_size = ND_INTEL_STATUS_SIZE,
+   },
+   .cmd = {
+   .status = 0,
+   },
+   };
+
+   if (!test_bit(NVDIMM_INTEL_SET_PASSPHRASE, &nfit_mem->dsm_mask))
+   return -ENOTTY;
+
+   if (old_data)
+   memcpy(nd_cmd.cmd.old_pass, old_data->data,
+   sizeof(nd_cmd.cmd.old_pass));
+   else
+   memset(nd_cmd.cmd.old_pass, 0, sizeof(nd_cmd.cmd.old_pass));
+   memcpy(nd_cmd.cmd.new_pass, new_data->data,
+   sizeof(nd_cmd.cmd.new_pass));
+   rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_CALL, &nd_cmd,
+   sizeof(nd_cmd), &cmd_rc);
+   if (rc < 0)
+   goto out;
+   if (cmd_rc < 0) {
+   rc = cmd_rc;
+   goto out;
+   }
+
+   switch (nd_cmd.cmd.status) {
+   case 0:
+   break;
+   case ND_INTEL_STATUS_INVALID_PASS:
+   rc = -EINVAL;
+   goto out;
+   case ND_INTEL_STATUS_INVALID_STATE:
+   default:
+   rc = -ENXIO;
+   goto out;
+   }
+
+ out:
+   return rc;
+}
+
 static int intel_dimm_security_unlock(struct nvdimm_bus *nvdimm_bus,
struct nvdimm *nvdimm, const struct nvdimm_key_data *nkey)
 {
@@ -149,4 +216,5 @@ static int intel_dimm_security_state(struct nvdimm_bus 
*nvdimm_bus,
 const struct nvdimm_security_ops intel_security_ops = {
.state = intel_dimm_security_state,
.unlock = intel_dimm_security_unlock,
+   .change_key = intel_dimm_security_update_passphrase,
 };
diff --git a/drivers/nvdimm/dimm_devs.c b/drivers/nvdimm/dimm_devs.c
index e7f4c8a2b242..becbe670b572 100644
--- a/drivers/nvdimm/dimm_devs.c
+++ b/drivers/nvdimm/dimm_devs.c
@@ -20,6 +20,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include "nd-core.h"
 #include "label.h"
@@ -49,6 +50,58 @@ static struct key *nvdimm_get_key(struct device *dev)
return nvdimm->key;
 }
 
+/*
+ * Replacing the user key with a kernel key. The function expects that
+ * we hold the sem for the key passed in. The function will release that
+ * sem when done process. We will also hold the sem for the valid new key
+ * returned.
+ */
+static struct key *nvdimm_replace_key(struct key *key)
+{
+   struct key *new_key;
+   struct user_key_payload *payload;
+   int rc;
+
+   new_key = key_alloc(&key_type_logon, key->description,
+   GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
+   KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
+   if (IS_ERR(new_key))
+   return NULL;
+
+   payload = key->payload.data[0];
+   rc = key_instantiate_and_link(new_key, payload->data,
+   payload->datalen, NULL, NULL);
+   up_read(&key->se

Re: [PATCH v10 06/12] nfit/libnvdimm: add set passphrase support for Intel nvdimms

2018-09-28 Thread David Howells
Dave Jiang  wrote:

> + down_read(&key->sem);
> + payload = key->payload.data[0];
> + down_read(&user_key->sem);
> + upayload = user_key->payload.data[0];

Personally, I would do both downs first and then deref both payloads.  The
compiler probably will be blocked from rearranging things to move the first
deref after the second down.  Ideally it or the cpu should be able to move
things into the critical section, but the cpu barriers available may well
preclude that.  That means that the compiler has to use a resource (stack/reg)
to stash the value across the second down.

> +  * We don't need to release key->sem here because nvdimm_repalce_key

nvdimm_replace_key I presume.

> + sscanf(buf, "%s %u %u", cmd, &old_key, &new_key);

You should check that sscanf() returned 3.

Also, since there's no size limitation here on the cmd string, if someone, for
example, writes a string of SEC_CMD_SIZE lots of 'a', sscanf() will write all
of them into cmd[] and follow that with a NUL char, thereby overrunning your
cmd[] buffer.  You need to either make the buffer one bigger or put a size
limit in sscanf string, e.g.:

if (sscanf(buf, "%" __stringify(SEC_CMD_SIZE) "s %u %u", ...) == 3)

-~-
Other than that, I think this is mostly right.

David
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


Re: [PATCH v10 06/12] nfit/libnvdimm: add set passphrase support for Intel nvdimms

2018-09-28 Thread David Howells
David Howells  wrote:

>   if (sscanf(buf, "%" __stringify(SEC_CMD_SIZE) "s %u %u", ...) == 3)

Actually, that would need to be "SEC_CMD_SIZE - 1" - which might get
stringified as-is.

David
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm