block: fix FS_IOC_GETLBMD_CAP parsing in blkdev_common_ioctl()

Anders and Naresh found that the addition of the FS_IOC_GETLBMD_CAP
handling in the blockdev ioctl handler breaks all ioctls with
_IOC_NR==2, as the new command is not added to the switch but only
a few of the command bits are check.

Move the check into the blk_get_meta_cap() function itself and make
it return -ENOIOCTLCMD for any unsupported command code, including
those with a smaller size that previously returned -EINVAL.

For consistency this also drops the check for NULL 'arg' that
is really useless, as any invalid pointer should return -EFAULT.

Fixes: 9eb22f7fed ("fs: add ioctl to query metadata and protection info capabilities")
Link: https://lore.kernel.org/all/CA+G9fYvk9HHE5UJ7cdJHTcY6P5JKnp+_e+sdC5U-ZQFTP9_hqQ@mail.gmail.com/
Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org>
Cc: Anders Roxell <anders.roxell@linaro.org>
Cc: Naresh Kamboju <naresh.kamboju@linaro.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/20250711084708.2714436-1-arnd@kernel.org
Tested-by: Anders Roxell <anders.roxell@linaro.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Arnd Bergmann 2025-07-11 10:46:51 +02:00 committed by Christian Brauner
parent 4a3def74d9
commit 42b0ef01e6
No known key found for this signature in database
GPG Key ID: 91C61BC06578DCA2
2 changed files with 10 additions and 6 deletions

View File

@ -62,10 +62,12 @@ int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,
struct logical_block_metadata_cap meta_cap = {}; struct logical_block_metadata_cap meta_cap = {};
size_t usize = _IOC_SIZE(cmd); size_t usize = _IOC_SIZE(cmd);
if (!argp) if (_IOC_DIR(cmd) != _IOC_DIR(FS_IOC_GETLBMD_CAP) ||
return -EINVAL; _IOC_TYPE(cmd) != _IOC_TYPE(FS_IOC_GETLBMD_CAP) ||
if (usize < LBMD_SIZE_VER0) _IOC_NR(cmd) != _IOC_NR(FS_IOC_GETLBMD_CAP) ||
return -EINVAL; _IOC_SIZE(cmd) < LBMD_SIZE_VER0)
return -ENOIOCTLCMD;
if (!bi) if (!bi)
goto out; goto out;

View File

@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
void __user *argp) void __user *argp)
{ {
unsigned int max_sectors; unsigned int max_sectors;
int ret;
if (_IOC_NR(cmd) == _IOC_NR(FS_IOC_GETLBMD_CAP)) ret = blk_get_meta_cap(bdev, cmd, argp);
return blk_get_meta_cap(bdev, cmd, argp); if (ret != -ENOIOCTLCMD)
return ret;
switch (cmd) { switch (cmd) {
case BLKFLSBUF: case BLKFLSBUF: