diff --git a/libs/clipboard/src/platform/unix/serv_files.rs b/libs/clipboard/src/platform/unix/serv_files.rs index b03edb2ff..d64c32f8d 100644 --- a/libs/clipboard/src/platform/unix/serv_files.rs +++ b/libs/clipboard/src/platform/unix/serv_files.rs @@ -17,7 +17,7 @@ use std::{ atomic::{AtomicU64, Ordering}, Arc, }, - time::SystemTime, + time::{Duration, Instant, SystemTime}, usize, }; @@ -40,6 +40,9 @@ lazy_static::lazy_static! { const MAX_RETIRED_CLIP_FILES: usize = 4; // Source of `ClipFiles::last_used`. static RETIRED_USE_SEQ: AtomicU64 = AtomicU64::new(0); +// A retired list nobody has read for this long is dropped: its transfer is over, or its peer is +// gone. The current list is kept, as before. +const RETIRED_CLIP_FILES_TTL: Duration = Duration::from_secs(5 * 60); // The first descriptor's `clsid`, inside the 32 reserved bytes every peer skips. A random // nonce there makes each copy's list id distinct, even when two copies have identical @@ -100,6 +103,8 @@ struct ClipFiles { // When a retired list was retired or last read; the lowest is evicted first. Kept apart // from the queue order, which is the order lists were sent and picks a peer's list. last_used: u64, + // When a retired list was retired or last read, for `RETIRED_CLIP_FILES_TTL`. + last_used_at: Option, } impl ClipFiles { @@ -352,9 +357,19 @@ fn select_clip_files<'a>( }, }; files.last_used = RETIRED_USE_SEQ.fetch_add(1, Ordering::Relaxed) + 1; + files.last_used_at = Some(Instant::now()); Some(files) } +// `retain` keeps the queue in the order lists were sent. +fn expire_retired(retired: &mut VecDeque) { + retired.retain(|files| { + files + .last_used_at + .map_or(true, |at| at.elapsed() < RETIRED_CLIP_FILES_TTL) + }); +} + // Keep a list a peer was sent, so its streams do not read the new copy at the same indexes. fn retire_if_served(mut replaced: ClipFiles) { if replaced.served_to.is_empty() { @@ -362,7 +377,9 @@ fn retire_if_served(mut replaced: ClipFiles) { } replaced.release_handles(); replaced.last_used = RETIRED_USE_SEQ.fetch_add(1, Ordering::Relaxed) + 1; + replaced.last_used_at = Some(Instant::now()); let mut retired = RETIRED_CLIP_FILES.lock(); + expire_retired(&mut retired); if retired.len() == MAX_RETIRED_CLIP_FILES { // The list retired or read longest ago, so a transfer still reading keeps its list. let oldest = (0..retired.len()).min_by_key(|&i| retired[i].last_used); @@ -409,6 +426,7 @@ pub fn read_file_contents( let mut current = CLIP_FILES.lock(); let mut retired = RETIRED_CLIP_FILES.lock(); + expire_retired(&mut retired); let Some(clip_files) = select_clip_files(&mut current, &mut retired, conn_id, clip_data_id) else { return vec![Err(CliprdrError::InvalidRequest { @@ -969,4 +987,33 @@ mod sig_test { clear_files(); } + + #[test] + fn an_idle_retired_list_expires() { + let tmp = TmpDir::new("idle_list"); + let first = tmp.join("first.bin"); + fs::write(&first, b"AAAAAAAA").unwrap(); + let second = tmp.join("second.bin"); + fs::write(&second, b"BBBBBBBB").unwrap(); + + let _guard = lock_clip_files(); + clear_files(); + sync_files(&[path_str(&first)]).unwrap(); + let id = file_list_id(&get_file_list_pdu(1)); + let idx = CLIP_FILES.lock().first_file_index as i32; + let read = |id| read_file_contents(1, 7, idx, 0x2, 0, 0, 4, id); + assert_eq!(range_data(read(Some(id))), b"AAAA"); + sync_files(&[path_str(&second)]).unwrap(); + + // Unread for longer than the TTL: dropped, with its open file, before the next lookup. + RETIRED_CLIP_FILES.lock()[0].last_used_at = + Instant::now().checked_sub(RETIRED_CLIP_FILES_TTL + Duration::from_secs(1)); + assert!(matches!( + read(Some(id)).last(), + Some(Err(CliprdrError::InvalidRequest { .. })) + )); + assert!(RETIRED_CLIP_FILES.lock().is_empty()); + + clear_files(); + } } diff --git a/libs/clipboard/src/windows/wf_cliprdr.c b/libs/clipboard/src/windows/wf_cliprdr.c index 96cb14098..721141d7e 100644 --- a/libs/clipboard/src/windows/wf_cliprdr.c +++ b/libs/clipboard/src/windows/wf_cliprdr.c @@ -57,10 +57,12 @@ /* File lists kept after they were sent, so a transfer still in flight keeps reading its own * files after a later copy or paste replaces them. */ #define WF_CLIPRDR_SERVED_FILE_LISTS 4u -/* Largest range served in one request: by the unix file server (MAX_RANGE_READ in - * serv_files.rs) and by the served-list path here. Larger IStream reads are split into - * requests of this size. */ +/* Largest range served in one request, here and by the unix file server (MAX_RANGE_READ in + * serv_files.rs). Larger IStream reads are split into requests of this size. */ #define WF_CLIPRDR_MAX_RANGE_READ (16u * 1024u * 1024u) +/* An earlier file list nobody has read for this long is dropped: its transfer is over, or its + * peer is gone. The current list is kept, as before. */ +#define WF_CLIPRDR_SERVED_FILE_LIST_TTL_MS (5ull * 60 * 1000) static const WCHAR WF_CLIPRDR_SUPERSCRIPT_DIGITS[] = L"\x00B9\x00B2\x00B3"; static const WCHAR WF_CLIPRDR_INVALID_FILE_NAME_CHARS[] = L"<>:\"|?*"; @@ -406,6 +408,7 @@ struct wf_served_file_list UINT32 *connIDs; // connections the list was sent to size_t nConnIDs; UINT64 last_served; // served_file_list_seq when last sent; the lowest is evicted first + ULONGLONG last_used_tick; // GetTickCount64() when last sent or read size_t nFiles; size_t first_file_index; WCHAR **file_names; @@ -2604,6 +2607,36 @@ static BOOL wf_cliprdr_copy_file_list(wfClipboard *clipboard, wfServedFileList * return TRUE; } +static void wf_cliprdr_touch_served_file_list(wfClipboard *clipboard, UINT32 id) +{ + size_t i; + + for (i = 0; i < WF_CLIPRDR_SERVED_FILE_LISTS; i++) + { + wfServedFileList *list = &clipboard->served_file_lists[i]; + if (list->file_names && list->id == id) + { + list->last_served = ++clipboard->served_file_list_seq; + list->last_used_tick = GetTickCount64(); + return; + } + } +} + +static void wf_cliprdr_expire_served_file_lists(wfClipboard *clipboard) +{ + ULONGLONG now = GetTickCount64(); + size_t i; + + for (i = 0; i < WF_CLIPRDR_SERVED_FILE_LISTS; i++) + { + wfServedFileList *list = &clipboard->served_file_lists[i]; + if (list->file_names && list->id != clipboard->file_list_id && + now - list->last_used_tick > WF_CLIPRDR_SERVED_FILE_LIST_TTL_MS) + wf_cliprdr_free_served_file_list(list); + } +} + /* One slot per list, shared by every connection it was sent to. A full table evicts the list * sent least recently, so a list a transfer has just started on is kept. */ static void wf_cliprdr_remember_served_file_list(wfClipboard *clipboard, UINT32 connID, @@ -2613,6 +2646,7 @@ static void wf_cliprdr_remember_served_file_list(wfClipboard *clipboard, UINT32 UINT32 *connIDs; size_t i; + wf_cliprdr_expire_served_file_lists(clipboard); for (i = 0; i < WF_CLIPRDR_SERVED_FILE_LISTS; i++) { if (clipboard->served_file_lists[i].file_names && @@ -2640,6 +2674,7 @@ static void wf_cliprdr_remember_served_file_list(wfClipboard *clipboard, UINT32 } } list->last_served = ++clipboard->served_file_list_seq; + list->last_used_tick = GetTickCount64(); if (wf_cliprdr_served_to(list, connID)) return; @@ -2650,6 +2685,45 @@ static void wf_cliprdr_remember_served_file_list(wfClipboard *clipboard, UINT32 list->connIDs = connIDs; } +/* Reads like wf_cliprdr_get_file_contents, but only from the file the list was sent with: a + * different size or last-write time means the path now names another file, and the read fails + * rather than continuing a transfer with its bytes. */ +static BOOL wf_cliprdr_get_served_file_contents(const WCHAR *file_name, const FILEDESCRIPTORW *dsc, + BYTE *buffer, DWORD positionLow, DWORD positionHigh, + DWORD nRequested, DWORD *puSize) +{ + BOOL res = FALSE; + HANDLE hFile; + LARGE_INTEGER size; + LARGE_INTEGER position; + FILETIME lastWrite; + DWORD nGet; + + hFile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (hFile == INVALID_HANDLE_VALUE) + return FALSE; + + if (GetFileSizeEx(hFile, &size) && + (UINT64)size.QuadPart == (((UINT64)dsc->nFileSizeHigh << 32) | dsc->nFileSizeLow) && + (!(dsc->dwFlags & FD_WRITESTIME) || + (GetFileTime(hFile, NULL, NULL, &lastWrite) && + CompareFileTime(&lastWrite, &dsc->ftLastWriteTime) == 0))) + { + position.LowPart = positionLow; + position.HighPart = (LONG)positionHigh; + if (SetFilePointerEx(hFile, position, NULL, FILE_BEGIN) && + ReadFile(hFile, buffer, nRequested, &nGet, NULL)) + { + *puSize = nGet; + res = TRUE; + } + } + + CloseHandle(hFile); + return res; +} + /* Serves a request by path from the file list it names, if that list was sent to the same * connection. An unknown list fails the request rather than falling back to other files. */ static BOOL wf_cliprdr_read_served_file_list(wfClipboard *clipboard, @@ -2659,6 +2733,7 @@ static BOOL wf_cliprdr_read_served_file_list(wfClipboard *clipboard, wfServedFileList *list = NULL; size_t i; + wf_cliprdr_expire_served_file_lists(clipboard); for (i = 0; i < WF_CLIPRDR_SERVED_FILE_LISTS; i++) { if (clipboard->served_file_lists[i].file_names && @@ -2673,6 +2748,7 @@ static BOOL wf_cliprdr_read_served_file_list(wfClipboard *clipboard, return FALSE; // A list being read is not the next one evicted. list->last_served = ++clipboard->served_file_list_seq; + list->last_used_tick = GetTickCount64(); if (request->dwFlags == FILECONTENTS_SIZE) { @@ -2689,9 +2765,10 @@ static BOOL wf_cliprdr_read_served_file_list(wfClipboard *clipboard, { clipboard->context->HandleClipboardFiles(request->connID, list->nFiles, list->file_names); } - return wf_cliprdr_get_file_contents(list->file_names[request->listIndex], data, - request->nPositionLow, request->nPositionHigh, cbRequested, - puSize); + return wf_cliprdr_get_served_file_contents( + list->file_names[request->listIndex], + &((const FILEGROUPDESCRIPTORW *)list->descriptors)->fgd[request->listIndex], data, + request->nPositionLow, request->nPositionHigh, cbRequested, puSize); } /* path_name has a '\' at the end. e.g. c:\newfolder\, file_name is c:\newfolder\new.txt */ @@ -3652,6 +3729,15 @@ wf_cliprdr_server_file_contents_request(CliprdrClientContext *context, return ERROR_INTERNAL_ERROR; } + // Refuse a range over the cap before allocating for it. Clients of this version split larger + // reads; an older Windows client reading more in one IStream::Read fails, as with unix owners. + if (fileContentsRequest->dwFlags == FILECONTENTS_RANGE && + fileContentsRequest->cbRequested > WF_CLIPRDR_MAX_RANGE_READ) + { + ZeroMemory(&vStgMedium, sizeof(STGMEDIUM)); // read at exit + goto exit; + } + // A stream whose list is no longer the one on this clipboard, because a newer list was sent // or the clipboard now holds files from a peer, reads by path from the list it came from. // Only lists sent to this connection are served, so the check below still stops a paste @@ -3661,11 +3747,8 @@ wf_cliprdr_server_file_contents_request(CliprdrClientContext *context, is_set_by_instance(clipboard) || is_file_descriptor_from_remote())) { ZeroMemory(&vStgMedium, sizeof(STGMEDIUM)); // read at exit - // Clients that send an id split reads to WF_CLIPRDR_MAX_RANGE_READ; refuse anything - // else before allocating what the peer asked for. if (fileContentsRequest->dwFlags != FILECONTENTS_SIZE && - (fileContentsRequest->dwFlags != FILECONTENTS_RANGE || - fileContentsRequest->cbRequested > WF_CLIPRDR_MAX_RANGE_READ)) + fileContentsRequest->dwFlags != FILECONTENTS_RANGE) goto exit; cbRequested = fileContentsRequest->dwFlags == FILECONTENTS_SIZE ? sizeof(UINT64) @@ -3677,6 +3760,11 @@ wf_cliprdr_server_file_contents_request(CliprdrClientContext *context, goto exit; } + // The current list is read through the existing path below; keep its kept copy from being + // evicted or expiring while it is read, so the transfer can go on after a newer list is sent. + if (fileContentsRequest->haveClipDataId) + wf_cliprdr_touch_served_file_list(clipboard, fileContentsRequest->clipDataId); + // If the clipboard is set by the instance, or the file descriptor is from remote, // we should not process the request. // Because this may be the following cases: