blob: dce132e6aa59ab9568a143569ff0125ce5694029 [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.
Robert Craigfced3de2013-03-26 08:09:09 -040050 * Returns a pointer into the src string, leaving off where the copy
51 * has stopped. The copy will stop when dstlen, srclen or a null
52 * character on src has been reached.
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080053 */
Robert Craigfced3de2013-03-26 08:09:09 -040054static const char*
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080055string_copy(char* dst, size_t dstlen, const char* src, size_t srclen)
56{
57 const char* srcend = src + srclen;
58 const char* dstend = dst + dstlen;
59
60 if (dstlen == 0)
Robert Craigfced3de2013-03-26 08:09:09 -040061 return src;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080062
63 dstend--; /* make room for terminating zero */
64
65 while (dst < dstend && src < srcend && *src != '\0')
66 *dst++ = *src++;
67
68 *dst = '\0'; /* zero-terminate result */
Robert Craigfced3de2013-03-26 08:09:09 -040069 return src;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080070}
71
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020072/* Open 'filename' and map it into our address-space.
73 * Returns buffer address, or NULL on error
74 * On exit, *filesize will be set to the file's size, or 0 on error
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080075 */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020076static void*
77map_file(const char* filename, size_t* filesize)
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080078{
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020079 int fd, ret, old_errno;
80 struct stat st;
81 size_t length = 0;
82 void* address = NULL;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080083
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020084 *filesize = 0;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080085
86 /* open the file for reading */
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020087 fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080088 if (fd < 0)
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020089 return NULL;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080090
David 'Digit' Turner5792ce72011-08-27 18:48:45 +020091 /* get its size */
92 ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
93 if (ret < 0)
94 goto EXIT;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -080095
Nick Kralevich4ae77162012-02-09 11:22:33 -080096 /* Ensure that the file is owned by the system user */
97 if ((st.st_uid != AID_SYSTEM) || (st.st_gid != AID_SYSTEM)) {
98 goto EXIT;
99 }
100
101 /* Ensure that the file has sane permissions */
102 if ((st.st_mode & S_IWOTH) != 0) {
103 goto EXIT;
104 }
105
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200106 /* Ensure that the size is not ridiculously large */
107 length = (size_t)st.st_size;
108 if ((off_t)length != st.st_size) {
109 errno = ENOMEM;
110 goto EXIT;
111 }
112
113 /* Memory-map the file now */
114 address = TEMP_FAILURE_RETRY(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0));
115 if (address == MAP_FAILED) {
116 address = NULL;
117 goto EXIT;
118 }
119
120 /* We're good, return size */
121 *filesize = length;
122
123EXIT:
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800124 /* close the file, preserve old errno for better diagnostics */
125 old_errno = errno;
126 close(fd);
127 errno = old_errno;
128
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200129 return address;
130}
131
132/* unmap the file, but preserve errno */
133static void
134unmap_file(void* address, size_t size)
135{
136 int old_errno = errno;
137 TEMP_FAILURE_RETRY(munmap(address, size));
138 errno = old_errno;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800139}
140
141/* Check that a given directory:
142 * - exists
143 * - is owned by a given uid/gid
144 * - is a real directory, not a symlink
145 * - isn't readable or writable by others
146 *
147 * Return 0 on success, or -1 on error.
148 * errno is set to EINVAL in case of failed check.
149 */
150static int
151check_directory_ownership(const char* path, uid_t uid)
152{
153 int ret;
154 struct stat st;
155
156 do {
157 ret = lstat(path, &st);
158 } while (ret < 0 && errno == EINTR);
159
160 if (ret < 0)
161 return -1;
162
163 /* must be a real directory, not a symlink */
164 if (!S_ISDIR(st.st_mode))
165 goto BAD;
166
167 /* must be owned by specific uid/gid */
168 if (st.st_uid != uid || st.st_gid != uid)
169 goto BAD;
170
171 /* must not be readable or writable by others */
172 if ((st.st_mode & (S_IROTH|S_IWOTH)) != 0)
173 goto BAD;
174
175 /* everything ok */
176 return 0;
177
178BAD:
179 errno = EINVAL;
180 return -1;
181}
182
183/* This function is used to check the data directory path for safety.
184 * We check that every sub-directory is owned by the 'system' user
185 * and exists and is not a symlink. We also check that the full directory
186 * path is properly owned by the user ID.
187 *
188 * Return 0 on success, -1 on error.
189 */
190int
191check_data_path(const char* dataPath, uid_t uid)
192{
193 int nn;
194
195 /* the path should be absolute */
196 if (dataPath[0] != '/') {
197 errno = EINVAL;
198 return -1;
199 }
200
201 /* look for all sub-paths, we do that by finding
202 * directory separators in the input path and
203 * checking each sub-path independently
204 */
205 for (nn = 1; dataPath[nn] != '\0'; nn++)
206 {
207 char subpath[PATH_MAX];
208
209 /* skip non-separator characters */
210 if (dataPath[nn] != '/')
211 continue;
212
213 /* handle trailing separator case */
214 if (dataPath[nn+1] == '\0') {
215 break;
216 }
217
218 /* found a separator, check that dataPath is not too long. */
219 if (nn >= (int)(sizeof subpath)) {
220 errno = EINVAL;
221 return -1;
222 }
223
224 /* reject any '..' subpath */
225 if (nn >= 3 &&
226 dataPath[nn-3] == '/' &&
227 dataPath[nn-2] == '.' &&
228 dataPath[nn-1] == '.') {
229 errno = EINVAL;
230 return -1;
231 }
232
233 /* copy to 'subpath', then check ownership */
234 memcpy(subpath, dataPath, nn);
235 subpath[nn] = '\0';
236
237 if (check_directory_ownership(subpath, AID_SYSTEM) < 0)
238 return -1;
239 }
240
241 /* All sub-paths were checked, now verify that the full data
242 * directory is owned by the application uid
243 */
244 if (check_directory_ownership(dataPath, uid) < 0)
245 return -1;
246
247 /* all clear */
248 return 0;
249}
250
251/* Return TRUE iff a character is a space or tab */
252static inline int
253is_space(char c)
254{
255 return (c == ' ' || c == '\t');
256}
257
258/* Skip any space or tab character from 'p' until 'end' is reached.
259 * Return new position.
260 */
261static const char*
262skip_spaces(const char* p, const char* end)
263{
264 while (p < end && is_space(*p))
265 p++;
266
267 return p;
268}
269
270/* Skip any non-space and non-tab character from 'p' until 'end'.
271 * Return new position.
272 */
273static const char*
274skip_non_spaces(const char* p, const char* end)
275{
276 while (p < end && !is_space(*p))
277 p++;
278
279 return p;
280}
281
282/* Find the first occurence of 'ch' between 'p' and 'end'
283 * Return its position, or 'end' if none is found.
284 */
285static const char*
286find_first(const char* p, const char* end, char ch)
287{
288 while (p < end && *p != ch)
289 p++;
290
291 return p;
292}
293
294/* Check that the non-space string starting at 'p' and eventually
295 * ending at 'end' equals 'name'. Return new position (after name)
296 * on success, or NULL on failure.
297 *
298 * This function fails is 'name' is NULL, empty or contains any space.
299 */
300static const char*
301compare_name(const char* p, const char* end, const char* name)
302{
303 /* 'name' must not be NULL or empty */
304 if (name == NULL || name[0] == '\0' || p == end)
305 return NULL;
306
307 /* compare characters to those in 'name', excluding spaces */
308 while (*name) {
309 /* note, we don't check for *p == '\0' since
310 * it will be caught in the next conditional.
311 */
312 if (p >= end || is_space(*p))
313 goto BAD;
314
315 if (*p != *name)
316 goto BAD;
317
318 p++;
319 name++;
320 }
321
322 /* must be followed by end of line or space */
323 if (p < end && !is_space(*p))
324 goto BAD;
325
326 return p;
327
328BAD:
329 return NULL;
330}
331
332/* Parse one or more whitespace characters starting from '*pp'
333 * until 'end' is reached. Updates '*pp' on exit.
334 *
335 * Return 0 on success, -1 on failure.
336 */
337static int
338parse_spaces(const char** pp, const char* end)
339{
340 const char* p = *pp;
341
342 if (p >= end || !is_space(*p)) {
343 errno = EINVAL;
344 return -1;
345 }
346 p = skip_spaces(p, end);
347 *pp = p;
348 return 0;
349}
350
351/* Parse a positive decimal number starting from '*pp' until 'end'
352 * is reached. Adjust '*pp' on exit. Return decimal value or -1
353 * in case of error.
354 *
355 * If the value is larger than INT_MAX, -1 will be returned,
356 * and errno set to EOVERFLOW.
357 *
358 * If '*pp' does not start with a decimal digit, -1 is returned
359 * and errno set to EINVAL.
360 */
361static int
362parse_positive_decimal(const char** pp, const char* end)
363{
364 const char* p = *pp;
365 int value = 0;
366 int overflow = 0;
367
368 if (p >= end || *p < '0' || *p > '9') {
369 errno = EINVAL;
370 return -1;
371 }
372
373 while (p < end) {
374 int ch = *p;
375 unsigned d = (unsigned)(ch - '0');
376 int val2;
377
378 if (d >= 10U) /* d is unsigned, no lower bound check */
379 break;
380
381 val2 = value*10 + (int)d;
382 if (val2 < value)
383 overflow = 1;
384 value = val2;
385 p++;
386 }
387 *pp = p;
388
389 if (overflow) {
390 errno = EOVERFLOW;
391 value = -1;
392 }
393 return value;
394
395BAD:
396 *pp = p;
397 return -1;
398}
399
400/* Read the system's package database and extract information about
401 * 'pkgname'. Return 0 in case of success, or -1 in case of error.
402 *
403 * If the package is unknown, return -1 and set errno to ENOENT
404 * If the package database is corrupted, return -1 and set errno to EINVAL
405 */
406int
407get_package_info(const char* pkgName, PackageInfo *info)
408{
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200409 char* buffer;
410 size_t buffer_len;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800411 const char* p;
412 const char* buffer_end;
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200413 int result = -1;
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800414
415 info->uid = 0;
416 info->isDebuggable = 0;
417 info->dataDir[0] = '\0';
Robert Craigfced3de2013-03-26 08:09:09 -0400418 info->seinfo[0] = '\0';
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800419
David 'Digit' Turner5792ce72011-08-27 18:48:45 +0200420 buffer = map_file(PACKAGES_LIST_FILE, &buffer_len);
421 if (buffer == NULL)
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800422 return -1;
423
424 p = buffer;
425 buffer_end = buffer + buffer_len;
426
427 /* expect the following format on each line of the control file:
428 *
Robert Craigfced3de2013-03-26 08:09:09 -0400429 * <pkgName> <uid> <debugFlag> <dataDir> <seinfo>
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800430 *
431 * where:
432 * <pkgName> is the package's name
433 * <uid> is the application-specific user Id (decimal)
434 * <debugFlag> is 1 if the package is debuggable, or 0 otherwise
435 * <dataDir> is the path to the package's data directory (e.g. /data/data/com.example.foo)
Robert Craigfced3de2013-03-26 08:09:09 -0400436 * <seinfo> is the seinfo label associated with the package
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800437 *
438 * The file is generated in com.android.server.PackageManagerService.Settings.writeLP()
439 */
440
441 while (p < buffer_end) {
442 /* find end of current line and start of next one */
443 const char* end = find_first(p, buffer_end, '\n');
444 const char* next = (end < buffer_end) ? end + 1 : buffer_end;
445 const char* q;
446 int uid, debugFlag;
447
448 /* first field is the package name */
449 p = compare_name(p, end, pkgName);
450 if (p == NULL)
451 goto NEXT_LINE;
452
453 /* skip spaces */
454 if (parse_spaces(&p, end) < 0)
455 goto BAD_FORMAT;
456
457 /* second field is the pid */
458 uid = parse_positive_decimal(&p, end);
459 if (uid < 0)
460 return -1;
461
462 info->uid = (uid_t) uid;
463
464 /* skip spaces */
465 if (parse_spaces(&p, end) < 0)
466 goto BAD_FORMAT;
467
468 /* third field is debug flag (0 or 1) */
469 debugFlag = parse_positive_decimal(&p, end);
470 switch (debugFlag) {
471 case 0:
472 info->isDebuggable = 0;
473 break;
474 case 1:
475 info->isDebuggable = 1;
476 break;
477 default:
478 goto BAD_FORMAT;
479 }
480
481 /* skip spaces */
482 if (parse_spaces(&p, end) < 0)
483 goto BAD_FORMAT;
484
485 /* fourth field is data directory path and must not contain
486 * spaces.
487 */
488 q = skip_non_spaces(p, end);
489 if (q == p)
490 goto BAD_FORMAT;
491
Robert Craigfced3de2013-03-26 08:09:09 -0400492 p = string_copy(info->dataDir, sizeof info->dataDir, p, q - p);
493
494 /* skip spaces */
495 if (parse_spaces(&p, end) < 0)
496 goto BAD_FORMAT;
497
498 /* fifth field is the seinfo string */
499 q = skip_non_spaces(p, end);
500 if (q == p)
501 goto BAD_FORMAT;
502
503 string_copy(info->seinfo, sizeof info->seinfo, p, q - p);
David 'Digit' Turner1f4d9522010-03-02 18:05:23 -0800504
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}