Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 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 |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 20 | #include <optional> |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 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 | |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 32 | static std::optional<std::string> ReadFile(int fd) { |
| 33 | std::string str; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 34 | char buf[1024]; |
| 35 | ssize_t r; |
| 36 | while ((r = read(fd, buf, sizeof(buf))) > 0) { |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 37 | str.append(buf, r); |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 38 | } |
| 39 | if (r != 0) { |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 40 | return std::nullopt; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 41 | } |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 42 | return std::move(str); |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | namespace android { |
| 46 | namespace util { |
| 47 | |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 48 | ProcResult ExecuteBinary(const std::vector<std::string>& argv) { |
| 49 | int stdout[2]; // [0] read, [1] write |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 50 | if (pipe(stdout) != 0) { |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 51 | PLOG(ERROR) << "out pipe"; |
| 52 | return ProcResult{-1}; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 55 | int stderr[2]; // [0] read, [1] write |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 56 | if (pipe(stderr) != 0) { |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 57 | PLOG(ERROR) << "err pipe"; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 58 | close(stdout[0]); |
| 59 | close(stdout[1]); |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 60 | return ProcResult{-1}; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Ryan Mitchell | a8f1366 | 2019-06-26 15:39:52 -0700 | [diff] [blame] | 63 | auto gid = getgid(); |
| 64 | auto uid = getuid(); |
| 65 | |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 66 | // better keep no C++ objects going into the child here |
| 67 | auto argv0 = (char const**)malloc(sizeof(char*) * (argv.size() + 1)); |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 68 | for (size_t i = 0; i < argv.size(); i++) { |
| 69 | argv0[i] = argv[i].c_str(); |
| 70 | } |
| 71 | argv0[argv.size()] = nullptr; |
Peter Collingbourne | b3982fc | 2021-04-15 21:42:37 -0700 | [diff] [blame] | 72 | int pid = fork(); |
| 73 | switch (pid) { |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 74 | case -1: // error |
| 75 | free(argv0); |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 76 | close(stdout[0]); |
| 77 | close(stdout[1]); |
| 78 | close(stderr[0]); |
| 79 | close(stderr[1]); |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 80 | PLOG(ERROR) << "fork"; |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 81 | return ProcResult{-1}; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 82 | case 0: // child |
Ryan Mitchell | a8f1366 | 2019-06-26 15:39:52 -0700 | [diff] [blame] | 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 | |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 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; |
Peter Collingbourne | b3982fc | 2021-04-15 21:42:37 -0700 | [diff] [blame] | 109 | waitpid(pid, &status, 0); |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 110 | if (!WIFEXITED(status)) { |
Peter Collingbourne | b3982fc | 2021-04-15 21:42:37 -0700 | [diff] [blame] | 111 | close(stdout[0]); |
| 112 | close(stderr[0]); |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 113 | return ProcResult{-1}; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 114 | } |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 115 | ProcResult result(status); |
| 116 | auto out = ReadFile(stdout[0]); |
| 117 | result.stdout_str = out ? std::move(*out) : ""; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 118 | close(stdout[0]); |
Yurii Zubrytskyi | 1fc44eb | 2022-09-28 22:52:37 -0700 | [diff] [blame] | 119 | auto err = ReadFile(stderr[0]); |
| 120 | result.stderr_str = err ? std::move(*err) : ""; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 121 | close(stderr[0]); |
Yi Kong | 95af4ca | 2024-08-12 07:03:29 +0800 | [diff] [blame] | 122 | return result; |
Mårten Kongstad | 2503a49 | 2018-09-27 13:32:30 +0200 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | } // namespace util |
| 127 | } // namespace android |
| 128 | #endif |