blob: 9e1f5bb229210c567441053fe2f4d888aaf67d9b [file] [log] [blame]
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -08001/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17#include <errno.h>
18#include <fcntl.h>
Mark Salyzyn68ffc742015-04-01 07:42:46 -070019#include <string.h>
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020020#include <sys/mman.h>
Mark Salyzyn68ffc742015-04-01 07:42:46 -070021#include <sys/stat.h>
22#include <unistd.h>
23
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080024#include <private/android_filesystem_config.h>
25#include "package.h"
26
27/*
28 * WARNING WARNING WARNING WARNING
29 *
30 * The following code runs as root on production devices, before
31 * the run-as command has dropped the uid/gid. Hence be very
32 * conservative and keep in mind the following:
33 *
34 * - Performance does not matter here, clarity and safety of the code
35 * does however. Documentation is a must.
36 *
37 * - Avoid calling C library functions with complex implementations
38 * like malloc() and printf(). You want to depend on simple system
39 * calls instead, which behaviour is not going to be altered in
40 * unpredictible ways by environment variables or system properties.
41 *
42 * - Do not trust user input and/or the filesystem whenever possible.
43 *
44 */
45
46/* The file containing the list of installed packages on the system */
47#define PACKAGES_LIST_FILE "/data/system/packages.list"
48
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080049/* Copy 'srclen' string bytes from 'src' into buffer 'dst' of size 'dstlen'
50 * This function always zero-terminate the destination buffer unless
51 * 'dstlen' is 0, even in case of overflow.
Robert Craigfced3de2013-03-26 08:09:09 -040052 * Returns a pointer into the src string, leaving off where the copy
53 * has stopped. The copy will stop when dstlen, srclen or a null
54 * character on src has been reached.
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080055 */
Robert Craigfced3de2013-03-26 08:09:09 -040056static const char*
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080057string_copy(char* dst, size_t dstlen, const char* src, size_t srclen)
58{
59 const char* srcend = src + srclen;
60 const char* dstend = dst + dstlen;
61
62 if (dstlen == 0)
Robert Craigfced3de2013-03-26 08:09:09 -040063 return src;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080064
65 dstend--; /* make room for terminating zero */
66
67 while (dst < dstend && src < srcend && *src != '\0')
68 *dst++ = *src++;
69
70 *dst = '\0'; /* zero-terminate result */
Robert Craigfced3de2013-03-26 08:09:09 -040071 return src;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080072}
73
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020074/* Open 'filename' and map it into our address-space.
75 * Returns buffer address, or NULL on error
76 * On exit, *filesize will be set to the file's size, or 0 on error
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080077 */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020078static void*
79map_file(const char* filename, size_t* filesize)
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080080{
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020081 int fd, ret, old_errno;
82 struct stat st;
83 size_t length = 0;
84 void* address = NULL;
Nick Kralevich080427e2013-02-15 14:39:15 -080085 gid_t oldegid;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080086
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020087 *filesize = 0;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080088
Nick Kralevich080427e2013-02-15 14:39:15 -080089 /*
90 * Temporarily switch effective GID to allow us to read
91 * the packages file
92 */
93
94 oldegid = getegid();
Alex Klyubin18860c52013-08-20 15:16:31 -070095 if (setegid(AID_PACKAGE_INFO) < 0) {
Nick Kralevich080427e2013-02-15 14:39:15 -080096 return NULL;
97 }
98
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080099 /* open the file for reading */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200100 fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
Nick Kralevich080427e2013-02-15 14:39:15 -0800101 if (fd < 0) {
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200102 return NULL;
Nick Kralevich080427e2013-02-15 14:39:15 -0800103 }
104
105 /* restore back to our old egid */
106 if (setegid(oldegid) < 0) {
107 goto EXIT;
108 }
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800109
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200110 /* get its size */
111 ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
112 if (ret < 0)
113 goto EXIT;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800114
Nick Kralevich4ae77162012-02-09 11:22:33 -0800115 /* Ensure that the file is owned by the system user */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700116 if ((st.st_uid != AID_SYSTEM) || (st.st_gid != AID_PACKAGE_INFO)) {
Nick Kralevich4ae77162012-02-09 11:22:33 -0800117 goto EXIT;
118 }
119
120 /* Ensure that the file has sane permissions */
121 if ((st.st_mode & S_IWOTH) != 0) {
122 goto EXIT;
123 }
124
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200125 /* Ensure that the size is not ridiculously large */
126 length = (size_t)st.st_size;
127 if ((off_t)length != st.st_size) {
128 errno = ENOMEM;
129 goto EXIT;
130 }
131
132 /* Memory-map the file now */
Mark Salyzyn2e6e2712014-05-08 14:09:01 -0700133 do {
134 address = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
135 } while (address == MAP_FAILED && errno == EINTR);
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200136 if (address == MAP_FAILED) {
137 address = NULL;
138 goto EXIT;
139 }
140
141 /* We're good, return size */
142 *filesize = length;
143
144EXIT:
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800145 /* close the file, preserve old errno for better diagnostics */
146 old_errno = errno;
147 close(fd);
148 errno = old_errno;
149
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200150 return address;
151}
152
153/* unmap the file, but preserve errno */
154static void
155unmap_file(void* address, size_t size)
156{
157 int old_errno = errno;
158 TEMP_FAILURE_RETRY(munmap(address, size));
159 errno = old_errno;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800160}
161
162/* Check that a given directory:
163 * - exists
164 * - is owned by a given uid/gid
165 * - is a real directory, not a symlink
166 * - isn't readable or writable by others
167 *
168 * Return 0 on success, or -1 on error.
169 * errno is set to EINVAL in case of failed check.
170 */
171static int
172check_directory_ownership(const char* path, uid_t uid)
173{
174 int ret;
175 struct stat st;
176
177 do {
178 ret = lstat(path, &st);
179 } while (ret < 0 && errno == EINTR);
180
181 if (ret < 0)
182 return -1;
183
184 /* must be a real directory, not a symlink */
185 if (!S_ISDIR(st.st_mode))
186 goto BAD;
187
188 /* must be owned by specific uid/gid */
189 if (st.st_uid != uid || st.st_gid != uid)
190 goto BAD;
191
192 /* must not be readable or writable by others */
193 if ((st.st_mode & (S_IROTH|S_IWOTH)) != 0)
194 goto BAD;
195
196 /* everything ok */
197 return 0;
198
199BAD:
200 errno = EINVAL;
201 return -1;
202}
203
204/* This function is used to check the data directory path for safety.
205 * We check that every sub-directory is owned by the 'system' user
206 * and exists and is not a symlink. We also check that the full directory
207 * path is properly owned by the user ID.
208 *
209 * Return 0 on success, -1 on error.
210 */
211int
212check_data_path(const char* dataPath, uid_t uid)
213{
214 int nn;
215
216 /* the path should be absolute */
217 if (dataPath[0] != '/') {
218 errno = EINVAL;
219 return -1;
220 }
221
222 /* look for all sub-paths, we do that by finding
223 * directory separators in the input path and
224 * checking each sub-path independently
225 */
226 for (nn = 1; dataPath[nn] != '\0'; nn++)
227 {
228 char subpath[PATH_MAX];
229
230 /* skip non-separator characters */
231 if (dataPath[nn] != '/')
232 continue;
233
234 /* handle trailing separator case */
235 if (dataPath[nn+1] == '\0') {
236 break;
237 }
238
239 /* found a separator, check that dataPath is not too long. */
240 if (nn >= (int)(sizeof subpath)) {
241 errno = EINVAL;
242 return -1;
243 }
244
245 /* reject any '..' subpath */
246 if (nn >= 3 &&
247 dataPath[nn-3] == '/' &&
248 dataPath[nn-2] == '.' &&
249 dataPath[nn-1] == '.') {
250 errno = EINVAL;
251 return -1;
252 }
253
254 /* copy to 'subpath', then check ownership */
255 memcpy(subpath, dataPath, nn);
256 subpath[nn] = '\0';
257
258 if (check_directory_ownership(subpath, AID_SYSTEM) < 0)
259 return -1;
260 }
261
262 /* All sub-paths were checked, now verify that the full data
263 * directory is owned by the application uid
264 */
265 if (check_directory_ownership(dataPath, uid) < 0)
266 return -1;
267
268 /* all clear */
269 return 0;
270}
271
272/* Return TRUE iff a character is a space or tab */
273static inline int
274is_space(char c)
275{
276 return (c == ' ' || c == '\t');
277}
278
279/* Skip any space or tab character from 'p' until 'end' is reached.
280 * Return new position.
281 */
282static const char*
283skip_spaces(const char* p, const char* end)
284{
285 while (p < end && is_space(*p))
286 p++;
287
288 return p;
289}
290
291/* Skip any non-space and non-tab character from 'p' until 'end'.
292 * Return new position.
293 */
294static const char*
295skip_non_spaces(const char* p, const char* end)
296{
297 while (p < end && !is_space(*p))
298 p++;
299
300 return p;
301}
302
303/* Find the first occurence of 'ch' between 'p' and 'end'
304 * Return its position, or 'end' if none is found.
305 */
306static const char*
307find_first(const char* p, const char* end, char ch)
308{
309 while (p < end && *p != ch)
310 p++;
311
312 return p;
313}
314
315/* Check that the non-space string starting at 'p' and eventually
316 * ending at 'end' equals 'name'. Return new position (after name)
317 * on success, or NULL on failure.
318 *
319 * This function fails is 'name' is NULL, empty or contains any space.
320 */
321static const char*
322compare_name(const char* p, const char* end, const char* name)
323{
324 /* 'name' must not be NULL or empty */
325 if (name == NULL || name[0] == '\0' || p == end)
326 return NULL;
327
328 /* compare characters to those in 'name', excluding spaces */
329 while (*name) {
330 /* note, we don't check for *p == '\0' since
331 * it will be caught in the next conditional.
332 */
333 if (p >= end || is_space(*p))
334 goto BAD;
335
336 if (*p != *name)
337 goto BAD;
338
339 p++;
340 name++;
341 }
342
343 /* must be followed by end of line or space */
344 if (p < end && !is_space(*p))
345 goto BAD;
346
347 return p;
348
349BAD:
350 return NULL;
351}
352
353/* Parse one or more whitespace characters starting from '*pp'
354 * until 'end' is reached. Updates '*pp' on exit.
355 *
356 * Return 0 on success, -1 on failure.
357 */
358static int
359parse_spaces(const char** pp, const char* end)
360{
361 const char* p = *pp;
362
363 if (p >= end || !is_space(*p)) {
364 errno = EINVAL;
365 return -1;
366 }
367 p = skip_spaces(p, end);
368 *pp = p;
369 return 0;
370}
371
372/* Parse a positive decimal number starting from '*pp' until 'end'
373 * is reached. Adjust '*pp' on exit. Return decimal value or -1
374 * in case of error.
375 *
376 * If the value is larger than INT_MAX, -1 will be returned,
377 * and errno set to EOVERFLOW.
378 *
379 * If '*pp' does not start with a decimal digit, -1 is returned
380 * and errno set to EINVAL.
381 */
382static int
383parse_positive_decimal(const char** pp, const char* end)
384{
385 const char* p = *pp;
386 int value = 0;
387 int overflow = 0;
388
389 if (p >= end || *p < '0' || *p > '9') {
390 errno = EINVAL;
391 return -1;
392 }
393
394 while (p < end) {
395 int ch = *p;
396 unsigned d = (unsigned)(ch - '0');
397 int val2;
398
399 if (d >= 10U) /* d is unsigned, no lower bound check */
400 break;
401
402 val2 = value*10 + (int)d;
403 if (val2 < value)
404 overflow = 1;
405 value = val2;
406 p++;
407 }
408 *pp = p;
409
410 if (overflow) {
411 errno = EOVERFLOW;
412 value = -1;
413 }
414 return value;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800415}
416
417/* Read the system's package database and extract information about
418 * 'pkgname'. Return 0 in case of success, or -1 in case of error.
419 *
420 * If the package is unknown, return -1 and set errno to ENOENT
421 * If the package database is corrupted, return -1 and set errno to EINVAL
422 */
423int
424get_package_info(const char* pkgName, PackageInfo *info)
425{
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200426 char* buffer;
427 size_t buffer_len;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800428 const char* p;
429 const char* buffer_end;
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200430 int result = -1;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800431
432 info->uid = 0;
433 info->isDebuggable = 0;
434 info->dataDir[0] = '\0';
Robert Craigfced3de2013-03-26 08:09:09 -0400435 info->seinfo[0] = '\0';
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800436
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200437 buffer = map_file(PACKAGES_LIST_FILE, &buffer_len);
438 if (buffer == NULL)
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800439 return -1;
440
441 p = buffer;
442 buffer_end = buffer + buffer_len;
443
444 /* expect the following format on each line of the control file:
445 *
Robert Craigfced3de2013-03-26 08:09:09 -0400446 * <pkgName> <uid> <debugFlag> <dataDir> <seinfo>
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800447 *
448 * where:
449 * <pkgName> is the package's name
450 * <uid> is the application-specific user Id (decimal)
451 * <debugFlag> is 1 if the package is debuggable, or 0 otherwise
452 * <dataDir> is the path to the package's data directory (e.g. /data/data/com.example.foo)
Robert Craigfced3de2013-03-26 08:09:09 -0400453 * <seinfo> is the seinfo label associated with the package
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800454 *
455 * The file is generated in com.android.server.PackageManagerService.Settings.writeLP()
456 */
457
458 while (p < buffer_end) {
459 /* find end of current line and start of next one */
460 const char* end = find_first(p, buffer_end, '\n');
461 const char* next = (end < buffer_end) ? end + 1 : buffer_end;
462 const char* q;
463 int uid, debugFlag;
464
465 /* first field is the package name */
466 p = compare_name(p, end, pkgName);
467 if (p == NULL)
468 goto NEXT_LINE;
469
470 /* skip spaces */
471 if (parse_spaces(&p, end) < 0)
472 goto BAD_FORMAT;
473
474 /* second field is the pid */
475 uid = parse_positive_decimal(&p, end);
476 if (uid < 0)
477 return -1;
478
479 info->uid = (uid_t) uid;
480
481 /* skip spaces */
482 if (parse_spaces(&p, end) < 0)
483 goto BAD_FORMAT;
484
485 /* third field is debug flag (0 or 1) */
486 debugFlag = parse_positive_decimal(&p, end);
487 switch (debugFlag) {
488 case 0:
489 info->isDebuggable = 0;
490 break;
491 case 1:
492 info->isDebuggable = 1;
493 break;
494 default:
495 goto BAD_FORMAT;
496 }
497
498 /* skip spaces */
499 if (parse_spaces(&p, end) < 0)
500 goto BAD_FORMAT;
501
502 /* fourth field is data directory path and must not contain
503 * spaces.
504 */
505 q = skip_non_spaces(p, end);
506 if (q == p)
507 goto BAD_FORMAT;
508
Robert Craigfced3de2013-03-26 08:09:09 -0400509 p = string_copy(info->dataDir, sizeof info->dataDir, p, q - p);
510
511 /* skip spaces */
512 if (parse_spaces(&p, end) < 0)
513 goto BAD_FORMAT;
514
515 /* fifth field is the seinfo string */
516 q = skip_non_spaces(p, end);
517 if (q == p)
518 goto BAD_FORMAT;
519
520 string_copy(info->seinfo, sizeof info->seinfo, p, q - p);
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800521
522 /* Ignore the rest */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200523 result = 0;
524 goto EXIT;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800525
526 NEXT_LINE:
527 p = next;
528 }
529
530 /* the package is unknown */
531 errno = ENOENT;
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200532 result = -1;
533 goto EXIT;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800534
535BAD_FORMAT:
536 errno = EINVAL;
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200537 result = -1;
538
539EXIT:
540 unmap_file(buffer, buffer_len);
541 return result;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800542}