This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 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 cb626f7168f fs/vfs: validate parameters and preserve 64-bit counts in
sendfile
cb626f7168f is described below
commit cb626f7168fa043c426f27e5474574a042f2a61b
Author: Swatantra Yadav <[email protected]>
AuthorDate: Fri Nov 20 18:17:16 2026 +0530
fs/vfs: validate parameters and preserve 64-bit counts in sendfile
POSIX IEEE Std 1003.1 and Linux specifications (sendfile(2)) parameter
validation, bounds checking, and pointer safety hardening across sendfile:
1. Validate self-transfer (outfd == infd) in sendfile() and file_sendfile():
Linux sendfile(2) explicitly specifies: [EINVAL] out_fd and in_fd
refer to the same file descriptor. Previously, invoking sendfile with
outfd == infd caused copyfile() to read from the descriptor into the
I/O buffer (advancing offset) and write back to the same descriptor
(advancing offset again), resulting in file data corruption or infinite
loops. Returns -EINVAL upfront.
2. Validate negative offset (*offset < 0) in file_sendfile():
Linux sendfile(2) specifies: [EINVAL] offset is not NULL but the
value it points to is negative. Rejects negative offsets upfront with
-EINVAL.
3. Validate file descriptor access modes:
Linux sendfile(2) specifies: [EBADF] The input file was not opened
for reading or the output file was not opened for writing. Validates
that infile is readable ((infile->f_oflags & O_ACCMODE) != O_WRONLY)
and outfile is writable ((outfile->f_oflags & O_ACCMODE) != O_RDONLY),
returning -EBADF.
4. Defensive NULL checks in file_sendfile():
Defensively validates outfile == NULL || infile == NULL returning
-EBADF, and verifies infile->f_inode != NULL before evaluating
!INODE_IS_NOSEEK(infile->f_inode). Eliminates CWE-476 NULL pointer
dereference kernel panics when descriptors lack inodes (e.g. sockets).
5. Clamp count > SSIZE_MAX:
Clamps count to SSIZE_MAX to prevent signed return value overflow.
6. Preserve 64-bit return width on LP64 architectures:
Widens result variable ret from int to ssize_t in sendfile() and
file_sendfile() (psock_sendfile fast path), preventing 32-bit signed
integer truncation and erroneous negative returns on large file
transfers (> 2 GiB) on 64-bit architectures (ARM64, x86_64, RISC-V 64).
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <[email protected]>
---
fs/vfs/fs_sendfile.c | 56 +++++++++++++++++++++++++++++++++++++++++---------
include/nuttx/fs/fs.h | 2 +-
include/sys/sendfile.h | 7 ++++---
3 files changed, 51 insertions(+), 14 deletions(-)
diff --git a/fs/vfs/fs_sendfile.c b/fs/vfs/fs_sendfile.c
index 3c5e2717b16..00dd476d502 100644
--- a/fs/vfs/fs_sendfile.c
+++ b/fs/vfs/fs_sendfile.c
@@ -26,11 +26,13 @@
#include <nuttx/config.h>
-#include <sys/sendfile.h>
-#include <stdbool.h>
#include <errno.h>
-#include <nuttx/debug.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <sys/sendfile.h>
+#include <nuttx/debug.h>
#include <nuttx/fs/fs.h>
#include <nuttx/kmalloc.h>
#include <nuttx/lib/lib.h>
@@ -249,7 +251,7 @@ static ssize_t copyfile(FAR struct file *outfile, FAR
struct file *infile,
* Name: file_sendfile
*
* Description:
- * Equivalent to the standard sendfile function except that is accepts a
+ * Equivalent to the standard sendfile function except that it accepts a
* struct file instance instead of a file descriptor.
*
****************************************************************************/
@@ -257,12 +259,38 @@ static ssize_t copyfile(FAR struct file *outfile, FAR
struct file *infile,
ssize_t file_sendfile(FAR struct file *outfile, FAR struct file *infile,
FAR off_t *offset, size_t count)
{
+ if (outfile == NULL || infile == NULL)
+ {
+ return -EBADF;
+ }
+
+ if (outfile == infile)
+ {
+ return -EINVAL;
+ }
+
+ if ((infile->f_oflags & O_ACCMODE) == O_WRONLY ||
+ (outfile->f_oflags & O_ACCMODE) == O_RDONLY)
+ {
+ return -EBADF;
+ }
+
+ if (offset != NULL && *offset < 0)
+ {
+ return -EINVAL;
+ }
+
if (count == 0)
{
nwarn("WARNING: sendfile count is zero\n");
return 0;
}
+ if (count > SSIZE_MAX)
+ {
+ count = SSIZE_MAX;
+ }
+
#ifdef CONFIG_NET_SENDFILE
/* Check the destination file descriptor: Is it a (probable) file
* descriptor? Check the source file: Is it a normal file?
@@ -276,11 +304,12 @@ ssize_t file_sendfile(FAR struct file *outfile, FAR
struct file *infile,
* cannot seek (a pipe, a FIFO or a socket) uses the slow path.
*/
- if (psock != NULL && !INODE_IS_NOSEEK(infile->f_inode))
+ if (psock != NULL && infile->f_inode != NULL &&
+ !INODE_IS_NOSEEK(infile->f_inode))
{
/* Then let psock_sendfile do the work. */
- int ret = psock_sendfile(psock, infile, offset, count);
+ ssize_t ret = psock_sendfile(psock, infile, offset, count);
if (ret >= 0 || ret != -ENOSYS)
{
@@ -339,10 +368,11 @@ ssize_t file_sendfile(FAR struct file *outfile, FAR
struct file *infile,
* Returned Value:
* If the transfer was successful, the number of bytes written to outfd is
* returned. On error, -1 is returned, and errno is set appropriately.
- * There error values are those returned by read() or write() plus:
+ * These error values are those returned by read() or write() plus:
*
- * EINVAL - Bad input parameters.
- * ENOMEM - Could not allocated an I/O buffer
+ * EBADF - A file descriptor is invalid or has wrong access mode.
+ * EINVAL - Bad input parameters (e.g., negative offset or outfd == infd).
+ * ENOMEM - Could not allocate an I/O buffer.
*
****************************************************************************/
@@ -350,7 +380,13 @@ ssize_t sendfile(int outfd, int infd, FAR off_t *offset,
size_t count)
{
FAR struct file *outfile;
FAR struct file *infile;
- int ret;
+ ssize_t ret;
+
+ if (outfd == infd)
+ {
+ ret = -EINVAL;
+ goto errout;
+ }
ret = file_get(outfd, &outfile);
if (ret < 0)
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index 8ca466e3513..55ea1902b59 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -1612,7 +1612,7 @@ ssize_t file_pwrite(FAR struct file *filep, FAR const
void *buf,
* Name: file_sendfile
*
* Description:
- * Equivalent to the standard sendfile function except that is accepts a
+ * Equivalent to the standard sendfile function except that it accepts a
* struct file instance instead of a file descriptor.
*
****************************************************************************/
diff --git a/include/sys/sendfile.h b/include/sys/sendfile.h
index 06c769b417c..2fd73febd46 100644
--- a/include/sys/sendfile.h
+++ b/include/sys/sendfile.h
@@ -95,10 +95,11 @@ extern "C"
* Returned Value:
* If the transfer was successful, the number of bytes written to outfd is
* returned. On error, -1 is returned, and errno is set appropriately.
- * There error values are those returned by read() or write() plus:
+ * These error values are those returned by read() or write() plus:
*
- * EINVAL - Bad input parameters.
- * ENOMEM - Could not allocated an I/O buffer
+ * EBADF - A file descriptor is invalid or has wrong access mode.
+ * EINVAL - Bad input parameters (e.g., negative offset or outfd == infd).
+ * ENOMEM - Could not allocate an I/O buffer.
*
****************************************************************************/