aboutsummaryrefslogtreecommitdiff
path: root/host/commands/process_sandboxer/main.cpp
blob: 3c5af47d5a5bf70fe2ad893145a38e43c0e87ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>

#include <memory>
#include <string>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/log/check.h"
#include "absl/log/initialize.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#include "sandboxed_api/sandbox2/executor.h"
#include "sandboxed_api/sandbox2/sandbox2.h"
#pragma clang diagnostic pop
#include "sandboxed_api/util/path.h"

#include "host/commands/process_sandboxer/policies.h"

inline constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE";

ABSL_FLAG(std::string, host_artifacts_path, "", "Host exes and libs");
ABSL_FLAG(std::string, log_dir, "", "Where to write log files");
ABSL_FLAG(std::vector<std::string>, inherited_fds, std::vector<std::string>(),
          "File descriptors to keep in the sandbox");

using sapi::file::CleanPath;
using sapi::file::JoinPath;

namespace cuttlefish {

int ProcessSandboxerMain(int argc, char** argv) {
  absl::InitializeLog();
  auto args = absl::ParseCommandLine(argc, argv);

  HostInfo host;
  host.artifacts_path = CleanPath(absl::GetFlag(FLAGS_host_artifacts_path));
  host.cuttlefish_config_path = CleanPath(getenv(kCuttlefishConfigEnvVarName));
  host.log_dir = CleanPath(absl::GetFlag(FLAGS_log_dir));
  setenv("LD_LIBRARY_PATH", JoinPath(host.artifacts_path, "lib64").c_str(), 1);

  CHECK_GE(args.size(), 1);
  auto exe = CleanPath(args[1]);
  std::vector<std::string> exe_argv(++args.begin(), args.end());
  auto executor = std::make_unique<sandbox2::Executor>(exe, exe_argv);

  for (const auto& inherited_fd : absl::GetFlag(FLAGS_inherited_fds)) {
    int fd;
    CHECK(absl::SimpleAtoi(inherited_fd, &fd));
    executor->ipc()->MapFd(fd, fd);  // Will close `fd` in this process
  }

  sandbox2::Sandbox2 sb(std::move(executor), PolicyForExecutable(host, exe));

  auto res = sb.Run();
  CHECK_EQ(res.final_status(), sandbox2::Result::OK) << res.ToString();
  return 0;
}

}  // namespace cuttlefish

int main(int argc, char** argv) {
  return cuttlefish::ProcessSandboxerMain(argc, argv);
}