diff --git a/cache.c b/cache.c index d35cb4a8..6e295757 100644 --- a/cache.c +++ b/cache.c @@ -19,6 +19,7 @@ #define DEFAULT_MAX_CACHE_SIZE 10000 #define DEFAULT_CACHE_CLEAN_INTERVAL_SECS 60 #define DEFAULT_MIN_CACHE_CLEAN_INTERVAL_SECS 5 +#define MAX_READDIR_CACHE_SIZE (64U * 1024U * 1024U) struct cache { int on; @@ -33,6 +34,8 @@ struct cache { pthread_mutex_t lock; time_t last_cleaned; uint64_t write_ctr; + size_t dir_count; + size_t dir_size; }; static struct cache cache; @@ -41,6 +44,8 @@ struct node { struct stat stat; time_t stat_valid; GPtrArray *dir; + size_t dir_count; + size_t dir_size; time_t dir_valid; char *link; time_t link_valid; @@ -52,6 +57,7 @@ struct readdir_handle { void *buf; fuse_fill_dir_t filler; GPtrArray *dir; + size_t dir_size; uint64_t wrctr; }; @@ -68,12 +74,25 @@ struct cache_dirent { struct stat stat; }; +static void free_node_dir(struct node *node) +{ + if (node->dir == NULL) + return; + + cache.dir_count -= node->dir_count; + cache.dir_size -= node->dir_size; + g_ptr_array_free(node->dir, TRUE); + node->dir = NULL; + node->dir_count = 0; + node->dir_size = 0; + node->dir_valid = 0; + node->valid = MAX(node->stat_valid, node->link_valid); +} + static void free_node(gpointer node_) { struct node *node = (struct node *) node_; - if (node->dir != NULL) { - g_ptr_array_free(node->dir, TRUE); - } + free_node_dir(node); g_free(node->link); g_free(node); } @@ -89,17 +108,20 @@ static void free_cache_dirent(gpointer data) { static int cache_clean_entry(void *key_, struct node *node, time_t *now) { (void) key_; + if (node->dir != NULL && *now > node->dir_valid) + free_node_dir(node); if (*now > node->valid) return TRUE; else return FALSE; } -static void cache_clean(void) +static void cache_clean(int for_admission) { time_t now = time(NULL); if (now > cache.last_cleaned + cache.min_clean_interval_secs && - (g_hash_table_size(cache.table) > cache.max_size || + (for_admission || + g_hash_table_size(cache.table) > cache.max_size || now > cache.last_cleaned + cache.clean_interval_secs)) { g_hash_table_foreach_remove(cache.table, (GHRFunc) cache_clean_entry, &now); @@ -186,37 +208,81 @@ static struct node *cache_get(const char *path) return node; } -void cache_add_attr(const char *path, const struct stat *stbuf, uint64_t wrctr) +static void cache_add_attr_internal(const char *path, + const struct stat *stbuf, uint64_t wrctr, + int enforce_limit) { struct node *node; pthread_mutex_lock(&cache.lock); + if (enforce_limit) + cache_clean(1); if (wrctr == cache.write_ctr) { - node = cache_get(path); + node = cache_lookup(path); + if (node == NULL) { + if (enforce_limit && + g_hash_table_size(cache.table) >= cache.max_size) + goto out; + node = cache_get(path); + } node->stat = *stbuf; node->stat_valid = time(NULL) + cache.stat_timeout_secs; if (node->stat_valid > node->valid) node->valid = node->stat_valid; - cache_clean(); + cache_clean(0); } +out: pthread_mutex_unlock(&cache.lock); } -static void cache_add_dir(const char *path, GPtrArray *dir) +void cache_add_attr(const char *path, const struct stat *stbuf, uint64_t wrctr) +{ + cache_add_attr_internal(path, stbuf, wrctr, 0); +} + +/* Returns whether ownership of dir was transferred to the cache. */ +static int cache_add_dir(const char *path, GPtrArray *dir, size_t dir_size) { struct node *node; + size_t dir_count = dir->len - 1; /* Exclude the trailing NULL. */ + size_t old_count; + size_t old_size; + size_t retained_count; + size_t retained_size; pthread_mutex_lock(&cache.lock); - node = cache_get(path); - if (node->dir != NULL) { - g_ptr_array_free(node->dir, TRUE); - } + cache_clean(1); + node = cache_lookup(path); + old_count = node != NULL ? node->dir_count : 0; + 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 || + retained_count > cache.max_size - dir_count || + dir_size > MAX_READDIR_CACHE_SIZE || + retained_size > MAX_READDIR_CACHE_SIZE - dir_size) { + pthread_mutex_unlock(&cache.lock); + return 0; + } + if (node == NULL) { + if (g_hash_table_size(cache.table) >= cache.max_size) { + pthread_mutex_unlock(&cache.lock); + return 0; + } + node = cache_get(path); + } + free_node_dir(node); node->dir = dir; + node->dir_count = dir_count; + node->dir_size = dir_size; + cache.dir_count += dir_count; + cache.dir_size += dir_size; node->dir_valid = time(NULL) + cache.dir_timeout_secs; if (node->dir_valid > node->valid) node->valid = node->dir_valid; - cache_clean(); + cache_clean(0); pthread_mutex_unlock(&cache.lock); + return 1; } static size_t my_strnlen(const char *s, size_t maxsize) @@ -237,7 +303,7 @@ static void cache_add_link(const char *path, const char *link, size_t size) node->link_valid = time(NULL) + cache.link_timeout_secs; if (node->link_valid > node->valid) node->valid = node->link_valid; - cache_clean(); + cache_clean(0); pthread_mutex_unlock(&cache.lock); } @@ -358,20 +424,38 @@ static int cache_dirfill (void *buf, const char *name, ch = (struct readdir_handle*) buf; err = ch->filler(ch->buf, name, stbuf, off, flags); - if (!err) { - struct cache_dirent *cdent = g_malloc(sizeof(struct cache_dirent)); - cdent->name = g_strdup(name); - cdent->stat = *stbuf; - g_ptr_array_add(ch->dir, cdent); - if (stbuf->st_mode & S_IFMT) { - char *fullpath; - const char *basepath = !ch->path[1] ? "" : ch->path; - - fullpath = g_strdup_printf("%s/%s", basepath, name); - cache_add_attr(fullpath, stbuf, ch->wrctr); - g_free(fullpath); + if (err && ch->dir != NULL) { + g_ptr_array_free(ch->dir, TRUE); + ch->dir = NULL; + ch->dir_size = 0; + } + if (!err && ch->dir != NULL) { + size_t name_size = strlen(name) + 1; + size_t entry_size = sizeof(struct cache_dirent) + + sizeof(gpointer) + name_size; + + if (ch->dir->len >= cache.max_size || + entry_size > MAX_READDIR_CACHE_SIZE - ch->dir_size) { + g_ptr_array_free(ch->dir, TRUE); + ch->dir = NULL; + ch->dir_size = 0; + } else { + struct cache_dirent *cdent = + g_malloc(sizeof(struct cache_dirent)); + cdent->name = g_strdup(name); + cdent->stat = *stbuf; + g_ptr_array_add(ch->dir, cdent); + ch->dir_size += entry_size; } } + if (!err && (stbuf->st_mode & S_IFMT)) { + char *fullpath; + const char *basepath = !ch->path[1] ? "" : ch->path; + + fullpath = g_strdup_printf("%s/%s", basepath, name); + cache_add_attr_internal(fullpath, stbuf, ch->wrctr, 1); + g_free(fullpath); + } return err; } @@ -433,17 +517,20 @@ static int cache_readdir(const char *path, void *buf, fuse_fill_dir_t filler, ch.path = path; ch.buf = buf; ch.filler = filler; - ch.dir = g_ptr_array_new(); - g_ptr_array_set_free_func(ch.dir, free_cache_dirent); + ch.dir = NULL; + ch.dir_size = 0; + if (cache.max_size != 0) { + ch.dir = g_ptr_array_new(); + g_ptr_array_set_free_func(ch.dir, free_cache_dirent); + } ch.wrctr = cache_get_write_ctr(); /* Always request a fresh, complete enumeration to (re)fill the cache */ err = cache.next_oper->readdir(path, &ch, cache_dirfill, 0, fi, flags); - g_ptr_array_add(ch.dir, NULL); dir = ch.dir; - if (!err) { - cache_add_dir(path, dir); - } else { - g_ptr_array_free(dir, TRUE); + if (dir != NULL) { + g_ptr_array_add(dir, NULL); + if (err || !cache_add_dir(path, dir, ch.dir_size)) + g_ptr_array_free(dir, TRUE); } return err; @@ -600,6 +687,8 @@ struct fuse_operations *cache_wrap(struct fuse_operations *oper) { static struct fuse_operations cache_oper; cache.next_oper = oper; + cache.dir_count = 0; + cache.dir_size = 0; cache_fill(oper, &cache_oper); pthread_mutex_init(&cache.lock, NULL); diff --git a/sshfs.c b/sshfs.c index 4cdf1223..bf605076 100644 --- a/sshfs.c +++ b/sshfs.c @@ -129,9 +129,18 @@ #define PROTO_VERSION 3 #define MY_EOF 1 +#define MY_READDIR_TOO_LARGE 2 #define MAX_REPLY_LEN (1 << 17) +/* + * sshfs uses old-style readdir and keeps a complete directory snapshot so + * repeated enumerations can be served after the single-pass SFTP handle is + * exhausted. Bound the persistent allocations in one such snapshot. SFTP + * reply buffers are bounded separately by MAX_REPLY_LEN and READDIR_MAX. + */ +#define MAX_READDIR_SNAPSHOT_SIZE (64U * 1024U * 1024U) + #define RENAME_TEMP_CHARS 8 #define SFTP_SERVER_PATH "/usr/lib/sftp-server" @@ -982,7 +991,8 @@ static int buf_get_statvfs(struct buffer *buf, struct statvfs *stbuf) return 0; } -static int buf_get_entries(struct buffer *buf, GPtrArray *entries) +static int buf_get_entries(struct buffer *buf, GPtrArray *entries, + size_t *snapshot_size) { uint32_t count; unsigned i; @@ -992,16 +1002,34 @@ static int buf_get_entries(struct buffer *buf, GPtrArray *entries) for (i = 0; i < count; i++) { int err = -1; + size_t name_offset = buf->len; + size_t name_size; char *name; char *longname; struct stat stbuf; if (buf_get_string(buf, &name) == -1) return -EIO; + /* buf_get_string allocated the encoded length plus NUL. */ + name_size = buf->len - name_offset - sizeof(uint32_t) + 1; if (buf_get_string(buf, &longname) != -1) { free(longname); err = buf_get_attrs(buf, &stbuf, NULL); if (!err) { + size_t entry_size; struct dir_entry *entry; + + /* + * Account for every persistent allocation made for an + * entry. GPtrArray may reserve some additional pointer + * slots, but that is bounded by a constant factor. + */ + entry_size = sizeof(*entry) + sizeof(gpointer); + if (name_size > MAX_READDIR_SNAPSHOT_SIZE - entry_size || + *snapshot_size > MAX_READDIR_SNAPSHOT_SIZE - + entry_size - name_size) { + free(name); + return MY_READDIR_TOO_LARGE; + } if (sshfs.follow_symlinks && S_ISLNK(stbuf.st_mode)) { stbuf.st_mode = 0; @@ -1011,6 +1039,7 @@ static int buf_get_entries(struct buffer *buf, GPtrArray *entries) entry->stbuf = stbuf; g_ptr_array_add(entries, entry); name = NULL; /* ownership passed to entry */ + *snapshot_size += entry_size + name_size; } } free(name); @@ -2333,7 +2362,7 @@ static int sshfs_req_pending(struct request *req) } static int sftp_readdir_async(struct conn *conn, struct buffer *handle, - GPtrArray *entries) + GPtrArray *entries, size_t *snapshot_size) { int err = 0; int outstanding = 0; @@ -2384,15 +2413,21 @@ static int sftp_readdir_async(struct conn *conn, struct buffer *handle, tmperr = sftp_request_wait(req, SSH_FXP_READDIR, SSH_FXP_NAME, &name); + if (done) { + if (!tmperr) + buf_free(&name); + continue; + } - if (tmperr && !done) { + if (tmperr) { err = tmperr; if (err == MY_EOF) err = 0; done = 1; } if (!done) { - err = buf_get_entries(&name, entries); + err = buf_get_entries(&name, entries, + snapshot_size); buf_free(&name); /* increase number of outstanding requests */ @@ -2410,14 +2445,14 @@ static int sftp_readdir_async(struct conn *conn, struct buffer *handle, } static int sftp_readdir_sync(struct conn *conn, struct buffer *handle, - GPtrArray *entries) + GPtrArray *entries, size_t *snapshot_size) { int err; do { struct buffer name; err = sftp_request(conn, SSH_FXP_READDIR, handle, SSH_FXP_NAME, &name); if (!err) { - err = buf_get_entries(&name, entries); + err = buf_get_entries(&name, entries, snapshot_size); buf_free(&name); } } while (!err); @@ -2509,6 +2544,15 @@ static int sshfs_readdir(const char *path, void *dbuf, fuse_fill_dir_t filler, /* Removed or replaced through this mount, so it had to be empty */ if (path == NULL) return 0; + /* + * Retrying an over-limit snapshot on the same handle would let a peer + * repeatedly consume the full budget. Keep the error sticky until + * releasedir. ENOMEM accurately reports that the snapshot's memory budget + * was exhausted without triggering libc's special EOVERFLOW handling for + * directory entries. + */ + if (handle->read_err == MY_READDIR_TOO_LARGE) + return -ENOMEM; /* * offset == 0 starts a fresh enumeration (a new listing or a @@ -2522,6 +2566,8 @@ static int sshfs_readdir(const char *path, void *dbuf, fuse_fill_dir_t filler, * See: https://github.com/libfuse/sshfs/issues/338 */ if (offset == 0 || handle->entries == NULL) { + size_t snapshot_size = 0; + if (handle->buf_exhausted || !handle->has_handle || strcmp(path, handle->path) != 0) handle->read_err = sshfs_reopen_dir(handle, path); @@ -2533,17 +2579,20 @@ static int sshfs_readdir(const char *path, void *dbuf, fuse_fill_dir_t filler, if (sshfs.sync_readdir) handle->read_err = sftp_readdir_sync( handle->conn, &handle->buf, - handle->entries); + handle->entries, &snapshot_size); else handle->read_err = sftp_readdir_async( handle->conn, &handle->buf, - handle->entries); + handle->entries, &snapshot_size); handle->buf_exhausted = 1; + if (handle->read_err) + g_ptr_array_set_size(handle->entries, 0); } } if (handle->read_err) - return handle->read_err; + return handle->read_err == MY_READDIR_TOO_LARGE ? + -ENOMEM : handle->read_err; /* * Old-style readdir: hand FUSE the complete listing with offset 0 and