libsemanage: sync filesystem with sandbox
PATCH 1/1 libsemanage: sync filesystem with sandbox
Commit 331a109f91ea ("libsemanage: fsync final files before rename") added fsync() for policy files and improved situation when something unexpected happens right after rename(). However the module store could be affected as well. After the following steps module files could be 0 size:
1. Run semanage fcontext -a -t var_t "/tmp/abc"
2. Force shutdown the server during the command is run, or right after it's finished
3. Boot the system and look for empty files:
# find /var/lib/selinux/targeted/ -type f -size 0 | wc -l
1266It looks like this situation can be avoided it the filesystem with the store is sync()ed before rename()
--- libsemanage/src/semanage_store.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
libsemanage/src/semanage_store.c +15 -0
@@ -1764,6 +1764,21 @@ static int semanage_commit_sandbox(semanage_handle_t * sh)/* clean up some files from the sandbox before install */ /* remove homedir_template from sandbox */ +/* sync filesystem with sandbox first */ + fd =open (sandbox,O_DIRECTORY ); +if (fd ==-1 ) { +ERR (sh,"Error while opening %s for syncfs(): %d" , sandbox, errno); + retval =-1 ; + goto cleanup; + } +if (syncfs (fd) ==-1 ) { +ERR (sh,"Error while syncing %s to filesystem: %d" , sandbox, errno); +close (fd); + retval =-1 ; + goto cleanup; + } +close (fd); +if (rename (sandbox, active) ==-1 ) {ERR (sh,"Error while renaming %s to %s." , sandbox, active);/* note that if an error occurs during the next
Signature · 1 line
2.30.0
PATCH v2 1/1 libsemanage: sync filesystem with sandbox
Commit 331a109f91ea ("libsemanage: fsync final files before rename") added fsync() for policy files and improved situation when something unexpected happens right after rename(). However the module store could be affected as well. After the following steps module files could be 0 size:
1. Run semanage fcontext -a -t var_t "/tmp/abc"
2. Force shutdown the server during the command is run, or right after it's finished
3. Boot the system and look for empty files:
# find /var/lib/selinux/targeted/ -type f -size 0 | wc -l
1266It looks like this situation can be avoided it the filesystem with the store is sync()ed before rename()
---
- fixed close(fd) indentation
libsemanage/src/semanage_store.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
libsemanage/src/semanage_store.c +15 -0
@@ -1764,6 +1764,21 @@ static int semanage_commit_sandbox(semanage_handle_t * sh)/* clean up some files from the sandbox before install */ /* remove homedir_template from sandbox */ +/* sync filesystem with sandbox first */ + fd =open (sandbox,O_DIRECTORY ); +if (fd ==-1 ) { +ERR (sh,"Error while opening %s for syncfs(): %d" , sandbox, errno); + retval =-1 ; + goto cleanup; + } +if (syncfs (fd) ==-1 ) { +ERR (sh,"Error while syncing %s to filesystem: %d" , sandbox, errno); +close (fd); + retval =-1 ; + goto cleanup; + } +close (fd); +if (rename (sandbox, active) ==-1 ) {ERR (sh,"Error while renaming %s to %s." , sandbox, active);/* note that if an error occurs during the next
Signature · 1 line
2.30.0
Quoted 52 lines · Petr Lautrbach
Commit
331a109f91ea("libsemanage: fsync final files before rename") addedfsync()for policy files and improved situation when something unexpected happens right afterrename(). However the module store could be affected as well. After the following steps module files could be 0 size:1. Run
semanage fcontext -a -t var_t "/tmp/abc"
2. Force shutdown the server during the command is run, or right after it's finished
3. Boot the system and look for empty files:# find /var/lib/selinux/targeted/ -type f -size 0 | wc -l 1266It looks like this situation can be avoided it the filesystem with the store is sync()ed before
rename()Signed-off-by Petr Lautrbach <[email protected]>---
- fixed close(fd) indentation
libsemanage/src/semanage_store.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)libsemanage/src/semanage_store.c +15
-1@@ -1764,6 +1764,21 @@ static int semanage_commit_sandbox(semanage_handle_t * sh)/* clean up some files from the sandbox before install */ /* remove homedir_template from sandbox */ +/* sync filesystem with sandbox first */ + fd =open (sandbox,O_DIRECTORY ); +if (fd ==-1 ) { +ERR (sh,"Error while opening %s for syncfs(): %d" , sandbox, errno); + retval =-1 ; + goto cleanup; + } +if (syncfs (fd) ==-1 ) { +ERR (sh,"Error while syncing %s to filesystem: %d" , sandbox, errno); +close (fd); + retval =-1 ; + goto cleanup; + } +close (fd); +if (rename (sandbox, active) ==-1 ) {ERR (sh,"Error while renaming %s to %s." , sandbox, active);/* note that if an error occurs during the next --
Quoted 2 lines
2.30.0
Hello,
The sync logic seems to be fine, but why does it happen between rename(active, backup) and rename(sandbox, active)? It feels more logical to me if the syncing (which could take time) was done before the rename dance (so before rename(active, backup)). Nevertheless I could be missing something to understand your choice. If it is so, a comment about why syncfs() is done after rename(active, backup) would be very useful.
Cheers,
Nicolas
Quoted 57 lines · Nicolas Iooss
Quoted 52 lines · Petr Lautrbach
Commit
331a109f91ea("libsemanage: fsync final files before rename") addedfsync()for policy files and improved situation when something unexpected happens right afterrename(). However the module store could be affected as well. After the following steps module files could be 0 size:1. Run
semanage fcontext -a -t var_t "/tmp/abc"
2. Force shutdown the server during the command is run, or right after it's finished
3. Boot the system and look for empty files:# find /var/lib/selinux/targeted/ -type f -size 0 | wc -l 1266It looks like this situation can be avoided it the filesystem with the store is sync()ed before
rename()Signed-off-by Petr Lautrbach <[email protected]>---
- fixed close(fd) indentation
libsemanage/src/semanage_store.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)libsemanage/src/semanage_store.c +15
-1@@ -1764,6 +1764,21 @@ static int semanage_commit_sandbox(semanage_handle_t * sh)/* clean up some files from the sandbox before install */ /* remove homedir_template from sandbox */ +/* sync filesystem with sandbox first */ + fd =open (sandbox,O_DIRECTORY ); +if (fd ==-1 ) { +ERR (sh,"Error while opening %s for syncfs(): %d" , sandbox, errno); + retval =-1 ; + goto cleanup; + } +if (syncfs (fd) ==-1 ) { +ERR (sh,"Error while syncing %s to filesystem: %d" , sandbox, errno); +close (fd); + retval =-1 ; + goto cleanup; + } +close (fd); +if (rename (sandbox, active) ==-1 ) {ERR (sh,"Error while renaming %s to %s." , sandbox, active);/* note that if an error occurs during the next -- Quoted 2 lines
2.30.0
Hello,
Quoted 8 lines
The sync logic seems to be fine, but why does it happen between
rename(active, backup)andrename(sandbox, active)? It feels more logical to me if the syncing (which could take time) was done before the rename dance (so beforerename(active, backup)). Nevertheless I could be missing something to understand your choice. If it is so, a comment about whysyncfs()is done afterrename(active, backup)would be very useful.
My original idea was to do syncfs(sandbox) just before sandbox is renamed to active. But you are right that it should happen before rename(active, backup) as if syncfs() failed there would be no active anymore. I'll send another patch.
Thanks!
Petr