Define and use path_alts_t

The context file locations are represented using a 2-dimension array of
paths. Use a struct to simplify the syntax.

Bug: 234313751
Test: m
Change-Id: Iaf62955d1c142f4210215cecbee427e91031516e
This commit is contained in:
Thiébaud Weksteen 2022-10-03 15:13:49 +11:00
parent 12b4861e66
commit c8b3ae636f
4 changed files with 37 additions and 37 deletions

View file

@ -16,7 +16,7 @@
#endif // LOG_EVENT_STRING
#endif // __ANDROID_VNDK__
static const char* const service_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
static const path_alts_t service_context_paths = { .paths = {
{
"/system/etc/selinux/plat_service_contexts",
"/plat_service_contexts"
@ -36,9 +36,9 @@ static const char* const service_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEX
"/vendor/etc/selinux/vendor_service_contexts",
"/vendor_service_contexts"
}
};
}};
static const char* const hwservice_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
static const path_alts_t hwservice_context_paths = { .paths = {
{
"/system/etc/selinux/plat_hwservice_contexts",
"/plat_hwservice_contexts"
@ -59,16 +59,16 @@ static const char* const hwservice_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONT
"/odm/etc/selinux/odm_hwservice_contexts",
"/odm_hwservice_contexts"
},
};
}};
static const char* const vndservice_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
static const path_alts_t vndservice_context_paths = { .paths = {
{
"/vendor/etc/selinux/vndservice_contexts",
"/vndservice_contexts"
}
};
}};
static const char* const keystore2_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
static const path_alts_t keystore2_context_paths = { .paths = {
{
"/system/etc/selinux/plat_keystore2_key_contexts",
"/plat_keystore2_key_contexts"
@ -85,16 +85,16 @@ static const char* const keystore2_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONT
"/vendor/etc/selinux/vendor_keystore2_key_contexts",
"/vendor_keystore2_key_contexts"
}
};
}};
size_t find_existing_files(
const char* const path_sets[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS],
const path_alts_t *path_sets,
const char* paths[MAX_CONTEXT_PATHS])
{
size_t i, j, len = 0;
for (i = 0; i < MAX_CONTEXT_PATHS; i++) {
for (j = 0; j < MAX_ALT_CONTEXT_PATHS; j++) {
const char* file = path_sets[i][j];
const char* file = path_sets->paths[i][j];
if (file && access(file, R_OK) != -1) {
paths[len++] = file;
/* Within each set, only the first valid entry is used */
@ -140,8 +140,8 @@ struct selabel_handle* initialize_backend(
struct selabel_handle* context_handle(
unsigned int backend,
const char* const context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS],
const char* name)
const path_alts_t *context_paths,
const char *name)
{
const char* existing_paths[MAX_CONTEXT_PATHS];
struct selinux_opt opts[MAX_CONTEXT_PATHS];
@ -155,22 +155,22 @@ struct selabel_handle* context_handle(
struct selabel_handle* selinux_android_service_context_handle(void)
{
return context_handle(SELABEL_CTX_ANDROID_SERVICE, service_context_paths, "service");
return context_handle(SELABEL_CTX_ANDROID_SERVICE, &service_context_paths, "service");
}
struct selabel_handle* selinux_android_hw_service_context_handle(void)
{
return context_handle(SELABEL_CTX_ANDROID_SERVICE, hwservice_context_paths, "hwservice");
return context_handle(SELABEL_CTX_ANDROID_SERVICE, &hwservice_context_paths, "hwservice");
}
struct selabel_handle* selinux_android_vendor_service_context_handle(void)
{
return context_handle(SELABEL_CTX_ANDROID_SERVICE, vndservice_context_paths, "vndservice");
return context_handle(SELABEL_CTX_ANDROID_SERVICE, &vndservice_context_paths, "vndservice");
}
struct selabel_handle* selinux_android_keystore2_key_context_handle(void)
{
return context_handle(SELABEL_CTX_ANDROID_KEYSTORE2_KEY, keystore2_context_paths, "keystore2");
return context_handle(SELABEL_CTX_ANDROID_KEYSTORE2_KEY, &keystore2_context_paths, "keystore2");
}
int selinux_log_callback(int type, const char *fmt, ...)

View file

@ -13,12 +13,15 @@ extern "C" {
#define MAX_CONTEXT_PATHS 6
// The maximum number of alternatives for a file on one partition.
#define MAX_ALT_CONTEXT_PATHS 2
typedef struct path_alts {
const char *paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS];
} path_alts_t;
/* Within each set of files, adds the first file that is accessible to `paths`.
* Returns the number of accessible files. */
size_t find_existing_files(
const char* const path_sets[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS],
const char* paths[MAX_CONTEXT_PATHS]);
const path_alts_t *path_sets,
const char *paths[MAX_CONTEXT_PATHS]);
/* Converts an array of file paths into an array of options for selabel_open.
* opts must be at least as large as paths. */
@ -38,7 +41,7 @@ struct selabel_handle* initialize_backend(
/* Initialize a backend using a set of context paths */
struct selabel_handle* context_handle(
unsigned int backend,
const char* const context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS],
const path_alts_t *context_paths,
const char* name);

View file

@ -37,7 +37,7 @@
* /system/etc/selinux/plat_file_contexts exists, /plat_file_contexts will be
* ignored).
*/
static const char* const file_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
static const path_alts_t file_context_paths = { .paths = {
{
"/system/etc/selinux/plat_file_contexts",
"/plat_file_contexts"
@ -61,14 +61,14 @@ static const char* const file_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_P
"/odm/etc/selinux/odm_file_contexts",
"/odm_file_contexts"
}
};
}};
/* Locations for the seapp_contexts files. For each partition, only the first
* existing entry will be used (for example, if
* /system/etc/selinux/plat_seapp_contexts exists, /plat_seapp_contexts will be
* ignored).
*/
static const char* const seapp_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
static const path_alts_t seapp_context_paths = { .paths = {
{
"/system/etc/selinux/plat_seapp_contexts",
"/plat_seapp_contexts"
@ -92,7 +92,7 @@ static const char* const seapp_context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_
"/odm/etc/selinux/odm_seapp_contexts",
"/odm_seapp_contexts"
}
};
}};
/* Returns a handle for the file contexts backend, initialized with the Android
* configuration */
@ -102,7 +102,7 @@ struct selabel_handle* selinux_android_file_context_handle(void)
struct selinux_opt opts[MAX_CONTEXT_PATHS + 1];
int npaths, nopts;
npaths = find_existing_files(file_context_paths, file_contexts);
npaths = find_existing_files(&file_context_paths, file_contexts);
paths_to_opts(file_contexts, npaths, opts);
opts[npaths].type = SELABEL_OPT_BASEONLY;
@ -327,7 +327,7 @@ int selinux_android_seapp_context_reload(void)
int ret;
const char* seapp_contexts_files[MAX_CONTEXT_PATHS];
files_len = find_existing_files(seapp_context_paths, seapp_contexts_files);
files_len = find_existing_files(&seapp_context_paths, seapp_contexts_files);
/* Reset the current entries */
free_seapp_contexts();

View file

@ -32,15 +32,13 @@ TEST_F(AndroidSELinuxTest, LoadAndLookupServiceContext)
"android.hardware.power.IPower/default u:object_r:hal_power_service:s0\n",
vendor_contexts);
static const char *const
service_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
{ service_contexts.c_str(),
unused_service_contexts.c_str() },
{ vendor_contexts.c_str() }
};
const path_alts_t service_paths = { .paths = {
{ service_contexts.c_str(), unused_service_contexts.c_str() },
{ vendor_contexts.c_str() }
}};
struct selabel_handle *handle = context_handle(
SELABEL_CTX_ANDROID_SERVICE, service_paths, "test_service");
SELABEL_CTX_ANDROID_SERVICE, &service_paths, "test_service");
EXPECT_NE(handle, nullptr);
char *tcontext;
@ -75,12 +73,11 @@ TEST_F(AndroidSELinuxTest, FailLoadingServiceContext)
WriteStringToFile("garbage\n", service_contexts);
static const char *const
service_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
{ service_contexts.c_str() }
};
const path_alts_t service_paths = { .paths = {
{ service_contexts.c_str() }
}};
struct selabel_handle *handle = context_handle(
SELABEL_CTX_ANDROID_SERVICE, service_paths, "test_service");
SELABEL_CTX_ANDROID_SERVICE, &service_paths, "test_service");
EXPECT_EQ(handle, nullptr);
}