|
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,83 @@ 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 |
| 258 | + // mount. |
| 259 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 260 | + |
| 261 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 262 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE( |
| 263 | + Mount("", dir.path(), kTmpfs, MS_NOEXEC, "", 0)); |
| 264 | + auto file = ASSERT_NO_ERRNO_AND_VALUE( |
| 265 | + TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 266 | + |
| 267 | + FileDescriptor fd = |
| 268 | + ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 269 | + ASSERT_THAT(reinterpret_cast<uintptr_t>( |
| 270 | + mmap(0, kPageSize, PROT_EXEC, MAP_PRIVATE, fd.get(), 0)), |
| 271 | + SyscallFailsWithErrno(EPERM)); |
| 272 | +} |
| 273 | + |
| 274 | +TEST(MountTest, MMapWithExecProtSucceedsOnExecutableVolumeFile) { |
| 275 | + // Capability is needed to create tmpfs. |
| 276 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 277 | + |
| 278 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 279 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE(Mount("", dir.path(), kTmpfs, 0, "", 0)); |
| 280 | + auto file = ASSERT_NO_ERRNO_AND_VALUE( |
| 281 | + TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 282 | + |
| 283 | + FileDescriptor fd = |
| 284 | + ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 285 | + |
| 286 | + void* address = mmap(0, kPageSize, PROT_EXEC, MAP_PRIVATE, fd.get(), 0); |
| 287 | + EXPECT_NE(address, MAP_FAILED); |
| 288 | + |
| 289 | + MunmapSafe(address, kPageSize); |
| 290 | +} |
| 291 | + |
| 292 | +TEST(MountTest, MMapWithoutNoExecProtSucceedsOnNoExecFile) { |
| 293 | + // Capability is needed to create tmpfs. |
| 294 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 295 | + |
| 296 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 297 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE( |
| 298 | + Mount("", dir.path(), kTmpfs, MS_NOEXEC, "", 0)); |
| 299 | + auto file = ASSERT_NO_ERRNO_AND_VALUE( |
| 300 | + TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 301 | + FileDescriptor fd = |
| 302 | + ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 303 | + |
| 304 | + void* address = |
| 305 | + mmap(0, kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd.get(), 0); |
| 306 | + EXPECT_NE(address, MAP_FAILED); |
| 307 | + |
| 308 | + MunmapSafe(address, kPageSize); |
| 309 | +} |
| 310 | + |
| 311 | +TEST(MountTest, MProtectWithNoExecProtFailsOnNoExecFile) { |
| 312 | + // Capability is needed to create tmpfs. |
| 313 | + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); |
| 314 | + |
| 315 | + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); |
| 316 | + auto ret = ASSERT_NO_ERRNO_AND_VALUE( |
| 317 | + Mount("", dir.path(), kTmpfs, MS_NOEXEC, "", 0)); |
| 318 | + auto file = ASSERT_NO_ERRNO_AND_VALUE( |
| 319 | + TempPath::CreateFileWith(dir.path(), "random1", 0777)); |
| 320 | + FileDescriptor fd = |
| 321 | + ASSERT_NO_ERRNO_AND_VALUE(Open(file.path().c_str(), O_RDWR)); |
| 322 | + |
| 323 | + void* address = |
| 324 | + mmap(0, kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd.get(), 0); |
| 325 | + EXPECT_NE(address, MAP_FAILED); |
| 326 | + |
| 327 | + ASSERT_THAT(mprotect(address, kPageSize, PROT_EXEC), |
| 328 | + SyscallFailsWithErrno(EACCES)); |
| 329 | + |
| 330 | + MunmapSafe(address, kPageSize); |
| 331 | +} |
| 332 | + |
255 | 333 | TEST(MountTest, UmountMountsStackedOnDot) {
|
256 | 334 | SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN)));
|
257 | 335 | // Verify that unmounting at "." properly unmounts the mount at the top of
|
|
0 commit comments