From 22eea44401c74e50eb55e0dcb6503e1596cbc01b Mon Sep 17 00:00:00 2001 From: zgzxx Date: Wed, 22 Mar 2023 11:26:35 +0800 Subject: [PATCH] backport patches from upstream --- ...ount-temporary-bind-mounts-on-SIGINT.patch | 86 ++++++++++++++ ...ential-NULL-reference-in-load_checks.patch | 38 ++++++ ...atabase-if-the-fcontext-is-non-local.patch | 65 ++++++++++ ...olicy-Cache-conditional-rule-queries.patch | 74 ++++++++++++ ...cy-add-missing-booleans-to-man-pages.patch | 112 ++++++++++++++++++ ...-Call-os.makedirs-with-exist_ok-True.patch | 39 ++++++ policycoreutils.spec | 11 +- 7 files changed, 424 insertions(+), 1 deletion(-) create mode 100644 backport-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch create mode 100644 backport-policycoreutils-fix-potential-NULL-reference-in-load_checks.patch create mode 100644 backport-python-Do-not-query-the-local-database-if-the-fcontext-is-non-local.patch create mode 100644 backport-python-sepolicy-Cache-conditional-rule-queries.patch create mode 100644 backport-python-sepolicy-add-missing-booleans-to-man-pages.patch create mode 100644 backport-sepolicy-Call-os.makedirs-with-exist_ok-True.patch diff --git a/backport-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch b/backport-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch new file mode 100644 index 0000000..9aee05b --- /dev/null +++ b/backport-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch @@ -0,0 +1,86 @@ +From 25d7941aee870c16264e7c3dfe7f48b9efbf524f Mon Sep 17 00:00:00 2001 +From: Petr Lautrbach +Date: Mon, 7 Nov 2022 10:25:05 +0100 +Subject: [PATCH] fixfiles: Unmount temporary bind mounts on SIGINT +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +`fixfiles -M relabel` temporary bind mounts file systems before +relabeling, but it left the / directory mounted in /tmp/tmp.XXXX when a +user hit CTRL-C. It means that if the user run `fixfiles -M relabel` +again and answered Y to clean out /tmp directory, it would remove all +data from mounted fs. + +This patch changes the location where `fixfiles` mounts fs to /run, uses +private mount namespace via unshare and adds a handler for exit signals +which tries to umount fs mounted by `fixfiles`. + +Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2125355 + +Signed-off-by: Petr Lautrbach +Tested-by: Christian Göttsche +Acked-by: James Carter + +Conflict adapt context, delete THREADS +--- + policycoreutils/scripts/fixfiles | 36 +++++++++++++++++++++++++------- + 1 file changed, 28 insertions(+), 8 deletions(-) + +diff --git a/policycoreutils/scripts/fixfiles b/policycoreutils/scripts/fixfiles +index c72ca0eb..166af6f3 100755 +--- a/policycoreutils/scripts/fixfiles ++++ b/policycoreutils/scripts/fixfiles +@@ -207,6 +207,25 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' ' + [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr + } + ++# unmount tmp bind mount before exit ++umount_TMP_MOUNT() { ++ if [ -n "$TMP_MOUNT" ]; then ++ umount "${TMP_MOUNT}${m}" || exit 130 ++ rm -rf "${TMP_MOUNT}" || echo "Error cleaning up." ++ fi ++ exit 130 ++} ++ ++fix_labels_on_mountpoint() { ++ test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1 ++ mkdir -p "${TMP_MOUNT}${m}" || exit 1 ++ mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1 ++ ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}" ++ umount "${TMP_MOUNT}${m}" || exit 1 ++ rm -rf "${TMP_MOUNT}" || echo "Error cleaning up." ++} ++export -f fix_labels_on_mountpoint ++ + # + # restore + # if called with -n will only check file context +@@ -252,14 +271,15 @@ case "$RESTORE_MODE" in + # we bind mount so we can fix the labels of files that have already been + # mounted over + for m in `echo $FILESYSTEMSRW`; do +- TMP_MOUNT="$(mktemp -d)" +- test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1 +- +- mkdir -p "${TMP_MOUNT}${m}" || exit 1 +- mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1 +- ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}" +- umount "${TMP_MOUNT}${m}" || exit 1 +- rm -rf "${TMP_MOUNT}" || echo "Error cleaning up." ++ TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)" ++ export SETFILES VERBOSE EXCLUDEDIRS FORCEFLAG THREADS FC TMP_MOUNT m ++ if type unshare &> /dev/null; then ++ unshare -m bash -c "fix_labels_on_mountpoint $*" || exit $? ++ else ++ trap umount_TMP_MOUNT EXIT ++ fix_labels_on_mountpoint $* ++ trap EXIT ++ fi + done; + fi + else +-- +2.27.0 + diff --git a/backport-policycoreutils-fix-potential-NULL-reference-in-load_checks.patch b/backport-policycoreutils-fix-potential-NULL-reference-in-load_checks.patch new file mode 100644 index 0000000..8c33426 --- /dev/null +++ b/backport-policycoreutils-fix-potential-NULL-reference-in-load_checks.patch @@ -0,0 +1,38 @@ +From 1fe82e5cf581158cdfa184c64218b0bade82b01a Mon Sep 17 00:00:00 2001 +From: Jie Lu +Date: Mon, 5 Dec 2022 17:36:44 +0800 +Subject: [PATCH] policycoreutils: fix potential NULL reference in load_checks + +In load_checks(), add return check for malloc() to avoid NULL reference. + +Signed-off-by: Jie Lu +Acked-by: James Carter +--- + policycoreutils/sestatus/sestatus.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/policycoreutils/sestatus/sestatus.c b/policycoreutils/sestatus/sestatus.c +index 7dcc9944..6c95828e 100644 +--- a/policycoreutils/sestatus/sestatus.c ++++ b/policycoreutils/sestatus/sestatus.c +@@ -140,6 +140,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) + pc[*npc] = + (char *)malloc((buf_len) * + sizeof(char)); ++ if (!pc[*npc]) ++ break; + memcpy(pc[*npc], bufp, buf_len); + (*npc)++; + bufp = NULL; +@@ -150,6 +152,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) + fc[*nfc] = + (char *)malloc((buf_len) * + sizeof(char)); ++ if (!fc[*nfc]) ++ break; + memcpy(fc[*nfc], bufp, buf_len); + (*nfc)++; + bufp = NULL; +-- +2.27.0 + diff --git a/backport-python-Do-not-query-the-local-database-if-the-fcontext-is-non-local.patch b/backport-python-Do-not-query-the-local-database-if-the-fcontext-is-non-local.patch new file mode 100644 index 0000000..6bc860c --- /dev/null +++ b/backport-python-Do-not-query-the-local-database-if-the-fcontext-is-non-local.patch @@ -0,0 +1,65 @@ +From 7238ad32a3171d82bba9b99660e55399161236fc Mon Sep 17 00:00:00 2001 +From: James Carter +Date: Wed, 19 Oct 2022 14:20:11 -0400 +Subject: [PATCH] python: Do not query the local database if the fcontext is + non-local + +Vit Mojzis reports that an error message is produced when modifying +a non-local fcontext. + +He gives the following example: + # semanage fcontext -f f -m -t passwd_file_t /etc/security/opasswd + libsemanage.dbase_llist_query: could not query record value (No such file or directory). + +When modifying an fcontext, the non-local database is checked for the +key and then, if it is not found there, the local database is checked. +If the key doesn't exist, then an error is raised. If the key exists +then the local database is queried first and, if that fails, the non- +local database is queried. + +The error is from querying the local database when the fcontext is in +the non-local database. + +Instead, if the fcontext is in the non-local database, just query +the non-local database. Only query the local database if the +fcontext was found in it. + +Reported-by: Vit Mojzis +Signed-off-by: James Carter +--- + python/semanage/seobject.py | 15 +++++++++------ + 1 file changed, 9 insertions(+), 6 deletions(-) + +diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py +index 0782c082..d82da494 100644 +--- a/python/semanage/seobject.py ++++ b/python/semanage/seobject.py +@@ -2504,16 +2504,19 @@ class fcontextRecords(semanageRecords): + (rc, exists) = semanage_fcontext_exists(self.sh, k) + if rc < 0: + raise ValueError(_("Could not check if file context for %s is defined") % target) +- if not exists: ++ if exists: ++ try: ++ (rc, fcontext) = semanage_fcontext_query(self.sh, k) ++ except OSError: ++ raise ValueError(_("Could not query file context for %s") % target) ++ else: + (rc, exists) = semanage_fcontext_exists_local(self.sh, k) ++ if rc < 0: ++ raise ValueError(_("Could not check if file context for %s is defined") % target) + if not exists: + raise ValueError(_("File context for %s is not defined") % target) +- +- try: +- (rc, fcontext) = semanage_fcontext_query_local(self.sh, k) +- except OSError: + try: +- (rc, fcontext) = semanage_fcontext_query(self.sh, k) ++ (rc, fcontext) = semanage_fcontext_query_local(self.sh, k) + except OSError: + raise ValueError(_("Could not query file context for %s") % target) + +-- +2.27.0 + diff --git a/backport-python-sepolicy-Cache-conditional-rule-queries.patch b/backport-python-sepolicy-Cache-conditional-rule-queries.patch new file mode 100644 index 0000000..e786ffa --- /dev/null +++ b/backport-python-sepolicy-Cache-conditional-rule-queries.patch @@ -0,0 +1,74 @@ +From d8eb8b309f7a3e11bdcf86505ed24c1d50007213 Mon Sep 17 00:00:00 2001 +From: Vit Mojzis +Date: Mon, 30 Jan 2023 18:58:28 +0100 +Subject: [PATCH] python/sepolicy: Cache conditional rule queries + +Commit 7506771e4b630fe0ab853f96574e039055cb72eb +"add missing booleans to man pages" dramatically slowed down +"sepolicy manpage -a" by removing caching of setools rule query. +Re-add said caching and update the query to only return conditional +rules. + +Before commit 7506771e: + #time sepolicy manpage -a + real 1m43.153s + # time sepolicy manpage -d httpd_t + real 0m4.493s + +After commit 7506771e: + #time sepolicy manpage -a + real 1h56m43.153s + # time sepolicy manpage -d httpd_t + real 0m8.352s + +After this commit: + #time sepolicy manpage -a + real 1m41.074s + # time sepolicy manpage -d httpd_t + real 0m7.358s + +Signed-off-by: Vit Mojzis +Acked-by: James Carter +--- + python/sepolicy/sepolicy/__init__.py | 11 ++++++++++- + 1 file changed, 10 insertions(+), 1 deletion(-) + +diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py +index e2d5c11a..c177cdfc 100644 +--- a/python/sepolicy/sepolicy/__init__.py ++++ b/python/sepolicy/sepolicy/__init__.py +@@ -125,6 +125,7 @@ all_attributes = None + booleans = None + booleans_dict = None + all_allow_rules = None ++all_bool_rules = None + all_transitions = None + + +@@ -1136,6 +1137,14 @@ def get_all_allow_rules(): + all_allow_rules = search([ALLOW]) + return all_allow_rules + ++def get_all_bool_rules(): ++ global all_bool_rules ++ if not all_bool_rules: ++ q = TERuleQuery(_pol, boolean=".*", boolean_regex=True, ++ ruletype=[ALLOW, DONTAUDIT]) ++ all_bool_rules = [_setools_rule_to_dict(x) for x in q.results()] ++ return all_bool_rules ++ + def get_all_transitions(): + global all_transitions + if not all_transitions: +@@ -1146,7 +1155,7 @@ def get_bools(setype): + bools = [] + domainbools = [] + domainname, short_name = gen_short_name(setype) +- for i in map(lambda x: x['booleans'], filter(lambda x: 'booleans' in x and x['source'] == setype, search([ALLOW, DONTAUDIT]))): ++ for i in map(lambda x: x['booleans'], filter(lambda x: 'booleans' in x and x['source'] == setype, get_all_bool_rules())): + for b in i: + if not isinstance(b, tuple): + continue +-- +2.27.0 + diff --git a/backport-python-sepolicy-add-missing-booleans-to-man-pages.patch b/backport-python-sepolicy-add-missing-booleans-to-man-pages.patch new file mode 100644 index 0000000..6eed870 --- /dev/null +++ b/backport-python-sepolicy-add-missing-booleans-to-man-pages.patch @@ -0,0 +1,112 @@ +From 7506771e4b630fe0ab853f96574e039055cb72eb Mon Sep 17 00:00:00 2001 +From: Vit Mojzis +Date: Tue, 10 Jan 2023 11:37:26 +0100 +Subject: [PATCH] python/sepolicy: add missing booleans to man pages + +get_bools should return a list of booleans that can affect given type, +but it did not handle non trivial conditional statements properly +(returning the whole conditional statement instead of a list of booleans +in the statement). + +e.g. for +allow httpd_t spamc_t:process transition; [ httpd_can_check_spam && httpd_can_sendmail ]:True +get_bools used to return [("httpd_can_check_spam && httpd_can_sendmail", False)] instead of +[("httpd_can_check_spam", False), ("httpd_can_sendmail", False)] + +- rename "boolean" in sepolicy rule dictionary to "booleans" to suggest + it can contain multiple values and make sure it is populated correctly +- add "conditional" key to the rule dictionary to accommodate + get_conditionals, which requires the whole conditional statement +- extend get_bools search to dontaudit rules so that it covers booleans + like httpd_dontaudit_search_dirs + +Note: get_bools uses security_get_boolean_active to get the boolean + value, but the value is later used to represent the default. + Not ideal, but I'm not aware of a way to get the actual defaults. + +Fixes: + "sepolicy manpage" generates man pages that are missing booleans + which are included in non trivial conditional expressions + e.g. httpd_selinux(8) does not include httpd_can_check_spam, + httpd_tmp_exec, httpd_unified, or httpd_use_gpg + + This fix, however, also adds some not strictly related booleans + to some man pages. e.g. use_nfs_home_dirs and + use_samba_home_dirs are added to httpd_selinux(8) + +Signed-off-by: Vit Mojzis +Acked-by: Jason Zaman +--- + python/sepolicy/sepolicy/__init__.py | 21 +++++++++++++-------- + 1 file changed, 13 insertions(+), 8 deletions(-) + +diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py +index be89dd2f..e2d5c11a 100644 +--- a/python/sepolicy/sepolicy/__init__.py ++++ b/python/sepolicy/sepolicy/__init__.py +@@ -335,7 +335,12 @@ def _setools_rule_to_dict(rule): + pass + + try: +- d['boolean'] = [(str(rule.conditional), enabled)] ++ d['booleans'] = [(str(b), b.state) for b in rule.conditional.booleans] ++ except AttributeError: ++ pass ++ ++ try: ++ d['conditional'] = str(rule.conditional) + except AttributeError: + pass + +@@ -440,12 +445,12 @@ def get_conditionals(src, dest, tclass, perm): + x['source'] in src_list and + x['target'] in dest_list and + set(perm).issubset(x[PERMS]) and +- 'boolean' in x, ++ 'conditional' in x, + get_all_allow_rules())) + + try: + for i in allows: +- tdict.update({'source': i['source'], 'boolean': i['boolean']}) ++ tdict.update({'source': i['source'], 'conditional': (i['conditional'], i['enabled'])}) + if tdict not in tlist: + tlist.append(tdict) + tdict = {} +@@ -459,10 +464,10 @@ def get_conditionals_format_text(cond): + + enabled = False + for x in cond: +- if x['boolean'][0][1]: ++ if x['conditional'][1]: + enabled = True + break +- return _("-- Allowed %s [ %s ]") % (enabled, " || ".join(set(map(lambda x: "%s=%d" % (x['boolean'][0][0], x['boolean'][0][1]), cond)))) ++ return _("-- Allowed %s [ %s ]") % (enabled, " || ".join(set(map(lambda x: "%s=%d" % (x['conditional'][0], x['conditional'][1]), cond)))) + + + def get_types_from_attribute(attribute): +@@ -716,9 +721,9 @@ def get_boolean_rules(setype, boolean): + boollist = [] + permlist = search([ALLOW], {'source': setype}) + for p in permlist: +- if "boolean" in p: ++ if "booleans" in p: + try: +- for b in p["boolean"]: ++ for b in p["booleans"]: + if boolean in b: + boollist.append(p) + except: +@@ -1141,7 +1146,7 @@ def get_bools(setype): + bools = [] + domainbools = [] + domainname, short_name = gen_short_name(setype) +- for i in map(lambda x: x['boolean'], filter(lambda x: 'boolean' in x and x['source'] == setype, get_all_allow_rules())): ++ for i in map(lambda x: x['booleans'], filter(lambda x: 'booleans' in x and x['source'] == setype, search([ALLOW, DONTAUDIT]))): + for b in i: + if not isinstance(b, tuple): + continue +-- +2.27.0 + diff --git a/backport-sepolicy-Call-os.makedirs-with-exist_ok-True.patch b/backport-sepolicy-Call-os.makedirs-with-exist_ok-True.patch new file mode 100644 index 0000000..297d812 --- /dev/null +++ b/backport-sepolicy-Call-os.makedirs-with-exist_ok-True.patch @@ -0,0 +1,39 @@ +From 7ff1d7f1c2a141a24a2af5db01fff07754ba18bc Mon Sep 17 00:00:00 2001 +From: Petr Lautrbach +Date: Mon, 12 Dec 2022 18:43:49 +0100 +Subject: [PATCH] sepolicy: Call os.makedirs() with exist_ok=True + +Since commit 7494bb1298b3 ("sepolicy: generate man pages in parallel") +man pages are generated in parallel and there's a race between +os.path.exists() and os.makedirs(). + +The check os.path.exists() is not necessary when os.makedirs() is called +with exist_ok=True. + +Fixes: +/usr/bin/sepolicy manpage -a -p /__w/usr/share/man/man8/ -w -r /__w/ +FileExistsError: [Errno 17] File exists: '/__w/usr/share/man/man8/' + +Signed-off-by: Petr Lautrbach +Acked-by: James Carter +--- + python/sepolicy/sepolicy/manpage.py | 3 +-- + 1 file changed, 1 insertion(+), 2 deletions(-) + +diff --git a/python/sepolicy/sepolicy/manpage.py b/python/sepolicy/sepolicy/manpage.py +index edeb3b77..1bff8f9a 100755 +--- a/python/sepolicy/sepolicy/manpage.py ++++ b/python/sepolicy/sepolicy/manpage.py +@@ -376,8 +376,7 @@ class ManPage: + + self.fcdict = sepolicy.get_fcdict(self.fcpath) + +- if not os.path.exists(path): +- os.makedirs(path) ++ os.makedirs(path, exist_ok=True) + + self.path = path + +-- +2.27.0 + diff --git a/policycoreutils.spec b/policycoreutils.spec index c72a6b1..85252b8 100644 --- a/policycoreutils.spec +++ b/policycoreutils.spec @@ -3,7 +3,7 @@ Name: policycoreutils Version: 3.3 -Release: 5 +Release: 6 Summary: Policy core utilities of selinux License: GPLv2 URL: https://github.com/SELinuxProject @@ -22,6 +22,12 @@ Patch6002: backport-newrole-ensure-password-memory-erasure.patch Patch6003: backport-semodule_package-Close-leaking-fd.patch Patch6004: backport-python-Split-semanage-import-into-two-transactions.patch Patch6005: backport-python-audit2allow-close-file-stream-on-error.patch +Patch6006: backport-python-Do-not-query-the-local-database-if-the-fcontext-is-non-local.patch +Patch6007: backport-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch +Patch6008: backport-sepolicy-Call-os.makedirs-with-exist_ok-True.patch +Patch6009: backport-policycoreutils-fix-potential-NULL-reference-in-load_checks.patch +Patch6010: backport-python-sepolicy-add-missing-booleans-to-man-pages.patch +Patch6011: backport-python-sepolicy-Cache-conditional-rule-queries.patch BuildRequires: gcc BuildRequires: pam-devel libsepol-static >= 3.3 libsemanage-static >= 3.3 libselinux-devel >= 3.3 libcap-devel audit-libs-devel gettext @@ -262,6 +268,9 @@ find %{buildroot}%{python3_sitelib} %{buildroot}%{python3_sitearch} \ %{_mandir}/* %changelog +* Wed Mar 22 2023 zhangguangzhi - 3.3-6 +- backport patches from upstream + * Sat Feb 4 2023 zhangguangzhi - 3.3-5 - modify buildrequires to glib2-devel -- Gitee