Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions src/libltfs/ltfs_fsops.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,26 @@ int ltfs_fsops_unlink(const char *path, ltfs_file_id *id, struct ltfs_volume *vo
}
parent = d->parent;

/* Can't remove non-empty directories */
if (d->isdir) {
ret = 0;
acquireread_mrsw(&d->contents_lock);
if (HASH_COUNT(d->child_list) != 0)
ret = -LTFS_DIRNOTEMPTY;
releaseread_mrsw(&d->contents_lock);
if (ret < 0) {
releasewrite_mrsw(&parent->contents_lock);
fs_release_dentry(parent);
releaseread_mrsw(&vol->lock);
free(path_norm);
fs_release_dentry(d);
return ret;
}
}

/* Lock order: parent contents_lock, parent meta_lock, then child meta_lock */
acquirewrite_mrsw(&parent->meta_lock);

Comment on lines +480 to +482

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.

if (parent->is_immutable || parent->is_appendonly) {
ltfsmsg(LTFS_ERR, 17237E, "unlink: parent is WORM");
ret = -LTFS_WORM_ENABLED;
Expand All @@ -471,18 +491,6 @@ int ltfs_fsops_unlink(const char *path, ltfs_file_id *id, struct ltfs_volume *vo
goto out;
}

/* Can't remove non-empty directories */
if (d->isdir) {
ret = 0;
acquireread_mrsw(&d->contents_lock);
if (HASH_COUNT(d->child_list) != 0)
ret = -LTFS_DIRNOTEMPTY;
releaseread_mrsw(&d->contents_lock);
if (ret < 0)
goto out;
}

acquirewrite_mrsw(&parent->meta_lock);
acquirewrite_mrsw(&d->meta_lock);

if (dcache_initialized(vol)) {
Expand Down Expand Up @@ -640,19 +648,6 @@ int ltfs_fsops_rename(const char *from, const char *to, ltfs_file_id *id, struct
goto out_release;
}

if (fromdir->is_appendonly || fromdir->is_immutable ) {
ltfsmsg(LTFS_ERR, 17237E, "rename: parent is WORM");
ret = -LTFS_WORM_ENABLED;
acquirewrite_mrsw(&fromdir->meta_lock);
goto out_release;
}
if (todir->is_immutable || todir->is_appendonly) {
ltfsmsg(LTFS_ERR, 17237E, "rename: target dir is WORM");
ret = -LTFS_WORM_ENABLED;
acquirewrite_mrsw(&fromdir->meta_lock);
goto out_release;
}

/* Take locks in the appropriate order and look up the source and destination dentries */
if (todir == fromdir || fs_is_predecessor(todir, fromdir)) {
acquirewrite_mrsw(&todir->contents_lock);
Expand Down Expand Up @@ -760,6 +755,16 @@ int ltfs_fsops_rename(const char *from, const char *to, ltfs_file_id *id, struct
}
#endif

if (fromdir->is_immutable || fromdir->is_appendonly ||
todir->is_immutable || todir->is_appendonly) {
ltfsmsg(LTFS_ERR, 17237E, "rename: source or target dir is WORM");
ret = -LTFS_WORM_ENABLED;
fs_release_dentry(fromdentry);
if (todentry && todentry != fromdentry)
fs_release_dentry(todentry);
goto out_unlock;
}

if (fromdentry->is_immutable || fromdentry->is_appendonly) {
ltfsmsg(LTFS_ERR, 17237E, "rename: src entry is WORM");
ret = -LTFS_WORM_ENABLED;
Expand Down Expand Up @@ -857,6 +862,9 @@ int ltfs_fsops_rename(const char *from, const char *to, ltfs_file_id *id, struct
fromdentry->name.percent_encode = fs_is_percent_encode_required(fromdentry->name.name);
fromdentry->platform_safe_name = to_filename_copy2;
fromdentry->matches_name_criteria = index_criteria_match(fromdentry, vol);
/* fromdentry owns the buffers now; keep out_free from freeing them again */
to_filename_copy = NULL;
to_filename_copy2 = NULL;

/* Add fromdentry to new directory */
todir->child_list = fs_add_key_to_hash_table(todir->child_list, fromdentry, &ret);
Expand Down
Loading