All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Smalley <stephen.smalley.work@gmail.com>
To: Nicolas Iooss <nicolas.iooss@m4x.org>
Cc: SElinux list <selinux@vger.kernel.org>
Subject: Re: [PATCH] libsemanage: fsync before rename
Date: Wed, 13 May 2020 15:08:14 -0400	[thread overview]
Message-ID: <CAEjxPJ6U4vx+OB8WD689Vki1jVLeBzOgfgMawhbsT-KCXVn-WQ@mail.gmail.com> (raw)
In-Reply-To: <CAJfZ7=mvfqdh_5KFff7itT3m3FPVnMpcG8_kn+U3EYOw8KebiA@mail.gmail.com>

On Wed, May 13, 2020 at 2:52 PM Nicolas Iooss <nicolas.iooss@m4x.org> wrote:
>
> On Wed, May 13, 2020 at 8:46 PM Smalley <stephen.smalley.work@gmail.com> wrote:
> >
> > From: Stephen Smalley <stephen.smalley.work@gmail.com>
> >
> > Prior to rename(2)'ing new files into place, fsync(2) them to ensure
> > the contents will be fully written prior to rename.  While we are here,
> > also fix checking of write(2) to detect short writes.  This code could
> > be more generally improved but keeping to the minimal changes required
> > to fix this bug.
> >
> > Fixes: https://github.com/SELinuxProject/selinux/issues/237
> > Signed-off-by: Stephen Smalley <stephen.smalley.work@gmail.com>
> > ---
> >  libsemanage/src/semanage_store.c | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/libsemanage/src/semanage_store.c b/libsemanage/src/semanage_store.c
> > index 859c0a22..3cac36d4 100644
> > --- a/libsemanage/src/semanage_store.c
> > +++ b/libsemanage/src/semanage_store.c
> > @@ -735,7 +735,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode)
> >         }
> >         umask(mask);
> >         while (retval == 0 && (amount_read = read(in, buf, sizeof(buf))) > 0) {
> > -               if (write(out, buf, amount_read) < 0) {
> > +               if (write(out, buf, amount_read) != amount_read) {
> >                         errsv = errno;
> >                         retval = -1;
> >                 }
>
> If I remember correctly, errno is not defined if a short write
> happens. If this is confirmed and if you want to keep the patch short,
> you could for example use errsv = EIO if write() returned a value
> different from -1 and from amount_read.

True.  It also occurred to me that this is too heavyweight given how
widely semanage_copy_file() is used within libsemanage; performing a
fsync(2) for every one of these file copies will be very expensive.
We only really care about it when copying the final files to
/etc/selinux, not for the copying under /var/lib/selinux IIUC.  So I
guess I need a bool argument or similar to semanage_copy_file() to
indicate when a fsync is required.  Even with this change, installing
the final SELinux policy files under /etc/selinux won't be fully
atomic; one can still end up with a mix of old and new (e.g. new
policy.32 with old file_contexts).  But that's out of scope for this
particular bug.

      reply	other threads:[~2020-05-13 19:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-13 18:46 [PATCH] libsemanage: fsync before rename Smalley
2020-05-13 18:52 ` Nicolas Iooss
2020-05-13 19:08   ` Stephen Smalley [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=CAEjxPJ6U4vx+OB8WD689Vki1jVLeBzOgfgMawhbsT-KCXVn-WQ@mail.gmail.com \
    --to=stephen.smalley.work@gmail.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.