Skip to contentSkip to conversation

nvme: don't allow unprivileged Write Zeroes passthrough on read-only FDs

PATCH In review Christoph Hellwig 5 participants 6 messages Nov 29, 2022 – Dec 1, 2022 NVMe

lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

PATCH 1/1 nvme: don't allow unprivileged Write Zeroes passthrough on read-only FDs

Unfortunately Write Zeroes is coded as a no data transfer opcode in NVMe, so don't allow it on a read-only FD for unprivileged users.

Fixes 855b7717f44b ("nvme: fine-granular CAP_SYS_ADMIN for nvme io commands")
---
 drivers/nvme/host/ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/ioctl.c +1 -1
index 9550a69029b368..8aefe9c904dc9a 100644
@@ -45,7 +45,7 @@ static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c,
 	 * special file is open for writing, but always allow I/O commands that
 	 * transfer data from the controller.
 	 */
-	if (nvme_is_write(c))
+	if (nvme_is_write(c) || c->common.opcode == nvme_cmd_write_zeroes)
 		return mode & FMODE_WRITE;
 	return true;
 }
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Christoph Hellwig

Unfortunately Write Zeroes is coded as a no data transfer opcode in NVMe, so don't allow it on a read-only FD for unprivileged users.

Fixes 855b7717f44b ("nvme: fine-granular CAP_SYS_ADMIN for nvme io commands")
---
  drivers/nvme/host/ioctl.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/ioctl.c +0 -0
index 9550a69029b368..8aefe9c904dc9a 100644

drivers/nvme/host/ioctl.c static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c, · line 51

Quoted 8 lines
drivers/nvme/host/ioctl.c +1 -1
@@ -45,7 +45,7 @@ static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c,
  	 * special file is open for writing, but always allow I/O commands that
  	 * transfer data from the controller.
  	 */
-	if (nvme_is_write(c))
+	if (nvme_is_write(c) || c->common.opcode == nvme_cmd_write_zeroes)
  		return mode & FMODE_WRITE;
  	return true;
  }

Looks good.

-ck

#3 lore raw
In reply to Christoph Hellwig
Quoted 14 lines · Christoph Hellwig

Unfortunately Write Zeroes is coded as a no data transfer opcode in NVMe, so don't allow it on a read-only FD for unprivileged users.

Fixes 855b7717f44b ("nvme: fine-granular CAP_SYS_ADMIN for nvme io commands")

---
drivers/nvme/host/ioctl.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/ioctl.c +0 -0
index 9550a69029b368..8aefe9c904dc9a 100644

drivers/nvme/host/ioctl.c static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c, · line 49

Quoted 6 lines
drivers/nvme/host/ioctl.c +1 -1
@@ -45,7 +45,7 @@ static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c,
 	 * special file is open for writing, but always allow I/O commands that
 	 * transfer data from the controller.
 	 */
-	if (nvme_is_write(c))
+	if (nvme_is_write(c) || c->common.opcode == nvme_cmd_write_zeroes)
 		return mode & FMODE_WRITE;

I was thinking why check for write_zeroes should not go inside nvme_is_write itself. Then I saw various callers of nvme_is_write, and that killed the thought.

Another thought is - does it make sense to include nvme_cmd_flush too? That is also declared as no-data-transfer in spec. Flush alone can't make any difference when writes are not allowed in first place. So this is about whether we care for empty flushes.

And on spec - not sure whether the criteria has changed of late. copy command also does not involve data-transfer but bit is set there.

maintainer
#4 lore raw
In reply to Christoph Hellwig

drivers/nvme/host/ioctl.c static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c, · line 49

Quoted 3 lines · Christoph Hellwig
drivers/nvme/host/ioctl.c +1 -1
-	if (nvme_is_write(c))
+	if (nvme_is_write(c) || c->common.opcode == nvme_cmd_write_zeroes)
 		return mode & FMODE_WRITE;

Write Uncorrectable should also be checked, and any future opcodes that can modify media. Maybe use Command Effects Log's LBCC field instead? We can preload known effects for older nvme's that don't support that log page.

author maintainer
#5 lore raw
Quoted 9 lines · Keith Busch

drivers/nvme/host/ioctl.c static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c, · line 49

Quoted 3 lines · Christoph Hellwig
drivers/nvme/host/ioctl.c +1 -1
-	if (nvme_is_write(c))
+	if (nvme_is_write(c) || c->common.opcode == nvme_cmd_write_zeroes)
 		return mode & FMODE_WRITE;

Write Uncorrectable should also be checked, and any future opcodes that can modify media. Maybe use Command Effects Log's LBCC field instead? We can preload known effects for older nvme's that don't support that log page.

Yes, that might be a better idea. I'll try to find some time in the next days to implement that.