11f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/*
21f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner**
31f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** Copyright 2010, The Android Open Source Project
41f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner**
51f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** Licensed under the Apache License, Version 2.0 (the "License");
61f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** you may not use this file except in compliance with the License.
71f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** You may obtain a copy of the License at
81f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner**
91f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner**     http://www.apache.org/licenses/LICENSE-2.0
101f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner**
111f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** Unless required by applicable law or agreed to in writing, software
121f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** distributed under the License is distributed on an "AS IS" BASIS,
131f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
141f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** See the License for the specific language governing permissions and
151f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner** limitations under the License.
161f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner*/
171f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner#include <errno.h>
181f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner#include <fcntl.h>
19a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov#include <stdio.h>
2068ffc74e32dba4799ac3249c6835ca1ef2fe5f04Mark Salyzyn#include <string.h>
215792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner#include <sys/mman.h>
2268ffc74e32dba4799ac3249c6835ca1ef2fe5f04Mark Salyzyn#include <sys/stat.h>
2368ffc74e32dba4799ac3249c6835ca1ef2fe5f04Mark Salyzyn#include <unistd.h>
2468ffc74e32dba4799ac3249c6835ca1ef2fe5f04Mark Salyzyn
251f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner#include <private/android_filesystem_config.h>
261f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner#include "package.h"
271f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
281f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/*
291f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *  WARNING WARNING WARNING WARNING
301f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
311f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *  The following code runs as root on production devices, before
321f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *  the run-as command has dropped the uid/gid. Hence be very
331f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *  conservative and keep in mind the following:
341f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
351f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *  - Performance does not matter here, clarity and safety of the code
361f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *    does however. Documentation is a must.
371f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
381f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *  - Avoid calling C library functions with complex implementations
391f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *    like malloc() and printf(). You want to depend on simple system
401f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *    calls instead, which behaviour is not going to be altered in
411f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *    unpredictible ways by environment variables or system properties.
421f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
431f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *  - Do not trust user input and/or the filesystem whenever possible.
441f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
451f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
461f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
471f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* The file containing the list of installed packages on the system */
481f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner#define PACKAGES_LIST_FILE  "/data/system/packages.list"
491f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
501f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Copy 'srclen' string bytes from 'src' into buffer 'dst' of size 'dstlen'
511f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * This function always zero-terminate the destination buffer unless
521f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * 'dstlen' is 0, even in case of overflow.
53fced3ded831cb084121b10a78c12de99c89004aaRobert Craig * Returns a pointer into the src string, leaving off where the copy
54fced3ded831cb084121b10a78c12de99c89004aaRobert Craig * has stopped. The copy will stop when dstlen, srclen or a null
55fced3ded831cb084121b10a78c12de99c89004aaRobert Craig * character on src has been reached.
561f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
57fced3ded831cb084121b10a78c12de99c89004aaRobert Craigstatic const char*
581f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstring_copy(char* dst, size_t dstlen, const char* src, size_t srclen)
591f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
601f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    const char* srcend = src + srclen;
611f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    const char* dstend = dst + dstlen;
621f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
631f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (dstlen == 0)
64fced3ded831cb084121b10a78c12de99c89004aaRobert Craig        return src;
651f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
661f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    dstend--; /* make room for terminating zero */
671f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
681f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    while (dst < dstend && src < srcend && *src != '\0')
691f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        *dst++ = *src++;
701f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
711f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    *dst = '\0'; /* zero-terminate result */
72fced3ded831cb084121b10a78c12de99c89004aaRobert Craig    return src;
731f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
741f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
755792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner/* Open 'filename' and map it into our address-space.
765792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner * Returns buffer address, or NULL on error
775792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner * On exit, *filesize will be set to the file's size, or 0 on error
781f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
795792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turnerstatic void*
805792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turnermap_file(const char* filename, size_t* filesize)
811f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
825792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    int  fd, ret, old_errno;
835792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    struct stat  st;
845792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    size_t  length = 0;
855792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    void*   address = NULL;
86080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    gid_t   oldegid;
871f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
885792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    *filesize = 0;
891f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
90080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    /*
91080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich     * Temporarily switch effective GID to allow us to read
92080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich     * the packages file
93080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich     */
94080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich
95080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    oldegid = getegid();
9618860c524915bc991a9015bdbab32e918f5298d7Alex Klyubin    if (setegid(AID_PACKAGE_INFO) < 0) {
97080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich        return NULL;
98080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    }
99080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich
1001f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* open the file for reading */
1015792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
102080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    if (fd < 0) {
1035792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        return NULL;
104080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    }
105080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich
106080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    /* restore back to our old egid */
107080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    if (setegid(oldegid) < 0) {
108080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich        goto EXIT;
109080427e4e2b1b72718b660e16b6cf38b3a3c4e3fNick Kralevich    }
1101f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1115792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    /* get its size */
1125792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
1135792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    if (ret < 0)
1145792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        goto EXIT;
1155792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner
1164ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich    /* Ensure that the file is owned by the system user */
117977a9f3b1a05e6168e8245a1e2061225b68b2b41Jeff Sharkey    if ((st.st_uid != AID_SYSTEM) || (st.st_gid != AID_PACKAGE_INFO)) {
1184ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich        goto EXIT;
1194ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich    }
1204ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich
1214ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich    /* Ensure that the file has sane permissions */
1224ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich    if ((st.st_mode & S_IWOTH) != 0) {
1234ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich        goto EXIT;
1244ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich    }
1254ae77160727f8b92d61028269d1f49ae16873a08Nick Kralevich
1265792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    /* Ensure that the size is not ridiculously large */
1275792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    length = (size_t)st.st_size;
1285792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    if ((off_t)length != st.st_size) {
1295792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        errno = ENOMEM;
1305792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        goto EXIT;
1315792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    }
1325792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner
1335792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    /* Memory-map the file now */
1342e6e2713fb71f7e18b782ac22a7b466545b79e89Mark Salyzyn    do {
1352e6e2713fb71f7e18b782ac22a7b466545b79e89Mark Salyzyn        address = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
1362e6e2713fb71f7e18b782ac22a7b466545b79e89Mark Salyzyn    } while (address == MAP_FAILED && errno == EINTR);
1375792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    if (address == MAP_FAILED) {
1385792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        address = NULL;
1395792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        goto EXIT;
1405792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    }
1411f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1425792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    /* We're good, return size */
1435792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    *filesize = length;
1445792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner
1455792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' TurnerEXIT:
1461f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* close the file, preserve old errno for better diagnostics */
1471f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    old_errno = errno;
1481f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    close(fd);
1491f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    errno = old_errno;
1501f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1515792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    return address;
1525792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner}
1535792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner
1545792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner/* unmap the file, but preserve errno */
1555792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turnerstatic void
1565792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turnerunmap_file(void*  address, size_t  size)
1575792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner{
1585792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    int old_errno = errno;
1595792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    TEMP_FAILURE_RETRY(munmap(address, size));
1605792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    errno = old_errno;
1611f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
1621f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1631f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Check that a given directory:
1641f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * - exists
1651f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * - is owned by a given uid/gid
1661f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * - is a real directory, not a symlink
1671f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * - isn't readable or writable by others
1681f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
1691f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * Return 0 on success, or -1 on error.
1701f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * errno is set to EINVAL in case of failed check.
1711f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
1721f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic int
1731f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnercheck_directory_ownership(const char* path, uid_t uid)
1741f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
1751f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    int ret;
1761f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    struct stat st;
1771f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1781f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    do {
1791f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        ret = lstat(path, &st);
1801f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    } while (ret < 0 && errno == EINTR);
1811f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1821f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (ret < 0)
1831f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        return -1;
1841f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1851f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* must be a real directory, not a symlink */
1861f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (!S_ISDIR(st.st_mode))
1871f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        goto BAD;
1881f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1891f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* must be owned by specific uid/gid */
1901f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (st.st_uid != uid || st.st_gid != uid)
1911f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        goto BAD;
1921f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1931f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* must not be readable or writable by others */
1941f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if ((st.st_mode & (S_IROTH|S_IWOTH)) != 0)
1951f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        goto BAD;
1961f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
1971f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* everything ok */
1981f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return 0;
1991f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2001f4d95296acf34a93128332441782a80c10845b4David 'Digit' TurnerBAD:
2011f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    errno = EINVAL;
2021f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return -1;
2031f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
2041f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2051f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* This function is used to check the data directory path for safety.
2061f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * We check that every sub-directory is owned by the 'system' user
2071f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * and exists and is not a symlink. We also check that the full directory
2081f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * path is properly owned by the user ID.
2091f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
2101f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * Return 0 on success, -1 on error.
2111f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
2121f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerint
2131f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnercheck_data_path(const char* dataPath, uid_t  uid)
2141f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
2151f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    int  nn;
2161f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2171f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* the path should be absolute */
2181f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (dataPath[0] != '/') {
2191f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        errno = EINVAL;
2201f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        return -1;
2211f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
2221f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2231f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* look for all sub-paths, we do that by finding
2241f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     * directory separators in the input path and
2251f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     * checking each sub-path independently
2261f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     */
2271f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    for (nn = 1; dataPath[nn] != '\0'; nn++)
2281f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    {
2291f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        char subpath[PATH_MAX];
2301f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2311f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* skip non-separator characters */
2321f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (dataPath[nn] != '/')
2331f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            continue;
2341f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2351f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* handle trailing separator case */
2361f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (dataPath[nn+1] == '\0') {
2371f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            break;
2381f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        }
2391f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2401f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* found a separator, check that dataPath is not too long. */
2411f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (nn >= (int)(sizeof subpath)) {
2421f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            errno = EINVAL;
2431f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            return -1;
2441f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        }
2451f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2461f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* reject any '..' subpath */
2471f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (nn >= 3               &&
2481f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            dataPath[nn-3] == '/' &&
2491f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            dataPath[nn-2] == '.' &&
2501f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            dataPath[nn-1] == '.') {
2511f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            errno = EINVAL;
2521f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            return -1;
2531f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        }
2541f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2551f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* copy to 'subpath', then check ownership */
2561f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        memcpy(subpath, dataPath, nn);
2571f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        subpath[nn] = '\0';
2581f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2591f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (check_directory_ownership(subpath, AID_SYSTEM) < 0)
2601f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            return -1;
2611f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
2621f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2631f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* All sub-paths were checked, now verify that the full data
2641f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     * directory is owned by the application uid
2651f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     */
2661f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (check_directory_ownership(dataPath, uid) < 0)
2671f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        return -1;
2681f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2691f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* all clear */
2701f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return 0;
2711f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
2721f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2731f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Return TRUE iff a character is a space or tab */
2741f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic inline int
2751f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turneris_space(char c)
2761f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
2771f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return (c == ' ' || c == '\t');
2781f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
2791f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2801f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Skip any space or tab character from 'p' until 'end' is reached.
2811f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * Return new position.
2821f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
2831f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic const char*
2841f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerskip_spaces(const char*  p, const char*  end)
2851f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
2861f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    while (p < end && is_space(*p))
2871f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        p++;
2881f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2891f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return p;
2901f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
2911f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
2921f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Skip any non-space and non-tab character from 'p' until 'end'.
2931f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * Return new position.
2941f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
2951f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic const char*
2961f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerskip_non_spaces(const char* p, const char* end)
2971f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
2981f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    while (p < end && !is_space(*p))
2991f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        p++;
3001f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3011f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return p;
3021f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
3031f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3041f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Find the first occurence of 'ch' between 'p' and 'end'
3051f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * Return its position, or 'end' if none is found.
3061f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
3071f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic const char*
3081f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerfind_first(const char* p, const char* end, char ch)
3091f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
3101f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    while (p < end && *p != ch)
3111f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        p++;
3121f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3131f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return p;
3141f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
3151f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3161f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Check that the non-space string starting at 'p' and eventually
3171f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * ending at 'end' equals 'name'. Return new position (after name)
3181f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * on success, or NULL on failure.
3191f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
3201f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * This function fails is 'name' is NULL, empty or contains any space.
3211f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
3221f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic const char*
3231f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnercompare_name(const char* p, const char* end, const char* name)
3241f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
3251f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* 'name' must not be NULL or empty */
3261f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (name == NULL || name[0] == '\0' || p == end)
3271f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        return NULL;
3281f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3291f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* compare characters to those in 'name', excluding spaces */
3301f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    while (*name) {
3311f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* note, we don't check for *p == '\0' since
3321f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner         * it will be caught in the next conditional.
3331f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner         */
3341f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (p >= end || is_space(*p))
3351f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto BAD;
3361f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3371f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (*p != *name)
3381f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto BAD;
3391f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3401f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        p++;
3411f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        name++;
3421f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
3431f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3441f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* must be followed by end of line or space */
3451f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (p < end && !is_space(*p))
3461f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        goto BAD;
3471f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3481f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return p;
3491f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3501f4d95296acf34a93128332441782a80c10845b4David 'Digit' TurnerBAD:
3511f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return NULL;
3521f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
3531f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3541f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Parse one or more whitespace characters starting from '*pp'
3551f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * until 'end' is reached. Updates '*pp' on exit.
3561f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
3571f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * Return 0 on success, -1 on failure.
3581f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
3591f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic int
3601f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerparse_spaces(const char** pp, const char* end)
3611f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
3621f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    const char* p = *pp;
3631f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3641f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (p >= end || !is_space(*p)) {
3651f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        errno = EINVAL;
3661f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        return -1;
3671f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
3681f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    p   = skip_spaces(p, end);
3691f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    *pp = p;
3701f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return 0;
3711f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
3721f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3731f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Parse a positive decimal number starting from '*pp' until 'end'
3741f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * is reached. Adjust '*pp' on exit. Return decimal value or -1
3751f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * in case of error.
3761f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
3771f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * If the value is larger than INT_MAX, -1 will be returned,
3781f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * and errno set to EOVERFLOW.
3791f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
3801f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * If '*pp' does not start with a decimal digit, -1 is returned
3811f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * and errno set to EINVAL.
3821f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
3831f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerstatic int
3841f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerparse_positive_decimal(const char** pp, const char* end)
3851f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
3861f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    const char* p = *pp;
3871f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    int value = 0;
3881f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    int overflow = 0;
3891f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3901f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (p >= end || *p < '0' || *p > '9') {
3911f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        errno = EINVAL;
3921f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        return -1;
3931f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
3941f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
3951f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    while (p < end) {
3961f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        int      ch = *p;
3971f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        unsigned d  = (unsigned)(ch - '0');
3981f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        int      val2;
3991f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4001f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (d >= 10U) /* d is unsigned, no lower bound check */
4011f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            break;
4021f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4031f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        val2 = value*10 + (int)d;
4041f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (val2 < value)
4051f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            overflow = 1;
4061f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        value = val2;
4071f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        p++;
4081f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
4091f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    *pp = p;
4101f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4111f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    if (overflow) {
4121f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        errno = EOVERFLOW;
4131f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        value = -1;
4141f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
4151f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    return value;
4161f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
4171f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4181f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner/* Read the system's package database and extract information about
4191f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * 'pkgname'. Return 0 in case of success, or -1 in case of error.
4201f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner *
4211f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * If the package is unknown, return -1 and set errno to ENOENT
4221f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner * If the package database is corrupted, return -1 and set errno to EINVAL
4231f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner */
4241f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turnerint
425a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalovget_package_info(const char* pkgName, uid_t userId, PackageInfo *info)
4261f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner{
4275792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    char*        buffer;
4285792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    size_t       buffer_len;
4291f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    const char*  p;
4301f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    const char*  buffer_end;
4315792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    int          result = -1;
4321f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4331f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    info->uid          = 0;
4341f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    info->isDebuggable = 0;
4351f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    info->dataDir[0]   = '\0';
436fced3ded831cb084121b10a78c12de99c89004aaRobert Craig    info->seinfo[0]    = '\0';
4371f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4385792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    buffer = map_file(PACKAGES_LIST_FILE, &buffer_len);
4395792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    if (buffer == NULL)
4401f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        return -1;
4411f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4421f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    p          = buffer;
4431f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    buffer_end = buffer + buffer_len;
4441f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4451f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* expect the following format on each line of the control file:
4461f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     *
447fced3ded831cb084121b10a78c12de99c89004aaRobert Craig     *  <pkgName> <uid> <debugFlag> <dataDir> <seinfo>
4481f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     *
4491f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     * where:
4501f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     *  <pkgName>    is the package's name
4511f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     *  <uid>        is the application-specific user Id (decimal)
4521f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     *  <debugFlag>  is 1 if the package is debuggable, or 0 otherwise
4531f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     *  <dataDir>    is the path to the package's data directory (e.g. /data/data/com.example.foo)
454fced3ded831cb084121b10a78c12de99c89004aaRobert Craig     *  <seinfo>     is the seinfo label associated with the package
4551f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     *
4561f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     * The file is generated in com.android.server.PackageManagerService.Settings.writeLP()
4571f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner     */
4581f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4591f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    while (p < buffer_end) {
4601f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* find end of current line and start of next one */
4611f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        const char*  end  = find_first(p, buffer_end, '\n');
4621f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        const char*  next = (end < buffer_end) ? end + 1 : buffer_end;
4631f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        const char*  q;
4641f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        int          uid, debugFlag;
4651f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4661f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* first field is the package name */
4671f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        p = compare_name(p, end, pkgName);
4681f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (p == NULL)
4691f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto NEXT_LINE;
4701f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4711f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* skip spaces */
4721f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (parse_spaces(&p, end) < 0)
4731f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto BAD_FORMAT;
4741f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4751f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* second field is the pid */
4761f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        uid = parse_positive_decimal(&p, end);
4771f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (uid < 0)
4781f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            return -1;
4791f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4801f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        info->uid = (uid_t) uid;
4811f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4821f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* skip spaces */
4831f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (parse_spaces(&p, end) < 0)
4841f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto BAD_FORMAT;
4851f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4861f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* third field is debug flag (0 or 1) */
4871f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        debugFlag = parse_positive_decimal(&p, end);
4881f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        switch (debugFlag) {
4891f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        case 0:
4901f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            info->isDebuggable = 0;
4911f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            break;
4921f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        case 1:
4931f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            info->isDebuggable = 1;
4941f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            break;
4951f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        default:
4961f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto BAD_FORMAT;
4971f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        }
4981f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
4991f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* skip spaces */
5001f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (parse_spaces(&p, end) < 0)
5011f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto BAD_FORMAT;
5021f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
5031f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* fourth field is data directory path and must not contain
5041f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner         * spaces.
5051f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner         */
5061f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        q = skip_non_spaces(p, end);
5071f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        if (q == p)
5081f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner            goto BAD_FORMAT;
5091f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
510a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov        /* If userId == 0 (i.e. user is device owner) we can use dataDir value
511a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov         * from packages.list, otherwise compose data directory as
512a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov         * /data/user/$uid/$packageId
513a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov         */
514a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov        if (userId == 0) {
515a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov            p = string_copy(info->dataDir, sizeof info->dataDir, p, q - p);
516a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov        } else {
517a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov            snprintf(info->dataDir,
518a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov                     sizeof info->dataDir,
519a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov                     "/data/user/%d/%s",
520a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov                     userId,
521a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov                     pkgName);
522a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov            p = q;
523a08d313bb87279d2a203cded92669638e3458f5fOleksiy Vyalov        }
524fced3ded831cb084121b10a78c12de99c89004aaRobert Craig
525fced3ded831cb084121b10a78c12de99c89004aaRobert Craig        /* skip spaces */
526fced3ded831cb084121b10a78c12de99c89004aaRobert Craig        if (parse_spaces(&p, end) < 0)
527fced3ded831cb084121b10a78c12de99c89004aaRobert Craig            goto BAD_FORMAT;
528fced3ded831cb084121b10a78c12de99c89004aaRobert Craig
529fced3ded831cb084121b10a78c12de99c89004aaRobert Craig        /* fifth field is the seinfo string */
530fced3ded831cb084121b10a78c12de99c89004aaRobert Craig        q = skip_non_spaces(p, end);
531fced3ded831cb084121b10a78c12de99c89004aaRobert Craig        if (q == p)
532fced3ded831cb084121b10a78c12de99c89004aaRobert Craig            goto BAD_FORMAT;
533fced3ded831cb084121b10a78c12de99c89004aaRobert Craig
534fced3ded831cb084121b10a78c12de99c89004aaRobert Craig        string_copy(info->seinfo, sizeof info->seinfo, p, q - p);
5351f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
5361f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        /* Ignore the rest */
5375792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        result = 0;
5385792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner        goto EXIT;
5391f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
5401f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    NEXT_LINE:
5411f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner        p = next;
5421f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    }
5431f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
5441f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    /* the package is unknown */
5451f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    errno = ENOENT;
5465792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    result = -1;
5475792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    goto EXIT;
5481f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner
5491f4d95296acf34a93128332441782a80c10845b4David 'Digit' TurnerBAD_FORMAT:
5501f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner    errno = EINVAL;
5515792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    result = -1;
5525792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner
5535792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' TurnerEXIT:
5545792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    unmap_file(buffer, buffer_len);
5555792ce79cc79cd0eef9fadd6351521b128b4e85cDavid 'Digit' Turner    return result;
5561f4d95296acf34a93128332441782a80c10845b4David 'Digit' Turner}
557