[RFC 1/2] nvme: add whitelisting infrastructure

Kanchan Joshi joshi.k at samsung.com
Fri Sep 9 09:33:06 PDT 2022


If CAP_SYS_ADMIN is present, nothing else is checked, as before.
If CAP_SYS_ADMIN is not present, take the decision based on
- type of nvme command (io or admin)
- nature of nvme-command (write or read)
- mode with which file was opened (read-only, read-write etc.)

io-commands that write/read are allowed only if matching file mode is
present.
for admin-commands, few read-only admin command are allowed and that too
when mode matches.

Signed-off-by: Kanchan Joshi <joshi.k at samsung.com>
---
 drivers/nvme/host/ioctl.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/drivers/nvme/host/ioctl.c b/drivers/nvme/host/ioctl.c
index 548aca8b5b9f..0d99135a1745 100644
--- a/drivers/nvme/host/ioctl.c
+++ b/drivers/nvme/host/ioctl.c
@@ -20,6 +20,42 @@ static void __user *nvme_to_user_ptr(uintptr_t ptrval)
 	return (void __user *)ptrval;
 }
 
+bool nvme_io_cmd_allowed(u8 opcode, fmode_t mode)
+{
+	/* allow write/read based on what was allowed for open */
+	/* TBD: try to use nvme_is_write() here */
+	if (opcode & 1)
+		return (mode & FMODE_WRITE);
+	else
+		return (mode & FMODE_READ);
+}
+
+bool nvme_admin_cmd_allowed(u8 opcode, fmode_t mode)
+{
+	/* allowed few read-only commands post the mode check */
+	switch (opcode) {
+	case nvme_admin_identify:
+	case nvme_admin_get_log_page:
+	case nvme_admin_get_features:
+		return (mode & FMODE_READ);
+	default:
+		return false;
+	}
+}
+
+bool nvme_cmd_allowed(struct nvme_ns *ns, u8 opcode, fmode_t mode)
+{
+	bool ret;
+	/* root can do anything */
+	if (capable(CAP_SYS_ADMIN))
+		return true;
+	if (ns == NULL)
+		ret = nvme_admin_cmd_allowed(opcode, mode);
+	else
+		ret = nvme_io_cmd_allowed(opcode, mode);
+	return ret;
+}
+
 static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
 		unsigned len, u32 seed, bool write)
 {
-- 
2.25.1




More information about the Linux-nvme mailing list