Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC. This depends on the mount options used for /dev/shm, which vary among different Linux distributions and possibly local configuration. It also depends on details of kernel--ChromeOS uses the noexec option for /dev/shm but its kernel allows mprotect with PROT_EXEC anyway.
| 161 | // depends on details of kernel--ChromeOS uses the noexec option for /dev/shm |
| 162 | // but its kernel allows mprotect with PROT_EXEC anyway. |
| 163 | bool DetermineDevShmExecutable() { |
| 164 | bool result = false; |
| 165 | FilePath path; |
| 166 | |
| 167 | ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path)); |
| 168 | if (fd.is_valid()) { |
| 169 | DeleteFile(path, false); |
| 170 | long sysconf_result = sysconf(_SC_PAGESIZE); |
| 171 | CHECK_GE(sysconf_result, 0); |
| 172 | size_t pagesize = static_cast<size_t>(sysconf_result); |
| 173 | CHECK_GE(sizeof(pagesize), sizeof(sysconf_result)); |
| 174 | void* mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0); |
| 175 | if (mapping != MAP_FAILED) { |
| 176 | if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0) |
| 177 | result = true; |
| 178 | munmap(mapping, pagesize); |
| 179 | } |
| 180 | } |
| 181 | return result; |
| 182 | } |
| 183 | #endif // defined(OS_LINUX) |
| 184 | |
| 185 | } // namespace |
no test coverage detected