diff --git a/SOURCES/rsync-3.1.3-cve-2018-25032.patch b/SOURCES/rsync-3.1.3-cve-2018-25032.patch new file mode 100644 index 0000000..e82e29c --- /dev/null +++ b/SOURCES/rsync-3.1.3-cve-2018-25032.patch @@ -0,0 +1,343 @@ +From 5c44459c3b28a9bd3283aaceab7c615f8020c531 Mon Sep 17 00:00:00 2001 +From: Mark Adler +Date: Tue, 17 Apr 2018 22:09:22 -0700 +Subject: [PATCH] Fix a bug that can crash deflate on some input when using + Z_FIXED. + +This bug was reported by Danilo Ramos of Eideticom, Inc. It has +lain in wait 13 years before being found! The bug was introduced +in zlib 1.2.2.2, with the addition of the Z_FIXED option. That +option forces the use of fixed Huffman codes. For rare inputs with +a large number of distant matches, the pending buffer into which +the compressed data is written can overwrite the distance symbol +table which it overlays. That results in corrupted output due to +invalid distances, and can result in out-of-bound accesses, +crashing the application. + +The fix here combines the distance buffer and literal/length +buffers into a single symbol buffer. Now three bytes of pending +buffer space are opened up for each literal or length/distance +pair consumed, instead of the previous two bytes. This assures +that the pending buffer cannot overwrite the symbol table, since +the maximum fixed code compressed length/distance is 31 bits, and +since there are four bytes of pending space for every three bytes +of symbol space. +--- + deflate.c | 74 ++++++++++++++++++++++++++++++++++++++++--------------- + deflate.h | 25 +++++++++---------- + trees.c | 50 +++++++++++-------------------------- + 3 files changed, 79 insertions(+), 70 deletions(-) + +diff --git a/zlib/deflate.c b/zlib/deflate.c +index 425babc00..19cba873a 100644 +--- a/zlib/deflate.c ++++ b/zlib/deflate.c +@@ -255,11 +255,6 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, + int wrap = 1; + static const char my_version[] = ZLIB_VERSION; + +- ushf *overlay; +- /* We overlay pending_buf and d_buf+l_buf. This works since the average +- * output size for (length,distance) codes is <= 24 bits. +- */ +- + if (version == Z_NULL || version[0] != my_version[0] || + stream_size != sizeof(z_stream)) { + return Z_VERSION_ERROR; +@@ -329,9 +324,47 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, + + s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ + +- overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); +- s->pending_buf = (uchf *) overlay; +- s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); ++ /* We overlay pending_buf and sym_buf. This works since the average size ++ * for length/distance pairs over any compressed block is assured to be 31 ++ * bits or less. ++ * ++ * Analysis: The longest fixed codes are a length code of 8 bits plus 5 ++ * extra bits, for lengths 131 to 257. The longest fixed distance codes are ++ * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest ++ * possible fixed-codes length/distance pair is then 31 bits total. ++ * ++ * sym_buf starts one-fourth of the way into pending_buf. So there are ++ * three bytes in sym_buf for every four bytes in pending_buf. Each symbol ++ * in sym_buf is three bytes -- two for the distance and one for the ++ * literal/length. As each symbol is consumed, the pointer to the next ++ * sym_buf value to read moves forward three bytes. From that symbol, up to ++ * 31 bits are written to pending_buf. The closest the written pending_buf ++ * bits gets to the next sym_buf symbol to read is just before the last ++ * code is written. At that time, 31*(n-2) bits have been written, just ++ * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at ++ * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1 ++ * symbols are written.) The closest the writing gets to what is unread is ++ * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and ++ * can range from 128 to 32768. ++ * ++ * Therefore, at a minimum, there are 142 bits of space between what is ++ * written and what is read in the overlain buffers, so the symbols cannot ++ * be overwritten by the compressed data. That space is actually 139 bits, ++ * due to the three-bit fixed-code block header. ++ * ++ * That covers the case where either Z_FIXED is specified, forcing fixed ++ * codes, or when the use of fixed codes is chosen, because that choice ++ * results in a smaller compressed block than dynamic codes. That latter ++ * condition then assures that the above analysis also covers all dynamic ++ * blocks. A dynamic-code block will only be chosen to be emitted if it has ++ * fewer bits than a fixed-code block would for the same set of symbols. ++ * Therefore its average symbol length is assured to be less than 31. So ++ * the compressed data for a dynamic block also cannot overwrite the ++ * symbols from which it is being constructed. ++ */ ++ ++ s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4); ++ s->pending_buf_size = (ulg)s->lit_bufsize * 4; + + if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || + s->pending_buf == Z_NULL) { +@@ -340,8 +373,12 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, + deflateEnd (strm); + return Z_MEM_ERROR; + } +- s->d_buf = overlay + s->lit_bufsize/sizeof(ush); +- s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; ++ s->sym_buf = s->pending_buf + s->lit_bufsize; ++ s->sym_end = (s->lit_bufsize - 1) * 3; ++ /* We avoid equality with lit_bufsize*3 because of wraparound at 64K ++ * on 16 bit machines and because stored blocks are restricted to ++ * 64K-1 bytes. ++ */ + + s->level = level; + s->strategy = strategy; +@@ -552,7 +589,7 @@ int ZEXPORT deflatePrime (strm, bits, value) + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + s = strm->state; +- if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) ++ if (s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3)) + return Z_BUF_ERROR; + do { + put = Buf_size - s->bi_valid; +@@ -1113,7 +1150,6 @@ int ZEXPORT deflateCopy (dest, source) + #else + deflate_state *ds; + deflate_state *ss; +- ushf *overlay; + + + if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { +@@ -1133,8 +1169,7 @@ int ZEXPORT deflateCopy (dest, source) + ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); + ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); + ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); +- overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); +- ds->pending_buf = (uchf *) overlay; ++ ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4); + + if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || + ds->pending_buf == Z_NULL) { +@@ -1148,8 +1183,7 @@ int ZEXPORT deflateCopy (dest, source) + zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); + + ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); +- ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); +- ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; ++ ds->sym_buf = ds->pending_buf + ds->lit_bufsize; + + ds->l_desc.dyn_tree = ds->dyn_ltree; + ds->d_desc.dyn_tree = ds->dyn_dtree; +@@ -1771,7 +1771,7 @@ local block_state deflate_fast(s, flush) + FLUSH_BLOCK(s, 1); + return finish_done; + } +- if (s->last_lit) ++ if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; + } +@@ -1912,7 +1912,7 @@ local block_state deflate_slow(s, flush) + FLUSH_BLOCK(s, 1); + return finish_done; + } +- if (s->last_lit) ++ if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; + } +@@ -1987,7 +1987,7 @@ local block_state deflate_rle(s, flush) + FLUSH_BLOCK(s, 1); + return finish_done; + } +- if (s->last_lit) ++ if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; + } +@@ -2026,7 +2026,7 @@ local block_state deflate_huff(s, flush) + FLUSH_BLOCK(s, 1); + return finish_done; + } +- if (s->last_lit) ++ if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; + } +diff --git a/zlib/deflate.h b/zlib/deflate.h +index 23ecdd312..d4cf1a98b 100644 +--- a/zlib/deflate.h ++++ b/zlib/deflate.h +@@ -217,7 +217,7 @@ typedef struct internal_state { + /* Depth of each subtree used as tie breaker for trees of equal frequency + */ + +- uchf *l_buf; /* buffer for literals or lengths */ ++ uchf *sym_buf; /* buffer for distances and literals/lengths */ + + uInt lit_bufsize; + /* Size of match buffer for literals/lengths. There are 4 reasons for +@@ -239,13 +239,8 @@ typedef struct internal_state { + * - I can't count above 4 + */ + +- uInt last_lit; /* running index in l_buf */ +- +- ushf *d_buf; +- /* Buffer for distances. To simplify the code, d_buf and l_buf have +- * the same number of elements. To use different lengths, an extra flag +- * array would be necessary. +- */ ++ uInt sym_next; /* running index in sym_buf */ ++ uInt sym_end; /* symbol table full when sym_next reaches this */ + + ulg opt_len; /* bit length of current block with optimal trees */ + ulg static_len; /* bit length of current block with static trees */ +@@ -317,20 +317,22 @@ void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, + + # define _tr_tally_lit(s, c, flush) \ + { uch cc = (c); \ +- s->d_buf[s->last_lit] = 0; \ +- s->l_buf[s->last_lit++] = cc; \ ++ s->sym_buf[s->sym_next++] = 0; \ ++ s->sym_buf[s->sym_next++] = 0; \ ++ s->sym_buf[s->sym_next++] = cc; \ + s->dyn_ltree[cc].Freq++; \ +- flush = (s->last_lit == s->lit_bufsize-1); \ ++ flush = (s->sym_next == s->sym_end); \ + } + # define _tr_tally_dist(s, distance, length, flush) \ + { uch len = (length); \ + ush dist = (distance); \ +- s->d_buf[s->last_lit] = dist; \ +- s->l_buf[s->last_lit++] = len; \ ++ s->sym_buf[s->sym_next++] = dist; \ ++ s->sym_buf[s->sym_next++] = dist >> 8; \ ++ s->sym_buf[s->sym_next++] = len; \ + dist--; \ + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ + s->dyn_dtree[d_code(dist)].Freq++; \ +- flush = (s->last_lit == s->lit_bufsize-1); \ ++ flush = (s->sym_next == s->sym_end); \ + } + #else + # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) +diff --git a/zlib/trees.c b/zlib/trees.c +index 4f4a65011..decaeb7c3 100644 +--- a/zlib/trees.c ++++ b/zlib/trees.c +@@ -416,7 +416,7 @@ local void init_block(s) + + s->dyn_ltree[END_BLOCK].Freq = 1; + s->opt_len = s->static_len = 0L; +- s->last_lit = s->matches = 0; ++ s->sym_next = s->matches = 0; + } + + #define SMALLEST 1 +@@ -948,7 +948,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) + + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, +- s->last_lit)); ++ s->sym_next / 3)); + + if (static_lenb <= opt_lenb) opt_lenb = static_lenb; + +@@ -1017,8 +1017,9 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc) + unsigned dist; /* distance of matched string */ + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ + { +- s->d_buf[s->last_lit] = (ush)dist; +- s->l_buf[s->last_lit++] = (uch)lc; ++ s->sym_buf[s->sym_next++] = dist; ++ s->sym_buf[s->sym_next++] = dist >> 8; ++ s->sym_buf[s->sym_next++] = lc; + if (dist == 0) { + /* lc is the unmatched char */ + s->dyn_ltree[lc].Freq++; +@@ -1033,30 +1034,7 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc) + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; + s->dyn_dtree[d_code(dist)].Freq++; + } +- +-#ifdef TRUNCATE_BLOCK +- /* Try to guess if it is profitable to stop the current block here */ +- if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { +- /* Compute an upper bound for the compressed length */ +- ulg out_length = (ulg)s->last_lit*8L; +- ulg in_length = (ulg)((long)s->strstart - s->block_start); +- int dcode; +- for (dcode = 0; dcode < D_CODES; dcode++) { +- out_length += (ulg)s->dyn_dtree[dcode].Freq * +- (5L+extra_dbits[dcode]); +- } +- out_length >>= 3; +- Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", +- s->last_lit, in_length, out_length, +- 100L - out_length*100L/in_length)); +- if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; +- } +-#endif +- return (s->last_lit == s->lit_bufsize-1); +- /* We avoid equality with lit_bufsize because of wraparound at 64K +- * on 16 bit machines and because stored blocks are restricted to +- * 64K-1 bytes. +- */ ++ return (s->sym_next == s->sym_end); + } + + /* =========================================================================== +@@ -1069,13 +1047,14 @@ local void compress_block(s, ltree, dtree) + { + unsigned dist; /* distance of matched string */ + int lc; /* match length or unmatched char (if dist == 0) */ +- unsigned lx = 0; /* running index in l_buf */ ++ unsigned sx = 0; /* running index in sym_buf */ + unsigned code; /* the code to send */ + int extra; /* number of extra bits to send */ + +- if (s->last_lit != 0) do { +- dist = s->d_buf[lx]; +- lc = s->l_buf[lx++]; ++ if (s->sym_next != 0) do { ++ dist = s->sym_buf[sx++] & 0xff; ++ dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; ++ lc = s->sym_buf[sx++]; + if (dist == 0) { + send_code(s, lc, ltree); /* send a literal byte */ + Tracecv(isgraph(lc), (stderr," '%c' ", lc)); +@@ -1100,11 +1079,10 @@ local void compress_block(s, ltree, dtree) + } + } /* literal or match pair ? */ + +- /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ +- Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, +- "pendingBuf overflow"); ++ /* Check that the overlay between pending_buf and sym_buf is ok: */ ++ Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow"); + +- } while (lx < s->last_lit); ++ } while (sx < s->sym_next); + + send_code(s, END_BLOCK, ltree); + } diff --git a/SOURCES/rsync-3.1.3-cve-2022-29154.patch b/SOURCES/rsync-3.1.3-cve-2022-29154.patch new file mode 100644 index 0000000..7589bb6 --- /dev/null +++ b/SOURCES/rsync-3.1.3-cve-2022-29154.patch @@ -0,0 +1,1065 @@ +diff --git a/exclude.c b/exclude.c +index 7989fb3..13c4253 100644 +--- a/exclude.c ++++ b/exclude.c +@@ -24,18 +24,26 @@ + + extern int am_server; + extern int am_sender; ++extern int am_generator; + extern int eol_nulls; + extern int io_error; ++extern int xfer_dirs; ++extern int recurse; + extern int local_server; + extern int prune_empty_dirs; + extern int ignore_perishable; ++extern int old_style_args; ++extern int relative_paths; + extern int delete_mode; + extern int delete_excluded; + extern int cvs_exclude; + extern int sanitize_paths; + extern int protocol_version; ++extern int read_batch; ++extern int list_only; + extern int module_id; + ++extern char *filesfrom_host; + extern char curr_dir[MAXPATHLEN]; + extern unsigned int curr_dir_len; + extern unsigned int module_dirlen; +@@ -43,8 +51,10 @@ extern unsigned int module_dirlen; + filter_rule_list filter_list = { .debug_type = "" }; + filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" }; + filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" }; ++filter_rule_list implied_filter_list = { .debug_type = " [implied]" }; + + int saw_xattr_filter = 0; ++int trust_sender_filter = 0; + + /* Need room enough for ":MODS " prefix plus some room to grow. */ + #define MAX_RULE_PREFIX (16) +@@ -293,6 +303,233 @@ static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_ + } + } + ++/* If the wildcards failed, the remote shell might give us a file matching the literal ++ * wildcards. Since "*" & "?" already match themselves, this just needs to deal with ++ * failed "[foo]" idioms. ++ */ ++static void maybe_add_literal_brackets_rule(filter_rule const *based_on, int arg_len) ++{ ++ filter_rule *rule; ++ const char *arg = based_on->pattern, *cp; ++ char *p; ++ int cnt = 0; ++ ++ if (arg_len < 0) ++ arg_len = strlen(arg); ++ ++ for (cp = arg; *cp; cp++) { ++ if (*cp == '\\' && cp[1]) { ++ cp++; ++ } else if (*cp == '[') ++ cnt++; ++ } ++ if (!cnt) ++ return; ++ ++ rule = new0(filter_rule); ++ rule->rflags = based_on->rflags; ++ rule->u.slash_cnt = based_on->u.slash_cnt; ++ p = rule->pattern = new_array(char, arg_len + cnt + 1); ++ for (cp = arg; *cp; ) { ++ if (*cp == '\\' && cp[1]) { ++ *p++ = *cp++; ++ } else if (*cp == '[') ++ *p++ = '\\'; ++ *p++ = *cp++; ++ } ++ *p++ = '\0'; ++ ++ rule->next = implied_filter_list.head; ++ implied_filter_list.head = rule; ++ if (DEBUG_GTE(FILTER, 3)) { ++ rprintf(FINFO, "[%s] add_implied_include(%s%s)\n", who_am_i(), rule->pattern, ++ rule->rflags & FILTRULE_DIRECTORY ? "/" : ""); ++ } ++} ++ ++static char *partial_string_buf = NULL; ++static int partial_string_len = 0; ++void implied_include_partial_string(const char *s_start, const char *s_end) ++{ ++ partial_string_len = s_end - s_start; ++ if (partial_string_len <= 0 || partial_string_len >= MAXPATHLEN) { /* too-large should be impossible... */ ++ partial_string_len = 0; ++ return; ++ } ++ if (!partial_string_buf) ++ partial_string_buf = new_array(char, MAXPATHLEN); ++ memcpy(partial_string_buf, s_start, partial_string_len); ++} ++ ++void free_implied_include_partial_string() ++{ ++ if (partial_string_buf) { ++ free(partial_string_buf); ++ partial_string_buf = NULL; ++ } ++ partial_string_len = 0; /* paranoia */ ++} ++ ++/* Each arg the client sends to the remote sender turns into an implied include ++ * that the receiver uses to validate the file list from the sender. */ ++void add_implied_include(const char *arg, int skip_daemon_module) ++{ ++ filter_rule *rule; ++ int arg_len, saw_wild = 0, saw_live_open_brkt = 0, backslash_cnt = 0; ++ int slash_cnt = 1; /* We know we're adding a leading slash. */ ++ const char *cp; ++ char *p; ++ if (am_server || old_style_args || list_only || read_batch || filesfrom_host != NULL) ++ return; ++ if (partial_string_len) { ++ arg_len = strlen(arg); ++ if (partial_string_len + arg_len >= MAXPATHLEN) { ++ partial_string_len = 0; ++ return; /* Should be impossible... */ ++ } ++ memcpy(partial_string_buf + partial_string_len, arg, arg_len + 1); ++ partial_string_len = 0; ++ arg = partial_string_buf; ++ } ++ if (skip_daemon_module) { ++ if ((cp = strchr(arg, '/')) != NULL) ++ arg = cp + 1; ++ else ++ arg = ""; ++ } ++ if (relative_paths) { ++ if ((cp = strstr(arg, "/./")) != NULL) ++ arg = cp + 3; ++ } else if ((cp = strrchr(arg, '/')) != NULL) { ++ arg = cp + 1; ++ } ++ if (*arg == '.' && arg[1] == '\0') ++ arg++; ++ arg_len = strlen(arg); ++ if (arg_len) { ++ if (strpbrk(arg, "*[?")) { ++ /* We need to add room to escape backslashes if wildcard chars are present. */ ++ for (cp = arg; (cp = strchr(cp, '\\')) != NULL; cp++) ++ arg_len++; ++ saw_wild = 1; ++ } ++ arg_len++; /* Leave room for the prefixed slash */ ++ rule = new0(filter_rule); ++ if (!implied_filter_list.head) ++ implied_filter_list.head = implied_filter_list.tail = rule; ++ else { ++ rule->next = implied_filter_list.head; ++ implied_filter_list.head = rule; ++ } ++ rule->rflags = FILTRULE_INCLUDE + (saw_wild ? FILTRULE_WILD : 0); ++ p = rule->pattern = new_array(char, arg_len + 1); ++ *p++ = '/'; ++ for (cp = arg; *cp; ) { ++ switch (*cp) { ++ case '\\': ++ if (cp[1] == ']') { ++ if (!saw_wild) ++ cp++; /* A \] in a non-wild filter causes a problem, so drop the \ . */ ++ } else if (!strchr("*[?", cp[1])) { ++ backslash_cnt++; ++ if (saw_wild) ++ *p++ = '\\'; ++ } ++ *p++ = *cp++; ++ break; ++ case '/': ++ if (p[-1] == '/') { /* This is safe because of the initial slash. */ ++ cp++; ++ break; ++ } ++ if (relative_paths) { ++ filter_rule const *ent; ++ int found = 0; ++ *p = '\0'; ++ for (ent = implied_filter_list.head; ent; ent = ent->next) { ++ if (ent != rule && strcmp(ent->pattern, rule->pattern) == 0) { ++ found = 1; ++ break; ++ } ++ } ++ if (!found) { ++ filter_rule *R_rule = new0(filter_rule); ++ R_rule->rflags = FILTRULE_INCLUDE | FILTRULE_DIRECTORY; ++ /* Check if our sub-path has wildcards or escaped backslashes */ ++ if (saw_wild && strpbrk(rule->pattern, "*[?\\")) ++ R_rule->rflags |= FILTRULE_WILD; ++ R_rule->pattern = strdup(rule->pattern); ++ R_rule->u.slash_cnt = slash_cnt; ++ R_rule->next = implied_filter_list.head; ++ implied_filter_list.head = R_rule; ++ if (DEBUG_GTE(FILTER, 3)) { ++ rprintf(FINFO, "[%s] add_implied_include(%s/)\n", ++ who_am_i(), R_rule->pattern); ++ } ++ if (saw_live_open_brkt) ++ maybe_add_literal_brackets_rule(R_rule, -1); ++ } ++ } ++ slash_cnt++; ++ *p++ = *cp++; ++ break; ++ case '[': ++ saw_live_open_brkt = 1; ++ *p++ = *cp++; ++ break; ++ default: ++ *p++ = *cp++; ++ break; ++ } ++ } ++ *p = '\0'; ++ rule->u.slash_cnt = slash_cnt; ++ arg = rule->pattern; ++ arg_len = p - arg; /* We recompute it due to backslash weirdness. */ ++ if (DEBUG_GTE(FILTER, 3)) ++ rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), rule->pattern); ++ if (saw_live_open_brkt) ++ maybe_add_literal_brackets_rule(rule, arg_len); ++ } ++ ++ if (recurse || xfer_dirs) { ++ /* Now create a rule with an added "/" & "**" or "*" at the end */ ++ rule = new0(filter_rule); ++ rule->rflags = FILTRULE_INCLUDE | FILTRULE_WILD; ++ if (recurse) ++ rule->rflags |= FILTRULE_WILD2; ++ /* We must leave enough room for / * * \0. */ ++ if (!saw_wild && backslash_cnt) { ++ /* We are appending a wildcard, so now the backslashes need to be escaped. */ ++ p = rule->pattern = new_array(char, arg_len + backslash_cnt + 3 + 1); ++ for (cp = arg; *cp; ) { ++ if (*cp == '\\') ++ *p++ = '\\'; ++ *p++ = *cp++; ++ } ++ } else { ++ p = rule->pattern = new_array(char, arg_len + 3 + 1); ++ if (arg_len) { ++ memcpy(p, arg, arg_len); ++ p += arg_len; ++ } ++ } ++ if (p[-1] != '/') ++ *p++ = '/'; ++ *p++ = '*'; ++ if (recurse) ++ *p++ = '*'; ++ *p = '\0'; ++ rule->u.slash_cnt = slash_cnt + 1; ++ rule->next = implied_filter_list.head; ++ implied_filter_list.head = rule; ++ if (DEBUG_GTE(FILTER, 3)) ++ rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), rule->pattern); ++ if (saw_live_open_brkt) ++ maybe_add_literal_brackets_rule(rule, p - rule->pattern); ++ } ++} ++ + /* This frees any non-inherited items, leaving just inherited items on the list. */ + static void pop_filter_list(filter_rule_list *listp) + { +@@ -709,11 +946,12 @@ static void report_filter_result(enum logcode code, char const *name, + filter_rule const *ent, + int name_flags, const char *type) + { ++ int log_level = am_sender || am_generator ? 1 : 3; ++ + /* If a trailing slash is present to match only directories, + * then it is stripped out by add_rule(). So as a special +- * case we add it back in here. */ +- +- if (DEBUG_GTE(FILTER, 1)) { ++ * case we add it back in the log output. */ ++ if (DEBUG_GTE(FILTER, log_level)) { + static char *actions[2][2] + = { {"show", "hid"}, {"risk", "protect"} }; + const char *w = who_am_i(); +@@ -721,7 +959,7 @@ static void report_filter_result(enum logcode code, char const *name, + : name_flags & NAME_IS_DIR ? "directory" + : "file"; + rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n", +- w, actions[*w!='s'][!(ent->rflags & FILTRULE_INCLUDE)], ++ w, actions[*w=='g'][!(ent->rflags & FILTRULE_INCLUDE)], + t, name, ent->pattern, + ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type); + } +@@ -894,6 +1132,7 @@ static filter_rule *parse_rule_tok(const char **rulestr_ptr, + } + switch (ch) { + case ':': ++ trust_sender_filter = 1; + rule->rflags |= FILTRULE_PERDIR_MERGE + | FILTRULE_FINISH_SETUP; + /* FALL THROUGH */ +diff --git a/flist.c b/flist.c +index 499440c..630d685 100644 +--- a/flist.c ++++ b/flist.c +@@ -70,6 +70,7 @@ extern int need_unsorted_flist; + extern int sender_symlink_iconv; + extern int output_needs_newline; + extern int sender_keeps_checksum; ++extern int trust_sender_filter; + extern int unsort_ndx; + extern uid_t our_uid; + extern struct stats stats; +@@ -80,8 +81,7 @@ extern char curr_dir[MAXPATHLEN]; + + extern struct chmod_mode_struct *chmod_modes; + +-extern filter_rule_list filter_list; +-extern filter_rule_list daemon_filter_list; ++extern filter_rule_list filter_list, implied_filter_list, daemon_filter_list; + + #ifdef ICONV_OPTION + extern int filesfrom_convert; +@@ -904,6 +904,19 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x + exit_cleanup(RERR_UNSUPPORTED); + } + ++ if (*thisname != '.' || thisname[1] != '\0') { ++ int filt_flags = S_ISDIR(mode) ? NAME_IS_DIR : NAME_IS_FILE; ++ if (!trust_sender_filter /* a per-dir filter rule means we must trust the sender's filtering */ ++ && filter_list.head && check_filter(&filter_list, FINFO, thisname, filt_flags) < 0) { ++ rprintf(FERROR, "ERROR: rejecting excluded file-list name: %s\n", thisname); ++ exit_cleanup(RERR_PROTOCOL); ++ } ++ if (implied_filter_list.head && check_filter(&implied_filter_list, FINFO, thisname, filt_flags) <= 0) { ++ rprintf(FERROR, "ERROR: rejecting unrequested file-list name: %s\n", thisname); ++ exit_cleanup(RERR_PROTOCOL); ++ } ++ } ++ + if (inc_recurse && S_ISDIR(mode)) { + if (one_file_system) { + /* Room to save the dir's device for -x */ +diff --git a/io.c b/io.c +index 59105ba..3aea50f 100644 +--- a/io.c ++++ b/io.c +@@ -374,6 +374,7 @@ static void forward_filesfrom_data(void) + free_xbuf(&ff_xb); + if (ff_reenable_multiplex >= 0) + io_start_multiplex_out(ff_reenable_multiplex); ++ free_implied_include_partial_string(); + } + return; + } +@@ -415,6 +416,7 @@ static void forward_filesfrom_data(void) + while (s != eob) { + if (*s++ == '\0') { + ff_xb.len = s - sob - 1; ++ add_implied_include(sob, 0); + if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0) + exit_cleanup(RERR_PROTOCOL); /* impossible? */ + write_buf(iobuf.out_fd, s-1, 1); /* Send the '\0'. */ +@@ -430,6 +432,7 @@ static void forward_filesfrom_data(void) + ff_lastchar = '\0'; + else { + /* Handle a partial string specially, saving any incomplete chars. */ ++ implied_include_partial_string(sob, s); + flags &= ~ICB_INCLUDE_INCOMPLETE; + if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0) { + if (errno == E2BIG) +@@ -446,13 +449,17 @@ static void forward_filesfrom_data(void) + char *f = ff_xb.buf + ff_xb.pos; + char *t = ff_xb.buf; + char *eob = f + len; ++ char *cur = t; + /* Eliminate any multi-'\0' runs. */ + while (f != eob) { + if (!(*t++ = *f++)) { ++ add_implied_include(cur, 0); ++ cur = t; + while (f != eob && *f == '\0') + f++; + } + } ++ implied_include_partial_string(cur, t); + ff_lastchar = f[-1]; + if ((len = t - ff_xb.buf) != 0) { + /* This will not circle back to perform_io() because we only get +diff --git a/main.c b/main.c +index 6113563..abe2ebf 100644 +--- a/main.c ++++ b/main.c +@@ -42,6 +42,7 @@ extern int output_needs_newline; + extern int need_messages_from_generator; + extern int kluge_around_eof; + extern int got_xfer_error; ++extern int old_style_args; + extern int msgs2stderr; + extern int module_id; + extern int read_only; +@@ -78,6 +79,7 @@ extern BOOL flist_receiving_enabled; + extern BOOL shutting_down; + extern int backup_dir_len; + extern int basis_dir_cnt; ++extern int trust_sender_filter; + extern struct stats stats; + extern char *stdout_format; + extern char *logfile_format; +@@ -93,7 +95,7 @@ extern char curr_dir[MAXPATHLEN]; + extern char backup_dir_buf[MAXPATHLEN]; + extern char *basis_dir[MAX_BASIS_DIRS+1]; + extern struct file_list *first_flist; +-extern filter_rule_list daemon_filter_list; ++extern filter_rule_list daemon_filter_list, implied_filter_list; + + uid_t our_uid; + gid_t our_gid; +@@ -503,11 +505,7 @@ static pid_t do_cmd(char *cmd, char *machine, char *user, char **remote_argv, in + rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n"); + exit_cleanup(RERR_SYNTAX); + } +- if (**remote_argv == '-') { +- if (asprintf(args + argc++, "./%s", *remote_argv++) < 0) +- out_of_memory("do_cmd"); +- } else +- args[argc++] = *remote_argv++; ++ args[argc++] = safe_arg(NULL, *remote_argv++); + remote_argc--; + } + } +@@ -534,6 +532,7 @@ static pid_t do_cmd(char *cmd, char *machine, char *user, char **remote_argv, in + #ifdef ICONV_CONST + setup_iconv(); + #endif ++ trust_sender_filter = 1; + } else if (local_server) { + /* If the user didn't request --[no-]whole-file, force + * it on, but only if we're not batch processing. */ +@@ -943,6 +942,7 @@ static int do_recv(int f_in, int f_out, char *local_name) + } + + am_generator = 1; ++ implied_filter_list.head = implied_filter_list.tail = NULL; + flist_receiving_enabled = True; + + io_end_multiplex_in(MPLX_SWITCHING); +@@ -1340,6 +1340,10 @@ static int start_client(int argc, char *argv[]) + remote_argc = argc = 1; + } + ++ /* A local transfer doesn't unbackslash anything, so leave the args alone. */ ++ if (local_server) ++ old_style_args = 2; ++ + if (!rsync_port && remote_argc && !**remote_argv) /* Turn an empty arg into a dot dir. */ + *remote_argv = "."; + +@@ -1358,6 +1362,8 @@ static int start_client(int argc, char *argv[]) + char *dummy_host; + int dummy_port = rsync_port; + int i; ++ if (filesfrom_fd < 0) ++ add_implied_include(remote_argv[0], daemon_over_rsh); + /* For remote source, any extra source args must have either + * the same hostname or an empty hostname. */ + for (i = 1; i < remote_argc; i++) { +@@ -1381,6 +1387,7 @@ static int start_client(int argc, char *argv[]) + if (!rsync_port && !*arg) /* Turn an empty arg into a dot dir. */ + arg = "."; + remote_argv[i] = arg; ++ add_implied_include(arg, daemon_over_rsh); + } + } + +diff --git a/receiver.c b/receiver.c +index d6a48f1..c0aa893 100644 +--- a/receiver.c ++++ b/receiver.c +@@ -577,10 +577,13 @@ int recv_files(int f_in, int f_out, char *local_name) + if (DEBUG_GTE(RECV, 1)) + rprintf(FINFO, "recv_files(%s)\n", fname); + +- if (daemon_filter_list.head && (*fname != '.' || fname[1] != '\0') +- && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) { +- rprintf(FERROR, "attempt to hack rsync failed.\n"); +- exit_cleanup(RERR_PROTOCOL); ++ if (daemon_filter_list.head && (*fname != '.' || fname[1] != '\0')) { ++ int filt_flags = S_ISDIR(file->mode) ? NAME_IS_DIR : NAME_IS_FILE; ++ if (check_filter(&daemon_filter_list, FLOG, fname, filt_flags) < 0) { ++ rprintf(FERROR, "ERROR: rejecting file transfer request for daemon excluded file: %s\n", ++ fname); ++ exit_cleanup(RERR_PROTOCOL); ++ } + } + + #ifdef SUPPORT_XATTRS +diff --git a/options.c b/options.c +index 43e8257..aaf8cc9 100644 +--- a/options.c ++++ b/options.c +@@ -99,6 +99,7 @@ int filesfrom_fd = -1; + char *filesfrom_host = NULL; + int eol_nulls = 0; + int protect_args = -1; ++int old_style_args = -1; + int human_readable = 1; + int recurse = 0; + int allow_inc_recurse = 1; +@@ -277,7 +278,7 @@ static struct output_struct debug_words[COUNT_DEBUG+1] = { + DEBUG_WORD(DELTASUM, W_SND|W_REC, "Debug delta-transfer checksumming (levels 1-4)"), + DEBUG_WORD(DUP, W_REC, "Debug weeding of duplicate names"), + DEBUG_WORD(EXIT, W_CLI|W_SRV, "Debug exit events (levels 1-3)"), +- DEBUG_WORD(FILTER, W_SND|W_REC, "Debug filter actions (levels 1-2)"), ++ DEBUG_WORD(FILTER, W_SND|W_REC, "Debug filter actions (levels 1-3)"), + DEBUG_WORD(FLIST, W_SND|W_REC, "Debug file-list operations (levels 1-4)"), + DEBUG_WORD(FUZZY, W_REC, "Debug fuzzy scoring (levels 1-2)"), + DEBUG_WORD(GENR, W_REC, "Debug generator functions"), +@@ -824,7 +825,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM, + OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD, + OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE, + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG, +- OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT, ++ OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT, OPT_OLD_ARGS, + OPT_SERVER, OPT_REFUSED_BASE = 9000}; + + static struct poptOption long_options[] = { +@@ -1011,6 +1012,8 @@ static struct poptOption long_options[] = { + {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 }, + {"from0", '0', POPT_ARG_VAL, &eol_nulls, 1, 0, 0}, + {"no-from0", 0, POPT_ARG_VAL, &eol_nulls, 0, 0, 0}, ++ {"old-args", 0, POPT_ARG_NONE, 0, OPT_OLD_ARGS, 0, 0}, ++ {"no-old-args", 0, POPT_ARG_VAL, &old_style_args, 0, 0, 0}, + {"protect-args", 's', POPT_ARG_VAL, &protect_args, 1, 0, 0}, + {"no-protect-args", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0}, + {"no-s", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0}, +@@ -1577,6 +1580,13 @@ int parse_arguments(int *argc_p, const char ***argv_p) + do_compression++; + break; + ++ case OPT_OLD_ARGS: ++ if (old_style_args <= 0) ++ old_style_args = 1; ++ else ++ old_style_args++; ++ break; ++ + case 'M': + arg = poptGetOptArg(pc); + if (*arg != '-') { +@@ -1829,6 +1839,21 @@ int parse_arguments(int *argc_p, const char ***argv_p) + } + } + ++ if (old_style_args < 0) { ++ if (!am_server && protect_args <= 0 && (arg = getenv("RSYNC_OLD_ARGS")) != NULL && *arg) { ++ protect_args = 0; ++ old_style_args = atoi(arg); ++ } else ++ old_style_args = 0; ++ } else if (old_style_args) { ++ if (protect_args > 0) { ++ snprintf(err_buf, sizeof err_buf, ++ "--protect-args conflicts with --old-args.\n"); ++ return 0; ++ } ++ protect_args = 0; ++ } ++ + if (protect_args < 0) { + if (am_server) + protect_args = 0; +@@ -2381,6 +2406,71 @@ int parse_arguments(int *argc_p, const char ***argv_p) + } + + ++static char SPLIT_ARG_WHEN_OLD[1]; ++ ++/** ++ * Do backslash quoting of any weird chars in "arg", append the resulting ++ * string to the end of the "opt" (which gets a "=" appended if it is not ++ * an empty or NULL string), and return the (perhaps malloced) result. ++ * If opt is NULL, arg is considered a filename arg that allows wildcards. ++ * If it is "" or any other value, it is considered an option. ++ **/ ++char *safe_arg(const char *opt, const char *arg) ++{ ++#define SHELL_CHARS "!#$&;|<>(){}\"' \t\\" ++#define WILD_CHARS "*?[]" /* We don't allow remote brace expansion */ ++ BOOL is_filename_arg = !opt; ++ char *escapes = is_filename_arg ? SHELL_CHARS : WILD_CHARS SHELL_CHARS; ++ BOOL escape_leading_dash = is_filename_arg && *arg == '-'; ++ BOOL escape_leading_tilde = 0; ++ int len1 = opt && *opt ? strlen(opt) + 1 : 0; ++ int len2 = strlen(arg); ++ int extras = escape_leading_dash ? 2 : 0; ++ char *ret; ++ if (!protect_args && old_style_args < 2 && (!old_style_args || (!is_filename_arg && opt != SPLIT_ARG_WHEN_OLD))) { ++ const char *f; ++ if (!old_style_args && *arg == '~' && (relative_paths || !strchr(arg, '/'))) { ++ extras++; ++ escape_leading_tilde = 1; ++ } ++ for (f = arg; *f; f++) { ++ if (strchr(escapes, *f)) ++ extras++; ++ } ++ } ++ if (!len1 && !extras) ++ return (char*)arg; ++ ret = new_array(char, len1 + len2 + extras + 1); ++ if (len1) { ++ memcpy(ret, opt, len1-1); ++ ret[len1-1] = '='; ++ } ++ if (escape_leading_dash) { ++ ret[len1++] = '.'; ++ ret[len1++] = '/'; ++ extras -= 2; ++ } ++ if (!extras) ++ memcpy(ret + len1, arg, len2); ++ else { ++ const char *f = arg; ++ char *t = ret + len1; ++ if (escape_leading_tilde) ++ *t++ = '\\'; ++ while (*f) { ++ if (*f == '\\') { ++ if (!is_filename_arg || !strchr(WILD_CHARS, f[1])) ++ *t++ = '\\'; ++ } else if (strchr(escapes, *f)) ++ *t++ = '\\'; ++ *t++ = *f++; ++ } ++ } ++ ret[len1+len2+extras] = '\0'; ++ return ret; ++} ++ ++ + /** + * Construct a filtered list of options to pass through from the + * client to the server. +@@ -2556,9 +2646,7 @@ void server_options(char **args, int *argc_p) + set++; + else + set = iconv_opt; +- if (asprintf(&arg, "--iconv=%s", set) < 0) +- goto oom; +- args[ac++] = arg; ++ args[ac++] = safe_arg("--iconv", set); + } + #endif + +@@ -2625,23 +2713,17 @@ void server_options(char **args, int *argc_p) + } + + if (backup_dir) { ++ /* This split idiom allows for ~/path expansion via the shell. */ + args[ac++] = "--backup-dir"; +- args[ac++] = backup_dir; ++ args[ac++] = safe_arg("", backup_dir); + } + + /* Only send --suffix if it specifies a non-default value. */ +- if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) { +- /* We use the following syntax to avoid weirdness with '~'. */ +- if (asprintf(&arg, "--suffix=%s", backup_suffix) < 0) +- goto oom; +- args[ac++] = arg; +- } ++ if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) ++ args[ac++] = safe_arg("--suffix", backup_suffix); + +- if (checksum_choice) { +- if (asprintf(&arg, "--checksum-choice=%s", checksum_choice) < 0) +- goto oom; +- args[ac++] = arg; +- } ++ if (checksum_choice) ++ args[ac++] = safe_arg("--checksum-choice", checksum_choice); + + if (am_sender) { + if (max_delete > 0) { +@@ -2650,14 +2732,10 @@ void server_options(char **args, int *argc_p) + args[ac++] = arg; + } else if (max_delete == 0) + args[ac++] = "--max-delete=-1"; +- if (min_size >= 0) { +- args[ac++] = "--min-size"; +- args[ac++] = min_size_arg; +- } +- if (max_size >= 0) { +- args[ac++] = "--max-size"; +- args[ac++] = max_size_arg; +- } ++ if (min_size >= 0) ++ args[ac++] = safe_arg("--min-size", min_size_arg); ++ if (max_size >= 0) ++ args[ac++] = safe_arg("--max-size", max_size_arg); + if (delete_before) + args[ac++] = "--delete-before"; + else if (delete_during == 2) +@@ -2681,11 +2759,8 @@ void server_options(char **args, int *argc_p) + if (do_stats) + args[ac++] = "--stats"; + } else { +- if (skip_compress) { +- if (asprintf(&arg, "--skip-compress=%s", skip_compress) < 0) +- goto oom; +- args[ac++] = arg; +- } ++ if (skip_compress) ++ args[ac++] = safe_arg("--skip-compress", skip_compress); + } + + /* --delete-missing-args needs the cooperation of both sides, but +@@ -2711,7 +2786,7 @@ void server_options(char **args, int *argc_p) + if (partial_dir && am_sender) { + if (partial_dir != tmp_partialdir) { + args[ac++] = "--partial-dir"; +- args[ac++] = partial_dir; ++ args[ac++] = safe_arg("", partial_dir); + } + if (delay_updates) + args[ac++] = "--delay-updates"; +@@ -2734,17 +2809,11 @@ void server_options(char **args, int *argc_p) + args[ac++] = "--use-qsort"; + + if (am_sender) { +- if (usermap) { +- if (asprintf(&arg, "--usermap=%s", usermap) < 0) +- goto oom; +- args[ac++] = arg; +- } ++ if (usermap) ++ args[ac++] = safe_arg("--usermap", usermap); + +- if (groupmap) { +- if (asprintf(&arg, "--groupmap=%s", groupmap) < 0) +- goto oom; +- args[ac++] = arg; +- } ++ if (groupmap) ++ args[ac++] = safe_arg("--groupmap", groupmap); + + if (ignore_existing) + args[ac++] = "--ignore-existing"; +@@ -2755,7 +2824,7 @@ void server_options(char **args, int *argc_p) + + if (tmpdir) { + args[ac++] = "--temp-dir"; +- args[ac++] = tmpdir; ++ args[ac++] = safe_arg("", tmpdir); + } + + if (basis_dir[0]) { +@@ -2765,7 +2834,7 @@ void server_options(char **args, int *argc_p) + */ + for (i = 0; i < basis_dir_cnt; i++) { + args[ac++] = dest_option; +- args[ac++] = basis_dir[i]; ++ args[ac++] = safe_arg("", basis_dir[i]); + } + } + } +@@ -2790,7 +2859,7 @@ void server_options(char **args, int *argc_p) + if (files_from && (!am_sender || filesfrom_host)) { + if (filesfrom_host) { + args[ac++] = "--files-from"; +- args[ac++] = files_from; ++ args[ac++] = safe_arg("", files_from); + if (eol_nulls) + args[ac++] = "--from0"; + } else { +@@ -2830,7 +2899,7 @@ void server_options(char **args, int *argc_p) + exit_cleanup(RERR_SYNTAX); + } + for (j = 1; j <= remote_option_cnt; j++) +- args[ac++] = (char*)remote_options[j]; ++ args[ac++] = safe_arg(SPLIT_ARG_WHEN_OLD, remote_options[j]); + } + + *argc_p = ac; +diff --git a/clientserver.c b/clientserver.c +index e2e2dc0..c18c024 100644 +--- a/clientserver.c ++++ b/clientserver.c +@@ -45,6 +45,7 @@ extern int protocol_version; + extern int io_timeout; + extern int no_detach; + extern int write_batch; ++extern int old_style_args; + extern int default_af_hint; + extern int logfile_format_has_i; + extern int logfile_format_has_o_or_i; +@@ -255,20 +256,45 @@ int start_inband_exchange(int f_in, int f_out, const char *user, int argc, char + + sargs[sargc++] = "."; + ++ if (!old_style_args) ++ snprintf(line, sizeof line, " %.*s/", modlen, modname); ++ + while (argc > 0) { + if (sargc >= MAX_ARGS - 1) { + arg_overflow: + rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n"); + exit_cleanup(RERR_SYNTAX); + } +- if (strncmp(*argv, modname, modlen) == 0 +- && argv[0][modlen] == '\0') ++ if (strncmp(*argv, modname, modlen) == 0 && argv[0][modlen] == '\0') + sargs[sargc++] = modname; /* we send "modname/" */ +- else if (**argv == '-') { +- if (asprintf(sargs + sargc++, "./%s", *argv) < 0) +- out_of_memory("start_inband_exchange"); +- } else +- sargs[sargc++] = *argv; ++ else { ++ char *arg = *argv; ++ int extra_chars = *arg == '-' ? 2 : 0; /* a leading dash needs a "./" prefix. */ ++ /* If --old-args was not specified, make sure that the arg won't split at a mod name! */ ++ if (!old_style_args && (p = strstr(arg, line)) != NULL) { ++ do { ++ extra_chars += 2; ++ } while ((p = strstr(p+1, line)) != NULL); ++ } ++ if (extra_chars) { ++ char *f = arg; ++ char *t = arg = new_array(char, strlen(arg) + extra_chars + 1); ++ if (*f == '-') { ++ *t++ = '.'; ++ *t++ = '/'; ++ } ++ while (*f) { ++ if (*f == ' ' && strncmp(f, line, modlen+2) == 0) { ++ *t++ = '['; ++ *t++ = *f++; ++ *t++ = ']'; ++ } else ++ *t++ = *f++; ++ } ++ *t = '\0'; ++ } ++ sargs[sargc++] = arg; ++ } + argv++; + argc--; + } +diff --git a/rsync.1 b/rsync.1 +index cf2f573..839f5ad 100644 +--- a/rsync.1 ++++ b/rsync.1 +@@ -197,7 +197,7 @@ or with the hostname omitted. For instance, all these work: + .br + \f(CWrsync \-av host::modname/file{1,2} host::modname/file3 /dest/\fP + .br +-\f(CWrsync \-av host::modname/file1 ::modname/file{3,4}\fP ++\f(CWrsync \-av host::modname/file1 ::modname/file{3,4} /dest/\fP + .RE + + .PP +@@ -211,18 +211,23 @@ examples: + .RE + + .PP +-This word\-splitting still works (by default) in the latest rsync, but is +-not as easy to use as the first method. +-.PP +-If you need to transfer a filename that contains whitespace, you can either +-specify the \fB\-\-protect\-args\fP (\fB\-s\fP) option, or you\(cq\&ll need to escape +-the whitespace in a way that the remote shell will understand. For +-instance: +-.PP +-.RS +-\f(CWrsync \-av host:'\&file\e name\e with\e spaces'\& /dest\fP ++Starting this version of rsync, filenames are passed to a remote shell ++in such a way as to preserve the characters you give it. ++Thus, if you ask for a file with spaces in the name, that's what the ++remote rsync looks for: ++.PP ++.RS ++\f(CWrsync \-aiv host:'\&a simple file.pdf'\& /dest/\fP + .RE + ++.PP ++If you use scripts that have been written to manually apply extra quoting to ++the remote rsync args (or to require remote arg splitting), you can ask rsync ++to let your script handle the extra escaping. This is done by either adding ++the \fB\-\-old\-args\fP option to the rsync runs in the script (which requires ++a new rsync) or exporting \fBRSYNC_OLD_ARGS\fP=1 and \fBRSYNC_PROTECT_ARGS\fP=0 ++(which works with old or new rsync versions). ++ + .PP + .SH "CONNECTING TO AN RSYNC DAEMON" + +@@ -429,6 +434,7 @@ to the detailed description below for a complete description. + \-\-append append data onto shorter files + \-\-append\-verify \-\-append w/old data in file checksum + \-d, \-\-dirs transfer directories without recursing ++ \-\-old\-dirs, \-\-old\-d works like --dirs when talking to old rsync + \-l, \-\-links copy symlinks as symlinks + \-L, \-\-copy\-links transform symlink into referent file/dir + \-\-copy\-unsafe\-links only \(dq\&unsafe\(dq\& symlinks are transformed +@@ -511,6 +517,7 @@ to the detailed description below for a complete description. + \-\-include\-from=FILE read include patterns from FILE + \-\-files\-from=FILE read list of source\-file names from FILE + \-0, \-\-from0 all *from/filter files are delimited by 0s ++ \-\-old\-args disable the modern arg-protection idiom + \-s, \-\-protect\-args no space\-splitting; wildcard chars only + \-\-address=ADDRESS bind address for outgoing socket to daemon + \-\-port=PORT specify double\-colon alternate port number +@@ -1857,10 +1864,10 @@ Be cautious using this, as it is possible to toggle an option that will cause + rsync to have a different idea about what data to expect next over the socket, + and that will make it fail in a cryptic fashion. + .IP +-Note that it is best to use a separate \fB\-\-remote\-option\fP for each option you +-want to pass. This makes your useage compatible with the \fB\-\-protect\-args\fP +-option. If that option is off, any spaces in your remote options will be split +-by the remote shell unless you take steps to protect them. ++Note that you should use a separate \fB\-M\fP for each remote option you ++want to pass. On older rsync versions, the presence of any spaces in the ++remote-option arg could cause it to be split into separate remote args, but ++this requires the use of \fB\-\-old\-args\fP in this version of rsync. + .IP + When performing a local transfer, the \(dq\&local\(dq\& side is the sender and the + \(dq\&remote\(dq\& side is the receiver. +@@ -2054,32 +2061,64 @@ merged files specified in a \fB\-\-filter\fP rule. + It does not affect \fB\-\-cvs\-exclude\fP (since all names read from a .cvsignore + file are split on whitespace). + .IP ++.IP "\fB\-\-old\-args\fP" ++This option tells rsync to stop trying to protect the arg values from ++unintended word-splitting or other misinterpretation by using its new ++backslash-escape idiom. The newest default is for remote filenames to only ++allow wildcards characters to be interpretated by the shell while ++protecting other shell-interpreted characters (and the args of options get ++even wildcards escaped). The only active wildcard characters on the remote ++side are: `*`, `?`, `[`, & `]`. ++.IP ++If you have a script that wants to use old-style arg splitting in the ++filenames, specify this option once. If the remote shell has a problem ++with any backslash escapes, specify the option twice. ++.IP ++You may also control this setting via the RSYNC_OLD_ARGS environment ++variable. If it has the value "1", rsync will default to a single-option ++setting. If it has the value "2" (or more), rsync will default to a ++repeated-option setting. If it is "0", you'll get the default escaping ++behavior. The environment is always overridden by manually specified ++positive or negative options (the negative is \fB\-\-no\-old\-args\fP). ++.IP ++Note that this option also disables the extra safety check added in this ++version of rsync, ++that ensures that a remote sender isn't including extra top-level items in ++the file-list that you didn't request. This side-effect is necessary ++because we can't know for sure what names to expect when the remote shell ++is interpreting the args. ++.IP ++This option conflicts with the \fB\-\-protect\-args\fP option. ++.IP + .IP "\fB\-s, \-\-protect\-args\fP" +-This option sends all filenames and most options to +-the remote rsync without allowing the remote shell to interpret them. This +-means that spaces are not split in names, and any non\-wildcard special +-characters are not translated (such as ~, $, ;, &, etc.). Wildcards are +-expanded on the remote host by rsync (instead of the shell doing it). ++This option sends all filenames and most options to the remote rsync ++without allowing the remote shell to interpret them. Wildcards are ++expanded on the remote host by rsync instead of the shell doing it. ++.IP ++This is similar to the new-style backslash-escaping of args that was added ++in this version of rsync, but supports some extra features and doesn't ++rely on backslash escaping in the remote shell. + .IP + If you use this option with \fB\-\-iconv\fP, the args related to the remote + side will also be translated + from the local to the remote character\-set. The translation happens before + wild\-cards are expanded. See also the \fB\-\-files\-from\fP option. + .IP +-You may also control this option via the RSYNC_PROTECT_ARGS environment +-variable. If this variable has a non\-zero value, this option will be enabled ++You may also control this setting via the RSYNC_PROTECT_ARGS environment ++variable. If it has a non-zero value, this setting will be enabled + by default, otherwise it will be disabled by default. Either state is + overridden by a manually specified positive or negative version of this option + (note that \fB\-\-no\-s\fP and \fB\-\-no\-protect\-args\fP are the negative versions). +-Since this option was first introduced in 3.0.0, you\(cq\&ll need to make sure it\(cq\&s +-disabled if you ever need to interact with a remote rsync that is older than +-that. +-.IP +-Rsync can also be configured (at build time) to have this option enabled by +-default (with is overridden by both the environment and the command\-line). +-This option will eventually become a new default setting at some +-as\-yet\-undetermined point in the future. ++This environment variable is also superseded by a non-zero \fBRSYNC_OLD_ARGS\fP export. + .IP ++You may need to disable this option when interacting with an older rsync ++(one prior to 3.0.0). ++.IP ++This option conflicts with the \fB\-\-old\-args\fP option. ++.IP ++Note that this option is incompatible with the use of the restricted rsync ++script (`rrsync`) since it hides options from the script's inspection. ++.IP + .IP "\fB\-T, \-\-temp\-dir=DIR\fP" + This option instructs rsync to use DIR as a + scratch directory when creating temporary copies of the files transferred +@@ -2371,7 +2410,11 @@ as a super\-user (see also the \fB\-\-fake\-super\fP option). For the \fB\-\-gr + option to have any effect, the \fB\-g\fP (\fB\-\-groups\fP) option must be used + (or implied), and the receiver will need to have permissions to set that + group. +-.IP ++.IP ++An older rsync client may need to use \fB\-\-protect\-args\fP (\fB\-s\fP) ++to avoid a complaint about wildcard characters, but a modern rsync handles ++this automatically. ++.IP + .IP "\fB\-\-chown=USER:GROUP\fP" + This option forces all files to be owned by USER + with group GROUP. This is a simpler interface than using \fB\-\-usermap\fP and +@@ -2382,6 +2425,10 @@ be omitted, but if USER is empty, a leading colon must be supplied. + .IP + If you specify \(dq\&\-\-chown=foo:bar, this is exactly the same as specifying + \(dq\&\-\-usermap=*:foo \-\-groupmap=*:bar\(dq\&, only easier. ++.IP ++An older rsync client may need to use \fB\-\-protect\-args\fP (\fB\-s\fP) to avoid a ++complaint about wildcard characters, but a modern rsync handles this ++automatically. + .IP + .IP "\fB\-\-timeout=TIMEOUT\fP" + This option allows you to set a maximum I/O +@@ -3983,10 +4030,24 @@ more details. + .IP "\fBRSYNC_ICONV\fP" + Specify a default \fB\-\-iconv\fP setting using this + environment variable. (First supported in 3.0.0.) ++.IP "\fBRSYNC_OLD_ARGS\fP" ++Specify a "1" if you want the \fB\-\-old\-args\fP option to be enabled by default, ++a "2" (or more) if you want it to be enabled in the option-repeated state, ++or a "0" to make sure that it is disabled by default. When this environment ++variable is set to a non-zero value, it supersedes the \fBRSYNC_PROTECT_ARGS\fP ++variable. ++.IP ++This variable is ignored if \fB\-\-old\-args\fP, \fB\-\-no\-old\-args\fP, or ++\fB\-\-protect\-args\fP is specified on the command line. + .IP "\fBRSYNC_PROTECT_ARGS\fP" + Specify a non\-zero numeric value if you want the + \fB\-\-protect\-args\fP option to be enabled by default, or a zero value to make + sure that it is disabled by default. (First supported in 3.1.0.) ++.IP ++This variable is ignored if \fB\-\-protect\-args\fP, \fB\-\-no\-protect\-args\fP, ++or \fB\-\-old\-args\fP is specified on the command line. ++.IP ++This variable is ignored if \fBRSYNC_OLD_ARGS\fP is set to a non-zero value. + .IP "\fBRSYNC_RSH\fP" + The RSYNC_RSH environment variable allows you to + override the default shell used as the transport for rsync. Command line diff --git a/SOURCES/rsync-3.1.3-cve-2022-37434.patch b/SOURCES/rsync-3.1.3-cve-2022-37434.patch new file mode 100644 index 0000000..2f8ec42 --- /dev/null +++ b/SOURCES/rsync-3.1.3-cve-2022-37434.patch @@ -0,0 +1,16 @@ +diff --git a/zlib/inflate.c b/zlib/inflate.c +index e43abd9..bd33c19 100644 +--- a/zlib/inflate.c ++++ b/zlib/inflate.c +@@ -740,8 +740,9 @@ int flush; + if (copy > have) copy = have; + if (copy) { + if (state->head != Z_NULL && +- state->head->extra != Z_NULL) { +- len = state->head->extra_len - state->length; ++ state->head->extra != Z_NULL && ++ (len = state->head->extra_len - state->length) < ++ state->head->extra_max) { + zmemcpy(state->head->extra + len, next, + len + copy > state->head->extra_max ? + state->head->extra_max - len : copy); diff --git a/SOURCES/rsync-3.1.3-sparse-block.patch b/SOURCES/rsync-3.1.3-sparse-block.patch new file mode 100644 index 0000000..ff5d092 --- /dev/null +++ b/SOURCES/rsync-3.1.3-sparse-block.patch @@ -0,0 +1,122 @@ +diff --git a/fileio.c b/fileio.c +index b183e20..72d6076 100644 +--- a/fileio.c ++++ b/fileio.c +@@ -34,6 +34,7 @@ + #define ALIGNED_LENGTH(len) ((((len) - 1) | (ALIGN_BOUNDRY-1)) + 1) + + extern int sparse_files; ++extern int sparse_files_block_size; + + OFF_T preallocated_len = 0; + +@@ -147,7 +148,7 @@ int write_file(int f, int use_seek, OFF_T offset, const char *buf, int len) + while (len > 0) { + int r1; + if (sparse_files > 0) { +- int len1 = MIN(len, SPARSE_WRITE_SIZE); ++ int len1 = MIN(len, sparse_files_block_size ? sparse_files_block_size : SPARSE_WRITE_SIZE); + r1 = write_sparse(f, use_seek, offset, buf, len1); + offset += r1; + } else { +diff --git a/options.c b/options.c +index 195672e..d08c05a 100644 +--- a/options.c ++++ b/options.c +@@ -76,6 +76,7 @@ int remove_source_files = 0; + int one_file_system = 0; + int protocol_version = PROTOCOL_VERSION; + int sparse_files = 0; ++long sparse_files_block_size = 0; + int preallocate_files = 0; + int do_compression = 0; + int def_compress_level = NOT_SPECIFIED; +@@ -717,6 +718,7 @@ void usage(enum logcode F) + rprintf(F," --fake-super store/recover privileged attrs using xattrs\n"); + #endif + rprintf(F," -S, --sparse turn sequences of nulls into sparse blocks\n"); ++ rprintf(F," --sparse-block=SIZE set block size used to handle sparse files\n"); + #ifdef SUPPORT_PREALLOCATION + rprintf(F," --preallocate allocate dest files before writing them\n"); + #else +@@ -927,6 +929,7 @@ static struct poptOption long_options[] = { + {"sparse", 'S', POPT_ARG_VAL, &sparse_files, 1, 0, 0 }, + {"no-sparse", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 }, + {"no-S", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 }, ++ {"sparse-block", 0, POPT_ARG_LONG, &sparse_files_block_size, 0, 0, 0 }, + {"preallocate", 0, POPT_ARG_NONE, &preallocate_files, 0, 0, 0}, + {"inplace", 0, POPT_ARG_VAL, &inplace, 1, 0, 0 }, + {"no-inplace", 0, POPT_ARG_VAL, &inplace, 0, 0, 0 }, +diff --git a/options.c b/options.c +index b12da55..5a27452 100644 +--- a/options.c ++++ b/options.c +@@ -2606,6 +2606,12 @@ void server_options(char **args, int *argc_p) + args[ac++] = arg; + } + ++ if (sparse_files_block_size) { ++ if (asprintf(&arg, "--sparse-block=%lu", sparse_files_block_size) < 0) ++ goto oom; ++ args[ac++] = arg; ++ } ++ + if (io_timeout) { + if (asprintf(&arg, "--timeout=%d", io_timeout) < 0) + goto oom; +diff --git a/rsync.yo b/rsync.yo +--- a/rsync.yo ++++ b/rsync.yo +@@ -377,6 +377,7 @@ to the detailed description below for a complete description. verb( + --super receiver attempts super-user activities + --fake-super store/recover privileged attrs using xattrs + -S, --sparse turn sequences of nulls into sparse blocks ++ --sparse-block=SIZE set block size used to handle sparse files + --preallocate allocate dest files before writing + -n, --dry-run perform a trial run with no changes made + -W, --whole-file copy files whole (w/o delta-xfer algorithm) +@@ -1299,6 +1300,15 @@ If combined with bf(--sparse), the file will only have sparse blocks (as + opposed to allocated sequences of null bytes) if the kernel version and + filesystem type support creating holes in the allocated data. + ++dit(bf(--sparse-block=SIZE)) Change the block size used to handle sparse files ++to SIZE bytes. This option only has an effect if the bf(--sparse) (bf(-S)) ++option was also specified. The default block size used by rsync to detect a ++file hole is 1024 bytes; when the receiver writes data to the destination file ++and option bf(--sparse) is used, rsync checks every 1024-bytes chunk to detect ++if they are actually filled with data or not. With certain filesystems, ++optimized to receive data streams for example, enlarging this block size can ++strongly increase performance. The option can be used to tune this block size. ++ + dit(bf(-n, --dry-run)) This makes rsync perform a trial run that doesn't + make any changes (and produces mostly the same output as a real run). It + is most commonly used in combination with the bf(-v, --verbose) and/or +diff --git a/rsync.1 b/rsync.1 +index 855dd47..1d7af3c 100644 +--- a/rsync.1 ++++ b/rsync.1 +@@ -454,6 +454,7 @@ to the detailed description below for a complete description. + \-\-super receiver attempts super\-user activities + \-\-fake\-super store/recover privileged attrs using xattrs + \-S, \-\-sparse turn sequences of nulls into sparse blocks ++ \-\-sparse-block=SIZE set block size used to handle sparse files + \-\-preallocate allocate dest files before writing + \-n, \-\-dry\-run perform a trial run with no changes made + \-W, \-\-whole\-file copy files whole (w/o delta\-xfer algorithm) +@@ -1493,6 +1493,16 @@ If combined with \fB\-\-sparse\fP, the file will only have sparse blocks (as + opposed to allocated sequences of null bytes) if the kernel version and + filesystem type support creating holes in the allocated data. + .IP ++.IP "\fB\-\-sparse\-block=SIZE\fP" ++Change the block size used to handle sparse files ++to SIZE bytes. This option only has an effect if the \fB\-\-sparse\fP (\fB\-S\fP) ++option was also specified. The default block size used by rsync to detect a ++file hole is 1024 bytes; when the receiver writes data to the destination file ++and option \fB\-\-sparse\fP is used, rsync checks every 1024\-bytes chunk to detect ++if they are actually filled with data or not. With certain filesystems, ++optimized to receive data streams for example, enlarging this block size can ++strongly increase performance. The option can be used to tune this block size. ++.IP + .IP "\fB\-n, \-\-dry\-run\fP" + This makes rsync perform a trial run that doesn\(cq\&t + make any changes (and produces mostly the same output as a real run). It diff --git a/SPECS/rsync.spec b/SPECS/rsync.spec index 2315339..e6050c5 100644 --- a/SPECS/rsync.spec +++ b/SPECS/rsync.spec @@ -9,7 +9,7 @@ Summary: A program for synchronizing files over a network Name: rsync Version: 3.1.3 -Release: 14%{?dist} +Release: 19%{?dist} Group: Applications/Internet URL: http://rsync.samba.org/ @@ -36,6 +36,10 @@ Patch5: rsync-3.1.3-ignore-missing.patch Patch6: rsync-3.1.3-append-check.patch Patch7: rsync-3.1.3-skip-compress.patch Patch8: rsync-3.1.3-xattr.patch +Patch9: rsync-3.1.3-cve-2018-25032.patch +Patch10: rsync-3.1.3-sparse-block.patch +Patch11: rsync-3.1.3-cve-2022-29154.patch +Patch12: rsync-3.1.3-cve-2022-37434.patch %description Rsync uses a reliable algorithm to bring remote and host files into @@ -82,6 +86,10 @@ patch -p1 -i patches/copy-devices.diff %patch6 -p1 -b .append %patch7 -p1 -b .skip-compress %patch8 -p1 -b .xattr +%patch9 -p1 -b .cve-2018-25032 +%patch10 -p1 -b .spars-block +%patch11 -p1 -b .cve-2022-29154 +%patch12 -p1 -b .cve-2022-37434 %build %configure @@ -128,6 +136,21 @@ chmod -x support/* %systemd_postun_with_restart rsyncd.service %changelog +* Thu Aug 18 2022 Michal Ruprich - 3.1.3-19 +- Resolves: #2116668 - zlib: a heap-based buffer over-read or buffer overflow in inflate in inflate.c via a large gzip header extra field + +* Mon Aug 15 2022 Michal Ruprich - 3.1.3-18 +- Resolves: #2111175 - remote arbitrary files write inside the directories of connecting peers + +* Mon Aug 08 2022 Michal Ruprich - 3.1.3-17 +- Related: #2043753 - New option should not be sent to the server every time + +* Thu Jul 28 2022 Michal Ruprich - 3.1.3-16 +- Resolves: #2043753 - [RFE] Improve defaults for sparse file buffering + +* Tue Apr 12 2022 Michal Ruprich - 3.1.3-15 +- Resolves: #2071513 - A flaw in zlib-1.2.11 when compressing (not decompressing!) certain inputs + * Mon Oct 11 2021 Michal Ruprich - 3.1.3-14 - Related: #1907443 - Adding fmf plans to run tests with tmt