From e7375adfcf7a11371aa87ef93339f603ceb6ddf5 Mon Sep 17 00:00:00 2001 From: seteiro <276597748+cms19859230182-lang@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:14:20 +0800 Subject: [PATCH] [dfs][nfs] bound READ copy length to request and data_len Fix #11917 nfs_read() used the server-controlled READ3resok.count as the memcpy length without checking it against the request or the decoded data. Clamp each copy to min(server count, args.count, data.data_len), advance buf and the remaining count by the bytes actually copied, and break out when a reply makes no progress so the loop cannot spin. Share DFS_NFS_MAX_MTU (1024) in dfs_nfs.h for both READ/WRITE request caps and the xdr_READ3resok() opaque decode limit, replacing the separate NFS_READ_MAX_DATA definition. In nfs_write(), clamp WRITE3resok.count to the submitted length and advance buf/count/offset only by the accepted bytes so short writes retry the tail correctly and zero-count replies stop the loop. Follow-up for Yunlk's review observations on nfs_read(): - When an RPC or NFS status failure occurs after earlier iterations already copied bytes and advanced fd->offset, keep total and return the bytes copied so far instead of resetting total to 0; only set total = -1 when total == 0. - Remove the stale placeholder comment inside the eof handling branch where fd->eof = TRUE and break are already performed. --- .../dfs/dfs_v1/filesystems/nfs/dfs_nfs.c | 73 +++++++++++++++---- .../dfs/dfs_v1/filesystems/nfs/dfs_nfs.h | 4 + .../dfs/dfs_v1/filesystems/nfs/nfs_xdr.c | 5 +- 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c b/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c index b24e6361845b..be2ce794a5fa 100644 --- a/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c @@ -17,9 +17,9 @@ #include "mount.h" #include "nfs.h" +#include "dfs_nfs.h" -#define NAME_MAX 64 -#define DFS_NFS_MAX_MTU 1024 +#define NAME_MAX 64 #ifdef _WIN32 #define strtok_r strtok_s @@ -555,7 +555,8 @@ int nfs_read(struct dfs_file *file, void *buf, size_t count) { READ3args args; READ3res res; - ssize_t bytes, total = 0; + size_t bytes; + ssize_t total = 0; nfs_file *fd; nfs_filesystem *nfs; @@ -580,33 +581,59 @@ int nfs_read(struct dfs_file *file, void *buf, size_t count) { args.offset = fd->offset; args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count; - count -= args.count; memset(&res, 0, sizeof(res)); if (nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) { rt_kprintf("Read failed\n"); - total = 0; + if (total == 0) + { + total = -1; + } break; } else if (res.status != NFS3_OK) { rt_kprintf("Read failed: %d\n", res.status); - total = 0; + if (total == 0) + { + total = -1; + } break; } else { + /* count and data.data_len are independent reply fields controlled + * by the server; never copy more than requested or decoded. */ bytes = res.READ3res_u.resok.count; - total += bytes; + if (bytes > args.count) + { + bytes = args.count; + } + if (bytes > res.READ3res_u.resok.data.data_len) + { + bytes = res.READ3res_u.resok.data.data_len; + } + + /* No progress and no EOF would keep the loop spinning. */ + if (bytes == 0) + { + if (res.READ3res_u.resok.eof) + { + fd->eof = TRUE; + } + break; + } + + total += (ssize_t)bytes; fd->offset += bytes; /* update current position */ file->pos = fd->offset; memcpy(buf, res.READ3res_u.resok.data.data_val, bytes); - buf = (void *)((char *)buf + args.count); + buf = (void *)((char *)buf + bytes); + count -= bytes; if (res.READ3res_u.resok.eof) { - /* something should probably be here */ fd->eof = TRUE; break; } @@ -624,7 +651,8 @@ int nfs_write(struct dfs_file *file, const void *buf, size_t count) { WRITE3args args; WRITE3res res; - ssize_t bytes, total = 0; + size_t bytes; + ssize_t total = 0; nfs_file *fd; nfs_filesystem *nfs; @@ -651,8 +679,6 @@ int nfs_write(struct dfs_file *file, const void *buf, size_t count) args.data.data_val = (void *)buf; args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count; args.data.data_len = args.count; - count -= args.count; - buf = (const void *)((char *)buf + args.count); if (nfsproc3_write_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) { @@ -669,13 +695,32 @@ int nfs_write(struct dfs_file *file, const void *buf, size_t count) else { bytes = res.WRITE3res_u.resok.count; + /* the server cannot have written more than was sent */ + if (bytes > args.count) + { + bytes = args.count; + } + + /* A short write is legal; advance by the accepted bytes only so + * the unacknowledged tail is retried at the correct offset. */ + if (bytes == 0) + { + break; + } + fd->offset += bytes; - total += bytes; + total += (ssize_t)bytes; /* update current position */ file->pos = fd->offset; /* update file size */ - if (fd->size < fd->offset) fd->size = fd->offset; + if (fd->size < fd->offset) + { + fd->size = fd->offset; + } file->vnode->size = fd->size; + + buf = (const void *)((char *)buf + bytes); + count -= bytes; } xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); } while (count > 0); diff --git a/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.h b/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.h index 460027410da9..82f330177248 100644 --- a/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.h +++ b/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.h @@ -10,6 +10,10 @@ #ifndef __NFS_H__ #define __NFS_H__ +/* Maximum payload in a single NFS READ/WRITE request, also used as the opaque + * data cap when decoding READ replies, so both limits always change together. */ +#define DFS_NFS_MAX_MTU 1024 + int nfs_init(void); #endif diff --git a/components/dfs/dfs_v1/filesystems/nfs/nfs_xdr.c b/components/dfs/dfs_v1/filesystems/nfs/nfs_xdr.c index 2662a17df617..d56d6db8da01 100644 --- a/components/dfs/dfs_v1/filesystems/nfs/nfs_xdr.c +++ b/components/dfs/dfs_v1/filesystems/nfs/nfs_xdr.c @@ -12,6 +12,7 @@ */ #include "nfs.h" +#include "dfs_nfs.h" /* This file is copied from RFC1813 * Copyright 1995 Sun Micrososystems (I assume) */ @@ -723,8 +724,10 @@ xdr_READ3resok(register XDR *xdrs, READ3resok *objp) return (FALSE); if (!xdr_bool(xdrs, &objp->eof)) return (FALSE); - if (!xdr_bytes(xdrs, (char **)&objp->data.data_val, (unsigned int *) &objp->data.data_len, ~0)) + if (!xdr_bytes(xdrs, (char **)&objp->data.data_val, (unsigned int *)&objp->data.data_len, DFS_NFS_MAX_MTU)) + { return (FALSE); + } return (TRUE); }