• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include <stdlib.h>
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "absl/log/check.h"
26 #include "absl/log/initialize.h"
27 #include "absl/strings/numbers.h"
28 #pragma clang diagnostic push
29 #pragma clang diagnostic ignored "-Wunused-parameter"
30 #include "sandboxed_api/sandbox2/executor.h"
31 #include "sandboxed_api/sandbox2/sandbox2.h"
32 #pragma clang diagnostic pop
33 #include "sandboxed_api/util/path.h"
34 
35 #include "host/commands/process_sandboxer/policies.h"
36 
37 inline constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE";
38 
39 ABSL_FLAG(std::string, host_artifacts_path, "", "Host exes and libs");
40 ABSL_FLAG(std::string, log_dir, "", "Where to write log files");
41 ABSL_FLAG(std::vector<std::string>, inherited_fds, std::vector<std::string>(),
42           "File descriptors to keep in the sandbox");
43 
44 using sapi::file::CleanPath;
45 using sapi::file::JoinPath;
46 
47 namespace cuttlefish {
48 
ProcessSandboxerMain(int argc,char ** argv)49 int ProcessSandboxerMain(int argc, char** argv) {
50   absl::InitializeLog();
51   auto args = absl::ParseCommandLine(argc, argv);
52 
53   HostInfo host;
54   host.artifacts_path = CleanPath(absl::GetFlag(FLAGS_host_artifacts_path));
55   host.cuttlefish_config_path = CleanPath(getenv(kCuttlefishConfigEnvVarName));
56   host.log_dir = CleanPath(absl::GetFlag(FLAGS_log_dir));
57   setenv("LD_LIBRARY_PATH", JoinPath(host.artifacts_path, "lib64").c_str(), 1);
58 
59   CHECK_GE(args.size(), 1);
60   auto exe = CleanPath(args[1]);
61   std::vector<std::string> exe_argv(++args.begin(), args.end());
62   auto executor = std::make_unique<sandbox2::Executor>(exe, exe_argv);
63 
64   for (const auto& inherited_fd : absl::GetFlag(FLAGS_inherited_fds)) {
65     int fd;
66     CHECK(absl::SimpleAtoi(inherited_fd, &fd));
67     executor->ipc()->MapFd(fd, fd);  // Will close `fd` in this process
68   }
69 
70   sandbox2::Sandbox2 sb(std::move(executor), PolicyForExecutable(host, exe));
71 
72   auto res = sb.Run();
73   CHECK_EQ(res.final_status(), sandbox2::Result::OK) << res.ToString();
74   return 0;
75 }
76 
77 }  // namespace cuttlefish
78 
main(int argc,char ** argv)79 int main(int argc, char** argv) {
80   return cuttlefish::ProcessSandboxerMain(argc, argv);
81 }
82