[PATCH] nfc: virtual_ncidev: Add missing ioctl compat handler

Chris Gellermann posted 1 patch 3 weeks ago
drivers/nfc/virtual_ncidev.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] nfc: virtual_ncidev: Add missing ioctl compat handler
Posted by Chris Gellermann 3 weeks ago
The compat handler for ioctls to the virtual nci device is missing. So,
nci-specific ioctls of a compat task return with -1 and errno set to
ENOTTY. Add a handler.

The handling of an ioctl() call of a compat task to get the index of
virtual nci device (IOCTL_GET_NCIDEV_IDX) lands in the default case of
the ioctl compat handler (see fs/ioctl.c):

COMPAT_SYSCALL_DEFINE3(ioctl, ...)
{
	...
	default:
       		error = do_vfs_ioctl(fd_file(f), fd, cmd, ...);
		if (error != -ENOIOCTLCMD)
			break;

		if (fd_file(f)->f_op->compat_ioctl)
			error = fd_file(f)->f_op->compat_ioctl(fd_file(f), cmd, arg);
		if (error == -ENOIOCTLCMD)
			error = -ENOTTY;
	...
}

There, do_vfs_ioctl() returns -ENOIOCTLCMD and compat_ioctl is not
set for virtual_ncidev_fops, i.e. f_op->compat_ioctl == NULL. So, the
ioctl() syscall returns with -1 and errno set to ENOTTY to the compat
task.

To fix this, use the compat_ptr_ioctl helper for compat handling here.
It shall be used for ioctls that "either ignore the argument or pass a
pointer to a compatible data type". The driver's sole ioctl takes a user
void pointer and copies nfc_dev->idx to it, a 4-byte integer across all
ABIs.

This issue has been found by running the nci_dev kernel selftest as
rv64 binary on top of a CHERI kernel, where the ioctl() ends up in
the ioctl compat handler, similar to a 32-bit application on top of a
64-bit kernel.

Fixes: e624e6c3e777 ("nfc: Add a virtual nci device driver")
Signed-off-by: Chris Gellermann <christian.gellermann@codasip.com>
---
 drivers/nfc/virtual_ncidev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/nfc/virtual_ncidev.c b/drivers/nfc/virtual_ncidev.c
index 8eeb447ac96e..e51c647b27eb 100644
--- a/drivers/nfc/virtual_ncidev.c
+++ b/drivers/nfc/virtual_ncidev.c
@@ -195,7 +195,8 @@ static const struct file_operations virtual_ncidev_fops = {
 	.write = virtual_ncidev_write,
 	.open = virtual_ncidev_open,
 	.release = virtual_ncidev_close,
-	.unlocked_ioctl = virtual_ncidev_ioctl
+	.unlocked_ioctl = virtual_ncidev_ioctl,
+	.compat_ioctl = compat_ptr_ioctl,
 };
 
 static struct miscdevice miscdev = {
-- 
2.47.3
Re: [PATCH] nfc: virtual_ncidev: Add missing ioctl compat handler
Posted by Simon Horman 2 weeks, 3 days ago
On Fri, Sep 04, 2026 at 06:42:52PM +0200, Chris Gellermann wrote:
> The compat handler for ioctls to the virtual nci device is missing. So,
> nci-specific ioctls of a compat task return with -1 and errno set to
> ENOTTY. Add a handler.
> 
> The handling of an ioctl() call of a compat task to get the index of
> virtual nci device (IOCTL_GET_NCIDEV_IDX) lands in the default case of
> the ioctl compat handler (see fs/ioctl.c):
> 
> COMPAT_SYSCALL_DEFINE3(ioctl, ...)
> {
> 	...
> 	default:
>        		error = do_vfs_ioctl(fd_file(f), fd, cmd, ...);
> 		if (error != -ENOIOCTLCMD)
> 			break;
> 
> 		if (fd_file(f)->f_op->compat_ioctl)
> 			error = fd_file(f)->f_op->compat_ioctl(fd_file(f), cmd, arg);
> 		if (error == -ENOIOCTLCMD)
> 			error = -ENOTTY;
> 	...
> }
> 
> There, do_vfs_ioctl() returns -ENOIOCTLCMD and compat_ioctl is not
> set for virtual_ncidev_fops, i.e. f_op->compat_ioctl == NULL. So, the
> ioctl() syscall returns with -1 and errno set to ENOTTY to the compat
> task.
> 
> To fix this, use the compat_ptr_ioctl helper for compat handling here.
> It shall be used for ioctls that "either ignore the argument or pass a
> pointer to a compatible data type". The driver's sole ioctl takes a user
> void pointer and copies nfc_dev->idx to it, a 4-byte integer across all
> ABIs.
> 
> This issue has been found by running the nci_dev kernel selftest as
> rv64 binary on top of a CHERI kernel, where the ioctl() ends up in
> the ioctl compat handler, similar to a 32-bit application on top of a
> 64-bit kernel.
> 
> Fixes: e624e6c3e777 ("nfc: Add a virtual nci device driver")
> Signed-off-by: Chris Gellermann <christian.gellermann@codasip.com>

Reviewed-by: Simon Horman <horms@kernel.org>