summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2020-09-03 21:06:03 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2020-09-03 21:06:03 +0000
commita2f248724c7a68dcf0601f05cd335334302931cc (patch)
treea803bd09a597a3e16d00786b3b1961fb86524527
parent480335dfd68d8fe84b79a1dd5861e239a3ef6cea (diff)
parent790ef0579311f5cbf0ab6258b2fb7f5e3747b1c9 (diff)
downloadcore-a2f248724c7a68dcf0601f05cd335334302931cc.tar.gz
Merge "ashmem: ensure ashmem fds are CLOEXEC."
-rw-r--r--libcutils/ashmem-dev.cpp10
-rw-r--r--libcutils/ashmem_test.cpp5
2 files changed, 12 insertions, 3 deletions
diff --git a/libcutils/ashmem-dev.cpp b/libcutils/ashmem-dev.cpp
index 233d400e0..6a27f9a20 100644
--- a/libcutils/ashmem-dev.cpp
+++ b/libcutils/ashmem-dev.cpp
@@ -159,9 +159,11 @@ static bool __has_memfd_support() {
return false;
}
- /* Check if kernel support exists, otherwise fall back to ashmem */
+ // Check if kernel support exists, otherwise fall back to ashmem.
+ // This code needs to build on old API levels, so we can't use the libc
+ // wrapper.
android::base::unique_fd fd(
- syscall(__NR_memfd_create, "test_android_memfd", MFD_ALLOW_SEALING));
+ syscall(__NR_memfd_create, "test_android_memfd", MFD_CLOEXEC | MFD_ALLOW_SEALING));
if (fd == -1) {
ALOGE("memfd_create failed: %s, no memfd support.\n", strerror(errno));
return false;
@@ -333,7 +335,9 @@ int ashmem_valid(int fd)
}
static int memfd_create_region(const char* name, size_t size) {
- android::base::unique_fd fd(syscall(__NR_memfd_create, name, MFD_ALLOW_SEALING));
+ // This code needs to build on old API levels, so we can't use the libc
+ // wrapper.
+ android::base::unique_fd fd(syscall(__NR_memfd_create, name, MFD_CLOEXEC | MFD_ALLOW_SEALING));
if (fd == -1) {
ALOGE("memfd_create(%s, %zd) failed: %s\n", name, size, strerror(errno));
diff --git a/libcutils/ashmem_test.cpp b/libcutils/ashmem_test.cpp
index b37d020fb..fb657f6e6 100644
--- a/libcutils/ashmem_test.cpp
+++ b/libcutils/ashmem_test.cpp
@@ -35,6 +35,11 @@ void TestCreateRegion(size_t size, unique_fd &fd, int prot) {
ASSERT_TRUE(ashmem_valid(fd));
ASSERT_EQ(size, static_cast<size_t>(ashmem_get_size_region(fd)));
ASSERT_EQ(0, ashmem_set_prot_region(fd, prot));
+
+ // We've been inconsistent historically about whether or not these file
+ // descriptors were CLOEXEC. Make sure we're consistent going forward.
+ // https://issuetracker.google.com/165667331
+ ASSERT_EQ(FD_CLOEXEC, (fcntl(fd, F_GETFD) & FD_CLOEXEC));
}
void TestMmap(const unique_fd& fd, size_t size, int prot, void** region, off_t off = 0) {