[PATCH for-next 1/2] nvme: add the permission-policy for command control
Kanchan Joshi
joshi.k at samsung.com
Mon Sep 26 07:54:29 PDT 2022
If CAP_SYS_ADMIN is present, nothing else is checked as before.
Otherwise takes the decision to allow the command based on following
rules:
- any admin-cmd is not allowed
- vendor-specific and fabric commmand are not allowed
- io-commands that can write are allowed if matching FMODE_WRITE
permission is present
- io-commands that read are allowed
Add a helper nvme_cmd_allowed that implements above policy. This is a prep
patch.
Signed-off-by: Kanchan Joshi <joshi.k at samsung.com>
---
drivers/nvme/host/ioctl.c | 19 +++++++++++++++++++
include/linux/nvme.h | 1 +
2 files changed, 20 insertions(+)
diff --git a/drivers/nvme/host/ioctl.c b/drivers/nvme/host/ioctl.c
index 548aca8b5b9f..6ca6477dd899 100644
--- a/drivers/nvme/host/ioctl.c
+++ b/drivers/nvme/host/ioctl.c
@@ -20,6 +20,25 @@ static void __user *nvme_to_user_ptr(uintptr_t ptrval)
return (void __user *)ptrval;
}
+bool nvme_cmd_allowed(struct nvme_ns *ns, u8 opcode, fmode_t mode)
+{
+ /* root can do anything */
+ if (capable(CAP_SYS_ADMIN))
+ return true;
+ /* admin commands are not allowed */
+ if (ns == NULL)
+ return false;
+ /* exclude vendor-specific io and fabrics commands */
+ if (opcode >= nvme_cmd_vendor_start ||
+ opcode== nvme_fabrics_command)
+ return false;
+ /* allow write cmds only if matching FMODE is present */
+ if (opcode & 1)
+ return mode & FMODE_WRITE;
+ /* allow read cmds */
+ return true;
+}
+
static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
unsigned len, u32 seed, bool write)
{
diff --git a/include/linux/nvme.h b/include/linux/nvme.h
index ae53d74f3696..8396eb7ecb68 100644
--- a/include/linux/nvme.h
+++ b/include/linux/nvme.h
@@ -797,6 +797,7 @@ enum nvme_opcode {
nvme_cmd_zone_mgmt_send = 0x79,
nvme_cmd_zone_mgmt_recv = 0x7a,
nvme_cmd_zone_append = 0x7d,
+ nvme_cmd_vendor_start = 0x80,
};
#define nvme_opcode_name(opcode) { opcode, #opcode }
--
2.25.1
More information about the Linux-nvme
mailing list