Re: [RFC 1/2] nvme: add whitelisting infrastructure

Joel Granados
2022-09-21T10:58:38Z

[-- Attachment #1: Type: text/plain, Size: 2203 bytes --]

On Fri, Sep 09, 2022 at 10:03:06PM +0530, Kanchan Joshi wrote:
> 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@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)
These all should be static functions. right? So we keep the scope within
the file?

Best
> +{
> + /* 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
>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]