Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 1 | #include <sys/types.h> |
| 2 | #include <unistd.h> |
| 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <ctype.h> |
| 7 | #include <errno.h> |
| 8 | #include <pwd.h> |
| 9 | #include <grp.h> |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 10 | #include <dirent.h> |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 11 | #include <sys/mman.h> |
| 12 | #include <sys/mount.h> |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 13 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <selinux/selinux.h> |
| 17 | #include <selinux/context.h> |
| 18 | #include <selinux/android.h> |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 19 | #include <selinux/label.h> |
Stephen Smalley | e8b0fd8 | 2012-07-31 09:12:53 -0400 | [diff] [blame] | 20 | #include <selinux/avc.h> |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 21 | #include <private/android_filesystem_config.h> |
Stephen Smalley | d10c343 | 2012-11-05 11:49:35 -0500 | [diff] [blame] | 22 | #include "policy.h" |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 23 | #include "callbacks.h" |
| 24 | #include "selinux_internal.h" |
| 25 | |
| 26 | /* |
| 27 | * XXX Where should this configuration file be located? |
| 28 | * Needs to be accessible by zygote and installd when |
| 29 | * setting credentials for app processes and setting permissions |
| 30 | * on app data directories. |
| 31 | */ |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 32 | static char const * const seapp_contexts_file[] = { |
repo sync | 6750780 | 2013-04-26 15:49:39 -0700 | [diff] [blame] | 33 | "/data/security/current/seapp_contexts", |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 34 | "/seapp_contexts", |
William Roberts | 8ed4242 | 2013-04-15 16:13:30 -0700 | [diff] [blame] | 35 | NULL }; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 36 | |
Stephen Smalley | 32ebfe8 | 2012-03-20 13:05:37 -0400 | [diff] [blame] | 37 | static const struct selinux_opt seopts[] = { |
Geremy Condra | 01cccbf | 2013-04-15 12:26:44 -0700 | [diff] [blame] | 38 | { SELABEL_OPT_PATH, "/data/security/current/file_contexts" }, |
Stephen Smalley | 32ebfe8 | 2012-03-20 13:05:37 -0400 | [diff] [blame] | 39 | { SELABEL_OPT_PATH, "/file_contexts" }, |
| 40 | { 0, NULL } }; |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 41 | |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 42 | static const struct selinux_opt seopt_backup[] = { |
Geremy Condra | 01cccbf | 2013-04-15 12:26:44 -0700 | [diff] [blame] | 43 | { SELABEL_OPT_PATH, "/data/security/current/file_contexts_backup" }, |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 44 | { SELABEL_OPT_PATH, "/file_contexts" }, |
| 45 | { 0, NULL } }; |
| 46 | |
Stephen Smalley | 4a655ec | 2012-09-18 15:08:32 -0400 | [diff] [blame] | 47 | static const char *const sepolicy_file[] = { |
Geremy Condra | 01cccbf | 2013-04-15 12:26:44 -0700 | [diff] [blame] | 48 | "/data/security/current/sepolicy", |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 49 | "/sepolicy", |
William Roberts | 8ed4242 | 2013-04-15 16:13:30 -0700 | [diff] [blame] | 50 | NULL }; |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 51 | |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 52 | enum levelFrom { |
| 53 | LEVELFROM_NONE, |
| 54 | LEVELFROM_APP, |
| 55 | LEVELFROM_USER, |
| 56 | LEVELFROM_ALL |
| 57 | }; |
| 58 | |
| 59 | #if DEBUG |
| 60 | static char const * const levelFromName[] = { |
| 61 | "none", |
| 62 | "app", |
| 63 | "user", |
| 64 | "all" |
| 65 | }; |
| 66 | #endif |
| 67 | |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 68 | struct prefix_str { |
| 69 | size_t len; |
| 70 | char *str; |
| 71 | char is_prefix; |
| 72 | }; |
| 73 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 74 | struct seapp_context { |
| 75 | /* input selectors */ |
| 76 | char isSystemServer; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 77 | struct prefix_str user; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 78 | char *seinfo; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 79 | struct prefix_str name; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 80 | /* outputs */ |
| 81 | char *domain; |
| 82 | char *type; |
| 83 | char *level; |
William Roberts | 1b36ad0 | 2012-07-27 13:52:33 -0700 | [diff] [blame] | 84 | char *sebool; |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 85 | enum levelFrom levelFrom; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static int seapp_context_cmp(const void *A, const void *B) |
| 89 | { |
Nick Kralevich | 833cba6 | 2013-11-19 11:24:33 -0800 | [diff] [blame] | 90 | const struct seapp_context *const *sp1 = (const struct seapp_context *const *) A; |
| 91 | const struct seapp_context *const *sp2 = (const struct seapp_context *const *) B; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 92 | const struct seapp_context *s1 = *sp1, *s2 = *sp2; |
| 93 | |
| 94 | /* Give precedence to isSystemServer=true. */ |
| 95 | if (s1->isSystemServer != s2->isSystemServer) |
| 96 | return (s1->isSystemServer ? -1 : 1); |
| 97 | |
| 98 | /* Give precedence to a specified user= over an unspecified user=. */ |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 99 | if (s1->user.str && !s2->user.str) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 100 | return -1; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 101 | if (!s1->user.str && s2->user.str) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 102 | return 1; |
| 103 | |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 104 | if (s1->user.str) { |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 105 | /* Give precedence to a fixed user= string over a prefix. */ |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 106 | if (s1->user.is_prefix != s2->user.is_prefix) |
| 107 | return (s2->user.is_prefix ? -1 : 1); |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 108 | |
| 109 | /* Give precedence to a longer prefix over a shorter prefix. */ |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 110 | if (s1->user.is_prefix && s1->user.len != s2->user.len) |
| 111 | return (s1->user.len > s2->user.len) ? -1 : 1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* Give precedence to a specified seinfo= over an unspecified seinfo=. */ |
| 115 | if (s1->seinfo && !s2->seinfo) |
| 116 | return -1; |
| 117 | if (!s1->seinfo && s2->seinfo) |
| 118 | return 1; |
| 119 | |
| 120 | /* Give precedence to a specified name= over an unspecified name=. */ |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 121 | if (s1->name.str && !s2->name.str) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 122 | return -1; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 123 | if (!s1->name.str && s2->name.str) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 124 | return 1; |
| 125 | |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 126 | if (s1->name.str) { |
| 127 | /* Give precedence to a fixed name= string over a prefix. */ |
| 128 | if (s1->name.is_prefix != s2->name.is_prefix) |
| 129 | return (s2->name.is_prefix ? -1 : 1); |
| 130 | |
| 131 | /* Give precedence to a longer prefix over a shorter prefix. */ |
| 132 | if (s1->name.is_prefix && s1->name.len != s2->name.len) |
| 133 | return (s1->name.len > s2->name.len) ? -1 : 1; |
| 134 | } |
| 135 | |
William Roberts | 1b36ad0 | 2012-07-27 13:52:33 -0700 | [diff] [blame] | 136 | /* Give precedence to a specified sebool= over an unspecified sebool=. */ |
| 137 | if (s1->sebool && !s2->sebool) |
| 138 | return -1; |
| 139 | if (!s1->sebool && s2->sebool) |
| 140 | return 1; |
| 141 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 142 | /* Anything else has equal precedence. */ |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static struct seapp_context **seapp_contexts = NULL; |
| 147 | static int nspec = 0; |
| 148 | |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 149 | int selinux_android_seapp_context_reload(void) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 150 | { |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 151 | FILE *fp = NULL; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 152 | char line_buf[BUFSIZ]; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 153 | char *token; |
| 154 | unsigned lineno; |
| 155 | struct seapp_context *cur; |
| 156 | char *p, *name = NULL, *value = NULL, *saveptr; |
| 157 | size_t len; |
Stephen Smalley | 300bebb | 2013-04-16 15:33:16 -0400 | [diff] [blame] | 158 | int i = 0, n, ret; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 159 | |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 160 | while ((fp==NULL) && seapp_contexts_file[i]) |
| 161 | fp = fopen(seapp_contexts_file[i++], "r"); |
| 162 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 163 | if (!fp) { |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 164 | selinux_log(SELINUX_ERROR, "%s: could not open any seapp_contexts file", __FUNCTION__); |
| 165 | return -1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Stephen Smalley | 300bebb | 2013-04-16 15:33:16 -0400 | [diff] [blame] | 168 | if (seapp_contexts) { |
| 169 | for (n = 0; n < nspec; n++) { |
| 170 | cur = seapp_contexts[n]; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 171 | free(cur->user.str); |
Stephen Smalley | 300bebb | 2013-04-16 15:33:16 -0400 | [diff] [blame] | 172 | free(cur->seinfo); |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 173 | free(cur->name.str); |
Stephen Smalley | 300bebb | 2013-04-16 15:33:16 -0400 | [diff] [blame] | 174 | free(cur->domain); |
| 175 | free(cur->type); |
| 176 | free(cur->level); |
| 177 | free(cur->sebool); |
| 178 | } |
| 179 | free(seapp_contexts); |
| 180 | } |
| 181 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 182 | nspec = 0; |
| 183 | while (fgets(line_buf, sizeof line_buf - 1, fp)) { |
| 184 | p = line_buf; |
| 185 | while (isspace(*p)) |
| 186 | p++; |
| 187 | if (*p == '#' || *p == 0) |
| 188 | continue; |
| 189 | nspec++; |
| 190 | } |
| 191 | |
Nick Kralevich | 833cba6 | 2013-11-19 11:24:33 -0800 | [diff] [blame] | 192 | seapp_contexts = (struct seapp_context **) calloc(nspec, sizeof(struct seapp_context *)); |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 193 | if (!seapp_contexts) |
| 194 | goto oom; |
| 195 | |
| 196 | rewind(fp); |
| 197 | nspec = 0; |
| 198 | lineno = 1; |
| 199 | while (fgets(line_buf, sizeof line_buf - 1, fp)) { |
| 200 | len = strlen(line_buf); |
| 201 | if (line_buf[len - 1] == '\n') |
| 202 | line_buf[len - 1] = 0; |
| 203 | p = line_buf; |
| 204 | while (isspace(*p)) |
| 205 | p++; |
| 206 | if (*p == '#' || *p == 0) |
| 207 | continue; |
| 208 | |
Nick Kralevich | 833cba6 | 2013-11-19 11:24:33 -0800 | [diff] [blame] | 209 | cur = (struct seapp_context *) calloc(1, sizeof(struct seapp_context)); |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 210 | if (!cur) |
| 211 | goto oom; |
| 212 | |
| 213 | token = strtok_r(p, " \t", &saveptr); |
| 214 | if (!token) |
| 215 | goto err; |
| 216 | |
| 217 | while (1) { |
| 218 | name = token; |
| 219 | value = strchr(name, '='); |
| 220 | if (!value) |
| 221 | goto err; |
| 222 | *value++ = 0; |
| 223 | |
| 224 | if (!strcasecmp(name, "isSystemServer")) { |
| 225 | if (!strcasecmp(value, "true")) |
| 226 | cur->isSystemServer = 1; |
| 227 | else if (!strcasecmp(value, "false")) |
| 228 | cur->isSystemServer = 0; |
| 229 | else { |
| 230 | goto err; |
| 231 | } |
| 232 | } else if (!strcasecmp(name, "user")) { |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 233 | cur->user.str = strdup(value); |
| 234 | if (!cur->user.str) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 235 | goto oom; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 236 | cur->user.len = strlen(cur->user.str); |
| 237 | if (cur->user.str[cur->user.len-1] == '*') |
| 238 | cur->user.is_prefix = 1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 239 | } else if (!strcasecmp(name, "seinfo")) { |
| 240 | cur->seinfo = strdup(value); |
| 241 | if (!cur->seinfo) |
| 242 | goto oom; |
| 243 | } else if (!strcasecmp(name, "name")) { |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 244 | cur->name.str = strdup(value); |
| 245 | if (!cur->name.str) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 246 | goto oom; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 247 | cur->name.len = strlen(cur->name.str); |
| 248 | if (cur->name.str[cur->name.len-1] == '*') |
| 249 | cur->name.is_prefix = 1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 250 | } else if (!strcasecmp(name, "domain")) { |
| 251 | cur->domain = strdup(value); |
| 252 | if (!cur->domain) |
| 253 | goto oom; |
| 254 | } else if (!strcasecmp(name, "type")) { |
| 255 | cur->type = strdup(value); |
| 256 | if (!cur->type) |
| 257 | goto oom; |
| 258 | } else if (!strcasecmp(name, "levelFromUid")) { |
| 259 | if (!strcasecmp(value, "true")) |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 260 | cur->levelFrom = LEVELFROM_APP; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 261 | else if (!strcasecmp(value, "false")) |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 262 | cur->levelFrom = LEVELFROM_NONE; |
| 263 | else { |
| 264 | goto err; |
| 265 | } |
| 266 | } else if (!strcasecmp(name, "levelFrom")) { |
| 267 | if (!strcasecmp(value, "none")) |
| 268 | cur->levelFrom = LEVELFROM_NONE; |
| 269 | else if (!strcasecmp(value, "app")) |
| 270 | cur->levelFrom = LEVELFROM_APP; |
| 271 | else if (!strcasecmp(value, "user")) |
| 272 | cur->levelFrom = LEVELFROM_USER; |
| 273 | else if (!strcasecmp(value, "all")) |
| 274 | cur->levelFrom = LEVELFROM_ALL; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 275 | else { |
| 276 | goto err; |
| 277 | } |
| 278 | } else if (!strcasecmp(name, "level")) { |
| 279 | cur->level = strdup(value); |
| 280 | if (!cur->level) |
| 281 | goto oom; |
William Roberts | 1b36ad0 | 2012-07-27 13:52:33 -0700 | [diff] [blame] | 282 | } else if (!strcasecmp(name, "sebool")) { |
| 283 | cur->sebool = strdup(value); |
| 284 | if (!cur->sebool) |
| 285 | goto oom; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 286 | } else |
| 287 | goto err; |
| 288 | |
| 289 | token = strtok_r(NULL, " \t", &saveptr); |
| 290 | if (!token) |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | seapp_contexts[nspec] = cur; |
| 295 | nspec++; |
| 296 | lineno++; |
| 297 | } |
| 298 | |
| 299 | qsort(seapp_contexts, nspec, sizeof(struct seapp_context *), |
| 300 | seapp_context_cmp); |
| 301 | |
| 302 | #if DEBUG |
| 303 | { |
| 304 | int i; |
| 305 | for (i = 0; i < nspec; i++) { |
| 306 | cur = seapp_contexts[i]; |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 307 | selinux_log(SELINUX_INFO, "%s: isSystemServer=%s user=%s seinfo=%s name=%s sebool=%s -> domain=%s type=%s level=%s levelFrom=%s", |
William Roberts | 1b36ad0 | 2012-07-27 13:52:33 -0700 | [diff] [blame] | 308 | __FUNCTION__, |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 309 | cur->isSystemServer ? "true" : "false", cur->user.str, |
| 310 | cur->seinfo, cur->name.str, cur->sebool, cur->domain, |
William Roberts | 1b36ad0 | 2012-07-27 13:52:33 -0700 | [diff] [blame] | 311 | cur->type, cur->level, |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 312 | levelFromName[cur->levelFrom]); |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | #endif |
| 316 | |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 317 | ret = 0; |
| 318 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 319 | out: |
| 320 | fclose(fp); |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 321 | return ret; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 322 | |
| 323 | err: |
| 324 | selinux_log(SELINUX_ERROR, "%s: Error reading %s, line %u, name %s, value %s\n", |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 325 | __FUNCTION__, seapp_contexts_file[i - 1], lineno, name, value); |
| 326 | ret = -1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 327 | goto out; |
| 328 | oom: |
| 329 | selinux_log(SELINUX_ERROR, |
| 330 | "%s: Out of memory\n", __FUNCTION__); |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 331 | ret = -1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 332 | goto out; |
| 333 | } |
| 334 | |
Stephen Smalley | 7446c91 | 2012-03-19 10:37:05 -0400 | [diff] [blame] | 335 | |
| 336 | static void seapp_context_init(void) |
| 337 | { |
| 338 | selinux_android_seapp_context_reload(); |
| 339 | } |
| 340 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 341 | static pthread_once_t once = PTHREAD_ONCE_INIT; |
| 342 | |
Stephen Smalley | d23b9e0 | 2012-09-21 10:45:39 -0400 | [diff] [blame] | 343 | /* |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 344 | * Max id that can be mapped to category set uniquely |
Stephen Smalley | d23b9e0 | 2012-09-21 10:45:39 -0400 | [diff] [blame] | 345 | * using the current scheme. |
| 346 | */ |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 347 | #define CAT_MAPPING_MAX_ID (0x1<<16) |
Stephen Smalley | d23b9e0 | 2012-09-21 10:45:39 -0400 | [diff] [blame] | 348 | |
| 349 | enum seapp_kind { |
| 350 | SEAPP_TYPE, |
| 351 | SEAPP_DOMAIN |
| 352 | }; |
| 353 | |
| 354 | static int seapp_context_lookup(enum seapp_kind kind, |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 355 | uid_t uid, |
| 356 | int isSystemServer, |
Stephen Smalley | edfaad8 | 2012-07-12 11:30:24 -0400 | [diff] [blame] | 357 | const char *seinfo, |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 358 | const char *pkgname, |
| 359 | context_t ctx) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 360 | { |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 361 | const char *username = NULL; |
Nick Kralevich | b1ae15a | 2013-07-11 17:35:53 -0700 | [diff] [blame] | 362 | struct seapp_context *cur = NULL; |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 363 | int i; |
| 364 | size_t n; |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 365 | uid_t userid; |
| 366 | uid_t appid; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 367 | |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 368 | userid = uid / AID_USER; |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 369 | appid = uid % AID_USER; |
| 370 | if (appid < AID_APP) { |
| 371 | for (n = 0; n < android_id_count; n++) { |
| 372 | if (android_ids[n].aid == appid) { |
| 373 | username = android_ids[n].name; |
| 374 | break; |
| 375 | } |
Stephen Smalley | ce4e2e6 | 2012-08-23 12:20:42 -0400 | [diff] [blame] | 376 | } |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 377 | if (!username) |
| 378 | goto err; |
| 379 | } else if (appid < AID_ISOLATED_START) { |
Stephen Smalley | 525a224 | 2012-09-24 10:22:45 -0400 | [diff] [blame] | 380 | username = "_app"; |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 381 | appid -= AID_APP; |
| 382 | } else { |
Stephen Smalley | 525a224 | 2012-09-24 10:22:45 -0400 | [diff] [blame] | 383 | username = "_isolated"; |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 384 | appid -= AID_ISOLATED_START; |
Stephen Smalley | ba70ee4 | 2012-07-10 15:49:04 -0400 | [diff] [blame] | 385 | } |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 386 | |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 387 | if (appid >= CAT_MAPPING_MAX_ID || userid >= CAT_MAPPING_MAX_ID) |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 388 | goto err; |
| 389 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 390 | for (i = 0; i < nspec; i++) { |
| 391 | cur = seapp_contexts[i]; |
| 392 | |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 393 | if (cur->isSystemServer != isSystemServer) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 394 | continue; |
| 395 | |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 396 | if (cur->user.str) { |
| 397 | if (cur->user.is_prefix) { |
| 398 | if (strncasecmp(username, cur->user.str, cur->user.len-1)) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 399 | continue; |
| 400 | } else { |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 401 | if (strcasecmp(username, cur->user.str)) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 402 | continue; |
| 403 | } |
| 404 | } |
| 405 | |
Stephen Smalley | edfaad8 | 2012-07-12 11:30:24 -0400 | [diff] [blame] | 406 | if (cur->seinfo) { |
| 407 | if (!seinfo || strcasecmp(seinfo, cur->seinfo)) |
| 408 | continue; |
| 409 | } |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 410 | |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 411 | if (cur->name.str) { |
| 412 | if(!pkgname) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 413 | continue; |
William Roberts | e3615f9 | 2013-09-11 20:35:53 -0700 | [diff] [blame] | 414 | |
| 415 | if (cur->name.is_prefix) { |
| 416 | if (strncasecmp(pkgname, cur->name.str, cur->name.len-1)) |
| 417 | continue; |
| 418 | } else { |
| 419 | if (strcasecmp(pkgname, cur->name.str)) |
| 420 | continue; |
| 421 | } |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 422 | } |
| 423 | |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 424 | if (kind == SEAPP_TYPE && !cur->type) |
| 425 | continue; |
| 426 | else if (kind == SEAPP_DOMAIN && !cur->domain) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 427 | continue; |
| 428 | |
William Roberts | 1b36ad0 | 2012-07-27 13:52:33 -0700 | [diff] [blame] | 429 | if (cur->sebool) { |
| 430 | int value = security_get_boolean_active(cur->sebool); |
| 431 | if (value == 0) |
| 432 | continue; |
| 433 | else if (value == -1) { |
| 434 | selinux_log(SELINUX_ERROR, \ |
| 435 | "Could not find boolean: %s ", cur->sebool); |
| 436 | goto err; |
| 437 | } |
| 438 | } |
| 439 | |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 440 | if (kind == SEAPP_TYPE) { |
| 441 | if (context_type_set(ctx, cur->type)) |
| 442 | goto oom; |
| 443 | } else if (kind == SEAPP_DOMAIN) { |
| 444 | if (context_type_set(ctx, cur->domain)) |
| 445 | goto oom; |
| 446 | } |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 447 | |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 448 | if (cur->levelFrom != LEVELFROM_NONE) { |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 449 | char level[255]; |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 450 | switch (cur->levelFrom) { |
| 451 | case LEVELFROM_APP: |
Stephen Smalley | df1ece2 | 2013-12-02 10:07:48 -0500 | [diff] [blame^] | 452 | snprintf(level, sizeof level, "s0:c%u,c%u", |
| 453 | appid & 0xff, |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 454 | 256 + (appid>>8 & 0xff)); |
| 455 | break; |
| 456 | case LEVELFROM_USER: |
Stephen Smalley | df1ece2 | 2013-12-02 10:07:48 -0500 | [diff] [blame^] | 457 | snprintf(level, sizeof level, "s0:c%u,c%u", |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 458 | 512 + (userid & 0xff), |
| 459 | 768 + (userid>>8 & 0xff)); |
| 460 | break; |
| 461 | case LEVELFROM_ALL: |
Stephen Smalley | df1ece2 | 2013-12-02 10:07:48 -0500 | [diff] [blame^] | 462 | snprintf(level, sizeof level, "s0:c%u,c%u,c%u,c%u", |
| 463 | appid & 0xff, |
Stephen Smalley | a879598 | 2012-11-28 10:42:46 -0500 | [diff] [blame] | 464 | 256 + (appid>>8 & 0xff), |
| 465 | 512 + (userid & 0xff), |
| 466 | 768 + (userid>>8 & 0xff)); |
| 467 | break; |
| 468 | default: |
| 469 | goto err; |
| 470 | } |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 471 | if (context_range_set(ctx, level)) |
| 472 | goto oom; |
| 473 | } else if (cur->level) { |
| 474 | if (context_range_set(ctx, cur->level)) |
| 475 | goto oom; |
| 476 | } |
William Roberts | 1b36ad0 | 2012-07-27 13:52:33 -0700 | [diff] [blame] | 477 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 478 | break; |
| 479 | } |
| 480 | |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 481 | if (kind == SEAPP_DOMAIN && i == nspec) { |
| 482 | /* |
| 483 | * No match. |
| 484 | * Fail to prevent staying in the zygote's context. |
| 485 | */ |
| 486 | selinux_log(SELINUX_ERROR, |
| 487 | "%s: No match for app with uid %d, seinfo %s, name %s\n", |
| 488 | __FUNCTION__, uid, seinfo, pkgname); |
| 489 | |
| 490 | if (security_getenforce() == 1) |
| 491 | goto err; |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | err: |
| 496 | return -1; |
| 497 | oom: |
| 498 | return -2; |
| 499 | } |
| 500 | |
| 501 | int selinux_android_setfilecon2(const char *pkgdir, |
| 502 | const char *pkgname, |
| 503 | const char *seinfo, |
| 504 | uid_t uid) |
| 505 | { |
Nick Kralevich | 9ca4088 | 2013-07-11 16:58:44 -0700 | [diff] [blame] | 506 | char *orig_ctx_str = NULL; |
| 507 | char *ctx_str = NULL; |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 508 | context_t ctx = NULL; |
Nick Kralevich | 9ca4088 | 2013-07-11 16:58:44 -0700 | [diff] [blame] | 509 | int rc = -1; |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 510 | |
| 511 | if (is_selinux_enabled() <= 0) |
| 512 | return 0; |
| 513 | |
| 514 | __selinux_once(once, seapp_context_init); |
| 515 | |
| 516 | rc = getfilecon(pkgdir, &ctx_str); |
| 517 | if (rc < 0) |
| 518 | goto err; |
| 519 | |
| 520 | ctx = context_new(ctx_str); |
| 521 | orig_ctx_str = ctx_str; |
| 522 | if (!ctx) |
| 523 | goto oom; |
| 524 | |
| 525 | rc = seapp_context_lookup(SEAPP_TYPE, uid, 0, seinfo, pkgname, ctx); |
| 526 | if (rc == -1) |
| 527 | goto err; |
| 528 | else if (rc == -2) |
| 529 | goto oom; |
| 530 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 531 | ctx_str = context_str(ctx); |
| 532 | if (!ctx_str) |
| 533 | goto oom; |
| 534 | |
| 535 | rc = security_check_context(ctx_str); |
| 536 | if (rc < 0) |
| 537 | goto err; |
| 538 | |
| 539 | if (strcmp(ctx_str, orig_ctx_str)) { |
| 540 | rc = setfilecon(pkgdir, ctx_str); |
| 541 | if (rc < 0) |
| 542 | goto err; |
| 543 | } |
| 544 | |
| 545 | rc = 0; |
| 546 | out: |
| 547 | freecon(orig_ctx_str); |
| 548 | context_free(ctx); |
| 549 | return rc; |
| 550 | err: |
| 551 | selinux_log(SELINUX_ERROR, "%s: Error setting context for pkgdir %s, uid %d: %s\n", |
| 552 | __FUNCTION__, pkgdir, uid, strerror(errno)); |
| 553 | rc = -1; |
| 554 | goto out; |
| 555 | oom: |
| 556 | selinux_log(SELINUX_ERROR, "%s: Out of memory\n", __FUNCTION__); |
| 557 | rc = -1; |
| 558 | goto out; |
| 559 | } |
| 560 | |
Stephen Smalley | edfaad8 | 2012-07-12 11:30:24 -0400 | [diff] [blame] | 561 | int selinux_android_setfilecon(const char *pkgdir, |
| 562 | const char *pkgname, |
| 563 | uid_t uid) |
| 564 | { |
| 565 | return selinux_android_setfilecon2(pkgdir, pkgname, NULL, uid); |
| 566 | } |
| 567 | |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 568 | int selinux_android_setcontext(uid_t uid, |
| 569 | int isSystemServer, |
| 570 | const char *seinfo, |
Stephen Smalley | ba70ee4 | 2012-07-10 15:49:04 -0400 | [diff] [blame] | 571 | const char *pkgname) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 572 | { |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 573 | char *orig_ctx_str = NULL, *ctx_str; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 574 | context_t ctx = NULL; |
Nick Kralevich | 9ca4088 | 2013-07-11 16:58:44 -0700 | [diff] [blame] | 575 | int rc = -1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 576 | |
| 577 | if (is_selinux_enabled() <= 0) |
| 578 | return 0; |
| 579 | |
| 580 | __selinux_once(once, seapp_context_init); |
| 581 | |
| 582 | rc = getcon(&ctx_str); |
| 583 | if (rc) |
| 584 | goto err; |
| 585 | |
| 586 | ctx = context_new(ctx_str); |
| 587 | orig_ctx_str = ctx_str; |
| 588 | if (!ctx) |
| 589 | goto oom; |
| 590 | |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 591 | rc = seapp_context_lookup(SEAPP_DOMAIN, uid, isSystemServer, seinfo, pkgname, ctx); |
| 592 | if (rc == -1) |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 593 | goto err; |
Stephen Smalley | 895b446 | 2012-09-19 16:27:36 -0400 | [diff] [blame] | 594 | else if (rc == -2) |
| 595 | goto oom; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 596 | |
| 597 | ctx_str = context_str(ctx); |
| 598 | if (!ctx_str) |
| 599 | goto oom; |
| 600 | |
| 601 | rc = security_check_context(ctx_str); |
| 602 | if (rc < 0) |
| 603 | goto err; |
| 604 | |
| 605 | if (strcmp(ctx_str, orig_ctx_str)) { |
| 606 | rc = setcon(ctx_str); |
| 607 | if (rc < 0) |
| 608 | goto err; |
| 609 | } |
| 610 | |
| 611 | rc = 0; |
| 612 | out: |
| 613 | freecon(orig_ctx_str); |
| 614 | context_free(ctx); |
Stephen Smalley | e8b0fd8 | 2012-07-31 09:12:53 -0400 | [diff] [blame] | 615 | avc_netlink_close(); |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 616 | return rc; |
| 617 | err: |
| 618 | if (isSystemServer) |
| 619 | selinux_log(SELINUX_ERROR, |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 620 | "%s: Error setting context for system server: %s\n", |
| 621 | __FUNCTION__, strerror(errno)); |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 622 | else |
| 623 | selinux_log(SELINUX_ERROR, |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 624 | "%s: Error setting context for app with uid %d, seinfo %s: %s\n", |
| 625 | __FUNCTION__, uid, seinfo, strerror(errno)); |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 626 | |
Stephen Smalley | f77e60d | 2012-07-27 15:41:10 -0400 | [diff] [blame] | 627 | rc = -1; |
Stephen Smalley | f074036 | 2012-01-04 12:30:47 -0500 | [diff] [blame] | 628 | goto out; |
| 629 | oom: |
| 630 | selinux_log(SELINUX_ERROR, "%s: Out of memory\n", __FUNCTION__); |
| 631 | rc = -1; |
| 632 | goto out; |
| 633 | } |
| 634 | |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 635 | static struct selabel_handle *sehandle = NULL; |
| 636 | |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 637 | static struct selabel_handle *get_selabel_handle(const struct selinux_opt opts[]) { |
Stephen Smalley | 906742d | 2012-08-23 16:04:59 -0400 | [diff] [blame] | 638 | struct selabel_handle *h; |
Stephen Smalley | 32ebfe8 | 2012-03-20 13:05:37 -0400 | [diff] [blame] | 639 | int i = 0; |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 640 | |
Stephen Smalley | 906742d | 2012-08-23 16:04:59 -0400 | [diff] [blame] | 641 | h = NULL; |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 642 | while ((h == NULL) && opts[i].value) { |
| 643 | h = selabel_open(SELABEL_CTX_FILE, &opts[i], 1); |
Stephen Smalley | 32ebfe8 | 2012-03-20 13:05:37 -0400 | [diff] [blame] | 644 | i++; |
| 645 | } |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 646 | |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 647 | return h; |
| 648 | } |
| 649 | |
| 650 | static struct selabel_handle *file_context_open(void) |
| 651 | { |
| 652 | struct selabel_handle *h; |
| 653 | |
| 654 | h = get_selabel_handle(seopts); |
| 655 | |
Stephen Smalley | 906742d | 2012-08-23 16:04:59 -0400 | [diff] [blame] | 656 | if (!h) |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 657 | selinux_log(SELINUX_ERROR, "%s: Error getting file context handle (%s)\n", |
| 658 | __FUNCTION__, strerror(errno)); |
| 659 | return h; |
| 660 | } |
| 661 | |
| 662 | static struct selabel_handle *file_context_backup_open(void) |
| 663 | { |
| 664 | struct selabel_handle *h; |
| 665 | |
| 666 | h = get_selabel_handle(seopt_backup); |
| 667 | |
| 668 | if (!h) |
| 669 | selinux_log(SELINUX_ERROR, "%s: Error getting backup file context handle (%s)\n", |
| 670 | __FUNCTION__, strerror(errno)); |
Stephen Smalley | 906742d | 2012-08-23 16:04:59 -0400 | [diff] [blame] | 671 | return h; |
| 672 | } |
| 673 | |
| 674 | static void file_context_init(void) |
| 675 | { |
| 676 | sehandle = file_context_open(); |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static pthread_once_t fc_once = PTHREAD_ONCE_INIT; |
| 680 | |
| 681 | int selinux_android_restorecon(const char *pathname) |
| 682 | { |
| 683 | |
Nick Kralevich | 9ca4088 | 2013-07-11 16:58:44 -0700 | [diff] [blame] | 684 | char* oldcontext = NULL; |
| 685 | char* newcontext = NULL; |
| 686 | struct stat sb; |
| 687 | int ret = -1; |
| 688 | |
Kenny Root | 20f62f3 | 2012-10-22 17:09:23 -0700 | [diff] [blame] | 689 | if (is_selinux_enabled() <= 0) |
| 690 | return 0; |
| 691 | |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 692 | __selinux_once(fc_once, file_context_init); |
| 693 | |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 694 | if (!sehandle) |
| 695 | goto bail; |
| 696 | |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 697 | if (lstat(pathname, &sb) < 0) |
| 698 | goto err; |
| 699 | |
Stephen Smalley | 0ca91b3 | 2012-03-19 10:25:53 -0400 | [diff] [blame] | 700 | if (lgetfilecon(pathname, &oldcontext) < 0) |
| 701 | goto err; |
| 702 | |
| 703 | if (selabel_lookup(sehandle, &newcontext, pathname, sb.st_mode) < 0) |
| 704 | goto err; |
| 705 | |
| 706 | if (strcmp(newcontext, "<<none>>") && strcmp(oldcontext, newcontext)) |
| 707 | if (lsetfilecon(pathname, newcontext) < 0) |
| 708 | goto err; |
| 709 | |
| 710 | ret = 0; |
| 711 | out: |
| 712 | if (oldcontext) |
| 713 | freecon(oldcontext); |
| 714 | if (newcontext) |
| 715 | freecon(newcontext); |
| 716 | |
| 717 | return ret; |
| 718 | |
| 719 | err: |
| 720 | selinux_log(SELINUX_ERROR, |
| 721 | "%s: Error restoring context for %s (%s)\n", |
| 722 | __FUNCTION__, pathname, strerror(errno)); |
| 723 | |
| 724 | bail: |
| 725 | ret = -1; |
| 726 | goto out; |
| 727 | } |
rpcraig | 9b10083 | 2012-07-27 06:36:59 -0400 | [diff] [blame] | 728 | |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 729 | struct selabel_handle* selinux_android_file_context_handle(void) |
| 730 | { |
Geremy Condra | 6064643 | 2013-04-10 17:58:48 -0700 | [diff] [blame] | 731 | return file_context_open(); |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 732 | } |
rpcraig | 9b10083 | 2012-07-27 06:36:59 -0400 | [diff] [blame] | 733 | |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 734 | int selinux_android_reload_policy(void) |
| 735 | { |
Stephen Smalley | 4a655ec | 2012-09-18 15:08:32 -0400 | [diff] [blame] | 736 | int fd = -1, rc; |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 737 | struct stat sb; |
| 738 | void *map = NULL; |
| 739 | int i = 0; |
| 740 | |
Stephen Smalley | 4a655ec | 2012-09-18 15:08:32 -0400 | [diff] [blame] | 741 | while (fd < 0 && sepolicy_file[i]) { |
Nick Kralevich | 9c30ac6 | 2013-05-06 12:03:07 -0700 | [diff] [blame] | 742 | fd = open(sepolicy_file[i], O_RDONLY | O_NOFOLLOW); |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 743 | i++; |
| 744 | } |
| 745 | if (fd < 0) { |
| 746 | selinux_log(SELINUX_ERROR, "SELinux: Could not open sepolicy: %s\n", |
| 747 | strerror(errno)); |
| 748 | return -1; |
| 749 | } |
| 750 | if (fstat(fd, &sb) < 0) { |
| 751 | selinux_log(SELINUX_ERROR, "SELinux: Could not stat %s: %s\n", |
Stephen Smalley | cb92504 | 2013-07-26 10:45:44 -0400 | [diff] [blame] | 752 | sepolicy_file[i-1], strerror(errno)); |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 753 | close(fd); |
| 754 | return -1; |
| 755 | } |
| 756 | map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 757 | if (map == MAP_FAILED) { |
| 758 | selinux_log(SELINUX_ERROR, "SELinux: Could not map %s: %s\n", |
Stephen Smalley | cb92504 | 2013-07-26 10:45:44 -0400 | [diff] [blame] | 759 | sepolicy_file[i-1], strerror(errno)); |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 760 | close(fd); |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | rc = security_load_policy(map, sb.st_size); |
| 765 | if (rc < 0) { |
| 766 | selinux_log(SELINUX_ERROR, "SELinux: Could not load policy: %s\n", |
| 767 | strerror(errno)); |
| 768 | munmap(map, sb.st_size); |
| 769 | close(fd); |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | munmap(map, sb.st_size); |
| 774 | close(fd); |
Stephen Smalley | cb92504 | 2013-07-26 10:45:44 -0400 | [diff] [blame] | 775 | selinux_log(SELINUX_INFO, "SELinux: Loaded policy from %s\n", sepolicy_file[i-1]); |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | int selinux_android_load_policy(void) |
| 781 | { |
Nick Kralevich | 833cba6 | 2013-11-19 11:24:33 -0800 | [diff] [blame] | 782 | const char *mnt = SELINUXMNT; |
Stephen Smalley | d10c343 | 2012-11-05 11:49:35 -0500 | [diff] [blame] | 783 | int rc; |
| 784 | rc = mount(SELINUXFS, mnt, SELINUXFS, 0, NULL); |
| 785 | if (rc < 0) { |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 786 | if (errno == ENODEV) { |
| 787 | /* SELinux not enabled in kernel */ |
| 788 | return -1; |
| 789 | } |
Stephen Smalley | d10c343 | 2012-11-05 11:49:35 -0500 | [diff] [blame] | 790 | if (errno == ENOENT) { |
| 791 | /* Fall back to legacy mountpoint. */ |
| 792 | mnt = OLDSELINUXMNT; |
Alice Chu | d2302ca | 2013-01-04 16:02:46 -0800 | [diff] [blame] | 793 | rc = mkdir(mnt, 0755); |
| 794 | if (rc == -1 && errno != EEXIST) { |
| 795 | selinux_log(SELINUX_ERROR,"SELinux: Could not mkdir: %s\n", |
| 796 | strerror(errno)); |
| 797 | return -1; |
| 798 | } |
Stephen Smalley | d10c343 | 2012-11-05 11:49:35 -0500 | [diff] [blame] | 799 | rc = mount(SELINUXFS, mnt, SELINUXFS, 0, NULL); |
| 800 | } |
| 801 | } |
| 802 | if (rc < 0) { |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 803 | selinux_log(SELINUX_ERROR,"SELinux: Could not mount selinuxfs: %s\n", |
| 804 | strerror(errno)); |
| 805 | return -1; |
| 806 | } |
Stephen Smalley | d10c343 | 2012-11-05 11:49:35 -0500 | [diff] [blame] | 807 | set_selinuxmnt(mnt); |
rpcraig | f1724a3 | 2012-08-01 15:35:37 -0400 | [diff] [blame] | 808 | |
| 809 | return selinux_android_reload_policy(); |
rpcraig | 9b10083 | 2012-07-27 06:36:59 -0400 | [diff] [blame] | 810 | } |