blob: 683dae6ec9a7b9c92a299ee49314e9b0834d0e9a [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>
19#include <unistd.h>
20#include <sys/stat.h>
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020021#include <sys/mman.h>
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080022#include <private/android_filesystem_config.h>
23#include "package.h"
24
25/*
26 * WARNING WARNING WARNING WARNING
27 *
28 * The following code runs as root on production devices, before
29 * the run-as command has dropped the uid/gid. Hence be very
30 * conservative and keep in mind the following:
31 *
32 * - Performance does not matter here, clarity and safety of the code
33 * does however. Documentation is a must.
34 *
35 * - Avoid calling C library functions with complex implementations
36 * like malloc() and printf(). You want to depend on simple system
37 * calls instead, which behaviour is not going to be altered in
38 * unpredictible ways by environment variables or system properties.
39 *
40 * - Do not trust user input and/or the filesystem whenever possible.
41 *
42 */
43
44/* The file containing the list of installed packages on the system */
45#define PACKAGES_LIST_FILE "/data/system/packages.list"
46
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080047/* Copy 'srclen' string bytes from 'src' into buffer 'dst' of size 'dstlen'
48 * This function always zero-terminate the destination buffer unless
49 * 'dstlen' is 0, even in case of overflow.
50 */
51static void
52string_copy(char* dst, size_t dstlen, const char* src, size_t srclen)
53{
54 const char* srcend = src + srclen;
55 const char* dstend = dst + dstlen;
56
57 if (dstlen == 0)
58 return;
59
60 dstend--; /* make room for terminating zero */
61
62 while (dst < dstend && src < srcend && *src != '\0')
63 *dst++ = *src++;
64
65 *dst = '\0'; /* zero-terminate result */
66}
67
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020068/* Open 'filename' and map it into our address-space.
69 * Returns buffer address, or NULL on error
70 * On exit, *filesize will be set to the file's size, or 0 on error
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080071 */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020072static void*
73map_file(const char* filename, size_t* filesize)
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080074{
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020075 int fd, ret, old_errno;
76 struct stat st;
77 size_t length = 0;
78 void* address = NULL;
Nick Kralevich080427e2013-02-15 14:39:15 -080079 gid_t oldegid;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080080
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020081 *filesize = 0;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080082
Nick Kralevich080427e2013-02-15 14:39:15 -080083 /*
84 * Temporarily switch effective GID to allow us to read
85 * the packages file
86 */
87
88 oldegid = getegid();
89 if (setegid(AID_SYSTEM) < 0) {
90 return NULL;
91 }
92
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080093 /* open the file for reading */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020094 fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
Nick Kralevich080427e2013-02-15 14:39:15 -080095 if (fd < 0) {
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020096 return NULL;
Nick Kralevich080427e2013-02-15 14:39:15 -080097 }
98
99 /* restore back to our old egid */
100 if (setegid(oldegid) < 0) {
101 goto EXIT;
102 }
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800103
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200104 /* get its size */
105 ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
106 if (ret < 0)
107 goto EXIT;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800108
Nick Kralevich4ae77162012-02-09 11:22:33 -0800109 /* Ensure that the file is owned by the system user */
110 if ((st.st_uid != AID_SYSTEM) || (st.st_gid != AID_SYSTEM)) {
111 goto EXIT;
112 }
113
114 /* Ensure that the file has sane permissions */
115 if ((st.st_mode & S_IWOTH) != 0) {
116 goto EXIT;
117 }
118
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200119 /* Ensure that the size is not ridiculously large */
120 length = (size_t)st.st_size;
121 if ((off_t)length != st.st_size) {
122 errno = ENOMEM;
123 goto EXIT;
124 }
125
126 /* Memory-map the file now */
127 address = TEMP_FAILURE_RETRY(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0));
128 if (address == MAP_FAILED) {
129 address = NULL;
130 goto EXIT;
131 }
132
133 /* We're good, return size */
134 *filesize = length;
135
136EXIT:
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800137 /* close the file, preserve old errno for better diagnostics */
138 old_errno = errno;
139 close(fd);
140 errno = old_errno;
141
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200142 return address;
143}
144
145/* unmap the file, but preserve errno */
146static void
147unmap_file(void* address, size_t size)
148{
149 int old_errno = errno;
150 TEMP_FAILURE_RETRY(munmap(address, size));
151 errno = old_errno;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800152}
153
154/* Check that a given directory:
155 * - exists
156 * - is owned by a given uid/gid
157 * - is a real directory, not a symlink
158 * - isn't readable or writable by others
159 *
160 * Return 0 on success, or -1 on error.
161 * errno is set to EINVAL in case of failed check.
162 */
163static int
164check_directory_ownership(const char* path, uid_t uid)
165{
166 int ret;
167 struct stat st;
168
169 do {
170 ret = lstat(path, &st);
171 } while (ret < 0 && errno == EINTR);
172
173 if (ret < 0)
174 return -1;
175
176 /* must be a real directory, not a symlink */
177 if (!S_ISDIR(st.st_mode))
178 goto BAD;
179
180 /* must be owned by specific uid/gid */
181 if (st.st_uid != uid || st.st_gid != uid)
182 goto BAD;
183
184 /* must not be readable or writable by others */
185 if ((st.st_mode & (S_IROTH|S_IWOTH)) != 0)
186 goto BAD;
187
188 /* everything ok */
189 return 0;
190
191BAD:
192 errno = EINVAL;
193 return -1;
194}
195
196/* This function is used to check the data directory path for safety.
197 * We check that every sub-directory is owned by the 'system' user
198 * and exists and is not a symlink. We also check that the full directory
199 * path is properly owned by the user ID.
200 *
201 * Return 0 on success, -1 on error.
202 */
203int
204check_data_path(const char* dataPath, uid_t uid)
205{
206 int nn;
207
208 /* the path should be absolute */
209 if (dataPath[0] != '/') {
210 errno = EINVAL;
211 return -1;
212 }
213
214 /* look for all sub-paths, we do that by finding
215 * directory separators in the input path and
216 * checking each sub-path independently
217 */
218 for (nn = 1; dataPath[nn] != '\0'; nn++)
219 {
220 char subpath[PATH_MAX];
221
222 /* skip non-separator characters */
223 if (dataPath[nn] != '/')
224 continue;
225
226 /* handle trailing separator case */
227 if (dataPath[nn+1] == '\0') {
228 break;
229 }
230
231 /* found a separator, check that dataPath is not too long. */
232 if (nn >= (int)(sizeof subpath)) {
233 errno = EINVAL;
234 return -1;
235 }
236
237 /* reject any '..' subpath */
238 if (nn >= 3 &&
239 dataPath[nn-3] == '/' &&
240 dataPath[nn-2] == '.' &&
241 dataPath[nn-1] == '.') {
242 errno = EINVAL;
243 return -1;
244 }
245
246 /* copy to 'subpath', then check ownership */
247 memcpy(subpath, dataPath, nn);
248 subpath[nn] = '\0';
249
250 if (check_directory_ownership(subpath, AID_SYSTEM) < 0)
251 return -1;
252 }
253
254 /* All sub-paths were checked, now verify that the full data
255 * directory is owned by the application uid
256 */
257 if (check_directory_ownership(dataPath, uid) < 0)
258 return -1;
259
260 /* all clear */
261 return 0;
262}
263
264/* Return TRUE iff a character is a space or tab */
265static inline int
266is_space(char c)
267{
268 return (c == ' ' || c == '\t');
269}
270
271/* Skip any space or tab character from 'p' until 'end' is reached.
272 * Return new position.
273 */
274static const char*
275skip_spaces(const char* p, const char* end)
276{
277 while (p < end && is_space(*p))
278 p++;
279
280 return p;
281}
282
283/* Skip any non-space and non-tab character from 'p' until 'end'.
284 * Return new position.
285 */
286static const char*
287skip_non_spaces(const char* p, const char* end)
288{
289 while (p < end && !is_space(*p))
290 p++;
291
292 return p;
293}
294
295/* Find the first occurence of 'ch' between 'p' and 'end'
296 * Return its position, or 'end' if none is found.
297 */
298static const char*
299find_first(const char* p, const char* end, char ch)
300{
301 while (p < end && *p != ch)
302 p++;
303
304 return p;
305}
306
307/* Check that the non-space string starting at 'p' and eventually
308 * ending at 'end' equals 'name'. Return new position (after name)
309 * on success, or NULL on failure.
310 *
311 * This function fails is 'name' is NULL, empty or contains any space.
312 */
313static const char*
314compare_name(const char* p, const char* end, const char* name)
315{
316 /* 'name' must not be NULL or empty */
317 if (name == NULL || name[0] == '\0' || p == end)
318 return NULL;
319
320 /* compare characters to those in 'name', excluding spaces */
321 while (*name) {
322 /* note, we don't check for *p == '\0' since
323 * it will be caught in the next conditional.
324 */
325 if (p >= end || is_space(*p))
326 goto BAD;
327
328 if (*p != *name)
329 goto BAD;
330
331 p++;
332 name++;
333 }
334
335 /* must be followed by end of line or space */
336 if (p < end && !is_space(*p))
337 goto BAD;
338
339 return p;
340
341BAD:
342 return NULL;
343}
344
345/* Parse one or more whitespace characters starting from '*pp'
346 * until 'end' is reached. Updates '*pp' on exit.
347 *
348 * Return 0 on success, -1 on failure.
349 */
350static int
351parse_spaces(const char** pp, const char* end)
352{
353 const char* p = *pp;
354
355 if (p >= end || !is_space(*p)) {
356 errno = EINVAL;
357 return -1;
358 }
359 p = skip_spaces(p, end);
360 *pp = p;
361 return 0;
362}
363
364/* Parse a positive decimal number starting from '*pp' until 'end'
365 * is reached. Adjust '*pp' on exit. Return decimal value or -1
366 * in case of error.
367 *
368 * If the value is larger than INT_MAX, -1 will be returned,
369 * and errno set to EOVERFLOW.
370 *
371 * If '*pp' does not start with a decimal digit, -1 is returned
372 * and errno set to EINVAL.
373 */
374static int
375parse_positive_decimal(const char** pp, const char* end)
376{
377 const char* p = *pp;
378 int value = 0;
379 int overflow = 0;
380
381 if (p >= end || *p < '0' || *p > '9') {
382 errno = EINVAL;
383 return -1;
384 }
385
386 while (p < end) {
387 int ch = *p;
388 unsigned d = (unsigned)(ch - '0');
389 int val2;
390
391 if (d >= 10U) /* d is unsigned, no lower bound check */
392 break;
393
394 val2 = value*10 + (int)d;
395 if (val2 < value)
396 overflow = 1;
397 value = val2;
398 p++;
399 }
400 *pp = p;
401
402 if (overflow) {
403 errno = EOVERFLOW;
404 value = -1;
405 }
406 return value;
407
408BAD:
409 *pp = p;
410 return -1;
411}
412
413/* Read the system's package database and extract information about
414 * 'pkgname'. Return 0 in case of success, or -1 in case of error.
415 *
416 * If the package is unknown, return -1 and set errno to ENOENT
417 * If the package database is corrupted, return -1 and set errno to EINVAL
418 */
419int
420get_package_info(const char* pkgName, PackageInfo *info)
421{
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200422 char* buffer;
423 size_t buffer_len;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800424 const char* p;
425 const char* buffer_end;
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200426 int result = -1;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800427
428 info->uid = 0;
429 info->isDebuggable = 0;
430 info->dataDir[0] = '\0';
431
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200432 buffer = map_file(PACKAGES_LIST_FILE, &buffer_len);
433 if (buffer == NULL)
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800434 return -1;
435
436 p = buffer;
437 buffer_end = buffer + buffer_len;
438
439 /* expect the following format on each line of the control file:
440 *
441 * <pkgName> <uid> <debugFlag> <dataDir>
442 *
443 * where:
444 * <pkgName> is the package's name
445 * <uid> is the application-specific user Id (decimal)
446 * <debugFlag> is 1 if the package is debuggable, or 0 otherwise
447 * <dataDir> is the path to the package's data directory (e.g. /data/data/com.example.foo)
448 *
449 * The file is generated in com.android.server.PackageManagerService.Settings.writeLP()
450 */
451
452 while (p < buffer_end) {
453 /* find end of current line and start of next one */
454 const char* end = find_first(p, buffer_end, '\n');
455 const char* next = (end < buffer_end) ? end + 1 : buffer_end;
456 const char* q;
457 int uid, debugFlag;
458
459 /* first field is the package name */
460 p = compare_name(p, end, pkgName);
461 if (p == NULL)
462 goto NEXT_LINE;
463
464 /* skip spaces */
465 if (parse_spaces(&p, end) < 0)
466 goto BAD_FORMAT;
467
468 /* second field is the pid */
469 uid = parse_positive_decimal(&p, end);
470 if (uid < 0)
471 return -1;
472
473 info->uid = (uid_t) uid;
474
475 /* skip spaces */
476 if (parse_spaces(&p, end) < 0)
477 goto BAD_FORMAT;
478
479 /* third field is debug flag (0 or 1) */
480 debugFlag = parse_positive_decimal(&p, end);
481 switch (debugFlag) {
482 case 0:
483 info->isDebuggable = 0;
484 break;
485 case 1:
486 info->isDebuggable = 1;
487 break;
488 default:
489 goto BAD_FORMAT;
490 }
491
492 /* skip spaces */
493 if (parse_spaces(&p, end) < 0)
494 goto BAD_FORMAT;
495
496 /* fourth field is data directory path and must not contain
497 * spaces.
498 */
499 q = skip_non_spaces(p, end);
500 if (q == p)
501 goto BAD_FORMAT;
502
503 string_copy(info->dataDir, sizeof info->dataDir, p, q - p);
504
505 /* Ignore the rest */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200506 result = 0;
507 goto EXIT;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800508
509 NEXT_LINE:
510 p = next;
511 }
512
513 /* the package is unknown */
514 errno = ENOENT;
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200515 result = -1;
516 goto EXIT;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800517
518BAD_FORMAT:
519 errno = EINVAL;
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200520 result = -1;
521
522EXIT:
523 unmap_file(buffer, buffer_len);
524 return result;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800525}