1*2d543d20SAndroid Build Coastguard Worker #pragma once 2*2d543d20SAndroid Build Coastguard Worker 3*2d543d20SAndroid Build Coastguard Worker #include <stdbool.h> 4*2d543d20SAndroid Build Coastguard Worker #include <sys/types.h> 5*2d543d20SAndroid Build Coastguard Worker 6*2d543d20SAndroid Build Coastguard Worker #include <selinux/context.h> 7*2d543d20SAndroid Build Coastguard Worker #include <selinux/selinux.h> 8*2d543d20SAndroid Build Coastguard Worker 9*2d543d20SAndroid Build Coastguard Worker #ifdef __cplusplus 10*2d543d20SAndroid Build Coastguard Worker extern "C" { 11*2d543d20SAndroid Build Coastguard Worker #endif 12*2d543d20SAndroid Build Coastguard Worker 13*2d543d20SAndroid Build Coastguard Worker // Context files (file_contexts, service_contexts, etc) may be spread over 14*2d543d20SAndroid Build Coastguard Worker // multiple partitions: system, system_ext, product, vendor and/or odm. 15*2d543d20SAndroid Build Coastguard Worker #define MAX_CONTEXT_PATHS 5 16*2d543d20SAndroid Build Coastguard Worker // The maximum number of alternatives for a file on one partition. 17*2d543d20SAndroid Build Coastguard Worker #define MAX_ALT_CONTEXT_PATHS 2 18*2d543d20SAndroid Build Coastguard Worker typedef struct path_alts { 19*2d543d20SAndroid Build Coastguard Worker const char *paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS]; 20*2d543d20SAndroid Build Coastguard Worker const char *partitions[MAX_CONTEXT_PATHS]; 21*2d543d20SAndroid Build Coastguard Worker } path_alts_t; 22*2d543d20SAndroid Build Coastguard Worker 23*2d543d20SAndroid Build Coastguard Worker /* Within each set of files, adds the first file that is accessible to `paths`. 24*2d543d20SAndroid Build Coastguard Worker * Returns the number of accessible files. */ 25*2d543d20SAndroid Build Coastguard Worker size_t find_existing_files( 26*2d543d20SAndroid Build Coastguard Worker const path_alts_t *path_sets, 27*2d543d20SAndroid Build Coastguard Worker const char *paths[MAX_CONTEXT_PATHS]); 28*2d543d20SAndroid Build Coastguard Worker 29*2d543d20SAndroid Build Coastguard Worker /* Within each set of files, adds the first file that is accessible to `paths`. 30*2d543d20SAndroid Build Coastguard Worker * Returns the number of accessible files. Also returns the partitions where 31*2d543d20SAndroid Build Coastguard Worker * the files exist. */ 32*2d543d20SAndroid Build Coastguard Worker size_t find_existing_files_with_partitions( 33*2d543d20SAndroid Build Coastguard Worker const path_alts_t *path_sets, 34*2d543d20SAndroid Build Coastguard Worker const char *paths[MAX_CONTEXT_PATHS], 35*2d543d20SAndroid Build Coastguard Worker const char *partitions[MAX_CONTEXT_PATHS]); 36*2d543d20SAndroid Build Coastguard Worker 37*2d543d20SAndroid Build Coastguard Worker /* Converts an array of file paths into an array of options for selabel_open. 38*2d543d20SAndroid Build Coastguard Worker * opts must be at least as large as paths. */ 39*2d543d20SAndroid Build Coastguard Worker void paths_to_opts( 40*2d543d20SAndroid Build Coastguard Worker const char* paths[MAX_CONTEXT_PATHS], 41*2d543d20SAndroid Build Coastguard Worker size_t npaths, 42*2d543d20SAndroid Build Coastguard Worker struct selinux_opt* const opts); 43*2d543d20SAndroid Build Coastguard Worker 44*2d543d20SAndroid Build Coastguard Worker /* Initialize a backend using the specified options. Ensure that any error is 45*2d543d20SAndroid Build Coastguard Worker * reported to the android logging facility */ 46*2d543d20SAndroid Build Coastguard Worker struct selabel_handle* initialize_backend( 47*2d543d20SAndroid Build Coastguard Worker unsigned int backend, 48*2d543d20SAndroid Build Coastguard Worker const char* name, 49*2d543d20SAndroid Build Coastguard Worker const struct selinux_opt* opts, 50*2d543d20SAndroid Build Coastguard Worker size_t nopts); 51*2d543d20SAndroid Build Coastguard Worker 52*2d543d20SAndroid Build Coastguard Worker /* Initialize a backend using a set of context paths */ 53*2d543d20SAndroid Build Coastguard Worker struct selabel_handle* context_handle( 54*2d543d20SAndroid Build Coastguard Worker unsigned int backend, 55*2d543d20SAndroid Build Coastguard Worker const path_alts_t *context_paths, 56*2d543d20SAndroid Build Coastguard Worker const char* name); 57*2d543d20SAndroid Build Coastguard Worker 58*2d543d20SAndroid Build Coastguard Worker /* 59*2d543d20SAndroid Build Coastguard Worker * This method helps in identifying paths that refer to users' app data. 60*2d543d20SAndroid Build Coastguard Worker * Labeling for app data is based on seapp_contexts and seinfo assignments 61*2d543d20SAndroid Build Coastguard Worker * rather than file_contexts and is managed by installd rather than by init. 62*2d543d20SAndroid Build Coastguard Worker */ 63*2d543d20SAndroid Build Coastguard Worker bool is_app_data_path(const char *pathname); 64*2d543d20SAndroid Build Coastguard Worker 65*2d543d20SAndroid Build Coastguard Worker /* 66*2d543d20SAndroid Build Coastguard Worker * Determines if a path is Credential Encrypted (CE). 67*2d543d20SAndroid Build Coastguard Worker * Some paths are not available when the device first boots (these are protected 68*2d543d20SAndroid Build Coastguard Worker * by a credential). They should not be processed by restorecon until decrypted. 69*2d543d20SAndroid Build Coastguard Worker * See also the --skip-ce option for restorecon. 70*2d543d20SAndroid Build Coastguard Worker */ 71*2d543d20SAndroid Build Coastguard Worker bool is_credential_encrypted_path(const char *pathname); 72*2d543d20SAndroid Build Coastguard Worker 73*2d543d20SAndroid Build Coastguard Worker /* Extract the pkgname and userid from a path. 74*2d543d20SAndroid Build Coastguard Worker * On success, the caller is responsible for free'ing pkgname. 75*2d543d20SAndroid Build Coastguard Worker * Returns 0 on success, -1 on invalid path, -2 on error. 76*2d543d20SAndroid Build Coastguard Worker */ 77*2d543d20SAndroid Build Coastguard Worker int extract_pkgname_and_userid(const char *pathname, char **pkgname, unsigned int *userid); 78*2d543d20SAndroid Build Coastguard Worker 79*2d543d20SAndroid Build Coastguard Worker /* The kind of request when looking up an seapp_context. */ 80*2d543d20SAndroid Build Coastguard Worker enum seapp_kind { 81*2d543d20SAndroid Build Coastguard Worker /* Returns the SELinux type for the app data directory */ 82*2d543d20SAndroid Build Coastguard Worker SEAPP_TYPE, 83*2d543d20SAndroid Build Coastguard Worker /* Returns the SELinux type for the app process */ 84*2d543d20SAndroid Build Coastguard Worker SEAPP_DOMAIN 85*2d543d20SAndroid Build Coastguard Worker }; 86*2d543d20SAndroid Build Coastguard Worker 87*2d543d20SAndroid Build Coastguard Worker /* Search an app (or its data) based on its name and information within the list 88*2d543d20SAndroid Build Coastguard Worker * of known seapp_contexts. If found, sets the type and categories of ctx and 89*2d543d20SAndroid Build Coastguard Worker * returns 0. Returns -1 in case of error; -2 for out of memory */ 90*2d543d20SAndroid Build Coastguard Worker int seapp_context_lookup(enum seapp_kind kind, 91*2d543d20SAndroid Build Coastguard Worker uid_t uid, 92*2d543d20SAndroid Build Coastguard Worker bool isSystemServer, 93*2d543d20SAndroid Build Coastguard Worker const char *seinfo, 94*2d543d20SAndroid Build Coastguard Worker const char *pkgname, 95*2d543d20SAndroid Build Coastguard Worker context_t ctx); 96*2d543d20SAndroid Build Coastguard Worker 97*2d543d20SAndroid Build Coastguard Worker /* Similar to seapp_context_lookup, but does not implicitly load and use the 98*2d543d20SAndroid Build Coastguard Worker * default context files. It should only be used for unit tests. */ 99*2d543d20SAndroid Build Coastguard Worker int seapp_context_lookup_internal(enum seapp_kind kind, 100*2d543d20SAndroid Build Coastguard Worker uid_t uid, 101*2d543d20SAndroid Build Coastguard Worker bool isSystemServer, 102*2d543d20SAndroid Build Coastguard Worker const char *seinfo, 103*2d543d20SAndroid Build Coastguard Worker const char *pkgname, 104*2d543d20SAndroid Build Coastguard Worker context_t ctx); 105*2d543d20SAndroid Build Coastguard Worker 106*2d543d20SAndroid Build Coastguard Worker /* Which categories should be associated to the process */ 107*2d543d20SAndroid Build Coastguard Worker enum levelFrom { 108*2d543d20SAndroid Build Coastguard Worker /* None */ 109*2d543d20SAndroid Build Coastguard Worker LEVELFROM_NONE, 110*2d543d20SAndroid Build Coastguard Worker /* The categories of the application */ 111*2d543d20SAndroid Build Coastguard Worker LEVELFROM_APP, 112*2d543d20SAndroid Build Coastguard Worker /* The categories of the end-user */ 113*2d543d20SAndroid Build Coastguard Worker LEVELFROM_USER, 114*2d543d20SAndroid Build Coastguard Worker /* Application and end-user */ 115*2d543d20SAndroid Build Coastguard Worker LEVELFROM_ALL 116*2d543d20SAndroid Build Coastguard Worker }; 117*2d543d20SAndroid Build Coastguard Worker 118*2d543d20SAndroid Build Coastguard Worker /* Sets the categories of ctx based on the level request */ 119*2d543d20SAndroid Build Coastguard Worker int set_range_from_level(context_t ctx, enum levelFrom levelFrom, uid_t userid, uid_t appid); 120*2d543d20SAndroid Build Coastguard Worker 121*2d543d20SAndroid Build Coastguard Worker /* Similar to seapp_context_reload, but does not implicitly load the default 122*2d543d20SAndroid Build Coastguard Worker * context files. It should only be used for unit tests. */ 123*2d543d20SAndroid Build Coastguard Worker int seapp_context_reload_internal(const path_alts_t *context_paths); 124*2d543d20SAndroid Build Coastguard Worker 125*2d543d20SAndroid Build Coastguard Worker #define SEINFO_BUFSIZ 256 126*2d543d20SAndroid Build Coastguard Worker /* A parsed seinfo */ 127*2d543d20SAndroid Build Coastguard Worker struct parsed_seinfo { 128*2d543d20SAndroid Build Coastguard Worker char base[SEINFO_BUFSIZ]; 129*2d543d20SAndroid Build Coastguard Worker #define IS_PRIV_APP (1 << 0) 130*2d543d20SAndroid Build Coastguard Worker #define IS_FROM_RUN_AS (1 << 1) 131*2d543d20SAndroid Build Coastguard Worker #define IS_EPHEMERAL_APP (1 << 2) 132*2d543d20SAndroid Build Coastguard Worker #define IS_ISOLATED_COMPUTE_APP (1 << 3) 133*2d543d20SAndroid Build Coastguard Worker #define IS_SDK_SANDBOX_AUDIT (1 << 4) 134*2d543d20SAndroid Build Coastguard Worker #define IS_SDK_SANDBOX_NEXT (1 << 5) 135*2d543d20SAndroid Build Coastguard Worker int32_t is; 136*2d543d20SAndroid Build Coastguard Worker bool isPreinstalledApp; 137*2d543d20SAndroid Build Coastguard Worker char partition[SEINFO_BUFSIZ]; 138*2d543d20SAndroid Build Coastguard Worker int32_t targetSdkVersion; 139*2d543d20SAndroid Build Coastguard Worker }; 140*2d543d20SAndroid Build Coastguard Worker 141*2d543d20SAndroid Build Coastguard Worker /* Parses an seinfo string. Returns -1 if an error occurred. */ 142*2d543d20SAndroid Build Coastguard Worker int parse_seinfo(const char* seinfo, struct parsed_seinfo* info); 143*2d543d20SAndroid Build Coastguard Worker #ifdef __cplusplus 144*2d543d20SAndroid Build Coastguard Worker } 145*2d543d20SAndroid Build Coastguard Worker #endif 146