|
62 | 62 | #include "test/util/fs_util.h"
|
63 | 63 | #include "test/util/linux_capability_util.h"
|
64 | 64 | #include "test/util/logging.h"
|
| 65 | +#include "test/util/memory_util.h" |
65 | 66 | #include "test/util/mount_util.h"
|
66 | 67 | #include "test/util/multiprocess_util.h"
|
67 | 68 | #include "test/util/posix_error.h"
|
@@ -252,6 +253,68 @@ TEST(MountTest, UmountDetach) {
|
252 | 253 | OpenAt(mounted_dir.get(), "..", O_DIRECTORY | O_RDONLY));
|
253 | 254 | }
|
254 | 255 |
|
| 256 | +TEST(MountTest, MMapWithExecProtFailsOnNoExecFile) { |
| 257 | + // Skips the test if test does not have needed capability to create the volume mount. |
| 258 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 259 | + |
| 260 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 261 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE(Mount("", dir.path(), kTmpfs, MS_NOEXEC, "", 0)); |
| 262 | + auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 263 | + |
| 264 | + FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 265 | + ASSERT_THAT( |
| 266 | + reinterpret_cast<uintptr_t>(mmap(0, kPageSize, PROT_EXEC, MAP_PRIVATE, fd.get(), 0)), |
| 267 | + SyscallFailsWithErrno(EPERM)); |
| 268 | +} |
| 269 | + |
| 270 | +TEST(MountTest, MMapWithExecProtSucceedsOnExecutableVolumeFile) { |
| 271 | + // Capability is needed to create tmpfs. |
| 272 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 273 | + |
| 274 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 275 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE(Mount("", dir.path(), kTmpfs, 0, "", 0)); |
| 276 | + auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 277 | + |
| 278 | + FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 279 | + |
| 280 | + void* address = mmap(0, kPageSize, PROT_EXEC, MAP_PRIVATE, fd.get(), 0); |
| 281 | + EXPECT_NE(address, MAP_FAILED); |
| 282 | + |
| 283 | + MunmapSafe(address, kPageSize); |
| 284 | +} |
| 285 | + |
| 286 | +TEST(MountTest, MMapWithoutNoExecProtSucceedsOnNoExecFile) { |
| 287 | + // Capability is needed to create tmpfs. |
| 288 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 289 | + |
| 290 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 291 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE(Mount("", dir.path(), kTmpfs, MS_NOEXEC, "", 0)); |
| 292 | + auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 293 | + FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 294 | + |
| 295 | + void* address = mmap(0, kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd.get(), 0); |
| 296 | + EXPECT_NE(address, MAP_FAILED); |
| 297 | + |
| 298 | + MunmapSafe(address, kPageSize); |
| 299 | +} |
| 300 | + |
| 301 | +TEST(MountTest, MProtectWithNoExecProtFailsOnNoExecFile) { |
| 302 | + // Capability is needed to create tmpfs. |
| 303 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 304 | + |
| 305 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 306 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE(Mount("", dir.path(), kTmpfs, MS_NOEXEC, "", 0)); |
| 307 | + auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 308 | + FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 309 | + |
| 310 | + void* address = mmap(0, kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd.get(), 0); |
| 311 | + EXPECT_NE(address, MAP_FAILED); |
| 312 | + |
| 313 | + ASSERT_THAT(mprotect(address, kPageSize, PROT_EXEC), SyscallFailsWithErrno(EACCES)); |
| 314 | + |
| 315 | + MunmapSafe(address, kPageSize); |
| 316 | +} |
| 317 | + |
255 | 318 | TEST(MountTest, UmountMountsStackedOnDot) {
|
256 | 319 | SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN)));
|
257 | 320 | // Verify that unmounting at "." properly unmounts the mount at the top of
|
|
0 commit comments