1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifdef _WIN32
18 // nothing to see here
19 #else
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 
28 #include "android-base/logging.h"
29 
30 #include "androidfw/PosixUtils.h"
31 
32 namespace {
33 
ReadFile(int fd)34 std::unique_ptr<std::string> ReadFile(int fd) {
35   std::unique_ptr<std::string> str(new std::string());
36   char buf[1024];
37   ssize_t r;
38   while ((r = read(fd, buf, sizeof(buf))) > 0) {
39     str->append(buf, r);
40   }
41   if (r != 0) {
42     return nullptr;
43   }
44   return str;
45 }
46 
47 }
48 
49 namespace android {
50 namespace util {
51 
ExecuteBinary(const std::vector<std::string> & argv)52 std::unique_ptr<ProcResult> ExecuteBinary(const std::vector<std::string>& argv) {
53   int stdout[2];  // stdout[0] read, stdout[1] write
54   if (pipe(stdout) != 0) {
55     PLOG(ERROR) << "pipe";
56     return nullptr;
57   }
58 
59   int stderr[2];  // stdout[0] read, stdout[1] write
60   if (pipe(stderr) != 0) {
61     PLOG(ERROR) << "pipe";
62     close(stdout[0]);
63     close(stdout[1]);
64     return nullptr;
65   }
66 
67   auto gid = getgid();
68   auto uid = getuid();
69 
70   char const** argv0 = (char const**)malloc(sizeof(char*) * (argv.size() + 1));
71   for (size_t i = 0; i < argv.size(); i++) {
72     argv0[i] = argv[i].c_str();
73   }
74   argv0[argv.size()] = nullptr;
75   switch (fork()) {
76     case -1: // error
77       free(argv0);
78       PLOG(ERROR) << "fork";
79       return nullptr;
80     case 0: // child
81       if (setgid(gid) != 0) {
82         PLOG(ERROR) << "setgid";
83         exit(1);
84       }
85 
86       if (setuid(uid) != 0) {
87         PLOG(ERROR) << "setuid";
88         exit(1);
89       }
90 
91       close(stdout[0]);
92       if (dup2(stdout[1], STDOUT_FILENO) == -1) {
93         abort();
94       }
95       close(stderr[0]);
96       if (dup2(stderr[1], STDERR_FILENO) == -1) {
97         abort();
98       }
99       execvp(argv0[0], const_cast<char* const*>(argv0));
100       PLOG(ERROR) << "execv";
101       abort();
102     default: // parent
103       free(argv0);
104       close(stdout[1]);
105       close(stderr[1]);
106       int status;
107       wait(&status);
108       if (!WIFEXITED(status)) {
109           return nullptr;
110       }
111       std::unique_ptr<ProcResult> result(new ProcResult());
112       result->status = status;
113       const auto out = ReadFile(stdout[0]);
114       result->stdout = out ? *out : "";
115       close(stdout[0]);
116       const auto err = ReadFile(stderr[0]);
117       result->stderr = err ? *err : "";
118       close(stderr[0]);
119       return result;
120   }
121 }
122 
123 } // namespace util
124 } // namespace android
125 #endif
126