Blame SOURCES/fapolicyd-exclude-list.patch

32a688
From 4066d92395c18ad435ee6ff8e1da2745a68bacc1 Mon Sep 17 00:00:00 2001
32a688
From: Radovan Sroka <rsroka@redhat.com>
32a688
Date: Fri, 24 Jun 2022 15:03:28 +0200
32a688
Subject: [PATCH] Introduce filtering of rpmdb
32a688
32a688
- this feature introduces very flexible filter syntax
32a688
- original filter was compiled in so this is very useful
32a688
- filter needs to keep a minimal set of files that will be excuted
32a688
  on the system eventually
32a688
- all the configuration can be done in /etc/fapolicyd/rpm-filter.conf
32a688
32a688
Signed-off-by: Radovan Sroka <rsroka@redhat.com>
32a688
---
32a688
 doc/Makefile.am           |   3 +-
32a688
 doc/rpm-filter.conf.5     |  63 +++++
32a688
 fapolicyd.spec            |   1 +
32a688
 init/Makefile.am          |   2 +
32a688
 init/rpm-filter.conf      |  42 ++++
32a688
 src/Makefile.am           |   8 +-
32a688
 src/library/llist.c       |  23 +-
32a688
 src/library/llist.h       |   1 +
32a688
 src/library/rpm-backend.c |  79 ++-----
32a688
 src/library/rpm-filter.c  | 487 ++++++++++++++++++++++++++++++++++++++
32a688
 src/library/rpm-filter.h  |  67 ++++++
32a688
 src/library/stack.c       |  89 +++++++
32a688
 src/library/stack.h       |  41 ++++
32a688
 13 files changed, 837 insertions(+), 69 deletions(-)
32a688
 create mode 100644 doc/rpm-filter.conf.5
32a688
 create mode 100644 init/rpm-filter.conf
32a688
 create mode 100644 src/library/rpm-filter.c
32a688
 create mode 100644 src/library/rpm-filter.h
32a688
 create mode 100644 src/library/stack.c
32a688
 create mode 100644 src/library/stack.h
32a688
32a688
diff --git a/doc/Makefile.am b/doc/Makefile.am
32a688
index f0b79080..726218ed 100644
32a688
--- a/doc/Makefile.am
32a688
+++ b/doc/Makefile.am
32a688
@@ -28,4 +28,5 @@ man_MANS = \
32a688
 	fapolicyd-cli.1 \
32a688
 	fapolicyd.rules.5 \
32a688
 	fapolicyd.trust.5 \
32a688
-	fapolicyd.conf.5
32a688
+	fapolicyd.conf.5 \
32a688
+	rpm-filter.conf.5
32a688
diff --git a/doc/rpm-filter.conf.5 b/doc/rpm-filter.conf.5
32a688
new file mode 100644
32a688
index 00000000..d415bd80
32a688
--- /dev/null
32a688
+++ b/doc/rpm-filter.conf.5
32a688
@@ -0,0 +1,63 @@
32a688
+.TH RPM_FILTER.CONF: "5" "January 2023" "Red Hat" "System Administration Utilities"
32a688
+.SH NAME
32a688
+rpm-filter.conf \- fapolicyd filter configuration file
32a688
+.SH DESCRIPTION
32a688
+The file
32a688
+.I /etc/fapolicyd/rpm-filter.conf
32a688
+contains configuration of the filter for the application allowlisting daemon. This filter specifies an allow or exclude list of files from rpm. Valid line starts with character '+', '-' or '#' for comments. The rest of the line contains a path specification. Space can be used as indentation to add more specific filters to the previous one. Note, that only one space is required for one level of an indent. If  there are multiple specifications on the same indentation level they extend the previous line with lower indentation, usually a directory.  The path may be specified using the glob pattern. A directory specification has to end with a slash ‘/’.
32a688
+
32a688
+The filters are processed as follows: Starting from the up the to bottom while in case of a match the result (+/-) is set unless there is an indented block which describes more detailed specification of the parent level match. The same processing logic is applied to the inner filters definitions. If there is no match, the parent’s result is set. If there is no match at all, the default result is minus (-).
32a688
+
32a688
+If the result was a plus (+), the respective file from the rpmdb is imported to the TrustDB. Vice versa, if the result was a minus (-), the respective file is not imported.
32a688
+
32a688
+From a performance point of view it is better to design an indented filter because in the ideal situation each component of the path is compared only once. In contrast to it, a filter without any indentation has to contain a full path which makes the pattern more complicated and thus slower to process. The motivation behind this is to have a flexible configuration and keep the TrustDB as small as possible to make the look-ups faster.
32a688
+
32a688
+
32a688
+
32a688
+.nf
32a688
+.B # this is simple allow list
32a688
+.B - /usr/bin/some_binary1
32a688
+.B - /usr/bin/some_binary2
32a688
+.B + /
32a688
+.fi
32a688
+
32a688
+.nf
32a688
+.B # this is the same
32a688
+.B + /
32a688
+.B \ + usr/bin/
32a688
+.B \ \ - some_binary1
32a688
+.B \ \ - some_binary2
32a688
+.fi
32a688
+
32a688
+.nf
32a688
+.B # this is similar allow list with a wildcard
32a688
+.B - /usr/bin/some_binary?
32a688
+.B + /
32a688
+.fi
32a688
+
32a688
+.nf
32a688
+.B # this is similar with another wildcard
32a688
+.B + /
32a688
+.B \ - usr/bin/some_binary*
32a688
+.fi
32a688
+
32a688
+.nf
32a688
+.B # keeps everything except usr/share except python and perl files
32a688
+.B # /usr/bin/ls - result is '+'
32a688
+.B # /usr/share/something - result is '-'
32a688
+.B # /usr/share/abcd.py - result is '+'
32a688
+.B + /
32a688
+.B \ - usr/share/
32a688
+.B \ \ + *.py
32a688
+.B \ \ + *.pl
32a688
+.fi
32a688
+
32a688
+.SH "SEE ALSO"
32a688
+.BR fapolicyd (8),
32a688
+.BR fapolicyd-cli (1)
32a688
+.BR fapolicy.rules (5)
32a688
+and
32a688
+.BR glob (7)
32a688
+
32a688
+.SH AUTHOR
32a688
+Radovan Sroka
32a688
diff --git a/init/Makefile.am b/init/Makefile.am
32a688
index da948e4e..1f23dffe 100644
32a688
--- a/init/Makefile.am
32a688
+++ b/init/Makefile.am
32a688
@@ -1,6 +1,7 @@
32a688
 EXTRA_DIST = \
