From 0ebc19ee486a8e928a68d8f98d07d40f176770aa Mon Sep 17 00:00:00 2001
From: Wayne Davison <wayne@opencoder.net>
Date: Thu, 14 Nov 2024 15:46:50 -0800
Subject: [PATCH 1/2] Refuse a duplicate dirlist.

---
 flist.c | 9 +++++++++
 rsync.h | 1 +
 2 files changed, 10 insertions(+)

Index: rsync-3.1.3/flist.c
===================================================================
--- rsync-3.1.3.orig/flist.c
+++ rsync-3.1.3/flist.c
@@ -2456,6 +2456,15 @@ struct file_list *recv_file_list(int f,
 		init_hard_links();
 #endif
 
+	if (inc_recurse && dir_ndx >= 0) {
+		struct file_struct *file = dir_flist->files[dir_ndx];
+		if (file->flags & FLAG_GOT_DIR_FLIST) {
+			rprintf(FERROR_XFER, "rsync: refusing malicious duplicate flist for dir %d\n", dir_ndx);
+			exit_cleanup(RERR_PROTOCOL);
+		}
+		file->flags |= FLAG_GOT_DIR_FLIST;
+	}
+
 	flist = flist_new(0, "recv_file_list");
 
 	if (inc_recurse) {
Index: rsync-3.1.3/rsync.h
===================================================================
--- rsync-3.1.3.orig/rsync.h
+++ rsync-3.1.3/rsync.h
@@ -75,6 +75,7 @@
 #define FLAG_DUPLICATE (1<<4)	/* sender */
 #define FLAG_MISSING_DIR (1<<4)	/* generator */
 #define FLAG_HLINKED (1<<5)	/* receiver/generator (checked on all types) */
+#define FLAG_GOT_DIR_FLIST (1<<5)/* sender/receiver/generator - dir_flist only */
 #define FLAG_HLINK_FIRST (1<<6)	/* receiver/generator (w/FLAG_HLINKED) */
 #define FLAG_IMPLIED_DIR (1<<6)	/* sender/receiver/generator (dirs only) */
 #define FLAG_HLINK_LAST (1<<7)	/* receiver/generator */
