Skip to content

bound memory used by directory listings - #409

Open
acts-1631 wants to merge 2 commits into
libfuse:masterfrom
acts-1631:bound-readdir-buffering
Open

acts-1631 wants to merge 2 commits into
libfuse:masterfrom
acts-1631:bound-readdir-buffering

Conversation

@acts-1631

Copy link
Copy Markdown
Contributor

sshfs_readdir() builds a complete in-memory snapshot before returning entries to FUSE. An authenticated server can keep returning valid READDIR replies and grow that snapshot without bound. With the default directory cache, cache_dirfill() also duplicates entries and retains a full child path for each attribute, so ordinary enumeration of a malicious tree can exhaust client memory.

Cap each SSHFS directory snapshot at 64 MiB of accounted entry storage. Oversized snapshots fail with ENOMEM and remain failed for that directory handle, preventing automatic retries from rebuilding the allocation. Completed asynchronous replies are drained without retaining their entries.

Limit readdir-derived cache nodes to dcache_max_size and enforce aggregate count and 64 MiB byte budgets for cached directory arrays. Entries that do not fit the cache budget are still returned to FUSE; they are simply not retained in the optional cache.

A server could keep returning valid READDIR replies while sshfs retained
the complete listing. The default cache could then duplicate those
entries and retain a full child path for each one, allowing ordinary
enumerations to exhaust client memory.

Limit each SSHFS snapshot to 64 MiB. Keep that error sticky for the
directory handle, clear partial snapshots, and drain completed async
replies. Also bound readdir-derived cache nodes and retained directory
entries globally while still returning uncached entries to FUSE.
Comment thread cache.c
old_size = node != NULL ? node->dir_size : 0;
retained_count = cache.dir_count - old_count;
retained_size = cache.dir_size - old_size;
if (dir_count > cache.max_size ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please reclaim expired data before node lookup and admission in both cache_add_dir() and the limited attribute path, then re-check the budgets. Cleanup must respond to a full cache while honoring min_clean_interval: admission stops at max_size, but the sweep requires size > max_size. In a fake-clock probe with three expired nodes after 30 seconds, head and reorder-only read a new directory twice; base reads it once.

Comment thread cache.c
retained_count = cache.dir_count - old_count;
retained_size = cache.dir_size - old_size;
if (dir_count > cache.max_size ||
retained_count > cache.max_size - dir_count ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please free expired listing arrays and refund dir_count/dir_size independently of valid stat/link data, before this check. cache_clean_entry() only tests node->valid, so stat refreshes can keep expired listings charged indefinitely. In a fake-clock probe, an expired eight-entry listing still consumes a ten-entry budget after cleanup, preventing a five-entry directory from being cached. Moving cleanup earlier does not fix this.

Comment thread cache.c
ch->dir = NULL;
ch->dir_size = 0;
}
if (!err && ch->dir != NULL) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please keep bounded attribute updates independent of ch->dir. Dropping the optional listing array currently skips updates even for existing nodes, adding avoidable backend getattr calls. With a three-node budget, a cached fifth child's expired attributes remain stale after a five-entry listing. Calling cache_add_attr_internal(..., 1) for successfully delivered entries refreshes that node without increasing the table size.

Limited readdir admission could reject new data while expired nodes or
directory arrays still consumed the corresponding budget. Attribute
refreshes also stopped after the optional listing array was discarded.

Sweep expired state before bounded admissions, release expired directory
arrays independently of other valid node data, and keep bounded
attribute refreshes active even when the listing is not cached.
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