From d493277ea11deb725dbf654c665bf3058027658d Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 3 Aug 2015 19:31:07 +0200 Subject: [PATCH] Reject invalid filter specifications --- HISTORY | 3 ++- Makefile.am | 3 +++ TODO | 3 --- cmdline/elem.c | 27 +++++++++++++++++++++++++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/HISTORY b/HISTORY index b2f8d835..d8693d9f 100644 --- a/HISTORY +++ b/HISTORY @@ -1,9 +1,10 @@ SnapRAID HISTORY ================ -8.2 2015/06 +8.2 2015/08 =========== * Update 'best' RAID functions for recent Atom CPUs. + * Validate filters specificatinos rejecting relative paths. 8.1 2015/05 =========== diff --git a/Makefile.am b/Makefile.am index dba84751..7847e948 100644 --- a/Makefile.am +++ b/Makefile.am @@ -270,6 +270,9 @@ endif echo --- Filter $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(CONF) -d disk1 check > output.log $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(CONF) -f \\*a -f a/ -f /b/a\\* check > output.log + $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(CONF) --test-expect-failure -f . check > output.log + $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(CONF) --test-expect-failure -f ./ check > output.log + $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(CONF) --test-expect-failure -f a/a check > output.log echo --- Delete some files and sync rm bench/disk4/a/9* rm bench/disk5/a/9* diff --git a/TODO b/TODO index 82c906e8..2b7d7654 100644 --- a/TODO +++ b/TODO @@ -5,9 +5,6 @@ This is the list of TODO items for SnapRAID. - Next -* Check if filters contains "." or "..". Return an error in this case. -Command line with -f. And also exclude/include rules. - * Allow to scrub/check/verify the just synched data. Like an option to scrub only recent data. Or more simply, we can add a new option at sync that avoid diff --git a/cmdline/elem.c b/cmdline/elem.c index 7cb270ba..7cffbc33 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -46,6 +46,8 @@ struct snapraid_filter* filter_alloc_file(int direction, const char* pattern) char* i; char* first; char* last; + int token_is_valid; + int token_is_filled; filter = malloc_nofail(sizeof(struct snapraid_filter)); pathimport(filter->pattern, sizeof(filter->pattern), pattern); @@ -54,14 +56,37 @@ struct snapraid_filter* filter_alloc_file(int direction, const char* pattern) /* find first and last slash */ first = 0; last = 0; + /* reject invalid tokens, like "", ".", ".." and more dots */ + token_is_valid = 0; + token_is_filled = 0; for (i = filter->pattern; *i; ++i) { if (*i == '/') { + /* reject invalid tokens, but accept an empty one as first */ + if (!token_is_valid && (first != 0 || token_is_filled)) { + free(filter); + return 0; + } + token_is_valid = 0; + token_is_filled = 0; + + /* update slash position */ if (!first) first = i; last = i; + } else if (*i != '.') { + token_is_valid = 1; + token_is_filled = 1; + } else { + token_is_filled = 1; } } + /* reject invalid tokens, but accept an empty one as last, but not if it's the only one */ + if (!token_is_valid && (first == 0 || token_is_filled)) { + free(filter); + return 0; + } + /* it's a file filter */ filter->is_disk = 0; @@ -86,10 +111,8 @@ struct snapraid_filter* filter_alloc_file(int direction, const char* pattern) /* a slash must be the first char, as we don't support PATH/FILE and PATH/DIR/ */ if (filter->pattern[0] != '/') { - /* LCOV_EXCL_START */ free(filter); return 0; - /* LCOV_EXCL_STOP */ } }