All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Lautrbach <plautrba@redhat.com>
To: SElinux list <selinux@vger.kernel.org>
Cc: Nicolas Iooss <nicolas.iooss@m4x.org>
Subject: Re: [PATCH v2] libsemanage: sync filesystem with sandbox
Date: Sun, 31 Jan 2021 11:17:06 +0100	[thread overview]
Message-ID: <878s89o1gd.fsf@redhat.com> (raw)
In-Reply-To: <CAJfZ7=kwaQcrDKF+2b4K4EPezXdRec_E7Fuia5FPY2xNWPF9rA@mail.gmail.com>

Nicolas Iooss <nicolas.iooss@m4x.org> writes:

> On Thu, Jan 28, 2021 at 11:44 AM Petr Lautrbach <plautrba@redhat.com> wrote:
>>
>> 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
>>     1266
>>
>> It looks like this situation can be avoided it the filesystem with the
>> store is sync()ed before rename()
>>
>> Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
>> ---
>>
>> - fixed close(fd) indentation
>>
>>  libsemanage/src/semanage_store.c | 15 +++++++++++++++
>>  1 file changed, 15 insertions(+)
>>
>> diff --git a/libsemanage/src/semanage_store.c b/libsemanage/src/semanage_store.c
>> index cd5e46bb2401..9a81be54db60 100644
>> --- a/libsemanage/src/semanage_store.c
>> +++ b/libsemanage/src/semanage_store.c
>> @@ -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
>> --
>> 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.
>

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


      reply	other threads:[~2021-01-31 10:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-28 10:27 [PATCH] libsemanage: sync filesystem with sandbox Petr Lautrbach
2021-01-28 10:42 ` [PATCH v2] " Petr Lautrbach
2021-01-30 13:45   ` Nicolas Iooss
2021-01-31 10:17     ` Petr Lautrbach [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878s89o1gd.fsf@redhat.com \
    --to=plautrba@redhat.com \
    --cc=nicolas.iooss@m4x.org \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.