Optionally rebuild policy store only if there were external changes to modules
v2 cover Optionally rebuild policy store only if there were external changes to modules
This series extends libsemanage and semodule with optional capability to detect external changes to modules and perform a rebuild if there are any. See patch 5 for motivation and more details.
The first four patches are preparatory cleanup/refactoring, patch 5 implements the libsemanage side of the feature described above, and patch 6 adds a command-line option to semodule that allows to turn on the feature.
Default behavior of libsemanage and semodule is not changed (apart from added checksum calculation on each transaction, which however seems to add no noticeable overhead based on benchmarks - see patch 5 for details).
Another minor difference is a slightly different format of the checksum string printed by semodule --checksum ... (see patch 2).
v2:
- fix a spelling mistake found by James Carter
- eliminate 'rebuild' goto label from semanage_direct_commit()
- extract some of the semanage_direct_commit() cleanups into a separate patch
- unify checksum string format (semodule --checksum and the internal store checksum now use the same format and a common formatting helper)
- also the semanage_module_compute_checksum() is updated to do the formatting rather than leaving it up to semodule
- add missing space for null terminator to the checksum string buffers
- make the semantics of check_ext_changes more natural (and make them actually match the documentation)
- before: had to be used along with the rebuild flag to have an effect if no other changes are being done in the transaction
- now: check_ext_changes == 1 simply forces a rebuild any time module content changes are detected (i.e. it's kind of a conditional version of -B)
- also now: rebuild + check_ext_changes == rebuild
- rename --smart to --rebuild-if-modules-changed (it's long, but it's the least ambiguous I could come up with)
- also update semodule.8 man page
- update commit messages to reflect new naming and behavior
Ondrej Mosnacek (6):
libsemanage: add missing include to boolean_record.c semodule,libsemanage: move module hashing into libsemanage
libsemanage: move compressed file handling into a separate object
libsemanage: clean up semanage_direct_commit() a bit
libsemanage: optionally rebuild policy when modules are changed
externally
semodule: add command-line option to detect module changes
libsemanage/include/semanage/handle.h | 5 +
libsemanage/include/semanage/modules.h | 26 +
libsemanage/src/boolean_record.c | 4 +-
libsemanage/src/compressed_file.c | 224 ++++++++
libsemanage/src/compressed_file.h | 78 +++
libsemanage/src/direct_api.c | 541 ++++++++----------
libsemanage/src/direct_api.h | 4 -
libsemanage/src/handle.c | 11 +-
libsemanage/src/handle.h | 1 +
libsemanage/src/libsemanage.map | 5 +
libsemanage/src/modules.c | 59 ++
libsemanage/src/modules.h | 3 +
libsemanage/src/semanage_store.c | 53 +-
libsemanage/src/semanage_store.h | 1 +
.../src/semanageswig_python_exception.i | 8 +
.../semodule => libsemanage/src}/sha256.c | 0
.../semodule => libsemanage/src}/sha256.h | 0
policycoreutils/semodule/Makefile | 2 +-
policycoreutils/semodule/semodule.8 | 7 +
policycoreutils/semodule/semodule.c | 85 ++-
20 files changed, 721 insertions(+), 396 deletions(-)
create mode 100644 libsemanage/src/compressed_file.c
create mode 100644 libsemanage/src/compressed_file.h
rename {policycoreutils/semodule => libsemanage/src}/sha256.c (100%)
rename {policycoreutils/semodule => libsemanage/src}/sha256.h (100%)Signature · 1 line
2.34.1
PATCH v2 1/6 libsemanage: add missing include to boolean_record.c
It uses asprintf(3), but doesn't directly include <stdio.h> - fix it.
--- libsemanage/src/boolean_record.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
libsemanage/src/boolean_record.c +3 -1
@@ -7,6 +7,9 @@*/ #include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <sepol/boolean_record.h> typedef sepol_bool_t semanage_bool_t ;
@@ -20,7 +23,6 @@ typedef semanage_bool_key_t record_key_t;#include "boolean_internal.h" #include "handle.h" #include "database.h" -#include <stdlib.h> #include <selinux/selinux.h> /* Key */
Signature · 1 line
2.34.1
Re: [PATCH userspace v2 1/6] libsemanage: add missing include to boolean_record.c
Quoted 4 lines · Ondrej Mosnacek
It uses asprintf(3), but doesn't directly include <
stdio.h> - fix it.Signed-off-by Ondrej Mosnacek <[email protected]>
For this series:
Quoted 29 lines
--- libsemanage/src/boolean_record.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)libsemanage/src/boolean_record.c +3
-2@@ -7,6 +7,9 @@*/ #include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <sepol/boolean_record.h> typedef sepol_bool_t semanage_bool_t ;@@ -20,7 +23,6 @@ typedef semanage_bool_key_t record_key_t;#include "boolean_internal.h" #include "handle.h" #include "database.h" -#include <stdlib.h> #include <selinux/selinux.h> /* Key */ --2.34.1
Re: [PATCH userspace v2 1/6] libsemanage: add missing include to boolean_record.c
Quoted 10 lines · James Carter
Quoted 4 lines · Ondrej Mosnacek
It uses asprintf(3), but doesn't directly include <
stdio.h> - fix it.Signed-off-by Ondrej Mosnacek <[email protected]>For this series:
Acked-by James Carter <[email protected]>
This series has been merged.
Thanks,
Jim
Quoted 29 lines
Quoted 29 lines
--- libsemanage/src/boolean_record.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)libsemanage/src/boolean_record.c +3
-2@@ -7,6 +7,9 @@*/ #include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <sepol/boolean_record.h> typedef sepol_bool_t semanage_bool_t ;@@ -20,7 +23,6 @@ typedef semanage_bool_key_t record_key_t;#include "boolean_internal.h" #include "handle.h" #include "database.h" -#include <stdlib.h> #include <selinux/selinux.h> /* Key */ --2.34.1
PATCH v2 2/6 semodule,libsemanage: move module hashing into libsemanage
In reply to Ondrej MosnacekThe main goal of this move is to have the SHA-256 implementation under libsemanage, since upcoming patches will make use of SHA-256 for a different (but similar) purpose in libsemanage. Having the hashing code in libsemanage will reduce code duplication and allow for easier hash algorithm upgrade in the future.
Note that libselinux currently also contains a hash function implementation (for yet another different purpose). This patch doesn't make any effort to address that duplicity yet.
This patch also changes the format of the hash string printed by semodule to include the name of the hash. The intent is to avoid ambiguity and potential collisions when the algorithm is potentially changed in the future.
---
libsemanage/include/semanage/modules.h | 26 ++++++++
libsemanage/src/libsemanage.map | 4 ++
libsemanage/src/modules.c | 59 +++++++++++++++++++
.../src/semanageswig_python_exception.i | 8 +++
.../semodule => libsemanage/src}/sha256.c | 0
.../semodule => libsemanage/src}/sha256.h | 0
policycoreutils/semodule/Makefile | 2 +-
policycoreutils/semodule/semodule.c | 53 +++++------------
8 files changed, 114 insertions(+), 38 deletions(-)
rename {policycoreutils/semodule => libsemanage/src}/sha256.c (100%)
rename {policycoreutils/semodule => libsemanage/src}/sha256.h (100%)libsemanage/include/semanage/modules.h +26 -0
@@ -282,4 +282,30 @@ extern int semanage_module_get_enabled(semanage_handle_t *sh,const semanage_module_key_t *modkey, int *enabled); +/* Compute checksum for @modkey module contents. +* +* If @checksum is NULL, the function will just return the length of the +* checksum string in @checksum_len (checksum strings are guaranteed to +* have a fixed length for a given libsemanage binary). @modkey and @cil +* are ignored in this case and should be set to NULL and 0 (respectively). +* +* If @checksum is non-NULL, on success, @checksum will point to a buffer +* containing the checksum string and @checksum_len will point to the +* length of the string (without the null terminator). The semantics of +* @cil are the same as for @extract_cil in semanage_module_extract(). +* +* The caller is responsible to free the buffer returned in @checksum (using +* free(3)). +* +* Callers may assume that if the checksum strings for two modules match, +* the module content is the same (collisions are theoretically possible, +* yet extremely unlikely). +* +* Returns 0 on success and -1 on error. +*/ +extern int semanage_module_compute_checksum (semanage_handle_t *sh, +semanage_module_key_t *modkey, +int cil,char **checksum, +size_t *checksum_len); +#endif
libsemanage/src/libsemanage.map +4 -0
@@ -345,3 +345,7 @@ LIBSEMANAGE_1.1 { semanage_module_remove_key; semanage_set_store_root; } LIBSEMANAGE_1.0; + +LIBSEMANAGE_3.4 { + semanage_module_compute_checksum; +} LIBSEMANAGE_1.1;
libsemanage/src/modules.c +59 -0
73 lines not on this page · raw message
libsemanage/src/semanageswig_python_exception.i +8 -0
@@ -351,6 +351,14 @@ } } +%exception semanage_module_compute_checksum { + $action + if (result < 0) { + PyErr_SetFromErrno(PyExc_OSError); + SWIG_fail; + } +} + %exception semanage_msg_get_level { $action if (result < 0) {
libsemanage/src/sha256.c renamed from policycoreutils/semodule/sha256.c +0 -0
libsemanage/src/sha256.h renamed from policycoreutils/semodule/sha256.h +0 -0
policycoreutils/semodule/Makefile +1 -1
@@ -6,7 +6,7 @@ MANDIR = $(PREFIX)/share/man CFLAGS ?= -Werror -Wall -W override LDLIBS += -lsepol -lselinux -lsemanage -SEMODULE_OBJS = semodule.o sha256.o +SEMODULE_OBJS = semodule.o all: semodule genhomedircon
policycoreutils/semodule/semodule.c +16 -37
91 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 3/6 libsemanage: move compressed file handling into a separate object
In reply to Ondrej MosnacekIn order to reduce exisiting and future code duplication and to avoid some unnecessary allocations and copying, factor the compressed file utility functions out into a separate C/header file and refactor their interface.
Note that this change effectively removes the __fsetlocking(3) call from semanage_load_files() - I haven't been able to figure out what purpose it serves, but it seems pointless...
--- libsemanage/src/compressed_file.c | 224 +++++++++++++++++++++++++ libsemanage/src/compressed_file.h | 78 +++++++++ libsemanage/src/direct_api.c | 263 +++++------------------------- libsemanage/src/direct_api.h | 4 - libsemanage/src/semanage_store.c | 52 ++---- 5 files changed, 354 insertions(+), 267 deletions(-) create mode 100644 libsemanage/src/compressed_file.c create mode 100644 libsemanage/src/compressed_file.h
libsemanage/src/compressed_file.c new file +224 -0
@@ -0,0 +1,224 @@ +/* Author: Jason Tang <[email protected]> +* Christopher Ashworth <[email protected]> +* Ondrej Mosnacek <[email protected]> +* +* Copyright (C) 2004-2006 Tresys Technology, LLC +* Copyright (C) 2005-2021 Red Hat, Inc. +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <stdlib.h> +#include <string.h> +#include <stdint.h> + +#include <unistd.h> +#include <fcntl.h> + +#include <bzlib.h> + +#include "compressed_file.h" + +#include "debug.h" + +#define BZ2_MAGICSTR "BZh" +#define BZ2_MAGICLEN (sizeof(BZ2_MAGICSTR)-1) + +/* bzip() a data to a file, returning the total number of compressed bytes +* in the file. Returns -1 if file could not be compressed. */ +static int bzip (semanage_handle_t *sh,const char *filename,void *data, +size_t num_bytes) +{ +BZFILE * b; +size_t size =1 <<16 ; +int bzerror; +size_t total =0 ; +size_t len =0 ; +FILE *f; + +if ((f =fopen (filename,"wb" )) ==NULL ) { +return -1 ; + } + +if (!sh->conf->bzip_blocksize) { +if (fwrite (data,1 , num_bytes, f) < num_bytes) { +fclose (f); +return -1 ; + } +fclose (f); +return 0 ; + } + + b =BZ2_bzWriteOpen ( &bzerror, f, sh->conf->bzip_blocksize,0 ,0 ); +if (bzerror !=BZ_OK ) { +BZ2_bzWriteClose ( &bzerror, b,1 ,0 ,0 ); +fclose (f); +return -1 ; + } + +while ( num_bytes > total ) { +if (num_bytes - total > size) { + len = size; + }else { + len = num_bytes - total; + } +BZ2_bzWrite ( &bzerror, b, (uint8_t *)data + total, len ); +if (bzerror ==BZ_IO_ERROR ) { +BZ2_bzWriteClose ( &bzerror, b,1 ,0 ,0 ); +fclose (f); +return -1 ; + } + total += len; + } + +BZ2_bzWriteClose ( &bzerror, b,0 ,0 ,0 ); +fclose (f); +if (bzerror ==BZ_IO_ERROR ) { +return -1 ; + } +return 0 ; +} + +/* bunzip() a file to '*data', returning the total number of uncompressed bytes +* in the file. Returns -1 if file could not be decompressed. */ +static ssize_t bunzip (semanage_handle_t *sh,FILE *f,void **data) +{ +BZFILE * b =NULL ; +size_t nBuf; +uint8_t * buf =NULL ; +size_t size =1 <<18 ; +size_t bufsize = size; +int bzerror; +size_t total =0 ; +uint8_t * uncompress =NULL ; +uint8_t * tmpalloc =NULL ; +int ret =-1 ; + + buf =malloc (bufsize); +if (buf ==NULL ) { +ERR (sh,"Failure allocating memory." ); + goto exit; + } + +/* Check if the file is bzipped */ + bzerror =fread (buf,1 ,BZ2_MAGICLEN , f); +rewind (f); +if ((bzerror !=BZ2_MAGICLEN ) ||memcmp (buf,BZ2_MAGICSTR ,BZ2_MAGICLEN )) { + goto exit; + } + + b =BZ2_bzReadOpen ( &bzerror, f,0 , sh->conf->bzip_small,NULL ,0 ); +if ( bzerror !=BZ_OK ) { +ERR (sh,"Failure opening bz2 archive." ); + goto exit; + } + + uncompress =malloc (size); +if (uncompress ==NULL ) { +ERR (sh,"Failure allocating memory." ); + goto exit; + } + +while ( bzerror ==BZ_OK ) { + nBuf =BZ2_bzRead ( &bzerror, b, buf, bufsize); +if (( bzerror ==BZ_OK ) || ( bzerror ==BZ_STREAM_END )) { +if (total + nBuf > size) { + size *=2 ; + tmpalloc =realloc (uncompress, size); +if (tmpalloc ==NULL ) { +ERR (sh,"Failure allocating memory." ); + goto exit; + } + uncompress = tmpalloc; + } +memcpy (&uncompress[total], buf, nBuf); + total += nBuf; + } + } +if ( bzerror !=BZ_STREAM_END ) { +ERR (sh,"Failure reading bz2 archive." ); + goto exit; + } + + ret = total; + *data = uncompress; + +exit: +BZ2_bzReadClose ( &bzerror, b ); +free (buf); +if ( ret <0 ) { +free (uncompress); + } +return ret; +} + +int map_compressed_file (semanage_handle_t *sh,const char *path, +struct file_contents *contents) +{ +ssize_t size =-1 ; +void *uncompress; +int ret =0 , fd =-1 ; +FILE *file =NULL ; + + fd =open (path,O_RDONLY ); +if (fd ==-1 ) { +ERR (sh,"Unable to open %s\n" , path); +return -1 ; + } + + file =fdopen (fd,"r" ); +if (file ==NULL ) { +ERR (sh,"Unable to open %s\n" , path); +close (fd); +return -1 ; + } + +if ((size =bunzip (sh, file, &uncompress)) >=0 ) { + contents->data = uncompress; + contents->len = size; + contents->compressed =1 ; + }else { +struct stat sb; +if (fstat (fd, &sb) ==-1 || + (uncompress =mmap (NULL , sb.st_size,PROT_READ ,MAP_PRIVATE , fd,0 )) == +MAP_FAILED ) { + ret =-1 ; + }else { + contents->data = uncompress; + contents->len = sb.st_size; + contents->compressed =0 ; + } + } +fclose (file); +return ret; +} + +void unmap_compressed_file (struct file_contents *contents) +{ +if (!contents->data) +return ; + +if (contents->compressed) { +free (contents->data); + }else { +munmap (contents->data, contents->len); + } +} + +int write_compressed_file (semanage_handle_t *sh,const char *path, +void *data,size_t len) +{ +return bzip (sh, path, data, len); +}
libsemanage/src/compressed_file.h new file +78 -0
78 lines not on this page · raw message
libsemanage/src/direct_api.c +42 -221
369 lines not on this page · raw message
libsemanage/src/direct_api.h +0 -4
@@ -39,8 +39,4 @@ int semanage_direct_access_check(struct semanage_handle *sh);int semanage_direct_mls_enabled (struct semanage_handle *sh); -#include <stdio.h> -#include <unistd.h> -ssize_t bunzip (struct semanage_handle *sh,FILE *f,char **data); -#endif
libsemanage/src/semanage_store.c +10 -42
76 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 4/6 libsemanage: clean up semanage_direct_commit() a bit
In reply to Ondrej MosnacekDo some minor cosmetic cleanup, mainly to eliminate the 'rebuilt' goto label.
--- libsemanage/src/direct_api.c | 91 ++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 46 deletions(-)
libsemanage/src/direct_api.c +45 -46
@@ -994,6 +994,16 @@ cleanup:return status; } +/* Files that must exist in order to skip policy rebuild. */ +static const int semanage_computed_files[] = { +SEMANAGE_STORE_KERNEL , +SEMANAGE_STORE_FC , +SEMANAGE_STORE_SEUSERS , +SEMANAGE_LINKED , +SEMANAGE_SEUSERS_LINKED , +SEMANAGE_USERS_EXTRA_LINKED +}; +/* Copies a file from src to dst. If dst already exists then * overwrite it. If source doesn't exist then return success. * Returns 0 on success, -1 on error. */
@@ -1053,6 +1063,14 @@ static int semanage_direct_commit(semanage_handle_t * sh) seusers_modified = seusers->dtable->is_modified (seusers->dbase); fcontexts_modified = fcontexts->dtable->is_modified (fcontexts->dbase); +/* Before we do anything else, flush the join to its component parts. +* This *does not* flush to disk automatically */ +if (users->dtable->is_modified (users->dbase)) { + retval = users->dtable->flush (sh, users->dbase); +if (retval <0 ) + goto cleanup; + } +/* Rebuild if explicitly requested or any module changes occurred. */ do_rebuild = sh->do_rebuild | sh->modules_modified;
@@ -1119,14 +1137,6 @@ static int semanage_direct_commit(semanage_handle_t * sh) } } -/* Before we do anything else, flush the join to its component parts. -* This *does not* flush to disk automatically */ -if (users->dtable->is_modified (users->dbase)) { - retval = users->dtable->flush (sh, users->dbase); -if (retval <0 ) - goto cleanup; - } -/* * This is for systems that have already migrated with an older version * of semanage_migrate_store. The older version did not copy
@@ -1135,48 +1145,20 @@ static int semanage_direct_commit(semanage_handle_t * sh)* in order to skip re-linking are present; otherwise, we force * a rebuild. */ -if (!do_rebuild) { -int files[] = {SEMANAGE_STORE_KERNEL , -SEMANAGE_STORE_FC , -SEMANAGE_STORE_SEUSERS , -SEMANAGE_LINKED , -SEMANAGE_SEUSERS_LINKED , -SEMANAGE_USERS_EXTRA_LINKED }; - -for (i =0 ; i < (int )ARRAY_SIZE (files); i++) { - path =semanage_path (SEMANAGE_TMP , files[i]); -if (stat (path, &sb) !=0 ) { -if (errno !=ENOENT ) { -ERR (sh,"Unable to access %s: %s\n" , path,strerror (errno)); - retval =-1 ; - goto cleanup; - } - - do_rebuild =1 ; - goto rebuild; +for (i =0 ; !do_rebuild && i < (int )ARRAY_SIZE (semanage_computed_files); i++) { + path =semanage_path (SEMANAGE_TMP , semanage_computed_files[i]); +if (stat (path, &sb) !=0 ) { +if (errno !=ENOENT ) { +ERR (sh,"Unable to access %s: %s\n" , path,strerror (errno)); + retval =-1 ; + goto cleanup; } + + do_rebuild =1 ; +break ; } } -rebuild: -/* -* Now that we know whether or not a rebuild is required, -* we can determine what else needs to be done. -* We need to write the kernel policy if we are rebuilding -* or if any other policy component that lives in the kernel -* policy has been modified. -* We need to install the policy files if any of the managed files -* that live under /etc/selinux (kernel policy, seusers, file contexts) -* will be modified. -*/ - do_write_kernel = do_rebuild | ports_modified | ibpkeys_modified | - ibendports_modified | - bools->dtable->is_modified (bools->dbase) | - ifaces->dtable->is_modified (ifaces->dbase) | - nodes->dtable->is_modified (nodes->dbase) | - users->dtable->is_modified (users_base->dbase); - do_install = do_write_kernel | seusers_modified | fcontexts_modified; -/* * If there were policy changes, or explicitly requested, or * any required files are missing, rebuild the policy.
@@ -1330,6 +1312,23 @@ rebuild: } } +/* +* Determine what else needs to be done. +* We need to write the kernel policy if we are rebuilding +* or if any other policy component that lives in the kernel +* policy has been modified. +* We need to install the policy files if any of the managed files +* that live under /etc/selinux (kernel policy, seusers, file contexts) +* will be modified. +*/ + do_write_kernel = do_rebuild | ports_modified | ibpkeys_modified | + ibendports_modified | + bools->dtable->is_modified (bools->dbase) | + ifaces->dtable->is_modified (ifaces->dbase) | + nodes->dtable->is_modified (nodes->dbase) | + users->dtable->is_modified (users_base->dbase); + do_install = do_write_kernel | seusers_modified | fcontexts_modified; +/* Attach our databases to the policydb we just created or loaded. */ dbase_policydb_attach ((dbase_policydb_t *) pusers_base->dbase, out);dbase_policydb_attach ((dbase_policydb_t *) pports->dbase, out);
Signature · 1 line
2.34.1
PATCH v2 5/6 libsemanage: optionally rebuild policy when modules are changed externally
In reply to Ondrej MosnacekIn Fedora/RHEL's selinux-policy package we ship a pre-built SELinux policy store in the RPMs. When updating the main policy RPM, care must be taken to rebuild the policy using semodule -B if there are any other SELinux modules installed (whether shipped via another RPM or manually installed locally).
However, this way of shipping/managing the policy creates complications on systems, where system files are managed by rpm-ostree (such as Fedora CoreOS or Red Hat CoreOS), where the "package update" process is more sophisticated.
(Disclaimer: The following is written according to my current limited understanding of rpm-ostree and may not be entirely accurate, but the gist of it should match the reality.)
Basically, one can think of rpm-ostree as a kind of Git for system files. The package content is provided on a "branch", where each "commit" represents a set of package updates layered on top of the previous commit (i.e. it is a rolling release with some defined package content snapshots). The user can then maintain their own branch with additional package updates/installations/... and "rebase" it on top of the main branch as needed. On top of that, the user can also have additional configuration files (or modifications to existing files) in /etc, which represent an additional layer on top of the package content.
When updating the system (i.e. rebasing on a new "commit" of the "main branch"), the files on the running system are not touched and the new system state is prepared under a new root directory, which is chrooted into on the next reboot.
When an rpm-ostree system is updated, there are three moments when the SELinux module store needs to be rebuilt to ensure that all modules are included in the binary policy:
1. When the local RPM installations are applied on top of the base system snapshot.
2. When local user configuartion is applied on top of that.
3. On system shutdown, to ensure that any changes in local configuration performed since (2.) are reflected in the final new system image.
Forcing a full rebuild at each step is not optimal and in many cases is not necessary, as the user may not have any custom modules installed.
Thus, this patch extends libsemanage to compute a checksum of the content of all enabled modules, which is stored in the store, and adds a flag to the libsemanage handle that instructs it to check the module content checksum against the one from the last successful transaction and force a full policy rebuild if they don't match.
This will allow rpm-ostree systems to potentially reduce delays when reconciling the module store when applying updates.
I wasn't able to measure any noticeable overhead of the hash computation, which is now added for every transaction (both before and after this change a full policy rebuild took about 7 seconds on my test x86 VM). With the new option check_ext_changes enabled, rebuilding a policy store with unchanged modules took only about 0.96 seconds.
--- libsemanage/include/semanage/handle.h | 5 + libsemanage/src/direct_api.c | 187 +++++++++++++++++++++----- libsemanage/src/handle.c | 11 +- libsemanage/src/handle.h | 1 + libsemanage/src/libsemanage.map | 1 + libsemanage/src/modules.c | 4 +- libsemanage/src/modules.h | 3 + libsemanage/src/semanage_store.c | 1 + libsemanage/src/semanage_store.h | 1 + 9 files changed, 175 insertions(+), 39 deletions(-)
libsemanage/include/semanage/handle.h +5 -0
@@ -66,6 +66,11 @@ extern void semanage_set_reload(semanage_handle_t * handle, int do_reload);* 1 for yes, 0 for no (default) */ extern void semanage_set_rebuild (semanage_handle_t * handle,int do_rebuild); +/* set whether to rebuild the policy on commit when potential changes +* to module files since last rebuild are detected, +* 1 for yes (default), 0 for no */ +extern void semanage_set_check_ext_changes (semanage_handle_t * handle,int do_check); +/* Fills *compiler_path with the location of the hll compiler sh->conf->compiler_directory_path * corresponding to lang_ext. * Upon success returns 0, -1 on error. */
libsemanage/src/direct_api.c +154 -33
277 lines not on this page · raw message
libsemanage/src/handle.c +7 -4
@@ -116,20 +116,23 @@ semanage_handle_t *semanage_handle_create(void)void semanage_set_rebuild (semanage_handle_t * sh,int do_rebuild) { -assert (sh !=NULL ); sh->do_rebuild = do_rebuild; -return ; }void semanage_set_reload (semanage_handle_t * sh,int do_reload) { -assert (sh !=NULL ); sh->do_reload = do_reload; -return ; +} + +void semanage_set_check_ext_changes (semanage_handle_t * sh,int do_check) +{ +assert (sh !=NULL ); + + sh->check_ext_changes = do_check; } int semanage_get_hll_compiler_path(semanage_handle_t *sh,
libsemanage/src/handle.h +1 -0
@@ -61,6 +61,7 @@ struct semanage_handle {int is_in_transaction;int do_reload;/* whether to reload policy after commit */ int do_rebuild;/* whether to rebuild policy if there were no changes */ +int check_ext_changes;/* whether to rebuild if external changes are detected via checksum */ int commit_err;/* set by semanage_direct_commit() if there are * any errors when building or committing the * sandbox to kernel policy at /etc/selinux
libsemanage/src/libsemanage.map +1 -0
@@ -348,4 +348,5 @@ LIBSEMANAGE_1.1 { LIBSEMANAGE_3.4 { semanage_module_compute_checksum; + semanage_set_check_ext_changes; } LIBSEMANAGE_1.1;
libsemanage/src/modules.c +2 -2
@@ -979,9 +979,9 @@ int semanage_module_remove_key(semanage_handle_t *sh, }static const char CHECKSUM_TYPE [] ="sha256" ; -static const size_t CHECKSUM_CONTENT_SIZE =sizeof (CHECKSUM_TYPE ) +1 +2 *SHA256_HASH_SIZE ; +const size_t CHECKSUM_CONTENT_SIZE =sizeof (CHECKSUM_TYPE ) +1 +2 *SHA256_HASH_SIZE ; -static void semanage_hash_to_checksum_string (const uint8_t *hash,char *checksum) +void semanage_hash_to_checksum_string (const uint8_t *hash,char *checksum) {size_t i;
libsemanage/src/modules.h +3 -0
@@ -102,4 +102,7 @@ int semanage_module_get_path(semanage_handle_t *sh,char *path, size_t len); +extern const size_t CHECKSUM_CONTENT_SIZE ; +void semanage_hash_to_checksum_string (const uint8_t *hash,char *checksum); +#endif
libsemanage/src/semanage_store.c +1 -0
@@ -115,6 +115,7 @@ static const char *semanage_sandbox_paths[SEMANAGE_STORE_NUM_PATHS] = {"/disable_dontaudit" ,"/preserve_tunables" ,"/modules/disabled" , +"/modules_checksum" ,"/policy.kern" ,"/file_contexts.local" , "/file_contexts.homedirs",
libsemanage/src/semanage_store.h +1 -0
@@ -60,6 +60,7 @@ enum semanage_sandbox_defs {SEMANAGE_DISABLE_DONTAUDIT ,SEMANAGE_PRESERVE_TUNABLES ,SEMANAGE_MODULES_DISABLED , +SEMANAGE_MODULES_CHECKSUM ,SEMANAGE_STORE_KERNEL ,SEMANAGE_STORE_FC_LOCAL , SEMANAGE_STORE_FC_HOMEDIRS,
Signature · 1 line
2.34.1
PATCH v2 6/6 semodule: add command-line option to detect module changes
In reply to Ondrej MosnacekAdd a new command-line option "--rebuild-if-modules-changed" to control the newly introduced check_ext_changes libsemanage flag.
For example, running semodule --rebuild-if-modules-changed will ensure that any externally added/removed modules (e.g. by an RPM transaction) are reflected in the compiled policy, while skipping the most expensive part of the rebuild if no module change was deteceted since the last libsemanage transaction.
--- policycoreutils/semodule/semodule.8 | 7 +++++++ policycoreutils/semodule/semodule.c | 32 ++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 7 deletions(-)
policycoreutils/semodule/semodule.8 +7 -0
@@ -23,6 +23,13 @@ force a reload of policy .B \-B, \-\-build force a rebuild of policy (also reloads unless \-n is used) .TP +.B \-\-rebuild-if-modules-changed +Force a rebuild of the policy if any changes to module content are detected +(by comparing with checksum from the last transaction). One can use this +instead of \-B to ensure that any changes to the module store done by an +external tool (e.g. a package manager) are applied, while automatically +skipping the rebuild if there are no new changes. +.TP .B \-D, \-\-disable_dontaudit Temporarily remove dontaudits from policy. Reverts whenever policy is rebuilt .TP
policycoreutils/semodule/semodule.c +25 -7
@@ -47,6 +47,7 @@ static int verbose;static int reload;static int no_reload;static int build; +static int check_ext_changes;static int disable_dontaudit;static int preserve_tunables;static int ignore_module_cache;
@@ -149,6 +150,9 @@ static void usage(char *progname)printf (" -c, --cil extract module as cil. This only affects module extraction.\n" );printf (" -H, --hll extract module as hll. This only affects module extraction.\n" );printf (" -m, --checksum print module checksum (SHA256).\n" ); +printf (" --rebuild-if-modules-changed\n" +" force policy rebuild if module content changed since\n" +" last rebuild (based on checksum)\n" ); }/* Sets the global mode variable to new_mode, but only if no other
@@ -180,6 +184,7 @@ static void set_mode(enum client_modes new_mode, char *arg)static void parse_command_line (int argc,char **argv) {static struct option opts[] = { + {"rebuild-if-modules-changed" ,0 ,NULL ,'\0' }, {"store" , required_argument,NULL ,'s' }, {"base" , required_argument,NULL ,'b' }, {"help" ,0 ,NULL ,'h' },
@@ -207,15 +212,26 @@ static void parse_command_line(int argc, char **argv) };int extract_selected =0 ;int cil_hll_set =0 ; -int i; +int i, longind; verbose =0 ; reload =0 ; no_reload =0 ; + check_ext_changes =0 ; priority =400 ;while ((i = -getopt_long (argc, argv,"s:b:hi:l::vr:u:RnNBDCPX:e:d:p:S:E:cHm" , opts, -NULL )) !=-1 ) { +getopt_long (argc, argv,"s:b:hi:l::vr:u:RnNBDCPX:e:d:p:S:E:cHm" , + opts, &longind)) !=-1 ) {switch (i) { +case '\0' : +switch (longind) { +case 0 :/* --rebuild-if-modules-changed */ + check_ext_changes =1 ; +break ; +default : +usage (argv[0 ]); +exit (1 ); + } +break ;case 'b' :fprintf (stderr,"The --base option is deprecated. Use --install instead.\n" );set_mode (INSTALL_M , optarg);
@@ -300,13 +316,13 @@ static void parse_command_line(int argc, char **argv) } } } -if ((build || reload) && num_commands) { +if ((build || reload || check_ext_changes) && num_commands) {fprintf (stderr,"build or reload should not be used with other commands\n" );usage (argv[0 ]);exit (1 ); } -if (num_commands ==0 && reload ==0 && build ==0 ) { +if (num_commands ==0 && reload ==0 && build ==0 && check_ext_changes ==0 ) {fprintf (stderr,"At least one mode must be specified.\n" );usage (argv[0 ]);exit (1 );
@@ -395,7 +411,7 @@ int main(int argc, char *argv[])cil_set_log_level (CIL_ERR + verbose); -if (build) +if (build || check_ext_changes) commit =1 ; sh =semanage_handle_create ();
@@ -434,7 +450,7 @@ int main(int argc, char *argv[]) } } -if (build) { +if (build || check_ext_changes) {if ((result =semanage_begin_transaction (sh)) <0 ) {fprintf (stderr,"%s: Could not begin transaction: %s\n" , argv[0 ], errno ?strerror (errno) :"" );
@@ -807,6 +823,8 @@ cleanup_disable:semanage_set_reload (sh,0 );if (build)semanage_set_rebuild (sh,1 ); +if (check_ext_changes) +semanage_set_check_ext_changes (sh,1 );if (disable_dontaudit)semanage_set_disable_dontaudit (sh,1 );else if (build)
Signature · 1 line
2.34.1