32a688
 	fapolicyd.service \
32a688
 	fapolicyd.conf \
32a688
+	rpm-filter.conf \
32a688
 	fapolicyd.trust \
32a688
 	fapolicyd-tmpfiles.conf \
32a688
 	fapolicyd-magic \
32a688
@@ -11,6 +12,7 @@ fapolicyddir = $(sysconfdir)/fapolicyd
32a688
 
32a688
 dist_fapolicyd_DATA = \
32a688
 	fapolicyd.conf \
32a688
+	rpm-filter.conf \
32a688
 	fapolicyd.trust
32a688
 
32a688
 systemdservicedir = $(systemdsystemunitdir)
32a688
diff --git a/init/rpm-filter.conf b/init/rpm-filter.conf
32a688
new file mode 100644
32a688
index 00000000..0c8fca40
32a688
--- /dev/null
32a688
+++ b/init/rpm-filter.conf
32a688
@@ -0,0 +1,42 @@
32a688
+# default filter file for fedora
32a688
+
32a688
++ /
32a688
+ - usr/include/
32a688
+ - usr/share/
32a688
+  # Python byte code
32a688
+  + *.py?
32a688
+  # Python text files
32a688
+  + *.py
32a688
+  # Some apps have a private libexec
32a688
+  + */libexec/*
32a688
+  # Ruby
32a688
+  + *.rb
32a688
+  # Perl
32a688
+  + *.pl
32a688
+  # System tap
32a688
+  + *.stp
32a688
+  # Javascript
32a688
+  + *.js
32a688
+  # Java archive
32a688
+  + *.jar
32a688
+  # M4
32a688
+  + *.m4
32a688
+  # PHP
32a688
+  + *.php
32a688
+  # Perl Modules
32a688
+  + *.pm
32a688
+  # Lua
32a688
+  + *.lua
32a688
+  # Java
32a688
+  + *.class
32a688
+  # Typescript
32a688
+  + *.ts
32a688
+  # Typescript JSX
32a688
+  + *.tsx
32a688
+  # Lisp
32a688
+  + *.el
32a688
+  # Compiled Lisp
32a688
+  + *.elc
32a688
+ - usr/src/kernel*/
32a688
+  + */scripts/*
32a688
+  + */tools/objtool/*
32a688
diff --git a/src/Makefile.am b/src/Makefile.am
32a688
index 547ea486..fd08eb06 100644
32a688
--- a/src/Makefile.am
32a688
+++ b/src/Makefile.am
32a688
@@ -62,13 +62,19 @@ libfapolicyd_la_SOURCES = \
32a688
 	library/subject-attr.h \
32a688
 	library/subject.c \
32a688
 	library/subject.h \
32a688
+	library/stack.c \
32a688
+	library/stack.h \
32a688
 	library/string-util.c \
32a688
 	library/string-util.h \
32a688
 	library/trust-file.c \
32a688
 	library/trust-file.h
32a688
 
32a688
 if WITH_RPM
32a688
-libfapolicyd_la_SOURCES += library/rpm-backend.c
32a688
+libfapolicyd_la_SOURCES += \
32a688
+	library/rpm-backend.c \
32a688
+	library/rpm-filter.c \
32a688
+	library/rpm-filter.h
32a688
+
32a688
 endif
32a688
 
32a688
 libfapolicyd_la_CFLAGS = $(fapolicyd_CFLAGS)
32a688
diff --git a/src/library/llist.c b/src/library/llist.c
32a688
index 6132805a..44cfb4a3 100644
32a688
--- a/src/library/llist.c
32a688
+++ b/src/library/llist.c
32a688
@@ -45,19 +45,36 @@ list_item_t *list_get_first(const list_t *list)
32a688
 	return list->first;
32a688
 }
32a688
 
32a688
-
32a688
-int list_append(list_t *list, const char *index, const char *data)
32a688
+static list_item_t * create_item(const char *index, const char *data)
32a688
 {
32a688
 	list_item_t *item = malloc(sizeof(list_item_t));
32a688
 	if (!item) {
32a688
 		msg(LOG_ERR, "Malloc failed");
32a688
-		return 1;
32a688
+		return item;
32a688
 	}
32a688
 
32a688
 	item->index = index;
32a688
 	item->data = data;
32a688
 	item->next = NULL;
32a688
 
32a688
+	return item;
32a688
+}
32a688
+
32a688
+int list_prepend(list_t *list, const char *index, const char *data)
32a688
+{
32a688
+	list_item_t *item = create_item(index, data);
32a688
+
32a688
+	item->next = list->first;
32a688
+	list->first = item;
32a688
+
32a688
+	++list->count;
32a688
+	return 0;
32a688
+}
32a688
+
32a688
+int list_append(list_t *list, const char *index, const char *data)
32a688
+{
32a688
+	list_item_t *item = create_item(index, data);
32a688
+
32a688
 	if (list->first) {
32a688
 		list->last->next = item;
32a688
 		list->last = item;
32a688
diff --git a/src/library/llist.h b/src/library/llist.h
32a688
index 0c1d85a7..59eccf17 100644
32a688
--- a/src/library/llist.h
32a688
+++ b/src/library/llist.h
32a688
@@ -40,6 +40,7 @@ typedef struct list_header {
32a688
 
32a688
 void list_init(list_t *list);
32a688
 list_item_t *list_get_first(const list_t *list);
32a688
+int list_prepend(list_t *list, const char *index, const char *data);
32a688
 int list_append(list_t *list, const char *index, const char *data);
32a688
 void list_destroy_item(list_item_t **item);
32a688
 void list_empty(list_t *list);
32a688
diff --git a/src/library/rpm-backend.c b/src/library/rpm-backend.c
32a688
index 7f1af438..0887d36a 100644
32a688
--- a/src/library/rpm-backend.c
32a688
+++ b/src/library/rpm-backend.c
32a688
@@ -40,6 +40,8 @@
32a688
 #include "fapolicyd-backend.h"
32a688
 #include "llist.h"
32a688
 
32a688
+#include "rpm-filter.h"
32a688
+
32a688
 static int rpm_init_backend(void);
32a688
 static int rpm_load_list(const conf_t *);
32a688
 static int rpm_destroy_backend(void);
32a688
@@ -176,69 +178,6 @@ static void close_rpm(void)
32a688
 	rpmlogClose();
32a688
 }
32a688
 
32a688
-// This function will check a passed file name to see if the path should
32a688
-// be kept or dropped. 1 means discard it, and 0 means keep it.
32a688
-static int drop_path(const char *file_name)
32a688
-{
32a688
-	const char *p = file_name;
32a688
-	if (!strncmp(p, "/usr", 4)) {
32a688
-		p += 4;
32a688
-
32a688
-		// Drop anything in /usr/include
32a688
-		if (!strncmp(p, "/include", 8))
32a688
-			return 1;
32a688
-
32a688
-		// Only keep languages from /usr/share
32a688
-		if (!strncmp(p, "/share", 6)) {
32a688
-			p += 6;
32a688
-			
32a688
-			// These are roughly ordered by quantity
32a688
-			static const char *arr_share[] = {
32a688
-				"*.py?",       // Python byte code
32a688
-				"*.py",        // Python text files
32a688
-				"*/libexec/*", // Some apps have a private libexec
32a688
-				"*.rb",        // Ruby
32a688
-				"*.pl",        // Perl
32a688
-				"*.stp",       // System tap
32a688
-				"*.js",        // Javascript
32a688
-				"*.jar",       // Java archive
32a688
-				"*.m4",        // M4
32a688
-				"*.php",       // PHP
32a688
-				"*.pm",        // Perl Modules
32a688
-				"*.lua",       // Lua
32a688
-				"*.class",     // Java
32a688
-				"*.ts",        // Typescript
32a688
-				"*.tsx",       // Typescript JSX
32a688
-				"*.el",        // Lisp
32a688
-				"*.elc",       // Compiled Lisp
32a688
-				NULL
32a688
-			};
32a688
-
32a688
-			for (int i = 0; arr_share[i]; ++i)
32a688
-				if (!fnmatch(arr_share[i], p, 0))
32a688
-					return 0;
32a688
-			return 1;
32a688
-		}
32a688
-
32a688
-		// Akmod needs scripts in /usr/src/kernel
32a688
-		if (!strncmp(p, "/src/kernel", 11)) {
32a688
-			p += 11;
32a688
-			
32a688
-			static const char *arr_src_kernel[] = {
32a688
-				"*/scripts/*",
32a688
-				"*/tools/objtool/*",
32a688
-				NULL
32a688
-			};
32a688
-			
32a688
-			for (int i = 0; arr_src_kernel[i]; ++i)
32a688
-				if (!fnmatch(arr_src_kernel[i], p, 0))
32a688
-					return 0;
32a688
-			return 1;
32a688
-		}
32a688
-	}
32a688
-	return 0;
32a688
-}
32a688
-
32a688
 struct _hash_record {
32a688
 	const char * key;
32a688
 	UT_hash_handle hh;
32a688
@@ -290,7 +229,8 @@ static int rpm_load_list(const conf_t *conf)
32a688
 			if (file_name == NULL)
32a688
 				continue;
32a688
 
32a688
-			if (drop_path(file_name)) {
32a688
+			// should we drop a path?
32a688
+			if (!filter_check(file_name)) {
32a688
 				free((void *)file_name);
32a688
 				free((void *)sha);
32a688
 				continue;
32a688
@@ -358,12 +298,23 @@ static int rpm_load_list(const conf_t *conf)
32a688
 
32a688
 static int rpm_init_backend(void)
32a688
 {
32a688
+	if (filter_init())
32a688
+		return 1;
32a688
+
32a688
+	if (filter_load_file()) {
32a688
+		filter_destroy();
32a688
+		return 1;
32a688
+	}
32a688
+
32a688
+
32a688
 	list_init(&rpm_backend.list);
32a688
+
32a688
 	return 0;
32a688
 }
32a688
 
32a688
 static int rpm_destroy_backend(void)
32a688
 {
32a688
+	filter_destroy();
32a688
 	list_empty(&rpm_backend.list);
32a688
 	return 0;
32a688
 }
32a688
diff --git a/src/library/rpm-filter.c b/src/library/rpm-filter.c
32a688
new file mode 100644
32a688
index 00000000..e3e3eb38
32a688
--- /dev/null
32a688
+++ b/src/library/rpm-filter.c
32a688
@@ -0,0 +1,487 @@
32a688
+/*
32a688
+* rpm-filter.c - filter for rpm trust source
32a688
+* Copyright (c) 2023 Red Hat Inc., Durham, North Carolina.
32a688
+* All Rights Reserved.
32a688
+*
32a688
+* This software may be freely redistributed and/or modified under the
32a688
+* terms of the GNU General Public License as published by the Free
32a688
+* Software Foundation; either version 2, or (at your option) any
32a688
+* later version.
32a688
+*
32a688
+* This program is distributed in the hope that it will be useful,
32a688
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
32a688
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32a688
+* GNU General Public License for more details.
32a688
+*
32a688
+* You should have received a copy of the GNU General Public License
32a688
+* along with this program; see the file COPYING. If not, write to the
32a688
+* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
32a688
+* Boston, MA 02110-1335, USA.
32a688
+*
32a688
+* Authors:
32a688
+*   Radovan Sroka <rsroka@redhat.com>
32a688
+*/
32a688
+
32a688
+#include "rpm-filter.h"
32a688
+
32a688
+#include <stdio.h>
32a688
+#include <string.h>
32a688
+#include <ctype.h>
32a688
+#include <fnmatch.h>
32a688
+
32a688
+#include "llist.h"
32a688
+#include "stack.h"
32a688
+#include "message.h"
32a688
+#include "string-util.h"
32a688
+
32a688
+
32a688
+#define RPM_FILTER_FILE "/etc/fapolicyd/rpm-filter.conf"
32a688
+
32a688
+rpm_filter_t *global_filter = NULL;
32a688
+
32a688
+static rpm_filter_t *filter_create_obj(void);
32a688
+static void filter_destroy_obj(rpm_filter_t *_filter);
32a688
+
32a688
+// init fuction of this module
32a688
+int filter_init(void)
32a688
+{
32a688
+	global_filter = filter_create_obj();
32a688
+	if (global_filter == NULL)
32a688
+		return 1;
32a688
+
32a688
+	return 0;
32a688
+}
32a688
+
32a688
+// destroy funtion of this module
32a688
+void filter_destroy(void)
32a688
+{
32a688
+	filter_destroy_obj(global_filter);
32a688
+	global_filter = NULL;
32a688
+}
32a688
+
32a688
+// alocate new filter object and fill with the defaults
32a688
+static rpm_filter_t *filter_create_obj(void)
32a688
+{
32a688
+	rpm_filter_t *filter = malloc(sizeof(rpm_filter_t));
32a688
+	if (filter) {
32a688
+		filter->type = NONE;
32a688
+		filter->path = NULL;
32a688
+		filter->len = 0;
32a688
+		filter->matched = 0;
32a688
+		filter->processed = 0;
32a688
+		list_init(&filter->list);
32a688
+	}
32a688
+	return filter;
32a688
+}
32a688
+
32a688
+// free all nested filters
32a688
+static void filter_destroy_obj(rpm_filter_t *_filter)
32a688
+{
32a688
+	if (_filter == NULL)
32a688
+		return;
32a688
+
32a688
+	rpm_filter_t *filter = _filter;
32a688
+	stack_t stack;
32a688
+	stack_init(&stack);
32a688
+
32a688
+	stack_push(&stack, filter);
32a688
+
32a688
+	while (!stack_is_empty(&stack)) {
32a688
+		filter = (rpm_filter_t*)stack_top(&stack);
32a688
+		if (filter->processed) {
32a688
+			(void)free(filter->path);
32a688
+			// asume that item->data is NULL
32a688
+			list_empty(&filter->list);
32a688
+			(void)free(filter);
32a688
+			stack_pop(&stack);
32a688
+			continue;
32a688
+		}
32a688
+
32a688
+		list_item_t *item = list_get_first(&filter->list);
32a688
+		for (; item != NULL ; item = item->next) {
32a688
+				rpm_filter_t *next_filter = (rpm_filter_t*)item->data;
32a688
+				// we can use list_empty() later
32a688
+				// we dont want to free filter right now
32a688
+				// it will freed after popping
32a688
+				item->data = NULL;
32a688
+				stack_push(&stack, next_filter);
32a688
+		}
32a688
+		filter->processed = 1;
32a688
+	}
32a688
+	stack_destroy(&stack);
32a688
+}
32a688
+
32a688
+// create struct and push it to the top of stack
32a688
+static void stack_push_vars(stack_t *_stack, int _level, int _offset, rpm_filter_t *_filter)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	stack_item_t *item = malloc(sizeof(stack_item_t));
32a688
+	if (item == NULL)
32a688
+		return;
32a688
+
32a688
+	item->level = _level;
32a688
+	item->offset = _offset;
32a688
+	item->filter = _filter;
32a688
+
32a688
+	stack_push(_stack, item);
32a688
+}
32a688
+
32a688
+// pop stack_item_t and free it
32a688
+static void stack_pop_vars(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	stack_item_t * item = (stack_item_t*)stack_top(_stack);
32a688
+	free(item);
32a688
+	stack_pop(_stack);
32a688
+}
32a688
+
32a688
+// pop all the stack_item_t and free them
32a688
+static void stack_pop_all_vars(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	while (!stack_is_empty(_stack))
32a688
+		stack_pop_vars(_stack);
32a688
+}
32a688
+
32a688
+// reset filter to default, pop top and free
32a688
+static void stack_pop_reset(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	stack_item_t *stack_item = (stack_item_t*)stack_top(_stack);
32a688
+	if (stack_item) {
32a688
+		stack_item->filter->matched = 0;
32a688
+		stack_item->filter->processed = 0;
32a688
+	}
32a688
+	free(stack_item);
32a688
+	stack_pop(_stack);
32a688
+}
32a688
+
32a688
+// reset and pop all the stack_item_t
32a688
+static void stack_pop_all_reset(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	while (!stack_is_empty(_stack))
32a688
+		stack_pop_reset(_stack);
32a688
+}
32a688
+
32a688
+// this funtion gets full path and checks it against filter
32a688
+// returns 1 for keeping the file and 0 for dropping it
32a688
+int filter_check(const char *_path)
32a688
+{
32a688
+	if (_path == NULL) {
32a688
+		msg(LOG_ERR, "filter_check: path is NULL, something is wrong!");
32a688
+		return 0;
32a688
+	}
32a688
+
32a688
+	rpm_filter_t *filter = global_filter;
32a688
+	char *path = strdup(_path);
32a688
+	size_t path_len = strlen(_path);
32a688
+	size_t offset = 0;
32a688
+	// Create a stack to store the filters that need to be checked
32a688
+	stack_t stack;
32a688
+	stack_init(&stack);
32a688
+
32a688
+	int res = 0;
32a688
+	int level = 0;
32a688
+
32a688
+	stack_push_vars(&stack, level, offset, filter);
32a688
+
32a688
+	while(!stack_is_empty(&stack)) {
32a688
+		int matched = 0;
32a688
+		filter->processed = 1;
32a688
+
32a688
+		// this is starting branch of the algo
32a688
+		// assuming that in root filter filter->path is NULL
32a688
+		if (filter->path == NULL) {
32a688
+			list_item_t *item = list_get_first(&filter->list);
32a688
+			// push all the descendants to the stack
32a688
+			for (; item != NULL ; item = item->next) {
32a688
+				rpm_filter_t *next_filter = (rpm_filter_t*)item->data;
32a688
+				stack_push_vars(&stack, level+1, offset, next_filter);
32a688
+			}
32a688
+
32a688
+		// usual branch, start with processing
32a688
+		} else {
32a688
+			// wildcard contition
32a688
+			char *is_wildcard = strpbrk(filter->path, "?*[");
32a688
+			if (is_wildcard) {
32a688
+				int count = 0;
32a688
+				char *filter_lim, *filter_old_lim;
32a688
+				filter_lim = filter_old_lim = filter->path;
32a688
+
32a688
+				char *path_lim, *path_old_lim;
32a688
+				path_lim = path_old_lim = path+offset;
32a688
+
32a688
+				// there can be wildcard in the dir name as well
32a688
+				// we need to count how many chars can be eaten by wildcard
32a688
+				while(1) {
32a688
+					filter_lim = strchr(filter_lim, '/');
32a688
+					path_lim = strchr(path_lim, '/');
32a688
+
32a688
+					if (filter_lim) {
32a688
+						count++;
32a688
+						filter_old_lim = filter_lim;
32a688
+						filter_lim++;
32a688
+					} else
32a688
+						break;
32a688
+
32a688
+					if (path_lim) {
32a688
+						path_old_lim = path_lim;
32a688
+						path_lim++;
32a688
+					} else
32a688
+						break;
32a688
+
32a688
+				}
32a688
+				// put 0 after the last /
32a688
+				char tmp = '\0';
32a688
+				if (count && *(filter_old_lim+1) == '\0') {
32a688
+					 tmp = *(path_old_lim+1);
32a688
+					*(path_old_lim+1) = '\0';
32a688
+				}
32a688
+
32a688
+				// check fnmatch
32a688
+				matched = !fnmatch(filter->path, path+offset, 0);
32a688
+
32a688
+				// and set back
32a688
+				if (count && *(filter_old_lim+1) == '\0')
32a688
+					*(path_old_lim+1) = tmp;
32a688
+
32a688
+				if (matched) {
32a688
+					offset = path_old_lim - path+offset;
32a688
+				}
32a688
+			} else {
32a688
+				// match normal path or just specific part of it
32a688
+				matched = !strncmp(path+offset, filter->path, filter->len);
32a688
+				if (matched)
32a688
+					offset += filter->len;
32a688
+			}
32a688
+
32a688
+			if (matched) {
32a688
+				level++;
32a688
+				filter->matched = 1;
32a688
+
32a688
+				// if matched we need ot push descendants to the stack
32a688
+				list_item_t *item = list_get_first(&filter->list);
32a688
+
32a688
+				// if there are no descendants and it is a wildcard then it's a match
32a688
+				if (item == NULL && is_wildcard) {
32a688
+					// if '+' ret 1 and if '-' ret 0
32a688
+					res = filter->type == ADD ? 1 : 0;
32a688
+					goto end;
32a688
+				}
32a688
+
32a688
+				// no descendants, and already compared whole path string so its a match
32a688
+				if (item == NULL && path_len == offset) {
32a688
+					// if '+' ret 1 and if '-' ret 0
32a688
+					res = filter->type == ADD ? 1 : 0;
32a688
+					goto end;
32a688
+				}
32a688
+
32a688
+				// push descendants to the stack
32a688
+				for (; item != NULL ; item = item->next) {
32a688
+					rpm_filter_t *next_filter = (rpm_filter_t*)item->data;
32a688
+					stack_push_vars(&stack, level, offset, next_filter);
32a688
+				}
32a688
+
32a688
+			}
32a688
+
32a688
+		}
32a688
+
32a688
+		stack_item_t * stack_item = NULL;
32a688
+		// popping processed filters from the top of the stack
32a688
+		do {
32a688
+			if (stack_item) {
32a688
+				filter = stack_item->filter;
32a688
+				offset = stack_item->offset;
32a688
+				level = stack_item->level;
32a688
+
32a688
+				// assuimg that nothing has matched on the upper level so it's a directory match
32a688
+				if (filter->matched && filter->path[filter->len-1] == '/') {
32a688
+					res = filter->type == ADD ? 1 : 0;
32a688
+					goto end;
32a688
+				}
32a688
+
32a688
+				// reset processed flag
32a688
+				stack_pop_reset(&stack);
32a688
+			}
32a688
+
32a688
+			stack_item = (stack_item_t*)stack_top(&stack);
32a688
+		} while(stack_item && stack_item->filter->processed);
32a688
+
32a688
+		if (!stack_item)
32a688
+			break;
32a688
+
32a688
+		filter = stack_item->filter;
32a688
+		offset = stack_item->offset;
32a688
+		level = stack_item->level;
32a688
+	}
32a688
+
32a688
+end:
32a688
+	// Clean up the stack
32a688
+	stack_pop_all_reset(&stack);
32a688
+	stack_destroy(&stack);
32a688
+	free(path);
32a688
+	return res;
32a688
+}
32a688
+
32a688
+// load rpm filter configuration file and fill the filter structure
32a688
+int filter_load_file(void)
32a688
+{
32a688
+	int res = 0;
32a688
+	FILE *stream = fopen(RPM_FILTER_FILE, "r");
32a688
+
32a688
+	if (stream == NULL) {
32a688
+		msg(LOG_ERR, "Cannot open filter file %s", RPM_FILTER_FILE);
32a688
+		return 1;
32a688
+	}
32a688
+
32a688
+	ssize_t nread;
32a688
+	size_t len = 0;
32a688
+	char * line = NULL;
32a688
+	long line_number = 0;
32a688
+	int last_level = 0;
32a688
+
32a688
+	stack_t stack;
32a688
+	stack_init(&stack);
32a688
+	stack_push_vars(&stack, last_level, 0, global_filter);
32a688
+
32a688
+	while ((nread = getline(&line, &len, stream)) != -1) {
32a688
+		line_number++;
32a688
+
32a688
+		if (line[0] == '\0' || line[0] == '\n') {
32a688
+			free(line);
32a688
+			line = NULL;
32a688
+			continue;
32a688
+		}
32a688
+
32a688
+		// get rid of the new line char
32a688
+		char * new_line = strchr(line, '\n');
32a688
+		if (new_line) {
32a688
+			*new_line = '\0';
32a688
+			len--;
32a688
+		}
32a688
+
32a688
+		int level = 1;
32a688
+		char * rest = line;
32a688
+		rpm_filter_type_t type = NONE;
32a688
+
32a688
+		for (size_t i = 0 ; i < len ; i++) {
32a688
+			switch (line[i]) {
32a688
+				case ' ':
32a688
+					level++;
32a688
+					continue;
32a688
+				case '+':
32a688
+					type = ADD;
32a688
+					break;
32a688
+				case '-':
32a688
+					type = SUB;
32a688
+					break;
32a688
+				case '#':
32a688
+					type = COMMENT;
32a688
+					break;
32a688
+				default:
32a688
+					type = BAD;
32a688
+					break;
32a688
+			}
32a688
+
32a688
+			// continue with next char
32a688
+			// skip + and space
32a688
+			rest = fapolicyd_strtrim(&(line[i+2]));
32a688
+			break;
32a688
+		}
32a688
+
32a688
+		// ignore comment
32a688
+		if (type == COMMENT) {
32a688
+			free(line);
32a688
+			line = NULL;
32a688
+			continue;
32a688
+		}
32a688
+
32a688
+		// if something bad return error
32a688
+		if (type == BAD) {
32a688
+			msg(LOG_ERR, "filter_load_file: cannot parse line number %ld, \"%s\"", line_number, line);
32a688
+			free(line);
32a688
+			line = NULL;
32a688
+			goto bad;
32a688
+		}
32a688
+
32a688
+		rpm_filter_t * filter = filter_create_obj();
32a688
+
32a688
+		if (filter) {
32a688
+			filter->path = strdup(rest);
32a688
+			filter->len = strlen(filter->path);
32a688
+			filter->type = type;
32a688
+		}
32a688
+
32a688
+		// comparing level of indetantion between the last line and the current one
32a688
+		last_level = ((stack_item_t*)stack_top(&stack))->level;
32a688
+		if (level == last_level) {
32a688
+
32a688
+			// since we are at the same level as filter before
32a688
+			// we need to pop the previous filter from the top
32a688
+			stack_pop_vars(&stack);
32a688
+
32a688
+			// pushing filter to the list of top's children list
32a688
+			list_prepend(&((stack_item_t*)stack_top(&stack))->filter->list, NULL, (void*)filter);
32a688
+
32a688
+			// pushing filter to the top of the stack
32a688
+			stack_push_vars(&stack, level, 0, filter);
32a688
+
32a688
+		} else if (level == last_level + 1) {
32a688
+			// this filter has higher level tha privious one
32a688
+			// we wont do pop just push
32a688
+
32a688
+			// pushing filter to the list of top's children list
32a688
+			list_prepend(&((stack_item_t*)stack_top(&stack))->filter->list, NULL, (void*)filter);
32a688
+
32a688
+			// pushing filter to the top of the stack
32a688
+			stack_push_vars(&stack, level, 0, filter);
32a688
+
32a688
+		} else if (level < last_level){
32a688
+			// level of indentation dropped
32a688
+			// we need to pop
32a688
+			// +1 is meant for getting rid of the current level so we can again push
32a688
+			for (int i = 0 ; i < last_level - level + 1; i++) {
32a688
+				stack_pop_vars(&stack);
32a688
+			}
32a688
+
32a688
+			// pushing filter to the list of top's children list
32a688
+			list_prepend(&((stack_item_t*)stack_top(&stack))->filter->list, NULL, (void*)filter);
32a688
+
32a688
+			// pushing filter to the top of the stack
32a688
+			stack_push_vars(&stack, level, 0, filter);
32a688
+
32a688
+		} else {
32a688
+			msg(LOG_ERR, "filter_load_file: paring error line: %ld, \"%s\"", line_number, line);
32a688
+			filter_destroy_obj(filter);
32a688
+			free(line);
32a688
+			goto bad;
32a688
+		}
32a688
+
32a688
+		free(line);
32a688
+		line = NULL;
32a688
+	}
32a688
+
32a688
+	goto good;
32a688
+bad:
32a688
+	res = 1;
32a688
+
32a688
+good:
32a688
+	fclose(stream);
32a688
+	stack_pop_all_vars(&stack);
32a688
+	stack_destroy(&stack);
32a688
+	if (global_filter->list.count == 0) {
32a688
+		msg(LOG_ERR, "filter_load_file: no valid filter provided in %s", RPM_FILTER_FILE);
32a688
+	}
32a688
+	return res;
32a688
+}
32a688
diff --git a/src/library/rpm-filter.h b/src/library/rpm-filter.h
32a688
new file mode 100644
32a688
index 00000000..2c49d338
32a688
--- /dev/null
32a688
+++ b/src/library/rpm-filter.h
32a688
@@ -0,0 +1,67 @@
32a688
+/*
32a688
+* rpm-filter.h - Header for rpm filter implementation
32a688
+* Copyright (c) 2023 Red Hat Inc., Durham, North Carolina.
32a688
+* All Rights Reserved.
32a688
+*
32a688
+* This software may be freely redistributed and/or modified under the
32a688
+* terms of the GNU General Public License as published by the Free
32a688
+* Software Foundation; either version 2, or (at your option) any
32a688
+* later version.
32a688
+*
32a688
+* This program is distributed in the hope that it will be useful,
32a688
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
32a688
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32a688
+* GNU General Public License for more details.
32a688
+*
32a688
+* You should have received a copy of the GNU General Public License
32a688
+* along with this program; see the file COPYING. If not, write to the
32a688
+* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
32a688
+* Boston, MA 02110-1335, USA.
32a688
+*
32a688
+* Authors:
32a688
+*   Radovan Sroka <rsroka@redhat.com>
32a688
+*/
32a688
+
32a688
+#ifndef FILTER_H_
32a688
+#define FILTER_H_
32a688
+
32a688
+#include <stdlib.h>
32a688
+#include <stddef.h>
32a688
+
32a688
+#include "llist.h"
32a688
+
32a688
+typedef enum rpm_filter_type
32a688
+{
32a688
+	NONE,
32a688
+	ADD,
32a688
+	SUB,
32a688
+	COMMENT,
32a688
+	BAD,
32a688
+} rpm_filter_type_t;
32a688
+
32a688
+typedef struct _rpm_filter
32a688
+{
32a688
+	rpm_filter_type_t type;
32a688
+	char * path;
32a688
+	size_t len;
32a688
+	int processed;
32a688
+	int matched;
32a688
+	list_t list;
32a688
+} rpm_filter_t;
32a688
+
32a688
+
32a688
+typedef struct _stack_item
32a688
+{
32a688
+	int level;
32a688
+	int offset;
32a688
+	rpm_filter_t *filter;
32a688
+} stack_item_t;
32a688
+
32a688
+
32a688
+int filter_init(void);
32a688
+void filter_destroy(void);
32a688
+int filter_check(const char *_path);
32a688
+int filter_load_file(void);
32a688
+
32a688
+
32a688
+#endif // FILTER_H_
32a688
diff --git a/src/library/stack.c b/src/library/stack.c
32a688
new file mode 100644
32a688
index 00000000..93141b2c
32a688
--- /dev/null
32a688
+++ b/src/library/stack.c
32a688
@@ -0,0 +1,89 @@
32a688
+/*
32a688
+* stack.c - generic stack impementation
32a688
+* Copyright (c) 2023 Red Hat Inc., Durham, North Carolina.
32a688
+* All Rights Reserved.
32a688
+*
32a688
+* This software may be freely redistributed and/or modified under the
32a688
+* terms of the GNU General Public License as published by the Free
32a688
+* Software Foundation; either version 2, or (at your option) any
32a688
+* later version.
32a688
+*
32a688
+* This program is distributed in the hope that it will be useful,
32a688
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
32a688
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32a688
+* GNU General Public License for more details.
32a688
+*
32a688
+* You should have received a copy of the GNU General Public License
32a688
+* along with this program; see the file COPYING. If not, write to the
32a688
+* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
32a688
+* Boston, MA 02110-1335, USA.
32a688
+*
32a688
+* Authors:
32a688
+*   Radovan Sroka <rsroka@redhat.com>
32a688
+*/
32a688
+
32a688
+#include "stack.h"
32a688
+#include <stddef.h>
32a688
+
32a688
+// init of the stack struct
32a688
+void stack_init(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	list_init(_stack);
32a688
+}
32a688
+
32a688
+// free all the resources from the stack
32a688
+void stack_destroy(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	list_empty(_stack);
32a688
+}
32a688
+
32a688
+// push to the top of the stack
32a688
+void stack_push(stack_t *_stack, void *_data)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	list_prepend(_stack, NULL, (void *)_data);
32a688
+}
32a688
+
32a688
+// pop the the top without returning what was on the top
32a688
+void stack_pop(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return;
32a688
+
32a688
+	list_item_t *first = _stack->first;
32a688
+	_stack->first = first->next;
32a688
+	first->data = NULL;
32a688
+	list_destroy_item(&first);
32a688
+	_stack->count--;
32a688
+
32a688
+	return;
32a688
+}
32a688
+
32a688
+// function returns 1 if stack is emtpy 0 if it's not
32a688
+int stack_is_empty(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return -1;
32a688
+
32a688
+	if (_stack->count == 0)
32a688
+		return 1;
32a688
+
32a688
+	return 0;
32a688
+}
32a688
+
32a688
+// return top of the stack without popping
32a688
+const void *stack_top(stack_t *_stack)
32a688
+{
32a688
+	if (_stack == NULL)
32a688
+		return NULL;
32a688
+
32a688
+	return _stack->first ? _stack->first->data : NULL;
32a688
+}
32a688
diff --git a/src/library/stack.h b/src/library/stack.h
32a688
new file mode 100644
32a688
index 00000000..042476e3
32a688
--- /dev/null
32a688
+++ b/src/library/stack.h
32a688
@@ -0,0 +1,41 @@
32a688
+/*
32a688
+* stack.h - header for generic stack implementation
32a688
+* Copyright (c) 2023 Red Hat Inc., Durham, North Carolina.
32a688
+* All Rights Reserved.
32a688
+*
32a688
+* This software may be freely redistributed and/or modified under the
32a688
+* terms of the GNU General Public License as published by the Free
32a688
+* Software Foundation; either version 2, or (at your option) any
32a688
+* later version.
32a688
+*
32a688
+* This program is distributed in the hope that it will be useful,
32a688
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
32a688
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32a688
+* GNU General Public License for more details.
32a688
+*
32a688
+* You should have received a copy of the GNU General Public License
32a688
+* along with this program; see the file COPYING. If not, write to the
32a688
+* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
32a688
+* Boston, MA 02110-1335, USA.
32a688
+*
32a688
+* Authors:
32a688
+*   Radovan Sroka <rsroka@redhat.com>
32a688
+*/
32a688
+
32a688
+
32a688
+#ifndef STACK_H_
32a688
+#define STACK_H_
32a688
+
32a688
+#include "llist.h"
32a688
+
32a688
+typedef list_t stack_t;
32a688
+
32a688
+void stack_init(stack_t *_stack);
32a688
+void stack_destroy(stack_t *_stack);
32a688
+void stack_push(stack_t *_stack, void *_data);
32a688
+void stack_pop(stack_t *_stack);
32a688
+int stack_is_empty(stack_t *_stack);
32a688
+const void *stack_top(stack_t *_stack);
32a688
+
32a688
+
32a688
+#endif // STACK_H_