This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 36900e3a59c fs: centralize device ioctl argument sizing for RPMSG
forwarding
36900e3a59c is described below
commit 36900e3a59ca0492182641c716005d963dc7ddb9
Author: Xiang Xiao <[email protected]>
AuthorDate: Sun Nov 29 01:19:06 2026 +0800
fs: centralize device ioctl argument sizing for RPMSG forwarding
Add ioctl_arglen() in VFS for contiguous device arguments and reuse it
in RPMSG clients. Preserve local operations and specialized pointer
handling.
Signed-off-by: Xiang Xiao <[email protected]>
Signed-off-by: jingfei <[email protected]>
---
Documentation/os/filesystem/index.rst | 31 ++++++
drivers/misc/rpmsgblk.c | 31 +++---
drivers/misc/rpmsgdev.c | 58 +++++-------
drivers/mtd/rpmsgmtd.c | 41 ++------
fs/rpmsgfs/rpmsgfs_client.c | 37 ++------
fs/vfs/fs_ioctl.c | 172 ++++++++++++++++++++++++++++++++++
include/nuttx/fs/ioctl.h | 23 +++++
7 files changed, 279 insertions(+), 114 deletions(-)
diff --git a/Documentation/os/filesystem/index.rst
b/Documentation/os/filesystem/index.rst
index 56bbc35283b..ab5a464a953 100644
--- a/Documentation/os/filesystem/index.rst
+++ b/Documentation/os/filesystem/index.rst
@@ -52,6 +52,37 @@ needs to implement. This also allows the file system to be
free from worry
about the device driver implementations for storage devices, as they also
expose a unified way of accessing the underlying devices.
+Ioctl Argument Sizes
+--------------------
+
+``ssize_t ioctl_arglen(int cmd, unsigned long arg)`` is declared in
+``<nuttx/fs/ioctl.h>`` and implemented in ``fs/vfs/fs_ioctl.c``. It describes
+the argument received at a device ioctl entry point, excluding any transport
+header:
+
+* A positive result is the number of bytes in the contiguous buffer pointed
+ to by ``arg``.
+* Zero means the argument is an immediate value or is unused. No argument
+ buffer should be copied.
+* A negative result is a negated errno: ``-ENOTTY`` for an unsupported
+ command, ``-EINVAL`` for a missing variable-size header or string, or
+ ``-EOVERFLOW`` when the size cannot be represented by ``ssize_t``.
+
+Variable-size commands require an accessible input header containing the
+length, or a NUL-terminated string. The helper checks size arithmetic; it
+does not validate pointer accessibility, buffer capacity, or the availability
+of an ioctl on a particular device. The caller must provide the complete
+input and output buffer described by the command.
+
+Requests needing nested-pointer marshalling, such as configdata, SMART
+sector I/O and MMC commands, remain transport-specific. Unknown
+driver-private commands are not inferred from their command number.
+
+RPMSG clients call ``ioctl_arglen()`` directly for flat
+arguments, while retaining local operations and specialized handling for
+non-flat requests. The remote device remains responsible for accepting
+or rejecting the command.
+
How VFS works
-------------
diff --git a/drivers/misc/rpmsgblk.c b/drivers/misc/rpmsgblk.c
index 7d6c788116c..bf5481bc794 100644
--- a/drivers/misc/rpmsgblk.c
+++ b/drivers/misc/rpmsgblk.c
@@ -95,7 +95,7 @@ static ssize_t rpmsgblk_write(FAR struct inode *inode,
blkcnt_t start_sector, unsigned int nsectors);
static int rpmsgblk_geometry(FAR struct inode *inode,
FAR struct geometry *geometry);
-static ssize_t rpmsgblk_ioctl_arglen(int cmd);
+static ssize_t rpmsgblk_ioctl_arglen(int cmd, unsigned long arg);
static int rpmsgblk_ioctl(FAR struct inode *inode, int cmd,
unsigned long arg);
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
@@ -500,19 +500,11 @@ out:
*
****************************************************************************/
-static ssize_t rpmsgblk_ioctl_arglen(int cmd)
+static ssize_t rpmsgblk_ioctl_arglen(int cmd, unsigned long arg)
{
switch (cmd)
{
- case BIOC_XIPBASE:
- return sizeof(uintptr_t);
- case BIOC_PROBE:
- case BIOC_EJECT:
- case BIOC_LLFORMAT:
case BIOC_GETFORMAT:
- case BIOC_ALLOCSECT:
- case BIOC_FREESECT:
- case BIOC_FLUSH:
return 0;
case BIOC_READSECT:
case BIOC_WRITESECT:
@@ -521,12 +513,8 @@ static ssize_t rpmsgblk_ioctl_arglen(int cmd)
return sizeof(struct mtd_smart_procfs_data_s);
case BIOC_DEBUGCMD:
return sizeof(struct mtd_smart_debug_data_s);
- case BIOC_PARTINFO:
- return sizeof(struct partition_info_s);
- case BIOC_BLKSSZGET:
- return sizeof(blksize_t);
default:
- return -ENOTTY;
+ return ioctl_arglen(cmd, arg);
}
}
@@ -688,21 +676,26 @@ static int rpmsgblk_default_ioctl(FAR struct inode
*inode, int cmd,
/* Call our internal routine to perform the ioctl */
- arglen = rpmsgblk_ioctl_arglen(cmd);
+ arglen = rpmsgblk_ioctl_arglen(cmd, arg);
if (arglen < 0)
{
return arglen;
}
- msglen = sizeof(*msg) + arglen - 1;
-
msg = rpmsgblk_get_tx_payload_buffer(priv, &space);
if (msg == NULL)
{
return -ENOMEM;
}
- DEBUGASSERT(space >= msglen);
+ if (space < sizeof(*msg) - 1 ||
+ arglen > space - (sizeof(*msg) - 1))
+ {
+ rpmsg_release_tx_buffer(&priv->ept, msg);
+ return -EMSGSIZE;
+ }
+
+ msglen = sizeof(*msg) - 1 + arglen;
msg->request = cmd;
msg->arg = arg;
diff --git a/drivers/misc/rpmsgdev.c b/drivers/misc/rpmsgdev.c
index 47e7bd4c394..aef92d1cca7 100644
--- a/drivers/misc/rpmsgdev.c
+++ b/drivers/misc/rpmsgdev.c
@@ -669,46 +669,32 @@ static ssize_t rpmsgdev_ioctl_arglen(int cmd, unsigned
long arg)
{
switch (cmd)
{
- case FIONWRITE:
- case FIONREAD:
- case FIONSPACE:
- case FBIOSET_POWER:
- case FBIOGET_POWER:
- case BATIOC_CURRENT:
- case BATIOC_VOLTAGE:
- case BATIOC_TEMPERATURE:
- case BATIOC_INPUT_CURRENT:
- case BATIOC_STATE:
- case BATIOC_ONLINE:
- case BATIOC_CAPACITY:
- case BATIOC_CHIPID:
- return sizeof(int);
- case TUNSETIFF:
- case TUNGETIFF:
- return sizeof(struct ifreq);
- case FIOC_FILEPATH:
- return PATH_MAX;
- case BATIOC_GET_PROTOCOL:
- case BATIOC_OPERATE:
- return sizeof(struct batio_operate_msg_s);
- case MSIOC_VENDOR:
- return sizeof(struct mouse_vendor_cmd_s) +
- ((FAR struct mouse_vendor_cmd_s *)(uintptr_t)arg)->len;
- case MSIOC_GRAB:
- return 0;
case CFGDIOC_GETCONFIG:
case CFGDIOC_SETCONFIG:
case CFGDIOC_FIRSTCONFIG:
case CFGDIOC_NEXTCONFIG:
- return sizeof(struct config_data_s) +
- ((FAR struct config_data_s *)(uintptr_t)arg)->len;
+ {
+ FAR const struct config_data_s *cfg =
+ (FAR const struct config_data_s *)(uintptr_t)arg;
+
+ if (cfg == NULL)
+ {
+ return -EINVAL;
+ }
+
+ if (cfg->len > SSIZE_MAX - sizeof(*cfg))
+ {
+ return -EOVERFLOW;
+ }
+
+ return sizeof(*cfg) + cfg->len;
+ }
+
case CFGDIOC_DELCONFIG:
return sizeof(struct config_data_s);
- case RPMSGDEV_PRIVATE_IOCTL:
- return sizeof(struct rpmsgdev_private_ioctl) +
- ((FAR struct rpmsgdev_private_ioctl *)(uintptr_t)arg)->len;
+
default:
- return -ENOTTY;
+ return ioctl_arglen(cmd, arg);
}
}
@@ -771,13 +757,15 @@ static int rpmsgdev_ioctl(FAR struct file *filep, int
cmd, unsigned long arg)
return -ENOMEM;
}
- msglen = sizeof(*msg) + arglen - 1;
- if (space < msglen)
+ if (space < sizeof(*msg) - 1 ||
+ arglen > space - (sizeof(*msg) - 1))
{
rpmsg_release_tx_buffer(&dev->ept, msg);
return -EMSGSIZE;
}
+ msglen = sizeof(*msg) - 1 + arglen;
+
msg->filep = priv->filep;
msg->request = cmd;
msg->arg = arg;
diff --git a/drivers/mtd/rpmsgmtd.c b/drivers/mtd/rpmsgmtd.c
index bd6e2b3d6c1..888272d1d30 100644
--- a/drivers/mtd/rpmsgmtd.c
+++ b/drivers/mtd/rpmsgmtd.c
@@ -93,7 +93,6 @@ static ssize_t rpmsgmtd_read(FAR struct mtd_dev_s *dev, off_t
offset,
static ssize_t rpmsgmtd_write(FAR struct mtd_dev_s *dev, off_t offset,
size_t nbytes, FAR const uint8_t *buffer);
#endif
-static ssize_t rpmsgmtd_ioctl_arglen(int cmd);
static int rpmsgmtd_ioctl(FAR struct mtd_dev_s *dev, int cmd,
unsigned long arg);
@@ -573,33 +572,6 @@ out:
}
#endif
-/****************************************************************************
- * Name: rpmsgmtd_ioctl_arglen
- *
- * Description:
- * Get rpmsg mtd ioctl argument length according to the command
- *
- * Parameters:
- * cmd - the ioctl command
- *
- * Returned Values:
- * 0 - ioctl command not support
- * positive - the argument length
- *
- ****************************************************************************/
-
-static ssize_t rpmsgmtd_ioctl_arglen(int cmd)
-{
- switch (cmd)
- {
- case MTDIOC_PROTECT:
- case MTDIOC_UNPROTECT:
- return sizeof(struct mtd_protect_s);
- default:
- return -ENOTTY;
- }
-}
-
/****************************************************************************
* Name: rpmsgmtd_ioctl
*
@@ -636,20 +608,27 @@ static int rpmsgmtd_ioctl(FAR struct mtd_dev_s *dev, int
cmd,
/* Call our internal routine to perform the ioctl */
- arglen = rpmsgmtd_ioctl_arglen(cmd);
+ arglen = ioctl_arglen(cmd, arg);
if (arglen < 0)
{
return arglen;
}
- msglen = sizeof(*msg) + arglen - 1;
-
msg = rpmsgmtd_get_tx_payload_buffer(priv, &space);
if (msg == NULL)
{
return -ENOMEM;
}
+ if (space < sizeof(*msg) - 1 ||
+ arglen > space - (sizeof(*msg) - 1))
+ {
+ rpmsg_release_tx_buffer(&priv->ept, msg);
+ return -EMSGSIZE;
+ }
+
+ msglen = sizeof(*msg) - 1 + arglen;
+
msg->request = cmd;
msg->arg = arg;
msg->arglen = arglen;
diff --git a/fs/rpmsgfs/rpmsgfs_client.c b/fs/rpmsgfs/rpmsgfs_client.c
index e2687bf2ee8..5ef81ef9ced 100644
--- a/fs/rpmsgfs/rpmsgfs_client.c
+++ b/fs/rpmsgfs/rpmsgfs_client.c
@@ -493,31 +493,6 @@ fail:
return ret;
}
-static ssize_t rpmsgfs_ioctl_arglen(int cmd)
-{
- switch (cmd)
- {
- case FIONBIO:
- case FIONWRITE:
- case FIONREAD:
- return sizeof(int);
- case FIOC_FILEPATH:
- return PATH_MAX;
- case TCDRN:
- case TCFLSH:
- return 0;
- case TCGETS:
- case TCSETS:
- return sizeof(struct termios);
- case FIOC_SETLK:
- case FIOC_GETLK:
- case FIOC_SETLKW:
- return sizeof(struct flock);
- default:
- return -ENOTTY;
- }
-}
-
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -699,7 +674,7 @@ off_t rpmsgfs_client_lseek(FAR void *handle, int fd,
int rpmsgfs_client_ioctl(FAR void *handle, int fd,
int request, unsigned long arg)
{
- ssize_t arglen = rpmsgfs_ioctl_arglen(request);
+ ssize_t arglen = ioctl_arglen(request, arg);
FAR struct rpmsgfs_s *priv = handle;
FAR struct rpmsgfs_ioctl_s *msg;
uint32_t space;
@@ -710,15 +685,19 @@ int rpmsgfs_client_ioctl(FAR void *handle, int fd,
return arglen;
}
- len = sizeof(*msg) + arglen;
-
msg = rpmsgfs_get_tx_payload_buffer(priv, &space);
if (msg == NULL)
{
return -ENOMEM;
}
- DEBUGASSERT(len <= space);
+ if (space < sizeof(*msg) || arglen > space - sizeof(*msg))
+ {
+ rpmsg_release_tx_buffer(&priv->ept, msg);
+ return -EMSGSIZE;
+ }
+
+ len = sizeof(*msg) + arglen;
msg->fd = fd;
msg->request = request;
diff --git a/fs/vfs/fs_ioctl.c b/fs/vfs/fs_ioctl.c
index 35d98a2e0f6..464efd7d6c6 100644
--- a/fs/vfs/fs_ioctl.c
+++ b/fs/vfs/fs_ioctl.c
@@ -31,6 +31,20 @@
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
+#include <limits.h>
+#include <net/if.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <termios.h>
+
+#include <nuttx/audio/audio.h>
+#include <nuttx/drivers/rpmsgdev.h>
+#include <nuttx/input/mouse.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/net/ioctl.h>
+#include <nuttx/power/battery_ioctl.h>
+#include <nuttx/video/fb.h>
#include "inode/inode.h"
#include "vfs.h"
@@ -39,6 +53,20 @@
* Private Functions
****************************************************************************/
+/****************************************************************************
+ * Name: ioctl_arglen_add
+ ****************************************************************************/
+
+static ssize_t ioctl_arglen_add(size_t header, size_t data)
+{
+ if (data > SSIZE_MAX - header)
+ {
+ return -EOVERFLOW;
+ }
+
+ return header + data;
+}
+
/****************************************************************************
* Name: file_vioctl
****************************************************************************/
@@ -257,6 +285,150 @@ static int nx_vioctl(int fd, int req, va_list ap)
* Public Functions
****************************************************************************/
+/****************************************************************************
+ * Name: ioctl_arglen
+ *
+ * Description:
+ * Calculate the size of a contiguous device ioctl argument buffer.
+ *
+ * Input Parameters:
+ * cmd - The ioctl command
+ * arg - The argument at the device ioctl entry point
+ *
+ * Returned Value:
+ * Positive buffer size, zero for a value argument, or a negated errno.
+ *
+ ****************************************************************************/
+
+ssize_t ioctl_arglen(int cmd, unsigned long arg)
+{
+ switch (cmd)
+ {
+ case FIONBIO:
+ case FIONWRITE:
+ case FIONREAD:
+ case FIONSPACE:
+ case FBIOGET_POWER:
+ case BATIOC_CURRENT:
+ case BATIOC_VOLTAGE:
+ case BATIOC_TEMPERATURE:
+ case BATIOC_INPUT_CURRENT:
+ case BATIOC_STATE:
+ case BATIOC_CAPACITY:
+ case BATIOC_CHIPID:
+ case BATIOC_GET_PROTOCOL:
+ return sizeof(int);
+
+ case BATIOC_ONLINE:
+ return sizeof(bool);
+
+ case FIOC_FILEPATH:
+ return PATH_MAX;
+
+ case TUNSETIFF:
+ case TUNGETIFF:
+ return sizeof(struct ifreq);
+
+ case FBIOSET_POWER:
+ case TCDRN:
+ case TCFLSH:
+ case MSIOC_GRAB:
+ case BIOC_PROBE:
+ case BIOC_EJECT:
+ case BIOC_LLFORMAT:
+ case BIOC_ALLOCSECT:
+ case BIOC_FREESECT:
+ case BIOC_FLUSH:
+ case MTDIOC_BULKERASE:
+ return 0;
+
+ case TCGETS:
+ case TCSETS:
+ return sizeof(struct termios);
+
+ case FIOC_SETLK:
+ case FIOC_GETLK:
+ case FIOC_SETLKW:
+ return sizeof(struct flock);
+
+ case BIOC_XIPBASE:
+ return sizeof(uintptr_t);
+
+ case BIOC_PARTINFO:
+ return sizeof(struct partition_info_s);
+
+ case BIOC_BLKSSZGET:
+ return sizeof(blksize_t);
+
+ case MTDIOC_ERASESTATE:
+ return sizeof(uint8_t);
+
+ case MTDIOC_PROTECT:
+ case MTDIOC_UNPROTECT:
+ return sizeof(struct mtd_protect_s);
+
+ case BATIOC_OPERATE:
+ return sizeof(struct batio_operate_msg_s);
+
+ case MSIOC_VENDOR:
+ {
+ FAR const struct mouse_vendor_cmd_s *req =
+ (FAR const struct mouse_vendor_cmd_s *)(uintptr_t)arg;
+
+ if (req == NULL)
+ {
+ return -EINVAL;
+ }
+
+ return ioctl_arglen_add(sizeof(*req), req->len);
+ }
+
+#ifdef CONFIG_DEV_RPMSG
+ case RPMSGDEV_PRIVATE_IOCTL:
+ {
+ FAR const struct rpmsgdev_private_ioctl *req =
+ (FAR const struct rpmsgdev_private_ioctl *)(uintptr_t)arg;
+
+ if (req == NULL)
+ {
+ return -EINVAL;
+ }
+
+ return ioctl_arglen_add(sizeof(*req), req->len);
+ }
+#endif
+
+#ifdef CONFIG_AUDIO
+ case AUDIOIOC_HWRESET:
+ return 0;
+
+ case AUDIOIOC_GETBUFFERINFO:
+ case AUDIOIOC_SETBUFFERINFO:
+ return sizeof(struct ap_buffer_info_s);
+
+ case AUDIOIOC_GETAUDIOINFO:
+ return sizeof(struct audio_info_s);
+
+ case AUDIOIOC_SETPARAMTER:
+ {
+ size_t len;
+
+ if (arg == 0)
+ {
+ return -EINVAL;
+ }
+
+ len = strnlen((FAR const char *)(uintptr_t)arg, SSIZE_MAX);
+ return ioctl_arglen_add(1, len);
+ }
+
+#endif
+
+ default:
+ return -ENOTTY;
+ }
+}
+
/****************************************************************************
* Name: file_ioctl
*
diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h
index f94bf529374..94a36f339f1 100644
--- a/include/nuttx/fs/ioctl.h
+++ b/include/nuttx/fs/ioctl.h
@@ -953,6 +953,29 @@ extern "C"
* Public Function Prototypes
****************************************************************************/
+/****************************************************************************
+ * Name: ioctl_arglen
+ *
+ * Description:
+ * Get the contiguous argument buffer size at the device ioctl entry point.
+ * Requests requiring nested pointer marshalling are not supported.
+ *
+ * Input Parameters:
+ * cmd - The ioctl command
+ * arg - The ioctl argument. For variable-size requests, the caller must
+ * provide an accessible header or a terminated string.
+ *
+ * Returned Value:
+ * >0: arg points to a buffer of this size.
+ * 0: arg is passed by value or ignored, with no buffer to copy.
+ * <0: Negated errno for an unsupported command or an invalid size.
+ * The result excludes transport headers and does not validate memory
+ * accessibility or guarantee that a particular transport supports cmd.
+ *
+ ****************************************************************************/
+
+ssize_t ioctl_arglen(int cmd, unsigned long arg);
+
#ifdef __cplusplus
}
#endif