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