-
Notifications
You must be signed in to change notification settings - Fork 179
nfs: don't reject WRITE on read-only-mode files (fixes git fsync EPERM on macOS) #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
connorblack
wants to merge
2
commits into
tursodatabase:main
Choose a base branch
from
connorblack:fix/nfs-fsync-readonly-mode-eperm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−17
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| //! Regression tests for write-permission handling in the NFS WRITE handler. | ||
| //! | ||
| //! These cover the macOS `agentfs run` failure where `fsync`/`close` on a file | ||
| //! whose mode bits lack owner-write (e.g. git loose objects created with | ||
| //! `git_mkstemp_mode(tmp, 0444)`) was rejected with NFS3ERR_ACCES even though the | ||
| //! descriptor was opened for writing. NFSv3 is stateless and never sees open(2), | ||
| //! so WRITE must not be gated on the file's current mode bits. | ||
|
|
||
| use std::io::Cursor; | ||
| use std::sync::Arc; | ||
|
|
||
| use num_traits::ToPrimitive; | ||
| use tokio::sync::Mutex; | ||
|
|
||
| use crate::nfs::AgentNFS; | ||
| use crate::nfsserve::context::RPCContext; | ||
| use crate::nfsserve::nfs::{self, nfsstat3}; | ||
| use crate::nfsserve::rpc::{auth_unix, rpc_msg}; | ||
| use crate::nfsserve::transaction_tracker::TransactionTracker; | ||
| use crate::nfsserve::xdr::XDR; | ||
|
|
||
| // Private items of the parent (nfs_handlers) module under test. | ||
| use super::{nfsproc3_write, WRITE3args}; | ||
|
|
||
| use agentfs_sdk::{AgentFS, AgentFSOptions, FileSystem}; | ||
|
|
||
| /// Non-root uid that will own the test files (so owner mode bits apply). | ||
| const OWNER_UID: u32 = 501; | ||
| const OWNER_GID: u32 = 20; | ||
|
connorblack marked this conversation as resolved.
|
||
|
|
||
| fn status_code(status: &nfsstat3) -> u32 { | ||
| status.to_u32().expect("nfsstat3 -> u32") | ||
| } | ||
|
|
||
| async fn make_context() -> (RPCContext, tempfile::TempDir) { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let db_path = dir.path().join("delta.db"); | ||
| let agentfs = AgentFS::open(AgentFSOptions::with_path(db_path.to_str().unwrap())) | ||
| .await | ||
| .expect("open AgentFS"); | ||
| let fs: Arc<Mutex<dyn FileSystem>> = Arc::new(Mutex::new(agentfs.fs)); | ||
| let vfs = Arc::new(AgentNFS::new(fs)); | ||
|
|
||
| let ctx = RPCContext { | ||
| local_port: 0, | ||
| client_addr: "127.0.0.1:0".to_string(), | ||
| auth: auth_unix { | ||
| stamp: 0, | ||
| machinename: Vec::new(), | ||
| uid: OWNER_UID, | ||
| gid: OWNER_GID, | ||
| gids: Vec::new(), | ||
| }, | ||
| vfs, | ||
| mount_signal: None, | ||
| export_name: Arc::new("/".to_string()), | ||
| transaction_tracker: Arc::new(TransactionTracker::new(std::time::Duration::from_secs(60))), | ||
| }; | ||
| (ctx, dir) | ||
| } | ||
|
|
||
| /// Create a file in the root directory with the given mode, owned by OWNER_UID, | ||
| /// and return its fileid. | ||
| async fn create_file_with_mode(ctx: &RPCContext, name: &str, mode: u32) -> nfs::fileid3 { | ||
| let root = ctx.vfs.root_dir(); | ||
| let attr = nfs::sattr3 { | ||
| mode: nfs::set_mode3::mode(mode), | ||
| ..Default::default() | ||
| }; | ||
| let (fileid, fattr) = ctx | ||
| .vfs | ||
| .create(root, &name.as_bytes().into(), attr, &ctx.auth) | ||
| .await | ||
| .expect("create file"); | ||
| assert_eq!(fattr.mode & 0o777, mode & 0o777, "mode should be set"); | ||
| fileid | ||
| } | ||
|
|
||
| /// Drive nfsproc3_write and return the NFS status code from the reply. | ||
| async fn run_write(ctx: &RPCContext, fileid: nfs::fileid3, offset: u64, data: &[u8]) -> nfsstat3 { | ||
| let args = WRITE3args { | ||
| file: ctx.vfs.id_to_fh(fileid), | ||
| offset, | ||
| count: data.len() as u32, | ||
| stable: 2, // FILE_SYNC | ||
|
connorblack marked this conversation as resolved.
Outdated
|
||
| data: data.to_vec(), | ||
| }; | ||
|
|
||
| let mut input = Vec::new(); | ||
| args.serialize(&mut input).expect("serialize WRITE3args"); | ||
|
|
||
| let mut out = Vec::new(); | ||
| let mut reader = Cursor::new(input); | ||
| nfsproc3_write(0x1234, &mut reader, &mut out, ctx) | ||
| .await | ||
| .expect("handler ran"); | ||
|
|
||
| // Reply layout: rpc_msg (success reply) followed by the nfsstat3 status. | ||
| let mut cursor = Cursor::new(out); | ||
| let mut reply = rpc_msg::default(); | ||
| reply.deserialize(&mut cursor).expect("deserialize rpc_msg"); | ||
| let mut status = nfsstat3::NFS3_OK; | ||
| status.deserialize(&mut cursor).expect("deserialize status"); | ||
| status | ||
| } | ||
|
|
||
| /// THE REGRESSION: a file whose mode lacks owner-write (0o444) must still accept | ||
| /// a WRITE from its owner. Pre-fix this returned NFS3ERR_ACCES, which surfaced as | ||
| /// "Permission denied" on git's fsync/close of a loose object. | ||
| #[tokio::test] | ||
| async fn write_succeeds_on_readonly_mode_file() { | ||
| let (ctx, _dir) = make_context().await; | ||
| let fileid = create_file_with_mode(&ctx, "loose-object", 0o444).await; | ||
|
|
||
| let status = run_write(&ctx, fileid, 0, &[b'x'; 64]).await; | ||
| assert_eq!( | ||
| status_code(&status), | ||
| status_code(&nfsstat3::NFS3_OK), | ||
| "WRITE on a 0444-mode file owned by the caller must succeed \ | ||
| (regression: was NFS3ERR_ACCES = {})", | ||
| status_code(&nfsstat3::NFS3ERR_ACCES) | ||
| ); | ||
|
|
||
| // Guard against a vacuous test: confirm the bytes actually landed. | ||
| let (read_back, _eof) = ctx.vfs.read(fileid, 0, 64).await.expect("read back"); | ||
| assert_eq!( | ||
| read_back, | ||
| vec![b'x'; 64], | ||
| "written data should be persisted" | ||
| ); | ||
| } | ||
|
|
||
| /// A writable-mode file (0o644) must also accept WRITE — guards against the fix | ||
| /// regressing the ordinary path. | ||
| #[tokio::test] | ||
| async fn write_succeeds_on_writable_mode_file() { | ||
| let (ctx, _dir) = make_context().await; | ||
| let fileid = create_file_with_mode(&ctx, "normal", 0o644).await; | ||
|
|
||
| let status = run_write(&ctx, fileid, 0, b"hello").await; | ||
| assert_eq!( | ||
| status_code(&status), | ||
| status_code(&nfsstat3::NFS3_OK), | ||
| "WRITE on a 0644 file must succeed" | ||
| ); | ||
|
|
||
| let (read_back, _eof) = ctx.vfs.read(fileid, 0, 5).await.expect("read back"); | ||
| assert_eq!(read_back, b"hello".to_vec()); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.