block: fix FS_IOC_GETLBMD_CAP parsing in blkdev_common_ioctl()
PATCH v2 1/1 block: fix FS_IOC_GETLBMD_CAP parsing in blkdev_common_ioctl()
From: Arnd Bergmann <[email protected]>
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.
---
v2: add the check in blk-integrity.c instead of ioctl.c
I've left out the maximum-size check this time, as there was no consensus on whether there should be one, or what value.
We still need to come up with a better way of handling these in general, for now the patch just addresses the immediate regression that Naresh found.
I have also sent a handful of patches for other drivers that have variations of the same bug.
--- block/blk-integrity.c | 10 ++++++---- block/ioctl.c | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-)
block/blk-integrity.c +6 -4
@@ -62,10 +62,12 @@ int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,struct logical_block_metadata_cap meta_cap = {};size_t usize =_IOC_SIZE (cmd); -if (!argp) -return -EINVAL ; -if (usize <LBMD_SIZE_VER0 ) -return -EINVAL ; +if (_IOC_DIR (cmd) !=_IOC_DIR (FS_IOC_GETLBMD_CAP ) || +_IOC_TYPE (cmd) !=_IOC_TYPE (FS_IOC_GETLBMD_CAP ) || +_IOC_NR (cmd) !=_IOC_NR (FS_IOC_GETLBMD_CAP ) || +_IOC_SIZE (cmd) <LBMD_SIZE_VER0 ) +return -ENOIOCTLCMD ; +if (!bi) goto out;
block/ioctl.c +4 -2
@@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, void __user *argp) {unsigned int max_sectors; +int ret; -if (_IOC_NR (cmd) ==_IOC_NR (FS_IOC_GETLBMD_CAP )) -return blk_get_meta_cap (bdev, cmd, argp); + ret =blk_get_meta_cap (bdev, cmd, argp); +if (ret != -ENOIOCTLCMD ) +return ret;switch (cmd) {case BLKFLSBUF :
Signature · 1 line
2.39.5
Quoted 15 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Anders and Naresh found that the addition of the
FS_IOC_GETLBMD_CAPhandling 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-ENOIOCTLCMDfor 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.
Quoted 6 lines
Fixes 9eb22f7fedfc ("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/ open hereReported-by Naresh Kamboju <[email protected]>Cc Anders Roxell <[email protected]>Cc Naresh Kamboju <[email protected]>Signed-off-by Arnd Bergmann <[email protected]>
Tested it in qemu-aarch64, it now ran the ltp-smoke test fine.
Cheers,
Anders
Quoted 59 lines
---
v2: add the check inblk-integrity.cinstead ofioctl.cI've left out the maximum-size check this time, as there was no consensus on whether there should be one, or what value.
We still need to come up with a better way of handling these in general, for now the patch just addresses the immediate regression that Naresh found.
I have also sent a handful of patches for other drivers that have variations of the same bug.
--- block/blk-integrity.c | 10 ++++++---- block/ioctl.c | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-)block/blk-integrity.c +6
-4@@ -62,10 +62,12 @@ int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,struct logical_block_metadata_cap meta_cap = {};size_t usize =_IOC_SIZE (cmd); -if (!argp) -return -EINVAL ; -if (usize <LBMD_SIZE_VER0 ) -return -EINVAL ; +if (_IOC_DIR (cmd) !=_IOC_DIR (FS_IOC_GETLBMD_CAP ) || +_IOC_TYPE (cmd) !=_IOC_TYPE (FS_IOC_GETLBMD_CAP ) || +_IOC_NR (cmd) !=_IOC_NR (FS_IOC_GETLBMD_CAP ) || +_IOC_SIZE (cmd) <LBMD_SIZE_VER0 ) +return -ENOIOCTLCMD ; +if (!bi) goto out;block/ioctl.c +4
-3@@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, void __user *argp) {unsigned int max_sectors; +int ret; -if (_IOC_NR (cmd) ==_IOC_NR (FS_IOC_GETLBMD_CAP )) -return blk_get_meta_cap (bdev, cmd, argp); + ret =blk_get_meta_cap (bdev, cmd, argp); +if (ret != -ENOIOCTLCMD ) +return ret;switch (cmd) {case BLKFLSBUF : --2.39.5
Quoted 26 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Anders and Naresh found that the addition of the
FS_IOC_GETLBMD_CAPhandling 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-ENOIOCTLCMDfor 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 9eb22f7fedfc ("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/ open hereReported-by Naresh Kamboju <[email protected]>Cc Anders Roxell <[email protected]>Cc Naresh Kamboju <[email protected]>Signed-off-by Arnd Bergmann <[email protected]>---
v2: add the check inblk-integrity.cinstead ofioctl.cI've left out the maximum-size check this time, as there was no consensus on whether there should be one, or what value.
Quoted 6 lines
We still need to come up with a better way of handling these in general, for now the patch just addresses the immediate regression that Naresh found.
I have also sent a handful of patches for other drivers that have variations of the same bug.
Arnd, let me know how I can help!
Quoted 7 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Anders and Naresh found that the addition of the
FS_IOC_GETLBMD_CAPhandling 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.
Quoted 8 lines
Move the check into the
blk_get_meta_cap()function itself and make it return-ENOIOCTLCMDfor 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 9eb22f7fedfc ("fs: add ioctl to query metadata and protection info capabilities")
Since this isn't from my tree:
Signature · 1 line
Jens Axboe
Hi,
Quoted 64 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Anders and Naresh found that the addition of the
FS_IOC_GETLBMD_CAPhandling 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-ENOIOCTLCMDfor 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 9eb22f7fedfc ("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/ open hereReported-by Naresh Kamboju <[email protected]>Cc Anders Roxell <[email protected]>Cc Naresh Kamboju <[email protected]>Signed-off-by Arnd Bergmann <[email protected]>---
v2: add the check inblk-integrity.cinstead ofioctl.cI've left out the maximum-size check this time, as there was no consensus on whether there should be one, or what value.
We still need to come up with a better way of handling these in general, for now the patch just addresses the immediate regression that Naresh found.
I have also sent a handful of patches for other drivers that have variations of the same bug.
--- block/blk-integrity.c | 10 ++++++---- block/ioctl.c | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-)block/blk-integrity.c +6
-4@@ -62,10 +62,12 @@ int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,struct logical_block_metadata_cap meta_cap = {};size_t usize =_IOC_SIZE (cmd); -if (!argp) -return -EINVAL ; -if (usize <LBMD_SIZE_VER0 ) -return -EINVAL ; +if (_IOC_DIR (cmd) !=_IOC_DIR (FS_IOC_GETLBMD_CAP ) || +_IOC_TYPE (cmd) !=_IOC_TYPE (FS_IOC_GETLBMD_CAP ) || +_IOC_NR (cmd) !=_IOC_NR (FS_IOC_GETLBMD_CAP ) || +_IOC_SIZE (cmd) <LBMD_SIZE_VER0 ) +return -ENOIOCTLCMD ; +if (!bi) goto out;block/ioctl.c +0
-0@@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, void __user *argp)
block/ioctl.c static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, · line 574
Quoted 10 lines
block/ioctl.c +4
-2{unsigned int max_sectors; +int ret; -if (_IOC_NR (cmd) ==_IOC_NR (FS_IOC_GETLBMD_CAP )) -return blk_get_meta_cap (bdev, cmd, argp); + ret =blk_get_meta_cap (bdev, cmd, argp); +if (ret != -ENOIOCTLCMD ) +return ret;
This check seems to be incomplete. In the case when BLK_DEV_INTEGRITY is disabled the ioctl can never complete as blk_get_meta_cap will then always return -EOPNOTSUPP. Or should the !BLK_DEV_INTEGRITY stub be changed to return -ENOIOCTLCMD instead?
It makes e.g. cryptsetup fail in my initramfs. Adding -EOPNOTSUPP to the check fixes it for me:
block/ioctl.c +1 -1
@@ -569,7 +569,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,int ret; ret =blk_get_meta_cap (bdev, cmd, argp); -if (ret != -ENOIOCTLCMD ) +if (ret != -EOPNOTSUPP && ret != -ENOIOCTLCMD )return ret;switch (cmd) {
Regards,
Klara Modin
Quoted 6 lines
block/ioctl.c +0
-0switch (cmd) {case BLKFLSBUF :Signature · 1 line
2.39.5
Quoted 13 lines · Klara Modin
Quoted 12 lines
block/ioctl.c +1
-2@@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, void __user *argp) {unsigned int max_sectors; +int ret; -if (_IOC_NR (cmd) ==_IOC_NR (FS_IOC_GETLBMD_CAP )) -return blk_get_meta_cap (bdev, cmd, argp);
Quoted 8 lines
block/ioctl.c static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, · line 573
Quoted 3 lines
block/ioctl.c +3
-0+ ret =blk_get_meta_cap (bdev, cmd, argp); +if (ret != -ENOIOCTLCMD ) +return ret;This check seems to be incomplete. In the case when
BLK_DEV_INTEGRITYis disabled the ioctl can never complete asblk_get_meta_capwill then always return-EOPNOTSUPP. Or should the !BLK_DEV_INTEGRITY stub be changed to return-ENOIOCTLCMDinstead?
Ah, I did miss the stub.
Quoted 16 lines
It makes e.g. cryptsetup fail in my initramfs. Adding
-EOPNOTSUPPto the check fixes it for me:block/ioctl.c +1
-1@@ -569,7 +569,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, int ret; ret =blk_get_meta_cap (bdev, cmd, argp); -if (ret != -ENOIOCTLCMD ) +if (ret != -EOPNOTSUPP && ret != -ENOIOCTLCMD )return ret;
Quoted 1 line
switch (cmd) {
I think returning -ENOIOCTLCMD from the stub makes more sense, but I don't know what the motivation for the -EOPNOTSUPP was.
Arnd
Quoted 42 lines · Arnd Bergmann
Quoted 13 lines · Klara Modin
Quoted 12 lines
block/ioctl.c +1
-2@@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, void __user *argp) {unsigned int max_sectors; +int ret; -if (_IOC_NR (cmd) ==_IOC_NR (FS_IOC_GETLBMD_CAP )) -return blk_get_meta_cap (bdev, cmd, argp);Quoted 8 lines
block/ioctl.c static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, · line 573
Quoted 3 lines
block/ioctl.c +3
-0+ ret =blk_get_meta_cap (bdev, cmd, argp); +if (ret != -ENOIOCTLCMD ) +return ret;This check seems to be incomplete. In the case when
BLK_DEV_INTEGRITYis disabled the ioctl can never complete asblk_get_meta_capwill then always return-EOPNOTSUPP. Or should the !BLK_DEV_INTEGRITY stub be changed to return-ENOIOCTLCMDinstead?Ah, I did miss the stub.
Quoted 16 lines
It makes e.g. cryptsetup fail in my initramfs. Adding
-EOPNOTSUPPto the check fixes it for me:block/ioctl.c +1
-1@@ -569,7 +569,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, int ret; ret =blk_get_meta_cap (bdev, cmd, argp); -if (ret != -ENOIOCTLCMD ) +if (ret != -EOPNOTSUPP && ret != -ENOIOCTLCMD )return ret;
Quoted 6 lines
Quoted 1 line
switch (cmd) {
I think returning
-ENOIOCTLCMDfrom the stub makes more sense, but I don't know what the motivation for the-EOPNOTSUPPwas.Arnd
Should I send a patch changing the stub? At least from reading Documentation/driver-api/ioctl.rst it seems clear that only -ENOIOCTLCMD or -ENOTTY is correct when the command number is unknown.
I didn't find any particular reason in 9eb22f7fedfc ("fs: add ioctl to query metadata and protection info capabilities") for the -EOPNOTSUPP return.
Regards,
Klara Modin
Quoted 51 lines · Klara Modin
Quoted 42 lines · Arnd Bergmann
Quoted 13 lines · Klara Modin
Quoted 12 lines
block/ioctl.c +1
-2@@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, void __user *argp) {unsigned int max_sectors; +int ret; -if (_IOC_NR (cmd) ==_IOC_NR (FS_IOC_GETLBMD_CAP )) -return blk_get_meta_cap (bdev, cmd, argp);Quoted 8 lines
block/ioctl.c static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, · line 573
Quoted 3 lines
block/ioctl.c +3
-0+ ret =blk_get_meta_cap (bdev, cmd, argp); +if (ret != -ENOIOCTLCMD ) +return ret;This check seems to be incomplete. In the case when
BLK_DEV_INTEGRITYis disabled the ioctl can never complete asblk_get_meta_capwill then always return-EOPNOTSUPP. Or should the !BLK_DEV_INTEGRITY stub be changed to return-ENOIOCTLCMDinstead?Ah, I did miss the stub.
Quoted 16 lines
It makes e.g. cryptsetup fail in my initramfs. Adding
-EOPNOTSUPPto the check fixes it for me:block/ioctl.c +1
-1@@ -569,7 +569,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, int ret; ret =blk_get_meta_cap (bdev, cmd, argp); -if (ret != -ENOIOCTLCMD ) +if (ret != -EOPNOTSUPP && ret != -ENOIOCTLCMD )return ret;Quoted 6 lines
Quoted 1 line
switch (cmd) {I think returning
-ENOIOCTLCMDfrom the stub makes more sense, but I don't know what the motivation for the-EOPNOTSUPPwas.Arnd
Quoted 7 lines
Should I send a patch changing the stub? At least from reading
Documentation/driver-api/ioctl.rstit seems clear that only-ENOIOCTLCMDor-ENOTTYis correct when the command number is unknown.I didn't find any particular reason in
9eb22f7fedfc("fs: add ioctl to query metadata and protection info capabilities") for the-EOPNOTSUPPreturn.
Hi Klara,
Thanks for pointing this out — I had originally used -EOPNOTSUPP because the ioctl command is recognized, but the operation isn’t supported when CONFIG_BLK_DEV_INTEGRITY=n.
That said, I agree that returning -ENOIOCTLCMD from the stub might be more appropriate in this context.
Thanks,
Anuj
Quoted 4 lines
Regards,
Klara Modin
Quoted 26 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Anders and Naresh found that the addition of the
FS_IOC_GETLBMD_CAPhandling 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-ENOIOCTLCMDfor 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 9eb22f7fedfc ("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/ open hereReported-by Naresh Kamboju <[email protected]>Cc Anders Roxell <[email protected]>Cc Naresh Kamboju <[email protected]>Signed-off-by Arnd Bergmann <[email protected]>---
v2: add the check inblk-integrity.cinstead ofioctl.cI've left out the maximum-size check this time, as there was no consensus on whether there should be one, or what value.
Quoted 7 lines
We still need to come up with a better way of handling these in general, for now the patch just addresses the immediate regression that Naresh found.
I have also sent a handful of patches for other drivers that have variations of the same bug.
---
In my tests I've found that this patch, merged as commit 42b0ef01e6b5 ("block: fix FS_IOC_GETLBMD_CAP parsing in blkdev_common_ioctl()"), breaks udev operation on some of my test boards - no /dev/disk/* entries and directories are created. Reverting $subject on top of next-20250728 fixes/hides this problem. I suspect that another corner case is missing in the checks. I will try to investigate this a bit more later, probably tomorrow.
Quoted 33 lines
block/blk-integrity.c | 10 ++++++---- block/ioctl.c | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-)block/blk-integrity.c +6
-4@@ -62,10 +62,12 @@ int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,struct logical_block_metadata_cap meta_cap = {};size_t usize =_IOC_SIZE (cmd); -if (!argp) -return -EINVAL ; -if (usize <LBMD_SIZE_VER0 ) -return -EINVAL ; +if (_IOC_DIR (cmd) !=_IOC_DIR (FS_IOC_GETLBMD_CAP ) || +_IOC_TYPE (cmd) !=_IOC_TYPE (FS_IOC_GETLBMD_CAP ) || +_IOC_NR (cmd) !=_IOC_NR (FS_IOC_GETLBMD_CAP ) || +_IOC_SIZE (cmd) <LBMD_SIZE_VER0 ) +return -ENOIOCTLCMD ; +if (!bi) goto out;block/ioctl.c +0
-0@@ -566,9 +566,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, void __user *argp) {unsigned int max_sectors;
block/ioctl.c static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, · line 576
Quoted 10 lines
block/ioctl.c +4
-2+int ret; -if (_IOC_NR (cmd) ==_IOC_NR (FS_IOC_GETLBMD_CAP )) -return blk_get_meta_cap (bdev, cmd, argp); + ret =blk_get_meta_cap (bdev, cmd, argp); +if (ret != -ENOIOCTLCMD ) +return ret;switch (cmd) {case BLKFLSBUF :
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland