From abfe6060762db379a81dff0496c846d9102d758f Mon Sep 17 00:00:00 2001 From: Drew Fisher Date: Wed, 25 Mar 2020 13:17:28 -0700 Subject: [PATCH] fuchsia: remove use of replace_as_executable (second try) (flutter/engine#17313) On Fuchsia, we can now get executable VMOs from trusted backing filesystems. This allows us to remove the use of replace_as_executable in favor of opening files with `fdio_open_fd_at` with the `OPEN_RIGHT_EXECUTABLE` flag and getting VMOs by calling `fdio_get_vmo_exec`. By moving the responsibility for executability into the filesystem, we are able to remove `deprecated-ambient-replace-as-executable` from component manifests for non-JIT runners (the JIT runners still call replace_as_executable in Dart's allocator). It wasn't abundantly clear whether .cmx files for tests were used purely in AOT runtime environments or also saw JIT usage, so I left those as-is. For context: this is a second attempt at #16690, which was reverted because it broke the Dart JIT runner. The primary difference is that this time around, we correctly handle absolute vs relative paths, depending on whether library loading bottoms out in `fdio_open_fd` or `fdio_open_fd_at`. I've added additional assertions to help ensure any new usages use the correct shape of path. Testing: I verified locally that the flutter product runner works on Astro, and also successfully ran the Dart JIT example test (which was the thing blocking the google3 roll with the previous attempt at this patchset). Co-authored-by: Drew Fisher --- .../dart_runner/dart_component_controller.cc | 4 +- .../fuchsia/dart_runner/dart_runner.cc | 4 +- .../goodbye_dart/meta/goodbye_dart_aot.cmx | 3 -- .../meta/dart_aot_product_runner.cmx | 1 - .../dart_runner/meta/dart_aot_runner.cmx | 1 - .../fuchsia/dart_runner/service_isolate.cc | 12 ++--- .../platform/fuchsia/flutter/component.cc | 14 +----- .../meta/flutter_aot_product_runner.cmx | 1 - .../flutter/meta/flutter_aot_runner.cmx | 1 - .../shell/platform/fuchsia/flutter/runner.cc | 2 +- .../runtime/dart/utils/mapped_resource.cc | 48 ++++++++++--------- .../fuchsia/runtime/dart/utils/vmo.cc | 48 +++++++++++++++---- .../platform/fuchsia/runtime/dart/utils/vmo.h | 5 +- 13 files changed, 82 insertions(+), 62 deletions(-) diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc b/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc index 02f412353e..b558c9c171 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc @@ -201,12 +201,12 @@ bool DartComponentController::SetupFromKernel() { } if (!dart_utils::MappedResource::LoadFromNamespace( - nullptr, "pkg/data/isolate_core_snapshot_data.bin", + nullptr, "/pkg/data/isolate_core_snapshot_data.bin", isolate_snapshot_data_)) { return false; } if (!dart_utils::MappedResource::LoadFromNamespace( - nullptr, "pkg/data/isolate_core_snapshot_instructions.bin", + nullptr, "/pkg/data/isolate_core_snapshot_instructions.bin", isolate_snapshot_instructions_, true /* executable */)) { return false; } diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_runner.cc b/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_runner.cc index 74eb6b4240..7f72a824f1 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_runner.cc +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_runner.cc @@ -158,11 +158,11 @@ DartRunner::DartRunner() : context_(sys::ComponentContext::Create()) { params.vm_snapshot_instructions = ::_kDartVmSnapshotInstructions; #else if (!dart_utils::MappedResource::LoadFromNamespace( - nullptr, "pkg/data/vm_snapshot_data.bin", vm_snapshot_data_)) { + nullptr, "/pkg/data/vm_snapshot_data.bin", vm_snapshot_data_)) { FX_LOG(FATAL, LOG_TAG, "Failed to load vm snapshot data"); } if (!dart_utils::MappedResource::LoadFromNamespace( - nullptr, "pkg/data/vm_snapshot_instructions.bin", + nullptr, "/pkg/data/vm_snapshot_instructions.bin", vm_snapshot_instructions_, true /* executable */)) { FX_LOG(FATAL, LOG_TAG, "Failed to load vm snapshot instructions"); } diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/examples/goodbye_dart/meta/goodbye_dart_aot.cmx b/engine/src/flutter/shell/platform/fuchsia/dart_runner/examples/goodbye_dart/meta/goodbye_dart_aot.cmx index c6705a70ac..e041f24109 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/examples/goodbye_dart/meta/goodbye_dart_aot.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/examples/goodbye_dart/meta/goodbye_dart_aot.cmx @@ -3,9 +3,6 @@ "data": "data/goodbye_dart_aot" }, "sandbox": { - "features": [ - "deprecated-ambient-replace-as-executable" - ], "services": [ "fuchsia.intl.PropertyProvider", "fuchsia.sys.Environment" diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_product_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_product_runner.cmx index 629c352e01..26c6db097e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_product_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_product_runner.cmx @@ -5,7 +5,6 @@ "sandbox": { "features": [ "config-data", - "deprecated-ambient-replace-as-executable", "root-ssl-certificates" ], "services": [ diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_runner.cmx index 629c352e01..26c6db097e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/meta/dart_aot_runner.cmx @@ -5,7 +5,6 @@ "sandbox": { "features": [ "config-data", - "deprecated-ambient-replace-as-executable", "root-ssl-certificates" ], "services": [ diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/service_isolate.cc b/engine/src/flutter/shell/platform/fuchsia/dart_runner/service_isolate.cc index 6da8a3de47..4906a323bb 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/service_isolate.cc +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/service_isolate.cc @@ -82,7 +82,7 @@ Dart_Isolate CreateServiceIsolate(const char* uri, #if defined(AOT_RUNTIME) // The VM service was compiled as a separate app. - const char* snapshot_path = "pkg/data/vmservice_snapshot.so"; + const char* snapshot_path = "/pkg/data/vmservice_snapshot.so"; if (elf_snapshot.Load(nullptr, snapshot_path)) { vmservice_data = elf_snapshot.IsolateData(); vmservice_instructions = elf_snapshot.IsolateInstrs(); @@ -92,14 +92,14 @@ Dart_Isolate CreateServiceIsolate(const char* uri, } else { // The VM service was compiled as a separate app. const char* snapshot_data_path = - "pkg/data/vmservice_isolate_snapshot_data.bin"; + "/pkg/data/vmservice_isolate_snapshot_data.bin"; const char* snapshot_instructions_path = - "pkg/data/vmservice_isolate_snapshot_instructions.bin"; + "/pkg/data/vmservice_isolate_snapshot_instructions.bin"; #else // The VM service is embedded in the core snapshot. - const char* snapshot_data_path = "pkg/data/isolate_core_snapshot_data.bin"; + const char* snapshot_data_path = "/pkg/data/isolate_core_snapshot_data.bin"; const char* snapshot_instructions_path = - "pkg/data/isolate_core_snapshot_instructions.bin"; + "/pkg/data/isolate_core_snapshot_instructions.bin"; #endif if (!dart_utils::MappedResource::LoadFromNamespace( @@ -196,7 +196,7 @@ Dart_Isolate CreateServiceIsolate(const char* uri, Dart_Handle GetVMServiceAssetsArchiveCallback() { dart_utils::MappedResource observatory_tar; if (!dart_utils::MappedResource::LoadFromNamespace( - nullptr, "pkg/data/observatory.tar", observatory_tar)) { + nullptr, "/pkg/data/observatory.tar", observatory_tar)) { FX_LOG(ERROR, LOG_TAG, "Failed to load Observatory assets"); return nullptr; } diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc index 9f6bc6f915..e0d58f3e80 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc @@ -431,7 +431,8 @@ class FileInNamespaceBuffer final : public fml::Mapping { FileInNamespaceBuffer(int namespace_fd, const char* path, bool executable) : address_(nullptr), size_(0) { fuchsia::mem::Buffer buffer; - if (!dart_utils::VmoFromFilenameAt(namespace_fd, path, &buffer)) { + if (!dart_utils::VmoFromFilenameAt(namespace_fd, path, executable, + &buffer)) { return; } if (buffer.size == 0) { @@ -441,17 +442,6 @@ class FileInNamespaceBuffer final : public fml::Mapping { uint32_t flags = ZX_VM_PERM_READ; if (executable) { flags |= ZX_VM_PERM_EXECUTE; - - // VmoFromFilenameAt will return VMOs without ZX_RIGHT_EXECUTE, - // so we need replace_as_executable to be able to map them as - // ZX_VM_PERM_EXECUTE. - // TODO(mdempsky): Update comment once SEC-42 is fixed. - zx_status_t status = - buffer.vmo.replace_as_executable(zx::handle(), &buffer.vmo); - if (status != ZX_OK) { - FML_LOG(FATAL) << "Failed to make VMO executable: " - << zx_status_get_string(status); - } } uintptr_t addr; zx_status_t status = diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx index 019dc11340..914c22b6f2 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx @@ -5,7 +5,6 @@ "sandbox": { "features": [ "config-data", - "deprecated-ambient-replace-as-executable", "root-ssl-certificates", "vulkan" ], diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx index af0c724590..11ba40d787 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx @@ -5,7 +5,6 @@ "sandbox": { "features": [ "config-data", - "deprecated-ambient-replace-as-executable", "root-ssl-certificates", "vulkan" ], diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc index 2fe0b5626a..6459236730 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc @@ -87,7 +87,7 @@ bool InitializeICU() { const char* data_path = kIcuDataPath; fuchsia::mem::Buffer icu_data; - if (!dart_utils::VmoFromFilename(data_path, &icu_data)) { + if (!dart_utils::VmoFromFilename(data_path, false, &icu_data)) { return false; } diff --git a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/mapped_resource.cc b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/mapped_resource.cc index d8837e6fbc..0224cf25f7 100644 --- a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/mapped_resource.cc +++ b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/mapped_resource.cc @@ -32,41 +32,31 @@ static bool OpenVmo(fuchsia::mem::Buffer* resource_vmo, bool executable) { TRACE_DURATION("dart", "LoadFromNamespace", "path", path); - // openat of a path with a leading '/' ignores the namespace fd. - dart_utils::Check(path[0] != '/', LOG_TAG); - if (namespc == nullptr) { - if (!VmoFromFilename(path, resource_vmo)) { + // Opening a file in the root namespace expects an absolute path. + dart_utils::Check(path[0] == '/', LOG_TAG); + if (!VmoFromFilename(path, executable, resource_vmo)) { return false; } } else { + // openat of a path with a leading '/' ignores the namespace fd. + // require a relative path. + dart_utils::Check(path[0] != '/', LOG_TAG); + auto root_dir = fdio_ns_opendir(namespc); if (root_dir < 0) { FX_LOG(ERROR, LOG_TAG, "Failed to open namespace directory"); return false; } - bool result = dart_utils::VmoFromFilenameAt(root_dir, path, resource_vmo); + bool result = + dart_utils::VmoFromFilenameAt(root_dir, path, executable, resource_vmo); close(root_dir); if (!result) { return result; } } - if (executable) { - // VmoFromFilenameAt will return VMOs without ZX_RIGHT_EXECUTE, - // so we need replace_as_executable to be able to map them as - // ZX_VM_PERM_EXECUTE. - // TODO(mdempsky): Update comment once SEC-42 is fixed. - zx_status_t status = resource_vmo->vmo.replace_as_executable( - zx::handle(), &resource_vmo->vmo); - if (status != ZX_OK) { - FX_LOGF(ERROR, LOG_TAG, "Failed to make VMO executable: %s", - zx_status_get_string(status)); - return false; - } - } - return true; } @@ -114,10 +104,22 @@ MappedResource::~MappedResource() { static int OpenFdExec(const std::string& path, int dirfd) { int fd = -1; - zx_status_t result = fdio_open_fd_at( - dirfd, path.c_str(), - fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE, - &fd); + zx_status_t result; + if (dirfd == AT_FDCWD) { + // fdio_open_fd_at does not support AT_FDCWD, by design. Use fdio_open_fd + // and expect an absolute path for that usage pattern. + dart_utils::Check(path[0] == '/', LOG_TAG); + result = fdio_open_fd( + path.c_str(), + fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE, + &fd); + } else { + dart_utils::Check(path[0] != '/', LOG_TAG); + result = fdio_open_fd_at( + dirfd, path.c_str(), + fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE, + &fd); + } if (result != ZX_OK) { FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd_at(%s) failed: %s", path.c_str(), zx_status_get_string(result)); diff --git a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.cc b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.cc index 130181c777..87f181e38b 100644 --- a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.cc +++ b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.cc @@ -7,15 +7,18 @@ #include #include +#include #include +#include #include #include +#include #include "runtime/dart/utils/logging.h" namespace { -bool VmoFromFd(int fd, fuchsia::mem::Buffer* buffer) { +bool VmoFromFd(int fd, bool executable, fuchsia::mem::Buffer* buffer) { if (!buffer) { FX_LOG(FATAL, LOG_TAG, "Invalid buffer pointer"); } @@ -27,7 +30,14 @@ bool VmoFromFd(int fd, fuchsia::mem::Buffer* buffer) { } zx_handle_t result = ZX_HANDLE_INVALID; - if (fdio_get_vmo_copy(fd, &result) != ZX_OK) { + zx_status_t status; + if (executable) { + status = fdio_get_vmo_exec(fd, &result); + } else { + status = fdio_get_vmo_copy(fd, &result); + } + + if (status != ZX_OK) { return false; } @@ -42,20 +52,42 @@ bool VmoFromFd(int fd, fuchsia::mem::Buffer* buffer) { namespace dart_utils { bool VmoFromFilename(const std::string& filename, + bool executable, fuchsia::mem::Buffer* buffer) { - return VmoFromFilenameAt(AT_FDCWD, filename, buffer); + // Note: the implementation here cannot be shared with VmoFromFilenameAt + // because fdio_open_fd_at does not aim to provide POSIX compatibility, and + // thus does not handle AT_FDCWD as dirfd. + uint32_t flags = fuchsia::io::OPEN_RIGHT_READABLE | + (executable ? fuchsia::io::OPEN_RIGHT_EXECUTABLE : 0); + zx_status_t status; + int fd; + + status = fdio_open_fd(filename.c_str(), flags, &fd); + if (status != ZX_OK) { + FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd(\"%s\", %08x) failed: %s", + filename.c_str(), flags, zx_status_get_string(status)); + return false; + } + bool result = VmoFromFd(fd, executable, buffer); + close(fd); + return result; } bool VmoFromFilenameAt(int dirfd, const std::string& filename, + bool executable, fuchsia::mem::Buffer* buffer) { - int fd = openat(dirfd, filename.c_str(), O_RDONLY); - if (fd == -1) { - FX_LOGF(ERROR, LOG_TAG, "openat(\"%s\") failed: %s", filename.c_str(), - strerror(errno)); + uint32_t flags = fuchsia::io::OPEN_RIGHT_READABLE | + (executable ? fuchsia::io::OPEN_RIGHT_EXECUTABLE : 0); + zx_status_t status; + int fd; + status = fdio_open_fd_at(dirfd, filename.c_str(), flags, &fd); + if (status != ZX_OK) { + FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd_at(%d, \"%s\", %08x) failed: %s", + dirfd, filename.c_str(), flags, zx_status_get_string(status)); return false; } - bool result = VmoFromFd(fd, buffer); + bool result = VmoFromFd(fd, executable, buffer); close(fd); return result; } diff --git a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.h b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.h index bc4065eb81..52871d2e32 100644 --- a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.h +++ b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.h @@ -11,10 +11,13 @@ namespace dart_utils { -bool VmoFromFilename(const std::string& filename, fuchsia::mem::Buffer* buffer); +bool VmoFromFilename(const std::string& filename, + bool executable, + fuchsia::mem::Buffer* buffer); bool VmoFromFilenameAt(int dirfd, const std::string& filename, + bool executable, fuchsia::mem::Buffer* buffer); zx_status_t IsSizeValid(const fuchsia::mem::Buffer& buffer, bool* is_valid);