From d3664234b1d1ddec755a5539fefa0c802b7bc83e Mon Sep 17 00:00:00 2001 From: Corey Berla Date: Sun, 11 Oct 2026 16:33:46 +0000 Subject: [PATCH 1/2] file-operations: Ensure trash does not link to other files When emptying a mount's trash on unmount, any component of the trash paths could be a symbolic link, e.g. .Trash-1000/files pointing at /home, causing the link target's contents to be deleted. Checking for symbolic links beforehand is racy, so instead walk the trash directories by file descriptor, opened with O_NOFOLLOW and never crossing filesystems, and delete their contents with unlinkat(). The "files" and "info" directories themselves are now deleted too, which is fine as they are recreated when files are trashed. --- src/nautilus-file-operations.c | 149 ++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/src/nautilus-file-operations.c b/src/nautilus-file-operations.c index 0f0e665b79..f9a3a0aff3 100644 --- a/src/nautilus-file-operations.c +++ b/src/nautilus-file-operations.c @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include #include "nautilus-file-operations.h" @@ -29,6 +32,7 @@ #include #include #include +#include #include "nautilus-dialog-utilities.h" #include "nautilus-error-reporting.h" @@ -123,6 +127,7 @@ typedef struct { CommonJob common; GList *trash_dirs; + char *mount_root_path; gboolean should_confirm; NautilusOpCallback done_callback; gpointer done_callback_data; @@ -2223,6 +2228,129 @@ get_trash_dirs_for_mount (GMount *mount) return list; } +static void +close_fd (gpointer data) +{ + g_clear_fd ((int *) data, NULL); +} + +/* Doesn't follow symbolic links or cross into other filesystems. */ +static int +open_untrusted_dir_at (int dirfd, + const char *name, + dev_t dev) +{ + g_autofd int fd = openat (dirfd, name, + O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY); + struct stat st; + + if (fd < 0 || fstat (fd, &st) != 0 || st.st_dev != dev) + { + return -1; + } + + return g_steal_fd (&fd); +} + +static void +append_trash_dir_at (GArray *fds, + int parent_fd, + const char *name, + dev_t dev) +{ + int fd = open_untrusted_dir_at (parent_fd, name, dev); + + if (fd < 0) + { + return; + } + + g_array_append_val (fds, fd); +} + +/* The contents of a mount are not trusted, so paths below its root could be + * swapped for symbolic links at any time. Only use the returned fds. */ +static GArray * +open_trash_dirs_at_mount_root (const char *root_path) +{ + GArray *fds = g_array_new (FALSE, FALSE, sizeof (int)); + g_autofd int root_fd = -1; + g_autofd int shared_fd = -1; + g_autofree char *uid_name = g_strdup_printf ("%d", getuid ()); + g_autofree char *user_trash_name = g_strdup_printf (".Trash-%d", getuid ()); + struct stat root_st; + + g_array_set_clear_func (fds, close_fd); + + root_fd = open (root_path, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOCTTY); + if (root_fd < 0 || fstat (root_fd, &root_st) != 0) + { + return fds; + } + + shared_fd = open_untrusted_dir_at (root_fd, ".Trash", root_st.st_dev); + if (shared_fd >= 0) + { + append_trash_dir_at (fds, shared_fd, uid_name, root_st.st_dev); + } + + append_trash_dir_at (fds, root_fd, user_trash_name, root_st.st_dev); + + return fds; +} + +static char * +get_native_root_path (GMount *mount) +{ + g_autoptr (GFile) root = g_mount_get_root (mount); + + if (root == NULL || !g_file_is_native (root)) + { + return NULL; + } + + return g_file_get_path (root); +} + +static void +rm_rf_at (CommonJob *job, + int parent_fd, + const char *name, + dev_t dev) +{ + int fd = open_untrusted_dir_at (parent_fd, name, dev); + DIR *dir; + struct dirent *entry; + + if (fd < 0) + { + unlinkat (parent_fd, name, 0); + return; + } + + dir = fdopendir (fd); + if (dir == NULL) + { + close (fd); + return; + } + + while (!job_aborted (job) && (entry = readdir (dir)) != NULL) + { + if (!g_str_equal (entry->d_name, ".") && !g_str_equal (entry->d_name, "..")) + { + rm_rf_at (job, dirfd (dir), entry->d_name, dev); + } + } + + closedir (dir); + + if (!job_aborted (job)) + { + unlinkat (parent_fd, name, AT_REMOVEDIR); + } +} + static AdwDialog * create_empty_trash_prompt (UnmountData *data) { @@ -2272,7 +2400,7 @@ empty_trash_prompt_cb (AdwDialog *dialog, job = op_job_new (EmptyTrashJob, data->parent_window, NULL); job->should_confirm = FALSE; - job->trash_dirs = get_trash_dirs_for_mount (data->mount); + job->mount_root_path = get_native_root_path (data->mount); job->done_callback = empty_trash_for_unmount_done; job->done_callback_data = data; @@ -7303,6 +7431,7 @@ empty_trash_task_done (GObject *source_object, job = user_data; g_list_free_full (job->trash_dirs, g_object_unref); + g_free (job->mount_root_path); if (job->done_callback) { @@ -7336,7 +7465,23 @@ empty_trash_thread_func (GTask *task, { confirmed = TRUE; } - if (confirmed) + if (confirmed && job->mount_root_path != NULL) + { + g_autoptr (GArray) fds = open_trash_dirs_at_mount_root (job->mount_root_path); + + for (guint i = 0; i < fds->len && !job_aborted (common); i++) + { + int trash_fd = g_array_index (fds, int, i); + struct stat st; + + if (fstat (trash_fd, &st) == 0) + { + rm_rf_at (common, trash_fd, "files", st.st_dev); + rm_rf_at (common, trash_fd, "info", st.st_dev); + } + } + } + else if (confirmed) { for (l = job->trash_dirs; l != NULL && !job_aborted (common); -- GitLab From f4f5901bb6193f19bcda5d28d9cac46140c500f4 Mon Sep 17 00:00:00 2001 From: Corey Berla Date: Wed, 21 Oct 2026 16:36:34 -0700 Subject: [PATCH 2/2] file-operations: Simplify emptying trash Now that emptying mounts' trash no longer uses trash_dirs we only use that list for emptying the main trash. Simplify the logic to just delete top levels folders (which is all that is necessary for the main trash) --- src/nautilus-file-operations.c | 91 ++++++++++------------------------ 1 file changed, 27 insertions(+), 64 deletions(-) diff --git a/src/nautilus-file-operations.c b/src/nautilus-file-operations.c index f9a3a0aff3..50355b4f24 100644 --- a/src/nautilus-file-operations.c +++ b/src/nautilus-file-operations.c @@ -126,7 +126,6 @@ typedef struct typedef struct { CommonJob common; - GList *trash_dirs; char *mount_root_path; gboolean should_confirm; NautilusOpCallback done_callback; @@ -7364,61 +7363,37 @@ nautilus_file_operations_new_file (GtkWidget *parent_view, static void -delete_trash_file (CommonJob *job, - GFile *file, - gboolean del_file, - gboolean del_children) +delete_main_trash_contents (CommonJob *job) { + g_autoptr (GFile) trash = g_file_new_for_uri (SCHEME_TRASH ":"); + g_autoptr (GFileEnumerator) enumerator = NULL; GFileInfo *info; - GFile *child; - GFileEnumerator *enumerator; - if (job_aborted (job)) + enumerator = g_file_enumerate_children (trash, + G_FILE_ATTRIBUTE_STANDARD_NAME, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + job->cancellable, + NULL); + if (enumerator == NULL) { return; } - if (del_children) + /* The g_file_delete operation works differently for locations provided + * by the trash backend as it prevents modifications of trashed items + * For that reason, it is enough to call g_file_delete on top-level + * items only. + */ + while (!job_aborted (job) && + (info = g_file_enumerator_next_file (enumerator, job->cancellable, NULL)) != NULL) { - gboolean should_recurse; - - /* The g_file_delete operation works differently for locations provided - * by the trash backend as it prevents modifications of trashed items - * For that reason, it is enough to call g_file_delete on top-level - * items only. - */ - should_recurse = !g_file_has_uri_scheme (file, SCHEME_TRASH); - - enumerator = g_file_enumerate_children (file, - G_FILE_ATTRIBUTE_STANDARD_NAME "," - G_FILE_ATTRIBUTE_STANDARD_TYPE, - G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, - job->cancellable, - NULL); - if (enumerator) - { - while (!job_aborted (job) && - (info = g_file_enumerator_next_file (enumerator, job->cancellable, NULL)) != NULL) - { - gboolean is_dir; + g_autoptr (GFile) child = g_file_get_child (trash, g_file_info_get_name (info)); - child = g_file_get_child (file, - g_file_info_get_name (info)); - is_dir = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY); - - delete_trash_file (job, child, TRUE, should_recurse && is_dir); - g_object_unref (child); - g_object_unref (info); - } - g_file_enumerator_close (enumerator, job->cancellable, NULL); - g_object_unref (enumerator); - } + g_file_delete (child, job->cancellable, NULL); + g_object_unref (info); } - if (!job_aborted (job) && del_file) - { - g_file_delete (file, job->cancellable, NULL); - } + g_file_enumerator_close (enumerator, job->cancellable, NULL); } static void @@ -7430,7 +7405,6 @@ empty_trash_task_done (GObject *source_object, job = user_data; - g_list_free_full (job->trash_dirs, g_object_unref); g_free (job->mount_root_path); if (job->done_callback) @@ -7450,22 +7424,18 @@ empty_trash_thread_func (GTask *task, { EmptyTrashJob *job = task_data; CommonJob *common; - GList *l; - gboolean confirmed; common = (CommonJob *) job; nautilus_progress_info_start (job->common.progress); - if (job->should_confirm) + if (job->should_confirm && + !confirm_empty_trash (common)) { - confirmed = confirm_empty_trash (common); - } - else - { - confirmed = TRUE; + return; } - if (confirmed && job->mount_root_path != NULL) + + if (job->mount_root_path != NULL) { g_autoptr (GArray) fds = open_trash_dirs_at_mount_root (job->mount_root_path); @@ -7481,14 +7451,9 @@ empty_trash_thread_func (GTask *task, } } } - else if (confirmed) + else { - for (l = job->trash_dirs; - l != NULL && !job_aborted (common); - l = l->next) - { - delete_trash_file (common, l->data, FALSE, TRUE); - } + delete_main_trash_contents (common); } } @@ -7508,8 +7473,6 @@ nautilus_file_operations_empty_trash (GtkWidget *parent_vie } job = op_job_new (EmptyTrashJob, parent_window, dbus_data); - job->trash_dirs = g_list_prepend (job->trash_dirs, - g_file_new_for_uri (SCHEME_TRASH ":")); job->should_confirm = ask_confirmation; inhibit_power_manager ((CommonJob *) job, _("Emptying Trash")); -- GitLab