Skip to content

Fix locking and a double free in fsops rename and unlink - #615

Open
matejk wants to merge 3 commits into
LinearTapeFileSystem:mainfrom
matejk:fix/fsops-locking
Open

Fix locking and a double free in fsops rename and unlink#615
matejk wants to merge 3 commits into
LinearTapeFileSystem:mainfrom
matejk:fix/fsops-locking

Conversation

@matejk

@matejk matejk commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Two commits:

  • Releases of unheld meta_lock: in ltfs_fsops_rename, the directory WORM checks ran before todir's meta_lock was acquired but their error path released it (whenever todir != fromdir); ltfs_fsops_unlink had the same defect for parent->meta_lock on its WORM and non-empty-directory error paths. Releasing an unheld rwlock is undefined behaviour and can corrupt the lock state. The checks now run with the locks held; lock ordering is preserved.
  • Double free on a late rename failure: after the destination name buffers were assigned to fromdentry, a later failure freed them via out_free and left dangling pointers that were freed again when the dentry is disposed. Ownership is now cleared from the locals when it moves.

@vandelvan
vandelvan requested review from XV02, madjesc and syaoraang June 12, 2026 20:55
@matejk
matejk force-pushed the fix/fsops-locking branch from 9372f75 to bbdf041 Compare June 17, 2026 20:35
@matejk
matejk force-pushed the fix/fsops-locking branch from bbdf041 to 5ba5fc5 Compare August 17, 2026 20:10
@matejk
matejk changed the base branch from main to release/v2.4.9.0 August 17, 2026 20:10
@matejk
matejk changed the base branch from release/v2.4.9.0 to main August 17, 2026 20:11
@matejk
matejk force-pushed the fix/fsops-locking branch from 5ba5fc5 to a767658 Compare August 17, 2026 20:13
matejk added 2 commits August 17, 2026 22:20
The WORM and non-empty-directory checks ran before the lock was taken but
their error paths released it. Take it first; lock order is unchanged.
After ownership moves to fromdentry, out_free freed the buffers again.
@matejk
matejk force-pushed the fix/fsops-locking branch 2 times, most recently from 51ae222 to 3800dcb Compare August 17, 2026 20:28

@XV02 XV02 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a complex change, will keep reviewing, for the time being please check this concern, if you find it wrong please discuss it on this thread :DD

Comment thread src/libltfs/ltfs_fsops.c
Comment on lines +463 to +465
/* Lock order: parent contents_lock, parent meta_lock, then child meta_lock */
acquirewrite_mrsw(&parent->meta_lock);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving acquirewrite_mrsw(&parent->meta_lock) above the WORM and non-empty-dir checks fixes the original "release of unheld lock" bug, but creates a lock-order inversion with the non-empty-dir check. Every other site in this file (and the rest of the codebase) acquires contents_lock before meta_lock on the same node, and parent locks before child locks. Here the order is reversed: parent meta_lock is held when a child's contents_lock is taken. Any concurrent thread that holds d->contents_lock (e.g. a readdir) and then tries to acquire parent->meta_lock will deadlock with this path.

@matejk matejk Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is an inversion, but I have reordered the code so the pairing no longer occurs.

The documented order (struct dentry, ltfs.h:279) is: "contents_lock, meta_lock ... take all parent locks before any dentry locks." Parent contents -> parent meta -> child contents -> child meta is that rule, and it is the order fs_path_lookup itself uses (fs.c:627-640). A deadlock would need the inverse pair - a child contents_lock held while taking the parent's meta_lock - and no site in the tree does that; readdir takes d->contents_lock, releases it, then d->meta_lock, and never touches the parent's meta_lock.

Still, the non-empty check only needs the child's contents_lock, so it now runs before parent->meta_lock is taken (new commit). parent->meta_lock is held only for the WORM checks and the unlink, never while d->contents_lock is acquired.

The original defect: the WORM and non-empty error paths went to out -> fs_release_dentry_unlocked(parent) -> releasewrite_mrsw(&parent->meta_lock), but the lock was only acquired later (old line 485), so they released a lock never held. The WORM flags are also meta_lock-protected (ltfs.h; written under d->meta_lock in xattr.c), so reading them without it was a second defect.

…in unlink

The check needs only the child's contents_lock, so parent->meta_lock is no
longer held while it is taken.
@matejk
matejk requested a review from XV02 August 19, 2026 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants