All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Smalley <stephen.smalley.work@gmail.com>
To: "Christian Göttsche" <cgzones@googlemail.com>
Cc: SElinux list <selinux@vger.kernel.org>,
	Mike Palmiotto <mike.palmiotto@crunchydata.com>
Subject: Re: [PATCH 1/3] libselinux: refactor wrapper in sestatus.c for safe shared memory access
Date: Mon, 24 Aug 2020 10:50:28 -0400	[thread overview]
Message-ID: <CAEjxPJ6BMLeieB6cFcLm0r1TSbp9Dbu214jyCHsnGfiJuTiNyg@mail.gmail.com> (raw)
In-Reply-To: <20200824131841.55687-1-cgzones@googlemail.com>

On Mon, Aug 24, 2020 at 9:19 AM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
> ---
>  libselinux/src/sestatus.c | 35 +++++++++++------------------------
>  1 file changed, 11 insertions(+), 24 deletions(-)
>
> diff --git a/libselinux/src/sestatus.c b/libselinux/src/sestatus.c
> index 814e86ee..925e6079 100644
> --- a/libselinux/src/sestatus.c
> +++ b/libselinux/src/sestatus.c
> @@ -80,6 +80,14 @@ static inline uint32_t read_sequence(struct selinux_status_t *status)
>         return seqno;
>  }
>
> +/* sequence must not be changed during references */
> +#define sestatus_save_access(name, result)                          \
> +       uint32_t _seqno;                                            \
> +       do {                                                        \
> +               _seqno = read_sequence(selinux_status);             \
> +               (result) = selinux_status->name;                    \
> +       } while (_seqno != read_sequence(selinux_status))           \

I'm not sure how much we gain from this macro versus losing in
readability of the calling code.
It should be clear at the call site that we are setting result to the
value of selinux_status->name, either by
having the macro "return" the value to the caller or passing the
address of result.
If we are going to use a macro with a local variable declaration, then
it needs to be wrapped with do { ... } while (0)
to ensure that the variable has its own scope/block.
I'm also not clear on the naming - why "save_access" - is that
supposed to be "safe_access"?
It would be nice if the trailing backslashes were aligned.
To be clear, this code is not currently thread-safe; the "safety" has
to do with getting a consistent view of the SELinux kernel status
page.

> @@ -157,13 +164,7 @@ int selinux_status_getenforce(void)
>                 return fallback_enforcing;
>         }
>
> -       /* sequence must not be changed during references */
> -       do {
> -               seqno = read_sequence(selinux_status);
> -
> -               enforcing = selinux_status->enforcing;
> -
> -       } while (seqno != read_sequence(selinux_status));
> +       sestatus_save_access(enforcing, enforcing);

Someone reading the above code snippet has no idea that we just set
enforcing to selinux_status->enforcing.

  parent reply	other threads:[~2020-08-24 14:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-24 13:18 [PATCH 1/3] libselinux: refactor wrapper in sestatus.c for safe shared memory access Christian Göttsche
2020-08-24 13:18 ` [PATCH 2/3] libselinux: safely access shared memory in selinux_status_updated() Christian Göttsche
2020-08-24 13:18 ` [PATCH 3/3] libselinux: initialize last_policyload in selinux_status_open() Christian Göttsche
2020-08-24 14:50 ` Stephen Smalley [this message]
2020-08-25 15:32 ` [PATCH v2 0/2] selinux_status_ changes Christian Göttsche
2020-08-25 15:32   ` [PATCH v2 1/2] libselinux: safely access shared memory in selinux_status_updated() Christian Göttsche
2020-08-26 12:52     ` Stephen Smalley
2020-08-25 15:32   ` [PATCH v2 2/2] libselinux: initialize last_policyload in selinux_status_open() Christian Göttsche
2020-08-26 12:53     ` Stephen Smalley
2020-08-31 14:25       ` Stephen Smalley

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=CAEjxPJ6BMLeieB6cFcLm0r1TSbp9Dbu214jyCHsnGfiJuTiNyg@mail.gmail.com \
    --to=stephen.smalley.work@gmail.com \
    --cc=cgzones@googlemail.com \
    --cc=mike.palmiotto@crunchydata.com \
    --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.