SELinux Security Module development
 help / color / mirror / Atom feed
From: "Thiébaud Weksteen" <tweek@google.com>
To: selinux@vger.kernel.org
Cc: "James Carter" <jwcart2@gmail.com>,
	"Stephen Smalley" <stephen.smalley.work@gmail.com>,
	"Thiébaud Weksteen" <tweek@google.com>
Subject: [PATCH v2] libselinux: use designated initializers for label initfuncs
Date: Wed, 15 Jul 2026 09:25:11 +1000	[thread overview]
Message-ID: <20260714232511.2858868-1-tweek@google.com> (raw)

Previously, the initfuncs array relied on implicit positional ordering
to match backend context constants (e.g., SELABEL_CTX_FILE). If constants
were reordered or added, array indexing could become misaligned.

Use C99 designated initializers in initfuncs to explicitly bind each
initialization function to its corresponding context constant.

Additionally, define _SELABEL_CTX_NUM in include/selinux/label.h
to count total backends, and add a compile-time static_assert ensuring
initfuncs stays in sync with _SELABEL_CTX_NUM.

Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
v2: Rebase on selinux/main and format

 libselinux/include/selinux/label.h |  5 +++++
 libselinux/src/label.c             | 18 ++++++++++++------
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/libselinux/include/selinux/label.h b/libselinux/include/selinux/label.h
index 748b0db0..bde2501f 100644
--- a/libselinux/include/selinux/label.h
+++ b/libselinux/include/selinux/label.h
@@ -37,6 +37,11 @@ struct selabel_handle;
 #define SELABEL_CTX_ANDROID_PROP 4
 /* Android service contexts */
 #define SELABEL_CTX_ANDROID_SERVICE 5
+/*
+ * Total number of context backends. When adding a new backend,
+ * please increase this value.
+ */
+#define _SELABEL_CTX_NUM 6
 
 /*
  * Available options
diff --git a/libselinux/src/label.c b/libselinux/src/label.c
index 1ccd1abe..991b9769 100644
--- a/libselinux/src/label.c
+++ b/libselinux/src/label.c
@@ -4,6 +4,7 @@
  * Author : Eamon Walsh <ewalsh@epoch.ncsc.mil>
  */
 
+#include <assert.h>
 #include <sys/types.h>
 #include <ctype.h>
 #include <errno.h>
@@ -45,14 +46,19 @@ typedef int (*selabel_initfunc)(struct selabel_handle *rec,
 				const struct selinux_opt *opts, unsigned nopts);
 
 static const selabel_initfunc initfuncs[] = {
-	&selabel_file_init,
-	CONFIG_MEDIA_BACKEND(selabel_media_init),
-	CONFIG_X_BACKEND(selabel_x_init),
-	CONFIG_DB_BACKEND(selabel_db_init),
-	CONFIG_ANDROID_BACKEND(selabel_property_init),
-	CONFIG_ANDROID_BACKEND(selabel_service_init),
+	[SELABEL_CTX_FILE] = &selabel_file_init,
+	[SELABEL_CTX_MEDIA] = CONFIG_MEDIA_BACKEND(selabel_media_init),
+	[SELABEL_CTX_X] = CONFIG_X_BACKEND(selabel_x_init),
+	[SELABEL_CTX_DB] = CONFIG_DB_BACKEND(selabel_db_init),
+	[SELABEL_CTX_ANDROID_PROP] =
+		CONFIG_ANDROID_BACKEND(selabel_property_init),
+	[SELABEL_CTX_ANDROID_SERVICE] =
+		CONFIG_ANDROID_BACKEND(selabel_service_init),
 };
 
+static_assert(ARRAY_SIZE(initfuncs) == _SELABEL_CTX_NUM,
+	      "initfuncs array size does not match _SELABEL_CTX_NUM");
+
 static inline struct selabel_digest *
 selabel_is_digest_set(const struct selinux_opt *opts, unsigned n)
 {
-- 
2.55.0.229.g6434b31f56-goog


             reply	other threads:[~2026-07-14 23:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-14 23:25 Thiébaud Weksteen [this message]
2026-07-15 13:05 ` [PATCH v2] libselinux: use designated initializers for label initfuncs Stephen Smalley
2026-07-15 13:36   ` James Carter

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=20260714232511.2858868-1-tweek@google.com \
    --to=tweek@google.com \
    --cc=jwcart2@gmail.com \
    --cc=selinux@vger.kernel.org \
    --cc=stephen.smalley.work@gmail.com \
    /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