Skip to content
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

FileChannel.lock() doesn't work for read-only channels from SftpFileSystemProvider.newFileChannel() #384

Closed
tomaswolf opened this issue May 29, 2023 · 0 comments · Fixed by #389
Assignees
Labels
bug An issue describing a bug in the code
Milestone

Comments

@tomaswolf
Copy link
Member

tomaswolf commented May 29, 2023

Version

master

Bug description

The following code fails:

SftpFileSystemProvider provider = ...;
FileChannel channel = provider.newFileChannel(somePath, StandardOpenOption.READ);
FileLock lock = channel.lock(from, to, true);

This creates a read-only file channel and tries to obtain a shared (read) lock. It fails with an Apache MINA sshd server returning a general failure.

Actual behavior

Obtaining the lock throws an exception.

Expected behavior

The lock can be obtained (if there are no conflicts).

Relevant log output

No response

Other information

  • File locking is available only in SFTP v6. Current code doesn't check for this.
  • The client always sends zero as lock flags; it should send the appropriate read/write lock flags.
  • The server ignores flags passed, and also does not check for the SFTP version.
  • Shared read locks can be obtained only if the channel was opened for reading; exclusive write locks only if the channel was opened for writing. The client could check this condition before even making any server call.
@tomaswolf tomaswolf added this to the 2.10.1 milestone May 29, 2023
@tomaswolf tomaswolf added the bug An issue describing a bug in the code label May 29, 2023
@tomaswolf tomaswolf self-assigned this May 29, 2023
tomaswolf added a commit to tomaswolf/mina-sshd that referenced this issue Jun 3, 2023
SSH_FXP_BLOCK is available only in SFTPv6. It was introduced in draft
08. Throw an UnsupportedOperationException if the negotiated SFTP
version is lower.

Respect the contract of FileChannel.lock(): a shared lock can be
requested only if the channel was opened for reading, and an exclusive
lock only if the channel is writable. Check this, and throw appropriate
exceptions if the condition is violated.

Pass on the correct lock flags to the server. At the server, repeat the
version check, and respect the flags passed. Since Java does not have
delete locks we map them to write locks. The draft RFC[1] is silent on
what to do if no flags are passed at all: we use an exclusive  write
lock if the handle was opened for writing, and a shared read lock
otherwise. We cannot throw an exception since we must be able to support
old Apache MINA sshd clients that sent zero as lock flags.

[1] https://www.ietf.org/archive/id/draft-ietf-secsh-filexfer-13.txt

Bug: apache#384
tomaswolf added a commit to tomaswolf/mina-sshd that referenced this issue Jun 3, 2023
SSH_FXP_BLOCK is available only in SFTPv6. It was introduced in draft
08. Throw an UnsupportedOperationException if the negotiated SFTP
version is lower.

Respect the contract of FileChannel.lock(): a shared lock can be
requested only if the channel was opened for reading, and an exclusive
lock only if the channel is writable. Check this, and throw appropriate
exceptions if the condition is violated.

Pass on the correct lock flags to the server. At the server, repeat the
version check, and respect the flags passed. Since Java does not have
delete locks we map them to write locks. The draft RFC[1] is silent on
what to do if no flags are passed at all: we use an exclusive  write
lock if the handle was opened for writing, and a shared read lock
otherwise. We cannot throw an exception since we must be able to support
old Apache MINA sshd clients that sent zero as lock flags.

[1] https://www.ietf.org/archive/id/draft-ietf-secsh-filexfer-13.txt

Bug: apache#384
tomaswolf added a commit to tomaswolf/mina-sshd that referenced this issue Jun 5, 2023
SSH_FXP_BLOCK is available only in SFTPv6. It was introduced in draft
08. Throw an UnsupportedOperationException if the negotiated SFTP
version is lower.

Respect the contract of FileChannel.lock(): a shared lock can be
requested only if the channel was opened for reading, and an exclusive
lock only if the channel is writable. Check this, and throw appropriate
exceptions if the condition is violated.

Pass on the correct lock flags to the server. At the server, repeat the
version check, and respect the flags passed. Since Java does not have
delete locks we map them to write locks. The draft RFC[1] is silent on
what to do if no flags are passed at all: we use an exclusive  write
lock if the handle was opened for writing, and a shared read lock
otherwise. We cannot throw an exception since we must be able to support
old Apache MINA sshd clients that sent zero as lock flags.

[1] https://www.ietf.org/archive/id/draft-ietf-secsh-filexfer-13.txt

Bug: apache#384
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue describing a bug in the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant