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 "host/commands/process_sandboxer/policies.h"
18
19 #include <memory>
20
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/log/log.h"
23 #include "sandboxed_api/sandbox2/policybuilder.h"
24 #include "sandboxed_api/util/path.h"
25
26 using sapi::file::JoinPath;
27
28 namespace cuttlefish {
29
PolicyForExecutable(const HostInfo & host,std::string_view executable)30 std::unique_ptr<sandbox2::Policy> PolicyForExecutable(
31 const HostInfo& host, std::string_view executable) {
32 using Builder = sandbox2::PolicyBuilder(const HostInfo&);
33 absl::flat_hash_map<std::string, Builder*> builders;
34
35 builders[JoinPath(host.artifacts_path, "bin", "kernel_log_monitor")] =
36 KernelLogMonitorPolicy;
37 builders[JoinPath(host.artifacts_path, "bin", "logcat_receiver")] =
38 LogcatReceiverPolicy;
39
40 if (auto it = builders.find(executable); it != builders.end()) {
41 return (it->second)(host).BuildOrDie();
42 } else {
43 for (const auto& [target, unused] : builders) {
44 LOG(ERROR) << "Available policy: '" << target << "'";
45 }
46 LOG(FATAL) << "No policy defined for '" << executable << "'";
47 return sandbox2::PolicyBuilder().BuildOrDie();
48 }
49 }
50
51 } // namespace cuttlefish
52