Skip to contentSkip to conversation

libselinux: use designated initializers for label initfuncs

PATCH In review Thiébaud Weksteen 3 participants 3 messages Jul 14 – Jul 15 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libselinux: use designated initializers for label initfuncs

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.

---
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(-)
libselinux/include/selinux/label.h +5 -0
index 748b0db0..bde2501f 100644
@@ -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
libselinux/src/label.c +12 -6
index 1ccd1abe..991b9769 100644
@@ -4,6 +4,7 @@
  * Author : Eamon Walsh <[email protected]>
  */
 
+#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)
 {
Signature · 1 line
2.55.0.229.g6434b31f56-goog
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Thiébaud Weksteen

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.

Quoted 8 lines

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.

Quoted 64 lines

---
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(-)
libselinux/include/selinux/label.h +5 -0
index 748b0db0..bde2501f 100644
@@ -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
libselinux/src/label.c +12 -7
index 1ccd1abe..991b9769 100644
@@ -4,6 +4,7 @@
  * Author : Eamon Walsh <[email protected]>
  */
 
+#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

#2 lore raw
Quoted 10 lines · Stephen Smalley
Quoted 8 lines · Thiébaud Weksteen

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.

Quoted 8 lines
Quoted 5 lines

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.

Merged.
Thanks,
Jim

Quoted 65 lines
Quoted 65 lines

---
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(-)
libselinux/include/selinux/label.h +5 -0
index 748b0db0..bde2501f 100644
@@ -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
libselinux/src/label.c +12 -7
index 1ccd1abe..991b9769 100644
@@ -4,6 +4,7 @@
  * Author : Eamon Walsh <[email protected]>
  */
 
+#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