From: Chiara Meiohas Drivers communicate with device firmware either via register-based commands (writing parameters into device registers) or by passing a command buffer using shared-memory mechanisms. The proposed fw_validate_cmd hook is intended for the command buffer mechanism, which is commonly used on modern, complex devices. This hook allows inspecting firmware command buffers before they are sent to the device. The hook receives the command buffer, device, command class, and a class-specific id: - class_id (enum fw_cmd_class) allows BPF programs to differentiate between classes of firmware commands. In this series, class_id distinguishes between commands from the RDMA uverbs interface and from fwctl. - id is a class-specific device identifier. For uverbs, id is the RDMA driver identifier (enum rdma_driver_id). For fwctl, id is the device type (enum fwctl_device_type). The mailbox format varies across vendors and may even differ between firmware versions, so policy authors must be familiar with the specific device's mailbox format. BPF programs can be tailored to inspect the mailbox accordingly, making BPF the natural fit. Therefore, the hook is defined using the LSM_HOOK macro in bpf_lsm.c rather than in lsm_hook_defs.h, as it is a BPF-only hook. Signed-off-by: Chiara Meiohas Reviewed-by: Maher Sanalla Signed-off-by: Leon Romanovsky --- include/linux/bpf_lsm.h | 41 +++++++++++++++++++++++++++++++++++++++++ kernel/bpf/bpf_lsm.c | 11 +++++++++++ 2 files changed, 52 insertions(+) diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h index 643809cc78c33..7ad7e153f486c 100644 --- a/include/linux/bpf_lsm.h +++ b/include/linux/bpf_lsm.h @@ -12,6 +12,21 @@ #include #include +struct device; + +/** + * enum fw_cmd_class - Class of the firmware command passed to + * bpf_lsm_fw_validate_cmd. + * This allows BPF programs to distinguish between different command classes. + * + * @FW_CMD_CLASS_UVERBS: Command originated from the RDMA uverbs interface + * @FW_CMD_CLASS_FWCTL: Command originated from the fwctl interface + */ +enum fw_cmd_class { + FW_CMD_CLASS_UVERBS, + FW_CMD_CLASS_FWCTL, +}; + #ifdef CONFIG_BPF_LSM #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ @@ -53,6 +68,24 @@ int bpf_set_dentry_xattr_locked(struct dentry *dentry, const char *name__str, int bpf_remove_dentry_xattr_locked(struct dentry *dentry, const char *name__str); bool bpf_lsm_has_d_inode_locked(const struct bpf_prog *prog); +/** + * bpf_lsm_fw_validate_cmd() - Validate a firmware command + * @in: pointer to the firmware command input buffer + * @in_len: length of the firmware command input buffer + * @dev: device associated with the command + * @class_id: class of the firmware command + * @id: device identifier, specific to the command @class_id + * + * Check permissions before sending a firmware command generated by + * userspace to the device. + * + * Return: Returns 0 if permission is granted, or a negative errno + * value to deny the operation. + */ +int bpf_lsm_fw_validate_cmd(const void *in, size_t in_len, + const struct device *dev, + enum fw_cmd_class class_id, u32 id); + #else /* !CONFIG_BPF_LSM */ static inline bool bpf_lsm_is_sleepable_hook(u32 btf_id) @@ -104,6 +137,14 @@ static inline bool bpf_lsm_has_d_inode_locked(const struct bpf_prog *prog) { return false; } + +static inline int bpf_lsm_fw_validate_cmd(const void *in, size_t in_len, + const struct device *dev, + enum fw_cmd_class class_id, u32 id) +{ + return 0; +} + #endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */ diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c index 0c4a0c8e6f703..fbdc056995fee 100644 --- a/kernel/bpf/bpf_lsm.c +++ b/kernel/bpf/bpf_lsm.c @@ -28,12 +28,23 @@ __weak noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ } #include + +/* + * fw_validate_cmd is not in lsm_hook_defs.h because it is a BPF-only + * hook — mailbox formats are device-specific, making BPF the natural + * fit for inspection. + */ +LSM_HOOK(int, 0, fw_validate_cmd, const void *in, size_t in_len, + const struct device *dev, enum fw_cmd_class class_id, u32 id) +EXPORT_SYMBOL_GPL(bpf_lsm_fw_validate_cmd); + #undef LSM_HOOK #define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME) BTF_SET_START(bpf_lsm_hooks) #include #undef LSM_HOOK +BTF_ID(func, bpf_lsm_fw_validate_cmd) BTF_SET_END(bpf_lsm_hooks) BTF_SET_START(bpf_lsm_disabled_hooks) -- 2.53.0 From: Chiara Meiohas The first test validates that the BPF verifier accepts a program that accesses the hook parameters (in_len) and returns values in the valid errno range. The second test validates that the BPF verifier rejects a program that returns a positive value, which is outside the valid [-4095, 0] return range for BPF-LSM hooks. Signed-off-by: Chiara Meiohas Reviewed-by: Maher Sanalla Signed-off-by: Leon Romanovsky --- tools/testing/selftests/bpf/progs/verifier_lsm.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_lsm.c b/tools/testing/selftests/bpf/progs/verifier_lsm.c index 38e8e91768620..9b2487948f8cb 100644 --- a/tools/testing/selftests/bpf/progs/verifier_lsm.c +++ b/tools/testing/selftests/bpf/progs/verifier_lsm.c @@ -188,4 +188,27 @@ int BPF_PROG(null_check, struct file *file) return 0; } +SEC("lsm/fw_validate_cmd") +__description("lsm fw_validate_cmd: validate hook parameters") +__success +int BPF_PROG(fw_validate_cmd_test, const void *in, size_t in_len, + const struct device *dev, enum fw_cmd_class class_id, u32 id) +{ + if (!in_len) + return -22; + + return 0; +} + +SEC("lsm/fw_validate_cmd") +__description("lsm fw_validate_cmd: invalid positive return") +__failure __msg("R0 has smin=1 smax=1 should have been in [-4095, 0]") +__naked int fw_validate_cmd_fail(void *ctx) +{ + asm volatile ( + "r0 = 1;" + "exit;" + ::: __clobber_all); +} + char _license[] SEC("license") = "GPL"; -- 2.53.0 From: Chiara Meiohas DEVX is an RDMA uverbs extension that allows userspace to submit firmware command buffers. The driver inspects the command and then passes the buffer through for firmware execution. Call bpf_lsm_fw_validate_cmd() before dispatching firmware commands through DEVX. This allows BPF programs to implement custom policies and enforce per-command security policy on user-triggered firmware commands. For example, a BPF program could restrict specific firmware operations to privileged users. Signed-off-by: Chiara Meiohas Reviewed-by: Maher Sanalla Reviewed-by: Jonathan Cameron Reviewed-by: Dave Jiang Signed-off-by: Leon Romanovsky --- drivers/infiniband/hw/mlx5/devx.c | 49 +++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/drivers/infiniband/hw/mlx5/devx.c b/drivers/infiniband/hw/mlx5/devx.c index 0066b2738ac89..b7a2e19987018 100644 --- a/drivers/infiniband/hw/mlx5/devx.c +++ b/drivers/infiniband/hw/mlx5/devx.c @@ -18,6 +18,7 @@ #include "devx.h" #include "qp.h" #include +#include #define UVERBS_MODULE_NAME mlx5_ib #include @@ -1111,6 +1112,8 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OTHER)( struct mlx5_ib_dev *dev; void *cmd_in = uverbs_attr_get_alloced_ptr( attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_IN); + int cmd_in_len = uverbs_attr_get_len(attrs, + MLX5_IB_ATTR_DEVX_OTHER_CMD_IN); int cmd_out_len = uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_OUT); void *cmd_out; @@ -1135,9 +1138,12 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OTHER)( return PTR_ERR(cmd_out); MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid); - err = mlx5_cmd_do(dev->mdev, cmd_in, - uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_IN), - cmd_out, cmd_out_len); + err = bpf_lsm_fw_validate_cmd(cmd_in, cmd_in_len, &dev->ib_dev.dev, + FW_CMD_CLASS_UVERBS, RDMA_DRIVER_MLX5); + if (err) + return err; + + err = mlx5_cmd_do(dev->mdev, cmd_in, cmd_in_len, cmd_out, cmd_out_len); if (err && err != -EREMOTEIO) return err; @@ -1570,6 +1576,11 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)( devx_set_umem_valid(cmd_in); } + err = bpf_lsm_fw_validate_cmd(cmd_in, cmd_in_len, &dev->ib_dev.dev, + FW_CMD_CLASS_UVERBS, RDMA_DRIVER_MLX5); + if (err) + goto obj_free; + if (opcode == MLX5_CMD_OP_CREATE_DCT) { obj->flags |= DEVX_OBJ_FLAGS_DCT; err = mlx5_core_create_dct(dev, &obj->core_dct, cmd_in, @@ -1646,6 +1657,8 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_MODIFY)( struct uverbs_attr_bundle *attrs) { void *cmd_in = uverbs_attr_get_alloced_ptr(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN); + int cmd_in_len = uverbs_attr_get_len(attrs, + MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN); int cmd_out_len = uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_OUT); struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs, @@ -1676,10 +1689,12 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_MODIFY)( MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid); devx_set_umem_valid(cmd_in); + err = bpf_lsm_fw_validate_cmd(cmd_in, cmd_in_len, &mdev->ib_dev.dev, + FW_CMD_CLASS_UVERBS, RDMA_DRIVER_MLX5); + if (err) + return err; - err = mlx5_cmd_do(mdev->mdev, cmd_in, - uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN), - cmd_out, cmd_out_len); + err = mlx5_cmd_do(mdev->mdev, cmd_in, cmd_in_len, cmd_out, cmd_out_len); if (err && err != -EREMOTEIO) return err; @@ -1693,6 +1708,8 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_QUERY)( struct uverbs_attr_bundle *attrs) { void *cmd_in = uverbs_attr_get_alloced_ptr(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN); + int cmd_in_len = uverbs_attr_get_len(attrs, + MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN); int cmd_out_len = uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_OUT); struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs, @@ -1722,9 +1739,12 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_QUERY)( return PTR_ERR(cmd_out); MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid); - err = mlx5_cmd_do(mdev->mdev, cmd_in, - uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN), - cmd_out, cmd_out_len); + err = bpf_lsm_fw_validate_cmd(cmd_in, cmd_in_len, &mdev->ib_dev.dev, + FW_CMD_CLASS_UVERBS, RDMA_DRIVER_MLX5); + if (err) + return err; + + err = mlx5_cmd_do(mdev->mdev, cmd_in, cmd_in_len, cmd_out, cmd_out_len); if (err && err != -EREMOTEIO) return err; @@ -1832,6 +1852,8 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_ASYNC_QUERY)( { void *cmd_in = uverbs_attr_get_alloced_ptr(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_ASYNC_CMD_IN); + int cmd_in_len = uverbs_attr_get_len(attrs, + MLX5_IB_ATTR_DEVX_OBJ_QUERY_ASYNC_CMD_IN); struct ib_uobject *uobj = uverbs_attr_get_uobject( attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_ASYNC_HANDLE); @@ -1894,9 +1916,12 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_ASYNC_QUERY)( async_data->ev_file = ev_file; MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid); - err = mlx5_cmd_exec_cb(&ev_file->async_ctx, cmd_in, - uverbs_attr_get_len(attrs, - MLX5_IB_ATTR_DEVX_OBJ_QUERY_ASYNC_CMD_IN), + err = bpf_lsm_fw_validate_cmd(cmd_in, cmd_in_len, &mdev->ib_dev.dev, + FW_CMD_CLASS_UVERBS, RDMA_DRIVER_MLX5); + if (err) + goto free_async; + + err = mlx5_cmd_exec_cb(&ev_file->async_ctx, cmd_in, cmd_in_len, async_data->hdr.out_data, async_data->cmd_out_len, devx_query_callback, &async_data->cb_work); -- 2.53.0 From: Chiara Meiohas fwctl is subsystem which exposes a firmware interface directly to userspace: it allows userspace to send device specific command buffers to firmware. fwctl is focused on debugging, configuration and provisioning of the device. Call bpf_lsm_fw_validate_cmd() before dispatching the user-provided firmware command. This allows BPF programs to implement custom policies and enforce per-command security policy on user-triggered firmware commands. For example, a BPF program could filter firmware commands based on their opcode. Signed-off-by: Chiara Meiohas Reviewed-by: Maher Sanalla Reviewed-by: Jonathan Cameron Reviewed-by: Dave Jiang Signed-off-by: Leon Romanovsky --- drivers/fwctl/mlx5/main.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/fwctl/mlx5/main.c b/drivers/fwctl/mlx5/main.c index e86ab703c767a..c49dfa1d172d9 100644 --- a/drivers/fwctl/mlx5/main.c +++ b/drivers/fwctl/mlx5/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #define mlx5ctl_err(mcdev, format, ...) \ dev_err(&mcdev->fwctl.dev, format, ##__VA_ARGS__) @@ -324,6 +325,15 @@ static void *mlx5ctl_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope, if (!mlx5ctl_validate_rpc(rpc_in, scope)) return ERR_PTR(-EBADMSG); + /* Enforce the user context for the command */ + MLX5_SET(mbox_in_hdr, rpc_in, uid, mfd->uctx_uid); + + ret = bpf_lsm_fw_validate_cmd(rpc_in, in_len, &mcdev->fwctl.dev, + FW_CMD_CLASS_FWCTL, + FWCTL_DEVICE_TYPE_MLX5); + if (ret) + return ERR_PTR(ret); + /* * mlx5_cmd_do() copies the input message to its own buffer before * executing it, so we can reuse the allocation for the output. @@ -336,8 +346,6 @@ static void *mlx5ctl_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope, return ERR_PTR(-ENOMEM); } - /* Enforce the user context for the command */ - MLX5_SET(mbox_in_hdr, rpc_in, uid, mfd->uctx_uid); ret = mlx5_cmd_do(mcdev->mdev, rpc_in, in_len, rpc_out, *out_len); mlx5ctl_dbg(mcdev, -- 2.53.0