Copilot commented on code in PR #21779:
URL: https://github.com/apache/nuttx/pull/21779#discussion_r4564249605
##########
fs/nfs/nfs_util.c:
##########
@@ -101,6 +103,245 @@ static inline int nfs_pathsegment(FAR const char **path,
FAR char *buffer,
}
}
+#ifdef CONFIG_FS_LINKS
+/****************************************************************************
+ * Name: nfs_readlink_internal
+ *
+ * Description:
+ * Read the target of a symbolic link.
+ *
+ * Input Parameters:
+ * nmp - NFS mount point
+ * fhandle - File handle of the symbolic link
+ * buffer - Buffer to store the link target
+ * bufsize - Size of the buffer
+ *
+ * Returned Value:
+ * On success, returns the length of the link target stored in 'buffer'.
+ * On failure, returns a negative errno value.
+ *
+ ****************************************************************************/
+
+int nfs_readlink_internal(FAR struct nfsmount *nmp,
+ FAR struct file_handle *fhandle,
+ FAR char *buffer, size_t bufsize)
+{
+ FAR struct rpc_reply_readlink *readlink;
+ FAR uint32_t *ptr;
+ uint32_t length;
+ int reqlen;
+ int ret;
+
+ /* Create the READLINK RPC call arguments */
+
+ ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.readlink.readlink;
+ reqlen = 0;
+
+ /* Copy the variable-length file handle */
+
+ *ptr++ = txdr_unsigned(fhandle->length);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, &fhandle->handle, fhandle->length);
+ reqlen += uint32_alignup(fhandle->length);
+
+ /* Perform the READLINK RPC */
+
+ nfs_statistics(NFSPROC_READLINK);
+ ret = nfs_request(nmp, NFSPROC_READLINK,
+ &nmp->nm_msgbuffer.readlink, reqlen,
+ nmp->nm_iobuffer, nmp->nm_buflen);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_request failed: %d\n", ret);
+ return ret;
+ }
+
+ /* Parse the response */
+
+ readlink = (FAR struct rpc_reply_readlink *)nmp->nm_iobuffer;
+ ptr = (FAR uint32_t *)&readlink->readlink;
+
+ /* Skip attributes if present */
+
+ if (*ptr++)
+ {
+ ptr += sizeof(struct nfs_fattr) / sizeof(uint32_t);
+ }
+
+ /* Get the target path length */
+
+ length = fxdr_unsigned(uint32_t, *ptr);
+ ptr++;
+
+ /* POSIX readlink() behavior: if buffer is too small, truncate the result
+ * and return the number of bytes placed in buffer (not an error).
+ */
+
+ if (length > bufsize)
+ {
+ length = bufsize;
+ }
+
+ /* Copy the target path */
+
+ memcpy(buffer, ptr, length);
+ return length;
+}
+
+/****************************************************************************
+ * Name: nfs_followlink
+ *
+ * Description:
+ * Follow a symbolic link to its ultimate target. This function resolves
+ * a symbolic link by reading its target and resolving the path from
+ * the containing directory.
+ *
+ * Input Parameters:
+ * nmp - NFS mount point
+ * dir_fhandle - File handle of the directory containing the symlink
+ * fhandle - File handle of the symbolic link (input/output)
+ * obj_attr - Object attributes (input/output)
+ * symloop_count - Pointer to symlink resolution depth counter
+ *
+ * Returned Value:
+ * Zero on success; a negative errno value on failure.
+ *
+ ****************************************************************************/
+
+static int nfs_followlink(FAR struct nfsmount *nmp,
+ FAR struct file_handle *dir_fhandle,
+ FAR struct file_handle *fhandle,
+ FAR struct nfs_fattr *obj_attr,
+ FAR int *symloop_count)
+{
+ struct file_handle parent_fhandle;
+ struct file_handle tmphandle;
+ FAR const char *linkptr;
+ FAR char *linkpath;
+ FAR char *buffer;
+ char terminator;
+ uint32_t tmp;
+ int ret;
+
+ /* Limit the number of symbolic links that we pass through */
+
+ if (*symloop_count >= SYMLOOP_MAX)
+ {
+ ferr("ERROR: Too many symbolic links (loop?)\n");
+ return -ELOOP;
+ }
+
+ (*symloop_count)++;
+ linkpath = lib_get_tempbuffer(PATH_MAX);
+ if (linkpath == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for symlink path\n");
+ return -ENOMEM;
+ }
+
+ buffer = lib_get_tempbuffer(NAME_MAX + 1);
+ if (buffer == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for path segment\n");
+ lib_put_tempbuffer(linkpath);
+ return -ENOMEM;
+ }
+
+ /* Read the symlink target */
+
+ ret = nfs_readlink_internal(nmp, fhandle, linkpath, PATH_MAX - 1);
+ if (ret < 0)
+ {
+ goto errout;
+ }
+
+ linkpath[ret] = '\0';
+
+ /* Our fs can only resolve relative path symbolic links.
+ * Absolute paths are not supported.
+ */
+
+ if (linkpath[0] == '/')
+ {
+ ret = -ENOSYS;
+ goto errout;
+ }
Review Comment:
Symlink resolution currently rejects only absolute targets; relative targets
containing `..` components can traverse upward via server-side lookup (and may
escape the export/mount root depending on server behavior), which conflicts
with the PR description stating targets are resolved inside the mount. If the
intent is to enforce 'stay within mount', explicitly reject `..` path
components during `nfs_followlink()` (or otherwise constrain upward traversal
using the mount root handle).
##########
fs/nfs/nfs_util.c:
##########
@@ -101,6 +103,245 @@ static inline int nfs_pathsegment(FAR const char **path,
FAR char *buffer,
}
}
+#ifdef CONFIG_FS_LINKS
+/****************************************************************************
+ * Name: nfs_readlink_internal
+ *
+ * Description:
+ * Read the target of a symbolic link.
+ *
+ * Input Parameters:
+ * nmp - NFS mount point
+ * fhandle - File handle of the symbolic link
+ * buffer - Buffer to store the link target
+ * bufsize - Size of the buffer
+ *
+ * Returned Value:
+ * On success, returns the length of the link target stored in 'buffer'.
+ * On failure, returns a negative errno value.
+ *
+ ****************************************************************************/
+
+int nfs_readlink_internal(FAR struct nfsmount *nmp,
+ FAR struct file_handle *fhandle,
+ FAR char *buffer, size_t bufsize)
+{
+ FAR struct rpc_reply_readlink *readlink;
+ FAR uint32_t *ptr;
+ uint32_t length;
+ int reqlen;
+ int ret;
+
+ /* Create the READLINK RPC call arguments */
+
+ ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.readlink.readlink;
+ reqlen = 0;
+
+ /* Copy the variable-length file handle */
+
+ *ptr++ = txdr_unsigned(fhandle->length);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, &fhandle->handle, fhandle->length);
+ reqlen += uint32_alignup(fhandle->length);
+
+ /* Perform the READLINK RPC */
+
+ nfs_statistics(NFSPROC_READLINK);
+ ret = nfs_request(nmp, NFSPROC_READLINK,
+ &nmp->nm_msgbuffer.readlink, reqlen,
+ nmp->nm_iobuffer, nmp->nm_buflen);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_request failed: %d\n", ret);
+ return ret;
+ }
+
+ /* Parse the response */
+
+ readlink = (FAR struct rpc_reply_readlink *)nmp->nm_iobuffer;
+ ptr = (FAR uint32_t *)&readlink->readlink;
+
+ /* Skip attributes if present */
+
+ if (*ptr++)
+ {
+ ptr += sizeof(struct nfs_fattr) / sizeof(uint32_t);
+ }
+
+ /* Get the target path length */
+
+ length = fxdr_unsigned(uint32_t, *ptr);
+ ptr++;
+
+ /* POSIX readlink() behavior: if buffer is too small, truncate the result
+ * and return the number of bytes placed in buffer (not an error).
+ */
+
+ if (length > bufsize)
+ {
+ length = bufsize;
+ }
+
+ /* Copy the target path */
+
+ memcpy(buffer, ptr, length);
+ return length;
+}
+
+/****************************************************************************
+ * Name: nfs_followlink
+ *
+ * Description:
+ * Follow a symbolic link to its ultimate target. This function resolves
+ * a symbolic link by reading its target and resolving the path from
+ * the containing directory.
+ *
+ * Input Parameters:
+ * nmp - NFS mount point
+ * dir_fhandle - File handle of the directory containing the symlink
+ * fhandle - File handle of the symbolic link (input/output)
+ * obj_attr - Object attributes (input/output)
+ * symloop_count - Pointer to symlink resolution depth counter
+ *
+ * Returned Value:
+ * Zero on success; a negative errno value on failure.
+ *
+ ****************************************************************************/
+
+static int nfs_followlink(FAR struct nfsmount *nmp,
+ FAR struct file_handle *dir_fhandle,
+ FAR struct file_handle *fhandle,
+ FAR struct nfs_fattr *obj_attr,
+ FAR int *symloop_count)
+{
+ struct file_handle parent_fhandle;
+ struct file_handle tmphandle;
+ FAR const char *linkptr;
+ FAR char *linkpath;
+ FAR char *buffer;
+ char terminator;
+ uint32_t tmp;
+ int ret;
+
+ /* Limit the number of symbolic links that we pass through */
+
+ if (*symloop_count >= SYMLOOP_MAX)
+ {
+ ferr("ERROR: Too many symbolic links (loop?)\n");
+ return -ELOOP;
+ }
+
+ (*symloop_count)++;
+ linkpath = lib_get_tempbuffer(PATH_MAX);
+ if (linkpath == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for symlink path\n");
+ return -ENOMEM;
+ }
+
+ buffer = lib_get_tempbuffer(NAME_MAX + 1);
+ if (buffer == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for path segment\n");
+ lib_put_tempbuffer(linkpath);
+ return -ENOMEM;
+ }
+
+ /* Read the symlink target */
+
+ ret = nfs_readlink_internal(nmp, fhandle, linkpath, PATH_MAX - 1);
+ if (ret < 0)
+ {
+ goto errout;
+ }
+
+ linkpath[ret] = '\0';
+
+ /* Our fs can only resolve relative path symbolic links.
+ * Absolute paths are not supported.
+ */
+
+ if (linkpath[0] == '/')
+ {
+ ret = -ENOSYS;
+ goto errout;
+ }
+
+ memcpy(&tmphandle, dir_fhandle, sizeof(tmphandle));
+
+ /* Process each path component of the symlink target */
+
+ linkptr = linkpath;
+ for (; ; )
+ {
+ /* Set the parent directory which we will use for followlink
+ * in this iteration.
+ */
+
+ memcpy(&parent_fhandle, &tmphandle, sizeof(parent_fhandle));
+
+ /* Extract the next path component */
+
+ ret = nfs_pathsegment(&linkptr, buffer, &terminator);
Review Comment:
Symlink resolution currently rejects only absolute targets; relative targets
containing `..` components can traverse upward via server-side lookup (and may
escape the export/mount root depending on server behavior), which conflicts
with the PR description stating targets are resolved inside the mount. If the
intent is to enforce 'stay within mount', explicitly reject `..` path
components during `nfs_followlink()` (or otherwise constrain upward traversal
using the mount root handle).
##########
fs/nfs/nfs_vfsops.c:
##########
@@ -3026,3 +3047,363 @@ static int nfs_chstat(FAR struct inode *mountpt, FAR
const char *relpath,
nxmutex_unlock(&nmp->nm_lock);
return ret;
}
+
+#ifdef CONFIG_FS_LINKS
+/****************************************************************************
+ * Name: nfs_symlink
+ *
+ * Description:
+ * Create a symbolic link at 'relpath' pointing to 'target'
+ *
+ * Returned Value:
+ * 0 on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int nfs_symlink(FAR struct inode *mountpt,
+ FAR const char *target, FAR const char *relpath)
+{
+ FAR struct nfsmount *nmp;
+ struct file_handle fhandle;
+ FAR char *filename;
+ struct nfs_fattr fattr;
+ FAR uint32_t *ptr;
+ int namelen;
+ int reqlen;
+ int ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(mountpt && mountpt->i_private && target && relpath);
+
+ /* Get the mountpoint private data from the inode structure */
+
+ nmp = mountpt->i_private;
+
+ /* Allocate buffer for filename */
+
+ filename = lib_get_tempbuffer(NAME_MAX + 1);
+ if (filename == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for filename\n");
+ return -ENOMEM;
+ }
+
+ ret = nxmutex_lock(&nmp->nm_lock);
+ if (ret < 0)
+ {
+ goto errout_with_filename;
+ }
+
+ /* Find the parent directory of the symlink */
+
+ ret = nfs_finddir(nmp, relpath, &fhandle, &fattr, filename);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_finddir failed: %d\n", ret);
+ goto errout_with_lock;
+ }
+
+ /* Create the SYMLINK RPC call arguments */
+
+ ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.symlink.symlink;
+ reqlen = 0;
+
+ /* Copy the variable length, directory file handle */
+
+ *ptr++ = txdr_unsigned(fhandle.length);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, &fhandle.handle, fhandle.length);
+ reqlen += uint32_alignup(fhandle.length);
+ ptr += uint32_increment(fhandle.length);
+
+ /* Copy the variable-length symlink name */
+
+ namelen = strlen(filename);
+ *ptr++ = txdr_unsigned(namelen);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, filename, namelen);
+ ptr += uint32_increment(namelen);
+ reqlen += uint32_alignup(namelen);
+
+ /* Set symlink attributes - mode 0777 */
+
+ *ptr++ = nfs_true;
+ *ptr++ = txdr_unsigned(0777);
+ reqlen += 2 * sizeof(uint32_t);
+
+ /* Other attributes: don't set */
+
+ *ptr++ = nfs_false;
+ *ptr++ = nfs_false;
+ *ptr++ = nfs_false;
+ *ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE);
+ *ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE);
+ reqlen += 5 * sizeof(uint32_t);
+
+ /* Copy the target path */
+
+ namelen = strlen(target);
+ *ptr++ = txdr_unsigned(namelen);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, target, namelen);
+ reqlen += uint32_alignup(namelen);
+
+ /* Perform the SYMLINK RPC */
+
+ nfs_statistics(NFSPROC_SYMLINK);
+ ret = nfs_request(nmp, NFSPROC_SYMLINK,
+ &nmp->nm_msgbuffer.symlink, reqlen,
+ nmp->nm_iobuffer, nmp->nm_buflen);
+
+errout_with_lock:
+ nxmutex_unlock(&nmp->nm_lock);
+
+errout_with_filename:
+ lib_put_tempbuffer(filename);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: nfs_readlink
+ *
+ * Description:
+ * Read the target of a symbolic link
+ *
+ * Returned Value:
+ * The number of bytes placed in the buffer on success; a negated errno
+ * value on failure.
+ *
+ ****************************************************************************/
+
+static ssize_t nfs_readlink(FAR struct inode *mountpt,
+ FAR const char *relpath,
+ FAR char *buf, size_t bufsize)
+{
+ FAR struct nfsmount *nmp;
+ struct file_handle fhandle;
+ struct nfs_fattr attributes;
+ int ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(mountpt && mountpt->i_private);
+
+ /* Get the mountpoint private data from the inode structure */
+
+ nmp = mountpt->i_private;
+ DEBUGASSERT(nmp && buf);
+
+ ret = nxmutex_lock(&nmp->nm_lock);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* Find the symlink (don't follow it) */
+
+ ret = nfs_findnode(nmp, relpath, &fhandle, &attributes, NULL, true);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_findnode failed: %d\n", ret);
+ goto errout_with_lock;
+ }
+
+ /* Read the symlink target using the internal helper */
+
+ ret = nfs_readlink_internal(nmp, &fhandle, buf, bufsize);
+
+errout_with_lock:
+ nxmutex_unlock(&nmp->nm_lock);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: nfs_link
+ *
+ * Description:
+ * Create a hard link at 'relpath2' pointing to 'relpath1'
+ *
+ * Returned Value:
+ * 0 on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int nfs_link(FAR struct inode *mountpt,
+ FAR const char *relpath1, FAR const char *relpath2)
+{
+ FAR struct nfsmount *nmp;
+ struct file_handle target_fhandle;
+ struct file_handle link_fhandle;
+ struct nfs_fattr fattr;
+ FAR char *filename;
+ FAR uint32_t *ptr;
+ int namelen;
+ int reqlen;
+ int ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(mountpt && mountpt->i_private);
+
+ /* Get the mountpoint private data from the inode structure */
+
+ nmp = mountpt->i_private;
+ DEBUGASSERT(nmp);
+
+ /* Allocate buffer for filename */
+
+ filename = lib_get_tempbuffer(NAME_MAX + 1);
+ if (filename == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for filename\n");
+ return -ENOMEM;
+ }
+
+ ret = nxmutex_lock(&nmp->nm_lock);
+ if (ret < 0)
+ {
+ goto errout_with_filename;
+ }
+
+ /* Find the target file */
+
+ ret = nfs_findnode(nmp, relpath1, &target_fhandle, &fattr, NULL, false);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_findnode (relpath1) failed: %d\n", ret);
+ goto errout_with_lock;
+ }
+
+ /* Find the parent directory of the link */
+
+ ret = nfs_finddir(nmp, relpath2, &link_fhandle, &fattr, filename);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_finddir (relpath2) failed: %d\n", ret);
+ goto errout_with_lock;
+ }
+
+ /* Create the LINK RPC call arguments */
+
+ ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.link.link;
+ reqlen = 0;
+
+ /* Copy the target file handle */
+
+ *ptr++ = txdr_unsigned(target_fhandle.length);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, &target_fhandle.handle, target_fhandle.length);
+ reqlen += uint32_alignup(target_fhandle.length);
+ ptr += uint32_increment(target_fhandle.length);
+
+ /* Copy the link directory file handle */
+
+ *ptr++ = txdr_unsigned(link_fhandle.length);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, &link_fhandle.handle, link_fhandle.length);
+ reqlen += uint32_alignup(link_fhandle.length);
+ ptr += uint32_increment(link_fhandle.length);
+
+ /* Copy the link name */
+
+ namelen = strlen(filename);
+ *ptr++ = txdr_unsigned(namelen);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, filename, namelen);
+ reqlen += uint32_alignup(namelen);
+
+ /* Perform the LINK RPC */
+
+ nfs_statistics(NFSPROC_LINK);
+ ret = nfs_request(nmp, NFSPROC_LINK,
+ &nmp->nm_msgbuffer.link, reqlen,
+ nmp->nm_iobuffer, nmp->nm_buflen);
+
+errout_with_lock:
+ nxmutex_unlock(&nmp->nm_lock);
+
+errout_with_filename:
+ lib_put_tempbuffer(filename);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: nfs_lstat
+ *
+ * Description:
+ * Get file status without following symbolic links
+ *
+ * Returned Value:
+ * 0 on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int nfs_lstat(FAR struct inode *mountpt, FAR const char *relpath,
+ FAR struct stat *buf)
+{
+ FAR struct nfsmount *nmp;
+ struct file_handle fhandle;
+ struct nfs_fattr attributes;
+ struct timespec ts;
+ int ret;
+
+ finfo("relpath=%s\n", relpath);
Review Comment:
This introduces an unconditional info log on every `lstat()` call, which can
be hot and may flood logs / impact performance in real deployments. Consider
removing it or gating it behind a debug-specific config (or using a
lower-frequency trace mechanism consistent with other NFS VFS ops).
##########
fs/nfs/nfs_vfsops.c:
##########
@@ -3026,3 +3047,363 @@ static int nfs_chstat(FAR struct inode *mountpt, FAR
const char *relpath,
nxmutex_unlock(&nmp->nm_lock);
return ret;
}
+
+#ifdef CONFIG_FS_LINKS
+/****************************************************************************
+ * Name: nfs_symlink
+ *
+ * Description:
+ * Create a symbolic link at 'relpath' pointing to 'target'
+ *
+ * Returned Value:
+ * 0 on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int nfs_symlink(FAR struct inode *mountpt,
+ FAR const char *target, FAR const char *relpath)
+{
+ FAR struct nfsmount *nmp;
+ struct file_handle fhandle;
+ FAR char *filename;
+ struct nfs_fattr fattr;
+ FAR uint32_t *ptr;
+ int namelen;
+ int reqlen;
+ int ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(mountpt && mountpt->i_private && target && relpath);
+
+ /* Get the mountpoint private data from the inode structure */
+
+ nmp = mountpt->i_private;
+
+ /* Allocate buffer for filename */
+
+ filename = lib_get_tempbuffer(NAME_MAX + 1);
+ if (filename == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for filename\n");
+ return -ENOMEM;
+ }
+
+ ret = nxmutex_lock(&nmp->nm_lock);
+ if (ret < 0)
+ {
+ goto errout_with_filename;
+ }
+
+ /* Find the parent directory of the symlink */
+
+ ret = nfs_finddir(nmp, relpath, &fhandle, &fattr, filename);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_finddir failed: %d\n", ret);
+ goto errout_with_lock;
+ }
+
+ /* Create the SYMLINK RPC call arguments */
+
+ ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.symlink.symlink;
+ reqlen = 0;
+
+ /* Copy the variable length, directory file handle */
+
+ *ptr++ = txdr_unsigned(fhandle.length);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, &fhandle.handle, fhandle.length);
+ reqlen += uint32_alignup(fhandle.length);
+ ptr += uint32_increment(fhandle.length);
+
+ /* Copy the variable-length symlink name */
+
+ namelen = strlen(filename);
+ *ptr++ = txdr_unsigned(namelen);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, filename, namelen);
+ ptr += uint32_increment(namelen);
+ reqlen += uint32_alignup(namelen);
+
+ /* Set symlink attributes - mode 0777 */
+
+ *ptr++ = nfs_true;
+ *ptr++ = txdr_unsigned(0777);
+ reqlen += 2 * sizeof(uint32_t);
+
+ /* Other attributes: don't set */
+
+ *ptr++ = nfs_false;
+ *ptr++ = nfs_false;
+ *ptr++ = nfs_false;
+ *ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE);
+ *ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE);
+ reqlen += 5 * sizeof(uint32_t);
+
+ /* Copy the target path */
+
+ namelen = strlen(target);
+ *ptr++ = txdr_unsigned(namelen);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, target, namelen);
+ reqlen += uint32_alignup(namelen);
Review Comment:
The SYMLINK target length is not bounded before copying into
`nm_msgbuffer.symlink`, so a long `target` can overrun the request buffer
(memory corruption). Add an explicit maximum check (e.g., against
`NFS_MAXPATHLEN` and/or `sizeof(nmp->nm_msgbuffer.symlink)` minus
header/fields) and fail with a suitable errno like `-ENAMETOOLONG`.
##########
fs/nfs/nfs_util.c:
##########
@@ -101,6 +103,245 @@ static inline int nfs_pathsegment(FAR const char **path,
FAR char *buffer,
}
}
+#ifdef CONFIG_FS_LINKS
+/****************************************************************************
+ * Name: nfs_readlink_internal
+ *
+ * Description:
+ * Read the target of a symbolic link.
+ *
+ * Input Parameters:
+ * nmp - NFS mount point
+ * fhandle - File handle of the symbolic link
+ * buffer - Buffer to store the link target
+ * bufsize - Size of the buffer
+ *
+ * Returned Value:
+ * On success, returns the length of the link target stored in 'buffer'.
+ * On failure, returns a negative errno value.
+ *
+ ****************************************************************************/
+
+int nfs_readlink_internal(FAR struct nfsmount *nmp,
+ FAR struct file_handle *fhandle,
+ FAR char *buffer, size_t bufsize)
+{
+ FAR struct rpc_reply_readlink *readlink;
+ FAR uint32_t *ptr;
+ uint32_t length;
+ int reqlen;
+ int ret;
+
+ /* Create the READLINK RPC call arguments */
+
+ ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.readlink.readlink;
+ reqlen = 0;
+
+ /* Copy the variable-length file handle */
+
+ *ptr++ = txdr_unsigned(fhandle->length);
+ reqlen += sizeof(uint32_t);
+
+ memcpy(ptr, &fhandle->handle, fhandle->length);
+ reqlen += uint32_alignup(fhandle->length);
+
+ /* Perform the READLINK RPC */
+
+ nfs_statistics(NFSPROC_READLINK);
+ ret = nfs_request(nmp, NFSPROC_READLINK,
+ &nmp->nm_msgbuffer.readlink, reqlen,
+ nmp->nm_iobuffer, nmp->nm_buflen);
+ if (ret != OK)
+ {
+ ferr("ERROR: nfs_request failed: %d\n", ret);
+ return ret;
+ }
+
+ /* Parse the response */
+
+ readlink = (FAR struct rpc_reply_readlink *)nmp->nm_iobuffer;
+ ptr = (FAR uint32_t *)&readlink->readlink;
+
+ /* Skip attributes if present */
+
+ if (*ptr++)
+ {
+ ptr += sizeof(struct nfs_fattr) / sizeof(uint32_t);
+ }
+
+ /* Get the target path length */
+
+ length = fxdr_unsigned(uint32_t, *ptr);
+ ptr++;
+
+ /* POSIX readlink() behavior: if buffer is too small, truncate the result
+ * and return the number of bytes placed in buffer (not an error).
+ */
+
+ if (length > bufsize)
+ {
+ length = bufsize;
+ }
+
+ /* Copy the target path */
+
+ memcpy(buffer, ptr, length);
+ return length;
+}
+
+/****************************************************************************
+ * Name: nfs_followlink
+ *
+ * Description:
+ * Follow a symbolic link to its ultimate target. This function resolves
+ * a symbolic link by reading its target and resolving the path from
+ * the containing directory.
+ *
+ * Input Parameters:
+ * nmp - NFS mount point
+ * dir_fhandle - File handle of the directory containing the symlink
+ * fhandle - File handle of the symbolic link (input/output)
+ * obj_attr - Object attributes (input/output)
+ * symloop_count - Pointer to symlink resolution depth counter
+ *
+ * Returned Value:
+ * Zero on success; a negative errno value on failure.
+ *
+ ****************************************************************************/
+
+static int nfs_followlink(FAR struct nfsmount *nmp,
+ FAR struct file_handle *dir_fhandle,
+ FAR struct file_handle *fhandle,
+ FAR struct nfs_fattr *obj_attr,
+ FAR int *symloop_count)
+{
+ struct file_handle parent_fhandle;
+ struct file_handle tmphandle;
+ FAR const char *linkptr;
+ FAR char *linkpath;
+ FAR char *buffer;
+ char terminator;
+ uint32_t tmp;
+ int ret;
+
+ /* Limit the number of symbolic links that we pass through */
+
+ if (*symloop_count >= SYMLOOP_MAX)
+ {
+ ferr("ERROR: Too many symbolic links (loop?)\n");
+ return -ELOOP;
+ }
+
+ (*symloop_count)++;
+ linkpath = lib_get_tempbuffer(PATH_MAX);
+ if (linkpath == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for symlink path\n");
+ return -ENOMEM;
+ }
+
+ buffer = lib_get_tempbuffer(NAME_MAX + 1);
+ if (buffer == NULL)
+ {
+ ferr("ERROR: Failed to allocate buffer for path segment\n");
+ lib_put_tempbuffer(linkpath);
+ return -ENOMEM;
+ }
+
+ /* Read the symlink target */
+
+ ret = nfs_readlink_internal(nmp, fhandle, linkpath, PATH_MAX - 1);
+ if (ret < 0)
+ {
+ goto errout;
+ }
+
+ linkpath[ret] = '\0';
+
+ /* Our fs can only resolve relative path symbolic links.
+ * Absolute paths are not supported.
+ */
+
+ if (linkpath[0] == '/')
+ {
+ ret = -ENOSYS;
+ goto errout;
+ }
+
+ memcpy(&tmphandle, dir_fhandle, sizeof(tmphandle));
+
+ /* Process each path component of the symlink target */
+
+ linkptr = linkpath;
+ for (; ; )
+ {
+ /* Set the parent directory which we will use for followlink
+ * in this iteration.
+ */
+
+ memcpy(&parent_fhandle, &tmphandle, sizeof(parent_fhandle));
+
+ /* Extract the next path component */
+
+ ret = nfs_pathsegment(&linkptr, buffer, &terminator);
+ if (ret != OK)
+ {
+ goto errout;
+ }
+
+ /* Lookup this component */
+
+ ret = nfs_lookup(nmp, buffer, &tmphandle, obj_attr, NULL);
Review Comment:
Symlink resolution currently rejects only absolute targets; relative targets
containing `..` components can traverse upward via server-side lookup (and may
escape the export/mount root depending on server behavior), which conflicts
with the PR description stating targets are resolved inside the mount. If the
intent is to enforce 'stay within mount', explicitly reject `..` path
components during `nfs_followlink()` (or otherwise constrain upward traversal
using the mount root handle).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]