Jorge Lucangeli Obes | fbfb8e8 | 2017-02-14 10:33:41 -0500 | [diff] [blame^] | 1 | // Copyright 2015, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <fcntl.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/unique_fd.h> |
| 22 | |
| 23 | #include <libminijail.h> |
| 24 | #include <scoped_minijail.h> |
| 25 | |
| 26 | #include "minijail.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | int SetUpMinijail(const std::string& seccomp_policy_path) |
| 31 | { |
| 32 | // No seccomp policy defined for this architecture. |
| 33 | if (access(seccomp_policy_path.c_str(), R_OK) == -1) { |
| 34 | LOG(WARNING) << "No seccomp policy defined for this architecture."; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | int policy_fd = TEMP_FAILURE_RETRY(open(seccomp_policy_path.c_str(), O_RDONLY | O_CLOEXEC)); |
| 39 | if (policy_fd == -1) { |
| 40 | PLOG(FATAL) << "Failed to open seccomp policy file '" << seccomp_policy_path << "'"; |
| 41 | } |
| 42 | |
| 43 | ScopedMinijail jail{minijail_new()}; |
| 44 | if (!jail) { |
| 45 | LOG(WARNING) << "Failed to create minijail."; |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | minijail_no_new_privs(jail.get()); |
| 50 | minijail_log_seccomp_filter_failures(jail.get()); |
| 51 | minijail_use_seccomp_filter(jail.get()); |
| 52 | // This closes |policy_fd|. |
| 53 | minijail_parse_seccomp_filters_from_fd(jail.get(), policy_fd); |
| 54 | minijail_enter(jail.get()); |
| 55 | return 0; |
| 56 | } |
| 57 | } |