Blame SOURCES/fapolicyd-exclude-list.patch

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