block: ublk: switch to ioctl command encoding
Subsystems (2)
PATCH v2 1/1 block: ublk: switch to ioctl command encoding
All ublk commands(control, IO) should have taken ioctl command encoding from the beginning, because ioctl command encoding defines each code uniquely, so driver can figure out wrong command sent from userspace easily;
2) it might help security subsystem for audit uring cmd[1].
Unfortunately we didn't do that way, and it could be one lesson for ublk driver.
So switch to ioctl command encoding now, we still support commands encoded in old way, but they become legacy definition. Any new command should take ioctl encoding.
ublksrv code for switching to ioctl command encoding:
https://github.com/ming1/ubdsrv/commits/ioctl_cmd_encoding
[1] https://lore.kernel.org/io-uring/CAHC9VhSVzujW9LOj5Km80AjU0EfAuukoLrxO6BEfnXeK_s6bAg@mail.gmail.com/
---
V2:
- simplify implementation
- add config option of BLK_DEV_UBLK_NO_OLD_CMD, suggested by Christoph
- improve commit log, and fix typo
drivers/block/Kconfig | 17 ++++++++++++++ drivers/block/ublk_drv.c | 29 +++++++++++++++++------ include/uapi/linux/ublk_cmd.h | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 7 deletions(-)
drivers/block/Kconfig +17 -0
@@ -385,6 +385,23 @@ config BLK_DEV_UBLK can handle batch more effectively, but task_work_add() isn't exported for module, so ublk has to be built to kernel. +config BLK_DEV_UBLK_NO_OLD_CMD + bool "Disable old command opcode" + depends on BLK_DEV_UBLK + default n + help + ublk driver started to take plain command encoding, which turns out + one bad way. The traditional ioctl command opcode encodes more + info and basically defines each code uniquely, so opcode conflict + is avoided, and driver can handle wrong cmd easily, meantime it + may help security subsystem to audit uring command. + + Say Y if you don't want to support old command opcode. It is suggested + to enable Y if your application(ublk server) switches to ioctl cmd + encoding. + + Say N if your application still uses old command opcode. + source "drivers/block/rnbd/Kconfig" endif # BLK_DEV
drivers/block/ublk_drv.c +22 -7
@@ -53,7 +53,8 @@ | UBLK_F_NEED_GET_DATA \ | UBLK_F_USER_RECOVERY \ | UBLK_F_USER_RECOVERY_REISSUE \ - | UBLK_F_UNPRIVILEGED_DEV) + | UBLK_F_UNPRIVILEGED_DEV \ + | UBLK_F_CMD_IOCTL_ENCODE)/* All UBLK_PARAM_TYPE_* should be included here */ #define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | \
@@ -1260,6 +1261,7 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)struct ublk_queue *ubq;struct ublk_io *io;u32 cmd_op = cmd->cmd_op; +u32 ioc_type =_IOC_TYPE (cmd_op);unsigned tag = ub_cmd->tag;int ret = -EINVAL ;struct request *req;
@@ -1268,6 +1270,11 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) __func__, cmd->cmd_op, ub_cmd->q_id, tag, ub_cmd->result); +if (IS_ENABLED (CONFIG_BLK_DEV_UBLK_NO_OLD_CMD ) && ioc_type !='u' ) +return -EOPNOTSUPP ; +if (ioc_type !='u' && ioc_type !=0 ) +return -EOPNOTSUPP ; +if (ub_cmd->q_id >= ub->dev_info.nr_hw_queues) goto out;
@@ -1294,10 +1301,10 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)* iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA. */ if ((!!(io->flags &UBLK_IO_FLAG_NEED_GET_DATA )) - ^ (cmd_op ==UBLK_IO_NEED_GET_DATA )) + ^ (_IOC_NR (cmd_op) ==UBLK_IO_NEED_GET_DATA )) goto out; -switch (cmd_op) { +switch (_IOC_NR (cmd_op)) {case UBLK_IO_FETCH_REQ :/* UBLK_IO_FETCH_REQ is only allowed before queue is setup */ if (ublk_queue_ready (ubq)) {
@@ -1743,6 +1750,8 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)if (!IS_BUILTIN (CONFIG_BLK_DEV_UBLK )) ub->dev_info.flags |=UBLK_F_URING_CMD_COMP_IN_TASK ; + ub->dev_info.flags |=UBLK_F_CMD_IOCTL_ENCODE ; +/* We are not ready to support zero copy */ ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY ;
@@ -2099,7 +2108,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,* know if the specified device is created as unprivileged * mode. */ -if (cmd->cmd_op !=UBLK_CMD_GET_DEV_INFO2 ) +if (_IOC_NR (cmd->cmd_op) !=UBLK_CMD_GET_DEV_INFO2 )return 0 ; }
@@ -2125,7 +2134,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub, dev_path[header->dev_path_len] =0 ; ret = -EINVAL ; -switch (cmd->cmd_op) { +switch (_IOC_NR (cmd->cmd_op)) {case UBLK_CMD_GET_DEV_INFO :case UBLK_CMD_GET_DEV_INFO2 :case UBLK_CMD_GET_QUEUE_AFFINITY :
@@ -2164,8 +2173,14 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd, {struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;struct ublk_device *ub =NULL ; +u32 ioc_type =_IOC_TYPE (cmd->cmd_op);int ret = -EINVAL ; +if (IS_ENABLED (CONFIG_BLK_DEV_UBLK_NO_OLD_CMD ) && ioc_type !='u' ) +return -EOPNOTSUPP ; +if (ioc_type !='u' && ioc_type !=0 ) +return -EOPNOTSUPP ; +if (issue_flags &IO_URING_F_NONBLOCK )return -EAGAIN ;
@@ -2174,7 +2189,7 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,if (!(issue_flags &IO_URING_F_SQE128 )) goto out; -if (cmd->cmd_op !=UBLK_CMD_ADD_DEV ) { +if (_IOC_NR (cmd->cmd_op) !=UBLK_CMD_ADD_DEV ) { ret = -ENODEV ; ub =ublk_get_device_from_id (header->dev_id);if (!ub)
@@ -2189,7 +2204,7 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,if (ret) goto put_dev; -switch (cmd->cmd_op) { +switch (_IOC_NR (cmd->cmd_op)) {case UBLK_CMD_START_DEV : ret =ublk_ctrl_start_dev (ub, cmd);break ;
include/uapi/linux/ublk_cmd.h +43 -0
71 lines not on this page · raw message
Signature · 1 line
2.38.1
Quoted 6 lines · Ming Lei
All ublk
commands(control, IO)should have taken ioctl command encoding from the beginning, because ioctl command encoding defines each code uniquely, so driver can figure out wrong command sent from userspace easily;
2) it might help security subsystem for audit uring cmd[1].Unfortunately we didn't do that way, and it could be one lesson for ublk driver.
FYI, a bunch of overly long lines in the commit message here, please stick to 73 lines.
Quoted 10 lines
So switch to ioctl command encoding now, we still support commands encoded in old way, but they become legacy definition. Any new command should take ioctl encoding.
ublksrv code for switching to ioctl command encoding:
https://github.com/ming1/ubdsrv/commits/ioctl_cmd_encoding
[1] https://lore.kernel.org/io-uring/CAHC9VhSVzujW9LOj5Km80AjU0EfAuukoLrxO6BEfnXeK_s6bAg@mail.gmail.com/
Maybe quote the main contents here?
drivers/block/Kconfig config BLK_DEV_UBLK · line 391
Quoted 4 lines
drivers/block/Kconfig +4
-0+config BLK_DEV_UBLK_NO_OLD_CMD + bool "Disable old command opcode" + depends on BLK_DEV_UBLK + default n
I'd invert this option and have a
BLKDEV_UBLK_LEGACY_OPCODES
or so commands.
(also n is the default default, so no need to state it, but with inverting the option that becomes moot anyway).
drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 1276
Quoted 4 lines
drivers/block/ublk_drv.c +4
-0+if (IS_ENABLED (CONFIG_BLK_DEV_UBLK_NO_OLD_CMD ) && ioc_type !='u' ) +return -EOPNOTSUPP ; +if (ioc_type !='u' && ioc_type !=0 ) +return -EOPNOTSUPP ;
Maybe switch these checks around for them to make more sense?
drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 1276
Quoted 4 lines
drivers/block/ublk_drv.c +4
-0+if (IS_ENABLED (CONFIG_BLK_DEV_UBLK_NO_OLD_CMD ) && ioc_type !='u' ) +return -EOPNOTSUPP ; +if (ioc_type !='u' && ioc_type !=0 ) +return -EOPNOTSUPP ;
And maybe factor this into a helper?