SELinux Security Module development
 help / color / mirror / Atom feed
From: Petr Lautrbach <plautrba@redhat.com>
To: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>,
	SElinux list <selinux@vger.kernel.org>
Cc: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>
Subject: Re: [PATCH] gui: do not recreate /etc/selinux/config
Date: Tue, 15 Mar 2022 09:47:50 +0100	[thread overview]
Message-ID: <87mthrtxe1.fsf@redhat.com> (raw)
In-Reply-To: <5b0956dd-ad07-6209-7b68-d0574874876c@rosalinux.ru>

Mikhail Novosyolov <m.novosyolov@rosalinux.ru> writes:

> /etc/selinux/config.bck was created and then replaced /etc/selinux/config.
> /etc/selinux/config is often read by libselinux from non-root,
> it must have mode 0644, but, when umask is 077, it became not world-readable
> after running system-config-gui.
>
> Overwrite the existing file instead of creating a new one.
>
> Unfortunately, we may get a corrupted file if the GUI is closed when writing it,
> but writing takes only a bit of time, plus we save a backup for manual restoring in such case.
>

I don't think it's a good idea. The final operation needs to be atomic.

If you want to preserve mode and other attributes you can use
shutil.copystat(), what about this:

        fd = open(backup_path, "w")
        ...
        fd.close()
        shutil.copystat(path, backup_path)    


> At Github: https://github.com/SELinuxProject/selinux/pull/345
>
> Signed-off-by: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>
> ---
>  gui/statusPage.py | 25 ++++++++++++++++++-------
>  1 file changed, 18 insertions(+), 7 deletions(-)
>
> diff --git a/gui/statusPage.py b/gui/statusPage.py
> index 766854b1..ded3929d 100644
> --- a/gui/statusPage.py
> +++ b/gui/statusPage.py
> @@ -18,6 +18,7 @@
>  ## Author: Dan Walsh
>  import os
>  import sys
> +import tempfile
>  from gi.repository import Gtk
>  import selinux
>  
> @@ -162,12 +163,20 @@ class statusPage:
>          self.enabled = enabled
>  
>      def write_selinux_config(self, enforcing, type):
> -        path = selinux.selinux_path() + "config"
> -        backup_path = path + ".bck"
> -        fd = open(path)
> -        lines = fd.readlines()
> -        fd.close()
> -        fd = open(backup_path, "w")
> +        selinux_path = selinux.selinux_path()
> +        path = selinux_path + "config"
> +        # Make a backup /etc/selinux/config.*.bck
> +        backup_path = tempfile.mkstemp(prefix="config.", dir=selinux_path, suffix=".bck")[1]
> +        fd1 = open(path, "r")
> +        lines = fd1.readlines()
> +        fd1.close()
> +        fd2 = open(backup_path, "a")
> +        for l in lines:
> +            fd2.write(l)
> +        fd2.close()
> +        # Write to path, not backup_path, to guarantee that file metadata
> +        # (permissions, xattrs, including SELinux labels etc.) is not lost.
> +        fd = open(path, "w")
>          for l in lines:
>              if l.startswith("SELINUX="):
>                  fd.write("SELINUX=%s\n" % enforcing)
> @@ -177,7 +186,9 @@ class statusPage:
>                  continue
>              fd.write(l)
>          fd.close()
> -        os.rename(backup_path, path)
> +        # Here we are sure that we are deleting our backup,
> +        # not another file or directory
> +        os.unlink(backup_path)
>  
>      def read_selinux_config(self):
>          self.initialtype = selinux.selinux_getpolicytype()[1]
> -- 
> 2.31.1


      reply	other threads:[~2022-03-15  8:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-11 12:36 [PATCH] gui: do not recreate /etc/selinux/config Mikhail Novosyolov
2022-03-15  8:47 ` 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=87mthrtxe1.fsf@redhat.com \
    --to=plautrba@redhat.com \
    --cc=m.novosyolov@rosalinux.ru \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox