• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <optional>
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 
ReadFile(int fd)32 static std::optional<std::string> ReadFile(int fd) {
33   std::string str;
34   char buf[1024];
35   ssize_t r;
36   while ((r = read(fd, buf, sizeof(buf))) > 0) {
37     str.append(buf, r);
38   }
39   if (r != 0) {
40     return std::nullopt;
41   }
42   return std::move(str);
43 }
44 
45 namespace android {
46 namespace util {
47 
ExecuteBinary(const std::vector<std::string> & argv)48 ProcResult ExecuteBinary(const std::vector<std::string>& argv) {
49   int stdout[2];  // [0] read, [1] write
50   if (pipe(stdout) != 0) {
51     PLOG(ERROR) << "out pipe";
52     return ProcResult{-1};
53   }
54 
55   int stderr[2];  // [0] read, [1] write
56   if (pipe(stderr) != 0) {
57     PLOG(ERROR) << "err pipe";
58     close(stdout[0]);
59     close(stdout[1]);
60     return ProcResult{-1};
61   }
62 
63   auto gid = getgid();
64   auto uid = getuid();
65 
66   // better keep no C++ objects going into the child here
67   auto argv0 = (char const**)malloc(sizeof(char*) * (argv.size() + 1));
68   for (size_t i = 0; i < argv.size(); i++) {
69     argv0[i] = argv[i].c_str();
70   }
71   argv0[argv.size()] = nullptr;
72   int pid = fork();
73   switch (pid) {
74     case -1: // error
75       free(argv0);
76       close(stdout[0]);
77       close(stdout[1]);
78       close(stderr[0]);
79       close(stderr[1]);
80       PLOG(ERROR) << "fork";
81       return ProcResult{-1};
82     case 0: // child
83       if (setgid(gid) != 0) {
84         PLOG(ERROR) << "setgid";
85         exit(1);
86       }
87 
88       if (setuid(uid) != 0) {
89         PLOG(ERROR) << "setuid";
90         exit(1);
91       }
92 
93       close(stdout[0]);
94       if (dup2(stdout[1], STDOUT_FILENO) == -1) {
95         abort();
96       }
97       close(stderr[0]);
98       if (dup2(stderr[1], STDERR_FILENO) == -1) {
99         abort();
100       }
101       execvp(argv0[0], const_cast<char* const*>(argv0));
102       PLOG(ERROR) << "execv";
103       abort();
104     default: // parent
105       free(argv0);
106       close(stdout[1]);
107       close(stderr[1]);
108       int status;
109       waitpid(pid, &status, 0);
110       if (!WIFEXITED(status)) {
111           close(stdout[0]);
112           close(stderr[0]);
113           return ProcResult{-1};
114       }
115       ProcResult result(status);
116       auto out = ReadFile(stdout[0]);
117       result.stdout_str = out ? std::move(*out) : "";
118       close(stdout[0]);
119       auto err = ReadFile(stderr[0]);
120       result.stderr_str = err ? std::move(*err) : "";
121       close(stderr[0]);
122       return std::move(result);
123   }
124 }
125 
126 } // namespace util
127 } // namespace android
128 #endif
129