Skip to contentSkip to conversation

nfsd: reject LAYOUTGET on non-regular files

PATCH Superseded Jiwoong Wi 2 participants 6 messages Nov 30 – Dec 1

Subsystems (3)
lore
Share & tools
lore

Jiwoong Wi

author
#0 lore raw

PATCH 1/1 nfsd: reject LAYOUTGET on non-regular files

LAYOUTGET expects the current filehandle to identify a file, but nfsd4_layoutget() passes a zero type to fh_verify(). An authenticated NFSv4.1 client can therefore request an RW block layout for a directory.

With a nonzero minimum length, the block layout provider asks the filesystem to allocate the requested range. On XFS this can create noncontiguous directory data blocks. A later CREATE can then index beyond the leaf directory bests table and cause a two-byte heap out-of-bounds write.

Check the dentry type after fh_verify() so its security and access checks retain their existing order. Return NFS4ERR_WRONG_TYPE for non-regular files before any layout provider or filesystem callback is invoked.

Validation used an arm64 kernel with 64 KiB pages, Generic KASAN, and lockdep. The full kernel built with GCC 13.3 and CONFIG_WERROR=y; the changed object also built with W=1.
The unpatched three-block directory layout trigger reproduced the out-of-bounds write twice. With this change, directory and symlink LAYOUTGET requests returned NFS4ERR_WRONG_TYPE, while a regular-file LAYOUTGET succeeded. The CREATE following the directory request and NFSv4.1 mount, write/readback, and unmount tests completed without a KASAN, Oops, or panic report.

The patched behavior was confirmed on the nfsd-testing base shown below.

Fixes 9cf514ccfacb ("nfsd: implement pNFS operations")
---
 fs/nfsd/nfs4proc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/nfsd/nfs4proc.c +4 -0
index bb149a3e9a..92a92b3e6d 100644
@@ -3082,6 +3082,10 @@ nfsd4_layoutget(struct svc_rqst *rqstp,
 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
 	if (nfserr)
 		goto out;
+	if (!d_is_reg(current_fh->fh_dentry)) {
+		nfserr = nfserr_wrong_type;
+		goto out;
+	}
 
 	nfserr = nfserr_layoutunavailable;
 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);

base-commit: 36fd4049e3c2bb901118e11be63bf7a7922bea8c

Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw
Quoted 3 lines · Jiwoong Wi

LAYOUTGET expects the current filehandle to identify a file, but nfsd4_layoutget() passes a zero type to fh_verify(). An authenticated NFSv4.1 client can therefore request an RW block layout for a directory.

Before the d_is_reg() check is reached, nfsd4_preprocess_layout_stateid() requires a stateid whose file matches the current filehandle. OPEN cannot produce such a stateid for a directory: an OPEN by name fails nfsd_check_obj_isreg(), and an OPEN by filehandle fails in nfsd_file_acquire_opened(), which verifies with S_IFREG. As far as I can tell, the only stateid that names a directory is a directory delegation from GET_DIR_DELEGATION. The commit message should state that prerequisite.

Quoted 1 line
Fixes 9cf514ccfacb ("nfsd: implement pNFS operations")

On kernels without GET_DIR_DELEGATION, LAYOUTGET on a directory already fails with NFS4ERR_BAD_STATEID at the fh_match() check. The commit that made this reachable is:

Fixes 8b99f6a8c116 ("nfsd: wire up GET_DIR_DELEGATION handling")

While reviewing this patch I found that LAYOUTCOMMIT has the same gap, which does not depend on directory delegations. nfsd4_layoutcommit() also calls fh_verify() with a zero type. When loca_reclaim is set during the grace period, it skips nfsd4_preprocess_layout_stateid() entirely and calls ->proc_layoutcommit() without any stateid.

On XFS with the block or SCSI layout, a reclaim LAYOUTCOMMIT with an empty extent list and loca_last_write_offset inside a leaf directory's leaf block passes xfs_pnfs_validate_isize(), because that block is mapped and written. xfs_fs_commit_blocks() then logs a directory i_disk_size of about 32 GiB. xfs_dir2_format() keys on the last extent, so the directory still reads correctly. The inflated size stops xfs_dir2_shrink_inode() from trimming i_disk_size and weakens the bestcount check in xfs_dir2_leaf_to_node(). xfs_scrub will flag the inode. I haven't uncovered a memory safety consequence. This path dates back to 9cf514ccfacb.

The fix would be the same d_is_reg() check after fh_verify() in nfsd4_layoutcommit().

Please send a v2 that has this patch with the updated Fixes: tag and commit message fixup I mentioned above, and a second patch that addresses this issue in LAYOUTCOMMIT.

Signature · 1 line
Chuck Lever
author
#2 lore raw

PATCH v2 1/2 nfsd: reject LAYOUTGET on non-regular files

In reply to Jiwoong Wi

