All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Christian Göttsche" <cgzones@googlemail.com>
To: selinux@vger.kernel.org
Cc: Mike Palmiotto <mike.palmiotto@crunchydata.com>
Subject: [PATCH v2 1/2] libselinux: safely access shared memory in selinux_status_updated()
Date: Tue, 25 Aug 2020 17:32:04 +0200	[thread overview]
Message-ID: <20200825153205.224136-2-cgzones@googlemail.com> (raw)
In-Reply-To: <20200825153205.224136-1-cgzones@googlemail.com>

Access the shared nenory safe in regard to consistent view of the SELinux
kernel status page - not in regard to thread-safety.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/src/sestatus.c | 40 ++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/libselinux/src/sestatus.c b/libselinux/src/sestatus.c
index 814e86ee..ca2d3bbf 100644
--- a/libselinux/src/sestatus.c
+++ b/libselinux/src/sestatus.c
@@ -91,7 +91,9 @@ static inline uint32_t read_sequence(struct selinux_status_t *status)
 int selinux_status_updated(void)
 {
 	uint32_t	curr_seqno;
-	int		result = 0;
+	uint32_t	tmp_seqno;
+	uint32_t	enforcing;
+	uint32_t	policyload;
 
 	if (selinux_status == NULL) {
 		errno = EINVAL;
@@ -117,21 +119,29 @@ int selinux_status_updated(void)
 	if (last_seqno & 0x0001)
 		last_seqno = curr_seqno;
 
-	if (last_seqno != curr_seqno)
-	{
-		if (avc_enforcing != (int) selinux_status->enforcing) {
-			if (avc_process_setenforce(selinux_status->enforcing) < 0)
-				return -1;
-		}
-		if (last_policyload != selinux_status->policyload) {
-			if (avc_process_policyload(selinux_status->policyload) < 0)
-				return -1;
-			last_policyload = selinux_status->policyload;
-		}
-		last_seqno = curr_seqno;
-		result = 1;
+	if (last_seqno == curr_seqno)
+		return 0;
+
+	/* sequence must not be changed during references */
+	do {
+		enforcing = selinux_status->enforcing;
+		policyload = selinux_status->policyload;
+		tmp_seqno = curr_seqno;
+		curr_seqno = read_sequence(selinux_status);
+	} while (tmp_seqno != curr_seqno);
+
+	if (avc_enforcing != (int) enforcing) {
+		if (avc_process_setenforce(enforcing) < 0)
+			return -1;
+	}
+	if (last_policyload != policyload) {
+		if (avc_process_policyload(policyload) < 0)
+			return -1;
+		last_policyload = policyload;
 	}
-	return result;
+	last_seqno = curr_seqno;
+
+	return 1;
 }
 
 /*
-- 
2.28.0


  reply	other threads:[~2020-08-25 15:32 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 ` [PATCH 1/3] libselinux: refactor wrapper in sestatus.c for safe shared memory access Stephen Smalley
2020-08-25 15:32 ` [PATCH v2 0/2] selinux_status_ changes Christian Göttsche
2020-08-25 15:32   ` Christian Göttsche [this message]
2020-08-26 12:52     ` [PATCH v2 1/2] libselinux: safely access shared memory in selinux_status_updated() 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=20200825153205.224136-2-cgzones@googlemail.com \
    --to=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.