| 43 | } |
| 44 | |
| 45 | void fix_linux_binary_flags(const fs::path& bin_path) { |
| 46 | const auto bin_path_str = kb::path::to_str(bin_path); |
| 47 | |
| 48 | ELFIO::elfio reader; |
| 49 | if(!reader.load(bin_path_str)) { |
| 50 | LOG_ERROR("Failed to read Linux binary: {}", bin_path_str); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // Iterate over program headers to find PT_GNU_STACK |
| 55 | for(const auto& segment : reader.segments) { |
| 56 | if(segment->get_type() != ELFIO::PT_GNU_STACK) { continue; } |
| 57 | ELFIO::Elf_Xword flags = segment->get_flags(); |
| 58 | |
| 59 | if(flags & ELFIO::PF_X) { |
| 60 | flags &= ~ELFIO::PF_X; |
| 61 | segment->set_flags(flags); |
| 62 | |
| 63 | LOG_INFO("Cleared PF_X on PT_GNU_STACK in binary: {}", bin_path_str); |
| 64 | } |
| 65 | |
| 66 | break; // No need to iterate other segments |
| 67 | } |
| 68 | |
| 69 | if(!reader.save(bin_path_str)) { |
| 70 | LOG_ERROR("Failed to save Linux binary: {}", bin_path_str); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Some older libsteam_api.so binaries have PF_X (execute) flag from the PT_GNU_STACK enabled. |
no outgoing calls
no test coverage detected