LAYOUTGET expects the current filehandle to identify a regular file, but nfsd4_layoutget() passes a zero type to fh_verify(). A client can obtain a directory delegation via GET_DIR_DELEGATION and use its stateid with the matching directory filehandle. This passes the filehandle match in nfsd4_preprocess_layout_stateid(), allowing an RW block layout for the directory.

With a nonzero minimum length, the block layout provider asks the filesystem to allocate the requested range. On XFS this can create noncontiguous directory data blocks. A later CREATE can then index beyond the leaf directory bests table and cause a two-byte heap out-of-bounds write.

Check the dentry type after fh_verify() so its security and access checks retain their existing order. Return NFS4ERR_WRONG_TYPE for non-regular files before any layout provider or filesystem callback is invoked.

Validation used an arm64 kernel with 64 KiB pages, Generic KASAN, and lockdep. The full kernel built with GCC 13.3 and CONFIG_WERROR=y; the changed object also built with W=1.
The unpatched three-block directory layout trigger reproduced the out-of-bounds write twice. With this change, directory and symlink LAYOUTGET requests returned NFS4ERR_WRONG_TYPE, while a regular-file LAYOUTGET succeeded. The CREATE following the directory request and NFSv4.1 mount, write/readback, and unmount tests completed without a KASAN, Oops, or panic report.

---
Changes since v1:
- Explain the GET_DIR_DELEGATION prerequisite.
- Correct the Fixes tag.
- Add a separate LAYOUTCOMMIT fix as patch 2/2.

v1: https://lore.kernel.org/r/[email protected]/

 fs/nfsd/nfs4proc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/nfsd/nfs4proc.c +4 -0
index bb149a3e9a..92a92b3e6d 100644
@@ -3082,6 +3082,10 @@ nfsd4_layoutget(struct svc_rqst *rqstp,
 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
 	if (nfserr)
 		goto out;
+	if (!d_is_reg(current_fh->fh_dentry)) {
+		nfserr = nfserr_wrong_type;
+		goto out;
+	}
 
 	nfserr = nfserr_layoutunavailable;
 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);

base-commit: 3c6328ce32970f15f882ff3c6ab1b1648723b000

Signature · 1 line
2.50.1 (Apple Git-155)
author
#3 lore raw

PATCH v2 2/2 nfsd: reject LAYOUTCOMMIT on non-regular files

nfsd4_layoutcommit() passes type 0 to fh_verify(). An NFSv4.1 client with a session and a writable directory filehandle on a pNFS-enabled export can send a reclaim LAYOUTCOMMIT during the server's grace period. The reclaim path skips layout stateid preprocessing, so a fabricated stateid and no prior layout or directory delegation still reach the layout provider. This is not a valid protocol reclaim.

For XFS block and SCSI layouts, an empty extent list and a last-write offset in a mapped directory metadata block can inflate i_disk_size beyond the 32 GiB directory data section. A leaf directory can reach about 32 GiB, and a node directory can reach about 64 GiB. The bad size survives remount; later growth of a leaf directory can trigger XFS corruption detection and filesystem shutdown.

Check d_is_reg() after fh_verify() and before invoking the layout provider. Return NFS4ERR_INVAL for non-regular files, an error permitted for LAYOUTCOMMIT.

An arm64 QEMU server with Generic KASAN reproduced the directory size change from 4096 to 34359742464 bytes. A clean remount retained the bad size, and xfs_repair -n marked the root directory inode's size bad. A subsequent CREATE workload triggered XFS shutdown. A node directory's size also changed from 139264 to 68719480832 bytes. No KASAN report was observed in these runs.

With this change on nfsd-testing 3c6328ce3297, the same LAYOUTCOMMIT returned NFS4ERR_INVAL, the directory size stayed at 4096 bytes, and 48 subsequent CREATE/REMOVE pairs completed without a KASAN or Oops report.

Fixes 9cf514ccfacb ("nfsd: implement pNFS operations")
Fixes e0963ce53b00 ("NFSD: Allow layoutcommit during grace period")
---
 fs/nfsd/nfs4proc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/nfsd/nfs4proc.c +4 -0
index 92a92b3e6d..6c5656e148 100644
@@ -3161,6 +3161,10 @@ nfsd4_layoutcommit(struct svc_rqst *rqstp,
 			   NFSD_MAY_WRITE | NFSD_MAY_OWNER_OVERRIDE);
 	if (nfserr)
 		goto out;
+	if (!d_is_reg(current_fh->fh_dentry)) {
+		nfserr = nfserr_inval;
+		goto out;
+	}
 
 	nfserr = nfserr_layoutunavailable;
 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
Signature · 1 line
2.50.1 (Apple Git-155)
author
#4 lore raw

PATCH v3 1/2 nfsd: reject LAYOUTGET on non-regular files

In reply to Jiwoong Wi

LAYOUTGET expects the current filehandle to identify a regular file, but nfsd4_layoutget() passes a zero type to fh_verify(). A client can obtain a directory delegation via GET_DIR_DELEGATION and use its stateid with the matching directory filehandle. This passes the filehandle match in nfsd4_preprocess_layout_stateid(), allowing an RW block layout for the directory.

With a nonzero minimum length, the block layout provider asks the filesystem to allocate the requested range. On XFS this can create noncontiguous directory data blocks. A later CREATE can then index beyond the leaf directory bests table and cause a two-byte heap out-of-bounds write.

Check the dentry type after fh_verify() so its security and access checks retain their existing order. Return NFS4ERR_WRONG_TYPE for non-regular files before any layout provider or filesystem callback is invoked.

Validation used an arm64 kernel with 64 KiB pages, Generic KASAN, and lockdep. The full kernel built with GCC 13.3 and CONFIG_WERROR=y; the changed object also built with W=1.
The unpatched three-block directory layout trigger reproduced the out-of-bounds write twice. With this change, directory and symlink LAYOUTGET requests returned NFS4ERR_WRONG_TYPE, while a regular-file LAYOUTGET succeeded. The CREATE following the directory request and NFSv4.1 mount, write/readback, and unmount tests completed without a KASAN, Oops, or panic report.

The vulnerability research and validation were conducted by members of the Tobabz team as part of the Best of the Best 15th program.

---
Changes since v2:
- Add the Tobabz team research and validation credit to the commit message.
- No code changes.

Changes since v1:
- Explain the GET_DIR_DELEGATION prerequisite.
- Correct the Fixes tag.
- Add a separate LAYOUTCOMMIT fix as patch 2/2.

v2: https://lore.kernel.org/r/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/

 fs/nfsd/nfs4proc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/nfsd/nfs4proc.c +4 -0
index bb149a3e9a..92a92b3e6d 100644
@@ -3082,6 +3082,10 @@ nfsd4_layoutget(struct svc_rqst *rqstp,
 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
 	if (nfserr)
 		goto out;
+	if (!d_is_reg(current_fh->fh_dentry)) {
+		nfserr = nfserr_wrong_type;
+		goto out;
+	}
 
 	nfserr = nfserr_layoutunavailable;
 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);

base-commit: 3c6328ce32970f15f882ff3c6ab1b1648723b000

Signature · 1 line
2.50.1 (Apple Git-155)
author
#5 lore raw

PATCH v3 2/2 nfsd: reject LAYOUTCOMMIT on non-regular files

nfsd4_layoutcommit() passes type 0 to fh_verify(). An NFSv4.1 client with a session and a writable directory filehandle on a pNFS-enabled export can send a reclaim LAYOUTCOMMIT during the server's grace period. The reclaim path skips layout stateid preprocessing, so a fabricated stateid and no prior layout or directory delegation still reach the layout provider. This is not a valid protocol reclaim.

For XFS block and SCSI layouts, an empty extent list and a last-write offset in a mapped directory metadata block can inflate i_disk_size beyond the 32 GiB directory data section. A leaf directory can reach about 32 GiB, and a node directory can reach about 64 GiB. The bad size survives remount; later growth of a leaf directory can trigger XFS corruption detection and filesystem shutdown.

Check d_is_reg() after fh_verify() and before invoking the layout provider. Return NFS4ERR_INVAL for non-regular files, an error permitted for LAYOUTCOMMIT.

An arm64 QEMU server with Generic KASAN reproduced the directory size change from 4096 to 34359742464 bytes. A clean remount retained the bad size, and xfs_repair -n marked the root directory inode's size bad. A subsequent CREATE workload triggered XFS shutdown. A node directory's size also changed from 139264 to 68719480832 bytes. No KASAN report was observed in these runs.

With this change on nfsd-testing 3c6328ce3297, the same LAYOUTCOMMIT returned NFS4ERR_INVAL, the directory size stayed at 4096 bytes, and 48 subsequent CREATE/REMOVE pairs completed without a KASAN or Oops report.

The vulnerability research and validation were conducted by members of the Tobabz team as part of the Best of the Best 15th program.

Fixes 9cf514ccfacb ("nfsd: implement pNFS operations")
Fixes e0963ce53b00 ("NFSD: Allow layoutcommit during grace period")

---
Changes since v2:
- Add the Tobabz team research and validation credit to the commit message.
- No code changes.

v2: https://lore.kernel.org/r/[email protected]/

 fs/nfsd/nfs4proc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/nfsd/nfs4proc.c +4 -0
index 92a92b3e6d..6c5656e148 100644
@@ -3161,6 +3161,10 @@ nfsd4_layoutcommit(struct svc_rqst *rqstp,
 			   NFSD_MAY_WRITE | NFSD_MAY_OWNER_OVERRIDE);
 	if (nfserr)
 		goto out;
+	if (!d_is_reg(current_fh->fh_dentry)) {
+		nfserr = nfserr_inval;
+		goto out;
+	}
 
 	nfserr = nfserr_layoutunavailable;
 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
Signature · 1 line
2.50.1 (Apple Git-155)